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