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 <asm/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 disable_irq(client->irq); 545 data->in_fw_update = true; 546 547 retval = __elan_update_firmware(data, fw); 548 if (retval) { 549 dev_err(&client->dev, "firmware update failed: %d\n", retval); 550 data->ops->iap_reset(client); 551 } else { 552 /* Reinitialize TP after fw is updated */ 553 elan_initialize(data, false); 554 elan_query_device_info(data); 555 } 556 557 data->in_fw_update = false; 558 enable_irq(client->irq); 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 const struct firmware *fw; 625 char *fw_name; 626 int error; 627 const u8 *fw_signature; 628 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF}; 629 630 if (data->fw_validpage_count == 0) 631 return -EINVAL; 632 633 /* Look for a firmware with the product id appended. */ 634 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id); 635 if (!fw_name) { 636 dev_err(dev, "failed to allocate memory for firmware name\n"); 637 return -ENOMEM; 638 } 639 640 dev_info(dev, "requesting fw '%s'\n", fw_name); 641 error = request_firmware(&fw, fw_name, dev); 642 kfree(fw_name); 643 if (error) { 644 dev_err(dev, "failed to request firmware: %d\n", error); 645 return error; 646 } 647 648 /* Firmware file must match signature data */ 649 fw_signature = &fw->data[data->fw_signature_address]; 650 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) { 651 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n", 652 (int)sizeof(signature), signature, 653 (int)sizeof(signature), fw_signature); 654 error = -EBADF; 655 goto out_release_fw; 656 } 657 658 error = mutex_lock_interruptible(&data->sysfs_mutex); 659 if (error) 660 goto out_release_fw; 661 662 error = elan_update_firmware(data, fw); 663 664 mutex_unlock(&data->sysfs_mutex); 665 666 out_release_fw: 667 release_firmware(fw); 668 return error ?: count; 669 } 670 671 static ssize_t calibrate_store(struct device *dev, 672 struct device_attribute *attr, 673 const char *buf, size_t count) 674 { 675 struct i2c_client *client = to_i2c_client(dev); 676 struct elan_tp_data *data = i2c_get_clientdata(client); 677 int tries = 20; 678 int retval; 679 int error; 680 u8 val[ETP_CALIBRATE_MAX_LEN]; 681 682 retval = mutex_lock_interruptible(&data->sysfs_mutex); 683 if (retval) 684 return retval; 685 686 disable_irq(client->irq); 687 688 data->mode |= ETP_ENABLE_CALIBRATE; 689 retval = data->ops->set_mode(client, data->mode); 690 if (retval) { 691 dev_err(dev, "failed to enable calibration mode: %d\n", 692 retval); 693 goto out; 694 } 695 696 retval = data->ops->calibrate(client); 697 if (retval) { 698 dev_err(dev, "failed to start calibration: %d\n", 699 retval); 700 goto out_disable_calibrate; 701 } 702 703 val[0] = 0xff; 704 do { 705 /* Wait 250ms before checking if calibration has completed. */ 706 msleep(250); 707 708 retval = data->ops->calibrate_result(client, val); 709 if (retval) 710 dev_err(dev, "failed to check calibration result: %d\n", 711 retval); 712 else if (val[0] == 0) 713 break; /* calibration done */ 714 715 } while (--tries); 716 717 if (tries == 0) { 718 dev_err(dev, "failed to calibrate. Timeout.\n"); 719 retval = -ETIMEDOUT; 720 } 721 722 out_disable_calibrate: 723 data->mode &= ~ETP_ENABLE_CALIBRATE; 724 error = data->ops->set_mode(data->client, data->mode); 725 if (error) { 726 dev_err(dev, "failed to disable calibration mode: %d\n", 727 error); 728 if (!retval) 729 retval = error; 730 } 731 out: 732 enable_irq(client->irq); 733 mutex_unlock(&data->sysfs_mutex); 734 return retval ?: count; 735 } 736 737 static ssize_t elan_sysfs_read_mode(struct device *dev, 738 struct device_attribute *attr, 739 char *buf) 740 { 741 struct i2c_client *client = to_i2c_client(dev); 742 struct elan_tp_data *data = i2c_get_clientdata(client); 743 int error; 744 enum tp_mode mode; 745 746 error = mutex_lock_interruptible(&data->sysfs_mutex); 747 if (error) 748 return error; 749 750 error = data->ops->iap_get_mode(data->client, &mode); 751 752 mutex_unlock(&data->sysfs_mutex); 753 754 if (error) 755 return error; 756 757 return sysfs_emit(buf, "%d\n", (int)mode); 758 } 759 760 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL); 761 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL); 762 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL); 763 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL); 764 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL); 765 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL); 766 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw); 767 768 static DEVICE_ATTR_WO(calibrate); 769 770 static struct attribute *elan_sysfs_entries[] = { 771 &dev_attr_product_id.attr, 772 &dev_attr_firmware_version.attr, 773 &dev_attr_sample_version.attr, 774 &dev_attr_iap_version.attr, 775 &dev_attr_fw_checksum.attr, 776 &dev_attr_calibrate.attr, 777 &dev_attr_mode.attr, 778 &dev_attr_update_fw.attr, 779 NULL, 780 }; 781 782 static const struct attribute_group elan_sysfs_group = { 783 .attrs = elan_sysfs_entries, 784 }; 785 786 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr, 787 const char *buf, size_t count) 788 { 789 struct i2c_client *client = to_i2c_client(dev); 790 struct elan_tp_data *data = i2c_get_clientdata(client); 791 int error; 792 int retval; 793 794 retval = mutex_lock_interruptible(&data->sysfs_mutex); 795 if (retval) 796 return retval; 797 798 disable_irq(client->irq); 799 800 data->baseline_ready = false; 801 802 data->mode |= ETP_ENABLE_CALIBRATE; 803 retval = data->ops->set_mode(data->client, data->mode); 804 if (retval) { 805 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n", 806 retval); 807 goto out; 808 } 809 810 msleep(250); 811 812 retval = data->ops->get_baseline_data(data->client, true, 813 &data->max_baseline); 814 if (retval) { 815 dev_err(dev, "Failed to read max baseline form device: %d\n", 816 retval); 817 goto out_disable_calibrate; 818 } 819 820 retval = data->ops->get_baseline_data(data->client, false, 821 &data->min_baseline); 822 if (retval) { 823 dev_err(dev, "Failed to read min baseline form device: %d\n", 824 retval); 825 goto out_disable_calibrate; 826 } 827 828 data->baseline_ready = true; 829 830 out_disable_calibrate: 831 data->mode &= ~ETP_ENABLE_CALIBRATE; 832 error = data->ops->set_mode(data->client, data->mode); 833 if (error) { 834 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n", 835 error); 836 if (!retval) 837 retval = error; 838 } 839 out: 840 enable_irq(client->irq); 841 mutex_unlock(&data->sysfs_mutex); 842 return retval ?: count; 843 } 844 845 static ssize_t min_show(struct device *dev, 846 struct device_attribute *attr, char *buf) 847 { 848 struct i2c_client *client = to_i2c_client(dev); 849 struct elan_tp_data *data = i2c_get_clientdata(client); 850 int retval; 851 852 retval = mutex_lock_interruptible(&data->sysfs_mutex); 853 if (retval) 854 return retval; 855 856 if (!data->baseline_ready) { 857 retval = -ENODATA; 858 goto out; 859 } 860 861 retval = sysfs_emit(buf, "%d", data->min_baseline); 862 863 out: 864 mutex_unlock(&data->sysfs_mutex); 865 return retval; 866 } 867 868 static ssize_t max_show(struct device *dev, 869 struct device_attribute *attr, char *buf) 870 { 871 struct i2c_client *client = to_i2c_client(dev); 872 struct elan_tp_data *data = i2c_get_clientdata(client); 873 int retval; 874 875 retval = mutex_lock_interruptible(&data->sysfs_mutex); 876 if (retval) 877 return retval; 878 879 if (!data->baseline_ready) { 880 retval = -ENODATA; 881 goto out; 882 } 883 884 retval = sysfs_emit(buf, "%d", data->max_baseline); 885 886 out: 887 mutex_unlock(&data->sysfs_mutex); 888 return retval; 889 } 890 891 892 static DEVICE_ATTR_WO(acquire); 893 static DEVICE_ATTR_RO(min); 894 static DEVICE_ATTR_RO(max); 895 896 static struct attribute *elan_baseline_sysfs_entries[] = { 897 &dev_attr_acquire.attr, 898 &dev_attr_min.attr, 899 &dev_attr_max.attr, 900 NULL, 901 }; 902 903 static const struct attribute_group elan_baseline_sysfs_group = { 904 .name = "baseline", 905 .attrs = elan_baseline_sysfs_entries, 906 }; 907 908 static const struct attribute_group *elan_sysfs_groups[] = { 909 &elan_sysfs_group, 910 &elan_baseline_sysfs_group, 911 NULL 912 }; 913 914 /* 915 ****************************************************************** 916 * Elan isr functions 917 ****************************************************************** 918 */ 919 static void elan_report_contact(struct elan_tp_data *data, int contact_num, 920 bool contact_valid, bool high_precision, 921 u8 *packet, u8 *finger_data) 922 { 923 struct input_dev *input = data->input; 924 unsigned int pos_x, pos_y; 925 unsigned int pressure, scaled_pressure; 926 927 if (contact_valid) { 928 if (high_precision) { 929 pos_x = get_unaligned_be16(&finger_data[0]); 930 pos_y = get_unaligned_be16(&finger_data[2]); 931 } else { 932 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1]; 933 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2]; 934 } 935 936 if (pos_x > data->max_x || pos_y > data->max_y) { 937 dev_dbg(input->dev.parent, 938 "[%d] x=%d y=%d over max (%d, %d)", 939 contact_num, pos_x, pos_y, 940 data->max_x, data->max_y); 941 return; 942 } 943 944 pressure = finger_data[4]; 945 scaled_pressure = pressure + data->pressure_adjustment; 946 if (scaled_pressure > ETP_MAX_PRESSURE) 947 scaled_pressure = ETP_MAX_PRESSURE; 948 949 input_mt_slot(input, contact_num); 950 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 951 input_report_abs(input, ABS_MT_POSITION_X, pos_x); 952 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y); 953 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure); 954 955 if (data->report_features & ETP_FEATURE_REPORT_MK) { 956 unsigned int mk_x, mk_y, area_x, area_y; 957 u8 mk_data = high_precision ? 958 packet[ETP_MK_DATA_OFFSET + contact_num] : 959 finger_data[3]; 960 961 mk_x = mk_data & 0x0f; 962 mk_y = mk_data >> 4; 963 964 /* 965 * To avoid treating large finger as palm, let's reduce 966 * the width x and y per trace. 967 */ 968 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE); 969 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE); 970 971 input_report_abs(input, ABS_TOOL_WIDTH, mk_x); 972 input_report_abs(input, ABS_MT_TOUCH_MAJOR, 973 max(area_x, area_y)); 974 input_report_abs(input, ABS_MT_TOUCH_MINOR, 975 min(area_x, area_y)); 976 } 977 } else { 978 input_mt_slot(input, contact_num); 979 input_mt_report_slot_inactive(input); 980 } 981 } 982 983 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet, 984 bool high_precision) 985 { 986 struct input_dev *input = data->input; 987 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET]; 988 int i; 989 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET]; 990 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET]; 991 bool contact_valid, hover_event; 992 993 pm_wakeup_event(&data->client->dev, 0); 994 995 hover_event = hover_info & BIT(6); 996 997 for (i = 0; i < ETP_MAX_FINGERS; i++) { 998 contact_valid = tp_info & BIT(3 + i); 999 elan_report_contact(data, i, contact_valid, high_precision, 1000 packet, finger_data); 1001 if (contact_valid) 1002 finger_data += ETP_FINGER_DATA_LEN; 1003 } 1004 1005 input_report_key(input, BTN_LEFT, tp_info & BIT(0)); 1006 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2)); 1007 input_report_key(input, BTN_RIGHT, tp_info & BIT(1)); 1008 input_report_abs(input, ABS_DISTANCE, hover_event != 0); 1009 input_mt_report_pointer_emulation(input, true); 1010 input_sync(input); 1011 } 1012 1013 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report) 1014 { 1015 struct input_dev *input = data->tp_input; 1016 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1]; 1017 int x, y; 1018 1019 pm_wakeup_event(&data->client->dev, 0); 1020 1021 if (!data->tp_input) { 1022 dev_warn_once(&data->client->dev, 1023 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n"); 1024 return; 1025 } 1026 1027 input_report_key(input, BTN_LEFT, packet[0] & 0x01); 1028 input_report_key(input, BTN_RIGHT, packet[0] & 0x02); 1029 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04); 1030 1031 if ((packet[3] & 0x0F) == 0x06) { 1032 x = packet[4] - (int)((packet[1] ^ 0x80) << 1); 1033 y = (int)((packet[2] ^ 0x80) << 1) - packet[5]; 1034 1035 input_report_rel(input, REL_X, x); 1036 input_report_rel(input, REL_Y, y); 1037 } 1038 1039 input_sync(input); 1040 } 1041 1042 static irqreturn_t elan_isr(int irq, void *dev_id) 1043 { 1044 struct elan_tp_data *data = dev_id; 1045 int error; 1046 u8 report[ETP_MAX_REPORT_LEN]; 1047 1048 /* 1049 * When device is connected to i2c bus, when all IAP page writes 1050 * complete, the driver will receive interrupt and must read 1051 * 0000 to confirm that IAP is finished. 1052 */ 1053 if (data->in_fw_update) { 1054 complete(&data->fw_completion); 1055 goto out; 1056 } 1057 1058 error = data->ops->get_report(data->client, report, data->report_len); 1059 if (error) 1060 goto out; 1061 1062 switch (report[ETP_REPORT_ID_OFFSET]) { 1063 case ETP_REPORT_ID: 1064 elan_report_absolute(data, report, false); 1065 break; 1066 case ETP_REPORT_ID2: 1067 elan_report_absolute(data, report, true); 1068 break; 1069 case ETP_TP_REPORT_ID: 1070 case ETP_TP_REPORT_ID2: 1071 elan_report_trackpoint(data, report); 1072 break; 1073 default: 1074 dev_err(&data->client->dev, "invalid report id data (%x)\n", 1075 report[ETP_REPORT_ID_OFFSET]); 1076 } 1077 1078 out: 1079 return IRQ_HANDLED; 1080 } 1081 1082 /* 1083 ****************************************************************** 1084 * Elan initialization functions 1085 ****************************************************************** 1086 */ 1087 1088 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data) 1089 { 1090 struct device *dev = &data->client->dev; 1091 struct input_dev *input; 1092 1093 input = devm_input_allocate_device(dev); 1094 if (!input) 1095 return -ENOMEM; 1096 1097 input->name = "Elan TrackPoint"; 1098 input->id.bustype = BUS_I2C; 1099 input->id.vendor = ELAN_VENDOR_ID; 1100 input->id.product = data->product_id; 1101 input_set_drvdata(input, data); 1102 1103 input_set_capability(input, EV_REL, REL_X); 1104 input_set_capability(input, EV_REL, REL_Y); 1105 input_set_capability(input, EV_KEY, BTN_LEFT); 1106 input_set_capability(input, EV_KEY, BTN_RIGHT); 1107 input_set_capability(input, EV_KEY, BTN_MIDDLE); 1108 1109 __set_bit(INPUT_PROP_POINTER, input->propbit); 1110 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit); 1111 1112 data->tp_input = input; 1113 1114 return 0; 1115 } 1116 1117 static int elan_setup_input_device(struct elan_tp_data *data) 1118 { 1119 struct device *dev = &data->client->dev; 1120 struct input_dev *input; 1121 unsigned int max_width = max(data->width_x, data->width_y); 1122 unsigned int min_width = min(data->width_x, data->width_y); 1123 int error; 1124 1125 input = devm_input_allocate_device(dev); 1126 if (!input) 1127 return -ENOMEM; 1128 1129 input->name = "Elan Touchpad"; 1130 input->id.bustype = BUS_I2C; 1131 input->id.vendor = ELAN_VENDOR_ID; 1132 input->id.product = data->product_id; 1133 input_set_drvdata(input, data); 1134 1135 error = input_mt_init_slots(input, ETP_MAX_FINGERS, 1136 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); 1137 if (error) { 1138 dev_err(dev, "failed to initialize MT slots: %d\n", error); 1139 return error; 1140 } 1141 1142 __set_bit(EV_ABS, input->evbit); 1143 __set_bit(INPUT_PROP_POINTER, input->propbit); 1144 if (data->clickpad) { 1145 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 1146 } else { 1147 __set_bit(BTN_RIGHT, input->keybit); 1148 if (data->middle_button) 1149 __set_bit(BTN_MIDDLE, input->keybit); 1150 } 1151 __set_bit(BTN_LEFT, input->keybit); 1152 1153 /* Set up ST parameters */ 1154 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0); 1155 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0); 1156 input_abs_set_res(input, ABS_X, data->x_res); 1157 input_abs_set_res(input, ABS_Y, data->y_res); 1158 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0); 1159 if (data->report_features & ETP_FEATURE_REPORT_MK) 1160 input_set_abs_params(input, ABS_TOOL_WIDTH, 1161 0, ETP_FINGER_WIDTH, 0, 0); 1162 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0); 1163 1164 /* And MT parameters */ 1165 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0); 1166 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0); 1167 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res); 1168 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res); 1169 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 1170 ETP_MAX_PRESSURE, 0, 0); 1171 if (data->report_features & ETP_FEATURE_REPORT_MK) { 1172 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 1173 0, ETP_FINGER_WIDTH * max_width, 0, 0); 1174 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 1175 0, ETP_FINGER_WIDTH * min_width, 0, 0); 1176 } 1177 1178 data->input = input; 1179 1180 return 0; 1181 } 1182 1183 static void elan_disable_regulator(void *_data) 1184 { 1185 struct elan_tp_data *data = _data; 1186 1187 regulator_disable(data->vcc); 1188 } 1189 1190 static int elan_probe(struct i2c_client *client) 1191 { 1192 const struct elan_transport_ops *transport_ops; 1193 struct device *dev = &client->dev; 1194 struct elan_tp_data *data; 1195 unsigned long irqflags; 1196 int error; 1197 1198 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) && 1199 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1200 transport_ops = &elan_i2c_ops; 1201 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) && 1202 i2c_check_functionality(client->adapter, 1203 I2C_FUNC_SMBUS_BYTE_DATA | 1204 I2C_FUNC_SMBUS_BLOCK_DATA | 1205 I2C_FUNC_SMBUS_I2C_BLOCK)) { 1206 transport_ops = &elan_smbus_ops; 1207 } else { 1208 dev_err(dev, "not a supported I2C/SMBus adapter\n"); 1209 return -EIO; 1210 } 1211 1212 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL); 1213 if (!data) 1214 return -ENOMEM; 1215 1216 i2c_set_clientdata(client, data); 1217 1218 data->ops = transport_ops; 1219 data->client = client; 1220 init_completion(&data->fw_completion); 1221 mutex_init(&data->sysfs_mutex); 1222 1223 data->vcc = devm_regulator_get(dev, "vcc"); 1224 if (IS_ERR(data->vcc)) 1225 return dev_err_probe(dev, PTR_ERR(data->vcc), "Failed to get 'vcc' regulator\n"); 1226 1227 error = regulator_enable(data->vcc); 1228 if (error) { 1229 dev_err(dev, "Failed to enable regulator: %d\n", error); 1230 return error; 1231 } 1232 1233 error = devm_add_action_or_reset(dev, elan_disable_regulator, data); 1234 if (error) { 1235 dev_err(dev, "Failed to add disable regulator action: %d\n", 1236 error); 1237 return error; 1238 } 1239 1240 /* Make sure there is something at this address */ 1241 error = i2c_smbus_read_byte(client); 1242 if (error < 0) { 1243 dev_dbg(&client->dev, "nothing at this address: %d\n", error); 1244 return -ENXIO; 1245 } 1246 1247 /* Initialize the touchpad. */ 1248 error = elan_initialize(data, false); 1249 if (error) 1250 return error; 1251 1252 error = elan_query_device_info(data); 1253 if (error) 1254 return error; 1255 1256 error = elan_query_device_parameters(data); 1257 if (error) 1258 return error; 1259 1260 dev_info(dev, 1261 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n", 1262 data->product_id, 1263 data->fw_version, 1264 data->sm_version, 1265 data->iap_version); 1266 1267 dev_dbg(dev, 1268 "Elan Touchpad Extra Information:\n" 1269 " Max ABS X,Y: %d,%d\n" 1270 " Width X,Y: %d,%d\n" 1271 " Resolution X,Y: %d,%d (dots/mm)\n" 1272 " ic type: 0x%x\n" 1273 " info pattern: 0x%x\n", 1274 data->max_x, data->max_y, 1275 data->width_x, data->width_y, 1276 data->x_res, data->y_res, 1277 data->ic_type, data->pattern); 1278 1279 /* Set up input device properties based on queried parameters. */ 1280 error = elan_setup_input_device(data); 1281 if (error) 1282 return error; 1283 1284 if (device_property_read_bool(&client->dev, "elan,trackpoint")) { 1285 error = elan_setup_trackpoint_input_device(data); 1286 if (error) 1287 return error; 1288 } 1289 1290 /* 1291 * Platform code (ACPI, DTS) should normally set up interrupt 1292 * for us, but in case it did not let's fall back to using falling 1293 * edge to be compatible with older Chromebooks. 1294 */ 1295 irqflags = irq_get_trigger_type(client->irq); 1296 if (!irqflags) 1297 irqflags = IRQF_TRIGGER_FALLING; 1298 1299 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr, 1300 irqflags | IRQF_ONESHOT, 1301 client->name, data); 1302 if (error) { 1303 dev_err(dev, "cannot register irq=%d\n", client->irq); 1304 return error; 1305 } 1306 1307 error = input_register_device(data->input); 1308 if (error) { 1309 dev_err(dev, "failed to register input device: %d\n", error); 1310 return error; 1311 } 1312 1313 if (data->tp_input) { 1314 error = input_register_device(data->tp_input); 1315 if (error) { 1316 dev_err(&client->dev, 1317 "failed to register TrackPoint input device: %d\n", 1318 error); 1319 return error; 1320 } 1321 } 1322 1323 return 0; 1324 } 1325 1326 static int elan_suspend(struct device *dev) 1327 { 1328 struct i2c_client *client = to_i2c_client(dev); 1329 struct elan_tp_data *data = i2c_get_clientdata(client); 1330 int ret; 1331 1332 /* 1333 * We are taking the mutex to make sure sysfs operations are 1334 * complete before we attempt to bring the device into low[er] 1335 * power mode. 1336 */ 1337 ret = mutex_lock_interruptible(&data->sysfs_mutex); 1338 if (ret) 1339 return ret; 1340 1341 disable_irq(client->irq); 1342 1343 if (device_may_wakeup(dev)) { 1344 ret = elan_sleep(data); 1345 } else { 1346 ret = elan_set_power(data, false); 1347 if (ret) 1348 goto err; 1349 1350 ret = regulator_disable(data->vcc); 1351 if (ret) { 1352 dev_err(dev, "error %d disabling regulator\n", ret); 1353 /* Attempt to power the chip back up */ 1354 elan_set_power(data, true); 1355 } 1356 } 1357 1358 err: 1359 mutex_unlock(&data->sysfs_mutex); 1360 return ret; 1361 } 1362 1363 static int elan_resume(struct device *dev) 1364 { 1365 struct i2c_client *client = to_i2c_client(dev); 1366 struct elan_tp_data *data = i2c_get_clientdata(client); 1367 int error; 1368 1369 if (!device_may_wakeup(dev)) { 1370 error = regulator_enable(data->vcc); 1371 if (error) { 1372 dev_err(dev, "error %d enabling regulator\n", error); 1373 goto err; 1374 } 1375 } 1376 1377 error = elan_set_power(data, true); 1378 if (error) { 1379 dev_err(dev, "power up when resuming failed: %d\n", error); 1380 goto err; 1381 } 1382 1383 error = elan_initialize(data, data->quirks & ETP_QUIRK_QUICK_WAKEUP); 1384 if (error) 1385 dev_err(dev, "initialize when resuming failed: %d\n", error); 1386 1387 err: 1388 enable_irq(data->client->irq); 1389 return error; 1390 } 1391 1392 static DEFINE_SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume); 1393 1394 static const struct i2c_device_id elan_id[] = { 1395 { DRIVER_NAME, 0 }, 1396 { }, 1397 }; 1398 MODULE_DEVICE_TABLE(i2c, elan_id); 1399 1400 #ifdef CONFIG_ACPI 1401 #include <linux/input/elan-i2c-ids.h> 1402 MODULE_DEVICE_TABLE(acpi, elan_acpi_id); 1403 #endif 1404 1405 #ifdef CONFIG_OF 1406 static const struct of_device_id elan_of_match[] = { 1407 { .compatible = "elan,ekth3000" }, 1408 { /* sentinel */ } 1409 }; 1410 MODULE_DEVICE_TABLE(of, elan_of_match); 1411 #endif 1412 1413 static struct i2c_driver elan_driver = { 1414 .driver = { 1415 .name = DRIVER_NAME, 1416 .pm = pm_sleep_ptr(&elan_pm_ops), 1417 .acpi_match_table = ACPI_PTR(elan_acpi_id), 1418 .of_match_table = of_match_ptr(elan_of_match), 1419 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1420 .dev_groups = elan_sysfs_groups, 1421 }, 1422 .probe = elan_probe, 1423 .id_table = elan_id, 1424 }; 1425 1426 module_i2c_driver(elan_driver); 1427 1428 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>"); 1429 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver"); 1430 MODULE_LICENSE("GPL"); 1431