Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
96enum {
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
107struct 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
136static 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
151static 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
166static 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
230static 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
239static 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
246static 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
262static 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
271static 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
284static 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
295static 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
304static int __maybe_unused ad7879_suspend(struct device *dev)
305{
306 struct ad7879 *ts = dev_get_drvdata(dev);
307
308 guard(mutex)(&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 return 0;
316}
317
318static int __maybe_unused ad7879_resume(struct device *dev)
319{
320 struct ad7879 *ts = dev_get_drvdata(dev);
321
322 guard(mutex)(&ts->input->mutex);
323
324 if (ts->suspended && !ts->disabled && input_device_enabled(ts->input))
325 __ad7879_enable(ts);
326
327 ts->suspended = false;
328
329 return 0;
330}
331
332SIMPLE_DEV_PM_OPS(ad7879_pm_ops, ad7879_suspend, ad7879_resume);
333EXPORT_SYMBOL(ad7879_pm_ops);
334
335static void ad7879_toggle(struct ad7879 *ts, bool disable)
336{
337 guard(mutex)(&ts->input->mutex);
338
339 if (!ts->suspended && input_device_enabled(ts->input)) {
340
341 if (disable) {
342 if (ts->disabled)
343 __ad7879_enable(ts);
344 } else {
345 if (!ts->disabled)
346 __ad7879_disable(ts);
347 }
348 }
349
350 ts->disabled = disable;
351}
352
353static ssize_t ad7879_disable_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
356 struct ad7879 *ts = dev_get_drvdata(dev);
357
358 return sprintf(buf, "%u\n", ts->disabled);
359}
360
361static ssize_t ad7879_disable_store(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
364{
365 struct ad7879 *ts = dev_get_drvdata(dev);
366 unsigned int val;
367 int error;
368
369 error = kstrtouint(buf, 10, &val);
370 if (error)
371 return error;
372
373 ad7879_toggle(ts, val);
374
375 return count;
376}
377
378static DEVICE_ATTR(disable, 0664, ad7879_disable_show, ad7879_disable_store);
379
380static struct attribute *ad7879_attributes[] = {
381 &dev_attr_disable.attr,
382 NULL
383};
384
385static const struct attribute_group ad7879_attr_group = {
386 .attrs = ad7879_attributes,
387};
388
389const struct attribute_group *ad7879_groups[] = {
390 &ad7879_attr_group,
391 NULL
392};
393EXPORT_SYMBOL_GPL(ad7879_groups);
394
395#ifdef CONFIG_GPIOLIB
396static int ad7879_gpio_direction_input(struct gpio_chip *chip,
397 unsigned gpio)
398{
399 struct ad7879 *ts = gpiochip_get_data(chip);
400
401 guard(mutex)(&ts->mutex);
402
403 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIODIR | AD7879_GPIOPOL;
404 return ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
405}
406
407static int ad7879_gpio_direction_output(struct gpio_chip *chip,
408 unsigned gpio, int level)
409{
410 struct ad7879 *ts = gpiochip_get_data(chip);
411
412 guard(mutex)(&ts->mutex);
413
414 ts->cmd_crtl2 &= ~AD7879_GPIODIR;
415 ts->cmd_crtl2 |= AD7879_GPIO_EN | AD7879_GPIOPOL;
416 if (level)
417 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
418 else
419 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
420
421 return ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
422}
423
424static int ad7879_gpio_get_value(struct gpio_chip *chip, unsigned int gpio)
425{
426 struct ad7879 *ts = gpiochip_get_data(chip);
427 u16 val;
428
429 guard(mutex)(&ts->mutex);
430
431 val = ad7879_read(ts, AD7879_REG_CTRL2);
432 return !!(val & AD7879_GPIO_DATA);
433}
434
435static int ad7879_gpio_set_value(struct gpio_chip *chip, unsigned int gpio,
436 int value)
437{
438 struct ad7879 *ts = gpiochip_get_data(chip);
439
440 guard(mutex)(&ts->mutex);
441
442 if (value)
443 ts->cmd_crtl2 |= AD7879_GPIO_DATA;
444 else
445 ts->cmd_crtl2 &= ~AD7879_GPIO_DATA;
446
447 return ad7879_write(ts, AD7879_REG_CTRL2, ts->cmd_crtl2);
448}
449
450static int ad7879_gpio_add(struct ad7879 *ts)
451{
452 int ret = 0;
453
454 mutex_init(&ts->mutex);
455
456 /* Do not create a chip unless flagged for it */
457 if (!device_property_read_bool(ts->dev, "gpio-controller"))
458 return 0;
459
460 ts->gc.direction_input = ad7879_gpio_direction_input;
461 ts->gc.direction_output = ad7879_gpio_direction_output;
462 ts->gc.get = ad7879_gpio_get_value;
463 ts->gc.set = ad7879_gpio_set_value;
464 ts->gc.can_sleep = 1;
465 ts->gc.base = -1;
466 ts->gc.ngpio = 1;
467 ts->gc.label = "AD7879-GPIO";
468 ts->gc.owner = THIS_MODULE;
469 ts->gc.parent = ts->dev;
470
471 ret = devm_gpiochip_add_data(ts->dev, &ts->gc, ts);
472 if (ret)
473 dev_err(ts->dev, "failed to register gpio %d\n",
474 ts->gc.base);
475
476 return ret;
477}
478#else
479static int ad7879_gpio_add(struct ad7879 *ts)
480{
481 return 0;
482}
483#endif
484
485static int ad7879_parse_dt(struct device *dev, struct ad7879 *ts)
486{
487 int err;
488 u32 tmp;
489
490 err = device_property_read_u32(dev, "adi,resistance-plate-x", &tmp);
491 if (err) {
492 dev_err(dev, "failed to get resistance-plate-x property\n");
493 return err;
494 }
495 ts->x_plate_ohms = (u16)tmp;
496
497 device_property_read_u8(dev, "adi,first-conversion-delay",
498 &ts->first_conversion_delay);
499 device_property_read_u8(dev, "adi,acquisition-time",
500 &ts->acquisition_time);
501 device_property_read_u8(dev, "adi,median-filter-size", &ts->median);
502 device_property_read_u8(dev, "adi,averaging", &ts->averaging);
503 device_property_read_u8(dev, "adi,conversion-interval",
504 &ts->pen_down_acc_interval);
505
506 ts->swap_xy = device_property_read_bool(dev, "touchscreen-swapped-x-y");
507
508 return 0;
509}
510
511int ad7879_probe(struct device *dev, struct regmap *regmap,
512 int irq, u16 bustype, u8 devid)
513{
514 struct ad7879 *ts;
515 struct input_dev *input_dev;
516 int err;
517 u16 revid;
518
519 if (irq <= 0) {
520 dev_err(dev, "No IRQ specified\n");
521 return -EINVAL;
522 }
523
524 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);
525 if (!ts)
526 return -ENOMEM;
527
528 err = ad7879_parse_dt(dev, ts);
529 if (err)
530 return err;
531
532 input_dev = devm_input_allocate_device(dev);
533 if (!input_dev) {
534 dev_err(dev, "Failed to allocate input device\n");
535 return -ENOMEM;
536 }
537
538 ts->dev = dev;
539 ts->input = input_dev;
540 ts->irq = irq;
541 ts->regmap = regmap;
542
543 timer_setup(&ts->timer, ad7879_timer, 0);
544 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(dev));
545
546 input_dev->name = "AD7879 Touchscreen";
547 input_dev->phys = ts->phys;
548 input_dev->dev.parent = dev;
549 input_dev->id.bustype = bustype;
550
551 input_dev->open = ad7879_open;
552 input_dev->close = ad7879_close;
553
554 input_set_drvdata(input_dev, ts);
555
556 input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
557
558 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
559 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
560 input_set_capability(input_dev, EV_ABS, ABS_PRESSURE);
561 touchscreen_parse_properties(input_dev, false, NULL);
562 if (!input_abs_get_max(input_dev, ABS_PRESSURE)) {
563 dev_err(dev, "Touchscreen pressure is not specified\n");
564 return -EINVAL;
565 }
566
567 err = ad7879_write(ts, AD7879_REG_CTRL2, AD7879_RESET);
568 if (err < 0) {
569 dev_err(dev, "Failed to write %s\n", input_dev->name);
570 return err;
571 }
572
573 revid = ad7879_read(ts, AD7879_REG_REVID);
574 input_dev->id.product = (revid & 0xff);
575 input_dev->id.version = revid >> 8;
576 if (input_dev->id.product != devid) {
577 dev_err(dev, "Failed to probe %s (%x vs %x)\n",
578 input_dev->name, devid, revid);
579 return -ENODEV;
580 }
581
582 ts->cmd_crtl3 = AD7879_YPLUS_BIT |
583 AD7879_XPLUS_BIT |
584 AD7879_Z2_BIT |
585 AD7879_Z1_BIT |
586 AD7879_TEMPMASK_BIT |
587 AD7879_AUXVBATMASK_BIT |
588 AD7879_GPIOALERTMASK_BIT;
589
590 ts->cmd_crtl2 = AD7879_PM(AD7879_PM_DYN) | AD7879_DFR |
591 AD7879_AVG(ts->averaging) |
592 AD7879_MFS(ts->median) |
593 AD7879_FCD(ts->first_conversion_delay);
594
595 ts->cmd_crtl1 = AD7879_MODE_INT | AD7879_MODE_SEQ1 |
596 AD7879_ACQ(ts->acquisition_time) |
597 AD7879_TMR(ts->pen_down_acc_interval);
598
599 err = devm_request_threaded_irq(dev, ts->irq, NULL, ad7879_irq,
600 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
601 dev_name(dev), ts);
602 if (err) {
603 dev_err(dev, "Failed to request IRQ: %d\n", err);
604 return err;
605 }
606
607 __ad7879_disable(ts);
608
609 err = ad7879_gpio_add(ts);
610 if (err)
611 return err;
612
613 err = input_register_device(input_dev);
614 if (err)
615 return err;
616
617 dev_set_drvdata(dev, ts);
618
619 return 0;
620}
621EXPORT_SYMBOL(ad7879_probe);
622
623MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
624MODULE_DESCRIPTION("AD7879(-1) touchscreen Driver");
625MODULE_LICENSE("GPL");