cros_ec_keyb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/bitops.h>
  25. #include <linux/i2c.h>
  26. #include <linux/input.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <linux/notifier.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/input/matrix_keypad.h>
  33. #include <linux/mfd/cros_ec.h>
  34. #include <linux/mfd/cros_ec_commands.h>
  35. /*
  36. * @rows: Number of rows in the keypad
  37. * @cols: Number of columns in the keypad
  38. * @row_shift: log2 or number of rows, rounded up
  39. * @keymap_data: Matrix keymap data used to convert to keyscan values
  40. * @ghost_filter: true to enable the matrix key-ghosting filter
  41. * @valid_keys: bitmap of existing keys for each matrix column
  42. * @old_kb_state: bitmap of keys pressed last scan
  43. * @dev: Device pointer
  44. * @idev: Input device
  45. * @ec: Top level ChromeOS device to use to talk to EC
  46. * @notifier: interrupt event notifier for transport devices
  47. */
  48. struct cros_ec_keyb {
  49. unsigned int rows;
  50. unsigned int cols;
  51. int row_shift;
  52. const struct matrix_keymap_data *keymap_data;
  53. bool ghost_filter;
  54. uint8_t *valid_keys;
  55. uint8_t *old_kb_state;
  56. struct device *dev;
  57. struct input_dev *idev;
  58. struct cros_ec_device *ec;
  59. struct notifier_block notifier;
  60. };
  61. /*
  62. * Returns true when there is at least one combination of pressed keys that
  63. * results in ghosting.
  64. */
  65. static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
  66. {
  67. int col1, col2, buf1, buf2;
  68. struct device *dev = ckdev->dev;
  69. uint8_t *valid_keys = ckdev->valid_keys;
  70. /*
  71. * Ghosting happens if for any pressed key X there are other keys
  72. * pressed both in the same row and column of X as, for instance,
  73. * in the following diagram:
  74. *
  75. * . . Y . g .
  76. * . . . . . .
  77. * . . . . . .
  78. * . . X . Z .
  79. *
  80. * In this case only X, Y, and Z are pressed, but g appears to be
  81. * pressed too (see Wikipedia).
  82. */
  83. for (col1 = 0; col1 < ckdev->cols; col1++) {
  84. buf1 = buf[col1] & valid_keys[col1];
  85. for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
  86. buf2 = buf[col2] & valid_keys[col2];
  87. if (hweight8(buf1 & buf2) > 1) {
  88. dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
  89. col1, buf1, col2, buf2);
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. /*
  97. * Compares the new keyboard state to the old one and produces key
  98. * press/release events accordingly. The keyboard state is 13 bytes (one byte
  99. * per column)
  100. */
  101. static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
  102. uint8_t *kb_state, int len)
  103. {
  104. struct input_dev *idev = ckdev->idev;
  105. int col, row;
  106. int new_state;
  107. int old_state;
  108. int num_cols;
  109. num_cols = len;
  110. if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
  111. /*
  112. * Simple-minded solution: ignore this state. The obvious
  113. * improvement is to only ignore changes to keys involved in
  114. * the ghosting, but process the other changes.
  115. */
  116. dev_dbg(ckdev->dev, "ghosting found\n");
  117. return;
  118. }
  119. for (col = 0; col < ckdev->cols; col++) {
  120. for (row = 0; row < ckdev->rows; row++) {
  121. int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
  122. const unsigned short *keycodes = idev->keycode;
  123. new_state = kb_state[col] & (1 << row);
  124. old_state = ckdev->old_kb_state[col] & (1 << row);
  125. if (new_state != old_state) {
  126. dev_dbg(ckdev->dev,
  127. "changed: [r%d c%d]: byte %02x\n",
  128. row, col, new_state);
  129. input_report_key(idev, keycodes[pos],
  130. new_state);
  131. }
  132. }
  133. ckdev->old_kb_state[col] = kb_state[col];
  134. }
  135. input_sync(ckdev->idev);
  136. }
  137. static int cros_ec_keyb_open(struct input_dev *dev)
  138. {
  139. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  140. return blocking_notifier_chain_register(&ckdev->ec->event_notifier,
  141. &ckdev->notifier);
  142. }
  143. static void cros_ec_keyb_close(struct input_dev *dev)
  144. {
  145. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  146. blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
  147. &ckdev->notifier);
  148. }
  149. static int cros_ec_keyb_work(struct notifier_block *nb,
  150. unsigned long queued_during_suspend, void *_notify)
  151. {
  152. struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
  153. notifier);
  154. if (ckdev->ec->event_data.event_type != EC_MKBP_EVENT_KEY_MATRIX)
  155. return NOTIFY_DONE;
  156. /*
  157. * If EC is not the wake source, discard key state changes during
  158. * suspend.
  159. */
  160. if (queued_during_suspend)
  161. return NOTIFY_OK;
  162. if (ckdev->ec->event_size != ckdev->cols) {
  163. dev_err(ckdev->dev,
  164. "Discarded incomplete key matrix event.\n");
  165. return NOTIFY_OK;
  166. }
  167. cros_ec_keyb_process(ckdev, ckdev->ec->event_data.data.key_matrix,
  168. ckdev->ec->event_size);
  169. return NOTIFY_OK;
  170. }
  171. /*
  172. * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
  173. * ghosting logic to ignore NULL or virtual keys.
  174. */
  175. static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
  176. {
  177. int row, col;
  178. int row_shift = ckdev->row_shift;
  179. unsigned short *keymap = ckdev->idev->keycode;
  180. unsigned short code;
  181. BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
  182. for (col = 0; col < ckdev->cols; col++) {
  183. for (row = 0; row < ckdev->rows; row++) {
  184. code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
  185. if (code && (code != KEY_BATTERY))
  186. ckdev->valid_keys[col] |= 1 << row;
  187. }
  188. dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
  189. col, ckdev->valid_keys[col]);
  190. }
  191. }
  192. static int cros_ec_keyb_probe(struct platform_device *pdev)
  193. {
  194. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  195. struct device *dev = &pdev->dev;
  196. struct cros_ec_keyb *ckdev;
  197. struct input_dev *idev;
  198. struct device_node *np;
  199. int err;
  200. np = pdev->dev.of_node;
  201. if (!np)
  202. return -ENODEV;
  203. ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
  204. if (!ckdev)
  205. return -ENOMEM;
  206. err = matrix_keypad_parse_of_params(dev, &ckdev->rows, &ckdev->cols);
  207. if (err)
  208. return err;
  209. ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  210. if (!ckdev->valid_keys)
  211. return -ENOMEM;
  212. ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
  213. if (!ckdev->old_kb_state)
  214. return -ENOMEM;
  215. idev = devm_input_allocate_device(dev);
  216. if (!idev)
  217. return -ENOMEM;
  218. ckdev->ec = ec;
  219. ckdev->notifier.notifier_call = cros_ec_keyb_work;
  220. ckdev->dev = dev;
  221. dev_set_drvdata(dev, ckdev);
  222. idev->name = CROS_EC_DEV_NAME;
  223. idev->phys = ec->phys_name;
  224. __set_bit(EV_REP, idev->evbit);
  225. idev->id.bustype = BUS_VIRTUAL;
  226. idev->id.version = 1;
  227. idev->id.product = 0;
  228. idev->dev.parent = dev;
  229. idev->open = cros_ec_keyb_open;
  230. idev->close = cros_ec_keyb_close;
  231. ckdev->ghost_filter = of_property_read_bool(np,
  232. "google,needs-ghost-filter");
  233. err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
  234. NULL, idev);
  235. if (err) {
  236. dev_err(dev, "cannot build key matrix\n");
  237. return err;
  238. }
  239. ckdev->row_shift = get_count_order(ckdev->cols);
  240. input_set_capability(idev, EV_MSC, MSC_SCAN);
  241. input_set_drvdata(idev, ckdev);
  242. ckdev->idev = idev;
  243. cros_ec_keyb_compute_valid_keys(ckdev);
  244. err = input_register_device(ckdev->idev);
  245. if (err) {
  246. dev_err(dev, "cannot register input device\n");
  247. return err;
  248. }
  249. return 0;
  250. }
  251. #ifdef CONFIG_OF
  252. static const struct of_device_id cros_ec_keyb_of_match[] = {
  253. { .compatible = "google,cros-ec-keyb" },
  254. {},
  255. };
  256. MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
  257. #endif
  258. static struct platform_driver cros_ec_keyb_driver = {
  259. .probe = cros_ec_keyb_probe,
  260. .driver = {
  261. .name = "cros-ec-keyb",
  262. .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
  263. },
  264. };
  265. module_platform_driver(cros_ec_keyb_driver);
  266. MODULE_LICENSE("GPL");
  267. MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
  268. MODULE_ALIAS("platform:cros-ec-keyb");