ti_am335x_tscadc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * TI Touch Screen / ADC MFD driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/regmap.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/sched.h>
  26. #include <linux/mfd/ti_am335x_tscadc.h>
  27. static unsigned int tscadc_readl(struct ti_tscadc_dev *tsadc, unsigned int reg)
  28. {
  29. unsigned int val;
  30. regmap_read(tsadc->regmap_tscadc, reg, &val);
  31. return val;
  32. }
  33. static void tscadc_writel(struct ti_tscadc_dev *tsadc, unsigned int reg,
  34. unsigned int val)
  35. {
  36. regmap_write(tsadc->regmap_tscadc, reg, val);
  37. }
  38. static const struct regmap_config tscadc_regmap_config = {
  39. .name = "ti_tscadc",
  40. .reg_bits = 32,
  41. .reg_stride = 4,
  42. .val_bits = 32,
  43. };
  44. void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tsadc, u32 val)
  45. {
  46. unsigned long flags;
  47. spin_lock_irqsave(&tsadc->reg_lock, flags);
  48. tsadc->reg_se_cache = val;
  49. if (tsadc->adc_waiting)
  50. wake_up(&tsadc->reg_se_wait);
  51. else if (!tsadc->adc_in_use)
  52. tscadc_writel(tsadc, REG_SE, val);
  53. spin_unlock_irqrestore(&tsadc->reg_lock, flags);
  54. }
  55. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
  56. static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tsadc)
  57. {
  58. DEFINE_WAIT(wait);
  59. u32 reg;
  60. /*
  61. * disable TSC steps so it does not run while the ADC is using it. If
  62. * write 0 while it is running (it just started or was already running)
  63. * then it completes all steps that were enabled and stops then.
  64. */
  65. tscadc_writel(tsadc, REG_SE, 0);
  66. reg = tscadc_readl(tsadc, REG_ADCFSM);
  67. if (reg & SEQ_STATUS) {
  68. tsadc->adc_waiting = true;
  69. prepare_to_wait(&tsadc->reg_se_wait, &wait,
  70. TASK_UNINTERRUPTIBLE);
  71. spin_unlock_irq(&tsadc->reg_lock);
  72. schedule();
  73. spin_lock_irq(&tsadc->reg_lock);
  74. finish_wait(&tsadc->reg_se_wait, &wait);
  75. reg = tscadc_readl(tsadc, REG_ADCFSM);
  76. WARN_ON(reg & SEQ_STATUS);
  77. tsadc->adc_waiting = false;
  78. }
  79. tsadc->adc_in_use = true;
  80. }
  81. void am335x_tsc_se_set_once(struct ti_tscadc_dev *tsadc, u32 val)
  82. {
  83. spin_lock_irq(&tsadc->reg_lock);
  84. am335x_tscadc_need_adc(tsadc);
  85. tscadc_writel(tsadc, REG_SE, val);
  86. spin_unlock_irq(&tsadc->reg_lock);
  87. }
  88. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
  89. void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tsadc)
  90. {
  91. unsigned long flags;
  92. spin_lock_irqsave(&tsadc->reg_lock, flags);
  93. tsadc->adc_in_use = false;
  94. tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
  95. spin_unlock_irqrestore(&tsadc->reg_lock, flags);
  96. }
  97. EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);
  98. void am335x_tsc_se_clr(struct ti_tscadc_dev *tsadc, u32 val)
  99. {
  100. unsigned long flags;
  101. spin_lock_irqsave(&tsadc->reg_lock, flags);
  102. tsadc->reg_se_cache &= ~val;
  103. tscadc_writel(tsadc, REG_SE, tsadc->reg_se_cache);
  104. spin_unlock_irqrestore(&tsadc->reg_lock, flags);
  105. }
  106. EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
  107. static void tscadc_idle_config(struct ti_tscadc_dev *config)
  108. {
  109. unsigned int idleconfig;
  110. idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
  111. STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
  112. tscadc_writel(config, REG_IDLECONFIG, idleconfig);
  113. }
  114. static int ti_tscadc_probe(struct platform_device *pdev)
  115. {
  116. struct ti_tscadc_dev *tscadc;
  117. struct resource *res;
  118. struct clk *clk;
  119. struct device_node *node = pdev->dev.of_node;
  120. struct mfd_cell *cell;
  121. struct property *prop;
  122. const __be32 *cur;
  123. u32 val;
  124. int err, ctrl;
  125. int clock_rate;
  126. int tsc_wires = 0, adc_channels = 0, total_channels;
  127. int readouts = 0;
  128. if (!pdev->dev.of_node) {
  129. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  130. return -EINVAL;
  131. }
  132. node = of_get_child_by_name(pdev->dev.of_node, "tsc");
  133. of_property_read_u32(node, "ti,wires", &tsc_wires);
  134. of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
  135. node = of_get_child_by_name(pdev->dev.of_node, "adc");
  136. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  137. adc_channels++;
  138. if (val > 7) {
  139. dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
  140. val);
  141. return -EINVAL;
  142. }
  143. }
  144. total_channels = tsc_wires + adc_channels;
  145. if (total_channels > 8) {
  146. dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
  147. return -EINVAL;
  148. }
  149. if (total_channels == 0) {
  150. dev_err(&pdev->dev, "Need atleast one channel.\n");
  151. return -EINVAL;
  152. }
  153. if (readouts * 2 + 2 + adc_channels > 16) {
  154. dev_err(&pdev->dev, "Too many step configurations requested\n");
  155. return -EINVAL;
  156. }
  157. /* Allocate memory for device */
  158. tscadc = devm_kzalloc(&pdev->dev,
  159. sizeof(struct ti_tscadc_dev), GFP_KERNEL);
  160. if (!tscadc) {
  161. dev_err(&pdev->dev, "failed to allocate memory.\n");
  162. return -ENOMEM;
  163. }
  164. tscadc->dev = &pdev->dev;
  165. err = platform_get_irq(pdev, 0);
  166. if (err < 0) {
  167. dev_err(&pdev->dev, "no irq ID is specified.\n");
  168. goto ret;
  169. } else
  170. tscadc->irq = err;
  171. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. tscadc->tscadc_base = devm_ioremap_resource(&pdev->dev, res);
  173. if (IS_ERR(tscadc->tscadc_base))
  174. return PTR_ERR(tscadc->tscadc_base);
  175. tscadc->regmap_tscadc = devm_regmap_init_mmio(&pdev->dev,
  176. tscadc->tscadc_base, &tscadc_regmap_config);
  177. if (IS_ERR(tscadc->regmap_tscadc)) {
  178. dev_err(&pdev->dev, "regmap init failed\n");
  179. err = PTR_ERR(tscadc->regmap_tscadc);
  180. goto ret;
  181. }
  182. spin_lock_init(&tscadc->reg_lock);
  183. init_waitqueue_head(&tscadc->reg_se_wait);
  184. pm_runtime_enable(&pdev->dev);
  185. pm_runtime_get_sync(&pdev->dev);
  186. /*
  187. * The TSC_ADC_Subsystem has 2 clock domains
  188. * OCP_CLK and ADC_CLK.
  189. * The ADC clock is expected to run at target of 3MHz,
  190. * and expected to capture 12-bit data at a rate of 200 KSPS.
  191. * The TSC_ADC_SS controller design assumes the OCP clock is
  192. * at least 6x faster than the ADC clock.
  193. */
  194. clk = clk_get(&pdev->dev, "adc_tsc_fck");
  195. if (IS_ERR(clk)) {
  196. dev_err(&pdev->dev, "failed to get TSC fck\n");
  197. err = PTR_ERR(clk);
  198. goto err_disable_clk;
  199. }
  200. clock_rate = clk_get_rate(clk);
  201. clk_put(clk);
  202. tscadc->clk_div = clock_rate / ADC_CLK;
  203. /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
  204. tscadc->clk_div--;
  205. tscadc_writel(tscadc, REG_CLKDIV, tscadc->clk_div);
  206. /* Set the control register bits */
  207. ctrl = CNTRLREG_STEPCONFIGWRT |
  208. CNTRLREG_STEPID;
  209. if (tsc_wires > 0)
  210. ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
  211. tscadc_writel(tscadc, REG_CTRL, ctrl);
  212. /* Set register bits for Idle Config Mode */
  213. if (tsc_wires > 0)
  214. tscadc_idle_config(tscadc);
  215. /* Enable the TSC module enable bit */
  216. ctrl = tscadc_readl(tscadc, REG_CTRL);
  217. ctrl |= CNTRLREG_TSCSSENB;
  218. tscadc_writel(tscadc, REG_CTRL, ctrl);
  219. tscadc->used_cells = 0;
  220. tscadc->tsc_cell = -1;
  221. tscadc->adc_cell = -1;
  222. /* TSC Cell */
  223. if (tsc_wires > 0) {
  224. tscadc->tsc_cell = tscadc->used_cells;
  225. cell = &tscadc->cells[tscadc->used_cells++];
  226. cell->name = "TI-am335x-tsc";
  227. cell->of_compatible = "ti,am3359-tsc";
  228. cell->platform_data = &tscadc;
  229. cell->pdata_size = sizeof(tscadc);
  230. }
  231. /* ADC Cell */
  232. if (adc_channels > 0) {
  233. tscadc->adc_cell = tscadc->used_cells;
  234. cell = &tscadc->cells[tscadc->used_cells++];
  235. cell->name = "TI-am335x-adc";
  236. cell->of_compatible = "ti,am3359-adc";
  237. cell->platform_data = &tscadc;
  238. cell->pdata_size = sizeof(tscadc);
  239. }
  240. err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
  241. tscadc->used_cells, NULL, 0, NULL);
  242. if (err < 0)
  243. goto err_disable_clk;
  244. device_init_wakeup(&pdev->dev, true);
  245. platform_set_drvdata(pdev, tscadc);
  246. return 0;
  247. err_disable_clk:
  248. pm_runtime_put_sync(&pdev->dev);
  249. pm_runtime_disable(&pdev->dev);
  250. ret:
  251. return err;
  252. }
  253. static int ti_tscadc_remove(struct platform_device *pdev)
  254. {
  255. struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
  256. tscadc_writel(tscadc, REG_SE, 0x00);
  257. pm_runtime_put_sync(&pdev->dev);
  258. pm_runtime_disable(&pdev->dev);
  259. mfd_remove_devices(tscadc->dev);
  260. return 0;
  261. }
  262. #ifdef CONFIG_PM
  263. static int tscadc_suspend(struct device *dev)
  264. {
  265. struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
  266. tscadc_writel(tscadc_dev, REG_SE, 0x00);
  267. pm_runtime_put_sync(dev);
  268. return 0;
  269. }
  270. static int tscadc_resume(struct device *dev)
  271. {
  272. struct ti_tscadc_dev *tscadc_dev = dev_get_drvdata(dev);
  273. unsigned int restore, ctrl;
  274. pm_runtime_get_sync(dev);
  275. /* context restore */
  276. ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
  277. if (tscadc_dev->tsc_cell != -1)
  278. ctrl |= CNTRLREG_TSCENB | CNTRLREG_4WIRE;
  279. tscadc_writel(tscadc_dev, REG_CTRL, ctrl);
  280. if (tscadc_dev->tsc_cell != -1)
  281. tscadc_idle_config(tscadc_dev);
  282. restore = tscadc_readl(tscadc_dev, REG_CTRL);
  283. tscadc_writel(tscadc_dev, REG_CTRL,
  284. (restore | CNTRLREG_TSCSSENB));
  285. tscadc_writel(tscadc_dev, REG_CLKDIV, tscadc_dev->clk_div);
  286. return 0;
  287. }
  288. static const struct dev_pm_ops tscadc_pm_ops = {
  289. .suspend = tscadc_suspend,
  290. .resume = tscadc_resume,
  291. };
  292. #define TSCADC_PM_OPS (&tscadc_pm_ops)
  293. #else
  294. #define TSCADC_PM_OPS NULL
  295. #endif
  296. static const struct of_device_id ti_tscadc_dt_ids[] = {
  297. { .compatible = "ti,am3359-tscadc", },
  298. { }
  299. };
  300. MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
  301. static struct platform_driver ti_tscadc_driver = {
  302. .driver = {
  303. .name = "ti_am3359-tscadc",
  304. .owner = THIS_MODULE,
  305. .pm = TSCADC_PM_OPS,
  306. .of_match_table = ti_tscadc_dt_ids,
  307. },
  308. .probe = ti_tscadc_probe,
  309. .remove = ti_tscadc_remove,
  310. };
  311. module_platform_driver(ti_tscadc_driver);
  312. MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
  313. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  314. MODULE_LICENSE("GPL");