cros_ec_keyb.c 9.8 KB

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