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