2 * Touchscreen driver for Sharp Corgi models (SL-C7xx)
4 * Copyright (c) 2004-2005 Richard Purdie
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.
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>
22 #include <asm/arch/corgi.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/pxa-regs.h>
27 #define PWR_MODE_ACTIVE 0
28 #define PWR_MODE_SUSPEND 1
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
45 struct input_dev input;
46 struct timer_list timer;
52 #define STATUS_HSYNC (GPLR(CORGI_GPIO_HSYNC) & GPIO_bit(CORGI_GPIO_HSYNC))
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))
59 #define WAIT_HS_400_VGA 7013U // 17.615us
60 #define WAIT_HS_400_QVGA 16622U // 41.750us
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 */
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);
77 static unsigned long calc_waittime(void)
79 int w100fb_xres = w100fb_get_xres();
80 unsigned int waittime = 0;
82 if (w100fb_get_blanking())
85 if (w100fb_xres == 480 || w100fb_xres == 640) {
86 waittime = WAIT_HS_400_VGA * get_clk_frequency_khz(0) / 398131U;
88 if (w100fb_get_fastsysclk() == 100)
89 waittime = waittime * 75 / 100;
91 if (w100fb_xres == 640)
97 return WAIT_HS_400_QVGA * get_clk_frequency_khz(0) / 398131U;
100 static int sync_receive_data_send_cmd(int doRecive, int doSend, unsigned int address, unsigned long wait_time)
102 unsigned long timer1 = 0, timer2, pmnc = 0;
105 if (wait_time && doSend) {
117 pos = corgi_ssp_ads7846_get();
120 int cmd = ADSCTRL_PD0 | ADSCTRL_PD1 | (address << ADSCTRL_ADR_SH) | ADSCTRL_STS;
122 corgi_ssp_ads7846_put(cmd);
123 corgi_ssp_ads7846_get();
126 /* Wait after HSync */
128 if (timer2-timer1 > wait_time) {
129 /* too slow - timeout, try again */
133 /* Wait after HSync */
136 while (timer2 - timer1 < wait_time)
139 corgi_ssp_ads7846_put(cmd);
140 if (wait_time && !(pmnc & 0x01))
146 static int read_xydata(struct corgi_ts *corgi_ts)
148 unsigned int x, y, z1, z2;
149 unsigned long flags, wait_time;
151 /* critical section */
152 local_irq_save(flags);
153 corgi_ssp_ads7846_lock();
154 wait_time=calc_waittime();
157 sync_receive_data_send_cmd(0, 1, 1u, wait_time);
160 sync_receive_data_send_cmd(1, 1, 1u, wait_time);
163 y = sync_receive_data_send_cmd(1, 1, 5u, wait_time);
166 x = sync_receive_data_send_cmd(1, 1, 3u, wait_time);
169 z1 = sync_receive_data_send_cmd(1, 1, 4u, wait_time);
170 z2 = sync_receive_data_send_cmd(1, 0, 4u, wait_time);
172 /* Power-Down Enable */
173 corgi_ssp_ads7846_put((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
174 corgi_ssp_ads7846_get();
176 corgi_ssp_ads7846_unlock();
177 local_irq_restore(flags);
179 if (x== 0 || y == 0 || z1 == 0 || (x * (z2 - z1) / z1) >= 15000) {
180 corgi_ts->tc.pressure = 0;
186 corgi_ts->tc.pressure = (x * (z2 - z1)) / z1;
190 static void new_data(struct corgi_ts *corgi_ts, struct pt_regs *regs)
192 if (corgi_ts->power_mode != PWR_MODE_ACTIVE)
195 if (!corgi_ts->tc.pressure && corgi_ts->pendown == 0)
199 input_regs(&corgi_ts->input, regs);
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);
208 static void ts_interrupt_main(struct corgi_ts *corgi_ts, int isTimer, struct pt_regs *regs)
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);
217 mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
219 if (corgi_ts->pendown == 1 || corgi_ts->pendown == 2) {
220 mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
225 if (corgi_ts->pendown) {
226 corgi_ts->tc.pressure = 0;
227 new_data(corgi_ts, regs);
230 /* Enable Falling Edge */
231 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
232 corgi_ts->pendown = 0;
236 static void corgi_ts_timer(unsigned long data)
238 struct corgi_ts *corgits_data = (struct corgi_ts *) data;
239 ts_interrupt_main(corgits_data, 1, NULL);
242 static irqreturn_t ts_interrupt(int irq, void *dev_id, struct pt_regs *regs)
244 struct corgi_ts *corgits_data = dev_id;
245 ts_interrupt_main(corgits_data, 0, regs);
250 static int corgits_suspend(struct device *dev, pm_message_t state, uint32_t level)
252 if (level == SUSPEND_POWER_DOWN) {
253 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
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;
261 corgi_ts->power_mode = PWR_MODE_SUSPEND;
263 corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
268 static int corgits_resume(struct device *dev, uint32_t level)
270 if (level == RESUME_POWER_ON) {
271 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
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;
281 #define corgits_suspend NULL
282 #define corgits_resume NULL
285 static int __init corgits_probe(struct device *dev)
287 struct corgi_ts *corgi_ts;
289 if (!(corgi_ts = kmalloc(sizeof(struct corgi_ts), GFP_KERNEL)))
292 dev_set_drvdata(dev, corgi_ts);
294 memset(corgi_ts, 0, sizeof(struct corgi_ts));
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);
303 strcpy(corgi_ts->phys, "corgits/input0");
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;
314 pxa_gpio_mode(CORGI_GPIO_TP_INT | GPIO_IN);
315 pxa_gpio_mode(CORGI_GPIO_HSYNC | GPIO_IN);
317 /* Initiaize ADS7846 Difference Reference mode */
318 corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
320 corgi_ssp_ads7846_putget((3u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
322 corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
324 corgi_ssp_ads7846_putget((5u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
327 init_timer(&corgi_ts->timer);
328 corgi_ts->timer.data = (unsigned long) corgi_ts;
329 corgi_ts->timer.function = corgi_ts_timer;
331 input_register_device(&corgi_ts->input);
332 corgi_ts->power_mode = PWR_MODE_ACTIVE;
334 if (request_irq(CORGI_IRQ_GPIO_TP_INT, ts_interrupt, SA_INTERRUPT, "ts", corgi_ts)) {
335 input_unregister_device(&corgi_ts->input);
340 /* Enable Falling Edge */
341 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
343 printk(KERN_INFO "input: Corgi Touchscreen Registered\n");
348 static int corgits_remove(struct device *dev)
350 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
352 free_irq(CORGI_IRQ_GPIO_TP_INT, NULL);
353 del_timer_sync(&corgi_ts->timer);
354 input_unregister_device(&corgi_ts->input);
359 static struct device_driver corgits_driver = {
361 .bus = &platform_bus_type,
362 .probe = corgits_probe,
363 .remove = corgits_remove,
364 .suspend = corgits_suspend,
365 .resume = corgits_resume,
368 static int __devinit corgits_init(void)
370 return driver_register(&corgits_driver);
373 static void __exit corgits_exit(void)
375 driver_unregister(&corgits_driver);
378 module_init(corgits_init);
379 module_exit(corgits_exit);
381 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
382 MODULE_DESCRIPTION("Corgi TouchScreen Driver");
383 MODULE_LICENSE("GPL");