Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
4 *
5 * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
6 * Based on: ads7846.c
7 *
8 * Bugs: Enter bugs at http://blackfin.uclinux.org/
9 *
10 * History:
11 * Copyright (c) 2005 David Brownell
12 * Copyright (c) 2006 Nokia Corporation
13 * Various changes: Imre Deak <imre.deak@nokia.com>
14 *
15 * Using code from:
16 * - corgi_ts.c
17 * Copyright (C) 2004-2005 Richard Purdie
18 * - omap_ts.[hc], ads7846.h, ts_osk.c
19 * Copyright (C) 2002 MontaVista Software
20 * Copyright (C) 2004 Texas Instruments
21 * Copyright (C) 2005 Dirk Behme
22 */
23
24
25#include <linux/device.h>
26#include <linux/delay.h>
27#include <linux/input.h>
28#include <linux/interrupt.h>
29#include <linux/pm.h>
30#include <linux/slab.h>
31#include <linux/spi/spi.h>
32#include <linux/spi/ad7877.h>
33#include <linux/module.h>
34#include <asm/irq.h>
35
36#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
37
38#define MAX_SPI_FREQ_HZ 20000000
39#define MAX_12BIT ((1<<12)-1)
40
41#define AD7877_REG_ZEROS 0
42#define AD7877_REG_CTRL1 1
43#define AD7877_REG_CTRL2 2
44#define AD7877_REG_ALERT 3
45#define AD7877_REG_AUX1HIGH 4
46#define AD7877_REG_AUX1LOW 5
47#define AD7877_REG_BAT1HIGH 6
48#define AD7877_REG_BAT1LOW 7
49#define AD7877_REG_BAT2HIGH 8
50#define AD7877_REG_BAT2LOW 9
51#define AD7877_REG_TEMP1HIGH 10
52#define AD7877_REG_TEMP1LOW 11
53#define AD7877_REG_SEQ0 12
54#define AD7877_REG_SEQ1 13
55#define AD7877_REG_DAC 14
56#define AD7877_REG_NONE1 15
57#define AD7877_REG_EXTWRITE 15
58#define AD7877_REG_XPLUS 16
59#define AD7877_REG_YPLUS 17
60#define AD7877_REG_Z2 18
61#define AD7877_REG_aux1 19
62#define AD7877_REG_aux2 20
63#define AD7877_REG_aux3 21
64#define AD7877_REG_bat1 22
65#define AD7877_REG_bat2 23
66#define AD7877_REG_temp1 24
67#define AD7877_REG_temp2 25
68#define AD7877_REG_Z1 26
69#define AD7877_REG_GPIOCTRL1 27
70#define AD7877_REG_GPIOCTRL2 28
71#define AD7877_REG_GPIODATA 29
72#define AD7877_REG_NONE2 30
73#define AD7877_REG_NONE3 31
74
75#define AD7877_SEQ_YPLUS_BIT (1<<11)
76#define AD7877_SEQ_XPLUS_BIT (1<<10)
77#define AD7877_SEQ_Z2_BIT (1<<9)
78#define AD7877_SEQ_AUX1_BIT (1<<8)
79#define AD7877_SEQ_AUX2_BIT (1<<7)
80#define AD7877_SEQ_AUX3_BIT (1<<6)
81#define AD7877_SEQ_BAT1_BIT (1<<5)
82#define AD7877_SEQ_BAT2_BIT (1<<4)
83#define AD7877_SEQ_TEMP1_BIT (1<<3)
84#define AD7877_SEQ_TEMP2_BIT (1<<2)
85#define AD7877_SEQ_Z1_BIT (1<<1)
86
87enum {
88 AD7877_SEQ_YPOS = 0,
89 AD7877_SEQ_XPOS = 1,
90 AD7877_SEQ_Z2 = 2,
91 AD7877_SEQ_AUX1 = 3,
92 AD7877_SEQ_AUX2 = 4,
93 AD7877_SEQ_AUX3 = 5,
94 AD7877_SEQ_BAT1 = 6,
95 AD7877_SEQ_BAT2 = 7,
96 AD7877_SEQ_TEMP1 = 8,
97 AD7877_SEQ_TEMP2 = 9,
98 AD7877_SEQ_Z1 = 10,
99 AD7877_NR_SENSE = 11,
100};
101
102/* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
103#define AD7877_DAC_CONF 0x1
104
105/* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
106#define AD7877_EXTW_GPIO_3_CONF 0x1C4
107#define AD7877_EXTW_GPIO_DATA 0x200
108
109/* Control REG 2 */
110#define AD7877_TMR(x) ((x & 0x3) << 0)
111#define AD7877_REF(x) ((x & 0x1) << 2)
112#define AD7877_POL(x) ((x & 0x1) << 3)
113#define AD7877_FCD(x) ((x & 0x3) << 4)
114#define AD7877_PM(x) ((x & 0x3) << 6)
115#define AD7877_ACQ(x) ((x & 0x3) << 8)
116#define AD7877_AVG(x) ((x & 0x3) << 10)
117
118/* Control REG 1 */
119#define AD7877_SER (1 << 11) /* non-differential */
120#define AD7877_DFR (0 << 11) /* differential */
121
122#define AD7877_MODE_NOC (0) /* Do not convert */
123#define AD7877_MODE_SCC (1) /* Single channel conversion */
124#define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
125#define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
126
127#define AD7877_CHANADD(x) ((x&0xF)<<7)
128#define AD7877_READADD(x) ((x)<<2)
129#define AD7877_WRITEADD(x) ((x)<<12)
130
131#define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
132 AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
133 AD7877_READADD(AD7877_REG_ ## x))
134
135#define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
136 AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
137
138/*
139 * Non-touchscreen sensors only use single-ended conversions.
140 */
141
142struct ser_req {
143 u16 reset;
144 u16 ref_on;
145 u16 command;
146 struct spi_message msg;
147 struct spi_transfer xfer[6];
148
149 /*
150 * DMA (thus cache coherency maintenance) requires the
151 * transfer buffers to live in their own cache lines.
152 */
153 u16 sample ____cacheline_aligned;
154};
155
156struct ad7877 {
157 struct input_dev *input;
158 char phys[32];
159
160 struct spi_device *spi;
161 u16 model;
162 u16 vref_delay_usecs;
163 u16 x_plate_ohms;
164 u16 pressure_max;
165
166 u16 cmd_crtl1;
167 u16 cmd_crtl2;
168 u16 cmd_dummy;
169 u16 dac;
170
171 u8 stopacq_polarity;
172 u8 first_conversion_delay;
173 u8 acquisition_time;
174 u8 averaging;
175 u8 pen_down_acc_interval;
176
177 struct spi_transfer xfer[AD7877_NR_SENSE + 2];
178 struct spi_message msg;
179
180 struct mutex mutex;
181 bool disabled; /* P: mutex */
182 bool gpio3; /* P: mutex */
183 bool gpio4; /* P: mutex */
184
185 spinlock_t lock;
186 struct timer_list timer; /* P: lock */
187
188 /*
189 * DMA (thus cache coherency maintenance) requires the
190 * transfer buffers to live in their own cache lines.
191 */
192 u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
193};
194
195static bool gpio3;
196module_param(gpio3, bool, 0);
197MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
198
199static int ad7877_read(struct spi_device *spi, u16 reg)
200{
201 struct ser_req *req;
202 int status, ret;
203
204 req = kzalloc(sizeof *req, GFP_KERNEL);
205 if (!req)
206 return -ENOMEM;
207
208 spi_message_init(&req->msg);
209
210 req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
211 AD7877_READADD(reg));
212 req->xfer[0].tx_buf = &req->command;
213 req->xfer[0].len = 2;
214 req->xfer[0].cs_change = 1;
215
216 req->xfer[1].rx_buf = &req->sample;
217 req->xfer[1].len = 2;
218
219 spi_message_add_tail(&req->xfer[0], &req->msg);
220 spi_message_add_tail(&req->xfer[1], &req->msg);
221
222 status = spi_sync(spi, &req->msg);
223 ret = status ? : req->sample;
224
225 kfree(req);
226
227 return ret;
228}
229
230static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
231{
232 struct ser_req *req;
233 int status;
234
235 req = kzalloc(sizeof *req, GFP_KERNEL);
236 if (!req)
237 return -ENOMEM;
238
239 spi_message_init(&req->msg);
240
241 req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
242 req->xfer[0].tx_buf = &req->command;
243 req->xfer[0].len = 2;
244
245 spi_message_add_tail(&req->xfer[0], &req->msg);
246
247 status = spi_sync(spi, &req->msg);
248
249 kfree(req);
250
251 return status;
252}
253
254static int ad7877_read_adc(struct spi_device *spi, unsigned command)
255{
256 struct ad7877 *ts = spi_get_drvdata(spi);
257 struct ser_req *req;
258 int status;
259 int sample;
260 int i;
261
262 req = kzalloc(sizeof *req, GFP_KERNEL);
263 if (!req)
264 return -ENOMEM;
265
266 spi_message_init(&req->msg);
267
268 /* activate reference, so it has time to settle; */
269 req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
270 AD7877_POL(ts->stopacq_polarity) |
271 AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
272 AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
273
274 req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
275
276 req->command = (u16) command;
277
278 req->xfer[0].tx_buf = &req->reset;
279 req->xfer[0].len = 2;
280 req->xfer[0].cs_change = 1;
281
282 req->xfer[1].tx_buf = &req->ref_on;
283 req->xfer[1].len = 2;
284 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
285 req->xfer[1].cs_change = 1;
286
287 req->xfer[2].tx_buf = &req->command;
288 req->xfer[2].len = 2;
289 req->xfer[2].delay_usecs = ts->vref_delay_usecs;
290 req->xfer[2].cs_change = 1;
291
292 req->xfer[3].rx_buf = &req->sample;
293 req->xfer[3].len = 2;
294 req->xfer[3].cs_change = 1;
295
296 req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
297 req->xfer[4].len = 2;
298 req->xfer[4].cs_change = 1;
299
300 req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
301 req->xfer[5].len = 2;
302
303 /* group all the transfers together, so we can't interfere with
304 * reading touchscreen state; disable penirq while sampling
305 */
306 for (i = 0; i < 6; i++)
307 spi_message_add_tail(&req->xfer[i], &req->msg);
308
309 status = spi_sync(spi, &req->msg);
310 sample = req->sample;
311
312 kfree(req);
313
314 return status ? : sample;
315}
316
317static int ad7877_process_data(struct ad7877 *ts)
318{
319 struct input_dev *input_dev = ts->input;
320 unsigned Rt;
321 u16 x, y, z1, z2;
322
323 x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
324 y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
325 z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
326 z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
327
328 /*
329 * The samples processed here are already preprocessed by the AD7877.
330 * The preprocessing function consists of an averaging filter.
331 * The combination of 'first conversion delay' and averaging provides a robust solution,
332 * discarding the spurious noise in the signal and keeping only the data of interest.
333 * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
334 * Other user-programmable conversion controls include variable acquisition time,
335 * and first conversion delay. Up to 16 averages can be taken per conversion.
336 */
337
338 if (likely(x && z1)) {
339 /* compute touch pressure resistance using equation #1 */
340 Rt = (z2 - z1) * x * ts->x_plate_ohms;
341 Rt /= z1;
342 Rt = (Rt + 2047) >> 12;
343
344 /*
345 * Sample found inconsistent, pressure is beyond
346 * the maximum. Don't report it to user space.
347 */
348 if (Rt > ts->pressure_max)
349 return -EINVAL;
350
351 if (!timer_pending(&ts->timer))
352 input_report_key(input_dev, BTN_TOUCH, 1);
353
354 input_report_abs(input_dev, ABS_X, x);
355 input_report_abs(input_dev, ABS_Y, y);
356 input_report_abs(input_dev, ABS_PRESSURE, Rt);
357 input_sync(input_dev);
358
359 return 0;
360 }
361
362 return -EINVAL;
363}
364
365static inline void ad7877_ts_event_release(struct ad7877 *ts)
366{
367 struct input_dev *input_dev = ts->input;
368
369 input_report_abs(input_dev, ABS_PRESSURE, 0);
370 input_report_key(input_dev, BTN_TOUCH, 0);
371 input_sync(input_dev);
372}
373
374static void ad7877_timer(struct timer_list *t)
375{
376 struct ad7877 *ts = from_timer(ts, t, timer);
377 unsigned long flags;
378
379 spin_lock_irqsave(&ts->lock, flags);
380 ad7877_ts_event_release(ts);
381 spin_unlock_irqrestore(&ts->lock, flags);
382}
383
384static irqreturn_t ad7877_irq(int irq, void *handle)
385{
386 struct ad7877 *ts = handle;
387 unsigned long flags;
388 int error;
389
390 error = spi_sync(ts->spi, &ts->msg);
391 if (error) {
392 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
393 goto out;
394 }
395
396 spin_lock_irqsave(&ts->lock, flags);
397 error = ad7877_process_data(ts);
398 if (!error)
399 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
400 spin_unlock_irqrestore(&ts->lock, flags);
401
402out:
403 return IRQ_HANDLED;
404}
405
406static void ad7877_disable(void *data)
407{
408 struct ad7877 *ts = data;
409
410 mutex_lock(&ts->mutex);
411
412 if (!ts->disabled) {
413 ts->disabled = true;
414 disable_irq(ts->spi->irq);
415
416 if (del_timer_sync(&ts->timer))
417 ad7877_ts_event_release(ts);
418 }
419
420 /*
421 * We know the chip's in lowpower mode since we always
422 * leave it that way after every request
423 */
424
425 mutex_unlock(&ts->mutex);
426}
427
428static void ad7877_enable(struct ad7877 *ts)
429{
430 mutex_lock(&ts->mutex);
431
432 if (ts->disabled) {
433 ts->disabled = false;
434 enable_irq(ts->spi->irq);
435 }
436
437 mutex_unlock(&ts->mutex);
438}
439
440#define SHOW(name) static ssize_t \
441name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
442{ \
443 struct ad7877 *ts = dev_get_drvdata(dev); \
444 ssize_t v = ad7877_read_adc(ts->spi, \
445 AD7877_READ_CHAN(name)); \
446 if (v < 0) \
447 return v; \
448 return sprintf(buf, "%u\n", (unsigned) v); \
449} \
450static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
451
452SHOW(aux1)
453SHOW(aux2)
454SHOW(aux3)
455SHOW(bat1)
456SHOW(bat2)
457SHOW(temp1)
458SHOW(temp2)
459
460static ssize_t ad7877_disable_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
463 struct ad7877 *ts = dev_get_drvdata(dev);
464
465 return sprintf(buf, "%u\n", ts->disabled);
466}
467
468static ssize_t ad7877_disable_store(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf, size_t count)
471{
472 struct ad7877 *ts = dev_get_drvdata(dev);
473 unsigned int val;
474 int error;
475
476 error = kstrtouint(buf, 10, &val);
477 if (error)
478 return error;
479
480 if (val)
481 ad7877_disable(ts);
482 else
483 ad7877_enable(ts);
484
485 return count;
486}
487
488static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
489
490static ssize_t ad7877_dac_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
493 struct ad7877 *ts = dev_get_drvdata(dev);
494
495 return sprintf(buf, "%u\n", ts->dac);
496}
497
498static ssize_t ad7877_dac_store(struct device *dev,
499 struct device_attribute *attr,
500 const char *buf, size_t count)
501{
502 struct ad7877 *ts = dev_get_drvdata(dev);
503 unsigned int val;
504 int error;
505
506 error = kstrtouint(buf, 10, &val);
507 if (error)
508 return error;
509
510 mutex_lock(&ts->mutex);
511 ts->dac = val & 0xFF;
512 ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
513 mutex_unlock(&ts->mutex);
514
515 return count;
516}
517
518static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
519
520static ssize_t ad7877_gpio3_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
523 struct ad7877 *ts = dev_get_drvdata(dev);
524
525 return sprintf(buf, "%u\n", ts->gpio3);
526}
527
528static ssize_t ad7877_gpio3_store(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 struct ad7877 *ts = dev_get_drvdata(dev);
533 unsigned int val;
534 int error;
535
536 error = kstrtouint(buf, 10, &val);
537 if (error)
538 return error;
539
540 mutex_lock(&ts->mutex);
541 ts->gpio3 = !!val;
542 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
543 (ts->gpio4 << 4) | (ts->gpio3 << 5));
544 mutex_unlock(&ts->mutex);
545
546 return count;
547}
548
549static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
550
551static ssize_t ad7877_gpio4_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 struct ad7877 *ts = dev_get_drvdata(dev);
555
556 return sprintf(buf, "%u\n", ts->gpio4);
557}
558
559static ssize_t ad7877_gpio4_store(struct device *dev,
560 struct device_attribute *attr,
561 const char *buf, size_t count)
562{
563 struct ad7877 *ts = dev_get_drvdata(dev);
564 unsigned int val;
565 int error;
566
567 error = kstrtouint(buf, 10, &val);
568 if (error)
569 return error;
570
571 mutex_lock(&ts->mutex);
572 ts->gpio4 = !!val;
573 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
574 (ts->gpio4 << 4) | (ts->gpio3 << 5));
575 mutex_unlock(&ts->mutex);
576
577 return count;
578}
579
580static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
581
582static struct attribute *ad7877_attributes[] = {
583 &dev_attr_temp1.attr,
584 &dev_attr_temp2.attr,
585 &dev_attr_aux1.attr,
586 &dev_attr_aux2.attr,
587 &dev_attr_aux3.attr,
588 &dev_attr_bat1.attr,
589 &dev_attr_bat2.attr,
590 &dev_attr_disable.attr,
591 &dev_attr_dac.attr,
592 &dev_attr_gpio3.attr,
593 &dev_attr_gpio4.attr,
594 NULL
595};
596
597static umode_t ad7877_attr_is_visible(struct kobject *kobj,
598 struct attribute *attr, int n)
599{
600 umode_t mode = attr->mode;
601
602 if (attr == &dev_attr_aux3.attr) {
603 if (gpio3)
604 mode = 0;
605 } else if (attr == &dev_attr_gpio3.attr) {
606 if (!gpio3)
607 mode = 0;
608 }
609
610 return mode;
611}
612
613static const struct attribute_group ad7877_attr_group = {
614 .is_visible = ad7877_attr_is_visible,
615 .attrs = ad7877_attributes,
616};
617
618static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
619{
620 struct spi_message *m;
621 int i;
622
623 ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
624 AD7877_POL(ts->stopacq_polarity) |
625 AD7877_AVG(ts->averaging) | AD7877_PM(1) |
626 AD7877_TMR(ts->pen_down_acc_interval) |
627 AD7877_ACQ(ts->acquisition_time) |
628 AD7877_FCD(ts->first_conversion_delay);
629
630 ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
631
632 ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
633 AD7877_READADD(AD7877_REG_XPLUS-1) |
634 AD7877_MODE_SEQ1 | AD7877_DFR;
635
636 ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
637
638 ts->cmd_dummy = 0;
639
640 m = &ts->msg;
641
642 spi_message_init(m);
643
644 m->context = ts;
645
646 ts->xfer[0].tx_buf = &ts->cmd_crtl1;
647 ts->xfer[0].len = 2;
648 ts->xfer[0].cs_change = 1;
649
650 spi_message_add_tail(&ts->xfer[0], m);
651
652 ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
653 ts->xfer[1].len = 2;
654 ts->xfer[1].cs_change = 1;
655
656 spi_message_add_tail(&ts->xfer[1], m);
657
658 for (i = 0; i < AD7877_NR_SENSE; i++) {
659 ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
660 ts->xfer[i + 2].len = 2;
661 if (i < (AD7877_NR_SENSE - 1))
662 ts->xfer[i + 2].cs_change = 1;
663 spi_message_add_tail(&ts->xfer[i + 2], m);
664 }
665}
666
667static int ad7877_probe(struct spi_device *spi)
668{
669 struct ad7877 *ts;
670 struct input_dev *input_dev;
671 struct ad7877_platform_data *pdata = dev_get_platdata(&spi->dev);
672 int err;
673 u16 verify;
674
675 if (!spi->irq) {
676 dev_dbg(&spi->dev, "no IRQ?\n");
677 return -ENODEV;
678 }
679
680 if (!pdata) {
681 dev_dbg(&spi->dev, "no platform data?\n");
682 return -ENODEV;
683 }
684
685 /* don't exceed max specified SPI CLK frequency */
686 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
687 dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
688 return -EINVAL;
689 }
690
691 spi->bits_per_word = 16;
692 err = spi_setup(spi);
693 if (err) {
694 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
695 return err;
696 }
697
698 ts = devm_kzalloc(&spi->dev, sizeof(struct ad7877), GFP_KERNEL);
699 if (!ts)
700 return -ENOMEM;
701
702 input_dev = devm_input_allocate_device(&spi->dev);
703 if (!input_dev)
704 return -ENOMEM;
705
706 err = devm_add_action_or_reset(&spi->dev, ad7877_disable, ts);
707 if (err)
708 return err;
709
710 spi_set_drvdata(spi, ts);
711 ts->spi = spi;
712 ts->input = input_dev;
713
714 timer_setup(&ts->timer, ad7877_timer, 0);
715 mutex_init(&ts->mutex);
716 spin_lock_init(&ts->lock);
717
718 ts->model = pdata->model ? : 7877;
719 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
720 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
721 ts->pressure_max = pdata->pressure_max ? : ~0;
722
723 ts->stopacq_polarity = pdata->stopacq_polarity;
724 ts->first_conversion_delay = pdata->first_conversion_delay;
725 ts->acquisition_time = pdata->acquisition_time;
726 ts->averaging = pdata->averaging;
727 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
728
729 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
730
731 input_dev->name = "AD7877 Touchscreen";
732 input_dev->phys = ts->phys;
733 input_dev->dev.parent = &spi->dev;
734
735 __set_bit(EV_KEY, input_dev->evbit);
736 __set_bit(BTN_TOUCH, input_dev->keybit);
737 __set_bit(EV_ABS, input_dev->evbit);
738 __set_bit(ABS_X, input_dev->absbit);
739 __set_bit(ABS_Y, input_dev->absbit);
740 __set_bit(ABS_PRESSURE, input_dev->absbit);
741
742 input_set_abs_params(input_dev, ABS_X,
743 pdata->x_min ? : 0,
744 pdata->x_max ? : MAX_12BIT,
745 0, 0);
746 input_set_abs_params(input_dev, ABS_Y,
747 pdata->y_min ? : 0,
748 pdata->y_max ? : MAX_12BIT,
749 0, 0);
750 input_set_abs_params(input_dev, ABS_PRESSURE,
751 pdata->pressure_min, pdata->pressure_max, 0, 0);
752
753 ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
754
755 verify = ad7877_read(spi, AD7877_REG_SEQ1);
756
757 if (verify != AD7877_MM_SEQUENCE) {
758 dev_err(&spi->dev, "%s: Failed to probe %s\n",
759 dev_name(&spi->dev), input_dev->name);
760 return -ENODEV;
761 }
762
763 if (gpio3)
764 ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
765
766 ad7877_setup_ts_def_msg(spi, ts);
767
768 /* Request AD7877 /DAV GPIO interrupt */
769
770 err = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, ad7877_irq,
771 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
772 spi->dev.driver->name, ts);
773 if (err) {
774 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
775 return err;
776 }
777
778 err = devm_device_add_group(&spi->dev, &ad7877_attr_group);
779 if (err)
780 return err;
781
782 err = input_register_device(input_dev);
783 if (err)
784 return err;
785
786 return 0;
787}
788
789static int __maybe_unused ad7877_suspend(struct device *dev)
790{
791 struct ad7877 *ts = dev_get_drvdata(dev);
792
793 ad7877_disable(ts);
794
795 return 0;
796}
797
798static int __maybe_unused ad7877_resume(struct device *dev)
799{
800 struct ad7877 *ts = dev_get_drvdata(dev);
801
802 ad7877_enable(ts);
803
804 return 0;
805}
806
807static SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
808
809static struct spi_driver ad7877_driver = {
810 .driver = {
811 .name = "ad7877",
812 .pm = &ad7877_pm,
813 },
814 .probe = ad7877_probe,
815};
816
817module_spi_driver(ad7877_driver);
818
819MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
820MODULE_DESCRIPTION("AD7877 touchscreen Driver");
821MODULE_LICENSE("GPL");
822MODULE_ALIAS("spi:ad7877");
1/*
2 * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
3 *
4 * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
5 * Based on: ads7846.c
6 *
7 * Bugs: Enter bugs at http://blackfin.uclinux.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see the file COPYING, or write
21 * to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 * History:
25 * Copyright (c) 2005 David Brownell
26 * Copyright (c) 2006 Nokia Corporation
27 * Various changes: Imre Deak <imre.deak@nokia.com>
28 *
29 * Using code from:
30 * - corgi_ts.c
31 * Copyright (C) 2004-2005 Richard Purdie
32 * - omap_ts.[hc], ads7846.h, ts_osk.c
33 * Copyright (C) 2002 MontaVista Software
34 * Copyright (C) 2004 Texas Instruments
35 * Copyright (C) 2005 Dirk Behme
36 */
37
38
39#include <linux/device.h>
40#include <linux/delay.h>
41#include <linux/input.h>
42#include <linux/interrupt.h>
43#include <linux/pm.h>
44#include <linux/slab.h>
45#include <linux/spi/spi.h>
46#include <linux/spi/ad7877.h>
47#include <linux/module.h>
48#include <asm/irq.h>
49
50#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
51
52#define MAX_SPI_FREQ_HZ 20000000
53#define MAX_12BIT ((1<<12)-1)
54
55#define AD7877_REG_ZEROS 0
56#define AD7877_REG_CTRL1 1
57#define AD7877_REG_CTRL2 2
58#define AD7877_REG_ALERT 3
59#define AD7877_REG_AUX1HIGH 4
60#define AD7877_REG_AUX1LOW 5
61#define AD7877_REG_BAT1HIGH 6
62#define AD7877_REG_BAT1LOW 7
63#define AD7877_REG_BAT2HIGH 8
64#define AD7877_REG_BAT2LOW 9
65#define AD7877_REG_TEMP1HIGH 10
66#define AD7877_REG_TEMP1LOW 11
67#define AD7877_REG_SEQ0 12
68#define AD7877_REG_SEQ1 13
69#define AD7877_REG_DAC 14
70#define AD7877_REG_NONE1 15
71#define AD7877_REG_EXTWRITE 15
72#define AD7877_REG_XPLUS 16
73#define AD7877_REG_YPLUS 17
74#define AD7877_REG_Z2 18
75#define AD7877_REG_aux1 19
76#define AD7877_REG_aux2 20
77#define AD7877_REG_aux3 21
78#define AD7877_REG_bat1 22
79#define AD7877_REG_bat2 23
80#define AD7877_REG_temp1 24
81#define AD7877_REG_temp2 25
82#define AD7877_REG_Z1 26
83#define AD7877_REG_GPIOCTRL1 27
84#define AD7877_REG_GPIOCTRL2 28
85#define AD7877_REG_GPIODATA 29
86#define AD7877_REG_NONE2 30
87#define AD7877_REG_NONE3 31
88
89#define AD7877_SEQ_YPLUS_BIT (1<<11)
90#define AD7877_SEQ_XPLUS_BIT (1<<10)
91#define AD7877_SEQ_Z2_BIT (1<<9)
92#define AD7877_SEQ_AUX1_BIT (1<<8)
93#define AD7877_SEQ_AUX2_BIT (1<<7)
94#define AD7877_SEQ_AUX3_BIT (1<<6)
95#define AD7877_SEQ_BAT1_BIT (1<<5)
96#define AD7877_SEQ_BAT2_BIT (1<<4)
97#define AD7877_SEQ_TEMP1_BIT (1<<3)
98#define AD7877_SEQ_TEMP2_BIT (1<<2)
99#define AD7877_SEQ_Z1_BIT (1<<1)
100
101enum {
102 AD7877_SEQ_YPOS = 0,
103 AD7877_SEQ_XPOS = 1,
104 AD7877_SEQ_Z2 = 2,
105 AD7877_SEQ_AUX1 = 3,
106 AD7877_SEQ_AUX2 = 4,
107 AD7877_SEQ_AUX3 = 5,
108 AD7877_SEQ_BAT1 = 6,
109 AD7877_SEQ_BAT2 = 7,
110 AD7877_SEQ_TEMP1 = 8,
111 AD7877_SEQ_TEMP2 = 9,
112 AD7877_SEQ_Z1 = 10,
113 AD7877_NR_SENSE = 11,
114};
115
116/* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
117#define AD7877_DAC_CONF 0x1
118
119/* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
120#define AD7877_EXTW_GPIO_3_CONF 0x1C4
121#define AD7877_EXTW_GPIO_DATA 0x200
122
123/* Control REG 2 */
124#define AD7877_TMR(x) ((x & 0x3) << 0)
125#define AD7877_REF(x) ((x & 0x1) << 2)
126#define AD7877_POL(x) ((x & 0x1) << 3)
127#define AD7877_FCD(x) ((x & 0x3) << 4)
128#define AD7877_PM(x) ((x & 0x3) << 6)
129#define AD7877_ACQ(x) ((x & 0x3) << 8)
130#define AD7877_AVG(x) ((x & 0x3) << 10)
131
132/* Control REG 1 */
133#define AD7877_SER (1 << 11) /* non-differential */
134#define AD7877_DFR (0 << 11) /* differential */
135
136#define AD7877_MODE_NOC (0) /* Do not convert */
137#define AD7877_MODE_SCC (1) /* Single channel conversion */
138#define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
139#define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
140
141#define AD7877_CHANADD(x) ((x&0xF)<<7)
142#define AD7877_READADD(x) ((x)<<2)
143#define AD7877_WRITEADD(x) ((x)<<12)
144
145#define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
146 AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
147 AD7877_READADD(AD7877_REG_ ## x))
148
149#define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
150 AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
151
152/*
153 * Non-touchscreen sensors only use single-ended conversions.
154 */
155
156struct ser_req {
157 u16 reset;
158 u16 ref_on;
159 u16 command;
160 struct spi_message msg;
161 struct spi_transfer xfer[6];
162
163 /*
164 * DMA (thus cache coherency maintenance) requires the
165 * transfer buffers to live in their own cache lines.
166 */
167 u16 sample ____cacheline_aligned;
168};
169
170struct ad7877 {
171 struct input_dev *input;
172 char phys[32];
173
174 struct spi_device *spi;
175 u16 model;
176 u16 vref_delay_usecs;
177 u16 x_plate_ohms;
178 u16 pressure_max;
179
180 u16 cmd_crtl1;
181 u16 cmd_crtl2;
182 u16 cmd_dummy;
183 u16 dac;
184
185 u8 stopacq_polarity;
186 u8 first_conversion_delay;
187 u8 acquisition_time;
188 u8 averaging;
189 u8 pen_down_acc_interval;
190
191 struct spi_transfer xfer[AD7877_NR_SENSE + 2];
192 struct spi_message msg;
193
194 struct mutex mutex;
195 bool disabled; /* P: mutex */
196 bool gpio3; /* P: mutex */
197 bool gpio4; /* P: mutex */
198
199 spinlock_t lock;
200 struct timer_list timer; /* P: lock */
201
202 /*
203 * DMA (thus cache coherency maintenance) requires the
204 * transfer buffers to live in their own cache lines.
205 */
206 u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
207};
208
209static bool gpio3;
210module_param(gpio3, bool, 0);
211MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
212
213static int ad7877_read(struct spi_device *spi, u16 reg)
214{
215 struct ser_req *req;
216 int status, ret;
217
218 req = kzalloc(sizeof *req, GFP_KERNEL);
219 if (!req)
220 return -ENOMEM;
221
222 spi_message_init(&req->msg);
223
224 req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
225 AD7877_READADD(reg));
226 req->xfer[0].tx_buf = &req->command;
227 req->xfer[0].len = 2;
228 req->xfer[0].cs_change = 1;
229
230 req->xfer[1].rx_buf = &req->sample;
231 req->xfer[1].len = 2;
232
233 spi_message_add_tail(&req->xfer[0], &req->msg);
234 spi_message_add_tail(&req->xfer[1], &req->msg);
235
236 status = spi_sync(spi, &req->msg);
237 ret = status ? : req->sample;
238
239 kfree(req);
240
241 return ret;
242}
243
244static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
245{
246 struct ser_req *req;
247 int status;
248
249 req = kzalloc(sizeof *req, GFP_KERNEL);
250 if (!req)
251 return -ENOMEM;
252
253 spi_message_init(&req->msg);
254
255 req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
256 req->xfer[0].tx_buf = &req->command;
257 req->xfer[0].len = 2;
258
259 spi_message_add_tail(&req->xfer[0], &req->msg);
260
261 status = spi_sync(spi, &req->msg);
262
263 kfree(req);
264
265 return status;
266}
267
268static int ad7877_read_adc(struct spi_device *spi, unsigned command)
269{
270 struct ad7877 *ts = spi_get_drvdata(spi);
271 struct ser_req *req;
272 int status;
273 int sample;
274 int i;
275
276 req = kzalloc(sizeof *req, GFP_KERNEL);
277 if (!req)
278 return -ENOMEM;
279
280 spi_message_init(&req->msg);
281
282 /* activate reference, so it has time to settle; */
283 req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
284 AD7877_POL(ts->stopacq_polarity) |
285 AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
286 AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
287
288 req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
289
290 req->command = (u16) command;
291
292 req->xfer[0].tx_buf = &req->reset;
293 req->xfer[0].len = 2;
294 req->xfer[0].cs_change = 1;
295
296 req->xfer[1].tx_buf = &req->ref_on;
297 req->xfer[1].len = 2;
298 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
299 req->xfer[1].cs_change = 1;
300
301 req->xfer[2].tx_buf = &req->command;
302 req->xfer[2].len = 2;
303 req->xfer[2].delay_usecs = ts->vref_delay_usecs;
304 req->xfer[2].cs_change = 1;
305
306 req->xfer[3].rx_buf = &req->sample;
307 req->xfer[3].len = 2;
308 req->xfer[3].cs_change = 1;
309
310 req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
311 req->xfer[4].len = 2;
312 req->xfer[4].cs_change = 1;
313
314 req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
315 req->xfer[5].len = 2;
316
317 /* group all the transfers together, so we can't interfere with
318 * reading touchscreen state; disable penirq while sampling
319 */
320 for (i = 0; i < 6; i++)
321 spi_message_add_tail(&req->xfer[i], &req->msg);
322
323 status = spi_sync(spi, &req->msg);
324 sample = req->sample;
325
326 kfree(req);
327
328 return status ? : sample;
329}
330
331static int ad7877_process_data(struct ad7877 *ts)
332{
333 struct input_dev *input_dev = ts->input;
334 unsigned Rt;
335 u16 x, y, z1, z2;
336
337 x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
338 y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
339 z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
340 z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
341
342 /*
343 * The samples processed here are already preprocessed by the AD7877.
344 * The preprocessing function consists of an averaging filter.
345 * The combination of 'first conversion delay' and averaging provides a robust solution,
346 * discarding the spurious noise in the signal and keeping only the data of interest.
347 * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
348 * Other user-programmable conversion controls include variable acquisition time,
349 * and first conversion delay. Up to 16 averages can be taken per conversion.
350 */
351
352 if (likely(x && z1)) {
353 /* compute touch pressure resistance using equation #1 */
354 Rt = (z2 - z1) * x * ts->x_plate_ohms;
355 Rt /= z1;
356 Rt = (Rt + 2047) >> 12;
357
358 /*
359 * Sample found inconsistent, pressure is beyond
360 * the maximum. Don't report it to user space.
361 */
362 if (Rt > ts->pressure_max)
363 return -EINVAL;
364
365 if (!timer_pending(&ts->timer))
366 input_report_key(input_dev, BTN_TOUCH, 1);
367
368 input_report_abs(input_dev, ABS_X, x);
369 input_report_abs(input_dev, ABS_Y, y);
370 input_report_abs(input_dev, ABS_PRESSURE, Rt);
371 input_sync(input_dev);
372
373 return 0;
374 }
375
376 return -EINVAL;
377}
378
379static inline void ad7877_ts_event_release(struct ad7877 *ts)
380{
381 struct input_dev *input_dev = ts->input;
382
383 input_report_abs(input_dev, ABS_PRESSURE, 0);
384 input_report_key(input_dev, BTN_TOUCH, 0);
385 input_sync(input_dev);
386}
387
388static void ad7877_timer(struct timer_list *t)
389{
390 struct ad7877 *ts = from_timer(ts, t, timer);
391 unsigned long flags;
392
393 spin_lock_irqsave(&ts->lock, flags);
394 ad7877_ts_event_release(ts);
395 spin_unlock_irqrestore(&ts->lock, flags);
396}
397
398static irqreturn_t ad7877_irq(int irq, void *handle)
399{
400 struct ad7877 *ts = handle;
401 unsigned long flags;
402 int error;
403
404 error = spi_sync(ts->spi, &ts->msg);
405 if (error) {
406 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
407 goto out;
408 }
409
410 spin_lock_irqsave(&ts->lock, flags);
411 error = ad7877_process_data(ts);
412 if (!error)
413 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
414 spin_unlock_irqrestore(&ts->lock, flags);
415
416out:
417 return IRQ_HANDLED;
418}
419
420static void ad7877_disable(void *data)
421{
422 struct ad7877 *ts = data;
423
424 mutex_lock(&ts->mutex);
425
426 if (!ts->disabled) {
427 ts->disabled = true;
428 disable_irq(ts->spi->irq);
429
430 if (del_timer_sync(&ts->timer))
431 ad7877_ts_event_release(ts);
432 }
433
434 /*
435 * We know the chip's in lowpower mode since we always
436 * leave it that way after every request
437 */
438
439 mutex_unlock(&ts->mutex);
440}
441
442static void ad7877_enable(struct ad7877 *ts)
443{
444 mutex_lock(&ts->mutex);
445
446 if (ts->disabled) {
447 ts->disabled = false;
448 enable_irq(ts->spi->irq);
449 }
450
451 mutex_unlock(&ts->mutex);
452}
453
454#define SHOW(name) static ssize_t \
455name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
456{ \
457 struct ad7877 *ts = dev_get_drvdata(dev); \
458 ssize_t v = ad7877_read_adc(ts->spi, \
459 AD7877_READ_CHAN(name)); \
460 if (v < 0) \
461 return v; \
462 return sprintf(buf, "%u\n", (unsigned) v); \
463} \
464static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
465
466SHOW(aux1)
467SHOW(aux2)
468SHOW(aux3)
469SHOW(bat1)
470SHOW(bat2)
471SHOW(temp1)
472SHOW(temp2)
473
474static ssize_t ad7877_disable_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
477 struct ad7877 *ts = dev_get_drvdata(dev);
478
479 return sprintf(buf, "%u\n", ts->disabled);
480}
481
482static ssize_t ad7877_disable_store(struct device *dev,
483 struct device_attribute *attr,
484 const char *buf, size_t count)
485{
486 struct ad7877 *ts = dev_get_drvdata(dev);
487 unsigned int val;
488 int error;
489
490 error = kstrtouint(buf, 10, &val);
491 if (error)
492 return error;
493
494 if (val)
495 ad7877_disable(ts);
496 else
497 ad7877_enable(ts);
498
499 return count;
500}
501
502static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
503
504static ssize_t ad7877_dac_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
507 struct ad7877 *ts = dev_get_drvdata(dev);
508
509 return sprintf(buf, "%u\n", ts->dac);
510}
511
512static ssize_t ad7877_dac_store(struct device *dev,
513 struct device_attribute *attr,
514 const char *buf, size_t count)
515{
516 struct ad7877 *ts = dev_get_drvdata(dev);
517 unsigned int val;
518 int error;
519
520 error = kstrtouint(buf, 10, &val);
521 if (error)
522 return error;
523
524 mutex_lock(&ts->mutex);
525 ts->dac = val & 0xFF;
526 ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
527 mutex_unlock(&ts->mutex);
528
529 return count;
530}
531
532static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
533
534static ssize_t ad7877_gpio3_show(struct device *dev,
535 struct device_attribute *attr, char *buf)
536{
537 struct ad7877 *ts = dev_get_drvdata(dev);
538
539 return sprintf(buf, "%u\n", ts->gpio3);
540}
541
542static ssize_t ad7877_gpio3_store(struct device *dev,
543 struct device_attribute *attr,
544 const char *buf, size_t count)
545{
546 struct ad7877 *ts = dev_get_drvdata(dev);
547 unsigned int val;
548 int error;
549
550 error = kstrtouint(buf, 10, &val);
551 if (error)
552 return error;
553
554 mutex_lock(&ts->mutex);
555 ts->gpio3 = !!val;
556 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
557 (ts->gpio4 << 4) | (ts->gpio3 << 5));
558 mutex_unlock(&ts->mutex);
559
560 return count;
561}
562
563static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
564
565static ssize_t ad7877_gpio4_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
568 struct ad7877 *ts = dev_get_drvdata(dev);
569
570 return sprintf(buf, "%u\n", ts->gpio4);
571}
572
573static ssize_t ad7877_gpio4_store(struct device *dev,
574 struct device_attribute *attr,
575 const char *buf, size_t count)
576{
577 struct ad7877 *ts = dev_get_drvdata(dev);
578 unsigned int val;
579 int error;
580
581 error = kstrtouint(buf, 10, &val);
582 if (error)
583 return error;
584
585 mutex_lock(&ts->mutex);
586 ts->gpio4 = !!val;
587 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
588 (ts->gpio4 << 4) | (ts->gpio3 << 5));
589 mutex_unlock(&ts->mutex);
590
591 return count;
592}
593
594static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
595
596static struct attribute *ad7877_attributes[] = {
597 &dev_attr_temp1.attr,
598 &dev_attr_temp2.attr,
599 &dev_attr_aux1.attr,
600 &dev_attr_aux2.attr,
601 &dev_attr_aux3.attr,
602 &dev_attr_bat1.attr,
603 &dev_attr_bat2.attr,
604 &dev_attr_disable.attr,
605 &dev_attr_dac.attr,
606 &dev_attr_gpio3.attr,
607 &dev_attr_gpio4.attr,
608 NULL
609};
610
611static umode_t ad7877_attr_is_visible(struct kobject *kobj,
612 struct attribute *attr, int n)
613{
614 umode_t mode = attr->mode;
615
616 if (attr == &dev_attr_aux3.attr) {
617 if (gpio3)
618 mode = 0;
619 } else if (attr == &dev_attr_gpio3.attr) {
620 if (!gpio3)
621 mode = 0;
622 }
623
624 return mode;
625}
626
627static const struct attribute_group ad7877_attr_group = {
628 .is_visible = ad7877_attr_is_visible,
629 .attrs = ad7877_attributes,
630};
631
632static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
633{
634 struct spi_message *m;
635 int i;
636
637 ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
638 AD7877_POL(ts->stopacq_polarity) |
639 AD7877_AVG(ts->averaging) | AD7877_PM(1) |
640 AD7877_TMR(ts->pen_down_acc_interval) |
641 AD7877_ACQ(ts->acquisition_time) |
642 AD7877_FCD(ts->first_conversion_delay);
643
644 ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
645
646 ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
647 AD7877_READADD(AD7877_REG_XPLUS-1) |
648 AD7877_MODE_SEQ1 | AD7877_DFR;
649
650 ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
651
652 ts->cmd_dummy = 0;
653
654 m = &ts->msg;
655
656 spi_message_init(m);
657
658 m->context = ts;
659
660 ts->xfer[0].tx_buf = &ts->cmd_crtl1;
661 ts->xfer[0].len = 2;
662 ts->xfer[0].cs_change = 1;
663
664 spi_message_add_tail(&ts->xfer[0], m);
665
666 ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
667 ts->xfer[1].len = 2;
668 ts->xfer[1].cs_change = 1;
669
670 spi_message_add_tail(&ts->xfer[1], m);
671
672 for (i = 0; i < AD7877_NR_SENSE; i++) {
673 ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
674 ts->xfer[i + 2].len = 2;
675 if (i < (AD7877_NR_SENSE - 1))
676 ts->xfer[i + 2].cs_change = 1;
677 spi_message_add_tail(&ts->xfer[i + 2], m);
678 }
679}
680
681static int ad7877_probe(struct spi_device *spi)
682{
683 struct ad7877 *ts;
684 struct input_dev *input_dev;
685 struct ad7877_platform_data *pdata = dev_get_platdata(&spi->dev);
686 int err;
687 u16 verify;
688
689 if (!spi->irq) {
690 dev_dbg(&spi->dev, "no IRQ?\n");
691 return -ENODEV;
692 }
693
694 if (!pdata) {
695 dev_dbg(&spi->dev, "no platform data?\n");
696 return -ENODEV;
697 }
698
699 /* don't exceed max specified SPI CLK frequency */
700 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
701 dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
702 return -EINVAL;
703 }
704
705 spi->bits_per_word = 16;
706 err = spi_setup(spi);
707 if (err) {
708 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
709 return err;
710 }
711
712 ts = devm_kzalloc(&spi->dev, sizeof(struct ad7877), GFP_KERNEL);
713 if (!ts)
714 return -ENOMEM;
715
716 input_dev = devm_input_allocate_device(&spi->dev);
717 if (!input_dev)
718 return -ENOMEM;
719
720 err = devm_add_action_or_reset(&spi->dev, ad7877_disable, ts);
721 if (err)
722 return err;
723
724 spi_set_drvdata(spi, ts);
725 ts->spi = spi;
726 ts->input = input_dev;
727
728 timer_setup(&ts->timer, ad7877_timer, 0);
729 mutex_init(&ts->mutex);
730 spin_lock_init(&ts->lock);
731
732 ts->model = pdata->model ? : 7877;
733 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
734 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
735 ts->pressure_max = pdata->pressure_max ? : ~0;
736
737 ts->stopacq_polarity = pdata->stopacq_polarity;
738 ts->first_conversion_delay = pdata->first_conversion_delay;
739 ts->acquisition_time = pdata->acquisition_time;
740 ts->averaging = pdata->averaging;
741 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
742
743 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
744
745 input_dev->name = "AD7877 Touchscreen";
746 input_dev->phys = ts->phys;
747 input_dev->dev.parent = &spi->dev;
748
749 __set_bit(EV_KEY, input_dev->evbit);
750 __set_bit(BTN_TOUCH, input_dev->keybit);
751 __set_bit(EV_ABS, input_dev->evbit);
752 __set_bit(ABS_X, input_dev->absbit);
753 __set_bit(ABS_Y, input_dev->absbit);
754 __set_bit(ABS_PRESSURE, input_dev->absbit);
755
756 input_set_abs_params(input_dev, ABS_X,
757 pdata->x_min ? : 0,
758 pdata->x_max ? : MAX_12BIT,
759 0, 0);
760 input_set_abs_params(input_dev, ABS_Y,
761 pdata->y_min ? : 0,
762 pdata->y_max ? : MAX_12BIT,
763 0, 0);
764 input_set_abs_params(input_dev, ABS_PRESSURE,
765 pdata->pressure_min, pdata->pressure_max, 0, 0);
766
767 ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
768
769 verify = ad7877_read(spi, AD7877_REG_SEQ1);
770
771 if (verify != AD7877_MM_SEQUENCE) {
772 dev_err(&spi->dev, "%s: Failed to probe %s\n",
773 dev_name(&spi->dev), input_dev->name);
774 return -ENODEV;
775 }
776
777 if (gpio3)
778 ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
779
780 ad7877_setup_ts_def_msg(spi, ts);
781
782 /* Request AD7877 /DAV GPIO interrupt */
783
784 err = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, ad7877_irq,
785 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
786 spi->dev.driver->name, ts);
787 if (err) {
788 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
789 return err;
790 }
791
792 err = devm_device_add_group(&spi->dev, &ad7877_attr_group);
793 if (err)
794 return err;
795
796 err = input_register_device(input_dev);
797 if (err)
798 return err;
799
800 return 0;
801}
802
803static int __maybe_unused ad7877_suspend(struct device *dev)
804{
805 struct ad7877 *ts = dev_get_drvdata(dev);
806
807 ad7877_disable(ts);
808
809 return 0;
810}
811
812static int __maybe_unused ad7877_resume(struct device *dev)
813{
814 struct ad7877 *ts = dev_get_drvdata(dev);
815
816 ad7877_enable(ts);
817
818 return 0;
819}
820
821static SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
822
823static struct spi_driver ad7877_driver = {
824 .driver = {
825 .name = "ad7877",
826 .pm = &ad7877_pm,
827 },
828 .probe = ad7877_probe,
829};
830
831module_spi_driver(ad7877_driver);
832
833MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
834MODULE_DESCRIPTION("AD7877 touchscreen Driver");
835MODULE_LICENSE("GPL");
836MODULE_ALIAS("spi:ad7877");