colibri-vf50-ts.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Toradex Colibri VF50 Touchscreen driver
  3. *
  4. * Copyright 2015 Toradex AG
  5. *
  6. * Originally authored by Stefan Agner for 3.0 kernel
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/iio/consumer.h>
  18. #include <linux/iio/types.h>
  19. #include <linux/input.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #define DRIVER_NAME "colibri-vf50-ts"
  29. #define DRV_VERSION "1.0"
  30. #define VF_ADC_MAX ((1 << 12) - 1)
  31. #define COLI_TOUCH_MIN_DELAY_US 1000
  32. #define COLI_TOUCH_MAX_DELAY_US 2000
  33. #define COLI_PULLUP_MIN_DELAY_US 10000
  34. #define COLI_PULLUP_MAX_DELAY_US 11000
  35. #define COLI_TOUCH_NO_OF_AVGS 5
  36. #define COLI_TOUCH_REQ_ADC_CHAN 4
  37. struct vf50_touch_device {
  38. struct platform_device *pdev;
  39. struct input_dev *ts_input;
  40. struct iio_channel *channels;
  41. struct gpio_desc *gpio_xp;
  42. struct gpio_desc *gpio_xm;
  43. struct gpio_desc *gpio_yp;
  44. struct gpio_desc *gpio_ym;
  45. int pen_irq;
  46. int min_pressure;
  47. bool stop_touchscreen;
  48. };
  49. /*
  50. * Enables given plates and measures touch parameters using ADC
  51. */
  52. static int adc_ts_measure(struct iio_channel *channel,
  53. struct gpio_desc *plate_p, struct gpio_desc *plate_m)
  54. {
  55. int i, value = 0, val = 0;
  56. int error;
  57. gpiod_set_value(plate_p, 1);
  58. gpiod_set_value(plate_m, 1);
  59. usleep_range(COLI_TOUCH_MIN_DELAY_US, COLI_TOUCH_MAX_DELAY_US);
  60. for (i = 0; i < COLI_TOUCH_NO_OF_AVGS; i++) {
  61. error = iio_read_channel_raw(channel, &val);
  62. if (error < 0) {
  63. value = error;
  64. goto error_iio_read;
  65. }
  66. value += val;
  67. }
  68. value /= COLI_TOUCH_NO_OF_AVGS;
  69. error_iio_read:
  70. gpiod_set_value(plate_p, 0);
  71. gpiod_set_value(plate_m, 0);
  72. return value;
  73. }
  74. /*
  75. * Enable touch detection using falling edge detection on XM
  76. */
  77. static void vf50_ts_enable_touch_detection(struct vf50_touch_device *vf50_ts)
  78. {
  79. /* Enable plate YM (needs to be strong GND, high active) */
  80. gpiod_set_value(vf50_ts->gpio_ym, 1);
  81. /*
  82. * Let the platform mux to idle state in order to enable
  83. * Pull-Up on GPIO
  84. */
  85. pinctrl_pm_select_idle_state(&vf50_ts->pdev->dev);
  86. /* Wait for the pull-up to be stable on high */
  87. usleep_range(COLI_PULLUP_MIN_DELAY_US, COLI_PULLUP_MAX_DELAY_US);
  88. }
  89. /*
  90. * ADC touch screen sampling bottom half irq handler
  91. */
  92. static irqreturn_t vf50_ts_irq_bh(int irq, void *private)
  93. {
  94. struct vf50_touch_device *vf50_ts = private;
  95. struct device *dev = &vf50_ts->pdev->dev;
  96. int val_x, val_y, val_z1, val_z2, val_p = 0;
  97. bool discard_val_on_start = true;
  98. /* Disable the touch detection plates */
  99. gpiod_set_value(vf50_ts->gpio_ym, 0);
  100. /* Let the platform mux to default state in order to mux as ADC */
  101. pinctrl_pm_select_default_state(dev);
  102. while (!vf50_ts->stop_touchscreen) {
  103. /* X-Direction */
  104. val_x = adc_ts_measure(&vf50_ts->channels[0],
  105. vf50_ts->gpio_xp, vf50_ts->gpio_xm);
  106. if (val_x < 0)
  107. break;
  108. /* Y-Direction */
  109. val_y = adc_ts_measure(&vf50_ts->channels[1],
  110. vf50_ts->gpio_yp, vf50_ts->gpio_ym);
  111. if (val_y < 0)
  112. break;
  113. /*
  114. * Touch pressure
  115. * Measure on XP/YM
  116. */
  117. val_z1 = adc_ts_measure(&vf50_ts->channels[2],
  118. vf50_ts->gpio_yp, vf50_ts->gpio_xm);
  119. if (val_z1 < 0)
  120. break;
  121. val_z2 = adc_ts_measure(&vf50_ts->channels[3],
  122. vf50_ts->gpio_yp, vf50_ts->gpio_xm);
  123. if (val_z2 < 0)
  124. break;
  125. /* Validate signal (avoid calculation using noise) */
  126. if (val_z1 > 64 && val_x > 64) {
  127. /*
  128. * Calculate resistance between the plates
  129. * lower resistance means higher pressure
  130. */
  131. int r_x = (1000 * val_x) / VF_ADC_MAX;
  132. val_p = (r_x * val_z2) / val_z1 - r_x;
  133. } else {
  134. val_p = 2000;
  135. }
  136. val_p = 2000 - val_p;
  137. dev_dbg(dev,
  138. "Measured values: x: %d, y: %d, z1: %d, z2: %d, p: %d\n",
  139. val_x, val_y, val_z1, val_z2, val_p);
  140. /*
  141. * If touch pressure is too low, stop measuring and reenable
  142. * touch detection
  143. */
  144. if (val_p < vf50_ts->min_pressure || val_p > 2000)
  145. break;
  146. /*
  147. * The pressure may not be enough for the first x and the
  148. * second y measurement, but, the pressure is ok when the
  149. * driver is doing the third and fourth measurement. To
  150. * take care of this, we drop the first measurement always.
  151. */
  152. if (discard_val_on_start) {
  153. discard_val_on_start = false;
  154. } else {
  155. /*
  156. * Report touch position and sleep for
  157. * the next measurement.
  158. */
  159. input_report_abs(vf50_ts->ts_input,
  160. ABS_X, VF_ADC_MAX - val_x);
  161. input_report_abs(vf50_ts->ts_input,
  162. ABS_Y, VF_ADC_MAX - val_y);
  163. input_report_abs(vf50_ts->ts_input,
  164. ABS_PRESSURE, val_p);
  165. input_report_key(vf50_ts->ts_input, BTN_TOUCH, 1);
  166. input_sync(vf50_ts->ts_input);
  167. }
  168. usleep_range(COLI_PULLUP_MIN_DELAY_US,
  169. COLI_PULLUP_MAX_DELAY_US);
  170. }
  171. /* Report no more touch, re-enable touch detection */
  172. input_report_abs(vf50_ts->ts_input, ABS_PRESSURE, 0);
  173. input_report_key(vf50_ts->ts_input, BTN_TOUCH, 0);
  174. input_sync(vf50_ts->ts_input);
  175. vf50_ts_enable_touch_detection(vf50_ts);
  176. return IRQ_HANDLED;
  177. }
  178. static int vf50_ts_open(struct input_dev *dev_input)
  179. {
  180. struct vf50_touch_device *touchdev = input_get_drvdata(dev_input);
  181. struct device *dev = &touchdev->pdev->dev;
  182. dev_dbg(dev, "Input device %s opened, starting touch detection\n",
  183. dev_input->name);
  184. touchdev->stop_touchscreen = false;
  185. /* Mux detection before request IRQ, wait for pull-up to settle */
  186. vf50_ts_enable_touch_detection(touchdev);
  187. return 0;
  188. }
  189. static void vf50_ts_close(struct input_dev *dev_input)
  190. {
  191. struct vf50_touch_device *touchdev = input_get_drvdata(dev_input);
  192. struct device *dev = &touchdev->pdev->dev;
  193. touchdev->stop_touchscreen = true;
  194. /* Make sure IRQ is not running past close */
  195. mb();
  196. synchronize_irq(touchdev->pen_irq);
  197. gpiod_set_value(touchdev->gpio_ym, 0);
  198. pinctrl_pm_select_default_state(dev);
  199. dev_dbg(dev, "Input device %s closed, disable touch detection\n",
  200. dev_input->name);
  201. }
  202. static int vf50_ts_get_gpiod(struct device *dev, struct gpio_desc **gpio_d,
  203. const char *con_id, enum gpiod_flags flags)
  204. {
  205. int error;
  206. *gpio_d = devm_gpiod_get(dev, con_id, flags);
  207. if (IS_ERR(*gpio_d)) {
  208. error = PTR_ERR(*gpio_d);
  209. dev_err(dev, "Could not get gpio_%s %d\n", con_id, error);
  210. return error;
  211. }
  212. return 0;
  213. }
  214. static void vf50_ts_channel_release(void *data)
  215. {
  216. struct iio_channel *channels = data;
  217. iio_channel_release_all(channels);
  218. }
  219. static int vf50_ts_probe(struct platform_device *pdev)
  220. {
  221. struct input_dev *input;
  222. struct iio_channel *channels;
  223. struct device *dev = &pdev->dev;
  224. struct vf50_touch_device *touchdev;
  225. int num_adc_channels;
  226. int error;
  227. channels = iio_channel_get_all(dev);
  228. if (IS_ERR(channels))
  229. return PTR_ERR(channels);
  230. error = devm_add_action(dev, vf50_ts_channel_release, channels);
  231. if (error) {
  232. iio_channel_release_all(channels);
  233. dev_err(dev, "Failed to register iio channel release action");
  234. return error;
  235. }
  236. num_adc_channels = 0;
  237. while (channels[num_adc_channels].indio_dev)
  238. num_adc_channels++;
  239. if (num_adc_channels != COLI_TOUCH_REQ_ADC_CHAN) {
  240. dev_err(dev, "Inadequate ADC channels specified\n");
  241. return -EINVAL;
  242. }
  243. touchdev = devm_kzalloc(dev, sizeof(*touchdev), GFP_KERNEL);
  244. if (!touchdev)
  245. return -ENOMEM;
  246. touchdev->pdev = pdev;
  247. touchdev->channels = channels;
  248. error = of_property_read_u32(dev->of_node, "vf50-ts-min-pressure",
  249. &touchdev->min_pressure);
  250. if (error)
  251. return error;
  252. input = devm_input_allocate_device(dev);
  253. if (!input) {
  254. dev_err(dev, "Failed to allocate TS input device\n");
  255. return -ENOMEM;
  256. }
  257. platform_set_drvdata(pdev, touchdev);
  258. input->name = DRIVER_NAME;
  259. input->id.bustype = BUS_HOST;
  260. input->dev.parent = dev;
  261. input->open = vf50_ts_open;
  262. input->close = vf50_ts_close;
  263. input_set_capability(input, EV_KEY, BTN_TOUCH);
  264. input_set_abs_params(input, ABS_X, 0, VF_ADC_MAX, 0, 0);
  265. input_set_abs_params(input, ABS_Y, 0, VF_ADC_MAX, 0, 0);
  266. input_set_abs_params(input, ABS_PRESSURE, 0, VF_ADC_MAX, 0, 0);
  267. touchdev->ts_input = input;
  268. input_set_drvdata(input, touchdev);
  269. error = input_register_device(input);
  270. if (error) {
  271. dev_err(dev, "Failed to register input device\n");
  272. return error;
  273. }
  274. error = vf50_ts_get_gpiod(dev, &touchdev->gpio_xp, "xp", GPIOD_OUT_LOW);
  275. if (error)
  276. return error;
  277. error = vf50_ts_get_gpiod(dev, &touchdev->gpio_xm,
  278. "xm", GPIOD_OUT_LOW);
  279. if (error)
  280. return error;
  281. error = vf50_ts_get_gpiod(dev, &touchdev->gpio_yp, "yp", GPIOD_OUT_LOW);
  282. if (error)
  283. return error;
  284. error = vf50_ts_get_gpiod(dev, &touchdev->gpio_ym, "ym", GPIOD_OUT_LOW);
  285. if (error)
  286. return error;
  287. touchdev->pen_irq = platform_get_irq(pdev, 0);
  288. if (touchdev->pen_irq < 0)
  289. return touchdev->pen_irq;
  290. error = devm_request_threaded_irq(dev, touchdev->pen_irq,
  291. NULL, vf50_ts_irq_bh, IRQF_ONESHOT,
  292. "vf50 touch", touchdev);
  293. if (error) {
  294. dev_err(dev, "Failed to request IRQ %d: %d\n",
  295. touchdev->pen_irq, error);
  296. return error;
  297. }
  298. return 0;
  299. }
  300. static const struct of_device_id vf50_touch_of_match[] = {
  301. { .compatible = "toradex,vf50-touchscreen", },
  302. { }
  303. };
  304. MODULE_DEVICE_TABLE(of, vf50_touch_of_match);
  305. static struct platform_driver vf50_touch_driver = {
  306. .driver = {
  307. .name = "toradex,vf50_touchctrl",
  308. .of_match_table = vf50_touch_of_match,
  309. },
  310. .probe = vf50_ts_probe,
  311. };
  312. module_platform_driver(vf50_touch_driver);
  313. MODULE_AUTHOR("Sanchayan Maity");
  314. MODULE_DESCRIPTION("Colibri VF50 Touchscreen driver");
  315. MODULE_LICENSE("GPL");
  316. MODULE_VERSION(DRV_VERSION);