mc13783_ts.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Driver for the Freescale Semiconductor MC13783 touchscreen.
  3. *
  4. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  5. * Copyright (C) 2009 Sascha Hauer, Pengutronix
  6. *
  7. * Initial development of this code was funded by
  8. * Phytec Messtechnik GmbH, http://www.phytec.de/
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. */
  14. #include <linux/platform_device.h>
  15. #include <linux/mfd/mc13783.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/input.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #define MC13783_TS_NAME "mc13783-ts"
  23. #define DEFAULT_SAMPLE_TOLERANCE 300
  24. static unsigned int sample_tolerance = DEFAULT_SAMPLE_TOLERANCE;
  25. module_param(sample_tolerance, uint, S_IRUGO | S_IWUSR);
  26. MODULE_PARM_DESC(sample_tolerance,
  27. "If the minimal and maximal value read out for one axis (out "
  28. "of three) differ by this value (default: "
  29. __stringify(DEFAULT_SAMPLE_TOLERANCE) ") or more, the reading "
  30. "is supposed to be wrong and is discarded. Set to 0 to "
  31. "disable this check.");
  32. struct mc13783_ts_priv {
  33. struct input_dev *idev;
  34. struct mc13xxx *mc13xxx;
  35. struct delayed_work work;
  36. struct workqueue_struct *workq;
  37. unsigned int sample[4];
  38. struct mc13xxx_ts_platform_data *touch;
  39. };
  40. static irqreturn_t mc13783_ts_handler(int irq, void *data)
  41. {
  42. struct mc13783_ts_priv *priv = data;
  43. mc13xxx_irq_ack(priv->mc13xxx, irq);
  44. /*
  45. * Kick off reading coordinates. Note that if work happens already
  46. * be queued for future execution (it rearms itself) it will not
  47. * be rescheduled for immediate execution here. However the rearm
  48. * delay is HZ / 50 which is acceptable.
  49. */
  50. queue_delayed_work(priv->workq, &priv->work, 0);
  51. return IRQ_HANDLED;
  52. }
  53. #define sort3(a0, a1, a2) ({ \
  54. if (a0 > a1) \
  55. swap(a0, a1); \
  56. if (a1 > a2) \
  57. swap(a1, a2); \
  58. if (a0 > a1) \
  59. swap(a0, a1); \
  60. })
  61. static void mc13783_ts_report_sample(struct mc13783_ts_priv *priv)
  62. {
  63. struct input_dev *idev = priv->idev;
  64. int x0, x1, x2, y0, y1, y2;
  65. int cr0, cr1;
  66. /*
  67. * the values are 10-bit wide only, but the two least significant
  68. * bits are for future 12 bit use and reading yields 0
  69. */
  70. x0 = priv->sample[0] & 0xfff;
  71. x1 = priv->sample[1] & 0xfff;
  72. x2 = priv->sample[2] & 0xfff;
  73. y0 = priv->sample[3] & 0xfff;
  74. y1 = (priv->sample[0] >> 12) & 0xfff;
  75. y2 = (priv->sample[1] >> 12) & 0xfff;
  76. cr0 = (priv->sample[2] >> 12) & 0xfff;
  77. cr1 = (priv->sample[3] >> 12) & 0xfff;
  78. dev_dbg(&idev->dev,
  79. "x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
  80. x0, x1, x2, y0, y1, y2, cr0, cr1);
  81. sort3(x0, x1, x2);
  82. sort3(y0, y1, y2);
  83. cr0 = (cr0 + cr1) / 2;
  84. if (!cr0 || !sample_tolerance ||
  85. (x2 - x0 < sample_tolerance &&
  86. y2 - y0 < sample_tolerance)) {
  87. /* report the median coordinate and average pressure */
  88. if (cr0) {
  89. input_report_abs(idev, ABS_X, x1);
  90. input_report_abs(idev, ABS_Y, y1);
  91. dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
  92. x1, y1, 0x1000 - cr0);
  93. queue_delayed_work(priv->workq, &priv->work, HZ / 50);
  94. } else
  95. dev_dbg(&idev->dev, "report release\n");
  96. input_report_abs(idev, ABS_PRESSURE,
  97. cr0 ? 0x1000 - cr0 : cr0);
  98. input_report_key(idev, BTN_TOUCH, cr0);
  99. input_sync(idev);
  100. } else
  101. dev_dbg(&idev->dev, "discard event\n");
  102. }
  103. static void mc13783_ts_work(struct work_struct *work)
  104. {
  105. struct mc13783_ts_priv *priv =
  106. container_of(work, struct mc13783_ts_priv, work.work);
  107. unsigned int mode = MC13XXX_ADC_MODE_TS;
  108. unsigned int channel = 12;
  109. if (mc13xxx_adc_do_conversion(priv->mc13xxx,
  110. mode, channel,
  111. priv->touch->ato, priv->touch->atox,
  112. priv->sample) == 0)
  113. mc13783_ts_report_sample(priv);
  114. }
  115. static int mc13783_ts_open(struct input_dev *dev)
  116. {
  117. struct mc13783_ts_priv *priv = input_get_drvdata(dev);
  118. int ret;
  119. mc13xxx_lock(priv->mc13xxx);
  120. mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TS);
  121. ret = mc13xxx_irq_request(priv->mc13xxx, MC13XXX_IRQ_TS,
  122. mc13783_ts_handler, MC13783_TS_NAME, priv);
  123. if (ret)
  124. goto out;
  125. ret = mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
  126. MC13XXX_ADC0_TSMOD_MASK, MC13XXX_ADC0_TSMOD0);
  127. if (ret)
  128. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
  129. out:
  130. mc13xxx_unlock(priv->mc13xxx);
  131. return ret;
  132. }
  133. static void mc13783_ts_close(struct input_dev *dev)
  134. {
  135. struct mc13783_ts_priv *priv = input_get_drvdata(dev);
  136. mc13xxx_lock(priv->mc13xxx);
  137. mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
  138. MC13XXX_ADC0_TSMOD_MASK, 0);
  139. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
  140. mc13xxx_unlock(priv->mc13xxx);
  141. cancel_delayed_work_sync(&priv->work);
  142. }
  143. static int __init mc13783_ts_probe(struct platform_device *pdev)
  144. {
  145. struct mc13783_ts_priv *priv;
  146. struct input_dev *idev;
  147. int ret = -ENOMEM;
  148. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  149. idev = input_allocate_device();
  150. if (!priv || !idev)
  151. goto err_free_mem;
  152. INIT_DELAYED_WORK(&priv->work, mc13783_ts_work);
  153. priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
  154. priv->idev = idev;
  155. priv->touch = dev_get_platdata(&pdev->dev);
  156. if (!priv->touch) {
  157. dev_err(&pdev->dev, "missing platform data\n");
  158. ret = -ENODEV;
  159. goto err_free_mem;
  160. }
  161. /*
  162. * We need separate workqueue because mc13783_adc_do_conversion
  163. * uses keventd and thus would deadlock.
  164. */
  165. priv->workq = create_singlethread_workqueue("mc13783_ts");
  166. if (!priv->workq)
  167. goto err_free_mem;
  168. idev->name = MC13783_TS_NAME;
  169. idev->dev.parent = &pdev->dev;
  170. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  171. idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  172. input_set_abs_params(idev, ABS_X, 0, 0xfff, 0, 0);
  173. input_set_abs_params(idev, ABS_Y, 0, 0xfff, 0, 0);
  174. input_set_abs_params(idev, ABS_PRESSURE, 0, 0xfff, 0, 0);
  175. idev->open = mc13783_ts_open;
  176. idev->close = mc13783_ts_close;
  177. input_set_drvdata(idev, priv);
  178. ret = input_register_device(priv->idev);
  179. if (ret) {
  180. dev_err(&pdev->dev,
  181. "register input device failed with %d\n", ret);
  182. goto err_destroy_wq;
  183. }
  184. platform_set_drvdata(pdev, priv);
  185. return 0;
  186. err_destroy_wq:
  187. destroy_workqueue(priv->workq);
  188. err_free_mem:
  189. input_free_device(idev);
  190. kfree(priv);
  191. return ret;
  192. }
  193. static int mc13783_ts_remove(struct platform_device *pdev)
  194. {
  195. struct mc13783_ts_priv *priv = platform_get_drvdata(pdev);
  196. destroy_workqueue(priv->workq);
  197. input_unregister_device(priv->idev);
  198. kfree(priv);
  199. return 0;
  200. }
  201. static struct platform_driver mc13783_ts_driver = {
  202. .remove = mc13783_ts_remove,
  203. .driver = {
  204. .name = MC13783_TS_NAME,
  205. },
  206. };
  207. module_platform_driver_probe(mc13783_ts_driver, mc13783_ts_probe);
  208. MODULE_DESCRIPTION("MC13783 input touchscreen driver");
  209. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  210. MODULE_LICENSE("GPL v2");
  211. MODULE_ALIAS("platform:" MC13783_TS_NAME);