da9034-ts.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Touchscreen driver for Dialog Semiconductor DA9034
  4. *
  5. * Copyright (C) 2006-2008 Marvell International Ltd.
  6. * Fengwei Yin <[email protected]>
  7. * Bin Yang <[email protected]>
  8. * Eric Miao <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/input.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/mfd/da903x.h>
  17. #include <linux/slab.h>
  18. #define DA9034_MANUAL_CTRL 0x50
  19. #define DA9034_LDO_ADC_EN (1 << 4)
  20. #define DA9034_AUTO_CTRL1 0x51
  21. #define DA9034_AUTO_CTRL2 0x52
  22. #define DA9034_AUTO_TSI_EN (1 << 3)
  23. #define DA9034_PEN_DETECT (1 << 4)
  24. #define DA9034_TSI_CTRL1 0x53
  25. #define DA9034_TSI_CTRL2 0x54
  26. #define DA9034_TSI_X_MSB 0x6c
  27. #define DA9034_TSI_Y_MSB 0x6d
  28. #define DA9034_TSI_XY_LSB 0x6e
  29. enum {
  30. STATE_IDLE, /* wait for pendown */
  31. STATE_BUSY, /* TSI busy sampling */
  32. STATE_STOP, /* sample available */
  33. STATE_WAIT, /* Wait to start next sample */
  34. };
  35. enum {
  36. EVENT_PEN_DOWN,
  37. EVENT_PEN_UP,
  38. EVENT_TSI_READY,
  39. EVENT_TIMEDOUT,
  40. };
  41. struct da9034_touch {
  42. struct device *da9034_dev;
  43. struct input_dev *input_dev;
  44. struct delayed_work tsi_work;
  45. struct notifier_block notifier;
  46. int state;
  47. int interval_ms;
  48. int x_inverted;
  49. int y_inverted;
  50. int last_x;
  51. int last_y;
  52. };
  53. static inline int is_pen_down(struct da9034_touch *touch)
  54. {
  55. return da903x_query_status(touch->da9034_dev, DA9034_STATUS_PEN_DOWN);
  56. }
  57. static inline int detect_pen_down(struct da9034_touch *touch, int on)
  58. {
  59. if (on)
  60. return da903x_set_bits(touch->da9034_dev,
  61. DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
  62. else
  63. return da903x_clr_bits(touch->da9034_dev,
  64. DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
  65. }
  66. static int read_tsi(struct da9034_touch *touch)
  67. {
  68. uint8_t _x, _y, _v;
  69. int ret;
  70. ret = da903x_read(touch->da9034_dev, DA9034_TSI_X_MSB, &_x);
  71. if (ret)
  72. return ret;
  73. ret = da903x_read(touch->da9034_dev, DA9034_TSI_Y_MSB, &_y);
  74. if (ret)
  75. return ret;
  76. ret = da903x_read(touch->da9034_dev, DA9034_TSI_XY_LSB, &_v);
  77. if (ret)
  78. return ret;
  79. touch->last_x = ((_x << 2) & 0x3fc) | (_v & 0x3);
  80. touch->last_y = ((_y << 2) & 0x3fc) | ((_v & 0xc) >> 2);
  81. return 0;
  82. }
  83. static inline int start_tsi(struct da9034_touch *touch)
  84. {
  85. return da903x_set_bits(touch->da9034_dev,
  86. DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
  87. }
  88. static inline int stop_tsi(struct da9034_touch *touch)
  89. {
  90. return da903x_clr_bits(touch->da9034_dev,
  91. DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
  92. }
  93. static inline void report_pen_down(struct da9034_touch *touch)
  94. {
  95. int x = touch->last_x;
  96. int y = touch->last_y;
  97. x &= 0xfff;
  98. if (touch->x_inverted)
  99. x = 1024 - x;
  100. y &= 0xfff;
  101. if (touch->y_inverted)
  102. y = 1024 - y;
  103. input_report_abs(touch->input_dev, ABS_X, x);
  104. input_report_abs(touch->input_dev, ABS_Y, y);
  105. input_report_key(touch->input_dev, BTN_TOUCH, 1);
  106. input_sync(touch->input_dev);
  107. }
  108. static inline void report_pen_up(struct da9034_touch *touch)
  109. {
  110. input_report_key(touch->input_dev, BTN_TOUCH, 0);
  111. input_sync(touch->input_dev);
  112. }
  113. static void da9034_event_handler(struct da9034_touch *touch, int event)
  114. {
  115. int err;
  116. switch (touch->state) {
  117. case STATE_IDLE:
  118. if (event != EVENT_PEN_DOWN)
  119. break;
  120. /* Enable auto measurement of the TSI, this will
  121. * automatically disable pen down detection
  122. */
  123. err = start_tsi(touch);
  124. if (err)
  125. goto err_reset;
  126. touch->state = STATE_BUSY;
  127. break;
  128. case STATE_BUSY:
  129. if (event != EVENT_TSI_READY)
  130. break;
  131. err = read_tsi(touch);
  132. if (err)
  133. goto err_reset;
  134. /* Disable auto measurement of the TSI, so that
  135. * pen down status will be available
  136. */
  137. err = stop_tsi(touch);
  138. if (err)
  139. goto err_reset;
  140. touch->state = STATE_STOP;
  141. /* FIXME: PEN_{UP/DOWN} events are expected to be
  142. * available by stopping TSI, but this is found not
  143. * always true, delay and simulate such an event
  144. * here is more reliable
  145. */
  146. mdelay(1);
  147. da9034_event_handler(touch,
  148. is_pen_down(touch) ? EVENT_PEN_DOWN :
  149. EVENT_PEN_UP);
  150. break;
  151. case STATE_STOP:
  152. if (event == EVENT_PEN_DOWN) {
  153. report_pen_down(touch);
  154. schedule_delayed_work(&touch->tsi_work,
  155. msecs_to_jiffies(touch->interval_ms));
  156. touch->state = STATE_WAIT;
  157. }
  158. if (event == EVENT_PEN_UP) {
  159. report_pen_up(touch);
  160. touch->state = STATE_IDLE;
  161. }
  162. break;
  163. case STATE_WAIT:
  164. if (event != EVENT_TIMEDOUT)
  165. break;
  166. if (is_pen_down(touch)) {
  167. start_tsi(touch);
  168. touch->state = STATE_BUSY;
  169. } else {
  170. report_pen_up(touch);
  171. touch->state = STATE_IDLE;
  172. }
  173. break;
  174. }
  175. return;
  176. err_reset:
  177. touch->state = STATE_IDLE;
  178. stop_tsi(touch);
  179. detect_pen_down(touch, 1);
  180. }
  181. static void da9034_tsi_work(struct work_struct *work)
  182. {
  183. struct da9034_touch *touch =
  184. container_of(work, struct da9034_touch, tsi_work.work);
  185. da9034_event_handler(touch, EVENT_TIMEDOUT);
  186. }
  187. static int da9034_touch_notifier(struct notifier_block *nb,
  188. unsigned long event, void *data)
  189. {
  190. struct da9034_touch *touch =
  191. container_of(nb, struct da9034_touch, notifier);
  192. if (event & DA9034_EVENT_TSI_READY)
  193. da9034_event_handler(touch, EVENT_TSI_READY);
  194. if ((event & DA9034_EVENT_PEN_DOWN) && touch->state == STATE_IDLE)
  195. da9034_event_handler(touch, EVENT_PEN_DOWN);
  196. return 0;
  197. }
  198. static int da9034_touch_open(struct input_dev *dev)
  199. {
  200. struct da9034_touch *touch = input_get_drvdata(dev);
  201. int ret;
  202. ret = da903x_register_notifier(touch->da9034_dev, &touch->notifier,
  203. DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
  204. if (ret)
  205. return -EBUSY;
  206. /* Enable ADC LDO */
  207. ret = da903x_set_bits(touch->da9034_dev,
  208. DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
  209. if (ret)
  210. return ret;
  211. /* TSI_DELAY: 3 slots, TSI_SKIP: 3 slots */
  212. ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL1, 0x1b);
  213. if (ret)
  214. return ret;
  215. ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL2, 0x00);
  216. if (ret)
  217. return ret;
  218. touch->state = STATE_IDLE;
  219. detect_pen_down(touch, 1);
  220. return 0;
  221. }
  222. static void da9034_touch_close(struct input_dev *dev)
  223. {
  224. struct da9034_touch *touch = input_get_drvdata(dev);
  225. da903x_unregister_notifier(touch->da9034_dev, &touch->notifier,
  226. DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
  227. cancel_delayed_work_sync(&touch->tsi_work);
  228. touch->state = STATE_IDLE;
  229. stop_tsi(touch);
  230. detect_pen_down(touch, 0);
  231. /* Disable ADC LDO */
  232. da903x_clr_bits(touch->da9034_dev,
  233. DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
  234. }
  235. static int da9034_touch_probe(struct platform_device *pdev)
  236. {
  237. struct da9034_touch_pdata *pdata = dev_get_platdata(&pdev->dev);
  238. struct da9034_touch *touch;
  239. struct input_dev *input_dev;
  240. int error;
  241. touch = devm_kzalloc(&pdev->dev, sizeof(struct da9034_touch),
  242. GFP_KERNEL);
  243. if (!touch) {
  244. dev_err(&pdev->dev, "failed to allocate driver data\n");
  245. return -ENOMEM;
  246. }
  247. touch->da9034_dev = pdev->dev.parent;
  248. if (pdata) {
  249. touch->interval_ms = pdata->interval_ms;
  250. touch->x_inverted = pdata->x_inverted;
  251. touch->y_inverted = pdata->y_inverted;
  252. } else {
  253. /* fallback into default */
  254. touch->interval_ms = 10;
  255. }
  256. INIT_DELAYED_WORK(&touch->tsi_work, da9034_tsi_work);
  257. touch->notifier.notifier_call = da9034_touch_notifier;
  258. input_dev = devm_input_allocate_device(&pdev->dev);
  259. if (!input_dev) {
  260. dev_err(&pdev->dev, "failed to allocate input device\n");
  261. return -ENOMEM;
  262. }
  263. input_dev->name = pdev->name;
  264. input_dev->open = da9034_touch_open;
  265. input_dev->close = da9034_touch_close;
  266. input_dev->dev.parent = &pdev->dev;
  267. __set_bit(EV_ABS, input_dev->evbit);
  268. __set_bit(ABS_X, input_dev->absbit);
  269. __set_bit(ABS_Y, input_dev->absbit);
  270. input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
  271. input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
  272. __set_bit(EV_KEY, input_dev->evbit);
  273. __set_bit(BTN_TOUCH, input_dev->keybit);
  274. touch->input_dev = input_dev;
  275. input_set_drvdata(input_dev, touch);
  276. error = input_register_device(input_dev);
  277. if (error)
  278. return error;
  279. return 0;
  280. }
  281. static struct platform_driver da9034_touch_driver = {
  282. .driver = {
  283. .name = "da9034-touch",
  284. },
  285. .probe = da9034_touch_probe,
  286. };
  287. module_platform_driver(da9034_touch_driver);
  288. MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9034");
  289. MODULE_AUTHOR("Eric Miao <[email protected]>, Bin Yang <[email protected]>");
  290. MODULE_LICENSE("GPL");
  291. MODULE_ALIAS("platform:da9034-touch");