stmpe-ts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /**
  57. * struct stmpe_touch - stmpe811 touch screen controller state
  58. * @stmpe: pointer back to STMPE MFD container
  59. * @idev: registered input device
  60. * @work: a work item used to scan the device
  61. * @dev: a pointer back to the MFD cell struct device*
  62. * @sample_time: ADC converstion time in number of clock.
  63. * (0 -> 36 clocks, 1 -> 44 clocks, 2 -> 56 clocks, 3 -> 64 clocks,
  64. * 4 -> 80 clocks, 5 -> 96 clocks, 6 -> 144 clocks),
  65. * recommended is 4.
  66. * @mod_12b: ADC Bit mode (0 -> 10bit ADC, 1 -> 12bit ADC)
  67. * @ref_sel: ADC reference source
  68. * (0 -> internal reference, 1 -> external reference)
  69. * @adc_freq: ADC Clock speed
  70. * (0 -> 1.625 MHz, 1 -> 3.25 MHz, 2 || 3 -> 6.5 MHz)
  71. * @ave_ctrl: Sample average control
  72. * (0 -> 1 sample, 1 -> 2 samples, 2 -> 4 samples, 3 -> 8 samples)
  73. * @touch_det_delay: Touch detect interrupt delay
  74. * (0 -> 10 us, 1 -> 50 us, 2 -> 100 us, 3 -> 500 us,
  75. * 4-> 1 ms, 5 -> 5 ms, 6 -> 10 ms, 7 -> 50 ms)
  76. * recommended is 3
  77. * @settling: Panel driver settling time
  78. * (0 -> 10 us, 1 -> 100 us, 2 -> 500 us, 3 -> 1 ms,
  79. * 4 -> 5 ms, 5 -> 10 ms, 6 for 50 ms, 7 -> 100 ms)
  80. * recommended is 2
  81. * @fraction_z: Length of the fractional part in z
  82. * (fraction_z ([0..7]) = Count of the fractional part)
  83. * recommended is 7
  84. * @i_drive: current limit value of the touchscreen drivers
  85. * (0 -> 20 mA typical 35 mA max, 1 -> 50 mA typical 80 mA max)
  86. */
  87. struct stmpe_touch {
  88. struct stmpe *stmpe;
  89. struct input_dev *idev;
  90. struct delayed_work work;
  91. struct device *dev;
  92. u8 sample_time;
  93. u8 mod_12b;
  94. u8 ref_sel;
  95. u8 adc_freq;
  96. u8 ave_ctrl;
  97. u8 touch_det_delay;
  98. u8 settling;
  99. u8 fraction_z;
  100. u8 i_drive;
  101. };
  102. static int __stmpe_reset_fifo(struct stmpe *stmpe)
  103. {
  104. int ret;
  105. ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
  106. STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);
  107. if (ret)
  108. return ret;
  109. return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,
  110. STMPE_FIFO_STA_RESET, 0);
  111. }
  112. static void stmpe_work(struct work_struct *work)
  113. {
  114. int int_sta;
  115. u32 timeout = 40;
  116. struct stmpe_touch *ts =
  117. container_of(work, struct stmpe_touch, work.work);
  118. int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
  119. /*
  120. * touch_det sometimes get desasserted or just get stuck. This appears
  121. * to be a silicon bug, We still have to clearify this with the
  122. * manufacture. As a workaround We release the key anyway if the
  123. * touch_det keeps coming in after 4ms, while the FIFO contains no value
  124. * during the whole time.
  125. */
  126. while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {
  127. timeout--;
  128. int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);
  129. udelay(100);
  130. }
  131. /* reset the FIFO before we report release event */
  132. __stmpe_reset_fifo(ts->stmpe);
  133. input_report_abs(ts->idev, ABS_PRESSURE, 0);
  134. input_report_key(ts->idev, BTN_TOUCH, 0);
  135. input_sync(ts->idev);
  136. }
  137. static irqreturn_t stmpe_ts_handler(int irq, void *data)
  138. {
  139. u8 data_set[4];
  140. int x, y, z;
  141. struct stmpe_touch *ts = data;
  142. /*
  143. * Cancel scheduled polling for release if we have new value
  144. * available. Wait if the polling is already running.
  145. */
  146. cancel_delayed_work_sync(&ts->work);
  147. /*
  148. * The FIFO sometimes just crashes and stops generating interrupts. This
  149. * appears to be a silicon bug. We still have to clearify this with
  150. * the manufacture. As a workaround we disable the TSC while we are
  151. * collecting data and flush the FIFO after reading
  152. */
  153. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  154. STMPE_TSC_CTRL_TSC_EN, 0);
  155. stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
  156. x = (data_set[0] << 4) | (data_set[1] >> 4);
  157. y = ((data_set[1] & 0xf) << 8) | data_set[2];
  158. z = data_set[3];
  159. input_report_abs(ts->idev, ABS_X, x);
  160. input_report_abs(ts->idev, ABS_Y, y);
  161. input_report_abs(ts->idev, ABS_PRESSURE, z);
  162. input_report_key(ts->idev, BTN_TOUCH, 1);
  163. input_sync(ts->idev);
  164. /* flush the FIFO after we have read out our values. */
  165. __stmpe_reset_fifo(ts->stmpe);
  166. /* reenable the tsc */
  167. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  168. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  169. /* start polling for touch_det to detect release */
  170. schedule_delayed_work(&ts->work, msecs_to_jiffies(50));
  171. return IRQ_HANDLED;
  172. }
  173. static int stmpe_init_hw(struct stmpe_touch *ts)
  174. {
  175. int ret;
  176. u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
  177. struct stmpe *stmpe = ts->stmpe;
  178. struct device *dev = ts->dev;
  179. ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
  180. if (ret) {
  181. dev_err(dev, "Could not enable clock for ADC and TS\n");
  182. return ret;
  183. }
  184. adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
  185. REF_SEL(ts->ref_sel);
  186. adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
  187. ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
  188. adc_ctrl1_mask, adc_ctrl1);
  189. if (ret) {
  190. dev_err(dev, "Could not setup ADC\n");
  191. return ret;
  192. }
  193. ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
  194. ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
  195. if (ret) {
  196. dev_err(dev, "Could not setup ADC\n");
  197. return ret;
  198. }
  199. tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
  200. SETTLING(ts->settling);
  201. tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
  202. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
  203. if (ret) {
  204. dev_err(dev, "Could not config touch\n");
  205. return ret;
  206. }
  207. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,
  208. FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
  209. if (ret) {
  210. dev_err(dev, "Could not config touch\n");
  211. return ret;
  212. }
  213. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
  214. I_DRIVE(0xff), I_DRIVE(ts->i_drive));
  215. if (ret) {
  216. dev_err(dev, "Could not config touch\n");
  217. return ret;
  218. }
  219. /* set FIFO to 1 for single point reading */
  220. ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
  221. if (ret) {
  222. dev_err(dev, "Could not set FIFO\n");
  223. return ret;
  224. }
  225. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
  226. OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
  227. if (ret) {
  228. dev_err(dev, "Could not set mode\n");
  229. return ret;
  230. }
  231. return 0;
  232. }
  233. static int stmpe_ts_open(struct input_dev *dev)
  234. {
  235. struct stmpe_touch *ts = input_get_drvdata(dev);
  236. int ret = 0;
  237. ret = __stmpe_reset_fifo(ts->stmpe);
  238. if (ret)
  239. return ret;
  240. return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  241. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  242. }
  243. static void stmpe_ts_close(struct input_dev *dev)
  244. {
  245. struct stmpe_touch *ts = input_get_drvdata(dev);
  246. cancel_delayed_work_sync(&ts->work);
  247. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  248. STMPE_TSC_CTRL_TSC_EN, 0);
  249. }
  250. static void stmpe_ts_get_platform_info(struct platform_device *pdev,
  251. struct stmpe_touch *ts)
  252. {
  253. struct device_node *np = pdev->dev.of_node;
  254. u32 val;
  255. if (np) {
  256. if (!of_property_read_u32(np, "st,sample-time", &val))
  257. ts->sample_time = val;
  258. if (!of_property_read_u32(np, "st,mod-12b", &val))
  259. ts->mod_12b = val;
  260. if (!of_property_read_u32(np, "st,ref-sel", &val))
  261. ts->ref_sel = val;
  262. if (!of_property_read_u32(np, "st,adc-freq", &val))
  263. ts->adc_freq = val;
  264. if (!of_property_read_u32(np, "st,ave-ctrl", &val))
  265. ts->ave_ctrl = val;
  266. if (!of_property_read_u32(np, "st,touch-det-delay", &val))
  267. ts->touch_det_delay = val;
  268. if (!of_property_read_u32(np, "st,settling", &val))
  269. ts->settling = val;
  270. if (!of_property_read_u32(np, "st,fraction-z", &val))
  271. ts->fraction_z = val;
  272. if (!of_property_read_u32(np, "st,i-drive", &val))
  273. ts->i_drive = val;
  274. }
  275. }
  276. static int stmpe_input_probe(struct platform_device *pdev)
  277. {
  278. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  279. struct stmpe_touch *ts;
  280. struct input_dev *idev;
  281. int error;
  282. int ts_irq;
  283. ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
  284. if (ts_irq < 0)
  285. return ts_irq;
  286. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  287. if (!ts)
  288. return -ENOMEM;
  289. idev = devm_input_allocate_device(&pdev->dev);
  290. if (!idev)
  291. return -ENOMEM;
  292. platform_set_drvdata(pdev, ts);
  293. ts->stmpe = stmpe;
  294. ts->idev = idev;
  295. ts->dev = &pdev->dev;
  296. stmpe_ts_get_platform_info(pdev, ts);
  297. INIT_DELAYED_WORK(&ts->work, stmpe_work);
  298. error = devm_request_threaded_irq(&pdev->dev, ts_irq,
  299. NULL, stmpe_ts_handler,
  300. IRQF_ONESHOT, STMPE_TS_NAME, ts);
  301. if (error) {
  302. dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
  303. return error;
  304. }
  305. error = stmpe_init_hw(ts);
  306. if (error)
  307. return error;
  308. idev->name = STMPE_TS_NAME;
  309. idev->phys = STMPE_TS_NAME"/input0";
  310. idev->id.bustype = BUS_I2C;
  311. idev->open = stmpe_ts_open;
  312. idev->close = stmpe_ts_close;
  313. input_set_drvdata(idev, ts);
  314. input_set_capability(idev, EV_KEY, BTN_TOUCH);
  315. input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
  316. input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
  317. input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
  318. error = input_register_device(idev);
  319. if (error) {
  320. dev_err(&pdev->dev, "Could not register input device\n");
  321. return error;
  322. }
  323. return 0;
  324. }
  325. static int stmpe_ts_remove(struct platform_device *pdev)
  326. {
  327. struct stmpe_touch *ts = platform_get_drvdata(pdev);
  328. stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
  329. return 0;
  330. }
  331. static struct platform_driver stmpe_ts_driver = {
  332. .driver = {
  333. .name = STMPE_TS_NAME,
  334. },
  335. .probe = stmpe_input_probe,
  336. .remove = stmpe_ts_remove,
  337. };
  338. module_platform_driver(stmpe_ts_driver);
  339. static const struct of_device_id stmpe_ts_ids[] = {
  340. { .compatible = "st,stmpe-ts", },
  341. { },
  342. };
  343. MODULE_DEVICE_TABLE(of, stmpe_ts_ids);
  344. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  345. MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
  346. MODULE_LICENSE("GPL");