blob: 8c12a974b411a75093c2f65319b4c88761bc5dd7 [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>
26
27#ifdef CONFIG_ARM
28#include <asm/mach-types.h>
29#ifdef CONFIG_ARCH_OMAP
30#include <asm/arch/gpio.h>
31#endif
David Brownellffa458c2006-01-08 13:34:21 -080032#endif
33
34
35/*
36 * This code has been lightly tested on an ads7846.
37 * Support for ads7843 and ads7845 has only been stubbed in.
38 *
39 * Not yet done: investigate the values reported. Are x/y/pressure
40 * event values sane enough for X11? How accurate are the temperature
41 * and voltage readings? (System-specific calibration should support
42 * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
43 *
44 * app note sbaa036 talks in more detail about accurate sampling...
45 * that ought to help in situations like LCDs inducing noise (which
46 * can also be helped by using synch signals) and more generally.
47 */
48
49#define TS_POLL_PERIOD msecs_to_jiffies(10)
50
David Brownelld93f70b2006-02-15 00:49:35 -050051/* this driver doesn't aim at the peak continuous sample rate */
52#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
53
David Brownellffa458c2006-01-08 13:34:21 -080054struct ts_event {
55 /* For portability, we can't read 12 bit values using SPI (which
56 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050057 * with msbs zeroed). Instead, we read them as two 8-bit values,
David Brownellffa458c2006-01-08 13:34:21 -080058 * which need byteswapping then range adjustment.
59 */
60 __be16 x;
61 __be16 y;
62 __be16 z1, z2;
63};
64
65struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050066 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080067 char phys[32];
68
69 struct spi_device *spi;
70 u16 model;
71 u16 vref_delay_usecs;
72 u16 x_plate_ohms;
73
David Brownelld93f70b2006-02-15 00:49:35 -050074 u8 read_x, read_y, read_z1, read_z2;
David Brownellffa458c2006-01-08 13:34:21 -080075 struct ts_event tc;
76
77 struct spi_transfer xfer[8];
78 struct spi_message msg;
79
80 spinlock_t lock;
81 struct timer_list timer; /* P: lock */
82 unsigned pendown:1; /* P: lock */
83 unsigned pending:1; /* P: lock */
84// FIXME remove "irq_disabled"
85 unsigned irq_disabled:1; /* P: lock */
86};
87
88/* leave chip selected when we're done, for quicker re-select? */
89#if 0
90#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
91#else
92#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
93#endif
94
95/*--------------------------------------------------------------------------*/
96
97/* The ADS7846 has touchscreen and other sensors.
98 * Earlier ads784x chips are somewhat compatible.
99 */
100#define ADS_START (1 << 7)
101#define ADS_A2A1A0_d_y (1 << 4) /* differential */
102#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
103#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
104#define ADS_A2A1A0_d_x (5 << 4) /* differential */
105#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
106#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
107#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
108#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
109#define ADS_8_BIT (1 << 3)
110#define ADS_12_BIT (0 << 3)
111#define ADS_SER (1 << 2) /* non-differential */
112#define ADS_DFR (0 << 2) /* differential */
113#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
114#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
115#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
116#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
117
118#define MAX_12BIT ((1<<12)-1)
119
120/* leave ADC powered up (disables penirq) between differential samples */
121#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
122 | ADS_12_BIT | ADS_DFR)
123
David Brownelld93f70b2006-02-15 00:49:35 -0500124#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON)
125#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
126#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
127#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_PDOWN) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800128
129/* single-ended samples need to first power up reference voltage;
130 * we leave both ADC and VREF powered
131 */
132#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
133 | ADS_12_BIT | ADS_SER)
134
David Brownelld93f70b2006-02-15 00:49:35 -0500135#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
136#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
David Brownellffa458c2006-01-08 13:34:21 -0800137
138/*--------------------------------------------------------------------------*/
139
140/*
141 * Non-touchscreen sensors only use single-ended conversions.
142 */
143
144struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500145 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800146 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500147 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800148 u16 scratch;
149 __be16 sample;
150 struct spi_message msg;
151 struct spi_transfer xfer[6];
152};
153
154static int ads7846_read12_ser(struct device *dev, unsigned command)
155{
156 struct spi_device *spi = to_spi_device(dev);
157 struct ads7846 *ts = dev_get_drvdata(dev);
158 struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL);
159 int status;
160 int sample;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500161 int i;
David Brownellffa458c2006-01-08 13:34:21 -0800162
163 if (!req)
164 return -ENOMEM;
165
Vitaly Wool8275c642006-01-08 13:34:28 -0800166 INIT_LIST_HEAD(&req->msg.transfers);
167
David Brownellffa458c2006-01-08 13:34:21 -0800168 /* activate reference, so it has time to settle; */
David Brownelld93f70b2006-02-15 00:49:35 -0500169 req->ref_on = REF_ON;
170 req->xfer[0].tx_buf = &req->ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800171 req->xfer[0].len = 1;
172 req->xfer[1].rx_buf = &req->scratch;
173 req->xfer[1].len = 2;
174
175 /*
176 * for external VREF, 0 usec (and assume it's always on);
177 * for 1uF, use 800 usec;
178 * no cap, 100 usec.
179 */
180 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
181
182 /* take sample */
183 req->command = (u8) command;
184 req->xfer[2].tx_buf = &req->command;
185 req->xfer[2].len = 1;
186 req->xfer[3].rx_buf = &req->sample;
187 req->xfer[3].len = 2;
188
189 /* REVISIT: take a few more samples, and compare ... */
190
191 /* turn off reference */
David Brownelld93f70b2006-02-15 00:49:35 -0500192 req->ref_off = REF_OFF;
193 req->xfer[4].tx_buf = &req->ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800194 req->xfer[4].len = 1;
195 req->xfer[5].rx_buf = &req->scratch;
196 req->xfer[5].len = 2;
197
198 CS_CHANGE(req->xfer[5]);
199
200 /* group all the transfers together, so we can't interfere with
201 * reading touchscreen state; disable penirq while sampling
202 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800203 for (i = 0; i < 6; i++)
204 spi_message_add_tail(&req->xfer[i], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800205
206 disable_irq(spi->irq);
207 status = spi_sync(spi, &req->msg);
208 enable_irq(spi->irq);
209
210 if (req->msg.status)
211 status = req->msg.status;
212 sample = be16_to_cpu(req->sample);
213 sample = sample >> 4;
214 kfree(req);
215
216 return status ? status : sample;
217}
218
219#define SHOW(name) static ssize_t \
220name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
221{ \
222 ssize_t v = ads7846_read12_ser(dev, \
223 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
224 if (v < 0) \
225 return v; \
226 return sprintf(buf, "%u\n", (unsigned) v); \
227} \
228static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
229
230SHOW(temp0)
231SHOW(temp1)
232SHOW(vaux)
233SHOW(vbatt)
234
235/*--------------------------------------------------------------------------*/
236
237/*
238 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
239 * to retrieve touchscreen status.
240 *
241 * The SPI transfer completion callback does the real work. It reports
242 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
243 */
244
245static void ads7846_rx(void *ads)
246{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500247 struct ads7846 *ts = ads;
248 struct input_dev *input_dev = ts->input;
249 unsigned Rt;
250 unsigned sync = 0;
251 u16 x, y, z1, z2;
252 unsigned long flags;
David Brownellffa458c2006-01-08 13:34:21 -0800253
254 /* adjust: 12 bit samples (left aligned), built from
255 * two 8 bit values writen msb-first.
256 */
257 x = be16_to_cpu(ts->tc.x) >> 4;
258 y = be16_to_cpu(ts->tc.y) >> 4;
259 z1 = be16_to_cpu(ts->tc.z1) >> 4;
260 z2 = be16_to_cpu(ts->tc.z2) >> 4;
261
262 /* range filtering */
263 if (x == MAX_12BIT)
264 x = 0;
265
266 if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
267 /* compute touch pressure resistance using equation #2 */
268 Rt = z2;
269 Rt -= z1;
270 Rt *= x;
271 Rt *= ts->x_plate_ohms;
272 Rt /= z1;
273 Rt = (Rt + 2047) >> 12;
274 } else
275 Rt = 0;
276
277 /* NOTE: "pendown" is inferred from pressure; we don't rely on
278 * being able to check nPENIRQ status, or "friendly" trigger modes
279 * (both-edges is much better than just-falling or low-level).
280 *
281 * REVISIT: some boards may require reading nPENIRQ; it's
282 * needed on 7843. and 7845 reads pressure differently...
283 *
284 * REVISIT: the touchscreen might not be connected; this code
285 * won't notice that, even if nPENIRQ never fires ...
286 */
287 if (!ts->pendown && Rt != 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500288 input_report_key(input_dev, BTN_TOUCH, 1);
David Brownellffa458c2006-01-08 13:34:21 -0800289 sync = 1;
290 } else if (ts->pendown && Rt == 0) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500291 input_report_key(input_dev, BTN_TOUCH, 0);
David Brownellffa458c2006-01-08 13:34:21 -0800292 sync = 1;
293 }
294
295 if (Rt) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500296 input_report_abs(input_dev, ABS_X, x);
297 input_report_abs(input_dev, ABS_Y, y);
298 input_report_abs(input_dev, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800299 sync = 1;
300 }
301 if (sync)
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500302 input_sync(input_dev);
David Brownellffa458c2006-01-08 13:34:21 -0800303
304#ifdef VERBOSE
305 if (Rt || ts->pendown)
306 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id,
307 x, y, Rt, Rt ? "" : " UP");
308#endif
309
310 /* don't retrigger while we're suspended */
311 spin_lock_irqsave(&ts->lock, flags);
312
313 ts->pendown = (Rt != 0);
314 ts->pending = 0;
315
316 if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) {
317 if (ts->pendown)
318 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD);
319 else if (ts->irq_disabled) {
320 ts->irq_disabled = 0;
321 enable_irq(ts->spi->irq);
322 }
323 }
324
325 spin_unlock_irqrestore(&ts->lock, flags);
326}
327
328static void ads7846_timer(unsigned long handle)
329{
330 struct ads7846 *ts = (void *)handle;
331 int status = 0;
332 unsigned long flags;
333
334 spin_lock_irqsave(&ts->lock, flags);
335 if (!ts->pending) {
336 ts->pending = 1;
337 if (!ts->irq_disabled) {
338 ts->irq_disabled = 1;
339 disable_irq(ts->spi->irq);
340 }
341 status = spi_async(ts->spi, &ts->msg);
342 if (status)
343 dev_err(&ts->spi->dev, "spi_async --> %d\n",
344 status);
345 }
346 spin_unlock_irqrestore(&ts->lock, flags);
347}
348
349static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
350{
351 ads7846_timer((unsigned long) handle);
352 return IRQ_HANDLED;
353}
354
355/*--------------------------------------------------------------------------*/
356
David Brownellffa458c2006-01-08 13:34:21 -0800357static int
David Brownell2e5a7bd2006-01-08 13:34:25 -0800358ads7846_suspend(struct spi_device *spi, pm_message_t message)
David Brownellffa458c2006-01-08 13:34:21 -0800359{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800360 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800361 unsigned long flags;
362
363 spin_lock_irqsave(&ts->lock, flags);
364
David Brownell2e5a7bd2006-01-08 13:34:25 -0800365 spi->dev.power.power_state = message;
David Brownellffa458c2006-01-08 13:34:21 -0800366
367 /* are we waiting for IRQ, or polling? */
368 if (!ts->pendown) {
369 if (!ts->irq_disabled) {
370 ts->irq_disabled = 1;
371 disable_irq(ts->spi->irq);
372 }
373 } else {
374 /* polling; force a final SPI completion;
375 * that will clean things up neatly
376 */
377 if (!ts->pending)
378 mod_timer(&ts->timer, jiffies);
379
380 while (ts->pendown || ts->pending) {
381 spin_unlock_irqrestore(&ts->lock, flags);
382 udelay(10);
383 spin_lock_irqsave(&ts->lock, flags);
384 }
385 }
386
387 /* we know the chip's in lowpower mode since we always
388 * leave it that way after every request
389 */
390
391 spin_unlock_irqrestore(&ts->lock, flags);
392 return 0;
393}
394
David Brownell2e5a7bd2006-01-08 13:34:25 -0800395static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800396{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800397 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800398
399 ts->irq_disabled = 0;
400 enable_irq(ts->spi->irq);
David Brownell2e5a7bd2006-01-08 13:34:25 -0800401 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800402 return 0;
403}
404
David Brownell2e5a7bd2006-01-08 13:34:25 -0800405static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800406{
David Brownellffa458c2006-01-08 13:34:21 -0800407 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500408 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800409 struct ads7846_platform_data *pdata = spi->dev.platform_data;
David Brownellffa458c2006-01-08 13:34:21 -0800410 struct spi_transfer *x;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500411 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800412
413 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800414 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800415 return -ENODEV;
416 }
417
418 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800419 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800420 return -ENODEV;
421 }
422
423 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500424 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800425 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500426 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800427 return -EINVAL;
428 }
429
430 /* We'd set the wordsize to 12 bits ... except that some controllers
431 * will then treat the 8 bit command words as 12 bits (and drop the
432 * four MSBs of the 12 bit result). Result: inputs must be shifted
433 * to discard the four garbage LSBs.
434 */
435
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500436 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
437 input_dev = input_allocate_device();
438 if (!ts || !input_dev) {
439 err = -ENOMEM;
440 goto err_free_mem;
441 }
David Brownellffa458c2006-01-08 13:34:21 -0800442
David Brownell2e5a7bd2006-01-08 13:34:25 -0800443 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500444 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800445
446 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500447 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800448
449 init_timer(&ts->timer);
450 ts->timer.data = (unsigned long) ts;
451 ts->timer.function = ads7846_timer;
452
453 ts->model = pdata->model ? : 7846;
454 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
455 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
456
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500457 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800458
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500459 input_dev->name = "ADS784x Touchscreen";
460 input_dev->phys = ts->phys;
461 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800462
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500463 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
464 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
465 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800466 pdata->x_min ? : 0,
467 pdata->x_max ? : MAX_12BIT,
468 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500469 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800470 pdata->y_min ? : 0,
471 pdata->y_max ? : MAX_12BIT,
472 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500473 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800474 pdata->pressure_min, pdata->pressure_max, 0, 0);
475
David Brownellffa458c2006-01-08 13:34:21 -0800476 /* set up the transfers to read touchscreen state; this assumes we
477 * use formula #2 for pressure, not #3.
478 */
David Brownelld93f70b2006-02-15 00:49:35 -0500479 INIT_LIST_HEAD(&ts->msg.transfers);
David Brownellffa458c2006-01-08 13:34:21 -0800480 x = ts->xfer;
481
482 /* y- still on; turn on only y+ (and ADC) */
David Brownelld93f70b2006-02-15 00:49:35 -0500483 ts->read_y = READ_Y;
484 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800485 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500486 spi_message_add_tail(x, &ts->msg);
487
David Brownellffa458c2006-01-08 13:34:21 -0800488 x++;
489 x->rx_buf = &ts->tc.y;
490 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500491 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800492
493 /* turn y+ off, x- on; we'll use formula #2 */
494 if (ts->model == 7846) {
David Brownelld93f70b2006-02-15 00:49:35 -0500495 x++;
496 ts->read_z1 = READ_Z1;
497 x->tx_buf = &ts->read_z1;
David Brownellffa458c2006-01-08 13:34:21 -0800498 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500499 spi_message_add_tail(x, &ts->msg);
500
David Brownellffa458c2006-01-08 13:34:21 -0800501 x++;
502 x->rx_buf = &ts->tc.z1;
503 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500504 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800505
David Brownelld93f70b2006-02-15 00:49:35 -0500506 x++;
507 ts->read_z2 = READ_Z2;
508 x->tx_buf = &ts->read_z2;
David Brownellffa458c2006-01-08 13:34:21 -0800509 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500510 spi_message_add_tail(x, &ts->msg);
511
David Brownellffa458c2006-01-08 13:34:21 -0800512 x++;
513 x->rx_buf = &ts->tc.z2;
514 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500515 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800516 }
517
518 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500519 x++;
520 ts->read_x = READ_X;
521 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800522 x->len = 1;
David Brownelld93f70b2006-02-15 00:49:35 -0500523 spi_message_add_tail(x, &ts->msg);
524
David Brownellffa458c2006-01-08 13:34:21 -0800525 x++;
526 x->rx_buf = &ts->tc.x;
527 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -0500528 CS_CHANGE(*x);
529 spi_message_add_tail(x, &ts->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800530
David Brownellffa458c2006-01-08 13:34:21 -0800531 ts->msg.complete = ads7846_rx;
532 ts->msg.context = ts;
533
Russell Kingf43aaba2006-01-19 12:26:57 +0000534 if (request_irq(spi->irq, ads7846_irq,
535 SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
536 spi->dev.bus_id, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800537 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500538 err = -EBUSY;
539 goto err_free_mem;
David Brownellffa458c2006-01-08 13:34:21 -0800540 }
David Brownellffa458c2006-01-08 13:34:21 -0800541
David Brownell2e5a7bd2006-01-08 13:34:25 -0800542 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -0800543
544 /* take a first sample, leaving nPENIRQ active; avoid
545 * the touchscreen, in case it's not connected.
546 */
David Brownell2e5a7bd2006-01-08 13:34:25 -0800547 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -0800548 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
549
550 /* ads7843/7845 don't have temperature sensors, and
551 * use the other sensors a bit differently too
552 */
553 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800554 device_create_file(&spi->dev, &dev_attr_temp0);
555 device_create_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800556 }
557 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800558 device_create_file(&spi->dev, &dev_attr_vbatt);
559 device_create_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800560
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500561 err = input_register_device(input_dev);
562 if (err)
563 goto err_free_irq;
564
David Brownellffa458c2006-01-08 13:34:21 -0800565 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500566
567 err_free_irq:
568 free_irq(spi->irq, ts);
569 err_free_mem:
570 input_free_device(input_dev);
571 kfree(ts);
572 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800573}
574
David Brownell2e5a7bd2006-01-08 13:34:25 -0800575static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800576{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800577 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800578
David Brownell2e5a7bd2006-01-08 13:34:25 -0800579 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -0800580 free_irq(ts->spi->irq, ts);
581 if (ts->irq_disabled)
582 enable_irq(ts->spi->irq);
583
584 if (ts->model == 7846) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800585 device_remove_file(&spi->dev, &dev_attr_temp0);
586 device_remove_file(&spi->dev, &dev_attr_temp1);
David Brownellffa458c2006-01-08 13:34:21 -0800587 }
588 if (ts->model != 7845)
David Brownell2e5a7bd2006-01-08 13:34:25 -0800589 device_remove_file(&spi->dev, &dev_attr_vbatt);
590 device_remove_file(&spi->dev, &dev_attr_vaux);
David Brownellffa458c2006-01-08 13:34:21 -0800591
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500592 input_unregister_device(ts->input);
David Brownellffa458c2006-01-08 13:34:21 -0800593 kfree(ts);
594
David Brownell2e5a7bd2006-01-08 13:34:25 -0800595 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -0800596 return 0;
597}
598
David Brownell2e5a7bd2006-01-08 13:34:25 -0800599static struct spi_driver ads7846_driver = {
600 .driver = {
601 .name = "ads7846",
602 .bus = &spi_bus_type,
603 .owner = THIS_MODULE,
604 },
David Brownellffa458c2006-01-08 13:34:21 -0800605 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -0800606 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -0800607 .suspend = ads7846_suspend,
608 .resume = ads7846_resume,
609};
610
611static int __init ads7846_init(void)
612{
613 /* grr, board-specific init should stay out of drivers!! */
614
615#ifdef CONFIG_ARCH_OMAP
616 if (machine_is_omap_osk()) {
617 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
618 omap_request_gpio(4);
619 omap_set_gpio_direction(4, 1);
620 omap_request_gpio(6);
621 omap_set_gpio_direction(6, 1);
622 }
623 // also TI 1510 Innovator, bitbanging through FPGA
624 // also Nokia 770
625 // also Palm Tungsten T2
626#endif
627
628 // PXA:
629 // also Dell Axim X50
630 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -0800631 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -0800632 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
633
David Brownell2e5a7bd2006-01-08 13:34:25 -0800634 // Atmel at91sam9261-EK uses ads7843
635
David Brownellffa458c2006-01-08 13:34:21 -0800636 // also various AMD Au1x00 devel boards
637
David Brownell2e5a7bd2006-01-08 13:34:25 -0800638 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800639}
640module_init(ads7846_init);
641
642static void __exit ads7846_exit(void)
643{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800644 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -0800645
646#ifdef CONFIG_ARCH_OMAP
647 if (machine_is_omap_osk()) {
648 omap_free_gpio(4);
649 omap_free_gpio(6);
650 }
651#endif
652
653}
654module_exit(ads7846_exit);
655
656MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
657MODULE_LICENSE("GPL");