GNU Linux-libre 4.4.288-gnu1
[releases.git] / drivers / input / keyboard / cros_ec_keyb.c
1 /*
2  * ChromeOS EC keyboard driver
3  *
4  * Copyright (C) 2012 Google, Inc
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * This driver uses the Chrome OS EC byte-level message-based protocol for
16  * communicating the keyboard state (which keys are pressed) from a keyboard EC
17  * to the AP over some bus (such as i2c, lpc, spi).  The EC does debouncing,
18  * but everything else (including deghosting) is done here.  The main
19  * motivation for this is to keep the EC firmware as simple as possible, since
20  * it cannot be easily upgraded and EC flash/IRAM space is relatively
21  * expensive.
22  */
23
24 #include <linux/module.h>
25 #include <linux/bitops.h>
26 #include <linux/i2c.h>
27 #include <linux/input.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/input/matrix_keypad.h>
33 #include <linux/mfd/cros_ec.h>
34 #include <linux/mfd/cros_ec_commands.h>
35
36 /*
37  * @rows: Number of rows in the keypad
38  * @cols: Number of columns in the keypad
39  * @row_shift: log2 or number of rows, rounded up
40  * @keymap_data: Matrix keymap data used to convert to keyscan values
41  * @ghost_filter: true to enable the matrix key-ghosting filter
42  * @valid_keys: bitmap of existing keys for each matrix column
43  * @old_kb_state: bitmap of keys pressed last scan
44  * @dev: Device pointer
45  * @idev: Input device
46  * @ec: Top level ChromeOS device to use to talk to EC
47  */
48 struct cros_ec_keyb {
49         unsigned int rows;
50         unsigned int cols;
51         int row_shift;
52         const struct matrix_keymap_data *keymap_data;
53         bool ghost_filter;
54         uint8_t *valid_keys;
55         uint8_t *old_kb_state;
56
57         struct device *dev;
58         struct input_dev *idev;
59         struct cros_ec_device *ec;
60 };
61
62
63 /*
64  * Returns true when there is at least one combination of pressed keys that
65  * results in ghosting.
66  */
67 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
68 {
69         int col1, col2, buf1, buf2;
70         struct device *dev = ckdev->dev;
71         uint8_t *valid_keys = ckdev->valid_keys;
72
73         /*
74          * Ghosting happens if for any pressed key X there are other keys
75          * pressed both in the same row and column of X as, for instance,
76          * in the following diagram:
77          *
78          * . . Y . g .
79          * . . . . . .
80          * . . . . . .
81          * . . X . Z .
82          *
83          * In this case only X, Y, and Z are pressed, but g appears to be
84          * pressed too (see Wikipedia).
85          */
86         for (col1 = 0; col1 < ckdev->cols; col1++) {
87                 buf1 = buf[col1] & valid_keys[col1];
88                 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
89                         buf2 = buf[col2] & valid_keys[col2];
90                         if (hweight8(buf1 & buf2) > 1) {
91                                 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
92                                         col1, buf1, col2, buf2);
93                                 return true;
94                         }
95                 }
96         }
97
98         return false;
99 }
100
101
102 /*
103  * Compares the new keyboard state to the old one and produces key
104  * press/release events accordingly.  The keyboard state is 13 bytes (one byte
105  * per column)
106  */
107 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
108                          uint8_t *kb_state, int len)
109 {
110         struct input_dev *idev = ckdev->idev;
111         int col, row;
112         int new_state;
113         int old_state;
114         int num_cols;
115
116         num_cols = len;
117
118         if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
119                 /*
120                  * Simple-minded solution: ignore this state. The obvious
121                  * improvement is to only ignore changes to keys involved in
122                  * the ghosting, but process the other changes.
123                  */
124                 dev_dbg(ckdev->dev, "ghosting found\n");
125                 return;
126         }
127
128         for (col = 0; col < ckdev->cols; col++) {
129                 for (row = 0; row < ckdev->rows; row++) {
130                         int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
131                         const unsigned short *keycodes = idev->keycode;
132
133                         new_state = kb_state[col] & (1 << row);
134                         old_state = ckdev->old_kb_state[col] & (1 << row);
135                         if (new_state != old_state) {
136                                 dev_dbg(ckdev->dev,
137                                         "changed: [r%d c%d]: byte %02x\n",
138                                         row, col, new_state);
139
140                                 input_event(idev, EV_MSC, MSC_SCAN, pos);
141                                 input_report_key(idev, keycodes[pos],
142                                                  new_state);
143                         }
144                 }
145                 ckdev->old_kb_state[col] = kb_state[col];
146         }
147         input_sync(ckdev->idev);
148 }
149
150 static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
151 {
152         int ret = 0;
153         struct cros_ec_command *msg;
154
155         msg = kmalloc(sizeof(*msg) + ckdev->cols, GFP_KERNEL);
156         if (!msg)
157                 return -ENOMEM;
158
159         msg->version = 0;
160         msg->command = EC_CMD_MKBP_STATE;
161         msg->insize = ckdev->cols;
162         msg->outsize = 0;
163
164         ret = cros_ec_cmd_xfer(ckdev->ec, msg);
165         if (ret < 0) {
166                 dev_err(ckdev->dev, "Error transferring EC message %d\n", ret);
167                 goto exit;
168         }
169
170         memcpy(kb_state, msg->data, ckdev->cols);
171 exit:
172         kfree(msg);
173         return ret;
174 }
175
176 static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
177 {
178         struct cros_ec_keyb *ckdev = data;
179         struct cros_ec_device *ec = ckdev->ec;
180         int ret;
181         uint8_t kb_state[ckdev->cols];
182
183         if (device_may_wakeup(ec->dev))
184                 pm_wakeup_event(ec->dev, 0);
185
186         ret = cros_ec_keyb_get_state(ckdev, kb_state);
187         if (ret >= 0)
188                 cros_ec_keyb_process(ckdev, kb_state, ret);
189         else
190                 dev_err(ec->dev, "failed to get keyboard state: %d\n", ret);
191
192         return IRQ_HANDLED;
193 }
194
195 static int cros_ec_keyb_open(struct input_dev *dev)
196 {
197         struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
198         struct cros_ec_device *ec = ckdev->ec;
199
200         return request_threaded_irq(ec->irq, NULL, cros_ec_keyb_irq,
201                                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
202                                         "cros_ec_keyb", ckdev);
203 }
204
205 static void cros_ec_keyb_close(struct input_dev *dev)
206 {
207         struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
208         struct cros_ec_device *ec = ckdev->ec;
209
210         free_irq(ec->irq, ckdev);
211 }
212
213 /*
214  * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW.  Used by
215  * ghosting logic to ignore NULL or virtual keys.
216  */
217 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
218 {
219         int row, col;
220         int row_shift = ckdev->row_shift;
221         unsigned short *keymap = ckdev->idev->keycode;
222         unsigned short code;
223
224         BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
225
226         for (col = 0; col < ckdev->cols; col++) {
227                 for (row = 0; row < ckdev->rows; row++) {
228                         code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
229                         if (code && (code != KEY_BATTERY))
230                                 ckdev->valid_keys[col] |= 1 << row;
231                 }
232                 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
233                         col, ckdev->valid_keys[col]);
234         }
235 }
236
237 static int cros_ec_keyb_probe(struct platform_device *pdev)
238 {
239         struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
240         struct device *dev = ec->dev;
241         struct cros_ec_keyb *ckdev;
242         struct input_dev *idev;
243         struct device_node *np;
244         int err;
245
246         np = pdev->dev.of_node;
247         if (!np)
248                 return -ENODEV;
249
250         ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
251         if (!ckdev)
252                 return -ENOMEM;
253         err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
254                                             &ckdev->cols);
255         if (err)
256                 return err;
257
258         ckdev->valid_keys = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
259         if (!ckdev->valid_keys)
260                 return -ENOMEM;
261
262         ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
263         if (!ckdev->old_kb_state)
264                 return -ENOMEM;
265
266         idev = devm_input_allocate_device(&pdev->dev);
267         if (!idev)
268                 return -ENOMEM;
269
270         if (!ec->irq) {
271                 dev_err(dev, "no EC IRQ specified\n");
272                 return -EINVAL;
273         }
274
275         ckdev->ec = ec;
276         ckdev->dev = dev;
277         dev_set_drvdata(&pdev->dev, ckdev);
278
279         idev->name = CROS_EC_DEV_NAME;
280         idev->phys = ec->phys_name;
281         __set_bit(EV_REP, idev->evbit);
282
283         idev->id.bustype = BUS_VIRTUAL;
284         idev->id.version = 1;
285         idev->id.product = 0;
286         idev->dev.parent = &pdev->dev;
287         idev->open = cros_ec_keyb_open;
288         idev->close = cros_ec_keyb_close;
289
290         ckdev->ghost_filter = of_property_read_bool(np,
291                                         "google,needs-ghost-filter");
292
293         err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
294                                          NULL, idev);
295         if (err) {
296                 dev_err(dev, "cannot build key matrix\n");
297                 return err;
298         }
299
300         ckdev->row_shift = get_count_order(ckdev->cols);
301
302         input_set_capability(idev, EV_MSC, MSC_SCAN);
303         input_set_drvdata(idev, ckdev);
304         ckdev->idev = idev;
305         cros_ec_keyb_compute_valid_keys(ckdev);
306
307         err = input_register_device(ckdev->idev);
308         if (err) {
309                 dev_err(dev, "cannot register input device\n");
310                 return err;
311         }
312
313         return 0;
314 }
315
316 #ifdef CONFIG_PM_SLEEP
317 /* Clear any keys in the buffer */
318 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
319 {
320         uint8_t old_state[ckdev->cols];
321         uint8_t new_state[ckdev->cols];
322         unsigned long duration;
323         int i, ret;
324
325         /*
326          * Keep reading until we see that the scan state does not change.
327          * That indicates that we are done.
328          *
329          * Assume that the EC keyscan buffer is at most 32 deep.
330          */
331         duration = jiffies;
332         ret = cros_ec_keyb_get_state(ckdev, new_state);
333         for (i = 1; !ret && i < 32; i++) {
334                 memcpy(old_state, new_state, sizeof(old_state));
335                 ret = cros_ec_keyb_get_state(ckdev, new_state);
336                 if (0 == memcmp(old_state, new_state, sizeof(old_state)))
337                         break;
338         }
339         duration = jiffies - duration;
340         dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
341                 jiffies_to_usecs(duration));
342 }
343
344 static int cros_ec_keyb_resume(struct device *dev)
345 {
346         struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
347
348         /*
349          * When the EC is not a wake source, then it could not have caused the
350          * resume, so we clear the EC's key scan buffer. If the EC was a
351          * wake source (e.g. the lid is open and the user might press a key to
352          * wake) then the key scan buffer should be preserved.
353          */
354         if (!ckdev->ec->was_wake_device)
355                 cros_ec_keyb_clear_keyboard(ckdev);
356
357         return 0;
358 }
359
360 #endif
361
362 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
363
364 #ifdef CONFIG_OF
365 static const struct of_device_id cros_ec_keyb_of_match[] = {
366         { .compatible = "google,cros-ec-keyb" },
367         {},
368 };
369 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
370 #endif
371
372 static struct platform_driver cros_ec_keyb_driver = {
373         .probe = cros_ec_keyb_probe,
374         .driver = {
375                 .name = "cros-ec-keyb",
376                 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
377                 .pm     = &cros_ec_keyb_pm_ops,
378         },
379 };
380
381 module_platform_driver(cros_ec_keyb_driver);
382
383 MODULE_LICENSE("GPL");
384 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
385 MODULE_ALIAS("platform:cros-ec-keyb");