blob: ce6f48c695f553a703a9c9af3c739996bc2c971c [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;
David Brownell7c6d0ee2008-04-02 00:43:01 -040083 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -080084 u16 vref_delay_usecs;
85 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -040086 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -080087
Imre Deak53a0ef892006-04-11 23:41:49 -040088 u8 read_x, read_y, read_z1, read_z2, pwrdown;
89 u16 dummy; /* for the pwrdown read */
David Brownellffa458c2006-01-08 13:34:21 -080090 struct ts_event tc;
91
Semih Hazare4f48862007-07-18 00:35:56 -040092 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -040093 struct spi_message msg[5];
Imre Deakd5b415c2006-04-26 00:13:18 -040094 struct spi_message *last_msg;
Imre Deak0b7018a2006-04-11 23:42:03 -040095 int msg_idx;
96 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -040097 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -040098 int last_read;
99
100 u16 debounce_max;
101 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400102 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800103
Semih Hazar1d258912007-07-18 00:36:04 -0400104 u16 penirq_recheck_delay_usecs;
105
David Brownellffa458c2006-01-08 13:34:21 -0800106 spinlock_t lock;
Imre Deak1936d592007-01-18 00:45:31 -0500107 struct hrtimer timer;
David Brownellffa458c2006-01-08 13:34:21 -0800108 unsigned pendown:1; /* P: lock */
109 unsigned pending:1; /* P: lock */
110// FIXME remove "irq_disabled"
111 unsigned irq_disabled:1; /* P: lock */
Imre Deak7de90a82006-04-11 23:43:55 -0400112 unsigned disabled:1;
David Brownellfbb38e32007-12-14 01:26:33 -0500113 unsigned is_suspended:1;
Imre Deakc9e617a2006-04-11 23:44:05 -0400114
Imre Deakda970e62007-01-18 00:44:41 -0500115 int (*filter)(void *data, int data_idx, int *val);
116 void *filter_data;
117 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400118 int (*get_pendown_state)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800119};
120
121/* leave chip selected when we're done, for quicker re-select? */
122#if 0
123#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
124#else
125#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
126#endif
127
128/*--------------------------------------------------------------------------*/
129
130/* The ADS7846 has touchscreen and other sensors.
131 * Earlier ads784x chips are somewhat compatible.
132 */
133#define ADS_START (1 << 7)
134#define ADS_A2A1A0_d_y (1 << 4) /* differential */
135#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
136#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
137#define ADS_A2A1A0_d_x (5 << 4) /* differential */
138#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
139#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
140#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
141#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
142#define ADS_8_BIT (1 << 3)
143#define ADS_12_BIT (0 << 3)
144#define ADS_SER (1 << 2) /* non-differential */
145#define ADS_DFR (0 << 2) /* differential */
146#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
147#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
148#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
149#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
150
151#define MAX_12BIT ((1<<12)-1)
152
153/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500154#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
155 | ADS_12_BIT | ADS_DFR | \
156 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800157
Imre Deakde2defd2007-01-18 00:45:21 -0500158#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
159#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
160#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400161
Imre Deakde2defd2007-01-18 00:45:21 -0500162#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
163#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800164
165/* single-ended samples need to first power up reference voltage;
166 * we leave both ADC and VREF powered
167 */
168#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
169 | ADS_12_BIT | ADS_SER)
170
Imre Deakde2defd2007-01-18 00:45:21 -0500171#define REF_ON (READ_12BIT_DFR(x, 1, 1))
172#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800173
174/*--------------------------------------------------------------------------*/
175
176/*
177 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500178 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
179 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800180 */
181
182struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500183 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800184 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500185 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800186 u16 scratch;
187 __be16 sample;
188 struct spi_message msg;
189 struct spi_transfer xfer[6];
190};
191
Imre Deak7de90a82006-04-11 23:43:55 -0400192static void ads7846_enable(struct ads7846 *ts);
193static void ads7846_disable(struct ads7846 *ts);
194
Imre Deakc9e617a2006-04-11 23:44:05 -0400195static int device_suspended(struct device *dev)
196{
197 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellfbb38e32007-12-14 01:26:33 -0500198 return ts->is_suspended || ts->disabled;
Imre Deakc9e617a2006-04-11 23:44:05 -0400199}
200
David Brownellffa458c2006-01-08 13:34:21 -0800201static int ads7846_read12_ser(struct device *dev, unsigned command)
202{
203 struct spi_device *spi = to_spi_device(dev);
204 struct ads7846 *ts = dev_get_drvdata(dev);
Christoph Lametere94b1762006-12-06 20:33:17 -0800205 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800206 int status;
David Brownell2c8dc072007-01-18 00:45:48 -0500207 int use_internal;
David Brownellffa458c2006-01-08 13:34:21 -0800208
209 if (!req)
210 return -ENOMEM;
211
Imre Deak0b7018a2006-04-11 23:42:03 -0400212 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800213
David Brownell2c8dc072007-01-18 00:45:48 -0500214 /* FIXME boards with ads7846 might use external vref instead ... */
215 use_internal = (ts->model == 7846);
David Brownellffa458c2006-01-08 13:34:21 -0800216
David Brownell2c8dc072007-01-18 00:45:48 -0500217 /* maybe turn on internal vREF, and let it settle */
218 if (use_internal) {
219 req->ref_on = REF_ON;
220 req->xfer[0].tx_buf = &req->ref_on;
221 req->xfer[0].len = 1;
222 spi_message_add_tail(&req->xfer[0], &req->msg);
223
224 req->xfer[1].rx_buf = &req->scratch;
225 req->xfer[1].len = 2;
226
227 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
228 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
229 spi_message_add_tail(&req->xfer[1], &req->msg);
230 }
David Brownellffa458c2006-01-08 13:34:21 -0800231
232 /* take sample */
233 req->command = (u8) command;
234 req->xfer[2].tx_buf = &req->command;
235 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500236 spi_message_add_tail(&req->xfer[2], &req->msg);
237
David Brownellffa458c2006-01-08 13:34:21 -0800238 req->xfer[3].rx_buf = &req->sample;
239 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500240 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800241
242 /* REVISIT: take a few more samples, and compare ... */
243
Nicolas Ferre969111e2007-02-28 23:51:03 -0500244 /* converter in low power mode & enable PENIRQ */
245 req->ref_off = PWRDOWN;
246 req->xfer[4].tx_buf = &req->ref_off;
247 req->xfer[4].len = 1;
248 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800249
Nicolas Ferre969111e2007-02-28 23:51:03 -0500250 req->xfer[5].rx_buf = &req->scratch;
251 req->xfer[5].len = 2;
252 CS_CHANGE(req->xfer[5]);
253 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800254
Imre Deakc9e617a2006-04-11 23:44:05 -0400255 ts->irq_disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800256 disable_irq(spi->irq);
257 status = spi_sync(spi, &req->msg);
Imre Deakc9e617a2006-04-11 23:44:05 -0400258 ts->irq_disabled = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800259 enable_irq(spi->irq);
260
Marc Pignatc24b2602007-12-04 23:45:11 -0800261 if (status == 0) {
262 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400263 status = be16_to_cpu(req->sample);
264 status = status >> 3;
265 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800266 }
David Brownell90845332006-05-25 18:44:20 -0700267
268 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400269 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800270}
271
David Brownell2c8dc072007-01-18 00:45:48 -0500272#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
273
274#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800275name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
276{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500277 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800278 ssize_t v = ads7846_read12_ser(dev, \
David Brownell2c8dc072007-01-18 00:45:48 -0500279 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
David Brownellffa458c2006-01-08 13:34:21 -0800280 if (v < 0) \
281 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500282 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800283} \
284static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
285
David Brownell2c8dc072007-01-18 00:45:48 -0500286
287/* Sysfs conventions report temperatures in millidegrees Celcius.
288 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
289 * accuracy scheme without calibration data. For now we won't try either;
290 * userspace sees raw sensor values, and must scale/calibrate appropriately.
291 */
292static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
293{
294 return v;
295}
296
297SHOW(temp0, temp0, null_adjust) /* temp1_input */
298SHOW(temp1, temp1, null_adjust) /* temp2_input */
299
300
301/* sysfs conventions report voltages in millivolts. We can convert voltages
302 * if we know vREF. userspace may need to scale vAUX to match the board's
303 * external resistors; we assume that vBATT only uses the internal ones.
304 */
305static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
306{
307 unsigned retval = v;
308
309 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400310 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500311 retval = retval >> 12;
312 return retval;
313}
314
315static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
316{
317 unsigned retval = vaux_adjust(ts, v);
318
319 /* ads7846 has a resistor ladder to scale this signal down */
320 if (ts->model == 7846)
321 retval *= 4;
322 return retval;
323}
324
325SHOW(in0_input, vaux, vaux_adjust)
326SHOW(in1_input, vbatt, vbatt_adjust)
327
328
329static struct attribute *ads7846_attributes[] = {
330 &dev_attr_temp0.attr,
331 &dev_attr_temp1.attr,
332 &dev_attr_in0_input.attr,
333 &dev_attr_in1_input.attr,
334 NULL,
335};
336
337static struct attribute_group ads7846_attr_group = {
338 .attrs = ads7846_attributes,
339};
340
341static struct attribute *ads7843_attributes[] = {
342 &dev_attr_in0_input.attr,
343 &dev_attr_in1_input.attr,
344 NULL,
345};
346
347static struct attribute_group ads7843_attr_group = {
348 .attrs = ads7843_attributes,
349};
350
351static struct attribute *ads7845_attributes[] = {
352 &dev_attr_in0_input.attr,
353 NULL,
354};
355
356static struct attribute_group ads7845_attr_group = {
357 .attrs = ads7845_attributes,
358};
359
360static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
361{
Tony Jones1beeffe2007-08-20 13:46:20 -0700362 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500363 int err;
364
365 /* hwmon sensors need a reference voltage */
366 switch (ts->model) {
367 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400368 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500369 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400370 ts->vref_mv = 2500;
David Brownell2c8dc072007-01-18 00:45:48 -0500371 }
372 break;
373 case 7845:
374 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400375 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500376 dev_warn(&spi->dev,
377 "external vREF for ADS%d not specified\n",
378 ts->model);
379 return 0;
380 }
381 break;
382 }
383
384 /* different chips have different sensor groups */
385 switch (ts->model) {
386 case 7846:
387 ts->attr_group = &ads7846_attr_group;
388 break;
389 case 7845:
390 ts->attr_group = &ads7845_attr_group;
391 break;
392 case 7843:
393 ts->attr_group = &ads7843_attr_group;
394 break;
395 default:
396 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
397 return 0;
398 }
399
400 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
401 if (err)
402 return err;
403
404 hwmon = hwmon_device_register(&spi->dev);
405 if (IS_ERR(hwmon)) {
406 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
407 return PTR_ERR(hwmon);
408 }
409
410 ts->hwmon = hwmon;
411 return 0;
412}
413
414static void ads784x_hwmon_unregister(struct spi_device *spi,
415 struct ads7846 *ts)
416{
417 if (ts->hwmon) {
418 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
419 hwmon_device_unregister(ts->hwmon);
420 }
421}
422
423#else
424static inline int ads784x_hwmon_register(struct spi_device *spi,
425 struct ads7846 *ts)
426{
427 return 0;
428}
429
430static inline void ads784x_hwmon_unregister(struct spi_device *spi,
431 struct ads7846 *ts)
432{
433}
434#endif
David Brownellffa458c2006-01-08 13:34:21 -0800435
Imre Deak438f2a72006-04-11 23:41:32 -0400436static int is_pen_down(struct device *dev)
437{
David Brownell2c8dc072007-01-18 00:45:48 -0500438 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak438f2a72006-04-11 23:41:32 -0400439
440 return ts->pendown;
441}
442
443static ssize_t ads7846_pen_down_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
446 return sprintf(buf, "%u\n", is_pen_down(dev));
447}
448
449static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
450
Imre Deak7de90a82006-04-11 23:43:55 -0400451static ssize_t ads7846_disable_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
454 struct ads7846 *ts = dev_get_drvdata(dev);
455
456 return sprintf(buf, "%u\n", ts->disabled);
457}
458
459static ssize_t ads7846_disable_store(struct device *dev,
460 struct device_attribute *attr,
461 const char *buf, size_t count)
462{
463 struct ads7846 *ts = dev_get_drvdata(dev);
464 char *endp;
465 int i;
466
467 i = simple_strtoul(buf, &endp, 10);
468 spin_lock_irq(&ts->lock);
469
470 if (i)
471 ads7846_disable(ts);
472 else
473 ads7846_enable(ts);
474
475 spin_unlock_irq(&ts->lock);
476
477 return count;
478}
479
480static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
481
David Brownell2c8dc072007-01-18 00:45:48 -0500482static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500483 &dev_attr_pen_down.attr,
484 &dev_attr_disable.attr,
485 NULL,
486};
487
David Brownell2c8dc072007-01-18 00:45:48 -0500488static struct attribute_group ads784x_attr_group = {
489 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500490};
491
David Brownellffa458c2006-01-08 13:34:21 -0800492/*--------------------------------------------------------------------------*/
493
494/*
495 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
496 * to retrieve touchscreen status.
497 *
498 * The SPI transfer completion callback does the real work. It reports
499 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
500 */
501
502static void ads7846_rx(void *ads)
503{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500504 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500505 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500506 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800507
Imre Deakda970e62007-01-18 00:44:41 -0500508 /* ads7846_rx_val() did in-place conversion (including byteswap) from
509 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800510 */
Imre Deakda970e62007-01-18 00:44:41 -0500511 x = ts->tc.x;
512 y = ts->tc.y;
513 z1 = ts->tc.z1;
514 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800515
516 /* range filtering */
517 if (x == MAX_12BIT)
518 x = 0;
519
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400520 if (ts->model == 7843) {
521 Rt = ts->pressure_max / 2;
522 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800523 /* compute touch pressure resistance using equation #2 */
524 Rt = z2;
525 Rt -= z1;
526 Rt *= x;
527 Rt *= ts->x_plate_ohms;
528 Rt /= z1;
529 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400530 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800531 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400532 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500533
Imre Deakd5b415c2006-04-26 00:13:18 -0400534 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500535 * the maximum. Don't report it to user space, repeat at least
536 * once more the measurement
537 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400538 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500539#ifdef VERBOSE
540 pr_debug("%s: ignored %d pressure %d\n",
541 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
542#endif
Imre Deak1936d592007-01-18 00:45:31 -0500543 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800544 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400545 return;
546 }
547
Semih Hazar1d258912007-07-18 00:36:04 -0400548 /* Maybe check the pendown state before reporting. This discards
549 * false readings when the pen is lifted.
550 */
551 if (ts->penirq_recheck_delay_usecs) {
552 udelay(ts->penirq_recheck_delay_usecs);
553 if (!ts->get_pendown_state())
554 Rt = 0;
555 }
556
Imre Deak15e35892007-01-18 00:45:43 -0500557 /* NOTE: We can't rely on the pressure to determine the pen down
558 * state, even this controller has a pressure sensor. The pressure
559 * value can fluctuate for quite a while after lifting the pen and
560 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800561 *
Imre Deak15e35892007-01-18 00:45:43 -0500562 * The only safe way to check for the pen up condition is in the
563 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800564 */
David Brownellffa458c2006-01-08 13:34:21 -0800565 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500566 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400567
Imre Deak15e35892007-01-18 00:45:43 -0500568 if (!ts->pendown) {
569 input_report_key(input, BTN_TOUCH, 1);
570 ts->pendown = 1;
571#ifdef VERBOSE
572 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800573#endif
Imre Deak15e35892007-01-18 00:45:43 -0500574 }
575 input_report_abs(input, ABS_X, x);
576 input_report_abs(input, ABS_Y, y);
577 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800578
Imre Deak15e35892007-01-18 00:45:43 -0500579 input_sync(input);
580#ifdef VERBOSE
581 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
582#endif
583 }
David Brownellffa458c2006-01-08 13:34:21 -0800584
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800585 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
586 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800587}
588
Imre Deakda970e62007-01-18 00:44:41 -0500589static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400590{
591 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400592
Imre Deakda970e62007-01-18 00:44:41 -0500593 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
594 /* Start over collecting consistent readings. */
595 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400596 /* Repeat it, if this was the first read or the read
597 * wasn't consistent enough. */
598 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500599 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400600 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500601 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400602 } else {
603 /* Maximum number of debouncing reached and still
604 * not enough number of consistent readings. Abort
605 * the whole sample, repeat it in the next sampling
606 * period.
607 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400608 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500609 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400610 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400611 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400612 if (++ts->read_rep > ts->debounce_rep) {
613 /* Got a good reading for this coordinate,
614 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400615 ts->read_cnt = 0;
616 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500617 return ADS7846_FILTER_OK;
618 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400619 /* Read more values that are consistent. */
620 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500621 return ADS7846_FILTER_REPEAT;
622 }
623 }
624}
625
626static int ads7846_no_filter(void *ads, int data_idx, int *val)
627{
628 return ADS7846_FILTER_OK;
629}
630
631static void ads7846_rx_val(void *ads)
632{
633 struct ads7846 *ts = ads;
634 struct spi_message *m;
635 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500636 int val;
637 int action;
638 int status;
639
640 m = &ts->msg[ts->msg_idx];
641 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500642
643 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
644 * built from two 8 bit values written msb-first.
645 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400646 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500647
648 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
649 switch (action) {
650 case ADS7846_FILTER_REPEAT:
651 break;
652 case ADS7846_FILTER_IGNORE:
653 ts->tc.ignore = 1;
654 /* Last message will contain ads7846_rx() as the
655 * completion function.
656 */
657 m = ts->last_msg;
658 break;
659 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400660 *(u16 *)t->rx_buf = val;
Imre Deakda970e62007-01-18 00:44:41 -0500661 ts->tc.ignore = 0;
662 m = &ts->msg[++ts->msg_idx];
663 break;
664 default:
665 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400666 }
667 status = spi_async(ts->spi, m);
668 if (status)
669 dev_err(&ts->spi->dev, "spi_async --> %d\n",
670 status);
671}
672
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800673static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800674{
Imre Deak1936d592007-01-18 00:45:31 -0500675 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800676 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800677
Imre Deakc9e617a2006-04-11 23:44:05 -0400678 spin_lock_irq(&ts->lock);
679
Imre Deak15e35892007-01-18 00:45:43 -0500680 if (unlikely(!ts->get_pendown_state() ||
681 device_suspended(&ts->spi->dev))) {
682 if (ts->pendown) {
683 struct input_dev *input = ts->input;
684
685 input_report_key(input, BTN_TOUCH, 0);
686 input_report_abs(input, ABS_PRESSURE, 0);
687 input_sync(input);
688
689 ts->pendown = 0;
690#ifdef VERBOSE
691 dev_dbg(&ts->spi->dev, "UP\n");
692#endif
693 }
694
David Brownell90845332006-05-25 18:44:20 -0700695 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400696 if (!device_suspended(&ts->spi->dev)) {
697 ts->irq_disabled = 0;
698 enable_irq(ts->spi->irq);
699 }
700 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400701 } else {
702 /* pen is still down, continue with the measurement */
703 ts->msg_idx = 0;
704 status = spi_async(ts->spi, &ts->msg[0]);
705 if (status)
706 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
707 }
708
709 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500710 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800711}
712
David Howells7d12e782006-10-05 14:55:46 +0100713static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800714{
Imre Deak0b7018a2006-04-11 23:42:03 -0400715 struct ads7846 *ts = handle;
716 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400717
718 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400719 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400720 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700721 /* The ARM do_simple_IRQ() dispatcher doesn't act
722 * like the other dispatchers: it will report IRQs
723 * even after they've been disabled. We work around
724 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400725 */
726 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400727 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400728 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500729 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800730 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400731 }
732 }
733 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400734
735 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800736}
737
738/*--------------------------------------------------------------------------*/
739
Imre Deak7de90a82006-04-11 23:43:55 -0400740/* Must be called with ts->lock held */
741static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800742{
Imre Deak7de90a82006-04-11 23:43:55 -0400743 if (ts->disabled)
744 return;
David Brownellffa458c2006-01-08 13:34:21 -0800745
Imre Deakc9e617a2006-04-11 23:44:05 -0400746 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800747
Imre Deakc9e617a2006-04-11 23:44:05 -0400748 /* are we waiting for IRQ, or polling? */
749 if (!ts->pending) {
750 ts->irq_disabled = 1;
751 disable_irq(ts->spi->irq);
752 } else {
753 /* the timer will run at least once more, and
754 * leave everything in a clean state, IRQ disabled
755 */
756 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400757 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400758 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400759 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800760 }
761 }
762
763 /* we know the chip's in lowpower mode since we always
764 * leave it that way after every request
765 */
766
Imre Deak7de90a82006-04-11 23:43:55 -0400767}
768
769/* Must be called with ts->lock held */
770static void ads7846_enable(struct ads7846 *ts)
771{
772 if (!ts->disabled)
773 return;
774
775 ts->disabled = 0;
776 ts->irq_disabled = 0;
777 enable_irq(ts->spi->irq);
778}
779
780static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
781{
782 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
783
784 spin_lock_irq(&ts->lock);
785
David Brownellfbb38e32007-12-14 01:26:33 -0500786 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400787 ads7846_disable(ts);
788
789 spin_unlock_irq(&ts->lock);
790
David Brownellffa458c2006-01-08 13:34:21 -0800791 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400792
David Brownellffa458c2006-01-08 13:34:21 -0800793}
794
David Brownell2e5a7bd2006-01-08 13:34:25 -0800795static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800796{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800797 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800798
Imre Deak7de90a82006-04-11 23:43:55 -0400799 spin_lock_irq(&ts->lock);
800
David Brownellfbb38e32007-12-14 01:26:33 -0500801 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400802 ads7846_enable(ts);
803
804 spin_unlock_irq(&ts->lock);
805
David Brownellffa458c2006-01-08 13:34:21 -0800806 return 0;
807}
808
David Brownell2e5a7bd2006-01-08 13:34:25 -0800809static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800810{
David Brownellffa458c2006-01-08 13:34:21 -0800811 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500812 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800813 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400814 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800815 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500816 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500817 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800818
819 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800820 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800821 return -ENODEV;
822 }
823
824 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800825 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800826 return -ENODEV;
827 }
828
829 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500830 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800831 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500832 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800833 return -EINVAL;
834 }
835
David Brownell90845332006-05-25 18:44:20 -0700836 /* REVISIT when the irq can be triggered active-low, or if for some
837 * reason the touchscreen isn't hooked up, we don't need to access
838 * the pendown state.
839 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400840 if (pdata->get_pendown_state == NULL) {
841 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
842 return -EINVAL;
843 }
844
David Brownell90845332006-05-25 18:44:20 -0700845 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
846 * that even if the hardware can do that, the SPI controller driver
847 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800848 */
David Brownell90845332006-05-25 18:44:20 -0700849 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400850 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500851 err = spi_setup(spi);
852 if (err < 0)
853 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800854
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500855 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
856 input_dev = input_allocate_device();
857 if (!ts || !input_dev) {
858 err = -ENOMEM;
859 goto err_free_mem;
860 }
David Brownellffa458c2006-01-08 13:34:21 -0800861
David Brownell2e5a7bd2006-01-08 13:34:25 -0800862 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800863
864 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500865 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400866 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800867
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800868 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800869 ts->timer.function = ads7846_timer;
870
Imre Deak7de90a82006-04-11 23:43:55 -0400871 spin_lock_init(&ts->lock);
872
David Brownellffa458c2006-01-08 13:34:21 -0800873 ts->model = pdata->model ? : 7846;
874 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
875 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400876 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500877
878 if (pdata->filter != NULL) {
879 if (pdata->filter_init != NULL) {
880 err = pdata->filter_init(pdata, &ts->filter_data);
881 if (err < 0)
882 goto err_free_mem;
883 }
884 ts->filter = pdata->filter;
885 ts->filter_cleanup = pdata->filter_cleanup;
886 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400887 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500888 if (ts->debounce_max < 2)
889 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400890 ts->debounce_tol = pdata->debounce_tol;
891 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500892 ts->filter = ads7846_debounce;
893 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400894 } else
Imre Deakda970e62007-01-18 00:44:41 -0500895 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400896 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800897
Semih Hazar1d258912007-07-18 00:36:04 -0400898 if (pdata->penirq_recheck_delay_usecs)
899 ts->penirq_recheck_delay_usecs =
900 pdata->penirq_recheck_delay_usecs;
901
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500902 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800903
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500904 input_dev->name = "ADS784x Touchscreen";
905 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400906 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800907
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700908 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
909 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500910 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800911 pdata->x_min ? : 0,
912 pdata->x_max ? : MAX_12BIT,
913 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500914 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800915 pdata->y_min ? : 0,
916 pdata->y_max ? : MAX_12BIT,
917 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500918 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800919 pdata->pressure_min, pdata->pressure_max, 0, 0);
920
Imre Deakde2defd2007-01-18 00:45:21 -0500921 vref = pdata->keep_vref_on;
922
David Brownellffa458c2006-01-08 13:34:21 -0800923 /* set up the transfers to read touchscreen state; this assumes we
924 * use formula #2 for pressure, not #3.
925 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400926 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800927 x = ts->xfer;
928
Imre Deak0b7018a2006-04-11 23:42:03 -0400929 spi_message_init(m);
930
David Brownellffa458c2006-01-08 13:34:21 -0800931 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500932 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500933 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800934 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400935 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500936
David Brownellffa458c2006-01-08 13:34:21 -0800937 x++;
938 x->rx_buf = &ts->tc.y;
939 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400940 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800941
Semih Hazare4f48862007-07-18 00:35:56 -0400942 /* the first sample after switching drivers can be low quality;
943 * optionally discard it, using a second one after the signals
944 * have had enough time to stabilize.
945 */
946 if (pdata->settle_delay_usecs) {
947 x->delay_usecs = pdata->settle_delay_usecs;
948
949 x++;
950 x->tx_buf = &ts->read_y;
951 x->len = 1;
952 spi_message_add_tail(x, m);
953
954 x++;
955 x->rx_buf = &ts->tc.y;
956 x->len = 2;
957 spi_message_add_tail(x, m);
958 }
959
Imre Deakda970e62007-01-18 00:44:41 -0500960 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400961 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500962
Imre Deak0b7018a2006-04-11 23:42:03 -0400963 m++;
964 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800965
966 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500967 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500968 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500969 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800970 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400971 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500972
David Brownellffa458c2006-01-08 13:34:21 -0800973 x++;
974 x->rx_buf = &ts->tc.x;
975 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400976 spi_message_add_tail(x, m);
977
Semih Hazare4f48862007-07-18 00:35:56 -0400978 /* ... maybe discard first sample ... */
979 if (pdata->settle_delay_usecs) {
980 x->delay_usecs = pdata->settle_delay_usecs;
981
982 x++;
983 x->tx_buf = &ts->read_x;
984 x->len = 1;
985 spi_message_add_tail(x, m);
986
987 x++;
988 x->rx_buf = &ts->tc.x;
989 x->len = 2;
990 spi_message_add_tail(x, m);
991 }
992
Imre Deakda970e62007-01-18 00:44:41 -0500993 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400994 m->context = ts;
995
996 /* turn y+ off, x- on; we'll use formula #2 */
997 if (ts->model == 7846) {
998 m++;
999 spi_message_init(m);
1000
1001 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001002 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001003 x->tx_buf = &ts->read_z1;
1004 x->len = 1;
1005 spi_message_add_tail(x, m);
1006
1007 x++;
1008 x->rx_buf = &ts->tc.z1;
1009 x->len = 2;
1010 spi_message_add_tail(x, m);
1011
Semih Hazare4f48862007-07-18 00:35:56 -04001012 /* ... maybe discard first sample ... */
1013 if (pdata->settle_delay_usecs) {
1014 x->delay_usecs = pdata->settle_delay_usecs;
1015
1016 x++;
1017 x->tx_buf = &ts->read_z1;
1018 x->len = 1;
1019 spi_message_add_tail(x, m);
1020
1021 x++;
1022 x->rx_buf = &ts->tc.z1;
1023 x->len = 2;
1024 spi_message_add_tail(x, m);
1025 }
1026
Imre Deakda970e62007-01-18 00:44:41 -05001027 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001028 m->context = ts;
1029
1030 m++;
1031 spi_message_init(m);
1032
1033 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001034 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001035 x->tx_buf = &ts->read_z2;
1036 x->len = 1;
1037 spi_message_add_tail(x, m);
1038
1039 x++;
1040 x->rx_buf = &ts->tc.z2;
1041 x->len = 2;
1042 spi_message_add_tail(x, m);
1043
Semih Hazare4f48862007-07-18 00:35:56 -04001044 /* ... maybe discard first sample ... */
1045 if (pdata->settle_delay_usecs) {
1046 x->delay_usecs = pdata->settle_delay_usecs;
1047
1048 x++;
1049 x->tx_buf = &ts->read_z2;
1050 x->len = 1;
1051 spi_message_add_tail(x, m);
1052
1053 x++;
1054 x->rx_buf = &ts->tc.z2;
1055 x->len = 2;
1056 spi_message_add_tail(x, m);
1057 }
1058
Imre Deakda970e62007-01-18 00:44:41 -05001059 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001060 m->context = ts;
1061 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001062
1063 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001064 m++;
1065 spi_message_init(m);
1066
Imre Deak53a0ef892006-04-11 23:41:49 -04001067 x++;
1068 ts->pwrdown = PWRDOWN;
1069 x->tx_buf = &ts->pwrdown;
1070 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001071 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001072
1073 x++;
1074 x->rx_buf = &ts->dummy;
1075 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001076 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001077 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001078
Imre Deak0b7018a2006-04-11 23:42:03 -04001079 m->complete = ads7846_rx;
1080 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001081
Imre Deakd5b415c2006-04-26 00:13:18 -04001082 ts->last_msg = m;
1083
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001084 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001085 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001086 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001087 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001088 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001089 }
David Brownellffa458c2006-01-08 13:34:21 -08001090
David Brownell2c8dc072007-01-18 00:45:48 -05001091 err = ads784x_hwmon_register(spi, ts);
1092 if (err)
1093 goto err_free_irq;
1094
David Brownell2e5a7bd2006-01-08 13:34:25 -08001095 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001096
David Brownell2c8dc072007-01-18 00:45:48 -05001097 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001098 * the touchscreen, in case it's not connected.
1099 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001100 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001101 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1102
David Brownell2c8dc072007-01-18 00:45:48 -05001103 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001104 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001105 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001106
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001107 err = input_register_device(input_dev);
1108 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001109 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001110
David Brownellffa458c2006-01-08 13:34:21 -08001111 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001112
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001113 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001114 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1115 err_remove_hwmon:
1116 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001117 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001118 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001119 err_cleanup_filter:
1120 if (ts->filter_cleanup)
1121 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001122 err_free_mem:
1123 input_free_device(input_dev);
1124 kfree(ts);
1125 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001126}
1127
David Brownell2e5a7bd2006-01-08 13:34:25 -08001128static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001129{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001130 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001131
David Brownell2c8dc072007-01-18 00:45:48 -05001132 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001133 input_unregister_device(ts->input);
1134
David Brownell2e5a7bd2006-01-08 13:34:25 -08001135 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001136
David Brownell2c8dc072007-01-18 00:45:48 -05001137 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001138
Imre Deak7de90a82006-04-11 23:43:55 -04001139 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001140 /* suspend left the IRQ disabled */
1141 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001142
Imre Deakda970e62007-01-18 00:44:41 -05001143 if (ts->filter_cleanup)
1144 ts->filter_cleanup(ts->filter_data);
1145
David Brownellffa458c2006-01-08 13:34:21 -08001146 kfree(ts);
1147
David Brownell2e5a7bd2006-01-08 13:34:25 -08001148 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001149 return 0;
1150}
1151
David Brownell2e5a7bd2006-01-08 13:34:25 -08001152static struct spi_driver ads7846_driver = {
1153 .driver = {
1154 .name = "ads7846",
1155 .bus = &spi_bus_type,
1156 .owner = THIS_MODULE,
1157 },
David Brownellffa458c2006-01-08 13:34:21 -08001158 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001159 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001160 .suspend = ads7846_suspend,
1161 .resume = ads7846_resume,
1162};
1163
1164static int __init ads7846_init(void)
1165{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001166 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001167}
1168module_init(ads7846_init);
1169
1170static void __exit ads7846_exit(void)
1171{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001172 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001173}
1174module_exit(ads7846_exit);
1175
1176MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1177MODULE_LICENSE("GPL");