Merge branch 'x86-trace-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / drivers / input / touchscreen / ads7846.c
CommitLineData
ffa458c1
DB
1/*
2 * ADS7846 based touchscreen and sensor driver
3 *
4 * Copyright (c) 2005 David Brownell
7de90a8c
ID
5 * Copyright (c) 2006 Nokia Corporation
6 * Various changes: Imre Deak <imre.deak@nokia.com>
ffa458c1
DB
7 *
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 */
2991a1ca 20#include <linux/types.h>
2c8dc071 21#include <linux/hwmon.h>
ffa458c1 22#include <linux/init.h>
2c8dc071 23#include <linux/err.h>
2991a1ca 24#include <linux/sched.h>
ffa458c1
DB
25#include <linux/delay.h>
26#include <linux/input.h>
27#include <linux/interrupt.h>
28#include <linux/slab.h>
3c36e719 29#include <linux/pm.h>
a608026e
DM
30#include <linux/of.h>
31#include <linux/of_gpio.h>
32#include <linux/of_device.h>
4d5975e5 33#include <linux/gpio.h>
ffa458c1
DB
34#include <linux/spi/spi.h>
35#include <linux/spi/ads7846.h>
91143379 36#include <linux/regulator/consumer.h>
d2d8442d 37#include <linux/module.h>
3ac8bf07 38#include <asm/irq.h>
ffa458c1 39
ffa458c1 40/*
9084533e 41 * This code has been heavily tested on a Nokia 770, and lightly
52ce4eaa 42 * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).
bff0de5f 43 * TSC2046 is just newer ads7846 silicon.
969111e9
NF
44 * Support for ads7843 tested on Atmel at91sam926x-EK.
45 * Support for ads7845 has only been stubbed in.
06a09124 46 * Support for Analog Devices AD7873 and AD7843 tested.
ffa458c1 47 *
7de90a8c
ID
48 * 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 *
52ce4eaa 54 * App note sbaa036 talks in more detail about accurate sampling...
ffa458c1
DB
55 * that ought to help in situations like LCDs inducing noise (which
56 * can also be helped by using synch signals) and more generally.
7de90a8c
ID
57 * 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.
ffa458c1
DB
60 */
61
2991a1ca
JW
62#define TS_POLL_DELAY 1 /* ms delay before the first sample */
63#define TS_POLL_PERIOD 5 /* ms delay between samples */
ffa458c1 64
d93f70b2
DB
65/* this driver doesn't aim at the peak continuous sample rate */
66#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
67
ffa458c1 68struct ts_event {
2991a1ca
JW
69 /*
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
d93f70b2 72 * with msbs zeroed). Instead, we read them as two 8-bit values,
da970e69 73 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
ffa458c1 74 */
da970e69
ID
75 u16 x;
76 u16 y;
77 u16 z1, z2;
2991a1ca 78 bool ignore;
3eac5c7e
AG
79 u8 x_buf[3];
80 u8 y_buf[3];
ffa458c1
DB
81};
82
e8f462d2
DT
83/*
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;
3eac5c7e
AG
92 /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */
93 u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];
e8f462d2
DT
94};
95
ffa458c1 96struct ads7846 {
a90f7e98 97 struct input_dev *input;
ffa458c1 98 char phys[32];
b58895f8 99 char name[32];
ffa458c1
DB
100
101 struct spi_device *spi;
91143379 102 struct regulator *reg;
2c8dc071
DB
103
104#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
8dd51650 105 struct attribute_group *attr_group;
1beeffe4 106 struct device *hwmon;
2c8dc071
DB
107#endif
108
ffa458c1 109 u16 model;
7c6d0ee1 110 u16 vref_mv;
ffa458c1
DB
111 u16 vref_delay_usecs;
112 u16 x_plate_ohms;
d5b415c9 113 u16 pressure_max;
ffa458c1 114
86579a4c 115 bool swap_xy;
ebcaaad9 116 bool use_internal;
86579a4c 117
e8f462d2 118 struct ads7846_packet *packet;
ffa458c1 119
e4f48861 120 struct spi_transfer xfer[18];
0b7018aa 121 struct spi_message msg[5];
2991a1ca
JW
122 int msg_count;
123 wait_queue_head_t wait;
124
125 bool pendown;
126
0b7018aa 127 int read_cnt;
d5b415c9 128 int read_rep;
0b7018aa
ID
129 int last_read;
130
131 u16 debounce_max;
132 u16 debounce_tol;
d5b415c9 133 u16 debounce_rep;
ffa458c1 134
1d25891f
SH
135 u16 penirq_recheck_delay_usecs;
136
2991a1ca
JW
137 struct mutex lock;
138 bool stopped; /* P: lock */
139 bool disabled; /* P: lock */
140 bool suspended; /* P: lock */
c9e617a5 141
da970e69
ID
142 int (*filter)(void *data, int data_idx, int *val);
143 void *filter_data;
144 void (*filter_cleanup)(void *data);
c9e617a5 145 int (*get_pendown_state)(void);
4d5975e5 146 int gpio_pendown;
fd746d54
EM
147
148 void (*wait_for_sync)(void);
ffa458c1
DB
149};
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 */
2991a1ca 176#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
ffa458c1
DB
177#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 */
de2defd9
ID
184#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))
ffa458c1 187
de2defd9
ID
188#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))
53a0ef89 191
de2defd9
ID
192#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
193#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
ffa458c1
DB
194
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
de2defd9
ID
201#define REF_ON (READ_12BIT_DFR(x, 1, 1))
202#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
ffa458c1 203
2991a1ca
JW
204/* 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{
f94352f8
MB
242 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
2991a1ca
JW
248 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
ffa458c1
DB
281/*--------------------------------------------------------------------------*/
282
283/*
284 * Non-touchscreen sensors only use single-ended conversions.
2c8dc071
DB
285 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
286 * ads7846 lets that pin be unconnected, to use internal vREF.
ffa458c1
DB
287 */
288
289struct ser_req {
d93f70b2 290 u8 ref_on;
ffa458c1 291 u8 command;
d93f70b2 292 u8 ref_off;
ffa458c1 293 u16 scratch;
ffa458c1
DB
294 struct spi_message msg;
295 struct spi_transfer xfer[6];
1dbe7dad
AS
296 /*
297 * DMA (thus cache coherency maintenance) requires the
298 * transfer buffers to live in their own cache lines.
299 */
300 __be16 sample ____cacheline_aligned;
ffa458c1
DB
301};
302
3eac5c7e
AG
303struct ads7845_ser_req {
304 u8 command[3];
3eac5c7e
AG
305 struct spi_message msg;
306 struct spi_transfer xfer[2];
1dbe7dad
AS
307 /*
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;
3eac5c7e
AG
312};
313
ffa458c1
DB
314static int ads7846_read12_ser(struct device *dev, unsigned command)
315{
2991a1ca
JW
316 struct spi_device *spi = to_spi_device(dev);
317 struct ads7846 *ts = dev_get_drvdata(dev);
318 struct ser_req *req;
319 int status;
ffa458c1 320
2991a1ca 321 req = kzalloc(sizeof *req, GFP_KERNEL);
ffa458c1
DB
322 if (!req)
323 return -ENOMEM;
324
0b7018aa 325 spi_message_init(&req->msg);
8275c642 326
2c8dc071 327 /* maybe turn on internal vREF, and let it settle */
ebcaaad9 328 if (ts->use_internal) {
2c8dc071
DB
329 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);
ebcaaad9
AS
340
341 /* Enable reference voltage */
342 command |= ADS_PD10_REF_ON;
2c8dc071 343 }
ffa458c1 344
ebcaaad9
AS
345 /* Enable ADC in every case */
346 command |= ADS_PD10_ADC_ON;
347
ffa458c1
DB
348 /* take sample */
349 req->command = (u8) command;
350 req->xfer[2].tx_buf = &req->command;
351 req->xfer[2].len = 1;
2c8dc071
DB
352 spi_message_add_tail(&req->xfer[2], &req->msg);
353
ffa458c1
DB
354 req->xfer[3].rx_buf = &req->sample;
355 req->xfer[3].len = 2;
2c8dc071 356 spi_message_add_tail(&req->xfer[3], &req->msg);
ffa458c1
DB
357
358 /* REVISIT: take a few more samples, and compare ... */
359
969111e9
NF
360 /* 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);
365
366 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);
ffa458c1 370
2991a1ca
JW
371 mutex_lock(&ts->lock);
372 ads7846_stop(ts);
ffa458c1 373 status = spi_sync(spi, &req->msg);
2991a1ca
JW
374 ads7846_restart(ts);
375 mutex_unlock(&ts->lock);
ffa458c1 376
c24b2602
MP
377 if (status == 0) {
378 /* on-wire is a must-ignore bit, a BE12 value, then padding */
7c6d0ee1
DB
379 status = be16_to_cpu(req->sample);
380 status = status >> 3;
381 status &= 0x0fff;
c24b2602 382 }
ffa458c1 383
9084533e 384 kfree(req);
7c6d0ee1 385 return status;
ffa458c1
DB
386}
387
3eac5c7e
AG
388static int ads7845_read12_ser(struct device *dev, unsigned command)
389{
2991a1ca
JW
390 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;
3eac5c7e 394
2991a1ca 395 req = kzalloc(sizeof *req, GFP_KERNEL);
3eac5c7e
AG
396 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
2991a1ca
JW
407 mutex_lock(&ts->lock);
408 ads7846_stop(ts);
3eac5c7e 409 status = spi_sync(spi, &req->msg);
2991a1ca
JW
410 ads7846_restart(ts);
411 mutex_unlock(&ts->lock);
3eac5c7e
AG
412
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
2c8dc071
DB
424#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
425
426#define SHOW(name, var, adjust) static ssize_t \
ffa458c1
DB
427name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
428{ \
2c8dc071 429 struct ads7846 *ts = dev_get_drvdata(dev); \
ffa458c1 430 ssize_t v = ads7846_read12_ser(dev, \
ebcaaad9 431 READ_12BIT_SER(var)); \
ffa458c1
DB
432 if (v < 0) \
433 return v; \
2c8dc071 434 return sprintf(buf, "%u\n", adjust(ts, v)); \
ffa458c1
DB
435} \
436static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
437
2c8dc071 438
b731d7b6 439/* Sysfs conventions report temperatures in millidegrees Celsius.
2c8dc071
DB
440 * 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 */
7c6d0ee1 462 retval *= ts->vref_mv;
2c8dc071 463 retval = retval >> 12;
2991a1ca 464
2c8dc071
DB
465 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;
2991a1ca 475
2c8dc071
DB
476 return retval;
477}
478
479SHOW(in0_input, vaux, vaux_adjust)
480SHOW(in1_input, vbatt, vbatt_adjust)
481
2c8dc071
DB
482static 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{
1beeffe4 515 struct device *hwmon;
2c8dc071
DB
516 int err;
517
518 /* hwmon sensors need a reference voltage */
519 switch (ts->model) {
520 case 7846:
7c6d0ee1 521 if (!ts->vref_mv) {
2c8dc071 522 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
7c6d0ee1 523 ts->vref_mv = 2500;
ebcaaad9 524 ts->use_internal = true;
2c8dc071
DB
525 }
526 break;
527 case 7845:
528 case 7843:
7c6d0ee1 529 if (!ts->vref_mv) {
2c8dc071
DB
530 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
ffa458c1 589
438f2a74
ID
590static ssize_t ads7846_pen_down_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
2991a1ca
JW
593 struct ads7846 *ts = dev_get_drvdata(dev);
594
595 return sprintf(buf, "%u\n", ts->pendown);
438f2a74
ID
596}
597
598static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
599
7de90a8c
ID
600static ssize_t ads7846_disable_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
602{
2991a1ca 603 struct ads7846 *ts = dev_get_drvdata(dev);
7de90a8c
ID
604
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);
76496e7a
JD
613 unsigned int i;
614 int err;
160f1fef 615
76496e7a
JD
616 err = kstrtouint(buf, 10, &i);
617 if (err)
618 return err;
7de90a8c 619
7de90a8c
ID
620 if (i)
621 ads7846_disable(ts);
622 else
623 ads7846_enable(ts);
624
7de90a8c
ID
625 return count;
626}
627
628static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
629
2c8dc071 630static struct attribute *ads784x_attributes[] = {
8dd51650
DT
631 &dev_attr_pen_down.attr,
632 &dev_attr_disable.attr,
633 NULL,
634};
635
2c8dc071
DB
636static struct attribute_group ads784x_attr_group = {
637 .attrs = ads784x_attributes,
8dd51650
DT
638};
639
ffa458c1
DB
640/*--------------------------------------------------------------------------*/
641
4d5975e5
EM
642static 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
fd746d54
EM
650static void null_wait_for_sync(void)
651{
652}
653
2991a1ca
JW
654static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
655{
656 struct ads7846 *ts = ads;
657
658 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) {
ffa458c1 751
2991a1ca
JW
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)
ffa458c1 780{
2991a1ca
JW
781 struct ads7846_packet *packet = ts->packet;
782 unsigned int Rt;
783 u16 x, y, z1, z2;
ffa458c1 784
2991a1ca
JW
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.
ffa458c1 789 */
3eac5c7e
AG
790 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 }
ffa458c1
DB
801
802 /* range filtering */
803 if (x == MAX_12BIT)
804 x = 0;
805
9460b652
HCE
806 if (ts->model == 7843) {
807 Rt = ts->pressure_max / 2;
3eac5c7e
AG
808 } 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);
9460b652 814 } else if (likely(x && z1)) {
ffa458c1
DB
815 /* 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;
9460b652 822 } else {
ffa458c1 823 Rt = 0;
9460b652 824 }
969111e9 825
2991a1ca
JW
826 /*
827 * Sample found inconsistent by debouncing or pressure is beyond
1936d590
ID
828 * the maximum. Don't report it to user space, repeat at least
829 * once more the measurement
830 */
e8f462d2 831 if (packet->tc.ignore || Rt > ts->pressure_max) {
52ce4eaa
PM
832 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
833 packet->tc.ignore, Rt);
d5b415c9
ID
834 return;
835 }
836
2991a1ca
JW
837 /*
838 * Maybe check the pendown state before reporting. This discards
1d25891f
SH
839 * false readings when the pen is lifted.
840 */
841 if (ts->penirq_recheck_delay_usecs) {
842 udelay(ts->penirq_recheck_delay_usecs);
4d5975e5 843 if (!get_pendown_state(ts))
1d25891f
SH
844 Rt = 0;
845 }
846
2991a1ca
JW
847 /*
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
15e3589e
ID
850 * value can fluctuate for quite a while after lifting the pen and
851 * in some cases may not even settle at the expected value.
ffa458c1 852 *
15e3589e
ID
853 * 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).
ffa458c1 855 */
ffa458c1 856 if (Rt) {
15e3589e 857 struct input_dev *input = ts->input;
ae82d5ab 858
2991a1ca
JW
859 if (ts->swap_xy)
860 swap(x, y);
861
15e3589e
ID
862 if (!ts->pendown) {
863 input_report_key(input, BTN_TOUCH, 1);
2991a1ca 864 ts->pendown = true;
52ce4eaa 865 dev_vdbg(&ts->spi->dev, "DOWN\n");
15e3589e 866 }
86579a4c 867
15e3589e
ID
868 input_report_abs(input, ABS_X, x);
869 input_report_abs(input, ABS_Y, y);
30ad7ba0 870 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
ffa458c1 871
15e3589e 872 input_sync(input);
52ce4eaa 873 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
15e3589e 874 }
da970e69
ID
875}
876
2991a1ca 877static irqreturn_t ads7846_hard_irq(int irq, void *handle)
da970e69 878{
2991a1ca 879 struct ads7846 *ts = handle;
da970e69 880
2991a1ca 881 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
0b7018aa
ID
882}
883
0b7018aa 884
7d12e780 885static irqreturn_t ads7846_irq(int irq, void *handle)
0b7018aa
ID
886{
887 struct ads7846 *ts = handle;
7de90a8c 888
2991a1ca
JW
889 /* Start with a small delay before checking pendown state */
890 msleep(TS_POLL_DELAY);
ffa458c1 891
2991a1ca 892 while (!ts->stopped && get_pendown_state(ts)) {
ffa458c1 893
2991a1ca
JW
894 /* pen is down, continue with the measurement */
895 ads7846_read_state(ts);
ffa458c1 896
2991a1ca
JW
897 if (!ts->stopped)
898 ads7846_report_state(ts);
c9e617a5 899
2991a1ca
JW
900 wait_event_timeout(ts->wait, ts->stopped,
901 msecs_to_jiffies(TS_POLL_PERIOD));
ffa458c1
DB
902 }
903
2991a1ca
JW
904 if (ts->pendown) {
905 struct input_dev *input = ts->input;
7de90a8c 906
2991a1ca
JW
907 input_report_key(input, BTN_TOUCH, 0);
908 input_report_abs(input, ABS_PRESSURE, 0);
909 input_sync(input);
7de90a8c 910
2991a1ca
JW
911 ts->pendown = false;
912 dev_vdbg(&ts->spi->dev, "UP\n");
913 }
91143379 914
2991a1ca 915 return IRQ_HANDLED;
7de90a8c
ID
916}
917
3c36e719
MB
918#ifdef CONFIG_PM_SLEEP
919static int ads7846_suspend(struct device *dev)
7de90a8c 920{
3c36e719 921 struct ads7846 *ts = dev_get_drvdata(dev);
7de90a8c 922
2991a1ca 923 mutex_lock(&ts->lock);
7de90a8c 924
2991a1ca 925 if (!ts->suspended) {
7de90a8c 926
2991a1ca
JW
927 if (!ts->disabled)
928 __ads7846_disable(ts);
7de90a8c 929
2991a1ca
JW
930 if (device_may_wakeup(&ts->spi->dev))
931 enable_irq_wake(ts->spi->irq);
fdba2bb1 932
2991a1ca
JW
933 ts->suspended = true;
934 }
935
936 mutex_unlock(&ts->lock);
7de90a8c 937
2991a1ca 938 return 0;
ffa458c1
DB
939}
940
3c36e719 941static int ads7846_resume(struct device *dev)
ffa458c1 942{
3c36e719 943 struct ads7846 *ts = dev_get_drvdata(dev);
ffa458c1 944
2991a1ca
JW
945 mutex_lock(&ts->lock);
946
947 if (ts->suspended) {
fdba2bb1 948
2991a1ca 949 ts->suspended = false;
7de90a8c 950
2991a1ca
JW
951 if (device_may_wakeup(&ts->spi->dev))
952 disable_irq_wake(ts->spi->irq);
7de90a8c 953
2991a1ca
JW
954 if (!ts->disabled)
955 __ads7846_enable(ts);
956 }
957
958 mutex_unlock(&ts->lock);
7de90a8c 959
ffa458c1
DB
960 return 0;
961}
3c36e719
MB
962#endif
963
964static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
ffa458c1 965
5298cc4c 966static int ads7846_setup_pendown(struct spi_device *spi,
57691a1e
DT
967 struct ads7846 *ts,
968 const struct ads7846_platform_data *pdata)
4d5975e5 969{
4d5975e5
EM
970 int err;
971
0fbc9fdb
DT
972 /*
973 * REVISIT when the irq can be triggered active-low, or if for some
4d5975e5
EM
974 * reason the touchscreen isn't hooked up, we don't need to access
975 * the pendown state.
976 */
4d5975e5
EM
977
978 if (pdata->get_pendown_state) {
979 ts->get_pendown_state = pdata->get_pendown_state;
0fbc9fdb 980 } else if (gpio_is_valid(pdata->gpio_pendown)) {
4d5975e5 981
58c24400
IG
982 err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN,
983 "ads7846_pendown");
0fbc9fdb 984 if (err) {
58c24400
IG
985 dev_err(&spi->dev,
986 "failed to request/setup pendown GPIO%d: %d\n",
987 pdata->gpio_pendown, err);
1201e7e6
IG
988 return err;
989 }
4d5975e5 990
0fbc9fdb
DT
991 ts->gpio_pendown = pdata->gpio_pendown;
992
c4f49254
IG
993 if (pdata->gpio_pendown_debounce)
994 gpio_set_debounce(pdata->gpio_pendown,
995 pdata->gpio_pendown_debounce);
0fbc9fdb
DT
996 } else {
997 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
998 return -EINVAL;
999 }
2991a1ca 1000
4d5975e5
EM
1001 return 0;
1002}
1003
2991a1ca
JW
1004/*
1005 * Set up the transfers to read touchscreen state; this assumes we
1006 * use formula #2 for pressure, not #3.
1007 */
5298cc4c 1008static void ads7846_setup_spi_msg(struct ads7846 *ts,
57691a1e 1009 const struct ads7846_platform_data *pdata)
ffa458c1 1010{
2991a1ca
JW
1011 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;
de2defd9 1015
06a09124 1016 if (ts->model == 7873) {
2991a1ca
JW
1017 /*
1018 * The AD7873 is almost identical to the ADS7846
06a09124 1019 * keep VREF off during differential/ratiometric
2991a1ca 1020 * conversion modes.
06a09124
MH
1021 */
1022 ts->model = 7846;
1023 vref = 0;
1024 }
1025
2991a1ca 1026 ts->msg_count = 1;
0b7018aa 1027 spi_message_init(m);
2991a1ca 1028 m->context = ts;
0b7018aa 1029
3eac5c7e
AG
1030 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);
d93f70b2 1044
3eac5c7e
AG
1045 x++;
1046 x->rx_buf = &packet->tc.y;
1047 x->len = 2;
1048 spi_message_add_tail(x, m);
1049 }
0b7018aa 1050
2991a1ca
JW
1051 /*
1052 * The first sample after switching drivers can be low quality;
e4f48861
SH
1053 * 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++;
e8f462d2 1060 x->tx_buf = &packet->read_y;
e4f48861
SH
1061 x->len = 1;
1062 spi_message_add_tail(x, m);
1063
1064 x++;
e8f462d2 1065 x->rx_buf = &packet->tc.y;
e4f48861
SH
1066 x->len = 2;
1067 spi_message_add_tail(x, m);
1068 }
1069
2991a1ca 1070 ts->msg_count++;
0b7018aa
ID
1071 m++;
1072 spi_message_init(m);
2991a1ca 1073 m->context = ts;
0b7018aa 1074
3eac5c7e
AG
1075 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);
0b7018aa 1091
3eac5c7e
AG
1092 x++;
1093 x->rx_buf = &packet->tc.x;
1094 x->len = 2;
1095 spi_message_add_tail(x, m);
1096 }
0b7018aa 1097
e4f48861
SH
1098 /* ... maybe discard first sample ... */
1099 if (pdata->settle_delay_usecs) {
1100 x->delay_usecs = pdata->settle_delay_usecs;
1101
1102 x++;
e8f462d2 1103 x->tx_buf = &packet->read_x;
e4f48861
SH
1104 x->len = 1;
1105 spi_message_add_tail(x, m);
1106
1107 x++;
e8f462d2 1108 x->rx_buf = &packet->tc.x;
e4f48861
SH
1109 x->len = 2;
1110 spi_message_add_tail(x, m);
1111 }
1112
ffa458c1
DB
1113 /* turn y+ off, x- on; we'll use formula #2 */
1114 if (ts->model == 7846) {
2991a1ca 1115 ts->msg_count++;
0b7018aa
ID
1116 m++;
1117 spi_message_init(m);
2991a1ca 1118 m->context = ts;
0b7018aa 1119
d93f70b2 1120 x++;
e8f462d2
DT
1121 packet->read_z1 = READ_Z1(vref);
1122 x->tx_buf = &packet->read_z1;
ffa458c1 1123 x->len = 1;
0b7018aa 1124 spi_message_add_tail(x, m);
d93f70b2 1125
ffa458c1 1126 x++;
e8f462d2 1127 x->rx_buf = &packet->tc.z1;
ffa458c1 1128 x->len = 2;
0b7018aa
ID
1129 spi_message_add_tail(x, m);
1130
e4f48861
SH
1131 /* ... maybe discard first sample ... */
1132 if (pdata->settle_delay_usecs) {
1133 x->delay_usecs = pdata->settle_delay_usecs;
1134
1135 x++;
e8f462d2 1136 x->tx_buf = &packet->read_z1;
e4f48861
SH
1137 x->len = 1;
1138 spi_message_add_tail(x, m);
1139
1140 x++;
e8f462d2 1141 x->rx_buf = &packet->tc.z1;
e4f48861
SH
1142 x->len = 2;
1143 spi_message_add_tail(x, m);
1144 }
1145
2991a1ca 1146 ts->msg_count++;
0b7018aa
ID
1147 m++;
1148 spi_message_init(m);
2991a1ca 1149 m->context = ts;
ffa458c1 1150
d93f70b2 1151 x++;
e8f462d2
DT
1152 packet->read_z2 = READ_Z2(vref);
1153 x->tx_buf = &packet->read_z2;
ffa458c1 1154 x->len = 1;
0b7018aa 1155 spi_message_add_tail(x, m);
d93f70b2 1156
ffa458c1 1157 x++;
e8f462d2 1158 x->rx_buf = &packet->tc.z2;
ffa458c1 1159 x->len = 2;
0b7018aa 1160 spi_message_add_tail(x, m);
ffa458c1 1161
e4f48861
SH
1162 /* ... maybe discard first sample ... */
1163 if (pdata->settle_delay_usecs) {
1164 x->delay_usecs = pdata->settle_delay_usecs;
1165
1166 x++;
e8f462d2 1167 x->tx_buf = &packet->read_z2;
e4f48861
SH
1168 x->len = 1;
1169 spi_message_add_tail(x, m);
1170
1171 x++;
e8f462d2 1172 x->rx_buf = &packet->tc.z2;
e4f48861
SH
1173 x->len = 2;
1174 spi_message_add_tail(x, m);
1175 }
0b7018aa 1176 }
53a0ef89
ID
1177
1178 /* power down */
2991a1ca 1179 ts->msg_count++;
0b7018aa
ID
1180 m++;
1181 spi_message_init(m);
2991a1ca 1182 m->context = ts;
0b7018aa 1183
3eac5c7e
AG
1184 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);
1197
1198 x++;
1199 x->rx_buf = &packet->dummy;
1200 x->len = 2;
1201 }
53a0ef89 1202
d93f70b2 1203 CS_CHANGE(*x);
0b7018aa 1204 spi_message_add_tail(x, m);
2991a1ca 1205}
ffa458c1 1206
a608026e
DM
1207#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
5298cc4c 1285static int ads7846_probe(struct spi_device *spi)
2991a1ca 1286{
a608026e 1287 const struct ads7846_platform_data *pdata;
2991a1ca
JW
1288 struct ads7846 *ts;
1289 struct ads7846_packet *packet;
1290 struct input_dev *input_dev;
2991a1ca
JW
1291 unsigned long irq_flags;
1292 int err;
1293
1294 if (!spi->irq) {
1295 dev_dbg(&spi->dev, "no IRQ?\n");
a608026e 1296 return -EINVAL;
2991a1ca
JW
1297 }
1298
1299 /* don't exceed max specified sample rate */
1300 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
a608026e 1301 dev_err(&spi->dev, "f(sample) %d KHz?\n",
2991a1ca
JW
1302 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1303 return -EINVAL;
1304 }
1305
a608026e
DM
1306 /*
1307 * We'd set TX word size 8 bits and RX word size to 13 bits ... except
2991a1ca
JW
1308 * 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;
ffa458c1 1316
2991a1ca
JW
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
c12454fa 1325 spi_set_drvdata(spi, ts);
2991a1ca
JW
1326
1327 ts->packet = packet;
1328 ts->spi = spi;
1329 ts->input = input_dev;
2991a1ca
JW
1330
1331 mutex_init(&ts->lock);
1332 init_waitqueue_head(&ts->wait);
1333
a608026e
DM
1334 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
2991a1ca
JW
1341 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
a608026e
DM
1346 ts->vref_mv = pdata->vref_mv;
1347 ts->swap_xy = pdata->swap_xy;
1348
2991a1ca
JW
1349 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
57691a1e 1369 err = ads7846_setup_pendown(spi, ts, pdata);
2991a1ca
JW
1370 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);
d5b415c9 1400
91143379
GI
1401 ts->reg = regulator_get(&spi->dev, "vcc");
1402 if (IS_ERR(ts->reg)) {
067fb2f6 1403 err = PTR_ERR(ts->reg);
56960b36 1404 dev_err(&spi->dev, "unable to get regulator: %d\n", err);
91143379
GI
1405 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
0f622bf4 1414 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
2991a1ca 1415 irq_flags |= IRQF_ONESHOT;
7804302b 1416
2991a1ca
JW
1417 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1418 irq_flags, spi->dev.driver->name, ts);
0f622bf4 1419 if (err && !pdata->irq_flags) {
c57c0a2a
MR
1420 dev_info(&spi->dev,
1421 "trying pin change workaround on irq %d\n", spi->irq);
2991a1ca
JW
1422 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);
0f622bf4
DT
1426 }
1427
1428 if (err) {
1429 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1430 goto err_disable_regulator;
ffa458c1 1431 }
ffa458c1 1432
2c8dc071
DB
1433 err = ads784x_hwmon_register(spi, ts);
1434 if (err)
1435 goto err_free_irq;
1436
2e5a7bd9 1437 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
ffa458c1 1438
2991a1ca
JW
1439 /*
1440 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
ffa458c1
DB
1441 * the touchscreen, in case it's not connected.
1442 */
3eac5c7e
AG
1443 if (ts->model == 7845)
1444 ads7845_read12_ser(&spi->dev, PWRDOWN);
1445 else
ebcaaad9 1446 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
ffa458c1 1447
2c8dc071 1448 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
8dd51650 1449 if (err)
2c8dc071 1450 goto err_remove_hwmon;
7de90a8c 1451
a90f7e98
DT
1452 err = input_register_device(input_dev);
1453 if (err)
8dd51650 1454 goto err_remove_attr_group;
a90f7e98 1455
fdba2bb1
RL
1456 device_init_wakeup(&spi->dev, pdata->wakeup);
1457
a608026e
DM
1458 /*
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
ffa458c1 1465 return 0;
a90f7e98 1466
8dd51650 1467 err_remove_attr_group:
2c8dc071
DB
1468 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1469 err_remove_hwmon:
1470 ads784x_hwmon_unregister(spi, ts);
8dd51650 1471 err_free_irq:
a90f7e98 1472 free_irq(spi->irq, ts);
91143379
GI
1473 err_disable_regulator:
1474 regulator_disable(ts->reg);
1475 err_put_regulator:
1476 regulator_put(ts->reg);
4d5975e5 1477 err_free_gpio:
0fbc9fdb 1478 if (!ts->get_pendown_state)
4d5975e5 1479 gpio_free(ts->gpio_pendown);
da970e69
ID
1480 err_cleanup_filter:
1481 if (ts->filter_cleanup)
1482 ts->filter_cleanup(ts->filter_data);
a90f7e98
DT
1483 err_free_mem:
1484 input_free_device(input_dev);
e8f462d2 1485 kfree(packet);
a90f7e98
DT
1486 kfree(ts);
1487 return err;
ffa458c1
DB
1488}
1489
e2619cf7 1490static int ads7846_remove(struct spi_device *spi)
ffa458c1 1491{
c12454fa 1492 struct ads7846 *ts = spi_get_drvdata(spi);
ffa458c1 1493
fdba2bb1
RL
1494 device_init_wakeup(&spi->dev, false);
1495
2c8dc071 1496 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
ffa458c1 1497
2991a1ca 1498 ads7846_disable(ts);
7de90a8c 1499 free_irq(ts->spi->irq, ts);
2991a1ca
JW
1500
1501 input_unregister_device(ts->input);
1502
1503 ads784x_hwmon_unregister(spi, ts);
7de90a8c 1504
91143379
GI
1505 regulator_disable(ts->reg);
1506 regulator_put(ts->reg);
1507
0fbc9fdb
DT
1508 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 */
4d5975e5 1513 gpio_free(ts->gpio_pendown);
0fbc9fdb 1514 }
4d5975e5 1515
da970e69
ID
1516 if (ts->filter_cleanup)
1517 ts->filter_cleanup(ts->filter_data);
1518
e8f462d2 1519 kfree(ts->packet);
ffa458c1
DB
1520 kfree(ts);
1521
2e5a7bd9 1522 dev_dbg(&spi->dev, "unregistered touchscreen\n");
2991a1ca 1523
ffa458c1
DB
1524 return 0;
1525}
1526
2e5a7bd9
DB
1527static struct spi_driver ads7846_driver = {
1528 .driver = {
1529 .name = "ads7846",
2e5a7bd9 1530 .owner = THIS_MODULE,
3c36e719 1531 .pm = &ads7846_pm,
a608026e 1532 .of_match_table = of_match_ptr(ads7846_dt_ids),
2e5a7bd9 1533 },
ffa458c1 1534 .probe = ads7846_probe,
1cb0aa88 1535 .remove = ads7846_remove,
ffa458c1
DB
1536};
1537
ca83922e 1538module_spi_driver(ads7846_driver);
ffa458c1
DB
1539
1540MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");
1541MODULE_LICENSE("GPL");
e0626e38 1542MODULE_ALIAS("spi:ads7846");
This page took 0.443218 seconds and 5 git commands to generate.