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