2 * Chromium OS Matrix Keyboard
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <key_matrix.h>
15 #include <stdio_dev.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
21 KBC_REPEAT_RATE_MS = 30,
22 KBC_REPEAT_DELAY_MS = 240,
26 struct cros_ec_dev *dev; /* The CROS_EC device */
27 struct input_config input; /* The input layer */
28 struct key_matrix matrix; /* The key matrix layer */
29 int key_rows; /* Number of keyboard rows */
30 int key_cols; /* Number of keyboard columns */
31 int ghost_filter; /* 1 to enable ghost filter, else 0 */
32 int inited; /* 1 if keyboard is ready */
37 * Check the keyboard controller and return a list of key matrix positions
38 * for which a key is pressed
40 * @param config Keyboard config
41 * @param keys List of keys that we have detected
42 * @param max_count Maximum number of keys to return
43 * @param samep Set to true if this scan repeats the last, else false
44 * @return number of pressed keys, 0 for none, -EIO on error
46 static int check_for_keys(struct keyb *config,
47 struct key_matrix_key *keys, int max_count,
50 struct key_matrix_key *key;
51 static struct mbkp_keyscan last_scan;
52 static bool last_scan_valid;
53 struct mbkp_keyscan scan;
54 unsigned int row, col, bit, data;
57 if (cros_ec_scan_keyboard(config->dev, &scan)) {
58 debug("%s: keyboard scan failed\n", __func__);
61 *samep = last_scan_valid && !memcmp(&last_scan, &scan, sizeof(scan));
64 * This is a bit odd. The EC has no way to tell us that it has run
65 * out of key scans. It just returns the same scan over and over
66 * again. So the only way to detect that we have run out is to detect
67 * that this scan is the same as the last.
69 last_scan_valid = true;
70 memcpy(&last_scan, &scan, sizeof(last_scan));
72 for (col = num_keys = bit = 0; col < config->matrix.num_cols;
74 for (row = 0; row < config->matrix.num_rows; row++) {
75 unsigned int mask = 1 << (bit & 7);
77 data = scan.data[bit / 8];
78 if ((data & mask) && num_keys < max_count) {
79 key = keys + num_keys++;
92 * Test if keys are available to be read
94 * @return 0 if no keys available, 1 if keys are available
96 static int kbd_tstc(struct stdio_dev *dev)
98 /* Just get input to do this for us */
99 return config.inited ? input_tstc(&config.input) : 0;
105 * @return ASCII key code, or 0 if no key, or -1 if error
107 static int kbd_getc(struct stdio_dev *dev)
109 /* Just get input to do this for us */
110 return config.inited ? input_getc(&config.input) : 0;
114 * Check the keyboard, and send any keys that are pressed.
116 * This is called by input_tstc() and input_getc() when they need more
119 * @param input Input configuration
120 * @return 1, to indicate that we have something to look at
122 int cros_ec_kbc_check(struct input_config *input)
124 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
125 static int last_num_keys;
126 struct key_matrix_key keys[KBC_MAX_KEYS];
127 int keycodes[KBC_MAX_KEYS];
128 int num_keys, num_keycodes;
129 int irq_pending, sent;
133 * Loop until the EC has no more keyscan records, or we have
134 * received at least one character. This means we know that tstc()
135 * will always return non-zero if keys have been pressed.
137 * Without this loop, a key release (which generates no new ascii
138 * characters) will cause us to exit this function, and just tstc()
139 * may return 0 before all keys have been read from the EC.
142 irq_pending = cros_ec_interrupt_pending(config.dev);
144 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS,
148 last_num_keys = num_keys;
149 memcpy(last_keys, keys, sizeof(keys));
152 * EC doesn't want to be asked, so use keys from last
155 num_keys = last_num_keys;
156 memcpy(keys, last_keys, sizeof(keys));
161 num_keycodes = key_matrix_decode(&config.matrix, keys,
162 num_keys, keycodes, KBC_MAX_KEYS);
163 sent = input_send_keycodes(input, keycodes, num_keycodes);
166 * For those ECs without an interrupt, stop scanning when we
167 * see that the scan is the same as last time.
169 if ((irq_pending < 0) && same)
171 } while (irq_pending && !sent);
177 * Decode MBKP keyboard details from the device tree
179 * @param blob Device tree blob
180 * @param node Node to decode from
181 * @param config Configuration data read from fdt
182 * @return 0 if ok, -1 on error
184 static int cros_ec_keyb_decode_fdt(const void *blob, int node,
188 * Get keyboard rows and columns - at present we are limited to
189 * 8 columns by the protocol (one byte per row scan)
191 config->key_rows = fdtdec_get_int(blob, node, "keypad,num-rows", 0);
192 config->key_cols = fdtdec_get_int(blob, node, "keypad,num-columns", 0);
193 if (!config->key_rows || !config->key_cols ||
194 config->key_rows * config->key_cols / 8
195 > CROS_EC_KEYSCAN_COLS) {
196 debug("%s: Invalid key matrix size %d x %d\n", __func__,
197 config->key_rows, config->key_cols);
200 config->ghost_filter = fdtdec_get_bool(blob, node,
201 "google,needs-ghost-filter");
206 * Set up the keyboard. This is called by the stdio device handler.
208 * We want to do this init when the keyboard is actually used rather than
209 * at start-up, since keyboard input may not currently be selected.
211 * @return 0 if ok, -1 on error
213 static int cros_ec_init_keyboard(struct stdio_dev *dev)
215 const void *blob = gd->fdt_blob;
218 config.dev = board_get_cros_ec_dev();
220 debug("%s: no cros_ec device: cannot init keyboard\n",
224 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
226 debug("%s: Node not found\n", __func__);
229 if (cros_ec_keyb_decode_fdt(blob, node, &config))
231 input_set_delays(&config.input, KBC_REPEAT_DELAY_MS,
233 if (key_matrix_init(&config.matrix, config.key_rows,
234 config.key_cols, config.ghost_filter)) {
235 debug("%s: cannot init key matrix\n", __func__);
238 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
239 debug("%s: Could not decode key matrix from fdt\n", __func__);
243 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
249 int drv_keyboard_init(void)
251 struct stdio_dev dev;
253 if (input_init(&config.input, 0)) {
254 debug("%s: Cannot set up input\n", __func__);
257 config.input.read_keys = cros_ec_kbc_check;
259 memset(&dev, '\0', sizeof(dev));
260 strcpy(dev.name, "cros-ec-keyb");
261 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
264 dev.start = cros_ec_init_keyboard;
266 /* Register the device. cros_ec_init_keyboard() will be called soon */
267 return input_stdio_register(&dev);