corgi_ts.c 9.4 KB

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