Merge remote-tracking branch 'remotes/tegra/android-tegra-2.6.36-honeycomb-mr1' into...
[firefly-linux-kernel-4.4.55.git] / drivers / input / touchscreen / xpt2046_tslib_ts.c
1 /*
2  * drivers/input/touchscreen/xpt2046_ts.c - driver for rk29 spi xpt2046 device and console
3  *
4  * Copyright (C) 2011 ROCKCHIP, Inc.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15  
16 #include <linux/hwmon.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/delay.h>
20 #include <linux/input.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/gpio.h>
24 #include <linux/spi/spi.h>
25 #include <asm/irq.h>
26 #include <mach/iomux.h>
27 #ifdef CONFIG_HAS_EARLYSUSPEND
28 #include <linux/earlysuspend.h>
29 #endif
30 #include "xpt2046_tslib_ts.h"
31 #include "ts_lib/tslib.h"
32 /*
33  * This code has been heavily tested on a Nokia 770, and lightly
34  * tested on other xpt2046 devices (OSK/Mistral, Lubbock).
35  * TSC2046 is just newer xpt2046 silicon.
36  * Support for ads7843 tested on Atmel at91sam926x-EK.
37  * Support for ads7845 has only been stubbed in.
38  *
39  * IRQ handling needs a workaround because of a shortcoming in handling
40  * edge triggered IRQs on some platforms like the OMAP1/2. These
41  * platforms don't handle the ARM lazy IRQ disabling properly, thus we
42  * have to maintain our own SW IRQ disabled status. This should be
43  * removed as soon as the affected platform's IRQ handling is fixed.
44  *
45  * app note sbaa036 talks in more detail about accurate sampling...
46  * that ought to help in situations like LCDs inducing noise (which
47  * can also be helped by using synch signals) and more generally.
48  * This driver tries to utilize the measures described in the app
49  * note. The strength of filtering can be set in the board-* specific
50  * files.
51  */
52 #define XPT2046_DEBUG                   0
53 #if XPT2046_DEBUG
54         #define xpt2046printk(msg...)   printk(msg);
55 #else
56         #define xpt2046printk(msg...)
57 #endif
58
59 #define TS_POLL_DELAY   (10)    /* ns delay before the first sample */
60 #define TS_POLL_PERIOD  (20)    /* ns delay between samples */
61
62 /* this driver doesn't aim at the peak continuous sample rate */
63 #define SAMPLE_BITS     (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
64
65 struct ts_event {
66         /* For portability, we can't read 12 bit values using SPI (which
67          * would make the controller deliver them as native byteorder u16
68          * with msbs zeroed).  Instead, we read them as two 8-bit values,
69          * *** WHICH NEED BYTESWAPPING *** and range adjustment.
70          */
71         u16     x;
72         u16     y;
73         int     ignore;
74 };
75
76 /*
77  * We allocate this separately to avoid cache line sharing issues when
78  * driver is used with DMA-based SPI controllers (like atmel_spi) on
79  * systems where main memory is not DMA-coherent (most non-x86 boards).
80  */
81 struct xpt2046_packet {
82         u8                      read_x, read_y, pwrdown;
83         u16                     dummy;          /* for the pwrdown read */
84         struct ts_event         tc;
85 };
86
87 struct xpt2046 {
88         struct input_dev        *input;
89         char    phys[32];
90         char    name[32];
91         char    pendown_iomux_name[IOMUX_NAME_SIZE];    
92         struct spi_device       *spi;
93
94         u16             model;
95         u16             x_min, x_max;   
96         u16             y_min, y_max; 
97         u16             debounce_max;
98         u16             debounce_tol;
99         u16             debounce_rep;
100         u16             penirq_recheck_delay_usecs;
101         bool    swap_xy;
102
103         struct xpt2046_packet   *packet;
104
105         struct spi_transfer     xfer[18];
106         struct spi_message      msg[5];
107         struct spi_message      *last_msg;
108         int             msg_idx;
109         int             read_cnt;
110         int             read_rep;
111         int             last_read;
112         int             pendown_iomux_mode;     
113         int     touch_ad_top;
114         int     touch_ad_bottom;
115         int     touch_ad_left;
116         int     touch_ad_right;
117         int     touch_virtualkey_length;
118         spinlock_t              lock;
119         struct delayed_work      d_work;
120         struct tslib_info tslib_info;
121         unsigned                pendown:1;      /* P: lock */
122         unsigned                pending:1;      /* P: lock */
123 // FIXME remove "irq_disabled"
124         unsigned                irq_disabled:1; /* P: lock */
125         unsigned                disabled:1;
126         unsigned                is_suspended:1;
127
128         int                     (*filter)(void *data, int data_idx, int *val);
129         void            *filter_data;
130         void            (*filter_cleanup)(void *data);
131         int                     (*get_pendown_state)(void);
132         int                     gpio_pendown;
133
134         void            (*wait_for_sync)(void);
135 #ifdef CONFIG_HAS_EARLYSUSPEND
136         struct early_suspend early_suspend;
137 #endif
138 };
139
140 /* leave chip selected when we're done, for quicker re-select? */
141 #if     0
142 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
143 #else
144 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
145 #endif
146
147 /*--------------------------------------------------------------------------*/
148
149 /* The xpt2046 has touchscreen and other sensors.
150  * Earlier xpt2046 chips are somewhat compatible.
151  */
152 #define XPT2046_START                   (1 << 7)
153 #define XPT2046_A2A1A0_d_y              (1 << 4)        /* differential */
154 #define XPT2046_A2A1A0_d_z1             (3 << 4)        /* differential */
155 #define XPT2046_A2A1A0_d_z2             (4 << 4)        /* differential */
156 #define XPT2046_A2A1A0_d_x              (5 << 4)        /* differential */
157 #define XPT2046_A2A1A0_temp0    (0 << 4)        /* non-differential */
158 #define XPT2046_A2A1A0_vbatt    (2 << 4)        /* non-differential */
159 #define XPT2046_A2A1A0_vaux             (6 << 4)        /* non-differential */
160 #define XPT2046_A2A1A0_temp1    (7 << 4)        /* non-differential */
161 #define XPT2046_8_BIT                   (1 << 3)
162 #define XPT2046_12_BIT                  (0 << 3)
163 #define XPT2046_SER                             (1 << 2)        /* non-differential */
164 #define XPT2046_DFR                             (0 << 2)        /* differential */
165 #define XPT2046_PD10_PDOWN              (0 << 0)        /* lowpower mode + penirq */
166 #define XPT2046_PD10_ADC_ON             (1 << 0)        /* ADC on */
167 #define XPT2046_PD10_REF_ON             (2 << 0)        /* vREF on + penirq */
168 #define XPT2046_PD10_ALL_ON             (3 << 0)        /* ADC + vREF on */
169
170 #define MAX_12BIT       ((1<<12)-1)
171
172 /* leave ADC powered up (disables penirq) between differential samples */
173 #define READ_12BIT_DFR(x, adc, vref) (XPT2046_START | XPT2046_A2A1A0_d_ ## x \
174         | XPT2046_12_BIT | XPT2046_DFR | \
175         (adc ? XPT2046_PD10_ADC_ON : 0) | (vref ? XPT2046_PD10_REF_ON : 0))
176
177 #define READ_Y(vref)    (READ_12BIT_DFR(y,  1, vref))
178 #define READ_Z1(vref)   (READ_12BIT_DFR(z1, 1, vref))
179 #define READ_Z2(vref)   (READ_12BIT_DFR(z2, 1, vref))
180
181 #define READ_X(vref)    (READ_12BIT_DFR(x,  1, vref))
182 #define PWRDOWN         (READ_12BIT_DFR(y,  0, 0))      /* LAST */
183
184 /* single-ended samples need to first power up reference voltage;
185  * we leave both ADC and VREF powered
186  */
187 #define READ_12BIT_SER(x) (XPT2046_START | XPT2046_A2A1A0_ ## x \
188         | XPT2046_12_BIT | XPT2046_SER)
189
190 #define REF_ON  (READ_12BIT_DFR(x, 1, 1))
191 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
192
193 /*--------------------------------------------------------------------------*/
194 /*
195  * touchscreen sensors  use differential conversions.
196  */
197
198 struct dfr_req {
199         u8                      command;
200         u8                      pwrdown;
201         u16                     dummy;          /* for the pwrdown read */
202         __be16                  sample;
203         struct spi_message      msg;
204         struct spi_transfer     xfer[4];
205 };
206 static DECLARE_WAIT_QUEUE_HEAD(wq_ts);
207 static int wq_condition_ts = 1;
208
209 static void xpt2046_enable(struct xpt2046 *ts);
210 static void xpt2046_disable(struct xpt2046 *ts);
211 static int xpt2046_verifyAndConvert(struct xpt2046 *ts,int adx, int ady,int *x, int *y)
212 {
213         *x = ts->x_max * (ts->touch_ad_left - adx)/(ts->touch_ad_left - ts->touch_ad_right);
214     *y = ts->y_max * (ts->touch_ad_top - ady)/(ts->touch_ad_top - ts->touch_ad_bottom);
215
216         xpt2046printk("%s:(%d/%d)\n",__FUNCTION__,*x, *y);
217         
218         if((*x< ts->x_min) || (*x > ts->x_max))
219                 return 1;
220
221         if((*y< ts->y_min) || (*y > ts->y_max + ts->touch_virtualkey_length))
222                 return 1;
223
224
225         return 0;
226 }
227 static int device_suspended(struct device *dev)
228 {
229         struct xpt2046 *ts = dev_get_drvdata(dev);
230         return ts->is_suspended || ts->disabled;
231 }
232
233 static int xpt2046_read12_dfr(struct device *dev, unsigned command)
234 {
235         struct spi_device       *spi = to_spi_device(dev);
236         struct xpt2046          *ts = dev_get_drvdata(dev);
237         struct dfr_req          *req = kzalloc(sizeof *req, GFP_KERNEL);
238         int                     status;
239
240         if (!req)
241                 return -ENOMEM;
242
243         spi_message_init(&req->msg);
244
245         /* take sample */
246         req->command = (u8) command;
247         req->xfer[0].tx_buf = &req->command;
248         req->xfer[0].len = 1;
249         spi_message_add_tail(&req->xfer[0], &req->msg);
250
251         req->xfer[1].rx_buf = &req->sample;
252         req->xfer[1].len = 2;
253         spi_message_add_tail(&req->xfer[1], &req->msg);
254
255         /* converter in low power mode & enable PENIRQ */
256         req->pwrdown= PWRDOWN;
257         req->xfer[2].tx_buf = &req->pwrdown;
258         req->xfer[2].len = 1;
259         spi_message_add_tail(&req->xfer[2], &req->msg);
260
261         req->xfer[3].rx_buf = &req->dummy;
262         req->xfer[3].len = 2;
263         CS_CHANGE(req->xfer[3]);
264         spi_message_add_tail(&req->xfer[3], &req->msg);
265
266         ts->irq_disabled = 1;
267         disable_irq(spi->irq);
268         status = spi_sync(spi, &req->msg);
269         ts->irq_disabled = 0;
270         enable_irq(spi->irq);
271         
272         if (status == 0) {
273                 /* on-wire is a must-ignore bit, a BE12 value, then padding */
274                 status = be16_to_cpu(req->sample);
275                 status = status >> 3;
276                 status &= 0x0fff;
277                 xpt2046printk("***>%s:status=%d\n",__FUNCTION__,status);
278         }
279
280         kfree(req);
281         return status;
282 }
283
284
285
286 /*--------------------------------------------------------------------------*/
287
288 static int get_pendown_state(struct xpt2046 *ts)
289 {
290         if (ts->get_pendown_state)
291                 return ts->get_pendown_state();
292
293         return !gpio_get_value(ts->gpio_pendown);
294 }
295
296 static void null_wait_for_sync(void)
297 {
298         
299 }
300
301 /*
302  * PENIRQ only kicks the timer.  The timer only reissues the SPI transfer,
303  * to retrieve touchscreen status.
304  *
305  * The SPI transfer completion callback does the real work.  It reports
306  * touchscreen events and reactivates the timer (or IRQ) as appropriate.
307  */
308 static void xpt2046_rx(void *xpt)
309 {
310         /* xpt2046_rx_val() did in-place conversion (including byteswap) from
311          * on-the-wire format as part of debouncing to get stable readings.
312          */
313         wq_condition_ts= 1;
314         wake_up(&wq_ts);
315
316     return;
317 }
318
319 static int xpt2046_debounce(void *xpt, int data_idx, int *val)
320 {
321         struct xpt2046          *ts = xpt;
322         static int average_val[2];
323         
324
325         xpt2046printk("***>%s:%d,%d,%d,%d,%d,%d,%d,%d\n",__FUNCTION__,
326                 data_idx,ts->last_read,
327           ts->read_cnt,ts->debounce_max,
328                 abs(ts->last_read - *val),ts->debounce_tol,
329                 ts->read_rep,ts->debounce_rep);
330         
331         /* discard the first sample. */
332          //on info_it50, the top-left area(1cmx1cm top-left square ) is not responding cause the first sample is invalid, @sep 17th
333         if(!ts->read_cnt)
334         {
335                 //udelay(100);
336                 ts->read_cnt++;
337                 return XPT2046_FILTER_REPEAT;
338         }
339         if(*val == 4095 || *val == 0)
340         {
341                 ts->read_cnt = 0;
342                 ts->last_read = 0;
343                 memset(average_val,0,sizeof(average_val));
344                 xpt2046printk("***>%s:*val == 4095 || *val == 0\n",__FUNCTION__);
345                 return XPT2046_FILTER_IGNORE;
346         }
347         /* discard the first sample. */
348 /*      if(!ts->read_cnt)
349         {
350                 ts->read_cnt++;
351                 return XPT2046_FILTER_REPEAT;
352         }
353 move discard ahead  */
354
355         if (ts->read_cnt==1 || (abs(ts->last_read - *val) > ts->debounce_tol)) {
356                 /* Start over collecting consistent readings. */
357                 ts->read_rep = 1;
358                 average_val[data_idx] = *val;
359                 /* Repeat it, if this was the first read or the read
360                  * wasn't consistent enough. */
361                 if (ts->read_cnt < ts->debounce_max) {
362                         ts->last_read = *val;
363                         ts->read_cnt++;
364                         return XPT2046_FILTER_REPEAT;
365                 } else {
366                         /* Maximum number of debouncing reached and still
367                          * not enough number of consistent readings. Abort
368                          * the whole sample, repeat it in the next sampling
369                          * period.
370                          */
371                         ts->read_cnt = 0;
372                         ts->last_read = 0;
373                         memset(average_val,0,sizeof(average_val));
374                         xpt2046printk("***>%s:XPT2046_FILTER_IGNORE\n",__FUNCTION__);
375                         return XPT2046_FILTER_IGNORE;
376                 }
377         } 
378         else {
379                 average_val[data_idx] += *val;
380                 
381                 if (++ts->read_rep >= ts->debounce_rep) {
382                         /* Got a good reading for this coordinate,
383                          * go for the next one. */
384                         ts->read_cnt = 0;
385                         ts->read_rep = 0;
386                         ts->last_read = 0;
387                         *val = average_val[data_idx]/(ts->debounce_rep);
388                         return XPT2046_FILTER_OK;
389                 } else {
390                         /* Read more values that are consistent. */
391                         ts->read_cnt++;
392                         
393                         return XPT2046_FILTER_REPEAT;
394                 }
395         }
396 }
397
398 static int xpt2046_no_filter(void *xpt, int data_idx, int *val)
399 {
400         return XPT2046_FILTER_OK;
401 }
402
403 static void xpt2046_rx_val(void *xpt)
404 {
405         struct xpt2046 *ts = xpt;
406         struct xpt2046_packet *packet = ts->packet;
407         struct spi_message *m;
408         struct spi_transfer *t;
409         int val;
410         int action;
411         int status;
412         
413         m = &ts->msg[ts->msg_idx];
414         t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
415
416         /* adjust:  on-wire is a must-ignore bit, a BE12 value, then padding;
417          * built from two 8 bit values written msb-first.
418          */
419         val = (be16_to_cpup((__be16 *)t->rx_buf) >> 3) & 0x0fff;
420
421         xpt2046printk("***>%s:value=%d\n",__FUNCTION__,val);
422         
423         action = ts->filter(ts->filter_data, ts->msg_idx, &val);
424         switch (action) {
425         case XPT2046_FILTER_REPEAT:
426                 break;
427         case XPT2046_FILTER_IGNORE:
428                 packet->tc.ignore = 1;
429                 /* Last message will contain xpt2046_rx() as the
430                  * completion function.
431                  */
432                 m = ts->last_msg;
433                 break;
434         case XPT2046_FILTER_OK:
435                 *(u16 *)t->rx_buf = val;
436                 packet->tc.ignore = 0;
437                 m = &ts->msg[++ts->msg_idx];
438                 break;
439         default:
440                 BUG();
441         }
442         ts->wait_for_sync();
443         status = spi_async(ts->spi, m);
444         if (status)
445                 dev_err(&ts->spi->dev, "spi_async --> %d\n",
446                                 status);
447 }
448
449 int xpt2046_get_raw_data(struct tslib_info *info, struct ts_sample *samp, int nr)
450 {
451                 
452                 struct xpt2046 *ts = container_of((void *)info, struct xpt2046, tslib_info);    
453                 struct xpt2046_packet   *packet = ts->packet;
454                 int             status = 0;
455                 memset(samp, 0, sizeof(struct ts_sample)*nr);
456                 
457                 wq_condition_ts = 0;
458                 
459                 ts->msg_idx = 0;
460                 ts->wait_for_sync();
461                 status = spi_async(ts->spi, &ts->msg[0]);
462                 if (status)
463                         dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
464                 
465                 xpt2046printk("xpt2046_get_raw_data:wait_event \n");
466                 if(!wait_event_timeout(wq_ts, wq_condition_ts, msecs_to_jiffies(100))) 
467                 {
468                         samp->x = 4095;
469                         samp->y = 4095;
470                         samp->pressure = 0;
471                         return 1;
472                 }
473                 
474                 samp->x = packet->tc.x;
475                 samp->y = packet->tc.y;
476                 samp->pressure = !(packet->tc.ignore);
477                 //xpt2046printk("xpt2046_get_raw_data:samp->x=%d,samp->y=%d,samp->pressure=%d \n",
478                         //samp->x,samp->y,samp->pressure);
479                 return 1;
480 }
481
482 static void xpt2046_report(struct xpt2046       *ts)
483 {
484         struct ts_sample samp[3];       
485         struct xpt2046_packet   *packet = ts->packet;
486         int ret,Rt=1;
487         int x = 0,y =0,z = 0;       
488         
489         ret = dejitter_read(&ts->tslib_info, samp, 1);
490
491         xpt2046printk("***>%s:ret=%d,samp[0].x=%d,samp[0].y=%d,samp[0].pressure=%d\n",
492                 __FUNCTION__,ret,samp[0].x,samp[0].y,samp[0].pressure);
493         if(ret == 1){
494                 x = samp[0].x;
495                 y = samp[0].y;
496                 z = samp[0].pressure;           
497         }
498         else if(ret == 2){
499                 x = (samp[0].x + samp[1].x) / 2;
500                 y = (samp[0].y + samp[1].y) / 2;
501                 z = (samp[0].pressure + samp[1].pressure) / 2;
502         }
503         else if(ret ==3){
504                 x = (samp[0].x + samp[1].x + samp[2].x)/3;
505                 y = (samp[0].y + samp[1].y + samp[2].y)/3;
506                 z = (samp[0].pressure + samp[1].pressure + samp[2].pressure)/3;
507         }
508         else{
509                 printk("no sampe\n");
510                 goto out;
511         }
512
513         if(z == 0)
514         {
515                 xpt2046printk("***>%s:z == 0\n",__FUNCTION__);
516                 goto out;
517         }
518
519         /* range filtering */
520         if (x == MAX_12BIT)
521                 x = 0;
522
523         /* Sample found inconsistent by debouncing or pressure is beyond
524          * the maximum. Don't report it to user space, repeat at least
525          * once more the measurement
526          */
527         if (packet->tc.ignore) {
528                 xpt2046printk("***>%s:ignored=%d\n",__FUNCTION__,packet->tc.ignore);
529                 schedule_delayed_work(&ts->d_work, msecs_to_jiffies(TS_POLL_PERIOD));
530                 return;
531         }
532
533         /* Maybe check the pendown state before reporting. This discards
534          * false readings when the pen is lifted.
535          */
536         if (ts->penirq_recheck_delay_usecs) {
537                 udelay(ts->penirq_recheck_delay_usecs);
538                 if (!get_pendown_state(ts))
539                 {
540                         xpt2046printk("***>%s:get_pendown_state(ts)==0,discard false reading\n",__FUNCTION__);
541                         Rt = 0;
542                 }
543         }
544
545         /* NOTE: We can't rely on the pressure to determine the pen down
546          * state, even this controller has a pressure sensor.  The pressure
547          * value can fluctuate for quite a while after lifting the pen and
548          * in some cases may not even settle at the expected value.
549          *
550          * The only safe way to check for the pen up condition is in the
551          * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
552          */
553         if (Rt) {
554                 struct input_dev *input = ts->input;
555                 
556                 if (ts->swap_xy)
557                         swap(x, y);     
558                 if(xpt2046_verifyAndConvert(ts,x,y,&x,&y))
559                 {
560                         xpt2046printk("***>%s:xpt2046_verifyAndConvert fail\n",__FUNCTION__);
561                         goto out;
562                 }
563                 
564                 if (!ts->pendown) {
565                         input_report_key(input, BTN_TOUCH, 1);
566                         ts->pendown = 1;
567                         xpt2046printk("***>%s:input_report_key(pen down)\n",__FUNCTION__);
568                 }
569                 
570                 input_report_abs(input, ABS_X, x);
571                 input_report_abs(input, ABS_Y, y);
572
573                 input_sync(input);
574                 xpt2046printk("***>%s:input_report_abs(%4d/%4d)\n",__FUNCTION__,x, y);
575         }
576         
577 out:
578         schedule_delayed_work(&ts->d_work, msecs_to_jiffies(TS_POLL_PERIOD));
579
580         return;
581 }
582
583 static void xpt2046_dwork_handler(struct work_struct *work)
584 {
585         struct xpt2046  *ts = (struct xpt2046 *)container_of(work, struct xpt2046, d_work.work);
586         
587         spin_lock(&ts->lock);
588
589         if (unlikely(!get_pendown_state(ts) ||
590                      device_suspended(&ts->spi->dev))) {
591                 if (ts->pendown) {
592                         struct input_dev *input = ts->input;
593                         variance_clear(&ts->tslib_info);
594                         input_report_key(input, BTN_TOUCH, 0);
595                         input_sync(input);
596
597                         ts->pendown = 0;
598                         
599                         xpt2046printk("***>%s:input_report_key(The touchscreen up)\n",__FUNCTION__);
600                 }
601
602                 /* measurement cycle ended */
603                 if (!device_suspended(&ts->spi->dev)) {
604                         xpt2046printk("***>%s:device_suspended==0\n",__FUNCTION__);
605                         ts->irq_disabled = 0;
606                         enable_irq(ts->spi->irq);
607                 }
608                 ts->pending = 0;
609         } else {
610                 /* pen is still down, continue with the measurement */
611                 xpt2046printk("***>%s:pen is still down, continue with the measurement\n",__FUNCTION__);
612                 spin_unlock(&ts->lock);
613                 xpt2046_report(ts);
614                 return;
615         }
616
617         spin_unlock(&ts->lock);
618         return;
619 }
620 static irqreturn_t xpt2046_irq(int irq, void *handle)
621 {
622         struct xpt2046 *ts = handle;
623         unsigned long flags;
624         
625         xpt2046printk("***>%s.....%s.....%d\n",__FILE__,__FUNCTION__,__LINE__);
626         
627         spin_lock_irqsave(&ts->lock, flags);
628
629         if (likely(get_pendown_state(ts))) {
630                 if (!ts->irq_disabled) {
631                         /* The ARM do_simple_IRQ() dispatcher doesn't act
632                          * like the other dispatchers:  it will report IRQs
633                          * even after they've been disabled.  We work around
634                          * that here.  (The "generic irq" framework may help...)
635                          */
636                         ts->irq_disabled = 1;
637                         disable_irq_nosync(ts->spi->irq);
638                         ts->pending = 1;
639                         schedule_delayed_work(&ts->d_work, msecs_to_jiffies(TS_POLL_DELAY));
640                 }
641         }
642         spin_unlock_irqrestore(&ts->lock, flags);
643
644         return IRQ_HANDLED;
645 }
646
647 /*--------------------------------------------------------------------------*/
648
649 /* Must be called with ts->lock held */
650 static void xpt2046_disable(struct xpt2046 *ts)
651 {
652         if (ts->disabled)
653                 return;
654
655         ts->disabled = 1;
656
657         /* are we waiting for IRQ, or polling? */
658         if (!ts->pending) {
659                 ts->irq_disabled = 1;
660                 disable_irq(ts->spi->irq);
661         } else {
662                 /* the timer will run at least once more, and
663                  * leave everything in a clean state, IRQ disabled
664                  */
665                 while (ts->pending) {
666                         spin_unlock_irq(&ts->lock);
667                         msleep(1);
668                         spin_lock_irq(&ts->lock);
669                 }
670         }
671
672         /* we know the chip's in lowpower mode since we always
673          * leave it that way after every request
674          */
675 }
676
677 /* Must be called with ts->lock held */
678 static void xpt2046_enable(struct xpt2046 *ts)
679 {
680         if (!ts->disabled)
681                 return;
682
683         ts->disabled = 0;
684         ts->irq_disabled = 0;
685         enable_irq(ts->spi->irq);
686 }
687
688 static int xpt2046_pSuspend(struct xpt2046 *ts)
689 {
690         spin_lock_irq(&ts->lock);
691
692         ts->is_suspended = 1;
693         xpt2046_disable(ts);
694                 
695         spin_unlock_irq(&ts->lock);
696
697         return 0;
698 }
699
700 static int xpt2046_pResume(struct xpt2046 *ts)
701 {
702         spin_lock_irq(&ts->lock);
703
704         ts->is_suspended = 0;
705         xpt2046_enable(ts);
706
707         spin_unlock_irq(&ts->lock);
708
709         return 0;
710 }
711
712 #if !defined(CONFIG_HAS_EARLYSUSPEND)
713 static int xpt2046_suspend(struct spi_device *spi, pm_message_t message)
714 {
715         struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
716         
717         printk("xpt2046_suspend\n");
718         
719         xpt2046_pSuspend(ts);
720         
721         return 0;
722 }
723
724 static int xpt2046_resume(struct spi_device *spi)
725 {
726         struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
727         
728         printk("xpt2046_resume\n");
729         
730         xpt2046_pResume(ts);
731
732         return 0;
733 }
734
735 #elif defined(CONFIG_HAS_EARLYSUSPEND)
736 static void xpt2046_early_suspend(struct early_suspend *h)
737 {
738         struct xpt2046  *ts;
739     ts = container_of(h, struct xpt2046, early_suspend);
740         
741         printk("xpt2046_suspend early\n");
742         
743         xpt2046_pSuspend(ts);
744         
745         return;
746 }
747
748 static void xpt2046_late_resume(struct early_suspend *h)
749 {
750         struct xpt2046  *ts;
751     ts = container_of(h, struct xpt2046, early_suspend);
752         
753         printk("xpt2046_resume late\n");
754         
755         xpt2046_pResume(ts);
756
757         return;
758 }
759 #endif
760
761 static int __devinit setup_pendown(struct spi_device *spi, struct xpt2046 *ts)
762 {
763         struct xpt2046_platform_data *pdata = spi->dev.platform_data;
764         int err;
765
766         /* REVISIT when the irq can be triggered active-low, or if for some
767          * reason the touchscreen isn't hooked up, we don't need to access
768          * the pendown state.
769          */
770         if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
771                 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
772                 return -EINVAL;
773         }
774
775         if (pdata->get_pendown_state) {
776                 ts->get_pendown_state = pdata->get_pendown_state;
777                 return 0;
778         }
779         
780     if (pdata->io_init) {
781         err = pdata->io_init();
782         if (err)
783             dev_err(&spi->dev, "xpt2046 io_init fail\n");
784     }
785         
786         ts->gpio_pendown = pdata->gpio_pendown;
787         strcpy(ts->pendown_iomux_name,pdata->pendown_iomux_name);
788         ts->pendown_iomux_mode = pdata->pendown_iomux_mode;
789         
790     rk29_mux_api_set(ts->pendown_iomux_name,pdata->pendown_iomux_mode);
791         err = gpio_request(pdata->gpio_pendown, "xpt2046_pendown");
792         if (err) {
793                 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
794                                 pdata->gpio_pendown);
795                 return err;
796         }
797         
798         err = gpio_pull_updown(pdata->gpio_pendown, GPIOPullUp);
799         if (err) {
800                 dev_err(&spi->dev, "failed to pullup pendown GPIO%d\n",
801                                 pdata->gpio_pendown);
802                 return err;
803         }
804         ts->gpio_pendown = pdata->gpio_pendown;
805         return 0;
806 }
807
808 static int __devinit xpt2046_probe(struct spi_device *spi)
809 {
810         struct xpt2046                  *ts;
811         struct xpt2046_packet           *packet;
812         struct input_dev                *input_dev;
813         struct xpt2046_platform_data    *pdata = spi->dev.platform_data;
814         struct spi_message              *m;
815         struct spi_transfer             *x;
816         int                             vref;
817         int                             err;
818         
819         if (!spi->irq) {
820                 dev_dbg(&spi->dev, "no IRQ?\n");
821                 return -ENODEV;
822         }
823         else{
824                 spi->irq = gpio_to_irq(spi->irq);
825                 dev_dbg(&spi->dev, "no IRQ?\n");
826         }
827             
828     if (!pdata) {
829                 dev_err(&spi->dev, "empty platform_data\n");
830                 return -EFAULT;
831     }
832     
833         /* don't exceed max specified sample rate */
834         if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
835                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
836                                 (spi->max_speed_hz/SAMPLE_BITS)/1000);
837                 return -EINVAL;
838         }
839
840         /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
841          * that even if the hardware can do that, the SPI controller driver
842          * may not.  So we stick to very-portable 8 bit words, both RX and TX.
843          */
844         spi->bits_per_word = 8;
845         spi->mode = SPI_MODE_0;
846         err = spi_setup(spi);
847         if (err < 0)
848                 return err;
849
850         ts = kzalloc(sizeof(struct xpt2046), GFP_KERNEL);
851         packet = kzalloc(sizeof(struct xpt2046_packet), GFP_KERNEL);
852         input_dev = input_allocate_device();
853         if (!ts || !packet || !input_dev) {
854                 err = -ENOMEM;
855                 goto err_free_mem;
856         }
857
858         dev_set_drvdata(&spi->dev, ts);
859
860         ts->packet = packet;
861         ts->spi = spi;
862         ts->input = input_dev;
863         ts->swap_xy = pdata->swap_xy;
864         
865         INIT_DELAYED_WORK(&ts->d_work, xpt2046_dwork_handler);
866         
867         tslib_init(&ts->tslib_info, xpt2046_get_raw_data);
868
869         spin_lock_init(&ts->lock);
870
871         ts->model = pdata->model ? : 2046;
872
873         if (pdata->filter != NULL) {
874                 if (pdata->filter_init != NULL) {
875                         err = pdata->filter_init(pdata, &ts->filter_data);
876                         if (err < 0)
877                                 goto err_free_mem;
878                 }
879                 ts->filter = pdata->filter;
880                 ts->filter_cleanup = pdata->filter_cleanup;
881         } else if (pdata->debounce_max) {
882                 ts->debounce_max = pdata->debounce_max;
883                 if (ts->debounce_max < pdata->debounce_rep)
884                         ts->debounce_max = pdata->debounce_rep;
885                 ts->debounce_tol = pdata->debounce_tol;
886                 ts->debounce_rep = pdata->debounce_rep;
887                 ts->filter = xpt2046_debounce;
888                 ts->filter_data = ts;
889         } else
890                 ts->filter = xpt2046_no_filter;
891
892         err = setup_pendown(spi, ts);
893         if (err)
894                 goto err_cleanup_filter;
895
896         if (pdata->penirq_recheck_delay_usecs)
897                 ts->penirq_recheck_delay_usecs =
898                                 pdata->penirq_recheck_delay_usecs;
899
900         ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
901         ts->x_min = pdata->x_min;
902         ts->x_max = pdata->x_max;
903         ts->y_min = pdata->y_min;
904         ts->y_max = pdata->y_max;
905         
906         ts->touch_ad_top = pdata->touch_ad_top;
907         ts->touch_ad_bottom= pdata->touch_ad_bottom;
908         ts->touch_ad_left= pdata->touch_ad_left;
909         ts->touch_ad_right= pdata->touch_ad_right;
910         
911         ts->touch_virtualkey_length = pdata->touch_virtualkey_length;
912         
913         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
914         snprintf(ts->name, sizeof(ts->name), "xpt%d-touchscreen", ts->model);
915
916         input_dev->name = ts->name;
917         input_dev->phys = ts->phys;
918         input_dev->dev.parent = &spi->dev;
919
920         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
921         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
922         input_set_abs_params(input_dev, ABS_X,
923                         ts->x_min ? : 0,
924                         ts->x_max ? : MAX_12BIT,
925                         0, 0);
926         input_set_abs_params(input_dev, ABS_Y,
927                         ts->y_min ? : 0,
928                         ts->y_max ? : MAX_12BIT,
929                         0, 0);
930         
931         vref = pdata->keep_vref_on;
932
933         /* set up the transfers to read touchscreen state; this assumes we
934          * use formula #2 for pressure, not #3.
935          */
936         m = &ts->msg[0];
937         x = ts->xfer;
938
939         spi_message_init(m);
940
941         /* y- still on; turn on only y+ (and ADC) */
942         packet->read_y = READ_Y(vref);
943         x->tx_buf = &packet->read_y;
944         x->len = 1;
945         spi_message_add_tail(x, m);
946
947         x++;
948         x->rx_buf = &packet->tc.y;
949         x->len = 2;
950         spi_message_add_tail(x, m);
951
952         m->complete = xpt2046_rx_val;
953         m->context = ts;
954
955         m++;
956         spi_message_init(m);
957
958         /* turn y- off, x+ on, then leave in lowpower */
959         x++;
960         packet->read_x = READ_X(vref);
961         x->tx_buf = &packet->read_x;
962         x->len = 1;
963         spi_message_add_tail(x, m);
964
965         x++;
966         x->rx_buf = &packet->tc.x;
967         x->len = 2;
968         spi_message_add_tail(x, m);
969
970         m->complete = xpt2046_rx_val;
971         m->context = ts;
972
973         /* power down */
974         m++;
975         spi_message_init(m);
976
977         x++;
978         packet->pwrdown = PWRDOWN;
979         x->tx_buf = &packet->pwrdown;
980         x->len = 1;
981         spi_message_add_tail(x, m);
982
983         x++;
984         x->rx_buf = &packet->dummy;
985         x->len = 2;
986         CS_CHANGE(*x);
987         spi_message_add_tail(x, m);
988
989         m->complete = xpt2046_rx;
990         m->context = ts;
991
992         ts->last_msg = m;
993
994         if (request_irq(spi->irq, xpt2046_irq, IRQF_TRIGGER_FALLING,
995                         spi->dev.driver->name, ts)) {
996                 xpt2046printk("%s:trying pin change workaround on irq %d\n",__FUNCTION__,spi->irq);
997                 err = request_irq(spi->irq, xpt2046_irq,
998                                   IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
999                                   spi->dev.driver->name, ts);
1000                 if (err) {
1001                         dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
1002                         goto err_free_gpio;
1003                 }
1004         }
1005         xpt2046printk("***>%s:touchscreen irq %d\n",__FUNCTION__,spi->irq);
1006         
1007         /* take a first sample, leaving nPENIRQ active and vREF off; avoid
1008          * the touchscreen, in case it's not connected.
1009          */
1010         xpt2046_read12_dfr(&spi->dev,READ_X(1));
1011
1012         err = input_register_device(input_dev);
1013         if (err)
1014                 goto err_remove_attr_group;
1015         
1016 #ifdef CONFIG_HAS_EARLYSUSPEND
1017         ts->early_suspend.suspend = xpt2046_early_suspend;
1018         ts->early_suspend.resume = xpt2046_late_resume;
1019         ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1020         register_early_suspend(&ts->early_suspend);
1021 #endif
1022
1023     xpt2046printk("xpt2046_ts: driver initialized\n");
1024         return 0;
1025
1026  err_remove_attr_group:
1027         free_irq(spi->irq, ts);
1028  err_free_gpio:
1029         if (ts->gpio_pendown != -1)
1030                 gpio_free(ts->gpio_pendown);
1031  err_cleanup_filter:
1032         if (ts->filter_cleanup)
1033                 ts->filter_cleanup(ts->filter_data);
1034  err_free_mem:
1035         input_free_device(input_dev);
1036         kfree(packet);
1037         kfree(ts);
1038         return err;
1039 }
1040
1041 static int __devexit xpt2046_remove(struct spi_device *spi)
1042 {
1043         struct xpt2046          *ts = dev_get_drvdata(&spi->dev);
1044
1045         input_unregister_device(ts->input);
1046         
1047 #if !defined(CONFIG_HAS_EARLYSUSPEND)
1048         xpt2046_suspend(spi, PMSG_SUSPEND);
1049 #elif defined(CONFIG_HAS_EARLYSUSPEND)
1050         xpt2046_early_suspend(&ts->early_suspend);
1051         unregister_early_suspend(&ts->early_suspend);
1052 #endif
1053
1054         free_irq(ts->spi->irq, ts);
1055         /* suspend left the IRQ disabled */
1056         enable_irq(ts->spi->irq);
1057
1058         if (ts->gpio_pendown != -1)
1059                 gpio_free(ts->gpio_pendown);
1060
1061         if (ts->filter_cleanup)
1062                 ts->filter_cleanup(ts->filter_data);
1063
1064         kfree(ts->packet);
1065         kfree(ts);
1066
1067         dev_dbg(&spi->dev, "unregistered touchscreen\n");
1068         return 0;
1069 }
1070
1071 static struct spi_driver xpt2046_driver = {
1072         .driver = {
1073                 .name   = "xpt2046_ts",
1074                 .bus    = &spi_bus_type,
1075                 .owner  = THIS_MODULE,
1076         },
1077         .probe          = xpt2046_probe,
1078         .remove         = __devexit_p(xpt2046_remove),
1079 #ifndef CONFIG_HAS_EARLYSUSPEND
1080         .suspend        = xpt2046_suspend,
1081         .resume         = xpt2046_resume,
1082 #endif
1083 };
1084
1085 static int __init xpt2046_init(void)
1086 {
1087         return spi_register_driver(&xpt2046_driver);
1088 }
1089 module_init(xpt2046_init);
1090
1091 static void __exit xpt2046_exit(void)
1092 {
1093         spi_unregister_driver(&xpt2046_driver);
1094 }
1095 module_exit(xpt2046_exit);
1096
1097 MODULE_DESCRIPTION("rk29xx spi xpt2046 TouchScreen Driver");
1098 MODULE_LICENSE("GPL");
1099 MODULE_ALIAS("spi:xpt2046");