1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 DAC driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/reset.h>
18 #include "stm32-dac-core.h"
21 * struct stm32_dac_priv - stm32 DAC core private data
22 * @pclk: peripheral clock common for all DACs
23 * @rst: peripheral reset control
24 * @vref: regulator reference
25 * @common: Common data for all DAC instances
27 struct stm32_dac_priv {
29 struct reset_control *rst;
30 struct regulator *vref;
31 struct stm32_dac_common common;
35 * struct stm32_dac_cfg - DAC configuration
36 * @has_hfsel: DAC has high frequency control
38 struct stm32_dac_cfg {
42 static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
44 return container_of(com, struct stm32_dac_priv, common);
47 static const struct regmap_config stm32_dac_regmap_cfg = {
50 .reg_stride = sizeof(u32),
51 .max_register = 0x3fc,
54 static int stm32_dac_core_hw_start(struct device *dev)
56 struct stm32_dac_common *common = dev_get_drvdata(dev);
57 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
60 ret = regulator_enable(priv->vref);
62 dev_err(dev, "vref enable failed: %d\n", ret);
66 ret = clk_prepare_enable(priv->pclk);
68 dev_err(dev, "pclk enable failed: %d\n", ret);
69 goto err_regulator_disable;
74 err_regulator_disable:
75 regulator_disable(priv->vref);
80 static void stm32_dac_core_hw_stop(struct device *dev)
82 struct stm32_dac_common *common = dev_get_drvdata(dev);
83 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
85 clk_disable_unprepare(priv->pclk);
86 regulator_disable(priv->vref);
89 static int stm32_dac_probe(struct platform_device *pdev)
91 struct device *dev = &pdev->dev;
92 const struct stm32_dac_cfg *cfg;
93 struct stm32_dac_priv *priv;
94 struct regmap *regmap;
102 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
105 platform_set_drvdata(pdev, &priv->common);
107 cfg = (const struct stm32_dac_cfg *)
108 of_match_device(dev->driver->of_match_table, dev)->data;
110 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111 mmio = devm_ioremap_resource(dev, res);
113 return PTR_ERR(mmio);
115 regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
116 &stm32_dac_regmap_cfg);
118 return PTR_ERR(regmap);
119 priv->common.regmap = regmap;
121 priv->pclk = devm_clk_get(dev, "pclk");
122 if (IS_ERR(priv->pclk)) {
123 ret = PTR_ERR(priv->pclk);
124 dev_err(dev, "pclk get failed\n");
128 priv->vref = devm_regulator_get(dev, "vref");
129 if (IS_ERR(priv->vref)) {
130 ret = PTR_ERR(priv->vref);
131 dev_err(dev, "vref get failed, %d\n", ret);
135 pm_runtime_get_noresume(dev);
136 pm_runtime_set_active(dev);
137 pm_runtime_enable(dev);
139 ret = stm32_dac_core_hw_start(dev);
143 ret = regulator_get_voltage(priv->vref);
145 dev_err(dev, "vref get voltage failed, %d\n", ret);
148 priv->common.vref_mv = ret / 1000;
149 dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
151 priv->rst = devm_reset_control_get_exclusive(dev, NULL);
152 if (!IS_ERR(priv->rst)) {
153 reset_control_assert(priv->rst);
155 reset_control_deassert(priv->rst);
158 if (cfg && cfg->has_hfsel) {
159 /* When clock speed is higher than 80MHz, set HFSEL */
160 priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
161 ret = regmap_update_bits(regmap, STM32_DAC_CR,
162 STM32H7_DAC_CR_HFSEL,
164 STM32H7_DAC_CR_HFSEL : 0);
170 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
172 dev_err(dev, "failed to populate DT children\n");
181 stm32_dac_core_hw_stop(dev);
183 pm_runtime_disable(dev);
184 pm_runtime_set_suspended(dev);
185 pm_runtime_put_noidle(dev);
190 static int stm32_dac_remove(struct platform_device *pdev)
192 pm_runtime_get_sync(&pdev->dev);
193 of_platform_depopulate(&pdev->dev);
194 stm32_dac_core_hw_stop(&pdev->dev);
195 pm_runtime_disable(&pdev->dev);
196 pm_runtime_set_suspended(&pdev->dev);
197 pm_runtime_put_noidle(&pdev->dev);
202 static int __maybe_unused stm32_dac_core_resume(struct device *dev)
204 struct stm32_dac_common *common = dev_get_drvdata(dev);
205 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
208 if (priv->common.hfsel) {
209 /* restore hfsel (maybe lost under low power state) */
210 ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
211 STM32H7_DAC_CR_HFSEL,
212 STM32H7_DAC_CR_HFSEL);
217 return pm_runtime_force_resume(dev);
220 static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
222 stm32_dac_core_hw_stop(dev);
227 static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
229 return stm32_dac_core_hw_start(dev);
232 static const struct dev_pm_ops stm32_dac_core_pm_ops = {
233 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
234 SET_RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
235 stm32_dac_core_runtime_resume,
239 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
243 static const struct of_device_id stm32_dac_of_match[] = {
245 .compatible = "st,stm32f4-dac-core",
247 .compatible = "st,stm32h7-dac-core",
248 .data = (void *)&stm32h7_dac_cfg,
252 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
254 static struct platform_driver stm32_dac_driver = {
255 .probe = stm32_dac_probe,
256 .remove = stm32_dac_remove,
258 .name = "stm32-dac-core",
259 .of_match_table = stm32_dac_of_match,
260 .pm = &stm32_dac_core_pm_ops,
263 module_platform_driver(stm32_dac_driver);
265 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
266 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
267 MODULE_LICENSE("GPL v2");
268 MODULE_ALIAS("platform:stm32-dac-core");