mc13783_ts.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. unsigned int sample[4];
  37. struct mc13xxx_ts_platform_data *touch;
  38. };
  39. static irqreturn_t mc13783_ts_handler(int irq, void *data)
  40. {
  41. struct mc13783_ts_priv *priv = data;
  42. mc13xxx_irq_ack(priv->mc13xxx, irq);
  43. /*
  44. * Kick off reading coordinates. Note that if work happens already
  45. * be queued for future execution (it rearms itself) it will not
  46. * be rescheduled for immediate execution here. However the rearm
  47. * delay is HZ / 50 which is acceptable.
  48. */
  49. schedule_delayed_work(&priv->work, 0);
  50. return IRQ_HANDLED;
  51. }
  52. #define sort3(a0, a1, a2) ({ \
  53. if (a0 > a1) \
  54. swap(a0, a1); \
  55. if (a1 > a2) \
  56. swap(a1, a2); \
  57. if (a0 > a1) \
  58. swap(a0, a1); \
  59. })
  60. static void mc13783_ts_report_sample(struct mc13783_ts_priv *priv)
  61. {
  62. struct input_dev *idev = priv->idev;
  63. int x0, x1, x2, y0, y1, y2;
  64. int cr0, cr1;
  65. /*
  66. * the values are 10-bit wide only, but the two least significant
  67. * bits are for future 12 bit use and reading yields 0
  68. */
  69. x0 = priv->sample[0] & 0xfff;
  70. x1 = priv->sample[1] & 0xfff;
  71. x2 = priv->sample[2] & 0xfff;
  72. y0 = priv->sample[3] & 0xfff;
  73. y1 = (priv->sample[0] >> 12) & 0xfff;
  74. y2 = (priv->sample[1] >> 12) & 0xfff;
  75. cr0 = (priv->sample[2] >> 12) & 0xfff;
  76. cr1 = (priv->sample[3] >> 12) & 0xfff;
  77. dev_dbg(&idev->dev,
  78. "x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
  79. x0, x1, x2, y0, y1, y2, cr0, cr1);
  80. sort3(x0, x1, x2);
  81. sort3(y0, y1, y2);
  82. cr0 = (cr0 + cr1) / 2;
  83. if (!cr0 || !sample_tolerance ||
  84. (x2 - x0 < sample_tolerance &&
  85. y2 - y0 < sample_tolerance)) {
  86. /* report the median coordinate and average pressure */
  87. if (cr0) {
  88. input_report_abs(idev, ABS_X, x1);
  89. input_report_abs(idev, ABS_Y, y1);
  90. dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
  91. x1, y1, 0x1000 - cr0);
  92. schedule_delayed_work(&priv->work, HZ / 50);
  93. } else {
  94. dev_dbg(&idev->dev, "report release\n");
  95. }
  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. }
  104. static void mc13783_ts_work(struct work_struct *work)
  105. {
  106. struct mc13783_ts_priv *priv =
  107. container_of(work, struct mc13783_ts_priv, work.work);
  108. unsigned int mode = MC13XXX_ADC_MODE_TS;
  109. unsigned int channel = 12;
  110. if (mc13xxx_adc_do_conversion(priv->mc13xxx,
  111. mode, channel,
  112. priv->touch->ato, priv->touch->atox,
  113. priv->sample) == 0)
  114. mc13783_ts_report_sample(priv);
  115. }
  116. static int mc13783_ts_open(struct input_dev *dev)
  117. {
  118. struct mc13783_ts_priv *priv = input_get_drvdata(dev);
  119. int ret;
  120. mc13xxx_lock(priv->mc13xxx);
  121. mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TS);
  122. ret = mc13xxx_irq_request(priv->mc13xxx, MC13XXX_IRQ_TS,
  123. mc13783_ts_handler, MC13783_TS_NAME, priv);
  124. if (ret)
  125. goto out;
  126. ret = mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
  127. MC13XXX_ADC0_TSMOD_MASK, MC13XXX_ADC0_TSMOD0);
  128. if (ret)
  129. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
  130. out:
  131. mc13xxx_unlock(priv->mc13xxx);
  132. return ret;
  133. }
  134. static void mc13783_ts_close(struct input_dev *dev)
  135. {
  136. struct mc13783_ts_priv *priv = input_get_drvdata(dev);
  137. mc13xxx_lock(priv->mc13xxx);
  138. mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
  139. MC13XXX_ADC0_TSMOD_MASK, 0);
  140. mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
  141. mc13xxx_unlock(priv->mc13xxx);
  142. cancel_delayed_work_sync(&priv->work);
  143. }
  144. static int __init mc13783_ts_probe(struct platform_device *pdev)
  145. {
  146. struct mc13783_ts_priv *priv;
  147. struct input_dev *idev;
  148. int ret = -ENOMEM;
  149. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  150. idev = input_allocate_device();
  151. if (!priv || !idev)
  152. goto err_free_mem;
  153. INIT_DELAYED_WORK(&priv->work, mc13783_ts_work);
  154. priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
  155. priv->idev = idev;
  156. priv->touch = dev_get_platdata(&pdev->dev);
  157. if (!priv->touch) {
  158. dev_err(&pdev->dev, "missing platform data\n");
  159. ret = -ENODEV;
  160. goto err_free_mem;
  161. }
  162. idev->name = MC13783_TS_NAME;
  163. idev->dev.parent = &pdev->dev;
  164. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  165. idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  166. input_set_abs_params(idev, ABS_X, 0, 0xfff, 0, 0);
  167. input_set_abs_params(idev, ABS_Y, 0, 0xfff, 0, 0);
  168. input_set_abs_params(idev, ABS_PRESSURE, 0, 0xfff, 0, 0);
  169. idev->open = mc13783_ts_open;
  170. idev->close = mc13783_ts_close;
  171. input_set_drvdata(idev, priv);
  172. ret = input_register_device(priv->idev);
  173. if (ret) {
  174. dev_err(&pdev->dev,
  175. "register input device failed with %d\n", ret);
  176. goto err_free_mem;
  177. }
  178. platform_set_drvdata(pdev, priv);
  179. return 0;
  180. err_free_mem:
  181. input_free_device(idev);
  182. kfree(priv);
  183. return ret;
  184. }
  185. static int mc13783_ts_remove(struct platform_device *pdev)
  186. {
  187. struct mc13783_ts_priv *priv = platform_get_drvdata(pdev);
  188. input_unregister_device(priv->idev);
  189. kfree(priv);
  190. return 0;
  191. }
  192. static struct platform_driver mc13783_ts_driver = {
  193. .remove = mc13783_ts_remove,
  194. .driver = {
  195. .name = MC13783_TS_NAME,
  196. },
  197. };
  198. module_platform_driver_probe(mc13783_ts_driver, mc13783_ts_probe);
  199. MODULE_DESCRIPTION("MC13783 input touchscreen driver");
  200. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  201. MODULE_LICENSE("GPL v2");
  202. MODULE_ALIAS("platform:" MC13783_TS_NAME);