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