corgi_ts.c 9.4 KB

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