blob: efbbbe48621a4b628ac0d72ea3ea49ae85f664d0 [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);
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400464 long i;
Imre Deak7de90a82006-04-11 23:43:55 -0400465
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400466 if (strict_strtoul(buf, 10, &i))
467 return -EINVAL;
468
Imre Deak7de90a82006-04-11 23:43:55 -0400469 spin_lock_irq(&ts->lock);
470
471 if (i)
472 ads7846_disable(ts);
473 else
474 ads7846_enable(ts);
475
476 spin_unlock_irq(&ts->lock);
477
478 return count;
479}
480
481static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
482
David Brownell2c8dc072007-01-18 00:45:48 -0500483static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500484 &dev_attr_pen_down.attr,
485 &dev_attr_disable.attr,
486 NULL,
487};
488
David Brownell2c8dc072007-01-18 00:45:48 -0500489static struct attribute_group ads784x_attr_group = {
490 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500491};
492
David Brownellffa458c2006-01-08 13:34:21 -0800493/*--------------------------------------------------------------------------*/
494
495/*
496 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
497 * to retrieve touchscreen status.
498 *
499 * The SPI transfer completion callback does the real work. It reports
500 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
501 */
502
503static void ads7846_rx(void *ads)
504{
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500505 struct ads7846 *ts = ads;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500506 unsigned Rt;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500507 u16 x, y, z1, z2;
David Brownellffa458c2006-01-08 13:34:21 -0800508
Imre Deakda970e62007-01-18 00:44:41 -0500509 /* ads7846_rx_val() did in-place conversion (including byteswap) from
510 * on-the-wire format as part of debouncing to get stable readings.
David Brownellffa458c2006-01-08 13:34:21 -0800511 */
Imre Deakda970e62007-01-18 00:44:41 -0500512 x = ts->tc.x;
513 y = ts->tc.y;
514 z1 = ts->tc.z1;
515 z2 = ts->tc.z2;
David Brownellffa458c2006-01-08 13:34:21 -0800516
517 /* range filtering */
518 if (x == MAX_12BIT)
519 x = 0;
520
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400521 if (ts->model == 7843) {
522 Rt = ts->pressure_max / 2;
523 } else 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;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400531 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800532 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400533 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500534
Imre Deakd5b415c2006-04-26 00:13:18 -0400535 /* Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500536 * the maximum. Don't report it to user space, repeat at least
537 * once more the measurement
538 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400539 if (ts->tc.ignore || Rt > ts->pressure_max) {
Imre Deak15e35892007-01-18 00:45:43 -0500540#ifdef VERBOSE
541 pr_debug("%s: ignored %d pressure %d\n",
542 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
543#endif
Imre Deak1936d592007-01-18 00:45:31 -0500544 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800545 HRTIMER_MODE_REL);
Imre Deakd5b415c2006-04-26 00:13:18 -0400546 return;
547 }
548
Semih Hazar1d258912007-07-18 00:36:04 -0400549 /* Maybe check the pendown state before reporting. This discards
550 * false readings when the pen is lifted.
551 */
552 if (ts->penirq_recheck_delay_usecs) {
553 udelay(ts->penirq_recheck_delay_usecs);
554 if (!ts->get_pendown_state())
555 Rt = 0;
556 }
557
Imre Deak15e35892007-01-18 00:45:43 -0500558 /* NOTE: We can't rely on the pressure to determine the pen down
559 * state, even this controller has a pressure sensor. The pressure
560 * value can fluctuate for quite a while after lifting the pen and
561 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800562 *
Imre Deak15e35892007-01-18 00:45:43 -0500563 * The only safe way to check for the pen up condition is in the
564 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800565 */
David Brownellffa458c2006-01-08 13:34:21 -0800566 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500567 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400568
Imre Deak15e35892007-01-18 00:45:43 -0500569 if (!ts->pendown) {
570 input_report_key(input, BTN_TOUCH, 1);
571 ts->pendown = 1;
572#ifdef VERBOSE
573 dev_dbg(&ts->spi->dev, "DOWN\n");
David Brownellffa458c2006-01-08 13:34:21 -0800574#endif
Imre Deak15e35892007-01-18 00:45:43 -0500575 }
576 input_report_abs(input, ABS_X, x);
577 input_report_abs(input, ABS_Y, y);
578 input_report_abs(input, ABS_PRESSURE, Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800579
Imre Deak15e35892007-01-18 00:45:43 -0500580 input_sync(input);
581#ifdef VERBOSE
582 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
583#endif
584 }
David Brownellffa458c2006-01-08 13:34:21 -0800585
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800586 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
587 HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800588}
589
Imre Deakda970e62007-01-18 00:44:41 -0500590static int ads7846_debounce(void *ads, int data_idx, int *val)
Imre Deak0b7018a2006-04-11 23:42:03 -0400591{
592 struct ads7846 *ts = ads;
Imre Deak0b7018a2006-04-11 23:42:03 -0400593
Imre Deakda970e62007-01-18 00:44:41 -0500594 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
595 /* Start over collecting consistent readings. */
596 ts->read_rep = 0;
Imre Deakd5b415c2006-04-26 00:13:18 -0400597 /* Repeat it, if this was the first read or the read
598 * wasn't consistent enough. */
599 if (ts->read_cnt < ts->debounce_max) {
Imre Deakda970e62007-01-18 00:44:41 -0500600 ts->last_read = *val;
Imre Deakd5b415c2006-04-26 00:13:18 -0400601 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500602 return ADS7846_FILTER_REPEAT;
Imre Deakd5b415c2006-04-26 00:13:18 -0400603 } else {
604 /* Maximum number of debouncing reached and still
605 * not enough number of consistent readings. Abort
606 * the whole sample, repeat it in the next sampling
607 * period.
608 */
Imre Deakd5b415c2006-04-26 00:13:18 -0400609 ts->read_cnt = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500610 return ADS7846_FILTER_IGNORE;
Imre Deakd5b415c2006-04-26 00:13:18 -0400611 }
Imre Deak0b7018a2006-04-11 23:42:03 -0400612 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400613 if (++ts->read_rep > ts->debounce_rep) {
614 /* Got a good reading for this coordinate,
615 * go for the next one. */
Imre Deakd5b415c2006-04-26 00:13:18 -0400616 ts->read_cnt = 0;
617 ts->read_rep = 0;
Imre Deakda970e62007-01-18 00:44:41 -0500618 return ADS7846_FILTER_OK;
619 } else {
Imre Deakd5b415c2006-04-26 00:13:18 -0400620 /* Read more values that are consistent. */
621 ts->read_cnt++;
Imre Deakda970e62007-01-18 00:44:41 -0500622 return ADS7846_FILTER_REPEAT;
623 }
624 }
625}
626
627static int ads7846_no_filter(void *ads, int data_idx, int *val)
628{
629 return ADS7846_FILTER_OK;
630}
631
632static void ads7846_rx_val(void *ads)
633{
634 struct ads7846 *ts = ads;
635 struct spi_message *m;
636 struct spi_transfer *t;
Imre Deakda970e62007-01-18 00:44:41 -0500637 int val;
638 int action;
639 int status;
640
641 m = &ts->msg[ts->msg_idx];
642 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
Imre Deakda970e62007-01-18 00:44:41 -0500643
644 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
645 * built from two 8 bit values written msb-first.
646 */
Harvey Harrison494f6852008-07-23 14:16:19 -0400647 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
Imre Deakda970e62007-01-18 00:44:41 -0500648
649 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
650 switch (action) {
651 case ADS7846_FILTER_REPEAT:
652 break;
653 case ADS7846_FILTER_IGNORE:
654 ts->tc.ignore = 1;
655 /* Last message will contain ads7846_rx() as the
656 * completion function.
657 */
658 m = ts->last_msg;
659 break;
660 case ADS7846_FILTER_OK:
Harvey Harrison494f6852008-07-23 14:16:19 -0400661 *(u16 *)t->rx_buf = val;
Imre Deakda970e62007-01-18 00:44:41 -0500662 ts->tc.ignore = 0;
663 m = &ts->msg[++ts->msg_idx];
664 break;
665 default:
666 BUG();
Imre Deak0b7018a2006-04-11 23:42:03 -0400667 }
668 status = spi_async(ts->spi, m);
669 if (status)
670 dev_err(&ts->spi->dev, "spi_async --> %d\n",
671 status);
672}
673
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800674static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800675{
Imre Deak1936d592007-01-18 00:45:31 -0500676 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
David Brownellffa458c2006-01-08 13:34:21 -0800677 int status = 0;
David Brownellffa458c2006-01-08 13:34:21 -0800678
Imre Deakc9e617a2006-04-11 23:44:05 -0400679 spin_lock_irq(&ts->lock);
680
Imre Deak15e35892007-01-18 00:45:43 -0500681 if (unlikely(!ts->get_pendown_state() ||
682 device_suspended(&ts->spi->dev))) {
683 if (ts->pendown) {
684 struct input_dev *input = ts->input;
685
686 input_report_key(input, BTN_TOUCH, 0);
687 input_report_abs(input, ABS_PRESSURE, 0);
688 input_sync(input);
689
690 ts->pendown = 0;
691#ifdef VERBOSE
692 dev_dbg(&ts->spi->dev, "UP\n");
693#endif
694 }
695
David Brownell90845332006-05-25 18:44:20 -0700696 /* measurement cycle ended */
Imre Deakc9e617a2006-04-11 23:44:05 -0400697 if (!device_suspended(&ts->spi->dev)) {
698 ts->irq_disabled = 0;
699 enable_irq(ts->spi->irq);
700 }
701 ts->pending = 0;
Imre Deakc9e617a2006-04-11 23:44:05 -0400702 } else {
703 /* pen is still down, continue with the measurement */
704 ts->msg_idx = 0;
705 status = spi_async(ts->spi, &ts->msg[0]);
706 if (status)
707 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
708 }
709
710 spin_unlock_irq(&ts->lock);
Imre Deak1936d592007-01-18 00:45:31 -0500711 return HRTIMER_NORESTART;
David Brownellffa458c2006-01-08 13:34:21 -0800712}
713
David Howells7d12e782006-10-05 14:55:46 +0100714static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800715{
Imre Deak0b7018a2006-04-11 23:42:03 -0400716 struct ads7846 *ts = handle;
717 unsigned long flags;
Imre Deak0b7018a2006-04-11 23:42:03 -0400718
719 spin_lock_irqsave(&ts->lock, flags);
Imre Deakc9e617a2006-04-11 23:44:05 -0400720 if (likely(ts->get_pendown_state())) {
Imre Deak0b7018a2006-04-11 23:42:03 -0400721 if (!ts->irq_disabled) {
David Brownell90845332006-05-25 18:44:20 -0700722 /* The ARM do_simple_IRQ() dispatcher doesn't act
723 * like the other dispatchers: it will report IRQs
724 * even after they've been disabled. We work around
725 * that here. (The "generic irq" framework may help...)
Imre Deak0b7018a2006-04-11 23:42:03 -0400726 */
727 ts->irq_disabled = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400728 disable_irq(ts->spi->irq);
Imre Deak0b7018a2006-04-11 23:42:03 -0400729 ts->pending = 1;
Imre Deak1936d592007-01-18 00:45:31 -0500730 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800731 HRTIMER_MODE_REL);
Imre Deak0b7018a2006-04-11 23:42:03 -0400732 }
733 }
734 spin_unlock_irqrestore(&ts->lock, flags);
Imre Deak7de90a82006-04-11 23:43:55 -0400735
736 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800737}
738
739/*--------------------------------------------------------------------------*/
740
Imre Deak7de90a82006-04-11 23:43:55 -0400741/* Must be called with ts->lock held */
742static void ads7846_disable(struct ads7846 *ts)
David Brownellffa458c2006-01-08 13:34:21 -0800743{
Imre Deak7de90a82006-04-11 23:43:55 -0400744 if (ts->disabled)
745 return;
David Brownellffa458c2006-01-08 13:34:21 -0800746
Imre Deakc9e617a2006-04-11 23:44:05 -0400747 ts->disabled = 1;
David Brownellffa458c2006-01-08 13:34:21 -0800748
Imre Deakc9e617a2006-04-11 23:44:05 -0400749 /* are we waiting for IRQ, or polling? */
750 if (!ts->pending) {
751 ts->irq_disabled = 1;
752 disable_irq(ts->spi->irq);
753 } else {
754 /* the timer will run at least once more, and
755 * leave everything in a clean state, IRQ disabled
756 */
757 while (ts->pending) {
Imre Deak7de90a82006-04-11 23:43:55 -0400758 spin_unlock_irq(&ts->lock);
Juha Yrjolac4febb92006-04-11 23:42:25 -0400759 msleep(1);
Imre Deak7de90a82006-04-11 23:43:55 -0400760 spin_lock_irq(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800761 }
762 }
763
764 /* we know the chip's in lowpower mode since we always
765 * leave it that way after every request
766 */
767
Imre Deak7de90a82006-04-11 23:43:55 -0400768}
769
770/* Must be called with ts->lock held */
771static void ads7846_enable(struct ads7846 *ts)
772{
773 if (!ts->disabled)
774 return;
775
776 ts->disabled = 0;
777 ts->irq_disabled = 0;
778 enable_irq(ts->spi->irq);
779}
780
781static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
782{
783 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
784
785 spin_lock_irq(&ts->lock);
786
David Brownellfbb38e32007-12-14 01:26:33 -0500787 ts->is_suspended = 1;
Imre Deak7de90a82006-04-11 23:43:55 -0400788 ads7846_disable(ts);
789
790 spin_unlock_irq(&ts->lock);
791
David Brownellffa458c2006-01-08 13:34:21 -0800792 return 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400793
David Brownellffa458c2006-01-08 13:34:21 -0800794}
795
David Brownell2e5a7bd2006-01-08 13:34:25 -0800796static int ads7846_resume(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800797{
David Brownell2e5a7bd2006-01-08 13:34:25 -0800798 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -0800799
Imre Deak7de90a82006-04-11 23:43:55 -0400800 spin_lock_irq(&ts->lock);
801
David Brownellfbb38e32007-12-14 01:26:33 -0500802 ts->is_suspended = 0;
Imre Deak7de90a82006-04-11 23:43:55 -0400803 ads7846_enable(ts);
804
805 spin_unlock_irq(&ts->lock);
806
David Brownellffa458c2006-01-08 13:34:21 -0800807 return 0;
808}
809
David Brownell2e5a7bd2006-01-08 13:34:25 -0800810static int __devinit ads7846_probe(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -0800811{
David Brownellffa458c2006-01-08 13:34:21 -0800812 struct ads7846 *ts;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500813 struct input_dev *input_dev;
David Brownell2e5a7bd2006-01-08 13:34:25 -0800814 struct ads7846_platform_data *pdata = spi->dev.platform_data;
Imre Deak0b7018a2006-04-11 23:42:03 -0400815 struct spi_message *m;
David Brownellffa458c2006-01-08 13:34:21 -0800816 struct spi_transfer *x;
Imre Deakde2defd2007-01-18 00:45:21 -0500817 int vref;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500818 int err;
David Brownellffa458c2006-01-08 13:34:21 -0800819
820 if (!spi->irq) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800821 dev_dbg(&spi->dev, "no IRQ?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800822 return -ENODEV;
823 }
824
825 if (!pdata) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800826 dev_dbg(&spi->dev, "no platform data?\n");
David Brownellffa458c2006-01-08 13:34:21 -0800827 return -ENODEV;
828 }
829
830 /* don't exceed max specified sample rate */
David Brownelld93f70b2006-02-15 00:49:35 -0500831 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -0800832 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
David Brownelld93f70b2006-02-15 00:49:35 -0500833 (spi->max_speed_hz/SAMPLE_BITS)/1000);
David Brownellffa458c2006-01-08 13:34:21 -0800834 return -EINVAL;
835 }
836
David Brownell90845332006-05-25 18:44:20 -0700837 /* REVISIT when the irq can be triggered active-low, or if for some
838 * reason the touchscreen isn't hooked up, we don't need to access
839 * the pendown state.
840 */
Imre Deakc9e617a2006-04-11 23:44:05 -0400841 if (pdata->get_pendown_state == NULL) {
842 dev_dbg(&spi->dev, "no get_pendown_state function?\n");
843 return -EINVAL;
844 }
845
David Brownell90845332006-05-25 18:44:20 -0700846 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
847 * that even if the hardware can do that, the SPI controller driver
848 * may not. So we stick to very-portable 8 bit words, both RX and TX.
David Brownellffa458c2006-01-08 13:34:21 -0800849 */
David Brownell90845332006-05-25 18:44:20 -0700850 spi->bits_per_word = 8;
Semih Hazar230ffc82007-05-22 23:35:12 -0400851 spi->mode = SPI_MODE_0;
Imre Deak7937e862007-01-18 00:45:38 -0500852 err = spi_setup(spi);
853 if (err < 0)
854 return err;
David Brownellffa458c2006-01-08 13:34:21 -0800855
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500856 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
857 input_dev = input_allocate_device();
858 if (!ts || !input_dev) {
859 err = -ENOMEM;
860 goto err_free_mem;
861 }
David Brownellffa458c2006-01-08 13:34:21 -0800862
David Brownell2e5a7bd2006-01-08 13:34:25 -0800863 dev_set_drvdata(&spi->dev, ts);
David Brownellffa458c2006-01-08 13:34:21 -0800864
865 ts->spi = spi;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500866 ts->input = input_dev;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400867 ts->vref_mv = pdata->vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800868
Thomas Gleixnerc9cb2e32007-02-16 01:27:49 -0800869 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
David Brownellffa458c2006-01-08 13:34:21 -0800870 ts->timer.function = ads7846_timer;
871
Imre Deak7de90a82006-04-11 23:43:55 -0400872 spin_lock_init(&ts->lock);
873
David Brownellffa458c2006-01-08 13:34:21 -0800874 ts->model = pdata->model ? : 7846;
875 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
876 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
Imre Deakd5b415c2006-04-26 00:13:18 -0400877 ts->pressure_max = pdata->pressure_max ? : ~0;
Imre Deakda970e62007-01-18 00:44:41 -0500878
879 if (pdata->filter != NULL) {
880 if (pdata->filter_init != NULL) {
881 err = pdata->filter_init(pdata, &ts->filter_data);
882 if (err < 0)
883 goto err_free_mem;
884 }
885 ts->filter = pdata->filter;
886 ts->filter_cleanup = pdata->filter_cleanup;
887 } else if (pdata->debounce_max) {
Imre Deakd5b415c2006-04-26 00:13:18 -0400888 ts->debounce_max = pdata->debounce_max;
Imre Deakda970e62007-01-18 00:44:41 -0500889 if (ts->debounce_max < 2)
890 ts->debounce_max = 2;
Imre Deakd5b415c2006-04-26 00:13:18 -0400891 ts->debounce_tol = pdata->debounce_tol;
892 ts->debounce_rep = pdata->debounce_rep;
Imre Deakda970e62007-01-18 00:44:41 -0500893 ts->filter = ads7846_debounce;
894 ts->filter_data = ts;
Imre Deakd5b415c2006-04-26 00:13:18 -0400895 } else
Imre Deakda970e62007-01-18 00:44:41 -0500896 ts->filter = ads7846_no_filter;
Imre Deakc9e617a2006-04-11 23:44:05 -0400897 ts->get_pendown_state = pdata->get_pendown_state;
David Brownellffa458c2006-01-08 13:34:21 -0800898
Semih Hazar1d258912007-07-18 00:36:04 -0400899 if (pdata->penirq_recheck_delay_usecs)
900 ts->penirq_recheck_delay_usecs =
901 pdata->penirq_recheck_delay_usecs;
902
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500903 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
David Brownellffa458c2006-01-08 13:34:21 -0800904
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500905 input_dev->name = "ADS784x Touchscreen";
906 input_dev->phys = ts->phys;
Dmitry Torokhova5394fb02007-04-12 01:35:14 -0400907 input_dev->dev.parent = &spi->dev;
David Brownellffa458c2006-01-08 13:34:21 -0800908
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700909 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
910 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500911 input_set_abs_params(input_dev, ABS_X,
David Brownellffa458c2006-01-08 13:34:21 -0800912 pdata->x_min ? : 0,
913 pdata->x_max ? : MAX_12BIT,
914 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500915 input_set_abs_params(input_dev, ABS_Y,
David Brownellffa458c2006-01-08 13:34:21 -0800916 pdata->y_min ? : 0,
917 pdata->y_max ? : MAX_12BIT,
918 0, 0);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -0500919 input_set_abs_params(input_dev, ABS_PRESSURE,
David Brownellffa458c2006-01-08 13:34:21 -0800920 pdata->pressure_min, pdata->pressure_max, 0, 0);
921
Imre Deakde2defd2007-01-18 00:45:21 -0500922 vref = pdata->keep_vref_on;
923
David Brownellffa458c2006-01-08 13:34:21 -0800924 /* set up the transfers to read touchscreen state; this assumes we
925 * use formula #2 for pressure, not #3.
926 */
Imre Deak0b7018a2006-04-11 23:42:03 -0400927 m = &ts->msg[0];
David Brownellffa458c2006-01-08 13:34:21 -0800928 x = ts->xfer;
929
Imre Deak0b7018a2006-04-11 23:42:03 -0400930 spi_message_init(m);
931
David Brownellffa458c2006-01-08 13:34:21 -0800932 /* y- still on; turn on only y+ (and ADC) */
Imre Deakde2defd2007-01-18 00:45:21 -0500933 ts->read_y = READ_Y(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500934 x->tx_buf = &ts->read_y;
David Brownellffa458c2006-01-08 13:34:21 -0800935 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400936 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500937
David Brownellffa458c2006-01-08 13:34:21 -0800938 x++;
939 x->rx_buf = &ts->tc.y;
940 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400941 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -0800942
Semih Hazare4f48862007-07-18 00:35:56 -0400943 /* the first sample after switching drivers can be low quality;
944 * optionally discard it, using a second one after the signals
945 * have had enough time to stabilize.
946 */
947 if (pdata->settle_delay_usecs) {
948 x->delay_usecs = pdata->settle_delay_usecs;
949
950 x++;
951 x->tx_buf = &ts->read_y;
952 x->len = 1;
953 spi_message_add_tail(x, m);
954
955 x++;
956 x->rx_buf = &ts->tc.y;
957 x->len = 2;
958 spi_message_add_tail(x, m);
959 }
960
Imre Deakda970e62007-01-18 00:44:41 -0500961 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400962 m->context = ts;
David Brownelld93f70b2006-02-15 00:49:35 -0500963
Imre Deak0b7018a2006-04-11 23:42:03 -0400964 m++;
965 spi_message_init(m);
David Brownellffa458c2006-01-08 13:34:21 -0800966
967 /* turn y- off, x+ on, then leave in lowpower */
David Brownelld93f70b2006-02-15 00:49:35 -0500968 x++;
Imre Deakde2defd2007-01-18 00:45:21 -0500969 ts->read_x = READ_X(vref);
David Brownelld93f70b2006-02-15 00:49:35 -0500970 x->tx_buf = &ts->read_x;
David Brownellffa458c2006-01-08 13:34:21 -0800971 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400972 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -0500973
David Brownellffa458c2006-01-08 13:34:21 -0800974 x++;
975 x->rx_buf = &ts->tc.x;
976 x->len = 2;
Imre Deak0b7018a2006-04-11 23:42:03 -0400977 spi_message_add_tail(x, m);
978
Semih Hazare4f48862007-07-18 00:35:56 -0400979 /* ... maybe discard first sample ... */
980 if (pdata->settle_delay_usecs) {
981 x->delay_usecs = pdata->settle_delay_usecs;
982
983 x++;
984 x->tx_buf = &ts->read_x;
985 x->len = 1;
986 spi_message_add_tail(x, m);
987
988 x++;
989 x->rx_buf = &ts->tc.x;
990 x->len = 2;
991 spi_message_add_tail(x, m);
992 }
993
Imre Deakda970e62007-01-18 00:44:41 -0500994 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -0400995 m->context = ts;
996
997 /* turn y+ off, x- on; we'll use formula #2 */
998 if (ts->model == 7846) {
999 m++;
1000 spi_message_init(m);
1001
1002 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001003 ts->read_z1 = READ_Z1(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001004 x->tx_buf = &ts->read_z1;
1005 x->len = 1;
1006 spi_message_add_tail(x, m);
1007
1008 x++;
1009 x->rx_buf = &ts->tc.z1;
1010 x->len = 2;
1011 spi_message_add_tail(x, m);
1012
Semih Hazare4f48862007-07-18 00:35:56 -04001013 /* ... maybe discard first sample ... */
1014 if (pdata->settle_delay_usecs) {
1015 x->delay_usecs = pdata->settle_delay_usecs;
1016
1017 x++;
1018 x->tx_buf = &ts->read_z1;
1019 x->len = 1;
1020 spi_message_add_tail(x, m);
1021
1022 x++;
1023 x->rx_buf = &ts->tc.z1;
1024 x->len = 2;
1025 spi_message_add_tail(x, m);
1026 }
1027
Imre Deakda970e62007-01-18 00:44:41 -05001028 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001029 m->context = ts;
1030
1031 m++;
1032 spi_message_init(m);
1033
1034 x++;
Imre Deakde2defd2007-01-18 00:45:21 -05001035 ts->read_z2 = READ_Z2(vref);
Imre Deak0b7018a2006-04-11 23:42:03 -04001036 x->tx_buf = &ts->read_z2;
1037 x->len = 1;
1038 spi_message_add_tail(x, m);
1039
1040 x++;
1041 x->rx_buf = &ts->tc.z2;
1042 x->len = 2;
1043 spi_message_add_tail(x, m);
1044
Semih Hazare4f48862007-07-18 00:35:56 -04001045 /* ... maybe discard first sample ... */
1046 if (pdata->settle_delay_usecs) {
1047 x->delay_usecs = pdata->settle_delay_usecs;
1048
1049 x++;
1050 x->tx_buf = &ts->read_z2;
1051 x->len = 1;
1052 spi_message_add_tail(x, m);
1053
1054 x++;
1055 x->rx_buf = &ts->tc.z2;
1056 x->len = 2;
1057 spi_message_add_tail(x, m);
1058 }
1059
Imre Deakda970e62007-01-18 00:44:41 -05001060 m->complete = ads7846_rx_val;
Imre Deak0b7018a2006-04-11 23:42:03 -04001061 m->context = ts;
1062 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001063
1064 /* power down */
Imre Deak0b7018a2006-04-11 23:42:03 -04001065 m++;
1066 spi_message_init(m);
1067
Imre Deak53a0ef892006-04-11 23:41:49 -04001068 x++;
1069 ts->pwrdown = PWRDOWN;
1070 x->tx_buf = &ts->pwrdown;
1071 x->len = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001072 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001073
1074 x++;
1075 x->rx_buf = &ts->dummy;
1076 x->len = 2;
David Brownelld93f70b2006-02-15 00:49:35 -05001077 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001078 spi_message_add_tail(x, m);
David Brownellffa458c2006-01-08 13:34:21 -08001079
Imre Deak0b7018a2006-04-11 23:42:03 -04001080 m->complete = ads7846_rx;
1081 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001082
Imre Deakd5b415c2006-04-26 00:13:18 -04001083 ts->last_msg = m;
1084
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001085 if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
David Brownell90845332006-05-25 18:44:20 -07001086 spi->dev.driver->name, ts)) {
David Brownell2e5a7bd2006-01-08 13:34:25 -08001087 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001088 err = -EBUSY;
Imre Deakda970e62007-01-18 00:44:41 -05001089 goto err_cleanup_filter;
David Brownellffa458c2006-01-08 13:34:21 -08001090 }
David Brownellffa458c2006-01-08 13:34:21 -08001091
David Brownell2c8dc072007-01-18 00:45:48 -05001092 err = ads784x_hwmon_register(spi, ts);
1093 if (err)
1094 goto err_free_irq;
1095
David Brownell2e5a7bd2006-01-08 13:34:25 -08001096 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001097
David Brownell2c8dc072007-01-18 00:45:48 -05001098 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001099 * the touchscreen, in case it's not connected.
1100 */
David Brownell2e5a7bd2006-01-08 13:34:25 -08001101 (void) ads7846_read12_ser(&spi->dev,
David Brownellffa458c2006-01-08 13:34:21 -08001102 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1103
David Brownell2c8dc072007-01-18 00:45:48 -05001104 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001105 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001106 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001107
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001108 err = input_register_device(input_dev);
1109 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001110 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001111
David Brownellffa458c2006-01-08 13:34:21 -08001112 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001113
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001114 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001115 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1116 err_remove_hwmon:
1117 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001118 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001119 free_irq(spi->irq, ts);
Imre Deakda970e62007-01-18 00:44:41 -05001120 err_cleanup_filter:
1121 if (ts->filter_cleanup)
1122 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001123 err_free_mem:
1124 input_free_device(input_dev);
1125 kfree(ts);
1126 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001127}
1128
David Brownell2e5a7bd2006-01-08 13:34:25 -08001129static int __devexit ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001130{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001131 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
David Brownellffa458c2006-01-08 13:34:21 -08001132
David Brownell2c8dc072007-01-18 00:45:48 -05001133 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001134 input_unregister_device(ts->input);
1135
David Brownell2e5a7bd2006-01-08 13:34:25 -08001136 ads7846_suspend(spi, PMSG_SUSPEND);
David Brownellffa458c2006-01-08 13:34:21 -08001137
David Brownell2c8dc072007-01-18 00:45:48 -05001138 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001139
Imre Deak7de90a82006-04-11 23:43:55 -04001140 free_irq(ts->spi->irq, ts);
Imre Deakc9e617a2006-04-11 23:44:05 -04001141 /* suspend left the IRQ disabled */
1142 enable_irq(ts->spi->irq);
Imre Deak7de90a82006-04-11 23:43:55 -04001143
Imre Deakda970e62007-01-18 00:44:41 -05001144 if (ts->filter_cleanup)
1145 ts->filter_cleanup(ts->filter_data);
1146
David Brownellffa458c2006-01-08 13:34:21 -08001147 kfree(ts);
1148
David Brownell2e5a7bd2006-01-08 13:34:25 -08001149 dev_dbg(&spi->dev, "unregistered touchscreen\n");
David Brownellffa458c2006-01-08 13:34:21 -08001150 return 0;
1151}
1152
David Brownell2e5a7bd2006-01-08 13:34:25 -08001153static struct spi_driver ads7846_driver = {
1154 .driver = {
1155 .name = "ads7846",
1156 .bus = &spi_bus_type,
1157 .owner = THIS_MODULE,
1158 },
David Brownellffa458c2006-01-08 13:34:21 -08001159 .probe = ads7846_probe,
David Brownell2e5a7bd2006-01-08 13:34:25 -08001160 .remove = __devexit_p(ads7846_remove),
David Brownellffa458c2006-01-08 13:34:21 -08001161 .suspend = ads7846_suspend,
1162 .resume = ads7846_resume,
1163};
1164
1165static int __init ads7846_init(void)
1166{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001167 return spi_register_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001168}
1169module_init(ads7846_init);
1170
1171static void __exit ads7846_exit(void)
1172{
David Brownell2e5a7bd2006-01-08 13:34:25 -08001173 spi_unregister_driver(&ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001174}
1175module_exit(ads7846_exit);
1176
1177MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1178MODULE_LICENSE("GPL");