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/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/notifier.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/sysrq.h>
34 #include <linux/input/matrix_keypad.h>
35 #include <linux/mfd/cros_ec.h>
36 #include <linux/mfd/cros_ec_commands.h>
38 #include <asm/unaligned.h>
41 * @rows: Number of rows in the keypad
42 * @cols: Number of columns in the keypad
43 * @row_shift: log2 or number of rows, rounded up
44 * @keymap_data: Matrix keymap data used to convert to keyscan values
45 * @ghost_filter: true to enable the matrix key-ghosting filter
46 * @valid_keys: bitmap of existing keys for each matrix column
47 * @old_kb_state: bitmap of keys pressed last scan
48 * @dev: Device pointer
49 * @ec: Top level ChromeOS device to use to talk to EC
50 * @idev: The input device for the matrix keys.
51 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
52 * @notifier: interrupt event notifier for transport devices
58 const struct matrix_keymap_data *keymap_data;
61 uint8_t *old_kb_state;
64 struct cros_ec_device *ec;
66 struct input_dev *idev;
67 struct input_dev *bs_idev;
68 struct notifier_block notifier;
73 * cros_ec_bs_map - Struct mapping Linux keycodes to EC button/switch bitmap
76 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
77 * @code: A linux keycode
78 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
79 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
80 * Only applicable to switches.
82 struct cros_ec_bs_map {
89 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
90 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
95 .bit = EC_MKBP_POWER_BUTTON,
100 .bit = EC_MKBP_VOL_UP,
104 .code = KEY_VOLUMEDOWN,
105 .bit = EC_MKBP_VOL_DOWN,
112 .bit = EC_MKBP_LID_OPEN,
117 .code = SW_TABLET_MODE,
118 .bit = EC_MKBP_TABLET_MODE,
123 * Returns true when there is at least one combination of pressed keys that
124 * results in ghosting.
126 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
128 int col1, col2, buf1, buf2;
129 struct device *dev = ckdev->dev;
130 uint8_t *valid_keys = ckdev->valid_keys;
133 * Ghosting happens if for any pressed key X there are other keys
134 * pressed both in the same row and column of X as, for instance,
135 * in the following diagram:
142 * In this case only X, Y, and Z are pressed, but g appears to be
143 * pressed too (see Wikipedia).
145 for (col1 = 0; col1 < ckdev->cols; col1++) {
146 buf1 = buf[col1] & valid_keys[col1];
147 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
148 buf2 = buf[col2] & valid_keys[col2];
149 if (hweight8(buf1 & buf2) > 1) {
150 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
151 col1, buf1, col2, buf2);
162 * Compares the new keyboard state to the old one and produces key
163 * press/release events accordingly. The keyboard state is 13 bytes (one byte
166 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
167 uint8_t *kb_state, int len)
169 struct input_dev *idev = ckdev->idev;
177 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
179 * Simple-minded solution: ignore this state. The obvious
180 * improvement is to only ignore changes to keys involved in
181 * the ghosting, but process the other changes.
183 dev_dbg(ckdev->dev, "ghosting found\n");
187 for (col = 0; col < ckdev->cols; col++) {
188 for (row = 0; row < ckdev->rows; row++) {
189 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
190 const unsigned short *keycodes = idev->keycode;
192 new_state = kb_state[col] & (1 << row);
193 old_state = ckdev->old_kb_state[col] & (1 << row);
194 if (new_state != old_state) {
196 "changed: [r%d c%d]: byte %02x\n",
197 row, col, new_state);
199 input_report_key(idev, keycodes[pos],
203 ckdev->old_kb_state[col] = kb_state[col];
205 input_sync(ckdev->idev);
209 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
211 * This takes a bitmap of buttons or switches from the EC and reports events,
212 * syncing at the end.
214 * @ckdev: The keyboard device.
215 * @ev_type: The input event type (e.g., EV_KEY).
216 * @mask: A bitmap of buttons from the EC.
218 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
219 unsigned int ev_type, u32 mask)
222 struct input_dev *idev = ckdev->bs_idev;
225 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
226 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
228 if (map->ev_type != ev_type)
231 input_event(idev, ev_type, map->code,
232 !!(mask & BIT(map->bit)) ^ map->inverted);
237 static int cros_ec_keyb_work(struct notifier_block *nb,
238 unsigned long queued_during_suspend, void *_notify)
240 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
243 unsigned int ev_type;
245 switch (ckdev->ec->event_data.event_type) {
246 case EC_MKBP_EVENT_KEY_MATRIX:
247 if (device_may_wakeup(ckdev->dev)) {
248 pm_wakeup_event(ckdev->dev, 0);
251 * If keyboard is not wake enabled, discard key state
252 * changes during suspend. Switches will be re-checked
253 * in cros_ec_keyb_resume() to be sure nothing is lost.
255 if (queued_during_suspend)
259 if (ckdev->ec->event_size != ckdev->cols) {
261 "Discarded incomplete key matrix event.\n");
265 cros_ec_keyb_process(ckdev,
266 ckdev->ec->event_data.data.key_matrix,
267 ckdev->ec->event_size);
270 case EC_MKBP_EVENT_SYSRQ:
271 if (device_may_wakeup(ckdev->dev))
272 pm_wakeup_event(ckdev->dev, 0);
273 else if (queued_during_suspend)
276 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
277 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
281 case EC_MKBP_EVENT_BUTTON:
282 case EC_MKBP_EVENT_SWITCH:
283 if (device_may_wakeup(ckdev->dev))
284 pm_wakeup_event(ckdev->dev, 0);
285 else if (queued_during_suspend)
288 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
289 val = get_unaligned_le32(
290 &ckdev->ec->event_data.data.buttons);
293 val = get_unaligned_le32(
294 &ckdev->ec->event_data.data.switches);
297 cros_ec_keyb_report_bs(ckdev, ev_type, val);
308 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
309 * ghosting logic to ignore NULL or virtual keys.
311 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
314 int row_shift = ckdev->row_shift;
315 unsigned short *keymap = ckdev->idev->keycode;
318 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
320 for (col = 0; col < ckdev->cols; col++) {
321 for (row = 0; row < ckdev->rows; row++) {
322 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
323 if (code && (code != KEY_BATTERY))
324 ckdev->valid_keys[col] |= 1 << row;
326 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
327 col, ckdev->valid_keys[col]);
332 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
334 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
335 * unmarshalling and different version nonsense into something simple.
337 * @ec_dev: The EC device
338 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
339 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
340 * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
341 * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
342 * @result: Where we'll store the result; a union
343 * @result_size: The size of the result. Expected to be the size of one of
344 * the elements in the union.
346 * Returns 0 if no error or -error upon error.
348 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
349 enum ec_mkbp_info_type info_type,
350 enum ec_mkbp_event event_type,
351 union ec_response_get_next_data *result,
354 struct ec_params_mkbp_info *params;
355 struct cros_ec_command *msg;
358 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
359 sizeof(*params)), GFP_KERNEL);
363 msg->command = EC_CMD_MKBP_INFO;
365 msg->outsize = sizeof(*params);
366 msg->insize = result_size;
367 params = (struct ec_params_mkbp_info *)msg->data;
368 params->info_type = info_type;
369 params->event_type = event_type;
371 ret = cros_ec_cmd_xfer(ec_dev, msg);
373 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
374 (int)info_type, (int)event_type, ret);
375 } else if (msg->result == EC_RES_INVALID_VERSION) {
376 /* With older ECs we just return 0 for everything */
377 memset(result, 0, result_size);
379 } else if (msg->result != EC_RES_SUCCESS) {
380 dev_warn(ec_dev->dev, "Error getting info %d/%d: %d\n",
381 (int)info_type, (int)event_type, msg->result);
383 } else if (ret != result_size) {
384 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
385 (int)info_type, (int)event_type,
389 memcpy(result, msg->data, result_size);
399 * cros_ec_keyb_query_switches - Query the state of switches and report
401 * This will ask the EC about the current state of switches and report to the
402 * kernel. Note that we don't query for buttons because they are more
403 * transitory and we'll get an update on the next release / press.
405 * @ckdev: The keyboard device
407 * Returns 0 if no error or -error upon error.
409 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
411 struct cros_ec_device *ec_dev = ckdev->ec;
412 union ec_response_get_next_data event_data = {};
415 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
416 EC_MKBP_EVENT_SWITCH, &event_data,
417 sizeof(event_data.switches));
421 cros_ec_keyb_report_bs(ckdev, EV_SW,
422 get_unaligned_le32(&event_data.switches));
428 * cros_ec_keyb_resume - Resume the keyboard
430 * We use the resume notification as a chance to query the EC for switches.
432 * @dev: The keyboard device
434 * Returns 0 if no error or -error upon error.
436 static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
438 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
441 return cros_ec_keyb_query_switches(ckdev);
447 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
449 * Handles all the bits of the keyboard driver related to non-matrix buttons
450 * and switches, including asking the EC about which are present and telling
451 * the kernel to expect them.
453 * If this device has no support for buttons and switches we'll return no error
454 * but the ckdev->bs_idev will remain NULL when this function exits.
456 * @ckdev: The keyboard device
458 * Returns 0 if no error or -error upon error.
460 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
462 struct cros_ec_device *ec_dev = ckdev->ec;
463 struct device *dev = ckdev->dev;
464 struct input_dev *idev;
465 union ec_response_get_next_data event_data = {};
472 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
473 EC_MKBP_EVENT_BUTTON, &event_data,
474 sizeof(event_data.buttons));
477 buttons = get_unaligned_le32(&event_data.buttons);
479 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
480 EC_MKBP_EVENT_SWITCH, &event_data,
481 sizeof(event_data.switches));
484 switches = get_unaligned_le32(&event_data.switches);
486 if (!buttons && !switches)
490 * We call the non-matrix buttons/switches 'input1', if present.
491 * Allocate phys before input dev, to ensure correct tear-down
494 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
498 idev = devm_input_allocate_device(dev);
502 idev->name = "cros_ec_buttons";
504 __set_bit(EV_REP, idev->evbit);
506 idev->id.bustype = BUS_VIRTUAL;
507 idev->id.version = 1;
508 idev->id.product = 0;
509 idev->dev.parent = dev;
511 input_set_drvdata(idev, ckdev);
512 ckdev->bs_idev = idev;
514 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
515 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
517 if (buttons & BIT(map->bit))
518 input_set_capability(idev, map->ev_type, map->code);
521 ret = cros_ec_keyb_query_switches(ckdev);
523 dev_err(dev, "cannot query switches\n");
527 ret = input_register_device(ckdev->bs_idev);
529 dev_err(dev, "cannot register input device\n");
537 * cros_ec_keyb_register_bs - Register matrix keys
539 * Handles all the bits of the keyboard driver related to matrix keys.
541 * @ckdev: The keyboard device
543 * Returns 0 if no error or -error upon error.
545 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
547 struct cros_ec_device *ec_dev = ckdev->ec;
548 struct device *dev = ckdev->dev;
549 struct input_dev *idev;
553 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
557 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
558 if (!ckdev->valid_keys)
561 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
562 if (!ckdev->old_kb_state)
566 * We call the keyboard matrix 'input0'. Allocate phys before input
567 * dev, to ensure correct tear-down ordering.
569 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
573 idev = devm_input_allocate_device(dev);
577 idev->name = CROS_EC_DEV_NAME;
579 __set_bit(EV_REP, idev->evbit);
581 idev->id.bustype = BUS_VIRTUAL;
582 idev->id.version = 1;
583 idev->id.product = 0;
584 idev->dev.parent = dev;
586 ckdev->ghost_filter = of_property_read_bool(dev->of_node,
587 "google,needs-ghost-filter");
589 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
592 dev_err(dev, "cannot build key matrix\n");
596 ckdev->row_shift = get_count_order(ckdev->cols);
598 input_set_capability(idev, EV_MSC, MSC_SCAN);
599 input_set_drvdata(idev, ckdev);
601 cros_ec_keyb_compute_valid_keys(ckdev);
603 err = input_register_device(ckdev->idev);
605 dev_err(dev, "cannot register input device\n");
612 static int cros_ec_keyb_probe(struct platform_device *pdev)
614 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
615 struct device *dev = &pdev->dev;
616 struct cros_ec_keyb *ckdev;
622 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
628 dev_set_drvdata(dev, ckdev);
630 err = cros_ec_keyb_register_matrix(ckdev);
632 dev_err(dev, "cannot register matrix inputs: %d\n", err);
636 err = cros_ec_keyb_register_bs(ckdev);
638 dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
642 ckdev->notifier.notifier_call = cros_ec_keyb_work;
643 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
646 dev_err(dev, "cannot register notifier: %d\n", err);
650 device_init_wakeup(ckdev->dev, true);
654 static int cros_ec_keyb_remove(struct platform_device *pdev)
656 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
658 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
665 static const struct of_device_id cros_ec_keyb_of_match[] = {
666 { .compatible = "google,cros-ec-keyb" },
669 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
672 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
674 static struct platform_driver cros_ec_keyb_driver = {
675 .probe = cros_ec_keyb_probe,
676 .remove = cros_ec_keyb_remove,
678 .name = "cros-ec-keyb",
679 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
680 .pm = &cros_ec_keyb_pm_ops,
684 module_platform_driver(cros_ec_keyb_driver);
686 MODULE_LICENSE("GPL");
687 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
688 MODULE_ALIAS("platform:cros-ec-keyb");