2 * drivers/input/touchscreen/xpt2046_cbn_ts.c - driver for rk2818 spi xpt2046 calibration device and console
4 * Copyright (C) 2010 ROCKCHIP, Inc.
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.
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.
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>
27 #include "xpt2046_cbn_ts.h"
28 #include "calibration_ts.h"
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.
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.
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
49 #define XPT2046_DEBUG 0
51 #define xpt2046printk(msg...) printk(msg);
53 #define xpt2046printk(msg...)
56 //#define TS_POLL_DELAY (15 * 1000000) /* ns delay before the first sample */
57 //#define TS_POLL_PERIOD (15 * 1000000) /* ns delay between samples */
58 #define TS_POLL_DELAY (10 * 1000000) /* ns delay before the first sample */
59 #define TS_POLL_PERIOD (20 * 1000000) /* ns delay between samples */
62 #define DEBOUNCE_REPTIME 3
63 /* this driver doesn't aim at the peak continuous sample rate */
64 #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
67 /* For portability, we can't read 12 bit values using SPI (which
68 * would make the controller deliver them as native byteorder u16
69 * with msbs zeroed). Instead, we read them as two 8-bit values,
70 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
78 * We allocate this separately to avoid cache line sharing issues when
79 * driver is used with DMA-based SPI controllers (like atmel_spi) on
80 * systems where main memory is not DMA-coherent (most non-x86 boards).
82 struct xpt2046_packet {
83 u8 read_x, read_y, pwrdown;
84 u16 dummy; /* for the pwrdown read */
89 struct input_dev *input;
93 struct spi_device *spi;
98 struct xpt2046_packet *packet;
100 struct spi_transfer xfer[18];
101 struct spi_message msg[5];
102 struct spi_message *last_msg;
112 u16 penirq_recheck_delay_usecs;
115 struct hrtimer timer;
116 unsigned pendown:1; /* P: lock */
117 unsigned pending:1; /* P: lock */
118 // FIXME remove "irq_disabled"
119 unsigned irq_disabled:1; /* P: lock */
121 unsigned is_suspended:1;
123 int (*filter)(void *data, int data_idx, int *val);
125 void (*filter_cleanup)(void *data);
126 int (*get_pendown_state)(void);
129 void (*wait_for_sync)(void);
132 /* leave chip selected when we're done, for quicker re-select? */
134 #define CS_CHANGE(xfer) ((xfer).cs_change = 1)
136 #define CS_CHANGE(xfer) ((xfer).cs_change = 0)
139 /*--------------------------------------------------------------------------*/
141 /* The xpt2046 has touchscreen and other sensors.
142 * Earlier xpt2046 chips are somewhat compatible.
144 #define XPT2046_START (1 << 7)
145 #define XPT2046_A2A1A0_d_y (1 << 4) /* differential */
146 #define XPT2046_A2A1A0_d_z1 (3 << 4) /* differential */
147 #define XPT2046_A2A1A0_d_z2 (4 << 4) /* differential */
148 #define XPT2046_A2A1A0_d_x (5 << 4) /* differential */
149 #define XPT2046_A2A1A0_temp0 (0 << 4) /* non-differential */
150 #define XPT2046_A2A1A0_vbatt (2 << 4) /* non-differential */
151 #define XPT2046_A2A1A0_vaux (6 << 4) /* non-differential */
152 #define XPT2046_A2A1A0_temp1 (7 << 4) /* non-differential */
153 #define XPT2046_8_BIT (1 << 3)
154 #define XPT2046_12_BIT (0 << 3)
155 #define XPT2046_SER (1 << 2) /* non-differential */
156 #define XPT2046_DFR (0 << 2) /* differential */
157 #define XPT2046_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */
158 #define XPT2046_PD10_ADC_ON (1 << 0) /* ADC on */
159 #define XPT2046_PD10_REF_ON (2 << 0) /* vREF on + penirq */
160 #define XPT2046_PD10_ALL_ON (3 << 0) /* ADC + vREF on */
162 #define MAX_12BIT ((1<<12)-1)
164 /* leave ADC powered up (disables penirq) between differential samples */
165 #define READ_12BIT_DFR(x, adc, vref) (XPT2046_START | XPT2046_A2A1A0_d_ ## x \
166 | XPT2046_12_BIT | XPT2046_DFR | \
167 (adc ? XPT2046_PD10_ADC_ON : 0) | (vref ? XPT2046_PD10_REF_ON : 0))
169 #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
170 #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
171 #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
173 #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
174 #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
176 /* single-ended samples need to first power up reference voltage;
177 * we leave both ADC and VREF powered
179 #define READ_12BIT_SER(x) (XPT2046_START | XPT2046_A2A1A0_ ## x \
180 | XPT2046_12_BIT | XPT2046_SER)
182 #define REF_ON (READ_12BIT_DFR(x, 1, 1))
183 #define REF_OFF (READ_12BIT_DFR(y, 0, 0))
185 /*--------------------------------------------------------------------------*/
187 * touchscreen sensors use differential conversions.
193 u16 dummy; /* for the pwrdown read */
195 struct spi_message msg;
196 struct spi_transfer xfer[4];
205 static struct xpt2046_platform_data xpt2046_info = {
214 .debounce_rep = DEBOUNCE_REPTIME,
216 .gpio_pendown = RK2818_PIN_PE3,
217 .penirq_recheck_delay_usecs = 1,
221 static void xpt2046_enable(struct xpt2046 *ts);
222 static void xpt2046_disable(struct xpt2046 *ts);
224 static POINT gADPoint;
225 int screen_x[] = { 50, 750, 50, 750, 400};
226 int screen_y[] = { 40, 40, 440, 440, 240};
227 int uncali_x[] = {329, 3750, 331, 3757, 2046};
228 int uncali_y[] = {593, 532, 3675, 3655, 2121};
230 // This code is touch check
231 static ssize_t xpt2046_mode_show(struct device_driver *drv,char *buf)
235 count = sprintf(buf,"xpt2046_mode_show:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
236 uncali_x[0], uncali_y[0],
237 uncali_x[1], uncali_y[1],
238 uncali_x[2], uncali_y[2],
239 uncali_x[3], uncali_y[3],
240 uncali_x[4], uncali_y[4]);
242 printk("buf: %s", buf);
247 static ssize_t xpt2046_mode_store(struct device_driver * drv, const char * buf, size_t count)
252 printk("xpt2046_mode_store: %s\n", buf);
254 for (i = 0; i < 5; i++)
256 strncpy(temp, buf + 5 * (j++), 4);
257 uncali_x[i] = simple_strtol(temp, NULL, 10);
258 strncpy(temp, buf + 5 * (j++), 4);
259 uncali_y[i] = simple_strtol(temp, NULL, 10);
260 printk("SN=%d uncali_x=%d uncali_y=%d\n",
261 i, uncali_x[i], uncali_y[i]);
267 //This code is Touch adc simple value
268 static ssize_t xpt2046_adc_show(struct device_driver *drv,char *buf)
270 printk("xpt2046_adc_show: x=%d y=%d\n", gADPoint.x, gADPoint.y);
272 return sprintf(buf, "%d,%d\n", gADPoint.x, gADPoint.y);
275 static ssize_t xpt2046_cali_status(struct device_driver *drv, char *buf)
279 ret = TouchPanelSetCalibration(4, screen_x, screen_y, uncali_x, uncali_y);
281 ret = sprintf(buf, "successful\n");
283 ret = sprintf(buf, "fail\n");
285 printk("xpt2046_cali_status: buf=<%s", buf);
290 //static DEVICE_ATTR(adc, 0666, xpt2046_adc_show, NULL);
291 //static DEVICE_ATTR(calistatus, 0666, xpt2046_cali_status, NULL);
292 //static DEVICE_ATTR(mode, 0666, xpt2046_mode_show, xpt2046_mode_store);
293 static DRIVER_ATTR(touchadc, 0666, xpt2046_adc_show, NULL);
294 static DRIVER_ATTR(calistatus, 0666, xpt2046_cali_status, NULL);
295 static DRIVER_ATTR(touchcheck, 0666, xpt2046_mode_show, xpt2046_mode_store);
297 static struct attribute *xpt2046_attributes[] = {
298 &dev_attr_touchadc.attr,
299 &dev_attr_calistatus.attr,
300 &dev_attr_touchcheck.attr,
304 static struct attribute_group xpt2046_attr_group = {
305 .attrs = xpt2046_attributes,
309 static int device_suspended(struct device *dev)
311 struct xpt2046 *ts = dev_get_drvdata(dev);
312 return ts->is_suspended || ts->disabled;
315 static int xpt2046_read12_dfr(struct device *dev, unsigned command)
317 struct spi_device *spi = to_spi_device(dev);
318 struct xpt2046 *ts = dev_get_drvdata(dev);
319 struct dfr_req *req = kzalloc(sizeof *req, GFP_KERNEL);
325 spi_message_init(&req->msg);
328 req->command = (u8) command;
329 req->xfer[0].tx_buf = &req->command;
330 req->xfer[0].len = 1;
331 spi_message_add_tail(&req->xfer[0], &req->msg);
333 req->xfer[1].rx_buf = &req->sample;
334 req->xfer[1].len = 2;
335 spi_message_add_tail(&req->xfer[1], &req->msg);
337 /* converter in low power mode & enable PENIRQ */
338 req->pwrdown= PWRDOWN;
339 req->xfer[2].tx_buf = &req->pwrdown;
340 req->xfer[2].len = 1;
341 spi_message_add_tail(&req->xfer[2], &req->msg);
343 req->xfer[3].rx_buf = &req->dummy;
344 req->xfer[3].len = 2;
345 CS_CHANGE(req->xfer[3]);
346 spi_message_add_tail(&req->xfer[3], &req->msg);
348 ts->irq_disabled = 1;
349 disable_irq(spi->irq);
350 status = spi_sync(spi, &req->msg);
351 ts->irq_disabled = 0;
352 enable_irq(spi->irq);
355 /* on-wire is a must-ignore bit, a BE12 value, then padding */
356 status = be16_to_cpu(req->sample);
357 status = status >> 3;
359 xpt2046printk("***>%s:status=%d\n",__FUNCTION__,status);
368 /*--------------------------------------------------------------------------*/
370 static int get_pendown_state(struct xpt2046 *ts)
372 if (ts->get_pendown_state)
373 return ts->get_pendown_state();
375 return !gpio_get_value(ts->gpio_pendown);
378 static void null_wait_for_sync(void)
384 * PENIRQ only kicks the timer. The timer only reissues the SPI transfer,
385 * to retrieve touchscreen status.
387 * The SPI transfer completion callback does the real work. It reports
388 * touchscreen events and reactivates the timer (or IRQ) as appropriate.
391 static void xpt2046_rx(void *xpt)
393 struct xpt2046 *ts = xpt;
394 struct xpt2046_packet *packet = ts->packet;
398 /* xpt2046_rx_val() did in-place conversion (including byteswap) from
399 * on-the-wire format as part of debouncing to get stable readings.
404 xpt2046printk("***>%s:x=%d,y=%d\n",__FUNCTION__,x,y);
406 /* range filtering */
410 /* Sample found inconsistent by debouncing or pressure is beyond
411 * the maximum. Don't report it to user space, repeat at least
412 * once more the measurement
414 if (packet->tc.ignore) {
416 xpt2046printk("***>%s:ignored=%d\n",__FUNCTION__,packet->tc.ignore);
418 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
423 /* Maybe check the pendown state before reporting. This discards
424 * false readings when the pen is lifted.
426 if (ts->penirq_recheck_delay_usecs) {
427 udelay(ts->penirq_recheck_delay_usecs);
428 if (!get_pendown_state(ts))
430 xpt2046printk("***>%s:get_pendown_state(ts)==0,discard false reading\n",__FUNCTION__);
435 /* NOTE: We can't rely on the pressure to determine the pen down
436 * state, even this controller has a pressure sensor. The pressure
437 * value can fluctuate for quite a while after lifting the pen and
438 * in some cases may not even settle at the expected value.
440 * The only safe way to check for the pen up condition is in the
441 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
444 struct input_dev *input = ts->input;
446 input_report_key(input, BTN_TOUCH, 1);
448 xpt2046printk("***>%s:input_report_key(pen down)\n",__FUNCTION__);
451 TouchPanelCalibrateAPoint(x, y, &xd, &yd);
462 input_report_abs(input, ABS_X, xd);
463 input_report_abs(input, ABS_Y, yd);
466 xpt2046printk("***>%s:input_report_abs(%4d/%4d)\n",__FUNCTION__,xd, yd);
469 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
473 static int xpt2046_debounce(void *xpt, int data_idx, int *val)
475 struct xpt2046 *ts = xpt;
476 static int average_val[2];
479 xpt2046printk("***>%s:%d,%d,%d,%d,%d,%d,%d,%d\n",__FUNCTION__,
480 data_idx,ts->last_read,
481 ts->read_cnt,ts->debounce_max,
482 abs(ts->last_read - *val),ts->debounce_tol,
483 ts->read_rep,ts->debounce_rep);
485 if(*val == 4095 || *val == 0)
489 memset(average_val,0,sizeof(average_val));
490 xpt2046printk("***>%s:*val == 4095 || *val == 0\n",__FUNCTION__);
491 return XPT2046_FILTER_IGNORE;
493 /* discard the first sample. */
498 return XPT2046_FILTER_REPEAT;
501 if (ts->read_cnt==1 || (abs(ts->last_read - *val) > ts->debounce_tol)) {
502 /* Start over collecting consistent readings. */
504 average_val[data_idx] = *val;
505 /* Repeat it, if this was the first read or the read
506 * wasn't consistent enough. */
507 if (ts->read_cnt < ts->debounce_max) {
508 ts->last_read = *val;
510 return XPT2046_FILTER_REPEAT;
512 /* Maximum number of debouncing reached and still
513 * not enough number of consistent readings. Abort
514 * the whole sample, repeat it in the next sampling
519 memset(average_val,0,sizeof(average_val));
520 xpt2046printk("***>%s:XPT2046_FILTER_IGNORE\n",__FUNCTION__);
521 return XPT2046_FILTER_IGNORE;
525 average_val[data_idx] += *val;
527 if (++ts->read_rep >= ts->debounce_rep) {
528 /* Got a good reading for this coordinate,
529 * go for the next one. */
533 *val = average_val[data_idx]/(ts->debounce_rep);
534 return XPT2046_FILTER_OK;
536 /* Read more values that are consistent. */
539 return XPT2046_FILTER_REPEAT;
544 static int xpt2046_no_filter(void *xpt, int data_idx, int *val)
546 return XPT2046_FILTER_OK;
549 static void xpt2046_rx_val(void *xpt)
551 struct xpt2046 *ts = xpt;
552 struct xpt2046_packet *packet = ts->packet;
553 struct spi_message *m;
554 struct spi_transfer *t;
559 m = &ts->msg[ts->msg_idx];
560 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
562 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
563 * built from two 8 bit values written msb-first.
565 val = (be16_to_cpup((__be16 *)t->rx_buf) >> 3) & 0x0fff;
567 xpt2046printk("***>%s:value=%d\n",__FUNCTION__,val);
569 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
571 case XPT2046_FILTER_REPEAT:
573 case XPT2046_FILTER_IGNORE:
574 packet->tc.ignore = 1;
575 /* Last message will contain xpt2046_rx() as the
576 * completion function.
580 case XPT2046_FILTER_OK:
581 *(u16 *)t->rx_buf = val;
582 packet->tc.ignore = 0;
583 m = &ts->msg[++ts->msg_idx];
589 status = spi_async(ts->spi, m);
591 dev_err(&ts->spi->dev, "spi_async --> %d\n",
595 static enum hrtimer_restart xpt2046_timer(struct hrtimer *handle)
597 struct xpt2046 *ts = container_of(handle, struct xpt2046, timer);
600 spin_lock(&ts->lock);
602 if (unlikely(!get_pendown_state(ts) ||
603 device_suspended(&ts->spi->dev))) {
605 struct input_dev *input = ts->input;
606 input_report_key(input, BTN_TOUCH, 0);
611 xpt2046printk("***>%s:input_report_key(The touchscreen up)\n",__FUNCTION__);
614 /* measurement cycle ended */
615 if (!device_suspended(&ts->spi->dev)) {
616 xpt2046printk("***>%s:device_suspended==0\n",__FUNCTION__);
617 ts->irq_disabled = 0;
618 enable_irq(ts->spi->irq);
622 /* pen is still down, continue with the measurement */
623 xpt2046printk("***>%s:pen is still down, continue with the measurement\n",__FUNCTION__);
626 status = spi_async(ts->spi, &ts->msg[0]);
628 dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
631 spin_unlock(&ts->lock);
632 return HRTIMER_NORESTART;
635 static irqreturn_t xpt2046_irq(int irq, void *handle)
637 struct xpt2046 *ts = handle;
640 xpt2046printk("***>%s.....%s.....%d\n",__FILE__,__FUNCTION__,__LINE__);
642 spin_lock_irqsave(&ts->lock, flags);
644 if (likely(get_pendown_state(ts))) {
645 if (!ts->irq_disabled) {
646 /* The ARM do_simple_IRQ() dispatcher doesn't act
647 * like the other dispatchers: it will report IRQs
648 * even after they've been disabled. We work around
649 * that here. (The "generic irq" framework may help...)
651 ts->irq_disabled = 1;
652 disable_irq_nosync(ts->spi->irq);
654 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
658 spin_unlock_irqrestore(&ts->lock, flags);
663 /*--------------------------------------------------------------------------*/
665 /* Must be called with ts->lock held */
666 static void xpt2046_disable(struct xpt2046 *ts)
673 /* are we waiting for IRQ, or polling? */
675 ts->irq_disabled = 1;
676 disable_irq(ts->spi->irq);
678 /* the timer will run at least once more, and
679 * leave everything in a clean state, IRQ disabled
681 while (ts->pending) {
682 spin_unlock_irq(&ts->lock);
684 spin_lock_irq(&ts->lock);
688 /* we know the chip's in lowpower mode since we always
689 * leave it that way after every request
693 /* Must be called with ts->lock held */
694 static void xpt2046_enable(struct xpt2046 *ts)
700 ts->irq_disabled = 0;
701 enable_irq(ts->spi->irq);
704 static int xpt2046_suspend(struct spi_device *spi, pm_message_t message)
706 struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
708 spin_lock_irq(&ts->lock);
710 ts->is_suspended = 1;
713 spin_unlock_irq(&ts->lock);
719 static int xpt2046_resume(struct spi_device *spi)
721 struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
723 spin_lock_irq(&ts->lock);
725 ts->is_suspended = 0;
728 spin_unlock_irq(&ts->lock);
733 static int __devinit setup_pendown(struct spi_device *spi, struct xpt2046 *ts)
735 struct xpt2046_platform_data *pdata = spi->dev.platform_data;
738 /* REVISIT when the irq can be triggered active-low, or if for some
739 * reason the touchscreen isn't hooked up, we don't need to access
742 if (!pdata->get_pendown_state && !gpio_is_valid(pdata->gpio_pendown)) {
743 dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");
747 if (pdata->get_pendown_state) {
748 ts->get_pendown_state = pdata->get_pendown_state;
752 if (pdata->io_init) {
753 err = pdata->io_init();
755 dev_err(&spi->dev, "xpt2046 io_init fail\n");
758 err = gpio_request(pdata->gpio_pendown, "xpt2046_pendown");
760 dev_err(&spi->dev, "failed to request pendown GPIO%d\n",
761 pdata->gpio_pendown);
765 ts->gpio_pendown = pdata->gpio_pendown;
769 static int __devinit xpt2046_probe(struct spi_device *spi)
772 struct xpt2046_packet *packet;
773 struct input_dev *input_dev;
774 struct xpt2046_platform_data *pdata = spi->dev.platform_data;
775 struct spi_message *m;
776 struct spi_transfer *x;
783 dev_dbg(&spi->dev, "no IRQ?\n");
787 spi->irq = gpio_to_irq(spi->irq);
788 dev_dbg(&spi->dev, "no IRQ?\n");
792 dev_err(&spi->dev, "empty platform_data\n");
796 /* don't exceed max specified sample rate */
797 if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
798 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
799 (spi->max_speed_hz/SAMPLE_BITS)/1000);
803 /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
804 * that even if the hardware can do that, the SPI controller driver
805 * may not. So we stick to very-portable 8 bit words, both RX and TX.
807 spi->bits_per_word = 8;
808 spi->mode = SPI_MODE_0;
809 err = spi_setup(spi);
813 ts = kzalloc(sizeof(struct xpt2046), GFP_KERNEL);
814 packet = kzalloc(sizeof(struct xpt2046_packet), GFP_KERNEL);
815 input_dev = input_allocate_device();
816 if (!ts || !packet || !input_dev) {
821 dev_set_drvdata(&spi->dev, ts);
825 ts->input = input_dev;
826 ts->swap_xy = pdata->swap_xy;
828 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
829 ts->timer.function = xpt2046_timer;
831 spin_lock_init(&ts->lock);
833 ts->model = pdata->model ? : 2046;
835 if (pdata->filter != NULL) {
836 if (pdata->filter_init != NULL) {
837 err = pdata->filter_init(pdata, &ts->filter_data);
841 ts->filter = pdata->filter;
842 ts->filter_cleanup = pdata->filter_cleanup;
843 } else if (pdata->debounce_max) {
844 ts->debounce_max = pdata->debounce_max;
845 if (ts->debounce_max < pdata->debounce_rep)
846 ts->debounce_max = pdata->debounce_rep;
847 ts->debounce_tol = pdata->debounce_tol;
848 ts->debounce_rep = pdata->debounce_rep;
849 ts->filter = xpt2046_debounce;
850 ts->filter_data = ts;
852 ts->filter = xpt2046_no_filter;
854 err = setup_pendown(spi, ts);
856 goto err_cleanup_filter;
858 if (pdata->penirq_recheck_delay_usecs)
859 ts->penirq_recheck_delay_usecs =
860 pdata->penirq_recheck_delay_usecs;
862 ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;
864 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
865 snprintf(ts->name, sizeof(ts->name), "XPT%d Touchscreen", ts->model);
867 input_dev->name = ts->name;
868 input_dev->phys = ts->phys;
869 input_dev->dev.parent = &spi->dev;
871 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
872 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
873 input_set_abs_params(input_dev, ABS_X,
875 pdata->x_max ? : MAX_12BIT,
877 input_set_abs_params(input_dev, ABS_Y,
879 pdata->y_max ? : MAX_12BIT,
882 vref = pdata->keep_vref_on;
884 /* set up the transfers to read touchscreen state; this assumes we
885 * use formula #2 for pressure, not #3.
892 /* y- still on; turn on only y+ (and ADC) */
893 packet->read_y = READ_Y(vref);
894 x->tx_buf = &packet->read_y;
896 spi_message_add_tail(x, m);
899 x->rx_buf = &packet->tc.y;
901 spi_message_add_tail(x, m);
903 m->complete = xpt2046_rx_val;
909 /* turn y- off, x+ on, then leave in lowpower */
911 packet->read_x = READ_X(vref);
912 x->tx_buf = &packet->read_x;
914 spi_message_add_tail(x, m);
917 x->rx_buf = &packet->tc.x;
919 spi_message_add_tail(x, m);
921 m->complete = xpt2046_rx_val;
929 packet->pwrdown = PWRDOWN;
930 x->tx_buf = &packet->pwrdown;
932 spi_message_add_tail(x, m);
935 x->rx_buf = &packet->dummy;
938 spi_message_add_tail(x, m);
940 m->complete = xpt2046_rx;
945 if (request_irq(spi->irq, xpt2046_irq, IRQF_TRIGGER_FALLING,
946 spi->dev.driver->name, ts)) {
947 printk("%s:trying pin change workaround on irq %d\n",__FUNCTION__,spi->irq);
948 err = request_irq(spi->irq, xpt2046_irq,
949 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
950 spi->dev.driver->name, ts);
952 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
956 xpt2046printk("***>%s:touchscreen irq %d\n",__FUNCTION__,spi->irq);
958 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
959 * the touchscreen, in case it's not connected.
961 xpt2046_read12_dfr(&spi->dev,READ_X(1));
963 //err = sysfs_create_group(&spi->dev.kobj, &xpt2046_attr_group);
965 goto err_remove_hwmon;
967 err = input_register_device(input_dev);
969 goto err_remove_attr_group;
970 printk("xpt2046_ts: driver initialized\n");
973 err_remove_attr_group:
974 //sysfs_remove_group(&spi->dev.kobj, &xpt2046_attr_group);
976 free_irq(spi->irq, ts);
978 if (ts->gpio_pendown != -1)
979 gpio_free(ts->gpio_pendown);
981 if (ts->filter_cleanup)
982 ts->filter_cleanup(ts->filter_data);
984 input_free_device(input_dev);
990 static int __devexit xpt2046_remove(struct spi_device *spi)
992 struct xpt2046 *ts = dev_get_drvdata(&spi->dev);
994 input_unregister_device(ts->input);
996 xpt2046_suspend(spi, PMSG_SUSPEND);
998 //sysfs_remove_group(&spi->dev.kobj, &xpt2046_attr_group);
1000 free_irq(ts->spi->irq, ts);
1001 /* suspend left the IRQ disabled */
1002 enable_irq(ts->spi->irq);
1004 if (ts->gpio_pendown != -1)
1005 gpio_free(ts->gpio_pendown);
1007 if (ts->filter_cleanup)
1008 ts->filter_cleanup(ts->filter_data);
1013 dev_dbg(&spi->dev, "unregistered touchscreen\n");
1017 static struct spi_driver xpt2046_driver = {
1019 .name = "xpt2046_ts",
1020 .bus = &spi_bus_type,
1021 .owner = THIS_MODULE,
1023 .probe = xpt2046_probe,
1024 .remove = __devexit_p(xpt2046_remove),
1025 .suspend = xpt2046_suspend,
1026 .resume = xpt2046_resume,
1029 static int __init xpt2046_init(void)
1031 //return spi_register_driver(&xpt2046_driver);
1032 int ret = spi_register_driver(&xpt2046_driver);
1038 ret = driver_create_file(&xpt2046_driver.driver, &driver_attr_touchcheck);
1039 ret += driver_create_file(&xpt2046_driver.driver, &driver_attr_touchadc);
1040 ret += driver_create_file(&xpt2046_driver.driver, &driver_attr_calistatus);
1045 module_init(xpt2046_init);
1047 static void __exit xpt2046_exit(void)
1049 //spi_unregister_driver(&xpt2046_driver);
1050 driver_remove_file(&xpt2046_driver.driver, &driver_attr_touchcheck);
1051 driver_remove_file(&xpt2046_driver.driver, &driver_attr_touchadc);
1052 driver_remove_file(&xpt2046_driver.driver, &driver_attr_calistatus);
1054 spi_unregister_driver(&xpt2046_driver);
1056 module_exit(xpt2046_exit);
1058 MODULE_DESCRIPTION("rk2818 spi xpt2046 TouchScreen Driver");
1059 MODULE_LICENSE("GPL");
1060 MODULE_ALIAS("spi:xpt2046");