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 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_exit_regmap(void *arg) 1125 { 1126 struct edt_ft5x06_ts_data *data = arg; 1127 1128 if (!IS_ERR_OR_NULL(data->regmap)) 1129 regmap_exit(data->regmap); 1130 } 1131 1132 static void edt_ft5x06_disable_regulators(void *arg) 1133 { 1134 struct edt_ft5x06_ts_data *data = arg; 1135 1136 regulator_disable(data->vcc); 1137 regulator_disable(data->iovcc); 1138 } 1139 1140 static int edt_ft5x06_ts_probe(struct i2c_client *client) 1141 { 1142 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1143 const struct edt_i2c_chip_data *chip_data; 1144 struct edt_ft5x06_ts_data *tsdata; 1145 unsigned int val; 1146 struct input_dev *input; 1147 unsigned long irq_flags; 1148 int error; 1149 u32 report_rate; 1150 1151 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n"); 1152 1153 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL); 1154 if (!tsdata) { 1155 dev_err(&client->dev, "failed to allocate driver data.\n"); 1156 return -ENOMEM; 1157 } 1158 1159 tsdata->regmap = regmap_init_i2c(client, &edt_ft5x06_i2c_regmap_config); 1160 if (IS_ERR(tsdata->regmap)) { 1161 dev_err(&client->dev, "regmap allocation failed\n"); 1162 return PTR_ERR(tsdata->regmap); 1163 } 1164 1165 /* 1166 * We are not using devm_regmap_init_i2c() and instead install a 1167 * custom action because we may replace regmap with M06-specific one 1168 * and we need to make sure that it will not be released too early. 1169 */ 1170 error = devm_add_action_or_reset(&client->dev, edt_ft5x06_exit_regmap, 1171 tsdata); 1172 if (error) 1173 return error; 1174 1175 chip_data = device_get_match_data(&client->dev); 1176 if (!chip_data) 1177 chip_data = (const struct edt_i2c_chip_data *)id->driver_data; 1178 if (!chip_data || !chip_data->max_support_points) { 1179 dev_err(&client->dev, "invalid or missing chip data\n"); 1180 return -EINVAL; 1181 } 1182 1183 tsdata->max_support_points = chip_data->max_support_points; 1184 1185 tsdata->vcc = devm_regulator_get(&client->dev, "vcc"); 1186 if (IS_ERR(tsdata->vcc)) 1187 return dev_err_probe(&client->dev, PTR_ERR(tsdata->vcc), 1188 "failed to request regulator\n"); 1189 1190 tsdata->iovcc = devm_regulator_get(&client->dev, "iovcc"); 1191 if (IS_ERR(tsdata->iovcc)) { 1192 error = PTR_ERR(tsdata->iovcc); 1193 if (error != -EPROBE_DEFER) 1194 dev_err(&client->dev, 1195 "failed to request iovcc regulator: %d\n", error); 1196 return error; 1197 } 1198 1199 error = regulator_enable(tsdata->iovcc); 1200 if (error < 0) { 1201 dev_err(&client->dev, "failed to enable iovcc: %d\n", error); 1202 return error; 1203 } 1204 1205 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */ 1206 usleep_range(10, 100); 1207 1208 error = regulator_enable(tsdata->vcc); 1209 if (error < 0) { 1210 dev_err(&client->dev, "failed to enable vcc: %d\n", error); 1211 regulator_disable(tsdata->iovcc); 1212 return error; 1213 } 1214 1215 error = devm_add_action_or_reset(&client->dev, 1216 edt_ft5x06_disable_regulators, 1217 tsdata); 1218 if (error) 1219 return error; 1220 1221 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev, 1222 "reset", GPIOD_OUT_HIGH); 1223 if (IS_ERR(tsdata->reset_gpio)) { 1224 error = PTR_ERR(tsdata->reset_gpio); 1225 dev_err(&client->dev, 1226 "Failed to request GPIO reset pin, error %d\n", error); 1227 return error; 1228 } 1229 1230 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev, 1231 "wake", GPIOD_OUT_LOW); 1232 if (IS_ERR(tsdata->wake_gpio)) { 1233 error = PTR_ERR(tsdata->wake_gpio); 1234 dev_err(&client->dev, 1235 "Failed to request GPIO wake pin, error %d\n", error); 1236 return error; 1237 } 1238 1239 /* 1240 * Check which sleep modes we can support. Power-off requires the 1241 * reset-pin to ensure correct power-down/power-up behaviour. Start with 1242 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep 1243 * mode. 1244 */ 1245 if (tsdata->reset_gpio) 1246 tsdata->suspend_mode = EDT_PMODE_POWEROFF; 1247 else if (tsdata->wake_gpio) 1248 tsdata->suspend_mode = EDT_PMODE_HIBERNATE; 1249 else 1250 tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED; 1251 1252 if (tsdata->wake_gpio) { 1253 usleep_range(5000, 6000); 1254 gpiod_set_value_cansleep(tsdata->wake_gpio, 1); 1255 usleep_range(5000, 6000); 1256 } 1257 1258 if (tsdata->reset_gpio) { 1259 usleep_range(5000, 6000); 1260 gpiod_set_value_cansleep(tsdata->reset_gpio, 0); 1261 msleep(300); 1262 } 1263 1264 input = devm_input_allocate_device(&client->dev); 1265 if (!input) { 1266 dev_err(&client->dev, "failed to allocate input device.\n"); 1267 return -ENOMEM; 1268 } 1269 1270 mutex_init(&tsdata->mutex); 1271 tsdata->client = client; 1272 tsdata->input = input; 1273 tsdata->factory_mode = false; 1274 i2c_set_clientdata(client, tsdata); 1275 1276 error = edt_ft5x06_ts_identify(client, tsdata); 1277 if (error) { 1278 dev_err(&client->dev, "touchscreen probe failed\n"); 1279 return error; 1280 } 1281 1282 /* 1283 * Dummy read access. EP0700MLP1 returns bogus data on the first 1284 * register read access and ignores writes. 1285 */ 1286 regmap_read(tsdata->regmap, 0x00, &val); 1287 1288 edt_ft5x06_ts_set_tdata_parameters(tsdata); 1289 edt_ft5x06_ts_set_regs(tsdata); 1290 edt_ft5x06_ts_get_defaults(&client->dev, tsdata); 1291 edt_ft5x06_ts_get_parameters(tsdata); 1292 1293 if (tsdata->reg_addr.reg_report_rate != NO_REGISTER && 1294 !device_property_read_u32(&client->dev, 1295 "report-rate-hz", &report_rate)) { 1296 if (tsdata->version == EDT_M06) 1297 tsdata->report_rate = clamp_val(report_rate, 30, 140); 1298 else 1299 tsdata->report_rate = clamp_val(report_rate, 1, 255); 1300 1301 if (report_rate != tsdata->report_rate) 1302 dev_warn(&client->dev, 1303 "report-rate %dHz is unsupported, use %dHz\n", 1304 report_rate, tsdata->report_rate); 1305 1306 if (tsdata->version == EDT_M06) 1307 tsdata->report_rate /= 10; 1308 1309 regmap_write(tsdata->regmap, tsdata->reg_addr.reg_report_rate, 1310 tsdata->report_rate); 1311 } 1312 1313 dev_dbg(&client->dev, 1314 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n", 1315 tsdata->name, tsdata->fw_version, tsdata->num_x, tsdata->num_y); 1316 1317 input->name = tsdata->name; 1318 input->id.bustype = BUS_I2C; 1319 input->dev.parent = &client->dev; 1320 1321 input_set_abs_params(input, ABS_MT_POSITION_X, 1322 0, tsdata->num_x * 64 - 1, 0, 0); 1323 input_set_abs_params(input, ABS_MT_POSITION_Y, 1324 0, tsdata->num_y * 64 - 1, 0, 0); 1325 1326 touchscreen_parse_properties(input, true, &tsdata->prop); 1327 1328 error = input_mt_init_slots(input, tsdata->max_support_points, 1329 INPUT_MT_DIRECT); 1330 if (error) { 1331 dev_err(&client->dev, "Unable to init MT slots.\n"); 1332 return error; 1333 } 1334 1335 irq_flags = irq_get_trigger_type(client->irq); 1336 if (irq_flags == IRQF_TRIGGER_NONE) 1337 irq_flags = IRQF_TRIGGER_FALLING; 1338 irq_flags |= IRQF_ONESHOT; 1339 1340 error = devm_request_threaded_irq(&client->dev, client->irq, 1341 NULL, edt_ft5x06_ts_isr, irq_flags, 1342 client->name, tsdata); 1343 if (error) { 1344 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n"); 1345 return error; 1346 } 1347 1348 error = input_register_device(input); 1349 if (error) 1350 return error; 1351 1352 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev)); 1353 1354 dev_dbg(&client->dev, 1355 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n", 1356 client->irq, 1357 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1, 1358 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1); 1359 1360 return 0; 1361 } 1362 1363 static void edt_ft5x06_ts_remove(struct i2c_client *client) 1364 { 1365 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1366 1367 edt_ft5x06_ts_teardown_debugfs(tsdata); 1368 } 1369 1370 static int edt_ft5x06_ts_suspend(struct device *dev) 1371 { 1372 struct i2c_client *client = to_i2c_client(dev); 1373 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1374 struct gpio_desc *reset_gpio = tsdata->reset_gpio; 1375 int ret; 1376 1377 if (device_may_wakeup(dev)) 1378 return 0; 1379 1380 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED) 1381 return 0; 1382 1383 /* Enter hibernate mode. */ 1384 ret = regmap_write(tsdata->regmap, PMOD_REGISTER_OPMODE, 1385 PMOD_REGISTER_HIBERNATE); 1386 if (ret) 1387 dev_warn(dev, "Failed to set hibernate mode\n"); 1388 1389 if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE) 1390 return 0; 1391 1392 /* 1393 * Power-off according the datasheet. Cut the power may leaf the irq 1394 * line in an undefined state depending on the host pull resistor 1395 * settings. Disable the irq to avoid adjusting each host till the 1396 * device is back in a full functional state. 1397 */ 1398 disable_irq(tsdata->client->irq); 1399 1400 gpiod_set_value_cansleep(reset_gpio, 1); 1401 usleep_range(1000, 2000); 1402 1403 ret = regulator_disable(tsdata->vcc); 1404 if (ret) 1405 dev_warn(dev, "Failed to disable vcc\n"); 1406 ret = regulator_disable(tsdata->iovcc); 1407 if (ret) 1408 dev_warn(dev, "Failed to disable iovcc\n"); 1409 1410 return 0; 1411 } 1412 1413 static int edt_ft5x06_ts_resume(struct device *dev) 1414 { 1415 struct i2c_client *client = to_i2c_client(dev); 1416 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client); 1417 int ret = 0; 1418 1419 if (device_may_wakeup(dev)) 1420 return 0; 1421 1422 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED) 1423 return 0; 1424 1425 if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) { 1426 struct gpio_desc *reset_gpio = tsdata->reset_gpio; 1427 1428 /* 1429 * We can't check if the regulator is a dummy or a real 1430 * regulator. So we need to specify the 5ms reset time (T_rst) 1431 * here instead of the 100us T_rtp time. We also need to wait 1432 * 300ms in case it was a real supply and the power was cutted 1433 * of. Toggle the reset pin is also a way to exit the hibernate 1434 * mode. 1435 */ 1436 gpiod_set_value_cansleep(reset_gpio, 1); 1437 usleep_range(5000, 6000); 1438 1439 ret = regulator_enable(tsdata->iovcc); 1440 if (ret) { 1441 dev_err(dev, "Failed to enable iovcc\n"); 1442 return ret; 1443 } 1444 1445 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */ 1446 usleep_range(10, 100); 1447 1448 ret = regulator_enable(tsdata->vcc); 1449 if (ret) { 1450 dev_err(dev, "Failed to enable vcc\n"); 1451 regulator_disable(tsdata->iovcc); 1452 return ret; 1453 } 1454 1455 usleep_range(1000, 2000); 1456 gpiod_set_value_cansleep(reset_gpio, 0); 1457 msleep(300); 1458 1459 edt_ft5x06_restore_reg_parameters(tsdata); 1460 enable_irq(tsdata->client->irq); 1461 1462 if (tsdata->factory_mode) 1463 ret = edt_ft5x06_factory_mode(tsdata); 1464 } else { 1465 struct gpio_desc *wake_gpio = tsdata->wake_gpio; 1466 1467 gpiod_set_value_cansleep(wake_gpio, 0); 1468 usleep_range(5000, 6000); 1469 gpiod_set_value_cansleep(wake_gpio, 1); 1470 } 1471 1472 return ret; 1473 } 1474 1475 static DEFINE_SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops, 1476 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume); 1477 1478 static const struct edt_i2c_chip_data edt_ft5x06_data = { 1479 .max_support_points = 5, 1480 }; 1481 1482 static const struct edt_i2c_chip_data edt_ft5452_data = { 1483 .max_support_points = 5, 1484 }; 1485 1486 static const struct edt_i2c_chip_data edt_ft5506_data = { 1487 .max_support_points = 10, 1488 }; 1489 1490 static const struct edt_i2c_chip_data edt_ft6236_data = { 1491 .max_support_points = 2, 1492 }; 1493 1494 static const struct edt_i2c_chip_data edt_ft8201_data = { 1495 .max_support_points = 10, 1496 }; 1497 1498 static const struct edt_i2c_chip_data edt_ft8719_data = { 1499 .max_support_points = 10, 1500 }; 1501 1502 static const struct i2c_device_id edt_ft5x06_ts_id[] = { 1503 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data }, 1504 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data }, 1505 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data }, 1506 { .name = "ft5452", .driver_data = (long)&edt_ft5452_data }, 1507 /* Note no edt- prefix for compatibility with the ft6236.c driver */ 1508 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data }, 1509 { .name = "ft8201", .driver_data = (long)&edt_ft8201_data }, 1510 { .name = "ft8719", .driver_data = (long)&edt_ft8719_data }, 1511 { /* sentinel */ } 1512 }; 1513 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id); 1514 1515 static const struct of_device_id edt_ft5x06_of_match[] = { 1516 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data }, 1517 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data }, 1518 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data }, 1519 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data }, 1520 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data }, 1521 { .compatible = "focaltech,ft5426", .data = &edt_ft5506_data }, 1522 { .compatible = "focaltech,ft5452", .data = &edt_ft5452_data }, 1523 /* Note focaltech vendor prefix for compatibility with ft6236.c */ 1524 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data }, 1525 { .compatible = "focaltech,ft8201", .data = &edt_ft8201_data }, 1526 { .compatible = "focaltech,ft8719", .data = &edt_ft8719_data }, 1527 { /* sentinel */ } 1528 }; 1529 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match); 1530 1531 static struct i2c_driver edt_ft5x06_ts_driver = { 1532 .driver = { 1533 .name = "edt_ft5x06", 1534 .dev_groups = edt_ft5x06_groups, 1535 .of_match_table = edt_ft5x06_of_match, 1536 .pm = pm_sleep_ptr(&edt_ft5x06_ts_pm_ops), 1537 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1538 }, 1539 .id_table = edt_ft5x06_ts_id, 1540 .probe = edt_ft5x06_ts_probe, 1541 .remove = edt_ft5x06_ts_remove, 1542 }; 1543 1544 module_i2c_driver(edt_ft5x06_ts_driver); 1545 1546 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>"); 1547 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver"); 1548 MODULE_LICENSE("GPL v2"); 1549