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