blob: 16031933a8f621266a555b09ff63979c0359ca0b [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;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070071 u8 x_buf[3];
72 u8 y_buf[3];
David Brownellffa458c2006-01-08 13:34:21 -080073};
74
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040075/*
76 * We allocate this separately to avoid cache line sharing issues when
77 * driver is used with DMA-based SPI controllers (like atmel_spi) on
78 * systems where main memory is not DMA-coherent (most non-x86 boards).
79 */
80struct ads7846_packet {
81 u8 read_x, read_y, read_z1, read_z2, pwrdown;
82 u16 dummy; /* for the pwrdown read */
83 struct ts_event tc;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070084 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
85 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040086};
87
David Brownellffa458c2006-01-08 13:34:21 -080088struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050089 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080090 char phys[32];
Michael Rothb58895f2009-05-18 16:05:12 -070091 char name[32];
David Brownellffa458c2006-01-08 13:34:21 -080092
93 struct spi_device *spi;
Grazvydas Ignotas91143372010-02-25 02:04:56 -080094 struct regulator *reg;
David Brownell2c8dc072007-01-18 00:45:48 -050095
96#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050097 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070098 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050099#endif
100
David Brownellffa458c2006-01-08 13:34:21 -0800101 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400102 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800103 u16 vref_delay_usecs;
104 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400105 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800106
Michael Roth86579a42009-05-18 16:04:44 -0700107 bool swap_xy;
108
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400109 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800110
Semih Hazare4f48862007-07-18 00:35:56 -0400111 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400112 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400113 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400114 int msg_idx;
115 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400116 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400117 int last_read;
118
119 u16 debounce_max;
120 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400121 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800122
Semih Hazar1d258912007-07-18 00:36:04 -0400123 u16 penirq_recheck_delay_usecs;
124
David Brownellffa458c2006-01-08 13:34:21 -0800125 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500126 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800127 unsigned pendown:1; /* P: lock */
128 unsigned pending:1; /* P: lock */
129// FIXME remove "irq_disabled"
130 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400131 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500132 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400133
Imre Deakda970e62007-01-18 00:44:41 -0500134 int (*filter)(void *data, int data_idx, int *val);
135 void *filter_data;
136 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400137 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400138 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700139
140 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800141};
142
143/* leave chip selected when we're done, for quicker re-select? */
144#if 0
145#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
146#else
147#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
148#endif
149
150/*--------------------------------------------------------------------------*/
151
152/* The ADS7846 has touchscreen and other sensors.
153 * Earlier ads784x chips are somewhat compatible.
154 */
155#define ADS_START (1 << 7)
156#define ADS_A2A1A0_d_y (1 << 4) /* differential */
157#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
158#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
159#define ADS_A2A1A0_d_x (5 << 4) /* differential */
160#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
161#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
162#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
163#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
164#define ADS_8_BIT (1 << 3)
165#define ADS_12_BIT (0 << 3)
166#define ADS_SER (1 << 2) /* non-differential */
167#define ADS_DFR (0 << 2) /* differential */
168#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
169#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
170#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
171#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
172
173#define MAX_12BIT ((1<<12)-1)
174
175/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500176#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
177 | ADS_12_BIT | ADS_DFR | \
178 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800179
Imre Deakde2defd2007-01-18 00:45:21 -0500180#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
181#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
182#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400183
Imre Deakde2defd2007-01-18 00:45:21 -0500184#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
185#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800186
187/* single-ended samples need to first power up reference voltage;
188 * we leave both ADC and VREF powered
189 */
190#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
191 | ADS_12_BIT | ADS_SER)
192
Imre Deakde2defd2007-01-18 00:45:21 -0500193#define REF_ON (READ_12BIT_DFR(x, 1, 1))
194#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800195
196/*--------------------------------------------------------------------------*/
197
198/*
199 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500200 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
201 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800202 */
203
204struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500205 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800206 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500207 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800208 u16 scratch;
209 __be16 sample;
210 struct spi_message msg;
211 struct spi_transfer xfer[6];
212};
213
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700214struct ads7845_ser_req {
215 u8 command[3];
216 u8 pwrdown[3];
217 u8 sample[3];
218 struct spi_message msg;
219 struct spi_transfer xfer[2];
220};
221
Imre Deak7de90a82006-04-11 23:43:55 -0400222static void ads7846_enable(struct ads7846 *ts);
223static void ads7846_disable(struct ads7846 *ts);
224
Imre Deakc9e617a2006-04-11 23:44:05 -0400225static int device_suspended(struct device *dev)
226{
227 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500228 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400229}
230
David Brownellffa458c2006-01-08 13:34:21 -0800231static int ads7846_read12_ser(struct device *dev, unsigned command)
232{
233 struct spi_device *spi = to_spi_device(dev);
234 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800235 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800236 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500237 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800238
239 if (!req)
240 return -ENOMEM;
241
Imre Deak0b7018a2006-04-11 23:42:03 -0400242 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800243
David Brownell2c8dc072007-01-18 00:45:48 -0500244 /* FIXME boards with ads7846 might use external vref instead ... */
245 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800246
David Brownell2c8dc072007-01-18 00:45:48 -0500247 /* maybe turn on internal vREF, and let it settle */
248 if (use_internal) {
249 req->ref_on = REF_ON;
250 req->xfer[0].tx_buf = &req->ref_on;
251 req->xfer[0].len = 1;
252 spi_message_add_tail(&req->xfer[0], &req->msg);
253
254 req->xfer[1].rx_buf = &req->scratch;
255 req->xfer[1].len = 2;
256
257 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
258 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
259 spi_message_add_tail(&req->xfer[1], &req->msg);
260 }
David Brownellffa458c2006-01-08 13:34:21 -0800261
262 /* take sample */
263 req->command = (u8) command;
264 req->xfer[2].tx_buf = &req->command;
265 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500266 spi_message_add_tail(&req->xfer[2], &req->msg);
267
David Brownellffa458c2006-01-08 13:34:21 -0800268 req->xfer[3].rx_buf = &req->sample;
269 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500270 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800271
272 /* REVISIT: take a few more samples, and compare ... */
273
Nicolas Ferre969111e2007-02-28 23:51:03 -0500274 /* converter in low power mode & enable PENIRQ */
275 req->ref_off = PWRDOWN;
276 req->xfer[4].tx_buf = &req->ref_off;
277 req->xfer[4].len = 1;
278 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800279
Nicolas Ferre969111e2007-02-28 23:51:03 -0500280 req->xfer[5].rx_buf = &req->scratch;
281 req->xfer[5].len = 2;
282 CS_CHANGE(req->xfer[5]);
283 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800284
Imre Deakc9e617a2006-04-11 23:44:05 -0400285 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800286 disable_irq(spi->irq);
287 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400288 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800289 enable_irq(spi->irq);
290
Marc Pignatc24b2602007-12-04 23:45:11 -0800291 if (status == 0) {
292 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400293 status = be16_to_cpu(req->sample);
294 status = status >> 3;
295 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800296 }
David Brownell90845332006-05-25 18:44:20 -0700297
298 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400299 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800300}
301
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700302static int ads7845_read12_ser(struct device *dev, unsigned command)
303{
304 struct spi_device *spi = to_spi_device(dev);
305 struct ads7846 *ts = dev_get_drvdata(dev);
306 struct ads7845_ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
307 int status;
308
309 if (!req)
310 return -ENOMEM;
311
312 spi_message_init(&req->msg);
313
314 req->command[0] = (u8) command;
315 req->xfer[0].tx_buf = req->command;
316 req->xfer[0].rx_buf = req->sample;
317 req->xfer[0].len = 3;
318 spi_message_add_tail(&req->xfer[0], &req->msg);
319
320 ts->irq_disabled = 1;
321 disable_irq(spi->irq);
322 status = spi_sync(spi, &req->msg);
323 ts->irq_disabled = 0;
324 enable_irq(spi->irq);
325
326 if (status == 0) {
327 /* BE12 value, then padding */
328 status = be16_to_cpu(*((u16 *)&req->sample[1]));
329 status = status >> 3;
330 status &= 0x0fff;
331 }
332
333 kfree(req);
334 return status;
335}
336
David Brownell2c8dc072007-01-18 00:45:48 -0500337#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
338
339#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800340name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
341{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500342 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800343 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500344 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800345 if (v < 0) \
346 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500347 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800348} \
349static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
350
David Brownell2c8dc072007-01-18 00:45:48 -0500351
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400352/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500353 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
354 * accuracy scheme without calibration data. For now we won't try either;
355 * userspace sees raw sensor values, and must scale/calibrate appropriately.
356 */
357static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
358{
359 return v;
360}
361
362SHOW(temp0, temp0, null_adjust) /* temp1_input */
363SHOW(temp1, temp1, null_adjust) /* temp2_input */
364
365
366/* sysfs conventions report voltages in millivolts. We can convert voltages
367 * if we know vREF. userspace may need to scale vAUX to match the board's
368 * external resistors; we assume that vBATT only uses the internal ones.
369 */
370static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
371{
372 unsigned retval = v;
373
374 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400375 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500376 retval = retval >> 12;
377 return retval;
378}
379
380static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
381{
382 unsigned retval = vaux_adjust(ts, v);
383
384 /* ads7846 has a resistor ladder to scale this signal down */
385 if (ts->model == 7846)
386 retval *= 4;
387 return retval;
388}
389
390SHOW(in0_input, vaux, vaux_adjust)
391SHOW(in1_input, vbatt, vbatt_adjust)
392
393
394static struct attribute *ads7846_attributes[] = {
395 &dev_attr_temp0.attr,
396 &dev_attr_temp1.attr,
397 &dev_attr_in0_input.attr,
398 &dev_attr_in1_input.attr,
399 NULL,
400};
401
402static struct attribute_group ads7846_attr_group = {
403 .attrs = ads7846_attributes,
404};
405
406static struct attribute *ads7843_attributes[] = {
407 &dev_attr_in0_input.attr,
408 &dev_attr_in1_input.attr,
409 NULL,
410};
411
412static struct attribute_group ads7843_attr_group = {
413 .attrs = ads7843_attributes,
414};
415
416static struct attribute *ads7845_attributes[] = {
417 &dev_attr_in0_input.attr,
418 NULL,
419};
420
421static struct attribute_group ads7845_attr_group = {
422 .attrs = ads7845_attributes,
423};
424
425static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
426{
Tony Jones1beeffe2007-08-20 13:46:20 -0700427 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500428 int err;
429
430 /* hwmon sensors need a reference voltage */
431 switch (ts->model) {
432 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400433 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500434 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400435 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500436 }
437 break;
438 case 7845:
439 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400440 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500441 dev_warn(&spi->dev,
442 "external vREF for ADS%d not specified\n",
443 ts->model);
444 return 0;
445 }
446 break;
447 }
448
449 /* different chips have different sensor groups */
450 switch (ts->model) {
451 case 7846:
452 ts->attr_group = &ads7846_attr_group;
453 break;
454 case 7845:
455 ts->attr_group = &ads7845_attr_group;
456 break;
457 case 7843:
458 ts->attr_group = &ads7843_attr_group;
459 break;
460 default:
461 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
462 return 0;
463 }
464
465 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
466 if (err)
467 return err;
468
469 hwmon = hwmon_device_register(&spi->dev);
470 if (IS_ERR(hwmon)) {
471 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
472 return PTR_ERR(hwmon);
473 }
474
475 ts->hwmon = hwmon;
476 return 0;
477}
478
479static void ads784x_hwmon_unregister(struct spi_device *spi,
480 struct ads7846 *ts)
481{
482 if (ts->hwmon) {
483 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
484 hwmon_device_unregister(ts->hwmon);
485 }
486}
487
488#else
489static inline int ads784x_hwmon_register(struct spi_device *spi,
490 struct ads7846 *ts)
491{
492 return 0;
493}
494
495static inline void ads784x_hwmon_unregister(struct spi_device *spi,
496 struct ads7846 *ts)
497{
498}
499#endif
David Brownellffa458c2006-01-08 13:34:21 -0800500
Imre Deak438f2a72006-04-11 23:41:32 -0400501static int is_pen_down(struct device *dev)
502{
David Brownell2c8dc072007-01-18 00:45:48 -0500503 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400504
505 return ts->pendown;
506}
507
508static ssize_t ads7846_pen_down_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
510{
511 return sprintf(buf, "%u\n", is_pen_down(dev));
512}
513
514static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
515
Imre Deak7de90a82006-04-11 23:43:55 -0400516static ssize_t ads7846_disable_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
519 struct ads7846 *ts = dev_get_drvdata(dev);
520
521 return sprintf(buf, "%u\n", ts->disabled);
522}
523
524static ssize_t ads7846_disable_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 struct ads7846 *ts = dev_get_drvdata(dev);
Harvey Harrison3a0c58d2008-12-23 04:09:28 -0500529 unsigned long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400530
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400531 if (strict_strtoul(buf, 10, &i))
532 return -EINVAL;
533
Imre Deak7de90a82006-04-11 23:43:55 -0400534 spin_lock_irq(&ts->lock);
535
536 if (i)
537 ads7846_disable(ts);
538 else
539 ads7846_enable(ts);
540
541 spin_unlock_irq(&ts->lock);
542
543 return count;
544}
545
546static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
547
David Brownell2c8dc072007-01-18 00:45:48 -0500548static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500549 &dev_attr_pen_down.attr,
550 &dev_attr_disable.attr,
551 NULL,
552};
553
David Brownell2c8dc072007-01-18 00:45:48 -0500554static struct attribute_group ads784x_attr_group = {
555 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500556};
557
David Brownellffa458c2006-01-08 13:34:21 -0800558/*--------------------------------------------------------------------------*/
559
Eric Miao4d5975e2008-09-10 12:06:15 -0400560static int get_pendown_state(struct ads7846 *ts)
561{
562 if (ts->get_pendown_state)
563 return ts->get_pendown_state();
564
565 return !gpio_get_value(ts->gpio_pendown);
566}
567
Eric Miaofd746d52009-04-11 16:54:59 -0700568static void null_wait_for_sync(void)
569{
570}
571
David Brownellffa458c2006-01-08 13:34:21 -0800572/*
573 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
574 * to retrieve touchscreen status.
575 *
576 * The SPI transfer completion callback does the real work. It reports
577 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
578 */
579
580static void ads7846_rx(void *ads)
581{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500582 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400583 struct ads7846_packet *packet = ts->packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500584 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500585 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800586
Imre Deakda970e62007-01-18 00:44:41 -0500587 /* ads7846_rx_val() did in-place conversion (including byteswap) from
588 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800589 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700590 if (ts->model == 7845) {
591 x = *(u16 *)packet->tc.x_buf;
592 y = *(u16 *)packet->tc.y_buf;
593 z1 = 0;
594 z2 = 0;
595 } else {
596 x = packet->tc.x;
597 y = packet->tc.y;
598 z1 = packet->tc.z1;
599 z2 = packet->tc.z2;
600 }
David Brownellffa458c2006-01-08 13:34:21 -0800601
602 /* range filtering */
603 if (x == MAX_12BIT)
604 x = 0;
605
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400606 if (ts->model == 7843) {
607 Rt = ts->pressure_max / 2;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700608 } else if (ts->model == 7845) {
609 if (get_pendown_state(ts))
610 Rt = ts->pressure_max / 2;
611 else
612 Rt = 0;
613 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400614 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800615 /* compute touch pressure resistance using equation #2 */
616 Rt = z2;
617 Rt -= z1;
618 Rt *= x;
619 Rt *= ts->x_plate_ohms;
620 Rt /= z1;
621 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400622 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800623 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400624 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500625
Imre Deakd5b415c2006-04-26 00:13:18 -0400626 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500627 * the maximum. Don't report it to user space, repeat at least
628 * once more the measurement
629 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400630 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800631 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
632 packet->tc.ignore, Rt);
Imre Deak1936d592007-01-18 00:45:31 -0500633 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800634 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400635 return;
636 }
637
Semih Hazar1d258912007-07-18 00:36:04 -0400638 /* Maybe check the pendown state before reporting. This discards
639 * false readings when the pen is lifted.
640 */
641 if (ts->penirq_recheck_delay_usecs) {
642 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400643 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400644 Rt = 0;
645 }
646
Imre Deak15e35892007-01-18 00:45:43 -0500647 /* NOTE: We can't rely on the pressure to determine the pen down
648 * state, even this controller has a pressure sensor. The pressure
649 * value can fluctuate for quite a while after lifting the pen and
650 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800651 *
Imre Deak15e35892007-01-18 00:45:43 -0500652 * The only safe way to check for the pen up condition is in the
653 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800654 */
David Brownellffa458c2006-01-08 13:34:21 -0800655 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500656 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400657
Imre Deak15e35892007-01-18 00:45:43 -0500658 if (!ts->pendown) {
659 input_report_key(input, BTN_TOUCH, 1);
660 ts->pendown = 1;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800661 dev_vdbg(&ts->spi->dev, "DOWN\n");
Imre Deak15e35892007-01-18 00:45:43 -0500662 }
Michael Roth86579a42009-05-18 16:04:44 -0700663
664 if (ts->swap_xy)
665 swap(x, y);
666
Imre Deak15e35892007-01-18 00:45:43 -0500667 input_report_abs(input, ABS_X, x);
668 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800669 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800670
Imre Deak15e35892007-01-18 00:45:43 -0500671 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800672 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500673 }
David Brownellffa458c2006-01-08 13:34:21 -0800674
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800675 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
676 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800677}
678
Imre Deakda970e62007-01-18 00:44:41 -0500679static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400680{
681 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400682
Imre Deakda970e62007-01-18 00:44:41 -0500683 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
684 /* Start over collecting consistent readings. */
685 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400686 /* Repeat it, if this was the first read or the read
687 * wasn't consistent enough. */
688 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500689 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400690 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500691 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400692 } else {
693 /* Maximum number of debouncing reached and still
694 * not enough number of consistent readings. Abort
695 * the whole sample, repeat it in the next sampling
696 * period.
697 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400698 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500699 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400700 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400701 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400702 if (++ts->read_rep > ts->debounce_rep) {
703 /* Got a good reading for this coordinate,
704 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400705 ts->read_cnt = 0;
706 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500707 return ADS7846_FILTER_OK;
708 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400709 /* Read more values that are consistent. */
710 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500711 return ADS7846_FILTER_REPEAT;
712 }
713 }
714}
715
716static int ads7846_no_filter(void *ads, int data_idx, int *val)
717{
718 return ADS7846_FILTER_OK;
719}
720
721static void ads7846_rx_val(void *ads)
722{
723 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400724 struct ads7846_packet *packet = ts->packet;
Imre Deakda970e62007-01-18 00:44:41 -0500725 struct spi_message *m;
726 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500727 int val;
728 int action;
729 int status;
730
731 m = &ts->msg[ts->msg_idx];
732 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500733
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700734 if (ts->model == 7845) {
735 val = be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
736 } else {
737 /* adjust: on-wire is a must-ignore bit, a BE12 value, then
738 * padding; built from two 8 bit values written msb-first.
739 */
740 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
741 }
Imre Deakda970e62007-01-18 00:44:41 -0500742
743 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
744 switch (action) {
745 case ADS7846_FILTER_REPEAT:
746 break;
747 case ADS7846_FILTER_IGNORE:
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400748 packet->tc.ignore = 1;
Imre Deakda970e62007-01-18 00:44:41 -0500749 /* Last message will contain ads7846_rx() as the
750 * completion function.
751 */
752 m = ts->last_msg;
753 break;
754 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400755 *(u16 *)t->rx_buf = val;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400756 packet->tc.ignore = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500757 m = &ts->msg[++ts->msg_idx];
758 break;
759 default:
760 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400761 }
Eric Miaofd746d52009-04-11 16:54:59 -0700762 ts->wait_for_sync();
Imre Deak0b7018a2006-04-11 23:42:03 -0400763 status = spi_async(ts->spi, m);
764 if (status)
765 dev_err(&ts->spi->dev, "spi_async --> %d\n",
766 status);
767}
768
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800769static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800770{
Imre Deak1936d592007-01-18 00:45:31 -0500771 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800772 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800773
Peter Zijlstraca109492008-11-25 12:43:51 +0100774 spin_lock(&ts->lock);
Imre Deakc9e617a2006-04-11 23:44:05 -0400775
Eric Miao4d5975e2008-09-10 12:06:15 -0400776 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500777 device_suspended(&ts->spi->dev))) {
778 if (ts->pendown) {
779 struct input_dev *input = ts->input;
780
781 input_report_key(input, BTN_TOUCH, 0);
782 input_report_abs(input, ABS_PRESSURE, 0);
783 input_sync(input);
784
785 ts->pendown = 0;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800786 dev_vdbg(&ts->spi->dev, "UP\n");
Imre Deak15e35892007-01-18 00:45:43 -0500787 }
788
David Brownell90845332006-05-25 18:44:20 -0700789 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400790 if (!device_suspended(&ts->spi->dev)) {
791 ts->irq_disabled = 0;
792 enable_irq(ts->spi->irq);
793 }
794 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400795 } else {
796 /* pen is still down, continue with the measurement */
797 ts->msg_idx = 0;
Eric Miaofd746d52009-04-11 16:54:59 -0700798 ts->wait_for_sync();
Imre Deakc9e617a2006-04-11 23:44:05 -0400799 status = spi_async(ts->spi, &ts->msg[0]);
800 if (status)
801 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
802 }
803
Peter Zijlstraca109492008-11-25 12:43:51 +0100804 spin_unlock(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500805 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800806}
807
David Howells7d12e782006-10-05 14:55:46 +0100808static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800809{
Imre Deak0b7018a2006-04-11 23:42:03 -0400810 struct ads7846 *ts = handle;
811 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400812
813 spin_lock_irqsave(&ts->lock, flags);
Eric Miao4d5975e2008-09-10 12:06:15 -0400814 if (likely(get_pendown_state(ts))) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400815 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700816 /* The ARM do_simple_IRQ() dispatcher doesn't act
817 * like the other dispatchers: it will report IRQs
818 * even after they've been disabled. We work around
819 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400820 */
821 ts->irq_disabled = 1;
Ben Nizette3f3e7c62009-04-15 18:57:55 -0700822 disable_irq_nosync(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400823 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500824 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800825 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400826 }
827 }
828 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400829
830 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800831}
832
833/*--------------------------------------------------------------------------*/
834
Imre Deak7de90a82006-04-11 23:43:55 -0400835/* Must be called with ts->lock held */
836static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800837{
Imre Deak7de90a82006-04-11 23:43:55 -0400838 if (ts->disabled)
839 return;
David Brownellffa458c2006-01-08 13:34:21 -0800840
Imre Deakc9e617a2006-04-11 23:44:05 -0400841 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800842
Imre Deakc9e617a2006-04-11 23:44:05 -0400843 /* are we waiting for IRQ, or polling? */
844 if (!ts->pending) {
845 ts->irq_disabled = 1;
846 disable_irq(ts->spi->irq);
847 } else {
848 /* the timer will run at least once more, and
849 * leave everything in a clean state, IRQ disabled
850 */
851 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400852 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400853 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400854 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800855 }
856 }
857
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800858 regulator_disable(ts->reg);
859
David Brownellffa458c2006-01-08 13:34:21 -0800860 /* we know the chip's in lowpower mode since we always
861 * leave it that way after every request
862 */
Imre Deak7de90a82006-04-11 23:43:55 -0400863}
864
865/* Must be called with ts->lock held */
866static void ads7846_enable(struct ads7846 *ts)
867{
868 if (!ts->disabled)
869 return;
870
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800871 regulator_enable(ts->reg);
872
Imre Deak7de90a82006-04-11 23:43:55 -0400873 ts->disabled = 0;
874 ts->irq_disabled = 0;
875 enable_irq(ts->spi->irq);
876}
877
878static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
879{
880 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
881
882 spin_lock_irq(&ts->lock);
883
David Brownellfbb38e32007-12-14 01:26:33 -0500884 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400885 ads7846_disable(ts);
886
887 spin_unlock_irq(&ts->lock);
888
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800889 if (device_may_wakeup(&ts->spi->dev))
890 enable_irq_wake(ts->spi->irq);
891
David Brownellffa458c2006-01-08 13:34:21 -0800892 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400893
David Brownellffa458c2006-01-08 13:34:21 -0800894}
895
David Brownell2e5a7bd2006-01-08 13:34:25 -0800896static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800897{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800898 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800899
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800900 if (device_may_wakeup(&ts->spi->dev))
901 disable_irq_wake(ts->spi->irq);
902
Imre Deak7de90a82006-04-11 23:43:55 -0400903 spin_lock_irq(&ts->lock);
904
David Brownellfbb38e32007-12-14 01:26:33 -0500905 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400906 ads7846_enable(ts);
907
908 spin_unlock_irq(&ts->lock);
909
David Brownellffa458c2006-01-08 13:34:21 -0800910 return 0;
911}
912
Eric Miao4d5975e2008-09-10 12:06:15 -0400913static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
914{
915 struct ads7846_platform_data *pdata = spi->dev.platform_data;
916 int err;
917
918 /* REVISIT when the irq can be triggered active-low, or if for some
919 * reason the touchscreen isn't hooked up, we don't need to access
920 * the pendown state.
921 */
922 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
923 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
924 return -EINVAL;
925 }
926
927 if (pdata->get_pendown_state) {
928 ts->get_pendown_state = pdata->get_pendown_state;
929 return 0;
930 }
931
932 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
933 if (err) {
934 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
935 pdata->gpio_pendown);
936 return err;
937 }
938
939 ts->gpio_pendown = pdata->gpio_pendown;
940 return 0;
941}
942
David Brownell2e5a7bd2006-01-08 13:34:25 -0800943static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800944{
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -0700945 struct ads7846 *ts;
946 struct ads7846_packet *packet;
947 struct input_dev *input_dev;
948 const struct ads7846_platform_data *pdata = spi->dev.platform_data;
949 struct spi_message *m;
950 struct spi_transfer *x;
951 unsigned long irq_flags;
952 int vref;
953 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800954
955 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800956 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800957 return -ENODEV;
958 }
959
960 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800961 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800962 return -ENODEV;
963 }
964
965 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500966 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800967 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500968 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800969 return -EINVAL;
970 }
971
David Brownell90845332006-05-25 18:44:20 -0700972 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
973 * that even if the hardware can do that, the SPI controller driver
974 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800975 */
David Brownell90845332006-05-25 18:44:20 -0700976 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400977 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500978 err = spi_setup(spi);
979 if (err < 0)
980 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800981
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500982 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400983 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500984 input_dev = input_allocate_device();
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400985 if (!ts || !packet || !input_dev) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500986 err = -ENOMEM;
987 goto err_free_mem;
988 }
David Brownellffa458c2006-01-08 13:34:21 -0800989
David Brownell2e5a7bd2006-01-08 13:34:25 -0800990 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800991
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400992 ts->packet = packet;
David Brownellffa458c2006-01-08 13:34:21 -0800993 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500994 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400995 ts->vref_mv = pdata->vref_mv;
Michael Roth86579a42009-05-18 16:04:44 -0700996 ts->swap_xy = pdata->swap_xy;
David Brownellffa458c2006-01-08 13:34:21 -0800997
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800998 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800999 ts->timer.function = ads7846_timer;
1000
Imre Deak7de90a82006-04-11 23:43:55 -04001001 spin_lock_init(&ts->lock);
1002
David Brownellffa458c2006-01-08 13:34:21 -08001003 ts->model = pdata->model ? : 7846;
1004 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1005 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -04001006 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -05001007
1008 if (pdata->filter != NULL) {
1009 if (pdata->filter_init != NULL) {
1010 err = pdata->filter_init(pdata, &ts->filter_data);
1011 if (err < 0)
1012 goto err_free_mem;
1013 }
1014 ts->filter = pdata->filter;
1015 ts->filter_cleanup = pdata->filter_cleanup;
1016 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -04001017 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -05001018 if (ts->debounce_max < 2)
1019 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -04001020 ts->debounce_tol = pdata->debounce_tol;
1021 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -05001022 ts->filter = ads7846_debounce;
1023 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -04001024 } else
Imre Deakda970e62007-01-18 00:44:41 -05001025 ts->filter = ads7846_no_filter;
Eric Miao4d5975e2008-09-10 12:06:15 -04001026
1027 err = setup_pendown(spi, ts);
1028 if (err)
1029 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001030
Semih Hazar1d258912007-07-18 00:36:04 -04001031 if (pdata->penirq_recheck_delay_usecs)
1032 ts->penirq_recheck_delay_usecs =
1033 pdata->penirq_recheck_delay_usecs;
1034
Eric Miaofd746d52009-04-11 16:54:59 -07001035 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1036
Kay Sieversa6c24902008-10-30 00:07:50 -04001037 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
Michael Rothb58895f2009-05-18 16:05:12 -07001038 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
David Brownellffa458c2006-01-08 13:34:21 -08001039
Michael Rothb58895f2009-05-18 16:05:12 -07001040 input_dev->name = ts->name;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001041 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -04001042 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -08001043
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001044 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1045 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001046 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -08001047 pdata->x_min ? : 0,
1048 pdata->x_max ? : MAX_12BIT,
1049 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001050 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -08001051 pdata->y_min ? : 0,
1052 pdata->y_max ? : MAX_12BIT,
1053 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001054 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -08001055 pdata->pressure_min, pdata->pressure_max, 0, 0);
1056
Imre Deakde2defd2007-01-18 00:45:21 -05001057 vref = pdata->keep_vref_on;
1058
Michael Hennerich06a09122010-03-09 20:38:45 -08001059 if (ts->model == 7873) {
1060 /* The AD7873 is almost identical to the ADS7846
1061 * keep VREF off during differential/ratiometric
1062 * conversion modes
1063 */
1064 ts->model = 7846;
1065 vref = 0;
1066 }
1067
David Brownellffa458c2006-01-08 13:34:21 -08001068 /* set up the transfers to read touchscreen state; this assumes we
1069 * use formula #2 for pressure, not #3.
1070 */
Imre Deak0b7018a2006-04-11 23:42:03 -04001071 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -08001072 x = ts->xfer;
1073
Imre Deak0b7018a2006-04-11 23:42:03 -04001074 spi_message_init(m);
1075
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001076 if (ts->model == 7845) {
1077 packet->read_y_cmd[0] = READ_Y(vref);
1078 packet->read_y_cmd[1] = 0;
1079 packet->read_y_cmd[2] = 0;
1080 x->tx_buf = &packet->read_y_cmd[0];
1081 x->rx_buf = &packet->tc.y_buf[0];
1082 x->len = 3;
1083 spi_message_add_tail(x, m);
1084 } else {
1085 /* y- still on; turn on only y+ (and ADC) */
1086 packet->read_y = READ_Y(vref);
1087 x->tx_buf = &packet->read_y;
1088 x->len = 1;
1089 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001090
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001091 x++;
1092 x->rx_buf = &packet->tc.y;
1093 x->len = 2;
1094 spi_message_add_tail(x, m);
1095 }
David Brownellffa458c2006-01-08 13:34:21 -08001096
Semih Hazare4f48862007-07-18 00:35:56 -04001097 /* the first sample after switching drivers can be low quality;
1098 * optionally discard it, using a second one after the signals
1099 * have had enough time to stabilize.
1100 */
1101 if (pdata->settle_delay_usecs) {
1102 x->delay_usecs = pdata->settle_delay_usecs;
1103
1104 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001105 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001106 x->len = 1;
1107 spi_message_add_tail(x, m);
1108
1109 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001110 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001111 x->len = 2;
1112 spi_message_add_tail(x, m);
1113 }
1114
Imre Deakda970e62007-01-18 00:44:41 -05001115 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001116 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -05001117
Imre Deak0b7018a2006-04-11 23:42:03 -04001118 m++;
1119 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -08001120
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001121 if (ts->model == 7845) {
1122 x++;
1123 packet->read_x_cmd[0] = READ_X(vref);
1124 packet->read_x_cmd[1] = 0;
1125 packet->read_x_cmd[2] = 0;
1126 x->tx_buf = &packet->read_x_cmd[0];
1127 x->rx_buf = &packet->tc.x_buf[0];
1128 x->len = 3;
1129 spi_message_add_tail(x, m);
1130 } else {
1131 /* turn y- off, x+ on, then leave in lowpower */
1132 x++;
1133 packet->read_x = READ_X(vref);
1134 x->tx_buf = &packet->read_x;
1135 x->len = 1;
1136 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001137
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001138 x++;
1139 x->rx_buf = &packet->tc.x;
1140 x->len = 2;
1141 spi_message_add_tail(x, m);
1142 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001143
Semih Hazare4f48862007-07-18 00:35:56 -04001144 /* ... maybe discard first sample ... */
1145 if (pdata->settle_delay_usecs) {
1146 x->delay_usecs = pdata->settle_delay_usecs;
1147
1148 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001149 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001150 x->len = 1;
1151 spi_message_add_tail(x, m);
1152
1153 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001154 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001155 x->len = 2;
1156 spi_message_add_tail(x, m);
1157 }
1158
Imre Deakda970e62007-01-18 00:44:41 -05001159 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001160 m->context = ts;
1161
1162 /* turn y+ off, x- on; we'll use formula #2 */
1163 if (ts->model == 7846) {
1164 m++;
1165 spi_message_init(m);
1166
1167 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001168 packet->read_z1 = READ_Z1(vref);
1169 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001170 x->len = 1;
1171 spi_message_add_tail(x, m);
1172
1173 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001174 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001175 x->len = 2;
1176 spi_message_add_tail(x, m);
1177
Semih Hazare4f48862007-07-18 00:35:56 -04001178 /* ... maybe discard first sample ... */
1179 if (pdata->settle_delay_usecs) {
1180 x->delay_usecs = pdata->settle_delay_usecs;
1181
1182 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001183 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001184 x->len = 1;
1185 spi_message_add_tail(x, m);
1186
1187 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001188 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001189 x->len = 2;
1190 spi_message_add_tail(x, m);
1191 }
1192
Imre Deakda970e62007-01-18 00:44:41 -05001193 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001194 m->context = ts;
1195
1196 m++;
1197 spi_message_init(m);
1198
1199 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001200 packet->read_z2 = READ_Z2(vref);
1201 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001202 x->len = 1;
1203 spi_message_add_tail(x, m);
1204
1205 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001206 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001207 x->len = 2;
1208 spi_message_add_tail(x, m);
1209
Semih Hazare4f48862007-07-18 00:35:56 -04001210 /* ... maybe discard first sample ... */
1211 if (pdata->settle_delay_usecs) {
1212 x->delay_usecs = pdata->settle_delay_usecs;
1213
1214 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001215 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001216 x->len = 1;
1217 spi_message_add_tail(x, m);
1218
1219 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001220 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001221 x->len = 2;
1222 spi_message_add_tail(x, m);
1223 }
1224
Imre Deakda970e62007-01-18 00:44:41 -05001225 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001226 m->context = ts;
1227 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001228
1229 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001230 m++;
1231 spi_message_init(m);
1232
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001233 if (ts->model == 7845) {
1234 x++;
1235 packet->pwrdown_cmd[0] = PWRDOWN;
1236 packet->pwrdown_cmd[1] = 0;
1237 packet->pwrdown_cmd[2] = 0;
1238 x->tx_buf = &packet->pwrdown_cmd[0];
1239 x->len = 3;
1240 } else {
1241 x++;
1242 packet->pwrdown = PWRDOWN;
1243 x->tx_buf = &packet->pwrdown;
1244 x->len = 1;
1245 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001246
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001247 x++;
1248 x->rx_buf = &packet->dummy;
1249 x->len = 2;
1250 }
1251
David Brownelld93f70b2006-02-15 00:49:35 -05001252 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001253 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001254
Imre Deak0b7018a2006-04-11 23:42:03 -04001255 m->complete = ads7846_rx;
1256 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001257
Imre Deakd5b415c2006-04-26 00:13:18 -04001258 ts->last_msg = m;
1259
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001260 ts->reg = regulator_get(&spi->dev, "vcc");
1261 if (IS_ERR(ts->reg)) {
Kevin Hilman067fb2f2010-05-26 23:30:55 -07001262 err = PTR_ERR(ts->reg);
Dmitry Torokhov56960b32010-06-02 00:40:06 -07001263 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001264 goto err_free_gpio;
1265 }
1266
1267 err = regulator_enable(ts->reg);
1268 if (err) {
1269 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1270 goto err_put_regulator;
1271 }
1272
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001273 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
Anatolij Gustschin78043022010-06-28 01:25:19 -07001274
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001275 err = request_irq(spi->irq, ads7846_irq, irq_flags,
1276 spi->dev.driver->name, ts);
1277
1278 if (err && !pdata->irq_flags) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001279 dev_info(&spi->dev,
1280 "trying pin change workaround on irq %d\n", spi->irq);
1281 err = request_irq(spi->irq, ads7846_irq,
1282 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1283 spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001284 }
1285
1286 if (err) {
1287 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1288 goto err_disable_regulator;
David Brownellffa458c2006-01-08 13:34:21 -08001289 }
David Brownellffa458c2006-01-08 13:34:21 -08001290
David Brownell2c8dc072007-01-18 00:45:48 -05001291 err = ads784x_hwmon_register(spi, ts);
1292 if (err)
1293 goto err_free_irq;
1294
David Brownell2e5a7bd2006-01-08 13:34:25 -08001295 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001296
David Brownell2c8dc072007-01-18 00:45:48 -05001297 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001298 * the touchscreen, in case it's not connected.
1299 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001300 if (ts->model == 7845)
1301 ads7845_read12_ser(&spi->dev, PWRDOWN);
1302 else
1303 (void) ads7846_read12_ser(&spi->dev,
1304 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
David Brownellffa458c2006-01-08 13:34:21 -08001305
David Brownell2c8dc072007-01-18 00:45:48 -05001306 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001307 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001308 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001309
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001310 err = input_register_device(input_dev);
1311 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001312 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001313
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001314 device_init_wakeup(&spi->dev, pdata->wakeup);
1315
David Brownellffa458c2006-01-08 13:34:21 -08001316 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001317
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001318 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001319 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1320 err_remove_hwmon:
1321 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001322 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001323 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001324 err_disable_regulator:
1325 regulator_disable(ts->reg);
1326 err_put_regulator:
1327 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001328 err_free_gpio:
1329 if (ts->gpio_pendown != -1)
1330 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001331 err_cleanup_filter:
1332 if (ts->filter_cleanup)
1333 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001334 err_free_mem:
1335 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001336 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001337 kfree(ts);
1338 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001339}
1340
David Brownell2e5a7bd2006-01-08 13:34:25 -08001341static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001342{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001343 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001344
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001345 device_init_wakeup(&spi->dev, false);
1346
David Brownell2c8dc072007-01-18 00:45:48 -05001347 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001348 input_unregister_device(ts->input);
1349
David Brownell2e5a7bd2006-01-08 13:34:25 -08001350 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001351
David Brownell2c8dc072007-01-18 00:45:48 -05001352 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001353
Imre Deak7de90a82006-04-11 23:43:55 -04001354 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001355 /* suspend left the IRQ disabled */
1356 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001357
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001358 regulator_disable(ts->reg);
1359 regulator_put(ts->reg);
1360
Eric Miao4d5975e2008-09-10 12:06:15 -04001361 if (ts->gpio_pendown != -1)
1362 gpio_free(ts->gpio_pendown);
1363
Imre Deakda970e62007-01-18 00:44:41 -05001364 if (ts->filter_cleanup)
1365 ts->filter_cleanup(ts->filter_data);
1366
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001367 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001368 kfree(ts);
1369
David Brownell2e5a7bd2006-01-08 13:34:25 -08001370 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001371 return 0;
1372}
1373
David Brownell2e5a7bd2006-01-08 13:34:25 -08001374static struct spi_driver ads7846_driver = {
1375 .driver = {
1376 .name = "ads7846",
1377 .bus = &spi_bus_type,
1378 .owner = THIS_MODULE,
1379 },
David Brownellffa458c2006-01-08 13:34:21 -08001380 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001381 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001382 .suspend = ads7846_suspend,
1383 .resume = ads7846_resume,
1384};
1385
1386static int __init ads7846_init(void)
1387{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001388 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001389}
1390module_init(ads7846_init);
1391
1392static void __exit ads7846_exit(void)
1393{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001394 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001395}
1396module_exit(ads7846_exit);
1397
1398MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1399MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001400MODULE_ALIAS("spi:ads7846");