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