cros_ec_keyb.c 9.8 KB

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