2 * mkbp.c - keyboard driver for Matrix KeyBoard Protocol keyboards.
4 * Copyright (C) 2012 Google, Inc
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * The MKBP (matrix keyboard protocol) is a message-based protocol for
22 * communicating the keyboard state (which keys are pressed) from a keyboard EC
23 * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
24 * but everything else (including deghosting) is done here. The main
25 * motivation for this is to keep the EC firmware as simple as possible, since
26 * it cannot be easily upgraded and EC flash/IRAM space is relatively
30 #include <linux/module.h>
31 #include <linux/i2c.h>
32 #include <linux/input.h>
33 #include <linux/kernel.h>
34 #include <linux/mfd/chromeos_ec.h>
35 #include <linux/mfd/chromeos_ec_commands.h>
36 #include <linux/notifier.h>
37 #include <linux/platform_device.h>
38 #include <linux/power_supply.h>
39 #include <linux/slab.h>
42 * The standard MKBP keyboard matrix table.
44 * These may become variables when we switch to the Device Tree. However, the
45 * code and the protocol assume that NUM_ROWS = 8 (one byte per column).
47 #define MKBP_NUM_ROWS 8
48 #define MKBP_NUM_COLS 13
52 struct input_dev *idev;
53 struct chromeos_ec_device *ec;
54 struct notifier_block notifier;
55 struct notifier_block wake_notifier;
56 uint8_t valid_keys[MKBP_NUM_COLS];
59 /* We will read this table from the Device Tree when we have one. */
60 static uint16_t mkbp_keycodes[MKBP_NUM_ROWS][MKBP_NUM_COLS] = {
61 { 0x0, KEY_LEFTMETA, KEY_F1, KEY_B,
62 KEY_F10, 0x0, KEY_N, 0x0,
63 KEY_EQUAL, 0x0, KEY_RIGHTALT, 0x0,
65 { 0x0, KEY_ESC, KEY_F4, KEY_G,
66 KEY_F7, 0x0, KEY_H, 0x0,
67 KEY_APOSTROPHE,KEY_F9, 0x0, KEY_BACKSPACE,
69 { KEY_LEFTCTRL, KEY_TAB, KEY_F3, KEY_T,
70 KEY_F6, KEY_RIGHTBRACE, KEY_Y, KEY_102ND,
71 KEY_LEFTBRACE,KEY_F8, 0x0, 0x0,
73 { 0x0, KEY_GRAVE, KEY_F2, KEY_5,
74 KEY_F5, 0x0, KEY_6, 0x0,
75 KEY_MINUS, 0x0, 0x0, KEY_BACKSLASH,
77 { KEY_RIGHTCTRL,KEY_A, KEY_D, KEY_F,
78 KEY_S, KEY_K, KEY_J, 0x0,
79 KEY_SEMICOLON,KEY_L, KEY_BACKSLASH, KEY_ENTER,
81 { 0x0, KEY_Z, KEY_C, KEY_V,
82 KEY_X, KEY_COMMA, KEY_M, KEY_LEFTSHIFT,
83 KEY_SLASH, KEY_DOT, 0x0, KEY_SPACE,
85 { 0x0, KEY_1, KEY_3, KEY_4,
86 KEY_2, KEY_8, KEY_7, 0x0,
87 KEY_0, KEY_9, KEY_LEFTALT, KEY_DOWN,
89 { KEY_BATTERY, KEY_Q, KEY_E, KEY_R,
90 KEY_W, KEY_I, KEY_U, KEY_RIGHTSHIFT,
91 KEY_P, KEY_O, 0x0, KEY_UP,
95 static uint8_t identity_keycodes[256];
98 * Sends a single key event to the input layer.
100 static inline void mkbp_send_key_event(struct mkbp_device *mkbp_dev,
101 int row, int col, int pressed)
103 struct input_dev *idev = mkbp_dev->idev;
104 int code = mkbp_keycodes[row][col];
106 /* This key signifies a change to power supply status */
107 if (code == KEY_BATTERY) {
108 if (mkbp_dev->ec->charger)
109 power_supply_changed(mkbp_dev->ec->charger);
113 input_report_key(idev, code, pressed);
117 * Returns true when there is at least one combination of pressed keys that
118 * results in ghosting.
120 static bool mkbp_has_ghosting(struct mkbp_device *mkbp_dev, uint8_t *buf)
123 int mask, mask_corner;
124 int pressed_in_row[MKBP_NUM_ROWS];
125 int pressed_in_col[MKBP_NUM_COLS];
126 struct device *dev = mkbp_dev->dev;
127 uint8_t *valid_keys = mkbp_dev->valid_keys;
134 int col_corner, row_corner;
136 memset(pressed_in_row, 0, sizeof(pressed_in_row));
137 memset(pressed_in_col, 0, sizeof(pressed_in_col));
139 * Ghosting happens if for any pressed key X there are other keys
141 * pressed both in the same row and column of X as, for instance,
142 * in the following diagram:
149 * In this case only X, Y, and Z are pressed, but g appears to be
150 * pressed too (see Wikipedia).
152 for (row = 0; row < MKBP_NUM_ROWS; row++) {
154 for (col = 0; col < MKBP_NUM_COLS; col++) {
155 if (mask & buf[col] & valid_keys[col]) {
156 pressed_in_row[row] += 1;
157 pressed_in_col[col] += 1;
158 if (pressed_in_col[col] > max_in_col)
159 max_in_col = pressed_in_col[col];
162 if (pressed_in_row[row] > max_in_row)
163 max_in_row = pressed_in_row[row];
166 if (max_in_col < 2 || max_in_row < 2)
169 /* Find possible ghosting locations. These are the corners of the L's.
170 * We know there is at least one L (i.e. one point whose row has at
171 * least two keys ON and whose column has at least two keys ON).
173 for (row = 0; row < MKBP_NUM_ROWS; row++) {
175 if (pressed_in_row[row] < 2)
177 for (col = 0; col < MKBP_NUM_COLS; col++) {
178 if (pressed_in_col[col] < 2)
180 if (buf[col] & mask & valid_keys[col]) {
181 corners[n_corners].row = row;
182 corners[n_corners].col = col;
184 if (n_corners == sizeof(corners) /
185 sizeof(corners[0])) {
187 dev_dbg(dev, "too many corners!");
193 /* Examine all corners for possible ghosting. */
194 for (n_corners--; n_corners >= 0; n_corners--) {
195 row_corner = corners[n_corners].row;
196 col_corner = corners[n_corners].col;
197 mask_corner = 1 << row_corner;
198 /* Find the other bits in this column. */
199 for (row = 0; row < MKBP_NUM_ROWS; row++) {
200 if (row == row_corner)
201 /* Skip the corner. */
204 if (!(buf[col_corner] & mask & valid_keys[col_corner]))
207 /* [row, col_corner] is ON. Find the other bits in
210 for (col = 0; col < MKBP_NUM_COLS; col++) {
211 if (col == col_corner)
212 /* Skip the corner. */
214 if (!(buf[col] & mask_corner))
217 /* If we get here, [row_corner, col] is ON,
218 * therefore [row, col] is the possible
219 * ghosting location (diagonally opposite). If
220 * that key is wired, we have ghosting.
222 if (valid_keys[col] & mask) {
223 dev_dbg(dev, "ghost found at: r%d c%d,"
224 " corners: r:0x%x c:0x%x\n",
225 row, col, row_corner,
236 * mkbp_old_state[row][col] is 1 when the most recent (valid) communication
237 * with the keyboard indicated that the key at row/col was in the pressed
240 static uint8_t mkbp_old_state[MKBP_NUM_ROWS][MKBP_NUM_COLS];
243 * Compares the new keyboard state to the old one and produces key
244 * press/release events accordingly. The keyboard state is 13 bytes (one byte
247 static void mkbp_process(struct mkbp_device *mkbp_dev,
248 uint8_t *kb_state, int len)
256 if (mkbp_has_ghosting(mkbp_dev, kb_state)) {
258 * Simple-minded solution: ignore this state. The obvious
259 * improvement is to only ignore changes to keys involved in
260 * the ghosting, but process the other changes.
262 dev_dbg(mkbp_dev->dev, "ghosting found\n");
266 for (col = 0; col < MKBP_NUM_COLS; col++) {
267 for (row = 0; row < MKBP_NUM_ROWS; row++) {
268 new_state = kb_state[col] & (1 << row);
269 if (!!new_state != mkbp_old_state[row][col]) {
270 dev_dbg(mkbp_dev->dev,
271 "changed: [r%d c%d]: byte %02x\n",
272 row, col, new_state);
274 if (new_state && !mkbp_old_state[row][col]) {
276 mkbp_send_key_event(mkbp_dev, row, col, 1);
277 mkbp_old_state[row][col] = 1;
278 } else if (!new_state && mkbp_old_state[row][col]) {
280 mkbp_send_key_event(mkbp_dev, row, col, 0);
281 mkbp_old_state[row][col] = 0;
285 input_sync(mkbp_dev->idev);
288 static int mkbp_open(struct input_dev *dev)
290 struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
293 ret = blocking_notifier_chain_register(&mkbp_dev->ec->event_notifier,
294 &mkbp_dev->notifier);
297 ret = blocking_notifier_chain_register(&mkbp_dev->ec->wake_notifier,
298 &mkbp_dev->wake_notifier);
300 blocking_notifier_chain_unregister(
301 &mkbp_dev->ec->event_notifier, &mkbp_dev->notifier);
308 static void mkbp_close(struct input_dev *dev)
310 struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
312 blocking_notifier_chain_unregister(&mkbp_dev->ec->event_notifier,
313 &mkbp_dev->notifier);
314 blocking_notifier_chain_unregister(&mkbp_dev->ec->wake_notifier,
315 &mkbp_dev->wake_notifier);
318 static int mkbp_get_state(struct mkbp_device *mkbp_dev, uint8_t *kb_state)
320 return mkbp_dev->ec->command_recv(mkbp_dev->ec, EC_CMD_MKBP_STATE,
321 kb_state, MKBP_NUM_COLS);
324 static int mkbp_work(struct notifier_block *nb,
325 unsigned long state, void *_notify)
328 struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
330 uint8_t kb_state[MKBP_NUM_COLS];
332 ret = mkbp_get_state(mkbp_dev, kb_state);
334 mkbp_process(mkbp_dev, kb_state, ret);
339 /* On resume, clear any keys in the buffer, crosbug.com/p/14523 */
340 static int mkbp_clear_keyboard(struct notifier_block *nb,
341 unsigned long state, void *_notify)
343 struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
345 uint8_t old_state[MKBP_NUM_COLS];
346 uint8_t new_state[MKBP_NUM_COLS];
347 unsigned long duration;
351 * Keep reading until we see that the scan state does not change.
352 * That indicates that we are done.
354 * Assume that the EC keyscan buffer is at most 32 deep.
356 * TODO(sjg@chromium.org): Add EC command to clear keyscan FIFO.
359 ret = mkbp_get_state(mkbp_dev, new_state);
360 for (i = 1; !ret && i < 32; i++) {
361 memcpy(old_state, new_state, sizeof(old_state));
362 ret = mkbp_get_state(mkbp_dev, new_state);
363 if (0 == memcmp(old_state, new_state, sizeof(old_state)))
366 duration = jiffies - duration;
367 dev_info(mkbp_dev->dev, "Discarded %d keyscan(s) in %dus\n", i,
368 jiffies_to_usecs(duration));
374 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
375 * ghosting logic to ignore NULL or virtual keys.
377 static void __devinit mkbp_compute_valid_keys(struct mkbp_device *mkbp_dev)
382 BUILD_BUG_ON(MKBP_NUM_ROWS > sizeof(mkbp_dev->valid_keys));
384 for (col = 0; col < MKBP_NUM_COLS; col++) {
385 for (row = 0; row < MKBP_NUM_ROWS; row++) {
386 code = mkbp_keycodes[row][col];
388 mkbp_dev->valid_keys[col] |= 1 << row;
390 dev_dbg(mkbp_dev->dev, "valid_keys[%02d] = 0x%02x\n",
391 col, mkbp_dev->valid_keys[col]);
395 static int __devinit mkbp_probe(struct platform_device *pdev)
397 struct chromeos_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
398 struct device *dev = ec->dev;
399 struct mkbp_device *mkbp_dev = NULL;
400 struct input_dev *idev = NULL;
402 bool input_device_registered = false;
404 dev_dbg(dev, "probing\n");
406 mkbp_dev = kzalloc(sizeof(*mkbp_dev), GFP_KERNEL);
407 idev = input_allocate_device();
408 if (idev == NULL || mkbp_dev == NULL) {
410 dev_err(dev, "cannot allocate\n");
415 mkbp_dev->notifier.notifier_call = mkbp_work;
416 mkbp_dev->wake_notifier.notifier_call = mkbp_clear_keyboard;
418 mkbp_compute_valid_keys(mkbp_dev);
420 idev->name = ec->get_name(ec);
421 idev->phys = ec->get_phys_name(ec);
422 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
423 idev->keycode = identity_keycodes;
424 idev->keycodesize = sizeof(identity_keycodes[0]);
426 sizeof(identity_keycodes) / sizeof(identity_keycodes[0]);
427 for (i = 0; i < idev->keycodemax; i++) {
428 identity_keycodes[i] = i;
429 input_set_capability(idev, EV_KEY, i);
432 /* TODO(sjg@chromium.org): This could be SPI or LPC */
433 idev->id.bustype = BUS_I2C;
434 idev->id.version = 1;
435 idev->id.product = 0;
436 idev->dev.parent = ec->get_parent(ec);
437 idev->open = mkbp_open;
438 idev->close = mkbp_close;
440 input_set_drvdata(idev, mkbp_dev);
441 mkbp_dev->idev = idev;
442 err = input_register_device(mkbp_dev->idev);
444 dev_err(dev, "cannot register input device\n");
447 /* We have seen the mkbp work function scheduled as much as 300ms after
448 * the interrupt service routine is called. The default autorepeat
449 * delay is 250ms. This can lead to spurious autorepeat. A better fix
450 * would be to collect time stamps in the ISR, but for the moment a
451 * longer delay helps.
453 * Also note that we must change the delay after device registration,
454 * or else the input layer assumes that the driver does its own
455 * autorepeat. (Which we will probably have to do.)
457 mkbp_dev->idev->rep[REP_DELAY] = 600;
458 input_device_registered = true;
460 dev_info(dev, "MKBP Keyboard ready\n");
464 if (input_device_registered)
465 input_unregister_device(idev);
467 input_free_device(idev);
471 static struct platform_driver mkbp_driver = {
479 module_platform_driver(mkbp_driver);
481 MODULE_LICENSE("GPL");
482 MODULE_DESCRIPTION("Matrix keyboard protocol driver");
483 MODULE_ALIAS("platform:mkbp");