WORKAROUND: mkbp: Remove KEY_BATTERY from valid keys.
[cascardo/linux.git] / drivers / input / keyboard / mkbp.c
1 /*
2  *  mkbp.c - keyboard driver for Matrix KeyBoard Protocol keyboards.
3  *
4  *  Copyright (C) 2012 Google, Inc
5  *
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.
10  *
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.
15  *
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
19  *
20  *
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
27  * expensive.
28  */
29
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>
40
41 /*
42  * The standard MKBP keyboard matrix table.
43  *
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).
46  */
47 #define MKBP_NUM_ROWS 8
48 #define MKBP_NUM_COLS 13
49
50 struct mkbp_device {
51         struct device *dev;
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];
57 };
58
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,
64           0x0 },
65         { 0x0,          KEY_ESC,        KEY_F4,         KEY_G,
66           KEY_F7,       0x0,            KEY_H,          0x0,
67           KEY_APOSTROPHE,KEY_F9,        0x0,            KEY_BACKSPACE,
68           0x0 },
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,
72           0x0 },
73         { 0x0,          KEY_GRAVE,      KEY_F2,         KEY_5,
74           KEY_F5,       0x0,            KEY_6,          0x0,
75           KEY_MINUS,    0x0,            0x0,            KEY_BACKSLASH,
76           0x0 },
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,
80           0x0 },
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,
84           0x0 },
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,
88           KEY_RIGHT },
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,
92           KEY_LEFT }
93 };
94
95 static uint8_t identity_keycodes[256];
96
97 /*
98  * Sends a single key event to the input layer.
99  */
100 static inline void mkbp_send_key_event(struct mkbp_device *mkbp_dev,
101                                 int row, int col, int pressed)
102 {
103         struct input_dev *idev = mkbp_dev->idev;
104         int code = mkbp_keycodes[row][col];
105
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);
110                 return;
111         }
112
113         input_report_key(idev, code, pressed);
114 }
115
116 /*
117  * Returns true when there is at least one combination of pressed keys that
118  * results in ghosting.
119  */
120 static bool mkbp_has_ghosting(struct mkbp_device *mkbp_dev, uint8_t *buf)
121 {
122         int col, row;
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;
128         int max_in_row = 0;
129         int max_in_col = 0;
130         int n_corners = 0;
131         struct {
132                 int row, col;
133         } corners[10];
134         int col_corner, row_corner;
135
136         memset(pressed_in_row, 0, sizeof(pressed_in_row));
137         memset(pressed_in_col, 0, sizeof(pressed_in_col));
138         /*
139          * Ghosting happens if for any pressed key X there are other keys
140
141          * pressed both in the same row and column of X as, for instance,
142          * in the following diagram:
143          *
144          * . . Y . g .
145          * . . . . . .
146          * . . . . . .
147          * . . X . Z .
148          *
149          * In this case only X, Y, and Z are pressed, but g appears to be
150          * pressed too (see Wikipedia).
151          */
152         for (row = 0; row < MKBP_NUM_ROWS; row++) {
153                 mask = 1 << 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];
160                         }
161                 }
162                 if (pressed_in_row[row] > max_in_row)
163                         max_in_row = pressed_in_row[row];
164         }
165
166         if (max_in_col < 2 || max_in_row < 2)
167                 return false;
168
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).
172         */
173         for (row = 0; row < MKBP_NUM_ROWS; row++) {
174                 mask = 1 << row;
175                 if (pressed_in_row[row] < 2)
176                         continue;
177                 for (col = 0; col < MKBP_NUM_COLS; col++) {
178                         if (pressed_in_col[col] < 2)
179                                 continue;
180                         if (buf[col] & mask & valid_keys[col]) {
181                                 corners[n_corners].row = row;
182                                 corners[n_corners].col = col;
183                                 n_corners++;
184                                 if (n_corners == sizeof(corners) /
185                                     sizeof(corners[0])) {
186                                         /* give up */
187                                         dev_dbg(dev, "too many corners!");
188                                         return true;
189                                 }
190                         }
191                 }
192         }
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. */
202                                 continue;
203                         mask = 1 << row;
204                         if (!(buf[col_corner] & mask & valid_keys[col_corner]))
205                                 /* Key is OFF */
206                                 continue;
207                         /* [row, col_corner] is ON.  Find the other bits in
208                          * row_corner.
209                          */
210                         for (col = 0; col < MKBP_NUM_COLS; col++) {
211                                 if (col == col_corner)
212                                         /* Skip the corner. */
213                                         continue;
214                                 if (!(buf[col] & mask_corner))
215                                         /* Key is OFF. */
216                                         continue;
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.
221                                  */
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,
226                                                 col_corner);
227                                         return true;
228                                 }
229                         }
230                 }
231         }
232         return false;
233 }
234
235 /*
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
238  * state.
239  */
240 static uint8_t mkbp_old_state[MKBP_NUM_ROWS][MKBP_NUM_COLS];
241
242 /*
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
245  * per column)
246  */
247 static void mkbp_process(struct mkbp_device *mkbp_dev,
248                          uint8_t *kb_state, int len)
249 {
250         int col, row;
251         int new_state;
252         int num_cols;
253
254         num_cols = len;
255
256         if (mkbp_has_ghosting(mkbp_dev, kb_state)) {
257                 /*
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.
261                  */
262                 dev_dbg(mkbp_dev->dev, "ghosting found\n");
263                 return;
264         }
265
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);
273                         }
274                         if (new_state && !mkbp_old_state[row][col]) {
275                                 /* key press */
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]) {
279                                 /* key release */
280                                 mkbp_send_key_event(mkbp_dev, row, col, 0);
281                                 mkbp_old_state[row][col] = 0;
282                         }
283                 }
284         }
285         input_sync(mkbp_dev->idev);
286 }
287
288 static int mkbp_open(struct input_dev *dev)
289 {
290         struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
291         int ret;
292
293         ret = blocking_notifier_chain_register(&mkbp_dev->ec->event_notifier,
294                                                 &mkbp_dev->notifier);
295         if (ret)
296                 return ret;
297         ret = blocking_notifier_chain_register(&mkbp_dev->ec->wake_notifier,
298                                                 &mkbp_dev->wake_notifier);
299         if (ret) {
300                 blocking_notifier_chain_unregister(
301                         &mkbp_dev->ec->event_notifier, &mkbp_dev->notifier);
302                 return ret;
303         }
304
305         return 0;
306 }
307
308 static void mkbp_close(struct input_dev *dev)
309 {
310         struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
311
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);
316 }
317
318 static int mkbp_get_state(struct mkbp_device *mkbp_dev, uint8_t *kb_state)
319 {
320         return mkbp_dev->ec->command_recv(mkbp_dev->ec, EC_CMD_MKBP_STATE,
321                                           kb_state, MKBP_NUM_COLS);
322 }
323
324 static int mkbp_work(struct notifier_block *nb,
325                      unsigned long state, void *_notify)
326 {
327         int ret;
328         struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
329                                                     notifier);
330         uint8_t kb_state[MKBP_NUM_COLS];
331
332         ret = mkbp_get_state(mkbp_dev, kb_state);
333         if (ret >= 0)
334                 mkbp_process(mkbp_dev, kb_state, ret);
335
336         return NOTIFY_DONE;
337 }
338
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)
342 {
343         struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
344                                                     wake_notifier);
345         uint8_t old_state[MKBP_NUM_COLS];
346         uint8_t new_state[MKBP_NUM_COLS];
347         unsigned long duration;
348         int i, ret;
349
350         /*
351          * Keep reading until we see that the scan state does not change.
352          * That indicates that we are done.
353          *
354          * Assume that the EC keyscan buffer is at most 32 deep.
355          *
356          * TODO(sjg@chromium.org): Add EC command to clear keyscan FIFO.
357          */
358         duration = jiffies;
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)))
364                         break;
365         }
366         duration = jiffies - duration;
367         dev_info(mkbp_dev->dev, "Discarded %d keyscan(s) in %dus\n", i,
368                 jiffies_to_usecs(duration));
369
370         return 0;
371 }
372
373 /*
374  * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
375  * ghosting logic to ignore NULL or virtual keys.
376  */
377 static void __devinit mkbp_compute_valid_keys(struct mkbp_device *mkbp_dev)
378 {
379         int row, col;
380         uint16_t code;
381
382         BUILD_BUG_ON(MKBP_NUM_ROWS > sizeof(mkbp_dev->valid_keys));
383
384         for (col = 0; col < MKBP_NUM_COLS; col++) {
385                 for (row = 0; row < MKBP_NUM_ROWS; row++) {
386                         code = mkbp_keycodes[row][col];
387                         if (code && (code != KEY_BATTERY))
388                                 mkbp_dev->valid_keys[col] |= 1 << row;
389                 }
390                 dev_dbg(mkbp_dev->dev, "valid_keys[%02d] = 0x%02x\n",
391                         col, mkbp_dev->valid_keys[col]);
392         }
393 }
394
395 static int __devinit mkbp_probe(struct platform_device *pdev)
396 {
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;
401         int i, err;
402         bool input_device_registered = false;
403
404         dev_dbg(dev, "probing\n");
405
406         mkbp_dev = kzalloc(sizeof(*mkbp_dev), GFP_KERNEL);
407         idev = input_allocate_device();
408         if (idev == NULL || mkbp_dev == NULL) {
409                 err = -ENOMEM;
410                 dev_err(dev, "cannot allocate\n");
411                 goto fail;
412         }
413
414         mkbp_dev->ec = ec;
415         mkbp_dev->notifier.notifier_call = mkbp_work;
416         mkbp_dev->wake_notifier.notifier_call = mkbp_clear_keyboard;
417         mkbp_dev->dev = dev;
418         mkbp_compute_valid_keys(mkbp_dev);
419
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]);
425         idev->keycodemax =
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);
430         }
431
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;
439
440         input_set_drvdata(idev, mkbp_dev);
441         mkbp_dev->idev = idev;
442         err = input_register_device(mkbp_dev->idev);
443         if (err) {
444                 dev_err(dev, "cannot register input device\n");
445                 goto fail;
446         }
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.
452          *
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.)
456          */
457         mkbp_dev->idev->rep[REP_DELAY] = 600;
458         input_device_registered = true;
459
460         dev_info(dev, "MKBP Keyboard ready\n");
461
462         return err;
463 fail:
464         if (input_device_registered)
465                 input_unregister_device(idev);
466         kfree(mkbp_dev);
467         input_free_device(idev);
468         return err;
469 }
470
471 static struct platform_driver mkbp_driver = {
472         .probe = mkbp_probe,
473         .driver = {
474                 .name = "mkbp",
475         },
476 };
477
478
479 module_platform_driver(mkbp_driver);
480
481 MODULE_LICENSE("GPL");
482 MODULE_DESCRIPTION("Matrix keyboard protocol driver");
483 MODULE_ALIAS("platform:mkbp");