ti_am335x_tscadc.c 8.8 KB

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