cros_ec_keyb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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->idev) {
  238. dev_warn_once(ckdev->dev,
  239. "Unexpected key matrix event\n");
  240. return NOTIFY_OK;
  241. }
  242. if (ckdev->ec->event_size != ckdev->cols) {
  243. dev_err(ckdev->dev,
  244. "Discarded key matrix event, unexpected length: %d != %d\n",
  245. ckdev->ec->event_size, ckdev->cols);
  246. return NOTIFY_OK;
  247. }
  248. cros_ec_keyb_process(ckdev,
  249. ckdev->ec->event_data.data.key_matrix,
  250. ckdev->ec->event_size);
  251. break;
  252. case EC_MKBP_EVENT_SYSRQ:
  253. pm_wakeup_event(ckdev->dev, 0);
  254. val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
  255. dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
  256. handle_sysrq(val);
  257. break;
  258. case EC_MKBP_EVENT_BUTTON:
  259. case EC_MKBP_EVENT_SWITCH:
  260. pm_wakeup_event(ckdev->dev, 0);
  261. if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
  262. val = get_unaligned_le32(
  263. &ckdev->ec->event_data.data.buttons);
  264. ev_type = EV_KEY;
  265. } else {
  266. val = get_unaligned_le32(
  267. &ckdev->ec->event_data.data.switches);
  268. ev_type = EV_SW;
  269. }
  270. cros_ec_keyb_report_bs(ckdev, ev_type, val);
  271. break;
  272. default:
  273. return NOTIFY_DONE;
  274. }
  275. return NOTIFY_OK;
  276. }
  277. /*
  278. * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
  279. * ghosting logic to ignore NULL or virtual keys.
  280. */
  281. static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
  282. {
  283. int row, col;
  284. int row_shift = ckdev->row_shift;
  285. unsigned short *keymap = ckdev->idev->keycode;
  286. unsigned short code;
  287. BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
  288. for (col = 0; col < ckdev->cols; col++) {
  289. for (row = 0; row < ckdev->rows; row++) {
  290. code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
  291. if (code && (code != KEY_BATTERY))
  292. ckdev->valid_keys[col] |= 1 << row;
  293. }
  294. dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
  295. col, ckdev->valid_keys[col]);
  296. }
  297. }
  298. /**
  299. * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
  300. *
  301. * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
  302. * unmarshalling and different version nonsense into something simple.
  303. *
  304. * @ec_dev: The EC device
  305. * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
  306. * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
  307. * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
  308. * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
  309. * @result: Where we'll store the result; a union
  310. * @result_size: The size of the result. Expected to be the size of one of
  311. * the elements in the union.
  312. *
  313. * Returns 0 if no error or -error upon error.
  314. */
  315. static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
  316. enum ec_mkbp_info_type info_type,
  317. enum ec_mkbp_event event_type,
  318. union ec_response_get_next_data *result,
  319. size_t result_size)
  320. {
  321. struct ec_params_mkbp_info *params;
  322. struct cros_ec_command *msg;
  323. int ret;
  324. msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
  325. sizeof(*params)), GFP_KERNEL);
  326. if (!msg)
  327. return -ENOMEM;
  328. msg->command = EC_CMD_MKBP_INFO;
  329. msg->version = 1;
  330. msg->outsize = sizeof(*params);
  331. msg->insize = result_size;
  332. params = (struct ec_params_mkbp_info *)msg->data;
  333. params->info_type = info_type;
  334. params->event_type = event_type;
  335. ret = cros_ec_cmd_xfer_status(ec_dev, msg);
  336. if (ret == -ENOPROTOOPT) {
  337. /* With older ECs we just return 0 for everything */
  338. memset(result, 0, result_size);
  339. ret = 0;
  340. } else if (ret < 0) {
  341. dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
  342. (int)info_type, (int)event_type, ret);
  343. } else if (ret != result_size) {
  344. dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
  345. (int)info_type, (int)event_type,
  346. ret, result_size);
  347. ret = -EPROTO;
  348. } else {
  349. memcpy(result, msg->data, result_size);
  350. ret = 0;
  351. }
  352. kfree(msg);
  353. return ret;
  354. }
  355. /**
  356. * cros_ec_keyb_query_switches - Query the state of switches and report
  357. *
  358. * This will ask the EC about the current state of switches and report to the
  359. * kernel. Note that we don't query for buttons because they are more
  360. * transitory and we'll get an update on the next release / press.
  361. *
  362. * @ckdev: The keyboard device
  363. *
  364. * Returns 0 if no error or -error upon error.
  365. */
  366. static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
  367. {
  368. struct cros_ec_device *ec_dev = ckdev->ec;
  369. union ec_response_get_next_data event_data = {};
  370. int ret;
  371. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
  372. EC_MKBP_EVENT_SWITCH, &event_data,
  373. sizeof(event_data.switches));
  374. if (ret)
  375. return ret;
  376. cros_ec_keyb_report_bs(ckdev, EV_SW,
  377. get_unaligned_le32(&event_data.switches));
  378. return 0;
  379. }
  380. /**
  381. * cros_ec_keyb_resume - Resume the keyboard
  382. *
  383. * We use the resume notification as a chance to query the EC for switches.
  384. *
  385. * @dev: The keyboard device
  386. *
  387. * Returns 0 if no error or -error upon error.
  388. */
  389. static int cros_ec_keyb_resume(struct device *dev)
  390. {
  391. struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  392. if (ckdev->bs_idev)
  393. return cros_ec_keyb_query_switches(ckdev);
  394. return 0;
  395. }
  396. /**
  397. * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
  398. *
  399. * Handles all the bits of the keyboard driver related to non-matrix buttons
  400. * and switches, including asking the EC about which are present and telling
  401. * the kernel to expect them.
  402. *
  403. * If this device has no support for buttons and switches we'll return no error
  404. * but the ckdev->bs_idev will remain NULL when this function exits.
  405. *
  406. * @ckdev: The keyboard device
  407. * @expect_buttons_switches: Indicates that EC must report button and/or
  408. * switch events
  409. *
  410. * Returns 0 if no error or -error upon error.
  411. */
  412. static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
  413. bool expect_buttons_switches)
  414. {
  415. struct cros_ec_device *ec_dev = ckdev->ec;
  416. struct device *dev = ckdev->dev;
  417. struct input_dev *idev;
  418. union ec_response_get_next_data event_data = {};
  419. const char *phys;
  420. u32 buttons;
  421. u32 switches;
  422. int ret;
  423. int i;
  424. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
  425. EC_MKBP_EVENT_BUTTON, &event_data,
  426. sizeof(event_data.buttons));
  427. if (ret)
  428. return ret;
  429. buttons = get_unaligned_le32(&event_data.buttons);
  430. ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
  431. EC_MKBP_EVENT_SWITCH, &event_data,
  432. sizeof(event_data.switches));
  433. if (ret)
  434. return ret;
  435. switches = get_unaligned_le32(&event_data.switches);
  436. if (!buttons && !switches)
  437. return expect_buttons_switches ? -EINVAL : 0;
  438. /*
  439. * We call the non-matrix buttons/switches 'input1', if present.
  440. * Allocate phys before input dev, to ensure correct tear-down
  441. * ordering.
  442. */
  443. phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
  444. if (!phys)
  445. return -ENOMEM;
  446. idev = devm_input_allocate_device(dev);
  447. if (!idev)
  448. return -ENOMEM;
  449. idev->name = "cros_ec_buttons";
  450. idev->phys = phys;
  451. __set_bit(EV_REP, idev->evbit);
  452. idev->id.bustype = BUS_VIRTUAL;
  453. idev->id.version = 1;
  454. idev->id.product = 0;
  455. idev->dev.parent = dev;
  456. input_set_drvdata(idev, ckdev);
  457. ckdev->bs_idev = idev;
  458. for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
  459. const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
  460. if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
  461. (map->ev_type == EV_SW && (switches & BIT(map->bit))))
  462. input_set_capability(idev, map->ev_type, map->code);
  463. }
  464. ret = cros_ec_keyb_query_switches(ckdev);
  465. if (ret) {
  466. dev_err(dev, "cannot query switches\n");
  467. return ret;
  468. }
  469. ret = input_register_device(ckdev->bs_idev);
  470. if (ret) {
  471. dev_err(dev, "cannot register input device\n");
  472. return ret;
  473. }
  474. return 0;
  475. }
  476. static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
  477. {
  478. u32 *physmap = ckdev->vdata.function_row_physmap;
  479. unsigned int row, col, scancode;
  480. int n_physmap;
  481. int error;
  482. int i;
  483. n_physmap = device_property_count_u32(ckdev->dev,
  484. "function-row-physmap");
  485. if (n_physmap <= 0)
  486. return;
  487. if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
  488. dev_warn(ckdev->dev,
  489. "only up to %d top row keys is supported (%d specified)\n",
  490. VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
  491. n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
  492. }
  493. error = device_property_read_u32_array(ckdev->dev,
  494. "function-row-physmap",
  495. physmap, n_physmap);
  496. if (error) {
  497. dev_warn(ckdev->dev,
  498. "failed to parse function-row-physmap property: %d\n",
  499. error);
  500. return;
  501. }
  502. /*
  503. * Convert (in place) from row/column encoding to matrix "scancode"
  504. * used by the driver.
  505. */
  506. for (i = 0; i < n_physmap; i++) {
  507. row = KEY_ROW(physmap[i]);
  508. col = KEY_COL(physmap[i]);
  509. scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
  510. physmap[i] = scancode;
  511. }
  512. ckdev->vdata.num_function_row_keys = n_physmap;
  513. }
  514. /**
  515. * cros_ec_keyb_register_matrix - Register matrix keys
  516. *
  517. * Handles all the bits of the keyboard driver related to matrix keys.
  518. *
  519. * @ckdev: The keyboard device
  520. *
  521. * Returns 0 if no error or -error upon error.
  522. */
  523. static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
  524. {
  525. struct cros_ec_device *ec_dev = ckdev->ec;
  526. struct device *dev = ckdev->dev;
  527. struct input_dev *idev;
  528. const char *phys;
  529. int err;
  530. err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
  531. if (err)
  532. return err;
  533. ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  534. if (!ckdev->valid_keys)
  535. return -ENOMEM;
  536. ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  537. if (!ckdev->old_kb_state)
  538. return -ENOMEM;
  539. /*
  540. * We call the keyboard matrix 'input0'. Allocate phys before input
  541. * dev, to ensure correct tear-down ordering.
  542. */
  543. phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
  544. if (!phys)
  545. return -ENOMEM;
  546. idev = devm_input_allocate_device(dev);
  547. if (!idev)
  548. return -ENOMEM;
  549. idev->name = CROS_EC_DEV_NAME;
  550. idev->phys = phys;
  551. __set_bit(EV_REP, idev->evbit);
  552. idev->id.bustype = BUS_VIRTUAL;
  553. idev->id.version = 1;
  554. idev->id.product = 0;
  555. idev->dev.parent = dev;
  556. ckdev->ghost_filter = device_property_read_bool(dev,
  557. "google,needs-ghost-filter");
  558. err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
  559. NULL, idev);
  560. if (err) {
  561. dev_err(dev, "cannot build key matrix\n");
  562. return err;
  563. }
  564. ckdev->row_shift = get_count_order(ckdev->cols);
  565. input_set_capability(idev, EV_MSC, MSC_SCAN);
  566. input_set_drvdata(idev, ckdev);
  567. ckdev->idev = idev;
  568. cros_ec_keyb_compute_valid_keys(ckdev);
  569. cros_ec_keyb_parse_vivaldi_physmap(ckdev);
  570. err = input_register_device(ckdev->idev);
  571. if (err) {
  572. dev_err(dev, "cannot register input device\n");
  573. return err;
  574. }
  575. return 0;
  576. }
  577. static ssize_t function_row_physmap_show(struct device *dev,
  578. struct device_attribute *attr,
  579. char *buf)
  580. {
  581. const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  582. const struct vivaldi_data *data = &ckdev->vdata;
  583. return vivaldi_function_row_physmap_show(data, buf);
  584. }
  585. static DEVICE_ATTR_RO(function_row_physmap);
  586. static struct attribute *cros_ec_keyb_attrs[] = {
  587. &dev_attr_function_row_physmap.attr,
  588. NULL,
  589. };
  590. static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
  591. struct attribute *attr,
  592. int n)
  593. {
  594. struct device *dev = kobj_to_dev(kobj);
  595. struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  596. if (attr == &dev_attr_function_row_physmap.attr &&
  597. !ckdev->vdata.num_function_row_keys)
  598. return 0;
  599. return attr->mode;
  600. }
  601. static const struct attribute_group cros_ec_keyb_group = {
  602. .is_visible = cros_ec_keyb_attr_is_visible,
  603. .attrs = cros_ec_keyb_attrs,
  604. };
  605. __ATTRIBUTE_GROUPS(cros_ec_keyb);
  606. static int cros_ec_keyb_probe(struct platform_device *pdev)
  607. {
  608. struct cros_ec_device *ec;
  609. struct device *dev = &pdev->dev;
  610. struct cros_ec_keyb *ckdev;
  611. bool buttons_switches_only = device_get_match_data(dev);
  612. int err;
  613. /*
  614. * If the parent ec device has not been probed yet, defer the probe of
  615. * this keyboard/button driver until later.
  616. */
  617. ec = dev_get_drvdata(pdev->dev.parent);
  618. if (!ec)
  619. return -EPROBE_DEFER;
  620. /*
  621. * Even if the cros_ec_device pointer is available, still need to check
  622. * if the device is fully registered before using it.
  623. */
  624. if (!cros_ec_device_registered(ec))
  625. return -EPROBE_DEFER;
  626. ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
  627. if (!ckdev)
  628. return -ENOMEM;
  629. ckdev->ec = ec;
  630. ckdev->dev = dev;
  631. dev_set_drvdata(dev, ckdev);
  632. if (!buttons_switches_only) {
  633. err = cros_ec_keyb_register_matrix(ckdev);
  634. if (err) {
  635. dev_err(dev, "cannot register matrix inputs: %d\n",
  636. err);
  637. return err;
  638. }
  639. }
  640. err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
  641. if (err) {
  642. dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
  643. return err;
  644. }
  645. ckdev->notifier.notifier_call = cros_ec_keyb_work;
  646. err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
  647. &ckdev->notifier);
  648. if (err) {
  649. dev_err(dev, "cannot register notifier: %d\n", err);
  650. return err;
  651. }
  652. device_init_wakeup(ckdev->dev, true);
  653. return 0;
  654. }
  655. static void cros_ec_keyb_remove(struct platform_device *pdev)
  656. {
  657. struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
  658. blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
  659. &ckdev->notifier);
  660. }
  661. #ifdef CONFIG_ACPI
  662. static const struct acpi_device_id cros_ec_keyb_acpi_match[] = {
  663. { "GOOG0007", true },
  664. { }
  665. };
  666. MODULE_DEVICE_TABLE(acpi, cros_ec_keyb_acpi_match);
  667. #endif
  668. #ifdef CONFIG_OF
  669. static const struct of_device_id cros_ec_keyb_of_match[] = {
  670. { .compatible = "google,cros-ec-keyb" },
  671. { .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
  672. {}
  673. };
  674. MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
  675. #endif
  676. static DEFINE_SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
  677. static struct platform_driver cros_ec_keyb_driver = {
  678. .probe = cros_ec_keyb_probe,
  679. .remove = cros_ec_keyb_remove,
  680. .driver = {
  681. .name = "cros-ec-keyb",
  682. .dev_groups = cros_ec_keyb_groups,
  683. .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
  684. .acpi_match_table = ACPI_PTR(cros_ec_keyb_acpi_match),
  685. .pm = pm_sleep_ptr(&cros_ec_keyb_pm_ops),
  686. },
  687. };
  688. module_platform_driver(cros_ec_keyb_driver);
  689. MODULE_LICENSE("GPL v2");
  690. MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
  691. MODULE_ALIAS("platform:cros-ec-keyb");