Linux Audio

Check our new training course

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