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