1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Elan I2C/SMBus Touchpad driver 4 * 5 * Copyright (c) 2013 ELAN Microelectronics Corp. 6 * 7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> 8 * Author: KT Liao <kt.liao@emc.com.tw> 9 * Version: 1.6.3 10 * 11 * Based on cyapa driver: 12 * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 13 * copyright (c) 2011-2012 Google, Inc. 14 * 15 * Trademarks are the property of their respective owners. 16 */ 17 18 #include <linux/acpi.h> 19 #include <linux/delay.h> 20 #include <linux/device.h> 21 #include <linux/firmware.h> 22 #include <linux/i2c.h> 23 #include <linux/init.h> 24 #include <linux/input/mt.h> 25 #include <linux/interrupt.h> 26 #include <linux/irq.h> 27 #include <linux/module.h> 28 #include <linux/slab.h> 29 #include <linux/kernel.h> 30 #include <linux/sched.h> 31 #include <linux/input.h> 32 #include <linux/uaccess.h> 33 #include <linux/jiffies.h> 34 #include <linux/completion.h> 35 #include <linux/of.h> 36 #include <linux/pm_wakeirq.h> 37 #include <linux/property.h> 38 #include <linux/regulator/consumer.h> 39 #include <linux/unaligned.h> 40 41 #include "elan_i2c.h" 42 43 #define DRIVER_NAME "elan_i2c" 44 #define ELAN_VENDOR_ID 0x04f3 45 #define ETP_MAX_PRESSURE 255 46 #define ETP_FWIDTH_REDUCE 90 47 #define ETP_FINGER_WIDTH 15 48 #define ETP_RETRY_COUNT 3 49 50 /* quirks to control the device */ 51 #define ETP_QUIRK_QUICK_WAKEUP BIT(0) 52 53 /* The main device structure */ 54 struct elan_tp_data { 55 struct i2c_client *client; 56 struct input_dev *input; 57 struct input_dev *tp_input; /* trackpoint input node */ 58 struct regulator *vcc; 59 60 const struct elan_transport_ops *ops; 61 62 /* for fw update */ 63 struct completion fw_completion; 64 bool in_fw_update; 65 66 struct mutex sysfs_mutex; 67 68 unsigned int max_x; 69 unsigned int max_y; 70 unsigned int width_x; 71 unsigned int width_y; 72 unsigned int x_res; 73 unsigned int y_res; 74 75 u8 pattern; 76 u16 product_id; 77 u8 fw_version; 78 u8 sm_version; 79 u8 iap_version; 80 u16 fw_checksum; 81 unsigned int report_features; 82 unsigned int report_len; 83 int pressure_adjustment; 84 u8 mode; 85 u16 ic_type; 86 u16 fw_validpage_count; 87 u16 fw_page_size; 88 u32 fw_signature_address; 89 90 u8 min_baseline; 91 u8 max_baseline; 92 bool baseline_ready; 93 u8 clickpad; 94 bool middle_button; 95 96 u32 quirks; /* Various quirks */ 97 }; 98 99 static u32 elan_i2c_lookup_quirks(u16 ic_type, u16 product_id) 100 { 101 static const struct { 102 u16 ic_type; 103 u16 product_id; 104 u32 quirks; 105 } elan_i2c_quirks[] = { 106 { 0x0D, ETP_PRODUCT_ID_DELBIN, ETP_QUIRK_QUICK_WAKEUP }, 107 { 0x0D, ETP_PRODUCT_ID_WHITEBOX, ETP_QUIRK_QUICK_WAKEUP }, 108 { 0x10, ETP_PRODUCT_ID_VOXEL, ETP_QUIRK_QUICK_WAKEUP }, 109 { 0x14, ETP_PRODUCT_ID_MAGPIE, ETP_QUIRK_QUICK_WAKEUP }, 110 { 0x14, ETP_PRODUCT_ID_BOBBA, ETP_QUIRK_QUICK_WAKEUP }, 111 }; 112 u32 quirks = 0; 113 int i; 114 115 for (i = 0; i < ARRAY_SIZE(elan_i2c_quirks); i++) { 116 if (elan_i2c_quirks[i].ic_type == ic_type && 117 elan_i2c_quirks[i].product_id == product_id) { 118 quirks = elan_i2c_quirks[i].quirks; 119 } 120 } 121 122 if (ic_type >= 0x0D && product_id >= 0x123) 123 quirks |= ETP_QUIRK_QUICK_WAKEUP; 124 125 return quirks; 126 } 127 128 static int elan_get_fwinfo(u16 ic_type, u8 iap_version, u16 *validpage_count, 129 u32 *signature_address, u16 *page_size) 130 { 131 switch (ic_type) { 132 case 0x00: 133 case 0x06: 134 case 0x08: 135 *validpage_count = 512; 136 break; 137 case 0x03: 138 case 0x07: 139 case 0x09: 140 case 0x0A: 141 case 0x0B: 142 case 0x0C: 143 *validpage_count = 768; 144 break; 145 case 0x0D: 146 *validpage_count = 896; 147 break; 148 case 0x0E: 149 *validpage_count = 640; 150 break; 151 case 0x10: 152 *validpage_count = 1024; 153 break; 154 case 0x11: 155 *validpage_count = 1280; 156 break; 157 case 0x13: 158 *validpage_count = 2048; 159 break; 160 case 0x14: 161 case 0x15: 162 *validpage_count = 1024; 163 break; 164 default: 165 /* unknown ic type clear value */ 166 *validpage_count = 0; 167 *signature_address = 0; 168 *page_size = 0; 169 return -ENXIO; 170 } 171 172 *signature_address = 173 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE; 174 175 if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) { 176 *validpage_count /= 8; 177 *page_size = ETP_FW_PAGE_SIZE_512; 178 } else if (ic_type >= 0x0D && iap_version >= 1) { 179 *validpage_count /= 2; 180 *page_size = ETP_FW_PAGE_SIZE_128; 181 } else { 182 *page_size = ETP_FW_PAGE_SIZE; 183 } 184 185 return 0; 186 } 187 188 static int elan_set_power(struct elan_tp_data *data, bool on) 189 { 190 int repeat = ETP_RETRY_COUNT; 191 int error; 192 193 do { 194 error = data->ops->power_control(data->client, on); 195 if (error >= 0) 196 return 0; 197 198 msleep(30); 199 } while (--repeat > 0); 200 201 dev_err(&data->client->dev, "failed to set power %s: %d\n", 202 on ? "on" : "off", error); 203 return error; 204 } 205 206 static int elan_sleep(struct elan_tp_data *data) 207 { 208 int repeat = ETP_RETRY_COUNT; 209 int error; 210 211 do { 212 error = data->ops->sleep_control(data->client, true); 213 if (!error) 214 return 0; 215 216 msleep(30); 217 } while (--repeat > 0); 218 219 return error; 220 } 221 222 static int elan_query_product(struct elan_tp_data *data) 223 { 224 int error; 225 226 error = data->ops->get_product_id(data->client, &data->product_id); 227 if (error) 228 return error; 229 230 error = data->ops->get_pattern(data->client, &data->pattern); 231 if (error) 232 return error; 233 234 error = data->ops->get_sm_version(data->client, data->pattern, 235 &data->ic_type, &data->sm_version, 236 &data->clickpad); 237 if (error) 238 return error; 239 240 return 0; 241 } 242 243 static int elan_check_ASUS_special_fw(struct elan_tp_data *data) 244 { 245 if (data->ic_type == 0x0E) { 246 switch (data->product_id) { 247 case 0x05 ... 0x07: 248 case 0x09: 249 case 0x13: 250 return true; 251 } 252 } else if (data->ic_type == 0x08 && data->product_id == 0x26) { 253 /* ASUS EeeBook X205TA */ 254 return true; 255 } 256 257 return false; 258 } 259 260 static int __elan_initialize(struct elan_tp_data *data, bool skip_reset) 261 { 262 struct i2c_client *client = data->client; 263 bool woken_up = false; 264 int error; 265 266 if (!skip_reset) { 267 error = data->ops->initialize(client); 268 if (error) { 269 dev_err(&client->dev, "device initialize failed: %d\n", error); 270 return error; 271 } 272 } 273 274 error = elan_query_product(data); 275 if (error) 276 return error; 277 278 /* 279 * Some ASUS devices were shipped with firmware that requires 280 * touchpads to be woken up first, before attempting to switch 281 * them into absolute reporting mode. 282 */ 283 if (elan_check_ASUS_special_fw(data)) { 284 error = data->ops->sleep_control(client, false); 285 if (error) { 286 dev_err(&client->dev, 287 "failed to wake device up: %d\n", error); 288 return error; 289 } 290 291 msleep(200); 292 woken_up = true; 293 } 294 295 data->mode |= ETP_ENABLE_ABS; 296 error = data->ops->set_mode(client, data->mode); 297 if (error) { 298 dev_err(&client->dev, 299 "failed to switch to absolute mode: %d\n", error); 300 return error; 301 } 302 303 if (!woken_up) { 304 error = data->ops->sleep_control(client, false); 305 if (error) { 306 dev_err(&client->dev, 307 "failed to wake device up: %d\n", error); 308 return error; 309 } 310 } 311 312 return 0; 313 } 314 315 static int elan_initialize(struct elan_tp_data *data, bool skip_reset) 316 { 317 int repeat = ETP_RETRY_COUNT; 318 int error; 319 320 do { 321 error = __elan_initialize(data, skip_reset); 322 if (!error) 323 return 0; 324 325 skip_reset = false; 326 msleep(30); 327 } while (--repeat > 0); 328 329 return error; 330 } 331 332 static int elan_query_device_info(struct elan_tp_data *data) 333 { 334 int error; 335 336 error = data->ops->get_version(data->client, data->pattern, false, 337 &data->fw_version); 338 if (error) 339 return error; 340 341 error = data->ops->get_checksum(data->client, false, 342 &data->fw_checksum); 343 if (error) 344 return error; 345 346 error = data->ops->get_version(data->client, data->pattern, 347 true, &data->iap_version); 348 if (error) 349 return error; 350 351 error = data->ops->get_pressure_adjustment(data->client, 352 &data->pressure_adjustment); 353 if (error) 354 return error; 355 356 error = data->ops->get_report_features(data->client, data->pattern, 357 &data->report_features, 358 &data->report_len); 359 if (error) 360 return error; 361 362 data->quirks = elan_i2c_lookup_quirks(data->ic_type, data->product_id); 363 364 error = elan_get_fwinfo(data->ic_type, data->iap_version, 365 &data->fw_validpage_count, 366 &data->fw_signature_address, 367 &data->fw_page_size); 368 if (error) 369 dev_warn(&data->client->dev, 370 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n", 371 data->iap_version, data->ic_type); 372 373 return 0; 374 } 375 376 static unsigned int elan_convert_resolution(u8 val, u8 pattern) 377 { 378 /* 379 * pattern <= 0x01: 380 * (value from firmware) * 10 + 790 = dpi 381 * else 382 * ((value from firmware) + 3) * 100 = dpi 383 */ 384 int res = pattern <= 0x01 ? 385 (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100; 386 /* 387 * We also have to convert dpi to dots/mm (*10/254 to avoid floating 388 * point). 389 */ 390 return res * 10 / 254; 391 } 392 393 static int elan_query_device_parameters(struct elan_tp_data *data) 394 { 395 struct i2c_client *client = data->client; 396 unsigned int x_traces, y_traces; 397 u32 x_mm, y_mm; 398 u8 hw_x_res, hw_y_res; 399 int error; 400 401 if (device_property_read_u32(&client->dev, 402 "touchscreen-size-x", &data->max_x) || 403 device_property_read_u32(&client->dev, 404 "touchscreen-size-y", &data->max_y)) { 405 error = data->ops->get_max(data->client, 406 &data->max_x, 407 &data->max_y); 408 if (error) 409 return error; 410 } else { 411 /* size is the maximum + 1 */ 412 --data->max_x; 413 --data->max_y; 414 } 415 416 if (device_property_read_u32(&client->dev, 417 "elan,x_traces", 418 &x_traces) || 419 device_property_read_u32(&client->dev, 420 "elan,y_traces", 421 &y_traces)) { 422 error = data->ops->get_num_traces(data->client, 423 &x_traces, &y_traces); 424 if (error) 425 return error; 426 } 427 data->width_x = data->max_x / x_traces; 428 data->width_y = data->max_y / y_traces; 429 430 if (device_property_read_u32(&client->dev, 431 "touchscreen-x-mm", &x_mm) || 432 device_property_read_u32(&client->dev, 433 "touchscreen-y-mm", &y_mm)) { 434 error = data->ops->get_resolution(data->client, 435 &hw_x_res, &hw_y_res); 436 if (error) 437 return error; 438 439 data->x_res = elan_convert_resolution(hw_x_res, data->pattern); 440 data->y_res = elan_convert_resolution(hw_y_res, data->pattern); 441 } else { 442 data->x_res = (data->max_x + 1) / x_mm; 443 data->y_res = (data->max_y + 1) / y_mm; 444 } 445 446 if (device_property_read_bool(&client->dev, "elan,clickpad")) 447 data->clickpad = 1; 448 449 if (device_property_read_bool(&client->dev, "elan,middle-button")) 450 data->middle_button = true; 451 452 return 0; 453 } 454 455 /* 456 ********************************************************** 457 * IAP firmware updater related routines 458 ********************************************************** 459 */ 460 static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size, 461 const u8 *page, u16 checksum, int idx) 462 { 463 int retry = ETP_RETRY_COUNT; 464 int error; 465 466 do { 467 error = data->ops->write_fw_block(data->client, page_size, 468 page, checksum, idx); 469 if (!error) 470 return 0; 471 472 dev_dbg(&data->client->dev, 473 "IAP retrying page %d (error: %d)\n", idx, error); 474 } while (--retry > 0); 475 476 return error; 477 } 478 479 static int __elan_update_firmware(struct elan_tp_data *data, 480 const struct firmware *fw) 481 { 482 struct i2c_client *client = data->client; 483 struct device *dev = &client->dev; 484 int i, j; 485 int error; 486 u16 iap_start_addr; 487 u16 boot_page_count; 488 u16 sw_checksum = 0, fw_checksum = 0; 489 490 error = data->ops->prepare_fw_update(client, data->ic_type, 491 data->iap_version, 492 data->fw_page_size); 493 if (error) 494 return error; 495 496 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]); 497 498 boot_page_count = (iap_start_addr * 2) / data->fw_page_size; 499 for (i = boot_page_count; i < data->fw_validpage_count; i++) { 500 u16 checksum = 0; 501 const u8 *page = &fw->data[i * data->fw_page_size]; 502 503 for (j = 0; j < data->fw_page_size; j += 2) 504 checksum += ((page[j + 1] << 8) | page[j]); 505 506 error = elan_write_fw_block(data, data->fw_page_size, 507 page, checksum, i); 508 if (error) { 509 dev_err(dev, "write page %d fail: %d\n", i, error); 510 return error; 511 } 512 513 sw_checksum += checksum; 514 } 515 516 /* Wait WDT reset and power on reset */ 517 msleep(600); 518 519 error = data->ops->finish_fw_update(client, &data->fw_completion); 520 if (error) 521 return error; 522 523 error = data->ops->get_checksum(client, true, &fw_checksum); 524 if (error) 525 return error; 526 527 if (sw_checksum != fw_checksum) { 528 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n", 529 sw_checksum, fw_checksum); 530 return -EIO; 531 } 532 533 return 0; 534 } 535 536 static int elan_update_firmware(struct elan_tp_data *data, 537 const struct firmware *fw) 538 { 539 struct i2c_client *client = data->client; 540 int retval; 541 542 dev_dbg(&client->dev, "Starting firmware update....\n"); 543 544 guard(disable_irq)(&client->irq); 545 546 data->in_fw_update = true; 547 548 retval = __elan_update_firmware(data, fw); 549 if (retval) { 550 dev_err(&client->dev, "firmware update failed: %d\n", retval); 551 data->ops->iap_reset(client); 552 } else { 553 /* Reinitialize TP after fw is updated */ 554 elan_initialize(data, false); 555 elan_query_device_info(data); 556 } 557 558 data->in_fw_update = false; 559 560 return retval; 561 } 562 563 /* 564 ******************************************************************* 565 * SYSFS attributes 566 ******************************************************************* 567 */ 568 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev, 569 struct device_attribute *attr, 570 char *buf) 571 { 572 struct i2c_client *client = to_i2c_client(dev); 573 struct elan_tp_data *data = i2c_get_clientdata(client); 574 575 return sysfs_emit(buf, "0x%04x\n", data->fw_checksum); 576 } 577 578 static ssize_t elan_sysfs_read_product_id(struct device *dev, 579 struct device_attribute *attr, 580 char *buf) 581 { 582 struct i2c_client *client = to_i2c_client(dev); 583 struct elan_tp_data *data = i2c_get_clientdata(client); 584 585 return sysfs_emit(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n", 586 data->product_id); 587 } 588 589 static ssize_t elan_sysfs_read_fw_ver(struct device *dev, 590 struct device_attribute *attr, 591 char *buf) 592 { 593 struct i2c_client *client = to_i2c_client(dev); 594 struct elan_tp_data *data = i2c_get_clientdata(client); 595 596 return sysfs_emit(buf, "%d.0\n", data->fw_version); 597 } 598 599 static ssize_t elan_sysfs_read_sm_ver(struct device *dev, 600 struct device_attribute *attr, 601 char *buf) 602 { 603 struct i2c_client *client = to_i2c_client(dev); 604 struct elan_tp_data *data = i2c_get_clientdata(client); 605 606 return sysfs_emit(buf, "%d.0\n", data->sm_version); 607 } 608 609 static ssize_t elan_sysfs_read_iap_ver(struct device *dev, 610 struct device_attribute *attr, 611 char *buf) 612 { 613 struct i2c_client *client = to_i2c_client(dev); 614 struct elan_tp_data *data = i2c_get_clientdata(client); 615 616 return sysfs_emit(buf, "%d.0\n", data->iap_version); 617 } 618 619 static ssize_t elan_sysfs_update_fw(struct device *dev, 620 struct device_attribute *attr, 621 const char *buf, size_t count) 622 { 623 struct elan_tp_data *data = dev_get_drvdata(dev); 624 int error; 625 const u8 *fw_signature; 626 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF}; 627 628 if (data->fw_validpage_count == 0) 629 return -EINVAL; 630 631 /* Look for a firmware with the product id appended. */ 632 const char *fw_name __free(kfree) = 633 kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id); 634 if (!fw_name) { 635 dev_err(dev, "failed to allocate memory for firmware name\n"); 636 return -ENOMEM; 637 } 638 639 dev_info(dev, "requesting fw '%s'\n", fw_name); 640 const struct firmware *fw __free(firmware) = NULL; 641 error = request_firmware(&fw, fw_name, dev); 642 if (error) { 643 dev_err(dev, "failed to request firmware: %d\n", error); 644 return error; 645 } 646 647 /* Firmware file must match signature data */ 648 fw_signature = &fw->data[data->fw_signature_address]; 649 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) { 650 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n", 651 (int)sizeof(signature), signature, 652 (int)sizeof(signature), fw_signature); 653 return -EBADF; 654 } 655 656 scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { 657 error = elan_update_firmware(data, fw); 658 if (error) 659 return error; 660 } 661 662 return count; 663 } 664 665 static int elan_calibrate(struct elan_tp_data *data) 666 { 667 struct i2c_client *client = data->client; 668 struct device *dev = &client->dev; 669 int tries = 20; 670 int retval; 671 int error; 672 u8 val[ETP_CALIBRATE_MAX_LEN]; 673 674 guard(disable_irq)(&client->irq); 675 676 data->mode |= ETP_ENABLE_CALIBRATE; 677 retval = data->ops->set_mode(client, data->mode); 678 if (retval) { 679 data->mode &= ~ETP_ENABLE_CALIBRATE; 680 dev_err(dev, "failed to enable calibration mode: %d\n", 681 retval); 682 return retval; 683 } 684 685 retval = data->ops->calibrate(client); 686 if (retval) { 687 dev_err(dev, "failed to start calibration: %d\n", 688 retval); 689 goto out_disable_calibrate; 690 } 691 692 val[0] = 0xff; 693 do { 694 /* Wait 250ms before checking if calibration has completed. */ 695 msleep(250); 696 697 retval = data->ops->calibrate_result(client, val); 698 if (retval) 699 dev_err(dev, "failed to check calibration result: %d\n", 700 retval); 701 else if (val[0] == 0) 702 break; /* calibration done */ 703 704 } while (--tries); 705 706 if (tries == 0) { 707 dev_err(dev, "failed to calibrate. Timeout.\n"); 708 retval = -ETIMEDOUT; 709 } 710 711 out_disable_calibrate: 712 data->mode &= ~ETP_ENABLE_CALIBRATE; 713 error = data->ops->set_mode(data->client, data->mode); 714 if (error) { 715 dev_err(dev, "failed to disable calibration mode: %d\n", 716 error); 717 if (!retval) 718 retval = error; 719 } 720 return retval; 721 } 722 723 static ssize_t calibrate_store(struct device *dev, 724 struct device_attribute *attr, 725 const char *buf, size_t count) 726 { 727 struct i2c_client *client = to_i2c_client(dev); 728 struct elan_tp_data *data = i2c_get_clientdata(client); 729 int error; 730 731 scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { 732 error = elan_calibrate(data); 733 if (error) 734 return error; 735 } 736 737 return count; 738 } 739 740 static ssize_t elan_sysfs_read_mode(struct device *dev, 741 struct device_attribute *attr, 742 char *buf) 743 { 744 struct i2c_client *client = to_i2c_client(dev); 745 struct elan_tp_data *data = i2c_get_clientdata(client); 746 int error; 747 enum tp_mode mode; 748 749 scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { 750 error = data->ops->iap_get_mode(data->client, &mode); 751 if (error) 752 return error; 753 } 754 755 return sysfs_emit(buf, "%d\n", (int)mode); 756 } 757 758 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL); 759 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL); 760 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL); 761 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL); 762 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL); 763 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL); 764 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw); 765 766 static DEVICE_ATTR_WO(calibrate); 767 768 static struct attribute *elan_sysfs_entries[] = { 769 &dev_attr_product_id.attr, 770 &dev_attr_firmware_version.attr, 771 &dev_attr_sample_version.attr, 772 &dev_attr_iap_version.attr, 773 &dev_attr_fw_checksum.attr, 774 &dev_attr_calibrate.attr, 775 &dev_attr_mode.attr, 776 &dev_attr_update_fw.attr, 777 NULL, 778 }; 779 780 static const struct attribute_group elan_sysfs_group = { 781 .attrs = elan_sysfs_entries, 782 }; 783 784 static int elan_acquire_baseline(struct elan_tp_data *data) 785 { 786 struct i2c_client *client = data->client; 787 struct device *dev = &client->dev; 788 int retval; 789 int error; 790 791 guard(disable_irq)(&client->irq); 792 793 data->baseline_ready = false; 794 795 data->mode |= ETP_ENABLE_CALIBRATE; 796 retval = data->ops->set_mode(client, data->mode); 797 if (retval) { 798 data->mode &= ~ETP_ENABLE_CALIBRATE; 799 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n", 800 retval); 801 return retval; 802 } 803 804 msleep(250); 805 806 retval = data->ops->get_baseline_data(client, true, 807 &data->max_baseline); 808 if (retval) { 809 dev_err(dev, "Failed to read max baseline from device: %d\n", 810 retval); 811 goto out_disable_calibrate; 812 } 813 814 retval = data->ops->get_baseline_data(client, false, 815 &data->min_baseline); 816 if (retval) { 817 dev_err(dev, "Failed to read min baseline from device: %d\n", 818 retval); 819 goto out_disable_calibrate; 820 } 821 822 data->baseline_ready = true; 823 824 out_disable_calibrate: 825 data->mode &= ~ETP_ENABLE_CALIBRATE; 826 error = data->ops->set_mode(client, data->mode); 827 if (error) { 828 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n", 829 error); 830 if (!retval) 831 retval = error; 832 } 833 834 return retval; 835 } 836 837 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr, 838 const char *buf, size_t count) 839 { 840 struct i2c_client *client = to_i2c_client(dev); 841 struct elan_tp_data *data = i2c_get_clientdata(client); 842 int error; 843 844 scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { 845 error = elan_acquire_baseline(data); 846 if (error) 847 return error; 848 } 849 850 return count; 851 } 852 853 static ssize_t min_show(struct device *dev, 854 struct device_attribute *attr, char *buf) 855 { 856 struct i2c_client *client = to_i2c_client(dev); 857 struct elan_tp_data *data = i2c_get_clientdata(client); 858 859 scoped_guard(mutex_intr, &data->sysfs_mutex) { 860 if (!data->baseline_ready) 861 return -ENODATA; 862 863 return sysfs_emit(buf, "%d", data->min_baseline); 864 } 865 866 return -EINTR; 867 } 868 869 static ssize_t max_show(struct device *dev, 870 struct device_attribute *attr, char *buf) 871 { 872 struct i2c_client *client = to_i2c_client(dev); 873 struct elan_tp_data *data = i2c_get_clientdata(client); 874 875 scoped_guard(mutex_intr, &data->sysfs_mutex) { 876 if (!data->baseline_ready) 877 return -ENODATA; 878 879 return sysfs_emit(buf, "%d", data->max_baseline); 880 } 881 882 return -EINTR; 883 } 884 885 static DEVICE_ATTR_WO(acquire); 886 static DEVICE_ATTR_RO(min); 887 static DEVICE_ATTR_RO(max); 888 889 static struct attribute *elan_baseline_sysfs_entries[] = { 890 &dev_attr_acquire.attr, 891 &dev_attr_min.attr, 892 &dev_attr_max.attr, 893 NULL, 894 }; 895 896 static const struct attribute_group elan_baseline_sysfs_group = { 897 .name = "baseline", 898 .attrs = elan_baseline_sysfs_entries, 899 }; 900 901 static const struct attribute_group *elan_sysfs_groups[] = { 902 &elan_sysfs_group, 903 &elan_baseline_sysfs_group, 904 NULL 905 }; 906 907 /* 908 ****************************************************************** 909 * Elan isr functions 910 ****************************************************************** 911 */ 912 static void elan_report_contact(struct elan_tp_data *data, int contact_num, 913 bool contact_valid, bool high_precision, 914 u8 *packet, u8 *finger_data) 915 { 916 struct input_dev *input = data->input; 917 unsigned int pos_x, pos_y; 918 unsigned int pressure, scaled_pressure; 919 920 if (contact_valid) { 921 if (high_precision) { 922 pos_x = get_unaligned_be16(&finger_data[0]); 923 pos_y = get_unaligned_be16(&finger_data[2]); 924 } else { 925 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1]; 926 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2]; 927 } 928 929 if (pos_x > data->max_x || pos_y > data->max_y) { 930 dev_dbg(input->dev.parent, 931 "[%d] x=%d y=%d over max (%d, %d)", 932 contact_num, pos_x, pos_y, 933 data->max_x, data->max_y); 934 return; 935 } 936 937 pressure = finger_data[4]; 938 scaled_pressure = pressure + data->pressure_adjustment; 939 if (scaled_pressure > ETP_MAX_PRESSURE) 940 scaled_pressure = ETP_MAX_PRESSURE; 941 942 input_mt_slot(input, contact_num); 943 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 944 input_report_abs(input, ABS_MT_POSITION_X, pos_x); 945 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y); 946 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure); 947 948 if (data->report_features & ETP_FEATURE_REPORT_MK) { 949 unsigned int mk_x, mk_y, area_x, area_y; 950 u8 mk_data = high_precision ? 951 packet[ETP_MK_DATA_OFFSET + contact_num] : 952 finger_data[3]; 953 954 mk_x = mk_data & 0x0f; 955 mk_y = mk_data >> 4; 956 957 /* 958 * To avoid treating large finger as palm, let's reduce 959 * the width x and y per trace. 960 */ 961 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE); 962 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE); 963 964 input_report_abs(input, ABS_TOOL_WIDTH, mk_x); 965 input_report_abs(input, ABS_MT_TOUCH_MAJOR, 966 max(area_x, area_y)); 967 input_report_abs(input, ABS_MT_TOUCH_MINOR, 968 min(area_x, area_y)); 969 } 970 } else { 971 input_mt_slot(input, contact_num); 972 input_mt_report_slot_inactive(input); 973 } 974 } 975 976 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet, 977 bool high_precision) 978 { 979 struct input_dev *input = data->input; 980 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET]; 981 int i; 982 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET]; 983 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET]; 984 bool contact_valid, hover_event; 985 986 pm_wakeup_event(&data->client->dev, 0); 987 988 hover_event = hover_info & BIT(6); 989 990 for (i = 0; i < ETP_MAX_FINGERS; i++) { 991 contact_valid = tp_info & BIT(3 + i); 992 elan_report_contact(data, i, contact_valid, high_precision, 993 packet, finger_data); 994 if (contact_valid) 995 finger_data += ETP_FINGER_DATA_LEN; 996 } 997 998 input_report_key(input, BTN_LEFT, tp_info & BIT(0)); 999 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2)); 1000 input_report_key(input, BTN_RIGHT, tp_info & BIT(1)); 1001 input_report_abs(input, ABS_DISTANCE, hover_event != 0); 1002 input_mt_report_pointer_emulation(input, true); 1003 input_sync(input); 1004 } 1005 1006 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report) 1007 { 1008 struct input_dev *input = data->tp_input; 1009 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1]; 1010 int x, y; 1011 1012 pm_wakeup_event(&data->client->dev, 0); 1013 1014 if (!data->tp_input) { 1015 dev_warn_once(&data->client->dev, 1016 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n"); 1017 return; 1018 } 1019 1020 input_report_key(input, BTN_LEFT, packet[0] & 0x01); 1021 input_report_key(input, BTN_RIGHT, packet[0] & 0x02); 1022 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04); 1023 1024 if ((packet[3] & 0x0F) == 0x06) { 1025 x = packet[4] - (int)((packet[1] ^ 0x80) << 1); 1026 y = (int)((packet[2] ^ 0x80) << 1) - packet[5]; 1027 1028 input_report_rel(input, REL_X, x); 1029 input_report_rel(input, REL_Y, y); 1030 } 1031 1032 input_sync(input); 1033 } 1034 1035 static irqreturn_t elan_isr(int irq, void *dev_id) 1036 { 1037 struct elan_tp_data *data = dev_id; 1038 int error; 1039 u8 report[ETP_MAX_REPORT_LEN]; 1040 1041 /* 1042 * When device is connected to i2c bus, when all IAP page writes 1043 * complete, the driver will receive interrupt and must read 1044 * 0000 to confirm that IAP is finished. 1045 */ 1046 if (data->in_fw_update) { 1047 complete(&data->fw_completion); 1048 goto out; 1049 } 1050 1051 error = data->ops->get_report(data->client, report, data->report_len); 1052 if (error) 1053 goto out; 1054 1055 switch (report[ETP_REPORT_ID_OFFSET]) { 1056 case ETP_REPORT_ID: 1057 elan_report_absolute(data, report, false); 1058 break; 1059 case ETP_REPORT_ID2: 1060 elan_report_absolute(data, report, true); 1061 break; 1062 case ETP_TP_REPORT_ID: 1063 case ETP_TP_REPORT_ID2: 1064 elan_report_trackpoint(data, report); 1065 break; 1066 default: 1067 dev_err(&data->client->dev, "invalid report id data (%x)\n", 1068 report[ETP_REPORT_ID_OFFSET]); 1069 } 1070 1071 out: 1072 return IRQ_HANDLED; 1073 } 1074 1075 /* 1076 ****************************************************************** 1077 * Elan initialization functions 1078 ****************************************************************** 1079 */ 1080 1081 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data) 1082 { 1083 struct device *dev = &data->client->dev; 1084 struct input_dev *input; 1085 1086 input = devm_input_allocate_device(dev); 1087 if (!input) 1088 return -ENOMEM; 1089 1090 input->name = "Elan TrackPoint"; 1091 input->id.bustype = BUS_I2C; 1092 input->id.vendor = ELAN_VENDOR_ID; 1093 input->id.product = data->product_id; 1094 input_set_drvdata(input, data); 1095 1096 input_set_capability(input, EV_REL, REL_X); 1097 input_set_capability(input, EV_REL, REL_Y); 1098 input_set_capability(input, EV_KEY, BTN_LEFT); 1099 input_set_capability(input, EV_KEY, BTN_RIGHT); 1100 input_set_capability(input, EV_KEY, BTN_MIDDLE); 1101 1102 __set_bit(INPUT_PROP_POINTER, input->propbit); 1103 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit); 1104 1105 data->tp_input = input; 1106 1107 return 0; 1108 } 1109 1110 static int elan_setup_input_device(struct elan_tp_data *data) 1111 { 1112 struct device *dev = &data->client->dev; 1113 struct input_dev *input; 1114 unsigned int max_width = max(data->width_x, data->width_y); 1115 unsigned int min_width = min(data->width_x, data->width_y); 1116 int error; 1117 1118 input = devm_input_allocate_device(dev); 1119 if (!input) 1120 return -ENOMEM; 1121 1122 input->name = "Elan Touchpad"; 1123 input->id.bustype = BUS_I2C; 1124 input->id.vendor = ELAN_VENDOR_ID; 1125 input->id.product = data->product_id; 1126 input_set_drvdata(input, data); 1127 1128 error = input_mt_init_slots(input, ETP_MAX_FINGERS, 1129 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); 1130 if (error) { 1131 dev_err(dev, "failed to initialize MT slots: %d\n", error); 1132 return error; 1133 } 1134 1135 __set_bit(EV_ABS, input->evbit); 1136 __set_bit(INPUT_PROP_POINTER, input->propbit); 1137 if (data->clickpad) { 1138 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 1139 } else { 1140 __set_bit(BTN_RIGHT, input->keybit); 1141 if (data->middle_button) 1142 __set_bit(BTN_MIDDLE, input->keybit); 1143 } 1144 __set_bit(BTN_LEFT, input->keybit); 1145 1146 /* Set up ST parameters */ 1147 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0); 1148 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0); 1149 input_abs_set_res(input, ABS_X, data->x_res); 1150 input_abs_set_res(input, ABS_Y, data->y_res); 1151 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0); 1152 if (data->report_features & ETP_FEATURE_REPORT_MK) 1153 input_set_abs_params(input, ABS_TOOL_WIDTH, 1154 0, ETP_FINGER_WIDTH, 0, 0); 1155 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0); 1156 1157 /* And MT parameters */ 1158 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0); 1159 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0); 1160 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res); 1161 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res); 1162 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 1163 ETP_MAX_PRESSURE, 0, 0); 1164 if (data->report_features & ETP_FEATURE_REPORT_MK) { 1165 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 1166 0, ETP_FINGER_WIDTH * max_width, 0, 0); 1167 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 1168 0, ETP_FINGER_WIDTH * min_width, 0, 0); 1169 } 1170 1171 data->input = input; 1172 1173 return 0; 1174 } 1175 1176 static void elan_disable_regulator(void *_data) 1177 { 1178 struct elan_tp_data *data = _data; 1179 1180 regulator_disable(data->vcc); 1181 } 1182 1183 static int elan_probe(struct i2c_client *client) 1184 { 1185 const struct elan_transport_ops *transport_ops; 1186 struct device *dev = &client->dev; 1187 struct elan_tp_data *data; 1188 unsigned long irqflags; 1189 int error; 1190 1191 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) && 1192 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1193 transport_ops = &elan_i2c_ops; 1194 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) && 1195 i2c_check_functionality(client->adapter, 1196 I2C_FUNC_SMBUS_BYTE_DATA | 1197 I2C_FUNC_SMBUS_BLOCK_DATA | 1198 I2C_FUNC_SMBUS_I2C_BLOCK)) { 1199 transport_ops = &elan_smbus_ops; 1200 } else { 1201 dev_err(dev, "not a supported I2C/SMBus adapter\n"); 1202 return -EIO; 1203 } 1204 1205 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL); 1206 if (!data) 1207 return -ENOMEM; 1208 1209 i2c_set_clientdata(client, data); 1210 1211 data->ops = transport_ops; 1212 data->client = client; 1213 init_completion(&data->fw_completion); 1214 mutex_init(&data->sysfs_mutex); 1215 1216 data->vcc = devm_regulator_get(dev, "vcc"); 1217 if (IS_ERR(data->vcc)) 1218 return dev_err_probe(dev, PTR_ERR(data->vcc), "Failed to get 'vcc' regulator\n"); 1219 1220 error = regulator_enable(data->vcc); 1221 if (error) { 1222 dev_err(dev, "Failed to enable regulator: %d\n", error); 1223 return error; 1224 } 1225 1226 error = devm_add_action_or_reset(dev, elan_disable_regulator, data); 1227 if (error) { 1228 dev_err(dev, "Failed to add disable regulator action: %d\n", 1229 error); 1230 return error; 1231 } 1232 1233 /* Make sure there is something at this address */ 1234 error = i2c_smbus_read_byte(client); 1235 if (error < 0) { 1236 dev_dbg(&client->dev, "nothing at this address: %d\n", error); 1237 return -ENXIO; 1238 } 1239 1240 /* Initialize the touchpad. */ 1241 error = elan_initialize(data, false); 1242 if (error) 1243 return error; 1244 1245 error = elan_query_device_info(data); 1246 if (error) 1247 return error; 1248 1249 error = elan_query_device_parameters(data); 1250 if (error) 1251 return error; 1252 1253 dev_info(dev, 1254 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n", 1255 data->product_id, 1256 data->fw_version, 1257 data->sm_version, 1258 data->iap_version); 1259 1260 dev_dbg(dev, 1261 "Elan Touchpad Extra Information:\n" 1262 " Max ABS X,Y: %d,%d\n" 1263 " Width X,Y: %d,%d\n" 1264 " Resolution X,Y: %d,%d (dots/mm)\n" 1265 " ic type: 0x%x\n" 1266 " info pattern: 0x%x\n", 1267 data->max_x, data->max_y, 1268 data->width_x, data->width_y, 1269 data->x_res, data->y_res, 1270 data->ic_type, data->pattern); 1271 1272 /* Set up input device properties based on queried parameters. */ 1273 error = elan_setup_input_device(data); 1274 if (error) 1275 return error; 1276 1277 if (device_property_read_bool(&client->dev, "elan,trackpoint")) { 1278 error = elan_setup_trackpoint_input_device(data); 1279 if (error) 1280 return error; 1281 } 1282 1283 /* 1284 * Platform code (ACPI, DTS) should normally set up interrupt 1285 * for us, but in case it did not let's fall back to using falling 1286 * edge to be compatible with older Chromebooks. 1287 */ 1288 irqflags = irq_get_trigger_type(client->irq); 1289 if (!irqflags) 1290 irqflags = IRQF_TRIGGER_FALLING; 1291 1292 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr, 1293 irqflags | IRQF_ONESHOT, 1294 client->name, data); 1295 if (error) { 1296 dev_err(dev, "cannot register irq=%d\n", client->irq); 1297 return error; 1298 } 1299 1300 error = input_register_device(data->input); 1301 if (error) { 1302 dev_err(dev, "failed to register input device: %d\n", error); 1303 return error; 1304 } 1305 1306 if (data->tp_input) { 1307 error = input_register_device(data->tp_input); 1308 if (error) { 1309 dev_err(&client->dev, 1310 "failed to register TrackPoint input device: %d\n", 1311 error); 1312 return error; 1313 } 1314 } 1315 1316 return 0; 1317 } 1318 1319 static int __elan_suspend(struct elan_tp_data *data) 1320 { 1321 struct i2c_client *client = data->client; 1322 int error; 1323 1324 if (device_may_wakeup(&client->dev)) 1325 return elan_sleep(data); 1326 1327 /* Touchpad is not a wakeup source */ 1328 error = elan_set_power(data, false); 1329 if (error) 1330 return error; 1331 1332 error = regulator_disable(data->vcc); 1333 if (error) { 1334 dev_err(&client->dev, 1335 "failed to disable regulator when suspending: %d\n", 1336 error); 1337 /* Attempt to power the chip back up */ 1338 elan_set_power(data, true); 1339 return error; 1340 } 1341 1342 return 0; 1343 } 1344 1345 static int elan_suspend(struct device *dev) 1346 { 1347 struct i2c_client *client = to_i2c_client(dev); 1348 struct elan_tp_data *data = i2c_get_clientdata(client); 1349 int error; 1350 1351 /* 1352 * We are taking the mutex to make sure sysfs operations are 1353 * complete before we attempt to bring the device into low[er] 1354 * power mode. 1355 */ 1356 scoped_cond_guard(mutex_intr, return -EINTR, &data->sysfs_mutex) { 1357 disable_irq(client->irq); 1358 1359 error = __elan_suspend(data); 1360 if (error) { 1361 enable_irq(client->irq); 1362 return error; 1363 } 1364 } 1365 1366 return 0; 1367 } 1368 1369 static int elan_resume(struct device *dev) 1370 { 1371 struct i2c_client *client = to_i2c_client(dev); 1372 struct elan_tp_data *data = i2c_get_clientdata(client); 1373 int error; 1374 1375 if (!device_may_wakeup(dev)) { 1376 error = regulator_enable(data->vcc); 1377 if (error) { 1378 dev_err(dev, "error %d enabling regulator\n", error); 1379 goto err; 1380 } 1381 } 1382 1383 error = elan_set_power(data, true); 1384 if (error) { 1385 dev_err(dev, "power up when resuming failed: %d\n", error); 1386 goto err; 1387 } 1388 1389 error = elan_initialize(data, data->quirks & ETP_QUIRK_QUICK_WAKEUP); 1390 if (error) 1391 dev_err(dev, "initialize when resuming failed: %d\n", error); 1392 1393 err: 1394 enable_irq(data->client->irq); 1395 return error; 1396 } 1397 1398 static DEFINE_SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume); 1399 1400 static const struct i2c_device_id elan_id[] = { 1401 { DRIVER_NAME }, 1402 { } 1403 }; 1404 MODULE_DEVICE_TABLE(i2c, elan_id); 1405 1406 #ifdef CONFIG_ACPI 1407 #include <linux/input/elan-i2c-ids.h> 1408 MODULE_DEVICE_TABLE(acpi, elan_acpi_id); 1409 #endif 1410 1411 #ifdef CONFIG_OF 1412 static const struct of_device_id elan_of_match[] = { 1413 { .compatible = "elan,ekth3000" }, 1414 { /* sentinel */ } 1415 }; 1416 MODULE_DEVICE_TABLE(of, elan_of_match); 1417 #endif 1418 1419 static struct i2c_driver elan_driver = { 1420 .driver = { 1421 .name = DRIVER_NAME, 1422 .pm = pm_sleep_ptr(&elan_pm_ops), 1423 .acpi_match_table = ACPI_PTR(elan_acpi_id), 1424 .of_match_table = of_match_ptr(elan_of_match), 1425 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1426 .dev_groups = elan_sysfs_groups, 1427 }, 1428 .probe = elan_probe, 1429 .id_table = elan_id, 1430 }; 1431 1432 module_i2c_driver(elan_driver); 1433 1434 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>"); 1435 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver"); 1436 MODULE_LICENSE("GPL"); 1437