blob: 69210cb56c543c808bb15dffdb62cbb651009d03 [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>
Grazvydas Ignotas91143372010-02-25 02:04:56 -080030#include <linux/regulator/consumer.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080031#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080032
David Brownellffa458c2006-01-08 13:34:21 -080033/*
David Brownell90845332006-05-25 18:44:20 -070034 * This code has been heavily tested on a Nokia 770, and lightly
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080035 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
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.
Michael Hennerich06a09122010-03-09 20:38:45 -080039 * Support for Analog Devices AD7873 and AD7843 tested.
David Brownellffa458c2006-01-08 13:34:21 -080040 *
Imre Deak7de90a82006-04-11 23:43:55 -040041 * IRQ handling needs a workaround because of a shortcoming in handling
42 * edge triggered IRQs on some platforms like the OMAP1/2. These
43 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
44 * have to maintain our own SW IRQ disabled status. This should be
45 * removed as soon as the affected platform's IRQ handling is fixed.
46 *
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080047 * App note sbaa036 talks in more detail about accurate sampling...
David Brownellffa458c2006-01-08 13:34:21 -080048 * that ought to help in situations like LCDs inducing noise (which
49 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040050 * This driver tries to utilize the measures described in the app
51 * note. The strength of filtering can be set in the board-* specific
52 * files.
David Brownellffa458c2006-01-08 13:34:21 -080053 */
54
Imre Deak1936d592007-01-18 00:45:31 -050055#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
56#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080057
David Brownelld93f70b2006-02-15 00:49:35 -050058/* this driver doesn't aim at the peak continuous sample rate */
59#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
60
David Brownellffa458c2006-01-08 13:34:21 -080061struct ts_event {
62 /* For portability, we can't read 12 bit values using SPI (which
63 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050064 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050065 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080066 */
Imre Deakda970e62007-01-18 00:44:41 -050067 u16 x;
68 u16 y;
69 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050070 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080071};
72
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040073/*
74 * We allocate this separately to avoid cache line sharing issues when
75 * driver is used with DMA-based SPI controllers (like atmel_spi) on
76 * systems where main memory is not DMA-coherent (most non-x86 boards).
77 */
78struct ads7846_packet {
79 u8 read_x, read_y, read_z1, read_z2, pwrdown;
80 u16 dummy; /* for the pwrdown read */
81 struct ts_event tc;
82};
83
David Brownellffa458c2006-01-08 13:34:21 -080084struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050085 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080086 char phys[32];
Michael Rothb58895f2009-05-18 16:05:12 -070087 char name[32];
David Brownellffa458c2006-01-08 13:34:21 -080088
89 struct spi_device *spi;
Grazvydas Ignotas91143372010-02-25 02:04:56 -080090 struct regulator *reg;
David Brownell2c8dc072007-01-18 00:45:48 -050091
92#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050093 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070094 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050095#endif
96
David Brownellffa458c2006-01-08 13:34:21 -080097 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -040098 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080099 u16 vref_delay_usecs;
100 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400101 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800102
Michael Roth86579a42009-05-18 16:04:44 -0700103 bool swap_xy;
104
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400105 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800106
Semih Hazare4f48862007-07-18 00:35:56 -0400107 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400108 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400109 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400110 int msg_idx;
111 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400112 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400113 int last_read;
114
115 u16 debounce_max;
116 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400117 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800118
Semih Hazar1d258912007-07-18 00:36:04 -0400119 u16 penirq_recheck_delay_usecs;
120
David Brownellffa458c2006-01-08 13:34:21 -0800121 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500122 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800123 unsigned pendown:1; /* P: lock */
124 unsigned pending:1; /* P: lock */
125// FIXME remove "irq_disabled"
126 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400127 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500128 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400129
Imre Deakda970e62007-01-18 00:44:41 -0500130 int (*filter)(void *data, int data_idx, int *val);
131 void *filter_data;
132 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400133 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400134 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700135
136 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800137};
138
139/* leave chip selected when we're done, for quicker re-select? */
140#if 0
141#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
142#else
143#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
144#endif
145
146/*--------------------------------------------------------------------------*/
147
148/* The ADS7846 has touchscreen and other sensors.
149 * Earlier ads784x chips are somewhat compatible.
150 */
151#define ADS_START (1 << 7)
152#define ADS_A2A1A0_d_y (1 << 4) /* differential */
153#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
154#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
155#define ADS_A2A1A0_d_x (5 << 4) /* differential */
156#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
157#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
158#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
159#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
160#define ADS_8_BIT (1 << 3)
161#define ADS_12_BIT (0 << 3)
162#define ADS_SER (1 << 2) /* non-differential */
163#define ADS_DFR (0 << 2) /* differential */
164#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
165#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
166#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
167#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
168
169#define MAX_12BIT ((1<<12)-1)
170
171/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500172#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
173 | ADS_12_BIT | ADS_DFR | \
174 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800175
Imre Deakde2defd2007-01-18 00:45:21 -0500176#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
177#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
178#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400179
Imre Deakde2defd2007-01-18 00:45:21 -0500180#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
181#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800182
183/* single-ended samples need to first power up reference voltage;
184 * we leave both ADC and VREF powered
185 */
186#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
187 | ADS_12_BIT | ADS_SER)
188
Imre Deakde2defd2007-01-18 00:45:21 -0500189#define REF_ON (READ_12BIT_DFR(x, 1, 1))
190#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800191
192/*--------------------------------------------------------------------------*/
193
194/*
195 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500196 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
197 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800198 */
199
200struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500201 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800202 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500203 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800204 u16 scratch;
205 __be16 sample;
206 struct spi_message msg;
207 struct spi_transfer xfer[6];
208};
209
Imre Deak7de90a82006-04-11 23:43:55 -0400210static void ads7846_enable(struct ads7846 *ts);
211static void ads7846_disable(struct ads7846 *ts);
212
Imre Deakc9e617a2006-04-11 23:44:05 -0400213static int device_suspended(struct device *dev)
214{
215 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500216 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400217}
218
David Brownellffa458c2006-01-08 13:34:21 -0800219static int ads7846_read12_ser(struct device *dev, unsigned command)
220{
221 struct spi_device *spi = to_spi_device(dev);
222 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800223 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800224 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500225 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800226
227 if (!req)
228 return -ENOMEM;
229
Imre Deak0b7018a2006-04-11 23:42:03 -0400230 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800231
David Brownell2c8dc072007-01-18 00:45:48 -0500232 /* FIXME boards with ads7846 might use external vref instead ... */
233 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800234
David Brownell2c8dc072007-01-18 00:45:48 -0500235 /* maybe turn on internal vREF, and let it settle */
236 if (use_internal) {
237 req->ref_on = REF_ON;
238 req->xfer[0].tx_buf = &req->ref_on;
239 req->xfer[0].len = 1;
240 spi_message_add_tail(&req->xfer[0], &req->msg);
241
242 req->xfer[1].rx_buf = &req->scratch;
243 req->xfer[1].len = 2;
244
245 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
246 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
247 spi_message_add_tail(&req->xfer[1], &req->msg);
248 }
David Brownellffa458c2006-01-08 13:34:21 -0800249
250 /* take sample */
251 req->command = (u8) command;
252 req->xfer[2].tx_buf = &req->command;
253 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500254 spi_message_add_tail(&req->xfer[2], &req->msg);
255
David Brownellffa458c2006-01-08 13:34:21 -0800256 req->xfer[3].rx_buf = &req->sample;
257 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500258 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800259
260 /* REVISIT: take a few more samples, and compare ... */
261
Nicolas Ferre969111e2007-02-28 23:51:03 -0500262 /* converter in low power mode & enable PENIRQ */
263 req->ref_off = PWRDOWN;
264 req->xfer[4].tx_buf = &req->ref_off;
265 req->xfer[4].len = 1;
266 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800267
Nicolas Ferre969111e2007-02-28 23:51:03 -0500268 req->xfer[5].rx_buf = &req->scratch;
269 req->xfer[5].len = 2;
270 CS_CHANGE(req->xfer[5]);
271 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800272
Imre Deakc9e617a2006-04-11 23:44:05 -0400273 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800274 disable_irq(spi->irq);
275 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400276 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800277 enable_irq(spi->irq);
278
Marc Pignatc24b2602007-12-04 23:45:11 -0800279 if (status == 0) {
280 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400281 status = be16_to_cpu(req->sample);
282 status = status >> 3;
283 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800284 }
David Brownell90845332006-05-25 18:44:20 -0700285
286 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400287 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800288}
289
David Brownell2c8dc072007-01-18 00:45:48 -0500290#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
291
292#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800293name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
294{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500295 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800296 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500297 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800298 if (v < 0) \
299 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500300 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800301} \
302static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
303
David Brownell2c8dc072007-01-18 00:45:48 -0500304
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400305/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500306 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
307 * accuracy scheme without calibration data. For now we won't try either;
308 * userspace sees raw sensor values, and must scale/calibrate appropriately.
309 */
310static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
311{
312 return v;
313}
314
315SHOW(temp0, temp0, null_adjust) /* temp1_input */
316SHOW(temp1, temp1, null_adjust) /* temp2_input */
317
318
319/* sysfs conventions report voltages in millivolts. We can convert voltages
320 * if we know vREF. userspace may need to scale vAUX to match the board's
321 * external resistors; we assume that vBATT only uses the internal ones.
322 */
323static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
324{
325 unsigned retval = v;
326
327 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400328 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500329 retval = retval >> 12;
330 return retval;
331}
332
333static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
334{
335 unsigned retval = vaux_adjust(ts, v);
336
337 /* ads7846 has a resistor ladder to scale this signal down */
338 if (ts->model == 7846)
339 retval *= 4;
340 return retval;
341}
342
343SHOW(in0_input, vaux, vaux_adjust)
344SHOW(in1_input, vbatt, vbatt_adjust)
345
346
347static struct attribute *ads7846_attributes[] = {
348 &dev_attr_temp0.attr,
349 &dev_attr_temp1.attr,
350 &dev_attr_in0_input.attr,
351 &dev_attr_in1_input.attr,
352 NULL,
353};
354
355static struct attribute_group ads7846_attr_group = {
356 .attrs = ads7846_attributes,
357};
358
359static struct attribute *ads7843_attributes[] = {
360 &dev_attr_in0_input.attr,
361 &dev_attr_in1_input.attr,
362 NULL,
363};
364
365static struct attribute_group ads7843_attr_group = {
366 .attrs = ads7843_attributes,
367};
368
369static struct attribute *ads7845_attributes[] = {
370 &dev_attr_in0_input.attr,
371 NULL,
372};
373
374static struct attribute_group ads7845_attr_group = {
375 .attrs = ads7845_attributes,
376};
377
378static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
379{
Tony Jones1beeffe2007-08-20 13:46:20 -0700380 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500381 int err;
382
383 /* hwmon sensors need a reference voltage */
384 switch (ts->model) {
385 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400386 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500387 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400388 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500389 }
390 break;
391 case 7845:
392 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400393 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500394 dev_warn(&spi->dev,
395 "external vREF for ADS%d not specified\n",
396 ts->model);
397 return 0;
398 }
399 break;
400 }
401
402 /* different chips have different sensor groups */
403 switch (ts->model) {
404 case 7846:
405 ts->attr_group = &ads7846_attr_group;
406 break;
407 case 7845:
408 ts->attr_group = &ads7845_attr_group;
409 break;
410 case 7843:
411 ts->attr_group = &ads7843_attr_group;
412 break;
413 default:
414 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
415 return 0;
416 }
417
418 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
419 if (err)
420 return err;
421
422 hwmon = hwmon_device_register(&spi->dev);
423 if (IS_ERR(hwmon)) {
424 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
425 return PTR_ERR(hwmon);
426 }
427
428 ts->hwmon = hwmon;
429 return 0;
430}
431
432static void ads784x_hwmon_unregister(struct spi_device *spi,
433 struct ads7846 *ts)
434{
435 if (ts->hwmon) {
436 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
437 hwmon_device_unregister(ts->hwmon);
438 }
439}
440
441#else
442static inline int ads784x_hwmon_register(struct spi_device *spi,
443 struct ads7846 *ts)
444{
445 return 0;
446}
447
448static inline void ads784x_hwmon_unregister(struct spi_device *spi,
449 struct ads7846 *ts)
450{
451}
452#endif
David Brownellffa458c2006-01-08 13:34:21 -0800453
Imre Deak438f2a72006-04-11 23:41:32 -0400454static int is_pen_down(struct device *dev)
455{
David Brownell2c8dc072007-01-18 00:45:48 -0500456 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400457
458 return ts->pendown;
459}
460
461static ssize_t ads7846_pen_down_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
464 return sprintf(buf, "%u\n", is_pen_down(dev));
465}
466
467static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
468
Imre Deak7de90a82006-04-11 23:43:55 -0400469static ssize_t ads7846_disable_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
472 struct ads7846 *ts = dev_get_drvdata(dev);
473
474 return sprintf(buf, "%u\n", ts->disabled);
475}
476
477static ssize_t ads7846_disable_store(struct device *dev,
478 struct device_attribute *attr,
479 const char *buf, size_t count)
480{
481 struct ads7846 *ts = dev_get_drvdata(dev);
Harvey Harrison3a0c58d2008-12-23 04:09:28 -0500482 unsigned long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400483
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400484 if (strict_strtoul(buf, 10, &i))
485 return -EINVAL;
486
Imre Deak7de90a82006-04-11 23:43:55 -0400487 spin_lock_irq(&ts->lock);
488
489 if (i)
490 ads7846_disable(ts);
491 else
492 ads7846_enable(ts);
493
494 spin_unlock_irq(&ts->lock);
495
496 return count;
497}
498
499static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
500
David Brownell2c8dc072007-01-18 00:45:48 -0500501static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500502 &dev_attr_pen_down.attr,
503 &dev_attr_disable.attr,
504 NULL,
505};
506
David Brownell2c8dc072007-01-18 00:45:48 -0500507static struct attribute_group ads784x_attr_group = {
508 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500509};
510
David Brownellffa458c2006-01-08 13:34:21 -0800511/*--------------------------------------------------------------------------*/
512
Eric Miao4d5975e2008-09-10 12:06:15 -0400513static int get_pendown_state(struct ads7846 *ts)
514{
515 if (ts->get_pendown_state)
516 return ts->get_pendown_state();
517
518 return !gpio_get_value(ts->gpio_pendown);
519}
520
Eric Miaofd746d52009-04-11 16:54:59 -0700521static void null_wait_for_sync(void)
522{
523}
524
David Brownellffa458c2006-01-08 13:34:21 -0800525/*
526 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
527 * to retrieve touchscreen status.
528 *
529 * The SPI transfer completion callback does the real work. It reports
530 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
531 */
532
533static void ads7846_rx(void *ads)
534{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500535 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400536 struct ads7846_packet *packet = ts->packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500537 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500538 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800539
Imre Deakda970e62007-01-18 00:44:41 -0500540 /* ads7846_rx_val() did in-place conversion (including byteswap) from
541 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800542 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400543 x = packet->tc.x;
544 y = packet->tc.y;
545 z1 = packet->tc.z1;
546 z2 = packet->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800547
548 /* range filtering */
549 if (x == MAX_12BIT)
550 x = 0;
551
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400552 if (ts->model == 7843) {
553 Rt = ts->pressure_max / 2;
554 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800555 /* compute touch pressure resistance using equation #2 */
556 Rt = z2;
557 Rt -= z1;
558 Rt *= x;
559 Rt *= ts->x_plate_ohms;
560 Rt /= z1;
561 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400562 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800563 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400564 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500565
Imre Deakd5b415c2006-04-26 00:13:18 -0400566 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500567 * the maximum. Don't report it to user space, repeat at least
568 * once more the measurement
569 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400570 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800571 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
572 packet->tc.ignore, Rt);
Imre Deak1936d592007-01-18 00:45:31 -0500573 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800574 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400575 return;
576 }
577
Semih Hazar1d258912007-07-18 00:36:04 -0400578 /* Maybe check the pendown state before reporting. This discards
579 * false readings when the pen is lifted.
580 */
581 if (ts->penirq_recheck_delay_usecs) {
582 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400583 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400584 Rt = 0;
585 }
586
Imre Deak15e35892007-01-18 00:45:43 -0500587 /* NOTE: We can't rely on the pressure to determine the pen down
588 * state, even this controller has a pressure sensor. The pressure
589 * value can fluctuate for quite a while after lifting the pen and
590 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800591 *
Imre Deak15e35892007-01-18 00:45:43 -0500592 * The only safe way to check for the pen up condition is in the
593 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800594 */
David Brownellffa458c2006-01-08 13:34:21 -0800595 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500596 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400597
Imre Deak15e35892007-01-18 00:45:43 -0500598 if (!ts->pendown) {
599 input_report_key(input, BTN_TOUCH, 1);
600 ts->pendown = 1;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800601 dev_vdbg(&ts->spi->dev, "DOWN\n");
Imre Deak15e35892007-01-18 00:45:43 -0500602 }
Michael Roth86579a42009-05-18 16:04:44 -0700603
604 if (ts->swap_xy)
605 swap(x, y);
606
Imre Deak15e35892007-01-18 00:45:43 -0500607 input_report_abs(input, ABS_X, x);
608 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800609 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800610
Imre Deak15e35892007-01-18 00:45:43 -0500611 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800612 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500613 }
David Brownellffa458c2006-01-08 13:34:21 -0800614
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800615 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
616 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800617}
618
Imre Deakda970e62007-01-18 00:44:41 -0500619static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400620{
621 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400622
Imre Deakda970e62007-01-18 00:44:41 -0500623 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
624 /* Start over collecting consistent readings. */
625 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400626 /* Repeat it, if this was the first read or the read
627 * wasn't consistent enough. */
628 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500629 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400630 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500631 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400632 } else {
633 /* Maximum number of debouncing reached and still
634 * not enough number of consistent readings. Abort
635 * the whole sample, repeat it in the next sampling
636 * period.
637 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400638 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500639 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400640 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400641 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400642 if (++ts->read_rep > ts->debounce_rep) {
643 /* Got a good reading for this coordinate,
644 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400645 ts->read_cnt = 0;
646 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500647 return ADS7846_FILTER_OK;
648 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400649 /* Read more values that are consistent. */
650 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500651 return ADS7846_FILTER_REPEAT;
652 }
653 }
654}
655
656static int ads7846_no_filter(void *ads, int data_idx, int *val)
657{
658 return ADS7846_FILTER_OK;
659}
660
661static void ads7846_rx_val(void *ads)
662{
663 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400664 struct ads7846_packet *packet = ts->packet;
Imre Deakda970e62007-01-18 00:44:41 -0500665 struct spi_message *m;
666 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500667 int val;
668 int action;
669 int status;
670
671 m = &ts->msg[ts->msg_idx];
672 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500673
674 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
675 * built from two 8 bit values written msb-first.
676 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400677 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500678
679 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
680 switch (action) {
681 case ADS7846_FILTER_REPEAT:
682 break;
683 case ADS7846_FILTER_IGNORE:
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400684 packet->tc.ignore = 1;
Imre Deakda970e62007-01-18 00:44:41 -0500685 /* Last message will contain ads7846_rx() as the
686 * completion function.
687 */
688 m = ts->last_msg;
689 break;
690 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400691 *(u16 *)t->rx_buf = val;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400692 packet->tc.ignore = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500693 m = &ts->msg[++ts->msg_idx];
694 break;
695 default:
696 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400697 }
Eric Miaofd746d52009-04-11 16:54:59 -0700698 ts->wait_for_sync();
Imre Deak0b7018a2006-04-11 23:42:03 -0400699 status = spi_async(ts->spi, m);
700 if (status)
701 dev_err(&ts->spi->dev, "spi_async --> %d\n",
702 status);
703}
704
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800705static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800706{
Imre Deak1936d592007-01-18 00:45:31 -0500707 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800708 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800709
Peter Zijlstraca109492008-11-25 12:43:51 +0100710 spin_lock(&ts->lock);
Imre Deakc9e617a2006-04-11 23:44:05 -0400711
Eric Miao4d5975e2008-09-10 12:06:15 -0400712 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500713 device_suspended(&ts->spi->dev))) {
714 if (ts->pendown) {
715 struct input_dev *input = ts->input;
716
717 input_report_key(input, BTN_TOUCH, 0);
718 input_report_abs(input, ABS_PRESSURE, 0);
719 input_sync(input);
720
721 ts->pendown = 0;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800722 dev_vdbg(&ts->spi->dev, "UP\n");
Imre Deak15e35892007-01-18 00:45:43 -0500723 }
724
David Brownell90845332006-05-25 18:44:20 -0700725 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400726 if (!device_suspended(&ts->spi->dev)) {
727 ts->irq_disabled = 0;
728 enable_irq(ts->spi->irq);
729 }
730 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400731 } else {
732 /* pen is still down, continue with the measurement */
733 ts->msg_idx = 0;
Eric Miaofd746d52009-04-11 16:54:59 -0700734 ts->wait_for_sync();
Imre Deakc9e617a2006-04-11 23:44:05 -0400735 status = spi_async(ts->spi, &ts->msg[0]);
736 if (status)
737 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
738 }
739
Peter Zijlstraca109492008-11-25 12:43:51 +0100740 spin_unlock(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500741 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800742}
743
David Howells7d12e782006-10-05 14:55:46 +0100744static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800745{
Imre Deak0b7018a2006-04-11 23:42:03 -0400746 struct ads7846 *ts = handle;
747 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400748
749 spin_lock_irqsave(&ts->lock, flags);
Eric Miao4d5975e2008-09-10 12:06:15 -0400750 if (likely(get_pendown_state(ts))) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400751 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700752 /* The ARM do_simple_IRQ() dispatcher doesn't act
753 * like the other dispatchers: it will report IRQs
754 * even after they've been disabled. We work around
755 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400756 */
757 ts->irq_disabled = 1;
Ben Nizette3f3e7c62009-04-15 18:57:55 -0700758 disable_irq_nosync(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400759 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500760 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800761 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400762 }
763 }
764 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400765
766 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800767}
768
769/*--------------------------------------------------------------------------*/
770
Imre Deak7de90a82006-04-11 23:43:55 -0400771/* Must be called with ts->lock held */
772static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800773{
Imre Deak7de90a82006-04-11 23:43:55 -0400774 if (ts->disabled)
775 return;
David Brownellffa458c2006-01-08 13:34:21 -0800776
Imre Deakc9e617a2006-04-11 23:44:05 -0400777 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800778
Imre Deakc9e617a2006-04-11 23:44:05 -0400779 /* are we waiting for IRQ, or polling? */
780 if (!ts->pending) {
781 ts->irq_disabled = 1;
782 disable_irq(ts->spi->irq);
783 } else {
784 /* the timer will run at least once more, and
785 * leave everything in a clean state, IRQ disabled
786 */
787 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400788 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400789 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400790 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800791 }
792 }
793
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800794 regulator_disable(ts->reg);
795
David Brownellffa458c2006-01-08 13:34:21 -0800796 /* we know the chip's in lowpower mode since we always
797 * leave it that way after every request
798 */
Imre Deak7de90a82006-04-11 23:43:55 -0400799}
800
801/* Must be called with ts->lock held */
802static void ads7846_enable(struct ads7846 *ts)
803{
804 if (!ts->disabled)
805 return;
806
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800807 regulator_enable(ts->reg);
808
Imre Deak7de90a82006-04-11 23:43:55 -0400809 ts->disabled = 0;
810 ts->irq_disabled = 0;
811 enable_irq(ts->spi->irq);
812}
813
814static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
815{
816 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
817
818 spin_lock_irq(&ts->lock);
819
David Brownellfbb38e32007-12-14 01:26:33 -0500820 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400821 ads7846_disable(ts);
822
823 spin_unlock_irq(&ts->lock);
824
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800825 if (device_may_wakeup(&ts->spi->dev))
826 enable_irq_wake(ts->spi->irq);
827
David Brownellffa458c2006-01-08 13:34:21 -0800828 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400829
David Brownellffa458c2006-01-08 13:34:21 -0800830}
831
David Brownell2e5a7bd2006-01-08 13:34:25 -0800832static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800833{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800834 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800835
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800836 if (device_may_wakeup(&ts->spi->dev))
837 disable_irq_wake(ts->spi->irq);
838
Imre Deak7de90a82006-04-11 23:43:55 -0400839 spin_lock_irq(&ts->lock);
840
David Brownellfbb38e32007-12-14 01:26:33 -0500841 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400842 ads7846_enable(ts);
843
844 spin_unlock_irq(&ts->lock);
845
David Brownellffa458c2006-01-08 13:34:21 -0800846 return 0;
847}
848
Eric Miao4d5975e2008-09-10 12:06:15 -0400849static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
850{
851 struct ads7846_platform_data *pdata = spi->dev.platform_data;
852 int err;
853
854 /* REVISIT when the irq can be triggered active-low, or if for some
855 * reason the touchscreen isn't hooked up, we don't need to access
856 * the pendown state.
857 */
858 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
859 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
860 return -EINVAL;
861 }
862
863 if (pdata->get_pendown_state) {
864 ts->get_pendown_state = pdata->get_pendown_state;
865 return 0;
866 }
867
868 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
869 if (err) {
870 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
871 pdata->gpio_pendown);
872 return err;
873 }
874
875 ts->gpio_pendown = pdata->gpio_pendown;
876 return 0;
877}
878
David Brownell2e5a7bd2006-01-08 13:34:25 -0800879static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800880{
David Brownellffa458c2006-01-08 13:34:21 -0800881 struct ads7846 *ts;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400882 struct ads7846_packet *packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500883 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800884 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400885 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800886 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500887 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500888 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800889
890 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800891 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800892 return -ENODEV;
893 }
894
895 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800896 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800897 return -ENODEV;
898 }
899
900 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500901 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800902 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500903 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800904 return -EINVAL;
905 }
906
David Brownell90845332006-05-25 18:44:20 -0700907 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
908 * that even if the hardware can do that, the SPI controller driver
909 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800910 */
David Brownell90845332006-05-25 18:44:20 -0700911 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400912 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500913 err = spi_setup(spi);
914 if (err < 0)
915 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800916
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500917 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400918 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500919 input_dev = input_allocate_device();
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400920 if (!ts || !packet || !input_dev) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500921 err = -ENOMEM;
922 goto err_free_mem;
923 }
David Brownellffa458c2006-01-08 13:34:21 -0800924
David Brownell2e5a7bd2006-01-08 13:34:25 -0800925 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800926
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400927 ts->packet = packet;
David Brownellffa458c2006-01-08 13:34:21 -0800928 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500929 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400930 ts->vref_mv = pdata->vref_mv;
Michael Roth86579a42009-05-18 16:04:44 -0700931 ts->swap_xy = pdata->swap_xy;
David Brownellffa458c2006-01-08 13:34:21 -0800932
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800933 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800934 ts->timer.function = ads7846_timer;
935
Imre Deak7de90a82006-04-11 23:43:55 -0400936 spin_lock_init(&ts->lock);
937
David Brownellffa458c2006-01-08 13:34:21 -0800938 ts->model = pdata->model ? : 7846;
939 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
940 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400941 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500942
943 if (pdata->filter != NULL) {
944 if (pdata->filter_init != NULL) {
945 err = pdata->filter_init(pdata, &ts->filter_data);
946 if (err < 0)
947 goto err_free_mem;
948 }
949 ts->filter = pdata->filter;
950 ts->filter_cleanup = pdata->filter_cleanup;
951 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400952 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500953 if (ts->debounce_max < 2)
954 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400955 ts->debounce_tol = pdata->debounce_tol;
956 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500957 ts->filter = ads7846_debounce;
958 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400959 } else
Imre Deakda970e62007-01-18 00:44:41 -0500960 ts->filter = ads7846_no_filter;
Eric Miao4d5975e2008-09-10 12:06:15 -0400961
962 err = setup_pendown(spi, ts);
963 if (err)
964 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800965
Semih Hazar1d258912007-07-18 00:36:04 -0400966 if (pdata->penirq_recheck_delay_usecs)
967 ts->penirq_recheck_delay_usecs =
968 pdata->penirq_recheck_delay_usecs;
969
Eric Miaofd746d52009-04-11 16:54:59 -0700970 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
971
Kay Sieversa6c24902008-10-30 00:07:50 -0400972 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
Michael Rothb58895f2009-05-18 16:05:12 -0700973 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
David Brownellffa458c2006-01-08 13:34:21 -0800974
Michael Rothb58895f2009-05-18 16:05:12 -0700975 input_dev->name = ts->name;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500976 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400977 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800978
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700979 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
980 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500981 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800982 pdata->x_min ? : 0,
983 pdata->x_max ? : MAX_12BIT,
984 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500985 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800986 pdata->y_min ? : 0,
987 pdata->y_max ? : MAX_12BIT,
988 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500989 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800990 pdata->pressure_min, pdata->pressure_max, 0, 0);
991
Imre Deakde2defd2007-01-18 00:45:21 -0500992 vref = pdata->keep_vref_on;
993
Michael Hennerich06a09122010-03-09 20:38:45 -0800994 if (ts->model == 7873) {
995 /* The AD7873 is almost identical to the ADS7846
996 * keep VREF off during differential/ratiometric
997 * conversion modes
998 */
999 ts->model = 7846;
1000 vref = 0;
1001 }
1002
David Brownellffa458c2006-01-08 13:34:21 -08001003 /* set up the transfers to read touchscreen state; this assumes we
1004 * use formula #2 for pressure, not #3.
1005 */
Imre Deak0b7018a2006-04-11 23:42:03 -04001006 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -08001007 x = ts->xfer;
1008
Imre Deak0b7018a2006-04-11 23:42:03 -04001009 spi_message_init(m);
1010
David Brownellffa458c2006-01-08 13:34:21 -08001011 /* y- still on; turn on only y+ (and ADC) */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001012 packet->read_y = READ_Y(vref);
1013 x->tx_buf = &packet->read_y;
David Brownellffa458c2006-01-08 13:34:21 -08001014 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001015 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001016
David Brownellffa458c2006-01-08 13:34:21 -08001017 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001018 x->rx_buf = &packet->tc.y;
David Brownellffa458c2006-01-08 13:34:21 -08001019 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001020 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001021
Semih Hazare4f48862007-07-18 00:35:56 -04001022 /* the first sample after switching drivers can be low quality;
1023 * optionally discard it, using a second one after the signals
1024 * have had enough time to stabilize.
1025 */
1026 if (pdata->settle_delay_usecs) {
1027 x->delay_usecs = pdata->settle_delay_usecs;
1028
1029 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001030 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001031 x->len = 1;
1032 spi_message_add_tail(x, m);
1033
1034 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001035 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001036 x->len = 2;
1037 spi_message_add_tail(x, m);
1038 }
1039
Imre Deakda970e62007-01-18 00:44:41 -05001040 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001041 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -05001042
Imre Deak0b7018a2006-04-11 23:42:03 -04001043 m++;
1044 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -08001045
1046 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -05001047 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001048 packet->read_x = READ_X(vref);
1049 x->tx_buf = &packet->read_x;
David Brownellffa458c2006-01-08 13:34:21 -08001050 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001051 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001052
David Brownellffa458c2006-01-08 13:34:21 -08001053 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001054 x->rx_buf = &packet->tc.x;
David Brownellffa458c2006-01-08 13:34:21 -08001055 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001056 spi_message_add_tail(x, m);
1057
Semih Hazare4f48862007-07-18 00:35:56 -04001058 /* ... maybe discard first sample ... */
1059 if (pdata->settle_delay_usecs) {
1060 x->delay_usecs = pdata->settle_delay_usecs;
1061
1062 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001063 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001064 x->len = 1;
1065 spi_message_add_tail(x, m);
1066
1067 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001068 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001069 x->len = 2;
1070 spi_message_add_tail(x, m);
1071 }
1072
Imre Deakda970e62007-01-18 00:44:41 -05001073 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001074 m->context = ts;
1075
1076 /* turn y+ off, x- on; we'll use formula #2 */
1077 if (ts->model == 7846) {
1078 m++;
1079 spi_message_init(m);
1080
1081 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001082 packet->read_z1 = READ_Z1(vref);
1083 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001084 x->len = 1;
1085 spi_message_add_tail(x, m);
1086
1087 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001088 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001089 x->len = 2;
1090 spi_message_add_tail(x, m);
1091
Semih Hazare4f48862007-07-18 00:35:56 -04001092 /* ... maybe discard first sample ... */
1093 if (pdata->settle_delay_usecs) {
1094 x->delay_usecs = pdata->settle_delay_usecs;
1095
1096 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001097 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001098 x->len = 1;
1099 spi_message_add_tail(x, m);
1100
1101 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001102 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001103 x->len = 2;
1104 spi_message_add_tail(x, m);
1105 }
1106
Imre Deakda970e62007-01-18 00:44:41 -05001107 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001108 m->context = ts;
1109
1110 m++;
1111 spi_message_init(m);
1112
1113 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001114 packet->read_z2 = READ_Z2(vref);
1115 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001116 x->len = 1;
1117 spi_message_add_tail(x, m);
1118
1119 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001120 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001121 x->len = 2;
1122 spi_message_add_tail(x, m);
1123
Semih Hazare4f48862007-07-18 00:35:56 -04001124 /* ... maybe discard first sample ... */
1125 if (pdata->settle_delay_usecs) {
1126 x->delay_usecs = pdata->settle_delay_usecs;
1127
1128 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001129 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001130 x->len = 1;
1131 spi_message_add_tail(x, m);
1132
1133 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001134 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001135 x->len = 2;
1136 spi_message_add_tail(x, m);
1137 }
1138
Imre Deakda970e62007-01-18 00:44:41 -05001139 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001140 m->context = ts;
1141 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001142
1143 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001144 m++;
1145 spi_message_init(m);
1146
Imre Deak53a0ef892006-04-11 23:41:49 -04001147 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001148 packet->pwrdown = PWRDOWN;
1149 x->tx_buf = &packet->pwrdown;
Imre Deak53a0ef892006-04-11 23:41:49 -04001150 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001151 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001152
1153 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001154 x->rx_buf = &packet->dummy;
Imre Deak53a0ef892006-04-11 23:41:49 -04001155 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001156 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001157 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001158
Imre Deak0b7018a2006-04-11 23:42:03 -04001159 m->complete = ads7846_rx;
1160 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001161
Imre Deakd5b415c2006-04-26 00:13:18 -04001162 ts->last_msg = m;
1163
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001164 ts->reg = regulator_get(&spi->dev, "vcc");
1165 if (IS_ERR(ts->reg)) {
Kevin Hilman067fb2f2010-05-26 23:30:55 -07001166 err = PTR_ERR(ts->reg);
Dmitry Torokhov56960b32010-06-02 00:40:06 -07001167 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001168 goto err_free_gpio;
1169 }
1170
1171 err = regulator_enable(ts->reg);
1172 if (err) {
1173 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1174 goto err_put_regulator;
1175 }
1176
Anatolij Gustschin78043022010-06-28 01:25:19 -07001177 if (!pdata->irq_flags)
1178 pdata->irq_flags = IRQF_TRIGGER_FALLING;
1179
1180 if (request_irq(spi->irq, ads7846_irq, pdata->irq_flags,
David Brownell90845332006-05-25 18:44:20 -07001181 spi->dev.driver->name, ts)) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001182 dev_info(&spi->dev,
1183 "trying pin change workaround on irq %d\n", spi->irq);
1184 err = request_irq(spi->irq, ads7846_irq,
1185 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1186 spi->dev.driver->name, ts);
1187 if (err) {
1188 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001189 goto err_disable_regulator;
Michael Rothc57c0a22009-06-10 23:30:55 -07001190 }
David Brownellffa458c2006-01-08 13:34:21 -08001191 }
David Brownellffa458c2006-01-08 13:34:21 -08001192
David Brownell2c8dc072007-01-18 00:45:48 -05001193 err = ads784x_hwmon_register(spi, ts);
1194 if (err)
1195 goto err_free_irq;
1196
David Brownell2e5a7bd2006-01-08 13:34:25 -08001197 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001198
David Brownell2c8dc072007-01-18 00:45:48 -05001199 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001200 * the touchscreen, in case it's not connected.
1201 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001202 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001203 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1204
David Brownell2c8dc072007-01-18 00:45:48 -05001205 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001206 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001207 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001208
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001209 err = input_register_device(input_dev);
1210 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001211 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001212
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001213 device_init_wakeup(&spi->dev, pdata->wakeup);
1214
David Brownellffa458c2006-01-08 13:34:21 -08001215 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001216
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001217 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001218 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1219 err_remove_hwmon:
1220 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001221 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001222 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001223 err_disable_regulator:
1224 regulator_disable(ts->reg);
1225 err_put_regulator:
1226 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001227 err_free_gpio:
1228 if (ts->gpio_pendown != -1)
1229 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001230 err_cleanup_filter:
1231 if (ts->filter_cleanup)
1232 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001233 err_free_mem:
1234 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001235 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001236 kfree(ts);
1237 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001238}
1239
David Brownell2e5a7bd2006-01-08 13:34:25 -08001240static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001241{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001242 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001243
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001244 device_init_wakeup(&spi->dev, false);
1245
David Brownell2c8dc072007-01-18 00:45:48 -05001246 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001247 input_unregister_device(ts->input);
1248
David Brownell2e5a7bd2006-01-08 13:34:25 -08001249 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001250
David Brownell2c8dc072007-01-18 00:45:48 -05001251 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001252
Imre Deak7de90a82006-04-11 23:43:55 -04001253 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001254 /* suspend left the IRQ disabled */
1255 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001256
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001257 regulator_disable(ts->reg);
1258 regulator_put(ts->reg);
1259
Eric Miao4d5975e2008-09-10 12:06:15 -04001260 if (ts->gpio_pendown != -1)
1261 gpio_free(ts->gpio_pendown);
1262
Imre Deakda970e62007-01-18 00:44:41 -05001263 if (ts->filter_cleanup)
1264 ts->filter_cleanup(ts->filter_data);
1265
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001266 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001267 kfree(ts);
1268
David Brownell2e5a7bd2006-01-08 13:34:25 -08001269 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001270 return 0;
1271}
1272
David Brownell2e5a7bd2006-01-08 13:34:25 -08001273static struct spi_driver ads7846_driver = {
1274 .driver = {
1275 .name = "ads7846",
1276 .bus = &spi_bus_type,
1277 .owner = THIS_MODULE,
1278 },
David Brownellffa458c2006-01-08 13:34:21 -08001279 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001280 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001281 .suspend = ads7846_suspend,
1282 .resume = ads7846_resume,
1283};
1284
1285static int __init ads7846_init(void)
1286{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001287 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001288}
1289module_init(ads7846_init);
1290
1291static void __exit ads7846_exit(void)
1292{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001293 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001294}
1295module_exit(ads7846_exit);
1296
1297MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1298MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001299MODULE_ALIAS("spi:ads7846");