xref: /linux/drivers/input/touchscreen/tsc200x-core.c (revision 075c777ed7b2a02a9af7345dd55ee0b4794cd5eb)
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 	int			irq;
110 };
111 
112 static void tsc200x_update_pen_state(struct tsc200x *ts,
113 				     int x, int y, int pressure)
114 {
115 	if (pressure) {
116 		touchscreen_report_pos(ts->idev, &ts->prop, x, y, false);
117 		input_report_abs(ts->idev, ABS_PRESSURE, pressure);
118 		if (!ts->pen_down) {
119 			input_report_key(ts->idev, BTN_TOUCH, !!pressure);
120 			ts->pen_down = true;
121 		}
122 	} else {
123 		input_report_abs(ts->idev, ABS_PRESSURE, 0);
124 		if (ts->pen_down) {
125 			input_report_key(ts->idev, BTN_TOUCH, 0);
126 			ts->pen_down = false;
127 		}
128 	}
129 	input_sync(ts->idev);
130 	dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,
131 		pressure);
132 }
133 
134 static irqreturn_t tsc200x_irq_thread(int irq, void *_ts)
135 {
136 	struct tsc200x *ts = _ts;
137 	unsigned long flags;
138 	unsigned int pressure;
139 	struct tsc200x_data tsdata;
140 	int error;
141 
142 	/* read the coordinates */
143 	error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata,
144 				 TSC200X_DATA_REGS);
145 	if (unlikely(error))
146 		goto out;
147 
148 	/* validate position */
149 	if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))
150 		goto out;
151 
152 	/* Skip reading if the pressure components are out of range */
153 	if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))
154 		goto out;
155 	if (unlikely(tsdata.z1 >= tsdata.z2))
156 		goto out;
157 
158        /*
159 	* Skip point if this is a pen down with the exact same values as
160 	* the value before pen-up - that implies SPI fed us stale data
161 	*/
162 	if (!ts->pen_down &&
163 	    ts->in_x == tsdata.x && ts->in_y == tsdata.y &&
164 	    ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {
165 		goto out;
166 	}
167 
168 	/*
169 	 * At this point we are happy we have a valid and useful reading.
170 	 * Remember it for later comparisons. We may now begin downsampling.
171 	 */
172 	ts->in_x = tsdata.x;
173 	ts->in_y = tsdata.y;
174 	ts->in_z1 = tsdata.z1;
175 	ts->in_z2 = tsdata.z2;
176 
177 	/* Compute touch pressure resistance using equation #1 */
178 	pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;
179 	pressure = pressure * ts->x_plate_ohm / 4096;
180 	if (unlikely(pressure > MAX_12BIT))
181 		goto out;
182 
183 	spin_lock_irqsave(&ts->lock, flags);
184 
185 	tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure);
186 	mod_timer(&ts->penup_timer,
187 		  jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS));
188 
189 	spin_unlock_irqrestore(&ts->lock, flags);
190 
191 	ts->last_valid_interrupt = jiffies;
192 out:
193 	return IRQ_HANDLED;
194 }
195 
196 static void tsc200x_penup_timer(struct timer_list *t)
197 {
198 	struct tsc200x *ts = from_timer(ts, t, penup_timer);
199 	unsigned long flags;
200 
201 	spin_lock_irqsave(&ts->lock, flags);
202 	tsc200x_update_pen_state(ts, 0, 0, 0);
203 	spin_unlock_irqrestore(&ts->lock, flags);
204 }
205 
206 static void tsc200x_start_scan(struct tsc200x *ts)
207 {
208 	regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE);
209 	regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE);
210 	regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE);
211 	ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL);
212 }
213 
214 static void tsc200x_stop_scan(struct tsc200x *ts)
215 {
216 	ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP);
217 }
218 
219 static void tsc200x_reset(struct tsc200x *ts)
220 {
221 	if (ts->reset_gpio) {
222 		gpiod_set_value_cansleep(ts->reset_gpio, 1);
223 		usleep_range(100, 500); /* only 10us required */
224 		gpiod_set_value_cansleep(ts->reset_gpio, 0);
225 	}
226 }
227 
228 /* must be called with ts->mutex held */
229 static void __tsc200x_disable(struct tsc200x *ts)
230 {
231 	tsc200x_stop_scan(ts);
232 
233 	disable_irq(ts->irq);
234 	del_timer_sync(&ts->penup_timer);
235 
236 	cancel_delayed_work_sync(&ts->esd_work);
237 
238 	enable_irq(ts->irq);
239 }
240 
241 /* must be called with ts->mutex held */
242 static void __tsc200x_enable(struct tsc200x *ts)
243 {
244 	tsc200x_start_scan(ts);
245 
246 	if (ts->esd_timeout && ts->reset_gpio) {
247 		ts->last_valid_interrupt = jiffies;
248 		schedule_delayed_work(&ts->esd_work,
249 				round_jiffies_relative(
250 					msecs_to_jiffies(ts->esd_timeout)));
251 	}
252 }
253 
254 static ssize_t tsc200x_selftest_show(struct device *dev,
255 				     struct device_attribute *attr,
256 				     char *buf)
257 {
258 	struct tsc200x *ts = dev_get_drvdata(dev);
259 	unsigned int temp_high;
260 	unsigned int temp_high_orig;
261 	unsigned int temp_high_test;
262 	bool success = true;
263 	int error;
264 
265 	mutex_lock(&ts->mutex);
266 
267 	/*
268 	 * Test TSC200X communications via temp high register.
269 	 */
270 	__tsc200x_disable(ts);
271 
272 	error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig);
273 	if (error) {
274 		dev_warn(dev, "selftest failed: read error %d\n", error);
275 		success = false;
276 		goto out;
277 	}
278 
279 	temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
280 
281 	error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test);
282 	if (error) {
283 		dev_warn(dev, "selftest failed: write error %d\n", error);
284 		success = false;
285 		goto out;
286 	}
287 
288 	error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
289 	if (error) {
290 		dev_warn(dev, "selftest failed: read error %d after write\n",
291 			 error);
292 		success = false;
293 		goto out;
294 	}
295 
296 	if (temp_high != temp_high_test) {
297 		dev_warn(dev, "selftest failed: %d != %d\n",
298 			 temp_high, temp_high_test);
299 		success = false;
300 	}
301 
302 	/* hardware reset */
303 	tsc200x_reset(ts);
304 
305 	if (!success)
306 		goto out;
307 
308 	/* test that the reset really happened */
309 	error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);
310 	if (error) {
311 		dev_warn(dev, "selftest failed: read error %d after reset\n",
312 			 error);
313 		success = false;
314 		goto out;
315 	}
316 
317 	if (temp_high != temp_high_orig) {
318 		dev_warn(dev, "selftest failed after reset: %d != %d\n",
319 			 temp_high, temp_high_orig);
320 		success = false;
321 	}
322 
323 out:
324 	__tsc200x_enable(ts);
325 	mutex_unlock(&ts->mutex);
326 
327 	return sprintf(buf, "%d\n", success);
328 }
329 
330 static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL);
331 
332 static struct attribute *tsc200x_attrs[] = {
333 	&dev_attr_selftest.attr,
334 	NULL
335 };
336 
337 static umode_t tsc200x_attr_is_visible(struct kobject *kobj,
338 				      struct attribute *attr, int n)
339 {
340 	struct device *dev = kobj_to_dev(kobj);
341 	struct tsc200x *ts = dev_get_drvdata(dev);
342 	umode_t mode = attr->mode;
343 
344 	if (attr == &dev_attr_selftest.attr) {
345 		if (!ts->reset_gpio)
346 			mode = 0;
347 	}
348 
349 	return mode;
350 }
351 
352 static const struct attribute_group tsc200x_attr_group = {
353 	.is_visible	= tsc200x_attr_is_visible,
354 	.attrs		= tsc200x_attrs,
355 };
356 
357 const struct attribute_group *tsc200x_groups[] = {
358 	&tsc200x_attr_group,
359 	NULL
360 };
361 EXPORT_SYMBOL_GPL(tsc200x_groups);
362 
363 static void tsc200x_esd_work(struct work_struct *work)
364 {
365 	struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work);
366 	int error;
367 	unsigned int r;
368 
369 	if (!mutex_trylock(&ts->mutex)) {
370 		/*
371 		 * If the mutex is taken, it means that disable or enable is in
372 		 * progress. In that case just reschedule the work. If the work
373 		 * is not needed, it will be canceled by disable.
374 		 */
375 		goto reschedule;
376 	}
377 
378 	if (time_is_after_jiffies(ts->last_valid_interrupt +
379 				  msecs_to_jiffies(ts->esd_timeout)))
380 		goto out;
381 
382 	/* We should be able to read register without disabling interrupts. */
383 	error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r);
384 	if (!error &&
385 	    !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) {
386 		goto out;
387 	}
388 
389 	/*
390 	 * If we could not read our known value from configuration register 0
391 	 * then we should reset the controller as if from power-up and start
392 	 * scanning again.
393 	 */
394 	dev_info(ts->dev, "TSC200X not responding - resetting\n");
395 
396 	disable_irq(ts->irq);
397 	del_timer_sync(&ts->penup_timer);
398 
399 	tsc200x_update_pen_state(ts, 0, 0, 0);
400 
401 	tsc200x_reset(ts);
402 
403 	enable_irq(ts->irq);
404 	tsc200x_start_scan(ts);
405 
406 out:
407 	mutex_unlock(&ts->mutex);
408 reschedule:
409 	/* re-arm the watchdog */
410 	schedule_delayed_work(&ts->esd_work,
411 			      round_jiffies_relative(
412 					msecs_to_jiffies(ts->esd_timeout)));
413 }
414 
415 static int tsc200x_open(struct input_dev *input)
416 {
417 	struct tsc200x *ts = input_get_drvdata(input);
418 
419 	mutex_lock(&ts->mutex);
420 
421 	if (!ts->suspended)
422 		__tsc200x_enable(ts);
423 
424 	ts->opened = true;
425 
426 	mutex_unlock(&ts->mutex);
427 
428 	return 0;
429 }
430 
431 static void tsc200x_close(struct input_dev *input)
432 {
433 	struct tsc200x *ts = input_get_drvdata(input);
434 
435 	mutex_lock(&ts->mutex);
436 
437 	if (!ts->suspended)
438 		__tsc200x_disable(ts);
439 
440 	ts->opened = false;
441 
442 	mutex_unlock(&ts->mutex);
443 }
444 
445 int tsc200x_probe(struct device *dev, int irq, const struct input_id *tsc_id,
446 		  struct regmap *regmap,
447 		  int (*tsc200x_cmd)(struct device *dev, u8 cmd))
448 {
449 	struct tsc200x *ts;
450 	struct input_dev *input_dev;
451 	u32 x_plate_ohm;
452 	u32 esd_timeout;
453 	int error;
454 
455 	if (irq <= 0) {
456 		dev_err(dev, "no irq\n");
457 		return -ENODEV;
458 	}
459 
460 	if (IS_ERR(regmap))
461 		return PTR_ERR(regmap);
462 
463 	if (!tsc200x_cmd) {
464 		dev_err(dev, "no cmd function\n");
465 		return -ENODEV;
466 	}
467 
468 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
469 	if (!ts)
470 		return -ENOMEM;
471 
472 	input_dev = devm_input_allocate_device(dev);
473 	if (!input_dev)
474 		return -ENOMEM;
475 
476 	ts->irq = irq;
477 	ts->dev = dev;
478 	ts->idev = input_dev;
479 	ts->regmap = regmap;
480 	ts->tsc200x_cmd = tsc200x_cmd;
481 
482 	error = device_property_read_u32(dev, "ti,x-plate-ohms", &x_plate_ohm);
483 	ts->x_plate_ohm = error ? TSC200X_DEF_RESISTOR : x_plate_ohm;
484 
485 	error = device_property_read_u32(dev, "ti,esd-recovery-timeout-ms",
486 					 &esd_timeout);
487 	ts->esd_timeout = error ? 0 : esd_timeout;
488 
489 	ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
490 	if (IS_ERR(ts->reset_gpio)) {
491 		error = PTR_ERR(ts->reset_gpio);
492 		dev_err(dev, "error acquiring reset gpio: %d\n", error);
493 		return error;
494 	}
495 
496 	error = devm_regulator_get_enable(dev, "vio");
497 	if (error) {
498 		dev_err(dev, "error acquiring vio regulator: %d\n", error);
499 		return error;
500 	}
501 
502 	mutex_init(&ts->mutex);
503 
504 	spin_lock_init(&ts->lock);
505 	timer_setup(&ts->penup_timer, tsc200x_penup_timer, 0);
506 
507 	INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work);
508 
509 	snprintf(ts->phys, sizeof(ts->phys),
510 		 "%s/input-ts", dev_name(dev));
511 
512 	if (tsc_id->product == 2004) {
513 		input_dev->name = "TSC200X touchscreen";
514 	} else {
515 		input_dev->name = devm_kasprintf(dev, GFP_KERNEL,
516 						 "TSC%04d touchscreen",
517 						 tsc_id->product);
518 		if (!input_dev->name)
519 			return -ENOMEM;
520 	}
521 
522 	input_dev->phys = ts->phys;
523 	input_dev->id = *tsc_id;
524 
525 	input_dev->open = tsc200x_open;
526 	input_dev->close = tsc200x_close;
527 
528 	input_set_drvdata(input_dev, ts);
529 
530 	__set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
531 	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
532 
533 	input_set_abs_params(input_dev, ABS_X,
534 			     0, MAX_12BIT, TSC200X_DEF_X_FUZZ, 0);
535 	input_set_abs_params(input_dev, ABS_Y,
536 			     0, MAX_12BIT, TSC200X_DEF_Y_FUZZ, 0);
537 	input_set_abs_params(input_dev, ABS_PRESSURE,
538 			     0, MAX_12BIT, TSC200X_DEF_P_FUZZ, 0);
539 
540 	touchscreen_parse_properties(input_dev, false, &ts->prop);
541 
542 	/* Ensure the touchscreen is off */
543 	tsc200x_stop_scan(ts);
544 
545 	error = devm_request_threaded_irq(dev, irq, NULL, tsc200x_irq_thread,
546 					  IRQF_ONESHOT, "tsc200x", ts);
547 	if (error) {
548 		dev_err(dev, "Failed to request irq, err: %d\n", error);
549 		return error;
550 	}
551 
552 	dev_set_drvdata(dev, ts);
553 
554 	error = input_register_device(ts->idev);
555 	if (error) {
556 		dev_err(dev,
557 			"Failed to register input device, err: %d\n", error);
558 		return error;
559 	}
560 
561 	irq_set_irq_wake(irq, 1);
562 	return 0;
563 }
564 EXPORT_SYMBOL_GPL(tsc200x_probe);
565 
566 static int tsc200x_suspend(struct device *dev)
567 {
568 	struct tsc200x *ts = dev_get_drvdata(dev);
569 
570 	mutex_lock(&ts->mutex);
571 
572 	if (!ts->suspended && ts->opened)
573 		__tsc200x_disable(ts);
574 
575 	ts->suspended = true;
576 
577 	mutex_unlock(&ts->mutex);
578 
579 	return 0;
580 }
581 
582 static int tsc200x_resume(struct device *dev)
583 {
584 	struct tsc200x *ts = dev_get_drvdata(dev);
585 
586 	mutex_lock(&ts->mutex);
587 
588 	if (ts->suspended && ts->opened)
589 		__tsc200x_enable(ts);
590 
591 	ts->suspended = false;
592 
593 	mutex_unlock(&ts->mutex);
594 
595 	return 0;
596 }
597 
598 EXPORT_GPL_SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume);
599 
600 MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");
601 MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");
602 MODULE_LICENSE("GPL");
603