cros_ec_keyb.c 6.5 KB

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