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