colibri-vf50-ts.c 9.5 KB

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