stmpe-ts.c 9.6 KB

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