Lines Matching +full:cros +full:- +full:ec +full:- +full:i2c

1 // SPDX-License-Identifier: GPL-2.0
2 // ChromeOS EC keyboard driver
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,
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
16 #include <linux/i2c.h>
35 * @ghost_filter: true to enable the matrix key-ghosting filter
39 * @ec: Top level ChromeOS device to use to talk to EC
41 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
54 struct cros_ec_device *ec; member
63 * cros_ec_bs_map - Struct mapping Linux keycodes to EC button/switch bitmap
79 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
119 struct device *dev = ckdev->dev; in cros_ec_keyb_has_ghosting()
120 uint8_t *valid_keys = ckdev->valid_keys; in cros_ec_keyb_has_ghosting()
135 for (col1 = 0; col1 < ckdev->cols; col1++) { in cros_ec_keyb_has_ghosting()
137 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) { in cros_ec_keyb_has_ghosting()
159 struct input_dev *idev = ckdev->idev; in cros_ec_keyb_process()
164 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) { in cros_ec_keyb_process()
166 * Simple-minded solution: ignore this state. The obvious in cros_ec_keyb_process()
170 dev_dbg(ckdev->dev, "ghosting found\n"); in cros_ec_keyb_process()
174 for (col = 0; col < ckdev->cols; col++) { in cros_ec_keyb_process()
175 for (row = 0; row < ckdev->rows; row++) { in cros_ec_keyb_process()
176 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift); in cros_ec_keyb_process()
177 const unsigned short *keycodes = idev->keycode; in cros_ec_keyb_process()
180 old_state = ckdev->old_kb_state[col] & (1 << row); in cros_ec_keyb_process()
182 dev_dbg(ckdev->dev, in cros_ec_keyb_process()
191 ckdev->old_kb_state[col] = kb_state[col]; in cros_ec_keyb_process()
193 input_sync(ckdev->idev); in cros_ec_keyb_process()
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,
204 * @mask: A bitmap of buttons from the EC.
210 struct input_dev *idev = ckdev->bs_idev; in cros_ec_keyb_report_bs()
216 if (map->ev_type != ev_type) in cros_ec_keyb_report_bs()
219 input_event(idev, ev_type, map->code, in cros_ec_keyb_report_bs()
220 !!(mask & BIT(map->bit)) ^ map->inverted); in cros_ec_keyb_report_bs()
235 * suspend. Switches will be re-checked in in cros_ec_keyb_work()
238 if (queued_during_suspend && !device_may_wakeup(ckdev->dev)) in cros_ec_keyb_work()
241 switch (ckdev->ec->event_data.event_type) { in cros_ec_keyb_work()
243 pm_wakeup_event(ckdev->dev, 0); in cros_ec_keyb_work()
245 if (ckdev->ec->event_size != ckdev->cols) { in cros_ec_keyb_work()
246 dev_err(ckdev->dev, in cros_ec_keyb_work()
252 ckdev->ec->event_data.data.key_matrix, in cros_ec_keyb_work()
253 ckdev->ec->event_size); in cros_ec_keyb_work()
257 pm_wakeup_event(ckdev->dev, 0); in cros_ec_keyb_work()
259 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq); in cros_ec_keyb_work()
260 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val); in cros_ec_keyb_work()
266 pm_wakeup_event(ckdev->dev, 0); in cros_ec_keyb_work()
268 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) { in cros_ec_keyb_work()
270 &ckdev->ec->event_data.data.buttons); in cros_ec_keyb_work()
274 &ckdev->ec->event_data.data.switches); in cros_ec_keyb_work()
294 int row_shift = ckdev->row_shift; in cros_ec_keyb_compute_valid_keys()
295 unsigned short *keymap = ckdev->idev->keycode; in cros_ec_keyb_compute_valid_keys()
298 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap)); in cros_ec_keyb_compute_valid_keys()
300 for (col = 0; col < ckdev->cols; col++) { in cros_ec_keyb_compute_valid_keys()
301 for (row = 0; row < ckdev->rows; row++) { in cros_ec_keyb_compute_valid_keys()
304 ckdev->valid_keys[col] |= 1 << row; in cros_ec_keyb_compute_valid_keys()
306 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n", in cros_ec_keyb_compute_valid_keys()
307 col, ckdev->valid_keys[col]); in cros_ec_keyb_compute_valid_keys()
312 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
317 * @ec_dev: The EC device
326 * Returns 0 if no error or -error upon error.
341 return -ENOMEM; in cros_ec_keyb_info()
343 msg->command = EC_CMD_MKBP_INFO; in cros_ec_keyb_info()
344 msg->version = 1; in cros_ec_keyb_info()
345 msg->outsize = sizeof(*params); in cros_ec_keyb_info()
346 msg->insize = result_size; in cros_ec_keyb_info()
347 params = (struct ec_params_mkbp_info *)msg->data; in cros_ec_keyb_info()
348 params->info_type = info_type; in cros_ec_keyb_info()
349 params->event_type = event_type; in cros_ec_keyb_info()
352 if (ret == -ENOPROTOOPT) { in cros_ec_keyb_info()
357 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n", in cros_ec_keyb_info()
360 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n", in cros_ec_keyb_info()
363 ret = -EPROTO; in cros_ec_keyb_info()
365 memcpy(result, msg->data, result_size); in cros_ec_keyb_info()
375 * cros_ec_keyb_query_switches - Query the state of switches and report
377 * This will ask the EC about the current state of switches and report to the
383 * Returns 0 if no error or -error upon error.
387 struct cros_ec_device *ec_dev = ckdev->ec; in cros_ec_keyb_query_switches()
404 * cros_ec_keyb_resume - Resume the keyboard
406 * We use the resume notification as a chance to query the EC for switches.
410 * Returns 0 if no error or -error upon error.
416 if (ckdev->bs_idev) in cros_ec_keyb_resume()
423 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
425 * Handles all the bits of the keyboard driver related to non-matrix buttons
426 * and switches, including asking the EC about which are present and telling
430 * but the ckdev->bs_idev will remain NULL when this function exits.
434 * Returns 0 if no error or -error upon error.
438 struct cros_ec_device *ec_dev = ckdev->ec; in cros_ec_keyb_register_bs()
439 struct device *dev = ckdev->dev; in cros_ec_keyb_register_bs()
466 * We call the non-matrix buttons/switches 'input1', if present. in cros_ec_keyb_register_bs()
467 * Allocate phys before input dev, to ensure correct tear-down in cros_ec_keyb_register_bs()
470 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name); in cros_ec_keyb_register_bs()
472 return -ENOMEM; in cros_ec_keyb_register_bs()
476 return -ENOMEM; in cros_ec_keyb_register_bs()
478 idev->name = "cros_ec_buttons"; in cros_ec_keyb_register_bs()
479 idev->phys = phys; in cros_ec_keyb_register_bs()
480 __set_bit(EV_REP, idev->evbit); in cros_ec_keyb_register_bs()
482 idev->id.bustype = BUS_VIRTUAL; in cros_ec_keyb_register_bs()
483 idev->id.version = 1; in cros_ec_keyb_register_bs()
484 idev->id.product = 0; in cros_ec_keyb_register_bs()
485 idev->dev.parent = dev; in cros_ec_keyb_register_bs()
488 ckdev->bs_idev = idev; in cros_ec_keyb_register_bs()
493 if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) || in cros_ec_keyb_register_bs()
494 (map->ev_type == EV_SW && (switches & BIT(map->bit)))) in cros_ec_keyb_register_bs()
495 input_set_capability(idev, map->ev_type, map->code); in cros_ec_keyb_register_bs()
504 ret = input_register_device(ckdev->bs_idev); in cros_ec_keyb_register_bs()
514 * cros_ec_keyb_register_bs - Register matrix keys
520 * Returns 0 if no error or -error upon error.
524 struct cros_ec_device *ec_dev = ckdev->ec; in cros_ec_keyb_register_matrix()
525 struct device *dev = ckdev->dev; in cros_ec_keyb_register_matrix()
530 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols); in cros_ec_keyb_register_matrix()
534 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL); in cros_ec_keyb_register_matrix()
535 if (!ckdev->valid_keys) in cros_ec_keyb_register_matrix()
536 return -ENOMEM; in cros_ec_keyb_register_matrix()
538 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL); in cros_ec_keyb_register_matrix()
539 if (!ckdev->old_kb_state) in cros_ec_keyb_register_matrix()
540 return -ENOMEM; in cros_ec_keyb_register_matrix()
544 * dev, to ensure correct tear-down ordering. in cros_ec_keyb_register_matrix()
546 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name); in cros_ec_keyb_register_matrix()
548 return -ENOMEM; in cros_ec_keyb_register_matrix()
552 return -ENOMEM; in cros_ec_keyb_register_matrix()
554 idev->name = CROS_EC_DEV_NAME; in cros_ec_keyb_register_matrix()
555 idev->phys = phys; in cros_ec_keyb_register_matrix()
556 __set_bit(EV_REP, idev->evbit); in cros_ec_keyb_register_matrix()
558 idev->id.bustype = BUS_VIRTUAL; in cros_ec_keyb_register_matrix()
559 idev->id.version = 1; in cros_ec_keyb_register_matrix()
560 idev->id.product = 0; in cros_ec_keyb_register_matrix()
561 idev->dev.parent = dev; in cros_ec_keyb_register_matrix()
563 ckdev->ghost_filter = of_property_read_bool(dev->of_node, in cros_ec_keyb_register_matrix()
564 "google,needs-ghost-filter"); in cros_ec_keyb_register_matrix()
566 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols, in cros_ec_keyb_register_matrix()
573 ckdev->row_shift = get_count_order(ckdev->cols); in cros_ec_keyb_register_matrix()
577 ckdev->idev = idev; in cros_ec_keyb_register_matrix()
580 err = input_register_device(ckdev->idev); in cros_ec_keyb_register_matrix()
591 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); in cros_ec_keyb_probe() local
592 struct device *dev = &pdev->dev; in cros_ec_keyb_probe()
596 if (!dev->of_node) in cros_ec_keyb_probe()
597 return -ENODEV; in cros_ec_keyb_probe()
601 return -ENOMEM; in cros_ec_keyb_probe()
603 ckdev->ec = ec; in cros_ec_keyb_probe()
604 ckdev->dev = dev; in cros_ec_keyb_probe()
615 dev_err(dev, "cannot register non-matrix inputs: %d\n", err); in cros_ec_keyb_probe()
619 ckdev->notifier.notifier_call = cros_ec_keyb_work; in cros_ec_keyb_probe()
620 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier, in cros_ec_keyb_probe()
621 &ckdev->notifier); in cros_ec_keyb_probe()
627 device_init_wakeup(ckdev->dev, true); in cros_ec_keyb_probe()
633 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev); in cros_ec_keyb_remove()
635 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier, in cros_ec_keyb_remove()
636 &ckdev->notifier); in cros_ec_keyb_remove()
643 { .compatible = "google,cros-ec-keyb" },
655 .name = "cros-ec-keyb",
664 MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
665 MODULE_ALIAS("platform:cros-ec-keyb");