blob: 6020a7dcce332be46ca39b61828da09e2d4109cb [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 Miao4d5975e2008-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 Miao4d5975e2008-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);
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400466 long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400467
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400468 if (strict_strtoul(buf, 10, &i))
469 return -EINVAL;
470
Imre Deak7de90a82006-04-11 23:43:55 -0400471 spin_lock_irq(&ts->lock);
472
473 if (i)
474 ads7846_disable(ts);
475 else
476 ads7846_enable(ts);
477
478 spin_unlock_irq(&ts->lock);
479
480 return count;
481}
482
483static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
484
David Brownell2c8dc072007-01-18 00:45:48 -0500485static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500486 &dev_attr_pen_down.attr,
487 &dev_attr_disable.attr,
488 NULL,
489};
490
David Brownell2c8dc072007-01-18 00:45:48 -0500491static struct attribute_group ads784x_attr_group = {
492 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500493};
494
David Brownellffa458c2006-01-08 13:34:21 -0800495/*--------------------------------------------------------------------------*/
496
Eric Miao4d5975e2008-09-10 12:06:15 -0400497static int get_pendown_state(struct ads7846 *ts)
498{
499 if (ts->get_pendown_state)
500 return ts->get_pendown_state();
501
502 return !gpio_get_value(ts->gpio_pendown);
503}
504
David Brownellffa458c2006-01-08 13:34:21 -0800505/*
506 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
507 * to retrieve touchscreen status.
508 *
509 * The SPI transfer completion callback does the real work. It reports
510 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
511 */
512
513static void ads7846_rx(void *ads)
514{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500515 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500516 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500517 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800518
Imre Deakda970e62007-01-18 00:44:41 -0500519 /* ads7846_rx_val() did in-place conversion (including byteswap) from
520 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800521 */
Imre Deakda970e62007-01-18 00:44:41 -0500522 x = ts->tc.x;
523 y = ts->tc.y;
524 z1 = ts->tc.z1;
525 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800526
527 /* range filtering */
528 if (x == MAX_12BIT)
529 x = 0;
530
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400531 if (ts->model == 7843) {
532 Rt = ts->pressure_max / 2;
533 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800534 /* compute touch pressure resistance using equation #2 */
535 Rt = z2;
536 Rt -= z1;
537 Rt *= x;
538 Rt *= ts->x_plate_ohms;
539 Rt /= z1;
540 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400541 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800542 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400543 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500544
Imre Deakd5b415c2006-04-26 00:13:18 -0400545 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500546 * the maximum. Don't report it to user space, repeat at least
547 * once more the measurement
548 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400549 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500550#ifdef VERBOSE
551 pr_debug("%s: ignored %d pressure %d\n",
552 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
553#endif
Imre Deak1936d592007-01-18 00:45:31 -0500554 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800555 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400556 return;
557 }
558
Semih Hazar1d258912007-07-18 00:36:04 -0400559 /* Maybe check the pendown state before reporting. This discards
560 * false readings when the pen is lifted.
561 */
562 if (ts->penirq_recheck_delay_usecs) {
563 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400564 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400565 Rt = 0;
566 }
567
Imre Deak15e35892007-01-18 00:45:43 -0500568 /* NOTE: We can't rely on the pressure to determine the pen down
569 * state, even this controller has a pressure sensor. The pressure
570 * value can fluctuate for quite a while after lifting the pen and
571 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800572 *
Imre Deak15e35892007-01-18 00:45:43 -0500573 * The only safe way to check for the pen up condition is in the
574 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800575 */
David Brownellffa458c2006-01-08 13:34:21 -0800576 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500577 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400578
Imre Deak15e35892007-01-18 00:45:43 -0500579 if (!ts->pendown) {
580 input_report_key(input, BTN_TOUCH, 1);
581 ts->pendown = 1;
582#ifdef VERBOSE
583 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800584#endif
Imre Deak15e35892007-01-18 00:45:43 -0500585 }
586 input_report_abs(input, ABS_X, x);
587 input_report_abs(input, ABS_Y, y);
588 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800589
Imre Deak15e35892007-01-18 00:45:43 -0500590 input_sync(input);
591#ifdef VERBOSE
592 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
593#endif
594 }
David Brownellffa458c2006-01-08 13:34:21 -0800595
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800596 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
597 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800598}
599
Imre Deakda970e62007-01-18 00:44:41 -0500600static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400601{
602 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400603
Imre Deakda970e62007-01-18 00:44:41 -0500604 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
605 /* Start over collecting consistent readings. */
606 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400607 /* Repeat it, if this was the first read or the read
608 * wasn't consistent enough. */
609 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500610 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400611 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500612 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400613 } else {
614 /* Maximum number of debouncing reached and still
615 * not enough number of consistent readings. Abort
616 * the whole sample, repeat it in the next sampling
617 * period.
618 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400619 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500620 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400621 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400622 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400623 if (++ts->read_rep > ts->debounce_rep) {
624 /* Got a good reading for this coordinate,
625 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400626 ts->read_cnt = 0;
627 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500628 return ADS7846_FILTER_OK;
629 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400630 /* Read more values that are consistent. */
631 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500632 return ADS7846_FILTER_REPEAT;
633 }
634 }
635}
636
637static int ads7846_no_filter(void *ads, int data_idx, int *val)
638{
639 return ADS7846_FILTER_OK;
640}
641
642static void ads7846_rx_val(void *ads)
643{
644 struct ads7846 *ts = ads;
645 struct spi_message *m;
646 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500647 int val;
648 int action;
649 int status;
650
651 m = &ts->msg[ts->msg_idx];
652 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500653
654 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
655 * built from two 8 bit values written msb-first.
656 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400657 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500658
659 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
660 switch (action) {
661 case ADS7846_FILTER_REPEAT:
662 break;
663 case ADS7846_FILTER_IGNORE:
664 ts->tc.ignore = 1;
665 /* Last message will contain ads7846_rx() as the
666 * completion function.
667 */
668 m = ts->last_msg;
669 break;
670 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400671 *(u16 *)t->rx_buf = val;
Imre Deakda970e62007-01-18 00:44:41 -0500672 ts->tc.ignore = 0;
673 m = &ts->msg[++ts->msg_idx];
674 break;
675 default:
676 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400677 }
678 status = spi_async(ts->spi, m);
679 if (status)
680 dev_err(&ts->spi->dev, "spi_async --> %d\n",
681 status);
682}
683
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800684static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800685{
Imre Deak1936d592007-01-18 00:45:31 -0500686 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800687 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800688
Imre Deakc9e617a2006-04-11 23:44:05 -0400689 spin_lock_irq(&ts->lock);
690
Eric Miao4d5975e2008-09-10 12:06:15 -0400691 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500692 device_suspended(&ts->spi->dev))) {
693 if (ts->pendown) {
694 struct input_dev *input = ts->input;
695
696 input_report_key(input, BTN_TOUCH, 0);
697 input_report_abs(input, ABS_PRESSURE, 0);
698 input_sync(input);
699
700 ts->pendown = 0;
701#ifdef VERBOSE
702 dev_dbg(&ts->spi->dev, "UP\n");
703#endif
704 }
705
David Brownell90845332006-05-25 18:44:20 -0700706 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400707 if (!device_suspended(&ts->spi->dev)) {
708 ts->irq_disabled = 0;
709 enable_irq(ts->spi->irq);
710 }
711 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400712 } else {
713 /* pen is still down, continue with the measurement */
714 ts->msg_idx = 0;
715 status = spi_async(ts->spi, &ts->msg[0]);
716 if (status)
717 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
718 }
719
720 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500721 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800722}
723
David Howells7d12e782006-10-05 14:55:46 +0100724static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800725{
Imre Deak0b7018a2006-04-11 23:42:03 -0400726 struct ads7846 *ts = handle;
727 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400728
729 spin_lock_irqsave(&ts->lock, flags);
Eric Miao4d5975e2008-09-10 12:06:15 -0400730 if (likely(get_pendown_state(ts))) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400731 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700732 /* The ARM do_simple_IRQ() dispatcher doesn't act
733 * like the other dispatchers: it will report IRQs
734 * even after they've been disabled. We work around
735 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400736 */
737 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400738 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400739 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500740 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800741 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400742 }
743 }
744 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400745
746 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800747}
748
749/*--------------------------------------------------------------------------*/
750
Imre Deak7de90a82006-04-11 23:43:55 -0400751/* Must be called with ts->lock held */
752static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800753{
Imre Deak7de90a82006-04-11 23:43:55 -0400754 if (ts->disabled)
755 return;
David Brownellffa458c2006-01-08 13:34:21 -0800756
Imre Deakc9e617a2006-04-11 23:44:05 -0400757 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800758
Imre Deakc9e617a2006-04-11 23:44:05 -0400759 /* are we waiting for IRQ, or polling? */
760 if (!ts->pending) {
761 ts->irq_disabled = 1;
762 disable_irq(ts->spi->irq);
763 } else {
764 /* the timer will run at least once more, and
765 * leave everything in a clean state, IRQ disabled
766 */
767 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400768 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400769 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400770 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800771 }
772 }
773
774 /* we know the chip's in lowpower mode since we always
775 * leave it that way after every request
776 */
777
Imre Deak7de90a82006-04-11 23:43:55 -0400778}
779
780/* Must be called with ts->lock held */
781static void ads7846_enable(struct ads7846 *ts)
782{
783 if (!ts->disabled)
784 return;
785
786 ts->disabled = 0;
787 ts->irq_disabled = 0;
788 enable_irq(ts->spi->irq);
789}
790
791static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
792{
793 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
794
795 spin_lock_irq(&ts->lock);
796
David Brownellfbb38e32007-12-14 01:26:33 -0500797 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400798 ads7846_disable(ts);
799
800 spin_unlock_irq(&ts->lock);
801
David Brownellffa458c2006-01-08 13:34:21 -0800802 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400803
David Brownellffa458c2006-01-08 13:34:21 -0800804}
805
David Brownell2e5a7bd2006-01-08 13:34:25 -0800806static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800807{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800808 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800809
Imre Deak7de90a82006-04-11 23:43:55 -0400810 spin_lock_irq(&ts->lock);
811
David Brownellfbb38e32007-12-14 01:26:33 -0500812 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400813 ads7846_enable(ts);
814
815 spin_unlock_irq(&ts->lock);
816
David Brownellffa458c2006-01-08 13:34:21 -0800817 return 0;
818}
819
Eric Miao4d5975e2008-09-10 12:06:15 -0400820static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
821{
822 struct ads7846_platform_data *pdata = spi->dev.platform_data;
823 int err;
824
825 /* REVISIT when the irq can be triggered active-low, or if for some
826 * reason the touchscreen isn't hooked up, we don't need to access
827 * the pendown state.
828 */
829 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
830 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
831 return -EINVAL;
832 }
833
834 if (pdata->get_pendown_state) {
835 ts->get_pendown_state = pdata->get_pendown_state;
836 return 0;
837 }
838
839 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
840 if (err) {
841 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
842 pdata->gpio_pendown);
843 return err;
844 }
845
846 ts->gpio_pendown = pdata->gpio_pendown;
847 return 0;
848}
849
David Brownell2e5a7bd2006-01-08 13:34:25 -0800850static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800851{
David Brownellffa458c2006-01-08 13:34:21 -0800852 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500853 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800854 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400855 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800856 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500857 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500858 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800859
860 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800861 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800862 return -ENODEV;
863 }
864
865 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800866 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800867 return -ENODEV;
868 }
869
870 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500871 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800872 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500873 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800874 return -EINVAL;
875 }
876
David Brownell90845332006-05-25 18:44:20 -0700877 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
878 * that even if the hardware can do that, the SPI controller driver
879 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800880 */
David Brownell90845332006-05-25 18:44:20 -0700881 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400882 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500883 err = spi_setup(spi);
884 if (err < 0)
885 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800886
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500887 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
888 input_dev = input_allocate_device();
889 if (!ts || !input_dev) {
890 err = -ENOMEM;
891 goto err_free_mem;
892 }
David Brownellffa458c2006-01-08 13:34:21 -0800893
David Brownell2e5a7bd2006-01-08 13:34:25 -0800894 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800895
896 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500897 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400898 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800899
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800900 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800901 ts->timer.function = ads7846_timer;
902
Imre Deak7de90a82006-04-11 23:43:55 -0400903 spin_lock_init(&ts->lock);
904
David Brownellffa458c2006-01-08 13:34:21 -0800905 ts->model = pdata->model ? : 7846;
906 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
907 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400908 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500909
910 if (pdata->filter != NULL) {
911 if (pdata->filter_init != NULL) {
912 err = pdata->filter_init(pdata, &ts->filter_data);
913 if (err < 0)
914 goto err_free_mem;
915 }
916 ts->filter = pdata->filter;
917 ts->filter_cleanup = pdata->filter_cleanup;
918 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400919 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500920 if (ts->debounce_max < 2)
921 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400922 ts->debounce_tol = pdata->debounce_tol;
923 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500924 ts->filter = ads7846_debounce;
925 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400926 } else
Imre Deakda970e62007-01-18 00:44:41 -0500927 ts->filter = ads7846_no_filter;
Eric Miao4d5975e2008-09-10 12:06:15 -0400928
929 err = setup_pendown(spi, ts);
930 if (err)
931 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800932
Semih Hazar1d258912007-07-18 00:36:04 -0400933 if (pdata->penirq_recheck_delay_usecs)
934 ts->penirq_recheck_delay_usecs =
935 pdata->penirq_recheck_delay_usecs;
936
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500937 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800938
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500939 input_dev->name = "ADS784x Touchscreen";
940 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400941 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800942
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700943 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
944 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500945 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800946 pdata->x_min ? : 0,
947 pdata->x_max ? : MAX_12BIT,
948 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500949 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800950 pdata->y_min ? : 0,
951 pdata->y_max ? : MAX_12BIT,
952 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500953 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800954 pdata->pressure_min, pdata->pressure_max, 0, 0);
955
Imre Deakde2defd2007-01-18 00:45:21 -0500956 vref = pdata->keep_vref_on;
957
David Brownellffa458c2006-01-08 13:34:21 -0800958 /* set up the transfers to read touchscreen state; this assumes we
959 * use formula #2 for pressure, not #3.
960 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400961 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800962 x = ts->xfer;
963
Imre Deak0b7018a2006-04-11 23:42:03 -0400964 spi_message_init(m);
965
David Brownellffa458c2006-01-08 13:34:21 -0800966 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500967 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500968 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800969 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400970 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500971
David Brownellffa458c2006-01-08 13:34:21 -0800972 x++;
973 x->rx_buf = &ts->tc.y;
974 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400975 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800976
Semih Hazare4f48862007-07-18 00:35:56 -0400977 /* the first sample after switching drivers can be low quality;
978 * optionally discard it, using a second one after the signals
979 * have had enough time to stabilize.
980 */
981 if (pdata->settle_delay_usecs) {
982 x->delay_usecs = pdata->settle_delay_usecs;
983
984 x++;
985 x->tx_buf = &ts->read_y;
986 x->len = 1;
987 spi_message_add_tail(x, m);
988
989 x++;
990 x->rx_buf = &ts->tc.y;
991 x->len = 2;
992 spi_message_add_tail(x, m);
993 }
994
Imre Deakda970e62007-01-18 00:44:41 -0500995 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400996 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500997
Imre Deak0b7018a2006-04-11 23:42:03 -0400998 m++;
999 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -08001000
1001 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -05001002 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001003 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -05001004 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -08001005 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001006 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001007
David Brownellffa458c2006-01-08 13:34:21 -08001008 x++;
1009 x->rx_buf = &ts->tc.x;
1010 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001011 spi_message_add_tail(x, m);
1012
Semih Hazare4f48862007-07-18 00:35:56 -04001013 /* ... maybe discard first sample ... */
1014 if (pdata->settle_delay_usecs) {
1015 x->delay_usecs = pdata->settle_delay_usecs;
1016
1017 x++;
1018 x->tx_buf = &ts->read_x;
1019 x->len = 1;
1020 spi_message_add_tail(x, m);
1021
1022 x++;
1023 x->rx_buf = &ts->tc.x;
1024 x->len = 2;
1025 spi_message_add_tail(x, m);
1026 }
1027
Imre Deakda970e62007-01-18 00:44:41 -05001028 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001029 m->context = ts;
1030
1031 /* turn y+ off, x- on; we'll use formula #2 */
1032 if (ts->model == 7846) {
1033 m++;
1034 spi_message_init(m);
1035
1036 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001037 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001038 x->tx_buf = &ts->read_z1;
1039 x->len = 1;
1040 spi_message_add_tail(x, m);
1041
1042 x++;
1043 x->rx_buf = &ts->tc.z1;
1044 x->len = 2;
1045 spi_message_add_tail(x, m);
1046
Semih Hazare4f48862007-07-18 00:35:56 -04001047 /* ... maybe discard first sample ... */
1048 if (pdata->settle_delay_usecs) {
1049 x->delay_usecs = pdata->settle_delay_usecs;
1050
1051 x++;
1052 x->tx_buf = &ts->read_z1;
1053 x->len = 1;
1054 spi_message_add_tail(x, m);
1055
1056 x++;
1057 x->rx_buf = &ts->tc.z1;
1058 x->len = 2;
1059 spi_message_add_tail(x, m);
1060 }
1061
Imre Deakda970e62007-01-18 00:44:41 -05001062 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001063 m->context = ts;
1064
1065 m++;
1066 spi_message_init(m);
1067
1068 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001069 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001070 x->tx_buf = &ts->read_z2;
1071 x->len = 1;
1072 spi_message_add_tail(x, m);
1073
1074 x++;
1075 x->rx_buf = &ts->tc.z2;
1076 x->len = 2;
1077 spi_message_add_tail(x, m);
1078
Semih Hazare4f48862007-07-18 00:35:56 -04001079 /* ... maybe discard first sample ... */
1080 if (pdata->settle_delay_usecs) {
1081 x->delay_usecs = pdata->settle_delay_usecs;
1082
1083 x++;
1084 x->tx_buf = &ts->read_z2;
1085 x->len = 1;
1086 spi_message_add_tail(x, m);
1087
1088 x++;
1089 x->rx_buf = &ts->tc.z2;
1090 x->len = 2;
1091 spi_message_add_tail(x, m);
1092 }
1093
Imre Deakda970e62007-01-18 00:44:41 -05001094 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001095 m->context = ts;
1096 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001097
1098 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001099 m++;
1100 spi_message_init(m);
1101
Imre Deak53a0ef892006-04-11 23:41:49 -04001102 x++;
1103 ts->pwrdown = PWRDOWN;
1104 x->tx_buf = &ts->pwrdown;
1105 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001106 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001107
1108 x++;
1109 x->rx_buf = &ts->dummy;
1110 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001111 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001112 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001113
Imre Deak0b7018a2006-04-11 23:42:03 -04001114 m->complete = ads7846_rx;
1115 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001116
Imre Deakd5b415c2006-04-26 00:13:18 -04001117 ts->last_msg = m;
1118
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001119 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001120 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001121 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001122 err = -EBUSY;
Eric Miao4d5975e2008-09-10 12:06:15 -04001123 goto err_free_gpio;
David Brownellffa458c2006-01-08 13:34:21 -08001124 }
David Brownellffa458c2006-01-08 13:34:21 -08001125
David Brownell2c8dc072007-01-18 00:45:48 -05001126 err = ads784x_hwmon_register(spi, ts);
1127 if (err)
1128 goto err_free_irq;
1129
David Brownell2e5a7bd2006-01-08 13:34:25 -08001130 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001131
David Brownell2c8dc072007-01-18 00:45:48 -05001132 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001133 * the touchscreen, in case it's not connected.
1134 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001135 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001136 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1137
David Brownell2c8dc072007-01-18 00:45:48 -05001138 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001139 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001140 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001141
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001142 err = input_register_device(input_dev);
1143 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001144 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001145
David Brownellffa458c2006-01-08 13:34:21 -08001146 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001147
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001148 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001149 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1150 err_remove_hwmon:
1151 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001152 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001153 free_irq(spi->irq, ts);
Eric Miao4d5975e2008-09-10 12:06:15 -04001154 err_free_gpio:
1155 if (ts->gpio_pendown != -1)
1156 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001157 err_cleanup_filter:
1158 if (ts->filter_cleanup)
1159 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001160 err_free_mem:
1161 input_free_device(input_dev);
1162 kfree(ts);
1163 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001164}
1165
David Brownell2e5a7bd2006-01-08 13:34:25 -08001166static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001167{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001168 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001169
David Brownell2c8dc072007-01-18 00:45:48 -05001170 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001171 input_unregister_device(ts->input);
1172
David Brownell2e5a7bd2006-01-08 13:34:25 -08001173 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001174
David Brownell2c8dc072007-01-18 00:45:48 -05001175 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001176
Imre Deak7de90a82006-04-11 23:43:55 -04001177 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001178 /* suspend left the IRQ disabled */
1179 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001180
Eric Miao4d5975e2008-09-10 12:06:15 -04001181 if (ts->gpio_pendown != -1)
1182 gpio_free(ts->gpio_pendown);
1183
Imre Deakda970e62007-01-18 00:44:41 -05001184 if (ts->filter_cleanup)
1185 ts->filter_cleanup(ts->filter_data);
1186
David Brownellffa458c2006-01-08 13:34:21 -08001187 kfree(ts);
1188
David Brownell2e5a7bd2006-01-08 13:34:25 -08001189 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001190 return 0;
1191}
1192
David Brownell2e5a7bd2006-01-08 13:34:25 -08001193static struct spi_driver ads7846_driver = {
1194 .driver = {
1195 .name = "ads7846",
1196 .bus = &spi_bus_type,
1197 .owner = THIS_MODULE,
1198 },
David Brownellffa458c2006-01-08 13:34:21 -08001199 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001200 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001201 .suspend = ads7846_suspend,
1202 .resume = ads7846_resume,
1203};
1204
1205static int __init ads7846_init(void)
1206{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001207 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001208}
1209module_init(ads7846_init);
1210
1211static void __exit ads7846_exit(void)
1212{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001213 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001214}
1215module_exit(ads7846_exit);
1216
1217MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1218MODULE_LICENSE("GPL");