corgi_ts.c 9.1 KB

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