cros_ec_keyb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS Matrix Keyboard
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. #include <common.h>
  8. #include <cros_ec.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <input.h>
  12. #include <keyboard.h>
  13. #include <key_matrix.h>
  14. #include <log.h>
  15. #include <stdio_dev.h>
  16. enum {
  17. KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
  18. KBC_REPEAT_RATE_MS = 30,
  19. KBC_REPEAT_DELAY_MS = 240,
  20. };
  21. struct cros_ec_keyb_priv {
  22. struct input_config *input; /* The input layer */
  23. struct key_matrix matrix; /* The key matrix layer */
  24. int key_rows; /* Number of keyboard rows */
  25. int key_cols; /* Number of keyboard columns */
  26. int ghost_filter; /* 1 to enable ghost filter, else 0 */
  27. };
  28. /**
  29. * Check the keyboard controller and return a list of key matrix positions
  30. * for which a key is pressed
  31. *
  32. * @param dev Keyboard device
  33. * @param keys List of keys that we have detected
  34. * @param max_count Maximum number of keys to return
  35. * @param samep Set to true if this scan repeats the last, else false
  36. * @return number of pressed keys, 0 for none, -EIO on error
  37. */
  38. static int check_for_keys(struct udevice *dev, struct key_matrix_key *keys,
  39. int max_count, bool *samep)
  40. {
  41. struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
  42. struct key_matrix_key *key;
  43. static struct mbkp_keyscan last_scan;
  44. static bool last_scan_valid;
  45. struct ec_response_get_next_event event;
  46. struct mbkp_keyscan *scan = (struct mbkp_keyscan *)
  47. &event.data.key_matrix;
  48. unsigned int row, col, bit, data;
  49. int num_keys;
  50. int ret;
  51. /* Get pending MKBP event. It may not be a key matrix event. */
  52. do {
  53. ret = cros_ec_get_next_event(dev->parent, &event);
  54. /* The EC has no events for us at this time. */
  55. if (ret == -EC_RES_UNAVAILABLE)
  56. return -EIO;
  57. else if (ret)
  58. break;
  59. } while (event.event_type != EC_MKBP_EVENT_KEY_MATRIX);
  60. /* Try the old command if the EC doesn't support the above. */
  61. if (ret == -EC_RES_INVALID_COMMAND) {
  62. if (cros_ec_scan_keyboard(dev->parent, scan)) {
  63. debug("%s: keyboard scan failed\n", __func__);
  64. return -EIO;
  65. }
  66. } else if (ret) {
  67. debug("%s: Error getting next MKBP event. (%d)\n",
  68. __func__, ret);
  69. return -EIO;
  70. }
  71. *samep = last_scan_valid && !memcmp(&last_scan, scan, sizeof(*scan));
  72. /*
  73. * This is a bit odd. The EC has no way to tell us that it has run
  74. * out of key scans. It just returns the same scan over and over
  75. * again. So the only way to detect that we have run out is to detect
  76. * that this scan is the same as the last.
  77. */
  78. last_scan_valid = true;
  79. memcpy(&last_scan, scan, sizeof(last_scan));
  80. for (col = num_keys = bit = 0; col < priv->matrix.num_cols;
  81. col++) {
  82. for (row = 0; row < priv->matrix.num_rows; row++) {
  83. unsigned int mask = 1 << (bit & 7);
  84. data = scan->data[bit / 8];
  85. if ((data & mask) && num_keys < max_count) {
  86. key = keys + num_keys++;
  87. key->row = row;
  88. key->col = col;
  89. key->valid = 1;
  90. }
  91. bit++;
  92. }
  93. }
  94. return num_keys;
  95. }
  96. /**
  97. * Check the keyboard, and send any keys that are pressed.
  98. *
  99. * This is called by input_tstc() and input_getc() when they need more
  100. * characters
  101. *
  102. * @param input Input configuration
  103. * @return 1, to indicate that we have something to look at
  104. */
  105. int cros_ec_kbc_check(struct input_config *input)
  106. {
  107. struct udevice *dev = input->dev;
  108. struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
  109. static struct key_matrix_key last_keys[KBC_MAX_KEYS];
  110. static int last_num_keys;
  111. struct key_matrix_key keys[KBC_MAX_KEYS];
  112. int keycodes[KBC_MAX_KEYS];
  113. int num_keys, num_keycodes;
  114. int irq_pending, sent;
  115. bool same = false;
  116. /*
  117. * Loop until the EC has no more keyscan records, or we have
  118. * received at least one character. This means we know that tstc()
  119. * will always return non-zero if keys have been pressed.
  120. *
  121. * Without this loop, a key release (which generates no new ascii
  122. * characters) will cause us to exit this function, and just tstc()
  123. * may return 0 before all keys have been read from the EC.
  124. */
  125. do {
  126. irq_pending = cros_ec_interrupt_pending(dev->parent);
  127. if (irq_pending) {
  128. num_keys = check_for_keys(dev, keys, KBC_MAX_KEYS,
  129. &same);
  130. if (num_keys < 0)
  131. return 0;
  132. last_num_keys = num_keys;
  133. memcpy(last_keys, keys, sizeof(keys));
  134. } else {
  135. /*
  136. * EC doesn't want to be asked, so use keys from last
  137. * time.
  138. */
  139. num_keys = last_num_keys;
  140. memcpy(keys, last_keys, sizeof(keys));
  141. }
  142. if (num_keys < 0)
  143. return -1;
  144. num_keycodes = key_matrix_decode(&priv->matrix, keys,
  145. num_keys, keycodes, KBC_MAX_KEYS);
  146. sent = input_send_keycodes(input, keycodes, num_keycodes);
  147. /*
  148. * For those ECs without an interrupt, stop scanning when we
  149. * see that the scan is the same as last time.
  150. */
  151. if ((irq_pending < 0) && same)
  152. break;
  153. } while (irq_pending && !sent);
  154. return 1;
  155. }
  156. /**
  157. * Decode MBKP keyboard details from the device tree
  158. *
  159. * @param blob Device tree blob
  160. * @param node Node to decode from
  161. * @param config Configuration data read from fdt
  162. * @return 0 if ok, -1 on error
  163. */
  164. static int cros_ec_keyb_decode_fdt(struct udevice *dev,
  165. struct cros_ec_keyb_priv *config)
  166. {
  167. /*
  168. * Get keyboard rows and columns - at present we are limited to
  169. * 8 columns by the protocol (one byte per row scan)
  170. */
  171. config->key_rows = dev_read_u32_default(dev, "keypad,num-rows", 0);
  172. config->key_cols = dev_read_u32_default(dev, "keypad,num-columns", 0);
  173. if (!config->key_rows || !config->key_cols ||
  174. config->key_rows * config->key_cols / 8
  175. > CROS_EC_KEYSCAN_COLS) {
  176. debug("%s: Invalid key matrix size %d x %d\n", __func__,
  177. config->key_rows, config->key_cols);
  178. return -1;
  179. }
  180. config->ghost_filter = dev_read_bool(dev, "google,needs-ghost-filter");
  181. return 0;
  182. }
  183. static int cros_ec_kbd_probe(struct udevice *dev)
  184. {
  185. struct cros_ec_keyb_priv *priv = dev_get_priv(dev);
  186. struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
  187. struct stdio_dev *sdev = &uc_priv->sdev;
  188. struct input_config *input = &uc_priv->input;
  189. int ret;
  190. ret = cros_ec_keyb_decode_fdt(dev, priv);
  191. if (ret) {
  192. debug("%s: Cannot decode node (ret=%d)\n", __func__, ret);
  193. return -EINVAL;
  194. }
  195. input_set_delays(input, KBC_REPEAT_DELAY_MS, KBC_REPEAT_RATE_MS);
  196. ret = key_matrix_init(&priv->matrix, priv->key_rows, priv->key_cols,
  197. priv->ghost_filter);
  198. if (ret) {
  199. debug("%s: cannot init key matrix\n", __func__);
  200. return ret;
  201. }
  202. ret = key_matrix_decode_fdt(dev, &priv->matrix);
  203. if (ret) {
  204. debug("%s: Could not decode key matrix from fdt\n", __func__);
  205. return ret;
  206. }
  207. debug("%s: Matrix keyboard %dx%d ready\n", __func__, priv->key_rows,
  208. priv->key_cols);
  209. priv->input = input;
  210. input->dev = dev;
  211. input_add_tables(input, false);
  212. input->read_keys = cros_ec_kbc_check;
  213. strcpy(sdev->name, "cros-ec-keyb");
  214. /* Register the device. cros_ec_init_keyboard() will be called soon */
  215. return input_stdio_register(sdev);
  216. }
  217. static const struct keyboard_ops cros_ec_kbd_ops = {
  218. };
  219. static const struct udevice_id cros_ec_kbd_ids[] = {
  220. { .compatible = "google,cros-ec-keyb" },
  221. { }
  222. };
  223. U_BOOT_DRIVER(google_cros_ec_keyb) = {
  224. .name = "google_cros_ec_keyb",
  225. .id = UCLASS_KEYBOARD,
  226. .of_match = cros_ec_kbd_ids,
  227. .probe = cros_ec_kbd_probe,
  228. .ops = &cros_ec_kbd_ops,
  229. .priv_auto_alloc_size = sizeof(struct cros_ec_keyb_priv),
  230. };