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.
29 #include <linux/module.h>
30 #include <linux/i2c.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/mfd/chromeos_ec.h>
34 #include <linux/notifier.h>
35 #include <linux/platform_device.h>
36 #include <linux/slab.h>
40 struct input_dev *idev;
41 struct chromeos_ec_device *ec;
42 struct notifier_block notifier;
43 struct work_struct work;
48 * The standard MKBP keyboard matrix table.
50 * These may become variables when we switch to the Device Tree. However, the
51 * code and the protocol assume that NUM_ROWS = 8 (one byte per column).
53 #define MKBP_NUM_ROWS 8
54 #define MKBP_NUM_COLS 13
56 /* We will read this table from the Device Tree when we have one. */
57 static uint16_t mkbp_keycodes[MKBP_NUM_ROWS][MKBP_NUM_COLS] = {
58 { 0x0, KEY_LEFTMETA, KEY_F1, KEY_B,
59 KEY_F10, 0x0, KEY_N, 0x0,
60 KEY_EQUAL, 0x0, KEY_RIGHTALT, 0x0,
62 { 0x0, KEY_ESC, KEY_F4, KEY_G,
63 KEY_F7, 0x0, KEY_H, 0x0,
64 KEY_APOSTROPHE,KEY_F9, 0x0, KEY_BACKSPACE,
66 { KEY_LEFTCTRL, KEY_TAB, KEY_F3, KEY_T,
67 KEY_F6, KEY_RIGHTBRACE, KEY_Y, 0x56,
68 KEY_LEFTBRACE,KEY_F8, 0x0, 0x0,
70 { 0x0, KEY_GRAVE, KEY_F2, KEY_5,
71 KEY_F5, 0x0, KEY_6, 0x0,
72 KEY_MINUS, 0x0, 0x0, KEY_BACKSLASH,
74 { KEY_RIGHTCTRL,KEY_A, KEY_D, KEY_F,
75 KEY_S, KEY_K, KEY_J, 0x0,
76 KEY_SEMICOLON,KEY_L, 0x0, KEY_ENTER,
78 { 0x0, KEY_Z, KEY_C, KEY_V,
79 KEY_X, KEY_COMMA, KEY_M, KEY_LEFTSHIFT,
80 KEY_SLASH, KEY_DOT, 0x0, KEY_SPACE,
82 { 0x0, KEY_1, KEY_3, KEY_4,
83 KEY_2, KEY_8, KEY_7, 0x0,
84 KEY_0, KEY_9, KEY_LEFTALT, KEY_DOWN,
86 { 0x0, KEY_Q, KEY_E, KEY_R,
87 KEY_W, KEY_I, KEY_U, KEY_RIGHTSHIFT,
88 KEY_P, KEY_O, 0x0, KEY_UP,
92 static uint8_t identity_keycodes[256];
95 * Sends a single key event to the input layer.
97 static inline void mkbp_send_key_event(struct mkbp_device *mkbp_dev,
98 int row, int col, int pressed)
100 struct input_dev *idev = mkbp_dev->idev;
101 int code = mkbp_keycodes[row][col];
103 input_report_key(idev, code, pressed);
107 * Returns true when there is at least one combination of pressed keys that
108 * results in ghosting.
110 static bool mkbp_has_ghosting(struct device *dev, uint8_t *buf)
114 int pressed_in_row[MKBP_NUM_ROWS];
115 int row_has_teeth[MKBP_NUM_ROWS];
117 memset(pressed_in_row, 0, sizeof(pressed_in_row));
118 memset(row_has_teeth, 0, sizeof(row_has_teeth));
120 * Ghosting happens if for any pressed key X there are other keys
121 * pressed both in the same row and column of X as, for instance,
122 * in the following diagram:
129 * In this case only X, Y, and Z are pressed, but g appears to be
130 * pressed too (see Wikipedia).
132 * We can detect ghosting in a single pass (*) over the keyboard state
133 * by maintaining two arrays. pressed_in_row counts how many pressed
134 * keys we have found in a row. row_has_teeth is true if any of the
135 * pressed keys for this row has other pressed keys in its column. If
136 * at any point of the scan we find that a row has multiple pressed
137 * keys, and at least one of them is at the intersection with a column
138 * with multiple pressed keys, we're sure there is ghosting.
139 * Conversely, if there is ghosting, we will detect such situation for
140 * at least one key during the pass.
142 * (*) This looks linear in the number of keys, but it's not. We can
143 * cheat because the number of rows is small.
145 for (row = 0; row < MKBP_NUM_ROWS; row++) {
147 for (col = 0; col < MKBP_NUM_COLS; col++) {
148 if (buf[col] & mask) {
149 pressed_in_row[row] += 1;
150 row_has_teeth[row] |= buf[col] & ~mask;
151 if (pressed_in_row[row] > 1 &&
152 row_has_teeth[row]) {
154 dev_dbg(dev, "ghost found at: r%d c%d,"
155 " pressed %d, teeth 0x%x\n",
156 row, col, pressed_in_row[row],
167 * mkbp_old_state[row][col] is 1 when the most recent (valid) communication
168 * with the keyboard indicated that the key at row/col was in the pressed
171 static uint8_t mkbp_old_state[MKBP_NUM_ROWS][MKBP_NUM_COLS];
174 * Compares the new keyboard state to the old one and produces key
175 * press/release events accordingly. The keyboard state is 13 bytes (one byte
178 static void mkbp_process(struct mkbp_device *mkbp_dev,
179 uint8_t *kb_state, int len)
187 if (mkbp_has_ghosting(mkbp_dev->dev, kb_state)) {
189 * Simple-minded solution: ignore this state. The obvious
190 * improvement is to only ignore changes to keys involved in
191 * the ghosting, but process the other changes.
193 dev_dbg(mkbp_dev->dev, "ghosting found\n");
197 for (col = 0; col < MKBP_NUM_COLS; col++) {
198 for (row = 0; row < MKBP_NUM_ROWS; row++) {
199 new_state = kb_state[col] & (1 << row);
200 if (new_state != mkbp_old_state[row][col]) {
201 dev_dbg(mkbp_dev->dev, "[r%d c%d]: byte %02x\n",
202 row, col, new_state);
204 if (new_state && !mkbp_old_state[row][col]) {
206 mkbp_send_key_event(mkbp_dev, row, col, 1);
207 mkbp_old_state[row][col] = 1;
208 } else if (!new_state && mkbp_old_state[row][col]) {
210 mkbp_send_key_event(mkbp_dev, row, col, 0);
211 mkbp_old_state[row][col] = 0;
215 input_sync(mkbp_dev->idev);
218 static int mkbp_open(struct input_dev *dev)
220 struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
222 return atomic_notifier_chain_register(&mkbp_dev->ec->event_notifier,
223 &mkbp_dev->notifier);
226 static void mkbp_close(struct input_dev *dev)
228 struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
230 atomic_notifier_chain_unregister(&mkbp_dev->ec->event_notifier,
231 &mkbp_dev->notifier);
234 static void mkbp_work(struct work_struct *work)
237 struct mkbp_device *mkbp_dev =
238 container_of(work, struct mkbp_device, work);
239 uint8_t kb_state[MKBP_NUM_COLS];
241 ret = mkbp_dev->ec->send_command(mkbp_dev->ec, MKBP_CMDC_KEY_STATE,
242 kb_state, MKBP_NUM_COLS);
244 mkbp_process(mkbp_dev, kb_state, ret);
247 static int mkbp_notify(struct notifier_block *nb,
248 unsigned long state, void *_notify)
250 struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
253 schedule_work(&mkbp_dev->work);
258 static int __devinit mkbp_probe(struct platform_device *pdev)
260 struct chromeos_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
261 struct device *dev = ec->dev;
262 struct mkbp_device *mkbp_dev = NULL;
263 struct input_dev *idev = NULL;
265 bool input_device_registered = false;
267 dev_dbg(dev, "probing\n");
269 mkbp_dev = kzalloc(sizeof(*mkbp_dev), GFP_KERNEL);
270 idev = input_allocate_device();
271 if (idev == NULL || mkbp_dev == NULL) {
273 dev_err(dev, "cannot allocate\n");
278 mkbp_dev->notifier.notifier_call = mkbp_notify;
281 INIT_WORK(&mkbp_dev->work, &mkbp_work);
283 idev->name = ec->client->name;
284 idev->phys = ec->client->adapter->name;
285 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
286 idev->keycode = identity_keycodes;
287 idev->keycodesize = sizeof(identity_keycodes[0]);
289 sizeof(identity_keycodes) / sizeof(identity_keycodes[0]);
290 for (i = 0; i < idev->keycodemax; i++) {
291 identity_keycodes[i] = i;
292 input_set_capability(idev, EV_KEY, i);
294 idev->id.bustype = BUS_I2C;
295 idev->id.version = 1;
296 idev->id.product = 0;
297 idev->dev.parent = &ec->client->dev;
298 idev->open = mkbp_open;
299 idev->close = mkbp_close;
301 input_set_drvdata(idev, mkbp_dev);
302 mkbp_dev->idev = idev;
303 err = input_register_device(mkbp_dev->idev);
305 dev_err(dev, "cannot register input device\n");
308 /* We have seen the mkbp work function scheduled as much as 300ms after
309 * the interrupt service routine is called. The default autorepeat
310 * delay is 250ms. This can lead to spurious autorepeat. A better fix
311 * would be to collect time stamps in the ISR, but for the moment a
312 * longer delay helps.
314 * Also note that we must change the delay after device registration,
315 * or else the input layer assumes that the driver does its own
316 * autorepeat. (Which we will probably have to do.)
318 mkbp_dev->idev->rep[REP_DELAY] = 600;
319 input_device_registered = true;
323 if (input_device_registered)
324 input_unregister_device(idev);
326 input_free_device(idev);
330 static struct platform_driver mkbp_driver = {
338 module_platform_driver(mkbp_driver);
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Matrix keyboard protocol driver");
342 MODULE_ALIAS("platform:mkbp");