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