cros_ec_keyb.c 20 KB

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