adapt all kind of board
[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_packet   *packet = ts->packet;
347         unsigned                Rt = 1;
348         u16                     x, y;
349         u8                      i;
350         /* xpt2046_rx_val() did in-place conversion (including byteswap) from
351          * on-the-wire format as part of debouncing to get stable readings.
352          */
353         x = packet->tc.x;
354         y = packet->tc.y;
355
356         xpt2046printk("***>%s:x=%d,y=%d\n",__FUNCTION__,x,y);
357
358         /* range filtering */
359         if (x == MAX_12BIT)
360                 x = 0;
361
362         /* Sample found inconsistent by debouncing or pressure is beyond
363          * the maximum. Don't report it to user space, repeat at least
364          * once more the measurement
365          */
366         if (packet->tc.ignore) {
367
368                 xpt2046printk("***>%s:ignored=%d\n",__FUNCTION__,packet->tc.ignore);
369         
370                 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
371                               HRTIMER_MODE_REL);
372                 return;
373         }
374
375         /* Maybe check the pendown state before reporting. This discards
376          * false readings when the pen is lifted.
377          */
378         if (ts->penirq_recheck_delay_usecs) {
379                 udelay(ts->penirq_recheck_delay_usecs);
380                 if (!get_pendown_state(ts))
381                 {
382                         xpt2046printk("***>%s:get_pendown_state(ts)==0,discard false reading\n",__FUNCTION__);
383                         Rt = 0;
384                 }
385         }
386
387         /* NOTE: We can't rely on the pressure to determine the pen down
388          * state, even this controller has a pressure sensor.  The pressure
389          * value can fluctuate for quite a while after lifting the pen and
390          * in some cases may not even settle at the expected value.
391          *
392          * The only safe way to check for the pen up condition is in the
393          * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
394          */
395         if (Rt) {
396                 struct input_dev *input = ts->input;
397 /*              if (!ts->pendown) {
398                         input_report_key(input, BTN_TOUCH, 1);
399                         ts->pendown = 1;
400                         xpt2046printk("***>%s:input_report_key(pen down)\n",__FUNCTION__);
401                 }
402 */              
403                 x =  AD_TO_X(x);
404                 y =  AD_TO_Y(y);
405
406                 if (ts->swap_xy)
407                         swap(x, y);     
408
409                 //add by zhao
410                 if ((y > KEY_TOP) && (y < KEY_BOTTOM))
411                 {
412                         for (i = 0; i < ARRAY_SIZE(panel_key_info); i++)
413                         {
414                                 if ((x > panel_key_info[i].left) && (x < panel_key_info[i].right))
415                                 {
416                                         panel_key_info[i].status = 1;
417                                         input_report_key(input, panel_key_info[i].key,1);
418                                         input_sync(input);
419                                         xpt2046printk("***>%s:input_report_key(%2d)(%4d , %4d)\n",__FUNCTION__,panel_key_info[i].key, x, y);    
420                                 }
421                         }
422                                         
423                 }
424                 else if (y <= xpt2046_info.y_max)
425                 {
426                         if (!ts->pendown) {
427                                 input_report_key(input, BTN_TOUCH, 1);
428                                 ts->pendown = 1;
429                                 xpt2046printk("***>%s:input_report_key(pen down)\n",__FUNCTION__);
430                         }
431                         input_report_abs(input, ABS_X, x);
432                         input_report_abs(input, ABS_Y, y);
433                         input_sync(input);
434                         xpt2046printk("***>%s:input_report_abs(%4d , %4d)\n",__FUNCTION__,x, y);
435                 }
436                 
437         }
438
439         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
440                         HRTIMER_MODE_REL);
441 }
442
443 static int xpt2046_debounce(void *xpt, int data_idx, int *val)
444 {
445         struct xpt2046          *ts = xpt;
446         static int average_val[2];
447         
448
449 /*      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__,
450                 data_idx,ts->last_read,
451           ts->read_cnt,ts->debounce_max,
452                 abs(ts->last_read - *val),ts->debounce_tol,
453                 ts->read_rep,ts->debounce_rep);
454 */      
455         if(*val == 4095 || *val == 0)
456         {
457                 ts->read_cnt = 0;
458                 ts->last_read = 0;
459                 memset(average_val,0,sizeof(average_val));
460                 xpt2046printk("***>%s:*val == 4095 || *val == 0\n",__FUNCTION__);
461                 return XPT2046_FILTER_IGNORE;
462         }
463         /* discard the first sample. */
464         if(!ts->read_cnt)
465         {
466                 ts->read_cnt++;
467                 return XPT2046_FILTER_REPEAT;
468         }
469
470         if (ts->read_cnt==1 || (abs(ts->last_read - *val) > ts->debounce_tol)) {
471                 /* Start over collecting consistent readings. */
472                 ts->read_rep = 1;
473                 average_val[data_idx] = *val;
474                 /* Repeat it, if this was the first read or the read
475                  * wasn't consistent enough. */
476                 if (ts->read_cnt < ts->debounce_max) {
477                         ts->last_read = *val;
478                         ts->read_cnt++;
479                         return XPT2046_FILTER_REPEAT;
480                 } else {
481                         /* Maximum number of debouncing reached and still
482                          * not enough number of consistent readings. Abort
483                          * the whole sample, repeat it in the next sampling
484                          * period.
485                          */
486                         ts->read_cnt = 0;
487                         ts->last_read = 0;
488                         memset(average_val,0,sizeof(average_val));
489                         xpt2046printk("***>%s:XPT2046_FILTER_IGNORE\n",__FUNCTION__);
490                         return XPT2046_FILTER_IGNORE;
491                 }
492         } 
493         else {
494                 average_val[data_idx] += *val;
495                 
496                 if (++ts->read_rep >= ts->debounce_rep) {
497                         /* Got a good reading for this coordinate,
498                          * go for the next one. */
499                         ts->read_cnt = 0;
500                         ts->read_rep = 0;
501                         ts->last_read = 0;
502                         *val = average_val[data_idx]/(ts->debounce_rep);
503                         return XPT2046_FILTER_OK;
504                 } else {
505                         /* Read more values that are consistent. */
506                         ts->read_cnt++;
507                         
508                         return XPT2046_FILTER_REPEAT;
509                 }
510         }
511 }
512
513 static int xpt2046_no_filter(void *xpt, int data_idx, int *val)
514 {
515         return XPT2046_FILTER_OK;
516 }
517
518 static void xpt2046_rx_val(void *xpt)
519 {
520         struct xpt2046 *ts = xpt;
521         struct xpt2046_packet *packet = ts->packet;
522         struct spi_message *m;
523         struct spi_transfer *t;
524         int val;
525         int action;
526         int status;
527         
528         m = &ts->msg[ts->msg_idx];
529         t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
530
531         /* adjust:  on-wire is a must-ignore bit, a BE12 value, then padding;
532          * built from two 8 bit values written msb-first.
533          */
534         val = (be16_to_cpup((__be16 *)t->rx_buf) >> 3) & 0x0fff;
535
536 //      xpt2046printk("***>%s:value=%d, x=%d, y=%d, rx_buf=%d\n",__FUNCTION__,val,packet->tc.x,packet->tc.y,*(u16 *)t->rx_buf);
537         
538         action = ts->filter(ts->filter_data, ts->msg_idx, &val);
539         switch (action) {
540         case XPT2046_FILTER_REPEAT:
541                 break;
542         case XPT2046_FILTER_IGNORE:
543                 packet->tc.ignore = 1;
544                 /* Last message will contain xpt2046_rx() as the
545                  * completion function.
546                  */
547                 m = ts->last_msg;
548                 break;
549         case XPT2046_FILTER_OK:
550                 *(u16 *)t->rx_buf = val;
551                 packet->tc.ignore = 0;
552                 m = &ts->msg[++ts->msg_idx];
553                 break;
554         default:
555                 BUG();
556         }
557         ts->wait_for_sync();
558         status = spi_async(ts->spi, m);
559         if (status)
560                 dev_err(&ts->spi->dev, "spi_async --> %d\n",
561                                 status);
562 }
563
564 static enum hrtimer_restart xpt2046_timer(struct hrtimer *handle)
565 {
566         struct xpt2046  *ts = container_of(handle, struct xpt2046, timer);
567         int             status = 0;
568         int             i;
569         
570         spin_lock(&ts->lock);
571
572         if (unlikely(!get_pendown_state(ts) ||
573                      device_suspended(&ts->spi->dev))) {
574                                         //add by zhao
575                 struct input_dev *input = ts->input;
576                 for(i = 0; i < ARRAY_SIZE(panel_key_info); i++)
577                 {
578                         if (panel_key_info[i].status)
579                         {
580                                 input_report_key(input, panel_key_info[i].key, 0);
581                                 panel_key_info[i].status = 0;
582                                 input_sync(input);
583                                 xpt2046printk("***>%s:input_report_key(KEY up)\n",__FUNCTION__);
584                         }
585                 }
586                         
587                 if (ts->pendown) {                      
588                         input_report_key(input, BTN_TOUCH, 0);
589                         input_sync(input);
590                         ts->pendown = 0;
591                 
592                         xpt2046printk("***>%s:input_report_key(The touchscreen up)\n",__FUNCTION__);
593                 }
594
595                 /* measurement cycle ended */
596                 if (!device_suspended(&ts->spi->dev)) {
597                         xpt2046printk("***>%s:device_suspended==0\n",__FUNCTION__);
598                         ts->irq_disabled = 0;
599                         enable_irq(ts->spi->irq);
600                 }
601                 ts->pending = 0;
602         } else {
603                 /* pen is still down, continue with the measurement */
604                 xpt2046printk("***>%s:pen is still down, continue with the measurement\n",__FUNCTION__);
605                 ts->msg_idx = 0;
606                 ts->wait_for_sync();
607                 status = spi_async(ts->spi, &ts->msg[0]);
608                 if (status)
609                         dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
610         }
611
612         spin_unlock(&ts->lock);
613         return HRTIMER_NORESTART;
614 }
615
616 static irqreturn_t xpt2046_irq(int irq, void *handle)
617 {
618         struct xpt2046 *ts = handle;
619         unsigned long flags;
620         xpt2046printk("\n-----------xpt2046_irq------------------\n");
621         xpt2046printk("***>%s.....%s.....%d\n",__FILE__,__FUNCTION__,__LINE__);
622         
623         spin_lock_irqsave(&ts->lock, flags);
624
625         if (likely(get_pendown_state(ts))) {
626                 if (!ts->irq_disabled) {
627                         /* The ARM do_simple_IRQ() dispatcher doesn't act
628                          * like the other dispatchers:  it will report IRQs
629                          * even after they've been disabled.  We work around
630                          * that here.  (The "generic irq" framework may help...)
631                          */
632                         ts->irq_disabled = 1;
633                         disable_irq_nosync(ts->spi->irq);
634                         ts->pending = 1;
635                         hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
636                                         HRTIMER_MODE_REL);
637                 }
638         }
639         spin_unlock_irqrestore(&ts->lock, flags);
640
641         return IRQ_HANDLED;
642 }
643
644 /*--------------------------------------------------------------------------*/
645
646 /* Must be called with ts->lock held */
647 static void xpt2046_disable(struct xpt2046 *ts)
648 {
649         if (ts->disabled)
650                 return;
651
652         ts->disabled = 1;
653
654         /* are we waiting for IRQ, or polling? */
655         if (!ts->pending) {
656                 ts->irq_disabled = 1;
657                 disable_irq(ts->spi->irq);
658         } else {
659                 /* the timer will run at least once more, and
660                  * leave everything in a clean state, IRQ disabled
661                  */
662                 while (ts->pending) {
663                         spin_unlock_irq(&ts->lock);
664                         msleep(1);
665                         spin_lock_irq(&ts->lock);
666                 }
667         }
668
669         /* we know the chip's in lowpower mode since we always
670          * leave it that way after every request
671          */
672 }
673
674 /* Must be called with ts->lock held */
675 static void xpt2046_enable(struct xpt2046 *ts)
676 {
677         if (!ts->disabled)
678                 return;
679
680         ts->disabled = 0;
681         ts->irq_disabled = 0;
682         enable_irq(ts->spi->irq);
683 }
684
685 static int xpt2046_suspend(struct spi_device *spi, pm_message_t message)
686 {
687         struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
688
689         spin_lock_irq(&ts->lock);
690
691         ts->is_suspended = 1;
692         xpt2046_disable(ts);
693
694         spin_unlock_irq(&ts->lock);
695
696         return 0;
697
698 }
699
700 static int xpt2046_resume(struct spi_device *spi)
701 {
702         struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
703
704         spin_lock_irq(&ts->lock);
705
706         ts->is_suspended = 0;
707         xpt2046_enable(ts);
708
709         spin_unlock_irq(&ts->lock);
710
711         return 0;
712 }
713
714 static int __devinit setup_pendown(struct spi_device *spi, struct xpt2046 *ts)
715 {
716         struct xpt2046_platform_data *pdata = spi->dev.platform_data;
717         int err;
718
719         /* REVISIT when the irq can be triggered active-low, or if for some
720          * reason the touchscreen isn't hooked up, we don't need to access
721          * the pendown state.
722          */
723         if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
724                 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
725                 return -EINVAL;
726         }
727
728         if (pdata->get_pendown_state) {
729                 ts->get_pendown_state = pdata->get_pendown_state;
730                 return 0;
731         }
732         
733     if (pdata->io_init) {
734         err = pdata->io_init();
735         if (err)
736             dev_err(&spi->dev, "xpt2046 io_init fail\n");
737     }
738     
739         err = gpio_request(pdata->gpio_pendown, "xpt2046_pendown");
740         if (err) {
741                 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
742                                 pdata->gpio_pendown);
743                 return err;
744         }
745
746         ts->gpio_pendown = pdata->gpio_pendown;
747         return 0;
748 }
749
750
751 static int __devinit xpt2046_probe(struct spi_device *spi)
752 {
753         struct xpt2046                  *ts;
754         struct xpt2046_packet           *packet;
755         struct input_dev                *input_dev;
756         struct xpt2046_platform_data    *pdata = spi->dev.platform_data;
757         struct spi_message              *m;
758         struct spi_transfer             *x;
759         int                             vref;
760         int                             err;
761         int                             i;
762
763         if (!spi->irq) {
764                 dev_dbg(&spi->dev, "no IRQ?\n");
765                 return -ENODEV;
766         }
767         else{
768                 spi->irq = gpio_to_irq(spi->irq);
769                 dev_dbg(&spi->dev, "no IRQ?\n");
770         }
771         
772         /*
773         if (!pdata) {
774                 xpt2046printk("-----------------pdata is null -------------------------\n");
775                 spi->dev.platform_data = &xpt2046_info;
776                 pdata = spi->dev.platform_data;
777         }
778         */
779
780     if (!pdata) {
781         dev_err(&spi->dev, "empty platform_data\n");
782         return -EFAULT;
783     }
784
785         /* don't exceed max specified sample rate */
786         if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
787                 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
788                                 (spi->max_speed_hz/SAMPLE_BITS)/1000);
789                 return -EINVAL;
790         }
791
792         /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
793          * that even if the hardware can do that, the SPI controller driver
794          * may not.  So we stick to very-portable 8 bit words, both RX and TX.
795          */
796         spi->bits_per_word = 8;
797         spi->mode = SPI_MODE_0;
798         err = spi_setup(spi);
799         if (err < 0)
800                 return err;
801
802         ts = kzalloc(sizeof(struct xpt2046), GFP_KERNEL);
803         packet = kzalloc(sizeof(struct xpt2046_packet), GFP_KERNEL);
804         input_dev = input_allocate_device();
805         if (!ts || !packet || !input_dev) {
806                 err = -ENOMEM;
807                 goto err_free_mem;
808         }
809
810         dev_set_drvdata(&spi->dev, ts);
811
812         ts->packet = packet;
813         ts->spi = spi;
814         ts->input = input_dev;
815         ts->swap_xy = pdata->swap_xy;
816
817         hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
818         ts->timer.function = xpt2046_timer;
819
820         spin_lock_init(&ts->lock);
821
822         ts->model = pdata->model ? : 2046;
823
824         if (pdata->filter != NULL) {
825                 if (pdata->filter_init != NULL) {
826                         err = pdata->filter_init(pdata, &ts->filter_data);
827                         if (err < 0)
828                                 goto err_free_mem;
829                 }
830                 ts->filter = pdata->filter;
831                 ts->filter_cleanup = pdata->filter_cleanup;
832         } else if (pdata->debounce_max) {
833                 ts->debounce_max = pdata->debounce_max;
834                 if (ts->debounce_max < pdata->debounce_rep)
835                         ts->debounce_max = pdata->debounce_rep;
836                 ts->debounce_tol = pdata->debounce_tol;
837                 ts->debounce_rep = pdata->debounce_rep;
838                 ts->filter = xpt2046_debounce;
839                 ts->filter_data = ts;
840         } else
841                 ts->filter = xpt2046_no_filter;
842
843         err = setup_pendown(spi, ts);
844         if (err)
845                 goto err_cleanup_filter;
846
847         if (pdata->penirq_recheck_delay_usecs)
848                 ts->penirq_recheck_delay_usecs =
849                                 pdata->penirq_recheck_delay_usecs;
850
851         ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
852
853         snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
854         snprintf(ts->name, sizeof(ts->name), "XPT%d Touchscreen", ts->model);
855
856         input_dev->name = ts->name;
857         input_dev->phys = ts->phys;
858         input_dev->dev.parent = &spi->dev;
859
860         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
861         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
862         //add by zhao
863         for (i = 0; i < ARRAY_SIZE(panel_key_info); i++)
864                 set_bit(panel_key_info[i].key, input_dev->keybit);
865         clear_bit(0, input_dev->keybit);
866         //end
867         input_set_abs_params(input_dev, ABS_X,
868                         pdata->x_min ? : 0,
869                         pdata->x_max ? : MAX_12BIT,
870                         0, 0);
871         input_set_abs_params(input_dev, ABS_Y,
872                         pdata->y_min ? : 0,
873                         pdata->y_max ? : MAX_12BIT,
874                         0, 0);
875         
876         vref = pdata->keep_vref_on;
877
878         /* set up the transfers to read touchscreen state; this assumes we
879          * use formula #2 for pressure, not #3.
880          */
881         m = &ts->msg[0];
882         x = ts->xfer;
883
884         spi_message_init(m);
885
886         /* y- still on; turn on only y+ (and ADC) */
887         packet->read_y = READ_Y(vref);
888         xpt2046printk("-------------read Y 0x%x\n",packet->read_y);     //add by zhao
889         x->tx_buf = &packet->read_y;
890         x->len = 1;
891         spi_message_add_tail(x, m);
892
893         x++;
894         x->rx_buf = &packet->tc.y;
895         x->len = 2;
896         spi_message_add_tail(x, m);
897
898         m->complete = xpt2046_rx_val;
899         m->context = ts;
900
901         m++;
902         spi_message_init(m);
903
904         /* turn y- off, x+ on, then leave in lowpower */
905         x++;
906         packet->read_x = READ_X(vref);
907         xpt2046printk("-------------read X 0x%x\n",packet->read_x);     //add by zhao
908         x->tx_buf = &packet->read_x;
909         x->len = 1;
910         spi_message_add_tail(x, m);
911
912         x++;
913         x->rx_buf = &packet->tc.x;
914         x->len = 2;
915         spi_message_add_tail(x, m);
916
917         m->complete = xpt2046_rx_val;
918         m->context = ts;
919
920         /* power down */
921         m++;
922         spi_message_init(m);
923
924         x++;
925         packet->pwrdown = PWRDOWN;
926         x->tx_buf = &packet->pwrdown;
927         x->len = 1;
928         spi_message_add_tail(x, m);
929
930         x++;
931         x->rx_buf = &packet->dummy;
932         x->len = 2;
933         CS_CHANGE(*x);
934         spi_message_add_tail(x, m);
935
936         m->complete = xpt2046_rx;
937         m->context = ts;
938
939         ts->last_msg = m;
940         xpt2046printk("***>%s:touchscreen irq %d\n",__FUNCTION__,spi->irq);
941         if (request_irq(spi->irq, xpt2046_irq, IRQF_TRIGGER_FALLING,
942                         spi->dev.driver->name, ts)) {
943                 printk("%s:trying pin change workaround on irq %d\n",__FUNCTION__,spi->irq);
944                 err = request_irq(spi->irq, xpt2046_irq,
945                                   IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
946                                   spi->dev.driver->name, ts);
947                 if (err) {
948                         dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
949                         goto err_free_gpio;
950                 }
951         }
952         
953         /* take a first sample, leaving nPENIRQ active and vREF off; avoid
954          * the touchscreen, in case it's not connected.
955          */
956         xpt2046_read12_dfr(&spi->dev,READ_X(1));
957
958         err = input_register_device(input_dev);
959         if (err)
960                 goto err_remove_attr_group;
961   printk("xpt2046_ts: driver initialized\n");
962         return 0;
963
964  err_remove_attr_group:
965         free_irq(spi->irq, ts);
966  err_free_gpio:
967         if (ts->gpio_pendown != -1)
968                 gpio_free(ts->gpio_pendown);
969  err_cleanup_filter:
970         if (ts->filter_cleanup)
971                 ts->filter_cleanup(ts->filter_data);
972  err_free_mem:
973         input_free_device(input_dev);
974         kfree(packet);
975         kfree(ts);
976         return err;
977 }
978
979 static int __devexit xpt2046_remove(struct spi_device *spi)
980 {
981         struct xpt2046          *ts = dev_get_drvdata(&spi->dev);
982
983         input_unregister_device(ts->input);
984
985         xpt2046_suspend(spi, PMSG_SUSPEND);
986
987         free_irq(ts->spi->irq, ts);
988         /* suspend left the IRQ disabled */
989         enable_irq(ts->spi->irq);
990
991         if (ts->gpio_pendown != -1)
992                 gpio_free(ts->gpio_pendown);
993
994         if (ts->filter_cleanup)
995                 ts->filter_cleanup(ts->filter_data);
996
997         kfree(ts->packet);
998         kfree(ts);
999
1000         dev_dbg(&spi->dev, "unregistered touchscreen\n");
1001         return 0;
1002 }
1003
1004 static struct spi_driver xpt2046_driver = {
1005         .driver = {
1006                 .name   = "xpt2046_ts",
1007                 .bus    = &spi_bus_type,
1008                 .owner  = THIS_MODULE,
1009         },
1010         .probe          = xpt2046_probe,
1011         .remove         = __devexit_p(xpt2046_remove),
1012         .suspend        = xpt2046_suspend,
1013         .resume         = xpt2046_resume,
1014 };
1015
1016 static int __init xpt2046_init(void)
1017 {
1018         return spi_register_driver(&xpt2046_driver);
1019 }
1020 module_init(xpt2046_init);
1021
1022 static void __exit xpt2046_exit(void)
1023 {
1024         spi_unregister_driver(&xpt2046_driver);
1025 }
1026 module_exit(xpt2046_exit);
1027
1028 MODULE_DESCRIPTION("rk2818 spi xpt2046 TouchScreen Driver");
1029 MODULE_LICENSE("GPL");
1030 MODULE_ALIAS("spi:xpt2046");