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 * @dev: Device pointer
43 * @ec: Top level ChromeOS device to use to talk to EC
44 * @event_notifier: interrupt event notifier for transport devices
50 const struct matrix_keymap_data
*keymap_data
;
54 struct input_dev
*idev
;
55 struct cros_ec_device
*ec
;
56 struct notifier_block notifier
;
60 static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb
*ckdev
,
61 uint8_t *buf
, int row
)
63 int pressed_in_row
= 0;
64 int row_has_teeth
= 0;
68 for (col
= 0; col
< ckdev
->cols
; col
++) {
69 if (buf
[col
] & mask
) {
71 row_has_teeth
|= buf
[col
] & ~mask
;
72 if (pressed_in_row
> 1 && row_has_teeth
) {
75 "ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
76 row
, col
, pressed_in_row
,
87 * Returns true when there is at least one combination of pressed keys that
88 * results in ghosting.
90 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb
*ckdev
, uint8_t *buf
)
95 * Ghosting happens if for any pressed key X there are other keys
96 * pressed both in the same row and column of X as, for instance,
97 * in the following diagram:
104 * In this case only X, Y, and Z are pressed, but g appears to be
105 * pressed too (see Wikipedia).
107 * We can detect ghosting in a single pass (*) over the keyboard state
108 * by maintaining two arrays. pressed_in_row counts how many pressed
109 * keys we have found in a row. row_has_teeth is true if any of the
110 * pressed keys for this row has other pressed keys in its column. If
111 * at any point of the scan we find that a row has multiple pressed
112 * keys, and at least one of them is at the intersection with a column
113 * with multiple pressed keys, we're sure there is ghosting.
114 * Conversely, if there is ghosting, we will detect such situation for
115 * at least one key during the pass.
117 * (*) This looks linear in the number of keys, but it's not. We can
118 * cheat because the number of rows is small.
120 for (row
= 0; row
< ckdev
->rows
; row
++)
121 if (cros_ec_keyb_row_has_ghosting(ckdev
, buf
, row
))
128 * Compares the new keyboard state to the old one and produces key
129 * press/release events accordingly. The keyboard state is 13 bytes (one byte
132 static void cros_ec_keyb_process(struct cros_ec_keyb
*ckdev
,
133 uint8_t *kb_state
, int len
)
135 struct input_dev
*idev
= ckdev
->idev
;
142 if (ckdev
->ghost_filter
&& cros_ec_keyb_has_ghosting(ckdev
, kb_state
)) {
144 * Simple-minded solution: ignore this state. The obvious
145 * improvement is to only ignore changes to keys involved in
146 * the ghosting, but process the other changes.
148 dev_dbg(ckdev
->dev
, "ghosting found\n");
152 for (col
= 0; col
< ckdev
->cols
; col
++) {
153 for (row
= 0; row
< ckdev
->rows
; row
++) {
154 int pos
= MATRIX_SCAN_CODE(row
, col
, ckdev
->row_shift
);
155 const unsigned short *keycodes
= idev
->keycode
;
158 code
= keycodes
[pos
];
159 new_state
= kb_state
[col
] & (1 << row
);
160 if (!!new_state
!= test_bit(code
, idev
->key
)) {
162 "changed: [r%d c%d]: byte %02x\n",
163 row
, col
, new_state
);
165 input_report_key(idev
, code
, new_state
);
169 input_sync(ckdev
->idev
);
172 static int cros_ec_keyb_open(struct input_dev
*dev
)
174 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
176 return blocking_notifier_chain_register(&ckdev
->ec
->event_notifier
,
180 static void cros_ec_keyb_close(struct input_dev
*dev
)
182 struct cros_ec_keyb
*ckdev
= input_get_drvdata(dev
);
184 blocking_notifier_chain_unregister(&ckdev
->ec
->event_notifier
,
188 static int cros_ec_keyb_get_state(struct cros_ec_keyb
*ckdev
, uint8_t *kb_state
)
190 return ckdev
->ec
->command_recv(ckdev
->ec
, EC_CMD_MKBP_STATE
,
191 kb_state
, ckdev
->cols
);
194 static int cros_ec_keyb_work(struct notifier_block
*nb
,
195 unsigned long state
, void *_notify
)
198 struct cros_ec_keyb
*ckdev
= container_of(nb
, struct cros_ec_keyb
,
200 uint8_t kb_state
[ckdev
->cols
];
202 ret
= cros_ec_keyb_get_state(ckdev
, kb_state
);
204 cros_ec_keyb_process(ckdev
, kb_state
, ret
);
209 static int cros_ec_keyb_probe(struct platform_device
*pdev
)
211 struct cros_ec_device
*ec
= dev_get_drvdata(pdev
->dev
.parent
);
212 struct device
*dev
= ec
->dev
;
213 struct cros_ec_keyb
*ckdev
;
214 struct input_dev
*idev
;
215 struct device_node
*np
;
218 np
= pdev
->dev
.of_node
;
222 ckdev
= devm_kzalloc(&pdev
->dev
, sizeof(*ckdev
), GFP_KERNEL
);
225 err
= matrix_keypad_parse_of_params(&pdev
->dev
, &ckdev
->rows
,
230 idev
= devm_input_allocate_device(&pdev
->dev
);
235 ckdev
->notifier
.notifier_call
= cros_ec_keyb_work
;
237 dev_set_drvdata(&pdev
->dev
, ckdev
);
239 idev
->name
= ec
->ec_name
;
240 idev
->phys
= ec
->phys_name
;
241 __set_bit(EV_REP
, idev
->evbit
);
243 idev
->id
.bustype
= BUS_VIRTUAL
;
244 idev
->id
.version
= 1;
245 idev
->id
.product
= 0;
246 idev
->dev
.parent
= &pdev
->dev
;
247 idev
->open
= cros_ec_keyb_open
;
248 idev
->close
= cros_ec_keyb_close
;
250 ckdev
->ghost_filter
= of_property_read_bool(np
,
251 "google,needs-ghost-filter");
253 err
= matrix_keypad_build_keymap(NULL
, NULL
, ckdev
->rows
, ckdev
->cols
,
256 dev_err(dev
, "cannot build key matrix\n");
260 ckdev
->row_shift
= get_count_order(ckdev
->cols
);
262 input_set_capability(idev
, EV_MSC
, MSC_SCAN
);
263 input_set_drvdata(idev
, ckdev
);
265 err
= input_register_device(ckdev
->idev
);
267 dev_err(dev
, "cannot register input device\n");
274 #ifdef CONFIG_PM_SLEEP
275 /* Clear any keys in the buffer */
276 static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb
*ckdev
)
278 uint8_t old_state
[ckdev
->cols
];
279 uint8_t new_state
[ckdev
->cols
];
280 unsigned long duration
;
284 * Keep reading until we see that the scan state does not change.
285 * That indicates that we are done.
287 * Assume that the EC keyscan buffer is at most 32 deep.
290 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
291 for (i
= 1; !ret
&& i
< 32; i
++) {
292 memcpy(old_state
, new_state
, sizeof(old_state
));
293 ret
= cros_ec_keyb_get_state(ckdev
, new_state
);
294 if (0 == memcmp(old_state
, new_state
, sizeof(old_state
)))
297 duration
= jiffies
- duration
;
298 dev_info(ckdev
->dev
, "Discarded %d keyscan(s) in %dus\n", i
,
299 jiffies_to_usecs(duration
));
302 static int cros_ec_keyb_resume(struct device
*dev
)
304 struct cros_ec_keyb
*ckdev
= dev_get_drvdata(dev
);
307 * When the EC is not a wake source, then it could not have caused the
308 * resume, so we clear the EC's key scan buffer. If the EC was a
309 * wake source (e.g. the lid is open and the user might press a key to
310 * wake) then the key scan buffer should be preserved.
312 if (ckdev
->ec
->was_wake_device
)
313 cros_ec_keyb_clear_keyboard(ckdev
);
320 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops
, NULL
, cros_ec_keyb_resume
);
322 static struct platform_driver cros_ec_keyb_driver
= {
323 .probe
= cros_ec_keyb_probe
,
325 .name
= "cros-ec-keyb",
326 .pm
= &cros_ec_keyb_pm_ops
,
330 module_platform_driver(cros_ec_keyb_driver
);
332 MODULE_LICENSE("GPL");
333 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
334 MODULE_ALIAS("platform:cros-ec-keyb");