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