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/clk.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
20 /* ADC controller bit defines */
21 #define ADC_DELAY 0xf00
23 #define ADC_TSC_Y (0x01 << 8)
24 #define ADC_TSC_X (0x00 << 8)
25 #define TSC_FOURWIRE (~(0x03 << 1))
26 #define ADC_CLK_EN (0x01 << 28) /* ADC clock enable */
27 #define ADC_READ_CON (0x01 << 12)
28 #define ADC_CONV (0x01 << 13)
29 #define ADC_SEMIAUTO (0x01 << 14)
30 #define ADC_WAITTRIG (0x03 << 14)
31 #define ADC_RST1 (0x01 << 16)
32 #define ADC_RST0 (0x00 << 16)
33 #define ADC_EN (0x01 << 17)
34 #define ADC_INT (0x01 << 18)
35 #define WT_INT (0x01 << 20)
36 #define ADC_INT_EN (0x01 << 21)
37 #define LVD_INT_EN (0x01 << 22)
38 #define WT_INT_EN (0x01 << 23)
39 #define ADC_DIV (0x04 << 1) /* div = 6 */
42 TS_WAIT_NEW_PACKET
, /* We are waiting next touch report */
43 TS_WAIT_X_COORD
, /* We are waiting for ADC to report X coord */
44 TS_WAIT_Y_COORD
, /* We are waiting for ADC to report Y coord */
45 TS_IDLE
, /* Input device is closed, don't do anything */
49 struct input_dev
*input
;
50 struct timer_list timer
;
58 static void w90p910_report_event(struct w90p910_ts
*w90p910_ts
, bool down
)
60 struct input_dev
*dev
= w90p910_ts
->input
;
63 input_report_abs(dev
, ABS_X
,
64 __raw_readl(w90p910_ts
->ts_reg
+ 0x0c));
65 input_report_abs(dev
, ABS_Y
,
66 __raw_readl(w90p910_ts
->ts_reg
+ 0x10));
69 input_report_key(dev
, BTN_TOUCH
, down
);
73 static void w90p910_prepare_x_reading(struct w90p910_ts
*w90p910_ts
)
77 __raw_writel(ADC_TSC_X
, w90p910_ts
->ts_reg
+ 0x04);
78 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
79 ctlreg
&= ~(ADC_WAITTRIG
| WT_INT
| WT_INT_EN
);
80 ctlreg
|= ADC_SEMIAUTO
| ADC_INT_EN
| ADC_CONV
;
81 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
83 w90p910_ts
->state
= TS_WAIT_X_COORD
;
86 static void w90p910_prepare_y_reading(struct w90p910_ts
*w90p910_ts
)
90 __raw_writel(ADC_TSC_Y
, w90p910_ts
->ts_reg
+ 0x04);
91 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
92 ctlreg
&= ~(ADC_WAITTRIG
| ADC_INT
| WT_INT_EN
);
93 ctlreg
|= ADC_SEMIAUTO
| ADC_INT_EN
| ADC_CONV
;
94 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
96 w90p910_ts
->state
= TS_WAIT_Y_COORD
;
99 static void w90p910_prepare_next_packet(struct w90p910_ts
*w90p910_ts
)
101 unsigned long ctlreg
;
103 ctlreg
= __raw_readl(w90p910_ts
->ts_reg
);
104 ctlreg
&= ~(ADC_INT
| ADC_INT_EN
| ADC_SEMIAUTO
| ADC_CONV
);
105 ctlreg
|= ADC_WAITTRIG
| WT_INT_EN
;
106 __raw_writel(ctlreg
, w90p910_ts
->ts_reg
);
108 w90p910_ts
->state
= TS_WAIT_NEW_PACKET
;
111 static irqreturn_t
w90p910_ts_interrupt(int irq
, void *dev_id
)
113 struct w90p910_ts
*w90p910_ts
= dev_id
;
116 spin_lock_irqsave(&w90p910_ts
->lock
, flags
);
118 switch (w90p910_ts
->state
) {
119 case TS_WAIT_NEW_PACKET
:
121 * The controller only generates interrupts when pen
124 del_timer(&w90p910_ts
->timer
);
125 w90p910_prepare_x_reading(w90p910_ts
);
129 case TS_WAIT_X_COORD
:
130 w90p910_prepare_y_reading(w90p910_ts
);
133 case TS_WAIT_Y_COORD
:
134 w90p910_report_event(w90p910_ts
, true);
135 w90p910_prepare_next_packet(w90p910_ts
);
136 mod_timer(&w90p910_ts
->timer
, jiffies
+ msecs_to_jiffies(100));
143 spin_unlock_irqrestore(&w90p910_ts
->lock
, flags
);
148 static void w90p910_check_pen_up(unsigned long data
)
150 struct w90p910_ts
*w90p910_ts
= (struct w90p910_ts
*) data
;
153 spin_lock_irqsave(&w90p910_ts
->lock
, flags
);
155 if (w90p910_ts
->state
== TS_WAIT_NEW_PACKET
&&
156 !(__raw_readl(w90p910_ts
->ts_reg
+ 0x04) & ADC_DOWN
)) {
158 w90p910_report_event(w90p910_ts
, false);
161 spin_unlock_irqrestore(&w90p910_ts
->lock
, flags
);
164 static int w90p910_open(struct input_dev
*dev
)
166 struct w90p910_ts
*w90p910_ts
= input_get_drvdata(dev
);
169 /* enable the ADC clock */
170 clk_enable(w90p910_ts
->clk
);
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 clk_disable(w90p910_ts
->clk
);
217 static int __devinit
w90x900ts_probe(struct platform_device
*pdev
)
219 struct w90p910_ts
*w90p910_ts
;
220 struct input_dev
*input_dev
;
221 struct resource
*res
;
224 w90p910_ts
= kzalloc(sizeof(struct w90p910_ts
), GFP_KERNEL
);
225 input_dev
= input_allocate_device();
226 if (!w90p910_ts
|| !input_dev
) {
231 w90p910_ts
->input
= input_dev
;
232 w90p910_ts
->state
= TS_IDLE
;
233 spin_lock_init(&w90p910_ts
->lock
);
234 setup_timer(&w90p910_ts
->timer
, w90p910_check_pen_up
,
235 (unsigned long)&w90p910_ts
);
237 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
243 if (!request_mem_region(res
->start
, resource_size(res
),
249 w90p910_ts
->ts_reg
= ioremap(res
->start
, resource_size(res
));
250 if (!w90p910_ts
->ts_reg
) {
255 w90p910_ts
->clk
= clk_get(&pdev
->dev
, NULL
);
256 if (IS_ERR(w90p910_ts
->clk
)) {
257 err
= PTR_ERR(w90p910_ts
->clk
);
261 input_dev
->name
= "W90P910 TouchScreen";
262 input_dev
->phys
= "w90p910ts/event0";
263 input_dev
->id
.bustype
= BUS_HOST
;
264 input_dev
->id
.vendor
= 0x0005;
265 input_dev
->id
.product
= 0x0001;
266 input_dev
->id
.version
= 0x0100;
267 input_dev
->dev
.parent
= &pdev
->dev
;
268 input_dev
->open
= w90p910_open
;
269 input_dev
->close
= w90p910_close
;
271 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
272 input_dev
->keybit
[BIT_WORD(BTN_TOUCH
)] = BIT_MASK(BTN_TOUCH
);
274 input_set_abs_params(input_dev
, ABS_X
, 0, 0x400, 0, 0);
275 input_set_abs_params(input_dev
, ABS_Y
, 0, 0x400, 0, 0);
277 input_set_drvdata(input_dev
, w90p910_ts
);
279 w90p910_ts
->irq_num
= platform_get_irq(pdev
, 0);
280 if (request_irq(w90p910_ts
->irq_num
, w90p910_ts_interrupt
,
281 IRQF_DISABLED
, "w90p910ts", w90p910_ts
)) {
286 err
= input_register_device(w90p910_ts
->input
);
290 platform_set_drvdata(pdev
, w90p910_ts
);
294 fail5
: free_irq(w90p910_ts
->irq_num
, w90p910_ts
);
295 fail4
: clk_put(w90p910_ts
->clk
);
296 fail3
: iounmap(w90p910_ts
->ts_reg
);
297 fail2
: release_mem_region(res
->start
, resource_size(res
));
298 fail1
: input_free_device(input_dev
);
303 static int __devexit
w90x900ts_remove(struct platform_device
*pdev
)
305 struct w90p910_ts
*w90p910_ts
= platform_get_drvdata(pdev
);
306 struct resource
*res
;
308 free_irq(w90p910_ts
->irq_num
, w90p910_ts
);
309 del_timer_sync(&w90p910_ts
->timer
);
310 iounmap(w90p910_ts
->ts_reg
);
312 clk_put(w90p910_ts
->clk
);
314 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
315 release_mem_region(res
->start
, resource_size(res
));
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
),
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:nuc900-ts");