stmpe-ts.c 10 KB

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