1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TSC2004/TSC2005 touchscreen driver core
5 * Copyright (C) 2006-2010 Nokia Corporation
6 * Copyright (C) 2015 QWERTY Embedded Design
7 * Copyright (C) 2015 EMAC Inc.
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>
21 #include <linux/regulator/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/gpio/consumer.h>
24 #include "tsc200x-core.h"
27 * The touchscreen interface operates as follows:
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
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.
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.
45 static const struct regmap_range tsc200x_writable_ranges[] = {
46 regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2),
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),
54 const struct regmap_config tsc200x_regmap_config = {
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,
65 EXPORT_SYMBOL_GPL(tsc200x_regmap_config);
73 #define TSC200X_DATA_REGS 4
77 struct regmap *regmap;
80 struct input_dev *idev;
85 /* raw copy of previous x,y,z */
91 struct touchscreen_properties prop;
94 struct timer_list penup_timer;
96 unsigned int esd_timeout;
97 struct delayed_work esd_work;
98 unsigned long last_valid_interrupt;
100 unsigned int x_plate_ohm;
107 struct regulator *vio;
109 struct gpio_desc *reset_gpio;
110 int (*tsc200x_cmd)(struct device *dev, u8 cmd);
114 static void tsc200x_update_pen_state(struct tsc200x *ts,
115 int x, int y, int pressure)
118 touchscreen_report_pos(ts->idev, &ts->prop, x, y, false);
119 input_report_abs(ts->idev, ABS_PRESSURE, pressure);
121 input_report_key(ts->idev, BTN_TOUCH, !!pressure);
125 input_report_abs(ts->idev, ABS_PRESSURE, 0);
127 input_report_key(ts->idev, BTN_TOUCH, 0);
128 ts->pen_down = false;
131 input_sync(ts->idev);
132 dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
136 static irqreturn_t tsc200x_irq_thread(int irq, void *_ts)
138 struct tsc200x *ts = _ts;
140 unsigned int pressure;
141 struct tsc200x_data tsdata;
144 /* read the coordinates */
145 error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata,
150 /* validate position */
151 if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))
154 /* Skip reading if the pressure components are out of range */
155 if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))
157 if (unlikely(tsdata.z1 >= tsdata.z2))
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
165 ts->in_x == tsdata.x && ts->in_y == tsdata.y &&
166 ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {
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.
176 ts->in_z1 = tsdata.z1;
177 ts->in_z2 = tsdata.z2;
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))
185 spin_lock_irqsave(&ts->lock, flags);
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));
191 spin_unlock_irqrestore(&ts->lock, flags);
193 ts->last_valid_interrupt = jiffies;
198 static void tsc200x_penup_timer(struct timer_list *t)
200 struct tsc200x *ts = from_timer(ts, t, penup_timer);
203 spin_lock_irqsave(&ts->lock, flags);
204 tsc200x_update_pen_state(ts, 0, 0, 0);
205 spin_unlock_irqrestore(&ts->lock, flags);
208 static void tsc200x_start_scan(struct tsc200x *ts)
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);
216 static void tsc200x_stop_scan(struct tsc200x *ts)
218 ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP);
221 static void tsc200x_reset(struct tsc200x *ts)
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);
230 /* must be called with ts->mutex held */
231 static void __tsc200x_disable(struct tsc200x *ts)
233 tsc200x_stop_scan(ts);
235 disable_irq(ts->irq);
236 del_timer_sync(&ts->penup_timer);
238 cancel_delayed_work_sync(&ts->esd_work);
243 /* must be called with ts->mutex held */
244 static void __tsc200x_enable(struct tsc200x *ts)
246 tsc200x_start_scan(ts);
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)));
256 static ssize_t tsc200x_selftest_show(struct device *dev,
257 struct device_attribute *attr,
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;
267 mutex_lock(&ts->mutex);
270 * Test TSC200X communications via temp high register.
272 __tsc200x_disable(ts);
274 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig);
276 dev_warn(dev, "selftest failed: read error %d\n", error);
281 temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
283 error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test);
285 dev_warn(dev, "selftest failed: write error %d\n", error);
290 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
292 dev_warn(dev, "selftest failed: read error %d after write\n",
298 if (temp_high != temp_high_test) {
299 dev_warn(dev, "selftest failed: %d != %d\n",
300 temp_high, temp_high_test);
310 /* test that the reset really happened */
311 error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
313 dev_warn(dev, "selftest failed: read error %d after reset\n",
319 if (temp_high != temp_high_orig) {
320 dev_warn(dev, "selftest failed after reset: %d != %d\n",
321 temp_high, temp_high_orig);
326 __tsc200x_enable(ts);
327 mutex_unlock(&ts->mutex);
329 return sprintf(buf, "%d\n", success);
332 static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL);
334 static struct attribute *tsc200x_attrs[] = {
335 &dev_attr_selftest.attr,
339 static umode_t tsc200x_attr_is_visible(struct kobject *kobj,
340 struct attribute *attr, int n)
342 struct device *dev = kobj_to_dev(kobj);
343 struct tsc200x *ts = dev_get_drvdata(dev);
344 umode_t mode = attr->mode;
346 if (attr == &dev_attr_selftest.attr) {
354 static const struct attribute_group tsc200x_attr_group = {
355 .is_visible = tsc200x_attr_is_visible,
356 .attrs = tsc200x_attrs,
359 static void tsc200x_esd_work(struct work_struct *work)
361 struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work);
365 if (!mutex_trylock(&ts->mutex)) {
367 * If the mutex is taken, it means that disable or enable is in
368 * progress. In that case just reschedule the work. If the work
369 * is not needed, it will be canceled by disable.
374 if (time_is_after_jiffies(ts->last_valid_interrupt +
375 msecs_to_jiffies(ts->esd_timeout)))
378 /* We should be able to read register without disabling interrupts. */
379 error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r);
381 !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) {
386 * If we could not read our known value from configuration register 0
387 * then we should reset the controller as if from power-up and start
390 dev_info(ts->dev, "TSC200X not responding - resetting\n");
392 disable_irq(ts->irq);
393 del_timer_sync(&ts->penup_timer);
395 tsc200x_update_pen_state(ts, 0, 0, 0);
400 tsc200x_start_scan(ts);
403 mutex_unlock(&ts->mutex);
405 /* re-arm the watchdog */
406 schedule_delayed_work(&ts->esd_work,
407 round_jiffies_relative(
408 msecs_to_jiffies(ts->esd_timeout)));
411 static int tsc200x_open(struct input_dev *input)
413 struct tsc200x *ts = input_get_drvdata(input);
415 mutex_lock(&ts->mutex);
418 __tsc200x_enable(ts);
422 mutex_unlock(&ts->mutex);
427 static void tsc200x_close(struct input_dev *input)
429 struct tsc200x *ts = input_get_drvdata(input);
431 mutex_lock(&ts->mutex);
434 __tsc200x_disable(ts);
438 mutex_unlock(&ts->mutex);
441 int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id,
442 struct regmap *regmap,
443 int (*tsc200x_cmd)(struct device *dev, u8 cmd))
446 struct input_dev *input_dev;
452 dev_err(dev, "no irq\n");
457 return PTR_ERR(regmap);
460 dev_err(dev, "no cmd function\n");
464 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
468 input_dev = devm_input_allocate_device(dev);
474 ts->idev = input_dev;
476 ts->tsc200x_cmd = tsc200x_cmd;
478 error = device_property_read_u32(dev, "ti,x-plate-ohms", &x_plate_ohm);
479 ts->x_plate_ohm = error ? TSC200X_DEF_RESISTOR : x_plate_ohm;
481 error = device_property_read_u32(dev, "ti,esd-recovery-timeout-ms",
483 ts->esd_timeout = error ? 0 : esd_timeout;
485 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
486 if (IS_ERR(ts->reset_gpio)) {
487 error = PTR_ERR(ts->reset_gpio);
488 dev_err(dev, "error acquiring reset gpio: %d\n", error);
492 ts->vio = devm_regulator_get(dev, "vio");
493 if (IS_ERR(ts->vio)) {
494 error = PTR_ERR(ts->vio);
495 dev_err(dev, "error acquiring vio regulator: %d", error);
499 mutex_init(&ts->mutex);
501 spin_lock_init(&ts->lock);
502 timer_setup(&ts->penup_timer, tsc200x_penup_timer, 0);
504 INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work);
506 snprintf(ts->phys, sizeof(ts->phys),
507 "%s/input-ts", dev_name(dev));
509 if (tsc_id->product == 2004) {
510 input_dev->name = "TSC200X touchscreen";
512 input_dev->name = devm_kasprintf(dev, GFP_KERNEL,
513 "TSC%04d touchscreen",
515 if (!input_dev->name)
519 input_dev->phys = ts->phys;
520 input_dev->id = *tsc_id;
522 input_dev->open = tsc200x_open;
523 input_dev->close = tsc200x_close;
525 input_set_drvdata(input_dev, ts);
527 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
528 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
530 input_set_abs_params(input_dev, ABS_X,
531 0, MAX_12BIT, TSC200X_DEF_X_FUZZ, 0);
532 input_set_abs_params(input_dev, ABS_Y,
533 0, MAX_12BIT, TSC200X_DEF_Y_FUZZ, 0);
534 input_set_abs_params(input_dev, ABS_PRESSURE,
535 0, MAX_12BIT, TSC200X_DEF_P_FUZZ, 0);
537 touchscreen_parse_properties(input_dev, false, &ts->prop);
539 /* Ensure the touchscreen is off */
540 tsc200x_stop_scan(ts);
542 error = devm_request_threaded_irq(dev, irq, NULL,
544 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
547 dev_err(dev, "Failed to request irq, err: %d\n", error);
551 error = regulator_enable(ts->vio);
555 dev_set_drvdata(dev, ts);
556 error = sysfs_create_group(&dev->kobj, &tsc200x_attr_group);
559 "Failed to create sysfs attributes, err: %d\n", error);
560 goto disable_regulator;
563 error = input_register_device(ts->idev);
566 "Failed to register input device, err: %d\n", error);
567 goto err_remove_sysfs;
570 irq_set_irq_wake(irq, 1);
574 sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
576 regulator_disable(ts->vio);
579 EXPORT_SYMBOL_GPL(tsc200x_probe);
581 void tsc200x_remove(struct device *dev)
583 struct tsc200x *ts = dev_get_drvdata(dev);
585 sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);
587 regulator_disable(ts->vio);
589 EXPORT_SYMBOL_GPL(tsc200x_remove);
591 static int __maybe_unused tsc200x_suspend(struct device *dev)
593 struct tsc200x *ts = dev_get_drvdata(dev);
595 mutex_lock(&ts->mutex);
597 if (!ts->suspended && ts->opened)
598 __tsc200x_disable(ts);
600 ts->suspended = true;
602 mutex_unlock(&ts->mutex);
607 static int __maybe_unused tsc200x_resume(struct device *dev)
609 struct tsc200x *ts = dev_get_drvdata(dev);
611 mutex_lock(&ts->mutex);
613 if (ts->suspended && ts->opened)
614 __tsc200x_enable(ts);
616 ts->suspended = false;
618 mutex_unlock(&ts->mutex);
623 SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume);
624 EXPORT_SYMBOL_GPL(tsc200x_pm_ops);
627 MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");
628 MODULE_LICENSE("GPL");