cros_ec_keyb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * ChromeOS EC keyboard driver
  3. *
  4. * Copyright (C) 2012 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * This driver uses the Chrome OS EC byte-level message-based protocol for
  16. * communicating the keyboard state (which keys are pressed) from a keyboard EC
  17. * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
  18. * but everything else (including deghosting) is done here. The main
  19. * motivation for this is to keep the EC firmware as simple as possible, since
  20. * it cannot be easily upgraded and EC flash/IRAM space is relatively
  21. * expensive.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/bitops.h>
  25. #include <linux/i2c.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <linux/notifier.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/sysrq.h>
  33. #include <linux/input/matrix_keypad.h>
  34. #include <linux/mfd/cros_ec.h>
  35. #include <linux/mfd/cros_ec_commands.h>
  36. #include <asm/unaligned.h>
  37. /*
  38. * @rows: Number of rows in the keypad
  39. * @cols: Number of columns in the keypad
  40. * @row_shift: log2 or number of rows, rounded up
  41. * @keymap_data: Matrix keymap data used to convert to keyscan values
  42. * @ghost_filter: true to enable the matrix key-ghosting filter
  43. * @valid_keys: bitmap of existing keys for each matrix column
  44. * @old_kb_state: bitmap of keys pressed last scan
  45. * @dev: Device pointer
  46. * @ec: Top level ChromeOS device to use to talk to EC
  47. * @idev: The input device for the matrix keys.
  48. * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
  49. * @notifier: interrupt event notifier for transport devices
  50. */
  51. struct cros_ec_keyb {
  52. unsigned int rows;
  53. unsigned int cols;
  54. int row_shift;
  55. const struct matrix_keymap_data *keymap_data;
  56. bool ghost_filter;
  57. uint8_t *valid_keys;
  58. uint8_t *old_kb_state;
  59. struct device *dev;
  60. struct cros_ec_device *ec;
  61. struct input_dev *idev;
  62. struct input_dev *bs_idev;
  63. struct notifier_block notifier;
  64. };
  65. /**
  66. * cros_ec_bs_map - Struct mapping Linux keycodes to EC button/switch bitmap
  67. * #defines
  68. *
  69. * @ev_type: The type of the input event to generate (e.g., EV_KEY).
  70. * @code: A linux keycode
  71. * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
  72. * @inverted: If the #define and EV_SW have opposite meanings, this is true.
  73. * Only applicable to switches.
  74. */
  75. struct cros_ec_bs_map {
  76. unsigned int ev_type;
  77. unsigned int code;
  78. u8 bit;
  79. bool inverted;
  80. };
  81. /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
  82. static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
  83. /* Buttons */
  84. {
  85. .ev_type = EV_KEY,
  86. .code = KEY_POWER,
  87. .bit = EC_MKBP_POWER_BUTTON,
  88. },
  89. {
  90. .ev_type = EV_KEY,
  91. .code = KEY_VOLUMEUP,
  92. .bit = EC_MKBP_VOL_UP,
  93. },
  94. {
  95. .ev_type = EV_KEY,
  96. .code = KEY_VOLUMEDOWN,
  97. .bit = EC_MKBP_VOL_DOWN,
  98. },
  99. /* Switches */
  100. {
  101. .ev_type = EV_SW,
  102. .code = SW_LID,
  103. .bit = EC_MKBP_LID_OPEN,
  104. .inverted = true,
  105. },
  106. {
  107. .ev_type = EV_SW,
  108. .code = SW_TABLET_MODE,
  109. .bit = EC_MKBP_TABLET_MODE,
  110. },
  111. };
  112. /*
  113. * Returns true when there is at least one combination of pressed keys that
  114. * results in ghosting.
  115. */
  116. static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
  117. {
  118. int col1, col2, buf1, buf2;
  119. struct device *dev = ckdev->dev;
  120. uint8_t *valid_keys = ckdev->valid_keys;
  121. /*
  122. * Ghosting happens if for any pressed key X there are other keys
  123. * pressed both in the same row and column of X as, for instance,
  124. * in the following diagram:
  125. *
  126. * . . Y . g .
  127. * . . . . . .
  128. * . . . . . .
  129. * . . X . Z .
  130. *
  131. * In this case only X, Y, and Z are pressed, but g appears to be
  132. * pressed too (see Wikipedia).
  133. */
  134. for (col1 = 0; col1 < ckdev->cols; col1++) {
  135. buf1 = buf[col1] & valid_keys[col1];
  136. for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
  137. buf2 = buf[col2] & valid_keys[col2];
  138. if (hweight8(buf1 & buf2) > 1) {
  139. dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
  140. col1, buf1, col2, buf2);
  141. return true;
  142. }
  143. }
  144. }
  145. return false;
  146. }
  147. /*
  148. * Compares the new keyboard state to the old one and produces key
  149. * press/release events accordingly. The keyboard state is 13 bytes (one byte
  150. * per column)
  151. */
  152. static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
  153. uint8_t *kb_state, int len)
  154. {
  155. struct input_dev *idev = ckdev->idev;
  156. int col, row;
  157. int new_state;
  158. int old_state;
  159. int num_cols;
  160. num_cols = len;
  161. if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
  162. /*
  163. * Simple-minded solution: ignore this state. The obvious
  164. * improvement is to only ignore changes to keys involved in
  165. * the ghosting, but process the other changes.
  166. */
  167. dev_dbg(ckdev->dev, "ghosting found\n");
  168. return;
  169. }
  170. for (col = 0; col < ckdev->cols; col++) {
  171. for (row = 0; row < ckdev->rows; row++) {
  172. int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
  173. const unsigned short *keycodes = idev->keycode;
  174. new_state = kb_state[col] & (1 << row);
  175. old_state = ckdev->old_kb_state[col] & (1 << row);
  176. if (new_state != old_state) {
  177. dev_dbg(ckdev->dev,
  178. "changed: [r%d c%d]: byte %02x\n",
  179. row, col, new_state);
  180. input_report_key(idev, keycodes[pos],
  181. new_state);
  182. }
  183. }
  184. ckdev->old_kb_state[col] = kb_state[col];
  185. }
  186. input_sync(ckdev->idev);
  187. }
  188. /**
  189. * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
  190. *
  191. * This takes a bitmap of buttons or switches from the EC and reports events,
  192. * syncing at the end.
  193. *
  194. * @ckdev: The keyboard device.
  195. * @ev_type: The input event type (e.g., EV_KEY).
  196. * @mask: A bitmap of buttons from the EC.
  197. */
  198. static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
  199. unsigned int ev_type, u32 mask)
  200. {
  201. struct input_dev *idev = ckdev->bs_idev;
  202. int i;
  203. for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
  204. const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
  205. if (map->ev_type != ev_type)
  206. continue;
  207. input_event(idev, ev_type, map->code,
  208. !!(mask & BIT(map->bit)) ^ map->inverted);
  209. }
  210. input_sync(idev);
  211. }
  212. static int cros_ec_keyb_work(struct notifier_block *nb,
  213. unsigned long queued_during_suspend, void *_notify)
  214. {
  215. struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
  216. notifier);
  217. u32 val;
  218. unsigned int ev_type;
  219. switch (ckdev->ec->event_data.event_type) {
  220. case EC_MKBP_EVENT_KEY_MATRIX:
  221. if (device_may_wakeup(ckdev->dev)) {
  222. pm_wakeup_event(ckdev->dev, 0);
  223. } else {
  224. /*
  225. * If keyboard is not wake enabled, discard key state
  226. * changes during suspend. Switches will be re-checked
  227. * in cros_ec_keyb_resume() to be sure nothing is lost.
  228. */
  229. if (queued_during_suspend)
  230. return NOTIFY_OK;
  231. }
  232. if (ckdev->ec->event_size != ckdev->cols) {
  233. dev_err(ckdev->dev,
  234. "Discarded incomplete key matrix event.\n");
  235. return NOTIFY_OK;
  236. }
  237. cros_ec_keyb_process(ckdev,
  238. ckdev->ec->event_data.data.key_matrix,
  239. ckdev->ec->event_size);
  240. break;
  241. case EC_MKBP_EVENT_SYSRQ:
  242. if (device_may_wakeup(ckdev->dev))
  243. pm_wakeup_event(ckdev->dev, 0);
  244. else if (queued_during_suspend)
  245. return NOTIFY_OK;
  246. val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
  247. dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
  248. handle_sysrq(val);
  249. break;
  250. case EC_MKBP_EVENT_BUTTON:
  251. case EC_MKBP_EVENT_SWITCH:
  252. if (device_may_wakeup(ckdev->dev))
  253. pm_wakeup_event(ckdev->dev, 0);
  254. else if (queued_during_suspend)
  255. return NOTIFY_OK;
  256. if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
  257. val = get_unaligned_le32(
  258. &ckdev->ec->event_data.data.buttons);
  259. ev_type = EV_KEY;
  260. } else {
  261. val = get_unaligned_le32(
  262. &ckdev->ec->event_data.data.switches);
  263. ev_type = EV_SW;
  264. }
  265. cros_ec_keyb_report_bs(ckdev, ev_type, val);
  266. break;
  267. default:
  268. return NOTIFY_DONE;
  269. }
  270. return NOTIFY_OK;
  271. }
  272. /*
  273. * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
  274. * ghosting logic to ignore NULL or virtual keys.
  275. */
  276. static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
  277. {
  278. int row, col;
  279. int row_shift = ckdev->row_shift;
  280. unsigned short *keymap = ckdev->idev->keycode;
  281. unsigned short code;
  282. BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
  283. for (col = 0; col < ckdev->cols; col++) {
  284. for (row = 0; row < ckdev->rows; row++) {
  285. code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
  286. if (code && (code != KEY_BATTERY))
  287. ckdev->valid_keys[col] |= 1 << row;
  288. }
  289. dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
  290. col, ckdev->valid_keys[col]);
  291. }
  292. }
  293. /**
  294. * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
  295. *
  296. * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
  297. * unmarshalling and different version nonsense into something simple.
  298. *
  299. * @ec_dev: The EC device
  300. * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
  301. * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
  302. * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
  303. * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
  304. * @result: Where we'll store the result; a union
  305. * @result_size: The size of the result. Expected to be the size of one of
  306. * the elements in the union.
  307. *
  308. * Returns 0 if no error or -error upon error.
  309. */
  310. static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
  311. enum ec_mkbp_info_type info_type,
  312. enum ec_mkbp_event event_type,
  313. union ec_response_get_next_data *result,
  314. size_t result_size)
  315. {
  316. struct ec_params_mkbp_info *params;
  317. struct cros_ec_command *msg;
  318. int ret;
  319. msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
  320. sizeof(*params)), GFP_KERNEL);
  321. if (!msg)
  322. return -ENOMEM;
  323. msg->command = EC_CMD_MKBP_INFO;
  324. msg->version = 1;
  325. msg->outsize = sizeof(*params);
  326. msg->insize = result_size;
  327. params = (struct ec_params_mkbp_info *)msg->data;
  328. params->info_type = info_type;
  329. params->event_type = event_type;
  330. ret = cros_ec_cmd_xfer(ec_dev, msg);
  331. if (ret < 0) {
  332. dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
  333. (int)info_type, (int)event_type, ret);
  334. } else if (msg->result == EC_RES_INVALID_VERSION) {
  335. /* With older ECs we just return 0 for everything */
  336. memset(result, 0, result_size);
  337. ret = 0;
  338. } else if (msg->result != EC_RES_SUCCESS) {
  339. dev_warn(ec_dev->dev, "Error getting info %d/%d: %d\n",
  340. (int)info_type, (int)event_type, msg->result);
  341. ret = -EPROTO;
  342. } else if (ret != result_size) {
  343. dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
  344. (int)info_type, (int)event_type,
  345. ret, result_size);
  346. ret = -EPROTO;
  347. } else {
  348. memcpy(result, msg->data, result_size);
  349. ret = 0;
  350. }
  351. kfree(msg);
  352. return ret;
  353. }
  354. /**
  355. * cros_ec_keyb_query_switches - Query the state of switches and report
  356. *
  357. * This will ask the EC about the current state of switches and report to the
  358. * kernel. Note that we don't query for buttons because they are more
  359. * transitory and we'll get an update on the next release / press.
  360. *
  361. * @ckdev: The keyboard device
  362. *
  363. * Returns 0 if no error or -error upon error.
  364. */
  365. static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
  366. {
  367. struct cros_ec_device *ec_dev = ckdev->ec;
  368. union ec_response_get_next_data event_data = {};
  369. int ret;
  370. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
  371. EC_MKBP_EVENT_SWITCH, &event_data,
  372. sizeof(event_data.switches));
  373. if (ret)
  374. return ret;
  375. cros_ec_keyb_report_bs(ckdev, EV_SW,
  376. get_unaligned_le32(&event_data.switches));
  377. return 0;
  378. }
  379. /**
  380. * cros_ec_keyb_resume - Resume the keyboard
  381. *
  382. * We use the resume notification as a chance to query the EC for switches.
  383. *
  384. * @dev: The keyboard device
  385. *
  386. * Returns 0 if no error or -error upon error.
  387. */
  388. static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
  389. {
  390. struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  391. if (ckdev->bs_idev)
  392. return cros_ec_keyb_query_switches(ckdev);
  393. return 0;
  394. }
  395. /**
  396. * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
  397. *
  398. * Handles all the bits of the keyboard driver related to non-matrix buttons
  399. * and switches, including asking the EC about which are present and telling
  400. * the kernel to expect them.
  401. *
  402. * If this device has no support for buttons and switches we'll return no error
  403. * but the ckdev->bs_idev will remain NULL when this function exits.
  404. *
  405. * @ckdev: The keyboard device
  406. *
  407. * Returns 0 if no error or -error upon error.
  408. */
  409. static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
  410. {
  411. struct cros_ec_device *ec_dev = ckdev->ec;
  412. struct device *dev = ckdev->dev;
  413. struct input_dev *idev;
  414. union ec_response_get_next_data event_data = {};
  415. const char *phys;
  416. u32 buttons;
  417. u32 switches;
  418. int ret;
  419. int i;
  420. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
  421. EC_MKBP_EVENT_BUTTON, &event_data,
  422. sizeof(event_data.buttons));
  423. if (ret)
  424. return ret;
  425. buttons = get_unaligned_le32(&event_data.buttons);
  426. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
  427. EC_MKBP_EVENT_SWITCH, &event_data,
  428. sizeof(event_data.switches));
  429. if (ret)
  430. return ret;
  431. switches = get_unaligned_le32(&event_data.switches);
  432. if (!buttons && !switches)
  433. return 0;
  434. /*
  435. * We call the non-matrix buttons/switches 'input1', if present.
  436. * Allocate phys before input dev, to ensure correct tear-down
  437. * ordering.
  438. */
  439. phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
  440. if (!phys)
  441. return -ENOMEM;
  442. idev = devm_input_allocate_device(dev);
  443. if (!idev)
  444. return -ENOMEM;
  445. idev->name = "cros_ec_buttons";
  446. idev->phys = phys;
  447. __set_bit(EV_REP, idev->evbit);
  448. idev->id.bustype = BUS_VIRTUAL;
  449. idev->id.version = 1;
  450. idev->id.product = 0;
  451. idev->dev.parent = dev;
  452. input_set_drvdata(idev, ckdev);
  453. ckdev->bs_idev = idev;
  454. for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
  455. const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
  456. if (buttons & BIT(map->bit))
  457. input_set_capability(idev, map->ev_type, map->code);
  458. }
  459. ret = cros_ec_keyb_query_switches(ckdev);
  460. if (ret) {
  461. dev_err(dev, "cannot query switches\n");
  462. return ret;
  463. }
  464. ret = input_register_device(ckdev->bs_idev);
  465. if (ret) {
  466. dev_err(dev, "cannot register input device\n");
  467. return ret;
  468. }
  469. return 0;
  470. }
  471. /**
  472. * cros_ec_keyb_register_bs - Register matrix keys
  473. *
  474. * Handles all the bits of the keyboard driver related to matrix keys.
  475. *
  476. * @ckdev: The keyboard device
  477. *
  478. * Returns 0 if no error or -error upon error.
  479. */
  480. static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
  481. {
  482. struct cros_ec_device *ec_dev = ckdev->ec;
  483. struct device *dev = ckdev->dev;
  484. struct input_dev *idev;
  485. const char *phys;
  486. int err;
  487. err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
  488. if (err)
  489. return err;
  490. ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  491. if (!ckdev->valid_keys)
  492. return -ENOMEM;
  493. ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  494. if (!ckdev->old_kb_state)
  495. return -ENOMEM;
  496. /*
  497. * We call the keyboard matrix 'input0'. Allocate phys before input
  498. * dev, to ensure correct tear-down ordering.
  499. */
  500. phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
  501. if (!phys)
  502. return -ENOMEM;
  503. idev = devm_input_allocate_device(dev);
  504. if (!idev)
  505. return -ENOMEM;
  506. idev->name = CROS_EC_DEV_NAME;
  507. idev->phys = phys;
  508. __set_bit(EV_REP, idev->evbit);
  509. idev->id.bustype = BUS_VIRTUAL;
  510. idev->id.version = 1;
  511. idev->id.product = 0;
  512. idev->dev.parent = dev;
  513. ckdev->ghost_filter = of_property_read_bool(dev->of_node,
  514. "google,needs-ghost-filter");
  515. err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
  516. NULL, idev);
  517. if (err) {
  518. dev_err(dev, "cannot build key matrix\n");
  519. return err;
  520. }
  521. ckdev->row_shift = get_count_order(ckdev->cols);
  522. input_set_capability(idev, EV_MSC, MSC_SCAN);
  523. input_set_drvdata(idev, ckdev);
  524. ckdev->idev = idev;
  525. cros_ec_keyb_compute_valid_keys(ckdev);
  526. err = input_register_device(ckdev->idev);
  527. if (err) {
  528. dev_err(dev, "cannot register input device\n");
  529. return err;
  530. }
  531. return 0;
  532. }
  533. static int cros_ec_keyb_probe(struct platform_device *pdev)
  534. {
  535. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  536. struct device *dev = &pdev->dev;
  537. struct cros_ec_keyb *ckdev;
  538. int err;
  539. if (!dev->of_node)
  540. return -ENODEV;
  541. ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
  542. if (!ckdev)
  543. return -ENOMEM;
  544. ckdev->ec = ec;
  545. ckdev->dev = dev;
  546. dev_set_drvdata(dev, ckdev);
  547. err = cros_ec_keyb_register_matrix(ckdev);
  548. if (err) {
  549. dev_err(dev, "cannot register matrix inputs: %d\n", err);
  550. return err;
  551. }
  552. err = cros_ec_keyb_register_bs(ckdev);
  553. if (err) {
  554. dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
  555. return err;
  556. }
  557. ckdev->notifier.notifier_call = cros_ec_keyb_work;
  558. err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
  559. &ckdev->notifier);
  560. if (err) {
  561. dev_err(dev, "cannot register notifier: %d\n", err);
  562. return err;
  563. }
  564. device_init_wakeup(ckdev->dev, true);
  565. return 0;
  566. }
  567. static int cros_ec_keyb_remove(struct platform_device *pdev)
  568. {
  569. struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
  570. blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
  571. &ckdev->notifier);
  572. return 0;
  573. }
  574. #ifdef CONFIG_OF
  575. static const struct of_device_id cros_ec_keyb_of_match[] = {
  576. { .compatible = "google,cros-ec-keyb" },
  577. {},
  578. };
  579. MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
  580. #endif
  581. static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
  582. static struct platform_driver cros_ec_keyb_driver = {
  583. .probe = cros_ec_keyb_probe,
  584. .remove = cros_ec_keyb_remove,
  585. .driver = {
  586. .name = "cros-ec-keyb",
  587. .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
  588. .pm = &cros_ec_keyb_pm_ops,
  589. },
  590. };
  591. module_platform_driver(cros_ec_keyb_driver);
  592. MODULE_LICENSE("GPL");
  593. MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
  594. MODULE_ALIAS("platform:cros-ec-keyb");