1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * TSC2004/TSC2005 touchscreen driver core 4 * 5 * Copyright (C) 2006-2010 Nokia Corporation 6 * Copyright (C) 2015 QWERTY Embedded Design 7 * Copyright (C) 2015 EMAC Inc. 8 * 9 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com> 10 * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com> 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/input.h> 16 #include <linux/input/touchscreen.h> 17 #include <linux/interrupt.h> 18 #include <linux/delay.h> 19 #include <linux/pm.h> 20 #include <linux/of.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/regmap.h> 23 #include <linux/gpio/consumer.h> 24 #include "tsc200x-core.h" 25 26 /* 27 * The touchscreen interface operates as follows: 28 * 29 * 1) Pen is pressed against the touchscreen. 30 * 2) TSC200X performs AD conversion. 31 * 3) After the conversion is done TSC200X drives DAV line down. 32 * 4) GPIO IRQ is received and tsc200x_irq_thread() is scheduled. 33 * 5) tsc200x_irq_thread() queues up a transfer to fetch the x, y, z1, z2 34 * values. 35 * 6) tsc200x_irq_thread() reports coordinates to input layer and sets up 36 * tsc200x_penup_timer() to be called after TSC200X_PENUP_TIME_MS (40ms). 37 * 7) When the penup timer expires, there have not been touch or DAV interrupts 38 * during the last 40ms which means the pen has been lifted. 39 * 40 * ESD recovery via a hardware reset is done if the TSC200X doesn't respond 41 * after a configurable period (in ms) of activity. If esd_timeout is 0, the 42 * watchdog is disabled. 43 */ 44 45 static const struct regmap_range tsc200x_writable_ranges[] = { 46 regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2), 47 }; 48 49 static const struct regmap_access_table tsc200x_writable_table = { 50 .yes_ranges = tsc200x_writable_ranges, 51 .n_yes_ranges = ARRAY_SIZE(tsc200x_writable_ranges), 52 }; 53 54 const struct regmap_config tsc200x_regmap_config = { 55 .reg_bits = 8, 56 .val_bits = 16, 57 .reg_stride = 0x08, 58 .max_register = 0x78, 59 .read_flag_mask = TSC200X_REG_READ, 60 .write_flag_mask = TSC200X_REG_PND0, 61 .wr_table = &tsc200x_writable_table, 62 .use_single_read = true, 63 .use_single_write = true, 64 }; 65 EXPORT_SYMBOL_GPL(tsc200x_regmap_config); 66 67 struct tsc200x_data { 68 u16 x; 69 u16 y; 70 u16 z1; 71 u16 z2; 72 } __packed; 73 #define TSC200X_DATA_REGS 4 74 75 struct tsc200x { 76 struct device *dev; 77 struct regmap *regmap; 78 __u16 bustype; 79 80 struct input_dev *idev; 81 char phys[32]; 82 83 struct mutex mutex; 84 85 /* raw copy of previous x,y,z */ 86 int in_x; 87 int in_y; 88 int in_z1; 89 int in_z2; 90 91 struct touchscreen_properties prop; 92 93 spinlock_t lock; 94 struct timer_list penup_timer; 95 96 unsigned int esd_timeout; 97 struct delayed_work esd_work; 98 unsigned long last_valid_interrupt; 99 100 unsigned int x_plate_ohm; 101 102 bool opened; 103 bool suspended; 104 105 bool pen_down; 106 107 struct gpio_desc *reset_gpio; 108 int (*tsc200x_cmd)(struct device *dev, u8 cmd); 109 110 int irq; 111 bool wake_irq_enabled; 112 }; 113 114 static void tsc200x_update_pen_state(struct tsc200x *ts, 115 int x, int y, int pressure) 116 { 117 if (pressure) { 118 touchscreen_report_pos(ts->idev, &ts->prop, x, y, false); 119 input_report_abs(ts->idev, ABS_PRESSURE, pressure); 120 if (!ts->pen_down) { 121 input_report_key(ts->idev, BTN_TOUCH, !!pressure); 122 ts->pen_down = true; 123 } 124 } else { 125 input_report_abs(ts->idev, ABS_PRESSURE, 0); 126 if (ts->pen_down) { 127 input_report_key(ts->idev, BTN_TOUCH, 0); 128 ts->pen_down = false; 129 } 130 } 131 input_sync(ts->idev); 132 dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y, 133 pressure); 134 } 135 136 static irqreturn_t tsc200x_irq_thread(int irq, void *_ts) 137 { 138 struct tsc200x *ts = _ts; 139 unsigned long flags; 140 unsigned int pressure; 141 struct tsc200x_data tsdata; 142 int error; 143 144 /* read the coordinates */ 145 error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata, 146 TSC200X_DATA_REGS); 147 if (unlikely(error)) 148 goto out; 149 150 /* validate position */ 151 if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT)) 152 goto out; 153 154 /* Skip reading if the pressure components are out of range */ 155 if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT)) 156 goto out; 157 if (unlikely(tsdata.z1 >= tsdata.z2)) 158 goto out; 159 160 /* 161 * Skip point if this is a pen down with the exact same values as 162 * the value before pen-up - that implies SPI fed us stale data 163 */ 164 if (!ts->pen_down && 165 ts->in_x == tsdata.x && ts->in_y == tsdata.y && 166 ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) { 167 goto out; 168 } 169 170 /* 171 * At this point we are happy we have a valid and useful reading. 172 * Remember it for later comparisons. We may now begin downsampling. 173 */ 174 ts->in_x = tsdata.x; 175 ts->in_y = tsdata.y; 176 ts->in_z1 = tsdata.z1; 177 ts->in_z2 = tsdata.z2; 178 179 /* Compute touch pressure resistance using equation #1 */ 180 pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1; 181 pressure = pressure * ts->x_plate_ohm / 4096; 182 if (unlikely(pressure > MAX_12BIT)) 183 goto out; 184 185 spin_lock_irqsave(&ts->lock, flags); 186 187 tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure); 188 mod_timer(&ts->penup_timer, 189 jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS)); 190 191 spin_unlock_irqrestore(&ts->lock, flags); 192 193 ts->last_valid_interrupt = jiffies; 194 out: 195 return IRQ_HANDLED; 196 } 197 198 static void tsc200x_penup_timer(struct timer_list *t) 199 { 200 struct tsc200x *ts = from_timer(ts, t, penup_timer); 201 unsigned long flags; 202 203 spin_lock_irqsave(&ts->lock, flags); 204 tsc200x_update_pen_state(ts, 0, 0, 0); 205 spin_unlock_irqrestore(&ts->lock, flags); 206 } 207 208 static void tsc200x_start_scan(struct tsc200x *ts) 209 { 210 regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE); 211 regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE); 212 regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE); 213 ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL); 214 } 215 216 static void tsc200x_stop_scan(struct tsc200x *ts) 217 { 218 ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP); 219 } 220 221 static void tsc200x_reset(struct tsc200x *ts) 222 { 223 if (ts->reset_gpio) { 224 gpiod_set_value_cansleep(ts->reset_gpio, 1); 225 usleep_range(100, 500); /* only 10us required */ 226 gpiod_set_value_cansleep(ts->reset_gpio, 0); 227 } 228 } 229 230 /* must be called with ts->mutex held */ 231 static void __tsc200x_disable(struct tsc200x *ts) 232 { 233 tsc200x_stop_scan(ts); 234 235 disable_irq(ts->irq); 236 del_timer_sync(&ts->penup_timer); 237 238 cancel_delayed_work_sync(&ts->esd_work); 239 240 enable_irq(ts->irq); 241 } 242 243 /* must be called with ts->mutex held */ 244 static void __tsc200x_enable(struct tsc200x *ts) 245 { 246 tsc200x_start_scan(ts); 247 248 if (ts->esd_timeout && ts->reset_gpio) { 249 ts->last_valid_interrupt = jiffies; 250 schedule_delayed_work(&ts->esd_work, 251 round_jiffies_relative( 252 msecs_to_jiffies(ts->esd_timeout))); 253 } 254 } 255 256 static ssize_t tsc200x_selftest_show(struct device *dev, 257 struct device_attribute *attr, 258 char *buf) 259 { 260 struct tsc200x *ts = dev_get_drvdata(dev); 261 unsigned int temp_high; 262 unsigned int temp_high_orig; 263 unsigned int temp_high_test; 264 bool success = true; 265 int error; 266 267 mutex_lock(&ts->mutex); 268 269 /* 270 * Test TSC200X communications via temp high register. 271 */ 272 __tsc200x_disable(ts); 273 274 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig); 275 if (error) { 276 dev_warn(dev, "selftest failed: read error %d\n", error); 277 success = false; 278 goto out; 279 } 280 281 temp_high_test = (temp_high_orig - 1) & MAX_12BIT; 282 283 error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test); 284 if (error) { 285 dev_warn(dev, "selftest failed: write error %d\n", error); 286 success = false; 287 goto out; 288 } 289 290 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high); 291 if (error) { 292 dev_warn(dev, "selftest failed: read error %d after write\n", 293 error); 294 success = false; 295 goto out; 296 } 297 298 if (temp_high != temp_high_test) { 299 dev_warn(dev, "selftest failed: %d != %d\n", 300 temp_high, temp_high_test); 301 success = false; 302 } 303 304 /* hardware reset */ 305 tsc200x_reset(ts); 306 307 if (!success) 308 goto out; 309 310 /* test that the reset really happened */ 311 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high); 312 if (error) { 313 dev_warn(dev, "selftest failed: read error %d after reset\n", 314 error); 315 success = false; 316 goto out; 317 } 318 319 if (temp_high != temp_high_orig) { 320 dev_warn(dev, "selftest failed after reset: %d != %d\n", 321 temp_high, temp_high_orig); 322 success = false; 323 } 324 325 out: 326 __tsc200x_enable(ts); 327 mutex_unlock(&ts->mutex); 328 329 return sprintf(buf, "%d\n", success); 330 } 331 332 static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL); 333 334 static struct attribute *tsc200x_attrs[] = { 335 &dev_attr_selftest.attr, 336 NULL 337 }; 338 339 static umode_t tsc200x_attr_is_visible(struct kobject *kobj, 340 struct attribute *attr, int n) 341 { 342 struct device *dev = kobj_to_dev(kobj); 343 struct tsc200x *ts = dev_get_drvdata(dev); 344 umode_t mode = attr->mode; 345 346 if (attr == &dev_attr_selftest.attr) { 347 if (!ts->reset_gpio) 348 mode = 0; 349 } 350 351 return mode; 352 } 353 354 static const struct attribute_group tsc200x_attr_group = { 355 .is_visible = tsc200x_attr_is_visible, 356 .attrs = tsc200x_attrs, 357 }; 358 359 const struct attribute_group *tsc200x_groups[] = { 360 &tsc200x_attr_group, 361 NULL 362 }; 363 EXPORT_SYMBOL_GPL(tsc200x_groups); 364 365 static void tsc200x_esd_work(struct work_struct *work) 366 { 367 struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work); 368 int error; 369 unsigned int r; 370 371 if (!mutex_trylock(&ts->mutex)) { 372 /* 373 * If the mutex is taken, it means that disable or enable is in 374 * progress. In that case just reschedule the work. If the work 375 * is not needed, it will be canceled by disable. 376 */ 377 goto reschedule; 378 } 379 380 if (time_is_after_jiffies(ts->last_valid_interrupt + 381 msecs_to_jiffies(ts->esd_timeout))) 382 goto out; 383 384 /* We should be able to read register without disabling interrupts. */ 385 error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r); 386 if (!error && 387 !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) { 388 goto out; 389 } 390 391 /* 392 * If we could not read our known value from configuration register 0 393 * then we should reset the controller as if from power-up and start 394 * scanning again. 395 */ 396 dev_info(ts->dev, "TSC200X not responding - resetting\n"); 397 398 disable_irq(ts->irq); 399 del_timer_sync(&ts->penup_timer); 400 401 tsc200x_update_pen_state(ts, 0, 0, 0); 402 403 tsc200x_reset(ts); 404 405 enable_irq(ts->irq); 406 tsc200x_start_scan(ts); 407 408 out: 409 mutex_unlock(&ts->mutex); 410 reschedule: 411 /* re-arm the watchdog */ 412 schedule_delayed_work(&ts->esd_work, 413 round_jiffies_relative( 414 msecs_to_jiffies(ts->esd_timeout))); 415 } 416 417 static int tsc200x_open(struct input_dev *input) 418 { 419 struct tsc200x *ts = input_get_drvdata(input); 420 421 mutex_lock(&ts->mutex); 422 423 if (!ts->suspended) 424 __tsc200x_enable(ts); 425 426 ts->opened = true; 427 428 mutex_unlock(&ts->mutex); 429 430 return 0; 431 } 432 433 static void tsc200x_close(struct input_dev *input) 434 { 435 struct tsc200x *ts = input_get_drvdata(input); 436 437 mutex_lock(&ts->mutex); 438 439 if (!ts->suspended) 440 __tsc200x_disable(ts); 441 442 ts->opened = false; 443 444 mutex_unlock(&ts->mutex); 445 } 446 447 int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id, 448 struct regmap *regmap, 449 int (*tsc200x_cmd)(struct device *dev, u8 cmd)) 450 { 451 struct tsc200x *ts; 452 struct input_dev *input_dev; 453 u32 x_plate_ohm; 454 u32 esd_timeout; 455 int error; 456 457 if (irq <= 0) { 458 dev_err(dev, "no irq\n"); 459 return -ENODEV; 460 } 461 462 if (IS_ERR(regmap)) 463 return PTR_ERR(regmap); 464 465 if (!tsc200x_cmd) { 466 dev_err(dev, "no cmd function\n"); 467 return -ENODEV; 468 } 469 470 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 471 if (!ts) 472 return -ENOMEM; 473 474 input_dev = devm_input_allocate_device(dev); 475 if (!input_dev) 476 return -ENOMEM; 477 478 ts->irq = irq; 479 ts->dev = dev; 480 ts->idev = input_dev; 481 ts->regmap = regmap; 482 ts->tsc200x_cmd = tsc200x_cmd; 483 484 error = device_property_read_u32(dev, "ti,x-plate-ohms", &x_plate_ohm); 485 ts->x_plate_ohm = error ? TSC200X_DEF_RESISTOR : x_plate_ohm; 486 487 error = device_property_read_u32(dev, "ti,esd-recovery-timeout-ms", 488 &esd_timeout); 489 ts->esd_timeout = error ? 0 : esd_timeout; 490 491 mutex_init(&ts->mutex); 492 493 spin_lock_init(&ts->lock); 494 timer_setup(&ts->penup_timer, tsc200x_penup_timer, 0); 495 496 INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work); 497 498 snprintf(ts->phys, sizeof(ts->phys), 499 "%s/input-ts", dev_name(dev)); 500 501 if (tsc_id->product == 2004) { 502 input_dev->name = "TSC200X touchscreen"; 503 } else { 504 input_dev->name = devm_kasprintf(dev, GFP_KERNEL, 505 "TSC%04d touchscreen", 506 tsc_id->product); 507 if (!input_dev->name) 508 return -ENOMEM; 509 } 510 511 input_dev->phys = ts->phys; 512 input_dev->id = *tsc_id; 513 514 input_dev->open = tsc200x_open; 515 input_dev->close = tsc200x_close; 516 517 input_set_drvdata(input_dev, ts); 518 519 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit); 520 input_set_capability(input_dev, EV_KEY, BTN_TOUCH); 521 522 input_set_abs_params(input_dev, ABS_X, 523 0, MAX_12BIT, TSC200X_DEF_X_FUZZ, 0); 524 input_set_abs_params(input_dev, ABS_Y, 525 0, MAX_12BIT, TSC200X_DEF_Y_FUZZ, 0); 526 input_set_abs_params(input_dev, ABS_PRESSURE, 527 0, MAX_12BIT, TSC200X_DEF_P_FUZZ, 0); 528 529 touchscreen_parse_properties(input_dev, false, &ts->prop); 530 531 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 532 error = PTR_ERR_OR_ZERO(ts->reset_gpio); 533 if (error) { 534 dev_err(dev, "error acquiring reset gpio: %d\n", error); 535 return error; 536 } 537 538 error = devm_regulator_get_enable(dev, "vio"); 539 if (error) { 540 dev_err(dev, "error acquiring vio regulator: %d\n", error); 541 return error; 542 } 543 544 tsc200x_reset(ts); 545 546 /* Ensure the touchscreen is off */ 547 tsc200x_stop_scan(ts); 548 549 error = devm_request_threaded_irq(dev, irq, NULL, tsc200x_irq_thread, 550 IRQF_ONESHOT, "tsc200x", ts); 551 if (error) { 552 dev_err(dev, "Failed to request irq, err: %d\n", error); 553 return error; 554 } 555 556 dev_set_drvdata(dev, ts); 557 558 error = input_register_device(ts->idev); 559 if (error) { 560 dev_err(dev, 561 "Failed to register input device, err: %d\n", error); 562 return error; 563 } 564 565 device_init_wakeup(dev, true); 566 567 return 0; 568 } 569 EXPORT_SYMBOL_GPL(tsc200x_probe); 570 571 static int tsc200x_suspend(struct device *dev) 572 { 573 struct tsc200x *ts = dev_get_drvdata(dev); 574 575 mutex_lock(&ts->mutex); 576 577 if (!ts->suspended && ts->opened) 578 __tsc200x_disable(ts); 579 580 ts->suspended = true; 581 582 if (device_may_wakeup(dev)) 583 ts->wake_irq_enabled = enable_irq_wake(ts->irq) == 0; 584 585 mutex_unlock(&ts->mutex); 586 587 return 0; 588 } 589 590 static int tsc200x_resume(struct device *dev) 591 { 592 struct tsc200x *ts = dev_get_drvdata(dev); 593 594 mutex_lock(&ts->mutex); 595 596 if (ts->wake_irq_enabled) { 597 disable_irq_wake(ts->irq); 598 ts->wake_irq_enabled = false; 599 } 600 601 if (ts->suspended && ts->opened) 602 __tsc200x_enable(ts); 603 604 ts->suspended = false; 605 606 mutex_unlock(&ts->mutex); 607 608 return 0; 609 } 610 611 EXPORT_GPL_SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume); 612 613 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>"); 614 MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core"); 615 MODULE_LICENSE("GPL"); 616