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