Commit e1793c69c82626717257285f437b0da7ad0d2e99

Authored by Jingoo Han
Committed by Dmitry Torokhov
1 parent c12454fad5

Input: ad7877 - use spi_get_drvdata() and spi_set_drvdata()

Use the wrapper functions for getting and setting the driver data using
spi_device instead of using dev_{get|set}_drvdata with &spi->dev, so we
can directly pass a struct spi_device.

Signed-off-by: Jingoo Han <jg1.han@samsung.com>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

Showing 1 changed file with 5 additions and 5 deletions Inline Diff

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