corgi_ts.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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/platform_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 <linux/irq.h>
  19. #include <mach/sharpsl.h>
  20. #include <mach/hardware.h>
  21. #include <mach/pxa-regs.h>
  22. #include <mach/pxa2xx-gpio.h>
  23. #define PWR_MODE_ACTIVE 0
  24. #define PWR_MODE_SUSPEND 1
  25. #define X_AXIS_MAX 3830
  26. #define X_AXIS_MIN 150
  27. #define Y_AXIS_MAX 3830
  28. #define Y_AXIS_MIN 190
  29. #define PRESSURE_MIN 0
  30. #define PRESSURE_MAX 15000
  31. struct ts_event {
  32. short pressure;
  33. short x;
  34. short y;
  35. };
  36. struct corgi_ts {
  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_invperiod = corgi_ts->machinfo->get_hsync_invperiod();
  67. if (hsync_invperiod)
  68. return get_clk_frequency_khz(0)*1000/hsync_invperiod;
  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 CCNT */
  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)
  147. {
  148. struct input_dev *dev = corgi_ts->input;
  149. if (corgi_ts->power_mode != PWR_MODE_ACTIVE)
  150. return;
  151. if (!corgi_ts->tc.pressure && corgi_ts->pendown == 0)
  152. return;
  153. input_report_abs(dev, ABS_X, corgi_ts->tc.x);
  154. input_report_abs(dev, ABS_Y, corgi_ts->tc.y);
  155. input_report_abs(dev, ABS_PRESSURE, corgi_ts->tc.pressure);
  156. input_report_key(dev, BTN_TOUCH, corgi_ts->pendown);
  157. input_sync(dev);
  158. }
  159. static void ts_interrupt_main(struct corgi_ts *corgi_ts, int isTimer)
  160. {
  161. if ((GPLR(IRQ_TO_GPIO(corgi_ts->irq_gpio)) & GPIO_bit(IRQ_TO_GPIO(corgi_ts->irq_gpio))) == 0) {
  162. /* Disable Interrupt */
  163. set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_NONE);
  164. if (read_xydata(corgi_ts)) {
  165. corgi_ts->pendown = 1;
  166. new_data(corgi_ts);
  167. }
  168. mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
  169. } else {
  170. if (corgi_ts->pendown == 1 || corgi_ts->pendown == 2) {
  171. mod_timer(&corgi_ts->timer, jiffies + HZ / 100);
  172. corgi_ts->pendown++;
  173. return;
  174. }
  175. if (corgi_ts->pendown) {
  176. corgi_ts->tc.pressure = 0;
  177. new_data(corgi_ts);
  178. }
  179. /* Enable Falling Edge */
  180. set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);
  181. corgi_ts->pendown = 0;
  182. }
  183. }
  184. static void corgi_ts_timer(unsigned long data)
  185. {
  186. struct corgi_ts *corgits_data = (struct corgi_ts *) data;
  187. ts_interrupt_main(corgits_data, 1);
  188. }
  189. static irqreturn_t ts_interrupt(int irq, void *dev_id)
  190. {
  191. struct corgi_ts *corgits_data = dev_id;
  192. ts_interrupt_main(corgits_data, 0);
  193. return IRQ_HANDLED;
  194. }
  195. #ifdef CONFIG_PM
  196. static int corgits_suspend(struct platform_device *dev, pm_message_t state)
  197. {
  198. struct corgi_ts *corgi_ts = platform_get_drvdata(dev);
  199. if (corgi_ts->pendown) {
  200. del_timer_sync(&corgi_ts->timer);
  201. corgi_ts->tc.pressure = 0;
  202. new_data(corgi_ts);
  203. corgi_ts->pendown = 0;
  204. }
  205. corgi_ts->power_mode = PWR_MODE_SUSPEND;
  206. corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  207. return 0;
  208. }
  209. static int corgits_resume(struct platform_device *dev)
  210. {
  211. struct corgi_ts *corgi_ts = platform_get_drvdata(dev);
  212. corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  213. /* Enable Falling Edge */
  214. set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);
  215. corgi_ts->power_mode = PWR_MODE_ACTIVE;
  216. return 0;
  217. }
  218. #else
  219. #define corgits_suspend NULL
  220. #define corgits_resume NULL
  221. #endif
  222. static int __init corgits_probe(struct platform_device *pdev)
  223. {
  224. struct corgi_ts *corgi_ts;
  225. struct input_dev *input_dev;
  226. int err = -ENOMEM;
  227. corgi_ts = kzalloc(sizeof(struct corgi_ts), GFP_KERNEL);
  228. input_dev = input_allocate_device();
  229. if (!corgi_ts || !input_dev)
  230. goto fail1;
  231. platform_set_drvdata(pdev, corgi_ts);
  232. corgi_ts->machinfo = pdev->dev.platform_data;
  233. corgi_ts->irq_gpio = platform_get_irq(pdev, 0);
  234. if (corgi_ts->irq_gpio < 0) {
  235. err = -ENODEV;
  236. goto fail1;
  237. }
  238. corgi_ts->input = input_dev;
  239. init_timer(&corgi_ts->timer);
  240. corgi_ts->timer.data = (unsigned long) corgi_ts;
  241. corgi_ts->timer.function = corgi_ts_timer;
  242. input_dev->name = "Corgi Touchscreen";
  243. input_dev->phys = "corgits/input0";
  244. input_dev->id.bustype = BUS_HOST;
  245. input_dev->id.vendor = 0x0001;
  246. input_dev->id.product = 0x0002;
  247. input_dev->id.version = 0x0100;
  248. input_dev->dev.parent = &pdev->dev;
  249. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  250. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  251. input_set_abs_params(input_dev, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
  252. input_set_abs_params(input_dev, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
  253. input_set_abs_params(input_dev, ABS_PRESSURE, PRESSURE_MIN, PRESSURE_MAX, 0, 0);
  254. pxa_gpio_mode(IRQ_TO_GPIO(corgi_ts->irq_gpio) | GPIO_IN);
  255. /* Initiaize ADS7846 Difference Reference mode */
  256. corgi_ssp_ads7846_putget((1u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  257. mdelay(5);
  258. corgi_ssp_ads7846_putget((3u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  259. mdelay(5);
  260. corgi_ssp_ads7846_putget((4u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  261. mdelay(5);
  262. corgi_ssp_ads7846_putget((5u << ADSCTRL_ADR_SH) | ADSCTRL_STS);
  263. mdelay(5);
  264. if (request_irq(corgi_ts->irq_gpio, ts_interrupt, IRQF_DISABLED, "ts", corgi_ts)) {
  265. err = -EBUSY;
  266. goto fail1;
  267. }
  268. err = input_register_device(corgi_ts->input);
  269. if (err)
  270. goto fail2;
  271. corgi_ts->power_mode = PWR_MODE_ACTIVE;
  272. /* Enable Falling Edge */
  273. set_irq_type(corgi_ts->irq_gpio, IRQ_TYPE_EDGE_FALLING);
  274. return 0;
  275. fail2: free_irq(corgi_ts->irq_gpio, corgi_ts);
  276. fail1: input_free_device(input_dev);
  277. kfree(corgi_ts);
  278. return err;
  279. }
  280. static int corgits_remove(struct platform_device *pdev)
  281. {
  282. struct corgi_ts *corgi_ts = platform_get_drvdata(pdev);
  283. free_irq(corgi_ts->irq_gpio, corgi_ts);
  284. del_timer_sync(&corgi_ts->timer);
  285. corgi_ts->machinfo->put_hsync();
  286. input_unregister_device(corgi_ts->input);
  287. kfree(corgi_ts);
  288. return 0;
  289. }
  290. static struct platform_driver corgits_driver = {
  291. .probe = corgits_probe,
  292. .remove = corgits_remove,
  293. .suspend = corgits_suspend,
  294. .resume = corgits_resume,
  295. .driver = {
  296. .name = "corgi-ts",
  297. .owner = THIS_MODULE,
  298. },
  299. };
  300. static int __devinit corgits_init(void)
  301. {
  302. return platform_driver_register(&corgits_driver);
  303. }
  304. static void __exit corgits_exit(void)
  305. {
  306. platform_driver_unregister(&corgits_driver);
  307. }
  308. module_init(corgits_init);
  309. module_exit(corgits_exit);
  310. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  311. MODULE_DESCRIPTION("Corgi TouchScreen Driver");
  312. MODULE_LICENSE("GPL");
  313. MODULE_ALIAS("platform:corgi-ts");