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