cros_ec_keyb.c 20 KB

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