blob: 8b05d8e975437501d73378d401e90a857db72d40 [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.
David Brownellffa458c2006-01-08 13:34:21 -080039 *
Imre Deak7de90a82006-04-11 23:43:55 -040040 * IRQ handling needs a workaround because of a shortcoming in handling
41 * edge triggered IRQs on some platforms like the OMAP1/2. These
42 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
43 * have to maintain our own SW IRQ disabled status. This should be
44 * removed as soon as the affected platform's IRQ handling is fixed.
45 *
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080046 * App note sbaa036 talks in more detail about accurate sampling...
David Brownellffa458c2006-01-08 13:34:21 -080047 * that ought to help in situations like LCDs inducing noise (which
48 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040049 * This driver tries to utilize the measures described in the app
50 * note. The strength of filtering can be set in the board-* specific
51 * files.
David Brownellffa458c2006-01-08 13:34:21 -080052 */
53
Imre Deak1936d592007-01-18 00:45:31 -050054#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
55#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080056
David Brownelld93f70b2006-02-15 00:49:35 -050057/* this driver doesn't aim at the peak continuous sample rate */
58#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
59
David Brownellffa458c2006-01-08 13:34:21 -080060struct ts_event {
61 /* For portability, we can't read 12 bit values using SPI (which
62 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050063 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050064 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080065 */
Imre Deakda970e62007-01-18 00:44:41 -050066 u16 x;
67 u16 y;
68 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050069 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080070};
71
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040072/*
73 * We allocate this separately to avoid cache line sharing issues when
74 * driver is used with DMA-based SPI controllers (like atmel_spi) on
75 * systems where main memory is not DMA-coherent (most non-x86 boards).
76 */
77struct ads7846_packet {
78 u8 read_x, read_y, read_z1, read_z2, pwrdown;
79 u16 dummy; /* for the pwrdown read */
80 struct ts_event tc;
81};
82
David Brownellffa458c2006-01-08 13:34:21 -080083struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050084 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080085 char phys[32];
Michael Rothb58895f2009-05-18 16:05:12 -070086 char name[32];
David Brownellffa458c2006-01-08 13:34:21 -080087
88 struct spi_device *spi;
Grazvydas Ignotas91143372010-02-25 02:04:56 -080089 struct regulator *reg;
David Brownell2c8dc072007-01-18 00:45:48 -050090
91#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050092 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070093 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050094#endif
95
David Brownellffa458c2006-01-08 13:34:21 -080096 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -040097 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080098 u16 vref_delay_usecs;
99 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400100 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800101
Michael Roth86579a42009-05-18 16:04:44 -0700102 bool swap_xy;
103
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400104 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800105
Semih Hazare4f48862007-07-18 00:35:56 -0400106 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400107 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400108 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400109 int msg_idx;
110 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400111 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400112 int last_read;
113
114 u16 debounce_max;
115 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400116 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800117
Semih Hazar1d258912007-07-18 00:36:04 -0400118 u16 penirq_recheck_delay_usecs;
119
David Brownellffa458c2006-01-08 13:34:21 -0800120 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500121 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800122 unsigned pendown:1; /* P: lock */
123 unsigned pending:1; /* P: lock */
124// FIXME remove "irq_disabled"
125 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400126 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500127 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400128
Imre Deakda970e62007-01-18 00:44:41 -0500129 int (*filter)(void *data, int data_idx, int *val);
130 void *filter_data;
131 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400132 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400133 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700134
135 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800136};
137
138/* leave chip selected when we're done, for quicker re-select? */
139#if 0
140#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
141#else
142#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
143#endif
144
145/*--------------------------------------------------------------------------*/
146
147/* The ADS7846 has touchscreen and other sensors.
148 * Earlier ads784x chips are somewhat compatible.
149 */
150#define ADS_START (1 << 7)
151#define ADS_A2A1A0_d_y (1 << 4) /* differential */
152#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
153#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
154#define ADS_A2A1A0_d_x (5 << 4) /* differential */
155#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
156#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
157#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
158#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
159#define ADS_8_BIT (1 << 3)
160#define ADS_12_BIT (0 << 3)
161#define ADS_SER (1 << 2) /* non-differential */
162#define ADS_DFR (0 << 2) /* differential */
163#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
164#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
165#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
166#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
167
168#define MAX_12BIT ((1<<12)-1)
169
170/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500171#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
172 | ADS_12_BIT | ADS_DFR | \
173 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800174
Imre Deakde2defd2007-01-18 00:45:21 -0500175#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
176#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
177#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400178
Imre Deakde2defd2007-01-18 00:45:21 -0500179#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
180#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800181
182/* single-ended samples need to first power up reference voltage;
183 * we leave both ADC and VREF powered
184 */
185#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
186 | ADS_12_BIT | ADS_SER)
187
Imre Deakde2defd2007-01-18 00:45:21 -0500188#define REF_ON (READ_12BIT_DFR(x, 1, 1))
189#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800190
191/*--------------------------------------------------------------------------*/
192
193/*
194 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500195 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
196 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800197 */
198
199struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500200 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800201 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500202 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800203 u16 scratch;
204 __be16 sample;
205 struct spi_message msg;
206 struct spi_transfer xfer[6];
207};
208
Imre Deak7de90a82006-04-11 23:43:55 -0400209static void ads7846_enable(struct ads7846 *ts);
210static void ads7846_disable(struct ads7846 *ts);
211
Imre Deakc9e617a2006-04-11 23:44:05 -0400212static int device_suspended(struct device *dev)
213{
214 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500215 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400216}
217
David Brownellffa458c2006-01-08 13:34:21 -0800218static int ads7846_read12_ser(struct device *dev, unsigned command)
219{
220 struct spi_device *spi = to_spi_device(dev);
221 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800222 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800223 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500224 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800225
226 if (!req)
227 return -ENOMEM;
228
Imre Deak0b7018a2006-04-11 23:42:03 -0400229 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800230
David Brownell2c8dc072007-01-18 00:45:48 -0500231 /* FIXME boards with ads7846 might use external vref instead ... */
232 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800233
David Brownell2c8dc072007-01-18 00:45:48 -0500234 /* maybe turn on internal vREF, and let it settle */
235 if (use_internal) {
236 req->ref_on = REF_ON;
237 req->xfer[0].tx_buf = &req->ref_on;
238 req->xfer[0].len = 1;
239 spi_message_add_tail(&req->xfer[0], &req->msg);
240
241 req->xfer[1].rx_buf = &req->scratch;
242 req->xfer[1].len = 2;
243
244 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
245 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
246 spi_message_add_tail(&req->xfer[1], &req->msg);
247 }
David Brownellffa458c2006-01-08 13:34:21 -0800248
249 /* take sample */
250 req->command = (u8) command;
251 req->xfer[2].tx_buf = &req->command;
252 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500253 spi_message_add_tail(&req->xfer[2], &req->msg);
254
David Brownellffa458c2006-01-08 13:34:21 -0800255 req->xfer[3].rx_buf = &req->sample;
256 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500257 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800258
259 /* REVISIT: take a few more samples, and compare ... */
260
Nicolas Ferre969111e2007-02-28 23:51:03 -0500261 /* converter in low power mode & enable PENIRQ */
262 req->ref_off = PWRDOWN;
263 req->xfer[4].tx_buf = &req->ref_off;
264 req->xfer[4].len = 1;
265 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800266
Nicolas Ferre969111e2007-02-28 23:51:03 -0500267 req->xfer[5].rx_buf = &req->scratch;
268 req->xfer[5].len = 2;
269 CS_CHANGE(req->xfer[5]);
270 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800271
Imre Deakc9e617a2006-04-11 23:44:05 -0400272 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800273 disable_irq(spi->irq);
274 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400275 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800276 enable_irq(spi->irq);
277
Marc Pignatc24b2602007-12-04 23:45:11 -0800278 if (status == 0) {
279 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400280 status = be16_to_cpu(req->sample);
281 status = status >> 3;
282 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800283 }
David Brownell90845332006-05-25 18:44:20 -0700284
285 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400286 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800287}
288
David Brownell2c8dc072007-01-18 00:45:48 -0500289#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
290
291#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800292name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
293{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500294 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800295 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500296 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800297 if (v < 0) \
298 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500299 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800300} \
301static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
302
David Brownell2c8dc072007-01-18 00:45:48 -0500303
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400304/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500305 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
306 * accuracy scheme without calibration data. For now we won't try either;
307 * userspace sees raw sensor values, and must scale/calibrate appropriately.
308 */
309static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
310{
311 return v;
312}
313
314SHOW(temp0, temp0, null_adjust) /* temp1_input */
315SHOW(temp1, temp1, null_adjust) /* temp2_input */
316
317
318/* sysfs conventions report voltages in millivolts. We can convert voltages
319 * if we know vREF. userspace may need to scale vAUX to match the board's
320 * external resistors; we assume that vBATT only uses the internal ones.
321 */
322static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
323{
324 unsigned retval = v;
325
326 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400327 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500328 retval = retval >> 12;
329 return retval;
330}
331
332static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
333{
334 unsigned retval = vaux_adjust(ts, v);
335
336 /* ads7846 has a resistor ladder to scale this signal down */
337 if (ts->model == 7846)
338 retval *= 4;
339 return retval;
340}
341
342SHOW(in0_input, vaux, vaux_adjust)
343SHOW(in1_input, vbatt, vbatt_adjust)
344
345
346static struct attribute *ads7846_attributes[] = {
347 &dev_attr_temp0.attr,
348 &dev_attr_temp1.attr,
349 &dev_attr_in0_input.attr,
350 &dev_attr_in1_input.attr,
351 NULL,
352};
353
354static struct attribute_group ads7846_attr_group = {
355 .attrs = ads7846_attributes,
356};
357
358static struct attribute *ads7843_attributes[] = {
359 &dev_attr_in0_input.attr,
360 &dev_attr_in1_input.attr,
361 NULL,
362};
363
364static struct attribute_group ads7843_attr_group = {
365 .attrs = ads7843_attributes,
366};
367
368static struct attribute *ads7845_attributes[] = {
369 &dev_attr_in0_input.attr,
370 NULL,
371};
372
373static struct attribute_group ads7845_attr_group = {
374 .attrs = ads7845_attributes,
375};
376
377static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
378{
Tony Jones1beeffe2007-08-20 13:46:20 -0700379 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500380 int err;
381
382 /* hwmon sensors need a reference voltage */
383 switch (ts->model) {
384 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400385 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500386 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400387 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500388 }
389 break;
390 case 7845:
391 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400392 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500393 dev_warn(&spi->dev,
394 "external vREF for ADS%d not specified\n",
395 ts->model);
396 return 0;
397 }
398 break;
399 }
400
401 /* different chips have different sensor groups */
402 switch (ts->model) {
403 case 7846:
404 ts->attr_group = &ads7846_attr_group;
405 break;
406 case 7845:
407 ts->attr_group = &ads7845_attr_group;
408 break;
409 case 7843:
410 ts->attr_group = &ads7843_attr_group;
411 break;
412 default:
413 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
414 return 0;
415 }
416
417 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
418 if (err)
419 return err;
420
421 hwmon = hwmon_device_register(&spi->dev);
422 if (IS_ERR(hwmon)) {
423 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
424 return PTR_ERR(hwmon);
425 }
426
427 ts->hwmon = hwmon;
428 return 0;
429}
430
431static void ads784x_hwmon_unregister(struct spi_device *spi,
432 struct ads7846 *ts)
433{
434 if (ts->hwmon) {
435 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
436 hwmon_device_unregister(ts->hwmon);
437 }
438}
439
440#else
441static inline int ads784x_hwmon_register(struct spi_device *spi,
442 struct ads7846 *ts)
443{
444 return 0;
445}
446
447static inline void ads784x_hwmon_unregister(struct spi_device *spi,
448 struct ads7846 *ts)
449{
450}
451#endif
David Brownellffa458c2006-01-08 13:34:21 -0800452
Imre Deak438f2a72006-04-11 23:41:32 -0400453static int is_pen_down(struct device *dev)
454{
David Brownell2c8dc072007-01-18 00:45:48 -0500455 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400456
457 return ts->pendown;
458}
459
460static ssize_t ads7846_pen_down_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
463 return sprintf(buf, "%u\n", is_pen_down(dev));
464}
465
466static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
467
Imre Deak7de90a82006-04-11 23:43:55 -0400468static ssize_t ads7846_disable_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
470{
471 struct ads7846 *ts = dev_get_drvdata(dev);
472
473 return sprintf(buf, "%u\n", ts->disabled);
474}
475
476static ssize_t ads7846_disable_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 struct ads7846 *ts = dev_get_drvdata(dev);
Harvey Harrison3a0c58d2008-12-23 04:09:28 -0500481 unsigned long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400482
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400483 if (strict_strtoul(buf, 10, &i))
484 return -EINVAL;
485
Imre Deak7de90a82006-04-11 23:43:55 -0400486 spin_lock_irq(&ts->lock);
487
488 if (i)
489 ads7846_disable(ts);
490 else
491 ads7846_enable(ts);
492
493 spin_unlock_irq(&ts->lock);
494
495 return count;
496}
497
498static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
499
David Brownell2c8dc072007-01-18 00:45:48 -0500500static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500501 &dev_attr_pen_down.attr,
502 &dev_attr_disable.attr,
503 NULL,
504};
505
David Brownell2c8dc072007-01-18 00:45:48 -0500506static struct attribute_group ads784x_attr_group = {
507 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500508};
509
David Brownellffa458c2006-01-08 13:34:21 -0800510/*--------------------------------------------------------------------------*/
511
Eric Miao4d5975e2008-09-10 12:06:15 -0400512static int get_pendown_state(struct ads7846 *ts)
513{
514 if (ts->get_pendown_state)
515 return ts->get_pendown_state();
516
517 return !gpio_get_value(ts->gpio_pendown);
518}
519
Eric Miaofd746d52009-04-11 16:54:59 -0700520static void null_wait_for_sync(void)
521{
522}
523
David Brownellffa458c2006-01-08 13:34:21 -0800524/*
525 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
526 * to retrieve touchscreen status.
527 *
528 * The SPI transfer completion callback does the real work. It reports
529 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
530 */
531
532static void ads7846_rx(void *ads)
533{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500534 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400535 struct ads7846_packet *packet = ts->packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500536 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500537 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800538
Imre Deakda970e62007-01-18 00:44:41 -0500539 /* ads7846_rx_val() did in-place conversion (including byteswap) from
540 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800541 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400542 x = packet->tc.x;
543 y = packet->tc.y;
544 z1 = packet->tc.z1;
545 z2 = packet->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800546
547 /* range filtering */
548 if (x == MAX_12BIT)
549 x = 0;
550
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400551 if (ts->model == 7843) {
552 Rt = ts->pressure_max / 2;
553 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800554 /* compute touch pressure resistance using equation #2 */
555 Rt = z2;
556 Rt -= z1;
557 Rt *= x;
558 Rt *= ts->x_plate_ohms;
559 Rt /= z1;
560 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400561 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800562 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400563 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500564
Imre Deakd5b415c2006-04-26 00:13:18 -0400565 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500566 * the maximum. Don't report it to user space, repeat at least
567 * once more the measurement
568 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400569 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800570 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
571 packet->tc.ignore, Rt);
Imre Deak1936d592007-01-18 00:45:31 -0500572 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800573 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400574 return;
575 }
576
Semih Hazar1d258912007-07-18 00:36:04 -0400577 /* Maybe check the pendown state before reporting. This discards
578 * false readings when the pen is lifted.
579 */
580 if (ts->penirq_recheck_delay_usecs) {
581 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400582 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400583 Rt = 0;
584 }
585
Imre Deak15e35892007-01-18 00:45:43 -0500586 /* NOTE: We can't rely on the pressure to determine the pen down
587 * state, even this controller has a pressure sensor. The pressure
588 * value can fluctuate for quite a while after lifting the pen and
589 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800590 *
Imre Deak15e35892007-01-18 00:45:43 -0500591 * The only safe way to check for the pen up condition is in the
592 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800593 */
David Brownellffa458c2006-01-08 13:34:21 -0800594 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500595 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400596
Imre Deak15e35892007-01-18 00:45:43 -0500597 if (!ts->pendown) {
598 input_report_key(input, BTN_TOUCH, 1);
599 ts->pendown = 1;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800600 dev_vdbg(&ts->spi->dev, "DOWN\n");
Imre Deak15e35892007-01-18 00:45:43 -0500601 }
Michael Roth86579a42009-05-18 16:04:44 -0700602
603 if (ts->swap_xy)
604 swap(x, y);
605
Imre Deak15e35892007-01-18 00:45:43 -0500606 input_report_abs(input, ABS_X, x);
607 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800608 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800609
Imre Deak15e35892007-01-18 00:45:43 -0500610 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800611 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500612 }
David Brownellffa458c2006-01-08 13:34:21 -0800613
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800614 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
615 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800616}
617
Imre Deakda970e62007-01-18 00:44:41 -0500618static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400619{
620 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400621
Imre Deakda970e62007-01-18 00:44:41 -0500622 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
623 /* Start over collecting consistent readings. */
624 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400625 /* Repeat it, if this was the first read or the read
626 * wasn't consistent enough. */
627 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500628 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400629 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500630 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400631 } else {
632 /* Maximum number of debouncing reached and still
633 * not enough number of consistent readings. Abort
634 * the whole sample, repeat it in the next sampling
635 * period.
636 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400637 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500638 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400639 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400640 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400641 if (++ts->read_rep > ts->debounce_rep) {
642 /* Got a good reading for this coordinate,
643 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400644 ts->read_cnt = 0;
645 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500646 return ADS7846_FILTER_OK;
647 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400648 /* Read more values that are consistent. */
649 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500650 return ADS7846_FILTER_REPEAT;
651 }
652 }
653}
654
655static int ads7846_no_filter(void *ads, int data_idx, int *val)
656{
657 return ADS7846_FILTER_OK;
658}
659
660static void ads7846_rx_val(void *ads)
661{
662 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400663 struct ads7846_packet *packet = ts->packet;
Imre Deakda970e62007-01-18 00:44:41 -0500664 struct spi_message *m;
665 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500666 int val;
667 int action;
668 int status;
669
670 m = &ts->msg[ts->msg_idx];
671 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500672
673 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
674 * built from two 8 bit values written msb-first.
675 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400676 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500677
678 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
679 switch (action) {
680 case ADS7846_FILTER_REPEAT:
681 break;
682 case ADS7846_FILTER_IGNORE:
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400683 packet->tc.ignore = 1;
Imre Deakda970e62007-01-18 00:44:41 -0500684 /* Last message will contain ads7846_rx() as the
685 * completion function.
686 */
687 m = ts->last_msg;
688 break;
689 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400690 *(u16 *)t->rx_buf = val;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400691 packet->tc.ignore = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500692 m = &ts->msg[++ts->msg_idx];
693 break;
694 default:
695 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400696 }
Eric Miaofd746d52009-04-11 16:54:59 -0700697 ts->wait_for_sync();
Imre Deak0b7018a2006-04-11 23:42:03 -0400698 status = spi_async(ts->spi, m);
699 if (status)
700 dev_err(&ts->spi->dev, "spi_async --> %d\n",
701 status);
702}
703
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800704static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800705{
Imre Deak1936d592007-01-18 00:45:31 -0500706 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800707 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800708
Peter Zijlstraca109492008-11-25 12:43:51 +0100709 spin_lock(&ts->lock);
Imre Deakc9e617a2006-04-11 23:44:05 -0400710
Eric Miao4d5975e2008-09-10 12:06:15 -0400711 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500712 device_suspended(&ts->spi->dev))) {
713 if (ts->pendown) {
714 struct input_dev *input = ts->input;
715
716 input_report_key(input, BTN_TOUCH, 0);
717 input_report_abs(input, ABS_PRESSURE, 0);
718 input_sync(input);
719
720 ts->pendown = 0;
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800721 dev_vdbg(&ts->spi->dev, "UP\n");
Imre Deak15e35892007-01-18 00:45:43 -0500722 }
723
David Brownell90845332006-05-25 18:44:20 -0700724 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400725 if (!device_suspended(&ts->spi->dev)) {
726 ts->irq_disabled = 0;
727 enable_irq(ts->spi->irq);
728 }
729 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400730 } else {
731 /* pen is still down, continue with the measurement */
732 ts->msg_idx = 0;
Eric Miaofd746d52009-04-11 16:54:59 -0700733 ts->wait_for_sync();
Imre Deakc9e617a2006-04-11 23:44:05 -0400734 status = spi_async(ts->spi, &ts->msg[0]);
735 if (status)
736 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
737 }
738
Peter Zijlstraca109492008-11-25 12:43:51 +0100739 spin_unlock(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500740 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800741}
742
David Howells7d12e782006-10-05 14:55:46 +0100743static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800744{
Imre Deak0b7018a2006-04-11 23:42:03 -0400745 struct ads7846 *ts = handle;
746 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400747
748 spin_lock_irqsave(&ts->lock, flags);
Eric Miao4d5975e2008-09-10 12:06:15 -0400749 if (likely(get_pendown_state(ts))) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400750 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700751 /* The ARM do_simple_IRQ() dispatcher doesn't act
752 * like the other dispatchers: it will report IRQs
753 * even after they've been disabled. We work around
754 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400755 */
756 ts->irq_disabled = 1;
Ben Nizette3f3e7c62009-04-15 18:57:55 -0700757 disable_irq_nosync(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400758 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500759 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800760 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400761 }
762 }
763 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400764
765 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800766}
767
768/*--------------------------------------------------------------------------*/
769
Imre Deak7de90a82006-04-11 23:43:55 -0400770/* Must be called with ts->lock held */
771static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800772{
Imre Deak7de90a82006-04-11 23:43:55 -0400773 if (ts->disabled)
774 return;
David Brownellffa458c2006-01-08 13:34:21 -0800775
Imre Deakc9e617a2006-04-11 23:44:05 -0400776 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800777
Imre Deakc9e617a2006-04-11 23:44:05 -0400778 /* are we waiting for IRQ, or polling? */
779 if (!ts->pending) {
780 ts->irq_disabled = 1;
781 disable_irq(ts->spi->irq);
782 } else {
783 /* the timer will run at least once more, and
784 * leave everything in a clean state, IRQ disabled
785 */
786 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400787 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400788 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400789 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800790 }
791 }
792
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800793 regulator_disable(ts->reg);
794
David Brownellffa458c2006-01-08 13:34:21 -0800795 /* we know the chip's in lowpower mode since we always
796 * leave it that way after every request
797 */
Imre Deak7de90a82006-04-11 23:43:55 -0400798}
799
800/* Must be called with ts->lock held */
801static void ads7846_enable(struct ads7846 *ts)
802{
803 if (!ts->disabled)
804 return;
805
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800806 regulator_enable(ts->reg);
807
Imre Deak7de90a82006-04-11 23:43:55 -0400808 ts->disabled = 0;
809 ts->irq_disabled = 0;
810 enable_irq(ts->spi->irq);
811}
812
813static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
814{
815 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
816
817 spin_lock_irq(&ts->lock);
818
David Brownellfbb38e32007-12-14 01:26:33 -0500819 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400820 ads7846_disable(ts);
821
822 spin_unlock_irq(&ts->lock);
823
David Brownellffa458c2006-01-08 13:34:21 -0800824 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400825
David Brownellffa458c2006-01-08 13:34:21 -0800826}
827
David Brownell2e5a7bd2006-01-08 13:34:25 -0800828static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800829{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800830 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800831
Imre Deak7de90a82006-04-11 23:43:55 -0400832 spin_lock_irq(&ts->lock);
833
David Brownellfbb38e32007-12-14 01:26:33 -0500834 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400835 ads7846_enable(ts);
836
837 spin_unlock_irq(&ts->lock);
838
David Brownellffa458c2006-01-08 13:34:21 -0800839 return 0;
840}
841
Eric Miao4d5975e2008-09-10 12:06:15 -0400842static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
843{
844 struct ads7846_platform_data *pdata = spi->dev.platform_data;
845 int err;
846
847 /* REVISIT when the irq can be triggered active-low, or if for some
848 * reason the touchscreen isn't hooked up, we don't need to access
849 * the pendown state.
850 */
851 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
852 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
853 return -EINVAL;
854 }
855
856 if (pdata->get_pendown_state) {
857 ts->get_pendown_state = pdata->get_pendown_state;
858 return 0;
859 }
860
861 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
862 if (err) {
863 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
864 pdata->gpio_pendown);
865 return err;
866 }
867
868 ts->gpio_pendown = pdata->gpio_pendown;
869 return 0;
870}
871
David Brownell2e5a7bd2006-01-08 13:34:25 -0800872static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800873{
David Brownellffa458c2006-01-08 13:34:21 -0800874 struct ads7846 *ts;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400875 struct ads7846_packet *packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500876 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800877 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400878 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800879 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500880 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500881 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800882
883 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800884 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800885 return -ENODEV;
886 }
887
888 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800889 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800890 return -ENODEV;
891 }
892
893 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500894 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800895 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500896 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800897 return -EINVAL;
898 }
899
David Brownell90845332006-05-25 18:44:20 -0700900 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
901 * that even if the hardware can do that, the SPI controller driver
902 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800903 */
David Brownell90845332006-05-25 18:44:20 -0700904 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400905 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500906 err = spi_setup(spi);
907 if (err < 0)
908 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800909
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500910 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400911 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500912 input_dev = input_allocate_device();
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400913 if (!ts || !packet || !input_dev) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500914 err = -ENOMEM;
915 goto err_free_mem;
916 }
David Brownellffa458c2006-01-08 13:34:21 -0800917
David Brownell2e5a7bd2006-01-08 13:34:25 -0800918 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800919
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400920 ts->packet = packet;
David Brownellffa458c2006-01-08 13:34:21 -0800921 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500922 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400923 ts->vref_mv = pdata->vref_mv;
Michael Roth86579a42009-05-18 16:04:44 -0700924 ts->swap_xy = pdata->swap_xy;
David Brownellffa458c2006-01-08 13:34:21 -0800925
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800926 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800927 ts->timer.function = ads7846_timer;
928
Imre Deak7de90a82006-04-11 23:43:55 -0400929 spin_lock_init(&ts->lock);
930
David Brownellffa458c2006-01-08 13:34:21 -0800931 ts->model = pdata->model ? : 7846;
932 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
933 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400934 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500935
936 if (pdata->filter != NULL) {
937 if (pdata->filter_init != NULL) {
938 err = pdata->filter_init(pdata, &ts->filter_data);
939 if (err < 0)
940 goto err_free_mem;
941 }
942 ts->filter = pdata->filter;
943 ts->filter_cleanup = pdata->filter_cleanup;
944 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400945 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500946 if (ts->debounce_max < 2)
947 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400948 ts->debounce_tol = pdata->debounce_tol;
949 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500950 ts->filter = ads7846_debounce;
951 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400952 } else
Imre Deakda970e62007-01-18 00:44:41 -0500953 ts->filter = ads7846_no_filter;
Eric Miao4d5975e2008-09-10 12:06:15 -0400954
955 err = setup_pendown(spi, ts);
956 if (err)
957 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800958
Semih Hazar1d258912007-07-18 00:36:04 -0400959 if (pdata->penirq_recheck_delay_usecs)
960 ts->penirq_recheck_delay_usecs =
961 pdata->penirq_recheck_delay_usecs;
962
Eric Miaofd746d52009-04-11 16:54:59 -0700963 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
964
Kay Sieversa6c24902008-10-30 00:07:50 -0400965 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
Michael Rothb58895f2009-05-18 16:05:12 -0700966 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
David Brownellffa458c2006-01-08 13:34:21 -0800967
Michael Rothb58895f2009-05-18 16:05:12 -0700968 input_dev->name = ts->name;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500969 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400970 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800971
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700972 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
973 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500974 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800975 pdata->x_min ? : 0,
976 pdata->x_max ? : MAX_12BIT,
977 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500978 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800979 pdata->y_min ? : 0,
980 pdata->y_max ? : MAX_12BIT,
981 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500982 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800983 pdata->pressure_min, pdata->pressure_max, 0, 0);
984
Imre Deakde2defd2007-01-18 00:45:21 -0500985 vref = pdata->keep_vref_on;
986
David Brownellffa458c2006-01-08 13:34:21 -0800987 /* set up the transfers to read touchscreen state; this assumes we
988 * use formula #2 for pressure, not #3.
989 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400990 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800991 x = ts->xfer;
992
Imre Deak0b7018a2006-04-11 23:42:03 -0400993 spi_message_init(m);
994
David Brownellffa458c2006-01-08 13:34:21 -0800995 /* y- still on; turn on only y+ (and ADC) */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400996 packet->read_y = READ_Y(vref);
997 x->tx_buf = &packet->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800998 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400999 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001000
David Brownellffa458c2006-01-08 13:34:21 -08001001 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001002 x->rx_buf = &packet->tc.y;
David Brownellffa458c2006-01-08 13:34:21 -08001003 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001004 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001005
Semih Hazare4f48862007-07-18 00:35:56 -04001006 /* the first sample after switching drivers can be low quality;
1007 * optionally discard it, using a second one after the signals
1008 * have had enough time to stabilize.
1009 */
1010 if (pdata->settle_delay_usecs) {
1011 x->delay_usecs = pdata->settle_delay_usecs;
1012
1013 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001014 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001015 x->len = 1;
1016 spi_message_add_tail(x, m);
1017
1018 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001019 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001020 x->len = 2;
1021 spi_message_add_tail(x, m);
1022 }
1023
Imre Deakda970e62007-01-18 00:44:41 -05001024 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001025 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -05001026
Imre Deak0b7018a2006-04-11 23:42:03 -04001027 m++;
1028 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -08001029
1030 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -05001031 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001032 packet->read_x = READ_X(vref);
1033 x->tx_buf = &packet->read_x;
David Brownellffa458c2006-01-08 13:34:21 -08001034 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001035 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001036
David Brownellffa458c2006-01-08 13:34:21 -08001037 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001038 x->rx_buf = &packet->tc.x;
David Brownellffa458c2006-01-08 13:34:21 -08001039 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001040 spi_message_add_tail(x, m);
1041
Semih Hazare4f48862007-07-18 00:35:56 -04001042 /* ... maybe discard first sample ... */
1043 if (pdata->settle_delay_usecs) {
1044 x->delay_usecs = pdata->settle_delay_usecs;
1045
1046 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001047 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001048 x->len = 1;
1049 spi_message_add_tail(x, m);
1050
1051 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001052 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001053 x->len = 2;
1054 spi_message_add_tail(x, m);
1055 }
1056
Imre Deakda970e62007-01-18 00:44:41 -05001057 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001058 m->context = ts;
1059
1060 /* turn y+ off, x- on; we'll use formula #2 */
1061 if (ts->model == 7846) {
1062 m++;
1063 spi_message_init(m);
1064
1065 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001066 packet->read_z1 = READ_Z1(vref);
1067 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001068 x->len = 1;
1069 spi_message_add_tail(x, m);
1070
1071 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001072 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001073 x->len = 2;
1074 spi_message_add_tail(x, m);
1075
Semih Hazare4f48862007-07-18 00:35:56 -04001076 /* ... maybe discard first sample ... */
1077 if (pdata->settle_delay_usecs) {
1078 x->delay_usecs = pdata->settle_delay_usecs;
1079
1080 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001081 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001082 x->len = 1;
1083 spi_message_add_tail(x, m);
1084
1085 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001086 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001087 x->len = 2;
1088 spi_message_add_tail(x, m);
1089 }
1090
Imre Deakda970e62007-01-18 00:44:41 -05001091 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001092 m->context = ts;
1093
1094 m++;
1095 spi_message_init(m);
1096
1097 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001098 packet->read_z2 = READ_Z2(vref);
1099 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001100 x->len = 1;
1101 spi_message_add_tail(x, m);
1102
1103 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001104 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001105 x->len = 2;
1106 spi_message_add_tail(x, m);
1107
Semih Hazare4f48862007-07-18 00:35:56 -04001108 /* ... maybe discard first sample ... */
1109 if (pdata->settle_delay_usecs) {
1110 x->delay_usecs = pdata->settle_delay_usecs;
1111
1112 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001113 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001114 x->len = 1;
1115 spi_message_add_tail(x, m);
1116
1117 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001118 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001119 x->len = 2;
1120 spi_message_add_tail(x, m);
1121 }
1122
Imre Deakda970e62007-01-18 00:44:41 -05001123 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001124 m->context = ts;
1125 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001126
1127 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001128 m++;
1129 spi_message_init(m);
1130
Imre Deak53a0ef892006-04-11 23:41:49 -04001131 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001132 packet->pwrdown = PWRDOWN;
1133 x->tx_buf = &packet->pwrdown;
Imre Deak53a0ef892006-04-11 23:41:49 -04001134 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001135 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001136
1137 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001138 x->rx_buf = &packet->dummy;
Imre Deak53a0ef892006-04-11 23:41:49 -04001139 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001140 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001141 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001142
Imre Deak0b7018a2006-04-11 23:42:03 -04001143 m->complete = ads7846_rx;
1144 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001145
Imre Deakd5b415c2006-04-26 00:13:18 -04001146 ts->last_msg = m;
1147
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001148 ts->reg = regulator_get(&spi->dev, "vcc");
1149 if (IS_ERR(ts->reg)) {
1150 dev_err(&spi->dev, "unable to get regulator: %ld\n",
1151 PTR_ERR(ts->reg));
1152 goto err_free_gpio;
1153 }
1154
1155 err = regulator_enable(ts->reg);
1156 if (err) {
1157 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1158 goto err_put_regulator;
1159 }
1160
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001161 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001162 spi->dev.driver->name, ts)) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001163 dev_info(&spi->dev,
1164 "trying pin change workaround on irq %d\n", spi->irq);
1165 err = request_irq(spi->irq, ads7846_irq,
1166 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1167 spi->dev.driver->name, ts);
1168 if (err) {
1169 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001170 goto err_disable_regulator;
Michael Rothc57c0a22009-06-10 23:30:55 -07001171 }
David Brownellffa458c2006-01-08 13:34:21 -08001172 }
David Brownellffa458c2006-01-08 13:34:21 -08001173
David Brownell2c8dc072007-01-18 00:45:48 -05001174 err = ads784x_hwmon_register(spi, ts);
1175 if (err)
1176 goto err_free_irq;
1177
David Brownell2e5a7bd2006-01-08 13:34:25 -08001178 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001179
David Brownell2c8dc072007-01-18 00:45:48 -05001180 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001181 * the touchscreen, in case it's not connected.
1182 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001183 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001184 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1185
David Brownell2c8dc072007-01-18 00:45:48 -05001186 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001187 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001188 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001189
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001190 err = input_register_device(input_dev);
1191 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001192 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001193
David Brownellffa458c2006-01-08 13:34:21 -08001194 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001195
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001196 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001197 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1198 err_remove_hwmon:
1199 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001200 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001201 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001202 err_disable_regulator:
1203 regulator_disable(ts->reg);
1204 err_put_regulator:
1205 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001206 err_free_gpio:
1207 if (ts->gpio_pendown != -1)
1208 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001209 err_cleanup_filter:
1210 if (ts->filter_cleanup)
1211 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001212 err_free_mem:
1213 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001214 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001215 kfree(ts);
1216 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001217}
1218
David Brownell2e5a7bd2006-01-08 13:34:25 -08001219static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001220{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001221 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001222
David Brownell2c8dc072007-01-18 00:45:48 -05001223 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001224 input_unregister_device(ts->input);
1225
David Brownell2e5a7bd2006-01-08 13:34:25 -08001226 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001227
David Brownell2c8dc072007-01-18 00:45:48 -05001228 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001229
Imre Deak7de90a82006-04-11 23:43:55 -04001230 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001231 /* suspend left the IRQ disabled */
1232 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001233
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001234 regulator_disable(ts->reg);
1235 regulator_put(ts->reg);
1236
Eric Miao4d5975e2008-09-10 12:06:15 -04001237 if (ts->gpio_pendown != -1)
1238 gpio_free(ts->gpio_pendown);
1239
Imre Deakda970e62007-01-18 00:44:41 -05001240 if (ts->filter_cleanup)
1241 ts->filter_cleanup(ts->filter_data);
1242
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001243 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001244 kfree(ts);
1245
David Brownell2e5a7bd2006-01-08 13:34:25 -08001246 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001247 return 0;
1248}
1249
David Brownell2e5a7bd2006-01-08 13:34:25 -08001250static struct spi_driver ads7846_driver = {
1251 .driver = {
1252 .name = "ads7846",
1253 .bus = &spi_bus_type,
1254 .owner = THIS_MODULE,
1255 },
David Brownellffa458c2006-01-08 13:34:21 -08001256 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001257 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001258 .suspend = ads7846_suspend,
1259 .resume = ads7846_resume,
1260};
1261
1262static int __init ads7846_init(void)
1263{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001264 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001265}
1266module_init(ads7846_init);
1267
1268static void __exit ads7846_exit(void)
1269{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001270 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001271}
1272module_exit(ads7846_exit);
1273
1274MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1275MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001276MODULE_ALIAS("spi:ads7846");