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/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/notifier.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/sysrq.h>
24 #include <linux/input/matrix_keypad.h>
25 #include <linux/mfd/cros_ec.h>
26 #include <linux/mfd/cros_ec_commands.h>
28 #include <asm/unaligned.h>
31 * @rows: Number of rows in the keypad
32 * @cols: Number of columns in the keypad
33 * @row_shift: log2 or number of rows, rounded up
34 * @keymap_data: Matrix keymap data used to convert to keyscan values
35 * @ghost_filter: true to enable the matrix key-ghosting filter
36 * @valid_keys: bitmap of existing keys for each matrix column
37 * @old_kb_state: bitmap of keys pressed last scan
38 * @dev: Device pointer
39 * @ec: Top level ChromeOS device to use to talk to EC
40 * @idev: The input device for the matrix keys.
41 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
42 * @notifier: interrupt event notifier for transport devices
48 const struct matrix_keymap_data *keymap_data;
51 uint8_t *old_kb_state;
54 struct cros_ec_device *ec;
56 struct input_dev *idev;
57 struct input_dev *bs_idev;
58 struct notifier_block notifier;
63 * cros_ec_bs_map - Struct mapping Linux keycodes to EC button/switch bitmap
66 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
67 * @code: A linux keycode
68 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
69 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
70 * Only applicable to switches.
72 struct cros_ec_bs_map {
79 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
80 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
85 .bit = EC_MKBP_POWER_BUTTON,
90 .bit = EC_MKBP_VOL_UP,
94 .code = KEY_VOLUMEDOWN,
95 .bit = EC_MKBP_VOL_DOWN,
102 .bit = EC_MKBP_LID_OPEN,
107 .code = SW_TABLET_MODE,
108 .bit = EC_MKBP_TABLET_MODE,
113 * Returns true when there is at least one combination of pressed keys that
114 * results in ghosting.
116 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
118 int col1, col2, buf1, buf2;
119 struct device *dev = ckdev->dev;
120 uint8_t *valid_keys = ckdev->valid_keys;
123 * Ghosting happens if for any pressed key X there are other keys
124 * pressed both in the same row and column of X as, for instance,
125 * in the following diagram:
132 * In this case only X, Y, and Z are pressed, but g appears to be
133 * pressed too (see Wikipedia).
135 for (col1 = 0; col1 < ckdev->cols; col1++) {
136 buf1 = buf[col1] & valid_keys[col1];
137 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
138 buf2 = buf[col2] & valid_keys[col2];
139 if (hweight8(buf1 & buf2) > 1) {
140 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
141 col1, buf1, col2, buf2);
152 * Compares the new keyboard state to the old one and produces key
153 * press/release events accordingly. The keyboard state is 13 bytes (one byte
156 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
157 uint8_t *kb_state, int len)
159 struct input_dev *idev = ckdev->idev;
164 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
166 * Simple-minded solution: ignore this state. The obvious
167 * improvement is to only ignore changes to keys involved in
168 * the ghosting, but process the other changes.
170 dev_dbg(ckdev->dev, "ghosting found\n");
174 for (col = 0; col < ckdev->cols; col++) {
175 for (row = 0; row < ckdev->rows; row++) {
176 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
177 const unsigned short *keycodes = idev->keycode;
179 new_state = kb_state[col] & (1 << row);
180 old_state = ckdev->old_kb_state[col] & (1 << row);
181 if (new_state != old_state) {
183 "changed: [r%d c%d]: byte %02x\n",
184 row, col, new_state);
186 input_event(idev, EV_MSC, MSC_SCAN, pos);
187 input_report_key(idev, keycodes[pos],
191 ckdev->old_kb_state[col] = kb_state[col];
193 input_sync(ckdev->idev);
197 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
199 * This takes a bitmap of buttons or switches from the EC and reports events,
200 * syncing at the end.
202 * @ckdev: The keyboard device.
203 * @ev_type: The input event type (e.g., EV_KEY).
204 * @mask: A bitmap of buttons from the EC.
206 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
207 unsigned int ev_type, u32 mask)
210 struct input_dev *idev = ckdev->bs_idev;
213 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
214 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
216 if (map->ev_type != ev_type)
219 input_event(idev, ev_type, map->code,
220 !!(mask & BIT(map->bit)) ^ map->inverted);
225 static int cros_ec_keyb_work(struct notifier_block *nb,
226 unsigned long queued_during_suspend, void *_notify)
228 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
231 unsigned int ev_type;
234 * If not wake enabled, discard key state changes during
235 * suspend. Switches will be re-checked in
236 * cros_ec_keyb_resume() to be sure nothing is lost.
238 if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
241 switch (ckdev->ec->event_data.event_type) {
242 case EC_MKBP_EVENT_KEY_MATRIX:
243 pm_wakeup_event(ckdev->dev, 0);
245 if (ckdev->ec->event_size != ckdev->cols) {
247 "Discarded incomplete key matrix event.\n");
251 cros_ec_keyb_process(ckdev,
252 ckdev->ec->event_data.data.key_matrix,
253 ckdev->ec->event_size);
256 case EC_MKBP_EVENT_SYSRQ:
257 pm_wakeup_event(ckdev->dev, 0);
259 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
260 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
264 case EC_MKBP_EVENT_BUTTON:
265 case EC_MKBP_EVENT_SWITCH:
266 pm_wakeup_event(ckdev->dev, 0);
268 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
269 val = get_unaligned_le32(
270 &ckdev->ec->event_data.data.buttons);
273 val = get_unaligned_le32(
274 &ckdev->ec->event_data.data.switches);
277 cros_ec_keyb_report_bs(ckdev, ev_type, val);
288 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
289 * ghosting logic to ignore NULL or virtual keys.
291 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
294 int row_shift = ckdev->row_shift;
295 unsigned short *keymap = ckdev->idev->keycode;
298 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
300 for (col = 0; col < ckdev->cols; col++) {
301 for (row = 0; row < ckdev->rows; row++) {
302 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
303 if (code && (code != KEY_BATTERY))
304 ckdev->valid_keys[col] |= 1 << row;
306 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
307 col, ckdev->valid_keys[col]);
312 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
314 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
315 * unmarshalling and different version nonsense into something simple.
317 * @ec_dev: The EC device
318 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
319 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
320 * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
321 * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
322 * @result: Where we'll store the result; a union
323 * @result_size: The size of the result. Expected to be the size of one of
324 * the elements in the union.
326 * Returns 0 if no error or -error upon error.
328 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
329 enum ec_mkbp_info_type info_type,
330 enum ec_mkbp_event event_type,
331 union ec_response_get_next_data *result,
334 struct ec_params_mkbp_info *params;
335 struct cros_ec_command *msg;
338 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
339 sizeof(*params)), GFP_KERNEL);
343 msg->command = EC_CMD_MKBP_INFO;
345 msg->outsize = sizeof(*params);
346 msg->insize = result_size;
347 params = (struct ec_params_mkbp_info *)msg->data;
348 params->info_type = info_type;
349 params->event_type = event_type;
351 ret = cros_ec_cmd_xfer(ec_dev, msg);
353 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
354 (int)info_type, (int)event_type, ret);
355 } else if (msg->result == EC_RES_INVALID_VERSION) {
356 /* With older ECs we just return 0 for everything */
357 memset(result, 0, result_size);
359 } else if (msg->result != EC_RES_SUCCESS) {
360 dev_warn(ec_dev->dev, "Error getting info %d/%d: %d\n",
361 (int)info_type, (int)event_type, msg->result);
363 } else if (ret != result_size) {
364 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
365 (int)info_type, (int)event_type,
369 memcpy(result, msg->data, result_size);
379 * cros_ec_keyb_query_switches - Query the state of switches and report
381 * This will ask the EC about the current state of switches and report to the
382 * kernel. Note that we don't query for buttons because they are more
383 * transitory and we'll get an update on the next release / press.
385 * @ckdev: The keyboard device
387 * Returns 0 if no error or -error upon error.
389 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
391 struct cros_ec_device *ec_dev = ckdev->ec;
392 union ec_response_get_next_data event_data = {};
395 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
396 EC_MKBP_EVENT_SWITCH, &event_data,
397 sizeof(event_data.switches));
401 cros_ec_keyb_report_bs(ckdev, EV_SW,
402 get_unaligned_le32(&event_data.switches));
408 * cros_ec_keyb_resume - Resume the keyboard
410 * We use the resume notification as a chance to query the EC for switches.
412 * @dev: The keyboard device
414 * Returns 0 if no error or -error upon error.
416 static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
418 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
421 return cros_ec_keyb_query_switches(ckdev);
427 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
429 * Handles all the bits of the keyboard driver related to non-matrix buttons
430 * and switches, including asking the EC about which are present and telling
431 * the kernel to expect them.
433 * If this device has no support for buttons and switches we'll return no error
434 * but the ckdev->bs_idev will remain NULL when this function exits.
436 * @ckdev: The keyboard device
438 * Returns 0 if no error or -error upon error.
440 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
442 struct cros_ec_device *ec_dev = ckdev->ec;
443 struct device *dev = ckdev->dev;
444 struct input_dev *idev;
445 union ec_response_get_next_data event_data = {};
452 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
453 EC_MKBP_EVENT_BUTTON, &event_data,
454 sizeof(event_data.buttons));
457 buttons = get_unaligned_le32(&event_data.buttons);
459 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
460 EC_MKBP_EVENT_SWITCH, &event_data,
461 sizeof(event_data.switches));
464 switches = get_unaligned_le32(&event_data.switches);
466 if (!buttons && !switches)
470 * We call the non-matrix buttons/switches 'input1', if present.
471 * Allocate phys before input dev, to ensure correct tear-down
474 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
478 idev = devm_input_allocate_device(dev);
482 idev->name = "cros_ec_buttons";
484 __set_bit(EV_REP, idev->evbit);
486 idev->id.bustype = BUS_VIRTUAL;
487 idev->id.version = 1;
488 idev->id.product = 0;
489 idev->dev.parent = dev;
491 input_set_drvdata(idev, ckdev);
492 ckdev->bs_idev = idev;
494 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
495 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
497 if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
498 (map->ev_type == EV_SW && (switches & BIT(map->bit))))
499 input_set_capability(idev, map->ev_type, map->code);
502 ret = cros_ec_keyb_query_switches(ckdev);
504 dev_err(dev, "cannot query switches\n");
508 ret = input_register_device(ckdev->bs_idev);
510 dev_err(dev, "cannot register input device\n");
518 * cros_ec_keyb_register_bs - Register matrix keys
520 * Handles all the bits of the keyboard driver related to matrix keys.
522 * @ckdev: The keyboard device
524 * Returns 0 if no error or -error upon error.
526 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
528 struct cros_ec_device *ec_dev = ckdev->ec;
529 struct device *dev = ckdev->dev;
530 struct input_dev *idev;
534 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
538 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
539 if (!ckdev->valid_keys)
542 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
543 if (!ckdev->old_kb_state)
547 * We call the keyboard matrix 'input0'. Allocate phys before input
548 * dev, to ensure correct tear-down ordering.
550 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
554 idev = devm_input_allocate_device(dev);
558 idev->name = CROS_EC_DEV_NAME;
560 __set_bit(EV_REP, idev->evbit);
562 idev->id.bustype = BUS_VIRTUAL;
563 idev->id.version = 1;
564 idev->id.product = 0;
565 idev->dev.parent = dev;
567 ckdev->ghost_filter = of_property_read_bool(dev->of_node,
568 "google,needs-ghost-filter");
570 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
573 dev_err(dev, "cannot build key matrix\n");
577 ckdev->row_shift = get_count_order(ckdev->cols);
579 input_set_capability(idev, EV_MSC, MSC_SCAN);
580 input_set_drvdata(idev, ckdev);
582 cros_ec_keyb_compute_valid_keys(ckdev);
584 err = input_register_device(ckdev->idev);
586 dev_err(dev, "cannot register input device\n");
593 static int cros_ec_keyb_probe(struct platform_device *pdev)
595 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
596 struct device *dev = &pdev->dev;
597 struct cros_ec_keyb *ckdev;
603 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
609 dev_set_drvdata(dev, ckdev);
611 err = cros_ec_keyb_register_matrix(ckdev);
613 dev_err(dev, "cannot register matrix inputs: %d\n", err);
617 err = cros_ec_keyb_register_bs(ckdev);
619 dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
623 ckdev->notifier.notifier_call = cros_ec_keyb_work;
624 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
627 dev_err(dev, "cannot register notifier: %d\n", err);
631 device_init_wakeup(ckdev->dev, true);
635 static int cros_ec_keyb_remove(struct platform_device *pdev)
637 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
639 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
646 static const struct of_device_id cros_ec_keyb_of_match[] = {
647 { .compatible = "google,cros-ec-keyb" },
650 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
653 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
655 static struct platform_driver cros_ec_keyb_driver = {
656 .probe = cros_ec_keyb_probe,
657 .remove = cros_ec_keyb_remove,
659 .name = "cros-ec-keyb",
660 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
661 .pm = &cros_ec_keyb_pm_ops,
665 module_platform_driver(cros_ec_keyb_driver);
667 MODULE_LICENSE("GPL v2");
668 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
669 MODULE_ALIAS("platform:cros-ec-keyb");