xref: /linux/drivers/input/touchscreen/ad7879.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AD7879/AD7889 based touchscreen and GPIO driver
4  *
5  * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
6  *
7  * History:
8  * Copyright (c) 2005 David Brownell
9  * Copyright (c) 2006 Nokia Corporation
10  * Various changes: Imre Deak <imre.deak@nokia.com>
11  *
12  * Using code from:
13  *  - corgi_ts.c
14  *	Copyright (C) 2004-2005 Richard Purdie
15  *  - omap_ts.[hc], ads7846.h, ts_osk.c
16  *	Copyright (C) 2002 MontaVista Software
17  *	Copyright (C) 2004 Texas Instruments
18  *	Copyright (C) 2005 Dirk Behme
19  *  - ad7877.c
20  *	Copyright (C) 2006-2008 Analog Devices Inc.
21  */
22 
23 #include <linux/device.h>
24 #include <linux/delay.h>
25 #include <linux/export.h>
26 #include <linux/input.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/property.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32 #include <linux/gpio/driver.h>
33 
34 #include <linux/input/touchscreen.h>
35 #include <linux/module.h>
36 #include "ad7879.h"
37 
38 #define AD7879_REG_ZEROS		0
39 #define AD7879_REG_CTRL1		1
40 #define AD7879_REG_CTRL2		2
41 #define AD7879_REG_CTRL3		3
42 #define AD7879_REG_AUX1HIGH		4
43 #define AD7879_REG_AUX1LOW		5
44 #define AD7879_REG_TEMP1HIGH		6
45 #define AD7879_REG_TEMP1LOW		7
46 #define AD7879_REG_XPLUS		8
47 #define AD7879_REG_YPLUS		9
48 #define AD7879_REG_Z1			10
49 #define AD7879_REG_Z2			11
50 #define AD7879_REG_AUXVBAT		12
51 #define AD7879_REG_TEMP			13
52 #define AD7879_REG_REVID		14
53 
54 /* Control REG 1 */
55 #define AD7879_TMR(x)			((x & 0xFF) << 0)
56 #define AD7879_ACQ(x)			((x & 0x3) << 8)
57 #define AD7879_MODE_NOC			(0 << 10)	/* Do not convert */
58 #define AD7879_MODE_SCC			(1 << 10)	/* Single channel conversion */
59 #define AD7879_MODE_SEQ0		(2 << 10)	/* Sequence 0 in Slave Mode */
60 #define AD7879_MODE_SEQ1		(3 << 10)	/* Sequence 1 in Master Mode */
61 #define AD7879_MODE_INT			(1 << 15)	/* PENIRQ disabled INT enabled */
62 
63 /* Control REG 2 */
64 #define AD7879_FCD(x)			((x & 0x3) << 0)
65 #define AD7879_RESET			(1 << 4)
66 #define AD7879_MFS(x)			((x & 0x3) << 5)
67 #define AD7879_AVG(x)			((x & 0x3) << 7)
68 #define	AD7879_SER			(1 << 9)	/* non-differential */
69 #define	AD7879_DFR			(0 << 9)	/* differential */
70 #define AD7879_GPIOPOL			(1 << 10)
71 #define AD7879_GPIODIR			(1 << 11)
72 #define AD7879_GPIO_DATA		(1 << 12)
73 #define AD7879_GPIO_EN			(1 << 13)
74 #define AD7879_PM(x)			((x & 0x3) << 14)
75 #define AD7879_PM_SHUTDOWN		(0)
76 #define AD7879_PM_DYN			(1)
77 #define AD7879_PM_FULLON		(2)
78 
79 /* Control REG 3 */
80 #define AD7879_TEMPMASK_BIT		(1<<15)
81 #define AD7879_AUXVBATMASK_BIT		(1<<14)
82 #define AD7879_INTMODE_BIT		(1<<13)
83 #define AD7879_GPIOALERTMASK_BIT	(1<<12)
84 #define AD7879_AUXLOW_BIT		(1<<11)
85 #define AD7879_AUXHIGH_BIT		(1<<10)
86 #define AD7879_TEMPLOW_BIT		(1<<9)
87 #define AD7879_TEMPHIGH_BIT		(1<<8)
88 #define AD7879_YPLUS_BIT		(1<<7)
89 #define AD7879_XPLUS_BIT		(1<<6)
90 #define AD7879_Z1_BIT			(1<<5)
91 #define AD7879_Z2_BIT			(1<<4)
92 #define AD7879_AUX_BIT			(1<<3)
93 #define AD7879_VBAT_BIT			(1<<2)
94 #define AD7879_TEMP_BIT			(1<<1)
95 
96 enum {
97 	AD7879_SEQ_YPOS  = 0,
98 	AD7879_SEQ_XPOS  = 1,
99 	AD7879_SEQ_Z1    = 2,
100 	AD7879_SEQ_Z2    = 3,
101 	AD7879_NR_SENSE  = 4,
102 };
103 
104 #define	MAX_12BIT			((1<<12)-1)
105 #define	TS_PEN_UP_TIMEOUT		msecs_to_jiffies(50)
106 
107 struct ad7879 {
108 	struct regmap		*regmap;
109 	struct device		*dev;
110 	struct input_dev	*input;
111 	struct timer_list	timer;
112 #ifdef CONFIG_GPIOLIB
113 	struct gpio_chip	gc;
114 	struct mutex		mutex;
115 #endif
116 	unsigned int		irq;
117 	bool			disabled;	/* P: input->mutex */
118 	bool			suspended;	/* P: input->mutex */
119 	bool			swap_xy;
120 	u16			conversion_data[AD7879_NR_SENSE];
121 	char			phys[32];
122 	u8			first_conversion_delay;
123 	u8			acquisition_time;
124 	u8			averaging;
125 	u8			pen_down_acc_interval;
126 	u8			median;
127 	u16			x_plate_ohms;
128 	u16			cmd_crtl1;
129 	u16			cmd_crtl2;
130 	u16			cmd_crtl3;
131 	int			x;
132 	int			y;
133 	int			Rt;
134 };
135 
136 static int ad7879_read(struct ad7879 *ts, u8 reg)
137 {
138 	unsigned int val;
139 	int error;
140 
141 	error = regmap_read(ts->regmap, reg, &val);
142 	if (error) {
143 		dev_err(ts->dev, "failed to read register %#02x: %d\n",
144 			reg, error);
145 		return error;
146 	}
147 
148 	return val;
149 }
150 
151 static int ad7879_write(struct ad7879 *ts, u8 reg, u16 val)
152 {
153 	int error;
154 
155 	error = regmap_write(ts->regmap, reg, val);
156 	if (error) {
157 		dev_err(ts->dev,
158 			"failed to write %#04x to register %#02x: %d\n",
159 			val, reg, error);
160 		return error;
161 	}
162 
163 	return 0;
164 }
165 
166 static int ad7879_report(struct ad7879 *ts)
167 {
168 	struct input_dev *input_dev = ts->input;
169 	unsigned Rt;
170 	u16 x, y, z1, z2;
171 
172 	x = ts->conversion_data[AD7879_SEQ_XPOS] & MAX_12BIT;
173 	y = ts->conversion_data[AD7879_SEQ_YPOS] & MAX_12BIT;
174 	z1 = ts->conversion_data[AD7879_SEQ_Z1] & MAX_12BIT;
175 	z2 = ts->conversion_data[AD7879_SEQ_Z2] & MAX_12BIT;
176 
177 	if (ts->swap_xy)
178 		swap(x, y);
179 
180 	/*
181 	 * The samples processed here are already preprocessed by the AD7879.
182 	 * The preprocessing function consists of a median and an averaging
183 	 * filter.  The combination of these two techniques provides a robust
184 	 * solution, discarding the spurious noise in the signal and keeping
185 	 * only the data of interest.  The size of both filters is
186 	 * programmable. (dev.platform_data, see linux/platform_data/ad7879.h)
187 	 * Other user-programmable conversion controls include variable
188 	 * acquisition time, and first conversion delay. Up to 16 averages can
189 	 * be taken per conversion.
190 	 */
191 
192 	if (likely(x && z1)) {
193 		/* compute touch pressure resistance using equation #1 */
194 		Rt = (z2 - z1) * x * ts->x_plate_ohms;
195 		Rt /= z1;
196 		Rt = (Rt + 2047) >> 12;
197 
198 		/*
199 		 * Sample found inconsistent, pressure is beyond
200 		 * the maximum. Don't report it to user space.
201 		 */
202 		if (Rt > input_abs_get_max(input_dev, ABS_PRESSURE))
203 			return -EINVAL;
204 
205 		/*
206 		 * Note that we delay reporting events by one sample.
207 		 * This is done to avoid reporting last sample of the
208 		 * touch sequence, which may be incomplete if finger
209 		 * leaves the surface before last reading is taken.
210 		 */
211 		if (timer_pending(&ts->timer)) {
212 			/* Touch continues */
213 			input_report_key(input_dev, BTN_TOUCH, 1);
214 			input_report_abs(input_dev, ABS_X, ts->x);
215 			input_report_abs(input_dev, ABS_Y, ts->y);
216 			input_report_abs(input_dev, ABS_PRESSURE, ts->Rt);
217 			input_sync(input_dev);
218 		}
219 
220 		ts->x = x;
221 		ts->y = y;
222 		ts->Rt = Rt;
223 
224 		return 0;
225 	}
226 
227 	return -EINVAL;
228 }
229 
230 static void ad7879_ts_event_release(struct ad7879 *ts)
231 {
232 	struct input_dev *input_dev = ts->input;
233 
234 	input_report_abs(input_dev, ABS_PRESSURE, 0);
235 	input_report_key(input_dev, BTN_TOUCH, 0);
236 	input_sync(input_dev);
237 }
238 
239 static void ad7879_timer(struct timer_list *t)
240 {
241 	struct ad7879 *ts = timer_container_of(ts, t, timer);
242 
243 	ad7879_ts_event_release(ts);
244 }
245 
246 static irqreturn_t ad7879_irq(int irq, void *handle)
247 {
248 	struct ad7879 *ts = handle;
249 	int error;
250 
251 	error = regmap_bulk_read(ts->regmap, AD7879_REG_XPLUS,
252 				 ts->conversion_data, AD7879_NR_SENSE);
253 	if (error)
254 		dev_err_ratelimited(ts->dev, "failed to read %#02x: %d\n",
255 				    AD7879_REG_XPLUS, error);
256 	else if (!ad7879_report(ts))
257 		mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
258 
259 	return IRQ_HANDLED;
260 }
261 
262 static void __ad7879_enable(struct ad7879 *ts)
263 {
264 	ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
265 	ad7879_write(ts, AD7879_REG_CTRL3, ts->cmd_crtl3);
266 	ad7879_write(ts, AD7879_REG_CTRL1, ts->cmd_crtl1);
267 
268 	enable_irq(ts->irq);
269 }
270 
271 static void __ad7879_disable(struct ad7879 *ts)
272 {
273 	u16 reg = (ts->cmd_crtl2 & ~AD7879_PM(-1)) |
274 		AD7879_PM(AD7879_PM_SHUTDOWN);
275 	disable_irq(ts->irq);
276 
277 	if (timer_delete_sync(&ts->timer))
278 		ad7879_ts_event_release(ts);
279 
280 	ad7879_write(ts, AD7879_REG_CTRL2, reg);
281 }
282 
283 
284 static int ad7879_open(struct input_dev *input)
285 {
286 	struct ad7879 *ts = input_get_drvdata(input);
287 
288 	/* protected by input->mutex */
289 	if (!ts->disabled && !ts->suspended)
290 		__ad7879_enable(ts);
291 
292 	return 0;
293 }
294 
295 static void ad7879_close(struct input_dev *input)
296 {
297 	struct ad7879 *ts = input_get_drvdata(input);
298 
299 	/* protected by input->mutex */
300 	if (!ts->disabled && !ts->suspended)
301 		__ad7879_disable(ts);
302 }
303 
304 static int __maybe_unused ad7879_suspend(struct device *dev)
305 {
306 	struct ad7879 *ts = dev_get_drvdata(dev);
307 
308 	mutex_lock(&ts->input->mutex);
309 
310 	if (!ts->suspended && !ts->disabled && input_device_enabled(ts->input))
311 		__ad7879_disable(ts);
312 
313 	ts->suspended = true;
314 
315 	mutex_unlock(&ts->input->mutex);
316 
317 	return 0;
318 }
319 
320 static int __maybe_unused ad7879_resume(struct device *dev)
321 {
322 	struct ad7879 *ts = dev_get_drvdata(dev);
323 
324 	mutex_lock(&ts->input->mutex);
325 
326 	if (ts->suspended && !ts->disabled && input_device_enabled(ts->input))
327 		__ad7879_enable(ts);
328 
329 	ts->suspended = false;
330 
331 	mutex_unlock(&ts->input->mutex);
332 
333 	return 0;
334 }
335 
336 SIMPLE_DEV_PM_OPS(ad7879_pm_ops, ad7879_suspend, ad7879_resume);
337 EXPORT_SYMBOL(ad7879_pm_ops);
338 
339 static void ad7879_toggle(struct ad7879 *ts, bool disable)
340 {
341 	mutex_lock(&ts->input->mutex);
342 
343 	if (!ts->suspended && input_device_enabled(ts->input)) {
344 
345 		if (disable) {
346 			if (ts->disabled)
347 				__ad7879_enable(ts);
348 		} else {
349 			if (!ts->disabled)
350 				__ad7879_disable(ts);
351 		}
352 	}
353 
354 	ts->disabled = disable;
355 
356 	mutex_unlock(&ts->input->mutex);
357 }
358 
359 static ssize_t ad7879_disable_show(struct device *dev,
360 				     struct device_attribute *attr, char *buf)
361 {
362 	struct ad7879 *ts = dev_get_drvdata(dev);
363 
364 	return sprintf(buf, "%u\n", ts->disabled);
365 }
366 
367 static ssize_t ad7879_disable_store(struct device *dev,
368 				     struct device_attribute *attr,
369 				     const char *buf, size_t count)
370 {
371 	struct ad7879 *ts = dev_get_drvdata(dev);
372 	unsigned int val;
373 	int error;
374 
375 	error = kstrtouint(buf, 10, &val);
376 	if (error)
377 		return error;
378 
379 	ad7879_toggle(ts, val);
380 
381 	return count;
382 }
383 
384 static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
385 
386 static struct attribute *ad7879_attributes[] = {
387 	&dev_attr_disable.attr,
388 	NULL
389 };
390 
391 static const struct attribute_group ad7879_attr_group = {
392 	.attrs = ad7879_attributes,
393 };
394 
395 const struct attribute_group *ad7879_groups[] = {
396 	&ad7879_attr_group,
397 	NULL
398 };
399 EXPORT_SYMBOL_GPL(ad7879_groups);
400 
401 #ifdef CONFIG_GPIOLIB
402 static int ad7879_gpio_direction_input(struct gpio_chip *chip,
403 					unsigned gpio)
404 {
405 	struct ad7879 *ts = gpiochip_get_data(chip);
406 	int err;
407 
408 	mutex_lock(&ts->mutex);
409 	ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
410 	err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
411 	mutex_unlock(&ts->mutex);
412 
413 	return err;
414 }
415 
416 static int ad7879_gpio_direction_output(struct gpio_chip *chip,
417 					unsigned gpio, int level)
418 {
419 	struct ad7879 *ts = gpiochip_get_data(chip);
420 	int err;
421 
422 	mutex_lock(&ts->mutex);
423 	ts->cmd_crtl2 &= ~AD7879_GPIODIR;
424 	ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
425 	if (level)
426 		ts->cmd_crtl2 |= AD7879_GPIO_DATA;
427 	else
428 		ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
429 
430 	err = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
431 	mutex_unlock(&ts->mutex);
432 
433 	return err;
434 }
435 
436 static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
437 {
438 	struct ad7879 *ts = gpiochip_get_data(chip);
439 	u16 val;
440 
441 	mutex_lock(&ts->mutex);
442 	val = ad7879_read(ts, AD7879_REG_CTRL2);
443 	mutex_unlock(&ts->mutex);
444 
445 	return !!(val & AD7879_GPIO_DATA);
446 }
447 
448 static int ad7879_gpio_set_value(struct gpio_chip *chip, unsigned int gpio,
449 				 int value)
450 {
451 	struct ad7879 *ts = gpiochip_get_data(chip);
452 	int ret;
453 
454 	mutex_lock(&ts->mutex);
455 	if (value)
456 		ts->cmd_crtl2 |= AD7879_GPIO_DATA;
457 	else
458 		ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
459 
460 	ret = ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
461 	mutex_unlock(&ts->mutex);
462 
463 	return ret;
464 }
465 
466 static int ad7879_gpio_add(struct ad7879 *ts)
467 {
468 	int ret = 0;
469 
470 	mutex_init(&ts->mutex);
471 
472 	/* Do not create a chip unless flagged for it */
473 	if (!device_property_read_bool(ts->dev, "gpio-controller"))
474 		return 0;
475 
476 	ts->gc.direction_input = ad7879_gpio_direction_input;
477 	ts->gc.direction_output = ad7879_gpio_direction_output;
478 	ts->gc.get = ad7879_gpio_get_value;
479 	ts->gc.set = ad7879_gpio_set_value;
480 	ts->gc.can_sleep = 1;
481 	ts->gc.base = -1;
482 	ts->gc.ngpio = 1;
483 	ts->gc.label = "AD7879-GPIO";
484 	ts->gc.owner = THIS_MODULE;
485 	ts->gc.parent = ts->dev;
486 
487 	ret = devm_gpiochip_add_data(ts->dev, &ts->gc, ts);
488 	if (ret)
489 		dev_err(ts->dev, "failed to register gpio %d\n",
490 			ts->gc.base);
491 
492 	return ret;
493 }
494 #else
495 static int ad7879_gpio_add(struct ad7879 *ts)
496 {
497 	return 0;
498 }
499 #endif
500 
501 static int ad7879_parse_dt(struct device *dev, struct ad7879 *ts)
502 {
503 	int err;
504 	u32 tmp;
505 
506 	err = device_property_read_u32(dev, "adi,resistance-plate-x", &tmp);
507 	if (err) {
508 		dev_err(dev, "failed to get resistance-plate-x property\n");
509 		return err;
510 	}
511 	ts->x_plate_ohms = (u16)tmp;
512 
513 	device_property_read_u8(dev, "adi,first-conversion-delay",
514 				&ts->first_conversion_delay);
515 	device_property_read_u8(dev, "adi,acquisition-time",
516 				&ts->acquisition_time);
517 	device_property_read_u8(dev, "adi,median-filter-size", &ts->median);
518 	device_property_read_u8(dev, "adi,averaging", &ts->averaging);
519 	device_property_read_u8(dev, "adi,conversion-interval",
520 				&ts->pen_down_acc_interval);
521 
522 	ts->swap_xy = device_property_read_bool(dev, "touchscreen-swapped-x-y");
523 
524 	return 0;
525 }
526 
527 int ad7879_probe(struct device *dev, struct regmap *regmap,
528 		 int irq, u16 bustype, u8 devid)
529 {
530 	struct ad7879 *ts;
531 	struct input_dev *input_dev;
532 	int err;
533 	u16 revid;
534 
535 	if (irq <= 0) {
536 		dev_err(dev, "No IRQ specified\n");
537 		return -EINVAL;
538 	}
539 
540 	ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
541 	if (!ts)
542 		return -ENOMEM;
543 
544 	err = ad7879_parse_dt(dev, ts);
545 	if (err)
546 		return err;
547 
548 	input_dev = devm_input_allocate_device(dev);
549 	if (!input_dev) {
550 		dev_err(dev, "Failed to allocate input device\n");
551 		return -ENOMEM;
552 	}
553 
554 	ts->dev = dev;
555 	ts->input = input_dev;
556 	ts->irq = irq;
557 	ts->regmap = regmap;
558 
559 	timer_setup(&ts->timer, ad7879_timer, 0);
560 	snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
561 
562 	input_dev->name = "AD7879 Touchscreen";
563 	input_dev->phys = ts->phys;
564 	input_dev->dev.parent = dev;
565 	input_dev->id.bustype = bustype;
566 
567 	input_dev->open = ad7879_open;
568 	input_dev->close = ad7879_close;
569 
570 	input_set_drvdata(input_dev, ts);
571 
572 	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
573 
574 	input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
575 	input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
576 	input_set_capability(input_dev, EV_ABS, ABS_PRESSURE);
577 	touchscreen_parse_properties(input_dev, false, NULL);
578 	if (!input_abs_get_max(input_dev, ABS_PRESSURE)) {
579 		dev_err(dev, "Touchscreen pressure is not specified\n");
580 		return -EINVAL;
581 	}
582 
583 	err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET);
584 	if (err < 0) {
585 		dev_err(dev, "Failed to write %s\n", input_dev->name);
586 		return err;
587 	}
588 
589 	revid = ad7879_read(ts, AD7879_REG_REVID);
590 	input_dev->id.product = (revid & 0xff);
591 	input_dev->id.version = revid >> 8;
592 	if (input_dev->id.product != devid) {
593 		dev_err(dev, "Failed to probe %s (%x vs %x)\n",
594 			input_dev->name, devid, revid);
595 		return -ENODEV;
596 	}
597 
598 	ts->cmd_crtl3 = AD7879_YPLUS_BIT |
599 			AD7879_XPLUS_BIT |
600 			AD7879_Z2_BIT |
601 			AD7879_Z1_BIT |
602 			AD7879_TEMPMASK_BIT |
603 			AD7879_AUXVBATMASK_BIT |
604 			AD7879_GPIOALERTMASK_BIT;
605 
606 	ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
607 			AD7879_AVG(ts->averaging) |
608 			AD7879_MFS(ts->median) |
609 			AD7879_FCD(ts->first_conversion_delay);
610 
611 	ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
612 			AD7879_ACQ(ts->acquisition_time) |
613 			AD7879_TMR(ts->pen_down_acc_interval);
614 
615 	err = devm_request_threaded_irq(dev, ts->irq, NULL, ad7879_irq,
616 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
617 					dev_name(dev), ts);
618 	if (err) {
619 		dev_err(dev, "Failed to request IRQ: %d\n", err);
620 		return err;
621 	}
622 
623 	__ad7879_disable(ts);
624 
625 	err = ad7879_gpio_add(ts);
626 	if (err)
627 		return err;
628 
629 	err = input_register_device(input_dev);
630 	if (err)
631 		return err;
632 
633 	dev_set_drvdata(dev, ts);
634 
635 	return 0;
636 }
637 EXPORT_SYMBOL(ad7879_probe);
638 
639 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
640 MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
641 MODULE_LICENSE("GPL");
642