2 * ChromeOS EC keyboard driver
4 * Copyright (C) 2012 Google, Inc
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.
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.
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
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>
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
41 * @old_kb_state: bitmap of keys pressed last scan
42 * @dev: Device pointer
44 * @ec: Top level ChromeOS device to use to talk to EC
45 * @event_notifier: interrupt event notifier for transport devices
51 const struct matrix_keymap_data
*keymap_data
;
53 uint8_t *old_kb_state
;
56 struct input_dev
*idev
;
57 struct cros_ec_device
*ec
;
58 struct notifier_block notifier
;
62 static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb
*ckdev
,
63 uint8_t *buf
, int row
)
65 int pressed_in_row
= 0;
66 int row_has_teeth
= 0;
70 for (col
= 0; col
< ckdev
->cols
; col
++) {
71 if (buf
[col
] & mask
) {
73 row_has_teeth
|= buf
[col
] & ~mask
;
74 if (pressed_in_row
> 1 && row_has_teeth
) {
77 "ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
78 row
, col
, pressed_in_row
,
89 * Returns true when there is at least one combination of pressed keys that
90 * results in ghosting.
92 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb
*ckdev
, uint8_t *buf
)
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:
106 * In this case only X, Y, and Z are pressed, but g appears to be
107 * pressed too (see Wikipedia).
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.
119 * (*) This looks linear in the number of keys, but it's not. We can
120 * cheat because the number of rows is small.
122 for (row
= 0; row
< ckdev
->rows
; row
++)
123 if (cros_ec_keyb_row_has_ghosting(ckdev
, buf
, row
))
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
134 static void cros_ec_keyb_process(struct cros_ec_keyb
*ckdev
,
135 uint8_t *kb_state
, int len
)
137 struct input_dev
*idev
= ckdev
->idev
;
145 if (ckdev
->ghost_filter
&& cros_ec_keyb_has_ghosting(ckdev
, kb_state
)) {
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.
151 dev_dbg(ckdev
->dev
, "ghosting found\n");
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
;
160 new_state
= kb_state
[col
] & (1 << row
);
161 old_state
= ckdev
->old_kb_state
[col
] & (1 << row
);
162 if (new_state
!= old_state
) {
164 "changed: [r%d c%d]: byte %02x\n",
165 row
, col
, new_state
);
167 input_report_key(idev
, keycodes
[pos
],
171 ckdev
->old_kb_state
[col
] = kb_state
[col
];
173 input_sync(ckdev
->idev
);
176 static int cros_ec_keyb_open(struct input_dev
*dev
)
178 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
180 return blocking_notifier_chain_register(&ckdev
->ec
->event_notifier
,
184 static void cros_ec_keyb_close(struct input_dev
*dev
)
186 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
188 blocking_notifier_chain_unregister(&ckdev
->ec
->event_notifier
,
192 static int cros_ec_keyb_get_state(struct cros_ec_keyb
*ckdev
, uint8_t *kb_state
)
194 return ckdev
->ec
->command_recv(ckdev
->ec
, EC_CMD_MKBP_STATE
,
195 kb_state
, ckdev
->cols
);
198 static int cros_ec_keyb_work(struct notifier_block
*nb
,
199 unsigned long state
, void *_notify
)
202 struct cros_ec_keyb
*ckdev
= container_of(nb
, struct cros_ec_keyb
,
204 uint8_t kb_state
[ckdev
->cols
];
206 ret
= cros_ec_keyb_get_state(ckdev
, kb_state
);
208 cros_ec_keyb_process(ckdev
, kb_state
, ret
);
213 static int cros_ec_keyb_probe(struct platform_device
*pdev
)
215 struct cros_ec_device
*ec
= dev_get_drvdata(pdev
->dev
.parent
);
216 struct device
*dev
= ec
->dev
;
217 struct cros_ec_keyb
*ckdev
;
218 struct input_dev
*idev
;
219 struct device_node
*np
;
222 np
= pdev
->dev
.of_node
;
226 ckdev
= devm_kzalloc(&pdev
->dev
, sizeof(*ckdev
), GFP_KERNEL
);
229 err
= matrix_keypad_parse_of_params(&pdev
->dev
, &ckdev
->rows
,
233 ckdev
->old_kb_state
= devm_kzalloc(&pdev
->dev
, ckdev
->cols
, GFP_KERNEL
);
234 if (!ckdev
->old_kb_state
)
237 idev
= devm_input_allocate_device(&pdev
->dev
);
242 ckdev
->notifier
.notifier_call
= cros_ec_keyb_work
;
244 dev_set_drvdata(&pdev
->dev
, ckdev
);
246 idev
->name
= ec
->ec_name
;
247 idev
->phys
= ec
->phys_name
;
248 __set_bit(EV_REP
, idev
->evbit
);
250 idev
->id
.bustype
= BUS_VIRTUAL
;
251 idev
->id
.version
= 1;
252 idev
->id
.product
= 0;
253 idev
->dev
.parent
= &pdev
->dev
;
254 idev
->open
= cros_ec_keyb_open
;
255 idev
->close
= cros_ec_keyb_close
;
257 ckdev
->ghost_filter
= of_property_read_bool(np
,
258 "google,needs-ghost-filter");
260 err
= matrix_keypad_build_keymap(NULL
, NULL
, ckdev
->rows
, ckdev
->cols
,
263 dev_err(dev
, "cannot build key matrix\n");
267 ckdev
->row_shift
= get_count_order(ckdev
->cols
);
269 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
270 input_set_drvdata(idev
, ckdev
);
272 err
= input_register_device(ckdev
->idev
);
274 dev_err(dev
, "cannot register input device\n");
281 #ifdef CONFIG_PM_SLEEP
282 /* Clear any keys in the buffer */
283 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb
*ckdev
)
285 uint8_t old_state
[ckdev
->cols
];
286 uint8_t new_state
[ckdev
->cols
];
287 unsigned long duration
;
291 * Keep reading until we see that the scan state does not change.
292 * That indicates that we are done.
294 * Assume that the EC keyscan buffer is at most 32 deep.
297 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
298 for (i
= 1; !ret
&& i
< 32; i
++) {
299 memcpy(old_state
, new_state
, sizeof(old_state
));
300 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
301 if (0 == memcmp(old_state
, new_state
, sizeof(old_state
)))
304 duration
= jiffies
- duration
;
305 dev_info(ckdev
->dev
, "Discarded %d keyscan(s) in %dus\n", i
,
306 jiffies_to_usecs(duration
));
309 static int cros_ec_keyb_resume(struct device
*dev
)
311 struct cros_ec_keyb
*ckdev
= dev_get_drvdata(dev
);
314 * When the EC is not a wake source, then it could not have caused the
315 * resume, so we clear the EC's key scan buffer. If the EC was a
316 * wake source (e.g. the lid is open and the user might press a key to
317 * wake) then the key scan buffer should be preserved.
319 if (ckdev
->ec
->was_wake_device
)
320 cros_ec_keyb_clear_keyboard(ckdev
);
327 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops
, NULL
, cros_ec_keyb_resume
);
329 static struct platform_driver cros_ec_keyb_driver
= {
330 .probe
= cros_ec_keyb_probe
,
332 .name
= "cros-ec-keyb",
333 .pm
= &cros_ec_keyb_pm_ops
,
337 module_platform_driver(cros_ec_keyb_driver
);
339 MODULE_LICENSE("GPL");
340 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
341 MODULE_ALIAS("platform:cros-ec-keyb");