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