2 * Copyright (c) 2008 Nuvoton technology corporation.
4 * Wan ZongShun <mcuos.com@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;version 2 of the License.
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
19 /* ADC controller bit defines */
20 #define ADC_DELAY 0xf00
22 #define ADC_TSC_Y (0x01 << 8)
23 #define ADC_TSC_X (0x00 << 8)
24 #define TSC_FOURWIRE (~(0x03 << 1))
25 #define ADC_CLK_EN (0x01 << 28) /* ADC clock enable */
26 #define ADC_READ_CON (0x01 << 12)
27 #define ADC_CONV (0x01 << 13)
28 #define ADC_SEMIAUTO (0x01 << 14)
29 #define ADC_WAITTRIG (0x03 << 14)
30 #define ADC_RST1 (0x01 << 16)
31 #define ADC_RST0 (0x00 << 16)
32 #define ADC_EN (0x01 << 17)
33 #define ADC_INT (0x01 << 18)
34 #define WT_INT (0x01 << 20)
35 #define ADC_INT_EN (0x01 << 21)
36 #define LVD_INT_EN (0x01 << 22)
37 #define WT_INT_EN (0x01 << 23)
38 #define ADC_DIV (0x04 << 1) /* div = 6 */
41 TS_WAIT_NEW_PACKET
, /* We are waiting next touch report */
42 TS_WAIT_X_COORD
, /* We are waiting for ADC to report X coord */
43 TS_WAIT_Y_COORD
, /* We are waiting for ADC to report Y coord */
44 TS_IDLE
, /* Input device is closed, don't do anything */
48 struct input_dev
*input
;
49 struct timer_list timer
;
51 void __iomem
*clocken
;
57 static void w90p910_report_event(struct w90p910_ts
*w90p910_ts
, bool down
)
59 struct input_dev
*dev
= w90p910_ts
->input
;
62 input_report_abs(dev
, ABS_X
,
63 __raw_readl(w90p910_ts
->ts_reg
+ 0x0c));
64 input_report_abs(dev
, ABS_Y
,
65 __raw_readl(w90p910_ts
->ts_reg
+ 0x10));
68 input_report_key(dev
, BTN_TOUCH
, down
);
72 static void w90p910_prepare_x_reading(struct w90p910_ts
*w90p910_ts
)
76 __raw_writel(ADC_TSC_X
, w90p910_ts
->ts_reg
+ 0x04);
77 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
78 ctlreg
&= ~(ADC_WAITTRIG
| WT_INT
| WT_INT_EN
);
79 ctlreg
|= ADC_SEMIAUTO
| ADC_INT_EN
| ADC_CONV
;
80 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
82 w90p910_ts
->state
= TS_WAIT_X_COORD
;
85 static void w90p910_prepare_y_reading(struct w90p910_ts
*w90p910_ts
)
89 __raw_writel(ADC_TSC_Y
, w90p910_ts
->ts_reg
+ 0x04);
90 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
91 ctlreg
&= ~(ADC_WAITTRIG
| ADC_INT
| WT_INT_EN
);
92 ctlreg
|= ADC_SEMIAUTO
| ADC_INT_EN
| ADC_CONV
;
93 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
95 w90p910_ts
->state
= TS_WAIT_Y_COORD
;
98 static void w90p910_prepare_next_packet(struct w90p910_ts
*w90p910_ts
)
100 unsigned long ctlreg
;
102 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
103 ctlreg
&= ~(ADC_INT
| ADC_INT_EN
| ADC_SEMIAUTO
| ADC_CONV
);
104 ctlreg
|= ADC_WAITTRIG
| WT_INT_EN
;
105 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
107 w90p910_ts
->state
= TS_WAIT_NEW_PACKET
;
110 static irqreturn_t
w90p910_ts_interrupt(int irq
, void *dev_id
)
112 struct w90p910_ts
*w90p910_ts
= dev_id
;
115 spin_lock_irqsave(&w90p910_ts
->lock
, flags
);
117 switch (w90p910_ts
->state
) {
118 case TS_WAIT_NEW_PACKET
:
120 * The controller only generates interrupts when pen
123 del_timer(&w90p910_ts
->timer
);
124 w90p910_prepare_x_reading(w90p910_ts
);
128 case TS_WAIT_X_COORD
:
129 w90p910_prepare_y_reading(w90p910_ts
);
132 case TS_WAIT_Y_COORD
:
133 w90p910_report_event(w90p910_ts
, true);
134 w90p910_prepare_next_packet(w90p910_ts
);
135 mod_timer(&w90p910_ts
->timer
, jiffies
+ msecs_to_jiffies(100));
142 spin_unlock_irqrestore(&w90p910_ts
->lock
, flags
);
147 static void w90p910_check_pen_up(unsigned long data
)
149 struct w90p910_ts
*w90p910_ts
= (struct w90p910_ts
*) data
;
152 spin_lock_irqsave(&w90p910_ts
->lock
, flags
);
154 if (w90p910_ts
->state
== TS_WAIT_NEW_PACKET
&&
155 !(__raw_readl(w90p910_ts
->ts_reg
+ 0x04) & ADC_DOWN
)) {
157 w90p910_report_event(w90p910_ts
, false);
160 spin_unlock_irqrestore(&w90p910_ts
->lock
, flags
);
163 static int w90p910_open(struct input_dev
*dev
)
165 struct w90p910_ts
*w90p910_ts
= input_get_drvdata(dev
);
168 /* enable the ADC clock */
169 val
= __raw_readl(w90p910_ts
->clocken
);
170 __raw_writel(val
| ADC_CLK_EN
, w90p910_ts
->clocken
);
172 __raw_writel(ADC_RST1
, w90p910_ts
->ts_reg
);
174 __raw_writel(ADC_RST0
, w90p910_ts
->ts_reg
);
177 /* set delay and screen type */
178 val
= __raw_readl(w90p910_ts
->ts_reg
+ 0x04);
179 __raw_writel(val
& TSC_FOURWIRE
, w90p910_ts
->ts_reg
+ 0x04);
180 __raw_writel(ADC_DELAY
, w90p910_ts
->ts_reg
+ 0x08);
182 w90p910_ts
->state
= TS_WAIT_NEW_PACKET
;
185 /* set trigger mode */
186 val
= __raw_readl(w90p910_ts
->ts_reg
);
187 val
|= ADC_WAITTRIG
| ADC_DIV
| ADC_EN
| WT_INT_EN
;
188 __raw_writel(val
, w90p910_ts
->ts_reg
);
193 static void w90p910_close(struct input_dev
*dev
)
195 struct w90p910_ts
*w90p910_ts
= input_get_drvdata(dev
);
198 /* disable trigger mode */
200 spin_lock_irq(&w90p910_ts
->lock
);
202 w90p910_ts
->state
= TS_IDLE
;
204 val
= __raw_readl(w90p910_ts
->ts_reg
);
205 val
&= ~(ADC_WAITTRIG
| ADC_DIV
| ADC_EN
| WT_INT_EN
| ADC_INT_EN
);
206 __raw_writel(val
, w90p910_ts
->ts_reg
);
208 spin_unlock_irq(&w90p910_ts
->lock
);
210 /* Now that interrupts are shut off we can safely delete timer */
211 del_timer_sync(&w90p910_ts
->timer
);
213 /* stop the ADC clock */
214 val
= __raw_readl(w90p910_ts
->clocken
);
215 __raw_writel(val
& ~ADC_CLK_EN
, w90p910_ts
->clocken
);
218 static int __devinit
w90x900ts_probe(struct platform_device
*pdev
)
220 struct w90p910_ts
*w90p910_ts
;
221 struct input_dev
*input_dev
;
222 struct resource
*res
;
225 w90p910_ts
= kzalloc(sizeof(struct w90p910_ts
), GFP_KERNEL
);
226 input_dev
= input_allocate_device();
227 if (!w90p910_ts
|| !input_dev
) {
232 w90p910_ts
->input
= input_dev
;
233 w90p910_ts
->state
= TS_IDLE
;
234 spin_lock_init(&w90p910_ts
->lock
);
235 setup_timer(&w90p910_ts
->timer
, w90p910_check_pen_up
,
236 (unsigned long)&w90p910_ts
);
238 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
244 if (!request_mem_region(res
->start
, res
->end
- res
->start
+ 1,
250 w90p910_ts
->ts_reg
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
251 if (!w90p910_ts
->ts_reg
) {
256 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
262 w90p910_ts
->clocken
= (void __iomem
*)res
->start
;
264 input_dev
->name
= "W90P910 TouchScreen";
265 input_dev
->phys
= "w90p910ts/event0";
266 input_dev
->id
.bustype
= BUS_HOST
;
267 input_dev
->id
.vendor
= 0x0005;
268 input_dev
->id
.product
= 0x0001;
269 input_dev
->id
.version
= 0x0100;
270 input_dev
->dev
.parent
= &pdev
->dev
;
271 input_dev
->open
= w90p910_open
;
272 input_dev
->close
= w90p910_close
;
274 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
275 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
277 input_set_abs_params(input_dev
, ABS_X
, 0, 0x400, 0, 0);
278 input_set_abs_params(input_dev
, ABS_Y
, 0, 0x400, 0, 0);
280 input_set_drvdata(input_dev
, w90p910_ts
);
282 w90p910_ts
->irq_num
= platform_get_irq(pdev
, 0);
283 if (request_irq(w90p910_ts
->irq_num
, w90p910_ts_interrupt
,
284 IRQF_DISABLED
, "w90p910ts", w90p910_ts
)) {
289 err
= input_register_device(w90p910_ts
->input
);
293 platform_set_drvdata(pdev
, w90p910_ts
);
297 fail4
: free_irq(w90p910_ts
->irq_num
, w90p910_ts
);
298 fail3
: iounmap(w90p910_ts
->ts_reg
);
299 fail2
: release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
300 fail1
: input_free_device(input_dev
);
305 static int __devexit
w90x900ts_remove(struct platform_device
*pdev
)
307 struct w90p910_ts
*w90p910_ts
= platform_get_drvdata(pdev
);
308 struct resource
*res
;
310 free_irq(w90p910_ts
->irq_num
, w90p910_ts
);
311 del_timer_sync(&w90p910_ts
->timer
);
312 iounmap(w90p910_ts
->ts_reg
);
314 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
315 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
317 input_unregister_device(w90p910_ts
->input
);
320 platform_set_drvdata(pdev
, NULL
);
325 static struct platform_driver w90x900ts_driver
= {
326 .probe
= w90x900ts_probe
,
327 .remove
= __devexit_p(w90x900ts_remove
),
329 .name
= "w90x900-ts",
330 .owner
= THIS_MODULE
,
334 static int __init
w90x900ts_init(void)
336 return platform_driver_register(&w90x900ts_driver
);
339 static void __exit
w90x900ts_exit(void)
341 platform_driver_unregister(&w90x900ts_driver
);
344 module_init(w90x900ts_init
);
345 module_exit(w90x900ts_exit
);
347 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
348 MODULE_DESCRIPTION("w90p910 touch screen driver!");
349 MODULE_LICENSE("GPL");
350 MODULE_ALIAS("platform:w90p910-ts");