wm831x-ts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Touchscreen driver for WM831x PMICs
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc.
  5. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/pm.h>
  17. #include <linux/input.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/mfd/wm831x/core.h>
  21. #include <linux/mfd/wm831x/irq.h>
  22. #include <linux/mfd/wm831x/pdata.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. /*
  27. * R16424 (0x4028) - Touch Control 1
  28. */
  29. #define WM831X_TCH_ENA 0x8000 /* TCH_ENA */
  30. #define WM831X_TCH_CVT_ENA 0x4000 /* TCH_CVT_ENA */
  31. #define WM831X_TCH_SLPENA 0x1000 /* TCH_SLPENA */
  32. #define WM831X_TCH_Z_ENA 0x0400 /* TCH_Z_ENA */
  33. #define WM831X_TCH_Y_ENA 0x0200 /* TCH_Y_ENA */
  34. #define WM831X_TCH_X_ENA 0x0100 /* TCH_X_ENA */
  35. #define WM831X_TCH_DELAY_MASK 0x00E0 /* TCH_DELAY - [7:5] */
  36. #define WM831X_TCH_DELAY_SHIFT 5 /* TCH_DELAY - [7:5] */
  37. #define WM831X_TCH_DELAY_WIDTH 3 /* TCH_DELAY - [7:5] */
  38. #define WM831X_TCH_RATE_MASK 0x001F /* TCH_RATE - [4:0] */
  39. #define WM831X_TCH_RATE_SHIFT 0 /* TCH_RATE - [4:0] */
  40. #define WM831X_TCH_RATE_WIDTH 5 /* TCH_RATE - [4:0] */
  41. /*
  42. * R16425 (0x4029) - Touch Control 2
  43. */
  44. #define WM831X_TCH_PD_WK 0x2000 /* TCH_PD_WK */
  45. #define WM831X_TCH_5WIRE 0x1000 /* TCH_5WIRE */
  46. #define WM831X_TCH_PDONLY 0x0800 /* TCH_PDONLY */
  47. #define WM831X_TCH_ISEL 0x0100 /* TCH_ISEL */
  48. #define WM831X_TCH_RPU_MASK 0x000F /* TCH_RPU - [3:0] */
  49. #define WM831X_TCH_RPU_SHIFT 0 /* TCH_RPU - [3:0] */
  50. #define WM831X_TCH_RPU_WIDTH 4 /* TCH_RPU - [3:0] */
  51. /*
  52. * R16426-8 (0x402A-C) - Touch Data X/Y/X
  53. */
  54. #define WM831X_TCH_PD 0x8000 /* TCH_PD1 */
  55. #define WM831X_TCH_DATA_MASK 0x0FFF /* TCH_DATA - [11:0] */
  56. #define WM831X_TCH_DATA_SHIFT 0 /* TCH_DATA - [11:0] */
  57. #define WM831X_TCH_DATA_WIDTH 12 /* TCH_DATA - [11:0] */
  58. struct wm831x_ts {
  59. struct input_dev *input_dev;
  60. struct wm831x *wm831x;
  61. unsigned int data_irq;
  62. unsigned int pd_irq;
  63. bool pressure;
  64. bool pen_down;
  65. struct work_struct pd_data_work;
  66. };
  67. static void wm831x_pd_data_work(struct work_struct *work)
  68. {
  69. struct wm831x_ts *wm831x_ts =
  70. container_of(work, struct wm831x_ts, pd_data_work);
  71. if (wm831x_ts->pen_down) {
  72. enable_irq(wm831x_ts->data_irq);
  73. dev_dbg(wm831x_ts->wm831x->dev, "IRQ PD->DATA done\n");
  74. } else {
  75. enable_irq(wm831x_ts->pd_irq);
  76. dev_dbg(wm831x_ts->wm831x->dev, "IRQ DATA->PD done\n");
  77. }
  78. }
  79. static irqreturn_t wm831x_ts_data_irq(int irq, void *irq_data)
  80. {
  81. struct wm831x_ts *wm831x_ts = irq_data;
  82. struct wm831x *wm831x = wm831x_ts->wm831x;
  83. static int data_types[] = { ABS_X, ABS_Y, ABS_PRESSURE };
  84. u16 data[3];
  85. int count;
  86. int i, ret;
  87. if (wm831x_ts->pressure)
  88. count = 3;
  89. else
  90. count = 2;
  91. wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
  92. WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
  93. ret = wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count,
  94. data);
  95. if (ret != 0) {
  96. dev_err(wm831x->dev, "Failed to read touch data: %d\n",
  97. ret);
  98. return IRQ_NONE;
  99. }
  100. /*
  101. * We get a pen down reading on every reading, report pen up if any
  102. * individual reading does so.
  103. */
  104. wm831x_ts->pen_down = true;
  105. for (i = 0; i < count; i++) {
  106. if (!(data[i] & WM831X_TCH_PD)) {
  107. wm831x_ts->pen_down = false;
  108. continue;
  109. }
  110. input_report_abs(wm831x_ts->input_dev, data_types[i],
  111. data[i] & WM831X_TCH_DATA_MASK);
  112. }
  113. if (!wm831x_ts->pen_down) {
  114. /* Switch from data to pen down */
  115. dev_dbg(wm831x->dev, "IRQ DATA->PD\n");
  116. disable_irq_nosync(wm831x_ts->data_irq);
  117. /* Don't need data any more */
  118. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  119. WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
  120. WM831X_TCH_Z_ENA, 0);
  121. /* Flush any final samples that arrived while reading */
  122. wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
  123. WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
  124. wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count, data);
  125. if (wm831x_ts->pressure)
  126. input_report_abs(wm831x_ts->input_dev,
  127. ABS_PRESSURE, 0);
  128. input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 0);
  129. schedule_work(&wm831x_ts->pd_data_work);
  130. } else {
  131. input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 1);
  132. }
  133. input_sync(wm831x_ts->input_dev);
  134. return IRQ_HANDLED;
  135. }
  136. static irqreturn_t wm831x_ts_pen_down_irq(int irq, void *irq_data)
  137. {
  138. struct wm831x_ts *wm831x_ts = irq_data;
  139. struct wm831x *wm831x = wm831x_ts->wm831x;
  140. int ena = 0;
  141. if (wm831x_ts->pen_down)
  142. return IRQ_HANDLED;
  143. disable_irq_nosync(wm831x_ts->pd_irq);
  144. /* Start collecting data */
  145. if (wm831x_ts->pressure)
  146. ena |= WM831X_TCH_Z_ENA;
  147. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  148. WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | WM831X_TCH_Z_ENA,
  149. WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | ena);
  150. wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
  151. WM831X_TCHPD_EINT, WM831X_TCHPD_EINT);
  152. wm831x_ts->pen_down = true;
  153. /* Switch from pen down to data */
  154. dev_dbg(wm831x->dev, "IRQ PD->DATA\n");
  155. schedule_work(&wm831x_ts->pd_data_work);
  156. return IRQ_HANDLED;
  157. }
  158. static int wm831x_ts_input_open(struct input_dev *idev)
  159. {
  160. struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
  161. struct wm831x *wm831x = wm831x_ts->wm831x;
  162. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  163. WM831X_TCH_ENA | WM831X_TCH_CVT_ENA |
  164. WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
  165. WM831X_TCH_Z_ENA, WM831X_TCH_ENA);
  166. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  167. WM831X_TCH_CVT_ENA, WM831X_TCH_CVT_ENA);
  168. return 0;
  169. }
  170. static void wm831x_ts_input_close(struct input_dev *idev)
  171. {
  172. struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
  173. struct wm831x *wm831x = wm831x_ts->wm831x;
  174. /* Shut the controller down, disabling all other functionality too */
  175. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  176. WM831X_TCH_ENA | WM831X_TCH_X_ENA |
  177. WM831X_TCH_Y_ENA | WM831X_TCH_Z_ENA, 0);
  178. /* Make sure any pending IRQs are done, the above will prevent
  179. * new ones firing.
  180. */
  181. synchronize_irq(wm831x_ts->data_irq);
  182. synchronize_irq(wm831x_ts->pd_irq);
  183. /* Make sure the IRQ completion work is quiesced */
  184. flush_work(&wm831x_ts->pd_data_work);
  185. /* If we ended up with the pen down then make sure we revert back
  186. * to pen detection state for the next time we start up.
  187. */
  188. if (wm831x_ts->pen_down) {
  189. disable_irq(wm831x_ts->data_irq);
  190. enable_irq(wm831x_ts->pd_irq);
  191. wm831x_ts->pen_down = false;
  192. }
  193. }
  194. static int wm831x_ts_probe(struct platform_device *pdev)
  195. {
  196. struct wm831x_ts *wm831x_ts;
  197. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  198. struct wm831x_pdata *core_pdata = dev_get_platdata(pdev->dev.parent);
  199. struct wm831x_touch_pdata *pdata = NULL;
  200. struct input_dev *input_dev;
  201. int error, irqf;
  202. if (core_pdata)
  203. pdata = core_pdata->touch;
  204. wm831x_ts = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ts),
  205. GFP_KERNEL);
  206. input_dev = devm_input_allocate_device(&pdev->dev);
  207. if (!wm831x_ts || !input_dev) {
  208. error = -ENOMEM;
  209. goto err_alloc;
  210. }
  211. wm831x_ts->wm831x = wm831x;
  212. wm831x_ts->input_dev = input_dev;
  213. INIT_WORK(&wm831x_ts->pd_data_work, wm831x_pd_data_work);
  214. /*
  215. * If we have a direct IRQ use it, otherwise use the interrupt
  216. * from the WM831x IRQ controller.
  217. */
  218. wm831x_ts->data_irq = wm831x_irq(wm831x,
  219. platform_get_irq_byname(pdev,
  220. "TCHDATA"));
  221. if (pdata && pdata->data_irq)
  222. wm831x_ts->data_irq = pdata->data_irq;
  223. wm831x_ts->pd_irq = wm831x_irq(wm831x,
  224. platform_get_irq_byname(pdev, "TCHPD"));
  225. if (pdata && pdata->pd_irq)
  226. wm831x_ts->pd_irq = pdata->pd_irq;
  227. if (pdata)
  228. wm831x_ts->pressure = pdata->pressure;
  229. else
  230. wm831x_ts->pressure = true;
  231. /* Five wire touchscreens can't report pressure */
  232. if (pdata && pdata->fivewire) {
  233. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
  234. WM831X_TCH_5WIRE, WM831X_TCH_5WIRE);
  235. /* Pressure measurements are not possible for five wire mode */
  236. WARN_ON(pdata->pressure && pdata->fivewire);
  237. wm831x_ts->pressure = false;
  238. } else {
  239. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
  240. WM831X_TCH_5WIRE, 0);
  241. }
  242. if (pdata) {
  243. switch (pdata->isel) {
  244. default:
  245. dev_err(&pdev->dev, "Unsupported ISEL setting: %d\n",
  246. pdata->isel);
  247. /* Fall through */
  248. case 200:
  249. case 0:
  250. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
  251. WM831X_TCH_ISEL, 0);
  252. break;
  253. case 400:
  254. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
  255. WM831X_TCH_ISEL, WM831X_TCH_ISEL);
  256. break;
  257. }
  258. }
  259. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
  260. WM831X_TCH_PDONLY, 0);
  261. /* Default to 96 samples/sec */
  262. wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
  263. WM831X_TCH_RATE_MASK, 6);
  264. if (pdata && pdata->data_irqf)
  265. irqf = pdata->data_irqf;
  266. else
  267. irqf = IRQF_TRIGGER_HIGH;
  268. error = request_threaded_irq(wm831x_ts->data_irq,
  269. NULL, wm831x_ts_data_irq,
  270. irqf | IRQF_ONESHOT,
  271. "Touchscreen data", wm831x_ts);
  272. if (error) {
  273. dev_err(&pdev->dev, "Failed to request data IRQ %d: %d\n",
  274. wm831x_ts->data_irq, error);
  275. goto err_alloc;
  276. }
  277. disable_irq(wm831x_ts->data_irq);
  278. if (pdata && pdata->pd_irqf)
  279. irqf = pdata->pd_irqf;
  280. else
  281. irqf = IRQF_TRIGGER_HIGH;
  282. error = request_threaded_irq(wm831x_ts->pd_irq,
  283. NULL, wm831x_ts_pen_down_irq,
  284. irqf | IRQF_ONESHOT,
  285. "Touchscreen pen down", wm831x_ts);
  286. if (error) {
  287. dev_err(&pdev->dev, "Failed to request pen down IRQ %d: %d\n",
  288. wm831x_ts->pd_irq, error);
  289. goto err_data_irq;
  290. }
  291. /* set up touch configuration */
  292. input_dev->name = "WM831x touchscreen";
  293. input_dev->phys = "wm831x";
  294. input_dev->open = wm831x_ts_input_open;
  295. input_dev->close = wm831x_ts_input_close;
  296. __set_bit(EV_ABS, input_dev->evbit);
  297. __set_bit(EV_KEY, input_dev->evbit);
  298. __set_bit(BTN_TOUCH, input_dev->keybit);
  299. input_set_abs_params(input_dev, ABS_X, 0, 4095, 5, 0);
  300. input_set_abs_params(input_dev, ABS_Y, 0, 4095, 5, 0);
  301. if (wm831x_ts->pressure)
  302. input_set_abs_params(input_dev, ABS_PRESSURE, 0, 4095, 5, 0);
  303. input_set_drvdata(input_dev, wm831x_ts);
  304. input_dev->dev.parent = &pdev->dev;
  305. error = input_register_device(input_dev);
  306. if (error)
  307. goto err_pd_irq;
  308. platform_set_drvdata(pdev, wm831x_ts);
  309. return 0;
  310. err_pd_irq:
  311. free_irq(wm831x_ts->pd_irq, wm831x_ts);
  312. err_data_irq:
  313. free_irq(wm831x_ts->data_irq, wm831x_ts);
  314. err_alloc:
  315. return error;
  316. }
  317. static int wm831x_ts_remove(struct platform_device *pdev)
  318. {
  319. struct wm831x_ts *wm831x_ts = platform_get_drvdata(pdev);
  320. free_irq(wm831x_ts->pd_irq, wm831x_ts);
  321. free_irq(wm831x_ts->data_irq, wm831x_ts);
  322. return 0;
  323. }
  324. static struct platform_driver wm831x_ts_driver = {
  325. .driver = {
  326. .name = "wm831x-touch",
  327. },
  328. .probe = wm831x_ts_probe,
  329. .remove = wm831x_ts_remove,
  330. };
  331. module_platform_driver(wm831x_ts_driver);
  332. /* Module information */
  333. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  334. MODULE_DESCRIPTION("WM831x PMIC touchscreen driver");
  335. MODULE_LICENSE("GPL");
  336. MODULE_ALIAS("platform:wm831x-touch");