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))
60 /* ADS7846 Touch Screen Controller bit definitions */
61 #define ADSCTRL_PD0 (1u << 0) /* PD0 */
62 #define ADSCTRL_PD1 (1u << 1) /* PD1 */
63 #define ADSCTRL_DFR (1u << 2) /* SER/DFR */
64 #define ADSCTRL_MOD (1u << 3) /* Mode */
65 #define ADSCTRL_ADR_SH 4 /* Address setting */
66 #define ADSCTRL_STS (1u << 7) /* Start Bit */
68 /* External Functions */
69 extern unsigned long w100fb_get_hsynclen(struct device *dev);
70 extern unsigned int get_clk_frequency_khz(int info);
72 static unsigned long calc_waittime(void)
74 unsigned long hsync_len = w100fb_get_hsynclen(&corgifb_device.dev);
77 return get_clk_frequency_khz(0)*1000/hsync_len;
82 static int sync_receive_data_send_cmd(int doRecive, int doSend, unsigned int address, unsigned long wait_time)
84 unsigned long timer1 = 0, timer2, pmnc = 0;
87 if (wait_time && doSend) {
99 pos = corgi_ssp_ads7846_get();
102 int cmd = ADSCTRL_PD0 | ADSCTRL_PD1 | (address << ADSCTRL_ADR_SH) | ADSCTRL_STS;
104 corgi_ssp_ads7846_put(cmd);
105 corgi_ssp_ads7846_get();
108 /* Wait after HSync */
110 if (timer2-timer1 > wait_time) {
111 /* too slow - timeout, try again */
115 /* Wait after HSync */
118 while (timer2 - timer1 < wait_time)
121 corgi_ssp_ads7846_put(cmd);
122 if (wait_time && !(pmnc & 0x01))
128 static int read_xydata(struct corgi_ts *corgi_ts)
130 unsigned int x, y, z1, z2;
131 unsigned long flags, wait_time;
133 /* critical section */
134 local_irq_save(flags);
135 corgi_ssp_ads7846_lock();
136 wait_time=calc_waittime();
139 sync_receive_data_send_cmd(0, 1, 1u, wait_time);
142 sync_receive_data_send_cmd(1, 1, 1u, wait_time);
145 y = sync_receive_data_send_cmd(1, 1, 5u, wait_time);
148 x = sync_receive_data_send_cmd(1, 1, 3u, wait_time);
151 z1 = sync_receive_data_send_cmd(1, 1, 4u, wait_time);
152 z2 = sync_receive_data_send_cmd(1, 0, 4u, wait_time);
154 /* Power-Down Enable */
155 corgi_ssp_ads7846_put((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
156 corgi_ssp_ads7846_get();
158 corgi_ssp_ads7846_unlock();
159 local_irq_restore(flags);
161 if (x== 0 || y == 0 || z1 == 0 || (x * (z2 - z1) / z1) >= 15000) {
162 corgi_ts->tc.pressure = 0;
168 corgi_ts->tc.pressure = (x * (z2 - z1)) / z1;
172 static void new_data(struct corgi_ts *corgi_ts, struct pt_regs *regs)
174 if (corgi_ts->power_mode != PWR_MODE_ACTIVE)
177 if (!corgi_ts->tc.pressure && corgi_ts->pendown == 0)
181 input_regs(&corgi_ts->input, regs);
183 input_report_abs(&corgi_ts->input, ABS_X, corgi_ts->tc.x);
184 input_report_abs(&corgi_ts->input, ABS_Y, corgi_ts->tc.y);
185 input_report_abs(&corgi_ts->input, ABS_PRESSURE, corgi_ts->tc.pressure);
186 input_report_key(&corgi_ts->input, BTN_TOUCH, (corgi_ts->pendown != 0));
187 input_sync(&corgi_ts->input);
190 static void ts_interrupt_main(struct corgi_ts *corgi_ts, int isTimer, struct pt_regs *regs)
192 if ((GPLR(CORGI_GPIO_TP_INT) & GPIO_bit(CORGI_GPIO_TP_INT)) == 0) {
193 /* Disable Interrupt */
194 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_NOEDGE);
195 if (read_xydata(corgi_ts)) {
196 corgi_ts->pendown = 1;
197 new_data(corgi_ts, regs);
199 mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
201 if (corgi_ts->pendown == 1 || corgi_ts->pendown == 2) {
202 mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
207 if (corgi_ts->pendown) {
208 corgi_ts->tc.pressure = 0;
209 new_data(corgi_ts, regs);
212 /* Enable Falling Edge */
213 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
214 corgi_ts->pendown = 0;
218 static void corgi_ts_timer(unsigned long data)
220 struct corgi_ts *corgits_data = (struct corgi_ts *) data;
221 ts_interrupt_main(corgits_data, 1, NULL);
224 static irqreturn_t ts_interrupt(int irq, void *dev_id, struct pt_regs *regs)
226 struct corgi_ts *corgits_data = dev_id;
227 ts_interrupt_main(corgits_data, 0, regs);
232 static int corgits_suspend(struct device *dev, pm_message_t state, uint32_t level)
234 if (level == SUSPEND_POWER_DOWN) {
235 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
237 if (corgi_ts->pendown) {
238 del_timer_sync(&corgi_ts->timer);
239 corgi_ts->tc.pressure = 0;
240 new_data(corgi_ts, NULL);
241 corgi_ts->pendown = 0;
243 corgi_ts->power_mode = PWR_MODE_SUSPEND;
245 corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
250 static int corgits_resume(struct device *dev, uint32_t level)
252 if (level == RESUME_POWER_ON) {
253 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
255 corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
256 /* Enable Falling Edge */
257 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
258 corgi_ts->power_mode = PWR_MODE_ACTIVE;
263 #define corgits_suspend NULL
264 #define corgits_resume NULL
267 static int __init corgits_probe(struct device *dev)
269 struct corgi_ts *corgi_ts;
271 if (!(corgi_ts = kmalloc(sizeof(struct corgi_ts), GFP_KERNEL)))
274 dev_set_drvdata(dev, corgi_ts);
276 memset(corgi_ts, 0, sizeof(struct corgi_ts));
278 init_input_dev(&corgi_ts->input);
279 corgi_ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
280 corgi_ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
281 input_set_abs_params(&corgi_ts->input, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
282 input_set_abs_params(&corgi_ts->input, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
283 input_set_abs_params(&corgi_ts->input, ABS_PRESSURE, PRESSURE_MIN, PRESSURE_MAX, 0, 0);
285 strcpy(corgi_ts->phys, "corgits/input0");
287 corgi_ts->input.private = corgi_ts;
288 corgi_ts->input.name = "Corgi Touchscreen";
289 corgi_ts->input.dev = dev;
290 corgi_ts->input.phys = corgi_ts->phys;
291 corgi_ts->input.id.bustype = BUS_HOST;
292 corgi_ts->input.id.vendor = 0x0001;
293 corgi_ts->input.id.product = 0x0002;
294 corgi_ts->input.id.version = 0x0100;
296 pxa_gpio_mode(CORGI_GPIO_TP_INT | GPIO_IN);
297 pxa_gpio_mode(CORGI_GPIO_HSYNC | GPIO_IN);
299 /* Initiaize ADS7846 Difference Reference mode */
300 corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
302 corgi_ssp_ads7846_putget((3u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
304 corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
306 corgi_ssp_ads7846_putget((5u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
309 init_timer(&corgi_ts->timer);
310 corgi_ts->timer.data = (unsigned long) corgi_ts;
311 corgi_ts->timer.function = corgi_ts_timer;
313 input_register_device(&corgi_ts->input);
314 corgi_ts->power_mode = PWR_MODE_ACTIVE;
316 if (request_irq(CORGI_IRQ_GPIO_TP_INT, ts_interrupt, SA_INTERRUPT, "ts", corgi_ts)) {
317 input_unregister_device(&corgi_ts->input);
322 /* Enable Falling Edge */
323 set_irq_type(CORGI_IRQ_GPIO_TP_INT, IRQT_FALLING);
325 printk(KERN_INFO "input: Corgi Touchscreen Registered\n");
330 static int corgits_remove(struct device *dev)
332 struct corgi_ts *corgi_ts = dev_get_drvdata(dev);
334 free_irq(CORGI_IRQ_GPIO_TP_INT, NULL);
335 del_timer_sync(&corgi_ts->timer);
336 input_unregister_device(&corgi_ts->input);
341 static struct device_driver corgits_driver = {
343 .bus = &platform_bus_type,
344 .probe = corgits_probe,
345 .remove = corgits_remove,
346 .suspend = corgits_suspend,
347 .resume = corgits_resume,
350 static int __devinit corgits_init(void)
352 return driver_register(&corgits_driver);
355 static void __exit corgits_exit(void)
357 driver_unregister(&corgits_driver);
360 module_init(corgits_init);
361 module_exit(corgits_exit);
363 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
364 MODULE_DESCRIPTION("Corgi TouchScreen Driver");
365 MODULE_LICENSE("GPL");