da9034-ts.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/input.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/mfd/da903x.h>
  21. #include <linux/slab.h>
  22. #define DA9034_MANUAL_CTRL 0x50
  23. #define DA9034_LDO_ADC_EN (1 << 4)
  24. #define DA9034_AUTO_CTRL1 0x51
  25. #define DA9034_AUTO_CTRL2 0x52
  26. #define DA9034_AUTO_TSI_EN (1 << 3)
  27. #define DA9034_PEN_DETECT (1 << 4)
  28. #define DA9034_TSI_CTRL1 0x53
  29. #define DA9034_TSI_CTRL2 0x54
  30. #define DA9034_TSI_X_MSB 0x6c
  31. #define DA9034_TSI_Y_MSB 0x6d
  32. #define DA9034_TSI_XY_LSB 0x6e
  33. enum {
  34. STATE_IDLE, /* wait for pendown */
  35. STATE_BUSY, /* TSI busy sampling */
  36. STATE_STOP, /* sample available */
  37. STATE_WAIT, /* Wait to start next sample */
  38. };
  39. enum {
  40. EVENT_PEN_DOWN,
  41. EVENT_PEN_UP,
  42. EVENT_TSI_READY,
  43. EVENT_TIMEDOUT,
  44. };
  45. struct da9034_touch {
  46. struct device *da9034_dev;
  47. struct input_dev *input_dev;
  48. struct delayed_work tsi_work;
  49. struct notifier_block notifier;
  50. int state;
  51. int interval_ms;
  52. int x_inverted;
  53. int y_inverted;
  54. int last_x;
  55. int last_y;
  56. };
  57. static inline int is_pen_down(struct da9034_touch *touch)
  58. {
  59. return da903x_query_status(touch->da9034_dev, DA9034_STATUS_PEN_DOWN);
  60. }
  61. static inline int detect_pen_down(struct da9034_touch *touch, int on)
  62. {
  63. if (on)
  64. return da903x_set_bits(touch->da9034_dev,
  65. DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
  66. else
  67. return da903x_clr_bits(touch->da9034_dev,
  68. DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
  69. }
  70. static int read_tsi(struct da9034_touch *touch)
  71. {
  72. uint8_t _x, _y, _v;
  73. int ret;
  74. ret = da903x_read(touch->da9034_dev, DA9034_TSI_X_MSB, &_x);
  75. if (ret)
  76. return ret;
  77. ret = da903x_read(touch->da9034_dev, DA9034_TSI_Y_MSB, &_y);
  78. if (ret)
  79. return ret;
  80. ret = da903x_read(touch->da9034_dev, DA9034_TSI_XY_LSB, &_v);
  81. if (ret)
  82. return ret;
  83. touch->last_x = ((_x << 2) & 0x3fc) | (_v & 0x3);
  84. touch->last_y = ((_y << 2) & 0x3fc) | ((_v & 0xc) >> 2);
  85. return 0;
  86. }
  87. static inline int start_tsi(struct da9034_touch *touch)
  88. {
  89. return da903x_set_bits(touch->da9034_dev,
  90. DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
  91. }
  92. static inline int stop_tsi(struct da9034_touch *touch)
  93. {
  94. return da903x_clr_bits(touch->da9034_dev,
  95. DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
  96. }
  97. static inline void report_pen_down(struct da9034_touch *touch)
  98. {
  99. int x = touch->last_x;
  100. int y = touch->last_y;
  101. x &= 0xfff;
  102. if (touch->x_inverted)
  103. x = 1024 - x;
  104. y &= 0xfff;
  105. if (touch->y_inverted)
  106. y = 1024 - y;
  107. input_report_abs(touch->input_dev, ABS_X, x);
  108. input_report_abs(touch->input_dev, ABS_Y, y);
  109. input_report_key(touch->input_dev, BTN_TOUCH, 1);
  110. input_sync(touch->input_dev);
  111. }
  112. static inline void report_pen_up(struct da9034_touch *touch)
  113. {
  114. input_report_key(touch->input_dev, BTN_TOUCH, 0);
  115. input_sync(touch->input_dev);
  116. }
  117. static void da9034_event_handler(struct da9034_touch *touch, int event)
  118. {
  119. int err;
  120. switch (touch->state) {
  121. case STATE_IDLE:
  122. if (event != EVENT_PEN_DOWN)
  123. break;
  124. /* Enable auto measurement of the TSI, this will
  125. * automatically disable pen down detection
  126. */
  127. err = start_tsi(touch);
  128. if (err)
  129. goto err_reset;
  130. touch->state = STATE_BUSY;
  131. break;
  132. case STATE_BUSY:
  133. if (event != EVENT_TSI_READY)
  134. break;
  135. err = read_tsi(touch);
  136. if (err)
  137. goto err_reset;
  138. /* Disable auto measurement of the TSI, so that
  139. * pen down status will be available
  140. */
  141. err = stop_tsi(touch);
  142. if (err)
  143. goto err_reset;
  144. touch->state = STATE_STOP;
  145. /* FIXME: PEN_{UP/DOWN} events are expected to be
  146. * available by stopping TSI, but this is found not
  147. * always true, delay and simulate such an event
  148. * here is more reliable
  149. */
  150. mdelay(1);
  151. da9034_event_handler(touch,
  152. is_pen_down(touch) ? EVENT_PEN_DOWN :
  153. EVENT_PEN_UP);
  154. break;
  155. case STATE_STOP:
  156. if (event == EVENT_PEN_DOWN) {
  157. report_pen_down(touch);
  158. schedule_delayed_work(&touch->tsi_work,
  159. msecs_to_jiffies(touch->interval_ms));
  160. touch->state = STATE_WAIT;
  161. }
  162. if (event == EVENT_PEN_UP) {
  163. report_pen_up(touch);
  164. touch->state = STATE_IDLE;
  165. }
  166. break;
  167. case STATE_WAIT:
  168. if (event != EVENT_TIMEDOUT)
  169. break;
  170. if (is_pen_down(touch)) {
  171. start_tsi(touch);
  172. touch->state = STATE_BUSY;
  173. } else {
  174. report_pen_up(touch);
  175. touch->state = STATE_IDLE;
  176. }
  177. break;
  178. }
  179. return;
  180. err_reset:
  181. touch->state = STATE_IDLE;
  182. stop_tsi(touch);
  183. detect_pen_down(touch, 1);
  184. }
  185. static void da9034_tsi_work(struct work_struct *work)
  186. {
  187. struct da9034_touch *touch =
  188. container_of(work, struct da9034_touch, tsi_work.work);
  189. da9034_event_handler(touch, EVENT_TIMEDOUT);
  190. }
  191. static int da9034_touch_notifier(struct notifier_block *nb,
  192. unsigned long event, void *data)
  193. {
  194. struct da9034_touch *touch =
  195. container_of(nb, struct da9034_touch, notifier);
  196. if (event & DA9034_EVENT_TSI_READY)
  197. da9034_event_handler(touch, EVENT_TSI_READY);
  198. if ((event & DA9034_EVENT_PEN_DOWN) && touch->state == STATE_IDLE)
  199. da9034_event_handler(touch, EVENT_PEN_DOWN);
  200. return 0;
  201. }
  202. static int da9034_touch_open(struct input_dev *dev)
  203. {
  204. struct da9034_touch *touch = input_get_drvdata(dev);
  205. int ret;
  206. ret = da903x_register_notifier(touch->da9034_dev, &touch->notifier,
  207. DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
  208. if (ret)
  209. return -EBUSY;
  210. /* Enable ADC LDO */
  211. ret = da903x_set_bits(touch->da9034_dev,
  212. DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
  213. if (ret)
  214. return ret;
  215. /* TSI_DELAY: 3 slots, TSI_SKIP: 3 slots */
  216. ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL1, 0x1b);
  217. if (ret)
  218. return ret;
  219. ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL2, 0x00);
  220. if (ret)
  221. return ret;
  222. touch->state = STATE_IDLE;
  223. detect_pen_down(touch, 1);
  224. return 0;
  225. }
  226. static void da9034_touch_close(struct input_dev *dev)
  227. {
  228. struct da9034_touch *touch = input_get_drvdata(dev);
  229. da903x_unregister_notifier(touch->da9034_dev, &touch->notifier,
  230. DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
  231. cancel_delayed_work_sync(&touch->tsi_work);
  232. touch->state = STATE_IDLE;
  233. stop_tsi(touch);
  234. detect_pen_down(touch, 0);
  235. /* Disable ADC LDO */
  236. da903x_clr_bits(touch->da9034_dev,
  237. DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
  238. }
  239. static int __devinit da9034_touch_probe(struct platform_device *pdev)
  240. {
  241. struct da9034_touch_pdata *pdata = pdev->dev.platform_data;
  242. struct da9034_touch *touch;
  243. struct input_dev *input_dev;
  244. int ret;
  245. touch = kzalloc(sizeof(struct da9034_touch), GFP_KERNEL);
  246. if (touch == NULL) {
  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. INIT_DELAYED_WORK(&touch->tsi_work, da9034_tsi_work);
  259. touch->notifier.notifier_call = da9034_touch_notifier;
  260. input_dev = input_allocate_device();
  261. if (!input_dev) {
  262. dev_err(&pdev->dev, "failed to allocate input device\n");
  263. ret = -ENOMEM;
  264. goto err_free_touch;
  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. ret = input_register_device(input_dev);
  280. if (ret)
  281. goto err_free_input;
  282. platform_set_drvdata(pdev, touch);
  283. return 0;
  284. err_free_input:
  285. input_free_device(input_dev);
  286. err_free_touch:
  287. kfree(touch);
  288. return ret;
  289. }
  290. static int __devexit da9034_touch_remove(struct platform_device *pdev)
  291. {
  292. struct da9034_touch *touch = platform_get_drvdata(pdev);
  293. input_unregister_device(touch->input_dev);
  294. kfree(touch);
  295. return 0;
  296. }
  297. static struct platform_driver da9034_touch_driver = {
  298. .driver = {
  299. .name = "da9034-touch",
  300. .owner = THIS_MODULE,
  301. },
  302. .probe = da9034_touch_probe,
  303. .remove = __devexit_p(da9034_touch_remove),
  304. };
  305. static int __init da9034_touch_init(void)
  306. {
  307. return platform_driver_register(&da9034_touch_driver);
  308. }
  309. module_init(da9034_touch_init);
  310. static void __exit da9034_touch_exit(void)
  311. {
  312. platform_driver_unregister(&da9034_touch_driver);
  313. }
  314. module_exit(da9034_touch_exit);
  315. MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9034");
  316. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>, Bin Yang <bin.yang@marvell.com>");
  317. MODULE_LICENSE("GPL");
  318. MODULE_ALIAS("platform:da9034-touch");