xref: /openbmc/linux/drivers/iio/dac/stm32-dac-core.c (revision 8365a898)
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  * @vref:		regulator reference
24  * @common:		Common data for all DAC instances
25  */
26 struct stm32_dac_priv {
27 	struct clk *pclk;
28 	struct regulator *vref;
29 	struct stm32_dac_common common;
30 };
31 
32 /**
33  * struct stm32_dac_cfg - DAC configuration
34  * @has_hfsel: DAC has high frequency control
35  */
36 struct stm32_dac_cfg {
37 	bool has_hfsel;
38 };
39 
40 static struct stm32_dac_priv *to_stm32_dac_priv(struct stm32_dac_common *com)
41 {
42 	return container_of(com, struct stm32_dac_priv, common);
43 }
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 
52 static int stm32_dac_core_hw_start(struct device *dev)
53 {
54 	struct stm32_dac_common *common = dev_get_drvdata(dev);
55 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
56 	int ret;
57 
58 	ret = regulator_enable(priv->vref);
59 	if (ret < 0) {
60 		dev_err(dev, "vref enable failed: %d\n", ret);
61 		return ret;
62 	}
63 
64 	ret = clk_prepare_enable(priv->pclk);
65 	if (ret < 0) {
66 		dev_err(dev, "pclk enable failed: %d\n", ret);
67 		goto err_regulator_disable;
68 	}
69 
70 	return 0;
71 
72 err_regulator_disable:
73 	regulator_disable(priv->vref);
74 
75 	return ret;
76 }
77 
78 static void stm32_dac_core_hw_stop(struct device *dev)
79 {
80 	struct stm32_dac_common *common = dev_get_drvdata(dev);
81 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
82 
83 	clk_disable_unprepare(priv->pclk);
84 	regulator_disable(priv->vref);
85 }
86 
87 static int stm32_dac_probe(struct platform_device *pdev)
88 {
89 	struct device *dev = &pdev->dev;
90 	const struct stm32_dac_cfg *cfg;
91 	struct stm32_dac_priv *priv;
92 	struct regmap *regmap;
93 	struct resource *res;
94 	void __iomem *mmio;
95 	struct reset_control *rst;
96 	int ret;
97 
98 	if (!dev->of_node)
99 		return -ENODEV;
100 
101 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
102 	if (!priv)
103 		return -ENOMEM;
104 	platform_set_drvdata(pdev, &priv->common);
105 
106 	cfg = (const struct stm32_dac_cfg *)
107 		of_match_device(dev->driver->of_match_table, dev)->data;
108 
109 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
110 	mmio = devm_ioremap_resource(dev, res);
111 	if (IS_ERR(mmio))
112 		return PTR_ERR(mmio);
113 
114 	regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
115 					   &stm32_dac_regmap_cfg);
116 	if (IS_ERR(regmap))
117 		return PTR_ERR(regmap);
118 	priv->common.regmap = regmap;
119 
120 	priv->pclk = devm_clk_get(dev, "pclk");
121 	if (IS_ERR(priv->pclk)) {
122 		ret = PTR_ERR(priv->pclk);
123 		dev_err(dev, "pclk get failed\n");
124 		return ret;
125 	}
126 
127 	priv->vref = devm_regulator_get(dev, "vref");
128 	if (IS_ERR(priv->vref)) {
129 		ret = PTR_ERR(priv->vref);
130 		dev_err(dev, "vref get failed, %d\n", ret);
131 		return ret;
132 	}
133 
134 	pm_runtime_get_noresume(dev);
135 	pm_runtime_set_active(dev);
136 	pm_runtime_enable(dev);
137 
138 	ret = stm32_dac_core_hw_start(dev);
139 	if (ret)
140 		goto err_pm_stop;
141 
142 	ret = regulator_get_voltage(priv->vref);
143 	if (ret < 0) {
144 		dev_err(dev, "vref get voltage failed, %d\n", ret);
145 		goto err_hw_stop;
146 	}
147 	priv->common.vref_mv = ret / 1000;
148 	dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
149 
150 	rst = devm_reset_control_get_optional_exclusive(dev, NULL);
151 	if (rst) {
152 		if (IS_ERR(rst)) {
153 			ret = PTR_ERR(rst);
154 			if (ret != -EPROBE_DEFER)
155 				dev_err(dev, "reset get failed, %d\n", ret);
156 
157 			goto err_hw_stop;
158 		}
159 
160 		reset_control_assert(rst);
161 		udelay(2);
162 		reset_control_deassert(rst);
163 	}
164 
165 	if (cfg && cfg->has_hfsel) {
166 		/* When clock speed is higher than 80MHz, set HFSEL */
167 		priv->common.hfsel = (clk_get_rate(priv->pclk) > 80000000UL);
168 		ret = regmap_update_bits(regmap, STM32_DAC_CR,
169 					 STM32H7_DAC_CR_HFSEL,
170 					 priv->common.hfsel ?
171 					 STM32H7_DAC_CR_HFSEL : 0);
172 		if (ret)
173 			goto err_hw_stop;
174 	}
175 
176 
177 	ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
178 	if (ret < 0) {
179 		dev_err(dev, "failed to populate DT children\n");
180 		goto err_hw_stop;
181 	}
182 
183 	pm_runtime_put(dev);
184 
185 	return 0;
186 
187 err_hw_stop:
188 	stm32_dac_core_hw_stop(dev);
189 err_pm_stop:
190 	pm_runtime_disable(dev);
191 	pm_runtime_set_suspended(dev);
192 	pm_runtime_put_noidle(dev);
193 
194 	return ret;
195 }
196 
197 static int stm32_dac_remove(struct platform_device *pdev)
198 {
199 	pm_runtime_get_sync(&pdev->dev);
200 	of_platform_depopulate(&pdev->dev);
201 	stm32_dac_core_hw_stop(&pdev->dev);
202 	pm_runtime_disable(&pdev->dev);
203 	pm_runtime_set_suspended(&pdev->dev);
204 	pm_runtime_put_noidle(&pdev->dev);
205 
206 	return 0;
207 }
208 
209 static int __maybe_unused stm32_dac_core_resume(struct device *dev)
210 {
211 	struct stm32_dac_common *common = dev_get_drvdata(dev);
212 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
213 	int ret;
214 
215 	if (priv->common.hfsel) {
216 		/* restore hfsel (maybe lost under low power state) */
217 		ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
218 					 STM32H7_DAC_CR_HFSEL,
219 					 STM32H7_DAC_CR_HFSEL);
220 		if (ret)
221 			return ret;
222 	}
223 
224 	return pm_runtime_force_resume(dev);
225 }
226 
227 static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
228 {
229 	stm32_dac_core_hw_stop(dev);
230 
231 	return 0;
232 }
233 
234 static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
235 {
236 	return stm32_dac_core_hw_start(dev);
237 }
238 
239 static const struct dev_pm_ops stm32_dac_core_pm_ops = {
240 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
241 	SET_RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
242 			   stm32_dac_core_runtime_resume,
243 			   NULL)
244 };
245 
246 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
247 	.has_hfsel = true,
248 };
249 
250 static const struct of_device_id stm32_dac_of_match[] = {
251 	{
252 		.compatible = "st,stm32f4-dac-core",
253 	}, {
254 		.compatible = "st,stm32h7-dac-core",
255 		.data = (void *)&stm32h7_dac_cfg,
256 	},
257 	{},
258 };
259 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
260 
261 static struct platform_driver stm32_dac_driver = {
262 	.probe = stm32_dac_probe,
263 	.remove = stm32_dac_remove,
264 	.driver = {
265 		.name = "stm32-dac-core",
266 		.of_match_table = stm32_dac_of_match,
267 		.pm = &stm32_dac_core_pm_ops,
268 	},
269 };
270 module_platform_driver(stm32_dac_driver);
271 
272 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
273 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
274 MODULE_LICENSE("GPL v2");
275 MODULE_ALIAS("platform:stm32-dac-core");
276