sun4i-ts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi/README)
  34. * (looks like the description in the A20 User Manual v1.3 is better
  35. * than the one in the A10 User Manual v.1.5)
  36. */
  37. #include <linux/err.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/thermal.h>
  40. #include <linux/init.h>
  41. #include <linux/input.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/io.h>
  44. #include <linux/module.h>
  45. #include <linux/of_platform.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/slab.h>
  48. #define TP_CTRL0 0x00
  49. #define TP_CTRL1 0x04
  50. #define TP_CTRL2 0x08
  51. #define TP_CTRL3 0x0c
  52. #define TP_INT_FIFOC 0x10
  53. #define TP_INT_FIFOS 0x14
  54. #define TP_TPR 0x18
  55. #define TP_CDAT 0x1c
  56. #define TEMP_DATA 0x20
  57. #define TP_DATA 0x24
  58. /* TP_CTRL0 bits */
  59. #define ADC_FIRST_DLY(x) ((x) << 24) /* 8 bits */
  60. #define ADC_FIRST_DLY_MODE(x) ((x) << 23)
  61. #define ADC_CLK_SEL(x) ((x) << 22)
  62. #define ADC_CLK_DIV(x) ((x) << 20) /* 3 bits */
  63. #define FS_DIV(x) ((x) << 16) /* 4 bits */
  64. #define T_ACQ(x) ((x) << 0) /* 16 bits */
  65. /* TP_CTRL1 bits */
  66. #define STYLUS_UP_DEBOUN(x) ((x) << 12) /* 8 bits */
  67. #define STYLUS_UP_DEBOUN_EN(x) ((x) << 9)
  68. #define TOUCH_PAN_CALI_EN(x) ((x) << 6)
  69. #define TP_DUAL_EN(x) ((x) << 5)
  70. #define TP_MODE_EN(x) ((x) << 4)
  71. #define TP_ADC_SELECT(x) ((x) << 3)
  72. #define ADC_CHAN_SELECT(x) ((x) << 0) /* 3 bits */
  73. /* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
  74. #define SUN6I_TP_MODE_EN(x) ((x) << 5)
  75. /* TP_CTRL2 bits */
  76. #define TP_SENSITIVE_ADJUST(x) ((x) << 28) /* 4 bits */
  77. #define TP_MODE_SELECT(x) ((x) << 26) /* 2 bits */
  78. #define PRE_MEA_EN(x) ((x) << 24)
  79. #define PRE_MEA_THRE_CNT(x) ((x) << 0) /* 24 bits */
  80. /* TP_CTRL3 bits */
  81. #define FILTER_EN(x) ((x) << 2)
  82. #define FILTER_TYPE(x) ((x) << 0) /* 2 bits */
  83. /* TP_INT_FIFOC irq and fifo mask / control bits */
  84. #define TEMP_IRQ_EN(x) ((x) << 18)
  85. #define OVERRUN_IRQ_EN(x) ((x) << 17)
  86. #define DATA_IRQ_EN(x) ((x) << 16)
  87. #define TP_DATA_XY_CHANGE(x) ((x) << 13)
  88. #define FIFO_TRIG(x) ((x) << 8) /* 5 bits */
  89. #define DATA_DRQ_EN(x) ((x) << 7)
  90. #define FIFO_FLUSH(x) ((x) << 4)
  91. #define TP_UP_IRQ_EN(x) ((x) << 1)
  92. #define TP_DOWN_IRQ_EN(x) ((x) << 0)
  93. /* TP_INT_FIFOS irq and fifo status bits */
  94. #define TEMP_DATA_PENDING BIT(18)
  95. #define FIFO_OVERRUN_PENDING BIT(17)
  96. #define FIFO_DATA_PENDING BIT(16)
  97. #define TP_IDLE_FLG BIT(2)
  98. #define TP_UP_PENDING BIT(1)
  99. #define TP_DOWN_PENDING BIT(0)
  100. /* TP_TPR bits */
  101. #define TEMP_ENABLE(x) ((x) << 16)
  102. #define TEMP_PERIOD(x) ((x) << 0) /* t = x * 256 * 16 / clkin */
  103. struct sun4i_ts_data {
  104. struct device *dev;
  105. struct input_dev *input;
  106. struct thermal_zone_device *tz;
  107. void __iomem *base;
  108. unsigned int irq;
  109. bool ignore_fifo_data;
  110. int temp_data;
  111. int temp_offset;
  112. int temp_step;
  113. };
  114. static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
  115. {
  116. u32 x, y;
  117. if (reg_val & FIFO_DATA_PENDING) {
  118. x = readl(ts->base + TP_DATA);
  119. y = readl(ts->base + TP_DATA);
  120. /* The 1st location reported after an up event is unreliable */
  121. if (!ts->ignore_fifo_data) {
  122. input_report_abs(ts->input, ABS_X, x);
  123. input_report_abs(ts->input, ABS_Y, y);
  124. /*
  125. * The hardware has a separate down status bit, but
  126. * that gets set before we get the first location,
  127. * resulting in reporting a click on the old location.
  128. */
  129. input_report_key(ts->input, BTN_TOUCH, 1);
  130. input_sync(ts->input);
  131. } else {
  132. ts->ignore_fifo_data = false;
  133. }
  134. }
  135. if (reg_val & TP_UP_PENDING) {
  136. ts->ignore_fifo_data = true;
  137. input_report_key(ts->input, BTN_TOUCH, 0);
  138. input_sync(ts->input);
  139. }
  140. }
  141. static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
  142. {
  143. struct sun4i_ts_data *ts = dev_id;
  144. u32 reg_val;
  145. reg_val = readl(ts->base + TP_INT_FIFOS);
  146. if (reg_val & TEMP_DATA_PENDING)
  147. ts->temp_data = readl(ts->base + TEMP_DATA);
  148. if (ts->input)
  149. sun4i_ts_irq_handle_input(ts, reg_val);
  150. writel(reg_val, ts->base + TP_INT_FIFOS);
  151. return IRQ_HANDLED;
  152. }
  153. static int sun4i_ts_open(struct input_dev *dev)
  154. {
  155. struct sun4i_ts_data *ts = input_get_drvdata(dev);
  156. /* Flush, set trig level to 1, enable temp, data and up irqs */
  157. writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
  158. TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  159. return 0;
  160. }
  161. static void sun4i_ts_close(struct input_dev *dev)
  162. {
  163. struct sun4i_ts_data *ts = input_get_drvdata(dev);
  164. /* Deactivate all input IRQs */
  165. writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  166. }
  167. static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
  168. {
  169. /* No temp_data until the first irq */
  170. if (ts->temp_data == -1)
  171. return -EAGAIN;
  172. *temp = ts->temp_data * ts->temp_step - ts->temp_offset;
  173. return 0;
  174. }
  175. static int sun4i_get_tz_temp(void *data, int *temp)
  176. {
  177. return sun4i_get_temp(data, temp);
  178. }
  179. static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
  180. .get_temp = sun4i_get_tz_temp,
  181. };
  182. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  183. char *buf)
  184. {
  185. struct sun4i_ts_data *ts = dev_get_drvdata(dev);
  186. int temp;
  187. int error;
  188. error = sun4i_get_temp(ts, &temp);
  189. if (error)
  190. return error;
  191. return sprintf(buf, "%d\n", temp);
  192. }
  193. static ssize_t show_temp_label(struct device *dev,
  194. struct device_attribute *devattr, char *buf)
  195. {
  196. return sprintf(buf, "SoC temperature\n");
  197. }
  198. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  199. static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
  200. static struct attribute *sun4i_ts_attrs[] = {
  201. &dev_attr_temp1_input.attr,
  202. &dev_attr_temp1_label.attr,
  203. NULL
  204. };
  205. ATTRIBUTE_GROUPS(sun4i_ts);
  206. static int sun4i_ts_probe(struct platform_device *pdev)
  207. {
  208. struct sun4i_ts_data *ts;
  209. struct device *dev = &pdev->dev;
  210. struct device_node *np = dev->of_node;
  211. struct device *hwmon;
  212. int error;
  213. u32 reg;
  214. bool ts_attached;
  215. u32 tp_sensitive_adjust = 15;
  216. u32 filter_type = 1;
  217. ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
  218. if (!ts)
  219. return -ENOMEM;
  220. ts->dev = dev;
  221. ts->ignore_fifo_data = true;
  222. ts->temp_data = -1;
  223. if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
  224. /* Allwinner SDK has temperature (C) = (value / 6) - 271 */
  225. ts->temp_offset = 271000;
  226. ts->temp_step = 167;
  227. } else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
  228. /*
  229. * The A10 temperature sensor has quite a wide spread, these
  230. * parameters are based on the averaging of the calibration
  231. * results of 4 completely different boards, with a spread of
  232. * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
  233. */
  234. ts->temp_offset = 257000;
  235. ts->temp_step = 133;
  236. } else {
  237. /*
  238. * The user manuals do not contain the formula for calculating
  239. * the temperature. The formula used here is from the AXP209,
  240. * which is designed by X-Powers, an affiliate of Allwinner:
  241. *
  242. * temperature (C) = (value * 0.1) - 144.7
  243. *
  244. * Allwinner does not have any documentation whatsoever for
  245. * this hardware. Moreover, it is claimed that the sensor
  246. * is inaccurate and cannot work properly.
  247. */
  248. ts->temp_offset = 144700;
  249. ts->temp_step = 100;
  250. }
  251. ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
  252. if (ts_attached) {
  253. ts->input = devm_input_allocate_device(dev);
  254. if (!ts->input)
  255. return -ENOMEM;
  256. ts->input->name = pdev->name;
  257. ts->input->phys = "sun4i_ts/input0";
  258. ts->input->open = sun4i_ts_open;
  259. ts->input->close = sun4i_ts_close;
  260. ts->input->id.bustype = BUS_HOST;
  261. ts->input->id.vendor = 0x0001;
  262. ts->input->id.product = 0x0001;
  263. ts->input->id.version = 0x0100;
  264. ts->input->evbit[0] = BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
  265. __set_bit(BTN_TOUCH, ts->input->keybit);
  266. input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
  267. input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
  268. input_set_drvdata(ts->input, ts);
  269. }
  270. ts->base = devm_ioremap_resource(dev,
  271. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  272. if (IS_ERR(ts->base))
  273. return PTR_ERR(ts->base);
  274. ts->irq = platform_get_irq(pdev, 0);
  275. error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
  276. if (error)
  277. return error;
  278. /*
  279. * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
  280. * t_acq = clkin / (16 * 64)
  281. */
  282. writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
  283. ts->base + TP_CTRL0);
  284. /*
  285. * tp_sensitive_adjust is an optional property
  286. * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
  287. */
  288. of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
  289. &tp_sensitive_adjust);
  290. writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
  291. ts->base + TP_CTRL2);
  292. /*
  293. * Enable median and averaging filter, optional property for
  294. * filter type.
  295. */
  296. of_property_read_u32(np, "allwinner,filter-type", &filter_type);
  297. writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
  298. /* Enable temperature measurement, period 1953 (2 seconds) */
  299. writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
  300. /*
  301. * Set stylus up debounce to aprox 10 ms, enable debounce, and
  302. * finally enable tp mode.
  303. */
  304. reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
  305. if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
  306. reg |= SUN6I_TP_MODE_EN(1);
  307. else
  308. reg |= TP_MODE_EN(1);
  309. writel(reg, ts->base + TP_CTRL1);
  310. /*
  311. * The thermal core does not register hwmon devices for DT-based
  312. * thermal zone sensors, such as this one.
  313. */
  314. hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
  315. ts, sun4i_ts_groups);
  316. if (IS_ERR(hwmon))
  317. return PTR_ERR(hwmon);
  318. ts->tz = thermal_zone_of_sensor_register(ts->dev, 0, ts,
  319. &sun4i_ts_tz_ops);
  320. if (IS_ERR(ts->tz))
  321. ts->tz = NULL;
  322. writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
  323. if (ts_attached) {
  324. error = input_register_device(ts->input);
  325. if (error) {
  326. writel(0, ts->base + TP_INT_FIFOC);
  327. thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
  328. return error;
  329. }
  330. }
  331. platform_set_drvdata(pdev, ts);
  332. return 0;
  333. }
  334. static int sun4i_ts_remove(struct platform_device *pdev)
  335. {
  336. struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
  337. /* Explicit unregister to avoid open/close changing the imask later */
  338. if (ts->input)
  339. input_unregister_device(ts->input);
  340. thermal_zone_of_sensor_unregister(ts->dev, ts->tz);
  341. /* Deactivate all IRQs */
  342. writel(0, ts->base + TP_INT_FIFOC);
  343. return 0;
  344. }
  345. static const struct of_device_id sun4i_ts_of_match[] = {
  346. { .compatible = "allwinner,sun4i-a10-ts", },
  347. { .compatible = "allwinner,sun5i-a13-ts", },
  348. { .compatible = "allwinner,sun6i-a31-ts", },
  349. { /* sentinel */ }
  350. };
  351. MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
  352. static struct platform_driver sun4i_ts_driver = {
  353. .driver = {
  354. .name = "sun4i-ts",
  355. .of_match_table = of_match_ptr(sun4i_ts_of_match),
  356. },
  357. .probe = sun4i_ts_probe,
  358. .remove = sun4i_ts_remove,
  359. };
  360. module_platform_driver(sun4i_ts_driver);
  361. MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
  362. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  363. MODULE_LICENSE("GPL");