aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Wang <jason77.wang@gmail.com>2010-10-13 14:35:40 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2010-10-18 00:10:28 -0400
commit2991a1ca6e9b13b639a82c0eec0cbc191bf1f42f (patch)
treeccf612f19613723148f9c40965af15a7f7c79af4
parent0982258264d2f615612ab957634efdeb874f47c8 (diff)
Input: ads7846 - switch to using threaded IRQ
Commit 9114337 introduces regulator operations in ads7846 touchscreen driver. Among these operations, some are called while holding a spinlock. On many platforms regulators reside on slow buses, such as I2C/SPI and require sleep while accessing them. The touchscreen itself is also a SPI device and currently relies on asynchronous SPI access to avoid sleeping in interrupt context. Let's switch to using threaded IRQ to be able to access SPI bus synchronously (which simplifies driver a bit); it also allows safe access to the regulators as well. This has been tested on the ti_omap3530evm board: 1) using ts_lib after normal boot 2) using ts_lib after "#echo 1/0 > /sys/bus/spi/devices/spi0.1/disable" 3) using ts_lib after "#echo mem > /sys/power/state" and "wake up" Also tested on pandora. Based on original patch by Dmitry Torokhov. Tested-by: Grazvydas Ignotas <notasas@gmail.com> Signed-off-by: Jason Wang <jason77.wang@gmail.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
-rw-r--r--drivers/input/touchscreen/ads7846.c886
1 files changed, 456 insertions, 430 deletions
diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c
index 16031933a8f6..14ea54b78e46 100644
--- a/drivers/input/touchscreen/ads7846.c
+++ b/drivers/input/touchscreen/ads7846.c
@@ -17,9 +17,11 @@
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>
@@ -52,22 +54,23 @@
52 * files. 54 * files.
53 */ 55 */
54 56
55#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */ 57#define TS_POLL_DELAY 1 /* ms delay before the first sample */
56#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */ 58#define TS_POLL_PERIOD 5 /* ms delay between samples */
57 59
58/* this driver doesn't aim at the peak continuous sample rate */ 60/* this driver doesn't aim at the peak continuous sample rate */
59#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) 61#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
60 62
61struct ts_event { 63struct ts_event {
62 /* For portability, we can't read 12 bit values using SPI (which 64 /*
63 * would make the controller deliver them as native byteorder u16 65 * For portability, we can't read 12 bit values using SPI (which
66 * would make the controller deliver them as native byte order u16
64 * with msbs zeroed). Instead, we read them as two 8-bit values, 67 * with msbs zeroed). Instead, we read them as two 8-bit values,
65 * *** WHICH NEED BYTESWAPPING *** and range adjustment. 68 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
66 */ 69 */
67 u16 x; 70 u16 x;
68 u16 y; 71 u16 y;
69 u16 z1, z2; 72 u16 z1, z2;
70 int ignore; 73 bool ignore;
71 u8 x_buf[3]; 74 u8 x_buf[3];
72 u8 y_buf[3]; 75 u8 y_buf[3];
73}; 76};
@@ -110,8 +113,11 @@ struct ads7846 {
110 113
111 struct spi_transfer xfer[18]; 114 struct spi_transfer xfer[18];
112 struct spi_message msg[5]; 115 struct spi_message msg[5];
113 struct spi_message *last_msg; 116 int msg_count;
114 int msg_idx; 117 wait_queue_head_t wait;
118
119 bool pendown;
120
115 int read_cnt; 121 int read_cnt;
116 int read_rep; 122 int read_rep;
117 int last_read; 123 int last_read;
@@ -122,14 +128,10 @@ struct ads7846 {
122 128
123 u16 penirq_recheck_delay_usecs; 129 u16 penirq_recheck_delay_usecs;
124 130
125 spinlock_t lock; 131 struct mutex lock;
126 struct hrtimer timer; 132 bool stopped; /* P: lock */
127 unsigned pendown:1; /* P: lock */ 133 bool disabled; /* P: lock */
128 unsigned pending:1; /* P: lock */ 134 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 135
134 int (*filter)(void *data, int data_idx, int *val); 136 int (*filter)(void *data, int data_idx, int *val);
135 void *filter_data; 137 void *filter_data;
@@ -165,7 +167,7 @@ struct ads7846 {
165#define ADS_12_BIT (0 << 3) 167#define ADS_12_BIT (0 << 3)
166#define ADS_SER (1 << 2) /* non-differential */ 168#define ADS_SER (1 << 2) /* non-differential */
167#define ADS_DFR (0 << 2) /* differential */ 169#define ADS_DFR (0 << 2) /* differential */
168#define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ 170#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */
169#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ 171#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */
170#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ 172#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */
171#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ 173#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
@@ -193,6 +195,78 @@ struct ads7846 {
193#define REF_ON (READ_12BIT_DFR(x, 1, 1)) 195#define REF_ON (READ_12BIT_DFR(x, 1, 1))
194#define REF_OFF (READ_12BIT_DFR(y, 0, 0)) 196#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
195 197
198/* Must be called with ts->lock held */
199static void ads7846_stop(struct ads7846 *ts)
200{
201 if (!ts->disabled && !ts->suspended) {
202 /* Signal IRQ thread to stop polling and disable the handler. */
203 ts->stopped = true;
204 mb();
205 wake_up(&ts->wait);
206 disable_irq(ts->spi->irq);
207 }
208}
209
210/* Must be called with ts->lock held */
211static void ads7846_restart(struct ads7846 *ts)
212{
213 if (!ts->disabled && !ts->suspended) {
214 /* Tell IRQ thread that it may poll the device. */
215 ts->stopped = false;
216 mb();
217 enable_irq(ts->spi->irq);
218 }
219}
220
221/* Must be called with ts->lock held */
222static void __ads7846_disable(struct ads7846 *ts)
223{
224 ads7846_stop(ts);
225 regulator_disable(ts->reg);
226
227 /*
228 * We know the chip's in low power mode since we always
229 * leave it that way after every request
230 */
231}
232
233/* Must be called with ts->lock held */
234static void __ads7846_enable(struct ads7846 *ts)
235{
236 regulator_enable(ts->reg);
237 ads7846_restart(ts);
238}
239
240static void ads7846_disable(struct ads7846 *ts)
241{
242 mutex_lock(&ts->lock);
243
244 if (!ts->disabled) {
245
246 if (!ts->suspended)
247 __ads7846_disable(ts);
248
249 ts->disabled = true;
250 }
251
252 mutex_unlock(&ts->lock);
253}
254
255static void ads7846_enable(struct ads7846 *ts)
256{
257 mutex_lock(&ts->lock);
258
259 if (ts->disabled) {
260
261 ts->disabled = false;
262
263 if (!ts->suspended)
264 __ads7846_enable(ts);
265 }
266
267 mutex_unlock(&ts->lock);
268}
269
196/*--------------------------------------------------------------------------*/ 270/*--------------------------------------------------------------------------*/
197 271
198/* 272/*
@@ -219,23 +293,15 @@ struct ads7845_ser_req {
219 struct spi_transfer xfer[2]; 293 struct spi_transfer xfer[2];
220}; 294};
221 295
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) 296static int ads7846_read12_ser(struct device *dev, unsigned command)
232{ 297{
233 struct spi_device *spi = to_spi_device(dev); 298 struct spi_device *spi = to_spi_device(dev);
234 struct ads7846 *ts = dev_get_drvdata(dev); 299 struct ads7846 *ts = dev_get_drvdata(dev);
235 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 300 struct ser_req *req;
236 int status; 301 int status;
237 int use_internal; 302 int use_internal;
238 303
304 req = kzalloc(sizeof *req, GFP_KERNEL);
239 if (!req) 305 if (!req)
240 return -ENOMEM; 306 return -ENOMEM;
241 307
@@ -282,11 +348,11 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
282 CS_CHANGE(req->xfer[5]); 348 CS_CHANGE(req->xfer[5]);
283 spi_message_add_tail(&req->xfer[5], &req->msg); 349 spi_message_add_tail(&req->xfer[5], &req->msg);
284 350
285 ts->irq_disabled = 1; 351 mutex_lock(&ts->lock);
286 disable_irq(spi->irq); 352 ads7846_stop(ts);
287 status = spi_sync(spi, &req->msg); 353 status = spi_sync(spi, &req->msg);
288 ts->irq_disabled = 0; 354 ads7846_restart(ts);
289 enable_irq(spi->irq); 355 mutex_unlock(&ts->lock);
290 356
291 if (status == 0) { 357 if (status == 0) {
292 /* on-wire is a must-ignore bit, a BE12 value, then padding */ 358 /* on-wire is a must-ignore bit, a BE12 value, then padding */
@@ -301,11 +367,12 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
301 367
302static int ads7845_read12_ser(struct device *dev, unsigned command) 368static int ads7845_read12_ser(struct device *dev, unsigned command)
303{ 369{
304 struct spi_device *spi = to_spi_device(dev); 370 struct spi_device *spi = to_spi_device(dev);
305 struct ads7846 *ts = dev_get_drvdata(dev); 371 struct ads7846 *ts = dev_get_drvdata(dev);
306 struct ads7845_ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 372 struct ads7845_ser_req *req;
307 int status; 373 int status;
308 374
375 req = kzalloc(sizeof *req, GFP_KERNEL);
309 if (!req) 376 if (!req)
310 return -ENOMEM; 377 return -ENOMEM;
311 378
@@ -317,11 +384,11 @@ static int ads7845_read12_ser(struct device *dev, unsigned command)
317 req->xfer[0].len = 3; 384 req->xfer[0].len = 3;
318 spi_message_add_tail(&req->xfer[0], &req->msg); 385 spi_message_add_tail(&req->xfer[0], &req->msg);
319 386
320 ts->irq_disabled = 1; 387 mutex_lock(&ts->lock);
321 disable_irq(spi->irq); 388 ads7846_stop(ts);
322 status = spi_sync(spi, &req->msg); 389 status = spi_sync(spi, &req->msg);
323 ts->irq_disabled = 0; 390 ads7846_restart(ts);
324 enable_irq(spi->irq); 391 mutex_unlock(&ts->lock);
325 392
326 if (status == 0) { 393 if (status == 0) {
327 /* BE12 value, then padding */ 394 /* BE12 value, then padding */
@@ -374,6 +441,7 @@ static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
374 /* external resistors may scale vAUX into 0..vREF */ 441 /* external resistors may scale vAUX into 0..vREF */
375 retval *= ts->vref_mv; 442 retval *= ts->vref_mv;
376 retval = retval >> 12; 443 retval = retval >> 12;
444
377 return retval; 445 return retval;
378} 446}
379 447
@@ -384,13 +452,13 @@ static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
384 /* ads7846 has a resistor ladder to scale this signal down */ 452 /* ads7846 has a resistor ladder to scale this signal down */
385 if (ts->model == 7846) 453 if (ts->model == 7846)
386 retval *= 4; 454 retval *= 4;
455
387 return retval; 456 return retval;
388} 457}
389 458
390SHOW(in0_input, vaux, vaux_adjust) 459SHOW(in0_input, vaux, vaux_adjust)
391SHOW(in1_input, vbatt, vbatt_adjust) 460SHOW(in1_input, vbatt, vbatt_adjust)
392 461
393
394static struct attribute *ads7846_attributes[] = { 462static struct attribute *ads7846_attributes[] = {
395 &dev_attr_temp0.attr, 463 &dev_attr_temp0.attr,
396 &dev_attr_temp1.attr, 464 &dev_attr_temp1.attr,
@@ -498,17 +566,12 @@ static inline void ads784x_hwmon_unregister(struct spi_device *spi,
498} 566}
499#endif 567#endif
500 568
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, 569static ssize_t ads7846_pen_down_show(struct device *dev,
509 struct device_attribute *attr, char *buf) 570 struct device_attribute *attr, char *buf)
510{ 571{
511 return sprintf(buf, "%u\n", is_pen_down(dev)); 572 struct ads7846 *ts = dev_get_drvdata(dev);
573
574 return sprintf(buf, "%u\n", ts->pendown);
512} 575}
513 576
514static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); 577static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
@@ -516,7 +579,7 @@ static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
516static ssize_t ads7846_disable_show(struct device *dev, 579static ssize_t ads7846_disable_show(struct device *dev,
517 struct device_attribute *attr, char *buf) 580 struct device_attribute *attr, char *buf)
518{ 581{
519 struct ads7846 *ts = dev_get_drvdata(dev); 582 struct ads7846 *ts = dev_get_drvdata(dev);
520 583
521 return sprintf(buf, "%u\n", ts->disabled); 584 return sprintf(buf, "%u\n", ts->disabled);
522} 585}
@@ -531,15 +594,11 @@ static ssize_t ads7846_disable_store(struct device *dev,
531 if (strict_strtoul(buf, 10, &i)) 594 if (strict_strtoul(buf, 10, &i))
532 return -EINVAL; 595 return -EINVAL;
533 596
534 spin_lock_irq(&ts->lock);
535
536 if (i) 597 if (i)
537 ads7846_disable(ts); 598 ads7846_disable(ts);
538 else 599 else
539 ads7846_enable(ts); 600 ads7846_enable(ts);
540 601
541 spin_unlock_irq(&ts->lock);
542
543 return count; 602 return count;
544} 603}
545 604
@@ -569,23 +628,141 @@ static void null_wait_for_sync(void)
569{ 628{
570} 629}
571 630
572/* 631static int ads7846_debounce_filter(void *ads, int data_idx, int *val)
573 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, 632{
574 * to retrieve touchscreen status. 633 struct ads7846 *ts = ads;
575 * 634
576 * The SPI transfer completion callback does the real work. It reports 635 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
577 * touchscreen events and reactivates the timer (or IRQ) as appropriate. 636 /* Start over collecting consistent readings. */
578 */ 637 ts->read_rep = 0;
638 /*
639 * Repeat it, if this was the first read or the read
640 * wasn't consistent enough.
641 */
642 if (ts->read_cnt < ts->debounce_max) {
643 ts->last_read = *val;
644 ts->read_cnt++;
645 return ADS7846_FILTER_REPEAT;
646 } else {
647 /*
648 * Maximum number of debouncing reached and still
649 * not enough number of consistent readings. Abort
650 * the whole sample, repeat it in the next sampling
651 * period.
652 */
653 ts->read_cnt = 0;
654 return ADS7846_FILTER_IGNORE;
655 }
656 } else {
657 if (++ts->read_rep > ts->debounce_rep) {
658 /*
659 * Got a good reading for this coordinate,
660 * go for the next one.
661 */
662 ts->read_cnt = 0;
663 ts->read_rep = 0;
664 return ADS7846_FILTER_OK;
665 } else {
666 /* Read more values that are consistent. */
667 ts->read_cnt++;
668 return ADS7846_FILTER_REPEAT;
669 }
670 }
671}
672
673static int ads7846_no_filter(void *ads, int data_idx, int *val)
674{
675 return ADS7846_FILTER_OK;
676}
677
678static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)
679{
680 struct spi_transfer *t =
681 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
682
683 if (ts->model == 7845) {
684 return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
685 } else {
686 /*
687 * adjust: on-wire is a must-ignore bit, a BE12 value, then
688 * padding; built from two 8 bit values written msb-first.
689 */
690 return be16_to_cpup((__be16 *)t->rx_buf) >> 3;
691 }
692}
693
694static void ads7846_update_value(struct spi_message *m, int val)
695{
696 struct spi_transfer *t =
697 list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
698
699 *(u16 *)t->rx_buf = val;
700}
701
702static void ads7846_read_state(struct ads7846 *ts)
703{
704 struct ads7846_packet *packet = ts->packet;
705 struct spi_message *m;
706 int msg_idx = 0;
707 int val;
708 int action;
709 int error;
710
711 while (msg_idx < ts->msg_count) {
712
713 ts->wait_for_sync();
714
715 m = &ts->msg[msg_idx];
716 error = spi_sync(ts->spi, m);
717 if (error) {
718 dev_err(&ts->spi->dev, "spi_async --> %d\n", error);
719 packet->tc.ignore = true;
720 return;
721 }
722
723 /*
724 * Last message is power down request, no need to convert
725 * or filter the value.
726 */
727 if (msg_idx < ts->msg_count - 1) {
579 728
580static void ads7846_rx(void *ads) 729 val = ads7846_get_value(ts, m);
730
731 action = ts->filter(ts->filter_data, msg_idx, &val);
732 switch (action) {
733 case ADS7846_FILTER_REPEAT:
734 continue;
735
736 case ADS7846_FILTER_IGNORE:
737 packet->tc.ignore = true;
738 msg_idx = ts->msg_count - 1;
739 continue;
740
741 case ADS7846_FILTER_OK:
742 ads7846_update_value(m, val);
743 packet->tc.ignore = false;
744 msg_idx++;
745 break;
746
747 default:
748 BUG();
749 }
750 } else {
751 msg_idx++;
752 }
753 }
754}
755
756static void ads7846_report_state(struct ads7846 *ts)
581{ 757{
582 struct ads7846 *ts = ads; 758 struct ads7846_packet *packet = ts->packet;
583 struct ads7846_packet *packet = ts->packet; 759 unsigned int Rt;
584 unsigned Rt; 760 u16 x, y, z1, z2;
585 u16 x, y, z1, z2;
586 761
587 /* ads7846_rx_val() did in-place conversion (including byteswap) from 762 /*
588 * on-the-wire format as part of debouncing to get stable readings. 763 * ads7846_get_value() does in-place conversion (including byte swap)
764 * from on-the-wire format as part of debouncing to get stable
765 * readings.
589 */ 766 */
590 if (ts->model == 7845) { 767 if (ts->model == 7845) {
591 x = *(u16 *)packet->tc.x_buf; 768 x = *(u16 *)packet->tc.x_buf;
@@ -623,19 +800,19 @@ static void ads7846_rx(void *ads)
623 Rt = 0; 800 Rt = 0;
624 } 801 }
625 802
626 /* Sample found inconsistent by debouncing or pressure is beyond 803 /*
804 * Sample found inconsistent by debouncing or pressure is beyond
627 * the maximum. Don't report it to user space, repeat at least 805 * the maximum. Don't report it to user space, repeat at least
628 * once more the measurement 806 * once more the measurement
629 */ 807 */
630 if (packet->tc.ignore || Rt > ts->pressure_max) { 808 if (packet->tc.ignore || Rt > ts->pressure_max) {
631 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", 809 dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",
632 packet->tc.ignore, Rt); 810 packet->tc.ignore, Rt);
633 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
634 HRTIMER_MODE_REL);
635 return; 811 return;
636 } 812 }
637 813
638 /* Maybe check the pendown state before reporting. This discards 814 /*
815 * Maybe check the pendown state before reporting. This discards
639 * false readings when the pen is lifted. 816 * false readings when the pen is lifted.
640 */ 817 */
641 if (ts->penirq_recheck_delay_usecs) { 818 if (ts->penirq_recheck_delay_usecs) {
@@ -644,8 +821,9 @@ static void ads7846_rx(void *ads)
644 Rt = 0; 821 Rt = 0;
645 } 822 }
646 823
647 /* NOTE: We can't rely on the pressure to determine the pen down 824 /*
648 * state, even this controller has a pressure sensor. The pressure 825 * NOTE: We can't rely on the pressure to determine the pen down
826 * state, even this controller has a pressure sensor. The pressure
649 * value can fluctuate for quite a while after lifting the pen and 827 * value can fluctuate for quite a while after lifting the pen and
650 * in some cases may not even settle at the expected value. 828 * in some cases may not even settle at the expected value.
651 * 829 *
@@ -655,15 +833,15 @@ static void ads7846_rx(void *ads)
655 if (Rt) { 833 if (Rt) {
656 struct input_dev *input = ts->input; 834 struct input_dev *input = ts->input;
657 835
836 if (ts->swap_xy)
837 swap(x, y);
838
658 if (!ts->pendown) { 839 if (!ts->pendown) {
659 input_report_key(input, BTN_TOUCH, 1); 840 input_report_key(input, BTN_TOUCH, 1);
660 ts->pendown = 1; 841 ts->pendown = true;
661 dev_vdbg(&ts->spi->dev, "DOWN\n"); 842 dev_vdbg(&ts->spi->dev, "DOWN\n");
662 } 843 }
663 844
664 if (ts->swap_xy)
665 swap(x, y);
666
667 input_report_abs(input, ABS_X, x); 845 input_report_abs(input, ABS_X, x);
668 input_report_abs(input, ABS_Y, y); 846 input_report_abs(input, ABS_Y, y);
669 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); 847 input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);
@@ -671,246 +849,94 @@ static void ads7846_rx(void *ads)
671 input_sync(input); 849 input_sync(input);
672 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); 850 dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
673 } 851 }
674
675 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
676 HRTIMER_MODE_REL);
677}
678
679static int ads7846_debounce(void *ads, int data_idx, int *val)
680{
681 struct ads7846 *ts = ads;
682
683 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
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} 852}
715 853
716static int ads7846_no_filter(void *ads, int data_idx, int *val) 854static irqreturn_t ads7846_hard_irq(int irq, void *handle)
717{ 855{
718 return ADS7846_FILTER_OK; 856 struct ads7846 *ts = handle;
719}
720
721static void ads7846_rx_val(void *ads)
722{
723 struct ads7846 *ts = ads;
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
734 if (ts->model == 7845) {
735 val = be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;
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 857
743 action = ts->filter(ts->filter_data, ts->msg_idx, &val); 858 return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
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} 859}
768 860
769static enum hrtimer_restart ads7846_timer(struct hrtimer *handle)
770{
771 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
772 int status = 0;
773
774 spin_lock(&ts->lock);
775
776 if (unlikely(!get_pendown_state(ts) ||
777 device_suspended(&ts->spi->dev))) {
778 if (ts->pendown) {
779 struct input_dev *input = ts->input;
780
781 input_report_key(input, BTN_TOUCH, 0);
782 input_report_abs(input, ABS_PRESSURE, 0);
783 input_sync(input);
784
785 ts->pendown = 0;
786 dev_vdbg(&ts->spi->dev, "UP\n");
787 }
788
789 /* measurement cycle ended */
790 if (!device_suspended(&ts->spi->dev)) {
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 }
803
804 spin_unlock(&ts->lock);
805 return HRTIMER_NORESTART;
806}
807 861
808static irqreturn_t ads7846_irq(int irq, void *handle) 862static irqreturn_t ads7846_irq(int irq, void *handle)
809{ 863{
810 struct ads7846 *ts = handle; 864 struct ads7846 *ts = handle;
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 865
830 return IRQ_HANDLED; 866 /* Start with a small delay before checking pendown state */
831} 867 msleep(TS_POLL_DELAY);
832 868
833/*--------------------------------------------------------------------------*/ 869 while (!ts->stopped && get_pendown_state(ts)) {
834 870
835/* Must be called with ts->lock held */ 871 /* pen is down, continue with the measurement */
836static void ads7846_disable(struct ads7846 *ts) 872 ads7846_read_state(ts);
837{
838 if (ts->disabled)
839 return;
840 873
841 ts->disabled = 1; 874 if (!ts->stopped)
875 ads7846_report_state(ts);
842 876
843 /* are we waiting for IRQ, or polling? */ 877 wait_event_timeout(ts->wait, ts->stopped,
844 if (!ts->pending) { 878 msecs_to_jiffies(TS_POLL_PERIOD));
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 } 879 }
857 880
858 regulator_disable(ts->reg); 881 if (ts->pendown) {
859 882 struct input_dev *input = ts->input;
860 /* we know the chip's in lowpower mode since we always
861 * leave it that way after every request
862 */
863}
864 883
865/* Must be called with ts->lock held */ 884 input_report_key(input, BTN_TOUCH, 0);
866static void ads7846_enable(struct ads7846 *ts) 885 input_report_abs(input, ABS_PRESSURE, 0);
867{ 886 input_sync(input);
868 if (!ts->disabled)
869 return;
870 887
871 regulator_enable(ts->reg); 888 ts->pendown = false;
889 dev_vdbg(&ts->spi->dev, "UP\n");
890 }
872 891
873 ts->disabled = 0; 892 return IRQ_HANDLED;
874 ts->irq_disabled = 0;
875 enable_irq(ts->spi->irq);
876} 893}
877 894
878static int ads7846_suspend(struct spi_device *spi, pm_message_t message) 895static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
879{ 896{
880 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 897 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
881 898
882 spin_lock_irq(&ts->lock); 899 mutex_lock(&ts->lock);
883 900
884 ts->is_suspended = 1; 901 if (!ts->suspended) {
885 ads7846_disable(ts);
886 902
887 spin_unlock_irq(&ts->lock); 903 if (!ts->disabled)
904 __ads7846_disable(ts);
888 905
889 if (device_may_wakeup(&ts->spi->dev)) 906 if (device_may_wakeup(&ts->spi->dev))
890 enable_irq_wake(ts->spi->irq); 907 enable_irq_wake(ts->spi->irq);
891 908
892 return 0; 909 ts->suspended = true;
910 }
911
912 mutex_unlock(&ts->lock);
893 913
914 return 0;
894} 915}
895 916
896static int ads7846_resume(struct spi_device *spi) 917static int ads7846_resume(struct spi_device *spi)
897{ 918{
898 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 919 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
899 920
900 if (device_may_wakeup(&ts->spi->dev)) 921 mutex_lock(&ts->lock);
901 disable_irq_wake(ts->spi->irq); 922
923 if (ts->suspended) {
902 924
903 spin_lock_irq(&ts->lock); 925 ts->suspended = false;
904 926
905 ts->is_suspended = 0; 927 if (device_may_wakeup(&ts->spi->dev))
906 ads7846_enable(ts); 928 disable_irq_wake(ts->spi->irq);
907 929
908 spin_unlock_irq(&ts->lock); 930 if (!ts->disabled)
931 __ads7846_enable(ts);
932 }
933
934 mutex_unlock(&ts->lock);
909 935
910 return 0; 936 return 0;
911} 937}
912 938
913static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts) 939static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)
914{ 940{
915 struct ads7846_platform_data *pdata = spi->dev.platform_data; 941 struct ads7846_platform_data *pdata = spi->dev.platform_data;
916 int err; 942 int err;
@@ -932,146 +958,40 @@ static int __devinit setup_pendown(struct spi_device *spi, struct ads7846 *ts)
932 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown"); 958 err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");
933 if (err) { 959 if (err) {
934 dev_err(&spi->dev, "failed to request pendown GPIO%d\n", 960 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
935 pdata->gpio_pendown); 961 pdata->gpio_pendown);
936 return err; 962 return err;
937 } 963 }
938 964
939 ts->gpio_pendown = pdata->gpio_pendown; 965 ts->gpio_pendown = pdata->gpio_pendown;
966
940 return 0; 967 return 0;
941} 968}
942 969
943static int __devinit ads7846_probe(struct spi_device *spi) 970/*
971 * Set up the transfers to read touchscreen state; this assumes we
972 * use formula #2 for pressure, not #3.
973 */
974static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,
975 const struct ads7846_platform_data *pdata)
944{ 976{
945 struct ads7846 *ts; 977 struct spi_message *m = &ts->msg[0];
946 struct ads7846_packet *packet; 978 struct spi_transfer *x = ts->xfer;
947 struct input_dev *input_dev; 979 struct ads7846_packet *packet = ts->packet;
948 const struct ads7846_platform_data *pdata = spi->dev.platform_data; 980 int vref = pdata->keep_vref_on;
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
960 if (!pdata) {
961 dev_dbg(&spi->dev, "no platform data?\n");
962 return -ENODEV;
963 }
964
965 /* don't exceed max specified sample rate */
966 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
967 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
968 (spi->max_speed_hz/SAMPLE_BITS)/1000);
969 return -EINVAL;
970 }
971
972 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
973 * that even if the hardware can do that, the SPI controller driver
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
1057 vref = pdata->keep_vref_on;
1058 981
1059 if (ts->model == 7873) { 982 if (ts->model == 7873) {
1060 /* The AD7873 is almost identical to the ADS7846 983 /*
984 * The AD7873 is almost identical to the ADS7846
1061 * keep VREF off during differential/ratiometric 985 * keep VREF off during differential/ratiometric
1062 * conversion modes 986 * conversion modes.
1063 */ 987 */
1064 ts->model = 7846; 988 ts->model = 7846;
1065 vref = 0; 989 vref = 0;
1066 } 990 }
1067 991
1068 /* set up the transfers to read touchscreen state; this assumes we 992 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); 993 spi_message_init(m);
994 m->context = ts;
1075 995
1076 if (ts->model == 7845) { 996 if (ts->model == 7845) {
1077 packet->read_y_cmd[0] = READ_Y(vref); 997 packet->read_y_cmd[0] = READ_Y(vref);
@@ -1094,7 +1014,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1094 spi_message_add_tail(x, m); 1014 spi_message_add_tail(x, m);
1095 } 1015 }
1096 1016
1097 /* the first sample after switching drivers can be low quality; 1017 /*
1018 * The first sample after switching drivers can be low quality;
1098 * optionally discard it, using a second one after the signals 1019 * optionally discard it, using a second one after the signals
1099 * have had enough time to stabilize. 1020 * have had enough time to stabilize.
1100 */ 1021 */
@@ -1112,11 +1033,10 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1112 spi_message_add_tail(x, m); 1033 spi_message_add_tail(x, m);
1113 } 1034 }
1114 1035
1115 m->complete = ads7846_rx_val; 1036 ts->msg_count++;
1116 m->context = ts;
1117
1118 m++; 1037 m++;
1119 spi_message_init(m); 1038 spi_message_init(m);
1039 m->context = ts;
1120 1040
1121 if (ts->model == 7845) { 1041 if (ts->model == 7845) {
1122 x++; 1042 x++;
@@ -1156,13 +1076,12 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1156 spi_message_add_tail(x, m); 1076 spi_message_add_tail(x, m);
1157 } 1077 }
1158 1078
1159 m->complete = ads7846_rx_val;
1160 m->context = ts;
1161
1162 /* turn y+ off, x- on; we'll use formula #2 */ 1079 /* turn y+ off, x- on; we'll use formula #2 */
1163 if (ts->model == 7846) { 1080 if (ts->model == 7846) {
1081 ts->msg_count++;
1164 m++; 1082 m++;
1165 spi_message_init(m); 1083 spi_message_init(m);
1084 m->context = ts;
1166 1085
1167 x++; 1086 x++;
1168 packet->read_z1 = READ_Z1(vref); 1087 packet->read_z1 = READ_Z1(vref);
@@ -1190,11 +1109,10 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1190 spi_message_add_tail(x, m); 1109 spi_message_add_tail(x, m);
1191 } 1110 }
1192 1111
1193 m->complete = ads7846_rx_val; 1112 ts->msg_count++;
1194 m->context = ts;
1195
1196 m++; 1113 m++;
1197 spi_message_init(m); 1114 spi_message_init(m);
1115 m->context = ts;
1198 1116
1199 x++; 1117 x++;
1200 packet->read_z2 = READ_Z2(vref); 1118 packet->read_z2 = READ_Z2(vref);
@@ -1221,14 +1139,13 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1221 x->len = 2; 1139 x->len = 2;
1222 spi_message_add_tail(x, m); 1140 spi_message_add_tail(x, m);
1223 } 1141 }
1224
1225 m->complete = ads7846_rx_val;
1226 m->context = ts;
1227 } 1142 }
1228 1143
1229 /* power down */ 1144 /* power down */
1145 ts->msg_count++;
1230 m++; 1146 m++;
1231 spi_message_init(m); 1147 spi_message_init(m);
1148 m->context = ts;
1232 1149
1233 if (ts->model == 7845) { 1150 if (ts->model == 7845) {
1234 x++; 1151 x++;
@@ -1251,11 +1168,119 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1251 1168
1252 CS_CHANGE(*x); 1169 CS_CHANGE(*x);
1253 spi_message_add_tail(x, m); 1170 spi_message_add_tail(x, m);
1171}
1254 1172
1255 m->complete = ads7846_rx; 1173static int __devinit ads7846_probe(struct spi_device *spi)
1256 m->context = ts; 1174{
1175 struct ads7846 *ts;
1176 struct ads7846_packet *packet;
1177 struct input_dev *input_dev;
1178 struct ads7846_platform_data *pdata = spi->dev.platform_data;
1179 unsigned long irq_flags;
1180 int err;
1181
1182 if (!spi->irq) {
1183 dev_dbg(&spi->dev, "no IRQ?\n");
1184 return -ENODEV;
1185 }
1186
1187 if (!pdata) {
1188 dev_dbg(&spi->dev, "no platform data?\n");
1189 return -ENODEV;
1190 }
1191
1192 /* don't exceed max specified sample rate */
1193 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
1194 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
1195 (spi->max_speed_hz/SAMPLE_BITS)/1000);
1196 return -EINVAL;
1197 }
1198
1199 /* We'd set TX word size 8 bits and RX word size to 13 bits ... except
1200 * that even if the hardware can do that, the SPI controller driver
1201 * may not. So we stick to very-portable 8 bit words, both RX and TX.
1202 */
1203 spi->bits_per_word = 8;
1204 spi->mode = SPI_MODE_0;
1205 err = spi_setup(spi);
1206 if (err < 0)
1207 return err;
1257 1208
1258 ts->last_msg = m; 1209 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
1210 packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);
1211 input_dev = input_allocate_device();
1212 if (!ts || !packet || !input_dev) {
1213 err = -ENOMEM;
1214 goto err_free_mem;
1215 }
1216
1217 dev_set_drvdata(&spi->dev, ts);
1218
1219 ts->packet = packet;
1220 ts->spi = spi;
1221 ts->input = input_dev;
1222 ts->vref_mv = pdata->vref_mv;
1223 ts->swap_xy = pdata->swap_xy;
1224
1225 mutex_init(&ts->lock);
1226 init_waitqueue_head(&ts->wait);
1227
1228 ts->model = pdata->model ? : 7846;
1229 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
1230 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
1231 ts->pressure_max = pdata->pressure_max ? : ~0;
1232
1233 if (pdata->filter != NULL) {
1234 if (pdata->filter_init != NULL) {
1235 err = pdata->filter_init(pdata, &ts->filter_data);
1236 if (err < 0)
1237 goto err_free_mem;
1238 }
1239 ts->filter = pdata->filter;
1240 ts->filter_cleanup = pdata->filter_cleanup;
1241 } else if (pdata->debounce_max) {
1242 ts->debounce_max = pdata->debounce_max;
1243 if (ts->debounce_max < 2)
1244 ts->debounce_max = 2;
1245 ts->debounce_tol = pdata->debounce_tol;
1246 ts->debounce_rep = pdata->debounce_rep;
1247 ts->filter = ads7846_debounce_filter;
1248 ts->filter_data = ts;
1249 } else {
1250 ts->filter = ads7846_no_filter;
1251 }
1252
1253 err = ads7846_setup_pendown(spi, ts);
1254 if (err)
1255 goto err_cleanup_filter;
1256
1257 if (pdata->penirq_recheck_delay_usecs)
1258 ts->penirq_recheck_delay_usecs =
1259 pdata->penirq_recheck_delay_usecs;
1260
1261 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
1262
1263 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
1264 snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);
1265
1266 input_dev->name = ts->name;
1267 input_dev->phys = ts->phys;
1268 input_dev->dev.parent = &spi->dev;
1269
1270 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1271 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
1272 input_set_abs_params(input_dev, ABS_X,
1273 pdata->x_min ? : 0,
1274 pdata->x_max ? : MAX_12BIT,
1275 0, 0);
1276 input_set_abs_params(input_dev, ABS_Y,
1277 pdata->y_min ? : 0,
1278 pdata->y_max ? : MAX_12BIT,
1279 0, 0);
1280 input_set_abs_params(input_dev, ABS_PRESSURE,
1281 pdata->pressure_min, pdata->pressure_max, 0, 0);
1282
1283 ads7846_setup_spi_msg(ts, pdata);
1259 1284
1260 ts->reg = regulator_get(&spi->dev, "vcc"); 1285 ts->reg = regulator_get(&spi->dev, "vcc");
1261 if (IS_ERR(ts->reg)) { 1286 if (IS_ERR(ts->reg)) {
@@ -1271,16 +1296,17 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1271 } 1296 }
1272 1297
1273 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING; 1298 irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;
1299 irq_flags |= IRQF_ONESHOT;
1274 1300
1275 err = request_irq(spi->irq, ads7846_irq, irq_flags, 1301 err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,
1276 spi->dev.driver->name, ts); 1302 irq_flags, spi->dev.driver->name, ts);
1277
1278 if (err && !pdata->irq_flags) { 1303 if (err && !pdata->irq_flags) {
1279 dev_info(&spi->dev, 1304 dev_info(&spi->dev,
1280 "trying pin change workaround on irq %d\n", spi->irq); 1305 "trying pin change workaround on irq %d\n", spi->irq);
1281 err = request_irq(spi->irq, ads7846_irq, 1306 irq_flags |= IRQF_TRIGGER_RISING;
1282 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 1307 err = request_threaded_irq(spi->irq,
1283 spi->dev.driver->name, ts); 1308 ads7846_hard_irq, ads7846_irq,
1309 irq_flags, spi->dev.driver->name, ts);
1284 } 1310 }
1285 1311
1286 if (err) { 1312 if (err) {
@@ -1294,7 +1320,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1294 1320
1295 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); 1321 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
1296 1322
1297 /* take a first sample, leaving nPENIRQ active and vREF off; avoid 1323 /*
1324 * Take a first sample, leaving nPENIRQ active and vREF off; avoid
1298 * the touchscreen, in case it's not connected. 1325 * the touchscreen, in case it's not connected.
1299 */ 1326 */
1300 if (ts->model == 7845) 1327 if (ts->model == 7845)
@@ -1340,20 +1367,18 @@ static int __devinit ads7846_probe(struct spi_device *spi)
1340 1367
1341static int __devexit ads7846_remove(struct spi_device *spi) 1368static int __devexit ads7846_remove(struct spi_device *spi)
1342{ 1369{
1343 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 1370 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
1344 1371
1345 device_init_wakeup(&spi->dev, false); 1372 device_init_wakeup(&spi->dev, false);
1346 1373
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); 1374 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1353 1375
1376 ads7846_disable(ts);
1354 free_irq(ts->spi->irq, ts); 1377 free_irq(ts->spi->irq, ts);
1355 /* suspend left the IRQ disabled */ 1378
1356 enable_irq(ts->spi->irq); 1379 input_unregister_device(ts->input);
1380
1381 ads784x_hwmon_unregister(spi, ts);
1357 1382
1358 regulator_disable(ts->reg); 1383 regulator_disable(ts->reg);
1359 regulator_put(ts->reg); 1384 regulator_put(ts->reg);
@@ -1368,6 +1393,7 @@ static int __devexit ads7846_remove(struct spi_device *spi)
1368 kfree(ts); 1393 kfree(ts);
1369 1394
1370 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 1395 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1396
1371 return 0; 1397 return 0;
1372} 1398}
1373 1399