1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) STMicroelectronics 2017
5 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
8 #include <linux/bitfield.h>
11 #include <linux/iopoll.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/pm_runtime.h>
19 /* STM32 VREFBUF registers */
20 #define STM32_VREFBUF_CSR 0x00
22 /* STM32 VREFBUF CSR bitfields */
23 #define STM32_VRS GENMASK(6, 4)
24 #define STM32_VRR BIT(3)
25 #define STM32_HIZ BIT(1)
26 #define STM32_ENVR BIT(0)
28 #define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS 10
30 struct stm32_vrefbuf
{
36 static const unsigned int stm32_vrefbuf_voltages
[] = {
37 /* Matches resp. VRS = 000b, 001b, 010b, 011b */
38 2500000, 2048000, 1800000, 1500000,
41 static int stm32_vrefbuf_enable(struct regulator_dev
*rdev
)
43 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
47 ret
= pm_runtime_get_sync(priv
->dev
);
49 pm_runtime_put_noidle(priv
->dev
);
53 val
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
);
54 val
= (val
& ~STM32_HIZ
) | STM32_ENVR
;
55 writel_relaxed(val
, priv
->base
+ STM32_VREFBUF_CSR
);
58 * Vrefbuf startup time depends on external capacitor: wait here for
59 * VRR to be set. That means output has reached expected value.
60 * ~650us sleep should be enough for caps up to 1.5uF. Use 10ms as
63 ret
= readl_poll_timeout(priv
->base
+ STM32_VREFBUF_CSR
, val
,
64 val
& STM32_VRR
, 650, 10000);
66 dev_err(&rdev
->dev
, "stm32 vrefbuf timed out!\n");
67 val
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
);
68 val
= (val
& ~STM32_ENVR
) | STM32_HIZ
;
69 writel_relaxed(val
, priv
->base
+ STM32_VREFBUF_CSR
);
72 pm_runtime_mark_last_busy(priv
->dev
);
73 pm_runtime_put_autosuspend(priv
->dev
);
78 static int stm32_vrefbuf_disable(struct regulator_dev
*rdev
)
80 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
84 ret
= pm_runtime_get_sync(priv
->dev
);
86 pm_runtime_put_noidle(priv
->dev
);
90 val
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
);
91 val
= (val
& ~STM32_ENVR
) | STM32_HIZ
;
92 writel_relaxed(val
, priv
->base
+ STM32_VREFBUF_CSR
);
94 pm_runtime_mark_last_busy(priv
->dev
);
95 pm_runtime_put_autosuspend(priv
->dev
);
100 static int stm32_vrefbuf_is_enabled(struct regulator_dev
*rdev
)
102 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
105 ret
= pm_runtime_get_sync(priv
->dev
);
107 pm_runtime_put_noidle(priv
->dev
);
111 ret
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
) & STM32_ENVR
;
113 pm_runtime_mark_last_busy(priv
->dev
);
114 pm_runtime_put_autosuspend(priv
->dev
);
119 static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev
*rdev
,
122 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
126 ret
= pm_runtime_get_sync(priv
->dev
);
128 pm_runtime_put_noidle(priv
->dev
);
132 val
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
);
133 val
= (val
& ~STM32_VRS
) | FIELD_PREP(STM32_VRS
, sel
);
134 writel_relaxed(val
, priv
->base
+ STM32_VREFBUF_CSR
);
136 pm_runtime_mark_last_busy(priv
->dev
);
137 pm_runtime_put_autosuspend(priv
->dev
);
142 static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev
*rdev
)
144 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
148 ret
= pm_runtime_get_sync(priv
->dev
);
150 pm_runtime_put_noidle(priv
->dev
);
154 val
= readl_relaxed(priv
->base
+ STM32_VREFBUF_CSR
);
155 ret
= FIELD_GET(STM32_VRS
, val
);
157 pm_runtime_mark_last_busy(priv
->dev
);
158 pm_runtime_put_autosuspend(priv
->dev
);
163 static const struct regulator_ops stm32_vrefbuf_volt_ops
= {
164 .enable
= stm32_vrefbuf_enable
,
165 .disable
= stm32_vrefbuf_disable
,
166 .is_enabled
= stm32_vrefbuf_is_enabled
,
167 .get_voltage_sel
= stm32_vrefbuf_get_voltage_sel
,
168 .set_voltage_sel
= stm32_vrefbuf_set_voltage_sel
,
169 .list_voltage
= regulator_list_voltage_table
,
172 static const struct regulator_desc stm32_vrefbuf_regu
= {
174 .supply_name
= "vdda",
175 .volt_table
= stm32_vrefbuf_voltages
,
176 .n_voltages
= ARRAY_SIZE(stm32_vrefbuf_voltages
),
177 .ops
= &stm32_vrefbuf_volt_ops
,
178 .type
= REGULATOR_VOLTAGE
,
179 .owner
= THIS_MODULE
,
182 static int stm32_vrefbuf_probe(struct platform_device
*pdev
)
184 struct stm32_vrefbuf
*priv
;
185 struct regulator_config config
= { };
186 struct regulator_dev
*rdev
;
189 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
192 priv
->dev
= &pdev
->dev
;
194 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
195 if (IS_ERR(priv
->base
))
196 return PTR_ERR(priv
->base
);
198 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
199 if (IS_ERR(priv
->clk
))
200 return PTR_ERR(priv
->clk
);
202 pm_runtime_get_noresume(&pdev
->dev
);
203 pm_runtime_set_active(&pdev
->dev
);
204 pm_runtime_set_autosuspend_delay(&pdev
->dev
,
205 STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS
);
206 pm_runtime_use_autosuspend(&pdev
->dev
);
207 pm_runtime_enable(&pdev
->dev
);
209 ret
= clk_prepare_enable(priv
->clk
);
211 dev_err(&pdev
->dev
, "clk prepare failed with error %d\n", ret
);
215 config
.dev
= &pdev
->dev
;
216 config
.driver_data
= priv
;
217 config
.of_node
= pdev
->dev
.of_node
;
218 config
.init_data
= of_get_regulator_init_data(&pdev
->dev
,
220 &stm32_vrefbuf_regu
);
222 rdev
= regulator_register(&stm32_vrefbuf_regu
, &config
);
225 dev_err(&pdev
->dev
, "register failed with error %d\n", ret
);
228 platform_set_drvdata(pdev
, rdev
);
230 pm_runtime_mark_last_busy(&pdev
->dev
);
231 pm_runtime_put_autosuspend(&pdev
->dev
);
236 clk_disable_unprepare(priv
->clk
);
238 pm_runtime_disable(&pdev
->dev
);
239 pm_runtime_set_suspended(&pdev
->dev
);
240 pm_runtime_put_noidle(&pdev
->dev
);
245 static int stm32_vrefbuf_remove(struct platform_device
*pdev
)
247 struct regulator_dev
*rdev
= platform_get_drvdata(pdev
);
248 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
250 pm_runtime_get_sync(&pdev
->dev
);
251 regulator_unregister(rdev
);
252 clk_disable_unprepare(priv
->clk
);
253 pm_runtime_disable(&pdev
->dev
);
254 pm_runtime_set_suspended(&pdev
->dev
);
255 pm_runtime_put_noidle(&pdev
->dev
);
260 static int __maybe_unused
stm32_vrefbuf_runtime_suspend(struct device
*dev
)
262 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
263 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
265 clk_disable_unprepare(priv
->clk
);
270 static int __maybe_unused
stm32_vrefbuf_runtime_resume(struct device
*dev
)
272 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
273 struct stm32_vrefbuf
*priv
= rdev_get_drvdata(rdev
);
275 return clk_prepare_enable(priv
->clk
);
278 static const struct dev_pm_ops stm32_vrefbuf_pm_ops
= {
279 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
280 pm_runtime_force_resume
)
281 SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend
,
282 stm32_vrefbuf_runtime_resume
,
286 static const struct of_device_id stm32_vrefbuf_of_match
[] = {
287 { .compatible
= "st,stm32-vrefbuf", },
290 MODULE_DEVICE_TABLE(of
, stm32_vrefbuf_of_match
);
292 static struct platform_driver stm32_vrefbuf_driver
= {
293 .probe
= stm32_vrefbuf_probe
,
294 .remove
= stm32_vrefbuf_remove
,
296 .name
= "stm32-vrefbuf",
297 .of_match_table
= of_match_ptr(stm32_vrefbuf_of_match
),
298 .pm
= &stm32_vrefbuf_pm_ops
,
301 module_platform_driver(stm32_vrefbuf_driver
);
303 MODULE_LICENSE("GPL v2");
304 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
305 MODULE_DESCRIPTION("STMicroelectronics STM32 VREFBUF driver");
306 MODULE_ALIAS("platform:stm32-vrefbuf");