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