xref: /openbmc/linux/drivers/iio/dac/stm32-dac-core.c (revision 228662b0)
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 	void __iomem *mmio;
94 	struct reset_control *rst;
95 	int ret;
96 
97 	if (!dev->of_node)
98 		return -ENODEV;
99 
100 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
101 	if (!priv)
102 		return -ENOMEM;
103 	platform_set_drvdata(pdev, &priv->common);
104 
105 	cfg = (const struct stm32_dac_cfg *)
106 		of_match_device(dev->driver->of_match_table, dev)->data;
107 
108 	mmio = devm_platform_ioremap_resource(pdev, 0);
109 	if (IS_ERR(mmio))
110 		return PTR_ERR(mmio);
111 
112 	regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
113 					   &stm32_dac_regmap_cfg);
114 	if (IS_ERR(regmap))
115 		return PTR_ERR(regmap);
116 	priv->common.regmap = regmap;
117 
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");
121 
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");
125 
126 	pm_runtime_get_noresume(dev);
127 	pm_runtime_set_active(dev);
128 	pm_runtime_enable(dev);
129 
130 	ret = stm32_dac_core_hw_start(dev);
131 	if (ret)
132 		goto err_pm_stop;
133 
134 	ret = regulator_get_voltage(priv->vref);
135 	if (ret < 0) {
136 		dev_err(dev, "vref get voltage failed, %d\n", ret);
137 		goto err_hw_stop;
138 	}
139 	priv->common.vref_mv = ret / 1000;
140 	dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
141 
142 	rst = devm_reset_control_get_optional_exclusive(dev, NULL);
143 	if (rst) {
144 		if (IS_ERR(rst)) {
145 			ret = dev_err_probe(dev, PTR_ERR(rst), "reset get failed\n");
146 			goto err_hw_stop;
147 		}
148 
149 		reset_control_assert(rst);
150 		udelay(2);
151 		reset_control_deassert(rst);
152 	}
153 
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,
159 					 priv->common.hfsel ?
160 					 STM32H7_DAC_CR_HFSEL : 0);
161 		if (ret)
162 			goto err_hw_stop;
163 	}
164 
165 
166 	ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
167 	if (ret < 0) {
168 		dev_err(dev, "failed to populate DT children\n");
169 		goto err_hw_stop;
170 	}
171 
172 	pm_runtime_put(dev);
173 
174 	return 0;
175 
176 err_hw_stop:
177 	stm32_dac_core_hw_stop(dev);
178 err_pm_stop:
179 	pm_runtime_disable(dev);
180 	pm_runtime_set_suspended(dev);
181 	pm_runtime_put_noidle(dev);
182 
183 	return ret;
184 }
185 
186 static int stm32_dac_remove(struct platform_device *pdev)
187 {
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);
194 
195 	return 0;
196 }
197 
198 static int stm32_dac_core_resume(struct device *dev)
199 {
200 	struct stm32_dac_common *common = dev_get_drvdata(dev);
201 	struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
202 	int ret;
203 
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);
209 		if (ret)
210 			return ret;
211 	}
212 
213 	return pm_runtime_force_resume(dev);
214 }
215 
216 static int stm32_dac_core_runtime_suspend(struct device *dev)
217 {
218 	stm32_dac_core_hw_stop(dev);
219 
220 	return 0;
221 }
222 
223 static int stm32_dac_core_runtime_resume(struct device *dev)
224 {
225 	return stm32_dac_core_hw_start(dev);
226 }
227 
228 static const struct dev_pm_ops stm32_dac_core_pm_ops = {
229 	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
230 	RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
231 		       stm32_dac_core_runtime_resume,
232 		       NULL)
233 };
234 
235 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
236 	.has_hfsel = true,
237 };
238 
239 static const struct of_device_id stm32_dac_of_match[] = {
240 	{
241 		.compatible = "st,stm32f4-dac-core",
242 	}, {
243 		.compatible = "st,stm32h7-dac-core",
244 		.data = (void *)&stm32h7_dac_cfg,
245 	},
246 	{},
247 };
248 MODULE_DEVICE_TABLE(of, stm32_dac_of_match);
249 
250 static struct platform_driver stm32_dac_driver = {
251 	.probe = stm32_dac_probe,
252 	.remove = stm32_dac_remove,
253 	.driver = {
254 		.name = "stm32-dac-core",
255 		.of_match_table = stm32_dac_of_match,
256 		.pm = pm_ptr(&stm32_dac_core_pm_ops),
257 	},
258 };
259 module_platform_driver(stm32_dac_driver);
260 
261 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
262 MODULE_DESCRIPTION("STMicroelectronics STM32 DAC core driver");
263 MODULE_LICENSE("GPL v2");
264 MODULE_ALIAS("platform:stm32-dac-core");
265