stm32-dac-core.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file is part of STM32 DAC driver
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/reset.h>
  16. #include "stm32-dac-core.h"
  17. /**
  18. * struct stm32_dac_priv - stm32 DAC core private data
  19. * @pclk: peripheral clock common for all DACs
  20. * @vref: regulator reference
  21. * @common: Common data for all DAC instances
  22. */
  23. struct stm32_dac_priv {
  24. struct clk *pclk;
  25. struct regulator *vref;
  26. struct stm32_dac_common common;
  27. };
  28. /**
  29. * struct stm32_dac_cfg - DAC configuration
  30. * @has_hfsel: DAC has high frequency control
  31. */
  32. struct stm32_dac_cfg {
  33. bool has_hfsel;
  34. };
  35. static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
  36. {
  37. return container_of(com, struct stm32_dac_priv, common);
  38. }
  39. static const struct regmap_config stm32_dac_regmap_cfg = {
  40. .reg_bits = 32,
  41. .val_bits = 32,
  42. .reg_stride = sizeof(u32),
  43. .max_register = 0x3fc,
  44. };
  45. static int stm32_dac_core_hw_start(struct device *dev)
  46. {
  47. struct stm32_dac_common *common = dev_get_drvdata(dev);
  48. struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
  49. int ret;
  50. ret = regulator_enable(priv->vref);
  51. if (ret < 0) {
  52. dev_err(dev, "vref enable failed: %d\n", ret);
  53. return ret;
  54. }
  55. ret = clk_prepare_enable(priv->pclk);
  56. if (ret < 0) {
  57. dev_err(dev, "pclk enable failed: %d\n", ret);
  58. goto err_regulator_disable;
  59. }
  60. return 0;
  61. err_regulator_disable:
  62. regulator_disable(priv->vref);
  63. return ret;
  64. }
  65. static void stm32_dac_core_hw_stop(struct device *dev)
  66. {
  67. struct stm32_dac_common *common = dev_get_drvdata(dev);
  68. struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
  69. clk_disable_unprepare(priv->pclk);
  70. regulator_disable(priv->vref);
  71. }
  72. static int stm32_dac_probe(struct platform_device *pdev)
  73. {
  74. struct device *dev = &pdev->dev;
  75. const struct stm32_dac_cfg *cfg;
  76. struct stm32_dac_priv *priv;
  77. struct regmap *regmap;
  78. struct resource *res;
  79. void __iomem *mmio;
  80. struct reset_control *rst;
  81. int ret;
  82. if (!dev->of_node)
  83. return -ENODEV;
  84. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  85. if (!priv)
  86. return -ENOMEM;
  87. platform_set_drvdata(pdev, &priv->common);
  88. cfg = (const struct stm32_dac_cfg *)
  89. of_match_device(dev->driver->of_match_table, dev)->data;
  90. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  91. mmio = devm_ioremap_resource(dev, res);
  92. if (IS_ERR(mmio))
  93. return PTR_ERR(mmio);
  94. regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
  95. &stm32_dac_regmap_cfg);
  96. if (IS_ERR(regmap))
  97. return PTR_ERR(regmap);
  98. priv->common.regmap = regmap;
  99. priv->pclk = devm_clk_get(dev, "pclk");
  100. if (IS_ERR(priv->pclk)) {
  101. ret = PTR_ERR(priv->pclk);
  102. dev_err(dev, "pclk get failed\n");
  103. return ret;
  104. }
  105. priv->vref = devm_regulator_get(dev, "vref");
  106. if (IS_ERR(priv->vref)) {
  107. ret = PTR_ERR(priv->vref);
  108. dev_err(dev, "vref get failed, %d\n", ret);
  109. return ret;
  110. }
  111. pm_runtime_get_noresume(dev);
  112. pm_runtime_set_active(dev);
  113. pm_runtime_enable(dev);
  114. ret = stm32_dac_core_hw_start(dev);
  115. if (ret)
  116. goto err_pm_stop;
  117. ret = regulator_get_voltage(priv->vref);
  118. if (ret < 0) {
  119. dev_err(dev, "vref get voltage failed, %d\n", ret);
  120. goto err_hw_stop;
  121. }
  122. priv->common.vref_mv = ret / 1000;
  123. dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
  124. rst = devm_reset_control_get_optional_exclusive(dev, NULL);
  125. if (rst) {
  126. if (IS_ERR(rst)) {
  127. ret = dev_err_probe(dev, PTR_ERR(rst), "reset get failed\n");
  128. goto err_hw_stop;
  129. }
  130. reset_control_assert(rst);
  131. udelay(2);
  132. reset_control_deassert(rst);
  133. }
  134. if (cfg && cfg->has_hfsel) {
  135. /* When clock speed is higher than 80MHz, set HFSEL */
  136. priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
  137. ret = regmap_update_bits(regmap, STM32_DAC_CR,
  138. STM32H7_DAC_CR_HFSEL,
  139. priv->common.hfsel ?
  140. STM32H7_DAC_CR_HFSEL : 0);
  141. if (ret)
  142. goto err_hw_stop;
  143. }
  144. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
  145. if (ret < 0) {
  146. dev_err(dev, "failed to populate DT children\n");
  147. goto err_hw_stop;
  148. }
  149. pm_runtime_put(dev);
  150. return 0;
  151. err_hw_stop:
  152. stm32_dac_core_hw_stop(dev);
  153. err_pm_stop:
  154. pm_runtime_disable(dev);
  155. pm_runtime_set_suspended(dev);
  156. pm_runtime_put_noidle(dev);
  157. return ret;
  158. }
  159. static int stm32_dac_remove(struct platform_device *pdev)
  160. {
  161. pm_runtime_get_sync(&pdev->dev);
  162. of_platform_depopulate(&pdev->dev);
  163. stm32_dac_core_hw_stop(&pdev->dev);
  164. pm_runtime_disable(&pdev->dev);
  165. pm_runtime_set_suspended(&pdev->dev);
  166. pm_runtime_put_noidle(&pdev->dev);
  167. return 0;
  168. }
  169. static int __maybe_unused stm32_dac_core_resume(struct device *dev)
  170. {
  171. struct stm32_dac_common *common = dev_get_drvdata(dev);
  172. struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
  173. int ret;
  174. if (priv->common.hfsel) {
  175. /* restore hfsel (maybe lost under low power state) */
  176. ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
  177. STM32H7_DAC_CR_HFSEL,
  178. STM32H7_DAC_CR_HFSEL);
  179. if (ret)
  180. return ret;
  181. }
  182. return pm_runtime_force_resume(dev);
  183. }
  184. static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
  185. {
  186. stm32_dac_core_hw_stop(dev);
  187. return 0;
  188. }
  189. static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
  190. {
  191. return stm32_dac_core_hw_start(dev);
  192. }
  193. static const struct dev_pm_ops stm32_dac_core_pm_ops = {
  194. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
  195. SET_RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
  196. stm32_dac_core_runtime_resume,
  197. NULL)
  198. };
  199. static const struct stm32_dac_cfg stm32h7_dac_cfg = {
  200. .has_hfsel = true,
  201. };
  202. static const struct of_device_id stm32_dac_of_match[] = {
  203. {
  204. .compatible = "st,stm32f4-dac-core",
  205. }, {
  206. .compatible = "st,stm32h7-dac-core",
  207. .data = (void *)&stm32h7_dac_cfg,
  208. },
  209. {},
  210. };
  211. MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
  212. static struct platform_driver stm32_dac_driver = {
  213. .probe = stm32_dac_probe,
  214. .remove = stm32_dac_remove,
  215. .driver = {
  216. .name = "stm32-dac-core",
  217. .of_match_table = stm32_dac_of_match,
  218. .pm = &stm32_dac_core_pm_ops,
  219. },
  220. };
  221. module_platform_driver(stm32_dac_driver);
  222. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  223. MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
  224. MODULE_LICENSE("GPL v2");
  225. MODULE_ALIAS("platform:stm32-dac-core");