sun4i-ts.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Allwinner sunxi resistive touchscreen controller driver
  3. *
  4. * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * The hwmon parts are based on work by Corentin LABBE which is:
  7. * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. /*
  20. * The sun4i-ts controller is capable of detecting a second touch, but when a
  21. * second touch is present then the accuracy becomes so bad the reported touch
  22. * location is not useable.
  23. *
  24. * The original android driver contains some complicated heuristics using the
  25. * aprox. distance between the 2 touches to see if the user is making a pinch
  26. * open / close movement, and then reports emulated multi-touch events around
  27. * the last touch coordinate (as the dual-touch coordinates are worthless).
  28. *
  29. * These kinds of heuristics are just asking for trouble (and don't belong
  30. * in the kernel). So this driver offers straight forward, reliable single
  31. * touch functionality only.
  32. */
  33. #include <linux/err.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/init.h>
  36. #include <linux/input.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/io.h>
  39. #include <linux/module.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/slab.h>
  43. #define TP_CTRL0 0x00
  44. #define TP_CTRL1 0x04
  45. #define TP_CTRL2 0x08
  46. #define TP_CTRL3 0x0c
  47. #define TP_INT_FIFOC 0x10
  48. #define TP_INT_FIFOS 0x14
  49. #define TP_TPR 0x18
  50. #define TP_CDAT 0x1c
  51. #define TEMP_DATA 0x20
  52. #define TP_DATA 0x24
  53. /* TP_CTRL0 bits */
  54. #define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
  55. #define ADC_FIRST_DLY_MODE(x) ((x) << 23)
  56. #define ADC_CLK_SEL(x) ((x) << 22)
  57. #define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
  58. #define FS_DIV(x) ((x) << 16) /* 4 bits */
  59. #define T_ACQ(x) ((x) << 0) /* 16 bits */
  60. /* TP_CTRL1 bits */
  61. #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
  62. #define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
  63. #define TOUCH_PAN_CALI_EN(x) ((x) << 6)
  64. #define TP_DUAL_EN(x) ((x) << 5)
  65. #define TP_MODE_EN(x) ((x) << 4)
  66. #define TP_ADC_SELECT(x) ((x) << 3)
  67. #define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
  68. /* TP_CTRL2 bits */
  69. #define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
  70. #define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
  71. #define PRE_MEA_EN(x) ((x) << 24)
  72. #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
  73. /* TP_CTRL3 bits */
  74. #define FILTER_EN(x) ((x) << 2)
  75. #define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
  76. /* TP_INT_FIFOC irq and fifo mask / control bits */
  77. #define TEMP_IRQ_EN(x) ((x) << 18)
  78. #define OVERRUN_IRQ_EN(x) ((x) << 17)
  79. #define DATA_IRQ_EN(x) ((x) << 16)
  80. #define TP_DATA_XY_CHANGE(x) ((x) << 13)
  81. #define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
  82. #define DATA_DRQ_EN(x) ((x) << 7)
  83. #define FIFO_FLUSH(x) ((x) << 4)
  84. #define TP_UP_IRQ_EN(x) ((x) << 1)
  85. #define TP_DOWN_IRQ_EN(x) ((x) << 0)
  86. /* TP_INT_FIFOS irq and fifo status bits */
  87. #define TEMP_DATA_PENDING BIT(18)
  88. #define FIFO_OVERRUN_PENDING BIT(17)
  89. #define FIFO_DATA_PENDING BIT(16)
  90. #define TP_IDLE_FLG BIT(2)
  91. #define TP_UP_PENDING BIT(1)
  92. #define TP_DOWN_PENDING BIT(0)
  93. /* TP_TPR bits */
  94. #define TEMP_ENABLE(x) ((x) << 16)
  95. #define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
  96. struct sun4i_ts_data {
  97. struct device *dev;
  98. struct input_dev *input;
  99. void __iomem *base;
  100. unsigned int irq;
  101. bool ignore_fifo_data;
  102. int temp_data;
  103. };
  104. static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
  105. {
  106. u32 x, y;
  107. if (reg_val & FIFO_DATA_PENDING) {
  108. x = readl(ts->base + TP_DATA);
  109. y = readl(ts->base + TP_DATA);
  110. /* The 1st location reported after an up event is unreliable */
  111. if (!ts->ignore_fifo_data) {
  112. input_report_abs(ts->input, ABS_X, x);
  113. input_report_abs(ts->input, ABS_Y, y);
  114. /*
  115. * The hardware has a separate down status bit, but
  116. * that gets set before we get the first location,
  117. * resulting in reporting a click on the old location.
  118. */
  119. input_report_key(ts->input, BTN_TOUCH, 1);
  120. input_sync(ts->input);
  121. } else {
  122. ts->ignore_fifo_data = false;
  123. }
  124. }
  125. if (reg_val & TP_UP_PENDING) {
  126. ts->ignore_fifo_data = true;
  127. input_report_key(ts->input, BTN_TOUCH, 0);
  128. input_sync(ts->input);
  129. }
  130. }
  131. static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
  132. {
  133. struct sun4i_ts_data *ts = dev_id;
  134. u32 reg_val;
  135. reg_val = readl(ts->base + TP_INT_FIFOS);
  136. if (reg_val & TEMP_DATA_PENDING)
  137. ts->temp_data = readl(ts->base + TEMP_DATA);
  138. if (ts->input)
  139. sun4i_ts_irq_handle_input(ts, reg_val);
  140. writel(reg_val, ts->base + TP_INT_FIFOS);
  141. return IRQ_HANDLED;
  142. }
  143. static int sun4i_ts_open(struct input_dev *dev)
  144. {
  145. struct sun4i_ts_data *ts = input_get_drvdata(dev);
  146. /* Flush, set trig level to 1, enable temp, data and up irqs */
  147. writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
  148. TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  149. return 0;
  150. }
  151. static void sun4i_ts_close(struct input_dev *dev)
  152. {
  153. struct sun4i_ts_data *ts = input_get_drvdata(dev);
  154. /* Deactivate all input IRQs */
  155. writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  156. }
  157. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  158. char *buf)
  159. {
  160. struct sun4i_ts_data *ts = dev_get_drvdata(dev);
  161. /* No temp_data until the first irq */
  162. if (ts->temp_data == -1)
  163. return -EAGAIN;
  164. return sprintf(buf, "%d\n", (ts->temp_data - 1447) * 100);
  165. }
  166. static ssize_t show_temp_label(struct device *dev,
  167. struct device_attribute *devattr, char *buf)
  168. {
  169. return sprintf(buf, "SoC temperature\n");
  170. }
  171. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  172. static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
  173. static struct attribute *sun4i_ts_attrs[] = {
  174. &dev_attr_temp1_input.attr,
  175. &dev_attr_temp1_label.attr,
  176. NULL
  177. };
  178. ATTRIBUTE_GROUPS(sun4i_ts);
  179. static int sun4i_ts_probe(struct platform_device *pdev)
  180. {
  181. struct sun4i_ts_data *ts;
  182. struct device *dev = &pdev->dev;
  183. struct device_node *np = dev->of_node;
  184. struct device *hwmon;
  185. int error;
  186. bool ts_attached;
  187. ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
  188. if (!ts)
  189. return -ENOMEM;
  190. ts->dev = dev;
  191. ts->ignore_fifo_data = true;
  192. ts->temp_data = -1;
  193. ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
  194. if (ts_attached) {
  195. ts->input = devm_input_allocate_device(dev);
  196. if (!ts->input)
  197. return -ENOMEM;
  198. ts->input->name = pdev->name;
  199. ts->input->phys = "sun4i_ts/input0";
  200. ts->input->open = sun4i_ts_open;
  201. ts->input->close = sun4i_ts_close;
  202. ts->input->id.bustype = BUS_HOST;
  203. ts->input->id.vendor = 0x0001;
  204. ts->input->id.product = 0x0001;
  205. ts->input->id.version = 0x0100;
  206. ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
  207. __set_bit(BTN_TOUCH, ts->input->keybit);
  208. input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
  209. input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
  210. input_set_drvdata(ts->input, ts);
  211. }
  212. ts->base = devm_ioremap_resource(dev,
  213. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  214. if (IS_ERR(ts->base))
  215. return PTR_ERR(ts->base);
  216. ts->irq = platform_get_irq(pdev, 0);
  217. error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
  218. if (error)
  219. return error;
  220. /*
  221. * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
  222. * t_acq = clkin / (16 * 64)
  223. */
  224. writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
  225. ts->base + TP_CTRL0);
  226. /*
  227. * sensitive_adjust = 15 : max, which is not all that sensitive,
  228. * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
  229. */
  230. writel(TP_SENSITIVE_ADJUST(15) | TP_MODE_SELECT(0),
  231. ts->base + TP_CTRL2);
  232. /* Enable median filter, type 1 : 5/3 */
  233. writel(FILTER_EN(1) | FILTER_TYPE(1), ts->base + TP_CTRL3);
  234. /* Enable temperature measurement, period 1953 (2 seconds) */
  235. writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
  236. /*
  237. * Set stylus up debounce to aprox 10 ms, enable debounce, and
  238. * finally enable tp mode.
  239. */
  240. writel(STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1) | TP_MODE_EN(1),
  241. ts->base + TP_CTRL1);
  242. hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
  243. ts, sun4i_ts_groups);
  244. if (IS_ERR(hwmon))
  245. return PTR_ERR(hwmon);
  246. writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  247. if (ts_attached) {
  248. error = input_register_device(ts->input);
  249. if (error) {
  250. writel(0, ts->base + TP_INT_FIFOC);
  251. return error;
  252. }
  253. }
  254. platform_set_drvdata(pdev, ts);
  255. return 0;
  256. }
  257. static int sun4i_ts_remove(struct platform_device *pdev)
  258. {
  259. struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
  260. /* Explicit unregister to avoid open/close changing the imask later */
  261. if (ts->input)
  262. input_unregister_device(ts->input);
  263. /* Deactivate all IRQs */
  264. writel(0, ts->base + TP_INT_FIFOC);
  265. return 0;
  266. }
  267. static const struct of_device_id sun4i_ts_of_match[] = {
  268. { .compatible = "allwinner,sun4i-a10-ts", },
  269. { /* sentinel */ }
  270. };
  271. MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
  272. static struct platform_driver sun4i_ts_driver = {
  273. .driver = {
  274. .owner = THIS_MODULE,
  275. .name = "sun4i-ts",
  276. .of_match_table = of_match_ptr(sun4i_ts_of_match),
  277. },
  278. .probe = sun4i_ts_probe,
  279. .remove = sun4i_ts_remove,
  280. };
  281. module_platform_driver(sun4i_ts_driver);
  282. MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
  283. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  284. MODULE_LICENSE("GPL");