blob: 0a26e0663542bf968b517ac6b0e087ff8cf64237 [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
Imre Deak7de90a82006-04-11 23:43:55 -04005 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
David Brownellffa458c2006-01-08 13:34:21 -08007 *
8 * Using code from:
9 * - corgi_ts.c
10 * Copyright (C) 2004-2005 Richard Purdie
11 * - omap_ts.[hc], ads7846.h, ts_osk.c
12 * Copyright (C) 2002 MontaVista Software
13 * Copyright (C) 2004 Texas Instruments
14 * Copyright (C) 2005 Dirk Behme
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
David Brownell2c8dc072007-01-18 00:45:48 -050020#include <linux/hwmon.h>
David Brownellffa458c2006-01-08 13:34:21 -080021#include <linux/init.h>
David Brownell2c8dc072007-01-18 00:45:48 -050022#include <linux/err.h>
David Brownellffa458c2006-01-08 13:34:21 -080023#include <linux/delay.h>
24#include <linux/input.h>
25#include <linux/interrupt.h>
26#include <linux/slab.h>
27#include <linux/spi/spi.h>
28#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080029#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080030
31#ifdef CONFIG_ARM
32#include <asm/mach-types.h>
33#ifdef CONFIG_ARCH_OMAP
34#include <asm/arch/gpio.h>
35#endif
David Brownellffa458c2006-01-08 13:34:21 -080036#endif
37
38
39/*
David Brownell90845332006-05-25 18:44:20 -070040 * This code has been heavily tested on a Nokia 770, and lightly
41 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
David Brownellffa458c2006-01-08 13:34:21 -080042 * Support for ads7843 and ads7845 has only been stubbed in.
43 *
Imre Deak7de90a82006-04-11 23:43:55 -040044 * IRQ handling needs a workaround because of a shortcoming in handling
45 * edge triggered IRQs on some platforms like the OMAP1/2. These
46 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
47 * have to maintain our own SW IRQ disabled status. This should be
48 * removed as soon as the affected platform's IRQ handling is fixed.
49 *
David Brownellffa458c2006-01-08 13:34:21 -080050 * app note sbaa036 talks in more detail about accurate sampling...
51 * that ought to help in situations like LCDs inducing noise (which
52 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040053 * This driver tries to utilize the measures described in the app
54 * note. The strength of filtering can be set in the board-* specific
55 * files.
David Brownellffa458c2006-01-08 13:34:21 -080056 */
57
Imre Deak1936d592007-01-18 00:45:31 -050058#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
59#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080060
David Brownelld93f70b2006-02-15 00:49:35 -050061/* this driver doesn't aim at the peak continuous sample rate */
62#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
63
David Brownellffa458c2006-01-08 13:34:21 -080064struct ts_event {
65 /* For portability, we can't read 12 bit values using SPI (which
66 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050067 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050068 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080069 */
Imre Deakda970e62007-01-18 00:44:41 -050070 u16 x;
71 u16 y;
72 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050073 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080074};
75
76struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050077 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080078 char phys[32];
79
80 struct spi_device *spi;
David Brownell2c8dc072007-01-18 00:45:48 -050081
82#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050083 struct attribute_group *attr_group;
David Brownell2c8dc072007-01-18 00:45:48 -050084 struct class_device *hwmon;
85#endif
86
David Brownellffa458c2006-01-08 13:34:21 -080087 u16 model;
88 u16 vref_delay_usecs;
89 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040090 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080091
Imre Deak53a0ef892006-04-11 23:41:49 -040092 u8 read_x, read_y, read_z1, read_z2, pwrdown;
93 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080094 struct ts_event tc;
95
Imre Deak53a0ef892006-04-11 23:41:49 -040096 struct spi_transfer xfer[10];
Imre Deak0b7018a2006-04-11 23:42:03 -040097 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040098 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040099 int msg_idx;
100 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400101 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400102 int last_read;
103
104 u16 debounce_max;
105 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400106 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800107
108 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500109 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800110 unsigned pendown:1; /* P: lock */
111 unsigned pending:1; /* P: lock */
112// FIXME remove "irq_disabled"
113 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400114 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400115
Imre Deakda970e62007-01-18 00:44:41 -0500116 int (*filter)(void *data, int data_idx, int *val);
117 void *filter_data;
118 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400119 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800120};
121
122/* leave chip selected when we're done, for quicker re-select? */
123#if 0
124#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
125#else
126#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
127#endif
128
129/*--------------------------------------------------------------------------*/
130
131/* The ADS7846 has touchscreen and other sensors.
132 * Earlier ads784x chips are somewhat compatible.
133 */
134#define ADS_START (1 << 7)
135#define ADS_A2A1A0_d_y (1 << 4) /* differential */
136#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
137#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
138#define ADS_A2A1A0_d_x (5 << 4) /* differential */
139#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
140#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
141#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
142#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
143#define ADS_8_BIT (1 << 3)
144#define ADS_12_BIT (0 << 3)
145#define ADS_SER (1 << 2) /* non-differential */
146#define ADS_DFR (0 << 2) /* differential */
147#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
148#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
149#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
150#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
151
152#define MAX_12BIT ((1<<12)-1)
153
154/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500155#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
156 | ADS_12_BIT | ADS_DFR | \
157 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800158
Imre Deakde2defd2007-01-18 00:45:21 -0500159#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
160#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
161#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400162
Imre Deakde2defd2007-01-18 00:45:21 -0500163#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
164#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800165
166/* single-ended samples need to first power up reference voltage;
167 * we leave both ADC and VREF powered
168 */
169#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
170 | ADS_12_BIT | ADS_SER)
171
Imre Deakde2defd2007-01-18 00:45:21 -0500172#define REF_ON (READ_12BIT_DFR(x, 1, 1))
173#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800174
175/*--------------------------------------------------------------------------*/
176
177/*
178 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500179 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
180 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800181 */
David Brownell2c8dc072007-01-18 00:45:48 -0500182static unsigned vREF_mV;
183module_param(vREF_mV, uint, 0);
184MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
David Brownellffa458c2006-01-08 13:34:21 -0800185
186struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500187 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800188 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500189 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800190 u16 scratch;
191 __be16 sample;
192 struct spi_message msg;
193 struct spi_transfer xfer[6];
194};
195
Imre Deak7de90a82006-04-11 23:43:55 -0400196static void ads7846_enable(struct ads7846 *ts);
197static void ads7846_disable(struct ads7846 *ts);
198
Imre Deakc9e617a2006-04-11 23:44:05 -0400199static int device_suspended(struct device *dev)
200{
201 struct ads7846 *ts = dev_get_drvdata(dev);
202 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
203}
204
David Brownellffa458c2006-01-08 13:34:21 -0800205static int ads7846_read12_ser(struct device *dev, unsigned command)
206{
207 struct spi_device *spi = to_spi_device(dev);
208 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800209 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800210 int status;
211 int sample;
David Brownell2c8dc072007-01-18 00:45:48 -0500212 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800213
214 if (!req)
215 return -ENOMEM;
216
Imre Deak0b7018a2006-04-11 23:42:03 -0400217 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800218
David Brownell2c8dc072007-01-18 00:45:48 -0500219 /* FIXME boards with ads7846 might use external vref instead ... */
220 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800221
David Brownell2c8dc072007-01-18 00:45:48 -0500222 /* maybe turn on internal vREF, and let it settle */
223 if (use_internal) {
224 req->ref_on = REF_ON;
225 req->xfer[0].tx_buf = &req->ref_on;
226 req->xfer[0].len = 1;
227 spi_message_add_tail(&req->xfer[0], &req->msg);
228
229 req->xfer[1].rx_buf = &req->scratch;
230 req->xfer[1].len = 2;
231
232 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
233 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
234 spi_message_add_tail(&req->xfer[1], &req->msg);
235 }
David Brownellffa458c2006-01-08 13:34:21 -0800236
237 /* take sample */
238 req->command = (u8) command;
239 req->xfer[2].tx_buf = &req->command;
240 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500241 spi_message_add_tail(&req->xfer[2], &req->msg);
242
David Brownellffa458c2006-01-08 13:34:21 -0800243 req->xfer[3].rx_buf = &req->sample;
244 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500245 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800246
247 /* REVISIT: take a few more samples, and compare ... */
248
David Brownell2c8dc072007-01-18 00:45:48 -0500249 /* maybe off internal vREF */
250 if (use_internal) {
251 req->ref_off = REF_OFF;
252 req->xfer[4].tx_buf = &req->ref_off;
253 req->xfer[4].len = 1;
254 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800255
David Brownell2c8dc072007-01-18 00:45:48 -0500256 req->xfer[5].rx_buf = &req->scratch;
257 req->xfer[5].len = 2;
258 CS_CHANGE(req->xfer[5]);
259 spi_message_add_tail(&req->xfer[5], &req->msg);
260 }
David Brownellffa458c2006-01-08 13:34:21 -0800261
Imre Deakc9e617a2006-04-11 23:44:05 -0400262 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800263 disable_irq(spi->irq);
264 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400265 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800266 enable_irq(spi->irq);
267
268 if (req->msg.status)
269 status = req->msg.status;
David Brownellffa458c2006-01-08 13:34:21 -0800270
David Brownell90845332006-05-25 18:44:20 -0700271 /* on-wire is a must-ignore bit, a BE12 value, then padding */
272 sample = be16_to_cpu(req->sample);
273 sample = sample >> 3;
274 sample &= 0x0fff;
275
276 kfree(req);
David Brownellffa458c2006-01-08 13:34:21 -0800277 return status ? status : sample;
278}
279
David Brownell2c8dc072007-01-18 00:45:48 -0500280#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
281
282#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800283name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
284{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500285 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800286 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500287 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800288 if (v < 0) \
289 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500290 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800291} \
292static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
293
David Brownell2c8dc072007-01-18 00:45:48 -0500294
295/* Sysfs conventions report temperatures in millidegrees Celcius.
296 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
297 * accuracy scheme without calibration data. For now we won't try either;
298 * userspace sees raw sensor values, and must scale/calibrate appropriately.
299 */
300static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
301{
302 return v;
303}
304
305SHOW(temp0, temp0, null_adjust) /* temp1_input */
306SHOW(temp1, temp1, null_adjust) /* temp2_input */
307
308
309/* sysfs conventions report voltages in millivolts. We can convert voltages
310 * if we know vREF. userspace may need to scale vAUX to match the board's
311 * external resistors; we assume that vBATT only uses the internal ones.
312 */
313static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
314{
315 unsigned retval = v;
316
317 /* external resistors may scale vAUX into 0..vREF */
318 retval *= vREF_mV;
319 retval = retval >> 12;
320 return retval;
321}
322
323static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
324{
325 unsigned retval = vaux_adjust(ts, v);
326
327 /* ads7846 has a resistor ladder to scale this signal down */
328 if (ts->model == 7846)
329 retval *= 4;
330 return retval;
331}
332
333SHOW(in0_input, vaux, vaux_adjust)
334SHOW(in1_input, vbatt, vbatt_adjust)
335
336
337static struct attribute *ads7846_attributes[] = {
338 &dev_attr_temp0.attr,
339 &dev_attr_temp1.attr,
340 &dev_attr_in0_input.attr,
341 &dev_attr_in1_input.attr,
342 NULL,
343};
344
345static struct attribute_group ads7846_attr_group = {
346 .attrs = ads7846_attributes,
347};
348
349static struct attribute *ads7843_attributes[] = {
350 &dev_attr_in0_input.attr,
351 &dev_attr_in1_input.attr,
352 NULL,
353};
354
355static struct attribute_group ads7843_attr_group = {
356 .attrs = ads7843_attributes,
357};
358
359static struct attribute *ads7845_attributes[] = {
360 &dev_attr_in0_input.attr,
361 NULL,
362};
363
364static struct attribute_group ads7845_attr_group = {
365 .attrs = ads7845_attributes,
366};
367
368static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
369{
370 struct class_device *hwmon;
371 int err;
372
373 /* hwmon sensors need a reference voltage */
374 switch (ts->model) {
375 case 7846:
376 if (!vREF_mV) {
377 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
378 vREF_mV = 2500;
379 }
380 break;
381 case 7845:
382 case 7843:
383 if (!vREF_mV) {
384 dev_warn(&spi->dev,
385 "external vREF for ADS%d not specified\n",
386 ts->model);
387 return 0;
388 }
389 break;
390 }
391
392 /* different chips have different sensor groups */
393 switch (ts->model) {
394 case 7846:
395 ts->attr_group = &ads7846_attr_group;
396 break;
397 case 7845:
398 ts->attr_group = &ads7845_attr_group;
399 break;
400 case 7843:
401 ts->attr_group = &ads7843_attr_group;
402 break;
403 default:
404 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
405 return 0;
406 }
407
408 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
409 if (err)
410 return err;
411
412 hwmon = hwmon_device_register(&spi->dev);
413 if (IS_ERR(hwmon)) {
414 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
415 return PTR_ERR(hwmon);
416 }
417
418 ts->hwmon = hwmon;
419 return 0;
420}
421
422static void ads784x_hwmon_unregister(struct spi_device *spi,
423 struct ads7846 *ts)
424{
425 if (ts->hwmon) {
426 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
427 hwmon_device_unregister(ts->hwmon);
428 }
429}
430
431#else
432static inline int ads784x_hwmon_register(struct spi_device *spi,
433 struct ads7846 *ts)
434{
435 return 0;
436}
437
438static inline void ads784x_hwmon_unregister(struct spi_device *spi,
439 struct ads7846 *ts)
440{
441}
442#endif
David Brownellffa458c2006-01-08 13:34:21 -0800443
Imre Deak438f2a72006-04-11 23:41:32 -0400444static int is_pen_down(struct device *dev)
445{
David Brownell2c8dc072007-01-18 00:45:48 -0500446 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400447
448 return ts->pendown;
449}
450
451static ssize_t ads7846_pen_down_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
454 return sprintf(buf, "%u\n", is_pen_down(dev));
455}
456
457static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
458
Imre Deak7de90a82006-04-11 23:43:55 -0400459static ssize_t ads7846_disable_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct ads7846 *ts = dev_get_drvdata(dev);
463
464 return sprintf(buf, "%u\n", ts->disabled);
465}
466
467static ssize_t ads7846_disable_store(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
470{
471 struct ads7846 *ts = dev_get_drvdata(dev);
472 char *endp;
473 int i;
474
475 i = simple_strtoul(buf, &endp, 10);
476 spin_lock_irq(&ts->lock);
477
478 if (i)
479 ads7846_disable(ts);
480 else
481 ads7846_enable(ts);
482
483 spin_unlock_irq(&ts->lock);
484
485 return count;
486}
487
488static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
489
David Brownell2c8dc072007-01-18 00:45:48 -0500490static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500491 &dev_attr_pen_down.attr,
492 &dev_attr_disable.attr,
493 NULL,
494};
495
David Brownell2c8dc072007-01-18 00:45:48 -0500496static struct attribute_group ads784x_attr_group = {
497 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500498};
499
David Brownellffa458c2006-01-08 13:34:21 -0800500/*--------------------------------------------------------------------------*/
501
502/*
503 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
504 * to retrieve touchscreen status.
505 *
506 * The SPI transfer completion callback does the real work. It reports
507 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
508 */
509
510static void ads7846_rx(void *ads)
511{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500512 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500513 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500514 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800515
Imre Deakda970e62007-01-18 00:44:41 -0500516 /* ads7846_rx_val() did in-place conversion (including byteswap) from
517 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800518 */
Imre Deakda970e62007-01-18 00:44:41 -0500519 x = ts->tc.x;
520 y = ts->tc.y;
521 z1 = ts->tc.z1;
522 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800523
524 /* range filtering */
525 if (x == MAX_12BIT)
526 x = 0;
527
Imre Deak15e35892007-01-18 00:45:43 -0500528 if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800529 /* compute touch pressure resistance using equation #2 */
530 Rt = z2;
531 Rt -= z1;
532 Rt *= x;
533 Rt *= ts->x_plate_ohms;
534 Rt /= z1;
535 Rt = (Rt + 2047) >> 12;
536 } else
537 Rt = 0;
538
Imre Deakd5b415c2006-04-26 00:13:18 -0400539 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500540 * the maximum. Don't report it to user space, repeat at least
541 * once more the measurement
542 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400543 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500544#ifdef VERBOSE
545 pr_debug("%s: ignored %d pressure %d\n",
546 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
547#endif
Imre Deak1936d592007-01-18 00:45:31 -0500548 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800549 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400550 return;
551 }
552
Imre Deak15e35892007-01-18 00:45:43 -0500553 /* NOTE: We can't rely on the pressure to determine the pen down
554 * state, even this controller has a pressure sensor. The pressure
555 * value can fluctuate for quite a while after lifting the pen and
556 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800557 *
Imre Deak15e35892007-01-18 00:45:43 -0500558 * The only safe way to check for the pen up condition is in the
559 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800560 */
David Brownellffa458c2006-01-08 13:34:21 -0800561 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500562 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400563
Imre Deak15e35892007-01-18 00:45:43 -0500564 if (!ts->pendown) {
565 input_report_key(input, BTN_TOUCH, 1);
566 ts->pendown = 1;
567#ifdef VERBOSE
568 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800569#endif
Imre Deak15e35892007-01-18 00:45:43 -0500570 }
571 input_report_abs(input, ABS_X, x);
572 input_report_abs(input, ABS_Y, y);
573 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800574
Imre Deak15e35892007-01-18 00:45:43 -0500575 input_sync(input);
576#ifdef VERBOSE
577 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
578#endif
579 }
David Brownellffa458c2006-01-08 13:34:21 -0800580
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800581 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
582 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800583}
584
Imre Deakda970e62007-01-18 00:44:41 -0500585static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400586{
587 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400588
Imre Deakda970e62007-01-18 00:44:41 -0500589 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
590 /* Start over collecting consistent readings. */
591 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400592 /* Repeat it, if this was the first read or the read
593 * wasn't consistent enough. */
594 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500595 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400596 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500597 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400598 } else {
599 /* Maximum number of debouncing reached and still
600 * not enough number of consistent readings. Abort
601 * the whole sample, repeat it in the next sampling
602 * period.
603 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400604 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500605 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400606 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400607 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400608 if (++ts->read_rep > ts->debounce_rep) {
609 /* Got a good reading for this coordinate,
610 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400611 ts->read_cnt = 0;
612 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500613 return ADS7846_FILTER_OK;
614 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400615 /* Read more values that are consistent. */
616 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500617 return ADS7846_FILTER_REPEAT;
618 }
619 }
620}
621
622static int ads7846_no_filter(void *ads, int data_idx, int *val)
623{
624 return ADS7846_FILTER_OK;
625}
626
627static void ads7846_rx_val(void *ads)
628{
629 struct ads7846 *ts = ads;
630 struct spi_message *m;
631 struct spi_transfer *t;
632 u16 *rx_val;
633 int val;
634 int action;
635 int status;
636
637 m = &ts->msg[ts->msg_idx];
638 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
639 rx_val = t->rx_buf;
640
641 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
642 * built from two 8 bit values written msb-first.
643 */
644 val = be16_to_cpu(*rx_val) >> 3;
645
646 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
647 switch (action) {
648 case ADS7846_FILTER_REPEAT:
649 break;
650 case ADS7846_FILTER_IGNORE:
651 ts->tc.ignore = 1;
652 /* Last message will contain ads7846_rx() as the
653 * completion function.
654 */
655 m = ts->last_msg;
656 break;
657 case ADS7846_FILTER_OK:
658 *rx_val = val;
659 ts->tc.ignore = 0;
660 m = &ts->msg[++ts->msg_idx];
661 break;
662 default:
663 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400664 }
665 status = spi_async(ts->spi, m);
666 if (status)
667 dev_err(&ts->spi->dev, "spi_async --> %d\n",
668 status);
669}
670
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800671static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800672{
Imre Deak1936d592007-01-18 00:45:31 -0500673 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800674 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800675
Imre Deakc9e617a2006-04-11 23:44:05 -0400676 spin_lock_irq(&ts->lock);
677
Imre Deak15e35892007-01-18 00:45:43 -0500678 if (unlikely(!ts->get_pendown_state() ||
679 device_suspended(&ts->spi->dev))) {
680 if (ts->pendown) {
681 struct input_dev *input = ts->input;
682
683 input_report_key(input, BTN_TOUCH, 0);
684 input_report_abs(input, ABS_PRESSURE, 0);
685 input_sync(input);
686
687 ts->pendown = 0;
688#ifdef VERBOSE
689 dev_dbg(&ts->spi->dev, "UP\n");
690#endif
691 }
692
David Brownell90845332006-05-25 18:44:20 -0700693 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400694 if (!device_suspended(&ts->spi->dev)) {
695 ts->irq_disabled = 0;
696 enable_irq(ts->spi->irq);
697 }
698 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400699 } else {
700 /* pen is still down, continue with the measurement */
701 ts->msg_idx = 0;
702 status = spi_async(ts->spi, &ts->msg[0]);
703 if (status)
704 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
705 }
706
707 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500708 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800709}
710
David Howells7d12e782006-10-05 14:55:46 +0100711static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800712{
Imre Deak0b7018a2006-04-11 23:42:03 -0400713 struct ads7846 *ts = handle;
714 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400715
716 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400717 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400718 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700719 /* The ARM do_simple_IRQ() dispatcher doesn't act
720 * like the other dispatchers: it will report IRQs
721 * even after they've been disabled. We work around
722 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400723 */
724 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400725 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400726 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500727 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800728 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400729 }
730 }
731 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400732
733 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800734}
735
736/*--------------------------------------------------------------------------*/
737
Imre Deak7de90a82006-04-11 23:43:55 -0400738/* Must be called with ts->lock held */
739static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800740{
Imre Deak7de90a82006-04-11 23:43:55 -0400741 if (ts->disabled)
742 return;
David Brownellffa458c2006-01-08 13:34:21 -0800743
Imre Deakc9e617a2006-04-11 23:44:05 -0400744 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800745
Imre Deakc9e617a2006-04-11 23:44:05 -0400746 /* are we waiting for IRQ, or polling? */
747 if (!ts->pending) {
748 ts->irq_disabled = 1;
749 disable_irq(ts->spi->irq);
750 } else {
751 /* the timer will run at least once more, and
752 * leave everything in a clean state, IRQ disabled
753 */
754 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400755 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400756 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400757 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800758 }
759 }
760
761 /* we know the chip's in lowpower mode since we always
762 * leave it that way after every request
763 */
764
Imre Deak7de90a82006-04-11 23:43:55 -0400765}
766
767/* Must be called with ts->lock held */
768static void ads7846_enable(struct ads7846 *ts)
769{
770 if (!ts->disabled)
771 return;
772
773 ts->disabled = 0;
774 ts->irq_disabled = 0;
775 enable_irq(ts->spi->irq);
776}
777
778static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
779{
780 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
781
782 spin_lock_irq(&ts->lock);
783
784 spi->dev.power.power_state = message;
785 ads7846_disable(ts);
786
787 spin_unlock_irq(&ts->lock);
788
David Brownellffa458c2006-01-08 13:34:21 -0800789 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400790
David Brownellffa458c2006-01-08 13:34:21 -0800791}
792
David Brownell2e5a7bd2006-01-08 13:34:25 -0800793static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800794{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800795 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800796
Imre Deak7de90a82006-04-11 23:43:55 -0400797 spin_lock_irq(&ts->lock);
798
David Brownell2e5a7bd2006-01-08 13:34:25 -0800799 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400800 ads7846_enable(ts);
801
802 spin_unlock_irq(&ts->lock);
803
David Brownellffa458c2006-01-08 13:34:21 -0800804 return 0;
805}
806
David Brownell2e5a7bd2006-01-08 13:34:25 -0800807static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800808{
David Brownellffa458c2006-01-08 13:34:21 -0800809 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500810 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800811 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400812 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800813 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500814 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500815 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800816
817 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800818 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800819 return -ENODEV;
820 }
821
822 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800823 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800824 return -ENODEV;
825 }
826
827 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500828 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800829 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500830 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800831 return -EINVAL;
832 }
833
David Brownell90845332006-05-25 18:44:20 -0700834 /* REVISIT when the irq can be triggered active-low, or if for some
835 * reason the touchscreen isn't hooked up, we don't need to access
836 * the pendown state.
837 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400838 if (pdata->get_pendown_state == NULL) {
839 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
840 return -EINVAL;
841 }
842
David Brownell90845332006-05-25 18:44:20 -0700843 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
844 * that even if the hardware can do that, the SPI controller driver
845 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800846 */
David Brownell90845332006-05-25 18:44:20 -0700847 spi->bits_per_word = 8;
Imre Deak7937e862007-01-18 00:45:38 -0500848 spi->mode = SPI_MODE_1;
849 err = spi_setup(spi);
850 if (err < 0)
851 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800852
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500853 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
854 input_dev = input_allocate_device();
855 if (!ts || !input_dev) {
856 err = -ENOMEM;
857 goto err_free_mem;
858 }
David Brownellffa458c2006-01-08 13:34:21 -0800859
David Brownell2e5a7bd2006-01-08 13:34:25 -0800860 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500861 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800862
863 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500864 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800865
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800866 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800867 ts->timer.function = ads7846_timer;
868
Imre Deak7de90a82006-04-11 23:43:55 -0400869 spin_lock_init(&ts->lock);
870
David Brownellffa458c2006-01-08 13:34:21 -0800871 ts->model = pdata->model ? : 7846;
872 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
873 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400874 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500875
876 if (pdata->filter != NULL) {
877 if (pdata->filter_init != NULL) {
878 err = pdata->filter_init(pdata, &ts->filter_data);
879 if (err < 0)
880 goto err_free_mem;
881 }
882 ts->filter = pdata->filter;
883 ts->filter_cleanup = pdata->filter_cleanup;
884 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400885 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500886 if (ts->debounce_max < 2)
887 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400888 ts->debounce_tol = pdata->debounce_tol;
889 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500890 ts->filter = ads7846_debounce;
891 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400892 } else
Imre Deakda970e62007-01-18 00:44:41 -0500893 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400894 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800895
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500896 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800897
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500898 input_dev->name = "ADS784x Touchscreen";
899 input_dev->phys = ts->phys;
900 input_dev->cdev.dev = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800901
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500902 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
903 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
904 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800905 pdata->x_min ? : 0,
906 pdata->x_max ? : MAX_12BIT,
907 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500908 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800909 pdata->y_min ? : 0,
910 pdata->y_max ? : MAX_12BIT,
911 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500912 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800913 pdata->pressure_min, pdata->pressure_max, 0, 0);
914
Imre Deakde2defd2007-01-18 00:45:21 -0500915 vref = pdata->keep_vref_on;
916
David Brownellffa458c2006-01-08 13:34:21 -0800917 /* set up the transfers to read touchscreen state; this assumes we
918 * use formula #2 for pressure, not #3.
919 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400920 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800921 x = ts->xfer;
922
Imre Deak0b7018a2006-04-11 23:42:03 -0400923 spi_message_init(m);
924
David Brownellffa458c2006-01-08 13:34:21 -0800925 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500926 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500927 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800928 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400929 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500930
David Brownellffa458c2006-01-08 13:34:21 -0800931 x++;
932 x->rx_buf = &ts->tc.y;
933 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400934 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800935
Imre Deakda970e62007-01-18 00:44:41 -0500936 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400937 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500938
Imre Deak0b7018a2006-04-11 23:42:03 -0400939 m++;
940 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800941
942 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500943 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500944 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500945 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800946 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400947 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500948
David Brownellffa458c2006-01-08 13:34:21 -0800949 x++;
950 x->rx_buf = &ts->tc.x;
951 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400952 spi_message_add_tail(x, m);
953
Imre Deakda970e62007-01-18 00:44:41 -0500954 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400955 m->context = ts;
956
957 /* turn y+ off, x- on; we'll use formula #2 */
958 if (ts->model == 7846) {
959 m++;
960 spi_message_init(m);
961
962 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500963 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400964 x->tx_buf = &ts->read_z1;
965 x->len = 1;
966 spi_message_add_tail(x, m);
967
968 x++;
969 x->rx_buf = &ts->tc.z1;
970 x->len = 2;
971 spi_message_add_tail(x, m);
972
Imre Deakda970e62007-01-18 00:44:41 -0500973 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400974 m->context = ts;
975
976 m++;
977 spi_message_init(m);
978
979 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500980 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -0400981 x->tx_buf = &ts->read_z2;
982 x->len = 1;
983 spi_message_add_tail(x, m);
984
985 x++;
986 x->rx_buf = &ts->tc.z2;
987 x->len = 2;
988 spi_message_add_tail(x, m);
989
Imre Deakda970e62007-01-18 00:44:41 -0500990 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400991 m->context = ts;
992 }
Imre Deak53a0ef892006-04-11 23:41:49 -0400993
994 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -0400995 m++;
996 spi_message_init(m);
997
Imre Deak53a0ef892006-04-11 23:41:49 -0400998 x++;
999 ts->pwrdown = PWRDOWN;
1000 x->tx_buf = &ts->pwrdown;
1001 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001002 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001003
1004 x++;
1005 x->rx_buf = &ts->dummy;
1006 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001007 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001008 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001009
Imre Deak0b7018a2006-04-11 23:42:03 -04001010 m->complete = ads7846_rx;
1011 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001012
Imre Deakd5b415c2006-04-26 00:13:18 -04001013 ts->last_msg = m;
1014
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001015 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001016 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001017 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001018 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001019 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001020 }
David Brownellffa458c2006-01-08 13:34:21 -08001021
David Brownell2c8dc072007-01-18 00:45:48 -05001022 err = ads784x_hwmon_register(spi, ts);
1023 if (err)
1024 goto err_free_irq;
1025
David Brownell2e5a7bd2006-01-08 13:34:25 -08001026 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001027
David Brownell2c8dc072007-01-18 00:45:48 -05001028 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001029 * the touchscreen, in case it's not connected.
1030 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001031 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001032 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1033
David Brownell2c8dc072007-01-18 00:45:48 -05001034 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001035 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001036 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001037
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001038 err = input_register_device(input_dev);
1039 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001040 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001041
David Brownellffa458c2006-01-08 13:34:21 -08001042 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001043
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001044 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001045 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1046 err_remove_hwmon:
1047 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001048 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001049 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001050 err_cleanup_filter:
1051 if (ts->filter_cleanup)
1052 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001053 err_free_mem:
1054 input_free_device(input_dev);
1055 kfree(ts);
1056 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001057}
1058
David Brownell2e5a7bd2006-01-08 13:34:25 -08001059static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001060{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001061 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001062
David Brownell2c8dc072007-01-18 00:45:48 -05001063 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001064 input_unregister_device(ts->input);
1065
David Brownell2e5a7bd2006-01-08 13:34:25 -08001066 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001067
David Brownell2c8dc072007-01-18 00:45:48 -05001068 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001069
Imre Deak7de90a82006-04-11 23:43:55 -04001070 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001071 /* suspend left the IRQ disabled */
1072 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001073
Imre Deakda970e62007-01-18 00:44:41 -05001074 if (ts->filter_cleanup)
1075 ts->filter_cleanup(ts->filter_data);
1076
David Brownellffa458c2006-01-08 13:34:21 -08001077 kfree(ts);
1078
David Brownell2e5a7bd2006-01-08 13:34:25 -08001079 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001080 return 0;
1081}
1082
David Brownell2e5a7bd2006-01-08 13:34:25 -08001083static struct spi_driver ads7846_driver = {
1084 .driver = {
1085 .name = "ads7846",
1086 .bus = &spi_bus_type,
1087 .owner = THIS_MODULE,
1088 },
David Brownellffa458c2006-01-08 13:34:21 -08001089 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001090 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001091 .suspend = ads7846_suspend,
1092 .resume = ads7846_resume,
1093};
1094
1095static int __init ads7846_init(void)
1096{
1097 /* grr, board-specific init should stay out of drivers!! */
1098
1099#ifdef CONFIG_ARCH_OMAP
1100 if (machine_is_omap_osk()) {
1101 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1102 omap_request_gpio(4);
1103 omap_set_gpio_direction(4, 1);
1104 omap_request_gpio(6);
1105 omap_set_gpio_direction(6, 1);
1106 }
1107 // also TI 1510 Innovator, bitbanging through FPGA
1108 // also Nokia 770
1109 // also Palm Tungsten T2
1110#endif
1111
1112 // PXA:
1113 // also Dell Axim X50
1114 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -08001115 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -08001116 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1117
David Brownell2e5a7bd2006-01-08 13:34:25 -08001118 // Atmel at91sam9261-EK uses ads7843
1119
David Brownellffa458c2006-01-08 13:34:21 -08001120 // also various AMD Au1x00 devel boards
1121
David Brownell2e5a7bd2006-01-08 13:34:25 -08001122 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001123}
1124module_init(ads7846_init);
1125
1126static void __exit ads7846_exit(void)
1127{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001128 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001129
1130#ifdef CONFIG_ARCH_OMAP
1131 if (machine_is_omap_osk()) {
1132 omap_free_gpio(4);
1133 omap_free_gpio(6);
1134 }
1135#endif
1136
1137}
1138module_exit(ads7846_exit);
1139
1140MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1141MODULE_LICENSE("GPL");