cros_ec_keyb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 = 0;
  137. struct cros_ec_command *msg;
  138. msg = kmalloc(sizeof(*msg) + ckdev->cols, GFP_KERNEL);
  139. if (!msg)
  140. return -ENOMEM;
  141. msg->version = 0;
  142. msg->command = EC_CMD_MKBP_STATE;
  143. msg->insize = ckdev->cols;
  144. msg->outsize = 0;
  145. ret = cros_ec_cmd_xfer(ckdev->ec, msg);
  146. if (ret < 0) {
  147. dev_err(ckdev->dev, "Error transferring EC message %d\n", ret);
  148. goto exit;
  149. }
  150. memcpy(kb_state, msg->data, ckdev->cols);
  151. exit:
  152. kfree(msg);
  153. return ret;
  154. }
  155. static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
  156. {
  157. struct cros_ec_keyb *ckdev = data;
  158. struct cros_ec_device *ec = ckdev->ec;
  159. int ret;
  160. uint8_t kb_state[ckdev->cols];
  161. if (device_may_wakeup(ec->dev))
  162. pm_wakeup_event(ec->dev, 0);
  163. ret = cros_ec_keyb_get_state(ckdev, kb_state);
  164. if (ret >= 0)
  165. cros_ec_keyb_process(ckdev, kb_state, ret);
  166. else
  167. dev_err(ec->dev, "failed to get keyboard state: %d\n", ret);
  168. return IRQ_HANDLED;
  169. }
  170. static int cros_ec_keyb_open(struct input_dev *dev)
  171. {
  172. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  173. struct cros_ec_device *ec = ckdev->ec;
  174. return request_threaded_irq(ec->irq, NULL, cros_ec_keyb_irq,
  175. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  176. "cros_ec_keyb", ckdev);
  177. }
  178. static void cros_ec_keyb_close(struct input_dev *dev)
  179. {
  180. struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
  181. struct cros_ec_device *ec = ckdev->ec;
  182. free_irq(ec->irq, ckdev);
  183. }
  184. /*
  185. * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
  186. * ghosting logic to ignore NULL or virtual keys.
  187. */
  188. static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
  189. {
  190. int row, col;
  191. int row_shift = ckdev->row_shift;
  192. unsigned short *keymap = ckdev->idev->keycode;
  193. unsigned short code;
  194. BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
  195. for (col = 0; col < ckdev->cols; col++) {
  196. for (row = 0; row < ckdev->rows; row++) {
  197. code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
  198. if (code && (code != KEY_BATTERY))
  199. ckdev->valid_keys[col] |= 1 << row;
  200. }
  201. dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
  202. col, ckdev->valid_keys[col]);
  203. }
  204. }
  205. static int cros_ec_keyb_probe(struct platform_device *pdev)
  206. {
  207. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  208. struct device *dev = ec->dev;
  209. struct cros_ec_keyb *ckdev;
  210. struct input_dev *idev;
  211. struct device_node *np;
  212. int err;
  213. np = pdev->dev.of_node;
  214. if (!np)
  215. return -ENODEV;
  216. ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
  217. if (!ckdev)
  218. return -ENOMEM;
  219. err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
  220. &ckdev->cols);
  221. if (err)
  222. return err;
  223. ckdev->valid_keys = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
  224. if (!ckdev->valid_keys)
  225. return -ENOMEM;
  226. ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
  227. if (!ckdev->old_kb_state)
  228. return -ENOMEM;
  229. idev = devm_input_allocate_device(&pdev->dev);
  230. if (!idev)
  231. return -ENOMEM;
  232. if (!ec->irq) {
  233. dev_err(dev, "no EC IRQ specified\n");
  234. return -EINVAL;
  235. }
  236. ckdev->ec = ec;
  237. ckdev->dev = dev;
  238. dev_set_drvdata(&pdev->dev, ckdev);
  239. idev->name = CROS_EC_DEV_NAME;
  240. idev->phys = ec->phys_name;
  241. __set_bit(EV_REP, idev->evbit);
  242. idev->id.bustype = BUS_VIRTUAL;
  243. idev->id.version = 1;
  244. idev->id.product = 0;
  245. idev->dev.parent = &pdev->dev;
  246. idev->open = cros_ec_keyb_open;
  247. idev->close = cros_ec_keyb_close;
  248. ckdev->ghost_filter = of_property_read_bool(np,
  249. "google,needs-ghost-filter");
  250. err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
  251. NULL, idev);
  252. if (err) {
  253. dev_err(dev, "cannot build key matrix\n");
  254. return err;
  255. }
  256. ckdev->row_shift = get_count_order(ckdev->cols);
  257. input_set_capability(idev, EV_MSC, MSC_SCAN);
  258. input_set_drvdata(idev, ckdev);
  259. ckdev->idev = idev;
  260. cros_ec_keyb_compute_valid_keys(ckdev);
  261. err = input_register_device(ckdev->idev);
  262. if (err) {
  263. dev_err(dev, "cannot register input device\n");
  264. return err;
  265. }
  266. return 0;
  267. }
  268. #ifdef CONFIG_PM_SLEEP
  269. /* Clear any keys in the buffer */
  270. static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
  271. {
  272. uint8_t old_state[ckdev->cols];
  273. uint8_t new_state[ckdev->cols];
  274. unsigned long duration;
  275. int i, ret;
  276. /*
  277. * Keep reading until we see that the scan state does not change.
  278. * That indicates that we are done.
  279. *
  280. * Assume that the EC keyscan buffer is at most 32 deep.
  281. */
  282. duration = jiffies;
  283. ret = cros_ec_keyb_get_state(ckdev, new_state);
  284. for (i = 1; !ret && i < 32; i++) {
  285. memcpy(old_state, new_state, sizeof(old_state));
  286. ret = cros_ec_keyb_get_state(ckdev, new_state);
  287. if (0 == memcmp(old_state, new_state, sizeof(old_state)))
  288. break;
  289. }
  290. duration = jiffies - duration;
  291. dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
  292. jiffies_to_usecs(duration));
  293. }
  294. static int cros_ec_keyb_resume(struct device *dev)
  295. {
  296. struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
  297. /*
  298. * When the EC is not a wake source, then it could not have caused the
  299. * resume, so we clear the EC's key scan buffer. If the EC was a
  300. * wake source (e.g. the lid is open and the user might press a key to
  301. * wake) then the key scan buffer should be preserved.
  302. */
  303. if (!ckdev->ec->was_wake_device)
  304. cros_ec_keyb_clear_keyboard(ckdev);
  305. return 0;
  306. }
  307. #endif
  308. static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
  309. #ifdef CONFIG_OF
  310. static const struct of_device_id cros_ec_keyb_of_match[] = {
  311. { .compatible = "google,cros-ec-keyb" },
  312. {},
  313. };
  314. MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
  315. #endif
  316. static struct platform_driver cros_ec_keyb_driver = {
  317. .probe = cros_ec_keyb_probe,
  318. .driver = {
  319. .name = "cros-ec-keyb",
  320. .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
  321. .pm = &cros_ec_keyb_pm_ops,
  322. },
  323. };
  324. module_platform_driver(cros_ec_keyb_driver);
  325. MODULE_LICENSE("GPL");
  326. MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
  327. MODULE_ALIAS("platform:cros-ec-keyb");