ti_am335x_tscadc.c 8.6 KB

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