blob: 569578638233b6b49bd638916cfc5eb1ea55c13e [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)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500105 struct attribute_group *attr_group;
Tony Jones1beeffe2007-08-20 13:46:20 -0700106 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500107#endif
108
David Brownellffa458c2006-01-08 13:34:21 -0800109 u16 model;
David Brownell7c6d0ee2008-04-02 00:43:01 -0400110 u16 vref_mv;
David Brownellffa458c2006-01-08 13:34:21 -0800111 u16 vref_delay_usecs;
112 u16 x_plate_ohms;
Imre Deakd5b415c2006-04-26 00:13:18 -0400113 u16 pressure_max;
David Brownellffa458c2006-01-08 13:34:21 -0800114
Michael Roth86579a42009-05-18 16:04:44 -0700115 bool swap_xy;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700116 bool use_internal;
Michael Roth86579a42009-05-18 16:04:44 -0700117
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400118 struct ads7846_packet *packet;
David Brownellffa458c2006-01-08 13:34:21 -0800119
Semih Hazare4f48862007-07-18 00:35:56 -0400120 struct spi_transfer xfer[18];
Imre Deak0b7018a2006-04-11 23:42:03 -0400121 struct spi_message msg[5];
Jason Wang2991a1c2010-10-13 11:35:40 -0700122 int msg_count;
123 wait_queue_head_t wait;
124
125 bool pendown;
126
Imre Deak0b7018a2006-04-11 23:42:03 -0400127 int read_cnt;
Imre Deakd5b415c2006-04-26 00:13:18 -0400128 int read_rep;
Imre Deak0b7018a2006-04-11 23:42:03 -0400129 int last_read;
130
131 u16 debounce_max;
132 u16 debounce_tol;
Imre Deakd5b415c2006-04-26 00:13:18 -0400133 u16 debounce_rep;
David Brownellffa458c2006-01-08 13:34:21 -0800134
Semih Hazar1d258912007-07-18 00:36:04 -0400135 u16 penirq_recheck_delay_usecs;
136
Jason Wang2991a1c2010-10-13 11:35:40 -0700137 struct mutex lock;
138 bool stopped; /* P: lock */
139 bool disabled; /* P: lock */
140 bool suspended; /* P: lock */
Imre Deakc9e617a2006-04-11 23:44:05 -0400141
Imre Deakda970e62007-01-18 00:44:41 -0500142 int (*filter)(void *data, int data_idx, int *val);
143 void *filter_data;
144 void (*filter_cleanup)(void *data);
Imre Deakc9e617a2006-04-11 23:44:05 -0400145 int (*get_pendown_state)(void);
Eric Miao4d5975e2008-09-10 12:06:15 -0400146 int gpio_pendown;
Eric Miaofd746d52009-04-11 16:54:59 -0700147
148 void (*wait_for_sync)(void);
David Brownellffa458c2006-01-08 13:34:21 -0800149};
150
151/* leave chip selected when we're done, for quicker re-select? */
152#if 0
153#define CS_CHANGE(xfer) ((xfer).cs_change = 1)
154#else
155#define CS_CHANGE(xfer) ((xfer).cs_change = 0)
156#endif
157
158/*--------------------------------------------------------------------------*/
159
160/* The ADS7846 has touchscreen and other sensors.
161 * Earlier ads784x chips are somewhat compatible.
162 */
163#define ADS_START (1 << 7)
164#define ADS_A2A1A0_d_y (1 << 4) /* differential */
165#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */
166#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */
167#define ADS_A2A1A0_d_x (5 << 4) /* differential */
168#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */
169#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */
170#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */
171#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */
172#define ADS_8_BIT (1 << 3)
173#define ADS_12_BIT (0 << 3)
174#define ADS_SER (1 << 2) /* non-differential */
175#define ADS_DFR (0 << 2) /* differential */
Jason Wang2991a1c2010-10-13 11:35:40 -0700176#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
David Brownellffa458c2006-01-08 13:34:21 -0800177#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
178#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
179#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
180
181#define MAX_12BIT ((1<<12)-1)
182
183/* leave ADC powered up (disables penirq) between differential samples */
Imre Deakde2defd2007-01-18 00:45:21 -0500184#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
185 | ADS_12_BIT | ADS_DFR | \
186 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
David Brownellffa458c2006-01-08 13:34:21 -0800187
Imre Deakde2defd2007-01-18 00:45:21 -0500188#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
189#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
190#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
Imre Deak53a0ef892006-04-11 23:41:49 -0400191
Imre Deakde2defd2007-01-18 00:45:21 -0500192#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
193#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
David Brownellffa458c2006-01-08 13:34:21 -0800194
195/* single-ended samples need to first power up reference voltage;
196 * we leave both ADC and VREF powered
197 */
198#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
199 | ADS_12_BIT | ADS_SER)
200
Imre Deakde2defd2007-01-18 00:45:21 -0500201#define REF_ON (READ_12BIT_DFR(x, 1, 1))
202#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
David Brownellffa458c2006-01-08 13:34:21 -0800203
Jason Wang2991a1c2010-10-13 11:35:40 -0700204/* Must be called with ts->lock held */
205static void ads7846_stop(struct ads7846 *ts)
206{
207 if (!ts->disabled && !ts->suspended) {
208 /* Signal IRQ thread to stop polling and disable the handler. */
209 ts->stopped = true;
210 mb();
211 wake_up(&ts->wait);
212 disable_irq(ts->spi->irq);
213 }
214}
215
216/* Must be called with ts->lock held */
217static void ads7846_restart(struct ads7846 *ts)
218{
219 if (!ts->disabled && !ts->suspended) {
220 /* Tell IRQ thread that it may poll the device. */
221 ts->stopped = false;
222 mb();
223 enable_irq(ts->spi->irq);
224 }
225}
226
227/* Must be called with ts->lock held */
228static void __ads7846_disable(struct ads7846 *ts)
229{
230 ads7846_stop(ts);
231 regulator_disable(ts->reg);
232
233 /*
234 * We know the chip's in low power mode since we always
235 * leave it that way after every request
236 */
237}
238
239/* Must be called with ts->lock held */
240static void __ads7846_enable(struct ads7846 *ts)
241{
Mark Brownf94352f2013-03-03 20:19:07 -0800242 int error;
243
244 error = regulator_enable(ts->reg);
245 if (error != 0)
246 dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error);
247
Jason Wang2991a1c2010-10-13 11:35:40 -0700248 ads7846_restart(ts);
249}
250
251static void ads7846_disable(struct ads7846 *ts)
252{
253 mutex_lock(&ts->lock);
254
255 if (!ts->disabled) {
256
257 if (!ts->suspended)
258 __ads7846_disable(ts);
259
260 ts->disabled = true;
261 }
262
263 mutex_unlock(&ts->lock);
264}
265
266static void ads7846_enable(struct ads7846 *ts)
267{
268 mutex_lock(&ts->lock);
269
270 if (ts->disabled) {
271
272 ts->disabled = false;
273
274 if (!ts->suspended)
275 __ads7846_enable(ts);
276 }
277
278 mutex_unlock(&ts->lock);
279}
280
David Brownellffa458c2006-01-08 13:34:21 -0800281/*--------------------------------------------------------------------------*/
282
283/*
284 * Non-touchscreen sensors only use single-ended conversions.
David Brownell2c8dc072007-01-18 00:45:48 -0500285 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
286 * ads7846 lets that pin be unconnected, to use internal vREF.
David Brownellffa458c2006-01-08 13:34:21 -0800287 */
288
289struct ser_req {
David Brownelld93f70b2006-02-15 00:49:35 -0500290 u8 ref_on;
David Brownellffa458c2006-01-08 13:34:21 -0800291 u8 command;
David Brownelld93f70b2006-02-15 00:49:35 -0500292 u8 ref_off;
David Brownellffa458c2006-01-08 13:34:21 -0800293 u16 scratch;
David Brownellffa458c2006-01-08 13:34:21 -0800294 struct spi_message msg;
295 struct spi_transfer xfer[6];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700296 /*
297 * DMA (thus cache coherency maintenance) requires the
298 * transfer buffers to live in their own cache lines.
299 */
300 __be16 sample ____cacheline_aligned;
David Brownellffa458c2006-01-08 13:34:21 -0800301};
302
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700303struct ads7845_ser_req {
304 u8 command[3];
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700305 struct spi_message msg;
306 struct spi_transfer xfer[2];
Alexander Stein1dbe7da2011-05-05 08:40:14 -0700307 /*
308 * DMA (thus cache coherency maintenance) requires the
309 * transfer buffers to live in their own cache lines.
310 */
311 u8 sample[3] ____cacheline_aligned;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700312};
313
David Brownellffa458c2006-01-08 13:34:21 -0800314static int ads7846_read12_ser(struct device *dev, unsigned command)
315{
Jason Wang2991a1c2010-10-13 11:35:40 -0700316 struct spi_device *spi = to_spi_device(dev);
317 struct ads7846 *ts = dev_get_drvdata(dev);
318 struct ser_req *req;
319 int status;
David Brownellffa458c2006-01-08 13:34:21 -0800320
Jason Wang2991a1c2010-10-13 11:35:40 -0700321 req = kzalloc(sizeof *req, GFP_KERNEL);
David Brownellffa458c2006-01-08 13:34:21 -0800322 if (!req)
323 return -ENOMEM;
324
Imre Deak0b7018a2006-04-11 23:42:03 -0400325 spi_message_init(&req->msg);
Vitaly Wool8275c642006-01-08 13:34:28 -0800326
David Brownell2c8dc072007-01-18 00:45:48 -0500327 /* maybe turn on internal vREF, and let it settle */
Alexander Steinebcaaad2011-05-11 16:24:08 -0700328 if (ts->use_internal) {
David Brownell2c8dc072007-01-18 00:45:48 -0500329 req->ref_on = REF_ON;
330 req->xfer[0].tx_buf = &req->ref_on;
331 req->xfer[0].len = 1;
332 spi_message_add_tail(&req->xfer[0], &req->msg);
333
334 req->xfer[1].rx_buf = &req->scratch;
335 req->xfer[1].len = 2;
336
337 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
338 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
339 spi_message_add_tail(&req->xfer[1], &req->msg);
Alexander Steinebcaaad2011-05-11 16:24:08 -0700340
341 /* Enable reference voltage */
342 command |= ADS_PD10_REF_ON;
David Brownell2c8dc072007-01-18 00:45:48 -0500343 }
David Brownellffa458c2006-01-08 13:34:21 -0800344
Alexander Steinebcaaad2011-05-11 16:24:08 -0700345 /* Enable ADC in every case */
346 command |= ADS_PD10_ADC_ON;
347
David Brownellffa458c2006-01-08 13:34:21 -0800348 /* take sample */
349 req->command = (u8) command;
350 req->xfer[2].tx_buf = &req->command;
351 req->xfer[2].len = 1;
David Brownell2c8dc072007-01-18 00:45:48 -0500352 spi_message_add_tail(&req->xfer[2], &req->msg);
353
David Brownellffa458c2006-01-08 13:34:21 -0800354 req->xfer[3].rx_buf = &req->sample;
355 req->xfer[3].len = 2;
David Brownell2c8dc072007-01-18 00:45:48 -0500356 spi_message_add_tail(&req->xfer[3], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800357
358 /* REVISIT: take a few more samples, and compare ... */
359
Nicolas Ferre969111e2007-02-28 23:51:03 -0500360 /* converter in low power mode & enable PENIRQ */
361 req->ref_off = PWRDOWN;
362 req->xfer[4].tx_buf = &req->ref_off;
363 req->xfer[4].len = 1;
364 spi_message_add_tail(&req->xfer[4], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800365
Nicolas Ferre969111e2007-02-28 23:51:03 -0500366 req->xfer[5].rx_buf = &req->scratch;
367 req->xfer[5].len = 2;
368 CS_CHANGE(req->xfer[5]);
369 spi_message_add_tail(&req->xfer[5], &req->msg);
David Brownellffa458c2006-01-08 13:34:21 -0800370
Jason Wang2991a1c2010-10-13 11:35:40 -0700371 mutex_lock(&ts->lock);
372 ads7846_stop(ts);
David Brownellffa458c2006-01-08 13:34:21 -0800373 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700374 ads7846_restart(ts);
375 mutex_unlock(&ts->lock);
David Brownellffa458c2006-01-08 13:34:21 -0800376
Marc Pignatc24b2602007-12-04 23:45:11 -0800377 if (status == 0) {
378 /* on-wire is a must-ignore bit, a BE12 value, then padding */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400379 status = be16_to_cpu(req->sample);
380 status = status >> 3;
381 status &= 0x0fff;
Marc Pignatc24b2602007-12-04 23:45:11 -0800382 }
David Brownell90845332006-05-25 18:44:20 -0700383
384 kfree(req);
David Brownell7c6d0ee2008-04-02 00:43:01 -0400385 return status;
David Brownellffa458c2006-01-08 13:34:21 -0800386}
387
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700388static int ads7845_read12_ser(struct device *dev, unsigned command)
389{
Jason Wang2991a1c2010-10-13 11:35:40 -0700390 struct spi_device *spi = to_spi_device(dev);
391 struct ads7846 *ts = dev_get_drvdata(dev);
392 struct ads7845_ser_req *req;
393 int status;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700394
Jason Wang2991a1c2010-10-13 11:35:40 -0700395 req = kzalloc(sizeof *req, GFP_KERNEL);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700396 if (!req)
397 return -ENOMEM;
398
399 spi_message_init(&req->msg);
400
401 req->command[0] = (u8) command;
402 req->xfer[0].tx_buf = req->command;
403 req->xfer[0].rx_buf = req->sample;
404 req->xfer[0].len = 3;
405 spi_message_add_tail(&req->xfer[0], &req->msg);
406
Jason Wang2991a1c2010-10-13 11:35:40 -0700407 mutex_lock(&ts->lock);
408 ads7846_stop(ts);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700409 status = spi_sync(spi, &req->msg);
Jason Wang2991a1c2010-10-13 11:35:40 -0700410 ads7846_restart(ts);
411 mutex_unlock(&ts->lock);
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700412
413 if (status == 0) {
414 /* BE12 value, then padding */
415 status = be16_to_cpu(*((u16 *)&req->sample[1]));
416 status = status >> 3;
417 status &= 0x0fff;
418 }
419
420 kfree(req);
421 return status;
422}
423
Fabio Estevamc52b4fc2013-12-01 22:11:26 -0800424#if IS_ENABLED(CONFIG_HWMON)
David Brownell2c8dc072007-01-18 00:45:48 -0500425
426#define SHOW(name, var, adjust) static ssize_t \
David Brownellffa458c2006-01-08 13:34:21 -0800427name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
428{ \
David Brownell2c8dc072007-01-18 00:45:48 -0500429 struct ads7846 *ts = dev_get_drvdata(dev); \
David Brownellffa458c2006-01-08 13:34:21 -0800430 ssize_t v = ads7846_read12_ser(dev, \
Alexander Steinebcaaad2011-05-11 16:24:08 -0700431 READ_12BIT_SER(var)); \
David Brownellffa458c2006-01-08 13:34:21 -0800432 if (v < 0) \
433 return v; \
David Brownell2c8dc072007-01-18 00:45:48 -0500434 return sprintf(buf, "%u\n", adjust(ts, v)); \
David Brownellffa458c2006-01-08 13:34:21 -0800435} \
436static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
437
David Brownell2c8dc072007-01-18 00:45:48 -0500438
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400439/* Sysfs conventions report temperatures in millidegrees Celsius.
David Brownell2c8dc072007-01-18 00:45:48 -0500440 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
441 * accuracy scheme without calibration data. For now we won't try either;
442 * userspace sees raw sensor values, and must scale/calibrate appropriately.
443 */
444static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
445{
446 return v;
447}
448
449SHOW(temp0, temp0, null_adjust) /* temp1_input */
450SHOW(temp1, temp1, null_adjust) /* temp2_input */
451
452
453/* sysfs conventions report voltages in millivolts. We can convert voltages
454 * if we know vREF. userspace may need to scale vAUX to match the board's
455 * external resistors; we assume that vBATT only uses the internal ones.
456 */
457static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
458{
459 unsigned retval = v;
460
461 /* external resistors may scale vAUX into 0..vREF */
David Brownell7c6d0ee2008-04-02 00:43:01 -0400462 retval *= ts->vref_mv;
David Brownell2c8dc072007-01-18 00:45:48 -0500463 retval = retval >> 12;
Jason Wang2991a1c2010-10-13 11:35:40 -0700464
David Brownell2c8dc072007-01-18 00:45:48 -0500465 return retval;
466}
467
468static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
469{
470 unsigned retval = vaux_adjust(ts, v);
471
472 /* ads7846 has a resistor ladder to scale this signal down */
473 if (ts->model == 7846)
474 retval *= 4;
Jason Wang2991a1c2010-10-13 11:35:40 -0700475
David Brownell2c8dc072007-01-18 00:45:48 -0500476 return retval;
477}
478
479SHOW(in0_input, vaux, vaux_adjust)
480SHOW(in1_input, vbatt, vbatt_adjust)
481
David Brownell2c8dc072007-01-18 00:45:48 -0500482static struct attribute *ads7846_attributes[] = {
483 &dev_attr_temp0.attr,
484 &dev_attr_temp1.attr,
485 &dev_attr_in0_input.attr,
486 &dev_attr_in1_input.attr,
487 NULL,
488};
489
490static struct attribute_group ads7846_attr_group = {
491 .attrs = ads7846_attributes,
492};
493
494static struct attribute *ads7843_attributes[] = {
495 &dev_attr_in0_input.attr,
496 &dev_attr_in1_input.attr,
497 NULL,
498};
499
500static struct attribute_group ads7843_attr_group = {
501 .attrs = ads7843_attributes,
502};
503
504static struct attribute *ads7845_attributes[] = {
505 &dev_attr_in0_input.attr,
506 NULL,
507};
508
509static struct attribute_group ads7845_attr_group = {
510 .attrs = ads7845_attributes,
511};
512
513static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
514{
Tony Jones1beeffe2007-08-20 13:46:20 -0700515 struct device *hwmon;
David Brownell2c8dc072007-01-18 00:45:48 -0500516 int err;
517
518 /* hwmon sensors need a reference voltage */
519 switch (ts->model) {
520 case 7846:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400521 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500522 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
David Brownell7c6d0ee2008-04-02 00:43:01 -0400523 ts->vref_mv = 2500;
Alexander Steinebcaaad2011-05-11 16:24:08 -0700524 ts->use_internal = true;
David Brownell2c8dc072007-01-18 00:45:48 -0500525 }
526 break;
527 case 7845:
528 case 7843:
David Brownell7c6d0ee2008-04-02 00:43:01 -0400529 if (!ts->vref_mv) {
David Brownell2c8dc072007-01-18 00:45:48 -0500530 dev_warn(&spi->dev,
531 "external vREF for ADS%d not specified\n",
532 ts->model);
533 return 0;
534 }
535 break;
536 }
537
538 /* different chips have different sensor groups */
539 switch (ts->model) {
540 case 7846:
541 ts->attr_group = &ads7846_attr_group;
542 break;
543 case 7845:
544 ts->attr_group = &ads7845_attr_group;
545 break;
546 case 7843:
547 ts->attr_group = &ads7843_attr_group;
548 break;
549 default:
550 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
551 return 0;
552 }
553
554 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
555 if (err)
556 return err;
557
558 hwmon = hwmon_device_register(&spi->dev);
559 if (IS_ERR(hwmon)) {
560 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
561 return PTR_ERR(hwmon);
562 }
563
564 ts->hwmon = hwmon;
565 return 0;
566}
567
568static void ads784x_hwmon_unregister(struct spi_device *spi,
569 struct ads7846 *ts)
570{
571 if (ts->hwmon) {
572 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
573 hwmon_device_unregister(ts->hwmon);
574 }
575}
576
577#else
578static inline int ads784x_hwmon_register(struct spi_device *spi,
579 struct ads7846 *ts)
580{
581 return 0;
582}
583
584static inline void ads784x_hwmon_unregister(struct spi_device *spi,
585 struct ads7846 *ts)
586{
587}
588#endif
David Brownellffa458c2006-01-08 13:34:21 -0800589
Imre Deak438f2a72006-04-11 23:41:32 -0400590static ssize_t ads7846_pen_down_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
Jason Wang2991a1c2010-10-13 11:35:40 -0700593 struct ads7846 *ts = dev_get_drvdata(dev);
594
595 return sprintf(buf, "%u\n", ts->pendown);
Imre Deak438f2a72006-04-11 23:41:32 -0400596}
597
598static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
599
Imre Deak7de90a82006-04-11 23:43:55 -0400600static ssize_t ads7846_disable_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
Jason Wang2991a1c2010-10-13 11:35:40 -0700603 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400604
605 return sprintf(buf, "%u\n", ts->disabled);
606}
607
608static ssize_t ads7846_disable_store(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf, size_t count)
611{
612 struct ads7846 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800613 unsigned int i;
614 int err;
Imre Deak7de90a82006-04-11 23:43:55 -0400615
JJ Ding76496e72011-11-09 10:20:14 -0800616 err = kstrtouint(buf, 10, &i);
617 if (err)
618 return err;
Joe Rouvier160f1fe2008-08-10 00:29:25 -0400619
Imre Deak7de90a82006-04-11 23:43:55 -0400620 if (i)
621 ads7846_disable(ts);
622 else
623 ads7846_enable(ts);
624
Imre Deak7de90a82006-04-11 23:43:55 -0400625 return count;
626}
627
628static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
629
David Brownell2c8dc072007-01-18 00:45:48 -0500630static struct attribute *ads784x_attributes[] = {
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500631 &dev_attr_pen_down.attr,
632 &dev_attr_disable.attr,
633 NULL,
634};
635
David Brownell2c8dc072007-01-18 00:45:48 -0500636static struct attribute_group ads784x_attr_group = {
637 .attrs = ads784x_attributes,
Dmitry Torokhov8dd51652006-11-02 23:34:09 -0500638};
639
David Brownellffa458c2006-01-08 13:34:21 -0800640/*--------------------------------------------------------------------------*/
641
Eric Miao4d5975e2008-09-10 12:06:15 -0400642static int get_pendown_state(struct ads7846 *ts)
643{
644 if (ts->get_pendown_state)
645 return ts->get_pendown_state();
646
647 return !gpio_get_value(ts->gpio_pendown);
648}
649
Eric Miaofd746d52009-04-11 16:54:59 -0700650static void null_wait_for_sync(void)
651{
652}
653
Jason Wang2991a1c2010-10-13 11:35:40 -0700654static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
David Brownellffa458c2006-01-08 13:34:21 -0800655{
Jason Wang2991a1c2010-10-13 11:35:40 -0700656 struct ads7846 *ts = ads;
David Brownellffa458c2006-01-08 13:34:21 -0800657
Jason Wang2991a1c2010-10-13 11:35:40 -0700658 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
659 /* Start over collecting consistent readings. */
660 ts->read_rep = 0;
661 /*
662 * Repeat it, if this was the first read or the read
663 * wasn't consistent enough.
664 */
665 if (ts->read_cnt < ts->debounce_max) {
666 ts->last_read = *val;
667 ts->read_cnt++;
668 return ADS7846_FILTER_REPEAT;
669 } else {
670 /*
671 * Maximum number of debouncing reached and still
672 * not enough number of consistent readings. Abort
673 * the whole sample, repeat it in the next sampling
674 * period.
675 */
676 ts->read_cnt = 0;
677 return ADS7846_FILTER_IGNORE;
678 }
679 } else {
680 if (++ts->read_rep > ts->debounce_rep) {
681 /*
682 * Got a good reading for this coordinate,
683 * go for the next one.
684 */
685 ts->read_cnt = 0;
686 ts->read_rep = 0;
687 return ADS7846_FILTER_OK;
688 } else {
689 /* Read more values that are consistent. */
690 ts->read_cnt++;
691 return ADS7846_FILTER_REPEAT;
692 }
693 }
694}
695
696static int ads7846_no_filter(void *ads, int data_idx, int *val)
697{
698 return ADS7846_FILTER_OK;
699}
700
701static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
702{
703 struct spi_transfer *t =
704 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
705
706 if (ts->model == 7845) {
707 return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
708 } else {
709 /*
710 * adjust: on-wire is a must-ignore bit, a BE12 value, then
711 * padding; built from two 8 bit values written msb-first.
712 */
713 return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
714 }
715}
716
717static void ads7846_update_value(struct spi_message *m, int val)
718{
719 struct spi_transfer *t =
720 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
721
722 *(u16 *)t->rx_buf = val;
723}
724
725static void ads7846_read_state(struct ads7846 *ts)
726{
727 struct ads7846_packet *packet = ts->packet;
728 struct spi_message *m;
729 int msg_idx = 0;
730 int val;
731 int action;
732 int error;
733
734 while (msg_idx < ts->msg_count) {
735
736 ts->wait_for_sync();
737
738 m = &ts->msg[msg_idx];
739 error = spi_sync(ts->spi, m);
740 if (error) {
741 dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
742 packet->tc.ignore = true;
743 return;
744 }
745
746 /*
747 * Last message is power down request, no need to convert
748 * or filter the value.
749 */
750 if (msg_idx < ts->msg_count - 1) {
751
752 val = ads7846_get_value(ts, m);
753
754 action = ts->filter(ts->filter_data, msg_idx, &val);
755 switch (action) {
756 case ADS7846_FILTER_REPEAT:
757 continue;
758
759 case ADS7846_FILTER_IGNORE:
760 packet->tc.ignore = true;
761 msg_idx = ts->msg_count - 1;
762 continue;
763
764 case ADS7846_FILTER_OK:
765 ads7846_update_value(m, val);
766 packet->tc.ignore = false;
767 msg_idx++;
768 break;
769
770 default:
771 BUG();
772 }
773 } else {
774 msg_idx++;
775 }
776 }
777}
778
779static void ads7846_report_state(struct ads7846 *ts)
780{
781 struct ads7846_packet *packet = ts->packet;
782 unsigned int Rt;
783 u16 x, y, z1, z2;
784
785 /*
786 * ads7846_get_value() does in-place conversion (including byte swap)
787 * from on-the-wire format as part of debouncing to get stable
788 * readings.
David Brownellffa458c2006-01-08 13:34:21 -0800789 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700790 if (ts->model == 7845) {
791 x = *(u16 *)packet->tc.x_buf;
792 y = *(u16 *)packet->tc.y_buf;
793 z1 = 0;
794 z2 = 0;
795 } else {
796 x = packet->tc.x;
797 y = packet->tc.y;
798 z1 = packet->tc.z1;
799 z2 = packet->tc.z2;
800 }
David Brownellffa458c2006-01-08 13:34:21 -0800801
802 /* range filtering */
803 if (x == MAX_12BIT)
804 x = 0;
805
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400806 if (ts->model == 7843) {
807 Rt = ts->pressure_max / 2;
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -0700808 } else if (ts->model == 7845) {
809 if (get_pendown_state(ts))
810 Rt = ts->pressure_max / 2;
811 else
812 Rt = 0;
813 dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400814 } else if (likely(x && z1)) {
David Brownellffa458c2006-01-08 13:34:21 -0800815 /* compute touch pressure resistance using equation #2 */
816 Rt = z2;
817 Rt -= z1;
818 Rt *= x;
819 Rt *= ts->x_plate_ohms;
820 Rt /= z1;
821 Rt = (Rt + 2047) >> 12;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400822 } else {
David Brownellffa458c2006-01-08 13:34:21 -0800823 Rt = 0;
Hans-Christian Egtvedt9460b652008-07-23 14:38:27 -0400824 }
Nicolas Ferre969111e2007-02-28 23:51:03 -0500825
Jason Wang2991a1c2010-10-13 11:35:40 -0700826 /*
827 * Sample found inconsistent by debouncing or pressure is beyond
Imre Deak1936d592007-01-18 00:45:31 -0500828 * the maximum. Don't report it to user space, repeat at least
829 * once more the measurement
830 */
Dmitry Torokhove8f462d2008-10-09 00:52:23 -0400831 if (packet->tc.ignore || Rt > ts->pressure_max) {
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800832 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
833 packet->tc.ignore, Rt);
Imre Deakd5b415c2006-04-26 00:13:18 -0400834 return;
835 }
836
Jason Wang2991a1c2010-10-13 11:35:40 -0700837 /*
838 * Maybe check the pendown state before reporting. This discards
Semih Hazar1d258912007-07-18 00:36:04 -0400839 * false readings when the pen is lifted.
840 */
841 if (ts->penirq_recheck_delay_usecs) {
842 udelay(ts->penirq_recheck_delay_usecs);
Eric Miao4d5975e2008-09-10 12:06:15 -0400843 if (!get_pendown_state(ts))
Semih Hazar1d258912007-07-18 00:36:04 -0400844 Rt = 0;
845 }
846
Jason Wang2991a1c2010-10-13 11:35:40 -0700847 /*
848 * NOTE: We can't rely on the pressure to determine the pen down
849 * state, even this controller has a pressure sensor. The pressure
Imre Deak15e35892007-01-18 00:45:43 -0500850 * value can fluctuate for quite a while after lifting the pen and
851 * in some cases may not even settle at the expected value.
David Brownellffa458c2006-01-08 13:34:21 -0800852 *
Imre Deak15e35892007-01-18 00:45:43 -0500853 * The only safe way to check for the pen up condition is in the
854 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
David Brownellffa458c2006-01-08 13:34:21 -0800855 */
David Brownellffa458c2006-01-08 13:34:21 -0800856 if (Rt) {
Imre Deak15e35892007-01-18 00:45:43 -0500857 struct input_dev *input = ts->input;
Imre Deakae82d5a2006-04-26 00:12:14 -0400858
Michael Roth86579a42009-05-18 16:04:44 -0700859 if (ts->swap_xy)
860 swap(x, y);
861
Jason Wang2991a1c2010-10-13 11:35:40 -0700862 if (!ts->pendown) {
863 input_report_key(input, BTN_TOUCH, 1);
864 ts->pendown = true;
865 dev_vdbg(&ts->spi->dev, "DOWN\n");
866 }
867
Imre Deak15e35892007-01-18 00:45:43 -0500868 input_report_abs(input, ABS_X, x);
869 input_report_abs(input, ABS_Y, y);
Pavel Machek30ad7ba2009-11-23 08:17:38 -0800870 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
David Brownellffa458c2006-01-08 13:34:21 -0800871
Imre Deak15e35892007-01-18 00:45:43 -0500872 input_sync(input);
Pavel Machek52ce4eaa2009-11-23 08:25:17 -0800873 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
Imre Deak15e35892007-01-18 00:45:43 -0500874 }
David Brownellffa458c2006-01-08 13:34:21 -0800875}
876
Jason Wang2991a1c2010-10-13 11:35:40 -0700877static irqreturn_t ads7846_hard_irq(int irq, void *handle)
Imre Deak0b7018a2006-04-11 23:42:03 -0400878{
Jason Wang2991a1c2010-10-13 11:35:40 -0700879 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400880
Jason Wang2991a1c2010-10-13 11:35:40 -0700881 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
Imre Deakda970e62007-01-18 00:44:41 -0500882}
883
David Brownellffa458c2006-01-08 13:34:21 -0800884
David Howells7d12e782006-10-05 14:55:46 +0100885static irqreturn_t ads7846_irq(int irq, void *handle)
David Brownellffa458c2006-01-08 13:34:21 -0800886{
Imre Deak0b7018a2006-04-11 23:42:03 -0400887 struct ads7846 *ts = handle;
Imre Deak0b7018a2006-04-11 23:42:03 -0400888
Jason Wang2991a1c2010-10-13 11:35:40 -0700889 /* Start with a small delay before checking pendown state */
890 msleep(TS_POLL_DELAY);
891
892 while (!ts->stopped && get_pendown_state(ts)) {
893
894 /* pen is down, continue with the measurement */
895 ads7846_read_state(ts);
896
897 if (!ts->stopped)
898 ads7846_report_state(ts);
899
900 wait_event_timeout(ts->wait, ts->stopped,
901 msecs_to_jiffies(TS_POLL_PERIOD));
Imre Deak0b7018a2006-04-11 23:42:03 -0400902 }
Jason Wang2991a1c2010-10-13 11:35:40 -0700903
904 if (ts->pendown) {
905 struct input_dev *input = ts->input;
906
907 input_report_key(input, BTN_TOUCH, 0);
908 input_report_abs(input, ABS_PRESSURE, 0);
909 input_sync(input);
910
911 ts->pendown = false;
912 dev_vdbg(&ts->spi->dev, "UP\n");
913 }
Imre Deak7de90a82006-04-11 23:43:55 -0400914
915 return IRQ_HANDLED;
David Brownellffa458c2006-01-08 13:34:21 -0800916}
917
Mark Brown3c36e712011-01-20 22:51:26 -0800918#ifdef CONFIG_PM_SLEEP
919static int ads7846_suspend(struct device *dev)
Imre Deak7de90a82006-04-11 23:43:55 -0400920{
Mark Brown3c36e712011-01-20 22:51:26 -0800921 struct ads7846 *ts = dev_get_drvdata(dev);
Imre Deak7de90a82006-04-11 23:43:55 -0400922
Jason Wang2991a1c2010-10-13 11:35:40 -0700923 mutex_lock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400924
Jason Wang2991a1c2010-10-13 11:35:40 -0700925 if (!ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400926
Jason Wang2991a1c2010-10-13 11:35:40 -0700927 if (!ts->disabled)
928 __ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -0400929
Jason Wang2991a1c2010-10-13 11:35:40 -0700930 if (device_may_wakeup(&ts->spi->dev))
931 enable_irq_wake(ts->spi->irq);
932
933 ts->suspended = true;
934 }
935
936 mutex_unlock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800937
David Brownellffa458c2006-01-08 13:34:21 -0800938 return 0;
939}
940
Mark Brown3c36e712011-01-20 22:51:26 -0800941static int ads7846_resume(struct device *dev)
David Brownellffa458c2006-01-08 13:34:21 -0800942{
Mark Brown3c36e712011-01-20 22:51:26 -0800943 struct ads7846 *ts = dev_get_drvdata(dev);
David Brownellffa458c2006-01-08 13:34:21 -0800944
Jason Wang2991a1c2010-10-13 11:35:40 -0700945 mutex_lock(&ts->lock);
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -0800946
Jason Wang2991a1c2010-10-13 11:35:40 -0700947 if (ts->suspended) {
Imre Deak7de90a82006-04-11 23:43:55 -0400948
Jason Wang2991a1c2010-10-13 11:35:40 -0700949 ts->suspended = false;
Imre Deak7de90a82006-04-11 23:43:55 -0400950
Jason Wang2991a1c2010-10-13 11:35:40 -0700951 if (device_may_wakeup(&ts->spi->dev))
952 disable_irq_wake(ts->spi->irq);
953
954 if (!ts->disabled)
955 __ads7846_enable(ts);
956 }
957
958 mutex_unlock(&ts->lock);
Imre Deak7de90a82006-04-11 23:43:55 -0400959
David Brownellffa458c2006-01-08 13:34:21 -0800960 return 0;
961}
Mark Brown3c36e712011-01-20 22:51:26 -0800962#endif
963
964static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
David Brownellffa458c2006-01-08 13:34:21 -0800965
Bill Pemberton5298cc42012-11-23 21:38:25 -0800966static int ads7846_setup_pendown(struct spi_device *spi,
Dmitry Torokhov57691a12013-06-27 23:29:25 -0700967 struct ads7846 *ts,
968 const struct ads7846_platform_data *pdata)
Eric Miao4d5975e2008-09-10 12:06:15 -0400969{
Eric Miao4d5975e2008-09-10 12:06:15 -0400970 int err;
971
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800972 /*
973 * REVISIT when the irq can be triggered active-low, or if for some
Eric Miao4d5975e2008-09-10 12:06:15 -0400974 * reason the touchscreen isn't hooked up, we don't need to access
975 * the pendown state.
976 */
Eric Miao4d5975e2008-09-10 12:06:15 -0400977
978 if (pdata->get_pendown_state) {
979 ts->get_pendown_state = pdata->get_pendown_state;
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800980 } else if (gpio_is_valid(pdata->gpio_pendown)) {
Eric Miao4d5975e2008-09-10 12:06:15 -0400981
Igor Grinberg58c24402011-06-27 13:06:27 -0700982 err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
983 "ads7846_pendown");
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800984 if (err) {
Igor Grinberg58c24402011-06-27 13:06:27 -0700985 dev_err(&spi->dev,
986 "failed to request/setup pendown GPIO%d: %d\n",
987 pdata->gpio_pendown, err);
Igor Grinberg1201e7e2011-05-11 15:45:05 -0700988 return err;
989 }
Eric Miao4d5975e2008-09-10 12:06:15 -0400990
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800991 ts->gpio_pendown = pdata->gpio_pendown;
992
Igor Grinbergc4f49252012-11-20 23:00:10 -0800993 if (pdata->gpio_pendown_debounce)
994 gpio_set_debounce(pdata->gpio_pendown,
995 pdata->gpio_pendown_debounce);
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -0800996 } else {
997 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
998 return -EINVAL;
999 }
Jason Wang2991a1c2010-10-13 11:35:40 -07001000
Eric Miao4d5975e2008-09-10 12:06:15 -04001001 return 0;
1002}
1003
Jason Wang2991a1c2010-10-13 11:35:40 -07001004/*
1005 * Set up the transfers to read touchscreen state; this assumes we
1006 * use formula #2 for pressure, not #3.
1007 */
Bill Pemberton5298cc42012-11-23 21:38:25 -08001008static void ads7846_setup_spi_msg(struct ads7846 *ts,
Dmitry Torokhov57691a12013-06-27 23:29:25 -07001009 const struct ads7846_platform_data *pdata)
David Brownellffa458c2006-01-08 13:34:21 -08001010{
Jason Wang2991a1c2010-10-13 11:35:40 -07001011 struct spi_message *m = &ts->msg[0];
1012 struct spi_transfer *x = ts->xfer;
1013 struct ads7846_packet *packet = ts->packet;
1014 int vref = pdata->keep_vref_on;
Imre Deakde2defd2007-01-18 00:45:21 -05001015
Michael Hennerich06a09122010-03-09 20:38:45 -08001016 if (ts->model == 7873) {
Jason Wang2991a1c2010-10-13 11:35:40 -07001017 /*
1018 * The AD7873 is almost identical to the ADS7846
Michael Hennerich06a09122010-03-09 20:38:45 -08001019 * keep VREF off during differential/ratiometric
Jason Wang2991a1c2010-10-13 11:35:40 -07001020 * conversion modes.
Michael Hennerich06a09122010-03-09 20:38:45 -08001021 */
1022 ts->model = 7846;
1023 vref = 0;
1024 }
1025
Jason Wang2991a1c2010-10-13 11:35:40 -07001026 ts->msg_count = 1;
Imre Deak0b7018a2006-04-11 23:42:03 -04001027 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001028 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001029
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001030 if (ts->model == 7845) {
1031 packet->read_y_cmd[0] = READ_Y(vref);
1032 packet->read_y_cmd[1] = 0;
1033 packet->read_y_cmd[2] = 0;
1034 x->tx_buf = &packet->read_y_cmd[0];
1035 x->rx_buf = &packet->tc.y_buf[0];
1036 x->len = 3;
1037 spi_message_add_tail(x, m);
1038 } else {
1039 /* y- still on; turn on only y+ (and ADC) */
1040 packet->read_y = READ_Y(vref);
1041 x->tx_buf = &packet->read_y;
1042 x->len = 1;
1043 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001044
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001045 x++;
1046 x->rx_buf = &packet->tc.y;
1047 x->len = 2;
1048 spi_message_add_tail(x, m);
1049 }
David Brownellffa458c2006-01-08 13:34:21 -08001050
Jason Wang2991a1c2010-10-13 11:35:40 -07001051 /*
1052 * The first sample after switching drivers can be low quality;
Semih Hazare4f48862007-07-18 00:35:56 -04001053 * optionally discard it, using a second one after the signals
1054 * have had enough time to stabilize.
1055 */
1056 if (pdata->settle_delay_usecs) {
1057 x->delay_usecs = pdata->settle_delay_usecs;
1058
1059 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001060 x->tx_buf = &packet->read_y;
Semih Hazare4f48862007-07-18 00:35:56 -04001061 x->len = 1;
1062 spi_message_add_tail(x, m);
1063
1064 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001065 x->rx_buf = &packet->tc.y;
Semih Hazare4f48862007-07-18 00:35:56 -04001066 x->len = 2;
1067 spi_message_add_tail(x, m);
1068 }
1069
Jason Wang2991a1c2010-10-13 11:35:40 -07001070 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001071 m++;
1072 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001073 m->context = ts;
David Brownellffa458c2006-01-08 13:34:21 -08001074
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001075 if (ts->model == 7845) {
1076 x++;
1077 packet->read_x_cmd[0] = READ_X(vref);
1078 packet->read_x_cmd[1] = 0;
1079 packet->read_x_cmd[2] = 0;
1080 x->tx_buf = &packet->read_x_cmd[0];
1081 x->rx_buf = &packet->tc.x_buf[0];
1082 x->len = 3;
1083 spi_message_add_tail(x, m);
1084 } else {
1085 /* turn y- off, x+ on, then leave in lowpower */
1086 x++;
1087 packet->read_x = READ_X(vref);
1088 x->tx_buf = &packet->read_x;
1089 x->len = 1;
1090 spi_message_add_tail(x, m);
David Brownelld93f70b2006-02-15 00:49:35 -05001091
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001092 x++;
1093 x->rx_buf = &packet->tc.x;
1094 x->len = 2;
1095 spi_message_add_tail(x, m);
1096 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001097
Semih Hazare4f48862007-07-18 00:35:56 -04001098 /* ... maybe discard first sample ... */
1099 if (pdata->settle_delay_usecs) {
1100 x->delay_usecs = pdata->settle_delay_usecs;
1101
1102 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001103 x->tx_buf = &packet->read_x;
Semih Hazare4f48862007-07-18 00:35:56 -04001104 x->len = 1;
1105 spi_message_add_tail(x, m);
1106
1107 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001108 x->rx_buf = &packet->tc.x;
Semih Hazare4f48862007-07-18 00:35:56 -04001109 x->len = 2;
1110 spi_message_add_tail(x, m);
1111 }
1112
Imre Deak0b7018a2006-04-11 23:42:03 -04001113 /* turn y+ off, x- on; we'll use formula #2 */
1114 if (ts->model == 7846) {
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_z1 = READ_Z1(vref);
1122 x->tx_buf = &packet->read_z1;
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.z1;
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_z1;
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.z1;
Semih Hazare4f48862007-07-18 00:35:56 -04001142 x->len = 2;
1143 spi_message_add_tail(x, m);
1144 }
1145
Jason Wang2991a1c2010-10-13 11:35:40 -07001146 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001147 m++;
1148 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001149 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001150
1151 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001152 packet->read_z2 = READ_Z2(vref);
1153 x->tx_buf = &packet->read_z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001154 x->len = 1;
1155 spi_message_add_tail(x, m);
1156
1157 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001158 x->rx_buf = &packet->tc.z2;
Imre Deak0b7018a2006-04-11 23:42:03 -04001159 x->len = 2;
1160 spi_message_add_tail(x, m);
1161
Semih Hazare4f48862007-07-18 00:35:56 -04001162 /* ... maybe discard first sample ... */
1163 if (pdata->settle_delay_usecs) {
1164 x->delay_usecs = pdata->settle_delay_usecs;
1165
1166 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001167 x->tx_buf = &packet->read_z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001168 x->len = 1;
1169 spi_message_add_tail(x, m);
1170
1171 x++;
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001172 x->rx_buf = &packet->tc.z2;
Semih Hazare4f48862007-07-18 00:35:56 -04001173 x->len = 2;
1174 spi_message_add_tail(x, m);
1175 }
Imre Deak0b7018a2006-04-11 23:42:03 -04001176 }
Imre Deak53a0ef892006-04-11 23:41:49 -04001177
1178 /* power down */
Jason Wang2991a1c2010-10-13 11:35:40 -07001179 ts->msg_count++;
Imre Deak0b7018a2006-04-11 23:42:03 -04001180 m++;
1181 spi_message_init(m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001182 m->context = ts;
Imre Deak0b7018a2006-04-11 23:42:03 -04001183
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001184 if (ts->model == 7845) {
1185 x++;
1186 packet->pwrdown_cmd[0] = PWRDOWN;
1187 packet->pwrdown_cmd[1] = 0;
1188 packet->pwrdown_cmd[2] = 0;
1189 x->tx_buf = &packet->pwrdown_cmd[0];
1190 x->len = 3;
1191 } else {
1192 x++;
1193 packet->pwrdown = PWRDOWN;
1194 x->tx_buf = &packet->pwrdown;
1195 x->len = 1;
1196 spi_message_add_tail(x, m);
Imre Deak53a0ef892006-04-11 23:41:49 -04001197
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001198 x++;
1199 x->rx_buf = &packet->dummy;
1200 x->len = 2;
1201 }
1202
David Brownelld93f70b2006-02-15 00:49:35 -05001203 CS_CHANGE(*x);
Imre Deak0b7018a2006-04-11 23:42:03 -04001204 spi_message_add_tail(x, m);
Jason Wang2991a1c2010-10-13 11:35:40 -07001205}
David Brownellffa458c2006-01-08 13:34:21 -08001206
Daniel Macka6080262013-06-27 23:42:17 -07001207#ifdef CONFIG_OF
1208static const struct of_device_id ads7846_dt_ids[] = {
1209 { .compatible = "ti,tsc2046", .data = (void *) 7846 },
1210 { .compatible = "ti,ads7843", .data = (void *) 7843 },
1211 { .compatible = "ti,ads7845", .data = (void *) 7845 },
1212 { .compatible = "ti,ads7846", .data = (void *) 7846 },
1213 { .compatible = "ti,ads7873", .data = (void *) 7873 },
1214 { }
1215};
1216MODULE_DEVICE_TABLE(of, ads7846_dt_ids);
1217
1218static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1219{
1220 struct ads7846_platform_data *pdata;
1221 struct device_node *node = dev->of_node;
1222 const struct of_device_id *match;
1223
1224 if (!node) {
1225 dev_err(dev, "Device does not have associated DT data\n");
1226 return ERR_PTR(-EINVAL);
1227 }
1228
1229 match = of_match_device(ads7846_dt_ids, dev);
1230 if (!match) {
1231 dev_err(dev, "Unknown device model\n");
1232 return ERR_PTR(-EINVAL);
1233 }
1234
1235 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1236 if (!pdata)
1237 return ERR_PTR(-ENOMEM);
1238
1239 pdata->model = (unsigned long)match->data;
1240
1241 of_property_read_u16(node, "ti,vref-delay-usecs",
1242 &pdata->vref_delay_usecs);
1243 of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv);
1244 pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on");
1245
1246 pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy");
1247
1248 of_property_read_u16(node, "ti,settle-delay-usec",
1249 &pdata->settle_delay_usecs);
1250 of_property_read_u16(node, "ti,penirq-recheck-delay-usecs",
1251 &pdata->penirq_recheck_delay_usecs);
1252
1253 of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms);
1254 of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms);
1255
1256 of_property_read_u16(node, "ti,x-min", &pdata->x_min);
1257 of_property_read_u16(node, "ti,y-min", &pdata->y_min);
1258 of_property_read_u16(node, "ti,x-max", &pdata->x_max);
1259 of_property_read_u16(node, "ti,y-max", &pdata->y_max);
1260
1261 of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min);
1262 of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max);
1263
1264 of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max);
1265 of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol);
1266 of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep);
1267
1268 of_property_read_u32(node, "ti,pendown-gpio-debounce",
1269 &pdata->gpio_pendown_debounce);
1270
1271 pdata->wakeup = of_property_read_bool(node, "linux,wakeup");
1272
1273 pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0);
1274
1275 return pdata;
1276}
1277#else
1278static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev)
1279{
1280 dev_err(dev, "no platform data defined\n");
1281 return ERR_PTR(-EINVAL);
1282}
1283#endif
1284
Bill Pemberton5298cc42012-11-23 21:38:25 -08001285static int ads7846_probe(struct spi_device *spi)
Jason Wang2991a1c2010-10-13 11:35:40 -07001286{
Daniel Macka6080262013-06-27 23:42:17 -07001287 const struct ads7846_platform_data *pdata;
Jason Wang2991a1c2010-10-13 11:35:40 -07001288 struct ads7846 *ts;
1289 struct ads7846_packet *packet;
1290 struct input_dev *input_dev;
Jason Wang2991a1c2010-10-13 11:35:40 -07001291 unsigned long irq_flags;
1292 int err;
David Brownellffa458c2006-01-08 13:34:21 -08001293
Jason Wang2991a1c2010-10-13 11:35:40 -07001294 if (!spi->irq) {
1295 dev_dbg(&spi->dev, "no IRQ?\n");
Daniel Macka6080262013-06-27 23:42:17 -07001296 return -EINVAL;
Jason Wang2991a1c2010-10-13 11:35:40 -07001297 }
1298
1299 /* don't exceed max specified sample rate */
1300 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
Daniel Macka6080262013-06-27 23:42:17 -07001301 dev_err(&spi->dev, "f(sample) %d KHz?\n",
Jason Wang2991a1c2010-10-13 11:35:40 -07001302 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1303 return -EINVAL;
1304 }
1305
Daniel Macka6080262013-06-27 23:42:17 -07001306 /*
1307 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
Jason Wang2991a1c2010-10-13 11:35:40 -07001308 * that even if the hardware can do that, the SPI controller driver
1309 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1310 */
1311 spi->bits_per_word = 8;
1312 spi->mode = SPI_MODE_0;
1313 err = spi_setup(spi);
1314 if (err < 0)
1315 return err;
1316
1317 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1318 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1319 input_dev = input_allocate_device();
1320 if (!ts || !packet || !input_dev) {
1321 err = -ENOMEM;
1322 goto err_free_mem;
1323 }
1324
Jingoo Hanc12454f2013-04-07 20:52:07 -07001325 spi_set_drvdata(spi, ts);
Jason Wang2991a1c2010-10-13 11:35:40 -07001326
1327 ts->packet = packet;
1328 ts->spi = spi;
1329 ts->input = input_dev;
Jason Wang2991a1c2010-10-13 11:35:40 -07001330
1331 mutex_init(&ts->lock);
1332 init_waitqueue_head(&ts->wait);
1333
Daniel Macka6080262013-06-27 23:42:17 -07001334 pdata = dev_get_platdata(&spi->dev);
1335 if (!pdata) {
1336 pdata = ads7846_probe_dt(&spi->dev);
1337 if (IS_ERR(pdata))
1338 return PTR_ERR(pdata);
1339 }
1340
Jason Wang2991a1c2010-10-13 11:35:40 -07001341 ts->model = pdata->model ? : 7846;
1342 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1343 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1344 ts->pressure_max = pdata->pressure_max ? : ~0;
1345
Daniel Macka6080262013-06-27 23:42:17 -07001346 ts->vref_mv = pdata->vref_mv;
1347 ts->swap_xy = pdata->swap_xy;
1348
Jason Wang2991a1c2010-10-13 11:35:40 -07001349 if (pdata->filter != NULL) {
1350 if (pdata->filter_init != NULL) {
1351 err = pdata->filter_init(pdata, &ts->filter_data);
1352 if (err < 0)
1353 goto err_free_mem;
1354 }
1355 ts->filter = pdata->filter;
1356 ts->filter_cleanup = pdata->filter_cleanup;
1357 } else if (pdata->debounce_max) {
1358 ts->debounce_max = pdata->debounce_max;
1359 if (ts->debounce_max < 2)
1360 ts->debounce_max = 2;
1361 ts->debounce_tol = pdata->debounce_tol;
1362 ts->debounce_rep = pdata->debounce_rep;
1363 ts->filter = ads7846_debounce_filter;
1364 ts->filter_data = ts;
1365 } else {
1366 ts->filter = ads7846_no_filter;
1367 }
1368
Dmitry Torokhov57691a12013-06-27 23:29:25 -07001369 err = ads7846_setup_pendown(spi, ts, pdata);
Jason Wang2991a1c2010-10-13 11:35:40 -07001370 if (err)
1371 goto err_cleanup_filter;
1372
1373 if (pdata->penirq_recheck_delay_usecs)
1374 ts->penirq_recheck_delay_usecs =
1375 pdata->penirq_recheck_delay_usecs;
1376
1377 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1378
1379 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1380 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1381
1382 input_dev->name = ts->name;
1383 input_dev->phys = ts->phys;
1384 input_dev->dev.parent = &spi->dev;
1385
1386 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1387 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1388 input_set_abs_params(input_dev, ABS_X,
1389 pdata->x_min ? : 0,
1390 pdata->x_max ? : MAX_12BIT,
1391 0, 0);
1392 input_set_abs_params(input_dev, ABS_Y,
1393 pdata->y_min ? : 0,
1394 pdata->y_max ? : MAX_12BIT,
1395 0, 0);
1396 input_set_abs_params(input_dev, ABS_PRESSURE,
1397 pdata->pressure_min, pdata->pressure_max, 0, 0);
1398
1399 ads7846_setup_spi_msg(ts, pdata);
Imre Deakd5b415c2006-04-26 00:13:18 -04001400
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001401 ts->reg = regulator_get(&spi->dev, "vcc");
1402 if (IS_ERR(ts->reg)) {
Kevin Hilman067fb2f2010-05-26 23:30:55 -07001403 err = PTR_ERR(ts->reg);
Dmitry Torokhov56960b32010-06-02 00:40:06 -07001404 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001405 goto err_free_gpio;
1406 }
1407
1408 err = regulator_enable(ts->reg);
1409 if (err) {
1410 dev_err(&spi->dev, "unable to enable regulator: %d\n", err);
1411 goto err_put_regulator;
1412 }
1413
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001414 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
Jason Wang2991a1c2010-10-13 11:35:40 -07001415 irq_flags |= IRQF_ONESHOT;
Anatolij Gustschin78043022010-06-28 01:25:19 -07001416
Jason Wang2991a1c2010-10-13 11:35:40 -07001417 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1418 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001419 if (err && !pdata->irq_flags) {
Michael Rothc57c0a22009-06-10 23:30:55 -07001420 dev_info(&spi->dev,
1421 "trying pin change workaround on irq %d\n", spi->irq);
Jason Wang2991a1c2010-10-13 11:35:40 -07001422 irq_flags |= IRQF_TRIGGER_RISING;
1423 err = request_threaded_irq(spi->irq,
1424 ads7846_hard_irq, ads7846_irq,
1425 irq_flags, spi->dev.driver->name, ts);
Dmitry Torokhov0f622bf2010-07-01 09:01:50 -07001426 }
1427
1428 if (err) {
1429 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1430 goto err_disable_regulator;
David Brownellffa458c2006-01-08 13:34:21 -08001431 }
David Brownellffa458c2006-01-08 13:34:21 -08001432
David Brownell2c8dc072007-01-18 00:45:48 -05001433 err = ads784x_hwmon_register(spi, ts);
1434 if (err)
1435 goto err_free_irq;
1436
David Brownell2e5a7bd2006-01-08 13:34:25 -08001437 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
David Brownellffa458c2006-01-08 13:34:21 -08001438
Jason Wang2991a1c2010-10-13 11:35:40 -07001439 /*
1440 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
David Brownellffa458c2006-01-08 13:34:21 -08001441 * the touchscreen, in case it's not connected.
1442 */
Anatolij Gustschin3eac5c72010-07-01 09:01:56 -07001443 if (ts->model == 7845)
1444 ads7845_read12_ser(&spi->dev, PWRDOWN);
1445 else
Alexander Steinebcaaad2011-05-11 16:24:08 -07001446 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
David Brownellffa458c2006-01-08 13:34:21 -08001447
David Brownell2c8dc072007-01-18 00:45:48 -05001448 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001449 if (err)
David Brownell2c8dc072007-01-18 00:45:48 -05001450 goto err_remove_hwmon;
Imre Deak7de90a82006-04-11 23:43:55 -04001451
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001452 err = input_register_device(input_dev);
1453 if (err)
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001454 goto err_remove_attr_group;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001455
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001456 device_init_wakeup(&spi->dev, pdata->wakeup);
1457
Daniel Macka6080262013-06-27 23:42:17 -07001458 /*
1459 * If device does not carry platform data we must have allocated it
1460 * when parsing DT data.
1461 */
1462 if (!dev_get_platdata(&spi->dev))
1463 devm_kfree(&spi->dev, (void *)pdata);
1464
David Brownellffa458c2006-01-08 13:34:21 -08001465 return 0;
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001466
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001467 err_remove_attr_group:
David Brownell2c8dc072007-01-18 00:45:48 -05001468 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1469 err_remove_hwmon:
1470 ads784x_hwmon_unregister(spi, ts);
Dmitry Torokhov8dd51652006-11-02 23:34:09 -05001471 err_free_irq:
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001472 free_irq(spi->irq, ts);
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001473 err_disable_regulator:
1474 regulator_disable(ts->reg);
1475 err_put_regulator:
1476 regulator_put(ts->reg);
Eric Miao4d5975e2008-09-10 12:06:15 -04001477 err_free_gpio:
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001478 if (!ts->get_pendown_state)
Eric Miao4d5975e2008-09-10 12:06:15 -04001479 gpio_free(ts->gpio_pendown);
Imre Deakda970e62007-01-18 00:44:41 -05001480 err_cleanup_filter:
1481 if (ts->filter_cleanup)
1482 ts->filter_cleanup(ts->filter_data);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001483 err_free_mem:
1484 input_free_device(input_dev);
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001485 kfree(packet);
Dmitry Torokhova90f7e92006-02-15 00:49:22 -05001486 kfree(ts);
1487 return err;
David Brownellffa458c2006-01-08 13:34:21 -08001488}
1489
Bill Pembertone2619cf2012-11-23 21:50:47 -08001490static int ads7846_remove(struct spi_device *spi)
David Brownellffa458c2006-01-08 13:34:21 -08001491{
Jingoo Hanc12454f2013-04-07 20:52:07 -07001492 struct ads7846 *ts = spi_get_drvdata(spi);
David Brownellffa458c2006-01-08 13:34:21 -08001493
Ranjith Lohithakshanfdba2bb2010-03-10 23:41:22 -08001494 device_init_wakeup(&spi->dev, false);
1495
David Brownell2c8dc072007-01-18 00:45:48 -05001496 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
David Brownellffa458c2006-01-08 13:34:21 -08001497
Jason Wang2991a1c2010-10-13 11:35:40 -07001498 ads7846_disable(ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001499 free_irq(ts->spi->irq, ts);
Jason Wang2991a1c2010-10-13 11:35:40 -07001500
1501 input_unregister_device(ts->input);
1502
1503 ads784x_hwmon_unregister(spi, ts);
Imre Deak7de90a82006-04-11 23:43:55 -04001504
Grazvydas Ignotas91143372010-02-25 02:04:56 -08001505 regulator_disable(ts->reg);
1506 regulator_put(ts->reg);
1507
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001508 if (!ts->get_pendown_state) {
1509 /*
1510 * If we are not using specialized pendown method we must
1511 * have been relying on gpio we set up ourselves.
1512 */
Eric Miao4d5975e2008-09-10 12:06:15 -04001513 gpio_free(ts->gpio_pendown);
Dmitry Torokhov0fbc9fd2011-02-04 00:37:26 -08001514 }
Eric Miao4d5975e2008-09-10 12:06:15 -04001515
Imre Deakda970e62007-01-18 00:44:41 -05001516 if (ts->filter_cleanup)
1517 ts->filter_cleanup(ts->filter_data);
1518
Dmitry Torokhove8f462d2008-10-09 00:52:23 -04001519 kfree(ts->packet);
David Brownellffa458c2006-01-08 13:34:21 -08001520 kfree(ts);
1521
David Brownell2e5a7bd2006-01-08 13:34:25 -08001522 dev_dbg(&spi->dev, "unregistered touchscreen\n");
Jason Wang2991a1c2010-10-13 11:35:40 -07001523
David Brownellffa458c2006-01-08 13:34:21 -08001524 return 0;
1525}
1526
David Brownell2e5a7bd2006-01-08 13:34:25 -08001527static struct spi_driver ads7846_driver = {
1528 .driver = {
1529 .name = "ads7846",
David Brownell2e5a7bd2006-01-08 13:34:25 -08001530 .owner = THIS_MODULE,
Mark Brown3c36e712011-01-20 22:51:26 -08001531 .pm = &ads7846_pm,
Daniel Macka6080262013-06-27 23:42:17 -07001532 .of_match_table = of_match_ptr(ads7846_dt_ids),
David Brownell2e5a7bd2006-01-08 13:34:25 -08001533 },
David Brownellffa458c2006-01-08 13:34:21 -08001534 .probe = ads7846_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001535 .remove = ads7846_remove,
David Brownellffa458c2006-01-08 13:34:21 -08001536};
1537
Axel Linca839222012-03-16 23:05:26 -07001538module_spi_driver(ads7846_driver);
David Brownellffa458c2006-01-08 13:34:21 -08001539
1540MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1541MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -07001542MODULE_ALIAS("spi:ads7846");