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