aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/ads7846.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/touchscreen/ads7846.c')
-rw-r--r--drivers/input/touchscreen/ads7846.c965
1 files changed, 509 insertions, 456 deletions
diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c
index 16031933a8f6..5196861b86ef 100644
--- a/drivers/input/touchscreen/ads7846.c
+++ b/drivers/input/touchscreen/ads7846.c
@@ -17,13 +17,16 @@
17 * it under the terms of the GNU General Public License version 2 as 17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation. 18 * published by the Free Software Foundation.
19 */ 19 */
20#include <linux/types.h>
20#include <linux/hwmon.h> 21#include <linux/hwmon.h>
21#include <linux/init.h> 22#include <linux/init.h>
22#include <linux/err.h> 23#include <linux/err.h>
24#include <linux/sched.h>
23#include <linux/delay.h> 25#include <linux/delay.h>
24#include <linux/input.h> 26#include <linux/input.h>
25#include <linux/interrupt.h> 27#include <linux/interrupt.h>
26#include <linux/slab.h> 28#include <linux/slab.h>
29#include <linux/pm.h>
27#include <linux/gpio.h> 30#include <linux/gpio.h>
28#include <linux/spi/spi.h> 31#include <linux/spi/spi.h>
29#include <linux/spi/ads7846.h> 32#include <linux/spi/ads7846.h>
@@ -52,22 +55,23 @@
52 * files. 55 * files.
53 */ 56 */
54 57
55#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */ 58#define TS_POLL_DELAY 1 /* ms delay before the first sample */
56#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */ 59#define TS_POLL_PERIOD 5 /* ms delay between samples */
57 60
58/* this driver doesn't aim at the peak continuous sample rate */ 61/* this driver doesn't aim at the peak continuous sample rate */
59#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) 62#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
60 63
61struct ts_event { 64struct ts_event {
62 /* For portability, we can't read 12 bit values using SPI (which 65 /*
63 * would make the controller deliver them as native byteorder u16 66 * For portability, we can't read 12 bit values using SPI (which
67 * would make the controller deliver them as native byte order u16
64 * with msbs zeroed). Instead, we read them as two 8-bit values, 68 * with msbs zeroed). Instead, we read them as two 8-bit values,
65 * *** WHICH NEED BYTESWAPPING *** and range adjustment. 69 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
66 */ 70 */
67 u16 x; 71 u16 x;
68 u16 y; 72 u16 y;
69 u16 z1, z2; 73 u16 z1, z2;
70 int ignore; 74 bool ignore;
71 u8 x_buf[3]; 75 u8 x_buf[3];
72 u8 y_buf[3]; 76 u8 y_buf[3];
73}; 77};
@@ -105,13 +109,17 @@ struct ads7846 {
105 u16 pressure_max; 109 u16 pressure_max;
106 110
107 bool swap_xy; 111 bool swap_xy;
112 bool use_internal;
108 113
109 struct ads7846_packet *packet; 114 struct ads7846_packet *packet;
110 115
111 struct spi_transfer xfer[18]; 116 struct spi_transfer xfer[18];
112 struct spi_message msg[5]; 117 struct spi_message msg[5];
113 struct spi_message *last_msg; 118 int msg_count;
114 int msg_idx; 119 wait_queue_head_t wait;
120
121 bool pendown;
122
115 int read_cnt; 123 int read_cnt;
116 int read_rep; 124 int read_rep;
117 int last_read; 125 int last_read;
@@ -122,14 +130,10 @@ struct ads7846 {
122 130
123 u16 penirq_recheck_delay_usecs; 131 u16 penirq_recheck_delay_usecs;
124 132
125 spinlock_t lock; 133 struct mutex lock;
126 struct hrtimer timer; 134 bool stopped; /* P: lock */
127 unsigned pendown:1; /* P: lock */ 135 bool disabled; /* P: lock */
128 unsigned pending:1; /* P: lock */ 136 bool suspended; /* P: lock */
129// FIXME remove "irq_disabled"
130 unsigned irq_disabled:1; /* P: lock */
131 unsigned disabled:1;
132 unsigned is_suspended:1;
133 137
134 int (*filter)(void *data, int data_idx, int *val); 138 int (*filter)(void *data, int data_idx, int *val);
135 void *filter_data; 139 void *filter_data;
@@ -165,7 +169,7 @@ struct ads7846 {
165#define ADS_12_BIT (0 << 3) 169#define ADS_12_BIT (0 << 3)
166#define ADS_SER (1 << 2) /* non-differential */ 170#define ADS_SER (1 << 2) /* non-differential */
167#define ADS_DFR (0 << 2) /* differential */ 171#define ADS_DFR (0 << 2) /* differential */
168#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ 172#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
169#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ 173#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
170#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ 174#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
171#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ 175#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
@@ -193,6 +197,78 @@ struct ads7846 {
193#define REF_ON (READ_12BIT_DFR(x, 1, 1)) 197#define REF_ON (READ_12BIT_DFR(x, 1, 1))
194#define REF_OFF (READ_12BIT_DFR(y, 0, 0)) 198#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
195 199
200/* Must be called with ts->lock held */
201static void ads7846_stop(struct ads7846 *ts)
202{
203 if (!ts->disabled && !ts->suspended) {
204 /* Signal IRQ thread to stop polling and disable the handler. */
205 ts->stopped = true;
206 mb();
207 wake_up(&ts->wait);
208 disable_irq(ts->spi->irq);
209 }
210}
211
212/* Must be called with ts->lock held */
213static void ads7846_restart(struct ads7846 *ts)
214{
215 if (!ts->disabled && !ts->suspended) {
216 /* Tell IRQ thread that it may poll the device. */
217 ts->stopped = false;
218 mb();
219 enable_irq(ts->spi->irq);
220 }
221}
222
223/* Must be called with ts->lock held */
224static void __ads7846_disable(struct ads7846 *ts)
225{
226 ads7846_stop(ts);
227 regulator_disable(ts->reg);
228
229 /*
230 * We know the chip's in low power mode since we always
231 * leave it that way after every request
232 */
233}
234
235/* Must be called with ts->lock held */
236static void __ads7846_enable(struct ads7846 *ts)
237{
238 regulator_enable(ts->reg);
239 ads7846_restart(ts);
240}
241
242static void ads7846_disable(struct ads7846 *ts)
243{
244 mutex_lock(&ts->lock);
245
246 if (!ts->disabled) {
247
248 if (!ts->suspended)
249 __ads7846_disable(ts);
250
251 ts->disabled = true;
252 }
253
254 mutex_unlock(&ts->lock);
255}
256
257static void ads7846_enable(struct ads7846 *ts)
258{
259 mutex_lock(&ts->lock);
260
261 if (ts->disabled) {
262
263 ts->disabled = false;
264
265 if (!ts->suspended)
266 __ads7846_enable(ts);
267 }
268
269 mutex_unlock(&ts->lock);
270}
271
196/*--------------------------------------------------------------------------*/ 272/*--------------------------------------------------------------------------*/
197 273
198/* 274/*
@@ -206,46 +282,41 @@ struct ser_req {
206 u8 command; 282 u8 command;
207 u8 ref_off; 283 u8 ref_off;
208 u16 scratch; 284 u16 scratch;
209 __be16 sample;
210 struct spi_message msg; 285 struct spi_message msg;
211 struct spi_transfer xfer[6]; 286 struct spi_transfer xfer[6];
287 /*
288 * DMA (thus cache coherency maintenance) requires the
289 * transfer buffers to live in their own cache lines.
290 */
291 __be16 sample ____cacheline_aligned;
212}; 292};
213 293
214struct ads7845_ser_req { 294struct ads7845_ser_req {
215 u8 command[3]; 295 u8 command[3];
216 u8 pwrdown[3];
217 u8 sample[3];
218 struct spi_message msg; 296 struct spi_message msg;
219 struct spi_transfer xfer[2]; 297 struct spi_transfer xfer[2];
298 /*
299 * DMA (thus cache coherency maintenance) requires the
300 * transfer buffers to live in their own cache lines.
301 */
302 u8 sample[3] ____cacheline_aligned;
220}; 303};
221 304
222static void ads7846_enable(struct ads7846 *ts);
223static void ads7846_disable(struct ads7846 *ts);
224
225static int device_suspended(struct device *dev)
226{
227 struct ads7846 *ts = dev_get_drvdata(dev);
228 return ts->is_suspended || ts->disabled;
229}
230
231static int ads7846_read12_ser(struct device *dev, unsigned command) 305static int ads7846_read12_ser(struct device *dev, unsigned command)
232{ 306{
233 struct spi_device *spi = to_spi_device(dev); 307 struct spi_device *spi = to_spi_device(dev);
234 struct ads7846 *ts = dev_get_drvdata(dev); 308 struct ads7846 *ts = dev_get_drvdata(dev);
235 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 309 struct ser_req *req;
236 int status; 310 int status;
237 int use_internal;
238 311
312 req = kzalloc(sizeof *req, GFP_KERNEL);
239 if (!req) 313 if (!req)
240 return -ENOMEM; 314 return -ENOMEM;
241 315
242 spi_message_init(&req->msg); 316 spi_message_init(&req->msg);
243 317
244 /* FIXME boards with ads7846 might use external vref instead ... */
245 use_internal = (ts->model == 7846);
246
247 /* maybe turn on internal vREF, and let it settle */ 318 /* maybe turn on internal vREF, and let it settle */
248 if (use_internal) { 319 if (ts->use_internal) {
249 req->ref_on = REF_ON; 320 req->ref_on = REF_ON;
250 req->xfer[0].tx_buf = &req->ref_on; 321 req->xfer[0].tx_buf = &req->ref_on;
251 req->xfer[0].len = 1; 322 req->xfer[0].len = 1;
@@ -257,8 +328,14 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
257 /* for 1uF, settle for 800 usec; no cap, 100 usec. */ 328 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
258 req->xfer[1].delay_usecs = ts->vref_delay_usecs; 329 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
259 spi_message_add_tail(&req->xfer[1], &req->msg); 330 spi_message_add_tail(&req->xfer[1], &req->msg);
331
332 /* Enable reference voltage */
333 command |= ADS_PD10_REF_ON;
260 } 334 }
261 335
336 /* Enable ADC in every case */
337 command |= ADS_PD10_ADC_ON;
338
262 /* take sample */ 339 /* take sample */
263 req->command = (u8) command; 340 req->command = (u8) command;
264 req->xfer[2].tx_buf = &req->command; 341 req->xfer[2].tx_buf = &req->command;
@@ -282,11 +359,11 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
282 CS_CHANGE(req->xfer[5]); 359 CS_CHANGE(req->xfer[5]);
283 spi_message_add_tail(&req->xfer[5], &req->msg); 360 spi_message_add_tail(&req->xfer[5], &req->msg);
284 361
285 ts->irq_disabled = 1; 362 mutex_lock(&ts->lock);
286 disable_irq(spi->irq); 363 ads7846_stop(ts);
287 status = spi_sync(spi, &req->msg); 364 status = spi_sync(spi, &req->msg);
288 ts->irq_disabled = 0; 365 ads7846_restart(ts);
289 enable_irq(spi->irq); 366 mutex_unlock(&ts->lock);
290 367
291 if (status == 0) { 368 if (status == 0) {
292 /* on-wire is a must-ignore bit, a BE12 value, then padding */ 369 /* on-wire is a must-ignore bit, a BE12 value, then padding */
@@ -301,11 +378,12 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
301 378
302static int ads7845_read12_ser(struct device *dev, unsigned command) 379static int ads7845_read12_ser(struct device *dev, unsigned command)
303{ 380{
304 struct spi_device *spi = to_spi_device(dev); 381 struct spi_device *spi = to_spi_device(dev);
305 struct ads7846 *ts = dev_get_drvdata(dev); 382 struct ads7846 *ts = dev_get_drvdata(dev);
306 struct ads7845_ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 383 struct ads7845_ser_req *req;
307 int status; 384 int status;
308 385
386 req = kzalloc(sizeof *req, GFP_KERNEL);
309 if (!req) 387 if (!req)
310 return -ENOMEM; 388 return -ENOMEM;
311 389
@@ -317,11 +395,11 @@ static int ads7845_read12_ser(struct device *dev, unsigned command)
317 req->xfer[0].len = 3; 395 req->xfer[0].len = 3;
318 spi_message_add_tail(&req->xfer[0], &req->msg); 396 spi_message_add_tail(&req->xfer[0], &req->msg);
319 397
320 ts->irq_disabled = 1; 398 mutex_lock(&ts->lock);
321 disable_irq(spi->irq); 399 ads7846_stop(ts);
322 status = spi_sync(spi, &req->msg); 400 status = spi_sync(spi, &req->msg);
323 ts->irq_disabled = 0; 401 ads7846_restart(ts);
324 enable_irq(spi->irq); 402 mutex_unlock(&ts->lock);
325 403
326 if (status == 0) { 404 if (status == 0) {
327 /* BE12 value, then padding */ 405 /* BE12 value, then padding */
@@ -341,7 +419,7 @@ name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
341{ \ 419{ \
342 struct ads7846 *ts = dev_get_drvdata(dev); \ 420 struct ads7846 *ts = dev_get_drvdata(dev); \
343 ssize_t v = ads7846_read12_ser(dev, \ 421 ssize_t v = ads7846_read12_ser(dev, \
344 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \ 422 READ_12BIT_SER(var)); \
345 if (v < 0) \ 423 if (v < 0) \
346 return v; \ 424 return v; \
347 return sprintf(buf, "%u\n", adjust(ts, v)); \ 425 return sprintf(buf, "%u\n", adjust(ts, v)); \
@@ -374,6 +452,7 @@ static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
374 /* external resistors may scale vAUX into 0..vREF */ 452 /* external resistors may scale vAUX into 0..vREF */
375 retval *= ts->vref_mv; 453 retval *= ts->vref_mv;
376 retval = retval >> 12; 454 retval = retval >> 12;
455
377 return retval; 456 return retval;
378} 457}
379 458
@@ -384,13 +463,13 @@ static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
384 /* ads7846 has a resistor ladder to scale this signal down */ 463 /* ads7846 has a resistor ladder to scale this signal down */
385 if (ts->model == 7846) 464 if (ts->model == 7846)
386 retval *= 4; 465 retval *= 4;
466
387 return retval; 467 return retval;
388} 468}
389 469
390SHOW(in0_input, vaux, vaux_adjust) 470SHOW(in0_input, vaux, vaux_adjust)
391SHOW(in1_input, vbatt, vbatt_adjust) 471SHOW(in1_input, vbatt, vbatt_adjust)
392 472
393
394static struct attribute *ads7846_attributes[] = { 473static struct attribute *ads7846_attributes[] = {
395 &dev_attr_temp0.attr, 474 &dev_attr_temp0.attr,
396 &dev_attr_temp1.attr, 475 &dev_attr_temp1.attr,
@@ -433,6 +512,7 @@ static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
433 if (!ts->vref_mv) { 512 if (!ts->vref_mv) {
434 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); 513 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
435 ts->vref_mv = 2500; 514 ts->vref_mv = 2500;
515 ts->use_internal = true;
436 } 516 }
437 break; 517 break;
438 case 7845: 518 case 7845:
@@ -498,17 +578,12 @@ static inline void ads784x_hwmon_unregister(struct spi_device *spi,
498} 578}
499#endif 579#endif
500 580
501static int is_pen_down(struct device *dev)
502{
503 struct ads7846 *ts = dev_get_drvdata(dev);
504
505 return ts->pendown;
506}
507
508static ssize_t ads7846_pen_down_show(struct device *dev, 581static ssize_t ads7846_pen_down_show(struct device *dev,
509 struct device_attribute *attr, char *buf) 582 struct device_attribute *attr, char *buf)
510{ 583{
511 return sprintf(buf, "%u\n", is_pen_down(dev)); 584 struct ads7846 *ts = dev_get_drvdata(dev);
585
586 return sprintf(buf, "%u\n", ts->pendown);
512} 587}
513 588
514static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); 589static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
@@ -516,7 +591,7 @@ static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
516static ssize_t ads7846_disable_show(struct device *dev, 591static ssize_t ads7846_disable_show(struct device *dev,
517 struct device_attribute *attr, char *buf) 592 struct device_attribute *attr, char *buf)
518{ 593{
519 struct ads7846 *ts = dev_get_drvdata(dev); 594 struct ads7846 *ts = dev_get_drvdata(dev);
520 595
521 return sprintf(buf, "%u\n", ts->disabled); 596 return sprintf(buf, "%u\n", ts->disabled);
522} 597}
@@ -531,15 +606,11 @@ static ssize_t ads7846_disable_store(struct device *dev,
531 if (strict_strtoul(buf, 10, &i)) 606 if (strict_strtoul(buf, 10, &i))
532 return -EINVAL; 607 return -EINVAL;
533 608
534 spin_lock_irq(&ts->lock);
535
536 if (i) 609 if (i)
537 ads7846_disable(ts); 610 ads7846_disable(ts);
538 else 611 else
539 ads7846_enable(ts); 612 ads7846_enable(ts);
540 613
541 spin_unlock_irq(&ts->lock);
542
543 return count; 614 return count;
544} 615}
545 616
@@ -569,23 +640,141 @@ static void null_wait_for_sync(void)
569{ 640{
570} 641}
571 642
572/* 643static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
573 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, 644{
574 * to retrieve touchscreen status. 645 struct ads7846 *ts = ads;
575 * 646
576 * The SPI transfer completion callback does the real work. It reports 647 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
577 * touchscreen events and reactivates the timer (or IRQ) as appropriate. 648 /* Start over collecting consistent readings. */
578 */ 649 ts->read_rep = 0;
650 /*
651 * Repeat it, if this was the first read or the read
652 * wasn't consistent enough.
653 */
654 if (ts->read_cnt < ts->debounce_max) {
655 ts->last_read = *val;
656 ts->read_cnt++;
657 return ADS7846_FILTER_REPEAT;
658 } else {
659 /*
660 * Maximum number of debouncing reached and still
661 * not enough number of consistent readings. Abort
662 * the whole sample, repeat it in the next sampling
663 * period.
664 */
665 ts->read_cnt = 0;
666 return ADS7846_FILTER_IGNORE;
667 }
668 } else {
669 if (++ts->read_rep > ts->debounce_rep) {
670 /*
671 * Got a good reading for this coordinate,
672 * go for the next one.
673 */
674 ts->read_cnt = 0;
675 ts->read_rep = 0;
676 return ADS7846_FILTER_OK;
677 } else {
678 /* Read more values that are consistent. */
679 ts->read_cnt++;
680 return ADS7846_FILTER_REPEAT;
681 }
682 }
683}
684
685static int ads7846_no_filter(void *ads, int data_idx, int *val)
686{
687 return ADS7846_FILTER_OK;
688}
689
690static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
691{
692 struct spi_transfer *t =
693 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
694
695 if (ts->model == 7845) {
696 return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
697 } else {
698 /*
699 * adjust: on-wire is a must-ignore bit, a BE12 value, then
700 * padding; built from two 8 bit values written msb-first.
701 */
702 return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
703 }
704}
705
706static void ads7846_update_value(struct spi_message *m, int val)
707{
708 struct spi_transfer *t =
709 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
579 710
580static void ads7846_rx(void *ads) 711 *(u16 *)t->rx_buf = val;
712}
713
714static void ads7846_read_state(struct ads7846 *ts)
581{ 715{
582 struct ads7846 *ts = ads; 716 struct ads7846_packet *packet = ts->packet;
583 struct ads7846_packet *packet = ts->packet; 717 struct spi_message *m;
584 unsigned Rt; 718 int msg_idx = 0;
585 u16 x, y, z1, z2; 719 int val;
720 int action;
721 int error;
586 722
587 /* ads7846_rx_val() did in-place conversion (including byteswap) from 723 while (msg_idx < ts->msg_count) {
588 * on-the-wire format as part of debouncing to get stable readings. 724
725 ts->wait_for_sync();
726
727 m = &ts->msg[msg_idx];
728 error = spi_sync(ts->spi, m);
729 if (error) {
730 dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
731 packet->tc.ignore = true;
732 return;
733 }
734
735 /*
736 * Last message is power down request, no need to convert
737 * or filter the value.
738 */
739 if (msg_idx < ts->msg_count - 1) {
740
741 val = ads7846_get_value(ts, m);
742
743 action = ts->filter(ts->filter_data, msg_idx, &val);
744 switch (action) {
745 case ADS7846_FILTER_REPEAT:
746 continue;
747
748 case ADS7846_FILTER_IGNORE:
749 packet->tc.ignore = true;
750 msg_idx = ts->msg_count - 1;
751 continue;
752
753 case ADS7846_FILTER_OK:
754 ads7846_update_value(m, val);
755 packet->tc.ignore = false;
756 msg_idx++;
757 break;
758
759 default:
760 BUG();
761 }
762 } else {
763 msg_idx++;
764 }
765 }
766}
767
768static void ads7846_report_state(struct ads7846 *ts)
769{
770 struct ads7846_packet *packet = ts->packet;
771 unsigned int Rt;
772 u16 x, y, z1, z2;
773
774 /*
775 * ads7846_get_value() does in-place conversion (including byte swap)
776 * from on-the-wire format as part of debouncing to get stable
777 * readings.
589 */ 778 */
590 if (ts->model == 7845) { 779 if (ts->model == 7845) {
591 x = *(u16 *)packet->tc.x_buf; 780 x = *(u16 *)packet->tc.x_buf;
@@ -623,19 +812,19 @@ static void ads7846_rx(void *ads)
623 Rt = 0; 812 Rt = 0;
624 } 813 }
625 814
626 /* Sample found inconsistent by debouncing or pressure is beyond 815 /*
816 * Sample found inconsistent by debouncing or pressure is beyond
627 * the maximum. Don't report it to user space, repeat at least 817 * the maximum. Don't report it to user space, repeat at least
628 * once more the measurement 818 * once more the measurement
629 */ 819 */
630 if (packet->tc.ignore || Rt > ts->pressure_max) { 820 if (packet->tc.ignore || Rt > ts->pressure_max) {
631 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", 821 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
632 packet->tc.ignore, Rt); 822 packet->tc.ignore, Rt);
633 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
634 HRTIMER_MODE_REL);
635 return; 823 return;
636 } 824 }
637 825
638 /* Maybe check the pendown state before reporting. This discards 826 /*
827 * Maybe check the pendown state before reporting. This discards
639 * false readings when the pen is lifted. 828 * false readings when the pen is lifted.
640 */ 829 */
641 if (ts->penirq_recheck_delay_usecs) { 830 if (ts->penirq_recheck_delay_usecs) {
@@ -644,8 +833,9 @@ static void ads7846_rx(void *ads)
644 Rt = 0; 833 Rt = 0;
645 } 834 }
646 835
647 /* NOTE: We can't rely on the pressure to determine the pen down 836 /*
648 * state, even this controller has a pressure sensor. The pressure 837 * NOTE: We can't rely on the pressure to determine the pen down
838 * state, even this controller has a pressure sensor. The pressure
649 * value can fluctuate for quite a while after lifting the pen and 839 * value can fluctuate for quite a while after lifting the pen and
650 * in some cases may not even settle at the expected value. 840 * in some cases may not even settle at the expected value.
651 * 841 *
@@ -655,15 +845,15 @@ static void ads7846_rx(void *ads)
655 if (Rt) { 845 if (Rt) {
656 struct input_dev *input = ts->input; 846 struct input_dev *input = ts->input;
657 847
848 if (ts->swap_xy)
849 swap(x, y);
850
658 if (!ts->pendown) { 851 if (!ts->pendown) {
659 input_report_key(input, BTN_TOUCH, 1); 852 input_report_key(input, BTN_TOUCH, 1);
660 ts->pendown = 1; 853 ts->pendown = true;
661 dev_vdbg(&ts->spi->dev, "DOWN\n"); 854 dev_vdbg(&ts->spi->dev, "DOWN\n");
662 } 855 }
663 856
664 if (ts->swap_xy)
665 swap(x, y);
666
667 input_report_abs(input, ABS_X, x); 857 input_report_abs(input, ABS_X, x);
668 input_report_abs(input, ABS_Y, y); 858 input_report_abs(input, ABS_Y, y);
669 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); 859 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
@@ -671,407 +861,161 @@ static void ads7846_rx(void *ads)
671 input_sync(input); 861 input_sync(input);
672 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); 862 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
673 } 863 }
674
675 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
676 HRTIMER_MODE_REL);
677} 864}
678 865
679static int ads7846_debounce(void *ads, int data_idx, int *val) 866static irqreturn_t ads7846_hard_irq(int irq, void *handle)
680{ 867{
681 struct ads7846 *ts = ads; 868 struct ads7846 *ts = handle;
682 869
683 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { 870 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
684 /* Start over collecting consistent readings. */
685 ts->read_rep = 0;
686 /* Repeat it, if this was the first read or the read
687 * wasn't consistent enough. */
688 if (ts->read_cnt < ts->debounce_max) {
689 ts->last_read = *val;
690 ts->read_cnt++;
691 return ADS7846_FILTER_REPEAT;
692 } else {
693 /* Maximum number of debouncing reached and still
694 * not enough number of consistent readings. Abort
695 * the whole sample, repeat it in the next sampling
696 * period.
697 */
698 ts->read_cnt = 0;
699 return ADS7846_FILTER_IGNORE;
700 }
701 } else {
702 if (++ts->read_rep > ts->debounce_rep) {
703 /* Got a good reading for this coordinate,
704 * go for the next one. */
705 ts->read_cnt = 0;
706 ts->read_rep = 0;
707 return ADS7846_FILTER_OK;
708 } else {
709 /* Read more values that are consistent. */
710 ts->read_cnt++;
711 return ADS7846_FILTER_REPEAT;
712 }
713 }
714} 871}
715 872
716static int ads7846_no_filter(void *ads, int data_idx, int *val)
717{
718 return ADS7846_FILTER_OK;
719}
720 873
721static void ads7846_rx_val(void *ads) 874static irqreturn_t ads7846_irq(int irq, void *handle)
722{ 875{
723 struct ads7846 *ts = ads; 876 struct ads7846 *ts = handle;
724 struct ads7846_packet *packet = ts->packet;
725 struct spi_message *m;
726 struct spi_transfer *t;
727 int val;
728 int action;
729 int status;
730
731 m = &ts->msg[ts->msg_idx];
732 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
733 877
734 if (ts->model == 7845) { 878 /* Start with a small delay before checking pendown state */
735 val = be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3; 879 msleep(TS_POLL_DELAY);
736 } else {
737 /* adjust: on-wire is a must-ignore bit, a BE12 value, then
738 * padding; built from two 8 bit values written msb-first.
739 */
740 val = be16_to_cpup((__be16 *)t->rx_buf) >> 3;
741 }
742 880
743 action = ts->filter(ts->filter_data, ts->msg_idx, &val); 881 while (!ts->stopped && get_pendown_state(ts)) {
744 switch (action) {
745 case ADS7846_FILTER_REPEAT:
746 break;
747 case ADS7846_FILTER_IGNORE:
748 packet->tc.ignore = 1;
749 /* Last message will contain ads7846_rx() as the
750 * completion function.
751 */
752 m = ts->last_msg;
753 break;
754 case ADS7846_FILTER_OK:
755 *(u16 *)t->rx_buf = val;
756 packet->tc.ignore = 0;
757 m = &ts->msg[++ts->msg_idx];
758 break;
759 default:
760 BUG();
761 }
762 ts->wait_for_sync();
763 status = spi_async(ts->spi, m);
764 if (status)
765 dev_err(&ts->spi->dev, "spi_async --> %d\n",
766 status);
767}
768 882
769static enum hrtimer_restart ads7846_timer(struct hrtimer *handle) 883 /* pen is down, continue with the measurement */
770{ 884 ads7846_read_state(ts);
771 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
772 int status = 0;
773 885
774 spin_lock(&ts->lock); 886 if (!ts->stopped)
887 ads7846_report_state(ts);
775 888
776 if (unlikely(!get_pendown_state(ts) || 889 wait_event_timeout(ts->wait, ts->stopped,
777 device_suspended(&ts->spi->dev))) { 890 msecs_to_jiffies(TS_POLL_PERIOD));
778 if (ts->pendown) { 891 }
779 struct input_dev *input = ts->input;
780 892
781 input_report_key(input, BTN_TOUCH, 0); 893 if (ts->pendown) {
782 input_report_abs(input, ABS_PRESSURE, 0); 894 struct input_dev *input = ts->input;
783 input_sync(input);
784 895
785 ts->pendown = 0; 896 input_report_key(input, BTN_TOUCH, 0);
786 dev_vdbg(&ts->spi->dev, "UP\n"); 897 input_report_abs(input, ABS_PRESSURE, 0);
787 } 898 input_sync(input);
788 899
789 /* measurement cycle ended */ 900 ts->pendown = false;
790 if (!device_suspended(&ts->spi->dev)) { 901 dev_vdbg(&ts->spi->dev, "UP\n");
791 ts->irq_disabled = 0;
792 enable_irq(ts->spi->irq);
793 }
794 ts->pending = 0;
795 } else {
796 /* pen is still down, continue with the measurement */
797 ts->msg_idx = 0;
798 ts->wait_for_sync();
799 status = spi_async(ts->spi, &ts->msg[0]);
800 if (status)
801 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
802 } 902 }
803 903
804 spin_unlock(&ts->lock); 904 return IRQ_HANDLED;
805 return HRTIMER_NORESTART;
806} 905}
807 906
808static irqreturn_t ads7846_irq(int irq, void *handle) 907#ifdef CONFIG_PM_SLEEP
908static int ads7846_suspend(struct device *dev)
809{ 909{
810 struct ads7846 *ts = handle; 910 struct ads7846 *ts = dev_get_drvdata(dev);
811 unsigned long flags;
812
813 spin_lock_irqsave(&ts->lock, flags);
814 if (likely(get_pendown_state(ts))) {
815 if (!ts->irq_disabled) {
816 /* The ARM do_simple_IRQ() dispatcher doesn't act
817 * like the other dispatchers: it will report IRQs
818 * even after they've been disabled. We work around
819 * that here. (The "generic irq" framework may help...)
820 */
821 ts->irq_disabled = 1;
822 disable_irq_nosync(ts->spi->irq);
823 ts->pending = 1;
824 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
825 HRTIMER_MODE_REL);
826 }
827 }
828 spin_unlock_irqrestore(&ts->lock, flags);
829 911
830 return IRQ_HANDLED; 912 mutex_lock(&ts->lock);
831}
832 913
833/*--------------------------------------------------------------------------*/ 914 if (!ts->suspended) {
834 915
835/* Must be called with ts->lock held */ 916 if (!ts->disabled)
836static void ads7846_disable(struct ads7846 *ts) 917 __ads7846_disable(ts);
837{
838 if (ts->disabled)
839 return;
840 918
841 ts->disabled = 1; 919 if (device_may_wakeup(&ts->spi->dev))
920 enable_irq_wake(ts->spi->irq);
842 921
843 /* are we waiting for IRQ, or polling? */ 922 ts->suspended = true;
844 if (!ts->pending) {
845 ts->irq_disabled = 1;
846 disable_irq(ts->spi->irq);
847 } else {
848 /* the timer will run at least once more, and
849 * leave everything in a clean state, IRQ disabled
850 */
851 while (ts->pending) {
852 spin_unlock_irq(&ts->lock);
853 msleep(1);
854 spin_lock_irq(&ts->lock);
855 }
856 } 923 }
857 924
858 regulator_disable(ts->reg); 925 mutex_unlock(&ts->lock);
859 926
860 /* we know the chip's in lowpower mode since we always 927 return 0;
861 * leave it that way after every request
862 */
863} 928}
864 929
865/* Must be called with ts->lock held */ 930static int ads7846_resume(struct device *dev)
866static void ads7846_enable(struct ads7846 *ts)
867{ 931{
868 if (!ts->disabled) 932 struct ads7846 *ts = dev_get_drvdata(dev);
869 return;
870
871 regulator_enable(ts->reg);
872 933
873 ts->disabled = 0; 934 mutex_lock(&ts->lock);
874 ts->irq_disabled = 0;
875 enable_irq(ts->spi->irq);
876}
877 935
878static int ads7846_suspend(struct spi_device *spi, pm_message_t message) 936 if (ts->suspended) {
879{
880 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
881 937
882 spin_lock_irq(&ts->lock); 938 ts->suspended = false;
883 939
884 ts->is_suspended = 1; 940 if (device_may_wakeup(&ts->spi->dev))
885 ads7846_disable(ts); 941 disable_irq_wake(ts->spi->irq);
886 942
887 spin_unlock_irq(&ts->lock); 943 if (!ts->disabled)
944 __ads7846_enable(ts);
945 }
888 946
889 if (device_may_wakeup(&ts->spi->dev)) 947 mutex_unlock(&ts->lock);
890 enable_irq_wake(ts->spi->irq);
891 948
892 return 0; 949 return 0;
893
894} 950}
951#endif
895 952
896static int ads7846_resume(struct spi_device *spi) 953static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);
897{
898 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
899
900 if (device_may_wakeup(&ts->spi->dev))
901 disable_irq_wake(ts->spi->irq);
902
903 spin_lock_irq(&ts->lock);
904
905 ts->is_suspended = 0;
906 ads7846_enable(ts);
907
908 spin_unlock_irq(&ts->lock);
909
910 return 0;
911}
912 954
913static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts) 955static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)
914{ 956{
915 struct ads7846_platform_data *pdata = spi->dev.platform_data; 957 struct ads7846_platform_data *pdata = spi->dev.platform_data;
916 int err; 958 int err;
917 959
918 /* REVISIT when the irq can be triggered active-low, or if for some 960 /*
961 * REVISIT when the irq can be triggered active-low, or if for some
919 * reason the touchscreen isn't hooked up, we don't need to access 962 * reason the touchscreen isn't hooked up, we don't need to access
920 * the pendown state. 963 * the pendown state.
921 */ 964 */
922 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
923 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
924 return -EINVAL;
925 }
926 965
927 if (pdata->get_pendown_state) { 966 if (pdata->get_pendown_state) {
928 ts->get_pendown_state = pdata->get_pendown_state; 967 ts->get_pendown_state = pdata->get_pendown_state;
929 return 0; 968 } else if (gpio_is_valid(pdata->gpio_pendown)) {
930 }
931 969
932 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown"); 970 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
933 if (err) { 971 if (err) {
934 dev_err(&spi->dev, "failed to request pendown GPIO%d\n", 972 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
935 pdata->gpio_pendown); 973 pdata->gpio_pendown);
936 return err; 974 return err;
937 } 975 }
938 976 err = gpio_direction_input(pdata->gpio_pendown);
939 ts->gpio_pendown = pdata->gpio_pendown; 977 if (err) {
940 return 0; 978 dev_err(&spi->dev, "failed to setup pendown GPIO%d\n",
941} 979 pdata->gpio_pendown);
942 980 gpio_free(pdata->gpio_pendown);
943static int __devinit ads7846_probe(struct spi_device *spi) 981 return err;
944{ 982 }
945 struct ads7846 *ts;
946 struct ads7846_packet *packet;
947 struct input_dev *input_dev;
948 const struct ads7846_platform_data *pdata = spi->dev.platform_data;
949 struct spi_message *m;
950 struct spi_transfer *x;
951 unsigned long irq_flags;
952 int vref;
953 int err;
954
955 if (!spi->irq) {
956 dev_dbg(&spi->dev, "no IRQ?\n");
957 return -ENODEV;
958 }
959 983
960 if (!pdata) { 984 ts->gpio_pendown = pdata->gpio_pendown;
961 dev_dbg(&spi->dev, "no platform data?\n");
962 return -ENODEV;
963 }
964 985
965 /* don't exceed max specified sample rate */ 986 } else {
966 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { 987 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
967 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
968 (spi->max_speed_hz/SAMPLE_BITS)/1000);
969 return -EINVAL; 988 return -EINVAL;
970 } 989 }
971 990
972 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except 991 return 0;
973 * that even if the hardware can do that, the SPI controller driver 992}
974 * may not. So we stick to very-portable 8 bit words, both RX and TX.
975 */
976 spi->bits_per_word = 8;
977 spi->mode = SPI_MODE_0;
978 err = spi_setup(spi);
979 if (err < 0)
980 return err;
981
982 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
983 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
984 input_dev = input_allocate_device();
985 if (!ts || !packet || !input_dev) {
986 err = -ENOMEM;
987 goto err_free_mem;
988 }
989
990 dev_set_drvdata(&spi->dev, ts);
991
992 ts->packet = packet;
993 ts->spi = spi;
994 ts->input = input_dev;
995 ts->vref_mv = pdata->vref_mv;
996 ts->swap_xy = pdata->swap_xy;
997
998 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
999 ts->timer.function = ads7846_timer;
1000
1001 spin_lock_init(&ts->lock);
1002
1003 ts->model = pdata->model ? : 7846;
1004 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1005 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1006 ts->pressure_max = pdata->pressure_max ? : ~0;
1007
1008 if (pdata->filter != NULL) {
1009 if (pdata->filter_init != NULL) {
1010 err = pdata->filter_init(pdata, &ts->filter_data);
1011 if (err < 0)
1012 goto err_free_mem;
1013 }
1014 ts->filter = pdata->filter;
1015 ts->filter_cleanup = pdata->filter_cleanup;
1016 } else if (pdata->debounce_max) {
1017 ts->debounce_max = pdata->debounce_max;
1018 if (ts->debounce_max < 2)
1019 ts->debounce_max = 2;
1020 ts->debounce_tol = pdata->debounce_tol;
1021 ts->debounce_rep = pdata->debounce_rep;
1022 ts->filter = ads7846_debounce;
1023 ts->filter_data = ts;
1024 } else
1025 ts->filter = ads7846_no_filter;
1026
1027 err = setup_pendown(spi, ts);
1028 if (err)
1029 goto err_cleanup_filter;
1030
1031 if (pdata->penirq_recheck_delay_usecs)
1032 ts->penirq_recheck_delay_usecs =
1033 pdata->penirq_recheck_delay_usecs;
1034
1035 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1036
1037 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1038 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1039
1040 input_dev->name = ts->name;
1041 input_dev->phys = ts->phys;
1042 input_dev->dev.parent = &spi->dev;
1043
1044 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1045 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1046 input_set_abs_params(input_dev, ABS_X,
1047 pdata->x_min ? : 0,
1048 pdata->x_max ? : MAX_12BIT,
1049 0, 0);
1050 input_set_abs_params(input_dev, ABS_Y,
1051 pdata->y_min ? : 0,
1052 pdata->y_max ? : MAX_12BIT,
1053 0, 0);
1054 input_set_abs_params(input_dev, ABS_PRESSURE,
1055 pdata->pressure_min, pdata->pressure_max, 0, 0);
1056 993
1057 vref = pdata->keep_vref_on; 994/*
995 * Set up the transfers to read touchscreen state; this assumes we
996 * use formula #2 for pressure, not #3.
997 */
998static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,
999 const struct ads7846_platform_data *pdata)
1000{
1001 struct spi_message *m = &ts->msg[0];
1002 struct spi_transfer *x = ts->xfer;
1003 struct ads7846_packet *packet = ts->packet;
1004 int vref = pdata->keep_vref_on;
1058 1005
1059 if (ts->model == 7873) { 1006 if (ts->model == 7873) {
1060 /* The AD7873 is almost identical to the ADS7846 1007 /*
1008 * The AD7873 is almost identical to the ADS7846
1061 * keep VREF off during differential/ratiometric 1009 * keep VREF off during differential/ratiometric
1062 * conversion modes 1010 * conversion modes.
1063 */ 1011 */
1064 ts->model = 7846; 1012 ts->model = 7846;
1065 vref = 0; 1013 vref = 0;
1066 } 1014 }
1067 1015
1068 /* set up the transfers to read touchscreen state; this assumes we 1016 ts->msg_count = 1;
1069 * use formula #2 for pressure, not #3.
1070 */
1071 m = &ts->msg[0];
1072 x = ts->xfer;
1073
1074 spi_message_init(m); 1017 spi_message_init(m);
1018 m->context = ts;
1075 1019
1076 if (ts->model == 7845) { 1020 if (ts->model == 7845) {
1077 packet->read_y_cmd[0] = READ_Y(vref); 1021 packet->read_y_cmd[0] = READ_Y(vref);
@@ -1094,7 +1038,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1094 spi_message_add_tail(x, m); 1038 spi_message_add_tail(x, m);
1095 } 1039 }
1096 1040
1097 /* the first sample after switching drivers can be low quality; 1041 /*
1042 * The first sample after switching drivers can be low quality;
1098 * optionally discard it, using a second one after the signals 1043 * optionally discard it, using a second one after the signals
1099 * have had enough time to stabilize. 1044 * have had enough time to stabilize.
1100 */ 1045 */
@@ -1112,11 +1057,10 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1112 spi_message_add_tail(x, m); 1057 spi_message_add_tail(x, m);
1113 } 1058 }
1114 1059
1115 m->complete = ads7846_rx_val; 1060 ts->msg_count++;
1116 m->context = ts;
1117
1118 m++; 1061 m++;
1119 spi_message_init(m); 1062 spi_message_init(m);
1063 m->context = ts;
1120 1064
1121 if (ts->model == 7845) { 1065 if (ts->model == 7845) {
1122 x++; 1066 x++;
@@ -1156,13 +1100,12 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1156 spi_message_add_tail(x, m); 1100 spi_message_add_tail(x, m);
1157 } 1101 }
1158 1102
1159 m->complete = ads7846_rx_val;
1160 m->context = ts;
1161
1162 /* turn y+ off, x- on; we'll use formula #2 */ 1103 /* turn y+ off, x- on; we'll use formula #2 */
1163 if (ts->model == 7846) { 1104 if (ts->model == 7846) {
1105 ts->msg_count++;
1164 m++; 1106 m++;
1165 spi_message_init(m); 1107 spi_message_init(m);
1108 m->context = ts;
1166 1109
1167 x++; 1110 x++;
1168 packet->read_z1 = READ_Z1(vref); 1111 packet->read_z1 = READ_Z1(vref);
@@ -1190,11 +1133,10 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1190 spi_message_add_tail(x, m); 1133 spi_message_add_tail(x, m);
1191 } 1134 }
1192 1135
1193 m->complete = ads7846_rx_val; 1136 ts->msg_count++;
1194 m->context = ts;
1195
1196 m++; 1137 m++;
1197 spi_message_init(m); 1138 spi_message_init(m);
1139 m->context = ts;
1198 1140
1199 x++; 1141 x++;
1200 packet->read_z2 = READ_Z2(vref); 1142 packet->read_z2 = READ_Z2(vref);
@@ -1221,14 +1163,13 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1221 x->len = 2; 1163 x->len = 2;
1222 spi_message_add_tail(x, m); 1164 spi_message_add_tail(x, m);
1223 } 1165 }
1224
1225 m->complete = ads7846_rx_val;
1226 m->context = ts;
1227 } 1166 }
1228 1167
1229 /* power down */ 1168 /* power down */
1169 ts->msg_count++;
1230 m++; 1170 m++;
1231 spi_message_init(m); 1171 spi_message_init(m);
1172 m->context = ts;
1232 1173
1233 if (ts->model == 7845) { 1174 if (ts->model == 7845) {
1234 x++; 1175 x++;
@@ -1251,11 +1192,119 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1251 1192
1252 CS_CHANGE(*x); 1193 CS_CHANGE(*x);
1253 spi_message_add_tail(x, m); 1194 spi_message_add_tail(x, m);
1195}
1254 1196
1255 m->complete = ads7846_rx; 1197static int __devinit ads7846_probe(struct spi_device *spi)
1256 m->context = ts; 1198{
1199 struct ads7846 *ts;
1200 struct ads7846_packet *packet;
1201 struct input_dev *input_dev;
1202 struct ads7846_platform_data *pdata = spi->dev.platform_data;
1203 unsigned long irq_flags;
1204 int err;
1205
1206 if (!spi->irq) {
1207 dev_dbg(&spi->dev, "no IRQ?\n");
1208 return -ENODEV;
1209 }
1210
1211 if (!pdata) {
1212 dev_dbg(&spi->dev, "no platform data?\n");
1213 return -ENODEV;
1214 }
1215
1216 /* don't exceed max specified sample rate */
1217 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1218 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
1219 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1220 return -EINVAL;
1221 }
1222
1223 /* We'd set TX word size 8 bits and RX word size to 13 bits ... except
1224 * that even if the hardware can do that, the SPI controller driver
1225 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1226 */
1227 spi->bits_per_word = 8;
1228 spi->mode = SPI_MODE_0;
1229 err = spi_setup(spi);
1230 if (err < 0)
1231 return err;
1232
1233 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1234 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1235 input_dev = input_allocate_device();
1236 if (!ts || !packet || !input_dev) {
1237 err = -ENOMEM;
1238 goto err_free_mem;
1239 }
1240
1241 dev_set_drvdata(&spi->dev, ts);
1242
1243 ts->packet = packet;
1244 ts->spi = spi;
1245 ts->input = input_dev;
1246 ts->vref_mv = pdata->vref_mv;
1247 ts->swap_xy = pdata->swap_xy;
1248
1249 mutex_init(&ts->lock);
1250 init_waitqueue_head(&ts->wait);
1251
1252 ts->model = pdata->model ? : 7846;
1253 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1254 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1255 ts->pressure_max = pdata->pressure_max ? : ~0;
1256
1257 if (pdata->filter != NULL) {
1258 if (pdata->filter_init != NULL) {
1259 err = pdata->filter_init(pdata, &ts->filter_data);
1260 if (err < 0)
1261 goto err_free_mem;
1262 }
1263 ts->filter = pdata->filter;
1264 ts->filter_cleanup = pdata->filter_cleanup;
1265 } else if (pdata->debounce_max) {
1266 ts->debounce_max = pdata->debounce_max;
1267 if (ts->debounce_max < 2)
1268 ts->debounce_max = 2;
1269 ts->debounce_tol = pdata->debounce_tol;
1270 ts->debounce_rep = pdata->debounce_rep;
1271 ts->filter = ads7846_debounce_filter;
1272 ts->filter_data = ts;
1273 } else {
1274 ts->filter = ads7846_no_filter;
1275 }
1276
1277 err = ads7846_setup_pendown(spi, ts);
1278 if (err)
1279 goto err_cleanup_filter;
1280
1281 if (pdata->penirq_recheck_delay_usecs)
1282 ts->penirq_recheck_delay_usecs =
1283 pdata->penirq_recheck_delay_usecs;
1284
1285 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1286
1287 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1288 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1289
1290 input_dev->name = ts->name;
1291 input_dev->phys = ts->phys;
1292 input_dev->dev.parent = &spi->dev;
1293
1294 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1295 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1296 input_set_abs_params(input_dev, ABS_X,
1297 pdata->x_min ? : 0,
1298 pdata->x_max ? : MAX_12BIT,
1299 0, 0);
1300 input_set_abs_params(input_dev, ABS_Y,
1301 pdata->y_min ? : 0,
1302 pdata->y_max ? : MAX_12BIT,
1303 0, 0);
1304 input_set_abs_params(input_dev, ABS_PRESSURE,
1305 pdata->pressure_min, pdata->pressure_max, 0, 0);
1257 1306
1258 ts->last_msg = m; 1307 ads7846_setup_spi_msg(ts, pdata);
1259 1308
1260 ts->reg = regulator_get(&spi->dev, "vcc"); 1309 ts->reg = regulator_get(&spi->dev, "vcc");
1261 if (IS_ERR(ts->reg)) { 1310 if (IS_ERR(ts->reg)) {
@@ -1271,16 +1320,17 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1271 } 1320 }
1272 1321
1273 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING; 1322 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1323 irq_flags |= IRQF_ONESHOT;
1274 1324
1275 err = request_irq(spi->irq, ads7846_irq, irq_flags, 1325 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1276 spi->dev.driver->name, ts); 1326 irq_flags, spi->dev.driver->name, ts);
1277
1278 if (err && !pdata->irq_flags) { 1327 if (err && !pdata->irq_flags) {
1279 dev_info(&spi->dev, 1328 dev_info(&spi->dev,
1280 "trying pin change workaround on irq %d\n", spi->irq); 1329 "trying pin change workaround on irq %d\n", spi->irq);
1281 err = request_irq(spi->irq, ads7846_irq, 1330 irq_flags |= IRQF_TRIGGER_RISING;
1282 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 1331 err = request_threaded_irq(spi->irq,
1283 spi->dev.driver->name, ts); 1332 ads7846_hard_irq, ads7846_irq,
1333 irq_flags, spi->dev.driver->name, ts);
1284 } 1334 }
1285 1335
1286 if (err) { 1336 if (err) {
@@ -1294,14 +1344,14 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1294 1344
1295 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); 1345 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1296 1346
1297 /* take a first sample, leaving nPENIRQ active and vREF off; avoid 1347 /*
1348 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1298 * the touchscreen, in case it's not connected. 1349 * the touchscreen, in case it's not connected.
1299 */ 1350 */
1300 if (ts->model == 7845) 1351 if (ts->model == 7845)
1301 ads7845_read12_ser(&spi->dev, PWRDOWN); 1352 ads7845_read12_ser(&spi->dev, PWRDOWN);
1302 else 1353 else
1303 (void) ads7846_read12_ser(&spi->dev, 1354 (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));
1304 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
1305 1355
1306 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group); 1356 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
1307 if (err) 1357 if (err)
@@ -1326,7 +1376,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1326 err_put_regulator: 1376 err_put_regulator:
1327 regulator_put(ts->reg); 1377 regulator_put(ts->reg);
1328 err_free_gpio: 1378 err_free_gpio:
1329 if (ts->gpio_pendown != -1) 1379 if (!ts->get_pendown_state)
1330 gpio_free(ts->gpio_pendown); 1380 gpio_free(ts->gpio_pendown);
1331 err_cleanup_filter: 1381 err_cleanup_filter:
1332 if (ts->filter_cleanup) 1382 if (ts->filter_cleanup)
@@ -1340,26 +1390,29 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1340 1390
1341static int __devexit ads7846_remove(struct spi_device *spi) 1391static int __devexit ads7846_remove(struct spi_device *spi)
1342{ 1392{
1343 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 1393 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1344 1394
1345 device_init_wakeup(&spi->dev, false); 1395 device_init_wakeup(&spi->dev, false);
1346 1396
1347 ads784x_hwmon_unregister(spi, ts);
1348 input_unregister_device(ts->input);
1349
1350 ads7846_suspend(spi, PMSG_SUSPEND);
1351
1352 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); 1397 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1353 1398
1399 ads7846_disable(ts);
1354 free_irq(ts->spi->irq, ts); 1400 free_irq(ts->spi->irq, ts);
1355 /* suspend left the IRQ disabled */ 1401
1356 enable_irq(ts->spi->irq); 1402 input_unregister_device(ts->input);
1403
1404 ads784x_hwmon_unregister(spi, ts);
1357 1405
1358 regulator_disable(ts->reg); 1406 regulator_disable(ts->reg);
1359 regulator_put(ts->reg); 1407 regulator_put(ts->reg);
1360 1408
1361 if (ts->gpio_pendown != -1) 1409 if (!ts->get_pendown_state) {
1410 /*
1411 * If we are not using specialized pendown method we must
1412 * have been relying on gpio we set up ourselves.
1413 */
1362 gpio_free(ts->gpio_pendown); 1414 gpio_free(ts->gpio_pendown);
1415 }
1363 1416
1364 if (ts->filter_cleanup) 1417 if (ts->filter_cleanup)
1365 ts->filter_cleanup(ts->filter_data); 1418 ts->filter_cleanup(ts->filter_data);
@@ -1368,6 +1421,7 @@ static int __devexit ads7846_remove(struct spi_device *spi)
1368 kfree(ts); 1421 kfree(ts);
1369 1422
1370 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 1423 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1424
1371 return 0; 1425 return 0;
1372} 1426}
1373 1427
@@ -1376,11 +1430,10 @@ static struct spi_driver ads7846_driver = {
1376 .name = "ads7846", 1430 .name = "ads7846",
1377 .bus = &spi_bus_type, 1431 .bus = &spi_bus_type,
1378 .owner = THIS_MODULE, 1432 .owner = THIS_MODULE,
1433 .pm = &ads7846_pm,
1379 }, 1434 },
1380 .probe = ads7846_probe, 1435 .probe = ads7846_probe,
1381 .remove = __devexit_p(ads7846_remove), 1436 .remove = __devexit_p(ads7846_remove),
1382 .suspend = ads7846_suspend,
1383 .resume = ads7846_resume,
1384}; 1437};
1385 1438
1386static int __init ads7846_init(void) 1439static int __init ads7846_init(void)