da9034-ts.c 8.5 KB

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