stmpe-ts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_report_key(ts->idev, BTN_TOUCH, 0);
  105. input_sync(ts->idev);
  106. }
  107. static irqreturn_t stmpe_ts_handler(int irq, void *data)
  108. {
  109. u8 data_set[4];
  110. int x, y, z;
  111. struct stmpe_touch *ts = data;
  112. /*
  113. * Cancel scheduled polling for release if we have new value
  114. * available. Wait if the polling is already running.
  115. */
  116. cancel_delayed_work_sync(&ts->work);
  117. /*
  118. * The FIFO sometimes just crashes and stops generating interrupts. This
  119. * appears to be a silicon bug. We still have to clearify this with
  120. * the manufacture. As a workaround we disable the TSC while we are
  121. * collecting data and flush the FIFO after reading
  122. */
  123. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  124. STMPE_TSC_CTRL_TSC_EN, 0);
  125. stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);
  126. x = (data_set[0] << 4) | (data_set[1] >> 4);
  127. y = ((data_set[1] & 0xf) << 8) | data_set[2];
  128. z = data_set[3];
  129. input_report_abs(ts->idev, ABS_X, x);
  130. input_report_abs(ts->idev, ABS_Y, y);
  131. input_report_abs(ts->idev, ABS_PRESSURE, z);
  132. input_report_key(ts->idev, BTN_TOUCH, 1);
  133. input_sync(ts->idev);
  134. /* flush the FIFO after we have read out our values. */
  135. __stmpe_reset_fifo(ts->stmpe);
  136. /* reenable the tsc */
  137. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  138. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  139. /* start polling for touch_det to detect release */
  140. schedule_delayed_work(&ts->work, HZ / 50);
  141. return IRQ_HANDLED;
  142. }
  143. static int stmpe_init_hw(struct stmpe_touch *ts)
  144. {
  145. int ret;
  146. u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;
  147. struct stmpe *stmpe = ts->stmpe;
  148. struct device *dev = ts->dev;
  149. ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);
  150. if (ret) {
  151. dev_err(dev, "Could not enable clock for ADC and TS\n");
  152. return ret;
  153. }
  154. adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |
  155. REF_SEL(ts->ref_sel);
  156. adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);
  157. ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,
  158. adc_ctrl1_mask, adc_ctrl1);
  159. if (ret) {
  160. dev_err(dev, "Could not setup ADC\n");
  161. return ret;
  162. }
  163. ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,
  164. ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));
  165. if (ret) {
  166. dev_err(dev, "Could not setup ADC\n");
  167. return ret;
  168. }
  169. tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |
  170. SETTLING(ts->settling);
  171. tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);
  172. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);
  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_FRACTION_Z,
  178. FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));
  179. if (ret) {
  180. dev_err(dev, "Could not config touch\n");
  181. return ret;
  182. }
  183. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,
  184. I_DRIVE(0xff), I_DRIVE(ts->i_drive));
  185. if (ret) {
  186. dev_err(dev, "Could not config touch\n");
  187. return ret;
  188. }
  189. /* set FIFO to 1 for single point reading */
  190. ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);
  191. if (ret) {
  192. dev_err(dev, "Could not set FIFO\n");
  193. return ret;
  194. }
  195. ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,
  196. OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));
  197. if (ret) {
  198. dev_err(dev, "Could not set mode\n");
  199. return ret;
  200. }
  201. return 0;
  202. }
  203. static int stmpe_ts_open(struct input_dev *dev)
  204. {
  205. struct stmpe_touch *ts = input_get_drvdata(dev);
  206. int ret = 0;
  207. ret = __stmpe_reset_fifo(ts->stmpe);
  208. if (ret)
  209. return ret;
  210. return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  211. STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);
  212. }
  213. static void stmpe_ts_close(struct input_dev *dev)
  214. {
  215. struct stmpe_touch *ts = input_get_drvdata(dev);
  216. cancel_delayed_work_sync(&ts->work);
  217. stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,
  218. STMPE_TSC_CTRL_TSC_EN, 0);
  219. }
  220. static void stmpe_ts_get_platform_info(struct platform_device *pdev,
  221. struct stmpe_touch *ts)
  222. {
  223. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  224. struct device_node *np = pdev->dev.of_node;
  225. struct stmpe_ts_platform_data *ts_pdata = NULL;
  226. ts->stmpe = stmpe;
  227. if (stmpe->pdata && stmpe->pdata->ts) {
  228. ts_pdata = stmpe->pdata->ts;
  229. ts->sample_time = ts_pdata->sample_time;
  230. ts->mod_12b = ts_pdata->mod_12b;
  231. ts->ref_sel = ts_pdata->ref_sel;
  232. ts->adc_freq = ts_pdata->adc_freq;
  233. ts->ave_ctrl = ts_pdata->ave_ctrl;
  234. ts->touch_det_delay = ts_pdata->touch_det_delay;
  235. ts->settling = ts_pdata->settling;
  236. ts->fraction_z = ts_pdata->fraction_z;
  237. ts->i_drive = ts_pdata->i_drive;
  238. } else if (np) {
  239. u32 val;
  240. if (!of_property_read_u32(np, "st,sample-time", &val))
  241. ts->sample_time = val;
  242. if (!of_property_read_u32(np, "st,mod-12b", &val))
  243. ts->mod_12b = val;
  244. if (!of_property_read_u32(np, "st,ref-sel", &val))
  245. ts->ref_sel = val;
  246. if (!of_property_read_u32(np, "st,adc-freq", &val))
  247. ts->adc_freq = val;
  248. if (!of_property_read_u32(np, "st,ave-ctrl", &val))
  249. ts->ave_ctrl = val;
  250. if (!of_property_read_u32(np, "st,touch-det-delay", &val))
  251. ts->touch_det_delay = val;
  252. if (!of_property_read_u32(np, "st,settling", &val))
  253. ts->settling = val;
  254. if (!of_property_read_u32(np, "st,fraction-z", &val))
  255. ts->fraction_z = val;
  256. if (!of_property_read_u32(np, "st,i-drive", &val))
  257. ts->i_drive = val;
  258. }
  259. }
  260. static int stmpe_input_probe(struct platform_device *pdev)
  261. {
  262. struct stmpe_touch *ts;
  263. struct input_dev *idev;
  264. int error;
  265. int ts_irq;
  266. ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");
  267. if (ts_irq < 0)
  268. return ts_irq;
  269. ts = devm_kzalloc(&pdev->dev, sizeof(*ts), GFP_KERNEL);
  270. if (!ts)
  271. return -ENOMEM;
  272. idev = devm_input_allocate_device(&pdev->dev);
  273. if (!idev)
  274. return -ENOMEM;
  275. platform_set_drvdata(pdev, ts);
  276. ts->idev = idev;
  277. ts->dev = &pdev->dev;
  278. stmpe_ts_get_platform_info(pdev, ts);
  279. INIT_DELAYED_WORK(&ts->work, stmpe_work);
  280. error = devm_request_threaded_irq(&pdev->dev, ts_irq,
  281. NULL, stmpe_ts_handler,
  282. IRQF_ONESHOT, STMPE_TS_NAME, ts);
  283. if (error) {
  284. dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);
  285. return error;
  286. }
  287. error = stmpe_init_hw(ts);
  288. if (error)
  289. return error;
  290. idev->name = STMPE_TS_NAME;
  291. idev->phys = STMPE_TS_NAME"/input0";
  292. idev->id.bustype = BUS_I2C;
  293. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  294. idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  295. idev->open = stmpe_ts_open;
  296. idev->close = stmpe_ts_close;
  297. input_set_drvdata(idev, ts);
  298. input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);
  299. input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);
  300. input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);
  301. error = input_register_device(idev);
  302. if (error) {
  303. dev_err(&pdev->dev, "Could not register input device\n");
  304. return error;
  305. }
  306. return 0;
  307. }
  308. static int stmpe_ts_remove(struct platform_device *pdev)
  309. {
  310. struct stmpe_touch *ts = platform_get_drvdata(pdev);
  311. stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);
  312. return 0;
  313. }
  314. static struct platform_driver stmpe_ts_driver = {
  315. .driver = {
  316. .name = STMPE_TS_NAME,
  317. .owner = THIS_MODULE,
  318. },
  319. .probe = stmpe_input_probe,
  320. .remove = stmpe_ts_remove,
  321. };
  322. module_platform_driver(stmpe_ts_driver);
  323. MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
  324. MODULE_DESCRIPTION("STMPEXXX touchscreen driver");
  325. MODULE_LICENSE("GPL");
  326. MODULE_ALIAS("platform:" STMPE_TS_NAME);