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