1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of STM32 DAC driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
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 * @vref: regulator reference
24 * @common: Common data for all DAC instances
26 struct stm32_dac_priv {
28 struct regulator *vref;
29 struct stm32_dac_common common;
33 * struct stm32_dac_cfg - DAC configuration
34 * @has_hfsel: DAC has high frequency control
36 struct stm32_dac_cfg {
40 static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
42 return container_of(com, struct stm32_dac_priv, common);
45 static const struct regmap_config stm32_dac_regmap_cfg = {
48 .reg_stride = sizeof(u32),
49 .max_register = 0x3fc,
52 static int stm32_dac_core_hw_start(struct device *dev)
54 struct stm32_dac_common *common = dev_get_drvdata(dev);
55 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
58 ret = regulator_enable(priv->vref);
60 dev_err(dev, "vref enable failed: %d\n", ret);
64 ret = clk_prepare_enable(priv->pclk);
66 dev_err(dev, "pclk enable failed: %d\n", ret);
67 goto err_regulator_disable;
72 err_regulator_disable:
73 regulator_disable(priv->vref);
78 static void stm32_dac_core_hw_stop(struct device *dev)
80 struct stm32_dac_common *common = dev_get_drvdata(dev);
81 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
83 clk_disable_unprepare(priv->pclk);
84 regulator_disable(priv->vref);
87 static int stm32_dac_probe(struct platform_device *pdev)
89 struct device *dev = &pdev->dev;
90 const struct stm32_dac_cfg *cfg;
91 struct stm32_dac_priv *priv;
92 struct regmap *regmap;
94 struct reset_control *rst;
100 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
103 platform_set_drvdata(pdev, &priv->common);
105 cfg = (const struct stm32_dac_cfg *)
106 of_match_device(dev->driver->of_match_table, dev)->data;
108 mmio = devm_platform_ioremap_resource(pdev, 0);
110 return PTR_ERR(mmio);
112 regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
113 &stm32_dac_regmap_cfg);
115 return PTR_ERR(regmap);
116 priv->common.regmap = regmap;
118 priv->pclk = devm_clk_get(dev, "pclk");
119 if (IS_ERR(priv->pclk))
120 return dev_err_probe(dev, PTR_ERR(priv->pclk), "pclk get failed\n");
122 priv->vref = devm_regulator_get(dev, "vref");
123 if (IS_ERR(priv->vref))
124 return dev_err_probe(dev, PTR_ERR(priv->vref), "vref get failed\n");
126 pm_runtime_get_noresume(dev);
127 pm_runtime_set_active(dev);
128 pm_runtime_enable(dev);
130 ret = stm32_dac_core_hw_start(dev);
134 ret = regulator_get_voltage(priv->vref);
136 dev_err(dev, "vref get voltage failed, %d\n", ret);
139 priv->common.vref_mv = ret / 1000;
140 dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
142 rst = devm_reset_control_get_optional_exclusive(dev, NULL);
145 ret = dev_err_probe(dev, PTR_ERR(rst), "reset get failed\n");
149 reset_control_assert(rst);
151 reset_control_deassert(rst);
154 if (cfg && cfg->has_hfsel) {
155 /* When clock speed is higher than 80MHz, set HFSEL */
156 priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
157 ret = regmap_update_bits(regmap, STM32_DAC_CR,
158 STM32H7_DAC_CR_HFSEL,
160 STM32H7_DAC_CR_HFSEL : 0);
166 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
168 dev_err(dev, "failed to populate DT children\n");
177 stm32_dac_core_hw_stop(dev);
179 pm_runtime_disable(dev);
180 pm_runtime_set_suspended(dev);
181 pm_runtime_put_noidle(dev);
186 static int stm32_dac_remove(struct platform_device *pdev)
188 pm_runtime_get_sync(&pdev->dev);
189 of_platform_depopulate(&pdev->dev);
190 stm32_dac_core_hw_stop(&pdev->dev);
191 pm_runtime_disable(&pdev->dev);
192 pm_runtime_set_suspended(&pdev->dev);
193 pm_runtime_put_noidle(&pdev->dev);
198 static int __maybe_unused stm32_dac_core_resume(struct device *dev)
200 struct stm32_dac_common *common = dev_get_drvdata(dev);
201 struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
204 if (priv->common.hfsel) {
205 /* restore hfsel (maybe lost under low power state) */
206 ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
207 STM32H7_DAC_CR_HFSEL,
208 STM32H7_DAC_CR_HFSEL);
213 return pm_runtime_force_resume(dev);
216 static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
218 stm32_dac_core_hw_stop(dev);
223 static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
225 return stm32_dac_core_hw_start(dev);
228 static const struct dev_pm_ops stm32_dac_core_pm_ops = {
229 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
230 SET_RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
231 stm32_dac_core_runtime_resume,
235 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
239 static const struct of_device_id stm32_dac_of_match[] = {
241 .compatible = "st,stm32f4-dac-core",
243 .compatible = "st,stm32h7-dac-core",
244 .data = (void *)&stm32h7_dac_cfg,
248 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
250 static struct platform_driver stm32_dac_driver = {
251 .probe = stm32_dac_probe,
252 .remove = stm32_dac_remove,
254 .name = "stm32-dac-core",
255 .of_match_table = stm32_dac_of_match,
256 .pm = &stm32_dac_core_pm_ops,
259 module_platform_driver(stm32_dac_driver);
262 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
263 MODULE_LICENSE("GPL v2");
264 MODULE_ALIAS("platform:stm32-dac-core");