xref: /openbmc/linux/drivers/iio/dac/stm32-dac-core.c (revision 715f23b6)
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 
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>
17 
18 #include "stm32-dac-core.h"
19 
20 /**
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
26  */
27 struct stm32_dac_priv {
28 	struct clk *pclk;
29 	struct reset_control *rst;
30 	struct regulator *vref;
31 	struct stm32_dac_common common;
32 };
33 
34 /**
35  * struct stm32_dac_cfg - DAC configuration
36  * @has_hfsel: DAC has high frequency control
37  */
38 struct stm32_dac_cfg {
39 	bool has_hfsel;
40 };
41 
42 static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
43 {
44 	return container_of(com, struct stm32_dac_priv, common);
45 }
46 
47 static const struct regmap_config stm32_dac_regmap_cfg = {
48 	.reg_bits = 32,
49 	.val_bits = 32,
50 	.reg_stride = sizeof(u32),
51 	.max_register = 0x3fc,
52 };
53 
54 static int stm32_dac_core_hw_start(struct device *dev)
55 {
56 	struct stm32_dac_common *common = dev_get_drvdata(dev);
57 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
58 	int ret;
59 
60 	ret = regulator_enable(priv->vref);
61 	if (ret < 0) {
62 		dev_err(dev, "vref enable failed: %d\n", ret);
63 		return ret;
64 	}
65 
66 	ret = clk_prepare_enable(priv->pclk);
67 	if (ret < 0) {
68 		dev_err(dev, "pclk enable failed: %d\n", ret);
69 		goto err_regulator_disable;
70 	}
71 
72 	return 0;
73 
74 err_regulator_disable:
75 	regulator_disable(priv->vref);
76 
77 	return ret;
78 }
79 
80 static void stm32_dac_core_hw_stop(struct device *dev)
81 {
82 	struct stm32_dac_common *common = dev_get_drvdata(dev);
83 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
84 
85 	clk_disable_unprepare(priv->pclk);
86 	regulator_disable(priv->vref);
87 }
88 
89 static int stm32_dac_probe(struct platform_device *pdev)
90 {
91 	struct device *dev = &pdev->dev;
92 	const struct stm32_dac_cfg *cfg;
93 	struct stm32_dac_priv *priv;
94 	struct regmap *regmap;
95 	struct resource *res;
96 	void __iomem *mmio;
97 	int ret;
98 
99 	if (!dev->of_node)
100 		return -ENODEV;
101 
102 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
103 	if (!priv)
104 		return -ENOMEM;
105 	platform_set_drvdata(pdev, &priv->common);
106 
107 	cfg = (const struct stm32_dac_cfg *)
108 		of_match_device(dev->driver->of_match_table, dev)->data;
109 
110 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111 	mmio = devm_ioremap_resource(dev, res);
112 	if (IS_ERR(mmio))
113 		return PTR_ERR(mmio);
114 
115 	regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
116 					   &stm32_dac_regmap_cfg);
117 	if (IS_ERR(regmap))
118 		return PTR_ERR(regmap);
119 	priv->common.regmap = regmap;
120 
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");
125 		return ret;
126 	}
127 
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);
132 		return ret;
133 	}
134 
135 	pm_runtime_get_noresume(dev);
136 	pm_runtime_set_active(dev);
137 	pm_runtime_enable(dev);
138 
139 	ret = stm32_dac_core_hw_start(dev);
140 	if (ret)
141 		goto err_pm_stop;
142 
143 	ret = regulator_get_voltage(priv->vref);
144 	if (ret < 0) {
145 		dev_err(dev, "vref get voltage failed, %d\n", ret);
146 		goto err_hw_stop;
147 	}
148 	priv->common.vref_mv = ret / 1000;
149 	dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
150 
151 	priv->rst = devm_reset_control_get_exclusive(dev, NULL);
152 	if (!IS_ERR(priv->rst)) {
153 		reset_control_assert(priv->rst);
154 		udelay(2);
155 		reset_control_deassert(priv->rst);
156 	}
157 
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,
163 					 priv->common.hfsel ?
164 					 STM32H7_DAC_CR_HFSEL : 0);
165 		if (ret)
166 			goto err_hw_stop;
167 	}
168 
169 
170 	ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
171 	if (ret < 0) {
172 		dev_err(dev, "failed to populate DT children\n");
173 		goto err_hw_stop;
174 	}
175 
176 	pm_runtime_put(dev);
177 
178 	return 0;
179 
180 err_hw_stop:
181 	stm32_dac_core_hw_stop(dev);
182 err_pm_stop:
183 	pm_runtime_disable(dev);
184 	pm_runtime_set_suspended(dev);
185 	pm_runtime_put_noidle(dev);
186 
187 	return ret;
188 }
189 
190 static int stm32_dac_remove(struct platform_device *pdev)
191 {
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);
198 
199 	return 0;
200 }
201 
202 static int __maybe_unused stm32_dac_core_resume(struct device *dev)
203 {
204 	struct stm32_dac_common *common = dev_get_drvdata(dev);
205 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
206 	int ret;
207 
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);
213 		if (ret)
214 			return ret;
215 	}
216 
217 	return pm_runtime_force_resume(dev);
218 }
219 
220 static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
221 {
222 	stm32_dac_core_hw_stop(dev);
223 
224 	return 0;
225 }
226 
227 static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
228 {
229 	return stm32_dac_core_hw_start(dev);
230 }
231 
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,
236 			   NULL)
237 };
238 
239 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
240 	.has_hfsel = true,
241 };
242 
243 static const struct of_device_id stm32_dac_of_match[] = {
244 	{
245 		.compatible = "st,stm32f4-dac-core",
246 	}, {
247 		.compatible = "st,stm32h7-dac-core",
248 		.data = (void *)&stm32h7_dac_cfg,
249 	},
250 	{},
251 };
252 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
253 
254 static struct platform_driver stm32_dac_driver = {
255 	.probe = stm32_dac_probe,
256 	.remove = stm32_dac_remove,
257 	.driver = {
258 		.name = "stm32-dac-core",
259 		.of_match_table = stm32_dac_of_match,
260 		.pm = &stm32_dac_core_pm_ops,
261 	},
262 };
263 module_platform_driver(stm32_dac_driver);
264 
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");
269