ti_am335x_tscadc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 const struct regmap_config tscadc_regmap_config = {
  28. .name = "ti_tscadc",
  29. .reg_bits = 32,
  30. .reg_stride = 4,
  31. .val_bits = 32,
  32. };
  33. void am335x_tsc_se_set_cache(struct ti_tscadc_dev *tscadc, u32 val)
  34. {
  35. unsigned long flags;
  36. spin_lock_irqsave(&tscadc->reg_lock, flags);
  37. tscadc->reg_se_cache |= val;
  38. if (tscadc->adc_waiting)
  39. wake_up(&tscadc->reg_se_wait);
  40. else if (!tscadc->adc_in_use)
  41. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  42. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  43. }
  44. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_cache);
  45. static void am335x_tscadc_need_adc(struct ti_tscadc_dev *tscadc)
  46. {
  47. DEFINE_WAIT(wait);
  48. u32 reg;
  49. regmap_read(tscadc->regmap, REG_ADCFSM, &reg);
  50. if (reg & SEQ_STATUS) {
  51. tscadc->adc_waiting = true;
  52. prepare_to_wait(&tscadc->reg_se_wait, &wait,
  53. TASK_UNINTERRUPTIBLE);
  54. spin_unlock_irq(&tscadc->reg_lock);
  55. schedule();
  56. spin_lock_irq(&tscadc->reg_lock);
  57. finish_wait(&tscadc->reg_se_wait, &wait);
  58. /*
  59. * Sequencer should either be idle or
  60. * busy applying the charge step.
  61. */
  62. regmap_read(tscadc->regmap, REG_ADCFSM, &reg);
  63. WARN_ON((reg & SEQ_STATUS) && !(reg & CHARGE_STEP));
  64. tscadc->adc_waiting = false;
  65. }
  66. tscadc->adc_in_use = true;
  67. }
  68. void am335x_tsc_se_set_once(struct ti_tscadc_dev *tscadc, u32 val)
  69. {
  70. spin_lock_irq(&tscadc->reg_lock);
  71. am335x_tscadc_need_adc(tscadc);
  72. regmap_write(tscadc->regmap, REG_SE, val);
  73. spin_unlock_irq(&tscadc->reg_lock);
  74. }
  75. EXPORT_SYMBOL_GPL(am335x_tsc_se_set_once);
  76. void am335x_tsc_se_adc_done(struct ti_tscadc_dev *tscadc)
  77. {
  78. unsigned long flags;
  79. spin_lock_irqsave(&tscadc->reg_lock, flags);
  80. tscadc->adc_in_use = false;
  81. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  82. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  83. }
  84. EXPORT_SYMBOL_GPL(am335x_tsc_se_adc_done);
  85. void am335x_tsc_se_clr(struct ti_tscadc_dev *tscadc, u32 val)
  86. {
  87. unsigned long flags;
  88. spin_lock_irqsave(&tscadc->reg_lock, flags);
  89. tscadc->reg_se_cache &= ~val;
  90. regmap_write(tscadc->regmap, REG_SE, tscadc->reg_se_cache);
  91. spin_unlock_irqrestore(&tscadc->reg_lock, flags);
  92. }
  93. EXPORT_SYMBOL_GPL(am335x_tsc_se_clr);
  94. static void tscadc_idle_config(struct ti_tscadc_dev *tscadc)
  95. {
  96. unsigned int idleconfig;
  97. idleconfig = STEPCONFIG_YNN | STEPCONFIG_INM_ADCREFM |
  98. STEPCONFIG_INP_ADCREFM | STEPCONFIG_YPN;
  99. regmap_write(tscadc->regmap, REG_IDLECONFIG, idleconfig);
  100. }
  101. static int ti_tscadc_probe(struct platform_device *pdev)
  102. {
  103. struct ti_tscadc_dev *tscadc;
  104. struct resource *res;
  105. struct clk *clk;
  106. struct device_node *node = pdev->dev.of_node;
  107. struct mfd_cell *cell;
  108. struct property *prop;
  109. const __be32 *cur;
  110. u32 val;
  111. int err, ctrl;
  112. int clock_rate;
  113. int tsc_wires = 0, adc_channels = 0, total_channels;
  114. int readouts = 0;
  115. if (!pdev->dev.of_node) {
  116. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  117. return -EINVAL;
  118. }
  119. node = of_get_child_by_name(pdev->dev.of_node, "tsc");
  120. of_property_read_u32(node, "ti,wires", &tsc_wires);
  121. of_property_read_u32(node, "ti,coordiante-readouts", &readouts);
  122. node = of_get_child_by_name(pdev->dev.of_node, "adc");
  123. of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) {
  124. adc_channels++;
  125. if (val > 7) {
  126. dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n",
  127. val);
  128. return -EINVAL;
  129. }
  130. }
  131. total_channels = tsc_wires + adc_channels;
  132. if (total_channels > 8) {
  133. dev_err(&pdev->dev, "Number of i/p channels more than 8\n");
  134. return -EINVAL;
  135. }
  136. if (total_channels == 0) {
  137. dev_err(&pdev->dev, "Need atleast one channel.\n");
  138. return -EINVAL;
  139. }
  140. if (readouts * 2 + 2 + adc_channels > 16) {
  141. dev_err(&pdev->dev, "Too many step configurations requested\n");
  142. return -EINVAL;
  143. }
  144. /* Allocate memory for device */
  145. tscadc = devm_kzalloc(&pdev->dev, sizeof(*tscadc), GFP_KERNEL);
  146. if (!tscadc) {
  147. dev_err(&pdev->dev, "failed to allocate memory.\n");
  148. return -ENOMEM;
  149. }
  150. tscadc->dev = &pdev->dev;
  151. err = platform_get_irq(pdev, 0);
  152. if (err < 0) {
  153. dev_err(&pdev->dev, "no irq ID is specified.\n");
  154. goto ret;
  155. } else
  156. tscadc->irq = err;
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. tscadc->tscadc_base = devm_ioremap_resource(&pdev->dev, res);
  159. if (IS_ERR(tscadc->tscadc_base))
  160. return PTR_ERR(tscadc->tscadc_base);
  161. tscadc->regmap = devm_regmap_init_mmio(&pdev->dev,
  162. tscadc->tscadc_base, &tscadc_regmap_config);
  163. if (IS_ERR(tscadc->regmap)) {
  164. dev_err(&pdev->dev, "regmap init failed\n");
  165. err = PTR_ERR(tscadc->regmap);
  166. goto ret;
  167. }
  168. spin_lock_init(&tscadc->reg_lock);
  169. init_waitqueue_head(&tscadc->reg_se_wait);
  170. pm_runtime_enable(&pdev->dev);
  171. pm_runtime_get_sync(&pdev->dev);
  172. /*
  173. * The TSC_ADC_Subsystem has 2 clock domains
  174. * OCP_CLK and ADC_CLK.
  175. * The ADC clock is expected to run at target of 3MHz,
  176. * and expected to capture 12-bit data at a rate of 200 KSPS.
  177. * The TSC_ADC_SS controller design assumes the OCP clock is
  178. * at least 6x faster than the ADC clock.
  179. */
  180. clk = clk_get(&pdev->dev, "adc_tsc_fck");
  181. if (IS_ERR(clk)) {
  182. dev_err(&pdev->dev, "failed to get TSC fck\n");
  183. err = PTR_ERR(clk);
  184. goto err_disable_clk;
  185. }
  186. clock_rate = clk_get_rate(clk);
  187. clk_put(clk);
  188. tscadc->clk_div = clock_rate / ADC_CLK;
  189. /* TSCADC_CLKDIV needs to be configured to the value minus 1 */
  190. tscadc->clk_div--;
  191. regmap_write(tscadc->regmap, REG_CLKDIV, tscadc->clk_div);
  192. /* Set the control register bits */
  193. ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
  194. regmap_write(tscadc->regmap, REG_CTRL, ctrl);
  195. /* Set register bits for Idle Config Mode */
  196. if (tsc_wires > 0) {
  197. tscadc->tsc_wires = tsc_wires;
  198. if (tsc_wires == 5)
  199. ctrl |= CNTRLREG_5WIRE | CNTRLREG_TSCENB;
  200. else
  201. ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
  202. tscadc_idle_config(tscadc);
  203. }
  204. /* Enable the TSC module enable bit */
  205. ctrl |= CNTRLREG_TSCSSENB;
  206. regmap_write(tscadc->regmap, REG_CTRL, ctrl);
  207. tscadc->used_cells = 0;
  208. tscadc->tsc_cell = -1;
  209. tscadc->adc_cell = -1;
  210. /* TSC Cell */
  211. if (tsc_wires > 0) {
  212. tscadc->tsc_cell = tscadc->used_cells;
  213. cell = &tscadc->cells[tscadc->used_cells++];
  214. cell->name = "TI-am335x-tsc";
  215. cell->of_compatible = "ti,am3359-tsc";
  216. cell->platform_data = &tscadc;
  217. cell->pdata_size = sizeof(tscadc);
  218. }
  219. /* ADC Cell */
  220. if (adc_channels > 0) {
  221. tscadc->adc_cell = tscadc->used_cells;
  222. cell = &tscadc->cells[tscadc->used_cells++];
  223. cell->name = "TI-am335x-adc";
  224. cell->of_compatible = "ti,am3359-adc";
  225. cell->platform_data = &tscadc;
  226. cell->pdata_size = sizeof(tscadc);
  227. }
  228. err = mfd_add_devices(&pdev->dev, pdev->id, tscadc->cells,
  229. tscadc->used_cells, NULL, 0, NULL);
  230. if (err < 0)
  231. goto err_disable_clk;
  232. device_init_wakeup(&pdev->dev, true);
  233. platform_set_drvdata(pdev, tscadc);
  234. return 0;
  235. err_disable_clk:
  236. pm_runtime_put_sync(&pdev->dev);
  237. pm_runtime_disable(&pdev->dev);
  238. ret:
  239. return err;
  240. }
  241. static int ti_tscadc_remove(struct platform_device *pdev)
  242. {
  243. struct ti_tscadc_dev *tscadc = platform_get_drvdata(pdev);
  244. regmap_write(tscadc->regmap, REG_SE, 0x00);
  245. pm_runtime_put_sync(&pdev->dev);
  246. pm_runtime_disable(&pdev->dev);
  247. mfd_remove_devices(tscadc->dev);
  248. return 0;
  249. }
  250. static int __maybe_unused tscadc_suspend(struct device *dev)
  251. {
  252. struct ti_tscadc_dev *tscadc = dev_get_drvdata(dev);
  253. regmap_write(tscadc->regmap, REG_SE, 0x00);
  254. pm_runtime_put_sync(dev);
  255. return 0;
  256. }
  257. static int __maybe_unused tscadc_resume(struct device *dev)
  258. {
  259. struct ti_tscadc_dev *tscadc = dev_get_drvdata(dev);
  260. u32 ctrl;
  261. pm_runtime_get_sync(dev);
  262. /* context restore */
  263. ctrl = CNTRLREG_STEPCONFIGWRT | CNTRLREG_STEPID;
  264. regmap_write(tscadc->regmap, REG_CTRL, ctrl);
  265. if (tscadc->tsc_cell != -1) {
  266. if (tscadc->tsc_wires == 5)
  267. ctrl |= CNTRLREG_5WIRE | CNTRLREG_TSCENB;
  268. else
  269. ctrl |= CNTRLREG_4WIRE | CNTRLREG_TSCENB;
  270. tscadc_idle_config(tscadc);
  271. }
  272. ctrl |= CNTRLREG_TSCSSENB;
  273. regmap_write(tscadc->regmap, REG_CTRL, ctrl);
  274. regmap_write(tscadc->regmap, REG_CLKDIV, tscadc->clk_div);
  275. return 0;
  276. }
  277. static SIMPLE_DEV_PM_OPS(tscadc_pm_ops, tscadc_suspend, tscadc_resume);
  278. static const struct of_device_id ti_tscadc_dt_ids[] = {
  279. { .compatible = "ti,am3359-tscadc", },
  280. { }
  281. };
  282. MODULE_DEVICE_TABLE(of, ti_tscadc_dt_ids);
  283. static struct platform_driver ti_tscadc_driver = {
  284. .driver = {
  285. .name = "ti_am3359-tscadc",
  286. .pm = &tscadc_pm_ops,
  287. .of_match_table = ti_tscadc_dt_ids,
  288. },
  289. .probe = ti_tscadc_probe,
  290. .remove = ti_tscadc_remove,
  291. };
  292. module_platform_driver(ti_tscadc_driver);
  293. MODULE_DESCRIPTION("TI touchscreen / ADC MFD controller driver");
  294. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  295. MODULE_LICENSE("GPL");