cros_ec_keyb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/i2c.h>
  25. #include <linux/input.h>
  26. #include <linux/kernel.h>
  27. #include <linux/notifier.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/input/matrix_keypad.h>
  31. #include <linux/mfd/cros_ec.h>
  32. #include <linux/mfd/cros_ec_commands.h>
  33. /*
  34. * @rows: Number of rows in the keypad
  35. * @cols: Number of columns in the keypad
  36. * @row_shift: log2 or number of rows, rounded up
  37. * @keymap_data: Matrix keymap data used to convert to keyscan values
  38. * @ghost_filter: true to enable the matrix key-ghosting filter
  39. * @old_kb_state: bitmap of keys pressed last scan
  40. * @dev: Device pointer
  41. * @idev: Input device
  42. * @ec: Top level ChromeOS device to use to talk to EC
  43. * @event_notifier: interrupt event notifier for transport devices
  44. */
  45. struct cros_ec_keyb {
  46. unsigned int rows;
  47. unsigned int cols;
  48. int row_shift;
  49. const struct matrix_keymap_data *keymap_data;
  50. bool ghost_filter;
  51. uint8_t *old_kb_state;
  52. struct device *dev;
  53. struct input_dev *idev;
  54. struct cros_ec_device *ec;
  55. struct notifier_block notifier;
  56. };
  57. static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb *ckdev,
  58. uint8_t *buf, int row)
  59. {
  60. int pressed_in_row = 0;
  61. int row_has_teeth = 0;
  62. int col, mask;
  63. mask = 1 << row;
  64. for (col = 0; col < ckdev->cols; col++) {
  65. if (buf[col] & mask) {
  66. pressed_in_row++;
  67. row_has_teeth |= buf[col] & ~mask;
  68. if (pressed_in_row > 1 && row_has_teeth) {
  69. /* ghosting */
  70. dev_dbg(ckdev->dev,
  71. "ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
  72. row, col, pressed_in_row,
  73. row_has_teeth);
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. /*
  81. * Returns true when there is at least one combination of pressed keys that
  82. * results in ghosting.
  83. */
  84. static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
  85. {
  86. int row;
  87. /*
  88. * Ghosting happens if for any pressed key X there are other keys
  89. * pressed both in the same row and column of X as, for instance,
  90. * in the following diagram:
  91. *
  92. * . . Y . g .
  93. * . . . . . .
  94. * . . . . . .
  95. * . . X . Z .
  96. *
  97. * In this case only X, Y, and Z are pressed, but g appears to be
  98. * pressed too (see Wikipedia).
  99. *
  100. * We can detect ghosting in a single pass (*) over the keyboard state
  101. * by maintaining two arrays. pressed_in_row counts how many pressed
  102. * keys we have found in a row. row_has_teeth is true if any of the
  103. * pressed keys for this row has other pressed keys in its column. If
  104. * at any point of the scan we find that a row has multiple pressed
  105. * keys, and at least one of them is at the intersection with a column
  106. * with multiple pressed keys, we're sure there is ghosting.
  107. * Conversely, if there is ghosting, we will detect such situation for
  108. * at least one key during the pass.
  109. *
  110. * (*) This looks linear in the number of keys, but it's not. We can
  111. * cheat because the number of rows is small.
  112. */
  113. for (row = 0; row < ckdev->rows; row++)
  114. if (cros_ec_keyb_row_has_ghosting(ckdev, buf, row))
  115. return true;
  116. return false;
  117. }
  118. /*
  119. * Compares the new keyboard state to the old one and produces key
  120. * press/release events accordingly. The keyboard state is 13 bytes (one byte
  121. * per column)
  122. */
  123. static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
  124. uint8_t *kb_state, int len)
  125. {
  126. struct input_dev *idev = ckdev->idev;
  127. int col, row;
  128. int new_state;
  129. int old_state;
  130. int num_cols;
  131. num_cols = len;
  132. if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
  133. /*
  134. * Simple-minded solution: ignore this state. The obvious
  135. * improvement is to only ignore changes to keys involved in
  136. * the ghosting, but process the other changes.
  137. */
  138. dev_dbg(ckdev->dev, "ghosting found\n");
  139. return;
  140. }
  141. for (col = 0; col < ckdev->cols; col++) {
  142. for (row = 0; row < ckdev->rows; row++) {
  143. int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
  144. const unsigned short *keycodes = idev->keycode;
  145. new_state = kb_state[col] & (1 << row);
  146. old_state = ckdev->old_kb_state[col] & (1 << row);
  147. if (new_state != old_state) {
  148. dev_dbg(ckdev->dev,
  149. "changed: [r%d c%d]: byte %02x\n",
  150. row, col, new_state);
  151. input_report_key(idev, keycodes[pos],
  152. new_state);
  153. }
  154. }
  155. ckdev->old_kb_state[col] = kb_state[col];
  156. }
  157. input_sync(ckdev->idev);
  158. }
  159. static int cros_ec_keyb_open(struct input_dev *dev)
  160. {
  161. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  162. return blocking_notifier_chain_register(&ckdev->ec->event_notifier,
  163. &ckdev->notifier);
  164. }
  165. static void cros_ec_keyb_close(struct input_dev *dev)
  166. {
  167. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  168. blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
  169. &ckdev->notifier);
  170. }
  171. static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
  172. {
  173. return ckdev->ec->command_recv(ckdev->ec, EC_CMD_MKBP_STATE,
  174. kb_state, ckdev->cols);
  175. }
  176. static int cros_ec_keyb_work(struct notifier_block *nb,
  177. unsigned long state, void *_notify)
  178. {
  179. int ret;
  180. struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
  181. notifier);
  182. uint8_t kb_state[ckdev->cols];
  183. ret = cros_ec_keyb_get_state(ckdev, kb_state);
  184. if (ret >= 0)
  185. cros_ec_keyb_process(ckdev, kb_state, ret);
  186. return NOTIFY_DONE;
  187. }
  188. static int cros_ec_keyb_probe(struct platform_device *pdev)
  189. {
  190. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  191. struct device *dev = ec->dev;
  192. struct cros_ec_keyb *ckdev;
  193. struct input_dev *idev;
  194. struct device_node *np;
  195. int err;
  196. np = pdev->dev.of_node;
  197. if (!np)
  198. return -ENODEV;
  199. ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
  200. if (!ckdev)
  201. return -ENOMEM;
  202. err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
  203. &ckdev->cols);
  204. if (err)
  205. return err;
  206. ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
  207. if (!ckdev->old_kb_state)
  208. return -ENOMEM;
  209. idev = devm_input_allocate_device(&pdev->dev);
  210. if (!idev)
  211. return -ENOMEM;
  212. ckdev->ec = ec;
  213. ckdev->notifier.notifier_call = cros_ec_keyb_work;
  214. ckdev->dev = dev;
  215. dev_set_drvdata(&pdev->dev, ckdev);
  216. idev->name = ec->ec_name;
  217. idev->phys = ec->phys_name;
  218. __set_bit(EV_REP, idev->evbit);
  219. idev->id.bustype = BUS_VIRTUAL;
  220. idev->id.version = 1;
  221. idev->id.product = 0;
  222. idev->dev.parent = &pdev->dev;
  223. idev->open = cros_ec_keyb_open;
  224. idev->close = cros_ec_keyb_close;
  225. ckdev->ghost_filter = of_property_read_bool(np,
  226. "google,needs-ghost-filter");
  227. err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
  228. NULL, idev);
  229. if (err) {
  230. dev_err(dev, "cannot build key matrix\n");
  231. return err;
  232. }
  233. ckdev->row_shift = get_count_order(ckdev->cols);
  234. input_set_capability(idev, EV_MSC, MSC_SCAN);
  235. input_set_drvdata(idev, ckdev);
  236. ckdev->idev = idev;
  237. err = input_register_device(ckdev->idev);
  238. if (err) {
  239. dev_err(dev, "cannot register input device\n");
  240. return err;
  241. }
  242. return 0;
  243. }
  244. #ifdef CONFIG_PM_SLEEP
  245. /* Clear any keys in the buffer */
  246. static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
  247. {
  248. uint8_t old_state[ckdev->cols];
  249. uint8_t new_state[ckdev->cols];
  250. unsigned long duration;
  251. int i, ret;
  252. /*
  253. * Keep reading until we see that the scan state does not change.
  254. * That indicates that we are done.
  255. *
  256. * Assume that the EC keyscan buffer is at most 32 deep.
  257. */
  258. duration = jiffies;
  259. ret = cros_ec_keyb_get_state(ckdev, new_state);
  260. for (i = 1; !ret && i < 32; i++) {
  261. memcpy(old_state, new_state, sizeof(old_state));
  262. ret = cros_ec_keyb_get_state(ckdev, new_state);
  263. if (0 == memcmp(old_state, new_state, sizeof(old_state)))
  264. break;
  265. }
  266. duration = jiffies - duration;
  267. dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
  268. jiffies_to_usecs(duration));
  269. }
  270. static int cros_ec_keyb_resume(struct device *dev)
  271. {
  272. struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  273. /*
  274. * When the EC is not a wake source, then it could not have caused the
  275. * resume, so we clear the EC's key scan buffer. If the EC was a
  276. * wake source (e.g. the lid is open and the user might press a key to
  277. * wake) then the key scan buffer should be preserved.
  278. */
  279. if (ckdev->ec->was_wake_device)
  280. cros_ec_keyb_clear_keyboard(ckdev);
  281. return 0;
  282. }
  283. #endif
  284. static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
  285. static struct platform_driver cros_ec_keyb_driver = {
  286. .probe = cros_ec_keyb_probe,
  287. .driver = {
  288. .name = "cros-ec-keyb",
  289. .pm = &cros_ec_keyb_pm_ops,
  290. },
  291. };
  292. module_platform_driver(cros_ec_keyb_driver);
  293. MODULE_LICENSE("GPL");
  294. MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
  295. MODULE_ALIAS("platform:cros-ec-keyb");