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