cros_ec_keyb.c 9.1 KB

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