stm32-dac-core.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/regulator/consumer.h>
  14. #include <linux/reset.h>
  15. #include "stm32-dac-core.h"
  16. /**
  17. * struct stm32_dac_priv - stm32 DAC core private data
  18. * @pclk: peripheral clock common for all DACs
  19. * @rst: peripheral reset control
  20. * @vref: regulator reference
  21. * @common: Common data for all DAC instances
  22. */
  23. struct stm32_dac_priv {
  24. struct clk *pclk;
  25. struct reset_control *rst;
  26. struct regulator *vref;
  27. struct stm32_dac_common common;
  28. };
  29. /**
  30. * struct stm32_dac_cfg - DAC configuration
  31. * @has_hfsel: DAC has high frequency control
  32. */
  33. struct stm32_dac_cfg {
  34. bool has_hfsel;
  35. };
  36. static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
  37. {
  38. return container_of(com, struct stm32_dac_priv, common);
  39. }
  40. static const struct regmap_config stm32_dac_regmap_cfg = {
  41. .reg_bits = 32,
  42. .val_bits = 32,
  43. .reg_stride = sizeof(u32),
  44. .max_register = 0x3fc,
  45. };
  46. static int stm32_dac_probe(struct platform_device *pdev)
  47. {
  48. struct device *dev = &pdev->dev;
  49. const struct stm32_dac_cfg *cfg;
  50. struct stm32_dac_priv *priv;
  51. struct regmap *regmap;
  52. struct resource *res;
  53. void __iomem *mmio;
  54. int ret;
  55. if (!dev->of_node)
  56. return -ENODEV;
  57. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  58. if (!priv)
  59. return -ENOMEM;
  60. cfg = (const struct stm32_dac_cfg *)
  61. of_match_device(dev->driver->of_match_table, dev)->data;
  62. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  63. mmio = devm_ioremap_resource(dev, res);
  64. if (IS_ERR(mmio))
  65. return PTR_ERR(mmio);
  66. regmap = devm_regmap_init_mmio(dev, mmio, &stm32_dac_regmap_cfg);
  67. if (IS_ERR(regmap))
  68. return PTR_ERR(regmap);
  69. priv->common.regmap = regmap;
  70. priv->vref = devm_regulator_get(dev, "vref");
  71. if (IS_ERR(priv->vref)) {
  72. ret = PTR_ERR(priv->vref);
  73. dev_err(dev, "vref get failed, %d\n", ret);
  74. return ret;
  75. }
  76. ret = regulator_enable(priv->vref);
  77. if (ret < 0) {
  78. dev_err(dev, "vref enable failed\n");
  79. return ret;
  80. }
  81. ret = regulator_get_voltage(priv->vref);
  82. if (ret < 0) {
  83. dev_err(dev, "vref get voltage failed, %d\n", ret);
  84. goto err_vref;
  85. }
  86. priv->common.vref_mv = ret / 1000;
  87. dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
  88. priv->pclk = devm_clk_get(dev, "pclk");
  89. if (IS_ERR(priv->pclk)) {
  90. ret = PTR_ERR(priv->pclk);
  91. dev_err(dev, "pclk get failed\n");
  92. goto err_vref;
  93. }
  94. ret = clk_prepare_enable(priv->pclk);
  95. if (ret < 0) {
  96. dev_err(dev, "pclk enable failed\n");
  97. goto err_vref;
  98. }
  99. priv->rst = devm_reset_control_get_exclusive(dev, NULL);
  100. if (!IS_ERR(priv->rst)) {
  101. reset_control_assert(priv->rst);
  102. udelay(2);
  103. reset_control_deassert(priv->rst);
  104. }
  105. if (cfg && cfg->has_hfsel) {
  106. /* When clock speed is higher than 80MHz, set HFSEL */
  107. priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
  108. ret = regmap_update_bits(regmap, STM32_DAC_CR,
  109. STM32H7_DAC_CR_HFSEL,
  110. priv->common.hfsel ?
  111. STM32H7_DAC_CR_HFSEL : 0);
  112. if (ret)
  113. goto err_pclk;
  114. }
  115. platform_set_drvdata(pdev, &priv->common);
  116. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
  117. if (ret < 0) {
  118. dev_err(dev, "failed to populate DT children\n");
  119. goto err_pclk;
  120. }
  121. return 0;
  122. err_pclk:
  123. clk_disable_unprepare(priv->pclk);
  124. err_vref:
  125. regulator_disable(priv->vref);
  126. return ret;
  127. }
  128. static int stm32_dac_remove(struct platform_device *pdev)
  129. {
  130. struct stm32_dac_common *common = platform_get_drvdata(pdev);
  131. struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
  132. of_platform_depopulate(&pdev->dev);
  133. clk_disable_unprepare(priv->pclk);
  134. regulator_disable(priv->vref);
  135. return 0;
  136. }
  137. static const struct stm32_dac_cfg stm32h7_dac_cfg = {
  138. .has_hfsel = true,
  139. };
  140. static const struct of_device_id stm32_dac_of_match[] = {
  141. {
  142. .compatible = "st,stm32f4-dac-core",
  143. }, {
  144. .compatible = "st,stm32h7-dac-core",
  145. .data = (void *)&stm32h7_dac_cfg,
  146. },
  147. {},
  148. };
  149. MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
  150. static struct platform_driver stm32_dac_driver = {
  151. .probe = stm32_dac_probe,
  152. .remove = stm32_dac_remove,
  153. .driver = {
  154. .name = "stm32-dac-core",
  155. .of_match_table = stm32_dac_of_match,
  156. },
  157. };
  158. module_platform_driver(stm32_dac_driver);
  159. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  160. MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
  161. MODULE_LICENSE("GPL v2");
  162. MODULE_ALIAS("platform:stm32-dac-core");