blob: a571aa965da0dadc026acaa7f8a323c502631b44 [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;
David Brownell7c6d0ee2008-04-02 00:43:01 -040090 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080091 u16 vref_delay_usecs;
92 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040093 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080094
Imre Deak53a0ef892006-04-11 23:41:49 -040095 u8 read_x, read_y, read_z1, read_z2, pwrdown;
96 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080097 struct ts_event tc;
98
Semih Hazare4f48862007-07-18 00:35:56 -040099 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400100 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400101 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400102 int msg_idx;
103 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400104 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400105 int last_read;
106
107 u16 debounce_max;
108 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400109 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800110
Semih Hazar1d258912007-07-18 00:36:04 -0400111 u16 penirq_recheck_delay_usecs;
112
David Brownellffa458c2006-01-08 13:34:21 -0800113 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500114 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800115 unsigned pendown:1; /* P: lock */
116 unsigned pending:1; /* P: lock */
117// FIXME remove "irq_disabled"
118 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400119 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500120 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400121
Imre Deakda970e62007-01-18 00:44:41 -0500122 int (*filter)(void *data, int data_idx, int *val);
123 void *filter_data;
124 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400125 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800126};
127
128/* leave chip selected when we're done, for quicker re-select? */
129#if 0
130#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
131#else
132#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
133#endif
134
135/*--------------------------------------------------------------------------*/
136
137/* The ADS7846 has touchscreen and other sensors.
138 * Earlier ads784x chips are somewhat compatible.
139 */
140#define ADS_START (1 << 7)
141#define ADS_A2A1A0_d_y (1 << 4) /* differential */
142#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
143#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
144#define ADS_A2A1A0_d_x (5 << 4) /* differential */
145#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
146#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
147#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
148#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
149#define ADS_8_BIT (1 << 3)
150#define ADS_12_BIT (0 << 3)
151#define ADS_SER (1 << 2) /* non-differential */
152#define ADS_DFR (0 << 2) /* differential */
153#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
154#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
155#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
156#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
157
158#define MAX_12BIT ((1<<12)-1)
159
160/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500161#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
162 | ADS_12_BIT | ADS_DFR | \
163 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800164
Imre Deakde2defd2007-01-18 00:45:21 -0500165#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
166#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
167#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400168
Imre Deakde2defd2007-01-18 00:45:21 -0500169#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
170#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800171
172/* single-ended samples need to first power up reference voltage;
173 * we leave both ADC and VREF powered
174 */
175#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
176 | ADS_12_BIT | ADS_SER)
177
Imre Deakde2defd2007-01-18 00:45:21 -0500178#define REF_ON (READ_12BIT_DFR(x, 1, 1))
179#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800180
181/*--------------------------------------------------------------------------*/
182
183/*
184 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500185 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
186 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800187 */
188
189struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500190 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800191 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500192 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800193 u16 scratch;
194 __be16 sample;
195 struct spi_message msg;
196 struct spi_transfer xfer[6];
197};
198
Imre Deak7de90a82006-04-11 23:43:55 -0400199static void ads7846_enable(struct ads7846 *ts);
200static void ads7846_disable(struct ads7846 *ts);
201
Imre Deakc9e617a2006-04-11 23:44:05 -0400202static int device_suspended(struct device *dev)
203{
204 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500205 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400206}
207
David Brownellffa458c2006-01-08 13:34:21 -0800208static int ads7846_read12_ser(struct device *dev, unsigned command)
209{
210 struct spi_device *spi = to_spi_device(dev);
211 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800212 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800213 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500214 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800215
216 if (!req)
217 return -ENOMEM;
218
Imre Deak0b7018a2006-04-11 23:42:03 -0400219 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800220
David Brownell2c8dc072007-01-18 00:45:48 -0500221 /* FIXME boards with ads7846 might use external vref instead ... */
222 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800223
David Brownell2c8dc072007-01-18 00:45:48 -0500224 /* maybe turn on internal vREF, and let it settle */
225 if (use_internal) {
226 req->ref_on = REF_ON;
227 req->xfer[0].tx_buf = &req->ref_on;
228 req->xfer[0].len = 1;
229 spi_message_add_tail(&req->xfer[0], &req->msg);
230
231 req->xfer[1].rx_buf = &req->scratch;
232 req->xfer[1].len = 2;
233
234 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
235 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
236 spi_message_add_tail(&req->xfer[1], &req->msg);
237 }
David Brownellffa458c2006-01-08 13:34:21 -0800238
239 /* take sample */
240 req->command = (u8) command;
241 req->xfer[2].tx_buf = &req->command;
242 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500243 spi_message_add_tail(&req->xfer[2], &req->msg);
244
David Brownellffa458c2006-01-08 13:34:21 -0800245 req->xfer[3].rx_buf = &req->sample;
246 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500247 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800248
249 /* REVISIT: take a few more samples, and compare ... */
250
Nicolas Ferre969111e2007-02-28 23:51:03 -0500251 /* converter in low power mode & enable PENIRQ */
252 req->ref_off = PWRDOWN;
253 req->xfer[4].tx_buf = &req->ref_off;
254 req->xfer[4].len = 1;
255 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800256
Nicolas Ferre969111e2007-02-28 23:51:03 -0500257 req->xfer[5].rx_buf = &req->scratch;
258 req->xfer[5].len = 2;
259 CS_CHANGE(req->xfer[5]);
260 spi_message_add_tail(&req->xfer[5], &req->msg);
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
Marc Pignatc24b2602007-12-04 23:45:11 -0800268 if (status == 0) {
269 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400270 status = be16_to_cpu(req->sample);
271 status = status >> 3;
272 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800273 }
David Brownell90845332006-05-25 18:44:20 -0700274
275 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400276 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800277}
278
David Brownell2c8dc072007-01-18 00:45:48 -0500279#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
280
281#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800282name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
283{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500284 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800285 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500286 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800287 if (v < 0) \
288 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500289 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800290} \
291static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
292
David Brownell2c8dc072007-01-18 00:45:48 -0500293
294/* Sysfs conventions report temperatures in millidegrees Celcius.
295 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
296 * accuracy scheme without calibration data. For now we won't try either;
297 * userspace sees raw sensor values, and must scale/calibrate appropriately.
298 */
299static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
300{
301 return v;
302}
303
304SHOW(temp0, temp0, null_adjust) /* temp1_input */
305SHOW(temp1, temp1, null_adjust) /* temp2_input */
306
307
308/* sysfs conventions report voltages in millivolts. We can convert voltages
309 * if we know vREF. userspace may need to scale vAUX to match the board's
310 * external resistors; we assume that vBATT only uses the internal ones.
311 */
312static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
313{
314 unsigned retval = v;
315
316 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400317 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500318 retval = retval >> 12;
319 return retval;
320}
321
322static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
323{
324 unsigned retval = vaux_adjust(ts, v);
325
326 /* ads7846 has a resistor ladder to scale this signal down */
327 if (ts->model == 7846)
328 retval *= 4;
329 return retval;
330}
331
332SHOW(in0_input, vaux, vaux_adjust)
333SHOW(in1_input, vbatt, vbatt_adjust)
334
335
336static struct attribute *ads7846_attributes[] = {
337 &dev_attr_temp0.attr,
338 &dev_attr_temp1.attr,
339 &dev_attr_in0_input.attr,
340 &dev_attr_in1_input.attr,
341 NULL,
342};
343
344static struct attribute_group ads7846_attr_group = {
345 .attrs = ads7846_attributes,
346};
347
348static struct attribute *ads7843_attributes[] = {
349 &dev_attr_in0_input.attr,
350 &dev_attr_in1_input.attr,
351 NULL,
352};
353
354static struct attribute_group ads7843_attr_group = {
355 .attrs = ads7843_attributes,
356};
357
358static struct attribute *ads7845_attributes[] = {
359 &dev_attr_in0_input.attr,
360 NULL,
361};
362
363static struct attribute_group ads7845_attr_group = {
364 .attrs = ads7845_attributes,
365};
366
367static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
368{
Tony Jones1beeffe2007-08-20 13:46:20 -0700369 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500370 int err;
371
372 /* hwmon sensors need a reference voltage */
373 switch (ts->model) {
374 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400375 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500376 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400377 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500378 }
379 break;
380 case 7845:
381 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400382 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500383 dev_warn(&spi->dev,
384 "external vREF for ADS%d not specified\n",
385 ts->model);
386 return 0;
387 }
388 break;
389 }
390
391 /* different chips have different sensor groups */
392 switch (ts->model) {
393 case 7846:
394 ts->attr_group = &ads7846_attr_group;
395 break;
396 case 7845:
397 ts->attr_group = &ads7845_attr_group;
398 break;
399 case 7843:
400 ts->attr_group = &ads7843_attr_group;
401 break;
402 default:
403 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
404 return 0;
405 }
406
407 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
408 if (err)
409 return err;
410
411 hwmon = hwmon_device_register(&spi->dev);
412 if (IS_ERR(hwmon)) {
413 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
414 return PTR_ERR(hwmon);
415 }
416
417 ts->hwmon = hwmon;
418 return 0;
419}
420
421static void ads784x_hwmon_unregister(struct spi_device *spi,
422 struct ads7846 *ts)
423{
424 if (ts->hwmon) {
425 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
426 hwmon_device_unregister(ts->hwmon);
427 }
428}
429
430#else
431static inline int ads784x_hwmon_register(struct spi_device *spi,
432 struct ads7846 *ts)
433{
434 return 0;
435}
436
437static inline void ads784x_hwmon_unregister(struct spi_device *spi,
438 struct ads7846 *ts)
439{
440}
441#endif
David Brownellffa458c2006-01-08 13:34:21 -0800442
Imre Deak438f2a72006-04-11 23:41:32 -0400443static int is_pen_down(struct device *dev)
444{
David Brownell2c8dc072007-01-18 00:45:48 -0500445 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400446
447 return ts->pendown;
448}
449
450static ssize_t ads7846_pen_down_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
453 return sprintf(buf, "%u\n", is_pen_down(dev));
454}
455
456static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
457
Imre Deak7de90a82006-04-11 23:43:55 -0400458static ssize_t ads7846_disable_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
460{
461 struct ads7846 *ts = dev_get_drvdata(dev);
462
463 return sprintf(buf, "%u\n", ts->disabled);
464}
465
466static ssize_t ads7846_disable_store(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
469{
470 struct ads7846 *ts = dev_get_drvdata(dev);
471 char *endp;
472 int i;
473
474 i = simple_strtoul(buf, &endp, 10);
475 spin_lock_irq(&ts->lock);
476
477 if (i)
478 ads7846_disable(ts);
479 else
480 ads7846_enable(ts);
481
482 spin_unlock_irq(&ts->lock);
483
484 return count;
485}
486
487static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
488
David Brownell2c8dc072007-01-18 00:45:48 -0500489static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500490 &dev_attr_pen_down.attr,
491 &dev_attr_disable.attr,
492 NULL,
493};
494
David Brownell2c8dc072007-01-18 00:45:48 -0500495static struct attribute_group ads784x_attr_group = {
496 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500497};
498
David Brownellffa458c2006-01-08 13:34:21 -0800499/*--------------------------------------------------------------------------*/
500
501/*
502 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
503 * to retrieve touchscreen status.
504 *
505 * The SPI transfer completion callback does the real work. It reports
506 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
507 */
508
509static void ads7846_rx(void *ads)
510{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500511 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500512 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500513 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800514
Imre Deakda970e62007-01-18 00:44:41 -0500515 /* ads7846_rx_val() did in-place conversion (including byteswap) from
516 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800517 */
Imre Deakda970e62007-01-18 00:44:41 -0500518 x = ts->tc.x;
519 y = ts->tc.y;
520 z1 = ts->tc.z1;
521 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800522
523 /* range filtering */
524 if (x == MAX_12BIT)
525 x = 0;
526
Imre Deak15e35892007-01-18 00:45:43 -0500527 if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800528 /* compute touch pressure resistance using equation #2 */
529 Rt = z2;
530 Rt -= z1;
531 Rt *= x;
532 Rt *= ts->x_plate_ohms;
533 Rt /= z1;
534 Rt = (Rt + 2047) >> 12;
535 } else
536 Rt = 0;
537
Nicolas Ferre969111e2007-02-28 23:51:03 -0500538 if (ts->model == 7843)
539 Rt = ts->pressure_max / 2;
540
Imre Deakd5b415c2006-04-26 00:13:18 -0400541 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500542 * the maximum. Don't report it to user space, repeat at least
543 * once more the measurement
544 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400545 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500546#ifdef VERBOSE
547 pr_debug("%s: ignored %d pressure %d\n",
548 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
549#endif
Imre Deak1936d592007-01-18 00:45:31 -0500550 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800551 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400552 return;
553 }
554
Semih Hazar1d258912007-07-18 00:36:04 -0400555 /* Maybe check the pendown state before reporting. This discards
556 * false readings when the pen is lifted.
557 */
558 if (ts->penirq_recheck_delay_usecs) {
559 udelay(ts->penirq_recheck_delay_usecs);
560 if (!ts->get_pendown_state())
561 Rt = 0;
562 }
563
Imre Deak15e35892007-01-18 00:45:43 -0500564 /* NOTE: We can't rely on the pressure to determine the pen down
565 * state, even this controller has a pressure sensor. The pressure
566 * value can fluctuate for quite a while after lifting the pen and
567 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800568 *
Imre Deak15e35892007-01-18 00:45:43 -0500569 * The only safe way to check for the pen up condition is in the
570 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800571 */
David Brownellffa458c2006-01-08 13:34:21 -0800572 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500573 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400574
Imre Deak15e35892007-01-18 00:45:43 -0500575 if (!ts->pendown) {
576 input_report_key(input, BTN_TOUCH, 1);
577 ts->pendown = 1;
578#ifdef VERBOSE
579 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800580#endif
Imre Deak15e35892007-01-18 00:45:43 -0500581 }
582 input_report_abs(input, ABS_X, x);
583 input_report_abs(input, ABS_Y, y);
584 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800585
Imre Deak15e35892007-01-18 00:45:43 -0500586 input_sync(input);
587#ifdef VERBOSE
588 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
589#endif
590 }
David Brownellffa458c2006-01-08 13:34:21 -0800591
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800592 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
593 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800594}
595
Imre Deakda970e62007-01-18 00:44:41 -0500596static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400597{
598 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400599
Imre Deakda970e62007-01-18 00:44:41 -0500600 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
601 /* Start over collecting consistent readings. */
602 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400603 /* Repeat it, if this was the first read or the read
604 * wasn't consistent enough. */
605 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500606 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400607 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500608 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400609 } else {
610 /* Maximum number of debouncing reached and still
611 * not enough number of consistent readings. Abort
612 * the whole sample, repeat it in the next sampling
613 * period.
614 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400615 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500616 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400617 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400618 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400619 if (++ts->read_rep > ts->debounce_rep) {
620 /* Got a good reading for this coordinate,
621 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400622 ts->read_cnt = 0;
623 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500624 return ADS7846_FILTER_OK;
625 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400626 /* Read more values that are consistent. */
627 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500628 return ADS7846_FILTER_REPEAT;
629 }
630 }
631}
632
633static int ads7846_no_filter(void *ads, int data_idx, int *val)
634{
635 return ADS7846_FILTER_OK;
636}
637
638static void ads7846_rx_val(void *ads)
639{
640 struct ads7846 *ts = ads;
641 struct spi_message *m;
642 struct spi_transfer *t;
643 u16 *rx_val;
644 int val;
645 int action;
646 int status;
647
648 m = &ts->msg[ts->msg_idx];
649 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
650 rx_val = t->rx_buf;
651
652 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
653 * built from two 8 bit values written msb-first.
654 */
655 val = be16_to_cpu(*rx_val) >> 3;
656
657 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
658 switch (action) {
659 case ADS7846_FILTER_REPEAT:
660 break;
661 case ADS7846_FILTER_IGNORE:
662 ts->tc.ignore = 1;
663 /* Last message will contain ads7846_rx() as the
664 * completion function.
665 */
666 m = ts->last_msg;
667 break;
668 case ADS7846_FILTER_OK:
669 *rx_val = val;
670 ts->tc.ignore = 0;
671 m = &ts->msg[++ts->msg_idx];
672 break;
673 default:
674 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400675 }
676 status = spi_async(ts->spi, m);
677 if (status)
678 dev_err(&ts->spi->dev, "spi_async --> %d\n",
679 status);
680}
681
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800682static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800683{
Imre Deak1936d592007-01-18 00:45:31 -0500684 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800685 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800686
Imre Deakc9e617a2006-04-11 23:44:05 -0400687 spin_lock_irq(&ts->lock);
688
Imre Deak15e35892007-01-18 00:45:43 -0500689 if (unlikely(!ts->get_pendown_state() ||
690 device_suspended(&ts->spi->dev))) {
691 if (ts->pendown) {
692 struct input_dev *input = ts->input;
693
694 input_report_key(input, BTN_TOUCH, 0);
695 input_report_abs(input, ABS_PRESSURE, 0);
696 input_sync(input);
697
698 ts->pendown = 0;
699#ifdef VERBOSE
700 dev_dbg(&ts->spi->dev, "UP\n");
701#endif
702 }
703
David Brownell90845332006-05-25 18:44:20 -0700704 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400705 if (!device_suspended(&ts->spi->dev)) {
706 ts->irq_disabled = 0;
707 enable_irq(ts->spi->irq);
708 }
709 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400710 } else {
711 /* pen is still down, continue with the measurement */
712 ts->msg_idx = 0;
713 status = spi_async(ts->spi, &ts->msg[0]);
714 if (status)
715 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
716 }
717
718 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500719 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800720}
721
David Howells7d12e782006-10-05 14:55:46 +0100722static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800723{
Imre Deak0b7018a2006-04-11 23:42:03 -0400724 struct ads7846 *ts = handle;
725 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400726
727 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400728 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400729 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700730 /* The ARM do_simple_IRQ() dispatcher doesn't act
731 * like the other dispatchers: it will report IRQs
732 * even after they've been disabled. We work around
733 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400734 */
735 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400736 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400737 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500738 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800739 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400740 }
741 }
742 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400743
744 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800745}
746
747/*--------------------------------------------------------------------------*/
748
Imre Deak7de90a82006-04-11 23:43:55 -0400749/* Must be called with ts->lock held */
750static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800751{
Imre Deak7de90a82006-04-11 23:43:55 -0400752 if (ts->disabled)
753 return;
David Brownellffa458c2006-01-08 13:34:21 -0800754
Imre Deakc9e617a2006-04-11 23:44:05 -0400755 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800756
Imre Deakc9e617a2006-04-11 23:44:05 -0400757 /* are we waiting for IRQ, or polling? */
758 if (!ts->pending) {
759 ts->irq_disabled = 1;
760 disable_irq(ts->spi->irq);
761 } else {
762 /* the timer will run at least once more, and
763 * leave everything in a clean state, IRQ disabled
764 */
765 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400766 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400767 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400768 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800769 }
770 }
771
772 /* we know the chip's in lowpower mode since we always
773 * leave it that way after every request
774 */
775
Imre Deak7de90a82006-04-11 23:43:55 -0400776}
777
778/* Must be called with ts->lock held */
779static void ads7846_enable(struct ads7846 *ts)
780{
781 if (!ts->disabled)
782 return;
783
784 ts->disabled = 0;
785 ts->irq_disabled = 0;
786 enable_irq(ts->spi->irq);
787}
788
789static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
790{
791 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
792
793 spin_lock_irq(&ts->lock);
794
David Brownellfbb38e32007-12-14 01:26:33 -0500795 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400796 ads7846_disable(ts);
797
798 spin_unlock_irq(&ts->lock);
799
David Brownellffa458c2006-01-08 13:34:21 -0800800 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400801
David Brownellffa458c2006-01-08 13:34:21 -0800802}
803
David Brownell2e5a7bd2006-01-08 13:34:25 -0800804static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800805{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800806 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800807
Imre Deak7de90a82006-04-11 23:43:55 -0400808 spin_lock_irq(&ts->lock);
809
David Brownellfbb38e32007-12-14 01:26:33 -0500810 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400811 ads7846_enable(ts);
812
813 spin_unlock_irq(&ts->lock);
814
David Brownellffa458c2006-01-08 13:34:21 -0800815 return 0;
816}
817
David Brownell2e5a7bd2006-01-08 13:34:25 -0800818static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800819{
David Brownellffa458c2006-01-08 13:34:21 -0800820 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500821 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800822 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400823 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800824 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500825 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500826 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800827
828 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800829 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800830 return -ENODEV;
831 }
832
833 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800834 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800835 return -ENODEV;
836 }
837
838 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500839 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800840 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500841 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800842 return -EINVAL;
843 }
844
David Brownell90845332006-05-25 18:44:20 -0700845 /* REVISIT when the irq can be triggered active-low, or if for some
846 * reason the touchscreen isn't hooked up, we don't need to access
847 * the pendown state.
848 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400849 if (pdata->get_pendown_state == NULL) {
850 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
851 return -EINVAL;
852 }
853
David Brownell90845332006-05-25 18:44:20 -0700854 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
855 * that even if the hardware can do that, the SPI controller driver
856 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800857 */
David Brownell90845332006-05-25 18:44:20 -0700858 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400859 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500860 err = spi_setup(spi);
861 if (err < 0)
862 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800863
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500864 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
865 input_dev = input_allocate_device();
866 if (!ts || !input_dev) {
867 err = -ENOMEM;
868 goto err_free_mem;
869 }
David Brownellffa458c2006-01-08 13:34:21 -0800870
David Brownell2e5a7bd2006-01-08 13:34:25 -0800871 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800872
873 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500874 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400875 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800876
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800877 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800878 ts->timer.function = ads7846_timer;
879
Imre Deak7de90a82006-04-11 23:43:55 -0400880 spin_lock_init(&ts->lock);
881
David Brownellffa458c2006-01-08 13:34:21 -0800882 ts->model = pdata->model ? : 7846;
883 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
884 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400885 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500886
887 if (pdata->filter != NULL) {
888 if (pdata->filter_init != NULL) {
889 err = pdata->filter_init(pdata, &ts->filter_data);
890 if (err < 0)
891 goto err_free_mem;
892 }
893 ts->filter = pdata->filter;
894 ts->filter_cleanup = pdata->filter_cleanup;
895 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400896 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500897 if (ts->debounce_max < 2)
898 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400899 ts->debounce_tol = pdata->debounce_tol;
900 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500901 ts->filter = ads7846_debounce;
902 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400903 } else
Imre Deakda970e62007-01-18 00:44:41 -0500904 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400905 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800906
Semih Hazar1d258912007-07-18 00:36:04 -0400907 if (pdata->penirq_recheck_delay_usecs)
908 ts->penirq_recheck_delay_usecs =
909 pdata->penirq_recheck_delay_usecs;
910
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500911 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800912
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500913 input_dev->name = "ADS784x Touchscreen";
914 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400915 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800916
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700917 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
918 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500919 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800920 pdata->x_min ? : 0,
921 pdata->x_max ? : MAX_12BIT,
922 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500923 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800924 pdata->y_min ? : 0,
925 pdata->y_max ? : MAX_12BIT,
926 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500927 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800928 pdata->pressure_min, pdata->pressure_max, 0, 0);
929
Imre Deakde2defd2007-01-18 00:45:21 -0500930 vref = pdata->keep_vref_on;
931
David Brownellffa458c2006-01-08 13:34:21 -0800932 /* set up the transfers to read touchscreen state; this assumes we
933 * use formula #2 for pressure, not #3.
934 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400935 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800936 x = ts->xfer;
937
Imre Deak0b7018a2006-04-11 23:42:03 -0400938 spi_message_init(m);
939
David Brownellffa458c2006-01-08 13:34:21 -0800940 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500941 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500942 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800943 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400944 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500945
David Brownellffa458c2006-01-08 13:34:21 -0800946 x++;
947 x->rx_buf = &ts->tc.y;
948 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400949 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800950
Semih Hazare4f48862007-07-18 00:35:56 -0400951 /* the first sample after switching drivers can be low quality;
952 * optionally discard it, using a second one after the signals
953 * have had enough time to stabilize.
954 */
955 if (pdata->settle_delay_usecs) {
956 x->delay_usecs = pdata->settle_delay_usecs;
957
958 x++;
959 x->tx_buf = &ts->read_y;
960 x->len = 1;
961 spi_message_add_tail(x, m);
962
963 x++;
964 x->rx_buf = &ts->tc.y;
965 x->len = 2;
966 spi_message_add_tail(x, m);
967 }
968
Imre Deakda970e62007-01-18 00:44:41 -0500969 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400970 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500971
Imre Deak0b7018a2006-04-11 23:42:03 -0400972 m++;
973 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800974
975 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500976 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500977 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500978 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800979 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400980 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500981
David Brownellffa458c2006-01-08 13:34:21 -0800982 x++;
983 x->rx_buf = &ts->tc.x;
984 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400985 spi_message_add_tail(x, m);
986
Semih Hazare4f48862007-07-18 00:35:56 -0400987 /* ... maybe discard first sample ... */
988 if (pdata->settle_delay_usecs) {
989 x->delay_usecs = pdata->settle_delay_usecs;
990
991 x++;
992 x->tx_buf = &ts->read_x;
993 x->len = 1;
994 spi_message_add_tail(x, m);
995
996 x++;
997 x->rx_buf = &ts->tc.x;
998 x->len = 2;
999 spi_message_add_tail(x, m);
1000 }
1001
Imre Deakda970e62007-01-18 00:44:41 -05001002 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001003 m->context = ts;
1004
1005 /* turn y+ off, x- on; we'll use formula #2 */
1006 if (ts->model == 7846) {
1007 m++;
1008 spi_message_init(m);
1009
1010 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001011 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001012 x->tx_buf = &ts->read_z1;
1013 x->len = 1;
1014 spi_message_add_tail(x, m);
1015
1016 x++;
1017 x->rx_buf = &ts->tc.z1;
1018 x->len = 2;
1019 spi_message_add_tail(x, m);
1020
Semih Hazare4f48862007-07-18 00:35:56 -04001021 /* ... maybe discard first sample ... */
1022 if (pdata->settle_delay_usecs) {
1023 x->delay_usecs = pdata->settle_delay_usecs;
1024
1025 x++;
1026 x->tx_buf = &ts->read_z1;
1027 x->len = 1;
1028 spi_message_add_tail(x, m);
1029
1030 x++;
1031 x->rx_buf = &ts->tc.z1;
1032 x->len = 2;
1033 spi_message_add_tail(x, m);
1034 }
1035
Imre Deakda970e62007-01-18 00:44:41 -05001036 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001037 m->context = ts;
1038
1039 m++;
1040 spi_message_init(m);
1041
1042 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001043 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001044 x->tx_buf = &ts->read_z2;
1045 x->len = 1;
1046 spi_message_add_tail(x, m);
1047
1048 x++;
1049 x->rx_buf = &ts->tc.z2;
1050 x->len = 2;
1051 spi_message_add_tail(x, m);
1052
Semih Hazare4f48862007-07-18 00:35:56 -04001053 /* ... maybe discard first sample ... */
1054 if (pdata->settle_delay_usecs) {
1055 x->delay_usecs = pdata->settle_delay_usecs;
1056
1057 x++;
1058 x->tx_buf = &ts->read_z2;
1059 x->len = 1;
1060 spi_message_add_tail(x, m);
1061
1062 x++;
1063 x->rx_buf = &ts->tc.z2;
1064 x->len = 2;
1065 spi_message_add_tail(x, m);
1066 }
1067
Imre Deakda970e62007-01-18 00:44:41 -05001068 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001069 m->context = ts;
1070 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001071
1072 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001073 m++;
1074 spi_message_init(m);
1075
Imre Deak53a0ef892006-04-11 23:41:49 -04001076 x++;
1077 ts->pwrdown = PWRDOWN;
1078 x->tx_buf = &ts->pwrdown;
1079 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001080 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001081
1082 x++;
1083 x->rx_buf = &ts->dummy;
1084 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001085 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001086 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001087
Imre Deak0b7018a2006-04-11 23:42:03 -04001088 m->complete = ads7846_rx;
1089 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001090
Imre Deakd5b415c2006-04-26 00:13:18 -04001091 ts->last_msg = m;
1092
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001093 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001094 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001095 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001096 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001097 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001098 }
David Brownellffa458c2006-01-08 13:34:21 -08001099
David Brownell2c8dc072007-01-18 00:45:48 -05001100 err = ads784x_hwmon_register(spi, ts);
1101 if (err)
1102 goto err_free_irq;
1103
David Brownell2e5a7bd2006-01-08 13:34:25 -08001104 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001105
David Brownell2c8dc072007-01-18 00:45:48 -05001106 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001107 * the touchscreen, in case it's not connected.
1108 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001109 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001110 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1111
David Brownell2c8dc072007-01-18 00:45:48 -05001112 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001113 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001114 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001115
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001116 err = input_register_device(input_dev);
1117 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001118 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001119
David Brownellffa458c2006-01-08 13:34:21 -08001120 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001121
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001122 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001123 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1124 err_remove_hwmon:
1125 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001126 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001127 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001128 err_cleanup_filter:
1129 if (ts->filter_cleanup)
1130 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001131 err_free_mem:
1132 input_free_device(input_dev);
1133 kfree(ts);
1134 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001135}
1136
David Brownell2e5a7bd2006-01-08 13:34:25 -08001137static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001138{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001139 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001140
David Brownell2c8dc072007-01-18 00:45:48 -05001141 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001142 input_unregister_device(ts->input);
1143
David Brownell2e5a7bd2006-01-08 13:34:25 -08001144 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001145
David Brownell2c8dc072007-01-18 00:45:48 -05001146 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001147
Imre Deak7de90a82006-04-11 23:43:55 -04001148 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001149 /* suspend left the IRQ disabled */
1150 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001151
Imre Deakda970e62007-01-18 00:44:41 -05001152 if (ts->filter_cleanup)
1153 ts->filter_cleanup(ts->filter_data);
1154
David Brownellffa458c2006-01-08 13:34:21 -08001155 kfree(ts);
1156
David Brownell2e5a7bd2006-01-08 13:34:25 -08001157 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001158 return 0;
1159}
1160
David Brownell2e5a7bd2006-01-08 13:34:25 -08001161static struct spi_driver ads7846_driver = {
1162 .driver = {
1163 .name = "ads7846",
1164 .bus = &spi_bus_type,
1165 .owner = THIS_MODULE,
1166 },
David Brownellffa458c2006-01-08 13:34:21 -08001167 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001168 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001169 .suspend = ads7846_suspend,
1170 .resume = ads7846_resume,
1171};
1172
1173static int __init ads7846_init(void)
1174{
1175 /* grr, board-specific init should stay out of drivers!! */
1176
1177#ifdef CONFIG_ARCH_OMAP
1178 if (machine_is_omap_osk()) {
1179 /* GPIO4 = PENIRQ; GPIO6 = BUSY */
1180 omap_request_gpio(4);
1181 omap_set_gpio_direction(4, 1);
1182 omap_request_gpio(6);
1183 omap_set_gpio_direction(6, 1);
1184 }
1185 // also TI 1510 Innovator, bitbanging through FPGA
1186 // also Nokia 770
1187 // also Palm Tungsten T2
1188#endif
1189
1190 // PXA:
1191 // also Dell Axim X50
1192 // also HP iPaq H191x/H192x/H415x/H435x
David Brownell2e5a7bd2006-01-08 13:34:25 -08001193 // also Intel Lubbock (additional to UCB1400; as temperature sensor)
David Brownellffa458c2006-01-08 13:34:21 -08001194 // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky)
1195
David Brownell2e5a7bd2006-01-08 13:34:25 -08001196 // Atmel at91sam9261-EK uses ads7843
1197
David Brownellffa458c2006-01-08 13:34:21 -08001198 // also various AMD Au1x00 devel boards
1199
David Brownell2e5a7bd2006-01-08 13:34:25 -08001200 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001201}
1202module_init(ads7846_init);
1203
1204static void __exit ads7846_exit(void)
1205{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001206 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001207
1208#ifdef CONFIG_ARCH_OMAP
1209 if (machine_is_omap_osk()) {
1210 omap_free_gpio(4);
1211 omap_free_gpio(6);
1212 }
1213#endif
1214
1215}
1216module_exit(ads7846_exit);
1217
1218MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1219MODULE_LICENSE("GPL");