2 * Chromium OS Matrix Keyboard
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <key_matrix.h>
14 #include <stdio_dev.h>
16 DECLARE_GLOBAL_DATA_PTR;
19 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
23 struct cros_ec_dev *dev; /* The CROS_EC device */
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 unsigned int repeat_delay_ms; /* Time before autorepeat starts */
29 unsigned int repeat_rate_ms; /* Autorepeat rate in ms */
30 int ghost_filter; /* 1 to enable ghost filter, else 0 */
31 int inited; /* 1 if keyboard is ready */
36 * Check the keyboard controller and return a list of key matrix positions
37 * for which a key is pressed
39 * @param config Keyboard config
40 * @param keys List of keys that we have detected
41 * @param max_count Maximum number of keys to return
42 * @return number of pressed keys, 0 for none
44 static int check_for_keys(struct keyb *config,
45 struct key_matrix_key *keys, int max_count)
47 struct key_matrix_key *key;
48 struct mbkp_keyscan scan;
49 unsigned int row, col, bit, data;
52 if (cros_ec_scan_keyboard(config->dev, &scan)) {
53 debug("%s: keyboard scan failed\n", __func__);
57 for (col = num_keys = bit = 0; col < config->matrix.num_cols;
59 for (row = 0; row < config->matrix.num_rows; row++) {
60 unsigned int mask = 1 << (bit & 7);
62 data = scan.data[bit / 8];
63 if ((data & mask) && num_keys < max_count) {
64 key = keys + num_keys++;
77 * Test if keys are available to be read
79 * @return 0 if no keys available, 1 if keys are available
81 static int kbd_tstc(void)
83 /* Just get input to do this for us */
84 return config.inited ? input_tstc(&config.input) : 0;
90 * @return ASCII key code, or 0 if no key, or -1 if error
92 static int kbd_getc(void)
94 /* Just get input to do this for us */
95 return config.inited ? input_getc(&config.input) : 0;
99 * Check the keyboard, and send any keys that are pressed.
101 * This is called by input_tstc() and input_getc() when they need more
104 * @param input Input configuration
105 * @return 1, to indicate that we have something to look at
107 int cros_ec_kbc_check(struct input_config *input)
109 static struct key_matrix_key last_keys[KBC_MAX_KEYS];
110 static int last_num_keys;
111 struct key_matrix_key keys[KBC_MAX_KEYS];
112 int keycodes[KBC_MAX_KEYS];
113 int num_keys, num_keycodes;
114 int irq_pending, sent;
117 * Loop until the EC has no more keyscan records, or we have
118 * received at least one character. This means we know that tstc()
119 * will always return non-zero if keys have been pressed.
121 * Without this loop, a key release (which generates no new ascii
122 * characters) will cause us to exit this function, and just tstc()
123 * may return 0 before all keys have been read from the EC.
126 irq_pending = cros_ec_interrupt_pending(config.dev);
128 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS);
129 last_num_keys = num_keys;
130 memcpy(last_keys, keys, sizeof(keys));
133 * EC doesn't want to be asked, so use keys from last
136 num_keys = last_num_keys;
137 memcpy(keys, last_keys, sizeof(keys));
142 num_keycodes = key_matrix_decode(&config.matrix, keys,
143 num_keys, keycodes, KBC_MAX_KEYS);
144 sent = input_send_keycodes(input, keycodes, num_keycodes);
145 } while (irq_pending && !sent);
151 * Decode MBKP keyboard details from the device tree
153 * @param blob Device tree blob
154 * @param node Node to decode from
155 * @param config Configuration data read from fdt
156 * @return 0 if ok, -1 on error
158 static int cros_ec_keyb_decode_fdt(const void *blob, int node,
162 * Get keyboard rows and columns - at present we are limited to
163 * 8 columns by the protocol (one byte per row scan)
165 config->key_rows = fdtdec_get_int(blob, node, "google,key-rows", 0);
166 config->key_cols = fdtdec_get_int(blob, node, "google,key-columns", 0);
167 if (!config->key_rows || !config->key_cols ||
168 config->key_rows * config->key_cols / 8
169 > CROS_EC_KEYSCAN_COLS) {
170 debug("%s: Invalid key matrix size %d x %d\n", __func__,
171 config->key_rows, config->key_cols);
174 config->repeat_delay_ms = fdtdec_get_int(blob, node,
175 "google,repeat-delay-ms", 0);
176 config->repeat_rate_ms = fdtdec_get_int(blob, node,
177 "google,repeat-rate-ms", 0);
178 config->ghost_filter = fdtdec_get_bool(blob, node,
179 "google,ghost-filter");
184 * Set up the keyboard. This is called by the stdio device handler.
186 * We want to do this init when the keyboard is actually used rather than
187 * at start-up, since keyboard input may not currently be selected.
189 * @return 0 if ok, -1 on error
191 static int cros_ec_init_keyboard(void)
193 const void *blob = gd->fdt_blob;
196 config.dev = board_get_cros_ec_dev();
198 debug("%s: no cros_ec device: cannot init keyboard\n",
202 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
204 debug("%s: Node not found\n", __func__);
207 if (cros_ec_keyb_decode_fdt(blob, node, &config))
209 input_set_delays(&config.input, config.repeat_delay_ms,
210 config.repeat_rate_ms);
211 if (key_matrix_init(&config.matrix, config.key_rows,
212 config.key_cols, config.ghost_filter)) {
213 debug("%s: cannot init key matrix\n", __func__);
216 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
217 debug("%s: Could not decode key matrix from fdt\n", __func__);
221 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
227 int drv_keyboard_init(void)
229 struct stdio_dev dev;
231 if (input_init(&config.input, 0)) {
232 debug("%s: Cannot set up input\n", __func__);
235 config.input.read_keys = cros_ec_kbc_check;
237 memset(&dev, '\0', sizeof(dev));
238 strcpy(dev.name, "cros-ec-keyb");
239 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
242 dev.start = cros_ec_init_keyboard;
244 /* Register the device. cros_ec_init_keyboard() will be called soon */
245 return input_stdio_register(&dev);