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.
9 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
10 * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>
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 */
92 struct timer_list penup_timer
;
94 unsigned int esd_timeout
;
95 struct delayed_work esd_work
;
96 unsigned long last_valid_interrupt
;
98 unsigned int x_plate_ohm
;
105 struct regulator
*vio
;
107 struct gpio_desc
*reset_gpio
;
108 int (*tsc200x_cmd
)(struct device
*dev
, u8 cmd
);
112 static void tsc200x_update_pen_state(struct tsc200x
*ts
,
113 int x
, int y
, int pressure
)
116 input_report_abs(ts
->idev
, ABS_X
, x
);
117 input_report_abs(ts
->idev
, ABS_Y
, y
);
118 input_report_abs(ts
->idev
, ABS_PRESSURE
, pressure
);
120 input_report_key(ts
->idev
, BTN_TOUCH
, !!pressure
);
124 input_report_abs(ts
->idev
, ABS_PRESSURE
, 0);
126 input_report_key(ts
->idev
, BTN_TOUCH
, 0);
127 ts
->pen_down
= false;
130 input_sync(ts
->idev
);
131 dev_dbg(ts
->dev
, "point(%4d,%4d), pressure (%4d)\n", x
, y
,
135 static irqreturn_t
tsc200x_irq_thread(int irq
, void *_ts
)
137 struct tsc200x
*ts
= _ts
;
139 unsigned int pressure
;
140 struct tsc200x_data tsdata
;
143 /* read the coordinates */
144 error
= regmap_bulk_read(ts
->regmap
, TSC200X_REG_X
, &tsdata
,
149 /* validate position */
150 if (unlikely(tsdata
.x
> MAX_12BIT
|| tsdata
.y
> MAX_12BIT
))
153 /* Skip reading if the pressure components are out of range */
154 if (unlikely(tsdata
.z1
== 0 || tsdata
.z2
> MAX_12BIT
))
156 if (unlikely(tsdata
.z1
>= tsdata
.z2
))
160 * Skip point if this is a pen down with the exact same values as
161 * the value before pen-up - that implies SPI fed us stale data
164 ts
->in_x
== tsdata
.x
&& ts
->in_y
== tsdata
.y
&&
165 ts
->in_z1
== tsdata
.z1
&& ts
->in_z2
== tsdata
.z2
) {
170 * At this point we are happy we have a valid and useful reading.
171 * Remember it for later comparisons. We may now begin downsampling.
175 ts
->in_z1
= tsdata
.z1
;
176 ts
->in_z2
= tsdata
.z2
;
178 /* Compute touch pressure resistance using equation #1 */
179 pressure
= tsdata
.x
* (tsdata
.z2
- tsdata
.z1
) / tsdata
.z1
;
180 pressure
= pressure
* ts
->x_plate_ohm
/ 4096;
181 if (unlikely(pressure
> MAX_12BIT
))
184 spin_lock_irqsave(&ts
->lock
, flags
);
186 tsc200x_update_pen_state(ts
, tsdata
.x
, tsdata
.y
, pressure
);
187 mod_timer(&ts
->penup_timer
,
188 jiffies
+ msecs_to_jiffies(TSC200X_PENUP_TIME_MS
));
190 spin_unlock_irqrestore(&ts
->lock
, flags
);
192 ts
->last_valid_interrupt
= jiffies
;
197 static void tsc200x_penup_timer(struct timer_list
*t
)
199 struct tsc200x
*ts
= from_timer(ts
, t
, penup_timer
);
202 spin_lock_irqsave(&ts
->lock
, flags
);
203 tsc200x_update_pen_state(ts
, 0, 0, 0);
204 spin_unlock_irqrestore(&ts
->lock
, flags
);
207 static void tsc200x_start_scan(struct tsc200x
*ts
)
209 regmap_write(ts
->regmap
, TSC200X_REG_CFR0
, TSC200X_CFR0_INITVALUE
);
210 regmap_write(ts
->regmap
, TSC200X_REG_CFR1
, TSC200X_CFR1_INITVALUE
);
211 regmap_write(ts
->regmap
, TSC200X_REG_CFR2
, TSC200X_CFR2_INITVALUE
);
212 ts
->tsc200x_cmd(ts
->dev
, TSC200X_CMD_NORMAL
);
215 static void tsc200x_stop_scan(struct tsc200x
*ts
)
217 ts
->tsc200x_cmd(ts
->dev
, TSC200X_CMD_STOP
);
220 static void tsc200x_reset(struct tsc200x
*ts
)
222 if (ts
->reset_gpio
) {
223 gpiod_set_value_cansleep(ts
->reset_gpio
, 1);
224 usleep_range(100, 500); /* only 10us required */
225 gpiod_set_value_cansleep(ts
->reset_gpio
, 0);
229 /* must be called with ts->mutex held */
230 static void __tsc200x_disable(struct tsc200x
*ts
)
232 tsc200x_stop_scan(ts
);
234 disable_irq(ts
->irq
);
235 del_timer_sync(&ts
->penup_timer
);
237 cancel_delayed_work_sync(&ts
->esd_work
);
242 /* must be called with ts->mutex held */
243 static void __tsc200x_enable(struct tsc200x
*ts
)
245 tsc200x_start_scan(ts
);
247 if (ts
->esd_timeout
&& ts
->reset_gpio
) {
248 ts
->last_valid_interrupt
= jiffies
;
249 schedule_delayed_work(&ts
->esd_work
,
250 round_jiffies_relative(
251 msecs_to_jiffies(ts
->esd_timeout
)));
255 static ssize_t
tsc200x_selftest_show(struct device
*dev
,
256 struct device_attribute
*attr
,
259 struct tsc200x
*ts
= dev_get_drvdata(dev
);
260 unsigned int temp_high
;
261 unsigned int temp_high_orig
;
262 unsigned int temp_high_test
;
266 mutex_lock(&ts
->mutex
);
269 * Test TSC200X communications via temp high register.
271 __tsc200x_disable(ts
);
273 error
= regmap_read(ts
->regmap
, TSC200X_REG_TEMP_HIGH
, &temp_high_orig
);
275 dev_warn(dev
, "selftest failed: read error %d\n", error
);
280 temp_high_test
= (temp_high_orig
- 1) & MAX_12BIT
;
282 error
= regmap_write(ts
->regmap
, TSC200X_REG_TEMP_HIGH
, temp_high_test
);
284 dev_warn(dev
, "selftest failed: write error %d\n", error
);
289 error
= regmap_read(ts
->regmap
, TSC200X_REG_TEMP_HIGH
, &temp_high
);
291 dev_warn(dev
, "selftest failed: read error %d after write\n",
297 if (temp_high
!= temp_high_test
) {
298 dev_warn(dev
, "selftest failed: %d != %d\n",
299 temp_high
, temp_high_test
);
309 /* test that the reset really happened */
310 error
= regmap_read(ts
->regmap
, TSC200X_REG_TEMP_HIGH
, &temp_high
);
312 dev_warn(dev
, "selftest failed: read error %d after reset\n",
318 if (temp_high
!= temp_high_orig
) {
319 dev_warn(dev
, "selftest failed after reset: %d != %d\n",
320 temp_high
, temp_high_orig
);
325 __tsc200x_enable(ts
);
326 mutex_unlock(&ts
->mutex
);
328 return sprintf(buf
, "%d\n", success
);
331 static DEVICE_ATTR(selftest
, S_IRUGO
, tsc200x_selftest_show
, NULL
);
333 static struct attribute
*tsc200x_attrs
[] = {
334 &dev_attr_selftest
.attr
,
338 static umode_t
tsc200x_attr_is_visible(struct kobject
*kobj
,
339 struct attribute
*attr
, int n
)
341 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
342 struct tsc200x
*ts
= dev_get_drvdata(dev
);
343 umode_t mode
= attr
->mode
;
345 if (attr
== &dev_attr_selftest
.attr
) {
353 static const struct attribute_group tsc200x_attr_group
= {
354 .is_visible
= tsc200x_attr_is_visible
,
355 .attrs
= tsc200x_attrs
,
358 static void tsc200x_esd_work(struct work_struct
*work
)
360 struct tsc200x
*ts
= container_of(work
, struct tsc200x
, esd_work
.work
);
364 if (!mutex_trylock(&ts
->mutex
)) {
366 * If the mutex is taken, it means that disable or enable is in
367 * progress. In that case just reschedule the work. If the work
368 * is not needed, it will be canceled by disable.
373 if (time_is_after_jiffies(ts
->last_valid_interrupt
+
374 msecs_to_jiffies(ts
->esd_timeout
)))
377 /* We should be able to read register without disabling interrupts. */
378 error
= regmap_read(ts
->regmap
, TSC200X_REG_CFR0
, &r
);
380 !((r
^ TSC200X_CFR0_INITVALUE
) & TSC200X_CFR0_RW_MASK
)) {
385 * If we could not read our known value from configuration register 0
386 * then we should reset the controller as if from power-up and start
389 dev_info(ts
->dev
, "TSC200X not responding - resetting\n");
391 disable_irq(ts
->irq
);
392 del_timer_sync(&ts
->penup_timer
);
394 tsc200x_update_pen_state(ts
, 0, 0, 0);
399 tsc200x_start_scan(ts
);
402 mutex_unlock(&ts
->mutex
);
404 /* re-arm the watchdog */
405 schedule_delayed_work(&ts
->esd_work
,
406 round_jiffies_relative(
407 msecs_to_jiffies(ts
->esd_timeout
)));
410 static int tsc200x_open(struct input_dev
*input
)
412 struct tsc200x
*ts
= input_get_drvdata(input
);
414 mutex_lock(&ts
->mutex
);
417 __tsc200x_enable(ts
);
421 mutex_unlock(&ts
->mutex
);
426 static void tsc200x_close(struct input_dev
*input
)
428 struct tsc200x
*ts
= input_get_drvdata(input
);
430 mutex_lock(&ts
->mutex
);
433 __tsc200x_disable(ts
);
437 mutex_unlock(&ts
->mutex
);
440 int tsc200x_probe(struct device
*dev
, int irq
, const struct input_id
*tsc_id
,
441 struct regmap
*regmap
,
442 int (*tsc200x_cmd
)(struct device
*dev
, u8 cmd
))
445 struct input_dev
*input_dev
;
451 dev_err(dev
, "no irq\n");
456 return PTR_ERR(regmap
);
459 dev_err(dev
, "no cmd function\n");
463 ts
= devm_kzalloc(dev
, sizeof(*ts
), GFP_KERNEL
);
467 input_dev
= devm_input_allocate_device(dev
);
473 ts
->idev
= input_dev
;
475 ts
->tsc200x_cmd
= tsc200x_cmd
;
477 error
= device_property_read_u32(dev
, "ti,x-plate-ohms", &x_plate_ohm
);
478 ts
->x_plate_ohm
= error
? TSC200X_DEF_RESISTOR
: x_plate_ohm
;
480 error
= device_property_read_u32(dev
, "ti,esd-recovery-timeout-ms",
482 ts
->esd_timeout
= error
? 0 : esd_timeout
;
484 ts
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
485 if (IS_ERR(ts
->reset_gpio
)) {
486 error
= PTR_ERR(ts
->reset_gpio
);
487 dev_err(dev
, "error acquiring reset gpio: %d\n", error
);
491 ts
->vio
= devm_regulator_get(dev
, "vio");
492 if (IS_ERR(ts
->vio
)) {
493 error
= PTR_ERR(ts
->vio
);
494 dev_err(dev
, "error acquiring vio regulator: %d", error
);
498 mutex_init(&ts
->mutex
);
500 spin_lock_init(&ts
->lock
);
501 timer_setup(&ts
->penup_timer
, tsc200x_penup_timer
, 0);
503 INIT_DELAYED_WORK(&ts
->esd_work
, tsc200x_esd_work
);
505 snprintf(ts
->phys
, sizeof(ts
->phys
),
506 "%s/input-ts", dev_name(dev
));
508 if (tsc_id
->product
== 2004) {
509 input_dev
->name
= "TSC200X touchscreen";
511 input_dev
->name
= devm_kasprintf(dev
, GFP_KERNEL
,
512 "TSC%04d touchscreen",
514 if (!input_dev
->name
)
518 input_dev
->phys
= ts
->phys
;
519 input_dev
->id
= *tsc_id
;
521 input_dev
->open
= tsc200x_open
;
522 input_dev
->close
= tsc200x_close
;
524 input_set_drvdata(input_dev
, ts
);
526 __set_bit(INPUT_PROP_DIRECT
, input_dev
->propbit
);
527 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
529 input_set_abs_params(input_dev
, ABS_X
,
530 0, MAX_12BIT
, TSC200X_DEF_X_FUZZ
, 0);
531 input_set_abs_params(input_dev
, ABS_Y
,
532 0, MAX_12BIT
, TSC200X_DEF_Y_FUZZ
, 0);
533 input_set_abs_params(input_dev
, ABS_PRESSURE
,
534 0, MAX_12BIT
, TSC200X_DEF_P_FUZZ
, 0);
536 touchscreen_parse_properties(input_dev
, false, NULL
);
538 /* Ensure the touchscreen is off */
539 tsc200x_stop_scan(ts
);
541 error
= devm_request_threaded_irq(dev
, irq
, NULL
,
543 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
546 dev_err(dev
, "Failed to request irq, err: %d\n", error
);
550 error
= regulator_enable(ts
->vio
);
554 dev_set_drvdata(dev
, ts
);
555 error
= sysfs_create_group(&dev
->kobj
, &tsc200x_attr_group
);
558 "Failed to create sysfs attributes, err: %d\n", error
);
559 goto disable_regulator
;
562 error
= input_register_device(ts
->idev
);
565 "Failed to register input device, err: %d\n", error
);
566 goto err_remove_sysfs
;
569 irq_set_irq_wake(irq
, 1);
573 sysfs_remove_group(&dev
->kobj
, &tsc200x_attr_group
);
575 regulator_disable(ts
->vio
);
578 EXPORT_SYMBOL_GPL(tsc200x_probe
);
580 int tsc200x_remove(struct device
*dev
)
582 struct tsc200x
*ts
= dev_get_drvdata(dev
);
584 sysfs_remove_group(&dev
->kobj
, &tsc200x_attr_group
);
586 regulator_disable(ts
->vio
);
590 EXPORT_SYMBOL_GPL(tsc200x_remove
);
592 static int __maybe_unused
tsc200x_suspend(struct device
*dev
)
594 struct tsc200x
*ts
= dev_get_drvdata(dev
);
596 mutex_lock(&ts
->mutex
);
598 if (!ts
->suspended
&& ts
->opened
)
599 __tsc200x_disable(ts
);
601 ts
->suspended
= true;
603 mutex_unlock(&ts
->mutex
);
608 static int __maybe_unused
tsc200x_resume(struct device
*dev
)
610 struct tsc200x
*ts
= dev_get_drvdata(dev
);
612 mutex_lock(&ts
->mutex
);
614 if (ts
->suspended
&& ts
->opened
)
615 __tsc200x_enable(ts
);
617 ts
->suspended
= false;
619 mutex_unlock(&ts
->mutex
);
624 SIMPLE_DEV_PM_OPS(tsc200x_pm_ops
, tsc200x_suspend
, tsc200x_resume
);
625 EXPORT_SYMBOL_GPL(tsc200x_pm_ops
);
627 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
628 MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");
629 MODULE_LICENSE("GPL");