2 * Chromium OS Matrix Keyboard
4 * Copyright (c) 2012 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <key_matrix.h>
29 #include <stdio_dev.h>
31 DECLARE_GLOBAL_DATA_PTR;
34 KBC_MAX_KEYS = 8, /* Maximum keys held down at once */
38 struct cros_ec_dev *dev; /* The CROS_EC device */
39 struct input_config input; /* The input layer */
40 struct key_matrix matrix; /* The key matrix layer */
41 int key_rows; /* Number of keyboard rows */
42 int key_cols; /* Number of keyboard columns */
43 unsigned int repeat_delay_ms; /* Time before autorepeat starts */
44 unsigned int repeat_rate_ms; /* Autorepeat rate in ms */
45 int ghost_filter; /* 1 to enable ghost filter, else 0 */
46 int inited; /* 1 if keyboard is ready */
51 * Check the keyboard controller and return a list of key matrix positions
52 * for which a key is pressed
54 * @param config Keyboard config
55 * @param keys List of keys that we have detected
56 * @param max_count Maximum number of keys to return
57 * @return number of pressed keys, 0 for none
59 static int check_for_keys(struct keyb *config,
60 struct key_matrix_key *keys, int max_count)
62 struct key_matrix_key *key;
63 struct mbkp_keyscan scan;
64 unsigned int row, col, bit, data;
67 if (cros_ec_scan_keyboard(config->dev, &scan)) {
68 debug("%s: keyboard scan failed\n", __func__);
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(void)
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(void)
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;
132 * Loop until the EC has no more keyscan records, or we have
133 * received at least one character. This means we know that tstc()
134 * will always return non-zero if keys have been pressed.
136 * Without this loop, a key release (which generates no new ascii
137 * characters) will cause us to exit this function, and just tstc()
138 * may return 0 before all keys have been read from the EC.
141 irq_pending = cros_ec_interrupt_pending(config.dev);
143 num_keys = check_for_keys(&config, keys, KBC_MAX_KEYS);
144 last_num_keys = num_keys;
145 memcpy(last_keys, keys, sizeof(keys));
148 * EC doesn't want to be asked, so use keys from last
151 num_keys = last_num_keys;
152 memcpy(keys, last_keys, sizeof(keys));
157 num_keycodes = key_matrix_decode(&config.matrix, keys,
158 num_keys, keycodes, KBC_MAX_KEYS);
159 sent = input_send_keycodes(input, keycodes, num_keycodes);
160 } while (irq_pending && !sent);
166 * Decode MBKP keyboard details from the device tree
168 * @param blob Device tree blob
169 * @param node Node to decode from
170 * @param config Configuration data read from fdt
171 * @return 0 if ok, -1 on error
173 static int cros_ec_keyb_decode_fdt(const void *blob, int node,
177 * Get keyboard rows and columns - at present we are limited to
178 * 8 columns by the protocol (one byte per row scan)
180 config->key_rows = fdtdec_get_int(blob, node, "google,key-rows", 0);
181 config->key_cols = fdtdec_get_int(blob, node, "google,key-columns", 0);
182 if (!config->key_rows || !config->key_cols ||
183 config->key_rows * config->key_cols / 8
184 > CROS_EC_KEYSCAN_COLS) {
185 debug("%s: Invalid key matrix size %d x %d\n", __func__,
186 config->key_rows, config->key_cols);
189 config->repeat_delay_ms = fdtdec_get_int(blob, node,
190 "google,repeat-delay-ms", 0);
191 config->repeat_rate_ms = fdtdec_get_int(blob, node,
192 "google,repeat-rate-ms", 0);
193 config->ghost_filter = fdtdec_get_bool(blob, node,
194 "google,ghost-filter");
199 * Set up the keyboard. This is called by the stdio device handler.
201 * We want to do this init when the keyboard is actually used rather than
202 * at start-up, since keyboard input may not currently be selected.
204 * @return 0 if ok, -1 on error
206 static int cros_ec_init_keyboard(void)
208 const void *blob = gd->fdt_blob;
211 config.dev = board_get_cros_ec_dev();
213 debug("%s: no cros_ec device: cannot init keyboard\n",
217 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
219 debug("%s: Node not found\n", __func__);
222 if (cros_ec_keyb_decode_fdt(blob, node, &config))
224 input_set_delays(&config.input, config.repeat_delay_ms,
225 config.repeat_rate_ms);
226 if (key_matrix_init(&config.matrix, config.key_rows,
227 config.key_cols, config.ghost_filter)) {
228 debug("%s: cannot init key matrix\n", __func__);
231 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
232 debug("%s: Could not decode key matrix from fdt\n", __func__);
236 debug("%s: Matrix keyboard %dx%d ready\n", __func__, config.key_rows,
242 int drv_keyboard_init(void)
244 struct stdio_dev dev;
246 if (input_init(&config.input, 0)) {
247 debug("%s: Cannot set up input\n", __func__);
250 config.input.read_keys = cros_ec_kbc_check;
252 memset(&dev, '\0', sizeof(dev));
253 strcpy(dev.name, "cros-ec-keyb");
254 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
257 dev.start = cros_ec_init_keyboard;
259 /* Register the device. cros_ec_init_keyboard() will be called soon */
260 return input_stdio_register(&dev);