blob: 51ae4fb7d123b2882380f1ec78531949c70c59a9 [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 Brownellbff0de52007-05-22 23:28:40 -040042 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050043 * Support for ads7843 tested on Atmel at91sam926x-EK.
44 * Support for ads7845 has only been stubbed in.
David Brownellffa458c2006-01-08 13:34:21 -080045 *
Imre Deak7de90a82006-04-11 23:43:55 -040046 * IRQ handling needs a workaround because of a shortcoming in handling
47 * edge triggered IRQs on some platforms like the OMAP1/2. These
48 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
49 * have to maintain our own SW IRQ disabled status. This should be
50 * removed as soon as the affected platform's IRQ handling is fixed.
51 *
David Brownellffa458c2006-01-08 13:34:21 -080052 * app note sbaa036 talks in more detail about accurate sampling...
53 * that ought to help in situations like LCDs inducing noise (which
54 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040055 * This driver tries to utilize the measures described in the app
56 * note. The strength of filtering can be set in the board-* specific
57 * files.
David Brownellffa458c2006-01-08 13:34:21 -080058 */
59
Imre Deak1936d592007-01-18 00:45:31 -050060#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
61#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080062
David Brownelld93f70b2006-02-15 00:49:35 -050063/* this driver doesn't aim at the peak continuous sample rate */
64#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
65
David Brownellffa458c2006-01-08 13:34:21 -080066struct ts_event {
67 /* For portability, we can't read 12 bit values using SPI (which
68 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050069 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050070 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080071 */
Imre Deakda970e62007-01-18 00:44:41 -050072 u16 x;
73 u16 y;
74 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050075 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080076};
77
78struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050079 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080080 char phys[32];
81
82 struct spi_device *spi;
David Brownell2c8dc072007-01-18 00:45:48 -050083
84#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050085 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070086 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050087#endif
88
David Brownellffa458c2006-01-08 13:34:21 -080089 u16 model;
90 u16 vref_delay_usecs;
91 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040092 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080093
Imre Deak53a0ef892006-04-11 23:41:49 -040094 u8 read_x, read_y, read_z1, read_z2, pwrdown;
95 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080096 struct ts_event tc;
97
Semih Hazare4f48862007-07-18 00:35:56 -040098 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -040099 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400100 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400101 int msg_idx;
102 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400103 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400104 int last_read;
105
106 u16 debounce_max;
107 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400108 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800109
Semih Hazar1d258912007-07-18 00:36:04 -0400110 u16 penirq_recheck_delay_usecs;
111
David Brownellffa458c2006-01-08 13:34:21 -0800112 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500113 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800114 unsigned pendown:1; /* P: lock */
115 unsigned pending:1; /* P: lock */
116// FIXME remove "irq_disabled"
117 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400118 unsigned disabled:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400119
Imre Deakda970e62007-01-18 00:44:41 -0500120 int (*filter)(void *data, int data_idx, int *val);
121 void *filter_data;
122 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400123 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800124};
125
126/* leave chip selected when we're done, for quicker re-select? */
127#if 0
128#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
129#else
130#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
131#endif
132
133/*--------------------------------------------------------------------------*/
134
135/* The ADS7846 has touchscreen and other sensors.
136 * Earlier ads784x chips are somewhat compatible.
137 */
138#define ADS_START (1 << 7)
139#define ADS_A2A1A0_d_y (1 << 4) /* differential */
140#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
141#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
142#define ADS_A2A1A0_d_x (5 << 4) /* differential */
143#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
144#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
145#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
146#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
147#define ADS_8_BIT (1 << 3)
148#define ADS_12_BIT (0 << 3)
149#define ADS_SER (1 << 2) /* non-differential */
150#define ADS_DFR (0 << 2) /* differential */
151#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
152#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
153#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
154#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
155
156#define MAX_12BIT ((1<<12)-1)
157
158/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500159#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
160 | ADS_12_BIT | ADS_DFR | \
161 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800162
Imre Deakde2defd2007-01-18 00:45:21 -0500163#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
164#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
165#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400166
Imre Deakde2defd2007-01-18 00:45:21 -0500167#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
168#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800169
170/* single-ended samples need to first power up reference voltage;
171 * we leave both ADC and VREF powered
172 */
173#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
174 | ADS_12_BIT | ADS_SER)
175
Imre Deakde2defd2007-01-18 00:45:21 -0500176#define REF_ON (READ_12BIT_DFR(x, 1, 1))
177#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800178
179/*--------------------------------------------------------------------------*/
180
181/*
182 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500183 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
184 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800185 */
David Brownell2c8dc072007-01-18 00:45:48 -0500186static unsigned vREF_mV;
187module_param(vREF_mV, uint, 0);
188MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
David Brownellffa458c2006-01-08 13:34:21 -0800189
190struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500191 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800192 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500193 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800194 u16 scratch;
195 __be16 sample;
196 struct spi_message msg;
197 struct spi_transfer xfer[6];
198};
199
Imre Deak7de90a82006-04-11 23:43:55 -0400200static void ads7846_enable(struct ads7846 *ts);
201static void ads7846_disable(struct ads7846 *ts);
202
Imre Deakc9e617a2006-04-11 23:44:05 -0400203static int device_suspended(struct device *dev)
204{
205 struct ads7846 *ts = dev_get_drvdata(dev);
206 return dev->power.power_state.event != PM_EVENT_ON || ts->disabled;
207}
208
David Brownellffa458c2006-01-08 13:34:21 -0800209static int ads7846_read12_ser(struct device *dev, unsigned command)
210{
211 struct spi_device *spi = to_spi_device(dev);
212 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800213 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800214 int status;
215 int sample;
David Brownell2c8dc072007-01-18 00:45:48 -0500216 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800217
218 if (!req)
219 return -ENOMEM;
220
Imre Deak0b7018a2006-04-11 23:42:03 -0400221 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800222
David Brownell2c8dc072007-01-18 00:45:48 -0500223 /* FIXME boards with ads7846 might use external vref instead ... */
224 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800225
David Brownell2c8dc072007-01-18 00:45:48 -0500226 /* maybe turn on internal vREF, and let it settle */
227 if (use_internal) {
228 req->ref_on = REF_ON;
229 req->xfer[0].tx_buf = &req->ref_on;
230 req->xfer[0].len = 1;
231 spi_message_add_tail(&req->xfer[0], &req->msg);
232
233 req->xfer[1].rx_buf = &req->scratch;
234 req->xfer[1].len = 2;
235
236 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
237 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
238 spi_message_add_tail(&req->xfer[1], &req->msg);
239 }
David Brownellffa458c2006-01-08 13:34:21 -0800240
241 /* take sample */
242 req->command = (u8) command;
243 req->xfer[2].tx_buf = &req->command;
244 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500245 spi_message_add_tail(&req->xfer[2], &req->msg);
246
David Brownellffa458c2006-01-08 13:34:21 -0800247 req->xfer[3].rx_buf = &req->sample;
248 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500249 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800250
251 /* REVISIT: take a few more samples, and compare ... */
252
Nicolas Ferre969111e2007-02-28 23:51:03 -0500253 /* converter in low power mode & enable PENIRQ */
254 req->ref_off = PWRDOWN;
255 req->xfer[4].tx_buf = &req->ref_off;
256 req->xfer[4].len = 1;
257 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800258
Nicolas Ferre969111e2007-02-28 23:51:03 -0500259 req->xfer[5].rx_buf = &req->scratch;
260 req->xfer[5].len = 2;
261 CS_CHANGE(req->xfer[5]);
262 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800263
Imre Deakc9e617a2006-04-11 23:44:05 -0400264 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800265 disable_irq(spi->irq);
266 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400267 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800268 enable_irq(spi->irq);
269
270 if (req->msg.status)
271 status = req->msg.status;
David Brownellffa458c2006-01-08 13:34:21 -0800272
David Brownell90845332006-05-25 18:44:20 -0700273 /* on-wire is a must-ignore bit, a BE12 value, then padding */
274 sample = be16_to_cpu(req->sample);
275 sample = sample >> 3;
276 sample &= 0x0fff;
277
278 kfree(req);
David Brownellffa458c2006-01-08 13:34:21 -0800279 return status ? status : sample;
280}
281
David Brownell2c8dc072007-01-18 00:45:48 -0500282#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
283
284#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800285name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
286{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500287 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800288 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500289 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800290 if (v < 0) \
291 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500292 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800293} \
294static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
295
David Brownell2c8dc072007-01-18 00:45:48 -0500296
297/* Sysfs conventions report temperatures in millidegrees Celcius.
298 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
299 * accuracy scheme without calibration data. For now we won't try either;
300 * userspace sees raw sensor values, and must scale/calibrate appropriately.
301 */
302static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
303{
304 return v;
305}
306
307SHOW(temp0, temp0, null_adjust) /* temp1_input */
308SHOW(temp1, temp1, null_adjust) /* temp2_input */
309
310
311/* sysfs conventions report voltages in millivolts. We can convert voltages
312 * if we know vREF. userspace may need to scale vAUX to match the board's
313 * external resistors; we assume that vBATT only uses the internal ones.
314 */
315static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
316{
317 unsigned retval = v;
318
319 /* external resistors may scale vAUX into 0..vREF */
320 retval *= vREF_mV;
321 retval = retval >> 12;
322 return retval;
323}
324
325static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
326{
327 unsigned retval = vaux_adjust(ts, v);
328
329 /* ads7846 has a resistor ladder to scale this signal down */
330 if (ts->model == 7846)
331 retval *= 4;
332 return retval;
333}
334
335SHOW(in0_input, vaux, vaux_adjust)
336SHOW(in1_input, vbatt, vbatt_adjust)
337
338
339static struct attribute *ads7846_attributes[] = {
340 &dev_attr_temp0.attr,
341 &dev_attr_temp1.attr,
342 &dev_attr_in0_input.attr,
343 &dev_attr_in1_input.attr,
344 NULL,
345};
346
347static struct attribute_group ads7846_attr_group = {
348 .attrs = ads7846_attributes,
349};
350
351static struct attribute *ads7843_attributes[] = {
352 &dev_attr_in0_input.attr,
353 &dev_attr_in1_input.attr,
354 NULL,
355};
356
357static struct attribute_group ads7843_attr_group = {
358 .attrs = ads7843_attributes,
359};
360
361static struct attribute *ads7845_attributes[] = {
362 &dev_attr_in0_input.attr,
363 NULL,
364};
365
366static struct attribute_group ads7845_attr_group = {
367 .attrs = ads7845_attributes,
368};
369
370static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
371{
Tony Jones1beeffe2007-08-20 13:46:20 -0700372 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500373 int err;
374
375 /* hwmon sensors need a reference voltage */
376 switch (ts->model) {
377 case 7846:
378 if (!vREF_mV) {
379 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
380 vREF_mV = 2500;
381 }
382 break;
383 case 7845:
384 case 7843:
385 if (!vREF_mV) {
386 dev_warn(&spi->dev,
387 "external vREF for ADS%d not specified\n",
388 ts->model);
389 return 0;
390 }
391 break;
392 }
393
394 /* different chips have different sensor groups */
395 switch (ts->model) {
396 case 7846:
397 ts->attr_group = &ads7846_attr_group;
398 break;
399 case 7845:
400 ts->attr_group = &ads7845_attr_group;
401 break;
402 case 7843:
403 ts->attr_group = &ads7843_attr_group;
404 break;
405 default:
406 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
407 return 0;
408 }
409
410 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
411 if (err)
412 return err;
413
414 hwmon = hwmon_device_register(&spi->dev);
415 if (IS_ERR(hwmon)) {
416 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
417 return PTR_ERR(hwmon);
418 }
419
420 ts->hwmon = hwmon;
421 return 0;
422}
423
424static void ads784x_hwmon_unregister(struct spi_device *spi,
425 struct ads7846 *ts)
426{
427 if (ts->hwmon) {
428 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
429 hwmon_device_unregister(ts->hwmon);
430 }
431}
432
433#else
434static inline int ads784x_hwmon_register(struct spi_device *spi,
435 struct ads7846 *ts)
436{
437 return 0;
438}
439
440static inline void ads784x_hwmon_unregister(struct spi_device *spi,
441 struct ads7846 *ts)
442{
443}
444#endif
David Brownellffa458c2006-01-08 13:34:21 -0800445
Imre Deak438f2a72006-04-11 23:41:32 -0400446static int is_pen_down(struct device *dev)
447{
David Brownell2c8dc072007-01-18 00:45:48 -0500448 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400449
450 return ts->pendown;
451}
452
453static ssize_t ads7846_pen_down_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455{
456 return sprintf(buf, "%u\n", is_pen_down(dev));
457}
458
459static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
460
Imre Deak7de90a82006-04-11 23:43:55 -0400461static ssize_t ads7846_disable_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
464 struct ads7846 *ts = dev_get_drvdata(dev);
465
466 return sprintf(buf, "%u\n", ts->disabled);
467}
468
469static ssize_t ads7846_disable_store(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
472{
473 struct ads7846 *ts = dev_get_drvdata(dev);
474 char *endp;
475 int i;
476
477 i = simple_strtoul(buf, &endp, 10);
478 spin_lock_irq(&ts->lock);
479
480 if (i)
481 ads7846_disable(ts);
482 else
483 ads7846_enable(ts);
484
485 spin_unlock_irq(&ts->lock);
486
487 return count;
488}
489
490static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
491
David Brownell2c8dc072007-01-18 00:45:48 -0500492static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500493 &dev_attr_pen_down.attr,
494 &dev_attr_disable.attr,
495 NULL,
496};
497
David Brownell2c8dc072007-01-18 00:45:48 -0500498static struct attribute_group ads784x_attr_group = {
499 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500500};
501
David Brownellffa458c2006-01-08 13:34:21 -0800502/*--------------------------------------------------------------------------*/
503
504/*
505 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
506 * to retrieve touchscreen status.
507 *
508 * The SPI transfer completion callback does the real work. It reports
509 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
510 */
511
512static void ads7846_rx(void *ads)
513{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500514 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500515 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500516 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800517
Imre Deakda970e62007-01-18 00:44:41 -0500518 /* ads7846_rx_val() did in-place conversion (including byteswap) from
519 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800520 */
Imre Deakda970e62007-01-18 00:44:41 -0500521 x = ts->tc.x;
522 y = ts->tc.y;
523 z1 = ts->tc.z1;
524 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800525
526 /* range filtering */
527 if (x == MAX_12BIT)
528 x = 0;
529
Imre Deak15e35892007-01-18 00:45:43 -0500530 if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800531 /* compute touch pressure resistance using equation #2 */
532 Rt = z2;
533 Rt -= z1;
534 Rt *= x;
535 Rt *= ts->x_plate_ohms;
536 Rt /= z1;
537 Rt = (Rt + 2047) >> 12;
538 } else
539 Rt = 0;
540
Nicolas Ferre969111e2007-02-28 23:51:03 -0500541 if (ts->model == 7843)
542 Rt = ts->pressure_max / 2;
543
Imre Deakd5b415c2006-04-26 00:13:18 -0400544 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500545 * the maximum. Don't report it to user space, repeat at least
546 * once more the measurement
547 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400548 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500549#ifdef VERBOSE
550 pr_debug("%s: ignored %d pressure %d\n",
551 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
552#endif
Imre Deak1936d592007-01-18 00:45:31 -0500553 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800554 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400555 return;
556 }
557
Semih Hazar1d258912007-07-18 00:36:04 -0400558 /* Maybe check the pendown state before reporting. This discards
559 * false readings when the pen is lifted.
560 */
561 if (ts->penirq_recheck_delay_usecs) {
562 udelay(ts->penirq_recheck_delay_usecs);
563 if (!ts->get_pendown_state())
564 Rt = 0;
565 }
566
Imre Deak15e35892007-01-18 00:45:43 -0500567 /* NOTE: We can't rely on the pressure to determine the pen down
568 * state, even this controller has a pressure sensor. The pressure
569 * value can fluctuate for quite a while after lifting the pen and
570 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800571 *
Imre Deak15e35892007-01-18 00:45:43 -0500572 * The only safe way to check for the pen up condition is in the
573 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800574 */
David Brownellffa458c2006-01-08 13:34:21 -0800575 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500576 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400577
Imre Deak15e35892007-01-18 00:45:43 -0500578 if (!ts->pendown) {
579 input_report_key(input, BTN_TOUCH, 1);
580 ts->pendown = 1;
581#ifdef VERBOSE
582 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800583#endif
Imre Deak15e35892007-01-18 00:45:43 -0500584 }
585 input_report_abs(input, ABS_X, x);
586 input_report_abs(input, ABS_Y, y);
587 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800588
Imre Deak15e35892007-01-18 00:45:43 -0500589 input_sync(input);
590#ifdef VERBOSE
591 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
592#endif
593 }
David Brownellffa458c2006-01-08 13:34:21 -0800594
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800595 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
596 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800597}
598
Imre Deakda970e62007-01-18 00:44:41 -0500599static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400600{
601 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400602
Imre Deakda970e62007-01-18 00:44:41 -0500603 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
604 /* Start over collecting consistent readings. */
605 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400606 /* Repeat it, if this was the first read or the read
607 * wasn't consistent enough. */
608 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500609 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400610 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500611 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400612 } else {
613 /* Maximum number of debouncing reached and still
614 * not enough number of consistent readings. Abort
615 * the whole sample, repeat it in the next sampling
616 * period.
617 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400618 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500619 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400620 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400621 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400622 if (++ts->read_rep > ts->debounce_rep) {
623 /* Got a good reading for this coordinate,
624 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400625 ts->read_cnt = 0;
626 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500627 return ADS7846_FILTER_OK;
628 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400629 /* Read more values that are consistent. */
630 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500631 return ADS7846_FILTER_REPEAT;
632 }
633 }
634}
635
636static int ads7846_no_filter(void *ads, int data_idx, int *val)
637{
638 return ADS7846_FILTER_OK;
639}
640
641static void ads7846_rx_val(void *ads)
642{
643 struct ads7846 *ts = ads;
644 struct spi_message *m;
645 struct spi_transfer *t;
646 u16 *rx_val;
647 int val;
648 int action;
649 int status;
650
651 m = &ts->msg[ts->msg_idx];
652 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
653 rx_val = t->rx_buf;
654
655 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
656 * built from two 8 bit values written msb-first.
657 */
658 val = be16_to_cpu(*rx_val) >> 3;
659
660 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
661 switch (action) {
662 case ADS7846_FILTER_REPEAT:
663 break;
664 case ADS7846_FILTER_IGNORE:
665 ts->tc.ignore = 1;
666 /* Last message will contain ads7846_rx() as the
667 * completion function.
668 */
669 m = ts->last_msg;
670 break;
671 case ADS7846_FILTER_OK:
672 *rx_val = val;
673 ts->tc.ignore = 0;
674 m = &ts->msg[++ts->msg_idx];
675 break;
676 default:
677 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400678 }
679 status = spi_async(ts->spi, m);
680 if (status)
681 dev_err(&ts->spi->dev, "spi_async --> %d\n",
682 status);
683}
684
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800685static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800686{
Imre Deak1936d592007-01-18 00:45:31 -0500687 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800688 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800689
Imre Deakc9e617a2006-04-11 23:44:05 -0400690 spin_lock_irq(&ts->lock);
691
Imre Deak15e35892007-01-18 00:45:43 -0500692 if (unlikely(!ts->get_pendown_state() ||
693 device_suspended(&ts->spi->dev))) {
694 if (ts->pendown) {
695 struct input_dev *input = ts->input;
696
697 input_report_key(input, BTN_TOUCH, 0);
698 input_report_abs(input, ABS_PRESSURE, 0);
699 input_sync(input);
700
701 ts->pendown = 0;
702#ifdef VERBOSE
703 dev_dbg(&ts->spi->dev, "UP\n");
704#endif
705 }
706
David Brownell90845332006-05-25 18:44:20 -0700707 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400708 if (!device_suspended(&ts->spi->dev)) {
709 ts->irq_disabled = 0;
710 enable_irq(ts->spi->irq);
711 }
712 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400713 } else {
714 /* pen is still down, continue with the measurement */
715 ts->msg_idx = 0;
716 status = spi_async(ts->spi, &ts->msg[0]);
717 if (status)
718 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
719 }
720
721 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500722 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800723}
724
David Howells7d12e782006-10-05 14:55:46 +0100725static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800726{
Imre Deak0b7018a2006-04-11 23:42:03 -0400727 struct ads7846 *ts = handle;
728 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400729
730 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400731 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400732 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700733 /* The ARM do_simple_IRQ() dispatcher doesn't act
734 * like the other dispatchers: it will report IRQs
735 * even after they've been disabled. We work around
736 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400737 */
738 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400739 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400740 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500741 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800742 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400743 }
744 }
745 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400746
747 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800748}
749
750/*--------------------------------------------------------------------------*/
751
Imre Deak7de90a82006-04-11 23:43:55 -0400752/* Must be called with ts->lock held */
753static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800754{
Imre Deak7de90a82006-04-11 23:43:55 -0400755 if (ts->disabled)
756 return;
David Brownellffa458c2006-01-08 13:34:21 -0800757
Imre Deakc9e617a2006-04-11 23:44:05 -0400758 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800759
Imre Deakc9e617a2006-04-11 23:44:05 -0400760 /* are we waiting for IRQ, or polling? */
761 if (!ts->pending) {
762 ts->irq_disabled = 1;
763 disable_irq(ts->spi->irq);
764 } else {
765 /* the timer will run at least once more, and
766 * leave everything in a clean state, IRQ disabled
767 */
768 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400769 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400770 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400771 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800772 }
773 }
774
775 /* we know the chip's in lowpower mode since we always
776 * leave it that way after every request
777 */
778
Imre Deak7de90a82006-04-11 23:43:55 -0400779}
780
781/* Must be called with ts->lock held */
782static void ads7846_enable(struct ads7846 *ts)
783{
784 if (!ts->disabled)
785 return;
786
787 ts->disabled = 0;
788 ts->irq_disabled = 0;
789 enable_irq(ts->spi->irq);
790}
791
792static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
793{
794 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
795
796 spin_lock_irq(&ts->lock);
797
798 spi->dev.power.power_state = message;
799 ads7846_disable(ts);
800
801 spin_unlock_irq(&ts->lock);
802
David Brownellffa458c2006-01-08 13:34:21 -0800803 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400804
David Brownellffa458c2006-01-08 13:34:21 -0800805}
806
David Brownell2e5a7bd2006-01-08 13:34:25 -0800807static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800808{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800809 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800810
Imre Deak7de90a82006-04-11 23:43:55 -0400811 spin_lock_irq(&ts->lock);
812
David Brownell2e5a7bd2006-01-08 13:34:25 -0800813 spi->dev.power.power_state = PMSG_ON;
Imre Deak7de90a82006-04-11 23:43:55 -0400814 ads7846_enable(ts);
815
816 spin_unlock_irq(&ts->lock);
817
David Brownellffa458c2006-01-08 13:34:21 -0800818 return 0;
819}
820
David Brownell2e5a7bd2006-01-08 13:34:25 -0800821static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800822{
David Brownellffa458c2006-01-08 13:34:21 -0800823 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500824 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800825 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400826 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800827 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500828 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500829 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800830
831 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800832 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800833 return -ENODEV;
834 }
835
836 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800837 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800838 return -ENODEV;
839 }
840
841 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500842 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800843 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500844 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800845 return -EINVAL;
846 }
847
David Brownell90845332006-05-25 18:44:20 -0700848 /* REVISIT when the irq can be triggered active-low, or if for some
849 * reason the touchscreen isn't hooked up, we don't need to access
850 * the pendown state.
851 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400852 if (pdata->get_pendown_state == NULL) {
853 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
854 return -EINVAL;
855 }
856
David Brownell90845332006-05-25 18:44:20 -0700857 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
858 * that even if the hardware can do that, the SPI controller driver
859 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800860 */
David Brownell90845332006-05-25 18:44:20 -0700861 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400862 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500863 err = spi_setup(spi);
864 if (err < 0)
865 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800866
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500867 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
868 input_dev = input_allocate_device();
869 if (!ts || !input_dev) {
870 err = -ENOMEM;
871 goto err_free_mem;
872 }
David Brownellffa458c2006-01-08 13:34:21 -0800873
David Brownell2e5a7bd2006-01-08 13:34:25 -0800874 dev_set_drvdata(&spi->dev, ts);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500875 spi->dev.power.power_state = PMSG_ON;
David Brownellffa458c2006-01-08 13:34:21 -0800876
877 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500878 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800879
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800880 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800881 ts->timer.function = ads7846_timer;
882
Imre Deak7de90a82006-04-11 23:43:55 -0400883 spin_lock_init(&ts->lock);
884
David Brownellffa458c2006-01-08 13:34:21 -0800885 ts->model = pdata->model ? : 7846;
886 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
887 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400888 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500889
890 if (pdata->filter != NULL) {
891 if (pdata->filter_init != NULL) {
892 err = pdata->filter_init(pdata, &ts->filter_data);
893 if (err < 0)
894 goto err_free_mem;
895 }
896 ts->filter = pdata->filter;
897 ts->filter_cleanup = pdata->filter_cleanup;
898 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400899 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500900 if (ts->debounce_max < 2)
901 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400902 ts->debounce_tol = pdata->debounce_tol;
903 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500904 ts->filter = ads7846_debounce;
905 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400906 } else
Imre Deakda970e62007-01-18 00:44:41 -0500907 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400908 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800909
Semih Hazar1d258912007-07-18 00:36:04 -0400910 if (pdata->penirq_recheck_delay_usecs)
911 ts->penirq_recheck_delay_usecs =
912 pdata->penirq_recheck_delay_usecs;
913
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500914 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800915
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500916 input_dev->name = "ADS784x Touchscreen";
917 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400918 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800919
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500920 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
921 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
922 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800923 pdata->x_min ? : 0,
924 pdata->x_max ? : MAX_12BIT,
925 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500926 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800927 pdata->y_min ? : 0,
928 pdata->y_max ? : MAX_12BIT,
929 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500930 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800931 pdata->pressure_min, pdata->pressure_max, 0, 0);
932
Imre Deakde2defd2007-01-18 00:45:21 -0500933 vref = pdata->keep_vref_on;
934
David Brownellffa458c2006-01-08 13:34:21 -0800935 /* set up the transfers to read touchscreen state; this assumes we
936 * use formula #2 for pressure, not #3.
937 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400938 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800939 x = ts->xfer;
940
Imre Deak0b7018a2006-04-11 23:42:03 -0400941 spi_message_init(m);
942
David Brownellffa458c2006-01-08 13:34:21 -0800943 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500944 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500945 x->tx_buf = &ts->read_y;
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.y;
951 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400952 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800953
Semih Hazare4f48862007-07-18 00:35:56 -0400954 /* the first sample after switching drivers can be low quality;
955 * optionally discard it, using a second one after the signals
956 * have had enough time to stabilize.
957 */
958 if (pdata->settle_delay_usecs) {
959 x->delay_usecs = pdata->settle_delay_usecs;
960
961 x++;
962 x->tx_buf = &ts->read_y;
963 x->len = 1;
964 spi_message_add_tail(x, m);
965
966 x++;
967 x->rx_buf = &ts->tc.y;
968 x->len = 2;
969 spi_message_add_tail(x, m);
970 }
971
Imre Deakda970e62007-01-18 00:44:41 -0500972 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400973 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500974
Imre Deak0b7018a2006-04-11 23:42:03 -0400975 m++;
976 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800977
978 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500979 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500980 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500981 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800982 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400983 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500984
David Brownellffa458c2006-01-08 13:34:21 -0800985 x++;
986 x->rx_buf = &ts->tc.x;
987 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400988 spi_message_add_tail(x, m);
989
Semih Hazare4f48862007-07-18 00:35:56 -0400990 /* ... maybe discard first sample ... */
991 if (pdata->settle_delay_usecs) {
992 x->delay_usecs = pdata->settle_delay_usecs;
993
994 x++;
995 x->tx_buf = &ts->read_x;
996 x->len = 1;
997 spi_message_add_tail(x, m);
998
999 x++;
1000 x->rx_buf = &ts->tc.x;
1001 x->len = 2;
1002 spi_message_add_tail(x, m);
1003 }
1004
Imre Deakda970e62007-01-18 00:44:41 -05001005 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001006 m->context = ts;
1007
1008 /* turn y+ off, x- on; we'll use formula #2 */
1009 if (ts->model == 7846) {
1010 m++;
1011 spi_message_init(m);
1012
1013 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001014 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001015 x->tx_buf = &ts->read_z1;
1016 x->len = 1;
1017 spi_message_add_tail(x, m);
1018
1019 x++;
1020 x->rx_buf = &ts->tc.z1;
1021 x->len = 2;
1022 spi_message_add_tail(x, m);
1023
Semih Hazare4f48862007-07-18 00:35:56 -04001024 /* ... maybe discard first sample ... */
1025 if (pdata->settle_delay_usecs) {
1026 x->delay_usecs = pdata->settle_delay_usecs;
1027
1028 x++;
1029 x->tx_buf = &ts->read_z1;
1030 x->len = 1;
1031 spi_message_add_tail(x, m);
1032
1033 x++;
1034 x->rx_buf = &ts->tc.z1;
1035 x->len = 2;
1036 spi_message_add_tail(x, m);
1037 }
1038
Imre Deakda970e62007-01-18 00:44:41 -05001039 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001040 m->context = ts;
1041
1042 m++;
1043 spi_message_init(m);
1044
1045 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001046 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001047 x->tx_buf = &ts->read_z2;
1048 x->len = 1;
1049 spi_message_add_tail(x, m);
1050
1051 x++;
1052 x->rx_buf = &ts->tc.z2;
1053 x->len = 2;
1054 spi_message_add_tail(x, m);
1055
Semih Hazare4f48862007-07-18 00:35:56 -04001056 /* ... maybe discard first sample ... */
1057 if (pdata->settle_delay_usecs) {
1058 x->delay_usecs = pdata->settle_delay_usecs;
1059
1060 x++;
1061 x->tx_buf = &ts->read_z2;
1062 x->len = 1;
1063 spi_message_add_tail(x, m);
1064
1065 x++;
1066 x->rx_buf = &ts->tc.z2;
1067 x->len = 2;
1068 spi_message_add_tail(x, m);
1069 }
1070
Imre Deakda970e62007-01-18 00:44:41 -05001071 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001072 m->context = ts;
1073 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001074
1075 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001076 m++;
1077 spi_message_init(m);
1078
Imre Deak53a0ef892006-04-11 23:41:49 -04001079 x++;
1080 ts->pwrdown = PWRDOWN;
1081 x->tx_buf = &ts->pwrdown;
1082 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001083 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001084
1085 x++;
1086 x->rx_buf = &ts->dummy;
1087 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001088 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001089 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001090
Imre Deak0b7018a2006-04-11 23:42:03 -04001091 m->complete = ads7846_rx;
1092 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001093
Imre Deakd5b415c2006-04-26 00:13:18 -04001094 ts->last_msg = m;
1095
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001096 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001097 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001098 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001099 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001100 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001101 }
David Brownellffa458c2006-01-08 13:34:21 -08001102
David Brownell2c8dc072007-01-18 00:45:48 -05001103 err = ads784x_hwmon_register(spi, ts);
1104 if (err)
1105 goto err_free_irq;
1106
David Brownell2e5a7bd2006-01-08 13:34:25 -08001107 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001108
David Brownell2c8dc072007-01-18 00:45:48 -05001109 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001110 * the touchscreen, in case it's not connected.
1111 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001112 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001113 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1114
David Brownell2c8dc072007-01-18 00:45:48 -05001115 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001116 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001117 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001118
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001119 err = input_register_device(input_dev);
1120 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001121 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001122
David Brownellffa458c2006-01-08 13:34:21 -08001123 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001124
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001125 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001126 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1127 err_remove_hwmon:
1128 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001129 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001130 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001131 err_cleanup_filter:
1132 if (ts->filter_cleanup)
1133 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001134 err_free_mem:
1135 input_free_device(input_dev);
1136 kfree(ts);
1137 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001138}
1139
David Brownell2e5a7bd2006-01-08 13:34:25 -08001140static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001141{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001142 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001143
David Brownell2c8dc072007-01-18 00:45:48 -05001144 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001145 input_unregister_device(ts->input);
1146
David Brownell2e5a7bd2006-01-08 13:34:25 -08001147 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001148
David Brownell2c8dc072007-01-18 00:45:48 -05001149 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001150
Imre Deak7de90a82006-04-11 23:43:55 -04001151 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001152 /* suspend left the IRQ disabled */
1153 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001154
Imre Deakda970e62007-01-18 00:44:41 -05001155 if (ts->filter_cleanup)
1156 ts->filter_cleanup(ts->filter_data);
1157
David Brownellffa458c2006-01-08 13:34:21 -08001158 kfree(ts);
1159
David Brownell2e5a7bd2006-01-08 13:34:25 -08001160 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001161 return 0;
1162}
1163
David Brownell2e5a7bd2006-01-08 13:34:25 -08001164static struct spi_driver ads7846_driver = {
1165 .driver = {
1166 .name = "ads7846",
1167 .bus = &spi_bus_type,
1168 .owner = THIS_MODULE,
1169 },
David Brownellffa458c2006-01-08 13:34:21 -08001170 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001171 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001172 .suspend = ads7846_suspend,
1173 .resume = ads7846_resume,
1174};
1175
1176static int __init ads7846_init(void)
1177{
1178 /* grr, board-specific init should stay out of drivers!! */
1179
1180#ifdef CONFIG_ARCH_OMAP
1181 if (machine_is_omap_osk()) {
1182 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1183 omap_request_gpio(4);
1184 omap_set_gpio_direction(4, 1);
1185 omap_request_gpio(6);
1186 omap_set_gpio_direction(6, 1);
1187 }
1188 // also TI 1510 Innovator, bitbanging through FPGA
1189 // also Nokia 770
1190 // also Palm Tungsten T2
1191#endif
1192
1193 // PXA:
1194 // also Dell Axim X50
1195 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -08001196 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -08001197 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1198
David Brownell2e5a7bd2006-01-08 13:34:25 -08001199 // Atmel at91sam9261-EK uses ads7843
1200
David Brownellffa458c2006-01-08 13:34:21 -08001201 // also various AMD Au1x00 devel boards
1202
David Brownell2e5a7bd2006-01-08 13:34:25 -08001203 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001204}
1205module_init(ads7846_init);
1206
1207static void __exit ads7846_exit(void)
1208{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001209 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001210
1211#ifdef CONFIG_ARCH_OMAP
1212 if (machine_is_omap_osk()) {
1213 omap_free_gpio(4);
1214 omap_free_gpio(6);
1215 }
1216#endif
1217
1218}
1219module_exit(ads7846_exit);
1220
1221MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1222MODULE_LICENSE("GPL");