blob: b8341ab99f55004942b8e406dc3d071efd8caf57 [file] [log] [blame]
Simon Glass6af6dc22013-02-25 14:08:41 -08001/*
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/i2c.h>
26#include <linux/input.h>
27#include <linux/kernel.h>
28#include <linux/notifier.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <linux/input/matrix_keypad.h>
32#include <linux/mfd/cros_ec.h>
33#include <linux/mfd/cros_ec_commands.h>
34
35/*
36 * @rows: Number of rows in the keypad
37 * @cols: Number of columns in the keypad
38 * @row_shift: log2 or number of rows, rounded up
39 * @keymap_data: Matrix keymap data used to convert to keyscan values
40 * @ghost_filter: true to enable the matrix key-ghosting filter
Doug Anderson64757eb2013-12-29 16:52:46 -080041 * @old_kb_state: bitmap of keys pressed last scan
Simon Glass6af6dc22013-02-25 14:08:41 -080042 * @dev: Device pointer
43 * @idev: Input device
44 * @ec: Top level ChromeOS device to use to talk to EC
45 * @event_notifier: interrupt event notifier for transport devices
46 */
47struct cros_ec_keyb {
48 unsigned int rows;
49 unsigned int cols;
50 int row_shift;
51 const struct matrix_keymap_data *keymap_data;
52 bool ghost_filter;
Doug Anderson64757eb2013-12-29 16:52:46 -080053 uint8_t *old_kb_state;
Simon Glass6af6dc22013-02-25 14:08:41 -080054
55 struct device *dev;
56 struct input_dev *idev;
57 struct cros_ec_device *ec;
58 struct notifier_block notifier;
59};
60
61
62static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb *ckdev,
63 uint8_t *buf, int row)
64{
65 int pressed_in_row = 0;
66 int row_has_teeth = 0;
67 int col, mask;
68
69 mask = 1 << row;
70 for (col = 0; col < ckdev->cols; col++) {
71 if (buf[col] & mask) {
72 pressed_in_row++;
73 row_has_teeth |= buf[col] & ~mask;
74 if (pressed_in_row > 1 && row_has_teeth) {
75 /* ghosting */
76 dev_dbg(ckdev->dev,
77 "ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
78 row, col, pressed_in_row,
79 row_has_teeth);
80 return true;
81 }
82 }
83 }
84
85 return false;
86}
87
88/*
89 * Returns true when there is at least one combination of pressed keys that
90 * results in ghosting.
91 */
92static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
93{
94 int row;
95
96 /*
97 * Ghosting happens if for any pressed key X there are other keys
98 * pressed both in the same row and column of X as, for instance,
99 * in the following diagram:
100 *
101 * . . Y . g .
102 * . . . . . .
103 * . . . . . .
104 * . . X . Z .
105 *
106 * In this case only X, Y, and Z are pressed, but g appears to be
107 * pressed too (see Wikipedia).
108 *
109 * We can detect ghosting in a single pass (*) over the keyboard state
110 * by maintaining two arrays. pressed_in_row counts how many pressed
111 * keys we have found in a row. row_has_teeth is true if any of the
112 * pressed keys for this row has other pressed keys in its column. If
113 * at any point of the scan we find that a row has multiple pressed
114 * keys, and at least one of them is at the intersection with a column
115 * with multiple pressed keys, we're sure there is ghosting.
116 * Conversely, if there is ghosting, we will detect such situation for
117 * at least one key during the pass.
118 *
119 * (*) This looks linear in the number of keys, but it's not. We can
120 * cheat because the number of rows is small.
121 */
122 for (row = 0; row < ckdev->rows; row++)
123 if (cros_ec_keyb_row_has_ghosting(ckdev, buf, row))
124 return true;
125
126 return false;
127}
128
129/*
130 * Compares the new keyboard state to the old one and produces key
131 * press/release events accordingly. The keyboard state is 13 bytes (one byte
132 * per column)
133 */
134static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
135 uint8_t *kb_state, int len)
136{
137 struct input_dev *idev = ckdev->idev;
138 int col, row;
139 int new_state;
Doug Anderson64757eb2013-12-29 16:52:46 -0800140 int old_state;
Simon Glass6af6dc22013-02-25 14:08:41 -0800141 int num_cols;
142
143 num_cols = len;
144
145 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
146 /*
147 * Simple-minded solution: ignore this state. The obvious
148 * improvement is to only ignore changes to keys involved in
149 * the ghosting, but process the other changes.
150 */
151 dev_dbg(ckdev->dev, "ghosting found\n");
152 return;
153 }
154
155 for (col = 0; col < ckdev->cols; col++) {
156 for (row = 0; row < ckdev->rows; row++) {
157 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
158 const unsigned short *keycodes = idev->keycode;
Simon Glass6af6dc22013-02-25 14:08:41 -0800159
Simon Glass6af6dc22013-02-25 14:08:41 -0800160 new_state = kb_state[col] & (1 << row);
Doug Anderson64757eb2013-12-29 16:52:46 -0800161 old_state = ckdev->old_kb_state[col] & (1 << row);
162 if (new_state != old_state) {
Simon Glass6af6dc22013-02-25 14:08:41 -0800163 dev_dbg(ckdev->dev,
164 "changed: [r%d c%d]: byte %02x\n",
165 row, col, new_state);
166
Doug Anderson64757eb2013-12-29 16:52:46 -0800167 input_report_key(idev, keycodes[pos],
168 new_state);
Simon Glass6af6dc22013-02-25 14:08:41 -0800169 }
170 }
Doug Anderson64757eb2013-12-29 16:52:46 -0800171 ckdev->old_kb_state[col] = kb_state[col];
Simon Glass6af6dc22013-02-25 14:08:41 -0800172 }
173 input_sync(ckdev->idev);
174}
175
176static int cros_ec_keyb_open(struct input_dev *dev)
177{
178 struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
179
180 return blocking_notifier_chain_register(&ckdev->ec->event_notifier,
181 &ckdev->notifier);
182}
183
184static void cros_ec_keyb_close(struct input_dev *dev)
185{
186 struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
187
188 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
189 &ckdev->notifier);
190}
191
192static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
193{
Bill Richardson5799f952014-06-18 11:14:04 -0700194 struct cros_ec_command msg = {
195 .version = 0,
196 .command = EC_CMD_MKBP_STATE,
197 .outdata = NULL,
198 .outsize = 0,
199 .indata = kb_state,
200 .insize = ckdev->cols,
201 };
202
203 return ckdev->ec->cmd_xfer(ckdev->ec, &msg);
Simon Glass6af6dc22013-02-25 14:08:41 -0800204}
205
206static int cros_ec_keyb_work(struct notifier_block *nb,
207 unsigned long state, void *_notify)
208{
209 int ret;
210 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
211 notifier);
212 uint8_t kb_state[ckdev->cols];
213
214 ret = cros_ec_keyb_get_state(ckdev, kb_state);
215 if (ret >= 0)
216 cros_ec_keyb_process(ckdev, kb_state, ret);
217
218 return NOTIFY_DONE;
219}
220
Simon Glass6af6dc22013-02-25 14:08:41 -0800221static int cros_ec_keyb_probe(struct platform_device *pdev)
222{
223 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
224 struct device *dev = ec->dev;
225 struct cros_ec_keyb *ckdev;
226 struct input_dev *idev;
227 struct device_node *np;
228 int err;
229
230 np = pdev->dev.of_node;
231 if (!np)
232 return -ENODEV;
233
234 ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
235 if (!ckdev)
236 return -ENOMEM;
237 err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
238 &ckdev->cols);
239 if (err)
240 return err;
Doug Anderson64757eb2013-12-29 16:52:46 -0800241 ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
242 if (!ckdev->old_kb_state)
243 return -ENOMEM;
Simon Glass6af6dc22013-02-25 14:08:41 -0800244
245 idev = devm_input_allocate_device(&pdev->dev);
246 if (!idev)
247 return -ENOMEM;
248
249 ckdev->ec = ec;
250 ckdev->notifier.notifier_call = cros_ec_keyb_work;
251 ckdev->dev = dev;
252 dev_set_drvdata(&pdev->dev, ckdev);
253
254 idev->name = ec->ec_name;
255 idev->phys = ec->phys_name;
256 __set_bit(EV_REP, idev->evbit);
257
258 idev->id.bustype = BUS_VIRTUAL;
259 idev->id.version = 1;
260 idev->id.product = 0;
261 idev->dev.parent = &pdev->dev;
262 idev->open = cros_ec_keyb_open;
263 idev->close = cros_ec_keyb_close;
264
265 ckdev->ghost_filter = of_property_read_bool(np,
266 "google,needs-ghost-filter");
267
268 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
269 NULL, idev);
270 if (err) {
271 dev_err(dev, "cannot build key matrix\n");
272 return err;
273 }
274
275 ckdev->row_shift = get_count_order(ckdev->cols);
276
277 input_set_capability(idev, EV_MSC, MSC_SCAN);
278 input_set_drvdata(idev, ckdev);
279 ckdev->idev = idev;
280 err = input_register_device(ckdev->idev);
281 if (err) {
282 dev_err(dev, "cannot register input device\n");
283 return err;
284 }
285
286 return 0;
287}
288
289#ifdef CONFIG_PM_SLEEP
Geert Uytterhoeven03832aa2013-05-08 23:41:18 +0200290/* Clear any keys in the buffer */
291static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
292{
293 uint8_t old_state[ckdev->cols];
294 uint8_t new_state[ckdev->cols];
295 unsigned long duration;
296 int i, ret;
297
298 /*
299 * Keep reading until we see that the scan state does not change.
300 * That indicates that we are done.
301 *
302 * Assume that the EC keyscan buffer is at most 32 deep.
303 */
304 duration = jiffies;
305 ret = cros_ec_keyb_get_state(ckdev, new_state);
306 for (i = 1; !ret && i < 32; i++) {
307 memcpy(old_state, new_state, sizeof(old_state));
308 ret = cros_ec_keyb_get_state(ckdev, new_state);
309 if (0 == memcmp(old_state, new_state, sizeof(old_state)))
310 break;
311 }
312 duration = jiffies - duration;
313 dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
314 jiffies_to_usecs(duration));
315}
316
Simon Glass6af6dc22013-02-25 14:08:41 -0800317static int cros_ec_keyb_resume(struct device *dev)
318{
319 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
320
321 /*
322 * When the EC is not a wake source, then it could not have caused the
323 * resume, so we clear the EC's key scan buffer. If the EC was a
324 * wake source (e.g. the lid is open and the user might press a key to
325 * wake) then the key scan buffer should be preserved.
326 */
327 if (ckdev->ec->was_wake_device)
328 cros_ec_keyb_clear_keyboard(ckdev);
329
330 return 0;
331}
332
333#endif
334
335static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
336
337static struct platform_driver cros_ec_keyb_driver = {
338 .probe = cros_ec_keyb_probe,
339 .driver = {
340 .name = "cros-ec-keyb",
341 .pm = &cros_ec_keyb_pm_ops,
342 },
343};
344
345module_platform_driver(cros_ec_keyb_driver);
346
347MODULE_LICENSE("GPL");
348MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
349MODULE_ALIAS("platform:cros-ec-keyb");