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