corgi_ts.c 9.6 KB

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