1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de> 4 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support) 5 * Lothar Waßmann <LW@KARO-electronics.de> (DT support) 6 */ 7 8 /* 9 * This is a driver for the EDT "Polytouch" family of touch controllers 10 * based on the FocalTech FT5x06 line of chips. 11 * 12 * Development of this driver has been sponsored by Glyn: 13 * http://www.glyn.com/Products/Displays 14 */ 15 16 #include <linux/debugfs.h> 17 #include <linux/delay.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/i2c.h> 20 #include <linux/interrupt.h> 21 #include <linux/input.h> 22 #include <linux/input/mt.h> 23 #include <linux/input/touchscreen.h> 24 #include <linux/irq.h> 25 #include <linux/kernel.h> 26 #include <linux/module.h> 27 #include <linux/property.h> 28 #include <linux/ratelimit.h> 29 #include <linux/regulator/consumer.h> 30 #include <linux/slab.h> 31 #include <linux/uaccess.h> 32 33 #include <asm/unaligned.h> 34 35 #define WORK_REGISTER_THRESHOLD 0x00 36 #define WORK_REGISTER_REPORT_RATE 0x08 37 #define WORK_REGISTER_GAIN 0x30 38 #define WORK_REGISTER_OFFSET 0x31 39 #define WORK_REGISTER_NUM_X 0x33 40 #define WORK_REGISTER_NUM_Y 0x34 41 42 #define PMOD_REGISTER_ACTIVE 0x00 43 #define PMOD_REGISTER_HIBERNATE 0x03 44 45 #define M09_REGISTER_THRESHOLD 0x80 46 #define M09_REGISTER_GAIN 0x92 47 #define M09_REGISTER_OFFSET 0x93 48 #define M09_REGISTER_NUM_X 0x94 49 #define M09_REGISTER_NUM_Y 0x95 50 51 #define M12_REGISTER_REPORT_RATE 0x88 52 53 #define EV_REGISTER_THRESHOLD 0x40 54 #define EV_REGISTER_GAIN 0x41 55 #define EV_REGISTER_OFFSET_Y 0x45 56 #define EV_REGISTER_OFFSET_X 0x46 57 58 #define NO_REGISTER 0xff 59 60 #define WORK_REGISTER_OPMODE 0x3c 61 #define FACTORY_REGISTER_OPMODE 0x01 62 #define PMOD_REGISTER_OPMODE 0xa5 63 64 #define TOUCH_EVENT_DOWN 0x00 65 #define TOUCH_EVENT_UP 0x01 66 #define TOUCH_EVENT_ON 0x02 67 #define TOUCH_EVENT_RESERVED 0x03 68 69 #define EDT_NAME_LEN 23 70 #define EDT_SWITCH_MODE_RETRIES 10 71 #define EDT_SWITCH_MODE_DELAY 5 /* msec */ 72 #define EDT_RAW_DATA_RETRIES 100 73 #define EDT_RAW_DATA_DELAY 1000 /* usec */ 74 75 #define EDT_DEFAULT_NUM_X 1024 76 #define EDT_DEFAULT_NUM_Y 1024 77 78 enum edt_pmode { 79 EDT_PMODE_NOT_SUPPORTED, 80 EDT_PMODE_HIBERNATE, 81 EDT_PMODE_POWEROFF, 82 }; 83 84 enum edt_ver { 85 EDT_M06, 86 EDT_M09, 87 EDT_M12, 88 EV_FT, 89 GENERIC_FT, 90 }; 91 92 struct edt_reg_addr { 93 int reg_threshold; 94 int reg_report_rate; 95 int reg_gain; 96 int reg_offset; 97 int reg_offset_x; 98 int reg_offset_y; 99 int reg_num_x; 100 int reg_num_y; 101 }; 102 103 struct edt_ft5x06_ts_data { 104 struct i2c_client *client; 105 struct input_dev *input; 106 struct touchscreen_properties prop; 107 u16 num_x; 108 u16 num_y; 109 struct regulator *vcc; 110 struct regulator *iovcc; 111 112 struct gpio_desc *reset_gpio; 113 struct gpio_desc *wake_gpio; 114 115 #if defined(CONFIG_DEBUG_FS) 116 struct dentry *debug_dir; 117 u8 *raw_buffer; 118 size_t raw_bufsize; 119 #endif 120 121 struct mutex mutex; 122 bool factory_mode; 123 enum edt_pmode suspend_mode; 124 int threshold; 125 int gain; 126 int offset; 127 int offset_x; 128 int offset_y; 129 int report_rate; 130 int max_support_points; 131 132 char name[EDT_NAME_LEN]; 133 char fw_version[EDT_NAME_LEN]; 134 135 struct edt_reg_addr reg_addr; 136 enum edt_ver version; 137 unsigned int crc_errors; 138 unsigned int header_errors; 139 }; 140 141 struct edt_i2c_chip_data { 142 int max_support_points; 143 }; 144 145 static int edt_ft5x06_ts_readwrite(struct i2c_client *client, 146 u16 wr_len, u8 *wr_buf, 147 u16 rd_len, u8 *rd_buf) 148 { 149 struct i2c_msg wrmsg[2]; 150 int i = 0; 151 int ret; 152 153 if (wr_len) { 154 wrmsg[i].addr = client->addr; 155 wrmsg[i].flags = 0; 156 wrmsg[i].len = wr_len; 157 wrmsg[i].buf = wr_buf; 158 i++; 159 } 160 if (rd_len) { 161 wrmsg[i].addr = client->addr; 162 wrmsg[i].flags = I2C_M_RD; 163 wrmsg[i].len = rd_len; 164 wrmsg[i].buf = rd_buf; 165 i++; 166 } 167 168 ret = i2c_transfer(client->adapter, wrmsg, i); 169 if (ret < 0) 170 return ret; 171 if (ret != i) 172 return -EIO; 173 174 return 0; 175 } 176 177 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata, 178 u8 *buf, int buflen) 179 { 180 int i; 181 u8 crc = 0; 182 183 for (i = 0; i < buflen - 1; i++) 184 crc ^= buf[i]; 185 186 if (crc != buf[buflen-1]) { 187 tsdata->crc_errors++; 188 dev_err_ratelimited(&tsdata->client->dev, 189 "crc error: 0x%02x expected, got 0x%02x\n", 190 crc, buf[buflen-1]); 191 return false; 192 } 193 194 return true; 195 } 196 197 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id) 198 { 199 struct edt_ft5x06_ts_data *tsdata = dev_id; 200 struct device *dev = &tsdata->client->dev; 201 u8 cmd; 202 u8 rdbuf[63]; 203 int i, type, x, y, id; 204 int offset, tplen, datalen, crclen; 205 int error; 206 207 switch (tsdata->version) { 208 case EDT_M06: 209 cmd = 0xf9; /* tell the controller to send touch data */ 210 offset = 5; /* where the actual touch data starts */ 211 tplen = 4; /* data comes in so called frames */ 212 crclen = 1; /* length of the crc data */ 213 break; 214 215 case EDT_M09: 216 case EDT_M12: 217 case EV_FT: 218 case GENERIC_FT: 219 cmd = 0x0; 220 offset = 3; 221 tplen = 6; 222 crclen = 0; 223 break; 224 225 default: 226 goto out; 227 } 228 229 memset(rdbuf, 0, sizeof(rdbuf)); 230 datalen = tplen * tsdata->max_support_points + offset + crclen; 231 232 error = edt_ft5x06_ts_readwrite(tsdata->client, 233 sizeof(cmd), &cmd, 234 datalen, rdbuf); 235 if (error) { 236 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n", 237 error); 238 goto out; 239 } 240 241 /* M09/M12 does not send header or CRC */ 242 if (tsdata->version == EDT_M06) { 243 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || 244 rdbuf[2] != datalen) { 245 tsdata->header_errors++; 246 dev_err_ratelimited(dev, 247 "Unexpected header: %02x%02x%02x!\n", 248 rdbuf[0], rdbuf[1], rdbuf[2]); 249 goto out; 250 } 251 252 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen)) 253 goto out; 254 } 255 256 for (i = 0; i < tsdata->max_support_points; i++) { 257 u8 *buf = &rdbuf[i * tplen + offset]; 258 259 type = buf[0] >> 6; 260 /* ignore Reserved events */ 261 if (type == TOUCH_EVENT_RESERVED) 262 continue; 263 264 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */ 265 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN) 266 continue; 267 268 x = get_unaligned_be16(buf) & 0x0fff; 269 y = get_unaligned_be16(buf + 2) & 0x0fff; 270 /* The FT5x26 send the y coordinate first */ 271 if (tsdata->version == EV_FT) 272 swap(x, y); 273 274 id = (buf[2] >> 4) & 0x0f; 275 276 input_mt_slot(tsdata->input, id); 277 if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, 278 type != TOUCH_EVENT_UP)) 279 touchscreen_report_pos(tsdata->input, &tsdata->prop, 280 x, y, true); 281 } 282 283 input_mt_report_pointer_emulation(tsdata->input, true); 284 input_sync(tsdata->input); 285 286 out: 287 return IRQ_HANDLED; 288 } 289 290 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata, 291 u8 addr, u8 value) 292 { 293 u8 wrbuf[4]; 294 295 switch (tsdata->version) { 296 case EDT_M06: 297 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; 298 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; 299 wrbuf[2] = value; 300 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2]; 301 return edt_ft5x06_ts_readwrite(tsdata->client, 4, 302 wrbuf, 0, NULL); 303 304 case EDT_M09: 305 case EDT_M12: 306 case EV_FT: 307 case GENERIC_FT: 308 wrbuf[0] = addr; 309 wrbuf[1] = value; 310 311 return edt_ft5x06_ts_readwrite(tsdata->client, 2, 312 wrbuf, 0, NULL); 313 314 default: 315 return -EINVAL; 316 } 317 } 318 319 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata, 320 u8 addr) 321 { 322 u8 wrbuf[2], rdbuf[2]; 323 int error; 324 325 switch (tsdata->version) { 326 case EDT_M06: 327 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc; 328 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f; 329 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40; 330 331 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2, 332 rdbuf); 333 if (error) 334 return error; 335 336 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) { 337 dev_err(&tsdata->client->dev, 338 "crc error: 0x%02x expected, got 0x%02x\n", 339 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], 340 rdbuf[1]); 341 return -EIO; 342 } 343 break; 344 345 case EDT_M09: 346 case EDT_M12: 347 case EV_FT: 348 case GENERIC_FT: 349 wrbuf[0] = addr; 350 error = edt_ft5x06_ts_readwrite(tsdata->client, 1, 351 wrbuf, 1, rdbuf); 352 if (error) 353 return error; 354 break; 355 356 default: 357 return -EINVAL; 358 } 359 360 return rdbuf[0]; 361 } 362 363 struct edt_ft5x06_attribute { 364 struct device_attribute dattr; 365 size_t field_offset; 366 u8 limit_low; 367 u8 limit_high; 368 u8 addr_m06; 369 u8 addr_m09; 370 u8 addr_ev; 371 }; 372 373 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \ 374 _limit_low, _limit_high) \ 375 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \ 376 .dattr = __ATTR(_field, _mode, \ 377 edt_ft5x06_setting_show, \ 378 edt_ft5x06_setting_store), \ 379 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \ 380 .addr_m06 = _addr_m06, \ 381 .addr_m09 = _addr_m09, \ 382 .addr_ev = _addr_ev, \ 383 .limit_low = _limit_low, \ 384 .limit_high = _limit_high, \ 385 } 386 387 static ssize_t edt_ft5x06_setting_show(struct device *dev, 388 struct device_attribute *dattr, 389 char *buf) 390 { 391 struct i2c_client *client = to_i2c_client(dev); 392 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 393 struct edt_ft5x06_attribute *attr = 394 container_of(dattr, struct edt_ft5x06_attribute, dattr); 395 u8 *field = (u8 *)tsdata + attr->field_offset; 396 int val; 397 size_t count = 0; 398 int error = 0; 399 u8 addr; 400 401 mutex_lock(&tsdata->mutex); 402 403 if (tsdata->factory_mode) { 404 error = -EIO; 405 goto out; 406 } 407 408 switch (tsdata->version) { 409 case EDT_M06: 410 addr = attr->addr_m06; 411 break; 412 413 case EDT_M09: 414 case EDT_M12: 415 case GENERIC_FT: 416 addr = attr->addr_m09; 417 break; 418 419 case EV_FT: 420 addr = attr->addr_ev; 421 break; 422 423 default: 424 error = -ENODEV; 425 goto out; 426 } 427 428 if (addr != NO_REGISTER) { 429 val = edt_ft5x06_register_read(tsdata, addr); 430 if (val < 0) { 431 error = val; 432 dev_err(&tsdata->client->dev, 433 "Failed to fetch attribute %s, error %d\n", 434 dattr->attr.name, error); 435 goto out; 436 } 437 } else { 438 val = *field; 439 } 440 441 if (val != *field) { 442 dev_warn(&tsdata->client->dev, 443 "%s: read (%d) and stored value (%d) differ\n", 444 dattr->attr.name, val, *field); 445 *field = val; 446 } 447 448 count = scnprintf(buf, PAGE_SIZE, "%d\n", val); 449 out: 450 mutex_unlock(&tsdata->mutex); 451 return error ?: count; 452 } 453 454 static ssize_t edt_ft5x06_setting_store(struct device *dev, 455 struct device_attribute *dattr, 456 const char *buf, size_t count) 457 { 458 struct i2c_client *client = to_i2c_client(dev); 459 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 460 struct edt_ft5x06_attribute *attr = 461 container_of(dattr, struct edt_ft5x06_attribute, dattr); 462 u8 *field = (u8 *)tsdata + attr->field_offset; 463 unsigned int val; 464 int error; 465 u8 addr; 466 467 mutex_lock(&tsdata->mutex); 468 469 if (tsdata->factory_mode) { 470 error = -EIO; 471 goto out; 472 } 473 474 error = kstrtouint(buf, 0, &val); 475 if (error) 476 goto out; 477 478 if (val < attr->limit_low || val > attr->limit_high) { 479 error = -ERANGE; 480 goto out; 481 } 482 483 switch (tsdata->version) { 484 case EDT_M06: 485 addr = attr->addr_m06; 486 break; 487 488 case EDT_M09: 489 case EDT_M12: 490 case GENERIC_FT: 491 addr = attr->addr_m09; 492 break; 493 494 case EV_FT: 495 addr = attr->addr_ev; 496 break; 497 498 default: 499 error = -ENODEV; 500 goto out; 501 } 502 503 if (addr != NO_REGISTER) { 504 error = edt_ft5x06_register_write(tsdata, addr, val); 505 if (error) { 506 dev_err(&tsdata->client->dev, 507 "Failed to update attribute %s, error: %d\n", 508 dattr->attr.name, error); 509 goto out; 510 } 511 } 512 *field = val; 513 514 out: 515 mutex_unlock(&tsdata->mutex); 516 return error ?: count; 517 } 518 519 /* m06, m09: range 0-31, m12: range 0-5 */ 520 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, 521 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31); 522 /* m06, m09: range 0-31, m12: range 0-16 */ 523 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, 524 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31); 525 /* m06, m09, m12: no supported, ev_ft: range 0-80 */ 526 static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER, 527 EV_REGISTER_OFFSET_X, 0, 80); 528 /* m06, m09, m12: no supported, ev_ft: range 0-80 */ 529 static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER, 530 EV_REGISTER_OFFSET_Y, 0, 80); 531 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */ 532 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD, 533 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255); 534 /* m06: range 3 to 14, m12: range 1 to 255 */ 535 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE, 536 M12_REGISTER_REPORT_RATE, NO_REGISTER, 0, 255); 537 538 static ssize_t model_show(struct device *dev, struct device_attribute *attr, 539 char *buf) 540 { 541 struct i2c_client *client = to_i2c_client(dev); 542 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 543 544 return sysfs_emit(buf, "%s\n", tsdata->name); 545 } 546 547 static DEVICE_ATTR_RO(model); 548 549 static ssize_t fw_version_show(struct device *dev, 550 struct device_attribute *attr, char *buf) 551 { 552 struct i2c_client *client = to_i2c_client(dev); 553 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 554 555 return sysfs_emit(buf, "%s\n", tsdata->fw_version); 556 } 557 558 static DEVICE_ATTR_RO(fw_version); 559 560 /* m06 only */ 561 static ssize_t header_errors_show(struct device *dev, 562 struct device_attribute *attr, char *buf) 563 { 564 struct i2c_client *client = to_i2c_client(dev); 565 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 566 567 return sysfs_emit(buf, "%d\n", tsdata->header_errors); 568 } 569 570 static DEVICE_ATTR_RO(header_errors); 571 572 /* m06 only */ 573 static ssize_t crc_errors_show(struct device *dev, 574 struct device_attribute *attr, char *buf) 575 { 576 struct i2c_client *client = to_i2c_client(dev); 577 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 578 579 return sysfs_emit(buf, "%d\n", tsdata->crc_errors); 580 } 581 582 static DEVICE_ATTR_RO(crc_errors); 583 584 static struct attribute *edt_ft5x06_attrs[] = { 585 &edt_ft5x06_attr_gain.dattr.attr, 586 &edt_ft5x06_attr_offset.dattr.attr, 587 &edt_ft5x06_attr_offset_x.dattr.attr, 588 &edt_ft5x06_attr_offset_y.dattr.attr, 589 &edt_ft5x06_attr_threshold.dattr.attr, 590 &edt_ft5x06_attr_report_rate.dattr.attr, 591 &dev_attr_model.attr, 592 &dev_attr_fw_version.attr, 593 &dev_attr_header_errors.attr, 594 &dev_attr_crc_errors.attr, 595 NULL 596 }; 597 598 static const struct attribute_group edt_ft5x06_attr_group = { 599 .attrs = edt_ft5x06_attrs, 600 }; 601 602 static void edt_ft5x06_restore_reg_parameters(struct edt_ft5x06_ts_data *tsdata) 603 { 604 struct edt_reg_addr *reg_addr = &tsdata->reg_addr; 605 606 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, 607 tsdata->threshold); 608 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, 609 tsdata->gain); 610 if (reg_addr->reg_offset != NO_REGISTER) 611 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, 612 tsdata->offset); 613 if (reg_addr->reg_offset_x != NO_REGISTER) 614 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, 615 tsdata->offset_x); 616 if (reg_addr->reg_offset_y != NO_REGISTER) 617 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, 618 tsdata->offset_y); 619 if (reg_addr->reg_report_rate != NO_REGISTER) 620 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate, 621 tsdata->report_rate); 622 623 } 624 625 #ifdef CONFIG_DEBUG_FS 626 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) 627 { 628 struct i2c_client *client = tsdata->client; 629 int retries = EDT_SWITCH_MODE_RETRIES; 630 int ret; 631 int error; 632 633 if (tsdata->version != EDT_M06) { 634 dev_err(&client->dev, 635 "No factory mode support for non-M06 devices\n"); 636 return -EINVAL; 637 } 638 639 disable_irq(client->irq); 640 641 if (!tsdata->raw_buffer) { 642 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y * 643 sizeof(u16); 644 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL); 645 if (!tsdata->raw_buffer) { 646 error = -ENOMEM; 647 goto err_out; 648 } 649 } 650 651 /* mode register is 0x3c when in the work mode */ 652 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03); 653 if (error) { 654 dev_err(&client->dev, 655 "failed to switch to factory mode, error %d\n", error); 656 goto err_out; 657 } 658 659 tsdata->factory_mode = true; 660 do { 661 mdelay(EDT_SWITCH_MODE_DELAY); 662 /* mode register is 0x01 when in factory mode */ 663 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE); 664 if (ret == 0x03) 665 break; 666 } while (--retries > 0); 667 668 if (retries == 0) { 669 dev_err(&client->dev, "not in factory mode after %dms.\n", 670 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); 671 error = -EIO; 672 goto err_out; 673 } 674 675 return 0; 676 677 err_out: 678 kfree(tsdata->raw_buffer); 679 tsdata->raw_buffer = NULL; 680 tsdata->factory_mode = false; 681 enable_irq(client->irq); 682 683 return error; 684 } 685 686 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata) 687 { 688 struct i2c_client *client = tsdata->client; 689 int retries = EDT_SWITCH_MODE_RETRIES; 690 int ret; 691 int error; 692 693 /* mode register is 0x01 when in the factory mode */ 694 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1); 695 if (error) { 696 dev_err(&client->dev, 697 "failed to switch to work mode, error: %d\n", error); 698 return error; 699 } 700 701 tsdata->factory_mode = false; 702 703 do { 704 mdelay(EDT_SWITCH_MODE_DELAY); 705 /* mode register is 0x01 when in factory mode */ 706 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE); 707 if (ret == 0x01) 708 break; 709 } while (--retries > 0); 710 711 if (retries == 0) { 712 dev_err(&client->dev, "not in work mode after %dms.\n", 713 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY); 714 tsdata->factory_mode = true; 715 return -EIO; 716 } 717 718 kfree(tsdata->raw_buffer); 719 tsdata->raw_buffer = NULL; 720 721 edt_ft5x06_restore_reg_parameters(tsdata); 722 enable_irq(client->irq); 723 724 return 0; 725 } 726 727 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode) 728 { 729 struct edt_ft5x06_ts_data *tsdata = data; 730 731 *mode = tsdata->factory_mode; 732 733 return 0; 734 }; 735 736 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode) 737 { 738 struct edt_ft5x06_ts_data *tsdata = data; 739 int retval = 0; 740 741 if (mode > 1) 742 return -ERANGE; 743 744 mutex_lock(&tsdata->mutex); 745 746 if (mode != tsdata->factory_mode) { 747 retval = mode ? edt_ft5x06_factory_mode(tsdata) : 748 edt_ft5x06_work_mode(tsdata); 749 } 750 751 mutex_unlock(&tsdata->mutex); 752 753 return retval; 754 }; 755 756 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get, 757 edt_ft5x06_debugfs_mode_set, "%llu\n"); 758 759 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file, 760 char __user *buf, size_t count, loff_t *off) 761 { 762 struct edt_ft5x06_ts_data *tsdata = file->private_data; 763 struct i2c_client *client = tsdata->client; 764 int retries = EDT_RAW_DATA_RETRIES; 765 int val, i, error; 766 size_t read = 0; 767 int colbytes; 768 char wrbuf[3]; 769 u8 *rdbuf; 770 771 if (*off < 0 || *off >= tsdata->raw_bufsize) 772 return 0; 773 774 mutex_lock(&tsdata->mutex); 775 776 if (!tsdata->factory_mode || !tsdata->raw_buffer) { 777 error = -EIO; 778 goto out; 779 } 780 781 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01); 782 if (error) { 783 dev_dbg(&client->dev, 784 "failed to write 0x08 register, error %d\n", error); 785 goto out; 786 } 787 788 do { 789 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100); 790 val = edt_ft5x06_register_read(tsdata, 0x08); 791 if (val < 1) 792 break; 793 } while (--retries > 0); 794 795 if (val < 0) { 796 error = val; 797 dev_dbg(&client->dev, 798 "failed to read 0x08 register, error %d\n", error); 799 goto out; 800 } 801 802 if (retries == 0) { 803 dev_dbg(&client->dev, 804 "timed out waiting for register to settle\n"); 805 error = -ETIMEDOUT; 806 goto out; 807 } 808 809 rdbuf = tsdata->raw_buffer; 810 colbytes = tsdata->num_y * sizeof(u16); 811 812 wrbuf[0] = 0xf5; 813 wrbuf[1] = 0x0e; 814 for (i = 0; i < tsdata->num_x; i++) { 815 wrbuf[2] = i; /* column index */ 816 error = edt_ft5x06_ts_readwrite(tsdata->client, 817 sizeof(wrbuf), wrbuf, 818 colbytes, rdbuf); 819 if (error) 820 goto out; 821 822 rdbuf += colbytes; 823 } 824 825 read = min_t(size_t, count, tsdata->raw_bufsize - *off); 826 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) { 827 error = -EFAULT; 828 goto out; 829 } 830 831 *off += read; 832 out: 833 mutex_unlock(&tsdata->mutex); 834 return error ?: read; 835 }; 836 837 static const struct file_operations debugfs_raw_data_fops = { 838 .open = simple_open, 839 .read = edt_ft5x06_debugfs_raw_data_read, 840 }; 841 842 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, 843 const char *debugfs_name) 844 { 845 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL); 846 847 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x); 848 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y); 849 850 debugfs_create_file("mode", S_IRUSR | S_IWUSR, 851 tsdata->debug_dir, tsdata, &debugfs_mode_fops); 852 debugfs_create_file("raw_data", S_IRUSR, 853 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops); 854 } 855 856 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) 857 { 858 debugfs_remove_recursive(tsdata->debug_dir); 859 kfree(tsdata->raw_buffer); 860 } 861 862 #else 863 864 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata) 865 { 866 return -ENOSYS; 867 } 868 869 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata, 870 const char *debugfs_name) 871 { 872 } 873 874 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata) 875 { 876 } 877 878 #endif /* CONFIG_DEBUGFS */ 879 880 static int edt_ft5x06_ts_identify(struct i2c_client *client, 881 struct edt_ft5x06_ts_data *tsdata) 882 { 883 u8 rdbuf[EDT_NAME_LEN]; 884 char *p; 885 int error; 886 char *model_name = tsdata->name; 887 char *fw_version = tsdata->fw_version; 888 889 /* see what we find if we assume it is a M06 * 890 * if we get less than EDT_NAME_LEN, we don't want 891 * to have garbage in there 892 */ 893 memset(rdbuf, 0, sizeof(rdbuf)); 894 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB", 895 EDT_NAME_LEN - 1, rdbuf); 896 if (error) 897 return error; 898 899 /* Probe content for something consistent. 900 * M06 starts with a response byte, M12 gives the data directly. 901 * M09/Generic does not provide model number information. 902 */ 903 if (!strncasecmp(rdbuf + 1, "EP0", 3)) { 904 tsdata->version = EDT_M06; 905 906 /* remove last '$' end marker */ 907 rdbuf[EDT_NAME_LEN - 1] = '\0'; 908 if (rdbuf[EDT_NAME_LEN - 2] == '$') 909 rdbuf[EDT_NAME_LEN - 2] = '\0'; 910 911 /* look for Model/Version separator */ 912 p = strchr(rdbuf, '*'); 913 if (p) 914 *p++ = '\0'; 915 strscpy(model_name, rdbuf + 1, EDT_NAME_LEN); 916 strscpy(fw_version, p ? p : "", EDT_NAME_LEN); 917 } else if (!strncasecmp(rdbuf, "EP0", 3)) { 918 tsdata->version = EDT_M12; 919 920 /* remove last '$' end marker */ 921 rdbuf[EDT_NAME_LEN - 2] = '\0'; 922 if (rdbuf[EDT_NAME_LEN - 3] == '$') 923 rdbuf[EDT_NAME_LEN - 3] = '\0'; 924 925 /* look for Model/Version separator */ 926 p = strchr(rdbuf, '*'); 927 if (p) 928 *p++ = '\0'; 929 strscpy(model_name, rdbuf, EDT_NAME_LEN); 930 strscpy(fw_version, p ? p : "", EDT_NAME_LEN); 931 } else { 932 /* If it is not an EDT M06/M12 touchscreen, then the model 933 * detection is a bit hairy. The different ft5x06 934 * firmares around don't reliably implement the 935 * identification registers. Well, we'll take a shot. 936 * 937 * The main difference between generic focaltec based 938 * touches and EDT M09 is that we know how to retrieve 939 * the max coordinates for the latter. 940 */ 941 tsdata->version = GENERIC_FT; 942 943 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6", 944 2, rdbuf); 945 if (error) 946 return error; 947 948 strscpy(fw_version, rdbuf, 2); 949 950 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8", 951 1, rdbuf); 952 if (error) 953 return error; 954 955 /* This "model identification" is not exact. Unfortunately 956 * not all firmwares for the ft5x06 put useful values in 957 * the identification registers. 958 */ 959 switch (rdbuf[0]) { 960 case 0x11: /* EDT EP0110M09 */ 961 case 0x35: /* EDT EP0350M09 */ 962 case 0x43: /* EDT EP0430M09 */ 963 case 0x50: /* EDT EP0500M09 */ 964 case 0x57: /* EDT EP0570M09 */ 965 case 0x70: /* EDT EP0700M09 */ 966 tsdata->version = EDT_M09; 967 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09", 968 rdbuf[0] >> 4, rdbuf[0] & 0x0F); 969 break; 970 case 0xa1: /* EDT EP1010ML00 */ 971 tsdata->version = EDT_M09; 972 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00", 973 rdbuf[0] >> 4, rdbuf[0] & 0x0F); 974 break; 975 case 0x5a: /* Solomon Goldentek Display */ 976 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0"); 977 break; 978 case 0x59: /* Evervision Display with FT5xx6 TS */ 979 tsdata->version = EV_FT; 980 error = edt_ft5x06_ts_readwrite(client, 1, "\x53", 981 1, rdbuf); 982 if (error) 983 return error; 984 strscpy(fw_version, rdbuf, 1); 985 snprintf(model_name, EDT_NAME_LEN, 986 "EVERVISION-FT5726NEi"); 987 break; 988 default: 989 snprintf(model_name, EDT_NAME_LEN, 990 "generic ft5x06 (%02x)", 991 rdbuf[0]); 992 break; 993 } 994 } 995 996 return 0; 997 } 998 999 static void edt_ft5x06_ts_get_defaults(struct device *dev, 1000 struct edt_ft5x06_ts_data *tsdata) 1001 { 1002 struct edt_reg_addr *reg_addr = &tsdata->reg_addr; 1003 u32 val; 1004 int error; 1005 1006 error = device_property_read_u32(dev, "threshold", &val); 1007 if (!error) { 1008 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val); 1009 tsdata->threshold = val; 1010 } 1011 1012 error = device_property_read_u32(dev, "gain", &val); 1013 if (!error) { 1014 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val); 1015 tsdata->gain = val; 1016 } 1017 1018 error = device_property_read_u32(dev, "offset", &val); 1019 if (!error) { 1020 if (reg_addr->reg_offset != NO_REGISTER) 1021 edt_ft5x06_register_write(tsdata, 1022 reg_addr->reg_offset, val); 1023 tsdata->offset = val; 1024 } 1025 1026 error = device_property_read_u32(dev, "offset-x", &val); 1027 if (!error) { 1028 if (reg_addr->reg_offset_x != NO_REGISTER) 1029 edt_ft5x06_register_write(tsdata, 1030 reg_addr->reg_offset_x, val); 1031 tsdata->offset_x = val; 1032 } 1033 1034 error = device_property_read_u32(dev, "offset-y", &val); 1035 if (!error) { 1036 if (reg_addr->reg_offset_y != NO_REGISTER) 1037 edt_ft5x06_register_write(tsdata, 1038 reg_addr->reg_offset_y, val); 1039 tsdata->offset_y = val; 1040 } 1041 } 1042 1043 static void edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata) 1044 { 1045 struct edt_reg_addr *reg_addr = &tsdata->reg_addr; 1046 1047 tsdata->threshold = edt_ft5x06_register_read(tsdata, 1048 reg_addr->reg_threshold); 1049 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain); 1050 if (reg_addr->reg_offset != NO_REGISTER) 1051 tsdata->offset = 1052 edt_ft5x06_register_read(tsdata, reg_addr->reg_offset); 1053 if (reg_addr->reg_offset_x != NO_REGISTER) 1054 tsdata->offset_x = edt_ft5x06_register_read(tsdata, 1055 reg_addr->reg_offset_x); 1056 if (reg_addr->reg_offset_y != NO_REGISTER) 1057 tsdata->offset_y = edt_ft5x06_register_read(tsdata, 1058 reg_addr->reg_offset_y); 1059 if (reg_addr->reg_report_rate != NO_REGISTER) 1060 tsdata->report_rate = edt_ft5x06_register_read(tsdata, 1061 reg_addr->reg_report_rate); 1062 tsdata->num_x = EDT_DEFAULT_NUM_X; 1063 if (reg_addr->reg_num_x != NO_REGISTER) 1064 tsdata->num_x = edt_ft5x06_register_read(tsdata, 1065 reg_addr->reg_num_x); 1066 tsdata->num_y = EDT_DEFAULT_NUM_Y; 1067 if (reg_addr->reg_num_y != NO_REGISTER) 1068 tsdata->num_y = edt_ft5x06_register_read(tsdata, 1069 reg_addr->reg_num_y); 1070 } 1071 1072 static void edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata) 1073 { 1074 struct edt_reg_addr *reg_addr = &tsdata->reg_addr; 1075 1076 switch (tsdata->version) { 1077 case EDT_M06: 1078 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD; 1079 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE; 1080 reg_addr->reg_gain = WORK_REGISTER_GAIN; 1081 reg_addr->reg_offset = WORK_REGISTER_OFFSET; 1082 reg_addr->reg_offset_x = NO_REGISTER; 1083 reg_addr->reg_offset_y = NO_REGISTER; 1084 reg_addr->reg_num_x = WORK_REGISTER_NUM_X; 1085 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y; 1086 break; 1087 1088 case EDT_M09: 1089 case EDT_M12: 1090 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; 1091 reg_addr->reg_report_rate = tsdata->version == EDT_M12 ? 1092 M12_REGISTER_REPORT_RATE : NO_REGISTER; 1093 reg_addr->reg_gain = M09_REGISTER_GAIN; 1094 reg_addr->reg_offset = M09_REGISTER_OFFSET; 1095 reg_addr->reg_offset_x = NO_REGISTER; 1096 reg_addr->reg_offset_y = NO_REGISTER; 1097 reg_addr->reg_num_x = M09_REGISTER_NUM_X; 1098 reg_addr->reg_num_y = M09_REGISTER_NUM_Y; 1099 break; 1100 1101 case EV_FT: 1102 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD; 1103 reg_addr->reg_report_rate = NO_REGISTER; 1104 reg_addr->reg_gain = EV_REGISTER_GAIN; 1105 reg_addr->reg_offset = NO_REGISTER; 1106 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X; 1107 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y; 1108 reg_addr->reg_num_x = NO_REGISTER; 1109 reg_addr->reg_num_y = NO_REGISTER; 1110 break; 1111 1112 case GENERIC_FT: 1113 /* this is a guesswork */ 1114 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD; 1115 reg_addr->reg_report_rate = NO_REGISTER; 1116 reg_addr->reg_gain = M09_REGISTER_GAIN; 1117 reg_addr->reg_offset = M09_REGISTER_OFFSET; 1118 reg_addr->reg_offset_x = NO_REGISTER; 1119 reg_addr->reg_offset_y = NO_REGISTER; 1120 reg_addr->reg_num_x = NO_REGISTER; 1121 reg_addr->reg_num_y = NO_REGISTER; 1122 break; 1123 } 1124 } 1125 1126 static void edt_ft5x06_disable_regulators(void *arg) 1127 { 1128 struct edt_ft5x06_ts_data *data = arg; 1129 1130 regulator_disable(data->vcc); 1131 regulator_disable(data->iovcc); 1132 } 1133 1134 static int edt_ft5x06_ts_probe(struct i2c_client *client) 1135 { 1136 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1137 const struct edt_i2c_chip_data *chip_data; 1138 struct edt_ft5x06_ts_data *tsdata; 1139 u8 buf[2] = { 0xfc, 0x00 }; 1140 struct input_dev *input; 1141 unsigned long irq_flags; 1142 int error; 1143 u32 report_rate; 1144 1145 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); 1146 1147 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL); 1148 if (!tsdata) { 1149 dev_err(&client->dev, "failed to allocate driver data.\n"); 1150 return -ENOMEM; 1151 } 1152 1153 chip_data = device_get_match_data(&client->dev); 1154 if (!chip_data) 1155 chip_data = (const struct edt_i2c_chip_data *)id->driver_data; 1156 if (!chip_data || !chip_data->max_support_points) { 1157 dev_err(&client->dev, "invalid or missing chip data\n"); 1158 return -EINVAL; 1159 } 1160 1161 tsdata->max_support_points = chip_data->max_support_points; 1162 1163 tsdata->vcc = devm_regulator_get(&client->dev, "vcc"); 1164 if (IS_ERR(tsdata->vcc)) { 1165 error = PTR_ERR(tsdata->vcc); 1166 if (error != -EPROBE_DEFER) 1167 dev_err(&client->dev, 1168 "failed to request regulator: %d\n", error); 1169 return error; 1170 } 1171 1172 tsdata->iovcc = devm_regulator_get(&client->dev, "iovcc"); 1173 if (IS_ERR(tsdata->iovcc)) { 1174 error = PTR_ERR(tsdata->iovcc); 1175 if (error != -EPROBE_DEFER) 1176 dev_err(&client->dev, 1177 "failed to request iovcc regulator: %d\n", error); 1178 return error; 1179 } 1180 1181 error = regulator_enable(tsdata->iovcc); 1182 if (error < 0) { 1183 dev_err(&client->dev, "failed to enable iovcc: %d\n", error); 1184 return error; 1185 } 1186 1187 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */ 1188 usleep_range(10, 100); 1189 1190 error = regulator_enable(tsdata->vcc); 1191 if (error < 0) { 1192 dev_err(&client->dev, "failed to enable vcc: %d\n", error); 1193 regulator_disable(tsdata->iovcc); 1194 return error; 1195 } 1196 1197 error = devm_add_action_or_reset(&client->dev, 1198 edt_ft5x06_disable_regulators, 1199 tsdata); 1200 if (error) 1201 return error; 1202 1203 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev, 1204 "reset", GPIOD_OUT_HIGH); 1205 if (IS_ERR(tsdata->reset_gpio)) { 1206 error = PTR_ERR(tsdata->reset_gpio); 1207 dev_err(&client->dev, 1208 "Failed to request GPIO reset pin, error %d\n", error); 1209 return error; 1210 } 1211 1212 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev, 1213 "wake", GPIOD_OUT_LOW); 1214 if (IS_ERR(tsdata->wake_gpio)) { 1215 error = PTR_ERR(tsdata->wake_gpio); 1216 dev_err(&client->dev, 1217 "Failed to request GPIO wake pin, error %d\n", error); 1218 return error; 1219 } 1220 1221 /* 1222 * Check which sleep modes we can support. Power-off requieres the 1223 * reset-pin to ensure correct power-down/power-up behaviour. Start with 1224 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep 1225 * mode. 1226 */ 1227 if (tsdata->reset_gpio) 1228 tsdata->suspend_mode = EDT_PMODE_POWEROFF; 1229 else if (tsdata->wake_gpio) 1230 tsdata->suspend_mode = EDT_PMODE_HIBERNATE; 1231 else 1232 tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED; 1233 1234 if (tsdata->wake_gpio) { 1235 usleep_range(5000, 6000); 1236 gpiod_set_value_cansleep(tsdata->wake_gpio, 1); 1237 } 1238 1239 if (tsdata->reset_gpio) { 1240 usleep_range(5000, 6000); 1241 gpiod_set_value_cansleep(tsdata->reset_gpio, 0); 1242 msleep(300); 1243 } 1244 1245 input = devm_input_allocate_device(&client->dev); 1246 if (!input) { 1247 dev_err(&client->dev, "failed to allocate input device.\n"); 1248 return -ENOMEM; 1249 } 1250 1251 mutex_init(&tsdata->mutex); 1252 tsdata->client = client; 1253 tsdata->input = input; 1254 tsdata->factory_mode = false; 1255 1256 error = edt_ft5x06_ts_identify(client, tsdata); 1257 if (error) { 1258 dev_err(&client->dev, "touchscreen probe failed\n"); 1259 return error; 1260 } 1261 1262 /* 1263 * Dummy read access. EP0700MLP1 returns bogus data on the first 1264 * register read access and ignores writes. 1265 */ 1266 edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf); 1267 1268 edt_ft5x06_ts_set_regs(tsdata); 1269 edt_ft5x06_ts_get_defaults(&client->dev, tsdata); 1270 edt_ft5x06_ts_get_parameters(tsdata); 1271 1272 if (tsdata->reg_addr.reg_report_rate != NO_REGISTER && 1273 !device_property_read_u32(&client->dev, 1274 "report-rate-hz", &report_rate)) { 1275 if (tsdata->version == EDT_M06) 1276 tsdata->report_rate = clamp_val(report_rate, 30, 140); 1277 else 1278 tsdata->report_rate = clamp_val(report_rate, 1, 255); 1279 1280 if (report_rate != tsdata->report_rate) 1281 dev_warn(&client->dev, 1282 "report-rate %dHz is unsupported, use %dHz\n", 1283 report_rate, tsdata->report_rate); 1284 1285 if (tsdata->version == EDT_M06) 1286 tsdata->report_rate /= 10; 1287 1288 edt_ft5x06_register_write(tsdata, 1289 tsdata->reg_addr.reg_report_rate, 1290 tsdata->report_rate); 1291 } 1292 1293 dev_dbg(&client->dev, 1294 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", 1295 tsdata->name, tsdata->fw_version, tsdata->num_x, tsdata->num_y); 1296 1297 input->name = tsdata->name; 1298 input->id.bustype = BUS_I2C; 1299 input->dev.parent = &client->dev; 1300 1301 input_set_abs_params(input, ABS_MT_POSITION_X, 1302 0, tsdata->num_x * 64 - 1, 0, 0); 1303 input_set_abs_params(input, ABS_MT_POSITION_Y, 1304 0, tsdata->num_y * 64 - 1, 0, 0); 1305 1306 touchscreen_parse_properties(input, true, &tsdata->prop); 1307 1308 error = input_mt_init_slots(input, tsdata->max_support_points, 1309 INPUT_MT_DIRECT); 1310 if (error) { 1311 dev_err(&client->dev, "Unable to init MT slots.\n"); 1312 return error; 1313 } 1314 1315 i2c_set_clientdata(client, tsdata); 1316 1317 irq_flags = irq_get_trigger_type(client->irq); 1318 if (irq_flags == IRQF_TRIGGER_NONE) 1319 irq_flags = IRQF_TRIGGER_FALLING; 1320 irq_flags |= IRQF_ONESHOT; 1321 1322 error = devm_request_threaded_irq(&client->dev, client->irq, 1323 NULL, edt_ft5x06_ts_isr, irq_flags, 1324 client->name, tsdata); 1325 if (error) { 1326 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 1327 return error; 1328 } 1329 1330 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group); 1331 if (error) 1332 return error; 1333 1334 error = input_register_device(input); 1335 if (error) 1336 return error; 1337 1338 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); 1339 1340 dev_dbg(&client->dev, 1341 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n", 1342 client->irq, 1343 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1, 1344 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1); 1345 1346 return 0; 1347 } 1348 1349 static void edt_ft5x06_ts_remove(struct i2c_client *client) 1350 { 1351 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1352 1353 edt_ft5x06_ts_teardown_debugfs(tsdata); 1354 } 1355 1356 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev) 1357 { 1358 struct i2c_client *client = to_i2c_client(dev); 1359 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1360 struct gpio_desc *reset_gpio = tsdata->reset_gpio; 1361 int ret; 1362 1363 if (device_may_wakeup(dev)) 1364 return 0; 1365 1366 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED) 1367 return 0; 1368 1369 /* Enter hibernate mode. */ 1370 ret = edt_ft5x06_register_write(tsdata, PMOD_REGISTER_OPMODE, 1371 PMOD_REGISTER_HIBERNATE); 1372 if (ret) 1373 dev_warn(dev, "Failed to set hibernate mode\n"); 1374 1375 if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE) 1376 return 0; 1377 1378 /* 1379 * Power-off according the datasheet. Cut the power may leaf the irq 1380 * line in an undefined state depending on the host pull resistor 1381 * settings. Disable the irq to avoid adjusting each host till the 1382 * device is back in a full functional state. 1383 */ 1384 disable_irq(tsdata->client->irq); 1385 1386 gpiod_set_value_cansleep(reset_gpio, 1); 1387 usleep_range(1000, 2000); 1388 1389 ret = regulator_disable(tsdata->vcc); 1390 if (ret) 1391 dev_warn(dev, "Failed to disable vcc\n"); 1392 ret = regulator_disable(tsdata->iovcc); 1393 if (ret) 1394 dev_warn(dev, "Failed to disable iovcc\n"); 1395 1396 return 0; 1397 } 1398 1399 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev) 1400 { 1401 struct i2c_client *client = to_i2c_client(dev); 1402 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1403 int ret = 0; 1404 1405 if (device_may_wakeup(dev)) 1406 return 0; 1407 1408 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED) 1409 return 0; 1410 1411 if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) { 1412 struct gpio_desc *reset_gpio = tsdata->reset_gpio; 1413 1414 /* 1415 * We can't check if the regulator is a dummy or a real 1416 * regulator. So we need to specify the 5ms reset time (T_rst) 1417 * here instead of the 100us T_rtp time. We also need to wait 1418 * 300ms in case it was a real supply and the power was cutted 1419 * of. Toggle the reset pin is also a way to exit the hibernate 1420 * mode. 1421 */ 1422 gpiod_set_value_cansleep(reset_gpio, 1); 1423 usleep_range(5000, 6000); 1424 1425 ret = regulator_enable(tsdata->iovcc); 1426 if (ret) { 1427 dev_err(dev, "Failed to enable iovcc\n"); 1428 return ret; 1429 } 1430 1431 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */ 1432 usleep_range(10, 100); 1433 1434 ret = regulator_enable(tsdata->vcc); 1435 if (ret) { 1436 dev_err(dev, "Failed to enable vcc\n"); 1437 regulator_disable(tsdata->iovcc); 1438 return ret; 1439 } 1440 1441 usleep_range(1000, 2000); 1442 gpiod_set_value_cansleep(reset_gpio, 0); 1443 msleep(300); 1444 1445 edt_ft5x06_restore_reg_parameters(tsdata); 1446 enable_irq(tsdata->client->irq); 1447 1448 if (tsdata->factory_mode) 1449 ret = edt_ft5x06_factory_mode(tsdata); 1450 } else { 1451 struct gpio_desc *wake_gpio = tsdata->wake_gpio; 1452 1453 gpiod_set_value_cansleep(wake_gpio, 0); 1454 usleep_range(5000, 6000); 1455 gpiod_set_value_cansleep(wake_gpio, 1); 1456 } 1457 1458 1459 return ret; 1460 } 1461 1462 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops, 1463 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume); 1464 1465 static const struct edt_i2c_chip_data edt_ft5x06_data = { 1466 .max_support_points = 5, 1467 }; 1468 1469 static const struct edt_i2c_chip_data edt_ft5506_data = { 1470 .max_support_points = 10, 1471 }; 1472 1473 static const struct edt_i2c_chip_data edt_ft6236_data = { 1474 .max_support_points = 2, 1475 }; 1476 1477 static const struct i2c_device_id edt_ft5x06_ts_id[] = { 1478 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data }, 1479 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data }, 1480 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data }, 1481 /* Note no edt- prefix for compatibility with the ft6236.c driver */ 1482 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data }, 1483 { /* sentinel */ } 1484 }; 1485 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); 1486 1487 static const struct of_device_id edt_ft5x06_of_match[] = { 1488 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data }, 1489 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data }, 1490 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data }, 1491 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data }, 1492 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data }, 1493 /* Note focaltech vendor prefix for compatibility with ft6236.c */ 1494 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data }, 1495 { /* sentinel */ } 1496 }; 1497 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match); 1498 1499 static struct i2c_driver edt_ft5x06_ts_driver = { 1500 .driver = { 1501 .name = "edt_ft5x06", 1502 .of_match_table = edt_ft5x06_of_match, 1503 .pm = &edt_ft5x06_ts_pm_ops, 1504 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1505 }, 1506 .id_table = edt_ft5x06_ts_id, 1507 .probe_new = edt_ft5x06_ts_probe, 1508 .remove = edt_ft5x06_ts_remove, 1509 }; 1510 1511 module_i2c_driver(edt_ft5x06_ts_driver); 1512 1513 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); 1514 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); 1515 MODULE_LICENSE("GPL v2"); 1516