da9052_tsi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * TSI driver for Dialog DA9052
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/input.h>
  16. #include <linux/delay.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/mfd/da9052/reg.h>
  20. #include <linux/mfd/da9052/da9052.h>
  21. #define TSI_PEN_DOWN_STATUS 0x40
  22. struct da9052_tsi {
  23. struct da9052 *da9052;
  24. struct input_dev *dev;
  25. struct delayed_work ts_pen_work;
  26. struct mutex mutex;
  27. bool stopped;
  28. bool adc_on;
  29. };
  30. static void da9052_ts_adc_toggle(struct da9052_tsi *tsi, bool on)
  31. {
  32. da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 0, on);
  33. tsi->adc_on = on;
  34. }
  35. static irqreturn_t da9052_ts_pendwn_irq(int irq, void *data)
  36. {
  37. struct da9052_tsi *tsi = data;
  38. if (!tsi->stopped) {
  39. /* Mask PEN_DOWN event and unmask TSI_READY event */
  40. da9052_disable_irq_nosync(tsi->da9052, DA9052_IRQ_PENDOWN);
  41. da9052_enable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
  42. da9052_ts_adc_toggle(tsi, true);
  43. schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
  44. }
  45. return IRQ_HANDLED;
  46. }
  47. static void da9052_ts_read(struct da9052_tsi *tsi)
  48. {
  49. struct input_dev *input = tsi->dev;
  50. int ret;
  51. u16 x, y, z;
  52. u8 v;
  53. ret = da9052_reg_read(tsi->da9052, DA9052_TSI_X_MSB_REG);
  54. if (ret < 0)
  55. return;
  56. x = (u16) ret;
  57. ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Y_MSB_REG);
  58. if (ret < 0)
  59. return;
  60. y = (u16) ret;
  61. ret = da9052_reg_read(tsi->da9052, DA9052_TSI_Z_MSB_REG);
  62. if (ret < 0)
  63. return;
  64. z = (u16) ret;
  65. ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
  66. if (ret < 0)
  67. return;
  68. v = (u8) ret;
  69. x = ((x << 2) & 0x3fc) | (v & 0x3);
  70. y = ((y << 2) & 0x3fc) | ((v & 0xc) >> 2);
  71. z = ((z << 2) & 0x3fc) | ((v & 0x30) >> 4);
  72. input_report_key(input, BTN_TOUCH, 1);
  73. input_report_abs(input, ABS_X, x);
  74. input_report_abs(input, ABS_Y, y);
  75. input_report_abs(input, ABS_PRESSURE, z);
  76. input_sync(input);
  77. }
  78. static irqreturn_t da9052_ts_datardy_irq(int irq, void *data)
  79. {
  80. struct da9052_tsi *tsi = data;
  81. da9052_ts_read(tsi);
  82. return IRQ_HANDLED;
  83. }
  84. static void da9052_ts_pen_work(struct work_struct *work)
  85. {
  86. struct da9052_tsi *tsi = container_of(work, struct da9052_tsi,
  87. ts_pen_work.work);
  88. if (!tsi->stopped) {
  89. int ret = da9052_reg_read(tsi->da9052, DA9052_TSI_LSB_REG);
  90. if (ret < 0 || (ret & TSI_PEN_DOWN_STATUS)) {
  91. /* Pen is still DOWN (or read error) */
  92. schedule_delayed_work(&tsi->ts_pen_work, HZ / 50);
  93. } else {
  94. struct input_dev *input = tsi->dev;
  95. /* Pen UP */
  96. da9052_ts_adc_toggle(tsi, false);
  97. /* Report Pen UP */
  98. input_report_key(input, BTN_TOUCH, 0);
  99. input_report_abs(input, ABS_PRESSURE, 0);
  100. input_sync(input);
  101. /*
  102. * FIXME: Fixes the unhandled irq issue when quick
  103. * pen down and pen up events occurs
  104. */
  105. ret = da9052_reg_update(tsi->da9052,
  106. DA9052_EVENT_B_REG, 0xC0, 0xC0);
  107. if (ret < 0)
  108. return;
  109. /* Mask TSI_READY event and unmask PEN_DOWN event */
  110. da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
  111. da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
  112. }
  113. }
  114. }
  115. static int da9052_ts_configure_gpio(struct da9052 *da9052)
  116. {
  117. int error;
  118. error = da9052_reg_update(da9052, DA9052_GPIO_2_3_REG, 0x30, 0);
  119. if (error < 0)
  120. return error;
  121. error = da9052_reg_update(da9052, DA9052_GPIO_4_5_REG, 0x33, 0);
  122. if (error < 0)
  123. return error;
  124. error = da9052_reg_update(da9052, DA9052_GPIO_6_7_REG, 0x33, 0);
  125. if (error < 0)
  126. return error;
  127. return 0;
  128. }
  129. static int da9052_configure_tsi(struct da9052_tsi *tsi)
  130. {
  131. int error;
  132. error = da9052_ts_configure_gpio(tsi->da9052);
  133. if (error)
  134. return error;
  135. /* Measure TSI sample every 1ms */
  136. error = da9052_reg_update(tsi->da9052, DA9052_ADC_CONT_REG,
  137. 1 << 6, 1 << 6);
  138. if (error < 0)
  139. return error;
  140. /* TSI_DELAY: 3 slots, TSI_SKIP: 0 slots, TSI_MODE: XYZP */
  141. error = da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 0xFC, 0xC0);
  142. if (error < 0)
  143. return error;
  144. /* Supply TSIRef through LD09 */
  145. error = da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x59);
  146. if (error < 0)
  147. return error;
  148. return 0;
  149. }
  150. static int da9052_ts_input_open(struct input_dev *input_dev)
  151. {
  152. struct da9052_tsi *tsi = input_get_drvdata(input_dev);
  153. tsi->stopped = false;
  154. mb();
  155. /* Unmask PEN_DOWN event */
  156. da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
  157. /* Enable Pen Detect Circuit */
  158. return da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG,
  159. 1 << 1, 1 << 1);
  160. }
  161. static void da9052_ts_input_close(struct input_dev *input_dev)
  162. {
  163. struct da9052_tsi *tsi = input_get_drvdata(input_dev);
  164. tsi->stopped = true;
  165. mb();
  166. da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
  167. cancel_delayed_work_sync(&tsi->ts_pen_work);
  168. if (tsi->adc_on) {
  169. da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
  170. da9052_ts_adc_toggle(tsi, false);
  171. /*
  172. * If ADC was on that means that pendwn IRQ was disabled
  173. * twice and we need to enable it to keep enable/disable
  174. * counter balanced. IRQ is still off though.
  175. */
  176. da9052_enable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
  177. }
  178. /* Disable Pen Detect Circuit */
  179. da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
  180. }
  181. static int da9052_ts_probe(struct platform_device *pdev)
  182. {
  183. struct da9052 *da9052;
  184. struct da9052_tsi *tsi;
  185. struct input_dev *input_dev;
  186. int error;
  187. da9052 = dev_get_drvdata(pdev->dev.parent);
  188. if (!da9052)
  189. return -EINVAL;
  190. tsi = kzalloc(sizeof(struct da9052_tsi), GFP_KERNEL);
  191. input_dev = input_allocate_device();
  192. if (!tsi || !input_dev) {
  193. error = -ENOMEM;
  194. goto err_free_mem;
  195. }
  196. tsi->da9052 = da9052;
  197. tsi->dev = input_dev;
  198. tsi->stopped = true;
  199. INIT_DELAYED_WORK(&tsi->ts_pen_work, da9052_ts_pen_work);
  200. input_dev->id.version = 0x0101;
  201. input_dev->id.vendor = 0x15B6;
  202. input_dev->id.product = 0x9052;
  203. input_dev->name = "Dialog DA9052 TouchScreen Driver";
  204. input_dev->dev.parent = &pdev->dev;
  205. input_dev->open = da9052_ts_input_open;
  206. input_dev->close = da9052_ts_input_close;
  207. __set_bit(EV_ABS, input_dev->evbit);
  208. __set_bit(EV_KEY, input_dev->evbit);
  209. __set_bit(BTN_TOUCH, input_dev->keybit);
  210. input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
  211. input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
  212. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 1023, 0, 0);
  213. input_set_drvdata(input_dev, tsi);
  214. /* Disable Pen Detect Circuit */
  215. da9052_reg_update(tsi->da9052, DA9052_TSI_CONT_A_REG, 1 << 1, 0);
  216. /* Disable ADC */
  217. da9052_ts_adc_toggle(tsi, false);
  218. error = da9052_request_irq(tsi->da9052, DA9052_IRQ_PENDOWN,
  219. "pendown-irq", da9052_ts_pendwn_irq, tsi);
  220. if (error) {
  221. dev_err(tsi->da9052->dev,
  222. "Failed to register PENDWN IRQ: %d\n", error);
  223. goto err_free_mem;
  224. }
  225. error = da9052_request_irq(tsi->da9052, DA9052_IRQ_TSIREADY,
  226. "tsiready-irq", da9052_ts_datardy_irq, tsi);
  227. if (error) {
  228. dev_err(tsi->da9052->dev,
  229. "Failed to register TSIRDY IRQ :%d\n", error);
  230. goto err_free_pendwn_irq;
  231. }
  232. /* Mask PEN_DOWN and TSI_READY events */
  233. da9052_disable_irq(tsi->da9052, DA9052_IRQ_PENDOWN);
  234. da9052_disable_irq(tsi->da9052, DA9052_IRQ_TSIREADY);
  235. error = da9052_configure_tsi(tsi);
  236. if (error)
  237. goto err_free_datardy_irq;
  238. error = input_register_device(tsi->dev);
  239. if (error)
  240. goto err_free_datardy_irq;
  241. platform_set_drvdata(pdev, tsi);
  242. return 0;
  243. err_free_datardy_irq:
  244. da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
  245. err_free_pendwn_irq:
  246. da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
  247. err_free_mem:
  248. kfree(tsi);
  249. input_free_device(input_dev);
  250. return error;
  251. }
  252. static int da9052_ts_remove(struct platform_device *pdev)
  253. {
  254. struct da9052_tsi *tsi = platform_get_drvdata(pdev);
  255. da9052_reg_write(tsi->da9052, DA9052_LDO9_REG, 0x19);
  256. da9052_free_irq(tsi->da9052, DA9052_IRQ_TSIREADY, tsi);
  257. da9052_free_irq(tsi->da9052, DA9052_IRQ_PENDOWN, tsi);
  258. input_unregister_device(tsi->dev);
  259. kfree(tsi);
  260. return 0;
  261. }
  262. static struct platform_driver da9052_tsi_driver = {
  263. .probe = da9052_ts_probe,
  264. .remove = da9052_ts_remove,
  265. .driver = {
  266. .name = "da9052-tsi",
  267. },
  268. };
  269. module_platform_driver(da9052_tsi_driver);
  270. MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9052");
  271. MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
  272. MODULE_LICENSE("GPL");
  273. MODULE_ALIAS("platform:da9052-tsi");