stmpe-ts.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * STMicroelectronics STMPE811 Touchscreen Driver
  4. *
  5. * (C) 2010 Luotao Fu <[email protected]>
  6. * All rights reserved.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/device.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/input.h>
  16. #include <linux/input/touchscreen.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <linux/i2c.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/mfd/stmpe.h>
  22. /* Register layouts and functionalities are identical on all stmpexxx variants
  23. * with touchscreen controller
  24. */
  25. #define STMPE_REG_INT_STA 0x0B
  26. #define STMPE_REG_TSC_CTRL 0x40
  27. #define STMPE_REG_TSC_CFG 0x41
  28. #define STMPE_REG_FIFO_TH 0x4A
  29. #define STMPE_REG_FIFO_STA 0x4B
  30. #define STMPE_REG_FIFO_SIZE 0x4C
  31. #define STMPE_REG_TSC_DATA_XYZ 0x52
  32. #define STMPE_REG_TSC_FRACTION_Z 0x56
  33. #define STMPE_REG_TSC_I_DRIVE 0x58
  34. #define OP_MOD_XYZ 0
  35. #define STMPE_TSC_CTRL_TSC_EN (1<<0)
  36. #define STMPE_FIFO_STA_RESET (1<<0)
  37. #define STMPE_IRQ_TOUCH_DET 0
  38. #define STMPE_TS_NAME "stmpe-ts"
  39. #define XY_MASK 0xfff
  40. /**
  41. * struct stmpe_touch - stmpe811 touch screen controller state
  42. * @stmpe: pointer back to STMPE MFD container
  43. * @idev: registered input device
  44. * @work: a work item used to scan the device
  45. * @dev: a pointer back to the MFD cell struct device*
  46. * @prop: Touchscreen properties
  47. * @ave_ctrl: Sample average control
  48. * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
  49. * @touch_det_delay: Touch detect interrupt delay
  50. * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
  51. * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
  52. * recommended is 3
  53. * @settling: Panel driver settling time
  54. * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
  55. * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
  56. * recommended is 2
  57. * @fraction_z: Length of the fractional part in z
  58. * (fraction_z ([0..7]) = Count of the fractional part)
  59. * recommended is 7
  60. * @i_drive: current limit value of the touchscreen drivers
  61. * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
  62. */
  63. struct stmpe_touch {
  64. struct stmpe *stmpe;
  65. struct input_dev *idev;
  66. struct delayed_work work;
  67. struct device *dev;
  68. struct touchscreen_properties prop;
  69. u8 ave_ctrl;
  70. u8 touch_det_delay;
  71. u8 settling;
  72. u8 fraction_z;
  73. u8 i_drive;
  74. };
  75. static int __stmpe_reset_fifo(struct stmpe *stmpe)
  76. {
  77. int ret;
  78. ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
  79. STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
  80. if (ret)
  81. return ret;
  82. return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
  83. STMPE_FIFO_STA_RESET, 0);
  84. }
  85. static void stmpe_work(struct work_struct *work)
  86. {
  87. int int_sta;
  88. u32 timeout = 40;
  89. struct stmpe_touch *ts =
  90. container_of(work, struct stmpe_touch, work.work);
  91. int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
  92. /*
  93. * touch_det sometimes get desasserted or just get stuck. This appears
  94. * to be a silicon bug, We still have to clearify this with the
  95. * manufacture. As a workaround We release the key anyway if the
  96. * touch_det keeps coming in after 4ms, while the FIFO contains no value
  97. * during the whole time.
  98. */
  99. while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
  100. timeout--;
  101. int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
  102. udelay(100);
  103. }
  104. /* reset the FIFO before we report release event */
  105. __stmpe_reset_fifo(ts->stmpe);
  106. input_report_abs(ts->idev, ABS_PRESSURE, 0);
  107. input_report_key(ts->idev, BTN_TOUCH, 0);
  108. input_sync(ts->idev);
  109. }
  110. static irqreturn_t stmpe_ts_handler(int irq, void *data)
  111. {
  112. u8 data_set[4];
  113. int x, y, z;
  114. struct stmpe_touch *ts = data;
  115. /*
  116. * Cancel scheduled polling for release if we have new value
  117. * available. Wait if the polling is already running.
  118. */
  119. cancel_delayed_work_sync(&ts->work);
  120. /*
  121. * The FIFO sometimes just crashes and stops generating interrupts. This
  122. * appears to be a silicon bug. We still have to clearify this with
  123. * the manufacture. As a workaround we disable the TSC while we are
  124. * collecting data and flush the FIFO after reading
  125. */
  126. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  127. STMPE_TSC_CTRL_TSC_EN, 0);
  128. stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
  129. x = (data_set[0] << 4) | (data_set[1] >> 4);
  130. y = ((data_set[1] & 0xf) << 8) | data_set[2];
  131. z = data_set[3];
  132. touchscreen_report_pos(ts->idev, &ts->prop, x, y, false);
  133. input_report_abs(ts->idev, ABS_PRESSURE, z);
  134. input_report_key(ts->idev, BTN_TOUCH, 1);
  135. input_sync(ts->idev);
  136. /* flush the FIFO after we have read out our values. */
  137. __stmpe_reset_fifo(ts->stmpe);
  138. /* reenable the tsc */
  139. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  140. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  141. /* start polling for touch_det to detect release */
  142. schedule_delayed_work(&ts->work, msecs_to_jiffies(50));
  143. return IRQ_HANDLED;
  144. }
  145. static int stmpe_init_hw(struct stmpe_touch *ts)
  146. {
  147. int ret;
  148. u8 tsc_cfg, tsc_cfg_mask;
  149. struct stmpe *stmpe = ts->stmpe;
  150. struct device *dev = ts->dev;
  151. ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
  152. if (ret) {
  153. dev_err(dev, "Could not enable clock for ADC and TS\n");
  154. return ret;
  155. }
  156. ret = stmpe811_adc_common_init(stmpe);
  157. if (ret) {
  158. stmpe_disable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
  159. return ret;
  160. }
  161. tsc_cfg = STMPE_AVE_CTRL(ts->ave_ctrl) |
  162. STMPE_DET_DELAY(ts->touch_det_delay) |
  163. STMPE_SETTLING(ts->settling);
  164. tsc_cfg_mask = STMPE_AVE_CTRL(0xff) | STMPE_DET_DELAY(0xff) |
  165. STMPE_SETTLING(0xff);
  166. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
  167. if (ret) {
  168. dev_err(dev, "Could not config touch\n");
  169. return ret;
  170. }
  171. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
  172. STMPE_FRACTION_Z(0xff), STMPE_FRACTION_Z(ts->fraction_z));
  173. if (ret) {
  174. dev_err(dev, "Could not config touch\n");
  175. return ret;
  176. }
  177. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
  178. STMPE_I_DRIVE(0xff), STMPE_I_DRIVE(ts->i_drive));
  179. if (ret) {
  180. dev_err(dev, "Could not config touch\n");
  181. return ret;
  182. }
  183. /* set FIFO to 1 for single point reading */
  184. ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
  185. if (ret) {
  186. dev_err(dev, "Could not set FIFO\n");
  187. return ret;
  188. }
  189. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
  190. STMPE_OP_MODE(0xff), STMPE_OP_MODE(OP_MOD_XYZ));
  191. if (ret) {
  192. dev_err(dev, "Could not set mode\n");
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static int stmpe_ts_open(struct input_dev *dev)
  198. {
  199. struct stmpe_touch *ts = input_get_drvdata(dev);
  200. int ret = 0;
  201. ret = __stmpe_reset_fifo(ts->stmpe);
  202. if (ret)
  203. return ret;
  204. return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  205. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  206. }
  207. static void stmpe_ts_close(struct input_dev *dev)
  208. {
  209. struct stmpe_touch *ts = input_get_drvdata(dev);
  210. cancel_delayed_work_sync(&ts->work);
  211. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  212. STMPE_TSC_CTRL_TSC_EN, 0);
  213. }
  214. static void stmpe_ts_get_platform_info(struct platform_device *pdev,
  215. struct stmpe_touch *ts)
  216. {
  217. struct device_node *np = pdev->dev.of_node;
  218. u32 val;
  219. if (np) {
  220. if (!of_property_read_u32(np, "st,sample-time", &val))
  221. ts->stmpe->sample_time = val;
  222. if (!of_property_read_u32(np, "st,mod-12b", &val))
  223. ts->stmpe->mod_12b = val;
  224. if (!of_property_read_u32(np, "st,ref-sel", &val))
  225. ts->stmpe->ref_sel = val;
  226. if (!of_property_read_u32(np, "st,adc-freq", &val))
  227. ts->stmpe->adc_freq = val;
  228. if (!of_property_read_u32(np, "st,ave-ctrl", &val))
  229. ts->ave_ctrl = val;
  230. if (!of_property_read_u32(np, "st,touch-det-delay", &val))
  231. ts->touch_det_delay = val;
  232. if (!of_property_read_u32(np, "st,settling", &val))
  233. ts->settling = val;
  234. if (!of_property_read_u32(np, "st,fraction-z", &val))
  235. ts->fraction_z = val;
  236. if (!of_property_read_u32(np, "st,i-drive", &val))
  237. ts->i_drive = val;
  238. }
  239. }
  240. static int stmpe_input_probe(struct platform_device *pdev)
  241. {
  242. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  243. struct stmpe_touch *ts;
  244. struct input_dev *idev;
  245. int error;
  246. int ts_irq;
  247. ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
  248. if (ts_irq < 0)
  249. return ts_irq;
  250. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  251. if (!ts)
  252. return -ENOMEM;
  253. idev = devm_input_allocate_device(&pdev->dev);
  254. if (!idev)
  255. return -ENOMEM;
  256. platform_set_drvdata(pdev, ts);
  257. ts->stmpe = stmpe;
  258. ts->idev = idev;
  259. ts->dev = &pdev->dev;
  260. stmpe_ts_get_platform_info(pdev, ts);
  261. INIT_DELAYED_WORK(&ts->work, stmpe_work);
  262. error = devm_request_threaded_irq(&pdev->dev, ts_irq,
  263. NULL, stmpe_ts_handler,
  264. IRQF_ONESHOT, STMPE_TS_NAME, ts);
  265. if (error) {
  266. dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
  267. return error;
  268. }
  269. error = stmpe_init_hw(ts);
  270. if (error)
  271. return error;
  272. idev->name = STMPE_TS_NAME;
  273. idev->phys = STMPE_TS_NAME"/input0";
  274. idev->id.bustype = BUS_I2C;
  275. idev->open = stmpe_ts_open;
  276. idev->close = stmpe_ts_close;
  277. input_set_drvdata(idev, ts);
  278. input_set_capability(idev, EV_KEY, BTN_TOUCH);
  279. input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
  280. input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
  281. input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
  282. touchscreen_parse_properties(idev, false, &ts->prop);
  283. error = input_register_device(idev);
  284. if (error) {
  285. dev_err(&pdev->dev, "Could not register input device\n");
  286. return error;
  287. }
  288. return 0;
  289. }
  290. static int stmpe_ts_remove(struct platform_device *pdev)
  291. {
  292. struct stmpe_touch *ts = platform_get_drvdata(pdev);
  293. stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
  294. return 0;
  295. }
  296. static struct platform_driver stmpe_ts_driver = {
  297. .driver = {
  298. .name = STMPE_TS_NAME,
  299. },
  300. .probe = stmpe_input_probe,
  301. .remove = stmpe_ts_remove,
  302. };
  303. module_platform_driver(stmpe_ts_driver);
  304. static const struct of_device_id stmpe_ts_ids[] = {
  305. { .compatible = "st,stmpe-ts", },
  306. { },
  307. };
  308. MODULE_DEVICE_TABLE(of, stmpe_ts_ids);
  309. MODULE_AUTHOR("Luotao Fu <[email protected]>");
  310. MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
  311. MODULE_LICENSE("GPL");