stm32-dac-core.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * This file is part of STM32 DAC driver
  3. *
  4. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  5. * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
  6. *
  7. * License type: GPLv2
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include "stm32-dac-core.h"
  28. /**
  29. * struct stm32_dac_priv - stm32 DAC core private data
  30. * @pclk: peripheral clock common for all DACs
  31. * @rst: peripheral reset control
  32. * @vref: regulator reference
  33. * @common: Common data for all DAC instances
  34. */
  35. struct stm32_dac_priv {
  36. struct clk *pclk;
  37. struct reset_control *rst;
  38. struct regulator *vref;
  39. struct stm32_dac_common common;
  40. };
  41. static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
  42. {
  43. return container_of(com, struct stm32_dac_priv, common);
  44. }
  45. static const struct regmap_config stm32_dac_regmap_cfg = {
  46. .reg_bits = 32,
  47. .val_bits = 32,
  48. .reg_stride = sizeof(u32),
  49. .max_register = 0x3fc,
  50. };
  51. static int stm32_dac_probe(struct platform_device *pdev)
  52. {
  53. struct device *dev = &pdev->dev;
  54. struct stm32_dac_priv *priv;
  55. struct regmap *regmap;
  56. struct resource *res;
  57. void __iomem *mmio;
  58. int ret;
  59. if (!dev->of_node)
  60. return -ENODEV;
  61. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  62. if (!priv)
  63. return -ENOMEM;
  64. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  65. mmio = devm_ioremap_resource(dev, res);
  66. if (IS_ERR(mmio))
  67. return PTR_ERR(mmio);
  68. regmap = devm_regmap_init_mmio(dev, mmio, &stm32_dac_regmap_cfg);
  69. if (IS_ERR(regmap))
  70. return PTR_ERR(regmap);
  71. priv->common.regmap = regmap;
  72. priv->vref = devm_regulator_get(dev, "vref");
  73. if (IS_ERR(priv->vref)) {
  74. ret = PTR_ERR(priv->vref);
  75. dev_err(dev, "vref get failed, %d\n", ret);
  76. return ret;
  77. }
  78. ret = regulator_enable(priv->vref);
  79. if (ret < 0) {
  80. dev_err(dev, "vref enable failed\n");
  81. return ret;
  82. }
  83. ret = regulator_get_voltage(priv->vref);
  84. if (ret < 0) {
  85. dev_err(dev, "vref get voltage failed, %d\n", ret);
  86. goto err_vref;
  87. }
  88. priv->common.vref_mv = ret / 1000;
  89. dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
  90. priv->pclk = devm_clk_get(dev, "pclk");
  91. if (IS_ERR(priv->pclk)) {
  92. ret = PTR_ERR(priv->pclk);
  93. dev_err(dev, "pclk get failed\n");
  94. goto err_vref;
  95. }
  96. ret = clk_prepare_enable(priv->pclk);
  97. if (ret < 0) {
  98. dev_err(dev, "pclk enable failed\n");
  99. goto err_vref;
  100. }
  101. priv->rst = devm_reset_control_get(dev, NULL);
  102. if (!IS_ERR(priv->rst)) {
  103. reset_control_assert(priv->rst);
  104. udelay(2);
  105. reset_control_deassert(priv->rst);
  106. }
  107. /* When clock speed is higher than 80MHz, set HFSEL */
  108. priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
  109. ret = regmap_update_bits(regmap, STM32_DAC_CR, STM32H7_DAC_CR_HFSEL,
  110. priv->common.hfsel ? STM32H7_DAC_CR_HFSEL : 0);
  111. if (ret)
  112. goto err_pclk;
  113. platform_set_drvdata(pdev, &priv->common);
  114. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
  115. if (ret < 0) {
  116. dev_err(dev, "failed to populate DT children\n");
  117. goto err_pclk;
  118. }
  119. return 0;
  120. err_pclk:
  121. clk_disable_unprepare(priv->pclk);
  122. err_vref:
  123. regulator_disable(priv->vref);
  124. return ret;
  125. }
  126. static int stm32_dac_remove(struct platform_device *pdev)
  127. {
  128. struct stm32_dac_common *common = platform_get_drvdata(pdev);
  129. struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
  130. of_platform_depopulate(&pdev->dev);
  131. clk_disable_unprepare(priv->pclk);
  132. regulator_disable(priv->vref);
  133. return 0;
  134. }
  135. static const struct of_device_id stm32_dac_of_match[] = {
  136. { .compatible = "st,stm32h7-dac-core", },
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
  140. static struct platform_driver stm32_dac_driver = {
  141. .probe = stm32_dac_probe,
  142. .remove = stm32_dac_remove,
  143. .driver = {
  144. .name = "stm32-dac-core",
  145. .of_match_table = stm32_dac_of_match,
  146. },
  147. };
  148. module_platform_driver(stm32_dac_driver);
  149. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  150. MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
  151. MODULE_LICENSE("GPL v2");
  152. MODULE_ALIAS("platform:stm32-dac-core");