blob: 8583c766d565201e224fb21abaedd6c9cb8cadb8 [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>
Eric Miao6ae19b02008-09-10 12:06:15 -040027#include <linux/gpio.h>
David Brownellffa458c2006-01-08 13:34:21 -080028#include <linux/spi/spi.h>
29#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080030#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080031
David Brownellffa458c2006-01-08 13:34:21 -080032
33/*
David Brownell90845332006-05-25 18:44:20 -070034 * This code has been heavily tested on a Nokia 770, and lightly
35 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
David Brownellbff0de52007-05-22 23:28:40 -040036 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050037 * Support for ads7843 tested on Atmel at91sam926x-EK.
38 * Support for ads7845 has only been stubbed in.
David Brownellffa458c2006-01-08 13:34:21 -080039 *
Imre Deak7de90a82006-04-11 23:43:55 -040040 * IRQ handling needs a workaround because of a shortcoming in handling
41 * edge triggered IRQs on some platforms like the OMAP1/2. These
42 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
43 * have to maintain our own SW IRQ disabled status. This should be
44 * removed as soon as the affected platform's IRQ handling is fixed.
45 *
David Brownellffa458c2006-01-08 13:34:21 -080046 * app note sbaa036 talks in more detail about accurate sampling...
47 * that ought to help in situations like LCDs inducing noise (which
48 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040049 * This driver tries to utilize the measures described in the app
50 * note. The strength of filtering can be set in the board-* specific
51 * files.
David Brownellffa458c2006-01-08 13:34:21 -080052 */
53
Imre Deak1936d592007-01-18 00:45:31 -050054#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
55#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080056
David Brownelld93f70b2006-02-15 00:49:35 -050057/* this driver doesn't aim at the peak continuous sample rate */
58#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
59
David Brownellffa458c2006-01-08 13:34:21 -080060struct ts_event {
61 /* For portability, we can't read 12 bit values using SPI (which
62 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050063 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050064 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080065 */
Imre Deakda970e62007-01-18 00:44:41 -050066 u16 x;
67 u16 y;
68 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050069 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080070};
71
72struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050073 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080074 char phys[32];
75
76 struct spi_device *spi;
David Brownell2c8dc072007-01-18 00:45:48 -050077
78#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050079 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070080 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050081#endif
82
David Brownellffa458c2006-01-08 13:34:21 -080083 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -040084 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080085 u16 vref_delay_usecs;
86 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040087 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080088
Imre Deak53a0ef892006-04-11 23:41:49 -040089 u8 read_x, read_y, read_z1, read_z2, pwrdown;
90 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080091 struct ts_event tc;
92
Semih Hazare4f48862007-07-18 00:35:56 -040093 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -040094 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040095 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040096 int msg_idx;
97 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040098 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040099 int last_read;
100
101 u16 debounce_max;
102 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400103 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800104
Semih Hazar1d258912007-07-18 00:36:04 -0400105 u16 penirq_recheck_delay_usecs;
106
David Brownellffa458c2006-01-08 13:34:21 -0800107 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500108 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800109 unsigned pendown:1; /* P: lock */
110 unsigned pending:1; /* P: lock */
111// FIXME remove "irq_disabled"
112 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400113 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500114 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400115
Imre Deakda970e62007-01-18 00:44:41 -0500116 int (*filter)(void *data, int data_idx, int *val);
117 void *filter_data;
118 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400119 int (*get_pendown_state)(void);
Eric Miao6ae19b02008-09-10 12:06:15 -0400120 int gpio_pendown;
David Brownellffa458c2006-01-08 13:34:21 -0800121};
122
123/* leave chip selected when we're done, for quicker re-select? */
124#if 0
125#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
126#else
127#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
128#endif
129
130/*--------------------------------------------------------------------------*/
131
132/* The ADS7846 has touchscreen and other sensors.
133 * Earlier ads784x chips are somewhat compatible.
134 */
135#define ADS_START (1 << 7)
136#define ADS_A2A1A0_d_y (1 << 4) /* differential */
137#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
138#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
139#define ADS_A2A1A0_d_x (5 << 4) /* differential */
140#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
141#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
142#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
143#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
144#define ADS_8_BIT (1 << 3)
145#define ADS_12_BIT (0 << 3)
146#define ADS_SER (1 << 2) /* non-differential */
147#define ADS_DFR (0 << 2) /* differential */
148#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
149#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
150#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
151#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
152
153#define MAX_12BIT ((1<<12)-1)
154
155/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500156#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
157 | ADS_12_BIT | ADS_DFR | \
158 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800159
Imre Deakde2defd2007-01-18 00:45:21 -0500160#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
161#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
162#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400163
Imre Deakde2defd2007-01-18 00:45:21 -0500164#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
165#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800166
167/* single-ended samples need to first power up reference voltage;
168 * we leave both ADC and VREF powered
169 */
170#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
171 | ADS_12_BIT | ADS_SER)
172
Imre Deakde2defd2007-01-18 00:45:21 -0500173#define REF_ON (READ_12BIT_DFR(x, 1, 1))
174#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800175
176/*--------------------------------------------------------------------------*/
177
178/*
179 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500180 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
181 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800182 */
183
184struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500185 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800186 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500187 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800188 u16 scratch;
189 __be16 sample;
190 struct spi_message msg;
191 struct spi_transfer xfer[6];
192};
193
Imre Deak7de90a82006-04-11 23:43:55 -0400194static void ads7846_enable(struct ads7846 *ts);
195static void ads7846_disable(struct ads7846 *ts);
196
Imre Deakc9e617a2006-04-11 23:44:05 -0400197static int device_suspended(struct device *dev)
198{
199 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500200 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400201}
202
David Brownellffa458c2006-01-08 13:34:21 -0800203static int ads7846_read12_ser(struct device *dev, unsigned command)
204{
205 struct spi_device *spi = to_spi_device(dev);
206 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800207 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800208 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500209 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800210
211 if (!req)
212 return -ENOMEM;
213
Imre Deak0b7018a2006-04-11 23:42:03 -0400214 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800215
David Brownell2c8dc072007-01-18 00:45:48 -0500216 /* FIXME boards with ads7846 might use external vref instead ... */
217 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800218
David Brownell2c8dc072007-01-18 00:45:48 -0500219 /* maybe turn on internal vREF, and let it settle */
220 if (use_internal) {
221 req->ref_on = REF_ON;
222 req->xfer[0].tx_buf = &req->ref_on;
223 req->xfer[0].len = 1;
224 spi_message_add_tail(&req->xfer[0], &req->msg);
225
226 req->xfer[1].rx_buf = &req->scratch;
227 req->xfer[1].len = 2;
228
229 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
230 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
231 spi_message_add_tail(&req->xfer[1], &req->msg);
232 }
David Brownellffa458c2006-01-08 13:34:21 -0800233
234 /* take sample */
235 req->command = (u8) command;
236 req->xfer[2].tx_buf = &req->command;
237 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500238 spi_message_add_tail(&req->xfer[2], &req->msg);
239
David Brownellffa458c2006-01-08 13:34:21 -0800240 req->xfer[3].rx_buf = &req->sample;
241 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500242 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800243
244 /* REVISIT: take a few more samples, and compare ... */
245
Nicolas Ferre969111e2007-02-28 23:51:03 -0500246 /* converter in low power mode & enable PENIRQ */
247 req->ref_off = PWRDOWN;
248 req->xfer[4].tx_buf = &req->ref_off;
249 req->xfer[4].len = 1;
250 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800251
Nicolas Ferre969111e2007-02-28 23:51:03 -0500252 req->xfer[5].rx_buf = &req->scratch;
253 req->xfer[5].len = 2;
254 CS_CHANGE(req->xfer[5]);
255 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800256
Imre Deakc9e617a2006-04-11 23:44:05 -0400257 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800258 disable_irq(spi->irq);
259 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400260 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800261 enable_irq(spi->irq);
262
Marc Pignatc24b2602007-12-04 23:45:11 -0800263 if (status == 0) {
264 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400265 status = be16_to_cpu(req->sample);
266 status = status >> 3;
267 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800268 }
David Brownell90845332006-05-25 18:44:20 -0700269
270 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400271 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800272}
273
David Brownell2c8dc072007-01-18 00:45:48 -0500274#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
275
276#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800277name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
278{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500279 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800280 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500281 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800282 if (v < 0) \
283 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500284 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800285} \
286static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
287
David Brownell2c8dc072007-01-18 00:45:48 -0500288
289/* Sysfs conventions report temperatures in millidegrees Celcius.
290 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
291 * accuracy scheme without calibration data. For now we won't try either;
292 * userspace sees raw sensor values, and must scale/calibrate appropriately.
293 */
294static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
295{
296 return v;
297}
298
299SHOW(temp0, temp0, null_adjust) /* temp1_input */
300SHOW(temp1, temp1, null_adjust) /* temp2_input */
301
302
303/* sysfs conventions report voltages in millivolts. We can convert voltages
304 * if we know vREF. userspace may need to scale vAUX to match the board's
305 * external resistors; we assume that vBATT only uses the internal ones.
306 */
307static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
308{
309 unsigned retval = v;
310
311 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400312 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500313 retval = retval >> 12;
314 return retval;
315}
316
317static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
318{
319 unsigned retval = vaux_adjust(ts, v);
320
321 /* ads7846 has a resistor ladder to scale this signal down */
322 if (ts->model == 7846)
323 retval *= 4;
324 return retval;
325}
326
327SHOW(in0_input, vaux, vaux_adjust)
328SHOW(in1_input, vbatt, vbatt_adjust)
329
330
331static struct attribute *ads7846_attributes[] = {
332 &dev_attr_temp0.attr,
333 &dev_attr_temp1.attr,
334 &dev_attr_in0_input.attr,
335 &dev_attr_in1_input.attr,
336 NULL,
337};
338
339static struct attribute_group ads7846_attr_group = {
340 .attrs = ads7846_attributes,
341};
342
343static struct attribute *ads7843_attributes[] = {
344 &dev_attr_in0_input.attr,
345 &dev_attr_in1_input.attr,
346 NULL,
347};
348
349static struct attribute_group ads7843_attr_group = {
350 .attrs = ads7843_attributes,
351};
352
353static struct attribute *ads7845_attributes[] = {
354 &dev_attr_in0_input.attr,
355 NULL,
356};
357
358static struct attribute_group ads7845_attr_group = {
359 .attrs = ads7845_attributes,
360};
361
362static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
363{
Tony Jones1beeffe2007-08-20 13:46:20 -0700364 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500365 int err;
366
367 /* hwmon sensors need a reference voltage */
368 switch (ts->model) {
369 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400370 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500371 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400372 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500373 }
374 break;
375 case 7845:
376 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400377 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500378 dev_warn(&spi->dev,
379 "external vREF for ADS%d not specified\n",
380 ts->model);
381 return 0;
382 }
383 break;
384 }
385
386 /* different chips have different sensor groups */
387 switch (ts->model) {
388 case 7846:
389 ts->attr_group = &ads7846_attr_group;
390 break;
391 case 7845:
392 ts->attr_group = &ads7845_attr_group;
393 break;
394 case 7843:
395 ts->attr_group = &ads7843_attr_group;
396 break;
397 default:
398 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
399 return 0;
400 }
401
402 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
403 if (err)
404 return err;
405
406 hwmon = hwmon_device_register(&spi->dev);
407 if (IS_ERR(hwmon)) {
408 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
409 return PTR_ERR(hwmon);
410 }
411
412 ts->hwmon = hwmon;
413 return 0;
414}
415
416static void ads784x_hwmon_unregister(struct spi_device *spi,
417 struct ads7846 *ts)
418{
419 if (ts->hwmon) {
420 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
421 hwmon_device_unregister(ts->hwmon);
422 }
423}
424
425#else
426static inline int ads784x_hwmon_register(struct spi_device *spi,
427 struct ads7846 *ts)
428{
429 return 0;
430}
431
432static inline void ads784x_hwmon_unregister(struct spi_device *spi,
433 struct ads7846 *ts)
434{
435}
436#endif
David Brownellffa458c2006-01-08 13:34:21 -0800437
Imre Deak438f2a72006-04-11 23:41:32 -0400438static int is_pen_down(struct device *dev)
439{
David Brownell2c8dc072007-01-18 00:45:48 -0500440 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400441
442 return ts->pendown;
443}
444
445static ssize_t ads7846_pen_down_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
447{
448 return sprintf(buf, "%u\n", is_pen_down(dev));
449}
450
451static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
452
Imre Deak7de90a82006-04-11 23:43:55 -0400453static ssize_t ads7846_disable_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455{
456 struct ads7846 *ts = dev_get_drvdata(dev);
457
458 return sprintf(buf, "%u\n", ts->disabled);
459}
460
461static ssize_t ads7846_disable_store(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
464{
465 struct ads7846 *ts = dev_get_drvdata(dev);
466 char *endp;
467 int i;
468
469 i = simple_strtoul(buf, &endp, 10);
470 spin_lock_irq(&ts->lock);
471
472 if (i)
473 ads7846_disable(ts);
474 else
475 ads7846_enable(ts);
476
477 spin_unlock_irq(&ts->lock);
478
479 return count;
480}
481
482static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
483
David Brownell2c8dc072007-01-18 00:45:48 -0500484static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500485 &dev_attr_pen_down.attr,
486 &dev_attr_disable.attr,
487 NULL,
488};
489
David Brownell2c8dc072007-01-18 00:45:48 -0500490static struct attribute_group ads784x_attr_group = {
491 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500492};
493
David Brownellffa458c2006-01-08 13:34:21 -0800494/*--------------------------------------------------------------------------*/
495
Eric Miao6ae19b02008-09-10 12:06:15 -0400496static int get_pendown_state(struct ads7846 *ts)
497{
498 if (ts->get_pendown_state)
499 return ts->get_pendown_state();
500
501 return !gpio_get_value(ts->gpio_pendown);
502}
503
David Brownellffa458c2006-01-08 13:34:21 -0800504/*
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
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400530 if (ts->model == 7843) {
531 Rt = ts->pressure_max / 2;
532 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800533 /* compute touch pressure resistance using equation #2 */
534 Rt = z2;
535 Rt -= z1;
536 Rt *= x;
537 Rt *= ts->x_plate_ohms;
538 Rt /= z1;
539 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400540 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800541 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400542 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500543
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);
Eric Miao6ae19b02008-09-10 12:06:15 -0400563 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400564 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;
Imre Deakda970e62007-01-18 00:44:41 -0500646 int val;
647 int action;
648 int status;
649
650 m = &ts->msg[ts->msg_idx];
651 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500652
653 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
654 * built from two 8 bit values written msb-first.
655 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400656 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500657
658 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
659 switch (action) {
660 case ADS7846_FILTER_REPEAT:
661 break;
662 case ADS7846_FILTER_IGNORE:
663 ts->tc.ignore = 1;
664 /* Last message will contain ads7846_rx() as the
665 * completion function.
666 */
667 m = ts->last_msg;
668 break;
669 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400670 *(u16 *)t->rx_buf = val;
Imre Deakda970e62007-01-18 00:44:41 -0500671 ts->tc.ignore = 0;
672 m = &ts->msg[++ts->msg_idx];
673 break;
674 default:
675 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400676 }
677 status = spi_async(ts->spi, m);
678 if (status)
679 dev_err(&ts->spi->dev, "spi_async --> %d\n",
680 status);
681}
682
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800683static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800684{
Imre Deak1936d592007-01-18 00:45:31 -0500685 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800686 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800687
Imre Deakc9e617a2006-04-11 23:44:05 -0400688 spin_lock_irq(&ts->lock);
689
Eric Miao6ae19b02008-09-10 12:06:15 -0400690 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500691 device_suspended(&ts->spi->dev))) {
692 if (ts->pendown) {
693 struct input_dev *input = ts->input;
694
695 input_report_key(input, BTN_TOUCH, 0);
696 input_report_abs(input, ABS_PRESSURE, 0);
697 input_sync(input);
698
699 ts->pendown = 0;
700#ifdef VERBOSE
701 dev_dbg(&ts->spi->dev, "UP\n");
702#endif
703 }
704
David Brownell90845332006-05-25 18:44:20 -0700705 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400706 if (!device_suspended(&ts->spi->dev)) {
707 ts->irq_disabled = 0;
708 enable_irq(ts->spi->irq);
709 }
710 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400711 } else {
712 /* pen is still down, continue with the measurement */
713 ts->msg_idx = 0;
714 status = spi_async(ts->spi, &ts->msg[0]);
715 if (status)
716 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
717 }
718
719 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500720 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800721}
722
David Howells7d12e782006-10-05 14:55:46 +0100723static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800724{
Imre Deak0b7018a2006-04-11 23:42:03 -0400725 struct ads7846 *ts = handle;
726 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400727
728 spin_lock_irqsave(&ts->lock, flags);
Eric Miao6ae19b02008-09-10 12:06:15 -0400729 if (likely(get_pendown_state(ts))) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400730 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700731 /* The ARM do_simple_IRQ() dispatcher doesn't act
732 * like the other dispatchers: it will report IRQs
733 * even after they've been disabled. We work around
734 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400735 */
736 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400737 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400738 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500739 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800740 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400741 }
742 }
743 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400744
745 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800746}
747
748/*--------------------------------------------------------------------------*/
749
Imre Deak7de90a82006-04-11 23:43:55 -0400750/* Must be called with ts->lock held */
751static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800752{
Imre Deak7de90a82006-04-11 23:43:55 -0400753 if (ts->disabled)
754 return;
David Brownellffa458c2006-01-08 13:34:21 -0800755
Imre Deakc9e617a2006-04-11 23:44:05 -0400756 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800757
Imre Deakc9e617a2006-04-11 23:44:05 -0400758 /* are we waiting for IRQ, or polling? */
759 if (!ts->pending) {
760 ts->irq_disabled = 1;
761 disable_irq(ts->spi->irq);
762 } else {
763 /* the timer will run at least once more, and
764 * leave everything in a clean state, IRQ disabled
765 */
766 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400767 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400768 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400769 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800770 }
771 }
772
773 /* we know the chip's in lowpower mode since we always
774 * leave it that way after every request
775 */
776
Imre Deak7de90a82006-04-11 23:43:55 -0400777}
778
779/* Must be called with ts->lock held */
780static void ads7846_enable(struct ads7846 *ts)
781{
782 if (!ts->disabled)
783 return;
784
785 ts->disabled = 0;
786 ts->irq_disabled = 0;
787 enable_irq(ts->spi->irq);
788}
789
790static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
791{
792 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
793
794 spin_lock_irq(&ts->lock);
795
David Brownellfbb38e32007-12-14 01:26:33 -0500796 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400797 ads7846_disable(ts);
798
799 spin_unlock_irq(&ts->lock);
800
David Brownellffa458c2006-01-08 13:34:21 -0800801 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400802
David Brownellffa458c2006-01-08 13:34:21 -0800803}
804
David Brownell2e5a7bd2006-01-08 13:34:25 -0800805static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800806{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800807 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800808
Imre Deak7de90a82006-04-11 23:43:55 -0400809 spin_lock_irq(&ts->lock);
810
David Brownellfbb38e32007-12-14 01:26:33 -0500811 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400812 ads7846_enable(ts);
813
814 spin_unlock_irq(&ts->lock);
815
David Brownellffa458c2006-01-08 13:34:21 -0800816 return 0;
817}
818
Eric Miao6ae19b02008-09-10 12:06:15 -0400819static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
820{
821 struct ads7846_platform_data *pdata = spi->dev.platform_data;
822 int err;
823
824 /* REVISIT when the irq can be triggered active-low, or if for some
825 * reason the touchscreen isn't hooked up, we don't need to access
826 * the pendown state.
827 */
828 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
829 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
830 return -EINVAL;
831 }
832
833 if (pdata->get_pendown_state) {
834 ts->get_pendown_state = pdata->get_pendown_state;
835 return 0;
836 }
837
838 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
839 if (err) {
840 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
841 pdata->gpio_pendown);
842 return err;
843 }
844
845 ts->gpio_pendown = pdata->gpio_pendown;
846 return 0;
847}
848
David Brownell2e5a7bd2006-01-08 13:34:25 -0800849static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800850{
David Brownellffa458c2006-01-08 13:34:21 -0800851 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500852 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800853 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400854 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800855 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500856 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500857 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800858
859 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800860 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800861 return -ENODEV;
862 }
863
864 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800865 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800866 return -ENODEV;
867 }
868
869 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500870 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800871 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500872 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800873 return -EINVAL;
874 }
875
David Brownell90845332006-05-25 18:44:20 -0700876 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
877 * that even if the hardware can do that, the SPI controller driver
878 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800879 */
David Brownell90845332006-05-25 18:44:20 -0700880 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400881 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500882 err = spi_setup(spi);
883 if (err < 0)
884 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800885
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500886 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
887 input_dev = input_allocate_device();
888 if (!ts || !input_dev) {
889 err = -ENOMEM;
890 goto err_free_mem;
891 }
David Brownellffa458c2006-01-08 13:34:21 -0800892
David Brownell2e5a7bd2006-01-08 13:34:25 -0800893 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800894
895 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500896 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400897 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800898
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800899 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800900 ts->timer.function = ads7846_timer;
901
Imre Deak7de90a82006-04-11 23:43:55 -0400902 spin_lock_init(&ts->lock);
903
David Brownellffa458c2006-01-08 13:34:21 -0800904 ts->model = pdata->model ? : 7846;
905 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
906 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400907 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500908
909 if (pdata->filter != NULL) {
910 if (pdata->filter_init != NULL) {
911 err = pdata->filter_init(pdata, &ts->filter_data);
912 if (err < 0)
913 goto err_free_mem;
914 }
915 ts->filter = pdata->filter;
916 ts->filter_cleanup = pdata->filter_cleanup;
917 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400918 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500919 if (ts->debounce_max < 2)
920 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400921 ts->debounce_tol = pdata->debounce_tol;
922 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500923 ts->filter = ads7846_debounce;
924 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400925 } else
Imre Deakda970e62007-01-18 00:44:41 -0500926 ts->filter = ads7846_no_filter;
Eric Miao6ae19b02008-09-10 12:06:15 -0400927
928 err = setup_pendown(spi, ts);
929 if (err)
930 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800931
Semih Hazar1d258912007-07-18 00:36:04 -0400932 if (pdata->penirq_recheck_delay_usecs)
933 ts->penirq_recheck_delay_usecs =
934 pdata->penirq_recheck_delay_usecs;
935
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500936 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800937
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500938 input_dev->name = "ADS784x Touchscreen";
939 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400940 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800941
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700942 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
943 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500944 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800945 pdata->x_min ? : 0,
946 pdata->x_max ? : MAX_12BIT,
947 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500948 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800949 pdata->y_min ? : 0,
950 pdata->y_max ? : MAX_12BIT,
951 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500952 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800953 pdata->pressure_min, pdata->pressure_max, 0, 0);
954
Imre Deakde2defd2007-01-18 00:45:21 -0500955 vref = pdata->keep_vref_on;
956
David Brownellffa458c2006-01-08 13:34:21 -0800957 /* set up the transfers to read touchscreen state; this assumes we
958 * use formula #2 for pressure, not #3.
959 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400960 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800961 x = ts->xfer;
962
Imre Deak0b7018a2006-04-11 23:42:03 -0400963 spi_message_init(m);
964
David Brownellffa458c2006-01-08 13:34:21 -0800965 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500966 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500967 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800968 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400969 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500970
David Brownellffa458c2006-01-08 13:34:21 -0800971 x++;
972 x->rx_buf = &ts->tc.y;
973 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400974 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800975
Semih Hazare4f48862007-07-18 00:35:56 -0400976 /* the first sample after switching drivers can be low quality;
977 * optionally discard it, using a second one after the signals
978 * have had enough time to stabilize.
979 */
980 if (pdata->settle_delay_usecs) {
981 x->delay_usecs = pdata->settle_delay_usecs;
982
983 x++;
984 x->tx_buf = &ts->read_y;
985 x->len = 1;
986 spi_message_add_tail(x, m);
987
988 x++;
989 x->rx_buf = &ts->tc.y;
990 x->len = 2;
991 spi_message_add_tail(x, m);
992 }
993
Imre Deakda970e62007-01-18 00:44:41 -0500994 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400995 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500996
Imre Deak0b7018a2006-04-11 23:42:03 -0400997 m++;
998 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800999
1000 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -05001001 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001002 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -05001003 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -08001004 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001005 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001006
David Brownellffa458c2006-01-08 13:34:21 -08001007 x++;
1008 x->rx_buf = &ts->tc.x;
1009 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001010 spi_message_add_tail(x, m);
1011
Semih Hazare4f48862007-07-18 00:35:56 -04001012 /* ... maybe discard first sample ... */
1013 if (pdata->settle_delay_usecs) {
1014 x->delay_usecs = pdata->settle_delay_usecs;
1015
1016 x++;
1017 x->tx_buf = &ts->read_x;
1018 x->len = 1;
1019 spi_message_add_tail(x, m);
1020
1021 x++;
1022 x->rx_buf = &ts->tc.x;
1023 x->len = 2;
1024 spi_message_add_tail(x, m);
1025 }
1026
Imre Deakda970e62007-01-18 00:44:41 -05001027 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001028 m->context = ts;
1029
1030 /* turn y+ off, x- on; we'll use formula #2 */
1031 if (ts->model == 7846) {
1032 m++;
1033 spi_message_init(m);
1034
1035 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001036 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001037 x->tx_buf = &ts->read_z1;
1038 x->len = 1;
1039 spi_message_add_tail(x, m);
1040
1041 x++;
1042 x->rx_buf = &ts->tc.z1;
1043 x->len = 2;
1044 spi_message_add_tail(x, m);
1045
Semih Hazare4f48862007-07-18 00:35:56 -04001046 /* ... maybe discard first sample ... */
1047 if (pdata->settle_delay_usecs) {
1048 x->delay_usecs = pdata->settle_delay_usecs;
1049
1050 x++;
1051 x->tx_buf = &ts->read_z1;
1052 x->len = 1;
1053 spi_message_add_tail(x, m);
1054
1055 x++;
1056 x->rx_buf = &ts->tc.z1;
1057 x->len = 2;
1058 spi_message_add_tail(x, m);
1059 }
1060
Imre Deakda970e62007-01-18 00:44:41 -05001061 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001062 m->context = ts;
1063
1064 m++;
1065 spi_message_init(m);
1066
1067 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001068 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001069 x->tx_buf = &ts->read_z2;
1070 x->len = 1;
1071 spi_message_add_tail(x, m);
1072
1073 x++;
1074 x->rx_buf = &ts->tc.z2;
1075 x->len = 2;
1076 spi_message_add_tail(x, m);
1077
Semih Hazare4f48862007-07-18 00:35:56 -04001078 /* ... maybe discard first sample ... */
1079 if (pdata->settle_delay_usecs) {
1080 x->delay_usecs = pdata->settle_delay_usecs;
1081
1082 x++;
1083 x->tx_buf = &ts->read_z2;
1084 x->len = 1;
1085 spi_message_add_tail(x, m);
1086
1087 x++;
1088 x->rx_buf = &ts->tc.z2;
1089 x->len = 2;
1090 spi_message_add_tail(x, m);
1091 }
1092
Imre Deakda970e62007-01-18 00:44:41 -05001093 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001094 m->context = ts;
1095 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001096
1097 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001098 m++;
1099 spi_message_init(m);
1100
Imre Deak53a0ef892006-04-11 23:41:49 -04001101 x++;
1102 ts->pwrdown = PWRDOWN;
1103 x->tx_buf = &ts->pwrdown;
1104 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001105 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001106
1107 x++;
1108 x->rx_buf = &ts->dummy;
1109 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001110 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001111 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001112
Imre Deak0b7018a2006-04-11 23:42:03 -04001113 m->complete = ads7846_rx;
1114 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001115
Imre Deakd5b415c2006-04-26 00:13:18 -04001116 ts->last_msg = m;
1117
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001118 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001119 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001120 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001121 err = -EBUSY;
Eric Miao6ae19b02008-09-10 12:06:15 -04001122 goto err_free_gpio;
David Brownellffa458c2006-01-08 13:34:21 -08001123 }
David Brownellffa458c2006-01-08 13:34:21 -08001124
David Brownell2c8dc072007-01-18 00:45:48 -05001125 err = ads784x_hwmon_register(spi, ts);
1126 if (err)
1127 goto err_free_irq;
1128
David Brownell2e5a7bd2006-01-08 13:34:25 -08001129 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001130
David Brownell2c8dc072007-01-18 00:45:48 -05001131 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001132 * the touchscreen, in case it's not connected.
1133 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001134 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001135 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1136
David Brownell2c8dc072007-01-18 00:45:48 -05001137 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001138 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001139 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001140
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001141 err = input_register_device(input_dev);
1142 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001143 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001144
David Brownellffa458c2006-01-08 13:34:21 -08001145 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001146
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001147 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001148 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1149 err_remove_hwmon:
1150 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001151 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001152 free_irq(spi->irq, ts);
Eric Miao6ae19b02008-09-10 12:06:15 -04001153 err_free_gpio:
1154 if (ts->gpio_pendown != -1)
1155 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001156 err_cleanup_filter:
1157 if (ts->filter_cleanup)
1158 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001159 err_free_mem:
1160 input_free_device(input_dev);
1161 kfree(ts);
1162 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001163}
1164
David Brownell2e5a7bd2006-01-08 13:34:25 -08001165static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001166{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001167 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001168
David Brownell2c8dc072007-01-18 00:45:48 -05001169 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001170 input_unregister_device(ts->input);
1171
David Brownell2e5a7bd2006-01-08 13:34:25 -08001172 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001173
David Brownell2c8dc072007-01-18 00:45:48 -05001174 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001175
Imre Deak7de90a82006-04-11 23:43:55 -04001176 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001177 /* suspend left the IRQ disabled */
1178 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001179
Eric Miao6ae19b02008-09-10 12:06:15 -04001180 if (ts->gpio_pendown != -1)
1181 gpio_free(ts->gpio_pendown);
1182
Imre Deakda970e62007-01-18 00:44:41 -05001183 if (ts->filter_cleanup)
1184 ts->filter_cleanup(ts->filter_data);
1185
David Brownellffa458c2006-01-08 13:34:21 -08001186 kfree(ts);
1187
David Brownell2e5a7bd2006-01-08 13:34:25 -08001188 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001189 return 0;
1190}
1191
David Brownell2e5a7bd2006-01-08 13:34:25 -08001192static struct spi_driver ads7846_driver = {
1193 .driver = {
1194 .name = "ads7846",
1195 .bus = &spi_bus_type,
1196 .owner = THIS_MODULE,
1197 },
David Brownellffa458c2006-01-08 13:34:21 -08001198 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001199 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001200 .suspend = ads7846_suspend,
1201 .resume = ads7846_resume,
1202};
1203
1204static int __init ads7846_init(void)
1205{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001206 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001207}
1208module_init(ads7846_init);
1209
1210static void __exit ads7846_exit(void)
1211{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001212 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001213}
1214module_exit(ads7846_exit);
1215
1216MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1217MODULE_LICENSE("GPL");