blob: cf7e69766b2b778ea77f8f797a7b56ba60a597ac [file] [log] [blame]
David Brownellffa458c2006-01-08 13:34:21 -08001/*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
Imre Deak7de90a82006-04-11 23:43:55 -04005 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
David Brownellffa458c2006-01-08 13:34:21 -08007 *
8 * Using code from:
9 * - corgi_ts.c
10 * Copyright (C) 2004-2005 Richard Purdie
11 * - omap_ts.[hc], ads7846.h, ts_osk.c
12 * Copyright (C) 2002 MontaVista Software
13 * Copyright (C) 2004 Texas Instruments
14 * Copyright (C) 2005 Dirk Behme
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
19 */
David Brownell2c8dc072007-01-18 00:45:48 -050020#include <linux/hwmon.h>
David Brownellffa458c2006-01-08 13:34:21 -080021#include <linux/init.h>
David Brownell2c8dc072007-01-18 00:45:48 -050022#include <linux/err.h>
David Brownellffa458c2006-01-08 13:34:21 -080023#include <linux/delay.h>
24#include <linux/input.h>
25#include <linux/interrupt.h>
26#include <linux/slab.h>
Eric Miao4d5975e2008-09-10 12:06:15 -040027#include <linux/gpio.h>
David Brownellffa458c2006-01-08 13:34:21 -080028#include <linux/spi/spi.h>
29#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080030#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080031
David Brownellffa458c2006-01-08 13:34:21 -080032
33/*
David Brownell90845332006-05-25 18:44:20 -070034 * This code has been heavily tested on a Nokia 770, and lightly
35 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
David Brownellbff0de52007-05-22 23:28:40 -040036 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050037 * Support for ads7843 tested on Atmel at91sam926x-EK.
38 * Support for ads7845 has only been stubbed in.
David Brownellffa458c2006-01-08 13:34:21 -080039 *
Imre Deak7de90a82006-04-11 23:43:55 -040040 * IRQ handling needs a workaround because of a shortcoming in handling
41 * edge triggered IRQs on some platforms like the OMAP1/2. These
42 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
43 * have to maintain our own SW IRQ disabled status. This should be
44 * removed as soon as the affected platform's IRQ handling is fixed.
45 *
David Brownellffa458c2006-01-08 13:34:21 -080046 * app note sbaa036 talks in more detail about accurate sampling...
47 * that ought to help in situations like LCDs inducing noise (which
48 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040049 * This driver tries to utilize the measures described in the app
50 * note. The strength of filtering can be set in the board-* specific
51 * files.
David Brownellffa458c2006-01-08 13:34:21 -080052 */
53
Imre Deak1936d592007-01-18 00:45:31 -050054#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
55#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080056
David Brownelld93f70b2006-02-15 00:49:35 -050057/* this driver doesn't aim at the peak continuous sample rate */
58#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
59
David Brownellffa458c2006-01-08 13:34:21 -080060struct ts_event {
61 /* For portability, we can't read 12 bit values using SPI (which
62 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050063 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050064 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080065 */
Imre Deakda970e62007-01-18 00:44:41 -050066 u16 x;
67 u16 y;
68 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050069 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080070};
71
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];
86
87 struct spi_device *spi;
David Brownell2c8dc072007-01-18 00:45:48 -050088
89#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050090 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070091 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050092#endif
93
David Brownellffa458c2006-01-08 13:34:21 -080094 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -040095 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080096 u16 vref_delay_usecs;
97 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040098 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080099
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400100 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800101
Semih Hazare4f48862007-07-18 00:35:56 -0400102 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400103 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -0400104 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -0400105 int msg_idx;
106 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400107 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400108 int last_read;
109
110 u16 debounce_max;
111 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400112 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800113
Semih Hazar1d258912007-07-18 00:36:04 -0400114 u16 penirq_recheck_delay_usecs;
115
David Brownellffa458c2006-01-08 13:34:21 -0800116 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500117 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800118 unsigned pendown:1; /* P: lock */
119 unsigned pending:1; /* P: lock */
120// FIXME remove "irq_disabled"
121 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400122 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500123 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400124
Imre Deakda970e62007-01-18 00:44:41 -0500125 int (*filter)(void *data, int data_idx, int *val);
126 void *filter_data;
127 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400128 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400129 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700130
131 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800132};
133
134/* leave chip selected when we're done, for quicker re-select? */
135#if 0
136#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
137#else
138#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
139#endif
140
141/*--------------------------------------------------------------------------*/
142
143/* The ADS7846 has touchscreen and other sensors.
144 * Earlier ads784x chips are somewhat compatible.
145 */
146#define ADS_START (1 << 7)
147#define ADS_A2A1A0_d_y (1 << 4) /* differential */
148#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
149#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
150#define ADS_A2A1A0_d_x (5 << 4) /* differential */
151#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
152#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
153#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
154#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
155#define ADS_8_BIT (1 << 3)
156#define ADS_12_BIT (0 << 3)
157#define ADS_SER (1 << 2) /* non-differential */
158#define ADS_DFR (0 << 2) /* differential */
159#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
160#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
161#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
162#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
163
164#define MAX_12BIT ((1<<12)-1)
165
166/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500167#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
168 | ADS_12_BIT | ADS_DFR | \
169 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800170
Imre Deakde2defd2007-01-18 00:45:21 -0500171#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
172#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
173#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400174
Imre Deakde2defd2007-01-18 00:45:21 -0500175#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
176#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800177
178/* single-ended samples need to first power up reference voltage;
179 * we leave both ADC and VREF powered
180 */
181#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
182 | ADS_12_BIT | ADS_SER)
183
Imre Deakde2defd2007-01-18 00:45:21 -0500184#define REF_ON (READ_12BIT_DFR(x, 1, 1))
185#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800186
187/*--------------------------------------------------------------------------*/
188
189/*
190 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500191 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
192 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800193 */
194
195struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500196 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800197 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500198 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800199 u16 scratch;
200 __be16 sample;
201 struct spi_message msg;
202 struct spi_transfer xfer[6];
203};
204
Imre Deak7de90a82006-04-11 23:43:55 -0400205static void ads7846_enable(struct ads7846 *ts);
206static void ads7846_disable(struct ads7846 *ts);
207
Imre Deakc9e617a2006-04-11 23:44:05 -0400208static int device_suspended(struct device *dev)
209{
210 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500211 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400212}
213
David Brownellffa458c2006-01-08 13:34:21 -0800214static int ads7846_read12_ser(struct device *dev, unsigned command)
215{
216 struct spi_device *spi = to_spi_device(dev);
217 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800218 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800219 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500220 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800221
222 if (!req)
223 return -ENOMEM;
224
Imre Deak0b7018a2006-04-11 23:42:03 -0400225 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800226
David Brownell2c8dc072007-01-18 00:45:48 -0500227 /* FIXME boards with ads7846 might use external vref instead ... */
228 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800229
David Brownell2c8dc072007-01-18 00:45:48 -0500230 /* maybe turn on internal vREF, and let it settle */
231 if (use_internal) {
232 req->ref_on = REF_ON;
233 req->xfer[0].tx_buf = &req->ref_on;
234 req->xfer[0].len = 1;
235 spi_message_add_tail(&req->xfer[0], &req->msg);
236
237 req->xfer[1].rx_buf = &req->scratch;
238 req->xfer[1].len = 2;
239
240 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
241 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
242 spi_message_add_tail(&req->xfer[1], &req->msg);
243 }
David Brownellffa458c2006-01-08 13:34:21 -0800244
245 /* take sample */
246 req->command = (u8) command;
247 req->xfer[2].tx_buf = &req->command;
248 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500249 spi_message_add_tail(&req->xfer[2], &req->msg);
250
David Brownellffa458c2006-01-08 13:34:21 -0800251 req->xfer[3].rx_buf = &req->sample;
252 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500253 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800254
255 /* REVISIT: take a few more samples, and compare ... */
256
Nicolas Ferre969111e2007-02-28 23:51:03 -0500257 /* converter in low power mode & enable PENIRQ */
258 req->ref_off = PWRDOWN;
259 req->xfer[4].tx_buf = &req->ref_off;
260 req->xfer[4].len = 1;
261 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800262
Nicolas Ferre969111e2007-02-28 23:51:03 -0500263 req->xfer[5].rx_buf = &req->scratch;
264 req->xfer[5].len = 2;
265 CS_CHANGE(req->xfer[5]);
266 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800267
Imre Deakc9e617a2006-04-11 23:44:05 -0400268 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800269 disable_irq(spi->irq);
270 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400271 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800272 enable_irq(spi->irq);
273
Marc Pignatc24b2602007-12-04 23:45:11 -0800274 if (status == 0) {
275 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400276 status = be16_to_cpu(req->sample);
277 status = status >> 3;
278 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800279 }
David Brownell90845332006-05-25 18:44:20 -0700280
281 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400282 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800283}
284
David Brownell2c8dc072007-01-18 00:45:48 -0500285#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
286
287#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800288name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
289{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500290 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800291 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500292 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800293 if (v < 0) \
294 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500295 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800296} \
297static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
298
David Brownell2c8dc072007-01-18 00:45:48 -0500299
300/* Sysfs conventions report temperatures in millidegrees Celcius.
301 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
302 * accuracy scheme without calibration data. For now we won't try either;
303 * userspace sees raw sensor values, and must scale/calibrate appropriately.
304 */
305static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
306{
307 return v;
308}
309
310SHOW(temp0, temp0, null_adjust) /* temp1_input */
311SHOW(temp1, temp1, null_adjust) /* temp2_input */
312
313
314/* sysfs conventions report voltages in millivolts. We can convert voltages
315 * if we know vREF. userspace may need to scale vAUX to match the board's
316 * external resistors; we assume that vBATT only uses the internal ones.
317 */
318static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
319{
320 unsigned retval = v;
321
322 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400323 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500324 retval = retval >> 12;
325 return retval;
326}
327
328static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
329{
330 unsigned retval = vaux_adjust(ts, v);
331
332 /* ads7846 has a resistor ladder to scale this signal down */
333 if (ts->model == 7846)
334 retval *= 4;
335 return retval;
336}
337
338SHOW(in0_input, vaux, vaux_adjust)
339SHOW(in1_input, vbatt, vbatt_adjust)
340
341
342static struct attribute *ads7846_attributes[] = {
343 &dev_attr_temp0.attr,
344 &dev_attr_temp1.attr,
345 &dev_attr_in0_input.attr,
346 &dev_attr_in1_input.attr,
347 NULL,
348};
349
350static struct attribute_group ads7846_attr_group = {
351 .attrs = ads7846_attributes,
352};
353
354static struct attribute *ads7843_attributes[] = {
355 &dev_attr_in0_input.attr,
356 &dev_attr_in1_input.attr,
357 NULL,
358};
359
360static struct attribute_group ads7843_attr_group = {
361 .attrs = ads7843_attributes,
362};
363
364static struct attribute *ads7845_attributes[] = {
365 &dev_attr_in0_input.attr,
366 NULL,
367};
368
369static struct attribute_group ads7845_attr_group = {
370 .attrs = ads7845_attributes,
371};
372
373static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
374{
Tony Jones1beeffe2007-08-20 13:46:20 -0700375 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500376 int err;
377
378 /* hwmon sensors need a reference voltage */
379 switch (ts->model) {
380 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400381 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500382 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400383 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500384 }
385 break;
386 case 7845:
387 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400388 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500389 dev_warn(&spi->dev,
390 "external vREF for ADS%d not specified\n",
391 ts->model);
392 return 0;
393 }
394 break;
395 }
396
397 /* different chips have different sensor groups */
398 switch (ts->model) {
399 case 7846:
400 ts->attr_group = &ads7846_attr_group;
401 break;
402 case 7845:
403 ts->attr_group = &ads7845_attr_group;
404 break;
405 case 7843:
406 ts->attr_group = &ads7843_attr_group;
407 break;
408 default:
409 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
410 return 0;
411 }
412
413 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
414 if (err)
415 return err;
416
417 hwmon = hwmon_device_register(&spi->dev);
418 if (IS_ERR(hwmon)) {
419 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
420 return PTR_ERR(hwmon);
421 }
422
423 ts->hwmon = hwmon;
424 return 0;
425}
426
427static void ads784x_hwmon_unregister(struct spi_device *spi,
428 struct ads7846 *ts)
429{
430 if (ts->hwmon) {
431 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
432 hwmon_device_unregister(ts->hwmon);
433 }
434}
435
436#else
437static inline int ads784x_hwmon_register(struct spi_device *spi,
438 struct ads7846 *ts)
439{
440 return 0;
441}
442
443static inline void ads784x_hwmon_unregister(struct spi_device *spi,
444 struct ads7846 *ts)
445{
446}
447#endif
David Brownellffa458c2006-01-08 13:34:21 -0800448
Imre Deak438f2a72006-04-11 23:41:32 -0400449static int is_pen_down(struct device *dev)
450{
David Brownell2c8dc072007-01-18 00:45:48 -0500451 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400452
453 return ts->pendown;
454}
455
456static ssize_t ads7846_pen_down_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
459 return sprintf(buf, "%u\n", is_pen_down(dev));
460}
461
462static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
463
Imre Deak7de90a82006-04-11 23:43:55 -0400464static ssize_t ads7846_disable_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
467 struct ads7846 *ts = dev_get_drvdata(dev);
468
469 return sprintf(buf, "%u\n", ts->disabled);
470}
471
472static ssize_t ads7846_disable_store(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
475{
476 struct ads7846 *ts = dev_get_drvdata(dev);
Harvey Harrison3a0c58d2008-12-23 04:09:28 -0500477 unsigned long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400478
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400479 if (strict_strtoul(buf, 10, &i))
480 return -EINVAL;
481
Imre Deak7de90a82006-04-11 23:43:55 -0400482 spin_lock_irq(&ts->lock);
483
484 if (i)
485 ads7846_disable(ts);
486 else
487 ads7846_enable(ts);
488
489 spin_unlock_irq(&ts->lock);
490
491 return count;
492}
493
494static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
495
David Brownell2c8dc072007-01-18 00:45:48 -0500496static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500497 &dev_attr_pen_down.attr,
498 &dev_attr_disable.attr,
499 NULL,
500};
501
David Brownell2c8dc072007-01-18 00:45:48 -0500502static struct attribute_group ads784x_attr_group = {
503 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500504};
505
David Brownellffa458c2006-01-08 13:34:21 -0800506/*--------------------------------------------------------------------------*/
507
Eric Miao4d5975e2008-09-10 12:06:15 -0400508static int get_pendown_state(struct ads7846 *ts)
509{
510 if (ts->get_pendown_state)
511 return ts->get_pendown_state();
512
513 return !gpio_get_value(ts->gpio_pendown);
514}
515
Eric Miaofd746d52009-04-11 16:54:59 -0700516static void null_wait_for_sync(void)
517{
518}
519
David Brownellffa458c2006-01-08 13:34:21 -0800520/*
521 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
522 * to retrieve touchscreen status.
523 *
524 * The SPI transfer completion callback does the real work. It reports
525 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
526 */
527
528static void ads7846_rx(void *ads)
529{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500530 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400531 struct ads7846_packet *packet = ts->packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500532 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500533 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800534
Imre Deakda970e62007-01-18 00:44:41 -0500535 /* ads7846_rx_val() did in-place conversion (including byteswap) from
536 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800537 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400538 x = packet->tc.x;
539 y = packet->tc.y;
540 z1 = packet->tc.z1;
541 z2 = packet->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800542
543 /* range filtering */
544 if (x == MAX_12BIT)
545 x = 0;
546
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400547 if (ts->model == 7843) {
548 Rt = ts->pressure_max / 2;
549 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800550 /* compute touch pressure resistance using equation #2 */
551 Rt = z2;
552 Rt -= z1;
553 Rt *= x;
554 Rt *= ts->x_plate_ohms;
555 Rt /= z1;
556 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400557 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800558 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400559 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500560
Imre Deakd5b415c2006-04-26 00:13:18 -0400561 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500562 * the maximum. Don't report it to user space, repeat at least
563 * once more the measurement
564 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400565 if (packet->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500566#ifdef VERBOSE
567 pr_debug("%s: ignored %d pressure %d\n",
Kay Sieversa6c24902008-10-30 00:07:50 -0400568 dev_name(&ts->spi->dev), packet->tc.ignore, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500569#endif
Imre Deak1936d592007-01-18 00:45:31 -0500570 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800571 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400572 return;
573 }
574
Semih Hazar1d258912007-07-18 00:36:04 -0400575 /* Maybe check the pendown state before reporting. This discards
576 * false readings when the pen is lifted.
577 */
578 if (ts->penirq_recheck_delay_usecs) {
579 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400580 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400581 Rt = 0;
582 }
583
Imre Deak15e35892007-01-18 00:45:43 -0500584 /* NOTE: We can't rely on the pressure to determine the pen down
585 * state, even this controller has a pressure sensor. The pressure
586 * value can fluctuate for quite a while after lifting the pen and
587 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800588 *
Imre Deak15e35892007-01-18 00:45:43 -0500589 * The only safe way to check for the pen up condition is in the
590 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800591 */
David Brownellffa458c2006-01-08 13:34:21 -0800592 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500593 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400594
Imre Deak15e35892007-01-18 00:45:43 -0500595 if (!ts->pendown) {
596 input_report_key(input, BTN_TOUCH, 1);
597 ts->pendown = 1;
598#ifdef VERBOSE
599 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800600#endif
Imre Deak15e35892007-01-18 00:45:43 -0500601 }
602 input_report_abs(input, ABS_X, x);
603 input_report_abs(input, ABS_Y, y);
604 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800605
Imre Deak15e35892007-01-18 00:45:43 -0500606 input_sync(input);
607#ifdef VERBOSE
608 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
609#endif
610 }
David Brownellffa458c2006-01-08 13:34:21 -0800611
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800612 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
613 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800614}
615
Imre Deakda970e62007-01-18 00:44:41 -0500616static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400617{
618 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400619
Imre Deakda970e62007-01-18 00:44:41 -0500620 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
621 /* Start over collecting consistent readings. */
622 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400623 /* Repeat it, if this was the first read or the read
624 * wasn't consistent enough. */
625 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500626 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400627 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500628 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400629 } else {
630 /* Maximum number of debouncing reached and still
631 * not enough number of consistent readings. Abort
632 * the whole sample, repeat it in the next sampling
633 * period.
634 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400635 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500636 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400637 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400638 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400639 if (++ts->read_rep > ts->debounce_rep) {
640 /* Got a good reading for this coordinate,
641 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400642 ts->read_cnt = 0;
643 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500644 return ADS7846_FILTER_OK;
645 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400646 /* Read more values that are consistent. */
647 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500648 return ADS7846_FILTER_REPEAT;
649 }
650 }
651}
652
653static int ads7846_no_filter(void *ads, int data_idx, int *val)
654{
655 return ADS7846_FILTER_OK;
656}
657
658static void ads7846_rx_val(void *ads)
659{
660 struct ads7846 *ts = ads;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400661 struct ads7846_packet *packet = ts->packet;
Imre Deakda970e62007-01-18 00:44:41 -0500662 struct spi_message *m;
663 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500664 int val;
665 int action;
666 int status;
667
668 m = &ts->msg[ts->msg_idx];
669 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500670
671 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
672 * built from two 8 bit values written msb-first.
673 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400674 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500675
676 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
677 switch (action) {
678 case ADS7846_FILTER_REPEAT:
679 break;
680 case ADS7846_FILTER_IGNORE:
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400681 packet->tc.ignore = 1;
Imre Deakda970e62007-01-18 00:44:41 -0500682 /* Last message will contain ads7846_rx() as the
683 * completion function.
684 */
685 m = ts->last_msg;
686 break;
687 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400688 *(u16 *)t->rx_buf = val;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400689 packet->tc.ignore = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500690 m = &ts->msg[++ts->msg_idx];
691 break;
692 default:
693 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400694 }
Eric Miaofd746d52009-04-11 16:54:59 -0700695 ts->wait_for_sync();
Imre Deak0b7018a2006-04-11 23:42:03 -0400696 status = spi_async(ts->spi, m);
697 if (status)
698 dev_err(&ts->spi->dev, "spi_async --> %d\n",
699 status);
700}
701
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800702static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800703{
Imre Deak1936d592007-01-18 00:45:31 -0500704 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800705 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800706
Peter Zijlstraca109492008-11-25 12:43:51 +0100707 spin_lock(&ts->lock);
Imre Deakc9e617a2006-04-11 23:44:05 -0400708
Eric Miao4d5975e2008-09-10 12:06:15 -0400709 if (unlikely(!get_pendown_state(ts) ||
Imre Deak15e35892007-01-18 00:45:43 -0500710 device_suspended(&ts->spi->dev))) {
711 if (ts->pendown) {
712 struct input_dev *input = ts->input;
713
714 input_report_key(input, BTN_TOUCH, 0);
715 input_report_abs(input, ABS_PRESSURE, 0);
716 input_sync(input);
717
718 ts->pendown = 0;
719#ifdef VERBOSE
720 dev_dbg(&ts->spi->dev, "UP\n");
721#endif
722 }
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;
Imre Deak0b7018a2006-04-11 23:42:03 -0400757 disable_irq(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
793 /* we know the chip's in lowpower mode since we always
794 * leave it that way after every request
795 */
Imre Deak7de90a82006-04-11 23:43:55 -0400796}
797
798/* Must be called with ts->lock held */
799static void ads7846_enable(struct ads7846 *ts)
800{
801 if (!ts->disabled)
802 return;
803
804 ts->disabled = 0;
805 ts->irq_disabled = 0;
806 enable_irq(ts->spi->irq);
807}
808
809static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
810{
811 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
812
813 spin_lock_irq(&ts->lock);
814
David Brownellfbb38e32007-12-14 01:26:33 -0500815 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400816 ads7846_disable(ts);
817
818 spin_unlock_irq(&ts->lock);
819
David Brownellffa458c2006-01-08 13:34:21 -0800820 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400821
David Brownellffa458c2006-01-08 13:34:21 -0800822}
823
David Brownell2e5a7bd2006-01-08 13:34:25 -0800824static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800825{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800826 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800827
Imre Deak7de90a82006-04-11 23:43:55 -0400828 spin_lock_irq(&ts->lock);
829
David Brownellfbb38e32007-12-14 01:26:33 -0500830 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400831 ads7846_enable(ts);
832
833 spin_unlock_irq(&ts->lock);
834
David Brownellffa458c2006-01-08 13:34:21 -0800835 return 0;
836}
837
Eric Miao4d5975e2008-09-10 12:06:15 -0400838static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
839{
840 struct ads7846_platform_data *pdata = spi->dev.platform_data;
841 int err;
842
843 /* REVISIT when the irq can be triggered active-low, or if for some
844 * reason the touchscreen isn't hooked up, we don't need to access
845 * the pendown state.
846 */
847 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
848 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
849 return -EINVAL;
850 }
851
852 if (pdata->get_pendown_state) {
853 ts->get_pendown_state = pdata->get_pendown_state;
854 return 0;
855 }
856
857 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
858 if (err) {
859 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
860 pdata->gpio_pendown);
861 return err;
862 }
863
864 ts->gpio_pendown = pdata->gpio_pendown;
865 return 0;
866}
867
David Brownell2e5a7bd2006-01-08 13:34:25 -0800868static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800869{
David Brownellffa458c2006-01-08 13:34:21 -0800870 struct ads7846 *ts;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400871 struct ads7846_packet *packet;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500872 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800873 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400874 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800875 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500876 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500877 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800878
879 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800880 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800881 return -ENODEV;
882 }
883
884 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800885 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800886 return -ENODEV;
887 }
888
889 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500890 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800891 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500892 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800893 return -EINVAL;
894 }
895
David Brownell90845332006-05-25 18:44:20 -0700896 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
897 * that even if the hardware can do that, the SPI controller driver
898 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800899 */
David Brownell90845332006-05-25 18:44:20 -0700900 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400901 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500902 err = spi_setup(spi);
903 if (err < 0)
904 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800905
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500906 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400907 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500908 input_dev = input_allocate_device();
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400909 if (!ts || !packet || !input_dev) {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500910 err = -ENOMEM;
911 goto err_free_mem;
912 }
David Brownellffa458c2006-01-08 13:34:21 -0800913
David Brownell2e5a7bd2006-01-08 13:34:25 -0800914 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800915
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400916 ts->packet = packet;
David Brownellffa458c2006-01-08 13:34:21 -0800917 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500918 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400919 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800920
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800921 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800922 ts->timer.function = ads7846_timer;
923
Imre Deak7de90a82006-04-11 23:43:55 -0400924 spin_lock_init(&ts->lock);
925
David Brownellffa458c2006-01-08 13:34:21 -0800926 ts->model = pdata->model ? : 7846;
927 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
928 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400929 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500930
931 if (pdata->filter != NULL) {
932 if (pdata->filter_init != NULL) {
933 err = pdata->filter_init(pdata, &ts->filter_data);
934 if (err < 0)
935 goto err_free_mem;
936 }
937 ts->filter = pdata->filter;
938 ts->filter_cleanup = pdata->filter_cleanup;
939 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400940 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500941 if (ts->debounce_max < 2)
942 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400943 ts->debounce_tol = pdata->debounce_tol;
944 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500945 ts->filter = ads7846_debounce;
946 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400947 } else
Imre Deakda970e62007-01-18 00:44:41 -0500948 ts->filter = ads7846_no_filter;
Eric Miao4d5975e2008-09-10 12:06:15 -0400949
950 err = setup_pendown(spi, ts);
951 if (err)
952 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -0800953
Semih Hazar1d258912007-07-18 00:36:04 -0400954 if (pdata->penirq_recheck_delay_usecs)
955 ts->penirq_recheck_delay_usecs =
956 pdata->penirq_recheck_delay_usecs;
957
Eric Miaofd746d52009-04-11 16:54:59 -0700958 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
959
Kay Sieversa6c24902008-10-30 00:07:50 -0400960 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
David Brownellffa458c2006-01-08 13:34:21 -0800961
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500962 input_dev->name = "ADS784x Touchscreen";
963 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400964 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800965
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700966 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
967 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500968 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800969 pdata->x_min ? : 0,
970 pdata->x_max ? : MAX_12BIT,
971 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500972 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800973 pdata->y_min ? : 0,
974 pdata->y_max ? : MAX_12BIT,
975 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500976 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800977 pdata->pressure_min, pdata->pressure_max, 0, 0);
978
Imre Deakde2defd2007-01-18 00:45:21 -0500979 vref = pdata->keep_vref_on;
980
David Brownellffa458c2006-01-08 13:34:21 -0800981 /* set up the transfers to read touchscreen state; this assumes we
982 * use formula #2 for pressure, not #3.
983 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400984 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800985 x = ts->xfer;
986
Imre Deak0b7018a2006-04-11 23:42:03 -0400987 spi_message_init(m);
988
David Brownellffa458c2006-01-08 13:34:21 -0800989 /* y- still on; turn on only y+ (and ADC) */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400990 packet->read_y = READ_Y(vref);
991 x->tx_buf = &packet->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800992 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400993 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500994
David Brownellffa458c2006-01-08 13:34:21 -0800995 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400996 x->rx_buf = &packet->tc.y;
David Brownellffa458c2006-01-08 13:34:21 -0800997 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400998 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800999
Semih Hazare4f48862007-07-18 00:35:56 -04001000 /* the first sample after switching drivers can be low quality;
1001 * optionally discard it, using a second one after the signals
1002 * have had enough time to stabilize.
1003 */
1004 if (pdata->settle_delay_usecs) {
1005 x->delay_usecs = pdata->settle_delay_usecs;
1006
1007 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001008 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001009 x->len = 1;
1010 spi_message_add_tail(x, m);
1011
1012 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001013 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001014 x->len = 2;
1015 spi_message_add_tail(x, m);
1016 }
1017
Imre Deakda970e62007-01-18 00:44:41 -05001018 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001019 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -05001020
Imre Deak0b7018a2006-04-11 23:42:03 -04001021 m++;
1022 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -08001023
1024 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -05001025 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001026 packet->read_x = READ_X(vref);
1027 x->tx_buf = &packet->read_x;
David Brownellffa458c2006-01-08 13:34:21 -08001028 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001029 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001030
David Brownellffa458c2006-01-08 13:34:21 -08001031 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001032 x->rx_buf = &packet->tc.x;
David Brownellffa458c2006-01-08 13:34:21 -08001033 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001034 spi_message_add_tail(x, m);
1035
Semih Hazare4f48862007-07-18 00:35:56 -04001036 /* ... maybe discard first sample ... */
1037 if (pdata->settle_delay_usecs) {
1038 x->delay_usecs = pdata->settle_delay_usecs;
1039
1040 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001041 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001042 x->len = 1;
1043 spi_message_add_tail(x, m);
1044
1045 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001046 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001047 x->len = 2;
1048 spi_message_add_tail(x, m);
1049 }
1050
Imre Deakda970e62007-01-18 00:44:41 -05001051 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001052 m->context = ts;
1053
1054 /* turn y+ off, x- on; we'll use formula #2 */
1055 if (ts->model == 7846) {
1056 m++;
1057 spi_message_init(m);
1058
1059 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001060 packet->read_z1 = READ_Z1(vref);
1061 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001062 x->len = 1;
1063 spi_message_add_tail(x, m);
1064
1065 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001066 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001067 x->len = 2;
1068 spi_message_add_tail(x, m);
1069
Semih Hazare4f48862007-07-18 00:35:56 -04001070 /* ... maybe discard first sample ... */
1071 if (pdata->settle_delay_usecs) {
1072 x->delay_usecs = pdata->settle_delay_usecs;
1073
1074 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001075 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001076 x->len = 1;
1077 spi_message_add_tail(x, m);
1078
1079 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001080 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001081 x->len = 2;
1082 spi_message_add_tail(x, m);
1083 }
1084
Imre Deakda970e62007-01-18 00:44:41 -05001085 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001086 m->context = ts;
1087
1088 m++;
1089 spi_message_init(m);
1090
1091 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001092 packet->read_z2 = READ_Z2(vref);
1093 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001094 x->len = 1;
1095 spi_message_add_tail(x, m);
1096
1097 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001098 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001099 x->len = 2;
1100 spi_message_add_tail(x, m);
1101
Semih Hazare4f48862007-07-18 00:35:56 -04001102 /* ... maybe discard first sample ... */
1103 if (pdata->settle_delay_usecs) {
1104 x->delay_usecs = pdata->settle_delay_usecs;
1105
1106 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001107 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001108 x->len = 1;
1109 spi_message_add_tail(x, m);
1110
1111 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001112 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001113 x->len = 2;
1114 spi_message_add_tail(x, m);
1115 }
1116
Imre Deakda970e62007-01-18 00:44:41 -05001117 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001118 m->context = ts;
1119 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001120
1121 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001122 m++;
1123 spi_message_init(m);
1124
Imre Deak53a0ef892006-04-11 23:41:49 -04001125 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001126 packet->pwrdown = PWRDOWN;
1127 x->tx_buf = &packet->pwrdown;
Imre Deak53a0ef892006-04-11 23:41:49 -04001128 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001129 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001130
1131 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001132 x->rx_buf = &packet->dummy;
Imre Deak53a0ef892006-04-11 23:41:49 -04001133 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001134 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001135 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001136
Imre Deak0b7018a2006-04-11 23:42:03 -04001137 m->complete = ads7846_rx;
1138 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001139
Imre Deakd5b415c2006-04-26 00:13:18 -04001140 ts->last_msg = m;
1141
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001142 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001143 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001144 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001145 err = -EBUSY;
Eric Miao4d5975e2008-09-10 12:06:15 -04001146 goto err_free_gpio;
David Brownellffa458c2006-01-08 13:34:21 -08001147 }
David Brownellffa458c2006-01-08 13:34:21 -08001148
David Brownell2c8dc072007-01-18 00:45:48 -05001149 err = ads784x_hwmon_register(spi, ts);
1150 if (err)
1151 goto err_free_irq;
1152
David Brownell2e5a7bd2006-01-08 13:34:25 -08001153 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001154
David Brownell2c8dc072007-01-18 00:45:48 -05001155 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001156 * the touchscreen, in case it's not connected.
1157 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001158 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001159 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1160
David Brownell2c8dc072007-01-18 00:45:48 -05001161 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001162 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001163 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001164
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001165 err = input_register_device(input_dev);
1166 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001167 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001168
David Brownellffa458c2006-01-08 13:34:21 -08001169 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001170
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001171 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001172 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1173 err_remove_hwmon:
1174 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001175 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001176 free_irq(spi->irq, ts);
Eric Miao4d5975e2008-09-10 12:06:15 -04001177 err_free_gpio:
1178 if (ts->gpio_pendown != -1)
1179 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001180 err_cleanup_filter:
1181 if (ts->filter_cleanup)
1182 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001183 err_free_mem:
1184 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001185 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001186 kfree(ts);
1187 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001188}
1189
David Brownell2e5a7bd2006-01-08 13:34:25 -08001190static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001191{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001192 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001193
David Brownell2c8dc072007-01-18 00:45:48 -05001194 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001195 input_unregister_device(ts->input);
1196
David Brownell2e5a7bd2006-01-08 13:34:25 -08001197 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001198
David Brownell2c8dc072007-01-18 00:45:48 -05001199 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001200
Imre Deak7de90a82006-04-11 23:43:55 -04001201 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001202 /* suspend left the IRQ disabled */
1203 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001204
Eric Miao4d5975e2008-09-10 12:06:15 -04001205 if (ts->gpio_pendown != -1)
1206 gpio_free(ts->gpio_pendown);
1207
Imre Deakda970e62007-01-18 00:44:41 -05001208 if (ts->filter_cleanup)
1209 ts->filter_cleanup(ts->filter_data);
1210
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001211 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001212 kfree(ts);
1213
David Brownell2e5a7bd2006-01-08 13:34:25 -08001214 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001215 return 0;
1216}
1217
David Brownell2e5a7bd2006-01-08 13:34:25 -08001218static struct spi_driver ads7846_driver = {
1219 .driver = {
1220 .name = "ads7846",
1221 .bus = &spi_bus_type,
1222 .owner = THIS_MODULE,
1223 },
David Brownellffa458c2006-01-08 13:34:21 -08001224 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001225 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001226 .suspend = ads7846_suspend,
1227 .resume = ads7846_resume,
1228};
1229
1230static int __init ads7846_init(void)
1231{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001232 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001233}
1234module_init(ads7846_init);
1235
1236static void __exit ads7846_exit(void)
1237{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001238 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001239}
1240module_exit(ads7846_exit);
1241
1242MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1243MODULE_LICENSE("GPL");