1b7b06f8bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2331b78edSMichael Hennerich /*
3331b78edSMichael Hennerich * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
4331b78edSMichael Hennerich *
5331b78edSMichael Hennerich * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
6331b78edSMichael Hennerich * Based on: ads7846.c
7331b78edSMichael Hennerich *
8331b78edSMichael Hennerich * Bugs: Enter bugs at http://blackfin.uclinux.org/
9331b78edSMichael Hennerich *
10331b78edSMichael Hennerich * History:
11331b78edSMichael Hennerich * Copyright (c) 2005 David Brownell
12331b78edSMichael Hennerich * Copyright (c) 2006 Nokia Corporation
13331b78edSMichael Hennerich * Various changes: Imre Deak <imre.deak@nokia.com>
14331b78edSMichael Hennerich *
15331b78edSMichael Hennerich * Using code from:
16331b78edSMichael Hennerich * - corgi_ts.c
17331b78edSMichael Hennerich * Copyright (C) 2004-2005 Richard Purdie
18331b78edSMichael Hennerich * - omap_ts.[hc], ads7846.h, ts_osk.c
19331b78edSMichael Hennerich * Copyright (C) 2002 MontaVista Software
20331b78edSMichael Hennerich * Copyright (C) 2004 Texas Instruments
21331b78edSMichael Hennerich * Copyright (C) 2005 Dirk Behme
22331b78edSMichael Hennerich */
23331b78edSMichael Hennerich
24331b78edSMichael Hennerich
25331b78edSMichael Hennerich #include <linux/device.h>
26331b78edSMichael Hennerich #include <linux/delay.h>
27331b78edSMichael Hennerich #include <linux/input.h>
28331b78edSMichael Hennerich #include <linux/interrupt.h>
29fccdd7c4SMark Brown #include <linux/pm.h>
30331b78edSMichael Hennerich #include <linux/slab.h>
31331b78edSMichael Hennerich #include <linux/spi/spi.h>
32331b78edSMichael Hennerich #include <linux/spi/ad7877.h>
33d2d8442dSPaul Gortmaker #include <linux/module.h>
34331b78edSMichael Hennerich #include <asm/irq.h>
35331b78edSMichael Hennerich
364eb6f91bSMichael Hennerich #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
37331b78edSMichael Hennerich
38331b78edSMichael Hennerich #define MAX_SPI_FREQ_HZ 20000000
39331b78edSMichael Hennerich #define MAX_12BIT ((1<<12)-1)
40331b78edSMichael Hennerich
41331b78edSMichael Hennerich #define AD7877_REG_ZEROS 0
42331b78edSMichael Hennerich #define AD7877_REG_CTRL1 1
43331b78edSMichael Hennerich #define AD7877_REG_CTRL2 2
44331b78edSMichael Hennerich #define AD7877_REG_ALERT 3
45331b78edSMichael Hennerich #define AD7877_REG_AUX1HIGH 4
46331b78edSMichael Hennerich #define AD7877_REG_AUX1LOW 5
47331b78edSMichael Hennerich #define AD7877_REG_BAT1HIGH 6
48331b78edSMichael Hennerich #define AD7877_REG_BAT1LOW 7
49331b78edSMichael Hennerich #define AD7877_REG_BAT2HIGH 8
50331b78edSMichael Hennerich #define AD7877_REG_BAT2LOW 9
51331b78edSMichael Hennerich #define AD7877_REG_TEMP1HIGH 10
52331b78edSMichael Hennerich #define AD7877_REG_TEMP1LOW 11
53331b78edSMichael Hennerich #define AD7877_REG_SEQ0 12
54331b78edSMichael Hennerich #define AD7877_REG_SEQ1 13
55331b78edSMichael Hennerich #define AD7877_REG_DAC 14
56331b78edSMichael Hennerich #define AD7877_REG_NONE1 15
57331b78edSMichael Hennerich #define AD7877_REG_EXTWRITE 15
58331b78edSMichael Hennerich #define AD7877_REG_XPLUS 16
59331b78edSMichael Hennerich #define AD7877_REG_YPLUS 17
60331b78edSMichael Hennerich #define AD7877_REG_Z2 18
61331b78edSMichael Hennerich #define AD7877_REG_aux1 19
62331b78edSMichael Hennerich #define AD7877_REG_aux2 20
63331b78edSMichael Hennerich #define AD7877_REG_aux3 21
64331b78edSMichael Hennerich #define AD7877_REG_bat1 22
65331b78edSMichael Hennerich #define AD7877_REG_bat2 23
66331b78edSMichael Hennerich #define AD7877_REG_temp1 24
67331b78edSMichael Hennerich #define AD7877_REG_temp2 25
68331b78edSMichael Hennerich #define AD7877_REG_Z1 26
69331b78edSMichael Hennerich #define AD7877_REG_GPIOCTRL1 27
70331b78edSMichael Hennerich #define AD7877_REG_GPIOCTRL2 28
71331b78edSMichael Hennerich #define AD7877_REG_GPIODATA 29
72331b78edSMichael Hennerich #define AD7877_REG_NONE2 30
73331b78edSMichael Hennerich #define AD7877_REG_NONE3 31
74331b78edSMichael Hennerich
75331b78edSMichael Hennerich #define AD7877_SEQ_YPLUS_BIT (1<<11)
76331b78edSMichael Hennerich #define AD7877_SEQ_XPLUS_BIT (1<<10)
77331b78edSMichael Hennerich #define AD7877_SEQ_Z2_BIT (1<<9)
78331b78edSMichael Hennerich #define AD7877_SEQ_AUX1_BIT (1<<8)
79331b78edSMichael Hennerich #define AD7877_SEQ_AUX2_BIT (1<<7)
80331b78edSMichael Hennerich #define AD7877_SEQ_AUX3_BIT (1<<6)
81331b78edSMichael Hennerich #define AD7877_SEQ_BAT1_BIT (1<<5)
82331b78edSMichael Hennerich #define AD7877_SEQ_BAT2_BIT (1<<4)
83331b78edSMichael Hennerich #define AD7877_SEQ_TEMP1_BIT (1<<3)
84331b78edSMichael Hennerich #define AD7877_SEQ_TEMP2_BIT (1<<2)
85331b78edSMichael Hennerich #define AD7877_SEQ_Z1_BIT (1<<1)
86331b78edSMichael Hennerich
87331b78edSMichael Hennerich enum {
88331b78edSMichael Hennerich AD7877_SEQ_YPOS = 0,
89331b78edSMichael Hennerich AD7877_SEQ_XPOS = 1,
90331b78edSMichael Hennerich AD7877_SEQ_Z2 = 2,
91331b78edSMichael Hennerich AD7877_SEQ_AUX1 = 3,
92331b78edSMichael Hennerich AD7877_SEQ_AUX2 = 4,
93331b78edSMichael Hennerich AD7877_SEQ_AUX3 = 5,
94331b78edSMichael Hennerich AD7877_SEQ_BAT1 = 6,
95331b78edSMichael Hennerich AD7877_SEQ_BAT2 = 7,
96331b78edSMichael Hennerich AD7877_SEQ_TEMP1 = 8,
97331b78edSMichael Hennerich AD7877_SEQ_TEMP2 = 9,
98331b78edSMichael Hennerich AD7877_SEQ_Z1 = 10,
99331b78edSMichael Hennerich AD7877_NR_SENSE = 11,
100331b78edSMichael Hennerich };
101331b78edSMichael Hennerich
102331b78edSMichael Hennerich /* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
103331b78edSMichael Hennerich #define AD7877_DAC_CONF 0x1
104331b78edSMichael Hennerich
105331b78edSMichael Hennerich /* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
106331b78edSMichael Hennerich #define AD7877_EXTW_GPIO_3_CONF 0x1C4
107331b78edSMichael Hennerich #define AD7877_EXTW_GPIO_DATA 0x200
108331b78edSMichael Hennerich
109331b78edSMichael Hennerich /* Control REG 2 */
110331b78edSMichael Hennerich #define AD7877_TMR(x) ((x & 0x3) << 0)
111331b78edSMichael Hennerich #define AD7877_REF(x) ((x & 0x1) << 2)
112331b78edSMichael Hennerich #define AD7877_POL(x) ((x & 0x1) << 3)
113331b78edSMichael Hennerich #define AD7877_FCD(x) ((x & 0x3) << 4)
114331b78edSMichael Hennerich #define AD7877_PM(x) ((x & 0x3) << 6)
115331b78edSMichael Hennerich #define AD7877_ACQ(x) ((x & 0x3) << 8)
116331b78edSMichael Hennerich #define AD7877_AVG(x) ((x & 0x3) << 10)
117331b78edSMichael Hennerich
118331b78edSMichael Hennerich /* Control REG 1 */
119331b78edSMichael Hennerich #define AD7877_SER (1 << 11) /* non-differential */
120331b78edSMichael Hennerich #define AD7877_DFR (0 << 11) /* differential */
121331b78edSMichael Hennerich
122331b78edSMichael Hennerich #define AD7877_MODE_NOC (0) /* Do not convert */
123331b78edSMichael Hennerich #define AD7877_MODE_SCC (1) /* Single channel conversion */
124331b78edSMichael Hennerich #define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
125331b78edSMichael Hennerich #define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
126331b78edSMichael Hennerich
127331b78edSMichael Hennerich #define AD7877_CHANADD(x) ((x&0xF)<<7)
128331b78edSMichael Hennerich #define AD7877_READADD(x) ((x)<<2)
129331b78edSMichael Hennerich #define AD7877_WRITEADD(x) ((x)<<12)
130331b78edSMichael Hennerich
131331b78edSMichael Hennerich #define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
132331b78edSMichael Hennerich AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
133331b78edSMichael Hennerich AD7877_READADD(AD7877_REG_ ## x))
134331b78edSMichael Hennerich
135331b78edSMichael Hennerich #define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
136331b78edSMichael Hennerich AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
137331b78edSMichael Hennerich
138331b78edSMichael Hennerich /*
139331b78edSMichael Hennerich * Non-touchscreen sensors only use single-ended conversions.
140331b78edSMichael Hennerich */
141331b78edSMichael Hennerich
142331b78edSMichael Hennerich struct ser_req {
143331b78edSMichael Hennerich u16 reset;
144331b78edSMichael Hennerich u16 ref_on;
145331b78edSMichael Hennerich u16 command;
146331b78edSMichael Hennerich struct spi_message msg;
147331b78edSMichael Hennerich struct spi_transfer xfer[6];
1483843384aSOskar Schirmer
1493843384aSOskar Schirmer /*
1503843384aSOskar Schirmer * DMA (thus cache coherency maintenance) requires the
1513843384aSOskar Schirmer * transfer buffers to live in their own cache lines.
1523843384aSOskar Schirmer */
1533843384aSOskar Schirmer u16 sample ____cacheline_aligned;
154331b78edSMichael Hennerich };
155331b78edSMichael Hennerich
156331b78edSMichael Hennerich struct ad7877 {
157331b78edSMichael Hennerich struct input_dev *input;
158331b78edSMichael Hennerich char phys[32];
159331b78edSMichael Hennerich
160331b78edSMichael Hennerich struct spi_device *spi;
161331b78edSMichael Hennerich u16 model;
162331b78edSMichael Hennerich u16 vref_delay_usecs;
163331b78edSMichael Hennerich u16 x_plate_ohms;
164331b78edSMichael Hennerich u16 pressure_max;
165331b78edSMichael Hennerich
166331b78edSMichael Hennerich u16 cmd_crtl1;
167331b78edSMichael Hennerich u16 cmd_crtl2;
168331b78edSMichael Hennerich u16 cmd_dummy;
169331b78edSMichael Hennerich u16 dac;
170331b78edSMichael Hennerich
171331b78edSMichael Hennerich u8 stopacq_polarity;
172331b78edSMichael Hennerich u8 first_conversion_delay;
173331b78edSMichael Hennerich u8 acquisition_time;
174331b78edSMichael Hennerich u8 averaging;
175331b78edSMichael Hennerich u8 pen_down_acc_interval;
176331b78edSMichael Hennerich
177331b78edSMichael Hennerich struct spi_transfer xfer[AD7877_NR_SENSE + 2];
178331b78edSMichael Hennerich struct spi_message msg;
179331b78edSMichael Hennerich
180331b78edSMichael Hennerich struct mutex mutex;
181b534422bSDmitry Torokhov bool disabled; /* P: mutex */
182b534422bSDmitry Torokhov bool gpio3; /* P: mutex */
183b534422bSDmitry Torokhov bool gpio4; /* P: mutex */
184331b78edSMichael Hennerich
185331b78edSMichael Hennerich spinlock_t lock;
186331b78edSMichael Hennerich struct timer_list timer; /* P: lock */
1873843384aSOskar Schirmer
1883843384aSOskar Schirmer /*
1893843384aSOskar Schirmer * DMA (thus cache coherency maintenance) requires the
1903843384aSOskar Schirmer * transfer buffers to live in their own cache lines.
1913843384aSOskar Schirmer */
1923843384aSOskar Schirmer u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
193331b78edSMichael Hennerich };
194331b78edSMichael Hennerich
1952fd18abaSDmitry Torokhov static bool gpio3;
1962fd18abaSDmitry Torokhov module_param(gpio3, bool, 0);
197331b78edSMichael Hennerich MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
198331b78edSMichael Hennerich
ad7877_read(struct spi_device * spi,u16 reg)199331b78edSMichael Hennerich static int ad7877_read(struct spi_device *spi, u16 reg)
200331b78edSMichael Hennerich {
201331b78edSMichael Hennerich struct ser_req *req;
202331b78edSMichael Hennerich int status, ret;
203331b78edSMichael Hennerich
204331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL);
205331b78edSMichael Hennerich if (!req)
206331b78edSMichael Hennerich return -ENOMEM;
207331b78edSMichael Hennerich
208331b78edSMichael Hennerich spi_message_init(&req->msg);
209331b78edSMichael Hennerich
210331b78edSMichael Hennerich req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
211331b78edSMichael Hennerich AD7877_READADD(reg));
212331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->command;
213331b78edSMichael Hennerich req->xfer[0].len = 2;
214e92c27fbSMichael Hennerich req->xfer[0].cs_change = 1;
215331b78edSMichael Hennerich
216331b78edSMichael Hennerich req->xfer[1].rx_buf = &req->sample;
217331b78edSMichael Hennerich req->xfer[1].len = 2;
218331b78edSMichael Hennerich
219331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[0], &req->msg);
220331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[1], &req->msg);
221331b78edSMichael Hennerich
222331b78edSMichael Hennerich status = spi_sync(spi, &req->msg);
223331b78edSMichael Hennerich ret = status ? : req->sample;
224331b78edSMichael Hennerich
225331b78edSMichael Hennerich kfree(req);
226331b78edSMichael Hennerich
227331b78edSMichael Hennerich return ret;
228331b78edSMichael Hennerich }
229331b78edSMichael Hennerich
ad7877_write(struct spi_device * spi,u16 reg,u16 val)230331b78edSMichael Hennerich static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
231331b78edSMichael Hennerich {
232331b78edSMichael Hennerich struct ser_req *req;
233331b78edSMichael Hennerich int status;
234331b78edSMichael Hennerich
235331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL);
236331b78edSMichael Hennerich if (!req)
237331b78edSMichael Hennerich return -ENOMEM;
238331b78edSMichael Hennerich
239331b78edSMichael Hennerich spi_message_init(&req->msg);
240331b78edSMichael Hennerich
241331b78edSMichael Hennerich req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
242331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->command;
243331b78edSMichael Hennerich req->xfer[0].len = 2;
244331b78edSMichael Hennerich
245331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[0], &req->msg);
246331b78edSMichael Hennerich
247331b78edSMichael Hennerich status = spi_sync(spi, &req->msg);
248331b78edSMichael Hennerich
249331b78edSMichael Hennerich kfree(req);
250331b78edSMichael Hennerich
251331b78edSMichael Hennerich return status;
252331b78edSMichael Hennerich }
253331b78edSMichael Hennerich
ad7877_read_adc(struct spi_device * spi,unsigned command)254331b78edSMichael Hennerich static int ad7877_read_adc(struct spi_device *spi, unsigned command)
255331b78edSMichael Hennerich {
256e1793c69SJingoo Han struct ad7877 *ts = spi_get_drvdata(spi);
257331b78edSMichael Hennerich struct ser_req *req;
258331b78edSMichael Hennerich int status;
259331b78edSMichael Hennerich int sample;
260331b78edSMichael Hennerich int i;
261331b78edSMichael Hennerich
262331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL);
263331b78edSMichael Hennerich if (!req)
264331b78edSMichael Hennerich return -ENOMEM;
265331b78edSMichael Hennerich
266331b78edSMichael Hennerich spi_message_init(&req->msg);
267331b78edSMichael Hennerich
268331b78edSMichael Hennerich /* activate reference, so it has time to settle; */
269331b78edSMichael Hennerich req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
270331b78edSMichael Hennerich AD7877_POL(ts->stopacq_polarity) |
271331b78edSMichael Hennerich AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
272331b78edSMichael Hennerich AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
273331b78edSMichael Hennerich
274331b78edSMichael Hennerich req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
275331b78edSMichael Hennerich
276331b78edSMichael Hennerich req->command = (u16) command;
277331b78edSMichael Hennerich
278331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->reset;
279331b78edSMichael Hennerich req->xfer[0].len = 2;
280e92c27fbSMichael Hennerich req->xfer[0].cs_change = 1;
281331b78edSMichael Hennerich
282331b78edSMichael Hennerich req->xfer[1].tx_buf = &req->ref_on;
283331b78edSMichael Hennerich req->xfer[1].len = 2;
2846782b5daSSergiu Cuciurean req->xfer[1].delay.value = ts->vref_delay_usecs;
2856782b5daSSergiu Cuciurean req->xfer[1].delay.unit = SPI_DELAY_UNIT_USECS;
286e92c27fbSMichael Hennerich req->xfer[1].cs_change = 1;
287331b78edSMichael Hennerich
288331b78edSMichael Hennerich req->xfer[2].tx_buf = &req->command;
289331b78edSMichael Hennerich req->xfer[2].len = 2;
2906782b5daSSergiu Cuciurean req->xfer[2].delay.value = ts->vref_delay_usecs;
2916782b5daSSergiu Cuciurean req->xfer[2].delay.unit = SPI_DELAY_UNIT_USECS;
292e92c27fbSMichael Hennerich req->xfer[2].cs_change = 1;
293331b78edSMichael Hennerich
294331b78edSMichael Hennerich req->xfer[3].rx_buf = &req->sample;
295331b78edSMichael Hennerich req->xfer[3].len = 2;
296e92c27fbSMichael Hennerich req->xfer[3].cs_change = 1;
297331b78edSMichael Hennerich
298331b78edSMichael Hennerich req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
299331b78edSMichael Hennerich req->xfer[4].len = 2;
300e92c27fbSMichael Hennerich req->xfer[4].cs_change = 1;
301331b78edSMichael Hennerich
302331b78edSMichael Hennerich req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
303331b78edSMichael Hennerich req->xfer[5].len = 2;
304331b78edSMichael Hennerich
305331b78edSMichael Hennerich /* group all the transfers together, so we can't interfere with
306331b78edSMichael Hennerich * reading touchscreen state; disable penirq while sampling
307331b78edSMichael Hennerich */
308331b78edSMichael Hennerich for (i = 0; i < 6; i++)
309331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[i], &req->msg);
310331b78edSMichael Hennerich
311331b78edSMichael Hennerich status = spi_sync(spi, &req->msg);
312331b78edSMichael Hennerich sample = req->sample;
313331b78edSMichael Hennerich
314331b78edSMichael Hennerich kfree(req);
315331b78edSMichael Hennerich
316331b78edSMichael Hennerich return status ? : sample;
317331b78edSMichael Hennerich }
318331b78edSMichael Hennerich
ad7877_process_data(struct ad7877 * ts)319b534422bSDmitry Torokhov static int ad7877_process_data(struct ad7877 *ts)
320331b78edSMichael Hennerich {
321331b78edSMichael Hennerich struct input_dev *input_dev = ts->input;
322331b78edSMichael Hennerich unsigned Rt;
323331b78edSMichael Hennerich u16 x, y, z1, z2;
324331b78edSMichael Hennerich
325331b78edSMichael Hennerich x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
326331b78edSMichael Hennerich y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
327331b78edSMichael Hennerich z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
328331b78edSMichael Hennerich z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
329331b78edSMichael Hennerich
330331b78edSMichael Hennerich /*
331331b78edSMichael Hennerich * The samples processed here are already preprocessed by the AD7877.
332331b78edSMichael Hennerich * The preprocessing function consists of an averaging filter.
333331b78edSMichael Hennerich * The combination of 'first conversion delay' and averaging provides a robust solution,
334331b78edSMichael Hennerich * discarding the spurious noise in the signal and keeping only the data of interest.
335331b78edSMichael Hennerich * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
336331b78edSMichael Hennerich * Other user-programmable conversion controls include variable acquisition time,
337331b78edSMichael Hennerich * and first conversion delay. Up to 16 averages can be taken per conversion.
338331b78edSMichael Hennerich */
339331b78edSMichael Hennerich
340331b78edSMichael Hennerich if (likely(x && z1)) {
341331b78edSMichael Hennerich /* compute touch pressure resistance using equation #1 */
342331b78edSMichael Hennerich Rt = (z2 - z1) * x * ts->x_plate_ohms;
343331b78edSMichael Hennerich Rt /= z1;
344331b78edSMichael Hennerich Rt = (Rt + 2047) >> 12;
345331b78edSMichael Hennerich
3461d02ad43SMichael Hennerich /*
3471d02ad43SMichael Hennerich * Sample found inconsistent, pressure is beyond
3481d02ad43SMichael Hennerich * the maximum. Don't report it to user space.
3491d02ad43SMichael Hennerich */
3501d02ad43SMichael Hennerich if (Rt > ts->pressure_max)
3511d02ad43SMichael Hennerich return -EINVAL;
3521d02ad43SMichael Hennerich
35347026b25SMichael Hennerich if (!timer_pending(&ts->timer))
35447026b25SMichael Hennerich input_report_key(input_dev, BTN_TOUCH, 1);
35547026b25SMichael Hennerich
356331b78edSMichael Hennerich input_report_abs(input_dev, ABS_X, x);
357331b78edSMichael Hennerich input_report_abs(input_dev, ABS_Y, y);
358331b78edSMichael Hennerich input_report_abs(input_dev, ABS_PRESSURE, Rt);
359331b78edSMichael Hennerich input_sync(input_dev);
360b534422bSDmitry Torokhov
36147026b25SMichael Hennerich return 0;
362331b78edSMichael Hennerich }
36347026b25SMichael Hennerich
36447026b25SMichael Hennerich return -EINVAL;
365331b78edSMichael Hennerich }
366331b78edSMichael Hennerich
ad7877_ts_event_release(struct ad7877 * ts)367331b78edSMichael Hennerich static inline void ad7877_ts_event_release(struct ad7877 *ts)
368331b78edSMichael Hennerich {
369331b78edSMichael Hennerich struct input_dev *input_dev = ts->input;
370331b78edSMichael Hennerich
371331b78edSMichael Hennerich input_report_abs(input_dev, ABS_PRESSURE, 0);
37247026b25SMichael Hennerich input_report_key(input_dev, BTN_TOUCH, 0);
373331b78edSMichael Hennerich input_sync(input_dev);
374331b78edSMichael Hennerich }
375331b78edSMichael Hennerich
ad7877_timer(struct timer_list * t)3768446b32bSstephen lu static void ad7877_timer(struct timer_list *t)
377331b78edSMichael Hennerich {
3788446b32bSstephen lu struct ad7877 *ts = from_timer(ts, t, timer);
379b534422bSDmitry Torokhov unsigned long flags;
380331b78edSMichael Hennerich
381b534422bSDmitry Torokhov spin_lock_irqsave(&ts->lock, flags);
382331b78edSMichael Hennerich ad7877_ts_event_release(ts);
383b534422bSDmitry Torokhov spin_unlock_irqrestore(&ts->lock, flags);
384331b78edSMichael Hennerich }
385331b78edSMichael Hennerich
ad7877_irq(int irq,void * handle)386331b78edSMichael Hennerich static irqreturn_t ad7877_irq(int irq, void *handle)
387331b78edSMichael Hennerich {
388331b78edSMichael Hennerich struct ad7877 *ts = handle;
389331b78edSMichael Hennerich unsigned long flags;
390b534422bSDmitry Torokhov int error;
391331b78edSMichael Hennerich
392b534422bSDmitry Torokhov error = spi_sync(ts->spi, &ts->msg);
393b534422bSDmitry Torokhov if (error) {
394b534422bSDmitry Torokhov dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
395b534422bSDmitry Torokhov goto out;
396b534422bSDmitry Torokhov }
397331b78edSMichael Hennerich
398331b78edSMichael Hennerich spin_lock_irqsave(&ts->lock, flags);
399b534422bSDmitry Torokhov error = ad7877_process_data(ts);
400b534422bSDmitry Torokhov if (!error)
401b534422bSDmitry Torokhov mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
402331b78edSMichael Hennerich spin_unlock_irqrestore(&ts->lock, flags);
403331b78edSMichael Hennerich
404b534422bSDmitry Torokhov out:
405331b78edSMichael Hennerich return IRQ_HANDLED;
406331b78edSMichael Hennerich }
407331b78edSMichael Hennerich
ad7877_disable(void * data)408fbe6a536SAndi Shyti static void ad7877_disable(void *data)
409331b78edSMichael Hennerich {
410fbe6a536SAndi Shyti struct ad7877 *ts = data;
411fbe6a536SAndi Shyti
412331b78edSMichael Hennerich mutex_lock(&ts->mutex);
413331b78edSMichael Hennerich
414331b78edSMichael Hennerich if (!ts->disabled) {
415b534422bSDmitry Torokhov ts->disabled = true;
416331b78edSMichael Hennerich disable_irq(ts->spi->irq);
417331b78edSMichael Hennerich
418331b78edSMichael Hennerich if (del_timer_sync(&ts->timer))
419331b78edSMichael Hennerich ad7877_ts_event_release(ts);
420331b78edSMichael Hennerich }
421331b78edSMichael Hennerich
422b534422bSDmitry Torokhov /*
423b534422bSDmitry Torokhov * We know the chip's in lowpower mode since we always
424331b78edSMichael Hennerich * leave it that way after every request
425331b78edSMichael Hennerich */
426331b78edSMichael Hennerich
427331b78edSMichael Hennerich mutex_unlock(&ts->mutex);
428331b78edSMichael Hennerich }
429331b78edSMichael Hennerich
ad7877_enable(struct ad7877 * ts)430331b78edSMichael Hennerich static void ad7877_enable(struct ad7877 *ts)
431331b78edSMichael Hennerich {
432331b78edSMichael Hennerich mutex_lock(&ts->mutex);
433331b78edSMichael Hennerich
434331b78edSMichael Hennerich if (ts->disabled) {
435b534422bSDmitry Torokhov ts->disabled = false;
436331b78edSMichael Hennerich enable_irq(ts->spi->irq);
437331b78edSMichael Hennerich }
438331b78edSMichael Hennerich
439331b78edSMichael Hennerich mutex_unlock(&ts->mutex);
440331b78edSMichael Hennerich }
441331b78edSMichael Hennerich
442331b78edSMichael Hennerich #define SHOW(name) static ssize_t \
443331b78edSMichael Hennerich name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
444331b78edSMichael Hennerich { \
445331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); \
446331b78edSMichael Hennerich ssize_t v = ad7877_read_adc(ts->spi, \
447331b78edSMichael Hennerich AD7877_READ_CHAN(name)); \
448331b78edSMichael Hennerich if (v < 0) \
449331b78edSMichael Hennerich return v; \
450331b78edSMichael Hennerich return sprintf(buf, "%u\n", (unsigned) v); \
451331b78edSMichael Hennerich } \
452331b78edSMichael Hennerich static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
453331b78edSMichael Hennerich
454331b78edSMichael Hennerich SHOW(aux1)
SHOW(aux2)455331b78edSMichael Hennerich SHOW(aux2)
456331b78edSMichael Hennerich SHOW(aux3)
457331b78edSMichael Hennerich SHOW(bat1)
458331b78edSMichael Hennerich SHOW(bat2)
459331b78edSMichael Hennerich SHOW(temp1)
460331b78edSMichael Hennerich SHOW(temp2)
461331b78edSMichael Hennerich
462331b78edSMichael Hennerich static ssize_t ad7877_disable_show(struct device *dev,
463331b78edSMichael Hennerich struct device_attribute *attr, char *buf)
464331b78edSMichael Hennerich {
465331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
466331b78edSMichael Hennerich
467331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->disabled);
468331b78edSMichael Hennerich }
469331b78edSMichael Hennerich
ad7877_disable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)470331b78edSMichael Hennerich static ssize_t ad7877_disable_store(struct device *dev,
471331b78edSMichael Hennerich struct device_attribute *attr,
472331b78edSMichael Hennerich const char *buf, size_t count)
473331b78edSMichael Hennerich {
474331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
47576496e7aSJJ Ding unsigned int val;
476331b78edSMichael Hennerich int error;
477331b78edSMichael Hennerich
47876496e7aSJJ Ding error = kstrtouint(buf, 10, &val);
479331b78edSMichael Hennerich if (error)
480331b78edSMichael Hennerich return error;
481331b78edSMichael Hennerich
482331b78edSMichael Hennerich if (val)
483331b78edSMichael Hennerich ad7877_disable(ts);
484331b78edSMichael Hennerich else
485331b78edSMichael Hennerich ad7877_enable(ts);
486331b78edSMichael Hennerich
487331b78edSMichael Hennerich return count;
488331b78edSMichael Hennerich }
489331b78edSMichael Hennerich
490331b78edSMichael Hennerich static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
491331b78edSMichael Hennerich
ad7877_dac_show(struct device * dev,struct device_attribute * attr,char * buf)492331b78edSMichael Hennerich static ssize_t ad7877_dac_show(struct device *dev,
493331b78edSMichael Hennerich struct device_attribute *attr, char *buf)
494331b78edSMichael Hennerich {
495331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
496331b78edSMichael Hennerich
497331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->dac);
498331b78edSMichael Hennerich }
499331b78edSMichael Hennerich
ad7877_dac_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)500331b78edSMichael Hennerich static ssize_t ad7877_dac_store(struct device *dev,
501331b78edSMichael Hennerich struct device_attribute *attr,
502331b78edSMichael Hennerich const char *buf, size_t count)
503331b78edSMichael Hennerich {
504331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
50576496e7aSJJ Ding unsigned int val;
506331b78edSMichael Hennerich int error;
507331b78edSMichael Hennerich
50876496e7aSJJ Ding error = kstrtouint(buf, 10, &val);
509331b78edSMichael Hennerich if (error)
510331b78edSMichael Hennerich return error;
511331b78edSMichael Hennerich
512331b78edSMichael Hennerich mutex_lock(&ts->mutex);
513331b78edSMichael Hennerich ts->dac = val & 0xFF;
514331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
515331b78edSMichael Hennerich mutex_unlock(&ts->mutex);
516331b78edSMichael Hennerich
517331b78edSMichael Hennerich return count;
518331b78edSMichael Hennerich }
519331b78edSMichael Hennerich
520331b78edSMichael Hennerich static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
521331b78edSMichael Hennerich
ad7877_gpio3_show(struct device * dev,struct device_attribute * attr,char * buf)522331b78edSMichael Hennerich static ssize_t ad7877_gpio3_show(struct device *dev,
523331b78edSMichael Hennerich struct device_attribute *attr, char *buf)
524331b78edSMichael Hennerich {
525331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
526331b78edSMichael Hennerich
527331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->gpio3);
528331b78edSMichael Hennerich }
529331b78edSMichael Hennerich
ad7877_gpio3_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)530331b78edSMichael Hennerich static ssize_t ad7877_gpio3_store(struct device *dev,
531331b78edSMichael Hennerich struct device_attribute *attr,
532331b78edSMichael Hennerich const char *buf, size_t count)
533331b78edSMichael Hennerich {
534331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
53576496e7aSJJ Ding unsigned int val;
536331b78edSMichael Hennerich int error;
537331b78edSMichael Hennerich
53876496e7aSJJ Ding error = kstrtouint(buf, 10, &val);
539331b78edSMichael Hennerich if (error)
540331b78edSMichael Hennerich return error;
541331b78edSMichael Hennerich
542331b78edSMichael Hennerich mutex_lock(&ts->mutex);
543331b78edSMichael Hennerich ts->gpio3 = !!val;
544331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
545331b78edSMichael Hennerich (ts->gpio4 << 4) | (ts->gpio3 << 5));
546331b78edSMichael Hennerich mutex_unlock(&ts->mutex);
547331b78edSMichael Hennerich
548331b78edSMichael Hennerich return count;
549331b78edSMichael Hennerich }
550331b78edSMichael Hennerich
551331b78edSMichael Hennerich static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
552331b78edSMichael Hennerich
ad7877_gpio4_show(struct device * dev,struct device_attribute * attr,char * buf)553331b78edSMichael Hennerich static ssize_t ad7877_gpio4_show(struct device *dev,
554331b78edSMichael Hennerich struct device_attribute *attr, char *buf)
555331b78edSMichael Hennerich {
556331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
557331b78edSMichael Hennerich
558331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->gpio4);
559331b78edSMichael Hennerich }
560331b78edSMichael Hennerich
ad7877_gpio4_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)561331b78edSMichael Hennerich static ssize_t ad7877_gpio4_store(struct device *dev,
562331b78edSMichael Hennerich struct device_attribute *attr,
563331b78edSMichael Hennerich const char *buf, size_t count)
564331b78edSMichael Hennerich {
565331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev);
56676496e7aSJJ Ding unsigned int val;
567331b78edSMichael Hennerich int error;
568331b78edSMichael Hennerich
56976496e7aSJJ Ding error = kstrtouint(buf, 10, &val);
570331b78edSMichael Hennerich if (error)
571331b78edSMichael Hennerich return error;
572331b78edSMichael Hennerich
573331b78edSMichael Hennerich mutex_lock(&ts->mutex);
574331b78edSMichael Hennerich ts->gpio4 = !!val;
575331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
576331b78edSMichael Hennerich (ts->gpio4 << 4) | (ts->gpio3 << 5));
577331b78edSMichael Hennerich mutex_unlock(&ts->mutex);
578331b78edSMichael Hennerich
579331b78edSMichael Hennerich return count;
580331b78edSMichael Hennerich }
581331b78edSMichael Hennerich
582331b78edSMichael Hennerich static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
583331b78edSMichael Hennerich
584331b78edSMichael Hennerich static struct attribute *ad7877_attributes[] = {
585331b78edSMichael Hennerich &dev_attr_temp1.attr,
586331b78edSMichael Hennerich &dev_attr_temp2.attr,
587331b78edSMichael Hennerich &dev_attr_aux1.attr,
588331b78edSMichael Hennerich &dev_attr_aux2.attr,
5892fd18abaSDmitry Torokhov &dev_attr_aux3.attr,
590331b78edSMichael Hennerich &dev_attr_bat1.attr,
591331b78edSMichael Hennerich &dev_attr_bat2.attr,
592331b78edSMichael Hennerich &dev_attr_disable.attr,
593331b78edSMichael Hennerich &dev_attr_dac.attr,
5942fd18abaSDmitry Torokhov &dev_attr_gpio3.attr,
595331b78edSMichael Hennerich &dev_attr_gpio4.attr,
596331b78edSMichael Hennerich NULL
597331b78edSMichael Hennerich };
598331b78edSMichael Hennerich
ad7877_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)599587a1f16SAl Viro static umode_t ad7877_attr_is_visible(struct kobject *kobj,
6002fd18abaSDmitry Torokhov struct attribute *attr, int n)
6012fd18abaSDmitry Torokhov {
602587a1f16SAl Viro umode_t mode = attr->mode;
6032fd18abaSDmitry Torokhov
6042fd18abaSDmitry Torokhov if (attr == &dev_attr_aux3.attr) {
6052fd18abaSDmitry Torokhov if (gpio3)
6062fd18abaSDmitry Torokhov mode = 0;
6072fd18abaSDmitry Torokhov } else if (attr == &dev_attr_gpio3.attr) {
6082fd18abaSDmitry Torokhov if (!gpio3)
6092fd18abaSDmitry Torokhov mode = 0;
6102fd18abaSDmitry Torokhov }
6112fd18abaSDmitry Torokhov
6122fd18abaSDmitry Torokhov return mode;
6132fd18abaSDmitry Torokhov }
6142fd18abaSDmitry Torokhov
615331b78edSMichael Hennerich static const struct attribute_group ad7877_attr_group = {
6162fd18abaSDmitry Torokhov .is_visible = ad7877_attr_is_visible,
617331b78edSMichael Hennerich .attrs = ad7877_attributes,
618331b78edSMichael Hennerich };
619331b78edSMichael Hennerich
ad7877_setup_ts_def_msg(struct spi_device * spi,struct ad7877 * ts)620331b78edSMichael Hennerich static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
621331b78edSMichael Hennerich {
622331b78edSMichael Hennerich struct spi_message *m;
623331b78edSMichael Hennerich int i;
624331b78edSMichael Hennerich
625331b78edSMichael Hennerich ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
626331b78edSMichael Hennerich AD7877_POL(ts->stopacq_polarity) |
627331b78edSMichael Hennerich AD7877_AVG(ts->averaging) | AD7877_PM(1) |
628331b78edSMichael Hennerich AD7877_TMR(ts->pen_down_acc_interval) |
629331b78edSMichael Hennerich AD7877_ACQ(ts->acquisition_time) |
630331b78edSMichael Hennerich AD7877_FCD(ts->first_conversion_delay);
631331b78edSMichael Hennerich
632331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
633331b78edSMichael Hennerich
634331b78edSMichael Hennerich ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
635331b78edSMichael Hennerich AD7877_READADD(AD7877_REG_XPLUS-1) |
636331b78edSMichael Hennerich AD7877_MODE_SEQ1 | AD7877_DFR;
637331b78edSMichael Hennerich
638331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
639331b78edSMichael Hennerich
640331b78edSMichael Hennerich ts->cmd_dummy = 0;
641331b78edSMichael Hennerich
642331b78edSMichael Hennerich m = &ts->msg;
643331b78edSMichael Hennerich
644331b78edSMichael Hennerich spi_message_init(m);
645331b78edSMichael Hennerich
646331b78edSMichael Hennerich m->context = ts;
647331b78edSMichael Hennerich
648331b78edSMichael Hennerich ts->xfer[0].tx_buf = &ts->cmd_crtl1;
649331b78edSMichael Hennerich ts->xfer[0].len = 2;
650e92c27fbSMichael Hennerich ts->xfer[0].cs_change = 1;
651331b78edSMichael Hennerich
652331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[0], m);
653331b78edSMichael Hennerich
654331b78edSMichael Hennerich ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
655331b78edSMichael Hennerich ts->xfer[1].len = 2;
656e92c27fbSMichael Hennerich ts->xfer[1].cs_change = 1;
657331b78edSMichael Hennerich
658331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[1], m);
659331b78edSMichael Hennerich
660e92c27fbSMichael Hennerich for (i = 0; i < AD7877_NR_SENSE; i++) {
661331b78edSMichael Hennerich ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
662331b78edSMichael Hennerich ts->xfer[i + 2].len = 2;
663e92c27fbSMichael Hennerich if (i < (AD7877_NR_SENSE - 1))
664e92c27fbSMichael Hennerich ts->xfer[i + 2].cs_change = 1;
665331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[i + 2], m);
666331b78edSMichael Hennerich }
667331b78edSMichael Hennerich }
668331b78edSMichael Hennerich
ad7877_probe(struct spi_device * spi)6695298cc4cSBill Pemberton static int ad7877_probe(struct spi_device *spi)
670331b78edSMichael Hennerich {
671331b78edSMichael Hennerich struct ad7877 *ts;
672331b78edSMichael Hennerich struct input_dev *input_dev;
673c838cb3dSJingoo Han struct ad7877_platform_data *pdata = dev_get_platdata(&spi->dev);
674331b78edSMichael Hennerich int err;
675331b78edSMichael Hennerich u16 verify;
676331b78edSMichael Hennerich
677331b78edSMichael Hennerich if (!spi->irq) {
678331b78edSMichael Hennerich dev_dbg(&spi->dev, "no IRQ?\n");
679331b78edSMichael Hennerich return -ENODEV;
680331b78edSMichael Hennerich }
681331b78edSMichael Hennerich
682331b78edSMichael Hennerich if (!pdata) {
683331b78edSMichael Hennerich dev_dbg(&spi->dev, "no platform data?\n");
684331b78edSMichael Hennerich return -ENODEV;
685331b78edSMichael Hennerich }
686331b78edSMichael Hennerich
687331b78edSMichael Hennerich /* don't exceed max specified SPI CLK frequency */
688331b78edSMichael Hennerich if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
689331b78edSMichael Hennerich dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
690331b78edSMichael Hennerich return -EINVAL;
691331b78edSMichael Hennerich }
692331b78edSMichael Hennerich
693cd9b6fdfSOskar Schirmer spi->bits_per_word = 16;
694cd9b6fdfSOskar Schirmer err = spi_setup(spi);
695cd9b6fdfSOskar Schirmer if (err) {
696cd9b6fdfSOskar Schirmer dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
697cd9b6fdfSOskar Schirmer return err;
698cd9b6fdfSOskar Schirmer }
699cd9b6fdfSOskar Schirmer
700fbe6a536SAndi Shyti ts = devm_kzalloc(&spi->dev, sizeof(struct ad7877), GFP_KERNEL);
701fbe6a536SAndi Shyti if (!ts)
702fbe6a536SAndi Shyti return -ENOMEM;
703fbe6a536SAndi Shyti
704fbe6a536SAndi Shyti input_dev = devm_input_allocate_device(&spi->dev);
705fbe6a536SAndi Shyti if (!input_dev)
706fbe6a536SAndi Shyti return -ENOMEM;
707fbe6a536SAndi Shyti
708fbe6a536SAndi Shyti err = devm_add_action_or_reset(&spi->dev, ad7877_disable, ts);
709fbe6a536SAndi Shyti if (err)
710fbe6a536SAndi Shyti return err;
711331b78edSMichael Hennerich
712e1793c69SJingoo Han spi_set_drvdata(spi, ts);
713331b78edSMichael Hennerich ts->spi = spi;
714331b78edSMichael Hennerich ts->input = input_dev;
715331b78edSMichael Hennerich
7168446b32bSstephen lu timer_setup(&ts->timer, ad7877_timer, 0);
717331b78edSMichael Hennerich mutex_init(&ts->mutex);
718331b78edSMichael Hennerich spin_lock_init(&ts->lock);
719331b78edSMichael Hennerich
720331b78edSMichael Hennerich ts->model = pdata->model ? : 7877;
721331b78edSMichael Hennerich ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
722331b78edSMichael Hennerich ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
723331b78edSMichael Hennerich ts->pressure_max = pdata->pressure_max ? : ~0;
724331b78edSMichael Hennerich
725331b78edSMichael Hennerich ts->stopacq_polarity = pdata->stopacq_polarity;
726331b78edSMichael Hennerich ts->first_conversion_delay = pdata->first_conversion_delay;
727331b78edSMichael Hennerich ts->acquisition_time = pdata->acquisition_time;
728331b78edSMichael Hennerich ts->averaging = pdata->averaging;
729331b78edSMichael Hennerich ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
730331b78edSMichael Hennerich
731331b78edSMichael Hennerich snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
732331b78edSMichael Hennerich
733331b78edSMichael Hennerich input_dev->name = "AD7877 Touchscreen";
734331b78edSMichael Hennerich input_dev->phys = ts->phys;
735331b78edSMichael Hennerich input_dev->dev.parent = &spi->dev;
736331b78edSMichael Hennerich
73747026b25SMichael Hennerich __set_bit(EV_KEY, input_dev->evbit);
73847026b25SMichael Hennerich __set_bit(BTN_TOUCH, input_dev->keybit);
739331b78edSMichael Hennerich __set_bit(EV_ABS, input_dev->evbit);
740331b78edSMichael Hennerich __set_bit(ABS_X, input_dev->absbit);
741331b78edSMichael Hennerich __set_bit(ABS_Y, input_dev->absbit);
742331b78edSMichael Hennerich __set_bit(ABS_PRESSURE, input_dev->absbit);
743331b78edSMichael Hennerich
744331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_X,
745331b78edSMichael Hennerich pdata->x_min ? : 0,
746331b78edSMichael Hennerich pdata->x_max ? : MAX_12BIT,
747331b78edSMichael Hennerich 0, 0);
748331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_Y,
749331b78edSMichael Hennerich pdata->y_min ? : 0,
750331b78edSMichael Hennerich pdata->y_max ? : MAX_12BIT,
751331b78edSMichael Hennerich 0, 0);
752331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_PRESSURE,
753331b78edSMichael Hennerich pdata->pressure_min, pdata->pressure_max, 0, 0);
754331b78edSMichael Hennerich
755331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
756331b78edSMichael Hennerich
757331b78edSMichael Hennerich verify = ad7877_read(spi, AD7877_REG_SEQ1);
758331b78edSMichael Hennerich
759331b78edSMichael Hennerich if (verify != AD7877_MM_SEQUENCE) {
760331b78edSMichael Hennerich dev_err(&spi->dev, "%s: Failed to probe %s\n",
761331b78edSMichael Hennerich dev_name(&spi->dev), input_dev->name);
762fbe6a536SAndi Shyti return -ENODEV;
763331b78edSMichael Hennerich }
764331b78edSMichael Hennerich
765331b78edSMichael Hennerich if (gpio3)
766331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
767331b78edSMichael Hennerich
768331b78edSMichael Hennerich ad7877_setup_ts_def_msg(spi, ts);
769331b78edSMichael Hennerich
770331b78edSMichael Hennerich /* Request AD7877 /DAV GPIO interrupt */
771331b78edSMichael Hennerich
772fbe6a536SAndi Shyti err = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, ad7877_irq,
773b534422bSDmitry Torokhov IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
7740bc69ce6SMichael Hennerich spi->dev.driver->name, ts);
775331b78edSMichael Hennerich if (err) {
776331b78edSMichael Hennerich dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
777331b78edSMichael Hennerich return err;
778331b78edSMichael Hennerich }
779331b78edSMichael Hennerich
780fbe6a536SAndi Shyti err = devm_device_add_group(&spi->dev, &ad7877_attr_group);
781fbe6a536SAndi Shyti if (err)
782fbe6a536SAndi Shyti return err;
783331b78edSMichael Hennerich
784fbe6a536SAndi Shyti err = input_register_device(input_dev);
785fbe6a536SAndi Shyti if (err)
786fbe6a536SAndi Shyti return err;
787331b78edSMichael Hennerich
788331b78edSMichael Hennerich return 0;
789331b78edSMichael Hennerich }
790331b78edSMichael Hennerich
ad7877_suspend(struct device * dev)791*458d7343SJonathan Cameron static int ad7877_suspend(struct device *dev)
792331b78edSMichael Hennerich {
793fccdd7c4SMark Brown struct ad7877 *ts = dev_get_drvdata(dev);
794331b78edSMichael Hennerich
795331b78edSMichael Hennerich ad7877_disable(ts);
796331b78edSMichael Hennerich
797331b78edSMichael Hennerich return 0;
798331b78edSMichael Hennerich }
799331b78edSMichael Hennerich
ad7877_resume(struct device * dev)800*458d7343SJonathan Cameron static int ad7877_resume(struct device *dev)
801331b78edSMichael Hennerich {
802fccdd7c4SMark Brown struct ad7877 *ts = dev_get_drvdata(dev);
803331b78edSMichael Hennerich
804331b78edSMichael Hennerich ad7877_enable(ts);
805331b78edSMichael Hennerich
806331b78edSMichael Hennerich return 0;
807331b78edSMichael Hennerich }
808331b78edSMichael Hennerich
809*458d7343SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
810fccdd7c4SMark Brown
811331b78edSMichael Hennerich static struct spi_driver ad7877_driver = {
812331b78edSMichael Hennerich .driver = {
813331b78edSMichael Hennerich .name = "ad7877",
814*458d7343SJonathan Cameron .pm = pm_sleep_ptr(&ad7877_pm),
815331b78edSMichael Hennerich },
816331b78edSMichael Hennerich .probe = ad7877_probe,
817331b78edSMichael Hennerich };
818331b78edSMichael Hennerich
819ca83922eSAxel Lin module_spi_driver(ad7877_driver);
820331b78edSMichael Hennerich
821331b78edSMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
822331b78edSMichael Hennerich MODULE_DESCRIPTION("AD7877 touchscreen Driver");
823331b78edSMichael Hennerich MODULE_LICENSE("GPL");
824e0626e38SAnton Vorontsov MODULE_ALIAS("spi:ad7877");
825