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