]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/touchscreen/corgi_ts.c
[PATCH] Corgi touchscreen: Fix a pmu bug
[karo-tx-linux.git] / drivers / input / touchscreen / corgi_ts.c
1 /*
2  *  Touchscreen driver for Sharp Corgi models (SL-C7xx)
3  *
4  *  Copyright (c) 2004-2005 Richard Purdie
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 version 2 as
8  *  published by the Free Software Foundation.
9  *
10  */
11
12
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/init.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <asm/irq.h>
21
22 #include <asm/arch/corgi.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/pxa-regs.h>
25
26
27 #define PWR_MODE_ACTIVE         0
28 #define PWR_MODE_SUSPEND        1
29
30 #define X_AXIS_MAX              3830
31 #define X_AXIS_MIN              150
32 #define Y_AXIS_MAX              3830
33 #define Y_AXIS_MIN              190
34 #define PRESSURE_MIN            0
35 #define PRESSURE_MAX            15000
36
37 struct ts_event {
38         short pressure;
39         short x;
40         short y;
41 };
42
43 struct corgi_ts {
44         char phys[32];
45         struct input_dev input;
46         struct timer_list timer;
47         struct ts_event tc;
48         int pendown;
49         int power_mode;
50 };
51
52 #define STATUS_HSYNC            (GPLR(CORGI_GPIO_HSYNC) & GPIO_bit(CORGI_GPIO_HSYNC))
53
54 #define SyncHS()        while((STATUS_HSYNC) == 0); while((STATUS_HSYNC) != 0);
55 #define CCNT(a)         asm volatile ("mrc p14, 0, %0, C1, C0, 0" : "=r"(a))
56 #define PMNC_GET(x)     asm volatile ("mrc p14, 0, %0, C0, C0, 0" : "=r"(x))
57 #define PMNC_SET(x)     asm volatile ("mcr p14, 0, %0, C0, C0, 0" : : "r"(x))
58
59 #define WAIT_HS_400_VGA         7013U   // 17.615us
60 #define WAIT_HS_400_QVGA        16622U  // 41.750us
61
62
63 /* ADS7846 Touch Screen Controller bit definitions */
64 #define ADSCTRL_PD0             (1u << 0)       /* PD0 */
65 #define ADSCTRL_PD1             (1u << 1)       /* PD1 */
66 #define ADSCTRL_DFR             (1u << 2)       /* SER/DFR */
67 #define ADSCTRL_MOD             (1u << 3)       /* Mode */
68 #define ADSCTRL_ADR_SH  4       /* Address setting */
69 #define ADSCTRL_STS             (1u << 7)       /* Start Bit */
70
71 /* External Functions */
72 extern int w100fb_get_xres(void);
73 extern int w100fb_get_blanking(void);
74 extern int w100fb_get_fastsysclk(void);
75 extern unsigned int get_clk_frequency_khz(int info);
76
77 static unsigned long calc_waittime(void)
78 {
79         int w100fb_xres = w100fb_get_xres();
80         unsigned int waittime = 0;
81
82         if (w100fb_get_blanking())
83                 return 0;
84
85         if (w100fb_xres == 480 || w100fb_xres == 640) {
86                 waittime = WAIT_HS_400_VGA * get_clk_frequency_khz(0) / 398131U;
87
88                 if (w100fb_get_fastsysclk() == 100)
89                         waittime = waittime * 75 / 100;
90
91                 if (w100fb_xres == 640)
92                         waittime *= 3;
93
94                 return waittime;
95         }
96
97         return WAIT_HS_400_QVGA * get_clk_frequency_khz(0) / 398131U;
98 }
99
100 static int sync_receive_data_send_cmd(int doRecive, int doSend, unsigned int address, unsigned long wait_time)
101 {
102         unsigned long timer1 = 0, timer2, pmnc = 0;
103         int pos = 0;
104
105         if (wait_time && doSend) {
106                 PMNC_GET(pmnc);
107                 if (!(pmnc & 0x01))
108                         PMNC_SET(0x01);
109
110                 /* polling HSync */
111                 SyncHS();
112                 /* get CCNT */
113                 CCNT(timer1);
114         }
115
116         if (doRecive)
117                 pos = corgi_ssp_ads7846_get();
118
119         if (doSend) {
120                 int cmd = ADSCTRL_PD0 | ADSCTRL_PD1 | (address << ADSCTRL_ADR_SH) | ADSCTRL_STS;
121                 /* dummy command */
122                 corgi_ssp_ads7846_put(cmd);
123                 corgi_ssp_ads7846_get();
124
125                 if (wait_time) {
126                         /* Wait after HSync */
127                         CCNT(timer2);
128                         if (timer2-timer1 > wait_time) {
129                                 /* too slow - timeout, try again */
130                                 SyncHS();
131                                 /* get OSCR */
132                                 CCNT(timer1);
133                                 /* Wait after HSync */
134                                 CCNT(timer2);
135                         }
136                         while (timer2 - timer1 < wait_time)
137                                 CCNT(timer2);
138                 }
139                 corgi_ssp_ads7846_put(cmd);
140                 if (wait_time && !(pmnc & 0x01))
141                         PMNC_SET(pmnc);
142         }
143         return pos;
144 }
145
146 static int read_xydata(struct corgi_ts *corgi_ts)
147 {
148         unsigned int x, y, z1, z2;
149         unsigned long flags, wait_time;
150
151         /* critical section */
152         local_irq_save(flags);
153         corgi_ssp_ads7846_lock();
154         wait_time=calc_waittime();
155
156         /* Y-axis */
157         sync_receive_data_send_cmd(0, 1, 1u, wait_time);
158
159         /* Y-axis */
160         sync_receive_data_send_cmd(1, 1, 1u, wait_time);
161
162         /* X-axis */
163         y = sync_receive_data_send_cmd(1, 1, 5u, wait_time);
164
165         /* Z1 */
166         x = sync_receive_data_send_cmd(1, 1, 3u, wait_time);
167
168         /* Z2 */
169         z1 = sync_receive_data_send_cmd(1, 1, 4u, wait_time);
170         z2 = sync_receive_data_send_cmd(1, 0, 4u, wait_time);
171
172         /* Power-Down Enable */
173         corgi_ssp_ads7846_put((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
174         corgi_ssp_ads7846_get();
175
176         corgi_ssp_ads7846_unlock();
177         local_irq_restore(flags);
178
179         if (x== 0 || y == 0 || z1 == 0 || (x * (z2 - z1) / z1) >= 15000) {
180                 corgi_ts->tc.pressure = 0;
181                 return 0;
182         }
183
184         corgi_ts->tc.x = x;
185         corgi_ts->tc.y = y;
186         corgi_ts->tc.pressure = (x * (z2 - z1)) / z1;
187         return 1;
188 }
189
190 static void new_data(struct corgi_ts *corgi_ts, struct pt_regs *regs)
191 {
192         if (corgi_ts->power_mode != PWR_MODE_ACTIVE)
193                 return;
194
195         if (!corgi_ts->tc.pressure && corgi_ts->pendown == 0)
196                 return;
197
198         if (regs)
199                 input_regs(&corgi_ts->input, regs);
200
201         input_report_abs(&corgi_ts->input, ABS_X, corgi_ts->tc.x);
202         input_report_abs(&corgi_ts->input, ABS_Y, corgi_ts->tc.y);
203         input_report_abs(&corgi_ts->input, ABS_PRESSURE, corgi_ts->tc.pressure);
204         input_report_key(&corgi_ts->input, BTN_TOUCH, (corgi_ts->pendown != 0));
205         input_sync(&corgi_ts->input);
206 }
207
208 static void ts_interrupt_main(struct corgi_ts *corgi_ts, int isTimer, struct pt_regs *regs)
209 {
210         if ((GPLR(CORGI_GPIO_TP_INT) & GPIO_bit(CORGI_GPIO_TP_INT)) == 0) {
211                 /* Disable Interrupt */
212                 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_NOEDGE);
213                 if (read_xydata(corgi_ts)) {
214                         corgi_ts->pendown = 1;
215                         new_data(corgi_ts, regs);
216                 }
217                 mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
218         } else {
219                 if (corgi_ts->pendown == 1 || corgi_ts->pendown == 2) {
220                         mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
221                         corgi_ts->pendown++;
222                         return;
223                 }
224
225                 if (corgi_ts->pendown) {
226                         corgi_ts->tc.pressure = 0;
227                         new_data(corgi_ts, regs);
228                 }
229
230                 /* Enable Falling Edge */
231                 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
232                 corgi_ts->pendown = 0;
233         }
234 }
235
236 static void corgi_ts_timer(unsigned long data)
237 {
238         struct corgi_ts *corgits_data = (struct corgi_ts *) data;
239         ts_interrupt_main(corgits_data, 1, NULL);
240 }
241
242 static irqreturn_t ts_interrupt(int irq, void *dev_id, struct pt_regs *regs)
243 {
244         struct corgi_ts *corgits_data = dev_id;
245         ts_interrupt_main(corgits_data, 0, regs);
246         return IRQ_HANDLED;
247 }
248
249 #ifdef CONFIG_PM
250 static int corgits_suspend(struct device *dev, pm_message_t state, uint32_t level)
251 {
252         if (level == SUSPEND_POWER_DOWN) {
253                 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
254
255                 if (corgi_ts->pendown) {
256                         del_timer_sync(&corgi_ts->timer);
257                         corgi_ts->tc.pressure = 0;
258                         new_data(corgi_ts, NULL);
259                         corgi_ts->pendown = 0;
260                 }
261                 corgi_ts->power_mode = PWR_MODE_SUSPEND;
262
263                 corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
264         }
265         return 0;
266 }
267
268 static int corgits_resume(struct device *dev, uint32_t level)
269 {
270         if (level == RESUME_POWER_ON) {
271                 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
272
273                 corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
274                 /* Enable Falling Edge */
275                 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
276                 corgi_ts->power_mode = PWR_MODE_ACTIVE;
277         }
278         return 0;
279 }
280 #else
281 #define corgits_suspend         NULL
282 #define corgits_resume          NULL
283 #endif
284
285 static int __init corgits_probe(struct device *dev)
286 {
287         struct corgi_ts *corgi_ts;
288
289         if (!(corgi_ts = kmalloc(sizeof(struct corgi_ts), GFP_KERNEL)))
290                 return -ENOMEM;
291
292         dev_set_drvdata(dev, corgi_ts);
293
294         memset(corgi_ts, 0, sizeof(struct corgi_ts));
295
296         init_input_dev(&corgi_ts->input);
297         corgi_ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
298         corgi_ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
299         input_set_abs_params(&corgi_ts->input, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
300         input_set_abs_params(&corgi_ts->input, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
301         input_set_abs_params(&corgi_ts->input, ABS_PRESSURE, PRESSURE_MIN, PRESSURE_MAX, 0, 0);
302
303         strcpy(corgi_ts->phys, "corgits/input0");
304
305         corgi_ts->input.private = corgi_ts;
306         corgi_ts->input.name = "Corgi Touchscreen";
307         corgi_ts->input.dev = dev;
308         corgi_ts->input.phys = corgi_ts->phys;
309         corgi_ts->input.id.bustype = BUS_HOST;
310         corgi_ts->input.id.vendor = 0x0001;
311         corgi_ts->input.id.product = 0x0002;
312         corgi_ts->input.id.version = 0x0100;
313
314         pxa_gpio_mode(CORGI_GPIO_TP_INT | GPIO_IN);
315         pxa_gpio_mode(CORGI_GPIO_HSYNC | GPIO_IN);
316
317         /* Initiaize ADS7846 Difference Reference mode */
318         corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
319         mdelay(5);
320         corgi_ssp_ads7846_putget((3u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
321         mdelay(5);
322         corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
323         mdelay(5);
324         corgi_ssp_ads7846_putget((5u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
325         mdelay(5);
326
327         init_timer(&corgi_ts->timer);
328         corgi_ts->timer.data = (unsigned long) corgi_ts;
329         corgi_ts->timer.function = corgi_ts_timer;
330
331         input_register_device(&corgi_ts->input);
332         corgi_ts->power_mode = PWR_MODE_ACTIVE;
333
334         if (request_irq(CORGI_IRQ_GPIO_TP_INT, ts_interrupt, SA_INTERRUPT, "ts", corgi_ts)) {
335                 input_unregister_device(&corgi_ts->input);
336                 kfree(corgi_ts);
337                 return -EBUSY;
338         }
339
340         /* Enable Falling Edge */
341         set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
342
343         printk(KERN_INFO "input: Corgi Touchscreen Registered\n");
344
345         return 0;
346 }
347
348 static int corgits_remove(struct device *dev)
349 {
350         struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
351
352         free_irq(CORGI_IRQ_GPIO_TP_INT, NULL);
353         del_timer_sync(&corgi_ts->timer);
354         input_unregister_device(&corgi_ts->input);
355         kfree(corgi_ts);
356         return 0;
357 }
358
359 static struct device_driver corgits_driver = {
360         .name           = "corgi-ts",
361         .bus            = &platform_bus_type,
362         .probe          = corgits_probe,
363         .remove         = corgits_remove,
364         .suspend        = corgits_suspend,
365         .resume         = corgits_resume,
366 };
367
368 static int __devinit corgits_init(void)
369 {
370         return driver_register(&corgits_driver);
371 }
372
373 static void __exit corgits_exit(void)
374 {
375         driver_unregister(&corgits_driver);
376 }
377
378 module_init(corgits_init);
379 module_exit(corgits_exit);
380
381 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
382 MODULE_DESCRIPTION("Corgi TouchScreen Driver");
383 MODULE_LICENSE("GPL");