stmpe-ts.c 9.5 KB

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