cros_ec_keyb.c 6.5 KB

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