da9034-ts.c 8.2 KB

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