CHROMIUM: mkbp: add keyboard driver
[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.
27  */
28
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>
37
38 struct mkbp_device {
39         struct device *dev;
40         struct input_dev *idev;
41         struct chromeos_ec_device *ec;
42         struct notifier_block notifier;
43         struct work_struct work;
44 };
45
46
47 /*
48  * The standard MKBP keyboard matrix table.
49  *
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).
52  */
53 #define MKBP_NUM_ROWS 8
54 #define MKBP_NUM_COLS 13
55
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,
61           0x0 },
62         { 0x0,          KEY_ESC,        KEY_F4,         KEY_G,
63           KEY_F7,       0x0,            KEY_H,          0x0,
64           KEY_APOSTROPHE,KEY_F9,        0x0,            KEY_BACKSPACE,
65           0x0 },
66         { KEY_LEFTCTRL, KEY_TAB,        KEY_F3,         KEY_T,
67           KEY_F6,       KEY_RIGHTBRACE, KEY_Y,          0x56,
68           KEY_LEFTBRACE,KEY_F8,         0x0,            0x0,
69           0x0 },
70         { 0x0,          KEY_GRAVE,      KEY_F2,         KEY_5,
71           KEY_F5,       0x0,            KEY_6,          0x0,
72           KEY_MINUS,    0x0,            0x0,            KEY_BACKSLASH,
73           0x0 },
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,
77           0x0 },
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,
81           0x0 },
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,
85           KEY_RIGHT },
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,
89           KEY_LEFT }
90 };
91
92 static uint8_t identity_keycodes[256];
93
94 /*
95  * Sends a single key event to the input layer.
96  */
97 static inline void mkbp_send_key_event(struct mkbp_device *mkbp_dev,
98                                 int row, int col, int pressed)
99 {
100         struct input_dev *idev = mkbp_dev->idev;
101         int code = mkbp_keycodes[row][col];
102
103         input_report_key(idev, code, pressed);
104 }
105
106 /*
107  * Returns true when there is at least one combination of pressed keys that
108  * results in ghosting.
109  */
110 static bool mkbp_has_ghosting(struct device *dev, uint8_t *buf)
111 {
112         int col, row;
113         int mask;
114         int pressed_in_row[MKBP_NUM_ROWS];
115         int row_has_teeth[MKBP_NUM_ROWS];
116
117         memset(pressed_in_row, 0, sizeof(pressed_in_row));
118         memset(row_has_teeth, 0, sizeof(row_has_teeth));
119         /*
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:
123          *
124          * . . Y . g .
125          * . . . . . .
126          * . . . . . .
127          * . . X . Z .
128          *
129          * In this case only X, Y, and Z are pressed, but g appears to be
130          * pressed too (see Wikipedia).
131          *
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.
141          *
142          * (*) This looks linear in the number of keys, but it's not.  We can
143          * cheat because the number of rows is small.
144          */
145         for (row = 0; row < MKBP_NUM_ROWS; row++) {
146                 mask = 1 << 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]) {
153                                         /* ghosting */
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],
157                                                 row_has_teeth[row]);
158                                         return true;
159                                 }
160                         }
161                 }
162         }
163         return false;
164 }
165
166 /*
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
169  * state.
170  */
171 static uint8_t mkbp_old_state[MKBP_NUM_ROWS][MKBP_NUM_COLS];
172
173 /*
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
176  * per column)
177  */
178 static void mkbp_process(struct mkbp_device *mkbp_dev,
179                          uint8_t *kb_state, int len)
180 {
181         int col, row;
182         int new_state;
183         int num_cols;
184
185         num_cols = len;
186
187         if (mkbp_has_ghosting(mkbp_dev->dev, kb_state)) {
188                 /*
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.
192                  */
193                 dev_dbg(mkbp_dev->dev, "ghosting found\n");
194                 return;
195         }
196
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);
203                         }
204                         if (new_state && !mkbp_old_state[row][col]) {
205                                 /* key press */
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]) {
209                                 /* key release */
210                                 mkbp_send_key_event(mkbp_dev, row, col, 0);
211                                 mkbp_old_state[row][col] = 0;
212                         }
213                 }
214         }
215         input_sync(mkbp_dev->idev);
216 }
217
218 static int mkbp_open(struct input_dev *dev)
219 {
220         struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
221
222         return atomic_notifier_chain_register(&mkbp_dev->ec->event_notifier,
223                                               &mkbp_dev->notifier);
224 }
225
226 static void mkbp_close(struct input_dev *dev)
227 {
228         struct mkbp_device *mkbp_dev = input_get_drvdata(dev);
229
230         atomic_notifier_chain_unregister(&mkbp_dev->ec->event_notifier,
231                                          &mkbp_dev->notifier);
232 }
233
234 static void mkbp_work(struct work_struct *work)
235 {
236         int ret;
237         struct mkbp_device *mkbp_dev =
238                         container_of(work, struct mkbp_device, work);
239         uint8_t kb_state[MKBP_NUM_COLS];
240
241         ret = mkbp_dev->ec->send_command(mkbp_dev->ec, MKBP_CMDC_KEY_STATE,
242                                     kb_state, MKBP_NUM_COLS);
243         if (ret >= 0)
244                 mkbp_process(mkbp_dev, kb_state, ret);
245 }
246
247 static int mkbp_notify(struct notifier_block *nb,
248                             unsigned long state, void *_notify)
249 {
250         struct mkbp_device *mkbp_dev = container_of(nb, struct mkbp_device,
251                                                     notifier);
252
253         schedule_work(&mkbp_dev->work);
254
255         return NOTIFY_DONE;
256 }
257
258 static int __devinit mkbp_probe(struct platform_device *pdev)
259 {
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;
264         int i, err;
265         bool input_device_registered = false;
266
267         dev_dbg(dev, "probing\n");
268
269         mkbp_dev = kzalloc(sizeof(*mkbp_dev), GFP_KERNEL);
270         idev = input_allocate_device();
271         if (idev == NULL || mkbp_dev == NULL) {
272                 err = -ENOMEM;
273                 dev_err(dev, "cannot allocate\n");
274                 goto fail;
275         }
276
277         mkbp_dev->ec = ec;
278         mkbp_dev->notifier.notifier_call = mkbp_notify;
279         mkbp_dev->dev = dev;
280
281         INIT_WORK(&mkbp_dev->work, &mkbp_work);
282
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]);
288         idev->keycodemax =
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);
293         }
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;
300
301         input_set_drvdata(idev, mkbp_dev);
302         mkbp_dev->idev = idev;
303         err = input_register_device(mkbp_dev->idev);
304         if (err) {
305                 dev_err(dev, "cannot register input device\n");
306                 goto fail;
307         }
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.
313          *
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.)
317          */
318         mkbp_dev->idev->rep[REP_DELAY] = 600;
319         input_device_registered = true;
320
321         return err;
322 fail:
323         if (input_device_registered)
324                 input_unregister_device(idev);
325         kfree(mkbp_dev);
326         input_free_device(idev);
327         return err;
328 }
329
330 static struct platform_driver mkbp_driver = {
331         .probe = mkbp_probe,
332         .driver = {
333                 .name = "mkbp",
334         },
335 };
336
337
338 module_platform_driver(mkbp_driver);
339
340 MODULE_LICENSE("GPL");
341 MODULE_DESCRIPTION("Matrix keyboard protocol driver");
342 MODULE_ALIAS("platform:mkbp");