blob: de31ec6fe9e47005ba94cc9eae222f922bb9c4b9 [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 */
Jason Wang2991a1c2010-10-13 11:35:40 -070020#include <linux/types.h>
David Brownell2c8dc072007-01-18 00:45:48 -050021#include <linux/hwmon.h>
David Brownellffa458c2006-01-08 13:34:21 -080022#include <linux/init.h>
David Brownell2c8dc072007-01-18 00:45:48 -050023#include <linux/err.h>
Jason Wang2991a1c2010-10-13 11:35:40 -070024#include <linux/sched.h>
David Brownellffa458c2006-01-08 13:34:21 -080025#include <linux/delay.h>
26#include <linux/input.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
Mark Brown3c36e712011-01-20 22:51:26 -080029#include <linux/pm.h>
Eric Miao4d5975e2008-09-10 12:06:15 -040030#include <linux/gpio.h>
David Brownellffa458c2006-01-08 13:34:21 -080031#include <linux/spi/spi.h>
32#include <linux/spi/ads7846.h>
Grazvydas Ignotas91143372010-02-25 02:04:56 -080033#include <linux/regulator/consumer.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040034#include <linux/module.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080035#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080036
David Brownellffa458c2006-01-08 13:34:21 -080037/*
David Brownell90845332006-05-25 18:44:20 -070038 * This code has been heavily tested on a Nokia 770, and lightly
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080039 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
David Brownellbff0de52007-05-22 23:28:40 -040040 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050041 * Support for ads7843 tested on Atmel at91sam926x-EK.
42 * Support for ads7845 has only been stubbed in.
Michael Hennerich06a09122010-03-09 20:38:45 -080043 * Support for Analog Devices AD7873 and AD7843 tested.
David Brownellffa458c2006-01-08 13:34:21 -080044 *
Imre Deak7de90a82006-04-11 23:43:55 -040045 * IRQ handling needs a workaround because of a shortcoming in handling
46 * edge triggered IRQs on some platforms like the OMAP1/2. These
47 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
48 * have to maintain our own SW IRQ disabled status. This should be
49 * removed as soon as the affected platform's IRQ handling is fixed.
50 *
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080051 * App note sbaa036 talks in more detail about accurate sampling...
David Brownellffa458c2006-01-08 13:34:21 -080052 * that ought to help in situations like LCDs inducing noise (which
53 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040054 * This driver tries to utilize the measures described in the app
55 * note. The strength of filtering can be set in the board-* specific
56 * files.
David Brownellffa458c2006-01-08 13:34:21 -080057 */
58
Jason Wang2991a1c2010-10-13 11:35:40 -070059#define TS_POLL_DELAY 1 /* ms delay before the first sample */
60#define TS_POLL_PERIOD 5 /* ms delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080061
David Brownelld93f70b2006-02-15 00:49:35 -050062/* this driver doesn't aim at the peak continuous sample rate */
63#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
64
David Brownellffa458c2006-01-08 13:34:21 -080065struct ts_event {
Jason Wang2991a1c2010-10-13 11:35:40 -070066 /*
67 * For portability, we can't read 12 bit values using SPI (which
68 * would make the controller deliver them as native byte order u16
David Brownelld93f70b2006-02-15 00:49:35 -050069 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050070 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080071 */
Imre Deakda970e62007-01-18 00:44:41 -050072 u16 x;
73 u16 y;
74 u16 z1, z2;
Jason Wang2991a1c2010-10-13 11:35:40 -070075 bool ignore;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070076 u8 x_buf[3];
77 u8 y_buf[3];
David Brownellffa458c2006-01-08 13:34:21 -080078};
79
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040080/*
81 * We allocate this separately to avoid cache line sharing issues when
82 * driver is used with DMA-based SPI controllers (like atmel_spi) on
83 * systems where main memory is not DMA-coherent (most non-x86 boards).
84 */
85struct ads7846_packet {
86 u8 read_x, read_y, read_z1, read_z2, pwrdown;
87 u16 dummy; /* for the pwrdown read */
88 struct ts_event tc;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070089 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
90 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040091};
92
David Brownellffa458c2006-01-08 13:34:21 -080093struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050094 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080095 char phys[32];
Michael Rothb58895f2009-05-18 16:05:12 -070096 char name[32];
David Brownellffa458c2006-01-08 13:34:21 -080097
98 struct spi_device *spi;
Grazvydas Ignotas91143372010-02-25 02:04:56 -080099 struct regulator *reg;
David Brownell2c8dc072007-01-18 00:45:48 -0500100
101#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500102 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -0700103 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500104#endif
105
David Brownellffa458c2006-01-08 13:34:21 -0800106 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400107 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800108 u16 vref_delay_usecs;
109 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400110 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800111
Michael Roth86579a42009-05-18 16:04:44 -0700112 bool swap_xy;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700113 bool use_internal;
Michael Roth86579a42009-05-18 16:04:44 -0700114
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400115 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800116
Semih Hazare4f48862007-07-18 00:35:56 -0400117 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400118 struct spi_message msg[5];
Jason Wang2991a1c2010-10-13 11:35:40 -0700119 int msg_count;
120 wait_queue_head_t wait;
121
122 bool pendown;
123
Imre Deak0b7018a2006-04-11 23:42:03 -0400124 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400125 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400126 int last_read;
127
128 u16 debounce_max;
129 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400130 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800131
Semih Hazar1d258912007-07-18 00:36:04 -0400132 u16 penirq_recheck_delay_usecs;
133
Jason Wang2991a1c2010-10-13 11:35:40 -0700134 struct mutex lock;
135 bool stopped; /* P: lock */
136 bool disabled; /* P: lock */
137 bool suspended; /* P: lock */
Imre Deakc9e617a2006-04-11 23:44:05 -0400138
Imre Deakda970e62007-01-18 00:44:41 -0500139 int (*filter)(void *data, int data_idx, int *val);
140 void *filter_data;
141 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400142 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400143 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700144
145 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800146};
147
148/* leave chip selected when we're done, for quicker re-select? */
149#if 0
150#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
151#else
152#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
153#endif
154
155/*--------------------------------------------------------------------------*/
156
157/* The ADS7846 has touchscreen and other sensors.
158 * Earlier ads784x chips are somewhat compatible.
159 */
160#define ADS_START (1 << 7)
161#define ADS_A2A1A0_d_y (1 << 4) /* differential */
162#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
163#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
164#define ADS_A2A1A0_d_x (5 << 4) /* differential */
165#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
166#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
167#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
168#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
169#define ADS_8_BIT (1 << 3)
170#define ADS_12_BIT (0 << 3)
171#define ADS_SER (1 << 2) /* non-differential */
172#define ADS_DFR (0 << 2) /* differential */
Jason Wang2991a1c2010-10-13 11:35:40 -0700173#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
David Brownellffa458c2006-01-08 13:34:21 -0800174#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
175#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
176#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
177
178#define MAX_12BIT ((1<<12)-1)
179
180/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500181#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
182 | ADS_12_BIT | ADS_DFR | \
183 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800184
Imre Deakde2defd2007-01-18 00:45:21 -0500185#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
186#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
187#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400188
Imre Deakde2defd2007-01-18 00:45:21 -0500189#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
190#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800191
192/* single-ended samples need to first power up reference voltage;
193 * we leave both ADC and VREF powered
194 */
195#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
196 | ADS_12_BIT | ADS_SER)
197
Imre Deakde2defd2007-01-18 00:45:21 -0500198#define REF_ON (READ_12BIT_DFR(x, 1, 1))
199#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800200
Jason Wang2991a1c2010-10-13 11:35:40 -0700201/* Must be called with ts->lock held */
202static void ads7846_stop(struct ads7846 *ts)
203{
204 if (!ts->disabled && !ts->suspended) {
205 /* Signal IRQ thread to stop polling and disable the handler. */
206 ts->stopped = true;
207 mb();
208 wake_up(&ts->wait);
209 disable_irq(ts->spi->irq);
210 }
211}
212
213/* Must be called with ts->lock held */
214static void ads7846_restart(struct ads7846 *ts)
215{
216 if (!ts->disabled && !ts->suspended) {
217 /* Tell IRQ thread that it may poll the device. */
218 ts->stopped = false;
219 mb();
220 enable_irq(ts->spi->irq);
221 }
222}
223
224/* Must be called with ts->lock held */
225static void __ads7846_disable(struct ads7846 *ts)
226{
227 ads7846_stop(ts);
228 regulator_disable(ts->reg);
229
230 /*
231 * We know the chip's in low power mode since we always
232 * leave it that way after every request
233 */
234}
235
236/* Must be called with ts->lock held */
237static void __ads7846_enable(struct ads7846 *ts)
238{
239 regulator_enable(ts->reg);
240 ads7846_restart(ts);
241}
242
243static void ads7846_disable(struct ads7846 *ts)
244{
245 mutex_lock(&ts->lock);
246
247 if (!ts->disabled) {
248
249 if (!ts->suspended)
250 __ads7846_disable(ts);
251
252 ts->disabled = true;
253 }
254
255 mutex_unlock(&ts->lock);
256}
257
258static void ads7846_enable(struct ads7846 *ts)
259{
260 mutex_lock(&ts->lock);
261
262 if (ts->disabled) {
263
264 ts->disabled = false;
265
266 if (!ts->suspended)
267 __ads7846_enable(ts);
268 }
269
270 mutex_unlock(&ts->lock);
271}
272
David Brownellffa458c2006-01-08 13:34:21 -0800273/*--------------------------------------------------------------------------*/
274
275/*
276 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500277 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
278 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800279 */
280
281struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500282 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800283 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500284 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800285 u16 scratch;
David Brownellffa458c2006-01-08 13:34:21 -0800286 struct spi_message msg;
287 struct spi_transfer xfer[6];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700288 /*
289 * DMA (thus cache coherency maintenance) requires the
290 * transfer buffers to live in their own cache lines.
291 */
292 __be16 sample ____cacheline_aligned;
David Brownellffa458c2006-01-08 13:34:21 -0800293};
294
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700295struct ads7845_ser_req {
296 u8 command[3];
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700297 struct spi_message msg;
298 struct spi_transfer xfer[2];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700299 /*
300 * DMA (thus cache coherency maintenance) requires the
301 * transfer buffers to live in their own cache lines.
302 */
303 u8 sample[3] ____cacheline_aligned;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700304};
305
David Brownellffa458c2006-01-08 13:34:21 -0800306static int ads7846_read12_ser(struct device *dev, unsigned command)
307{
Jason Wang2991a1c2010-10-13 11:35:40 -0700308 struct spi_device *spi = to_spi_device(dev);
309 struct ads7846 *ts = dev_get_drvdata(dev);
310 struct ser_req *req;
311 int status;
David Brownellffa458c2006-01-08 13:34:21 -0800312
Jason Wang2991a1c2010-10-13 11:35:40 -0700313 req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800314 if (!req)
315 return -ENOMEM;
316
Imre Deak0b7018a2006-04-11 23:42:03 -0400317 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800318
David Brownell2c8dc072007-01-18 00:45:48 -0500319 /* maybe turn on internal vREF, and let it settle */
Alexander Steinebcaaad2011-05-11 16:24:08 -0700320 if (ts->use_internal) {
David Brownell2c8dc072007-01-18 00:45:48 -0500321 req->ref_on = REF_ON;
322 req->xfer[0].tx_buf = &req->ref_on;
323 req->xfer[0].len = 1;
324 spi_message_add_tail(&req->xfer[0], &req->msg);
325
326 req->xfer[1].rx_buf = &req->scratch;
327 req->xfer[1].len = 2;
328
329 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
330 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
331 spi_message_add_tail(&req->xfer[1], &req->msg);
Alexander Steinebcaaad2011-05-11 16:24:08 -0700332
333 /* Enable reference voltage */
334 command |= ADS_PD10_REF_ON;
David Brownell2c8dc072007-01-18 00:45:48 -0500335 }
David Brownellffa458c2006-01-08 13:34:21 -0800336
Alexander Steinebcaaad2011-05-11 16:24:08 -0700337 /* Enable ADC in every case */
338 command |= ADS_PD10_ADC_ON;
339
David Brownellffa458c2006-01-08 13:34:21 -0800340 /* take sample */
341 req->command = (u8) command;
342 req->xfer[2].tx_buf = &req->command;
343 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500344 spi_message_add_tail(&req->xfer[2], &req->msg);
345
David Brownellffa458c2006-01-08 13:34:21 -0800346 req->xfer[3].rx_buf = &req->sample;
347 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500348 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800349
350 /* REVISIT: take a few more samples, and compare ... */
351
Nicolas Ferre969111e2007-02-28 23:51:03 -0500352 /* converter in low power mode & enable PENIRQ */
353 req->ref_off = PWRDOWN;
354 req->xfer[4].tx_buf = &req->ref_off;
355 req->xfer[4].len = 1;
356 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800357
Nicolas Ferre969111e2007-02-28 23:51:03 -0500358 req->xfer[5].rx_buf = &req->scratch;
359 req->xfer[5].len = 2;
360 CS_CHANGE(req->xfer[5]);
361 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800362
Jason Wang2991a1c2010-10-13 11:35:40 -0700363 mutex_lock(&ts->lock);
364 ads7846_stop(ts);
David Brownellffa458c2006-01-08 13:34:21 -0800365 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700366 ads7846_restart(ts);
367 mutex_unlock(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800368
Marc Pignatc24b2602007-12-04 23:45:11 -0800369 if (status == 0) {
370 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400371 status = be16_to_cpu(req->sample);
372 status = status >> 3;
373 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800374 }
David Brownell90845332006-05-25 18:44:20 -0700375
376 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400377 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800378}
379
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700380static int ads7845_read12_ser(struct device *dev, unsigned command)
381{
Jason Wang2991a1c2010-10-13 11:35:40 -0700382 struct spi_device *spi = to_spi_device(dev);
383 struct ads7846 *ts = dev_get_drvdata(dev);
384 struct ads7845_ser_req *req;
385 int status;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700386
Jason Wang2991a1c2010-10-13 11:35:40 -0700387 req = kzalloc(sizeof *req, GFP_KERNEL);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700388 if (!req)
389 return -ENOMEM;
390
391 spi_message_init(&req->msg);
392
393 req->command[0] = (u8) command;
394 req->xfer[0].tx_buf = req->command;
395 req->xfer[0].rx_buf = req->sample;
396 req->xfer[0].len = 3;
397 spi_message_add_tail(&req->xfer[0], &req->msg);
398
Jason Wang2991a1c2010-10-13 11:35:40 -0700399 mutex_lock(&ts->lock);
400 ads7846_stop(ts);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700401 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700402 ads7846_restart(ts);
403 mutex_unlock(&ts->lock);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700404
405 if (status == 0) {
406 /* BE12 value, then padding */
407 status = be16_to_cpu(*((u16 *)&req->sample[1]));
408 status = status >> 3;
409 status &= 0x0fff;
410 }
411
412 kfree(req);
413 return status;
414}
415
David Brownell2c8dc072007-01-18 00:45:48 -0500416#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
417
418#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800419name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
420{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500421 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800422 ssize_t v = ads7846_read12_ser(dev, \
Alexander Steinebcaaad2011-05-11 16:24:08 -0700423 READ_12BIT_SER(var)); \
David Brownellffa458c2006-01-08 13:34:21 -0800424 if (v < 0) \
425 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500426 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800427} \
428static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
429
David Brownell2c8dc072007-01-18 00:45:48 -0500430
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400431/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500432 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
433 * accuracy scheme without calibration data. For now we won't try either;
434 * userspace sees raw sensor values, and must scale/calibrate appropriately.
435 */
436static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
437{
438 return v;
439}
440
441SHOW(temp0, temp0, null_adjust) /* temp1_input */
442SHOW(temp1, temp1, null_adjust) /* temp2_input */
443
444
445/* sysfs conventions report voltages in millivolts. We can convert voltages
446 * if we know vREF. userspace may need to scale vAUX to match the board's
447 * external resistors; we assume that vBATT only uses the internal ones.
448 */
449static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
450{
451 unsigned retval = v;
452
453 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400454 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500455 retval = retval >> 12;
Jason Wang2991a1c2010-10-13 11:35:40 -0700456
David Brownell2c8dc072007-01-18 00:45:48 -0500457 return retval;
458}
459
460static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
461{
462 unsigned retval = vaux_adjust(ts, v);
463
464 /* ads7846 has a resistor ladder to scale this signal down */
465 if (ts->model == 7846)
466 retval *= 4;
Jason Wang2991a1c2010-10-13 11:35:40 -0700467
David Brownell2c8dc072007-01-18 00:45:48 -0500468 return retval;
469}
470
471SHOW(in0_input, vaux, vaux_adjust)
472SHOW(in1_input, vbatt, vbatt_adjust)
473
David Brownell2c8dc072007-01-18 00:45:48 -0500474static struct attribute *ads7846_attributes[] = {
475 &dev_attr_temp0.attr,
476 &dev_attr_temp1.attr,
477 &dev_attr_in0_input.attr,
478 &dev_attr_in1_input.attr,
479 NULL,
480};
481
482static struct attribute_group ads7846_attr_group = {
483 .attrs = ads7846_attributes,
484};
485
486static struct attribute *ads7843_attributes[] = {
487 &dev_attr_in0_input.attr,
488 &dev_attr_in1_input.attr,
489 NULL,
490};
491
492static struct attribute_group ads7843_attr_group = {
493 .attrs = ads7843_attributes,
494};
495
496static struct attribute *ads7845_attributes[] = {
497 &dev_attr_in0_input.attr,
498 NULL,
499};
500
501static struct attribute_group ads7845_attr_group = {
502 .attrs = ads7845_attributes,
503};
504
505static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
506{
Tony Jones1beeffe2007-08-20 13:46:20 -0700507 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500508 int err;
509
510 /* hwmon sensors need a reference voltage */
511 switch (ts->model) {
512 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400513 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500514 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400515 ts->vref_mv = 2500;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700516 ts->use_internal = true;
David Brownell2c8dc072007-01-18 00:45:48 -0500517 }
518 break;
519 case 7845:
520 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400521 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500522 dev_warn(&spi->dev,
523 "external vREF for ADS%d not specified\n",
524 ts->model);
525 return 0;
526 }
527 break;
528 }
529
530 /* different chips have different sensor groups */
531 switch (ts->model) {
532 case 7846:
533 ts->attr_group = &ads7846_attr_group;
534 break;
535 case 7845:
536 ts->attr_group = &ads7845_attr_group;
537 break;
538 case 7843:
539 ts->attr_group = &ads7843_attr_group;
540 break;
541 default:
542 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
543 return 0;
544 }
545
546 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
547 if (err)
548 return err;
549
550 hwmon = hwmon_device_register(&spi->dev);
551 if (IS_ERR(hwmon)) {
552 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
553 return PTR_ERR(hwmon);
554 }
555
556 ts->hwmon = hwmon;
557 return 0;
558}
559
560static void ads784x_hwmon_unregister(struct spi_device *spi,
561 struct ads7846 *ts)
562{
563 if (ts->hwmon) {
564 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
565 hwmon_device_unregister(ts->hwmon);
566 }
567}
568
569#else
570static inline int ads784x_hwmon_register(struct spi_device *spi,
571 struct ads7846 *ts)
572{
573 return 0;
574}
575
576static inline void ads784x_hwmon_unregister(struct spi_device *spi,
577 struct ads7846 *ts)
578{
579}
580#endif
David Brownellffa458c2006-01-08 13:34:21 -0800581
Imre Deak438f2a72006-04-11 23:41:32 -0400582static ssize_t ads7846_pen_down_show(struct device *dev,
583 struct device_attribute *attr, char *buf)
584{
Jason Wang2991a1c2010-10-13 11:35:40 -0700585 struct ads7846 *ts = dev_get_drvdata(dev);
586
587 return sprintf(buf, "%u\n", ts->pendown);
Imre Deak438f2a72006-04-11 23:41:32 -0400588}
589
590static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
591
Imre Deak7de90a82006-04-11 23:43:55 -0400592static ssize_t ads7846_disable_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
Jason Wang2991a1c2010-10-13 11:35:40 -0700595 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400596
597 return sprintf(buf, "%u\n", ts->disabled);
598}
599
600static ssize_t ads7846_disable_store(struct device *dev,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
603{
604 struct ads7846 *ts = dev_get_drvdata(dev);
Harvey Harrison3a0c58d2008-12-23 04:09:28 -0500605 unsigned long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400606
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400607 if (strict_strtoul(buf, 10, &i))
608 return -EINVAL;
609
Imre Deak7de90a82006-04-11 23:43:55 -0400610 if (i)
611 ads7846_disable(ts);
612 else
613 ads7846_enable(ts);
614
Imre Deak7de90a82006-04-11 23:43:55 -0400615 return count;
616}
617
618static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
619
David Brownell2c8dc072007-01-18 00:45:48 -0500620static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500621 &dev_attr_pen_down.attr,
622 &dev_attr_disable.attr,
623 NULL,
624};
625
David Brownell2c8dc072007-01-18 00:45:48 -0500626static struct attribute_group ads784x_attr_group = {
627 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500628};
629
David Brownellffa458c2006-01-08 13:34:21 -0800630/*--------------------------------------------------------------------------*/
631
Eric Miao4d5975e2008-09-10 12:06:15 -0400632static int get_pendown_state(struct ads7846 *ts)
633{
634 if (ts->get_pendown_state)
635 return ts->get_pendown_state();
636
637 return !gpio_get_value(ts->gpio_pendown);
638}
639
Eric Miaofd746d52009-04-11 16:54:59 -0700640static void null_wait_for_sync(void)
641{
642}
643
Jason Wang2991a1c2010-10-13 11:35:40 -0700644static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
David Brownellffa458c2006-01-08 13:34:21 -0800645{
Jason Wang2991a1c2010-10-13 11:35:40 -0700646 struct ads7846 *ts = ads;
David Brownellffa458c2006-01-08 13:34:21 -0800647
Jason Wang2991a1c2010-10-13 11:35:40 -0700648 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
649 /* Start over collecting consistent readings. */
650 ts->read_rep = 0;
651 /*
652 * Repeat it, if this was the first read or the read
653 * wasn't consistent enough.
654 */
655 if (ts->read_cnt < ts->debounce_max) {
656 ts->last_read = *val;
657 ts->read_cnt++;
658 return ADS7846_FILTER_REPEAT;
659 } else {
660 /*
661 * Maximum number of debouncing reached and still
662 * not enough number of consistent readings. Abort
663 * the whole sample, repeat it in the next sampling
664 * period.
665 */
666 ts->read_cnt = 0;
667 return ADS7846_FILTER_IGNORE;
668 }
669 } else {
670 if (++ts->read_rep > ts->debounce_rep) {
671 /*
672 * Got a good reading for this coordinate,
673 * go for the next one.
674 */
675 ts->read_cnt = 0;
676 ts->read_rep = 0;
677 return ADS7846_FILTER_OK;
678 } else {
679 /* Read more values that are consistent. */
680 ts->read_cnt++;
681 return ADS7846_FILTER_REPEAT;
682 }
683 }
684}
685
686static int ads7846_no_filter(void *ads, int data_idx, int *val)
687{
688 return ADS7846_FILTER_OK;
689}
690
691static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
692{
693 struct spi_transfer *t =
694 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
695
696 if (ts->model == 7845) {
697 return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
698 } else {
699 /*
700 * adjust: on-wire is a must-ignore bit, a BE12 value, then
701 * padding; built from two 8 bit values written msb-first.
702 */
703 return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
704 }
705}
706
707static void ads7846_update_value(struct spi_message *m, int val)
708{
709 struct spi_transfer *t =
710 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
711
712 *(u16 *)t->rx_buf = val;
713}
714
715static void ads7846_read_state(struct ads7846 *ts)
716{
717 struct ads7846_packet *packet = ts->packet;
718 struct spi_message *m;
719 int msg_idx = 0;
720 int val;
721 int action;
722 int error;
723
724 while (msg_idx < ts->msg_count) {
725
726 ts->wait_for_sync();
727
728 m = &ts->msg[msg_idx];
729 error = spi_sync(ts->spi, m);
730 if (error) {
731 dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
732 packet->tc.ignore = true;
733 return;
734 }
735
736 /*
737 * Last message is power down request, no need to convert
738 * or filter the value.
739 */
740 if (msg_idx < ts->msg_count - 1) {
741
742 val = ads7846_get_value(ts, m);
743
744 action = ts->filter(ts->filter_data, msg_idx, &val);
745 switch (action) {
746 case ADS7846_FILTER_REPEAT:
747 continue;
748
749 case ADS7846_FILTER_IGNORE:
750 packet->tc.ignore = true;
751 msg_idx = ts->msg_count - 1;
752 continue;
753
754 case ADS7846_FILTER_OK:
755 ads7846_update_value(m, val);
756 packet->tc.ignore = false;
757 msg_idx++;
758 break;
759
760 default:
761 BUG();
762 }
763 } else {
764 msg_idx++;
765 }
766 }
767}
768
769static void ads7846_report_state(struct ads7846 *ts)
770{
771 struct ads7846_packet *packet = ts->packet;
772 unsigned int Rt;
773 u16 x, y, z1, z2;
774
775 /*
776 * ads7846_get_value() does in-place conversion (including byte swap)
777 * from on-the-wire format as part of debouncing to get stable
778 * readings.
David Brownellffa458c2006-01-08 13:34:21 -0800779 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700780 if (ts->model == 7845) {
781 x = *(u16 *)packet->tc.x_buf;
782 y = *(u16 *)packet->tc.y_buf;
783 z1 = 0;
784 z2 = 0;
785 } else {
786 x = packet->tc.x;
787 y = packet->tc.y;
788 z1 = packet->tc.z1;
789 z2 = packet->tc.z2;
790 }
David Brownellffa458c2006-01-08 13:34:21 -0800791
792 /* range filtering */
793 if (x == MAX_12BIT)
794 x = 0;
795
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400796 if (ts->model == 7843) {
797 Rt = ts->pressure_max / 2;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700798 } else if (ts->model == 7845) {
799 if (get_pendown_state(ts))
800 Rt = ts->pressure_max / 2;
801 else
802 Rt = 0;
803 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400804 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800805 /* compute touch pressure resistance using equation #2 */
806 Rt = z2;
807 Rt -= z1;
808 Rt *= x;
809 Rt *= ts->x_plate_ohms;
810 Rt /= z1;
811 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400812 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800813 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400814 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500815
Jason Wang2991a1c2010-10-13 11:35:40 -0700816 /*
817 * Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500818 * the maximum. Don't report it to user space, repeat at least
819 * once more the measurement
820 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400821 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800822 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
823 packet->tc.ignore, Rt);
Imre Deakd5b415c2006-04-26 00:13:18 -0400824 return;
825 }
826
Jason Wang2991a1c2010-10-13 11:35:40 -0700827 /*
828 * Maybe check the pendown state before reporting. This discards
Semih Hazar1d258912007-07-18 00:36:04 -0400829 * false readings when the pen is lifted.
830 */
831 if (ts->penirq_recheck_delay_usecs) {
832 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400833 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400834 Rt = 0;
835 }
836
Jason Wang2991a1c2010-10-13 11:35:40 -0700837 /*
838 * NOTE: We can't rely on the pressure to determine the pen down
839 * state, even this controller has a pressure sensor. The pressure
Imre Deak15e35892007-01-18 00:45:43 -0500840 * value can fluctuate for quite a while after lifting the pen and
841 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800842 *
Imre Deak15e35892007-01-18 00:45:43 -0500843 * The only safe way to check for the pen up condition is in the
844 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800845 */
David Brownellffa458c2006-01-08 13:34:21 -0800846 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500847 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400848
Michael Roth86579a42009-05-18 16:04:44 -0700849 if (ts->swap_xy)
850 swap(x, y);
851
Jason Wang2991a1c2010-10-13 11:35:40 -0700852 if (!ts->pendown) {
853 input_report_key(input, BTN_TOUCH, 1);
854 ts->pendown = true;
855 dev_vdbg(&ts->spi->dev, "DOWN\n");
856 }
857
Imre Deak15e35892007-01-18 00:45:43 -0500858 input_report_abs(input, ABS_X, x);
859 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800860 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800861
Imre Deak15e35892007-01-18 00:45:43 -0500862 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800863 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500864 }
David Brownellffa458c2006-01-08 13:34:21 -0800865}
866
Jason Wang2991a1c2010-10-13 11:35:40 -0700867static irqreturn_t ads7846_hard_irq(int irq, void *handle)
Imre Deak0b7018a2006-04-11 23:42:03 -0400868{
Jason Wang2991a1c2010-10-13 11:35:40 -0700869 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400870
Jason Wang2991a1c2010-10-13 11:35:40 -0700871 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
Imre Deakda970e62007-01-18 00:44:41 -0500872}
873
David Brownellffa458c2006-01-08 13:34:21 -0800874
David Howells7d12e782006-10-05 14:55:46 +0100875static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800876{
Imre Deak0b7018a2006-04-11 23:42:03 -0400877 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400878
Jason Wang2991a1c2010-10-13 11:35:40 -0700879 /* Start with a small delay before checking pendown state */
880 msleep(TS_POLL_DELAY);
881
882 while (!ts->stopped && get_pendown_state(ts)) {
883
884 /* pen is down, continue with the measurement */
885 ads7846_read_state(ts);
886
887 if (!ts->stopped)
888 ads7846_report_state(ts);
889
890 wait_event_timeout(ts->wait, ts->stopped,
891 msecs_to_jiffies(TS_POLL_PERIOD));
Imre Deak0b7018a2006-04-11 23:42:03 -0400892 }
Jason Wang2991a1c2010-10-13 11:35:40 -0700893
894 if (ts->pendown) {
895 struct input_dev *input = ts->input;
896
897 input_report_key(input, BTN_TOUCH, 0);
898 input_report_abs(input, ABS_PRESSURE, 0);
899 input_sync(input);
900
901 ts->pendown = false;
902 dev_vdbg(&ts->spi->dev, "UP\n");
903 }
Imre Deak7de90a82006-04-11 23:43:55 -0400904
905 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800906}
907
Mark Brown3c36e712011-01-20 22:51:26 -0800908#ifdef CONFIG_PM_SLEEP
909static int ads7846_suspend(struct device *dev)
Imre Deak7de90a82006-04-11 23:43:55 -0400910{
Mark Brown3c36e712011-01-20 22:51:26 -0800911 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400912
Jason Wang2991a1c2010-10-13 11:35:40 -0700913 mutex_lock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400914
Jason Wang2991a1c2010-10-13 11:35:40 -0700915 if (!ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400916
Jason Wang2991a1c2010-10-13 11:35:40 -0700917 if (!ts->disabled)
918 __ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -0400919
Jason Wang2991a1c2010-10-13 11:35:40 -0700920 if (device_may_wakeup(&ts->spi->dev))
921 enable_irq_wake(ts->spi->irq);
922
923 ts->suspended = true;
924 }
925
926 mutex_unlock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800927
David Brownellffa458c2006-01-08 13:34:21 -0800928 return 0;
929}
930
Mark Brown3c36e712011-01-20 22:51:26 -0800931static int ads7846_resume(struct device *dev)
David Brownellffa458c2006-01-08 13:34:21 -0800932{
Mark Brown3c36e712011-01-20 22:51:26 -0800933 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellffa458c2006-01-08 13:34:21 -0800934
Jason Wang2991a1c2010-10-13 11:35:40 -0700935 mutex_lock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800936
Jason Wang2991a1c2010-10-13 11:35:40 -0700937 if (ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400938
Jason Wang2991a1c2010-10-13 11:35:40 -0700939 ts->suspended = false;
Imre Deak7de90a82006-04-11 23:43:55 -0400940
Jason Wang2991a1c2010-10-13 11:35:40 -0700941 if (device_may_wakeup(&ts->spi->dev))
942 disable_irq_wake(ts->spi->irq);
943
944 if (!ts->disabled)
945 __ads7846_enable(ts);
946 }
947
948 mutex_unlock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400949
David Brownellffa458c2006-01-08 13:34:21 -0800950 return 0;
951}
Mark Brown3c36e712011-01-20 22:51:26 -0800952#endif
953
954static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
David Brownellffa458c2006-01-08 13:34:21 -0800955
Jason Wang2991a1c2010-10-13 11:35:40 -0700956static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)
Eric Miao4d5975e2008-09-10 12:06:15 -0400957{
958 struct ads7846_platform_data *pdata = spi->dev.platform_data;
959 int err;
960
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800961 /*
962 * REVISIT when the irq can be triggered active-low, or if for some
Eric Miao4d5975e2008-09-10 12:06:15 -0400963 * reason the touchscreen isn't hooked up, we don't need to access
964 * the pendown state.
965 */
Eric Miao4d5975e2008-09-10 12:06:15 -0400966
967 if (pdata->get_pendown_state) {
968 ts->get_pendown_state = pdata->get_pendown_state;
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800969 } else if (gpio_is_valid(pdata->gpio_pendown)) {
Eric Miao4d5975e2008-09-10 12:06:15 -0400970
Igor Grinberg58c24402011-06-27 13:06:27 -0700971 err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
972 "ads7846_pendown");
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800973 if (err) {
Igor Grinberg58c24402011-06-27 13:06:27 -0700974 dev_err(&spi->dev,
975 "failed to request/setup pendown GPIO%d: %d\n",
976 pdata->gpio_pendown, err);
Igor Grinberg1201e7e2011-05-11 15:45:05 -0700977 return err;
978 }
Eric Miao4d5975e2008-09-10 12:06:15 -0400979
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800980 ts->gpio_pendown = pdata->gpio_pendown;
981
982 } else {
983 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
984 return -EINVAL;
985 }
Jason Wang2991a1c2010-10-13 11:35:40 -0700986
Eric Miao4d5975e2008-09-10 12:06:15 -0400987 return 0;
988}
989
Jason Wang2991a1c2010-10-13 11:35:40 -0700990/*
991 * Set up the transfers to read touchscreen state; this assumes we
992 * use formula #2 for pressure, not #3.
993 */
994static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,
995 const struct ads7846_platform_data *pdata)
David Brownellffa458c2006-01-08 13:34:21 -0800996{
Jason Wang2991a1c2010-10-13 11:35:40 -0700997 struct spi_message *m = &ts->msg[0];
998 struct spi_transfer *x = ts->xfer;
999 struct ads7846_packet *packet = ts->packet;
1000 int vref = pdata->keep_vref_on;
Imre Deakde2defd2007-01-18 00:45:21 -05001001
Michael Hennerich06a09122010-03-09 20:38:45 -08001002 if (ts->model == 7873) {
Jason Wang2991a1c2010-10-13 11:35:40 -07001003 /*
1004 * The AD7873 is almost identical to the ADS7846
Michael Hennerich06a09122010-03-09 20:38:45 -08001005 * keep VREF off during differential/ratiometric
Jason Wang2991a1c2010-10-13 11:35:40 -07001006 * conversion modes.
Michael Hennerich06a09122010-03-09 20:38:45 -08001007 */
1008 ts->model = 7846;
1009 vref = 0;
1010 }
1011
Jason Wang2991a1c2010-10-13 11:35:40 -07001012 ts->msg_count = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001013 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001014 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001015
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001016 if (ts->model == 7845) {
1017 packet->read_y_cmd[0] = READ_Y(vref);
1018 packet->read_y_cmd[1] = 0;
1019 packet->read_y_cmd[2] = 0;
1020 x->tx_buf = &packet->read_y_cmd[0];
1021 x->rx_buf = &packet->tc.y_buf[0];
1022 x->len = 3;
1023 spi_message_add_tail(x, m);
1024 } else {
1025 /* y- still on; turn on only y+ (and ADC) */
1026 packet->read_y = READ_Y(vref);
1027 x->tx_buf = &packet->read_y;
1028 x->len = 1;
1029 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001030
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001031 x++;
1032 x->rx_buf = &packet->tc.y;
1033 x->len = 2;
1034 spi_message_add_tail(x, m);
1035 }
David Brownellffa458c2006-01-08 13:34:21 -08001036
Jason Wang2991a1c2010-10-13 11:35:40 -07001037 /*
1038 * The first sample after switching drivers can be low quality;
Semih Hazare4f48862007-07-18 00:35:56 -04001039 * optionally discard it, using a second one after the signals
1040 * have had enough time to stabilize.
1041 */
1042 if (pdata->settle_delay_usecs) {
1043 x->delay_usecs = pdata->settle_delay_usecs;
1044
1045 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001046 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001047 x->len = 1;
1048 spi_message_add_tail(x, m);
1049
1050 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001051 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001052 x->len = 2;
1053 spi_message_add_tail(x, m);
1054 }
1055
Jason Wang2991a1c2010-10-13 11:35:40 -07001056 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001057 m++;
1058 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001059 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001060
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001061 if (ts->model == 7845) {
1062 x++;
1063 packet->read_x_cmd[0] = READ_X(vref);
1064 packet->read_x_cmd[1] = 0;
1065 packet->read_x_cmd[2] = 0;
1066 x->tx_buf = &packet->read_x_cmd[0];
1067 x->rx_buf = &packet->tc.x_buf[0];
1068 x->len = 3;
1069 spi_message_add_tail(x, m);
1070 } else {
1071 /* turn y- off, x+ on, then leave in lowpower */
1072 x++;
1073 packet->read_x = READ_X(vref);
1074 x->tx_buf = &packet->read_x;
1075 x->len = 1;
1076 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001077
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001078 x++;
1079 x->rx_buf = &packet->tc.x;
1080 x->len = 2;
1081 spi_message_add_tail(x, m);
1082 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001083
Semih Hazare4f48862007-07-18 00:35:56 -04001084 /* ... maybe discard first sample ... */
1085 if (pdata->settle_delay_usecs) {
1086 x->delay_usecs = pdata->settle_delay_usecs;
1087
1088 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001089 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001090 x->len = 1;
1091 spi_message_add_tail(x, m);
1092
1093 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001094 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001095 x->len = 2;
1096 spi_message_add_tail(x, m);
1097 }
1098
Imre Deak0b7018a2006-04-11 23:42:03 -04001099 /* turn y+ off, x- on; we'll use formula #2 */
1100 if (ts->model == 7846) {
Jason Wang2991a1c2010-10-13 11:35:40 -07001101 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001102 m++;
1103 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001104 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001105
1106 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001107 packet->read_z1 = READ_Z1(vref);
1108 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001109 x->len = 1;
1110 spi_message_add_tail(x, m);
1111
1112 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001113 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001114 x->len = 2;
1115 spi_message_add_tail(x, m);
1116
Semih Hazare4f48862007-07-18 00:35:56 -04001117 /* ... maybe discard first sample ... */
1118 if (pdata->settle_delay_usecs) {
1119 x->delay_usecs = pdata->settle_delay_usecs;
1120
1121 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001122 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001123 x->len = 1;
1124 spi_message_add_tail(x, m);
1125
1126 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001127 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001128 x->len = 2;
1129 spi_message_add_tail(x, m);
1130 }
1131
Jason Wang2991a1c2010-10-13 11:35:40 -07001132 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001133 m++;
1134 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001135 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001136
1137 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001138 packet->read_z2 = READ_Z2(vref);
1139 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001140 x->len = 1;
1141 spi_message_add_tail(x, m);
1142
1143 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001144 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001145 x->len = 2;
1146 spi_message_add_tail(x, m);
1147
Semih Hazare4f48862007-07-18 00:35:56 -04001148 /* ... maybe discard first sample ... */
1149 if (pdata->settle_delay_usecs) {
1150 x->delay_usecs = pdata->settle_delay_usecs;
1151
1152 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001153 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001154 x->len = 1;
1155 spi_message_add_tail(x, m);
1156
1157 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001158 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001159 x->len = 2;
1160 spi_message_add_tail(x, m);
1161 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001162 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001163
1164 /* power down */
Jason Wang2991a1c2010-10-13 11:35:40 -07001165 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001166 m++;
1167 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001168 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001169
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001170 if (ts->model == 7845) {
1171 x++;
1172 packet->pwrdown_cmd[0] = PWRDOWN;
1173 packet->pwrdown_cmd[1] = 0;
1174 packet->pwrdown_cmd[2] = 0;
1175 x->tx_buf = &packet->pwrdown_cmd[0];
1176 x->len = 3;
1177 } else {
1178 x++;
1179 packet->pwrdown = PWRDOWN;
1180 x->tx_buf = &packet->pwrdown;
1181 x->len = 1;
1182 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001183
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001184 x++;
1185 x->rx_buf = &packet->dummy;
1186 x->len = 2;
1187 }
1188
David Brownelld93f70b2006-02-15 00:49:35 -05001189 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001190 spi_message_add_tail(x, m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001191}
David Brownellffa458c2006-01-08 13:34:21 -08001192
Jason Wang2991a1c2010-10-13 11:35:40 -07001193static int __devinit ads7846_probe(struct spi_device *spi)
1194{
1195 struct ads7846 *ts;
1196 struct ads7846_packet *packet;
1197 struct input_dev *input_dev;
1198 struct ads7846_platform_data *pdata = spi->dev.platform_data;
1199 unsigned long irq_flags;
1200 int err;
David Brownellffa458c2006-01-08 13:34:21 -08001201
Jason Wang2991a1c2010-10-13 11:35:40 -07001202 if (!spi->irq) {
1203 dev_dbg(&spi->dev, "no IRQ?\n");
1204 return -ENODEV;
1205 }
1206
1207 if (!pdata) {
1208 dev_dbg(&spi->dev, "no platform data?\n");
1209 return -ENODEV;
1210 }
1211
1212 /* don't exceed max specified sample rate */
1213 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1214 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
1215 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1216 return -EINVAL;
1217 }
1218
1219 /* We'd set TX word size 8 bits and RX word size to 13 bits ... except
1220 * that even if the hardware can do that, the SPI controller driver
1221 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1222 */
1223 spi->bits_per_word = 8;
1224 spi->mode = SPI_MODE_0;
1225 err = spi_setup(spi);
1226 if (err < 0)
1227 return err;
1228
1229 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1230 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1231 input_dev = input_allocate_device();
1232 if (!ts || !packet || !input_dev) {
1233 err = -ENOMEM;
1234 goto err_free_mem;
1235 }
1236
1237 dev_set_drvdata(&spi->dev, ts);
1238
1239 ts->packet = packet;
1240 ts->spi = spi;
1241 ts->input = input_dev;
1242 ts->vref_mv = pdata->vref_mv;
1243 ts->swap_xy = pdata->swap_xy;
1244
1245 mutex_init(&ts->lock);
1246 init_waitqueue_head(&ts->wait);
1247
1248 ts->model = pdata->model ? : 7846;
1249 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1250 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1251 ts->pressure_max = pdata->pressure_max ? : ~0;
1252
1253 if (pdata->filter != NULL) {
1254 if (pdata->filter_init != NULL) {
1255 err = pdata->filter_init(pdata, &ts->filter_data);
1256 if (err < 0)
1257 goto err_free_mem;
1258 }
1259 ts->filter = pdata->filter;
1260 ts->filter_cleanup = pdata->filter_cleanup;
1261 } else if (pdata->debounce_max) {
1262 ts->debounce_max = pdata->debounce_max;
1263 if (ts->debounce_max < 2)
1264 ts->debounce_max = 2;
1265 ts->debounce_tol = pdata->debounce_tol;
1266 ts->debounce_rep = pdata->debounce_rep;
1267 ts->filter = ads7846_debounce_filter;
1268 ts->filter_data = ts;
1269 } else {
1270 ts->filter = ads7846_no_filter;
1271 }
1272
1273 err = ads7846_setup_pendown(spi, ts);
1274 if (err)
1275 goto err_cleanup_filter;
1276
1277 if (pdata->penirq_recheck_delay_usecs)
1278 ts->penirq_recheck_delay_usecs =
1279 pdata->penirq_recheck_delay_usecs;
1280
1281 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1282
1283 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1284 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1285
1286 input_dev->name = ts->name;
1287 input_dev->phys = ts->phys;
1288 input_dev->dev.parent = &spi->dev;
1289
1290 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1291 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1292 input_set_abs_params(input_dev, ABS_X,
1293 pdata->x_min ? : 0,
1294 pdata->x_max ? : MAX_12BIT,
1295 0, 0);
1296 input_set_abs_params(input_dev, ABS_Y,
1297 pdata->y_min ? : 0,
1298 pdata->y_max ? : MAX_12BIT,
1299 0, 0);
1300 input_set_abs_params(input_dev, ABS_PRESSURE,
1301 pdata->pressure_min, pdata->pressure_max, 0, 0);
1302
1303 ads7846_setup_spi_msg(ts, pdata);
Imre Deakd5b415c2006-04-26 00:13:18 -04001304
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001305 ts->reg = regulator_get(&spi->dev, "vcc");
1306 if (IS_ERR(ts->reg)) {
Kevin Hilman067fb2f2010-05-26 23:30:55 -07001307 err = PTR_ERR(ts->reg);
Dmitry Torokhov56960b32010-06-02 00:40:06 -07001308 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001309 goto err_free_gpio;
1310 }
1311
1312 err = regulator_enable(ts->reg);
1313 if (err) {
1314 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1315 goto err_put_regulator;
1316 }
1317
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001318 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
Jason Wang2991a1c2010-10-13 11:35:40 -07001319 irq_flags |= IRQF_ONESHOT;
Anatolij Gustschin78043022010-06-28 01:25:19 -07001320
Jason Wang2991a1c2010-10-13 11:35:40 -07001321 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1322 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001323 if (err && !pdata->irq_flags) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001324 dev_info(&spi->dev,
1325 "trying pin change workaround on irq %d\n", spi->irq);
Jason Wang2991a1c2010-10-13 11:35:40 -07001326 irq_flags |= IRQF_TRIGGER_RISING;
1327 err = request_threaded_irq(spi->irq,
1328 ads7846_hard_irq, ads7846_irq,
1329 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001330 }
1331
1332 if (err) {
1333 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1334 goto err_disable_regulator;
David Brownellffa458c2006-01-08 13:34:21 -08001335 }
David Brownellffa458c2006-01-08 13:34:21 -08001336
David Brownell2c8dc072007-01-18 00:45:48 -05001337 err = ads784x_hwmon_register(spi, ts);
1338 if (err)
1339 goto err_free_irq;
1340
David Brownell2e5a7bd2006-01-08 13:34:25 -08001341 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001342
Jason Wang2991a1c2010-10-13 11:35:40 -07001343 /*
1344 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001345 * the touchscreen, in case it's not connected.
1346 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001347 if (ts->model == 7845)
1348 ads7845_read12_ser(&spi->dev, PWRDOWN);
1349 else
Alexander Steinebcaaad2011-05-11 16:24:08 -07001350 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
David Brownellffa458c2006-01-08 13:34:21 -08001351
David Brownell2c8dc072007-01-18 00:45:48 -05001352 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001353 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001354 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001355
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001356 err = input_register_device(input_dev);
1357 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001358 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001359
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001360 device_init_wakeup(&spi->dev, pdata->wakeup);
1361
David Brownellffa458c2006-01-08 13:34:21 -08001362 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001363
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001364 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001365 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1366 err_remove_hwmon:
1367 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001368 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001369 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001370 err_disable_regulator:
1371 regulator_disable(ts->reg);
1372 err_put_regulator:
1373 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001374 err_free_gpio:
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001375 if (!ts->get_pendown_state)
Eric Miao4d5975e2008-09-10 12:06:15 -04001376 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001377 err_cleanup_filter:
1378 if (ts->filter_cleanup)
1379 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001380 err_free_mem:
1381 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001382 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001383 kfree(ts);
1384 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001385}
1386
David Brownell2e5a7bd2006-01-08 13:34:25 -08001387static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001388{
Jason Wang2991a1c2010-10-13 11:35:40 -07001389 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001390
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001391 device_init_wakeup(&spi->dev, false);
1392
David Brownell2c8dc072007-01-18 00:45:48 -05001393 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001394
Jason Wang2991a1c2010-10-13 11:35:40 -07001395 ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001396 free_irq(ts->spi->irq, ts);
Jason Wang2991a1c2010-10-13 11:35:40 -07001397
1398 input_unregister_device(ts->input);
1399
1400 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001401
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001402 regulator_disable(ts->reg);
1403 regulator_put(ts->reg);
1404
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001405 if (!ts->get_pendown_state) {
1406 /*
1407 * If we are not using specialized pendown method we must
1408 * have been relying on gpio we set up ourselves.
1409 */
Eric Miao4d5975e2008-09-10 12:06:15 -04001410 gpio_free(ts->gpio_pendown);
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001411 }
Eric Miao4d5975e2008-09-10 12:06:15 -04001412
Imre Deakda970e62007-01-18 00:44:41 -05001413 if (ts->filter_cleanup)
1414 ts->filter_cleanup(ts->filter_data);
1415
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001416 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001417 kfree(ts);
1418
David Brownell2e5a7bd2006-01-08 13:34:25 -08001419 dev_dbg(&spi->dev, "unregistered touchscreen\n");
Jason Wang2991a1c2010-10-13 11:35:40 -07001420
David Brownellffa458c2006-01-08 13:34:21 -08001421 return 0;
1422}
1423
David Brownell2e5a7bd2006-01-08 13:34:25 -08001424static struct spi_driver ads7846_driver = {
1425 .driver = {
1426 .name = "ads7846",
1427 .bus = &spi_bus_type,
1428 .owner = THIS_MODULE,
Mark Brown3c36e712011-01-20 22:51:26 -08001429 .pm = &ads7846_pm,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001430 },
David Brownellffa458c2006-01-08 13:34:21 -08001431 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001432 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001433};
1434
1435static int __init ads7846_init(void)
1436{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001437 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001438}
1439module_init(ads7846_init);
1440
1441static void __exit ads7846_exit(void)
1442{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001443 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001444}
1445module_exit(ads7846_exit);
1446
1447MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1448MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001449MODULE_ALIAS("spi:ads7846");