1 /*
2  * Copyright (c) 2008 Nuvoton technology corporation.
3  *
4  * Wan ZongShun <mcuos.com@gmail.com>
5  *
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.
9  *
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/io.h>
16 #include <linux/clk.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/slab.h>
20 
21 /* ADC controller bit defines */
22 #define ADC_DELAY	0xf00
23 #define ADC_DOWN	0x01
24 #define ADC_TSC_Y	(0x01 << 8)
25 #define ADC_TSC_X	(0x00 << 8)
26 #define TSC_FOURWIRE	(~(0x03 << 1))
27 #define ADC_CLK_EN	(0x01 << 28)	/* ADC clock enable */
28 #define ADC_READ_CON	(0x01 << 12)
29 #define ADC_CONV	(0x01 << 13)
30 #define ADC_SEMIAUTO	(0x01 << 14)
31 #define ADC_WAITTRIG	(0x03 << 14)
32 #define ADC_RST1	(0x01 << 16)
33 #define ADC_RST0	(0x00 << 16)
34 #define ADC_EN		(0x01 << 17)
35 #define ADC_INT		(0x01 << 18)
36 #define WT_INT		(0x01 << 20)
37 #define ADC_INT_EN	(0x01 << 21)
38 #define LVD_INT_EN	(0x01 << 22)
39 #define WT_INT_EN	(0x01 << 23)
40 #define ADC_DIV		(0x04 << 1)	/* div = 6 */
41 
42 enum ts_state {
43 	TS_WAIT_NEW_PACKET,	/* We are waiting next touch report */
44 	TS_WAIT_X_COORD,	/* We are waiting for ADC to report X coord */
45 	TS_WAIT_Y_COORD,	/* We are waiting for ADC to report Y coord */
46 	TS_IDLE,		/* Input device is closed, don't do anything */
47 };
48 
49 struct w90p910_ts {
50 	struct input_dev *input;
51 	struct timer_list timer;
52 	struct clk *clk;
53 	int irq_num;
54 	void __iomem *ts_reg;
55 	spinlock_t lock;
56 	enum ts_state state;
57 };
58 
w90p910_report_event(struct w90p910_ts * w90p910_ts,bool down)59 static void w90p910_report_event(struct w90p910_ts *w90p910_ts, bool down)
60 {
61 	struct input_dev *dev = w90p910_ts->input;
62 
63 	if (down) {
64 		input_report_abs(dev, ABS_X,
65 				 __raw_readl(w90p910_ts->ts_reg + 0x0c));
66 		input_report_abs(dev, ABS_Y,
67 				 __raw_readl(w90p910_ts->ts_reg + 0x10));
68 	}
69 
70 	input_report_key(dev, BTN_TOUCH, down);
71 	input_sync(dev);
72 }
73 
w90p910_prepare_x_reading(struct w90p910_ts * w90p910_ts)74 static void w90p910_prepare_x_reading(struct w90p910_ts *w90p910_ts)
75 {
76 	unsigned long ctlreg;
77 
78 	__raw_writel(ADC_TSC_X, w90p910_ts->ts_reg + 0x04);
79 	ctlreg = __raw_readl(w90p910_ts->ts_reg);
80 	ctlreg &= ~(ADC_WAITTRIG | WT_INT | WT_INT_EN);
81 	ctlreg |= ADC_SEMIAUTO | ADC_INT_EN | ADC_CONV;
82 	__raw_writel(ctlreg, w90p910_ts->ts_reg);
83 
84 	w90p910_ts->state = TS_WAIT_X_COORD;
85 }
86 
w90p910_prepare_y_reading(struct w90p910_ts * w90p910_ts)87 static void w90p910_prepare_y_reading(struct w90p910_ts *w90p910_ts)
88 {
89 	unsigned long ctlreg;
90 
91 	__raw_writel(ADC_TSC_Y, w90p910_ts->ts_reg + 0x04);
92 	ctlreg = __raw_readl(w90p910_ts->ts_reg);
93 	ctlreg &= ~(ADC_WAITTRIG | ADC_INT | WT_INT_EN);
94 	ctlreg |= ADC_SEMIAUTO | ADC_INT_EN | ADC_CONV;
95 	__raw_writel(ctlreg, w90p910_ts->ts_reg);
96 
97 	w90p910_ts->state = TS_WAIT_Y_COORD;
98 }
99 
w90p910_prepare_next_packet(struct w90p910_ts * w90p910_ts)100 static void w90p910_prepare_next_packet(struct w90p910_ts *w90p910_ts)
101 {
102 	unsigned long ctlreg;
103 
104 	ctlreg = __raw_readl(w90p910_ts->ts_reg);
105 	ctlreg &= ~(ADC_INT | ADC_INT_EN | ADC_SEMIAUTO | ADC_CONV);
106 	ctlreg |= ADC_WAITTRIG | WT_INT_EN;
107 	__raw_writel(ctlreg, w90p910_ts->ts_reg);
108 
109 	w90p910_ts->state = TS_WAIT_NEW_PACKET;
110 }
111 
w90p910_ts_interrupt(int irq,void * dev_id)112 static irqreturn_t w90p910_ts_interrupt(int irq, void *dev_id)
113 {
114 	struct w90p910_ts *w90p910_ts = dev_id;
115 	unsigned long flags;
116 
117 	spin_lock_irqsave(&w90p910_ts->lock, flags);
118 
119 	switch (w90p910_ts->state) {
120 	case TS_WAIT_NEW_PACKET:
121 		/*
122 		 * The controller only generates interrupts when pen
123 		 * is down.
124 		 */
125 		del_timer(&w90p910_ts->timer);
126 		w90p910_prepare_x_reading(w90p910_ts);
127 		break;
128 
129 
130 	case TS_WAIT_X_COORD:
131 		w90p910_prepare_y_reading(w90p910_ts);
132 		break;
133 
134 	case TS_WAIT_Y_COORD:
135 		w90p910_report_event(w90p910_ts, true);
136 		w90p910_prepare_next_packet(w90p910_ts);
137 		mod_timer(&w90p910_ts->timer, jiffies + msecs_to_jiffies(100));
138 		break;
139 
140 	case TS_IDLE:
141 		break;
142 	}
143 
144 	spin_unlock_irqrestore(&w90p910_ts->lock, flags);
145 
146 	return IRQ_HANDLED;
147 }
148 
w90p910_check_pen_up(struct timer_list * t)149 static void w90p910_check_pen_up(struct timer_list *t)
150 {
151 	struct w90p910_ts *w90p910_ts = from_timer(w90p910_ts, t, timer);
152 	unsigned long flags;
153 
154 	spin_lock_irqsave(&w90p910_ts->lock, flags);
155 
156 	if (w90p910_ts->state == TS_WAIT_NEW_PACKET &&
157 	    !(__raw_readl(w90p910_ts->ts_reg + 0x04) & ADC_DOWN)) {
158 
159 		w90p910_report_event(w90p910_ts, false);
160 	}
161 
162 	spin_unlock_irqrestore(&w90p910_ts->lock, flags);
163 }
164 
w90p910_open(struct input_dev * dev)165 static int w90p910_open(struct input_dev *dev)
166 {
167 	struct w90p910_ts *w90p910_ts = input_get_drvdata(dev);
168 	unsigned long val;
169 
170 	/* enable the ADC clock */
171 	clk_enable(w90p910_ts->clk);
172 
173 	__raw_writel(ADC_RST1, w90p910_ts->ts_reg);
174 	msleep(1);
175 	__raw_writel(ADC_RST0, w90p910_ts->ts_reg);
176 	msleep(1);
177 
178 	/* set delay and screen type */
179 	val = __raw_readl(w90p910_ts->ts_reg + 0x04);
180 	__raw_writel(val & TSC_FOURWIRE, w90p910_ts->ts_reg + 0x04);
181 	__raw_writel(ADC_DELAY, w90p910_ts->ts_reg + 0x08);
182 
183 	w90p910_ts->state = TS_WAIT_NEW_PACKET;
184 	wmb();
185 
186 	/* set trigger mode */
187 	val = __raw_readl(w90p910_ts->ts_reg);
188 	val |= ADC_WAITTRIG | ADC_DIV | ADC_EN | WT_INT_EN;
189 	__raw_writel(val, w90p910_ts->ts_reg);
190 
191 	return 0;
192 }
193 
w90p910_close(struct input_dev * dev)194 static void w90p910_close(struct input_dev *dev)
195 {
196 	struct w90p910_ts *w90p910_ts = input_get_drvdata(dev);
197 	unsigned long val;
198 
199 	/* disable trigger mode */
200 
201 	spin_lock_irq(&w90p910_ts->lock);
202 
203 	w90p910_ts->state = TS_IDLE;
204 
205 	val = __raw_readl(w90p910_ts->ts_reg);
206 	val &= ~(ADC_WAITTRIG | ADC_DIV | ADC_EN | WT_INT_EN | ADC_INT_EN);
207 	__raw_writel(val, w90p910_ts->ts_reg);
208 
209 	spin_unlock_irq(&w90p910_ts->lock);
210 
211 	/* Now that interrupts are shut off we can safely delete timer */
212 	del_timer_sync(&w90p910_ts->timer);
213 
214 	/* stop the ADC clock */
215 	clk_disable(w90p910_ts->clk);
216 }
217 
w90x900ts_probe(struct platform_device * pdev)218 static int w90x900ts_probe(struct platform_device *pdev)
219 {
220 	struct w90p910_ts *w90p910_ts;
221 	struct input_dev *input_dev;
222 	struct resource *res;
223 	int err;
224 
225 	w90p910_ts = kzalloc(sizeof(struct w90p910_ts), GFP_KERNEL);
226 	input_dev = input_allocate_device();
227 	if (!w90p910_ts || !input_dev) {
228 		err = -ENOMEM;
229 		goto fail1;
230 	}
231 
232 	w90p910_ts->input = input_dev;
233 	w90p910_ts->state = TS_IDLE;
234 	spin_lock_init(&w90p910_ts->lock);
235 	timer_setup(&w90p910_ts->timer, w90p910_check_pen_up, 0);
236 
237 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 	if (!res) {
239 		err = -ENXIO;
240 		goto fail1;
241 	}
242 
243 	if (!request_mem_region(res->start, resource_size(res),
244 				pdev->name)) {
245 		err = -EBUSY;
246 		goto fail1;
247 	}
248 
249 	w90p910_ts->ts_reg = ioremap(res->start, resource_size(res));
250 	if (!w90p910_ts->ts_reg) {
251 		err = -ENOMEM;
252 		goto fail2;
253 	}
254 
255 	w90p910_ts->clk = clk_get(&pdev->dev, NULL);
256 	if (IS_ERR(w90p910_ts->clk)) {
257 		err = PTR_ERR(w90p910_ts->clk);
258 		goto fail3;
259 	}
260 
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;
270 
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);
273 
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);
276 
277 	input_set_drvdata(input_dev, w90p910_ts);
278 
279 	w90p910_ts->irq_num = platform_get_irq(pdev, 0);
280 	if (request_irq(w90p910_ts->irq_num, w90p910_ts_interrupt,
281 			0, "w90p910ts", w90p910_ts)) {
282 		err = -EBUSY;
283 		goto fail4;
284 	}
285 
286 	err = input_register_device(w90p910_ts->input);
287 	if (err)
288 		goto fail5;
289 
290 	platform_set_drvdata(pdev, w90p910_ts);
291 
292 	return 0;
293 
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);
299 	kfree(w90p910_ts);
300 	return err;
301 }
302 
w90x900ts_remove(struct platform_device * pdev)303 static int w90x900ts_remove(struct platform_device *pdev)
304 {
305 	struct w90p910_ts *w90p910_ts = platform_get_drvdata(pdev);
306 	struct resource *res;
307 
308 	free_irq(w90p910_ts->irq_num, w90p910_ts);
309 	del_timer_sync(&w90p910_ts->timer);
310 	iounmap(w90p910_ts->ts_reg);
311 
312 	clk_put(w90p910_ts->clk);
313 
314 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
315 	release_mem_region(res->start, resource_size(res));
316 
317 	input_unregister_device(w90p910_ts->input);
318 	kfree(w90p910_ts);
319 
320 	return 0;
321 }
322 
323 static struct platform_driver w90x900ts_driver = {
324 	.probe		= w90x900ts_probe,
325 	.remove		= w90x900ts_remove,
326 	.driver		= {
327 		.name	= "nuc900-ts",
328 	},
329 };
330 module_platform_driver(w90x900ts_driver);
331 
332 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
333 MODULE_DESCRIPTION("w90p910 touch screen driver!");
334 MODULE_LICENSE("GPL");
335 MODULE_ALIAS("platform:nuc900-ts");
336