blob: f91592ab2f3aac006889068e91f842b07accb73c [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 */
Jason Wang2991a1c2010-10-13 11:35:40 -070020#include <linux/types.h>
David Brownell2c8dc072007-01-18 00:45:48 -050021#include <linux/hwmon.h>
David Brownellffa458c2006-01-08 13:34:21 -080022#include <linux/init.h>
David Brownell2c8dc072007-01-18 00:45:48 -050023#include <linux/err.h>
Jason Wang2991a1c2010-10-13 11:35:40 -070024#include <linux/sched.h>
David Brownellffa458c2006-01-08 13:34:21 -080025#include <linux/delay.h>
26#include <linux/input.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
Mark Brown3c36e712011-01-20 22:51:26 -080029#include <linux/pm.h>
Daniel Macka6080262013-06-27 23:42:17 -070030#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
Eric Miao4d5975e2008-09-10 12:06:15 -040033#include <linux/gpio.h>
David Brownellffa458c2006-01-08 13:34:21 -080034#include <linux/spi/spi.h>
35#include <linux/spi/ads7846.h>
Grazvydas Ignotas91143372010-02-25 02:04:56 -080036#include <linux/regulator/consumer.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040037#include <linux/module.h>
Andrew Morton3ac8bf02006-03-26 01:37:33 -080038#include <asm/irq.h>
David Brownellffa458c2006-01-08 13:34:21 -080039
David Brownellffa458c2006-01-08 13:34:21 -080040/*
David Brownell90845332006-05-25 18:44:20 -070041 * This code has been heavily tested on a Nokia 770, and lightly
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080042 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
David Brownellbff0de52007-05-22 23:28:40 -040043 * TSC2046 is just newer ads7846 silicon.
Nicolas Ferre969111e2007-02-28 23:51:03 -050044 * Support for ads7843 tested on Atmel at91sam926x-EK.
45 * Support for ads7845 has only been stubbed in.
Michael Hennerich06a09122010-03-09 20:38:45 -080046 * Support for Analog Devices AD7873 and AD7843 tested.
David Brownellffa458c2006-01-08 13:34:21 -080047 *
Imre Deak7de90a82006-04-11 23:43:55 -040048 * IRQ handling needs a workaround because of a shortcoming in handling
49 * edge triggered IRQs on some platforms like the OMAP1/2. These
50 * platforms don't handle the ARM lazy IRQ disabling properly, thus we
51 * have to maintain our own SW IRQ disabled status. This should be
52 * removed as soon as the affected platform's IRQ handling is fixed.
53 *
Pavel Machek52ce4eaa2009-11-23 08:25:17 -080054 * App note sbaa036 talks in more detail about accurate sampling...
David Brownellffa458c2006-01-08 13:34:21 -080055 * that ought to help in situations like LCDs inducing noise (which
56 * can also be helped by using synch signals) and more generally.
Imre Deak7de90a82006-04-11 23:43:55 -040057 * This driver tries to utilize the measures described in the app
58 * note. The strength of filtering can be set in the board-* specific
59 * files.
David Brownellffa458c2006-01-08 13:34:21 -080060 */
61
Jason Wang2991a1c2010-10-13 11:35:40 -070062#define TS_POLL_DELAY 1 /* ms delay before the first sample */
63#define TS_POLL_PERIOD 5 /* ms delay between samples */
David Brownellffa458c2006-01-08 13:34:21 -080064
David Brownelld93f70b2006-02-15 00:49:35 -050065/* this driver doesn't aim at the peak continuous sample rate */
66#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
67
David Brownellffa458c2006-01-08 13:34:21 -080068struct ts_event {
Jason Wang2991a1c2010-10-13 11:35:40 -070069 /*
70 * For portability, we can't read 12 bit values using SPI (which
71 * would make the controller deliver them as native byte order u16
David Brownelld93f70b2006-02-15 00:49:35 -050072 * with msbs zeroed). Instead, we read them as two 8-bit values,
Imre Deakda970e62007-01-18 00:44:41 -050073 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
David Brownellffa458c2006-01-08 13:34:21 -080074 */
Imre Deakda970e62007-01-18 00:44:41 -050075 u16 x;
76 u16 y;
77 u16 z1, z2;
Jason Wang2991a1c2010-10-13 11:35:40 -070078 bool ignore;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070079 u8 x_buf[3];
80 u8 y_buf[3];
David Brownellffa458c2006-01-08 13:34:21 -080081};
82
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040083/*
84 * We allocate this separately to avoid cache line sharing issues when
85 * driver is used with DMA-based SPI controllers (like atmel_spi) on
86 * systems where main memory is not DMA-coherent (most non-x86 boards).
87 */
88struct ads7846_packet {
89 u8 read_x, read_y, read_z1, read_z2, pwrdown;
90 u16 dummy; /* for the pwrdown read */
91 struct ts_event tc;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -070092 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
93 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
Dmitry Torokhove8f462d2008-10-09 00:52:23 -040094};
95
David Brownellffa458c2006-01-08 13:34:21 -080096struct ads7846 {
Dmitry Torokhova90f7e92006-02-15 00:49:22 -050097 struct input_dev *input;
David Brownellffa458c2006-01-08 13:34:21 -080098 char phys[32];
Michael Rothb58895f2009-05-18 16:05:12 -070099 char name[32];
David Brownellffa458c2006-01-08 13:34:21 -0800100
101 struct spi_device *spi;
Grazvydas Ignotas91143372010-02-25 02:04:56 -0800102 struct regulator *reg;
David Brownell2c8dc072007-01-18 00:45:48 -0500103
Fabio Estevamc52b4fc2013-12-01 22:11:26 -0800104#if IS_ENABLED(CONFIG_HWMON)
Tony Jones1beeffe2007-08-20 13:46:20 -0700105 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500106#endif
107
David Brownellffa458c2006-01-08 13:34:21 -0800108 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400109 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800110 u16 vref_delay_usecs;
111 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400112 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800113
Michael Roth86579a42009-05-18 16:04:44 -0700114 bool swap_xy;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700115 bool use_internal;
Michael Roth86579a42009-05-18 16:04:44 -0700116
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400117 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800118
Semih Hazare4f48862007-07-18 00:35:56 -0400119 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400120 struct spi_message msg[5];
Jason Wang2991a1c2010-10-13 11:35:40 -0700121 int msg_count;
122 wait_queue_head_t wait;
123
124 bool pendown;
125
Imre Deak0b7018a2006-04-11 23:42:03 -0400126 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400127 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400128 int last_read;
129
130 u16 debounce_max;
131 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400132 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800133
Semih Hazar1d258912007-07-18 00:36:04 -0400134 u16 penirq_recheck_delay_usecs;
135
Jason Wang2991a1c2010-10-13 11:35:40 -0700136 struct mutex lock;
137 bool stopped; /* P: lock */
138 bool disabled; /* P: lock */
139 bool suspended; /* P: lock */
Imre Deakc9e617a2006-04-11 23:44:05 -0400140
Imre Deakda970e62007-01-18 00:44:41 -0500141 int (*filter)(void *data, int data_idx, int *val);
142 void *filter_data;
143 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400144 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400145 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700146
147 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800148};
149
150/* leave chip selected when we're done, for quicker re-select? */
151#if 0
152#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
153#else
154#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
155#endif
156
157/*--------------------------------------------------------------------------*/
158
159/* The ADS7846 has touchscreen and other sensors.
160 * Earlier ads784x chips are somewhat compatible.
161 */
162#define ADS_START (1 << 7)
163#define ADS_A2A1A0_d_y (1 << 4) /* differential */
164#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
165#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
166#define ADS_A2A1A0_d_x (5 << 4) /* differential */
167#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
168#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
169#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
170#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
171#define ADS_8_BIT (1 << 3)
172#define ADS_12_BIT (0 << 3)
173#define ADS_SER (1 << 2) /* non-differential */
174#define ADS_DFR (0 << 2) /* differential */
Jason Wang2991a1c2010-10-13 11:35:40 -0700175#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
David Brownellffa458c2006-01-08 13:34:21 -0800176#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
177#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
178#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
179
180#define MAX_12BIT ((1<<12)-1)
181
182/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500183#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
184 | ADS_12_BIT | ADS_DFR | \
185 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800186
Imre Deakde2defd2007-01-18 00:45:21 -0500187#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
188#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
189#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400190
Imre Deakde2defd2007-01-18 00:45:21 -0500191#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
192#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800193
194/* single-ended samples need to first power up reference voltage;
195 * we leave both ADC and VREF powered
196 */
197#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
198 | ADS_12_BIT | ADS_SER)
199
Imre Deakde2defd2007-01-18 00:45:21 -0500200#define REF_ON (READ_12BIT_DFR(x, 1, 1))
201#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800202
Jason Wang2991a1c2010-10-13 11:35:40 -0700203/* Must be called with ts->lock held */
204static void ads7846_stop(struct ads7846 *ts)
205{
206 if (!ts->disabled && !ts->suspended) {
207 /* Signal IRQ thread to stop polling and disable the handler. */
208 ts->stopped = true;
209 mb();
210 wake_up(&ts->wait);
211 disable_irq(ts->spi->irq);
212 }
213}
214
215/* Must be called with ts->lock held */
216static void ads7846_restart(struct ads7846 *ts)
217{
218 if (!ts->disabled && !ts->suspended) {
219 /* Tell IRQ thread that it may poll the device. */
220 ts->stopped = false;
221 mb();
222 enable_irq(ts->spi->irq);
223 }
224}
225
226/* Must be called with ts->lock held */
227static void __ads7846_disable(struct ads7846 *ts)
228{
229 ads7846_stop(ts);
230 regulator_disable(ts->reg);
231
232 /*
233 * We know the chip's in low power mode since we always
234 * leave it that way after every request
235 */
236}
237
238/* Must be called with ts->lock held */
239static void __ads7846_enable(struct ads7846 *ts)
240{
Mark Brownf94352f2013-03-03 20:19:07 -0800241 int error;
242
243 error = regulator_enable(ts->reg);
244 if (error != 0)
245 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
246
Jason Wang2991a1c2010-10-13 11:35:40 -0700247 ads7846_restart(ts);
248}
249
250static void ads7846_disable(struct ads7846 *ts)
251{
252 mutex_lock(&ts->lock);
253
254 if (!ts->disabled) {
255
256 if (!ts->suspended)
257 __ads7846_disable(ts);
258
259 ts->disabled = true;
260 }
261
262 mutex_unlock(&ts->lock);
263}
264
265static void ads7846_enable(struct ads7846 *ts)
266{
267 mutex_lock(&ts->lock);
268
269 if (ts->disabled) {
270
271 ts->disabled = false;
272
273 if (!ts->suspended)
274 __ads7846_enable(ts);
275 }
276
277 mutex_unlock(&ts->lock);
278}
279
David Brownellffa458c2006-01-08 13:34:21 -0800280/*--------------------------------------------------------------------------*/
281
282/*
283 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500284 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
285 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800286 */
287
288struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500289 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800290 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500291 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800292 u16 scratch;
David Brownellffa458c2006-01-08 13:34:21 -0800293 struct spi_message msg;
294 struct spi_transfer xfer[6];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700295 /*
296 * DMA (thus cache coherency maintenance) requires the
297 * transfer buffers to live in their own cache lines.
298 */
299 __be16 sample ____cacheline_aligned;
David Brownellffa458c2006-01-08 13:34:21 -0800300};
301
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700302struct ads7845_ser_req {
303 u8 command[3];
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700304 struct spi_message msg;
305 struct spi_transfer xfer[2];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700306 /*
307 * DMA (thus cache coherency maintenance) requires the
308 * transfer buffers to live in their own cache lines.
309 */
310 u8 sample[3] ____cacheline_aligned;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700311};
312
David Brownellffa458c2006-01-08 13:34:21 -0800313static int ads7846_read12_ser(struct device *dev, unsigned command)
314{
Jason Wang2991a1c2010-10-13 11:35:40 -0700315 struct spi_device *spi = to_spi_device(dev);
316 struct ads7846 *ts = dev_get_drvdata(dev);
317 struct ser_req *req;
318 int status;
David Brownellffa458c2006-01-08 13:34:21 -0800319
Jason Wang2991a1c2010-10-13 11:35:40 -0700320 req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800321 if (!req)
322 return -ENOMEM;
323
Imre Deak0b7018a2006-04-11 23:42:03 -0400324 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800325
David Brownell2c8dc072007-01-18 00:45:48 -0500326 /* maybe turn on internal vREF, and let it settle */
Alexander Steinebcaaad2011-05-11 16:24:08 -0700327 if (ts->use_internal) {
David Brownell2c8dc072007-01-18 00:45:48 -0500328 req->ref_on = REF_ON;
329 req->xfer[0].tx_buf = &req->ref_on;
330 req->xfer[0].len = 1;
331 spi_message_add_tail(&req->xfer[0], &req->msg);
332
333 req->xfer[1].rx_buf = &req->scratch;
334 req->xfer[1].len = 2;
335
336 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
337 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
338 spi_message_add_tail(&req->xfer[1], &req->msg);
Alexander Steinebcaaad2011-05-11 16:24:08 -0700339
340 /* Enable reference voltage */
341 command |= ADS_PD10_REF_ON;
David Brownell2c8dc072007-01-18 00:45:48 -0500342 }
David Brownellffa458c2006-01-08 13:34:21 -0800343
Alexander Steinebcaaad2011-05-11 16:24:08 -0700344 /* Enable ADC in every case */
345 command |= ADS_PD10_ADC_ON;
346
David Brownellffa458c2006-01-08 13:34:21 -0800347 /* take sample */
348 req->command = (u8) command;
349 req->xfer[2].tx_buf = &req->command;
350 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500351 spi_message_add_tail(&req->xfer[2], &req->msg);
352
David Brownellffa458c2006-01-08 13:34:21 -0800353 req->xfer[3].rx_buf = &req->sample;
354 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500355 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800356
357 /* REVISIT: take a few more samples, and compare ... */
358
Nicolas Ferre969111e2007-02-28 23:51:03 -0500359 /* converter in low power mode & enable PENIRQ */
360 req->ref_off = PWRDOWN;
361 req->xfer[4].tx_buf = &req->ref_off;
362 req->xfer[4].len = 1;
363 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800364
Nicolas Ferre969111e2007-02-28 23:51:03 -0500365 req->xfer[5].rx_buf = &req->scratch;
366 req->xfer[5].len = 2;
367 CS_CHANGE(req->xfer[5]);
368 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800369
Jason Wang2991a1c2010-10-13 11:35:40 -0700370 mutex_lock(&ts->lock);
371 ads7846_stop(ts);
David Brownellffa458c2006-01-08 13:34:21 -0800372 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700373 ads7846_restart(ts);
374 mutex_unlock(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800375
Marc Pignatc24b2602007-12-04 23:45:11 -0800376 if (status == 0) {
377 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400378 status = be16_to_cpu(req->sample);
379 status = status >> 3;
380 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800381 }
David Brownell90845332006-05-25 18:44:20 -0700382
383 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400384 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800385}
386
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700387static int ads7845_read12_ser(struct device *dev, unsigned command)
388{
Jason Wang2991a1c2010-10-13 11:35:40 -0700389 struct spi_device *spi = to_spi_device(dev);
390 struct ads7846 *ts = dev_get_drvdata(dev);
391 struct ads7845_ser_req *req;
392 int status;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700393
Jason Wang2991a1c2010-10-13 11:35:40 -0700394 req = kzalloc(sizeof *req, GFP_KERNEL);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700395 if (!req)
396 return -ENOMEM;
397
398 spi_message_init(&req->msg);
399
400 req->command[0] = (u8) command;
401 req->xfer[0].tx_buf = req->command;
402 req->xfer[0].rx_buf = req->sample;
403 req->xfer[0].len = 3;
404 spi_message_add_tail(&req->xfer[0], &req->msg);
405
Jason Wang2991a1c2010-10-13 11:35:40 -0700406 mutex_lock(&ts->lock);
407 ads7846_stop(ts);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700408 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700409 ads7846_restart(ts);
410 mutex_unlock(&ts->lock);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700411
412 if (status == 0) {
413 /* BE12 value, then padding */
414 status = be16_to_cpu(*((u16 *)&req->sample[1]));
415 status = status >> 3;
416 status &= 0x0fff;
417 }
418
419 kfree(req);
420 return status;
421}
422
Fabio Estevamc52b4fc2013-12-01 22:11:26 -0800423#if IS_ENABLED(CONFIG_HWMON)
David Brownell2c8dc072007-01-18 00:45:48 -0500424
425#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800426name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
427{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500428 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800429 ssize_t v = ads7846_read12_ser(dev, \
Alexander Steinebcaaad2011-05-11 16:24:08 -0700430 READ_12BIT_SER(var)); \
David Brownellffa458c2006-01-08 13:34:21 -0800431 if (v < 0) \
432 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500433 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800434} \
435static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
436
David Brownell2c8dc072007-01-18 00:45:48 -0500437
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400438/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500439 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
440 * accuracy scheme without calibration data. For now we won't try either;
441 * userspace sees raw sensor values, and must scale/calibrate appropriately.
442 */
443static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
444{
445 return v;
446}
447
448SHOW(temp0, temp0, null_adjust) /* temp1_input */
449SHOW(temp1, temp1, null_adjust) /* temp2_input */
450
451
452/* sysfs conventions report voltages in millivolts. We can convert voltages
453 * if we know vREF. userspace may need to scale vAUX to match the board's
454 * external resistors; we assume that vBATT only uses the internal ones.
455 */
456static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
457{
458 unsigned retval = v;
459
460 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400461 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500462 retval = retval >> 12;
Jason Wang2991a1c2010-10-13 11:35:40 -0700463
David Brownell2c8dc072007-01-18 00:45:48 -0500464 return retval;
465}
466
467static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
468{
469 unsigned retval = vaux_adjust(ts, v);
470
471 /* ads7846 has a resistor ladder to scale this signal down */
472 if (ts->model == 7846)
473 retval *= 4;
Jason Wang2991a1c2010-10-13 11:35:40 -0700474
David Brownell2c8dc072007-01-18 00:45:48 -0500475 return retval;
476}
477
478SHOW(in0_input, vaux, vaux_adjust)
479SHOW(in1_input, vbatt, vbatt_adjust)
480
Guenter Roecke585c402013-11-27 20:08:33 -0800481static umode_t ads7846_is_visible(struct kobject *kobj, struct attribute *attr,
482 int index)
483{
484 struct device *dev = container_of(kobj, struct device, kobj);
485 struct ads7846 *ts = dev_get_drvdata(dev);
486
487 if (ts->model == 7843 && index < 2) /* in0, in1 */
488 return 0;
489 if (ts->model == 7845 && index != 2) /* in0 */
490 return 0;
491
492 return attr->mode;
493}
494
David Brownell2c8dc072007-01-18 00:45:48 -0500495static struct attribute *ads7846_attributes[] = {
Guenter Roecke585c402013-11-27 20:08:33 -0800496 &dev_attr_temp0.attr, /* 0 */
497 &dev_attr_temp1.attr, /* 1 */
498 &dev_attr_in0_input.attr, /* 2 */
499 &dev_attr_in1_input.attr, /* 3 */
David Brownell2c8dc072007-01-18 00:45:48 -0500500 NULL,
501};
502
503static struct attribute_group ads7846_attr_group = {
504 .attrs = ads7846_attributes,
Guenter Roecke585c402013-11-27 20:08:33 -0800505 .is_visible = ads7846_is_visible,
David Brownell2c8dc072007-01-18 00:45:48 -0500506};
Guenter Roecke585c402013-11-27 20:08:33 -0800507__ATTRIBUTE_GROUPS(ads7846_attr);
David Brownell2c8dc072007-01-18 00:45:48 -0500508
509static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
510{
David Brownell2c8dc072007-01-18 00:45:48 -0500511 /* hwmon sensors need a reference voltage */
512 switch (ts->model) {
513 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400514 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500515 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400516 ts->vref_mv = 2500;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700517 ts->use_internal = true;
David Brownell2c8dc072007-01-18 00:45:48 -0500518 }
519 break;
520 case 7845:
521 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400522 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500523 dev_warn(&spi->dev,
524 "external vREF for ADS%d not specified\n",
525 ts->model);
526 return 0;
527 }
528 break;
529 }
530
Guenter Roecke585c402013-11-27 20:08:33 -0800531 ts->hwmon = hwmon_device_register_with_groups(&spi->dev, spi->modalias,
532 ts, ads7846_attr_groups);
533 if (IS_ERR(ts->hwmon))
534 return PTR_ERR(ts->hwmon);
David Brownell2c8dc072007-01-18 00:45:48 -0500535
David Brownell2c8dc072007-01-18 00:45:48 -0500536 return 0;
537}
538
539static void ads784x_hwmon_unregister(struct spi_device *spi,
540 struct ads7846 *ts)
541{
Guenter Roecke585c402013-11-27 20:08:33 -0800542 if (ts->hwmon)
David Brownell2c8dc072007-01-18 00:45:48 -0500543 hwmon_device_unregister(ts->hwmon);
David Brownell2c8dc072007-01-18 00:45:48 -0500544}
545
546#else
547static inline int ads784x_hwmon_register(struct spi_device *spi,
548 struct ads7846 *ts)
549{
550 return 0;
551}
552
553static inline void ads784x_hwmon_unregister(struct spi_device *spi,
554 struct ads7846 *ts)
555{
556}
557#endif
David Brownellffa458c2006-01-08 13:34:21 -0800558
Imre Deak438f2a72006-04-11 23:41:32 -0400559static ssize_t ads7846_pen_down_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
561{
Jason Wang2991a1c2010-10-13 11:35:40 -0700562 struct ads7846 *ts = dev_get_drvdata(dev);
563
564 return sprintf(buf, "%u\n", ts->pendown);
Imre Deak438f2a72006-04-11 23:41:32 -0400565}
566
567static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
568
Imre Deak7de90a82006-04-11 23:43:55 -0400569static ssize_t ads7846_disable_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
Jason Wang2991a1c2010-10-13 11:35:40 -0700572 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400573
574 return sprintf(buf, "%u\n", ts->disabled);
575}
576
577static ssize_t ads7846_disable_store(struct device *dev,
578 struct device_attribute *attr,
579 const char *buf, size_t count)
580{
581 struct ads7846 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800582 unsigned int i;
583 int err;
Imre Deak7de90a82006-04-11 23:43:55 -0400584
JJ Ding76496e72011-11-09 10:20:14 -0800585 err = kstrtouint(buf, 10, &i);
586 if (err)
587 return err;
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400588
Imre Deak7de90a82006-04-11 23:43:55 -0400589 if (i)
590 ads7846_disable(ts);
591 else
592 ads7846_enable(ts);
593
Imre Deak7de90a82006-04-11 23:43:55 -0400594 return count;
595}
596
597static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
598
David Brownell2c8dc072007-01-18 00:45:48 -0500599static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500600 &dev_attr_pen_down.attr,
601 &dev_attr_disable.attr,
602 NULL,
603};
604
David Brownell2c8dc072007-01-18 00:45:48 -0500605static struct attribute_group ads784x_attr_group = {
606 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500607};
608
David Brownellffa458c2006-01-08 13:34:21 -0800609/*--------------------------------------------------------------------------*/
610
Eric Miao4d5975e2008-09-10 12:06:15 -0400611static int get_pendown_state(struct ads7846 *ts)
612{
613 if (ts->get_pendown_state)
614 return ts->get_pendown_state();
615
616 return !gpio_get_value(ts->gpio_pendown);
617}
618
Eric Miaofd746d52009-04-11 16:54:59 -0700619static void null_wait_for_sync(void)
620{
621}
622
Jason Wang2991a1c2010-10-13 11:35:40 -0700623static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
David Brownellffa458c2006-01-08 13:34:21 -0800624{
Jason Wang2991a1c2010-10-13 11:35:40 -0700625 struct ads7846 *ts = ads;
David Brownellffa458c2006-01-08 13:34:21 -0800626
Jason Wang2991a1c2010-10-13 11:35:40 -0700627 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
628 /* Start over collecting consistent readings. */
629 ts->read_rep = 0;
630 /*
631 * Repeat it, if this was the first read or the read
632 * wasn't consistent enough.
633 */
634 if (ts->read_cnt < ts->debounce_max) {
635 ts->last_read = *val;
636 ts->read_cnt++;
637 return ADS7846_FILTER_REPEAT;
638 } else {
639 /*
640 * Maximum number of debouncing reached and still
641 * not enough number of consistent readings. Abort
642 * the whole sample, repeat it in the next sampling
643 * period.
644 */
645 ts->read_cnt = 0;
646 return ADS7846_FILTER_IGNORE;
647 }
648 } else {
649 if (++ts->read_rep > ts->debounce_rep) {
650 /*
651 * Got a good reading for this coordinate,
652 * go for the next one.
653 */
654 ts->read_cnt = 0;
655 ts->read_rep = 0;
656 return ADS7846_FILTER_OK;
657 } else {
658 /* Read more values that are consistent. */
659 ts->read_cnt++;
660 return ADS7846_FILTER_REPEAT;
661 }
662 }
663}
664
665static int ads7846_no_filter(void *ads, int data_idx, int *val)
666{
667 return ADS7846_FILTER_OK;
668}
669
670static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
671{
672 struct spi_transfer *t =
673 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
674
675 if (ts->model == 7845) {
676 return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
677 } else {
678 /*
679 * adjust: on-wire is a must-ignore bit, a BE12 value, then
680 * padding; built from two 8 bit values written msb-first.
681 */
682 return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
683 }
684}
685
686static void ads7846_update_value(struct spi_message *m, int val)
687{
688 struct spi_transfer *t =
689 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
690
691 *(u16 *)t->rx_buf = val;
692}
693
694static void ads7846_read_state(struct ads7846 *ts)
695{
696 struct ads7846_packet *packet = ts->packet;
697 struct spi_message *m;
698 int msg_idx = 0;
699 int val;
700 int action;
701 int error;
702
703 while (msg_idx < ts->msg_count) {
704
705 ts->wait_for_sync();
706
707 m = &ts->msg[msg_idx];
708 error = spi_sync(ts->spi, m);
709 if (error) {
710 dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
711 packet->tc.ignore = true;
712 return;
713 }
714
715 /*
716 * Last message is power down request, no need to convert
717 * or filter the value.
718 */
719 if (msg_idx < ts->msg_count - 1) {
720
721 val = ads7846_get_value(ts, m);
722
723 action = ts->filter(ts->filter_data, msg_idx, &val);
724 switch (action) {
725 case ADS7846_FILTER_REPEAT:
726 continue;
727
728 case ADS7846_FILTER_IGNORE:
729 packet->tc.ignore = true;
730 msg_idx = ts->msg_count - 1;
731 continue;
732
733 case ADS7846_FILTER_OK:
734 ads7846_update_value(m, val);
735 packet->tc.ignore = false;
736 msg_idx++;
737 break;
738
739 default:
740 BUG();
741 }
742 } else {
743 msg_idx++;
744 }
745 }
746}
747
748static void ads7846_report_state(struct ads7846 *ts)
749{
750 struct ads7846_packet *packet = ts->packet;
751 unsigned int Rt;
752 u16 x, y, z1, z2;
753
754 /*
755 * ads7846_get_value() does in-place conversion (including byte swap)
756 * from on-the-wire format as part of debouncing to get stable
757 * readings.
David Brownellffa458c2006-01-08 13:34:21 -0800758 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700759 if (ts->model == 7845) {
760 x = *(u16 *)packet->tc.x_buf;
761 y = *(u16 *)packet->tc.y_buf;
762 z1 = 0;
763 z2 = 0;
764 } else {
765 x = packet->tc.x;
766 y = packet->tc.y;
767 z1 = packet->tc.z1;
768 z2 = packet->tc.z2;
769 }
David Brownellffa458c2006-01-08 13:34:21 -0800770
771 /* range filtering */
772 if (x == MAX_12BIT)
773 x = 0;
774
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400775 if (ts->model == 7843) {
776 Rt = ts->pressure_max / 2;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700777 } else if (ts->model == 7845) {
778 if (get_pendown_state(ts))
779 Rt = ts->pressure_max / 2;
780 else
781 Rt = 0;
782 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400783 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800784 /* compute touch pressure resistance using equation #2 */
785 Rt = z2;
786 Rt -= z1;
787 Rt *= x;
788 Rt *= ts->x_plate_ohms;
789 Rt /= z1;
790 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400791 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800792 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400793 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500794
Jason Wang2991a1c2010-10-13 11:35:40 -0700795 /*
796 * Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500797 * the maximum. Don't report it to user space, repeat at least
798 * once more the measurement
799 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400800 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800801 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
802 packet->tc.ignore, Rt);
Imre Deakd5b415c2006-04-26 00:13:18 -0400803 return;
804 }
805
Jason Wang2991a1c2010-10-13 11:35:40 -0700806 /*
807 * Maybe check the pendown state before reporting. This discards
Semih Hazar1d258912007-07-18 00:36:04 -0400808 * false readings when the pen is lifted.
809 */
810 if (ts->penirq_recheck_delay_usecs) {
811 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400812 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400813 Rt = 0;
814 }
815
Jason Wang2991a1c2010-10-13 11:35:40 -0700816 /*
817 * NOTE: We can't rely on the pressure to determine the pen down
818 * state, even this controller has a pressure sensor. The pressure
Imre Deak15e35892007-01-18 00:45:43 -0500819 * value can fluctuate for quite a while after lifting the pen and
820 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800821 *
Imre Deak15e35892007-01-18 00:45:43 -0500822 * The only safe way to check for the pen up condition is in the
823 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800824 */
David Brownellffa458c2006-01-08 13:34:21 -0800825 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500826 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400827
Michael Roth86579a42009-05-18 16:04:44 -0700828 if (ts->swap_xy)
829 swap(x, y);
830
Jason Wang2991a1c2010-10-13 11:35:40 -0700831 if (!ts->pendown) {
832 input_report_key(input, BTN_TOUCH, 1);
833 ts->pendown = true;
834 dev_vdbg(&ts->spi->dev, "DOWN\n");
835 }
836
Imre Deak15e35892007-01-18 00:45:43 -0500837 input_report_abs(input, ABS_X, x);
838 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800839 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800840
Imre Deak15e35892007-01-18 00:45:43 -0500841 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800842 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500843 }
David Brownellffa458c2006-01-08 13:34:21 -0800844}
845
Jason Wang2991a1c2010-10-13 11:35:40 -0700846static irqreturn_t ads7846_hard_irq(int irq, void *handle)
Imre Deak0b7018a2006-04-11 23:42:03 -0400847{
Jason Wang2991a1c2010-10-13 11:35:40 -0700848 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400849
Jason Wang2991a1c2010-10-13 11:35:40 -0700850 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
Imre Deakda970e62007-01-18 00:44:41 -0500851}
852
David Brownellffa458c2006-01-08 13:34:21 -0800853
David Howells7d12e782006-10-05 14:55:46 +0100854static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800855{
Imre Deak0b7018a2006-04-11 23:42:03 -0400856 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400857
Jason Wang2991a1c2010-10-13 11:35:40 -0700858 /* Start with a small delay before checking pendown state */
859 msleep(TS_POLL_DELAY);
860
861 while (!ts->stopped && get_pendown_state(ts)) {
862
863 /* pen is down, continue with the measurement */
864 ads7846_read_state(ts);
865
866 if (!ts->stopped)
867 ads7846_report_state(ts);
868
869 wait_event_timeout(ts->wait, ts->stopped,
870 msecs_to_jiffies(TS_POLL_PERIOD));
Imre Deak0b7018a2006-04-11 23:42:03 -0400871 }
Jason Wang2991a1c2010-10-13 11:35:40 -0700872
873 if (ts->pendown) {
874 struct input_dev *input = ts->input;
875
876 input_report_key(input, BTN_TOUCH, 0);
877 input_report_abs(input, ABS_PRESSURE, 0);
878 input_sync(input);
879
880 ts->pendown = false;
881 dev_vdbg(&ts->spi->dev, "UP\n");
882 }
Imre Deak7de90a82006-04-11 23:43:55 -0400883
884 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800885}
886
Mark Brown3c36e712011-01-20 22:51:26 -0800887#ifdef CONFIG_PM_SLEEP
888static int ads7846_suspend(struct device *dev)
Imre Deak7de90a82006-04-11 23:43:55 -0400889{
Mark Brown3c36e712011-01-20 22:51:26 -0800890 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400891
Jason Wang2991a1c2010-10-13 11:35:40 -0700892 mutex_lock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400893
Jason Wang2991a1c2010-10-13 11:35:40 -0700894 if (!ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400895
Jason Wang2991a1c2010-10-13 11:35:40 -0700896 if (!ts->disabled)
897 __ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -0400898
Jason Wang2991a1c2010-10-13 11:35:40 -0700899 if (device_may_wakeup(&ts->spi->dev))
900 enable_irq_wake(ts->spi->irq);
901
902 ts->suspended = true;
903 }
904
905 mutex_unlock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800906
David Brownellffa458c2006-01-08 13:34:21 -0800907 return 0;
908}
909
Mark Brown3c36e712011-01-20 22:51:26 -0800910static int ads7846_resume(struct device *dev)
David Brownellffa458c2006-01-08 13:34:21 -0800911{
Mark Brown3c36e712011-01-20 22:51:26 -0800912 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellffa458c2006-01-08 13:34:21 -0800913
Jason Wang2991a1c2010-10-13 11:35:40 -0700914 mutex_lock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800915
Jason Wang2991a1c2010-10-13 11:35:40 -0700916 if (ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400917
Jason Wang2991a1c2010-10-13 11:35:40 -0700918 ts->suspended = false;
Imre Deak7de90a82006-04-11 23:43:55 -0400919
Jason Wang2991a1c2010-10-13 11:35:40 -0700920 if (device_may_wakeup(&ts->spi->dev))
921 disable_irq_wake(ts->spi->irq);
922
923 if (!ts->disabled)
924 __ads7846_enable(ts);
925 }
926
927 mutex_unlock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400928
David Brownellffa458c2006-01-08 13:34:21 -0800929 return 0;
930}
Mark Brown3c36e712011-01-20 22:51:26 -0800931#endif
932
933static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
David Brownellffa458c2006-01-08 13:34:21 -0800934
Bill Pemberton5298cc42012-11-23 21:38:25 -0800935static int ads7846_setup_pendown(struct spi_device *spi,
Dmitry Torokhov57691a12013-06-27 23:29:25 -0700936 struct ads7846 *ts,
937 const struct ads7846_platform_data *pdata)
Eric Miao4d5975e2008-09-10 12:06:15 -0400938{
Eric Miao4d5975e2008-09-10 12:06:15 -0400939 int err;
940
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800941 /*
942 * REVISIT when the irq can be triggered active-low, or if for some
Eric Miao4d5975e2008-09-10 12:06:15 -0400943 * reason the touchscreen isn't hooked up, we don't need to access
944 * the pendown state.
945 */
Eric Miao4d5975e2008-09-10 12:06:15 -0400946
947 if (pdata->get_pendown_state) {
948 ts->get_pendown_state = pdata->get_pendown_state;
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800949 } else if (gpio_is_valid(pdata->gpio_pendown)) {
Eric Miao4d5975e2008-09-10 12:06:15 -0400950
Igor Grinberg58c24402011-06-27 13:06:27 -0700951 err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
952 "ads7846_pendown");
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800953 if (err) {
Igor Grinberg58c24402011-06-27 13:06:27 -0700954 dev_err(&spi->dev,
955 "failed to request/setup pendown GPIO%d: %d\n",
956 pdata->gpio_pendown, err);
Igor Grinberg1201e7e2011-05-11 15:45:05 -0700957 return err;
958 }
Eric Miao4d5975e2008-09-10 12:06:15 -0400959
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800960 ts->gpio_pendown = pdata->gpio_pendown;
961
Igor Grinbergc4f49252012-11-20 23:00:10 -0800962 if (pdata->gpio_pendown_debounce)
963 gpio_set_debounce(pdata->gpio_pendown,
964 pdata->gpio_pendown_debounce);
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800965 } else {
966 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
967 return -EINVAL;
968 }
Jason Wang2991a1c2010-10-13 11:35:40 -0700969
Eric Miao4d5975e2008-09-10 12:06:15 -0400970 return 0;
971}
972
Jason Wang2991a1c2010-10-13 11:35:40 -0700973/*
974 * Set up the transfers to read touchscreen state; this assumes we
975 * use formula #2 for pressure, not #3.
976 */
Bill Pemberton5298cc42012-11-23 21:38:25 -0800977static void ads7846_setup_spi_msg(struct ads7846 *ts,
Dmitry Torokhov57691a12013-06-27 23:29:25 -0700978 const struct ads7846_platform_data *pdata)
David Brownellffa458c2006-01-08 13:34:21 -0800979{
Jason Wang2991a1c2010-10-13 11:35:40 -0700980 struct spi_message *m = &ts->msg[0];
981 struct spi_transfer *x = ts->xfer;
982 struct ads7846_packet *packet = ts->packet;
983 int vref = pdata->keep_vref_on;
Imre Deakde2defd2007-01-18 00:45:21 -0500984
Michael Hennerich06a09122010-03-09 20:38:45 -0800985 if (ts->model == 7873) {
Jason Wang2991a1c2010-10-13 11:35:40 -0700986 /*
987 * The AD7873 is almost identical to the ADS7846
Michael Hennerich06a09122010-03-09 20:38:45 -0800988 * keep VREF off during differential/ratiometric
Jason Wang2991a1c2010-10-13 11:35:40 -0700989 * conversion modes.
Michael Hennerich06a09122010-03-09 20:38:45 -0800990 */
991 ts->model = 7846;
992 vref = 0;
993 }
994
Jason Wang2991a1c2010-10-13 11:35:40 -0700995 ts->msg_count = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -0400996 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -0700997 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -0400998
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700999 if (ts->model == 7845) {
1000 packet->read_y_cmd[0] = READ_Y(vref);
1001 packet->read_y_cmd[1] = 0;
1002 packet->read_y_cmd[2] = 0;
1003 x->tx_buf = &packet->read_y_cmd[0];
1004 x->rx_buf = &packet->tc.y_buf[0];
1005 x->len = 3;
1006 spi_message_add_tail(x, m);
1007 } else {
1008 /* y- still on; turn on only y+ (and ADC) */
1009 packet->read_y = READ_Y(vref);
1010 x->tx_buf = &packet->read_y;
1011 x->len = 1;
1012 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001013
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001014 x++;
1015 x->rx_buf = &packet->tc.y;
1016 x->len = 2;
1017 spi_message_add_tail(x, m);
1018 }
David Brownellffa458c2006-01-08 13:34:21 -08001019
Jason Wang2991a1c2010-10-13 11:35:40 -07001020 /*
1021 * The first sample after switching drivers can be low quality;
Semih Hazare4f48862007-07-18 00:35:56 -04001022 * optionally discard it, using a second one after the signals
1023 * have had enough time to stabilize.
1024 */
1025 if (pdata->settle_delay_usecs) {
1026 x->delay_usecs = pdata->settle_delay_usecs;
1027
1028 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001029 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001030 x->len = 1;
1031 spi_message_add_tail(x, m);
1032
1033 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001034 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001035 x->len = 2;
1036 spi_message_add_tail(x, m);
1037 }
1038
Jason Wang2991a1c2010-10-13 11:35:40 -07001039 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001040 m++;
1041 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001042 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001043
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001044 if (ts->model == 7845) {
1045 x++;
1046 packet->read_x_cmd[0] = READ_X(vref);
1047 packet->read_x_cmd[1] = 0;
1048 packet->read_x_cmd[2] = 0;
1049 x->tx_buf = &packet->read_x_cmd[0];
1050 x->rx_buf = &packet->tc.x_buf[0];
1051 x->len = 3;
1052 spi_message_add_tail(x, m);
1053 } else {
1054 /* turn y- off, x+ on, then leave in lowpower */
1055 x++;
1056 packet->read_x = READ_X(vref);
1057 x->tx_buf = &packet->read_x;
1058 x->len = 1;
1059 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001060
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001061 x++;
1062 x->rx_buf = &packet->tc.x;
1063 x->len = 2;
1064 spi_message_add_tail(x, m);
1065 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001066
Semih Hazare4f48862007-07-18 00:35:56 -04001067 /* ... maybe discard first sample ... */
1068 if (pdata->settle_delay_usecs) {
1069 x->delay_usecs = pdata->settle_delay_usecs;
1070
1071 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001072 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001073 x->len = 1;
1074 spi_message_add_tail(x, m);
1075
1076 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001077 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001078 x->len = 2;
1079 spi_message_add_tail(x, m);
1080 }
1081
Imre Deak0b7018a2006-04-11 23:42:03 -04001082 /* turn y+ off, x- on; we'll use formula #2 */
1083 if (ts->model == 7846) {
Jason Wang2991a1c2010-10-13 11:35:40 -07001084 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001085 m++;
1086 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001087 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001088
1089 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001090 packet->read_z1 = READ_Z1(vref);
1091 x->tx_buf = &packet->read_z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001092 x->len = 1;
1093 spi_message_add_tail(x, m);
1094
1095 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001096 x->rx_buf = &packet->tc.z1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001097 x->len = 2;
1098 spi_message_add_tail(x, m);
1099
Semih Hazare4f48862007-07-18 00:35:56 -04001100 /* ... maybe discard first sample ... */
1101 if (pdata->settle_delay_usecs) {
1102 x->delay_usecs = pdata->settle_delay_usecs;
1103
1104 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001105 x->tx_buf = &packet->read_z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001106 x->len = 1;
1107 spi_message_add_tail(x, m);
1108
1109 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001110 x->rx_buf = &packet->tc.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001111 x->len = 2;
1112 spi_message_add_tail(x, m);
1113 }
1114
Jason Wang2991a1c2010-10-13 11:35:40 -07001115 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001116 m++;
1117 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001118 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001119
1120 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001121 packet->read_z2 = READ_Z2(vref);
1122 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001123 x->len = 1;
1124 spi_message_add_tail(x, m);
1125
1126 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001127 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001128 x->len = 2;
1129 spi_message_add_tail(x, m);
1130
Semih Hazare4f48862007-07-18 00:35:56 -04001131 /* ... maybe discard first sample ... */
1132 if (pdata->settle_delay_usecs) {
1133 x->delay_usecs = pdata->settle_delay_usecs;
1134
1135 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001136 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001137 x->len = 1;
1138 spi_message_add_tail(x, m);
1139
1140 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001141 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001142 x->len = 2;
1143 spi_message_add_tail(x, m);
1144 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001145 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001146
1147 /* power down */
Jason Wang2991a1c2010-10-13 11:35:40 -07001148 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001149 m++;
1150 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001151 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001152
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001153 if (ts->model == 7845) {
1154 x++;
1155 packet->pwrdown_cmd[0] = PWRDOWN;
1156 packet->pwrdown_cmd[1] = 0;
1157 packet->pwrdown_cmd[2] = 0;
1158 x->tx_buf = &packet->pwrdown_cmd[0];
1159 x->len = 3;
1160 } else {
1161 x++;
1162 packet->pwrdown = PWRDOWN;
1163 x->tx_buf = &packet->pwrdown;
1164 x->len = 1;
1165 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001166
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001167 x++;
1168 x->rx_buf = &packet->dummy;
1169 x->len = 2;
1170 }
1171
David Brownelld93f70b2006-02-15 00:49:35 -05001172 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001173 spi_message_add_tail(x, m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001174}
David Brownellffa458c2006-01-08 13:34:21 -08001175
Daniel Macka6080262013-06-27 23:42:17 -07001176#ifdef CONFIG_OF
1177static const struct of_device_id ads7846_dt_ids[] = {
1178 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1179 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1180 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1181 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1182 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1183 { }
1184};
1185MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1186
1187static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1188{
1189 struct ads7846_platform_data *pdata;
1190 struct device_node *node = dev->of_node;
1191 const struct of_device_id *match;
1192
1193 if (!node) {
1194 dev_err(dev, "Device does not have associated DT data\n");
1195 return ERR_PTR(-EINVAL);
1196 }
1197
1198 match = of_match_device(ads7846_dt_ids, dev);
1199 if (!match) {
1200 dev_err(dev, "Unknown device model\n");
1201 return ERR_PTR(-EINVAL);
1202 }
1203
1204 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1205 if (!pdata)
1206 return ERR_PTR(-ENOMEM);
1207
1208 pdata->model = (unsigned long)match->data;
1209
1210 of_property_read_u16(node, "ti,vref-delay-usecs",
1211 &pdata->vref_delay_usecs);
1212 of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1213 pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1214
1215 pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1216
1217 of_property_read_u16(node, "ti,settle-delay-usec",
1218 &pdata->settle_delay_usecs);
1219 of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1220 &pdata->penirq_recheck_delay_usecs);
1221
1222 of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1223 of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1224
1225 of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1226 of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1227 of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1228 of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1229
1230 of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1231 of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1232
1233 of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1234 of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1235 of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1236
1237 of_property_read_u32(node, "ti,pendown-gpio-debounce",
1238 &pdata->gpio_pendown_debounce);
1239
1240 pdata->wakeup = of_property_read_bool(node, "linux,wakeup");
1241
1242 pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1243
1244 return pdata;
1245}
1246#else
1247static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1248{
1249 dev_err(dev, "no platform data defined\n");
1250 return ERR_PTR(-EINVAL);
1251}
1252#endif
1253
Bill Pemberton5298cc42012-11-23 21:38:25 -08001254static int ads7846_probe(struct spi_device *spi)
Jason Wang2991a1c2010-10-13 11:35:40 -07001255{
Daniel Macka6080262013-06-27 23:42:17 -07001256 const struct ads7846_platform_data *pdata;
Jason Wang2991a1c2010-10-13 11:35:40 -07001257 struct ads7846 *ts;
1258 struct ads7846_packet *packet;
1259 struct input_dev *input_dev;
Jason Wang2991a1c2010-10-13 11:35:40 -07001260 unsigned long irq_flags;
1261 int err;
David Brownellffa458c2006-01-08 13:34:21 -08001262
Jason Wang2991a1c2010-10-13 11:35:40 -07001263 if (!spi->irq) {
1264 dev_dbg(&spi->dev, "no IRQ?\n");
Daniel Macka6080262013-06-27 23:42:17 -07001265 return -EINVAL;
Jason Wang2991a1c2010-10-13 11:35:40 -07001266 }
1267
1268 /* don't exceed max specified sample rate */
1269 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
Daniel Macka6080262013-06-27 23:42:17 -07001270 dev_err(&spi->dev, "f(sample) %d KHz?\n",
Jason Wang2991a1c2010-10-13 11:35:40 -07001271 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1272 return -EINVAL;
1273 }
1274
Daniel Macka6080262013-06-27 23:42:17 -07001275 /*
1276 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
Jason Wang2991a1c2010-10-13 11:35:40 -07001277 * that even if the hardware can do that, the SPI controller driver
1278 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1279 */
1280 spi->bits_per_word = 8;
1281 spi->mode = SPI_MODE_0;
1282 err = spi_setup(spi);
1283 if (err < 0)
1284 return err;
1285
1286 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1287 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1288 input_dev = input_allocate_device();
1289 if (!ts || !packet || !input_dev) {
1290 err = -ENOMEM;
1291 goto err_free_mem;
1292 }
1293
Jingoo Hanc12454f2013-04-07 20:52:07 -07001294 spi_set_drvdata(spi, ts);
Jason Wang2991a1c2010-10-13 11:35:40 -07001295
1296 ts->packet = packet;
1297 ts->spi = spi;
1298 ts->input = input_dev;
Jason Wang2991a1c2010-10-13 11:35:40 -07001299
1300 mutex_init(&ts->lock);
1301 init_waitqueue_head(&ts->wait);
1302
Daniel Macka6080262013-06-27 23:42:17 -07001303 pdata = dev_get_platdata(&spi->dev);
1304 if (!pdata) {
1305 pdata = ads7846_probe_dt(&spi->dev);
1306 if (IS_ERR(pdata))
1307 return PTR_ERR(pdata);
1308 }
1309
Jason Wang2991a1c2010-10-13 11:35:40 -07001310 ts->model = pdata->model ? : 7846;
1311 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1312 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1313 ts->pressure_max = pdata->pressure_max ? : ~0;
1314
Daniel Macka6080262013-06-27 23:42:17 -07001315 ts->vref_mv = pdata->vref_mv;
1316 ts->swap_xy = pdata->swap_xy;
1317
Jason Wang2991a1c2010-10-13 11:35:40 -07001318 if (pdata->filter != NULL) {
1319 if (pdata->filter_init != NULL) {
1320 err = pdata->filter_init(pdata, &ts->filter_data);
1321 if (err < 0)
1322 goto err_free_mem;
1323 }
1324 ts->filter = pdata->filter;
1325 ts->filter_cleanup = pdata->filter_cleanup;
1326 } else if (pdata->debounce_max) {
1327 ts->debounce_max = pdata->debounce_max;
1328 if (ts->debounce_max < 2)
1329 ts->debounce_max = 2;
1330 ts->debounce_tol = pdata->debounce_tol;
1331 ts->debounce_rep = pdata->debounce_rep;
1332 ts->filter = ads7846_debounce_filter;
1333 ts->filter_data = ts;
1334 } else {
1335 ts->filter = ads7846_no_filter;
1336 }
1337
Dmitry Torokhov57691a12013-06-27 23:29:25 -07001338 err = ads7846_setup_pendown(spi, ts, pdata);
Jason Wang2991a1c2010-10-13 11:35:40 -07001339 if (err)
1340 goto err_cleanup_filter;
1341
1342 if (pdata->penirq_recheck_delay_usecs)
1343 ts->penirq_recheck_delay_usecs =
1344 pdata->penirq_recheck_delay_usecs;
1345
1346 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1347
1348 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1349 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1350
1351 input_dev->name = ts->name;
1352 input_dev->phys = ts->phys;
1353 input_dev->dev.parent = &spi->dev;
1354
1355 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1356 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1357 input_set_abs_params(input_dev, ABS_X,
1358 pdata->x_min ? : 0,
1359 pdata->x_max ? : MAX_12BIT,
1360 0, 0);
1361 input_set_abs_params(input_dev, ABS_Y,
1362 pdata->y_min ? : 0,
1363 pdata->y_max ? : MAX_12BIT,
1364 0, 0);
1365 input_set_abs_params(input_dev, ABS_PRESSURE,
1366 pdata->pressure_min, pdata->pressure_max, 0, 0);
1367
1368 ads7846_setup_spi_msg(ts, pdata);
Imre Deakd5b415c2006-04-26 00:13:18 -04001369
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001370 ts->reg = regulator_get(&spi->dev, "vcc");
1371 if (IS_ERR(ts->reg)) {
Kevin Hilman067fb2f2010-05-26 23:30:55 -07001372 err = PTR_ERR(ts->reg);
Dmitry Torokhov56960b32010-06-02 00:40:06 -07001373 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001374 goto err_free_gpio;
1375 }
1376
1377 err = regulator_enable(ts->reg);
1378 if (err) {
1379 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1380 goto err_put_regulator;
1381 }
1382
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001383 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
Jason Wang2991a1c2010-10-13 11:35:40 -07001384 irq_flags |= IRQF_ONESHOT;
Anatolij Gustschin78043022010-06-28 01:25:19 -07001385
Jason Wang2991a1c2010-10-13 11:35:40 -07001386 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1387 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001388 if (err && !pdata->irq_flags) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001389 dev_info(&spi->dev,
1390 "trying pin change workaround on irq %d\n", spi->irq);
Jason Wang2991a1c2010-10-13 11:35:40 -07001391 irq_flags |= IRQF_TRIGGER_RISING;
1392 err = request_threaded_irq(spi->irq,
1393 ads7846_hard_irq, ads7846_irq,
1394 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001395 }
1396
1397 if (err) {
1398 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1399 goto err_disable_regulator;
David Brownellffa458c2006-01-08 13:34:21 -08001400 }
David Brownellffa458c2006-01-08 13:34:21 -08001401
David Brownell2c8dc072007-01-18 00:45:48 -05001402 err = ads784x_hwmon_register(spi, ts);
1403 if (err)
1404 goto err_free_irq;
1405
David Brownell2e5a7bd2006-01-08 13:34:25 -08001406 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001407
Jason Wang2991a1c2010-10-13 11:35:40 -07001408 /*
1409 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001410 * the touchscreen, in case it's not connected.
1411 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001412 if (ts->model == 7845)
1413 ads7845_read12_ser(&spi->dev, PWRDOWN);
1414 else
Alexander Steinebcaaad2011-05-11 16:24:08 -07001415 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
David Brownellffa458c2006-01-08 13:34:21 -08001416
David Brownell2c8dc072007-01-18 00:45:48 -05001417 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001418 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001419 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001420
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001421 err = input_register_device(input_dev);
1422 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001423 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001424
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001425 device_init_wakeup(&spi->dev, pdata->wakeup);
1426
Daniel Macka6080262013-06-27 23:42:17 -07001427 /*
1428 * If device does not carry platform data we must have allocated it
1429 * when parsing DT data.
1430 */
1431 if (!dev_get_platdata(&spi->dev))
1432 devm_kfree(&spi->dev, (void *)pdata);
1433
David Brownellffa458c2006-01-08 13:34:21 -08001434 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001435
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001436 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001437 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1438 err_remove_hwmon:
1439 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001440 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001441 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001442 err_disable_regulator:
1443 regulator_disable(ts->reg);
1444 err_put_regulator:
1445 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001446 err_free_gpio:
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001447 if (!ts->get_pendown_state)
Eric Miao4d5975e2008-09-10 12:06:15 -04001448 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001449 err_cleanup_filter:
1450 if (ts->filter_cleanup)
1451 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001452 err_free_mem:
1453 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001454 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001455 kfree(ts);
1456 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001457}
1458
Bill Pembertone2619cf2012-11-23 21:50:47 -08001459static int ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001460{
Jingoo Hanc12454f2013-04-07 20:52:07 -07001461 struct ads7846 *ts = spi_get_drvdata(spi);
David Brownellffa458c2006-01-08 13:34:21 -08001462
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001463 device_init_wakeup(&spi->dev, false);
1464
David Brownell2c8dc072007-01-18 00:45:48 -05001465 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001466
Jason Wang2991a1c2010-10-13 11:35:40 -07001467 ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001468 free_irq(ts->spi->irq, ts);
Jason Wang2991a1c2010-10-13 11:35:40 -07001469
1470 input_unregister_device(ts->input);
1471
1472 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001473
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001474 regulator_disable(ts->reg);
1475 regulator_put(ts->reg);
1476
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001477 if (!ts->get_pendown_state) {
1478 /*
1479 * If we are not using specialized pendown method we must
1480 * have been relying on gpio we set up ourselves.
1481 */
Eric Miao4d5975e2008-09-10 12:06:15 -04001482 gpio_free(ts->gpio_pendown);
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001483 }
Eric Miao4d5975e2008-09-10 12:06:15 -04001484
Imre Deakda970e62007-01-18 00:44:41 -05001485 if (ts->filter_cleanup)
1486 ts->filter_cleanup(ts->filter_data);
1487
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001488 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001489 kfree(ts);
1490
David Brownell2e5a7bd2006-01-08 13:34:25 -08001491 dev_dbg(&spi->dev, "unregistered touchscreen\n");
Jason Wang2991a1c2010-10-13 11:35:40 -07001492
David Brownellffa458c2006-01-08 13:34:21 -08001493 return 0;
1494}
1495
David Brownell2e5a7bd2006-01-08 13:34:25 -08001496static struct spi_driver ads7846_driver = {
1497 .driver = {
1498 .name = "ads7846",
David Brownell2e5a7bd2006-01-08 13:34:25 -08001499 .owner = THIS_MODULE,
Mark Brown3c36e712011-01-20 22:51:26 -08001500 .pm = &ads7846_pm,
Daniel Macka6080262013-06-27 23:42:17 -07001501 .of_match_table = of_match_ptr(ads7846_dt_ids),
David Brownell2e5a7bd2006-01-08 13:34:25 -08001502 },
David Brownellffa458c2006-01-08 13:34:21 -08001503 .probe = ads7846_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001504 .remove = ads7846_remove,
David Brownellffa458c2006-01-08 13:34:21 -08001505};
1506
Axel Linca839222012-03-16 23:05:26 -07001507module_spi_driver(ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001508
1509MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1510MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001511MODULE_ALIAS("spi:ads7846");