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