blob: 39573b91c8de4be826ff4b2a44a3c5f6f71c36c4 [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>
27#include <linux/spi/spi.h>
28#include <linux/spi/ads7846.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080029#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080030
David Brownellffa458c2006-01-08 13:34:21 -080031
32/*
David Brownell90845332006-05-25 18:44:20 -070033 * This code has been heavily tested on a Nokia 770, and lightly
34 * tested on other ads7846 devices (OSK/Mistral, Lubbock).
David Brownellbff0de52007-05-22 23:28:40 -040035 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050036 * Support for ads7843 tested on Atmel at91sam926x-EK.
37 * Support for ads7845 has only been stubbed in.
David Brownellffa458c2006-01-08 13:34:21 -080038 *
Imre Deak7de90a82006-04-11 23:43:55 -040039 * IRQ handling needs a workaround because of a shortcoming in handling
40 * edge triggered IRQs on some platforms like the OMAP1/2. These
41 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
42 * have to maintain our own SW IRQ disabled status. This should be
43 * removed as soon as the affected platform's IRQ handling is fixed.
44 *
David Brownellffa458c2006-01-08 13:34:21 -080045 * app note sbaa036 talks in more detail about accurate sampling...
46 * that ought to help in situations like LCDs inducing noise (which
47 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040048 * This driver tries to utilize the measures described in the app
49 * note. The strength of filtering can be set in the board-* specific
50 * files.
David Brownellffa458c2006-01-08 13:34:21 -080051 */
52
Imre Deak1936d592007-01-18 00:45:31 -050053#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
54#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080055
David Brownelld93f70b2006-02-15 00:49:35 -050056/* this driver doesn't aim at the peak continuous sample rate */
57#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
58
David Brownellffa458c2006-01-08 13:34:21 -080059struct ts_event {
60 /* For portability, we can't read 12 bit values using SPI (which
61 * would make the controller deliver them as native byteorder u16
David Brownelld93f70b2006-02-15 00:49:35 -050062 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050063 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080064 */
Imre Deakda970e62007-01-18 00:44:41 -050065 u16 x;
66 u16 y;
67 u16 z1, z2;
David Brownell2c8dc072007-01-18 00:45:48 -050068 int ignore;
David Brownellffa458c2006-01-08 13:34:21 -080069};
70
71struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050072 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080073 char phys[32];
74
75 struct spi_device *spi;
David Brownell2c8dc072007-01-18 00:45:48 -050076
77#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -050078 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -070079 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -050080#endif
81
David Brownellffa458c2006-01-08 13:34:21 -080082 u16 model;
83 u16 vref_delay_usecs;
84 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040085 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080086
Imre Deak53a0ef892006-04-11 23:41:49 -040087 u8 read_x, read_y, read_z1, read_z2, pwrdown;
88 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080089 struct ts_event tc;
90
Semih Hazare4f48862007-07-18 00:35:56 -040091 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -040092 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040093 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040094 int msg_idx;
95 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040096 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040097 int last_read;
98
99 u16 debounce_max;
100 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400101 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800102
Semih Hazar1d258912007-07-18 00:36:04 -0400103 u16 penirq_recheck_delay_usecs;
104
David Brownellffa458c2006-01-08 13:34:21 -0800105 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500106 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800107 unsigned pendown:1; /* P: lock */
108 unsigned pending:1; /* P: lock */
109// FIXME remove "irq_disabled"
110 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400111 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500112 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400113
Imre Deakda970e62007-01-18 00:44:41 -0500114 int (*filter)(void *data, int data_idx, int *val);
115 void *filter_data;
116 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400117 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800118};
119
120/* leave chip selected when we're done, for quicker re-select? */
121#if 0
122#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
123#else
124#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
125#endif
126
127/*--------------------------------------------------------------------------*/
128
129/* The ADS7846 has touchscreen and other sensors.
130 * Earlier ads784x chips are somewhat compatible.
131 */
132#define ADS_START (1 << 7)
133#define ADS_A2A1A0_d_y (1 << 4) /* differential */
134#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
135#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
136#define ADS_A2A1A0_d_x (5 << 4) /* differential */
137#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
138#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
139#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
140#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
141#define ADS_8_BIT (1 << 3)
142#define ADS_12_BIT (0 << 3)
143#define ADS_SER (1 << 2) /* non-differential */
144#define ADS_DFR (0 << 2) /* differential */
145#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
146#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
147#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
148#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
149
150#define MAX_12BIT ((1<<12)-1)
151
152/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500153#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
154 | ADS_12_BIT | ADS_DFR | \
155 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800156
Imre Deakde2defd2007-01-18 00:45:21 -0500157#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
158#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
159#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400160
Imre Deakde2defd2007-01-18 00:45:21 -0500161#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
162#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800163
164/* single-ended samples need to first power up reference voltage;
165 * we leave both ADC and VREF powered
166 */
167#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
168 | ADS_12_BIT | ADS_SER)
169
Imre Deakde2defd2007-01-18 00:45:21 -0500170#define REF_ON (READ_12BIT_DFR(x, 1, 1))
171#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800172
173/*--------------------------------------------------------------------------*/
174
175/*
176 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500177 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
178 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800179 */
David Brownell2c8dc072007-01-18 00:45:48 -0500180static unsigned vREF_mV;
181module_param(vREF_mV, uint, 0);
182MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
David Brownellffa458c2006-01-08 13:34:21 -0800183
184struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500185 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800186 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500187 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800188 u16 scratch;
189 __be16 sample;
190 struct spi_message msg;
191 struct spi_transfer xfer[6];
192};
193
Imre Deak7de90a82006-04-11 23:43:55 -0400194static void ads7846_enable(struct ads7846 *ts);
195static void ads7846_disable(struct ads7846 *ts);
196
Imre Deakc9e617a2006-04-11 23:44:05 -0400197static int device_suspended(struct device *dev)
198{
199 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500200 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400201}
202
David Brownellffa458c2006-01-08 13:34:21 -0800203static int ads7846_read12_ser(struct device *dev, unsigned command)
204{
205 struct spi_device *spi = to_spi_device(dev);
206 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800207 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800208 int status;
Andrew Morton05be5fc2008-03-10 03:08:40 -0700209 int uninitialized_var(sample);
David Brownell2c8dc072007-01-18 00:45:48 -0500210 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800211
212 if (!req)
213 return -ENOMEM;
214
Imre Deak0b7018a2006-04-11 23:42:03 -0400215 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800216
David Brownell2c8dc072007-01-18 00:45:48 -0500217 /* FIXME boards with ads7846 might use external vref instead ... */
218 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800219
David Brownell2c8dc072007-01-18 00:45:48 -0500220 /* maybe turn on internal vREF, and let it settle */
221 if (use_internal) {
222 req->ref_on = REF_ON;
223 req->xfer[0].tx_buf = &req->ref_on;
224 req->xfer[0].len = 1;
225 spi_message_add_tail(&req->xfer[0], &req->msg);
226
227 req->xfer[1].rx_buf = &req->scratch;
228 req->xfer[1].len = 2;
229
230 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
231 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
232 spi_message_add_tail(&req->xfer[1], &req->msg);
233 }
David Brownellffa458c2006-01-08 13:34:21 -0800234
235 /* take sample */
236 req->command = (u8) command;
237 req->xfer[2].tx_buf = &req->command;
238 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500239 spi_message_add_tail(&req->xfer[2], &req->msg);
240
David Brownellffa458c2006-01-08 13:34:21 -0800241 req->xfer[3].rx_buf = &req->sample;
242 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500243 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800244
245 /* REVISIT: take a few more samples, and compare ... */
246
Nicolas Ferre969111e2007-02-28 23:51:03 -0500247 /* converter in low power mode & enable PENIRQ */
248 req->ref_off = PWRDOWN;
249 req->xfer[4].tx_buf = &req->ref_off;
250 req->xfer[4].len = 1;
251 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800252
Nicolas Ferre969111e2007-02-28 23:51:03 -0500253 req->xfer[5].rx_buf = &req->scratch;
254 req->xfer[5].len = 2;
255 CS_CHANGE(req->xfer[5]);
256 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800257
Imre Deakc9e617a2006-04-11 23:44:05 -0400258 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800259 disable_irq(spi->irq);
260 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400261 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800262 enable_irq(spi->irq);
263
Marc Pignatc24b2602007-12-04 23:45:11 -0800264 if (status == 0) {
265 /* on-wire is a must-ignore bit, a BE12 value, then padding */
266 sample = be16_to_cpu(req->sample);
267 sample = sample >> 3;
268 sample &= 0x0fff;
269 }
David Brownell90845332006-05-25 18:44:20 -0700270
271 kfree(req);
David Brownellffa458c2006-01-08 13:34:21 -0800272 return status ? status : sample;
273}
274
David Brownell2c8dc072007-01-18 00:45:48 -0500275#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
276
277#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800278name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
279{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500280 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800281 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500282 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800283 if (v < 0) \
284 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500285 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800286} \
287static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
288
David Brownell2c8dc072007-01-18 00:45:48 -0500289
290/* Sysfs conventions report temperatures in millidegrees Celcius.
291 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
292 * accuracy scheme without calibration data. For now we won't try either;
293 * userspace sees raw sensor values, and must scale/calibrate appropriately.
294 */
295static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
296{
297 return v;
298}
299
300SHOW(temp0, temp0, null_adjust) /* temp1_input */
301SHOW(temp1, temp1, null_adjust) /* temp2_input */
302
303
304/* sysfs conventions report voltages in millivolts. We can convert voltages
305 * if we know vREF. userspace may need to scale vAUX to match the board's
306 * external resistors; we assume that vBATT only uses the internal ones.
307 */
308static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
309{
310 unsigned retval = v;
311
312 /* external resistors may scale vAUX into 0..vREF */
313 retval *= vREF_mV;
314 retval = retval >> 12;
315 return retval;
316}
317
318static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
319{
320 unsigned retval = vaux_adjust(ts, v);
321
322 /* ads7846 has a resistor ladder to scale this signal down */
323 if (ts->model == 7846)
324 retval *= 4;
325 return retval;
326}
327
328SHOW(in0_input, vaux, vaux_adjust)
329SHOW(in1_input, vbatt, vbatt_adjust)
330
331
332static struct attribute *ads7846_attributes[] = {
333 &dev_attr_temp0.attr,
334 &dev_attr_temp1.attr,
335 &dev_attr_in0_input.attr,
336 &dev_attr_in1_input.attr,
337 NULL,
338};
339
340static struct attribute_group ads7846_attr_group = {
341 .attrs = ads7846_attributes,
342};
343
344static struct attribute *ads7843_attributes[] = {
345 &dev_attr_in0_input.attr,
346 &dev_attr_in1_input.attr,
347 NULL,
348};
349
350static struct attribute_group ads7843_attr_group = {
351 .attrs = ads7843_attributes,
352};
353
354static struct attribute *ads7845_attributes[] = {
355 &dev_attr_in0_input.attr,
356 NULL,
357};
358
359static struct attribute_group ads7845_attr_group = {
360 .attrs = ads7845_attributes,
361};
362
363static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
364{
Tony Jones1beeffe2007-08-20 13:46:20 -0700365 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500366 int err;
367
368 /* hwmon sensors need a reference voltage */
369 switch (ts->model) {
370 case 7846:
371 if (!vREF_mV) {
372 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
373 vREF_mV = 2500;
374 }
375 break;
376 case 7845:
377 case 7843:
378 if (!vREF_mV) {
379 dev_warn(&spi->dev,
380 "external vREF for ADS%d not specified\n",
381 ts->model);
382 return 0;
383 }
384 break;
385 }
386
387 /* different chips have different sensor groups */
388 switch (ts->model) {
389 case 7846:
390 ts->attr_group = &ads7846_attr_group;
391 break;
392 case 7845:
393 ts->attr_group = &ads7845_attr_group;
394 break;
395 case 7843:
396 ts->attr_group = &ads7843_attr_group;
397 break;
398 default:
399 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
400 return 0;
401 }
402
403 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
404 if (err)
405 return err;
406
407 hwmon = hwmon_device_register(&spi->dev);
408 if (IS_ERR(hwmon)) {
409 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
410 return PTR_ERR(hwmon);
411 }
412
413 ts->hwmon = hwmon;
414 return 0;
415}
416
417static void ads784x_hwmon_unregister(struct spi_device *spi,
418 struct ads7846 *ts)
419{
420 if (ts->hwmon) {
421 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
422 hwmon_device_unregister(ts->hwmon);
423 }
424}
425
426#else
427static inline int ads784x_hwmon_register(struct spi_device *spi,
428 struct ads7846 *ts)
429{
430 return 0;
431}
432
433static inline void ads784x_hwmon_unregister(struct spi_device *spi,
434 struct ads7846 *ts)
435{
436}
437#endif
David Brownellffa458c2006-01-08 13:34:21 -0800438
Imre Deak438f2a72006-04-11 23:41:32 -0400439static int is_pen_down(struct device *dev)
440{
David Brownell2c8dc072007-01-18 00:45:48 -0500441 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400442
443 return ts->pendown;
444}
445
446static ssize_t ads7846_pen_down_show(struct device *dev,
447 struct device_attribute *attr, char *buf)
448{
449 return sprintf(buf, "%u\n", is_pen_down(dev));
450}
451
452static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
453
Imre Deak7de90a82006-04-11 23:43:55 -0400454static ssize_t ads7846_disable_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
457 struct ads7846 *ts = dev_get_drvdata(dev);
458
459 return sprintf(buf, "%u\n", ts->disabled);
460}
461
462static ssize_t ads7846_disable_store(struct device *dev,
463 struct device_attribute *attr,
464 const char *buf, size_t count)
465{
466 struct ads7846 *ts = dev_get_drvdata(dev);
467 char *endp;
468 int i;
469
470 i = simple_strtoul(buf, &endp, 10);
471 spin_lock_irq(&ts->lock);
472
473 if (i)
474 ads7846_disable(ts);
475 else
476 ads7846_enable(ts);
477
478 spin_unlock_irq(&ts->lock);
479
480 return count;
481}
482
483static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
484
David Brownell2c8dc072007-01-18 00:45:48 -0500485static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500486 &dev_attr_pen_down.attr,
487 &dev_attr_disable.attr,
488 NULL,
489};
490
David Brownell2c8dc072007-01-18 00:45:48 -0500491static struct attribute_group ads784x_attr_group = {
492 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500493};
494
David Brownellffa458c2006-01-08 13:34:21 -0800495/*--------------------------------------------------------------------------*/
496
497/*
498 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
499 * to retrieve touchscreen status.
500 *
501 * The SPI transfer completion callback does the real work. It reports
502 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
503 */
504
505static void ads7846_rx(void *ads)
506{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500507 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500508 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500509 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800510
Imre Deakda970e62007-01-18 00:44:41 -0500511 /* ads7846_rx_val() did in-place conversion (including byteswap) from
512 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800513 */
Imre Deakda970e62007-01-18 00:44:41 -0500514 x = ts->tc.x;
515 y = ts->tc.y;
516 z1 = ts->tc.z1;
517 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800518
519 /* range filtering */
520 if (x == MAX_12BIT)
521 x = 0;
522
Imre Deak15e35892007-01-18 00:45:43 -0500523 if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800524 /* compute touch pressure resistance using equation #2 */
525 Rt = z2;
526 Rt -= z1;
527 Rt *= x;
528 Rt *= ts->x_plate_ohms;
529 Rt /= z1;
530 Rt = (Rt + 2047) >> 12;
531 } else
532 Rt = 0;
533
Nicolas Ferre969111e2007-02-28 23:51:03 -0500534 if (ts->model == 7843)
535 Rt = ts->pressure_max / 2;
536
Imre Deakd5b415c2006-04-26 00:13:18 -0400537 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500538 * the maximum. Don't report it to user space, repeat at least
539 * once more the measurement
540 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400541 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500542#ifdef VERBOSE
543 pr_debug("%s: ignored %d pressure %d\n",
544 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
545#endif
Imre Deak1936d592007-01-18 00:45:31 -0500546 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800547 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400548 return;
549 }
550
Semih Hazar1d258912007-07-18 00:36:04 -0400551 /* Maybe check the pendown state before reporting. This discards
552 * false readings when the pen is lifted.
553 */
554 if (ts->penirq_recheck_delay_usecs) {
555 udelay(ts->penirq_recheck_delay_usecs);
556 if (!ts->get_pendown_state())
557 Rt = 0;
558 }
559
Imre Deak15e35892007-01-18 00:45:43 -0500560 /* NOTE: We can't rely on the pressure to determine the pen down
561 * state, even this controller has a pressure sensor. The pressure
562 * value can fluctuate for quite a while after lifting the pen and
563 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800564 *
Imre Deak15e35892007-01-18 00:45:43 -0500565 * The only safe way to check for the pen up condition is in the
566 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800567 */
David Brownellffa458c2006-01-08 13:34:21 -0800568 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500569 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400570
Imre Deak15e35892007-01-18 00:45:43 -0500571 if (!ts->pendown) {
572 input_report_key(input, BTN_TOUCH, 1);
573 ts->pendown = 1;
574#ifdef VERBOSE
575 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800576#endif
Imre Deak15e35892007-01-18 00:45:43 -0500577 }
578 input_report_abs(input, ABS_X, x);
579 input_report_abs(input, ABS_Y, y);
580 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800581
Imre Deak15e35892007-01-18 00:45:43 -0500582 input_sync(input);
583#ifdef VERBOSE
584 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
585#endif
586 }
David Brownellffa458c2006-01-08 13:34:21 -0800587
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800588 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
589 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800590}
591
Imre Deakda970e62007-01-18 00:44:41 -0500592static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400593{
594 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400595
Imre Deakda970e62007-01-18 00:44:41 -0500596 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
597 /* Start over collecting consistent readings. */
598 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400599 /* Repeat it, if this was the first read or the read
600 * wasn't consistent enough. */
601 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500602 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400603 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500604 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400605 } else {
606 /* Maximum number of debouncing reached and still
607 * not enough number of consistent readings. Abort
608 * the whole sample, repeat it in the next sampling
609 * period.
610 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400611 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500612 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400613 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400614 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400615 if (++ts->read_rep > ts->debounce_rep) {
616 /* Got a good reading for this coordinate,
617 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400618 ts->read_cnt = 0;
619 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500620 return ADS7846_FILTER_OK;
621 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400622 /* Read more values that are consistent. */
623 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500624 return ADS7846_FILTER_REPEAT;
625 }
626 }
627}
628
629static int ads7846_no_filter(void *ads, int data_idx, int *val)
630{
631 return ADS7846_FILTER_OK;
632}
633
634static void ads7846_rx_val(void *ads)
635{
636 struct ads7846 *ts = ads;
637 struct spi_message *m;
638 struct spi_transfer *t;
639 u16 *rx_val;
640 int val;
641 int action;
642 int status;
643
644 m = &ts->msg[ts->msg_idx];
645 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
646 rx_val = t->rx_buf;
647
648 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
649 * built from two 8 bit values written msb-first.
650 */
651 val = be16_to_cpu(*rx_val) >> 3;
652
653 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
654 switch (action) {
655 case ADS7846_FILTER_REPEAT:
656 break;
657 case ADS7846_FILTER_IGNORE:
658 ts->tc.ignore = 1;
659 /* Last message will contain ads7846_rx() as the
660 * completion function.
661 */
662 m = ts->last_msg;
663 break;
664 case ADS7846_FILTER_OK:
665 *rx_val = val;
666 ts->tc.ignore = 0;
667 m = &ts->msg[++ts->msg_idx];
668 break;
669 default:
670 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400671 }
672 status = spi_async(ts->spi, m);
673 if (status)
674 dev_err(&ts->spi->dev, "spi_async --> %d\n",
675 status);
676}
677
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800678static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800679{
Imre Deak1936d592007-01-18 00:45:31 -0500680 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800681 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800682
Imre Deakc9e617a2006-04-11 23:44:05 -0400683 spin_lock_irq(&ts->lock);
684
Imre Deak15e35892007-01-18 00:45:43 -0500685 if (unlikely(!ts->get_pendown_state() ||
686 device_suspended(&ts->spi->dev))) {
687 if (ts->pendown) {
688 struct input_dev *input = ts->input;
689
690 input_report_key(input, BTN_TOUCH, 0);
691 input_report_abs(input, ABS_PRESSURE, 0);
692 input_sync(input);
693
694 ts->pendown = 0;
695#ifdef VERBOSE
696 dev_dbg(&ts->spi->dev, "UP\n");
697#endif
698 }
699
David Brownell90845332006-05-25 18:44:20 -0700700 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400701 if (!device_suspended(&ts->spi->dev)) {
702 ts->irq_disabled = 0;
703 enable_irq(ts->spi->irq);
704 }
705 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400706 } else {
707 /* pen is still down, continue with the measurement */
708 ts->msg_idx = 0;
709 status = spi_async(ts->spi, &ts->msg[0]);
710 if (status)
711 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
712 }
713
714 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500715 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800716}
717
David Howells7d12e782006-10-05 14:55:46 +0100718static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800719{
Imre Deak0b7018a2006-04-11 23:42:03 -0400720 struct ads7846 *ts = handle;
721 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400722
723 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400724 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400725 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700726 /* The ARM do_simple_IRQ() dispatcher doesn't act
727 * like the other dispatchers: it will report IRQs
728 * even after they've been disabled. We work around
729 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400730 */
731 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400732 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400733 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500734 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800735 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400736 }
737 }
738 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400739
740 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800741}
742
743/*--------------------------------------------------------------------------*/
744
Imre Deak7de90a82006-04-11 23:43:55 -0400745/* Must be called with ts->lock held */
746static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800747{
Imre Deak7de90a82006-04-11 23:43:55 -0400748 if (ts->disabled)
749 return;
David Brownellffa458c2006-01-08 13:34:21 -0800750
Imre Deakc9e617a2006-04-11 23:44:05 -0400751 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800752
Imre Deakc9e617a2006-04-11 23:44:05 -0400753 /* are we waiting for IRQ, or polling? */
754 if (!ts->pending) {
755 ts->irq_disabled = 1;
756 disable_irq(ts->spi->irq);
757 } else {
758 /* the timer will run at least once more, and
759 * leave everything in a clean state, IRQ disabled
760 */
761 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400762 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400763 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400764 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800765 }
766 }
767
768 /* we know the chip's in lowpower mode since we always
769 * leave it that way after every request
770 */
771
Imre Deak7de90a82006-04-11 23:43:55 -0400772}
773
774/* Must be called with ts->lock held */
775static void ads7846_enable(struct ads7846 *ts)
776{
777 if (!ts->disabled)
778 return;
779
780 ts->disabled = 0;
781 ts->irq_disabled = 0;
782 enable_irq(ts->spi->irq);
783}
784
785static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
786{
787 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
788
789 spin_lock_irq(&ts->lock);
790
David Brownellfbb38e32007-12-14 01:26:33 -0500791 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400792 ads7846_disable(ts);
793
794 spin_unlock_irq(&ts->lock);
795
David Brownellffa458c2006-01-08 13:34:21 -0800796 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400797
David Brownellffa458c2006-01-08 13:34:21 -0800798}
799
David Brownell2e5a7bd2006-01-08 13:34:25 -0800800static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800801{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800802 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800803
Imre Deak7de90a82006-04-11 23:43:55 -0400804 spin_lock_irq(&ts->lock);
805
David Brownellfbb38e32007-12-14 01:26:33 -0500806 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400807 ads7846_enable(ts);
808
809 spin_unlock_irq(&ts->lock);
810
David Brownellffa458c2006-01-08 13:34:21 -0800811 return 0;
812}
813
David Brownell2e5a7bd2006-01-08 13:34:25 -0800814static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800815{
David Brownellffa458c2006-01-08 13:34:21 -0800816 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500817 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800818 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400819 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800820 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500821 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500822 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800823
824 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800825 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800826 return -ENODEV;
827 }
828
829 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800830 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800831 return -ENODEV;
832 }
833
834 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500835 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800836 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500837 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800838 return -EINVAL;
839 }
840
David Brownell90845332006-05-25 18:44:20 -0700841 /* REVISIT when the irq can be triggered active-low, or if for some
842 * reason the touchscreen isn't hooked up, we don't need to access
843 * the pendown state.
844 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400845 if (pdata->get_pendown_state == NULL) {
846 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
847 return -EINVAL;
848 }
849
David Brownell90845332006-05-25 18:44:20 -0700850 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
851 * that even if the hardware can do that, the SPI controller driver
852 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800853 */
David Brownell90845332006-05-25 18:44:20 -0700854 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400855 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500856 err = spi_setup(spi);
857 if (err < 0)
858 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800859
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500860 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
861 input_dev = input_allocate_device();
862 if (!ts || !input_dev) {
863 err = -ENOMEM;
864 goto err_free_mem;
865 }
David Brownellffa458c2006-01-08 13:34:21 -0800866
David Brownell2e5a7bd2006-01-08 13:34:25 -0800867 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800868
869 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500870 ts->input = input_dev;
David Brownellffa458c2006-01-08 13:34:21 -0800871
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800872 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800873 ts->timer.function = ads7846_timer;
874
Imre Deak7de90a82006-04-11 23:43:55 -0400875 spin_lock_init(&ts->lock);
876
David Brownellffa458c2006-01-08 13:34:21 -0800877 ts->model = pdata->model ? : 7846;
878 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
879 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400880 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500881
882 if (pdata->filter != NULL) {
883 if (pdata->filter_init != NULL) {
884 err = pdata->filter_init(pdata, &ts->filter_data);
885 if (err < 0)
886 goto err_free_mem;
887 }
888 ts->filter = pdata->filter;
889 ts->filter_cleanup = pdata->filter_cleanup;
890 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400891 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500892 if (ts->debounce_max < 2)
893 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400894 ts->debounce_tol = pdata->debounce_tol;
895 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500896 ts->filter = ads7846_debounce;
897 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400898 } else
Imre Deakda970e62007-01-18 00:44:41 -0500899 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400900 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800901
Semih Hazar1d258912007-07-18 00:36:04 -0400902 if (pdata->penirq_recheck_delay_usecs)
903 ts->penirq_recheck_delay_usecs =
904 pdata->penirq_recheck_delay_usecs;
905
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500906 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800907
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500908 input_dev->name = "ADS784x Touchscreen";
909 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400910 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800911
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700912 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
913 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500914 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800915 pdata->x_min ? : 0,
916 pdata->x_max ? : MAX_12BIT,
917 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500918 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800919 pdata->y_min ? : 0,
920 pdata->y_max ? : MAX_12BIT,
921 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500922 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800923 pdata->pressure_min, pdata->pressure_max, 0, 0);
924
Imre Deakde2defd2007-01-18 00:45:21 -0500925 vref = pdata->keep_vref_on;
926
David Brownellffa458c2006-01-08 13:34:21 -0800927 /* set up the transfers to read touchscreen state; this assumes we
928 * use formula #2 for pressure, not #3.
929 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400930 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800931 x = ts->xfer;
932
Imre Deak0b7018a2006-04-11 23:42:03 -0400933 spi_message_init(m);
934
David Brownellffa458c2006-01-08 13:34:21 -0800935 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500936 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500937 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800938 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400939 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500940
David Brownellffa458c2006-01-08 13:34:21 -0800941 x++;
942 x->rx_buf = &ts->tc.y;
943 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400944 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800945
Semih Hazare4f48862007-07-18 00:35:56 -0400946 /* the first sample after switching drivers can be low quality;
947 * optionally discard it, using a second one after the signals
948 * have had enough time to stabilize.
949 */
950 if (pdata->settle_delay_usecs) {
951 x->delay_usecs = pdata->settle_delay_usecs;
952
953 x++;
954 x->tx_buf = &ts->read_y;
955 x->len = 1;
956 spi_message_add_tail(x, m);
957
958 x++;
959 x->rx_buf = &ts->tc.y;
960 x->len = 2;
961 spi_message_add_tail(x, m);
962 }
963
Imre Deakda970e62007-01-18 00:44:41 -0500964 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400965 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500966
Imre Deak0b7018a2006-04-11 23:42:03 -0400967 m++;
968 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800969
970 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500971 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500972 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500973 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800974 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400975 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500976
David Brownellffa458c2006-01-08 13:34:21 -0800977 x++;
978 x->rx_buf = &ts->tc.x;
979 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400980 spi_message_add_tail(x, m);
981
Semih Hazare4f48862007-07-18 00:35:56 -0400982 /* ... maybe discard first sample ... */
983 if (pdata->settle_delay_usecs) {
984 x->delay_usecs = pdata->settle_delay_usecs;
985
986 x++;
987 x->tx_buf = &ts->read_x;
988 x->len = 1;
989 spi_message_add_tail(x, m);
990
991 x++;
992 x->rx_buf = &ts->tc.x;
993 x->len = 2;
994 spi_message_add_tail(x, m);
995 }
996
Imre Deakda970e62007-01-18 00:44:41 -0500997 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400998 m->context = ts;
999
1000 /* turn y+ off, x- on; we'll use formula #2 */
1001 if (ts->model == 7846) {
1002 m++;
1003 spi_message_init(m);
1004
1005 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001006 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001007 x->tx_buf = &ts->read_z1;
1008 x->len = 1;
1009 spi_message_add_tail(x, m);
1010
1011 x++;
1012 x->rx_buf = &ts->tc.z1;
1013 x->len = 2;
1014 spi_message_add_tail(x, m);
1015
Semih Hazare4f48862007-07-18 00:35:56 -04001016 /* ... maybe discard first sample ... */
1017 if (pdata->settle_delay_usecs) {
1018 x->delay_usecs = pdata->settle_delay_usecs;
1019
1020 x++;
1021 x->tx_buf = &ts->read_z1;
1022 x->len = 1;
1023 spi_message_add_tail(x, m);
1024
1025 x++;
1026 x->rx_buf = &ts->tc.z1;
1027 x->len = 2;
1028 spi_message_add_tail(x, m);
1029 }
1030
Imre Deakda970e62007-01-18 00:44:41 -05001031 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001032 m->context = ts;
1033
1034 m++;
1035 spi_message_init(m);
1036
1037 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001038 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001039 x->tx_buf = &ts->read_z2;
1040 x->len = 1;
1041 spi_message_add_tail(x, m);
1042
1043 x++;
1044 x->rx_buf = &ts->tc.z2;
1045 x->len = 2;
1046 spi_message_add_tail(x, m);
1047
Semih Hazare4f48862007-07-18 00:35:56 -04001048 /* ... maybe discard first sample ... */
1049 if (pdata->settle_delay_usecs) {
1050 x->delay_usecs = pdata->settle_delay_usecs;
1051
1052 x++;
1053 x->tx_buf = &ts->read_z2;
1054 x->len = 1;
1055 spi_message_add_tail(x, m);
1056
1057 x++;
1058 x->rx_buf = &ts->tc.z2;
1059 x->len = 2;
1060 spi_message_add_tail(x, m);
1061 }
1062
Imre Deakda970e62007-01-18 00:44:41 -05001063 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001064 m->context = ts;
1065 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001066
1067 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001068 m++;
1069 spi_message_init(m);
1070
Imre Deak53a0ef892006-04-11 23:41:49 -04001071 x++;
1072 ts->pwrdown = PWRDOWN;
1073 x->tx_buf = &ts->pwrdown;
1074 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001075 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001076
1077 x++;
1078 x->rx_buf = &ts->dummy;
1079 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001080 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001081 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001082
Imre Deak0b7018a2006-04-11 23:42:03 -04001083 m->complete = ads7846_rx;
1084 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001085
Imre Deakd5b415c2006-04-26 00:13:18 -04001086 ts->last_msg = m;
1087
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001088 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001089 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001090 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001091 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001092 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001093 }
David Brownellffa458c2006-01-08 13:34:21 -08001094
David Brownell2c8dc072007-01-18 00:45:48 -05001095 err = ads784x_hwmon_register(spi, ts);
1096 if (err)
1097 goto err_free_irq;
1098
David Brownell2e5a7bd2006-01-08 13:34:25 -08001099 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001100
David Brownell2c8dc072007-01-18 00:45:48 -05001101 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001102 * the touchscreen, in case it's not connected.
1103 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001104 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001105 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1106
David Brownell2c8dc072007-01-18 00:45:48 -05001107 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001108 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001109 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001110
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001111 err = input_register_device(input_dev);
1112 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001113 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001114
David Brownellffa458c2006-01-08 13:34:21 -08001115 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001116
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001117 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001118 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1119 err_remove_hwmon:
1120 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001121 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001122 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001123 err_cleanup_filter:
1124 if (ts->filter_cleanup)
1125 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001126 err_free_mem:
1127 input_free_device(input_dev);
1128 kfree(ts);
1129 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001130}
1131
David Brownell2e5a7bd2006-01-08 13:34:25 -08001132static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001133{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001134 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001135
David Brownell2c8dc072007-01-18 00:45:48 -05001136 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001137 input_unregister_device(ts->input);
1138
David Brownell2e5a7bd2006-01-08 13:34:25 -08001139 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001140
David Brownell2c8dc072007-01-18 00:45:48 -05001141 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001142
Imre Deak7de90a82006-04-11 23:43:55 -04001143 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001144 /* suspend left the IRQ disabled */
1145 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001146
Imre Deakda970e62007-01-18 00:44:41 -05001147 if (ts->filter_cleanup)
1148 ts->filter_cleanup(ts->filter_data);
1149
David Brownellffa458c2006-01-08 13:34:21 -08001150 kfree(ts);
1151
David Brownell2e5a7bd2006-01-08 13:34:25 -08001152 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001153 return 0;
1154}
1155
David Brownell2e5a7bd2006-01-08 13:34:25 -08001156static struct spi_driver ads7846_driver = {
1157 .driver = {
1158 .name = "ads7846",
1159 .bus = &spi_bus_type,
1160 .owner = THIS_MODULE,
1161 },
David Brownellffa458c2006-01-08 13:34:21 -08001162 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001163 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001164 .suspend = ads7846_suspend,
1165 .resume = ads7846_resume,
1166};
1167
1168static int __init ads7846_init(void)
1169{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001170 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001171}
1172module_init(ads7846_init);
1173
1174static void __exit ads7846_exit(void)
1175{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001176 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001177}
1178module_exit(ads7846_exit);
1179
1180MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1181MODULE_LICENSE("GPL");