cros_ec_keyb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <errno.h>
  11. #include <fdtdec.h>
  12. #include <input.h>
  13. #include <key_matrix.h>
  14. #include <stdio_dev.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  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. static struct keyb {
  22. struct cros_ec_dev *dev; /* The CROS_EC device */
  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. int inited; /* 1 if keyboard is ready */
  29. } config;
  30. /**
  31. * Check the keyboard controller and return a list of key matrix positions
  32. * for which a key is pressed
  33. *
  34. * @param config Keyboard config
  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 keyb *config,
  41. struct key_matrix_key *keys, int max_count,
  42. bool *samep)
  43. {
  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(config->dev, &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 < config->matrix.num_cols;
  64. col++) {
  65. for (row = 0; row < config->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. * Test if keys are available to be read
  81. *
  82. * @return 0 if no keys available, 1 if keys are available
  83. */
  84. static int kbd_tstc(struct stdio_dev *dev)
  85. {
  86. /* Just get input to do this for us */
  87. return config.inited ? input_tstc(&config.input) : 0;
  88. }
  89. /**
  90. * Read a key
  91. *
  92. * @return ASCII key code, or 0 if no key, or -1 if error
  93. */
  94. static int kbd_getc(struct stdio_dev *dev)
  95. {
  96. /* Just get input to do this for us */
  97. return config.inited ? input_getc(&config.input) : 0;
  98. }
  99. /**
  100. * Check the keyboard, and send any keys that are pressed.
  101. *
  102. * This is called by input_tstc() and input_getc() when they need more
  103. * characters
  104. *
  105. * @param input Input configuration
  106. * @return 1, to indicate that we have something to look at
  107. */
  108. int cros_ec_kbc_check(struct input_config *input)
  109. {
  110. static struct key_matrix_key last_keys[KBC_MAX_KEYS];
  111. static int last_num_keys;
  112. struct key_matrix_key keys[KBC_MAX_KEYS];
  113. int keycodes[KBC_MAX_KEYS];
  114. int num_keys, num_keycodes;
  115. int irq_pending, sent;
  116. bool same = false;
  117. /*
  118. * Loop until the EC has no more keyscan records, or we have
  119. * received at least one character. This means we know that tstc()
  120. * will always return non-zero if keys have been pressed.
  121. *
  122. * Without this loop, a key release (which generates no new ascii
  123. * characters) will cause us to exit this function, and just tstc()
  124. * may return 0 before all keys have been read from the EC.
  125. */
  126. do {
  127. irq_pending = cros_ec_interrupt_pending(config.dev);
  128. if (irq_pending) {
  129. num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS,
  130. &same);
  131. if (num_keys < 0)
  132. return 0;
  133. last_num_keys = num_keys;
  134. memcpy(last_keys, keys, sizeof(keys));
  135. } else {
  136. /*
  137. * EC doesn't want to be asked, so use keys from last
  138. * time.
  139. */
  140. num_keys = last_num_keys;
  141. memcpy(keys, last_keys, sizeof(keys));
  142. }
  143. if (num_keys < 0)
  144. return -1;
  145. num_keycodes = key_matrix_decode(&config.matrix, keys,
  146. num_keys, keycodes, KBC_MAX_KEYS);
  147. sent = input_send_keycodes(input, keycodes, num_keycodes);
  148. /*
  149. * For those ECs without an interrupt, stop scanning when we
  150. * see that the scan is the same as last time.
  151. */
  152. if ((irq_pending < 0) && same)
  153. break;
  154. } while (irq_pending && !sent);
  155. return 1;
  156. }
  157. /**
  158. * Decode MBKP keyboard details from the device tree
  159. *
  160. * @param blob Device tree blob
  161. * @param node Node to decode from
  162. * @param config Configuration data read from fdt
  163. * @return 0 if ok, -1 on error
  164. */
  165. static int cros_ec_keyb_decode_fdt(const void *blob, int node,
  166. struct keyb *config)
  167. {
  168. /*
  169. * Get keyboard rows and columns - at present we are limited to
  170. * 8 columns by the protocol (one byte per row scan)
  171. */
  172. config->key_rows = fdtdec_get_int(blob, node, "keypad,num-rows", 0);
  173. config->key_cols = fdtdec_get_int(blob, node, "keypad,num-columns", 0);
  174. if (!config->key_rows || !config->key_cols ||
  175. config->key_rows * config->key_cols / 8
  176. > CROS_EC_KEYSCAN_COLS) {
  177. debug("%s: Invalid key matrix size %d x %d\n", __func__,
  178. config->key_rows, config->key_cols);
  179. return -1;
  180. }
  181. config->ghost_filter = fdtdec_get_bool(blob, node,
  182. "google,ghost-filter");
  183. return 0;
  184. }
  185. /**
  186. * Set up the keyboard. This is called by the stdio device handler.
  187. *
  188. * We want to do this init when the keyboard is actually used rather than
  189. * at start-up, since keyboard input may not currently be selected.
  190. *
  191. * @return 0 if ok, -1 on error
  192. */
  193. static int cros_ec_init_keyboard(struct stdio_dev *dev)
  194. {
  195. const void *blob = gd->fdt_blob;
  196. int node;
  197. config.dev = board_get_cros_ec_dev();
  198. if (!config.dev) {
  199. debug("%s: no cros_ec device: cannot init keyboard\n",
  200. __func__);
  201. return -1;
  202. }
  203. node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
  204. if (node < 0) {
  205. debug("%s: Node not found\n", __func__);
  206. return -1;
  207. }
  208. if (cros_ec_keyb_decode_fdt(blob, node, &config))
  209. return -1;
  210. input_set_delays(&config.input, KBC_REPEAT_DELAY_MS,
  211. KBC_REPEAT_RATE_MS);
  212. if (key_matrix_init(&config.matrix, config.key_rows,
  213. config.key_cols, config.ghost_filter)) {
  214. debug("%s: cannot init key matrix\n", __func__);
  215. return -1;
  216. }
  217. if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
  218. debug("%s: Could not decode key matrix from fdt\n", __func__);
  219. return -1;
  220. }
  221. config.inited = 1;
  222. debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
  223. config.key_cols);
  224. return 0;
  225. }
  226. int drv_keyboard_init(void)
  227. {
  228. struct stdio_dev dev;
  229. if (input_init(&config.input, 0)) {
  230. debug("%s: Cannot set up input\n", __func__);
  231. return -1;
  232. }
  233. config.input.read_keys = cros_ec_kbc_check;
  234. memset(&dev, '\0', sizeof(dev));
  235. strcpy(dev.name, "cros-ec-keyb");
  236. dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  237. dev.getc = kbd_getc;
  238. dev.tstc = kbd_tstc;
  239. dev.start = cros_ec_init_keyboard;
  240. /* Register the device. cros_ec_init_keyboard() will be called soon */
  241. return input_stdio_register(&dev);
  242. }