1 /* 2 * Elan I2C/SMBus Touchpad driver 3 * 4 * Copyright (c) 2013 ELAN Microelectronics Corp. 5 * 6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> 7 * Version: 1.5.7 8 * 9 * Based on cyapa driver: 10 * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 11 * copyright (c) 2011-2012 Google, Inc. 12 * 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms of the GNU General Public License version 2 as published 15 * by the Free Software Foundation. 16 * 17 * Trademarks are the property of their respective owners. 18 */ 19 20 #include <linux/acpi.h> 21 #include <linux/delay.h> 22 #include <linux/device.h> 23 #include <linux/firmware.h> 24 #include <linux/i2c.h> 25 #include <linux/init.h> 26 #include <linux/input/mt.h> 27 #include <linux/interrupt.h> 28 #include <linux/module.h> 29 #include <linux/slab.h> 30 #include <linux/kernel.h> 31 #include <linux/sched.h> 32 #include <linux/input.h> 33 #include <linux/uaccess.h> 34 #include <linux/jiffies.h> 35 #include <linux/completion.h> 36 #include <linux/of.h> 37 #include <linux/regulator/consumer.h> 38 #include <asm/unaligned.h> 39 40 #include "elan_i2c.h" 41 42 #define DRIVER_NAME "elan_i2c" 43 #define ELAN_DRIVER_VERSION "1.5.7" 44 #define ETP_MAX_PRESSURE 255 45 #define ETP_FWIDTH_REDUCE 90 46 #define ETP_FINGER_WIDTH 15 47 #define ETP_RETRY_COUNT 3 48 49 #define ETP_MAX_FINGERS 5 50 #define ETP_FINGER_DATA_LEN 5 51 #define ETP_REPORT_ID 0x5D 52 #define ETP_REPORT_ID_OFFSET 2 53 #define ETP_TOUCH_INFO_OFFSET 3 54 #define ETP_FINGER_DATA_OFFSET 4 55 #define ETP_HOVER_INFO_OFFSET 30 56 #define ETP_MAX_REPORT_LEN 34 57 58 /* The main device structure */ 59 struct elan_tp_data { 60 struct i2c_client *client; 61 struct input_dev *input; 62 struct regulator *vcc; 63 64 const struct elan_transport_ops *ops; 65 66 /* for fw update */ 67 struct completion fw_completion; 68 bool in_fw_update; 69 70 struct mutex sysfs_mutex; 71 72 unsigned int max_x; 73 unsigned int max_y; 74 unsigned int width_x; 75 unsigned int width_y; 76 unsigned int x_res; 77 unsigned int y_res; 78 79 u8 product_id; 80 u8 fw_version; 81 u8 sm_version; 82 u8 iap_version; 83 u16 fw_checksum; 84 int pressure_adjustment; 85 u8 mode; 86 87 bool irq_wake; 88 89 u8 min_baseline; 90 u8 max_baseline; 91 bool baseline_ready; 92 }; 93 94 static int elan_enable_power(struct elan_tp_data *data) 95 { 96 int repeat = ETP_RETRY_COUNT; 97 int error; 98 99 error = regulator_enable(data->vcc); 100 if (error) { 101 dev_err(&data->client->dev, 102 "failed to enable regulator: %d\n", error); 103 return error; 104 } 105 106 do { 107 error = data->ops->power_control(data->client, true); 108 if (error >= 0) 109 return 0; 110 111 msleep(30); 112 } while (--repeat > 0); 113 114 dev_err(&data->client->dev, "failed to enable power: %d\n", error); 115 return error; 116 } 117 118 static int elan_disable_power(struct elan_tp_data *data) 119 { 120 int repeat = ETP_RETRY_COUNT; 121 int error; 122 123 do { 124 error = data->ops->power_control(data->client, false); 125 if (!error) { 126 error = regulator_disable(data->vcc); 127 if (error) { 128 dev_err(&data->client->dev, 129 "failed to disable regulator: %d\n", 130 error); 131 /* Attempt to power the chip back up */ 132 data->ops->power_control(data->client, true); 133 break; 134 } 135 136 return 0; 137 } 138 139 msleep(30); 140 } while (--repeat > 0); 141 142 dev_err(&data->client->dev, "failed to disable power: %d\n", error); 143 return error; 144 } 145 146 static int elan_sleep(struct elan_tp_data *data) 147 { 148 int repeat = ETP_RETRY_COUNT; 149 int error; 150 151 do { 152 error = data->ops->sleep_control(data->client, true); 153 if (!error) 154 return 0; 155 156 msleep(30); 157 } while (--repeat > 0); 158 159 return error; 160 } 161 162 static int __elan_initialize(struct elan_tp_data *data) 163 { 164 struct i2c_client *client = data->client; 165 int error; 166 167 error = data->ops->initialize(client); 168 if (error) { 169 dev_err(&client->dev, "device initialize failed: %d\n", error); 170 return error; 171 } 172 173 data->mode |= ETP_ENABLE_ABS; 174 error = data->ops->set_mode(client, data->mode); 175 if (error) { 176 dev_err(&client->dev, 177 "failed to switch to absolute mode: %d\n", error); 178 return error; 179 } 180 181 error = data->ops->sleep_control(client, false); 182 if (error) { 183 dev_err(&client->dev, 184 "failed to wake device up: %d\n", error); 185 return error; 186 } 187 188 return 0; 189 } 190 191 static int elan_initialize(struct elan_tp_data *data) 192 { 193 int repeat = ETP_RETRY_COUNT; 194 int error; 195 196 do { 197 error = __elan_initialize(data); 198 if (!error) 199 return 0; 200 201 msleep(30); 202 } while (--repeat > 0); 203 204 return error; 205 } 206 207 static int elan_query_device_info(struct elan_tp_data *data) 208 { 209 int error; 210 211 error = data->ops->get_product_id(data->client, &data->product_id); 212 if (error) 213 return error; 214 215 error = data->ops->get_version(data->client, false, &data->fw_version); 216 if (error) 217 return error; 218 219 error = data->ops->get_checksum(data->client, false, 220 &data->fw_checksum); 221 if (error) 222 return error; 223 224 error = data->ops->get_sm_version(data->client, &data->sm_version); 225 if (error) 226 return error; 227 228 error = data->ops->get_version(data->client, true, &data->iap_version); 229 if (error) 230 return error; 231 232 error = data->ops->get_pressure_adjustment(data->client, 233 &data->pressure_adjustment); 234 if (error) 235 return error; 236 237 return 0; 238 } 239 240 static unsigned int elan_convert_resolution(u8 val) 241 { 242 /* 243 * (value from firmware) * 10 + 790 = dpi 244 * 245 * We also have to convert dpi to dots/mm (*10/254 to avoid floating 246 * point). 247 */ 248 249 return ((int)(char)val * 10 + 790) * 10 / 254; 250 } 251 252 static int elan_query_device_parameters(struct elan_tp_data *data) 253 { 254 unsigned int x_traces, y_traces; 255 u8 hw_x_res, hw_y_res; 256 int error; 257 258 error = data->ops->get_max(data->client, &data->max_x, &data->max_y); 259 if (error) 260 return error; 261 262 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces); 263 if (error) 264 return error; 265 266 data->width_x = data->max_x / x_traces; 267 data->width_y = data->max_y / y_traces; 268 269 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res); 270 if (error) 271 return error; 272 273 data->x_res = elan_convert_resolution(hw_x_res); 274 data->y_res = elan_convert_resolution(hw_y_res); 275 276 return 0; 277 } 278 279 /* 280 ********************************************************** 281 * IAP firmware updater related routines 282 ********************************************************** 283 */ 284 static int elan_write_fw_block(struct elan_tp_data *data, 285 const u8 *page, u16 checksum, int idx) 286 { 287 int retry = ETP_RETRY_COUNT; 288 int error; 289 290 do { 291 error = data->ops->write_fw_block(data->client, 292 page, checksum, idx); 293 if (!error) 294 return 0; 295 296 dev_dbg(&data->client->dev, 297 "IAP retrying page %d (error: %d)\n", idx, error); 298 } while (--retry > 0); 299 300 return error; 301 } 302 303 static int __elan_update_firmware(struct elan_tp_data *data, 304 const struct firmware *fw) 305 { 306 struct i2c_client *client = data->client; 307 struct device *dev = &client->dev; 308 int i, j; 309 int error; 310 u16 iap_start_addr; 311 u16 boot_page_count; 312 u16 sw_checksum = 0, fw_checksum = 0; 313 314 error = data->ops->prepare_fw_update(client); 315 if (error) 316 return error; 317 318 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]); 319 320 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE; 321 for (i = boot_page_count; i < ETP_FW_VAILDPAGE_COUNT; i++) { 322 u16 checksum = 0; 323 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE]; 324 325 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2) 326 checksum += ((page[j + 1] << 8) | page[j]); 327 328 error = elan_write_fw_block(data, page, checksum, i); 329 if (error) { 330 dev_err(dev, "write page %d fail: %d\n", i, error); 331 return error; 332 } 333 334 sw_checksum += checksum; 335 } 336 337 /* Wait WDT reset and power on reset */ 338 msleep(600); 339 340 error = data->ops->finish_fw_update(client, &data->fw_completion); 341 if (error) 342 return error; 343 344 error = data->ops->get_checksum(client, true, &fw_checksum); 345 if (error) 346 return error; 347 348 if (sw_checksum != fw_checksum) { 349 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n", 350 sw_checksum, fw_checksum); 351 return -EIO; 352 } 353 354 return 0; 355 } 356 357 static int elan_update_firmware(struct elan_tp_data *data, 358 const struct firmware *fw) 359 { 360 struct i2c_client *client = data->client; 361 int retval; 362 363 dev_dbg(&client->dev, "Starting firmware update....\n"); 364 365 disable_irq(client->irq); 366 data->in_fw_update = true; 367 368 retval = __elan_update_firmware(data, fw); 369 if (retval) { 370 dev_err(&client->dev, "firmware update failed: %d\n", retval); 371 data->ops->iap_reset(client); 372 } else { 373 /* Reinitialize TP after fw is updated */ 374 elan_initialize(data); 375 elan_query_device_info(data); 376 } 377 378 data->in_fw_update = false; 379 enable_irq(client->irq); 380 381 return retval; 382 } 383 384 /* 385 ******************************************************************* 386 * SYSFS attributes 387 ******************************************************************* 388 */ 389 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev, 390 struct device_attribute *attr, 391 char *buf) 392 { 393 struct i2c_client *client = to_i2c_client(dev); 394 struct elan_tp_data *data = i2c_get_clientdata(client); 395 396 return sprintf(buf, "0x%04x\n", data->fw_checksum); 397 } 398 399 static ssize_t elan_sysfs_read_product_id(struct device *dev, 400 struct device_attribute *attr, 401 char *buf) 402 { 403 struct i2c_client *client = to_i2c_client(dev); 404 struct elan_tp_data *data = i2c_get_clientdata(client); 405 406 return sprintf(buf, "%d.0\n", data->product_id); 407 } 408 409 static ssize_t elan_sysfs_read_fw_ver(struct device *dev, 410 struct device_attribute *attr, 411 char *buf) 412 { 413 struct i2c_client *client = to_i2c_client(dev); 414 struct elan_tp_data *data = i2c_get_clientdata(client); 415 416 return sprintf(buf, "%d.0\n", data->fw_version); 417 } 418 419 static ssize_t elan_sysfs_read_sm_ver(struct device *dev, 420 struct device_attribute *attr, 421 char *buf) 422 { 423 struct i2c_client *client = to_i2c_client(dev); 424 struct elan_tp_data *data = i2c_get_clientdata(client); 425 426 return sprintf(buf, "%d.0\n", data->sm_version); 427 } 428 429 static ssize_t elan_sysfs_read_iap_ver(struct device *dev, 430 struct device_attribute *attr, 431 char *buf) 432 { 433 struct i2c_client *client = to_i2c_client(dev); 434 struct elan_tp_data *data = i2c_get_clientdata(client); 435 436 return sprintf(buf, "%d.0\n", data->iap_version); 437 } 438 439 static ssize_t elan_sysfs_update_fw(struct device *dev, 440 struct device_attribute *attr, 441 const char *buf, size_t count) 442 { 443 struct elan_tp_data *data = dev_get_drvdata(dev); 444 const struct firmware *fw; 445 int error; 446 const u8 *fw_signature; 447 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF}; 448 449 error = request_firmware(&fw, ETP_FW_NAME, dev); 450 if (error) { 451 dev_err(dev, "cannot load firmware %s: %d\n", 452 ETP_FW_NAME, error); 453 return error; 454 } 455 456 /* Firmware file must match signature data */ 457 fw_signature = &fw->data[ETP_FW_SIGNATURE_ADDRESS]; 458 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) { 459 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n", 460 (int)sizeof(signature), signature, 461 (int)sizeof(signature), fw_signature); 462 error = -EBADF; 463 goto out_release_fw; 464 } 465 466 error = mutex_lock_interruptible(&data->sysfs_mutex); 467 if (error) 468 goto out_release_fw; 469 470 error = elan_update_firmware(data, fw); 471 472 mutex_unlock(&data->sysfs_mutex); 473 474 out_release_fw: 475 release_firmware(fw); 476 return error ?: count; 477 } 478 479 static ssize_t calibrate_store(struct device *dev, 480 struct device_attribute *attr, 481 const char *buf, size_t count) 482 { 483 struct i2c_client *client = to_i2c_client(dev); 484 struct elan_tp_data *data = i2c_get_clientdata(client); 485 int tries = 20; 486 int retval; 487 int error; 488 u8 val[3]; 489 490 retval = mutex_lock_interruptible(&data->sysfs_mutex); 491 if (retval) 492 return retval; 493 494 disable_irq(client->irq); 495 496 data->mode |= ETP_ENABLE_CALIBRATE; 497 retval = data->ops->set_mode(client, data->mode); 498 if (retval) { 499 dev_err(dev, "failed to enable calibration mode: %d\n", 500 retval); 501 goto out; 502 } 503 504 retval = data->ops->calibrate(client); 505 if (retval) { 506 dev_err(dev, "failed to start calibration: %d\n", 507 retval); 508 goto out_disable_calibrate; 509 } 510 511 val[0] = 0xff; 512 do { 513 /* Wait 250ms before checking if calibration has completed. */ 514 msleep(250); 515 516 retval = data->ops->calibrate_result(client, val); 517 if (retval) 518 dev_err(dev, "failed to check calibration result: %d\n", 519 retval); 520 else if (val[0] == 0) 521 break; /* calibration done */ 522 523 } while (--tries); 524 525 if (tries == 0) { 526 dev_err(dev, "failed to calibrate. Timeout.\n"); 527 retval = -ETIMEDOUT; 528 } 529 530 out_disable_calibrate: 531 data->mode &= ~ETP_ENABLE_CALIBRATE; 532 error = data->ops->set_mode(data->client, data->mode); 533 if (error) { 534 dev_err(dev, "failed to disable calibration mode: %d\n", 535 error); 536 if (!retval) 537 retval = error; 538 } 539 out: 540 enable_irq(client->irq); 541 mutex_unlock(&data->sysfs_mutex); 542 return retval ?: count; 543 } 544 545 static ssize_t elan_sysfs_read_mode(struct device *dev, 546 struct device_attribute *attr, 547 char *buf) 548 { 549 struct i2c_client *client = to_i2c_client(dev); 550 struct elan_tp_data *data = i2c_get_clientdata(client); 551 int error; 552 enum tp_mode mode; 553 554 error = mutex_lock_interruptible(&data->sysfs_mutex); 555 if (error) 556 return error; 557 558 error = data->ops->iap_get_mode(data->client, &mode); 559 560 mutex_unlock(&data->sysfs_mutex); 561 562 if (error) 563 return error; 564 565 return sprintf(buf, "%d\n", (int)mode); 566 } 567 568 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL); 569 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL); 570 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL); 571 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL); 572 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL); 573 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL); 574 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw); 575 576 static DEVICE_ATTR_WO(calibrate); 577 578 static struct attribute *elan_sysfs_entries[] = { 579 &dev_attr_product_id.attr, 580 &dev_attr_firmware_version.attr, 581 &dev_attr_sample_version.attr, 582 &dev_attr_iap_version.attr, 583 &dev_attr_fw_checksum.attr, 584 &dev_attr_calibrate.attr, 585 &dev_attr_mode.attr, 586 &dev_attr_update_fw.attr, 587 NULL, 588 }; 589 590 static const struct attribute_group elan_sysfs_group = { 591 .attrs = elan_sysfs_entries, 592 }; 593 594 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr, 595 const char *buf, size_t count) 596 { 597 struct i2c_client *client = to_i2c_client(dev); 598 struct elan_tp_data *data = i2c_get_clientdata(client); 599 int error; 600 int retval; 601 602 retval = mutex_lock_interruptible(&data->sysfs_mutex); 603 if (retval) 604 return retval; 605 606 disable_irq(client->irq); 607 608 data->baseline_ready = false; 609 610 data->mode |= ETP_ENABLE_CALIBRATE; 611 retval = data->ops->set_mode(data->client, data->mode); 612 if (retval) { 613 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n", 614 retval); 615 goto out; 616 } 617 618 msleep(250); 619 620 retval = data->ops->get_baseline_data(data->client, true, 621 &data->max_baseline); 622 if (retval) { 623 dev_err(dev, "Failed to read max baseline form device: %d\n", 624 retval); 625 goto out_disable_calibrate; 626 } 627 628 retval = data->ops->get_baseline_data(data->client, false, 629 &data->min_baseline); 630 if (retval) { 631 dev_err(dev, "Failed to read min baseline form device: %d\n", 632 retval); 633 goto out_disable_calibrate; 634 } 635 636 data->baseline_ready = true; 637 638 out_disable_calibrate: 639 data->mode &= ~ETP_ENABLE_CALIBRATE; 640 error = data->ops->set_mode(data->client, data->mode); 641 if (error) { 642 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n", 643 error); 644 if (!retval) 645 retval = error; 646 } 647 out: 648 enable_irq(client->irq); 649 mutex_unlock(&data->sysfs_mutex); 650 return retval ?: count; 651 } 652 653 static ssize_t min_show(struct device *dev, 654 struct device_attribute *attr, char *buf) 655 { 656 struct i2c_client *client = to_i2c_client(dev); 657 struct elan_tp_data *data = i2c_get_clientdata(client); 658 int retval; 659 660 retval = mutex_lock_interruptible(&data->sysfs_mutex); 661 if (retval) 662 return retval; 663 664 if (!data->baseline_ready) { 665 retval = -ENODATA; 666 goto out; 667 } 668 669 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline); 670 671 out: 672 mutex_unlock(&data->sysfs_mutex); 673 return retval; 674 } 675 676 static ssize_t max_show(struct device *dev, 677 struct device_attribute *attr, char *buf) 678 { 679 struct i2c_client *client = to_i2c_client(dev); 680 struct elan_tp_data *data = i2c_get_clientdata(client); 681 int retval; 682 683 retval = mutex_lock_interruptible(&data->sysfs_mutex); 684 if (retval) 685 return retval; 686 687 if (!data->baseline_ready) { 688 retval = -ENODATA; 689 goto out; 690 } 691 692 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline); 693 694 out: 695 mutex_unlock(&data->sysfs_mutex); 696 return retval; 697 } 698 699 700 static DEVICE_ATTR_WO(acquire); 701 static DEVICE_ATTR_RO(min); 702 static DEVICE_ATTR_RO(max); 703 704 static struct attribute *elan_baseline_sysfs_entries[] = { 705 &dev_attr_acquire.attr, 706 &dev_attr_min.attr, 707 &dev_attr_max.attr, 708 NULL, 709 }; 710 711 static const struct attribute_group elan_baseline_sysfs_group = { 712 .name = "baseline", 713 .attrs = elan_baseline_sysfs_entries, 714 }; 715 716 static const struct attribute_group *elan_sysfs_groups[] = { 717 &elan_sysfs_group, 718 &elan_baseline_sysfs_group, 719 NULL 720 }; 721 722 /* 723 ****************************************************************** 724 * Elan isr functions 725 ****************************************************************** 726 */ 727 static void elan_report_contact(struct elan_tp_data *data, 728 int contact_num, bool contact_valid, 729 bool hover_event, u8 *finger_data) 730 { 731 struct input_dev *input = data->input; 732 unsigned int pos_x, pos_y; 733 unsigned int pressure, mk_x, mk_y; 734 unsigned int area_x, area_y, major, minor; 735 unsigned int scaled_pressure; 736 737 if (contact_valid) { 738 pos_x = ((finger_data[0] & 0xf0) << 4) | 739 finger_data[1]; 740 pos_y = ((finger_data[0] & 0x0f) << 8) | 741 finger_data[2]; 742 mk_x = (finger_data[3] & 0x0f); 743 mk_y = (finger_data[3] >> 4); 744 pressure = finger_data[4]; 745 746 if (pos_x > data->max_x || pos_y > data->max_y) { 747 dev_dbg(input->dev.parent, 748 "[%d] x=%d y=%d over max (%d, %d)", 749 contact_num, pos_x, pos_y, 750 data->max_x, data->max_y); 751 return; 752 } 753 754 /* 755 * To avoid treating large finger as palm, let's reduce the 756 * width x and y per trace. 757 */ 758 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE); 759 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE); 760 761 major = max(area_x, area_y); 762 minor = min(area_x, area_y); 763 764 scaled_pressure = pressure + data->pressure_adjustment; 765 766 if (scaled_pressure > ETP_MAX_PRESSURE) 767 scaled_pressure = ETP_MAX_PRESSURE; 768 769 input_mt_slot(input, contact_num); 770 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 771 input_report_abs(input, ABS_MT_POSITION_X, pos_x); 772 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y); 773 input_report_abs(input, ABS_MT_DISTANCE, hover_event); 774 input_report_abs(input, ABS_MT_PRESSURE, 775 hover_event ? 0 : scaled_pressure); 776 input_report_abs(input, ABS_TOOL_WIDTH, mk_x); 777 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major); 778 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor); 779 } else { 780 input_mt_slot(input, contact_num); 781 input_mt_report_slot_state(input, MT_TOOL_FINGER, false); 782 } 783 } 784 785 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet) 786 { 787 struct input_dev *input = data->input; 788 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET]; 789 int i; 790 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET]; 791 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET]; 792 bool contact_valid, hover_event; 793 794 hover_event = hover_info & 0x40; 795 for (i = 0; i < ETP_MAX_FINGERS; i++) { 796 contact_valid = tp_info & (1U << (3 + i)); 797 elan_report_contact(data, i, contact_valid, hover_event, 798 finger_data); 799 800 if (contact_valid) 801 finger_data += ETP_FINGER_DATA_LEN; 802 } 803 804 input_report_key(input, BTN_LEFT, tp_info & 0x01); 805 input_mt_report_pointer_emulation(input, true); 806 input_sync(input); 807 } 808 809 static irqreturn_t elan_isr(int irq, void *dev_id) 810 { 811 struct elan_tp_data *data = dev_id; 812 struct device *dev = &data->client->dev; 813 int error; 814 u8 report[ETP_MAX_REPORT_LEN]; 815 816 /* 817 * When device is connected to i2c bus, when all IAP page writes 818 * complete, the driver will receive interrupt and must read 819 * 0000 to confirm that IAP is finished. 820 */ 821 if (data->in_fw_update) { 822 complete(&data->fw_completion); 823 goto out; 824 } 825 826 error = data->ops->get_report(data->client, report); 827 if (error) 828 goto out; 829 830 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID) 831 dev_err(dev, "invalid report id data (%x)\n", 832 report[ETP_REPORT_ID_OFFSET]); 833 else 834 elan_report_absolute(data, report); 835 836 out: 837 return IRQ_HANDLED; 838 } 839 840 /* 841 ****************************************************************** 842 * Elan initialization functions 843 ****************************************************************** 844 */ 845 static int elan_setup_input_device(struct elan_tp_data *data) 846 { 847 struct device *dev = &data->client->dev; 848 struct input_dev *input; 849 unsigned int max_width = max(data->width_x, data->width_y); 850 unsigned int min_width = min(data->width_x, data->width_y); 851 int error; 852 853 input = devm_input_allocate_device(dev); 854 if (!input) 855 return -ENOMEM; 856 857 input->name = "Elan Touchpad"; 858 input->id.bustype = BUS_I2C; 859 input_set_drvdata(input, data); 860 861 error = input_mt_init_slots(input, ETP_MAX_FINGERS, 862 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); 863 if (error) { 864 dev_err(dev, "failed to initialize MT slots: %d\n", error); 865 return error; 866 } 867 868 __set_bit(EV_ABS, input->evbit); 869 __set_bit(INPUT_PROP_POINTER, input->propbit); 870 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 871 __set_bit(BTN_LEFT, input->keybit); 872 873 /* Set up ST parameters */ 874 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0); 875 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0); 876 input_abs_set_res(input, ABS_X, data->x_res); 877 input_abs_set_res(input, ABS_Y, data->y_res); 878 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0); 879 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0); 880 881 /* And MT parameters */ 882 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0); 883 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0); 884 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res); 885 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res); 886 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 887 ETP_MAX_PRESSURE, 0, 0); 888 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 889 ETP_FINGER_WIDTH * max_width, 0, 0); 890 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 891 ETP_FINGER_WIDTH * min_width, 0, 0); 892 input_set_abs_params(input, ABS_MT_DISTANCE, 0, 1, 0, 0); 893 894 data->input = input; 895 896 return 0; 897 } 898 899 static void elan_disable_regulator(void *_data) 900 { 901 struct elan_tp_data *data = _data; 902 903 regulator_disable(data->vcc); 904 } 905 906 static void elan_remove_sysfs_groups(void *_data) 907 { 908 struct elan_tp_data *data = _data; 909 910 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups); 911 } 912 913 static int elan_probe(struct i2c_client *client, 914 const struct i2c_device_id *dev_id) 915 { 916 const struct elan_transport_ops *transport_ops; 917 struct device *dev = &client->dev; 918 struct elan_tp_data *data; 919 unsigned long irqflags; 920 int error; 921 922 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) && 923 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 924 transport_ops = &elan_i2c_ops; 925 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) && 926 i2c_check_functionality(client->adapter, 927 I2C_FUNC_SMBUS_BYTE_DATA | 928 I2C_FUNC_SMBUS_BLOCK_DATA | 929 I2C_FUNC_SMBUS_I2C_BLOCK)) { 930 transport_ops = &elan_smbus_ops; 931 } else { 932 dev_err(dev, "not a supported I2C/SMBus adapter\n"); 933 return -EIO; 934 } 935 936 data = devm_kzalloc(&client->dev, sizeof(struct elan_tp_data), 937 GFP_KERNEL); 938 if (!data) 939 return -ENOMEM; 940 941 i2c_set_clientdata(client, data); 942 943 data->ops = transport_ops; 944 data->client = client; 945 init_completion(&data->fw_completion); 946 mutex_init(&data->sysfs_mutex); 947 948 data->vcc = devm_regulator_get(&client->dev, "vcc"); 949 if (IS_ERR(data->vcc)) { 950 error = PTR_ERR(data->vcc); 951 if (error != -EPROBE_DEFER) 952 dev_err(&client->dev, 953 "Failed to get 'vcc' regulator: %d\n", 954 error); 955 return error; 956 } 957 958 error = regulator_enable(data->vcc); 959 if (error) { 960 dev_err(&client->dev, 961 "Failed to enable regulator: %d\n", error); 962 return error; 963 } 964 965 error = devm_add_action(&client->dev, 966 elan_disable_regulator, data); 967 if (error) { 968 regulator_disable(data->vcc); 969 dev_err(&client->dev, 970 "Failed to add disable regulator action: %d\n", 971 error); 972 return error; 973 } 974 975 /* Initialize the touchpad. */ 976 error = elan_initialize(data); 977 if (error) 978 return error; 979 980 error = elan_query_device_info(data); 981 if (error) 982 return error; 983 984 error = elan_query_device_parameters(data); 985 if (error) 986 return error; 987 988 dev_dbg(&client->dev, 989 "Elan Touchpad Information:\n" 990 " Module product ID: 0x%04x\n" 991 " Firmware Version: 0x%04x\n" 992 " Sample Version: 0x%04x\n" 993 " IAP Version: 0x%04x\n" 994 " Max ABS X,Y: %d,%d\n" 995 " Width X,Y: %d,%d\n" 996 " Resolution X,Y: %d,%d (dots/mm)\n", 997 data->product_id, 998 data->fw_version, 999 data->sm_version, 1000 data->iap_version, 1001 data->max_x, data->max_y, 1002 data->width_x, data->width_y, 1003 data->x_res, data->y_res); 1004 1005 /* Set up input device properties based on queried parameters. */ 1006 error = elan_setup_input_device(data); 1007 if (error) 1008 return error; 1009 1010 /* 1011 * Systems using device tree should set up interrupt via DTS, 1012 * the rest will use the default falling edge interrupts. 1013 */ 1014 irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING; 1015 1016 error = devm_request_threaded_irq(&client->dev, client->irq, 1017 NULL, elan_isr, 1018 irqflags | IRQF_ONESHOT, 1019 client->name, data); 1020 if (error) { 1021 dev_err(&client->dev, "cannot register irq=%d\n", client->irq); 1022 return error; 1023 } 1024 1025 error = sysfs_create_groups(&client->dev.kobj, elan_sysfs_groups); 1026 if (error) { 1027 dev_err(&client->dev, "failed to create sysfs attributes: %d\n", 1028 error); 1029 return error; 1030 } 1031 1032 error = devm_add_action(&client->dev, 1033 elan_remove_sysfs_groups, data); 1034 if (error) { 1035 elan_remove_sysfs_groups(data); 1036 dev_err(&client->dev, 1037 "Failed to add sysfs cleanup action: %d\n", 1038 error); 1039 return error; 1040 } 1041 1042 error = input_register_device(data->input); 1043 if (error) { 1044 dev_err(&client->dev, "failed to register input device: %d\n", 1045 error); 1046 return error; 1047 } 1048 1049 /* 1050 * Systems using device tree should set up wakeup via DTS, 1051 * the rest will configure device as wakeup source by default. 1052 */ 1053 if (!client->dev.of_node) 1054 device_init_wakeup(&client->dev, true); 1055 1056 return 0; 1057 } 1058 1059 static int __maybe_unused elan_suspend(struct device *dev) 1060 { 1061 struct i2c_client *client = to_i2c_client(dev); 1062 struct elan_tp_data *data = i2c_get_clientdata(client); 1063 int ret; 1064 1065 /* 1066 * We are taking the mutex to make sure sysfs operations are 1067 * complete before we attempt to bring the device into low[er] 1068 * power mode. 1069 */ 1070 ret = mutex_lock_interruptible(&data->sysfs_mutex); 1071 if (ret) 1072 return ret; 1073 1074 disable_irq(client->irq); 1075 1076 if (device_may_wakeup(dev)) { 1077 ret = elan_sleep(data); 1078 /* Enable wake from IRQ */ 1079 data->irq_wake = (enable_irq_wake(client->irq) == 0); 1080 } else { 1081 ret = elan_disable_power(data); 1082 } 1083 1084 mutex_unlock(&data->sysfs_mutex); 1085 return ret; 1086 } 1087 1088 static int __maybe_unused elan_resume(struct device *dev) 1089 { 1090 struct i2c_client *client = to_i2c_client(dev); 1091 struct elan_tp_data *data = i2c_get_clientdata(client); 1092 int error; 1093 1094 if (device_may_wakeup(dev) && data->irq_wake) { 1095 disable_irq_wake(client->irq); 1096 data->irq_wake = false; 1097 } 1098 1099 error = elan_enable_power(data); 1100 if (error) { 1101 dev_err(dev, "power up when resuming failed: %d\n", error); 1102 goto err; 1103 } 1104 1105 error = elan_initialize(data); 1106 if (error) 1107 dev_err(dev, "initialize when resuming failed: %d\n", error); 1108 1109 err: 1110 enable_irq(data->client->irq); 1111 return error; 1112 } 1113 1114 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume); 1115 1116 static const struct i2c_device_id elan_id[] = { 1117 { DRIVER_NAME, 0 }, 1118 { }, 1119 }; 1120 MODULE_DEVICE_TABLE(i2c, elan_id); 1121 1122 #ifdef CONFIG_ACPI 1123 static const struct acpi_device_id elan_acpi_id[] = { 1124 { "ELAN0000", 0 }, 1125 { } 1126 }; 1127 MODULE_DEVICE_TABLE(acpi, elan_acpi_id); 1128 #endif 1129 1130 #ifdef CONFIG_OF 1131 static const struct of_device_id elan_of_match[] = { 1132 { .compatible = "elan,ekth3000" }, 1133 { /* sentinel */ } 1134 }; 1135 MODULE_DEVICE_TABLE(of, elan_of_match); 1136 #endif 1137 1138 static struct i2c_driver elan_driver = { 1139 .driver = { 1140 .name = DRIVER_NAME, 1141 .owner = THIS_MODULE, 1142 .pm = &elan_pm_ops, 1143 .acpi_match_table = ACPI_PTR(elan_acpi_id), 1144 .of_match_table = of_match_ptr(elan_of_match), 1145 }, 1146 .probe = elan_probe, 1147 .id_table = elan_id, 1148 }; 1149 1150 module_i2c_driver(elan_driver); 1151 1152 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>"); 1153 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver"); 1154 MODULE_LICENSE("GPL"); 1155 MODULE_VERSION(ELAN_DRIVER_VERSION); 1156