1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC keyboard driver
4 // Copyright (C) 2012 Google, Inc.
6 // This driver uses the ChromeOS EC byte-level message-based protocol for
7 // communicating the keyboard state (which keys are pressed) from a keyboard EC
8 // to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
9 // but everything else (including deghosting) is done here. The main
10 // motivation for this is to keep the EC firmware as simple as possible, since
11 // it cannot be easily upgraded and EC flash/IRAM space is relatively
14 #include <linux/module.h>
15 #include <linux/bitops.h>
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/input/vivaldi-fmap.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/notifier.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/sysrq.h>
25 #include <linux/input/matrix_keypad.h>
26 #include <linux/platform_data/cros_ec_commands.h>
27 #include <linux/platform_data/cros_ec_proto.h>
29 #include <asm/unaligned.h>
32 * struct cros_ec_keyb - Structure representing EC keyboard device
34 * @rows: Number of rows in the keypad
35 * @cols: Number of columns in the keypad
36 * @row_shift: log2 or number of rows, rounded up
37 * @keymap_data: Matrix keymap data used to convert to keyscan values
38 * @ghost_filter: true to enable the matrix key-ghosting filter
39 * @valid_keys: bitmap of existing keys for each matrix column
40 * @old_kb_state: bitmap of keys pressed last scan
41 * @dev: Device pointer
42 * @ec: Top level ChromeOS device to use to talk to EC
43 * @idev: The input device for the matrix keys.
44 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
45 * @notifier: interrupt event notifier for transport devices
46 * @vdata: vivaldi function row data
52 const struct matrix_keymap_data *keymap_data;
55 uint8_t *old_kb_state;
58 struct cros_ec_device *ec;
60 struct input_dev *idev;
61 struct input_dev *bs_idev;
62 struct notifier_block notifier;
64 struct vivaldi_data vdata;
68 * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch
71 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
72 * @code: A linux keycode
73 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
74 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
75 * Only applicable to switches.
77 struct cros_ec_bs_map {
84 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
85 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
90 .bit = EC_MKBP_POWER_BUTTON,
95 .bit = EC_MKBP_VOL_UP,
99 .code = KEY_VOLUMEDOWN,
100 .bit = EC_MKBP_VOL_DOWN,
107 .bit = EC_MKBP_LID_OPEN,
112 .code = SW_TABLET_MODE,
113 .bit = EC_MKBP_TABLET_MODE,
118 * Returns true when there is at least one combination of pressed keys that
119 * results in ghosting.
121 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
123 int col1, col2, buf1, buf2;
124 struct device *dev = ckdev->dev;
125 uint8_t *valid_keys = ckdev->valid_keys;
128 * Ghosting happens if for any pressed key X there are other keys
129 * pressed both in the same row and column of X as, for instance,
130 * in the following diagram:
137 * In this case only X, Y, and Z are pressed, but g appears to be
138 * pressed too (see Wikipedia).
140 for (col1 = 0; col1 < ckdev->cols; col1++) {
141 buf1 = buf[col1] & valid_keys[col1];
142 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
143 buf2 = buf[col2] & valid_keys[col2];
144 if (hweight8(buf1 & buf2) > 1) {
145 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
146 col1, buf1, col2, buf2);
157 * Compares the new keyboard state to the old one and produces key
158 * press/release events accordingly. The keyboard state is 13 bytes (one byte
161 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
162 uint8_t *kb_state, int len)
164 struct input_dev *idev = ckdev->idev;
169 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
171 * Simple-minded solution: ignore this state. The obvious
172 * improvement is to only ignore changes to keys involved in
173 * the ghosting, but process the other changes.
175 dev_dbg(ckdev->dev, "ghosting found\n");
179 for (col = 0; col < ckdev->cols; col++) {
180 for (row = 0; row < ckdev->rows; row++) {
181 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
182 const unsigned short *keycodes = idev->keycode;
184 new_state = kb_state[col] & (1 << row);
185 old_state = ckdev->old_kb_state[col] & (1 << row);
186 if (new_state != old_state) {
188 "changed: [r%d c%d]: byte %02x\n",
189 row, col, new_state);
191 input_event(idev, EV_MSC, MSC_SCAN, pos);
192 input_report_key(idev, keycodes[pos],
196 ckdev->old_kb_state[col] = kb_state[col];
198 input_sync(ckdev->idev);
202 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
204 * This takes a bitmap of buttons or switches from the EC and reports events,
205 * syncing at the end.
207 * @ckdev: The keyboard device.
208 * @ev_type: The input event type (e.g., EV_KEY).
209 * @mask: A bitmap of buttons from the EC.
211 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
212 unsigned int ev_type, u32 mask)
215 struct input_dev *idev = ckdev->bs_idev;
218 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
219 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
221 if (map->ev_type != ev_type)
224 input_event(idev, ev_type, map->code,
225 !!(mask & BIT(map->bit)) ^ map->inverted);
230 static int cros_ec_keyb_work(struct notifier_block *nb,
231 unsigned long queued_during_suspend, void *_notify)
233 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
236 unsigned int ev_type;
239 * If not wake enabled, discard key state changes during
240 * suspend. Switches will be re-checked in
241 * cros_ec_keyb_resume() to be sure nothing is lost.
243 if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
246 switch (ckdev->ec->event_data.event_type) {
247 case EC_MKBP_EVENT_KEY_MATRIX:
248 pm_wakeup_event(ckdev->dev, 0);
250 if (ckdev->ec->event_size != ckdev->cols) {
252 "Discarded incomplete key matrix event.\n");
256 cros_ec_keyb_process(ckdev,
257 ckdev->ec->event_data.data.key_matrix,
258 ckdev->ec->event_size);
261 case EC_MKBP_EVENT_SYSRQ:
262 pm_wakeup_event(ckdev->dev, 0);
264 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
265 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
269 case EC_MKBP_EVENT_BUTTON:
270 case EC_MKBP_EVENT_SWITCH:
271 pm_wakeup_event(ckdev->dev, 0);
273 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
274 val = get_unaligned_le32(
275 &ckdev->ec->event_data.data.buttons);
278 val = get_unaligned_le32(
279 &ckdev->ec->event_data.data.switches);
282 cros_ec_keyb_report_bs(ckdev, ev_type, val);
293 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
294 * ghosting logic to ignore NULL or virtual keys.
296 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
299 int row_shift = ckdev->row_shift;
300 unsigned short *keymap = ckdev->idev->keycode;
303 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
305 for (col = 0; col < ckdev->cols; col++) {
306 for (row = 0; row < ckdev->rows; row++) {
307 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
308 if (code && (code != KEY_BATTERY))
309 ckdev->valid_keys[col] |= 1 << row;
311 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
312 col, ckdev->valid_keys[col]);
317 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
319 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
320 * unmarshalling and different version nonsense into something simple.
322 * @ec_dev: The EC device
323 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
324 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
325 * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
326 * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
327 * @result: Where we'll store the result; a union
328 * @result_size: The size of the result. Expected to be the size of one of
329 * the elements in the union.
331 * Returns 0 if no error or -error upon error.
333 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
334 enum ec_mkbp_info_type info_type,
335 enum ec_mkbp_event event_type,
336 union ec_response_get_next_data *result,
339 struct ec_params_mkbp_info *params;
340 struct cros_ec_command *msg;
343 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
344 sizeof(*params)), GFP_KERNEL);
348 msg->command = EC_CMD_MKBP_INFO;
350 msg->outsize = sizeof(*params);
351 msg->insize = result_size;
352 params = (struct ec_params_mkbp_info *)msg->data;
353 params->info_type = info_type;
354 params->event_type = event_type;
356 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
357 if (ret == -ENOPROTOOPT) {
358 /* With older ECs we just return 0 for everything */
359 memset(result, 0, result_size);
361 } else if (ret < 0) {
362 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
363 (int)info_type, (int)event_type, ret);
364 } else if (ret != result_size) {
365 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
366 (int)info_type, (int)event_type,
370 memcpy(result, msg->data, result_size);
380 * cros_ec_keyb_query_switches - Query the state of switches and report
382 * This will ask the EC about the current state of switches and report to the
383 * kernel. Note that we don't query for buttons because they are more
384 * transitory and we'll get an update on the next release / press.
386 * @ckdev: The keyboard device
388 * Returns 0 if no error or -error upon error.
390 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
392 struct cros_ec_device *ec_dev = ckdev->ec;
393 union ec_response_get_next_data event_data = {};
396 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
397 EC_MKBP_EVENT_SWITCH, &event_data,
398 sizeof(event_data.switches));
402 cros_ec_keyb_report_bs(ckdev, EV_SW,
403 get_unaligned_le32(&event_data.switches));
409 * cros_ec_keyb_resume - Resume the keyboard
411 * We use the resume notification as a chance to query the EC for switches.
413 * @dev: The keyboard device
415 * Returns 0 if no error or -error upon error.
417 static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
419 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
422 return cros_ec_keyb_query_switches(ckdev);
428 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
430 * Handles all the bits of the keyboard driver related to non-matrix buttons
431 * and switches, including asking the EC about which are present and telling
432 * the kernel to expect them.
434 * If this device has no support for buttons and switches we'll return no error
435 * but the ckdev->bs_idev will remain NULL when this function exits.
437 * @ckdev: The keyboard device
438 * @expect_buttons_switches: Indicates that EC must report button and/or
441 * Returns 0 if no error or -error upon error.
443 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev,
444 bool expect_buttons_switches)
446 struct cros_ec_device *ec_dev = ckdev->ec;
447 struct device *dev = ckdev->dev;
448 struct input_dev *idev;
449 union ec_response_get_next_data event_data = {};
456 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
457 EC_MKBP_EVENT_BUTTON, &event_data,
458 sizeof(event_data.buttons));
461 buttons = get_unaligned_le32(&event_data.buttons);
463 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
464 EC_MKBP_EVENT_SWITCH, &event_data,
465 sizeof(event_data.switches));
468 switches = get_unaligned_le32(&event_data.switches);
470 if (!buttons && !switches)
471 return expect_buttons_switches ? -EINVAL : 0;
474 * We call the non-matrix buttons/switches 'input1', if present.
475 * Allocate phys before input dev, to ensure correct tear-down
478 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
482 idev = devm_input_allocate_device(dev);
486 idev->name = "cros_ec_buttons";
488 __set_bit(EV_REP, idev->evbit);
490 idev->id.bustype = BUS_VIRTUAL;
491 idev->id.version = 1;
492 idev->id.product = 0;
493 idev->dev.parent = dev;
495 input_set_drvdata(idev, ckdev);
496 ckdev->bs_idev = idev;
498 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
499 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
501 if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
502 (map->ev_type == EV_SW && (switches & BIT(map->bit))))
503 input_set_capability(idev, map->ev_type, map->code);
506 ret = cros_ec_keyb_query_switches(ckdev);
508 dev_err(dev, "cannot query switches\n");
512 ret = input_register_device(ckdev->bs_idev);
514 dev_err(dev, "cannot register input device\n");
521 static void cros_ec_keyb_parse_vivaldi_physmap(struct cros_ec_keyb *ckdev)
523 u32 *physmap = ckdev->vdata.function_row_physmap;
524 unsigned int row, col, scancode;
529 n_physmap = device_property_count_u32(ckdev->dev,
530 "function-row-physmap");
534 if (n_physmap >= VIVALDI_MAX_FUNCTION_ROW_KEYS) {
536 "only up to %d top row keys is supported (%d specified)\n",
537 VIVALDI_MAX_FUNCTION_ROW_KEYS, n_physmap);
538 n_physmap = VIVALDI_MAX_FUNCTION_ROW_KEYS;
541 error = device_property_read_u32_array(ckdev->dev,
542 "function-row-physmap",
546 "failed to parse function-row-physmap property: %d\n",
552 * Convert (in place) from row/column encoding to matrix "scancode"
553 * used by the driver.
555 for (i = 0; i < n_physmap; i++) {
556 row = KEY_ROW(physmap[i]);
557 col = KEY_COL(physmap[i]);
558 scancode = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
559 physmap[i] = scancode;
562 ckdev->vdata.num_function_row_keys = n_physmap;
566 * cros_ec_keyb_register_matrix - Register matrix keys
568 * Handles all the bits of the keyboard driver related to matrix keys.
570 * @ckdev: The keyboard device
572 * Returns 0 if no error or -error upon error.
574 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
576 struct cros_ec_device *ec_dev = ckdev->ec;
577 struct device *dev = ckdev->dev;
578 struct input_dev *idev;
582 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
586 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
587 if (!ckdev->valid_keys)
590 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
591 if (!ckdev->old_kb_state)
595 * We call the keyboard matrix 'input0'. Allocate phys before input
596 * dev, to ensure correct tear-down ordering.
598 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
602 idev = devm_input_allocate_device(dev);
606 idev->name = CROS_EC_DEV_NAME;
608 __set_bit(EV_REP, idev->evbit);
610 idev->id.bustype = BUS_VIRTUAL;
611 idev->id.version = 1;
612 idev->id.product = 0;
613 idev->dev.parent = dev;
615 ckdev->ghost_filter = device_property_read_bool(dev,
616 "google,needs-ghost-filter");
618 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
621 dev_err(dev, "cannot build key matrix\n");
625 ckdev->row_shift = get_count_order(ckdev->cols);
627 input_set_capability(idev, EV_MSC, MSC_SCAN);
628 input_set_drvdata(idev, ckdev);
630 cros_ec_keyb_compute_valid_keys(ckdev);
631 cros_ec_keyb_parse_vivaldi_physmap(ckdev);
633 err = input_register_device(ckdev->idev);
635 dev_err(dev, "cannot register input device\n");
642 static ssize_t function_row_physmap_show(struct device *dev,
643 struct device_attribute *attr,
646 const struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
647 const struct vivaldi_data *data = &ckdev->vdata;
649 return vivaldi_function_row_physmap_show(data, buf);
652 static DEVICE_ATTR_RO(function_row_physmap);
654 static struct attribute *cros_ec_keyb_attrs[] = {
655 &dev_attr_function_row_physmap.attr,
659 static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
660 struct attribute *attr,
663 struct device *dev = kobj_to_dev(kobj);
664 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
666 if (attr == &dev_attr_function_row_physmap.attr &&
667 !ckdev->vdata.num_function_row_keys)
673 static const struct attribute_group cros_ec_keyb_attr_group = {
674 .is_visible = cros_ec_keyb_attr_is_visible,
675 .attrs = cros_ec_keyb_attrs,
678 static int cros_ec_keyb_probe(struct platform_device *pdev)
680 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
681 struct device *dev = &pdev->dev;
682 struct cros_ec_keyb *ckdev;
683 bool buttons_switches_only = device_get_match_data(dev);
689 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
695 dev_set_drvdata(dev, ckdev);
697 if (!buttons_switches_only) {
698 err = cros_ec_keyb_register_matrix(ckdev);
700 dev_err(dev, "cannot register matrix inputs: %d\n",
706 err = cros_ec_keyb_register_bs(ckdev, buttons_switches_only);
708 dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
712 err = devm_device_add_group(dev, &cros_ec_keyb_attr_group);
714 dev_err(dev, "failed to create attributes: %d\n", err);
718 ckdev->notifier.notifier_call = cros_ec_keyb_work;
719 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
722 dev_err(dev, "cannot register notifier: %d\n", err);
726 device_init_wakeup(ckdev->dev, true);
730 static int cros_ec_keyb_remove(struct platform_device *pdev)
732 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
734 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
741 static const struct of_device_id cros_ec_keyb_of_match[] = {
742 { .compatible = "google,cros-ec-keyb" },
743 { .compatible = "google,cros-ec-keyb-switches", .data = (void *)true },
746 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
749 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
751 static struct platform_driver cros_ec_keyb_driver = {
752 .probe = cros_ec_keyb_probe,
753 .remove = cros_ec_keyb_remove,
755 .name = "cros-ec-keyb",
756 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
757 .pm = &cros_ec_keyb_pm_ops,
761 module_platform_driver(cros_ec_keyb_driver);
763 MODULE_LICENSE("GPL v2");
764 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
765 MODULE_ALIAS("platform:cros-ec-keyb");