1 // SPDX-License-Identifier: GPL-2.0 2 // ChromeOS EC keyboard driver 3 // 4 // Copyright (C) 2012 Google, Inc. 5 // 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 12 // expensive. 13 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/platform_data/cros_ec_commands.h> 26 #include <linux/platform_data/cros_ec_proto.h> 27 28 #include <asm/unaligned.h> 29 30 /** 31 * struct cros_ec_keyb - Structure representing EC keyboard device 32 * 33 * @rows: Number of rows in the keypad 34 * @cols: Number of columns in the keypad 35 * @row_shift: log2 or number of rows, rounded up 36 * @keymap_data: Matrix keymap data used to convert to keyscan values 37 * @ghost_filter: true to enable the matrix key-ghosting filter 38 * @valid_keys: bitmap of existing keys for each matrix column 39 * @old_kb_state: bitmap of keys pressed last scan 40 * @dev: Device pointer 41 * @ec: Top level ChromeOS device to use to talk to EC 42 * @idev: The input device for the matrix keys. 43 * @bs_idev: The input device for non-matrix buttons and switches (or NULL). 44 * @notifier: interrupt event notifier for transport devices 45 */ 46 struct cros_ec_keyb { 47 unsigned int rows; 48 unsigned int cols; 49 int row_shift; 50 const struct matrix_keymap_data *keymap_data; 51 bool ghost_filter; 52 uint8_t *valid_keys; 53 uint8_t *old_kb_state; 54 55 struct device *dev; 56 struct cros_ec_device *ec; 57 58 struct input_dev *idev; 59 struct input_dev *bs_idev; 60 struct notifier_block notifier; 61 }; 62 63 /** 64 * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch 65 * bitmap #defines 66 * 67 * @ev_type: The type of the input event to generate (e.g., EV_KEY). 68 * @code: A linux keycode 69 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN 70 * @inverted: If the #define and EV_SW have opposite meanings, this is true. 71 * Only applicable to switches. 72 */ 73 struct cros_ec_bs_map { 74 unsigned int ev_type; 75 unsigned int code; 76 u8 bit; 77 bool inverted; 78 }; 79 80 /* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */ 81 static const struct cros_ec_bs_map cros_ec_keyb_bs[] = { 82 /* Buttons */ 83 { 84 .ev_type = EV_KEY, 85 .code = KEY_POWER, 86 .bit = EC_MKBP_POWER_BUTTON, 87 }, 88 { 89 .ev_type = EV_KEY, 90 .code = KEY_VOLUMEUP, 91 .bit = EC_MKBP_VOL_UP, 92 }, 93 { 94 .ev_type = EV_KEY, 95 .code = KEY_VOLUMEDOWN, 96 .bit = EC_MKBP_VOL_DOWN, 97 }, 98 99 /* Switches */ 100 { 101 .ev_type = EV_SW, 102 .code = SW_LID, 103 .bit = EC_MKBP_LID_OPEN, 104 .inverted = true, 105 }, 106 { 107 .ev_type = EV_SW, 108 .code = SW_TABLET_MODE, 109 .bit = EC_MKBP_TABLET_MODE, 110 }, 111 }; 112 113 /* 114 * Returns true when there is at least one combination of pressed keys that 115 * results in ghosting. 116 */ 117 static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf) 118 { 119 int col1, col2, buf1, buf2; 120 struct device *dev = ckdev->dev; 121 uint8_t *valid_keys = ckdev->valid_keys; 122 123 /* 124 * Ghosting happens if for any pressed key X there are other keys 125 * pressed both in the same row and column of X as, for instance, 126 * in the following diagram: 127 * 128 * . . Y . g . 129 * . . . . . . 130 * . . . . . . 131 * . . X . Z . 132 * 133 * In this case only X, Y, and Z are pressed, but g appears to be 134 * pressed too (see Wikipedia). 135 */ 136 for (col1 = 0; col1 < ckdev->cols; col1++) { 137 buf1 = buf[col1] & valid_keys[col1]; 138 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) { 139 buf2 = buf[col2] & valid_keys[col2]; 140 if (hweight8(buf1 & buf2) > 1) { 141 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x", 142 col1, buf1, col2, buf2); 143 return true; 144 } 145 } 146 } 147 148 return false; 149 } 150 151 152 /* 153 * Compares the new keyboard state to the old one and produces key 154 * press/release events accordingly. The keyboard state is 13 bytes (one byte 155 * per column) 156 */ 157 static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev, 158 uint8_t *kb_state, int len) 159 { 160 struct input_dev *idev = ckdev->idev; 161 int col, row; 162 int new_state; 163 int old_state; 164 165 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) { 166 /* 167 * Simple-minded solution: ignore this state. The obvious 168 * improvement is to only ignore changes to keys involved in 169 * the ghosting, but process the other changes. 170 */ 171 dev_dbg(ckdev->dev, "ghosting found\n"); 172 return; 173 } 174 175 for (col = 0; col < ckdev->cols; col++) { 176 for (row = 0; row < ckdev->rows; row++) { 177 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift); 178 const unsigned short *keycodes = idev->keycode; 179 180 new_state = kb_state[col] & (1 << row); 181 old_state = ckdev->old_kb_state[col] & (1 << row); 182 if (new_state != old_state) { 183 dev_dbg(ckdev->dev, 184 "changed: [r%d c%d]: byte %02x\n", 185 row, col, new_state); 186 187 input_event(idev, EV_MSC, MSC_SCAN, pos); 188 input_report_key(idev, keycodes[pos], 189 new_state); 190 } 191 } 192 ckdev->old_kb_state[col] = kb_state[col]; 193 } 194 input_sync(ckdev->idev); 195 } 196 197 /** 198 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches 199 * 200 * This takes a bitmap of buttons or switches from the EC and reports events, 201 * syncing at the end. 202 * 203 * @ckdev: The keyboard device. 204 * @ev_type: The input event type (e.g., EV_KEY). 205 * @mask: A bitmap of buttons from the EC. 206 */ 207 static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev, 208 unsigned int ev_type, u32 mask) 209 210 { 211 struct input_dev *idev = ckdev->bs_idev; 212 int i; 213 214 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) { 215 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i]; 216 217 if (map->ev_type != ev_type) 218 continue; 219 220 input_event(idev, ev_type, map->code, 221 !!(mask & BIT(map->bit)) ^ map->inverted); 222 } 223 input_sync(idev); 224 } 225 226 static int cros_ec_keyb_work(struct notifier_block *nb, 227 unsigned long queued_during_suspend, void *_notify) 228 { 229 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb, 230 notifier); 231 u32 val; 232 unsigned int ev_type; 233 234 /* 235 * If not wake enabled, discard key state changes during 236 * suspend. Switches will be re-checked in 237 * cros_ec_keyb_resume() to be sure nothing is lost. 238 */ 239 if (queued_during_suspend && !device_may_wakeup(ckdev->dev)) 240 return NOTIFY_OK; 241 242 switch (ckdev->ec->event_data.event_type) { 243 case EC_MKBP_EVENT_KEY_MATRIX: 244 pm_wakeup_event(ckdev->dev, 0); 245 246 if (ckdev->ec->event_size != ckdev->cols) { 247 dev_err(ckdev->dev, 248 "Discarded incomplete key matrix event.\n"); 249 return NOTIFY_OK; 250 } 251 252 cros_ec_keyb_process(ckdev, 253 ckdev->ec->event_data.data.key_matrix, 254 ckdev->ec->event_size); 255 break; 256 257 case EC_MKBP_EVENT_SYSRQ: 258 pm_wakeup_event(ckdev->dev, 0); 259 260 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq); 261 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val); 262 handle_sysrq(val); 263 break; 264 265 case EC_MKBP_EVENT_BUTTON: 266 case EC_MKBP_EVENT_SWITCH: 267 pm_wakeup_event(ckdev->dev, 0); 268 269 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) { 270 val = get_unaligned_le32( 271 &ckdev->ec->event_data.data.buttons); 272 ev_type = EV_KEY; 273 } else { 274 val = get_unaligned_le32( 275 &ckdev->ec->event_data.data.switches); 276 ev_type = EV_SW; 277 } 278 cros_ec_keyb_report_bs(ckdev, ev_type, val); 279 break; 280 281 default: 282 return NOTIFY_DONE; 283 } 284 285 return NOTIFY_OK; 286 } 287 288 /* 289 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by 290 * ghosting logic to ignore NULL or virtual keys. 291 */ 292 static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev) 293 { 294 int row, col; 295 int row_shift = ckdev->row_shift; 296 unsigned short *keymap = ckdev->idev->keycode; 297 unsigned short code; 298 299 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap)); 300 301 for (col = 0; col < ckdev->cols; col++) { 302 for (row = 0; row < ckdev->rows; row++) { 303 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)]; 304 if (code && (code != KEY_BATTERY)) 305 ckdev->valid_keys[col] |= 1 << row; 306 } 307 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n", 308 col, ckdev->valid_keys[col]); 309 } 310 } 311 312 /** 313 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO 314 * 315 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and 316 * unmarshalling and different version nonsense into something simple. 317 * 318 * @ec_dev: The EC device 319 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT. 320 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually 321 * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or 322 * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver. 323 * @result: Where we'll store the result; a union 324 * @result_size: The size of the result. Expected to be the size of one of 325 * the elements in the union. 326 * 327 * Returns 0 if no error or -error upon error. 328 */ 329 static int cros_ec_keyb_info(struct cros_ec_device *ec_dev, 330 enum ec_mkbp_info_type info_type, 331 enum ec_mkbp_event event_type, 332 union ec_response_get_next_data *result, 333 size_t result_size) 334 { 335 struct ec_params_mkbp_info *params; 336 struct cros_ec_command *msg; 337 int ret; 338 339 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size, 340 sizeof(*params)), GFP_KERNEL); 341 if (!msg) 342 return -ENOMEM; 343 344 msg->command = EC_CMD_MKBP_INFO; 345 msg->version = 1; 346 msg->outsize = sizeof(*params); 347 msg->insize = result_size; 348 params = (struct ec_params_mkbp_info *)msg->data; 349 params->info_type = info_type; 350 params->event_type = event_type; 351 352 ret = cros_ec_cmd_xfer_status(ec_dev, msg); 353 if (ret == -ENOPROTOOPT) { 354 /* With older ECs we just return 0 for everything */ 355 memset(result, 0, result_size); 356 ret = 0; 357 } else if (ret < 0) { 358 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n", 359 (int)info_type, (int)event_type, ret); 360 } else if (ret != result_size) { 361 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n", 362 (int)info_type, (int)event_type, 363 ret, result_size); 364 ret = -EPROTO; 365 } else { 366 memcpy(result, msg->data, result_size); 367 ret = 0; 368 } 369 370 kfree(msg); 371 372 return ret; 373 } 374 375 /** 376 * cros_ec_keyb_query_switches - Query the state of switches and report 377 * 378 * This will ask the EC about the current state of switches and report to the 379 * kernel. Note that we don't query for buttons because they are more 380 * transitory and we'll get an update on the next release / press. 381 * 382 * @ckdev: The keyboard device 383 * 384 * Returns 0 if no error or -error upon error. 385 */ 386 static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev) 387 { 388 struct cros_ec_device *ec_dev = ckdev->ec; 389 union ec_response_get_next_data event_data = {}; 390 int ret; 391 392 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT, 393 EC_MKBP_EVENT_SWITCH, &event_data, 394 sizeof(event_data.switches)); 395 if (ret) 396 return ret; 397 398 cros_ec_keyb_report_bs(ckdev, EV_SW, 399 get_unaligned_le32(&event_data.switches)); 400 401 return 0; 402 } 403 404 /** 405 * cros_ec_keyb_resume - Resume the keyboard 406 * 407 * We use the resume notification as a chance to query the EC for switches. 408 * 409 * @dev: The keyboard device 410 * 411 * Returns 0 if no error or -error upon error. 412 */ 413 static __maybe_unused int cros_ec_keyb_resume(struct device *dev) 414 { 415 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev); 416 417 if (ckdev->bs_idev) 418 return cros_ec_keyb_query_switches(ckdev); 419 420 return 0; 421 } 422 423 /** 424 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches 425 * 426 * Handles all the bits of the keyboard driver related to non-matrix buttons 427 * and switches, including asking the EC about which are present and telling 428 * the kernel to expect them. 429 * 430 * If this device has no support for buttons and switches we'll return no error 431 * but the ckdev->bs_idev will remain NULL when this function exits. 432 * 433 * @ckdev: The keyboard device 434 * 435 * Returns 0 if no error or -error upon error. 436 */ 437 static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev) 438 { 439 struct cros_ec_device *ec_dev = ckdev->ec; 440 struct device *dev = ckdev->dev; 441 struct input_dev *idev; 442 union ec_response_get_next_data event_data = {}; 443 const char *phys; 444 u32 buttons; 445 u32 switches; 446 int ret; 447 int i; 448 449 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED, 450 EC_MKBP_EVENT_BUTTON, &event_data, 451 sizeof(event_data.buttons)); 452 if (ret) 453 return ret; 454 buttons = get_unaligned_le32(&event_data.buttons); 455 456 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED, 457 EC_MKBP_EVENT_SWITCH, &event_data, 458 sizeof(event_data.switches)); 459 if (ret) 460 return ret; 461 switches = get_unaligned_le32(&event_data.switches); 462 463 if (!buttons && !switches) 464 return 0; 465 466 /* 467 * We call the non-matrix buttons/switches 'input1', if present. 468 * Allocate phys before input dev, to ensure correct tear-down 469 * ordering. 470 */ 471 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name); 472 if (!phys) 473 return -ENOMEM; 474 475 idev = devm_input_allocate_device(dev); 476 if (!idev) 477 return -ENOMEM; 478 479 idev->name = "cros_ec_buttons"; 480 idev->phys = phys; 481 __set_bit(EV_REP, idev->evbit); 482 483 idev->id.bustype = BUS_VIRTUAL; 484 idev->id.version = 1; 485 idev->id.product = 0; 486 idev->dev.parent = dev; 487 488 input_set_drvdata(idev, ckdev); 489 ckdev->bs_idev = idev; 490 491 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) { 492 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i]; 493 494 if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) || 495 (map->ev_type == EV_SW && (switches & BIT(map->bit)))) 496 input_set_capability(idev, map->ev_type, map->code); 497 } 498 499 ret = cros_ec_keyb_query_switches(ckdev); 500 if (ret) { 501 dev_err(dev, "cannot query switches\n"); 502 return ret; 503 } 504 505 ret = input_register_device(ckdev->bs_idev); 506 if (ret) { 507 dev_err(dev, "cannot register input device\n"); 508 return ret; 509 } 510 511 return 0; 512 } 513 514 /** 515 * cros_ec_keyb_register_bs - Register matrix keys 516 * 517 * Handles all the bits of the keyboard driver related to matrix keys. 518 * 519 * @ckdev: The keyboard device 520 * 521 * Returns 0 if no error or -error upon error. 522 */ 523 static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev) 524 { 525 struct cros_ec_device *ec_dev = ckdev->ec; 526 struct device *dev = ckdev->dev; 527 struct input_dev *idev; 528 const char *phys; 529 int err; 530 531 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols); 532 if (err) 533 return err; 534 535 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL); 536 if (!ckdev->valid_keys) 537 return -ENOMEM; 538 539 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL); 540 if (!ckdev->old_kb_state) 541 return -ENOMEM; 542 543 /* 544 * We call the keyboard matrix 'input0'. Allocate phys before input 545 * dev, to ensure correct tear-down ordering. 546 */ 547 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name); 548 if (!phys) 549 return -ENOMEM; 550 551 idev = devm_input_allocate_device(dev); 552 if (!idev) 553 return -ENOMEM; 554 555 idev->name = CROS_EC_DEV_NAME; 556 idev->phys = phys; 557 __set_bit(EV_REP, idev->evbit); 558 559 idev->id.bustype = BUS_VIRTUAL; 560 idev->id.version = 1; 561 idev->id.product = 0; 562 idev->dev.parent = dev; 563 564 ckdev->ghost_filter = of_property_read_bool(dev->of_node, 565 "google,needs-ghost-filter"); 566 567 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols, 568 NULL, idev); 569 if (err) { 570 dev_err(dev, "cannot build key matrix\n"); 571 return err; 572 } 573 574 ckdev->row_shift = get_count_order(ckdev->cols); 575 576 input_set_capability(idev, EV_MSC, MSC_SCAN); 577 input_set_drvdata(idev, ckdev); 578 ckdev->idev = idev; 579 cros_ec_keyb_compute_valid_keys(ckdev); 580 581 err = input_register_device(ckdev->idev); 582 if (err) { 583 dev_err(dev, "cannot register input device\n"); 584 return err; 585 } 586 587 return 0; 588 } 589 590 static int cros_ec_keyb_probe(struct platform_device *pdev) 591 { 592 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); 593 struct device *dev = &pdev->dev; 594 struct cros_ec_keyb *ckdev; 595 int err; 596 597 if (!dev->of_node) 598 return -ENODEV; 599 600 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL); 601 if (!ckdev) 602 return -ENOMEM; 603 604 ckdev->ec = ec; 605 ckdev->dev = dev; 606 dev_set_drvdata(dev, ckdev); 607 608 err = cros_ec_keyb_register_matrix(ckdev); 609 if (err) { 610 dev_err(dev, "cannot register matrix inputs: %d\n", err); 611 return err; 612 } 613 614 err = cros_ec_keyb_register_bs(ckdev); 615 if (err) { 616 dev_err(dev, "cannot register non-matrix inputs: %d\n", err); 617 return err; 618 } 619 620 ckdev->notifier.notifier_call = cros_ec_keyb_work; 621 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier, 622 &ckdev->notifier); 623 if (err) { 624 dev_err(dev, "cannot register notifier: %d\n", err); 625 return err; 626 } 627 628 device_init_wakeup(ckdev->dev, true); 629 return 0; 630 } 631 632 static int cros_ec_keyb_remove(struct platform_device *pdev) 633 { 634 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev); 635 636 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier, 637 &ckdev->notifier); 638 639 return 0; 640 } 641 642 #ifdef CONFIG_OF 643 static const struct of_device_id cros_ec_keyb_of_match[] = { 644 { .compatible = "google,cros-ec-keyb" }, 645 {}, 646 }; 647 MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match); 648 #endif 649 650 static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume); 651 652 static struct platform_driver cros_ec_keyb_driver = { 653 .probe = cros_ec_keyb_probe, 654 .remove = cros_ec_keyb_remove, 655 .driver = { 656 .name = "cros-ec-keyb", 657 .of_match_table = of_match_ptr(cros_ec_keyb_of_match), 658 .pm = &cros_ec_keyb_pm_ops, 659 }, 660 }; 661 662 module_platform_driver(cros_ec_keyb_driver); 663 664 MODULE_LICENSE("GPL v2"); 665 MODULE_DESCRIPTION("ChromeOS EC keyboard driver"); 666 MODULE_ALIAS("platform:cros-ec-keyb"); 667