colibri-vf50-ts.c 9.2 KB

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