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