blob: 46d1fec2cfd8952fca752f6e9fe4eb9af496daa0 [file] [log] [blame]
David Brownellffa458c2006-01-08 13:34:21 -08001/*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
5 *
6 * Using code from:
7 * - corgi_ts.c
8 * Copyright (C) 2004-2005 Richard Purdie
9 * - omap_ts.[hc], ads7846.h, ts_osk.c
10 * Copyright (C) 2002 MontaVista Software
11 * Copyright (C) 2004 Texas Instruments
12 * Copyright (C) 2005 Dirk Behme
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/device.h>
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/input.h>
22#include <linux/interrupt.h>
23#include <linux/slab.h>
24#include <linux/spi/spi.h>
25#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080026#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080027
28#ifdef CONFIG_ARM
29#include <asm/mach-types.h>
30#ifdef CONFIG_ARCH_OMAP
31#include <asm/arch/gpio.h>
32#endif
David Brownellffa458c2006-01-08 13:34:21 -080033#endif
34
35
36/*
37 * This code has been lightly tested on an ads7846.
38 * Support for ads7843 and ads7845 has only been stubbed in.
39 *
40 * Not yet done: investigate the values reported. Are x/y/pressure
41 * event values sane enough for X11? How accurate are the temperature
42 * and voltage readings? (System-specific calibration should support
43 * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
44 *
45 * app note sbaa036 talks in more detail about accurate sampling...
46 * that ought to help in situations like LCDs inducing noise (which
47 * can also be helped by using synch signals) and more generally.
48 */
49
50#define TS_POLL_PERIOD msecs_to_jiffies(10)
51
David Brownelld93f70b2006-02-15 00:49:35 -050052/* this driver doesn't aim at the peak continuous sample rate */
53#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
54
David Brownellffa458c2006-01-08 13:34:21 -080055struct ts_event {
56 /* For portability, we can't read 12 bit values using SPI (which
57 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050058 * with msbs zeroed). Instead, we read them as two 8-bit values,
David Brownellffa458c2006-01-08 13:34:21 -080059 * which need byteswapping then range adjustment.
60 */
61 __be16 x;
62 __be16 y;
63 __be16 z1, z2;
64};
65
66struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050067 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080068 char phys[32];
69
70 struct spi_device *spi;
71 u16 model;
72 u16 vref_delay_usecs;
73 u16 x_plate_ohms;
74
David Brownelld93f70b2006-02-15 00:49:35 -050075 u8 read_x, read_y, read_z1, read_z2;
David Brownellffa458c2006-01-08 13:34:21 -080076 struct ts_event tc;
77
78 struct spi_transfer xfer[8];
79 struct spi_message msg;
80
81 spinlock_t lock;
82 struct timer_list timer; /* P: lock */
83 unsigned pendown:1; /* P: lock */
84 unsigned pending:1; /* P: lock */
85// FIXME remove "irq_disabled"
86 unsigned irq_disabled:1; /* P: lock */
87};
88
89/* leave chip selected when we're done, for quicker re-select? */
90#if 0
91#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
92#else
93#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
94#endif
95
96/*--------------------------------------------------------------------------*/
97
98/* The ADS7846 has touchscreen and other sensors.
99 * Earlier ads784x chips are somewhat compatible.
100 */
101#define ADS_START (1 << 7)
102#define ADS_A2A1A0_d_y (1 << 4) /* differential */
103#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
104#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
105#define ADS_A2A1A0_d_x (5 << 4) /* differential */
106#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
107#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
108#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
109#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
110#define ADS_8_BIT (1 << 3)
111#define ADS_12_BIT (0 << 3)
112#define ADS_SER (1 << 2) /* non-differential */
113#define ADS_DFR (0 << 2) /* differential */
114#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
115#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
116#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
117#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
118
119#define MAX_12BIT ((1<<12)-1)
120
121/* leave ADC powered up (disables penirq) between differential samples */
122#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
123 | ADS_12_BIT | ADS_DFR)
124
David Brownelld93f70b2006-02-15 00:49:35 -0500125#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
126#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
127#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
128#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800129
130/* single-ended samples need to first power up reference voltage;
131 * we leave both ADC and VREF powered
132 */
133#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
134 | ADS_12_BIT | ADS_SER)
135
David Brownelld93f70b2006-02-15 00:49:35 -0500136#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
137#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800138
139/*--------------------------------------------------------------------------*/
140
141/*
142 * Non-touchscreen sensors only use single-ended conversions.
143 */
144
145struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500146 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800147 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500148 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800149 u16 scratch;
150 __be16 sample;
151 struct spi_message msg;
152 struct spi_transfer xfer[6];
153};
154
155static int ads7846_read12_ser(struct device *dev, unsigned command)
156{
157 struct spi_device *spi = to_spi_device(dev);
158 struct ads7846 *ts = dev_get_drvdata(dev);
159 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
160 int status;
161 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500162 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800163
164 if (!req)
165 return -ENOMEM;
166
Vitaly Wool8275c642006-01-08 13:34:28 -0800167 INIT_LIST_HEAD(&req->msg.transfers);
168
David Brownellffa458c2006-01-08 13:34:21 -0800169 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500170 req->ref_on = REF_ON;
171 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800172 req->xfer[0].len = 1;
173 req->xfer[1].rx_buf = &req->scratch;
174 req->xfer[1].len = 2;
175
176 /*
177 * for external VREF, 0 usec (and assume it's always on);
178 * for 1uF, use 800 usec;
179 * no cap, 100 usec.
180 */
181 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
182
183 /* take sample */
184 req->command = (u8) command;
185 req->xfer[2].tx_buf = &req->command;
186 req->xfer[2].len = 1;
187 req->xfer[3].rx_buf = &req->sample;
188 req->xfer[3].len = 2;
189
190 /* REVISIT: take a few more samples, and compare ... */
191
192 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500193 req->ref_off = REF_OFF;
194 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800195 req->xfer[4].len = 1;
196 req->xfer[5].rx_buf = &req->scratch;
197 req->xfer[5].len = 2;
198
199 CS_CHANGE(req->xfer[5]);
200
201 /* group all the transfers together, so we can't interfere with
202 * reading touchscreen state; disable penirq while sampling
203 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800204 for (i = 0; i < 6; i++)
205 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800206
207 disable_irq(spi->irq);
208 status = spi_sync(spi, &req->msg);
209 enable_irq(spi->irq);
210
211 if (req->msg.status)
212 status = req->msg.status;
213 sample = be16_to_cpu(req->sample);
214 sample = sample >> 4;
215 kfree(req);
216
217 return status ? status : sample;
218}
219
220#define SHOW(name) static ssize_t \
221name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
222{ \
223 ssize_t v = ads7846_read12_ser(dev, \
224 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
225 if (v < 0) \
226 return v; \
227 return sprintf(buf, "%u\n", (unsigned) v); \
228} \
229static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
230
231SHOW(temp0)
232SHOW(temp1)
233SHOW(vaux)
234SHOW(vbatt)
235
236/*--------------------------------------------------------------------------*/
237
238/*
239 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
240 * to retrieve touchscreen status.
241 *
242 * The SPI transfer completion callback does the real work. It reports
243 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
244 */
245
246static void ads7846_rx(void *ads)
247{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500248 struct ads7846 *ts = ads;
249 struct input_dev *input_dev = ts->input;
250 unsigned Rt;
251 unsigned sync = 0;
252 u16 x, y, z1, z2;
253 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800254
255 /* adjust: 12 bit samples (left aligned), built from
256 * two 8 bit values writen msb-first.
257 */
258 x = be16_to_cpu(ts->tc.x) >> 4;
259 y = be16_to_cpu(ts->tc.y) >> 4;
260 z1 = be16_to_cpu(ts->tc.z1) >> 4;
261 z2 = be16_to_cpu(ts->tc.z2) >> 4;
262
263 /* range filtering */
264 if (x == MAX_12BIT)
265 x = 0;
266
267 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
268 /* compute touch pressure resistance using equation #2 */
269 Rt = z2;
270 Rt -= z1;
271 Rt *= x;
272 Rt *= ts->x_plate_ohms;
273 Rt /= z1;
274 Rt = (Rt + 2047) >> 12;
275 } else
276 Rt = 0;
277
278 /* NOTE: "pendown" is inferred from pressure; we don't rely on
279 * being able to check nPENIRQ status, or "friendly" trigger modes
280 * (both-edges is much better than just-falling or low-level).
281 *
282 * REVISIT: some boards may require reading nPENIRQ; it's
283 * needed on 7843. and 7845 reads pressure differently...
284 *
285 * REVISIT: the touchscreen might not be connected; this code
286 * won't notice that, even if nPENIRQ never fires ...
287 */
288 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500289 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800290 sync = 1;
291 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500292 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800293 sync = 1;
294 }
295
296 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500297 input_report_abs(input_dev, ABS_X, x);
298 input_report_abs(input_dev, ABS_Y, y);
299 input_report_abs(input_dev, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800300 sync = 1;
301 }
302 if (sync)
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500303 input_sync(input_dev);
David Brownellffa458c2006-01-08 13:34:21 -0800304
305#ifdef VERBOSE
306 if (Rt || ts->pendown)
307 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
308 x, y, Rt, Rt ? "" : " UP");
309#endif
310
311 /* don't retrigger while we're suspended */
312 spin_lock_irqsave(&ts->lock, flags);
313
314 ts->pendown = (Rt != 0);
315 ts->pending = 0;
316
317 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
318 if (ts->pendown)
319 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
320 else if (ts->irq_disabled) {
321 ts->irq_disabled = 0;
322 enable_irq(ts->spi->irq);
323 }
324 }
325
326 spin_unlock_irqrestore(&ts->lock, flags);
327}
328
329static void ads7846_timer(unsigned long handle)
330{
331 struct ads7846 *ts = (void *)handle;
332 int status = 0;
333 unsigned long flags;
334
335 spin_lock_irqsave(&ts->lock, flags);
336 if (!ts->pending) {
337 ts->pending = 1;
338 if (!ts->irq_disabled) {
339 ts->irq_disabled = 1;
340 disable_irq(ts->spi->irq);
341 }
342 status = spi_async(ts->spi, &ts->msg);
343 if (status)
344 dev_err(&ts->spi->dev, "spi_async --> %d\n",
345 status);
346 }
347 spin_unlock_irqrestore(&ts->lock, flags);
348}
349
350static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
351{
352 ads7846_timer((unsigned long) handle);
353 return IRQ_HANDLED;
354}
355
356/*--------------------------------------------------------------------------*/
357
David Brownellffa458c2006-01-08 13:34:21 -0800358static int
David Brownell2e5a7bd2006-01-08 13:34:25 -0800359ads7846_suspend(struct spi_device *spi, pm_message_t message)
David Brownellffa458c2006-01-08 13:34:21 -0800360{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800361 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800362 unsigned long flags;
363
364 spin_lock_irqsave(&ts->lock, flags);
365
David Brownell2e5a7bd2006-01-08 13:34:25 -0800366 spi->dev.power.power_state = message;
David Brownellffa458c2006-01-08 13:34:21 -0800367
368 /* are we waiting for IRQ, or polling? */
369 if (!ts->pendown) {
370 if (!ts->irq_disabled) {
371 ts->irq_disabled = 1;
372 disable_irq(ts->spi->irq);
373 }
374 } else {
375 /* polling; force a final SPI completion;
376 * that will clean things up neatly
377 */
378 if (!ts->pending)
379 mod_timer(&ts->timer, jiffies);
380
381 while (ts->pendown || ts->pending) {
382 spin_unlock_irqrestore(&ts->lock, flags);
383 udelay(10);
384 spin_lock_irqsave(&ts->lock, flags);
385 }
386 }
387
388 /* we know the chip's in lowpower mode since we always
389 * leave it that way after every request
390 */
391
392 spin_unlock_irqrestore(&ts->lock, flags);
393 return 0;
394}
395
David Brownell2e5a7bd2006-01-08 13:34:25 -0800396static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800397{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800398 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800399
400 ts->irq_disabled = 0;
401 enable_irq(ts->spi->irq);
David Brownell2e5a7bd2006-01-08 13:34:25 -0800402 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800403 return 0;
404}
405
David Brownell2e5a7bd2006-01-08 13:34:25 -0800406static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800407{
David Brownellffa458c2006-01-08 13:34:21 -0800408 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500409 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800410 struct ads7846_platform_data *pdata = spi->dev.platform_data;
David Brownellffa458c2006-01-08 13:34:21 -0800411 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500412 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800413
414 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800415 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800416 return -ENODEV;
417 }
418
419 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800420 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800421 return -ENODEV;
422 }
423
424 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500425 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800426 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500427 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800428 return -EINVAL;
429 }
430
431 /* We'd set the wordsize to 12 bits ... except that some controllers
432 * will then treat the 8 bit command words as 12 bits (and drop the
433 * four MSBs of the 12 bit result). Result: inputs must be shifted
434 * to discard the four garbage LSBs.
435 */
436
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500437 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
438 input_dev = input_allocate_device();
439 if (!ts || !input_dev) {
440 err = -ENOMEM;
441 goto err_free_mem;
442 }
David Brownellffa458c2006-01-08 13:34:21 -0800443
David Brownell2e5a7bd2006-01-08 13:34:25 -0800444 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500445 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800446
447 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500448 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800449
450 init_timer(&ts->timer);
451 ts->timer.data = (unsigned long) ts;
452 ts->timer.function = ads7846_timer;
453
454 ts->model = pdata->model ? : 7846;
455 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
456 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
457
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500458 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800459
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500460 input_dev->name = "ADS784x Touchscreen";
461 input_dev->phys = ts->phys;
462 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800463
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500464 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
465 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
466 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800467 pdata->x_min ? : 0,
468 pdata->x_max ? : MAX_12BIT,
469 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500470 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800471 pdata->y_min ? : 0,
472 pdata->y_max ? : MAX_12BIT,
473 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500474 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800475 pdata->pressure_min, pdata->pressure_max, 0, 0);
476
David Brownellffa458c2006-01-08 13:34:21 -0800477 /* set up the transfers to read touchscreen state; this assumes we
478 * use formula #2 for pressure, not #3.
479 */
David Brownelld93f70b2006-02-15 00:49:35 -0500480 INIT_LIST_HEAD(&ts->msg.transfers);
David Brownellffa458c2006-01-08 13:34:21 -0800481 x = ts->xfer;
482
483 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500484 ts->read_y = READ_Y;
485 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800486 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500487 spi_message_add_tail(x, &ts->msg);
488
David Brownellffa458c2006-01-08 13:34:21 -0800489 x++;
490 x->rx_buf = &ts->tc.y;
491 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500492 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800493
494 /* turn y+ off, x- on; we'll use formula #2 */
495 if (ts->model == 7846) {
David Brownelld93f70b2006-02-15 00:49:35 -0500496 x++;
497 ts->read_z1 = READ_Z1;
498 x->tx_buf = &ts->read_z1;
David Brownellffa458c2006-01-08 13:34:21 -0800499 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500500 spi_message_add_tail(x, &ts->msg);
501
David Brownellffa458c2006-01-08 13:34:21 -0800502 x++;
503 x->rx_buf = &ts->tc.z1;
504 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500505 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800506
David Brownelld93f70b2006-02-15 00:49:35 -0500507 x++;
508 ts->read_z2 = READ_Z2;
509 x->tx_buf = &ts->read_z2;
David Brownellffa458c2006-01-08 13:34:21 -0800510 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500511 spi_message_add_tail(x, &ts->msg);
512
David Brownellffa458c2006-01-08 13:34:21 -0800513 x++;
514 x->rx_buf = &ts->tc.z2;
515 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500516 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800517 }
518
519 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500520 x++;
521 ts->read_x = READ_X;
522 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800523 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500524 spi_message_add_tail(x, &ts->msg);
525
David Brownellffa458c2006-01-08 13:34:21 -0800526 x++;
527 x->rx_buf = &ts->tc.x;
528 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500529 CS_CHANGE(*x);
530 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800531
David Brownellffa458c2006-01-08 13:34:21 -0800532 ts->msg.complete = ads7846_rx;
533 ts->msg.context = ts;
534
Russell Kingf43aaba2006-01-19 12:26:57 +0000535 if (request_irq(spi->irq, ads7846_irq,
536 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
537 spi->dev.bus_id, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800538 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500539 err = -EBUSY;
540 goto err_free_mem;
David Brownellffa458c2006-01-08 13:34:21 -0800541 }
David Brownellffa458c2006-01-08 13:34:21 -0800542
David Brownell2e5a7bd2006-01-08 13:34:25 -0800543 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800544
545 /* take a first sample, leaving nPENIRQ active; avoid
546 * the touchscreen, in case it's not connected.
547 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800548 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800549 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
550
551 /* ads7843/7845 don't have temperature sensors, and
552 * use the other sensors a bit differently too
553 */
554 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800555 device_create_file(&spi->dev, &dev_attr_temp0);
556 device_create_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800557 }
558 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800559 device_create_file(&spi->dev, &dev_attr_vbatt);
560 device_create_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800561
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500562 err = input_register_device(input_dev);
563 if (err)
564 goto err_free_irq;
565
David Brownellffa458c2006-01-08 13:34:21 -0800566 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500567
568 err_free_irq:
569 free_irq(spi->irq, ts);
570 err_free_mem:
571 input_free_device(input_dev);
572 kfree(ts);
573 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800574}
575
David Brownell2e5a7bd2006-01-08 13:34:25 -0800576static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800577{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800578 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800579
David Brownell2e5a7bd2006-01-08 13:34:25 -0800580 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800581 free_irq(ts->spi->irq, ts);
582 if (ts->irq_disabled)
583 enable_irq(ts->spi->irq);
584
585 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800586 device_remove_file(&spi->dev, &dev_attr_temp0);
587 device_remove_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800588 }
589 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800590 device_remove_file(&spi->dev, &dev_attr_vbatt);
591 device_remove_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800592
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500593 input_unregister_device(ts->input);
David Brownellffa458c2006-01-08 13:34:21 -0800594 kfree(ts);
595
David Brownell2e5a7bd2006-01-08 13:34:25 -0800596 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800597 return 0;
598}
599
David Brownell2e5a7bd2006-01-08 13:34:25 -0800600static struct spi_driver ads7846_driver = {
601 .driver = {
602 .name = "ads7846",
603 .bus = &spi_bus_type,
604 .owner = THIS_MODULE,
605 },
David Brownellffa458c2006-01-08 13:34:21 -0800606 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800607 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800608 .suspend = ads7846_suspend,
609 .resume = ads7846_resume,
610};
611
612static int __init ads7846_init(void)
613{
614 /* grr, board-specific init should stay out of drivers!! */
615
616#ifdef CONFIG_ARCH_OMAP
617 if (machine_is_omap_osk()) {
618 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
619 omap_request_gpio(4);
620 omap_set_gpio_direction(4, 1);
621 omap_request_gpio(6);
622 omap_set_gpio_direction(6, 1);
623 }
624 // also TI 1510 Innovator, bitbanging through FPGA
625 // also Nokia 770
626 // also Palm Tungsten T2
627#endif
628
629 // PXA:
630 // also Dell Axim X50
631 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800632 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800633 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
634
David Brownell2e5a7bd2006-01-08 13:34:25 -0800635 // Atmel at91sam9261-EK uses ads7843
636
David Brownellffa458c2006-01-08 13:34:21 -0800637 // also various AMD Au1x00 devel boards
638
David Brownell2e5a7bd2006-01-08 13:34:25 -0800639 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800640}
641module_init(ads7846_init);
642
643static void __exit ads7846_exit(void)
644{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800645 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800646
647#ifdef CONFIG_ARCH_OMAP
648 if (machine_is_omap_osk()) {
649 omap_free_gpio(4);
650 omap_free_gpio(6);
651 }
652#endif
653
654}
655module_exit(ads7846_exit);
656
657MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
658MODULE_LICENSE("GPL");