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