sun4i-ts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Allwinner sunxi resistive touchscreen controller driver
  4. *
  5. * Copyright (C) 2013 - 2014 Hans de Goede <[email protected]>
  6. *
  7. * The hwmon parts are based on work by Corentin LABBE which is:
  8. * Copyright (C) 2013 Corentin LABBE <[email protected]>
  9. */
  10. /*
  11. * The sun4i-ts controller is capable of detecting a second touch, but when a
  12. * second touch is present then the accuracy becomes so bad the reported touch
  13. * location is not useable.
  14. *
  15. * The original android driver contains some complicated heuristics using the
  16. * aprox. distance between the 2 touches to see if the user is making a pinch
  17. * open / close movement, and then reports emulated multi-touch events around
  18. * the last touch coordinate (as the dual-touch coordinates are worthless).
  19. *
  20. * These kinds of heuristics are just asking for trouble (and don't belong
  21. * in the kernel). So this driver offers straight forward, reliable single
  22. * touch functionality only.
  23. *
  24. * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi.rst)
  25. * (looks like the description in the A20 User Manual v1.3 is better
  26. * than the one in the A10 User Manual v.1.5)
  27. */
  28. #include <linux/err.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/thermal.h>
  31. #include <linux/init.h>
  32. #include <linux/input.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/io.h>
  35. #include <linux/module.h>
  36. #include <linux/of_platform.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/slab.h>
  39. #define TP_CTRL0 0x00
  40. #define TP_CTRL1 0x04
  41. #define TP_CTRL2 0x08
  42. #define TP_CTRL3 0x0c
  43. #define TP_INT_FIFOC 0x10
  44. #define TP_INT_FIFOS 0x14
  45. #define TP_TPR 0x18
  46. #define TP_CDAT 0x1c
  47. #define TEMP_DATA 0x20
  48. #define TP_DATA 0x24
  49. /* TP_CTRL0 bits */
  50. #define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
  51. #define ADC_FIRST_DLY_MODE(x) ((x) << 23)
  52. #define ADC_CLK_SEL(x) ((x) << 22)
  53. #define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
  54. #define FS_DIV(x) ((x) << 16) /* 4 bits */
  55. #define T_ACQ(x) ((x) << 0) /* 16 bits */
  56. /* TP_CTRL1 bits */
  57. #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
  58. #define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
  59. #define TOUCH_PAN_CALI_EN(x) ((x) << 6)
  60. #define TP_DUAL_EN(x) ((x) << 5)
  61. #define TP_MODE_EN(x) ((x) << 4)
  62. #define TP_ADC_SELECT(x) ((x) << 3)
  63. #define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
  64. /* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
  65. #define SUN6I_TP_MODE_EN(x) ((x) << 5)
  66. /* TP_CTRL2 bits */
  67. #define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
  68. #define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
  69. #define PRE_MEA_EN(x) ((x) << 24)
  70. #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
  71. /* TP_CTRL3 bits */
  72. #define FILTER_EN(x) ((x) << 2)
  73. #define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
  74. /* TP_INT_FIFOC irq and fifo mask / control bits */
  75. #define TEMP_IRQ_EN(x) ((x) << 18)
  76. #define OVERRUN_IRQ_EN(x) ((x) << 17)
  77. #define DATA_IRQ_EN(x) ((x) << 16)
  78. #define TP_DATA_XY_CHANGE(x) ((x) << 13)
  79. #define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
  80. #define DATA_DRQ_EN(x) ((x) << 7)
  81. #define FIFO_FLUSH(x) ((x) << 4)
  82. #define TP_UP_IRQ_EN(x) ((x) << 1)
  83. #define TP_DOWN_IRQ_EN(x) ((x) << 0)
  84. /* TP_INT_FIFOS irq and fifo status bits */
  85. #define TEMP_DATA_PENDING BIT(18)
  86. #define FIFO_OVERRUN_PENDING BIT(17)
  87. #define FIFO_DATA_PENDING BIT(16)
  88. #define TP_IDLE_FLG BIT(2)
  89. #define TP_UP_PENDING BIT(1)
  90. #define TP_DOWN_PENDING BIT(0)
  91. /* TP_TPR bits */
  92. #define TEMP_ENABLE(x) ((x) << 16)
  93. #define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
  94. struct sun4i_ts_data {
  95. struct device *dev;
  96. struct input_dev *input;
  97. void __iomem *base;
  98. unsigned int irq;
  99. bool ignore_fifo_data;
  100. int temp_data;
  101. int temp_offset;
  102. int temp_step;
  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 int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
  158. {
  159. /* No temp_data until the first irq */
  160. if (ts->temp_data == -1)
  161. return -EAGAIN;
  162. *temp = ts->temp_data * ts->temp_step - ts->temp_offset;
  163. return 0;
  164. }
  165. static int sun4i_get_tz_temp(struct thermal_zone_device *tz, int *temp)
  166. {
  167. return sun4i_get_temp(tz->devdata, temp);
  168. }
  169. static const struct thermal_zone_device_ops sun4i_ts_tz_ops = {
  170. .get_temp = sun4i_get_tz_temp,
  171. };
  172. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  173. char *buf)
  174. {
  175. struct sun4i_ts_data *ts = dev_get_drvdata(dev);
  176. int temp;
  177. int error;
  178. error = sun4i_get_temp(ts, &temp);
  179. if (error)
  180. return error;
  181. return sprintf(buf, "%d\n", temp);
  182. }
  183. static ssize_t show_temp_label(struct device *dev,
  184. struct device_attribute *devattr, char *buf)
  185. {
  186. return sprintf(buf, "SoC temperature\n");
  187. }
  188. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  189. static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
  190. static struct attribute *sun4i_ts_attrs[] = {
  191. &dev_attr_temp1_input.attr,
  192. &dev_attr_temp1_label.attr,
  193. NULL
  194. };
  195. ATTRIBUTE_GROUPS(sun4i_ts);
  196. static int sun4i_ts_probe(struct platform_device *pdev)
  197. {
  198. struct sun4i_ts_data *ts;
  199. struct device *dev = &pdev->dev;
  200. struct device_node *np = dev->of_node;
  201. struct device *hwmon;
  202. struct thermal_zone_device *thermal;
  203. int error;
  204. u32 reg;
  205. bool ts_attached;
  206. u32 tp_sensitive_adjust = 15;
  207. u32 filter_type = 1;
  208. ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
  209. if (!ts)
  210. return -ENOMEM;
  211. ts->dev = dev;
  212. ts->ignore_fifo_data = true;
  213. ts->temp_data = -1;
  214. if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
  215. /* Allwinner SDK has temperature (C) = (value / 6) - 271 */
  216. ts->temp_offset = 271000;
  217. ts->temp_step = 167;
  218. } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
  219. /*
  220. * The A10 temperature sensor has quite a wide spread, these
  221. * parameters are based on the averaging of the calibration
  222. * results of 4 completely different boards, with a spread of
  223. * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
  224. */
  225. ts->temp_offset = 257000;
  226. ts->temp_step = 133;
  227. } else {
  228. /*
  229. * The user manuals do not contain the formula for calculating
  230. * the temperature. The formula used here is from the AXP209,
  231. * which is designed by X-Powers, an affiliate of Allwinner:
  232. *
  233. * temperature (C) = (value * 0.1) - 144.7
  234. *
  235. * Allwinner does not have any documentation whatsoever for
  236. * this hardware. Moreover, it is claimed that the sensor
  237. * is inaccurate and cannot work properly.
  238. */
  239. ts->temp_offset = 144700;
  240. ts->temp_step = 100;
  241. }
  242. ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
  243. if (ts_attached) {
  244. ts->input = devm_input_allocate_device(dev);
  245. if (!ts->input)
  246. return -ENOMEM;
  247. ts->input->name = pdev->name;
  248. ts->input->phys = "sun4i_ts/input0";
  249. ts->input->open = sun4i_ts_open;
  250. ts->input->close = sun4i_ts_close;
  251. ts->input->id.bustype = BUS_HOST;
  252. ts->input->id.vendor = 0x0001;
  253. ts->input->id.product = 0x0001;
  254. ts->input->id.version = 0x0100;
  255. ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
  256. __set_bit(BTN_TOUCH, ts->input->keybit);
  257. input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
  258. input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
  259. input_set_drvdata(ts->input, ts);
  260. }
  261. ts->base = devm_platform_ioremap_resource(pdev, 0);
  262. if (IS_ERR(ts->base))
  263. return PTR_ERR(ts->base);
  264. ts->irq = platform_get_irq(pdev, 0);
  265. error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
  266. if (error)
  267. return error;
  268. /*
  269. * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
  270. * t_acq = clkin / (16 * 64)
  271. */
  272. writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
  273. ts->base + TP_CTRL0);
  274. /*
  275. * tp_sensitive_adjust is an optional property
  276. * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
  277. */
  278. of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
  279. &tp_sensitive_adjust);
  280. writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
  281. ts->base + TP_CTRL2);
  282. /*
  283. * Enable median and averaging filter, optional property for
  284. * filter type.
  285. */
  286. of_property_read_u32(np, "allwinner,filter-type", &filter_type);
  287. writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
  288. /* Enable temperature measurement, period 1953 (2 seconds) */
  289. writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
  290. /*
  291. * Set stylus up debounce to aprox 10 ms, enable debounce, and
  292. * finally enable tp mode.
  293. */
  294. reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
  295. if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
  296. reg |= SUN6I_TP_MODE_EN(1);
  297. else
  298. reg |= TP_MODE_EN(1);
  299. writel(reg, ts->base + TP_CTRL1);
  300. /*
  301. * The thermal core does not register hwmon devices for DT-based
  302. * thermal zone sensors, such as this one.
  303. */
  304. hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
  305. ts, sun4i_ts_groups);
  306. if (IS_ERR(hwmon))
  307. return PTR_ERR(hwmon);
  308. thermal = devm_thermal_of_zone_register(ts->dev, 0, ts,
  309. &sun4i_ts_tz_ops);
  310. if (IS_ERR(thermal))
  311. return PTR_ERR(thermal);
  312. writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  313. if (ts_attached) {
  314. error = input_register_device(ts->input);
  315. if (error) {
  316. writel(0, ts->base + TP_INT_FIFOC);
  317. return error;
  318. }
  319. }
  320. platform_set_drvdata(pdev, ts);
  321. return 0;
  322. }
  323. static int sun4i_ts_remove(struct platform_device *pdev)
  324. {
  325. struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
  326. /* Explicit unregister to avoid open/close changing the imask later */
  327. if (ts->input)
  328. input_unregister_device(ts->input);
  329. /* Deactivate all IRQs */
  330. writel(0, ts->base + TP_INT_FIFOC);
  331. return 0;
  332. }
  333. static const struct of_device_id sun4i_ts_of_match[] = {
  334. { .compatible = "allwinner,sun4i-a10-ts", },
  335. { .compatible = "allwinner,sun5i-a13-ts", },
  336. { .compatible = "allwinner,sun6i-a31-ts", },
  337. { /* sentinel */ }
  338. };
  339. MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
  340. static struct platform_driver sun4i_ts_driver = {
  341. .driver = {
  342. .name = "sun4i-ts",
  343. .of_match_table = of_match_ptr(sun4i_ts_of_match),
  344. },
  345. .probe = sun4i_ts_probe,
  346. .remove = sun4i_ts_remove,
  347. };
  348. module_platform_driver(sun4i_ts_driver);
  349. MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
  350. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  351. MODULE_LICENSE("GPL");