Skip to content

Commit be5295f

Browse files
fgasnier63broonie
authored andcommitted
regulator: stm32-vrefbuf: add power management support
Add support for suspend/resume and runtime PM to stm32-vrefbuf driver. Signed-off-by: Fabrice Gasnier <fabrice.gasnier@st.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 0e819b5 commit be5295f

1 file changed

Lines changed: 114 additions & 7 deletions

File tree

‎drivers/regulator/stm32-vrefbuf.c‎

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/platform_device.h>
1616
#include <linux/regulator/driver.h>
1717
#include <linux/regulator/of_regulator.h>
18+
#include <linux/pm_runtime.h>
1819

1920
/* STM32 VREFBUF registers */
2021
#define STM32_VREFBUF_CSR 0x00
@@ -25,9 +26,12 @@
2526
#define STM32_HIZ BIT(1)
2627
#define STM32_ENVR BIT(0)
2728

29+
#define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS 10
30+
2831
struct stm32_vrefbuf {
2932
void __iomem *base;
3033
struct clk *clk;
34+
struct device *dev;
3135
};
3236

3337
static const unsigned int stm32_vrefbuf_voltages[] = {
@@ -38,9 +42,16 @@ static const unsigned int stm32_vrefbuf_voltages[] = {
3842
static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
3943
{
4044
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
41-
u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
45+
u32 val;
4246
int ret;
4347

48+
ret = pm_runtime_get_sync(priv->dev);
49+
if (ret < 0) {
50+
pm_runtime_put_noidle(priv->dev);
51+
return ret;
52+
}
53+
54+
val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
4455
val = (val & ~STM32_HIZ) | STM32_ENVR;
4556
writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
4657

@@ -59,45 +70,95 @@ static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
5970
writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
6071
}
6172

73+
pm_runtime_mark_last_busy(priv->dev);
74+
pm_runtime_put_autosuspend(priv->dev);
75+
6276
return ret;
6377
}
6478

6579
static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
6680
{
6781
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
68-
u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
82+
u32 val;
83+
int ret;
6984

85+
ret = pm_runtime_get_sync(priv->dev);
86+
if (ret < 0) {
87+
pm_runtime_put_noidle(priv->dev);
88+
return ret;
89+
}
90+
91+
val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
7092
val = (val & ~STM32_ENVR) | STM32_HIZ;
7193
writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
7294

95+
pm_runtime_mark_last_busy(priv->dev);
96+
pm_runtime_put_autosuspend(priv->dev);
97+
7398
return 0;
7499
}
75100

76101
static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
77102
{
78103
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
104+
int ret;
105+
106+
ret = pm_runtime_get_sync(priv->dev);
107+
if (ret < 0) {
108+
pm_runtime_put_noidle(priv->dev);
109+
return ret;
110+
}
111+
112+
ret = readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
79113

80-
return readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
114+
pm_runtime_mark_last_busy(priv->dev);
115+
pm_runtime_put_autosuspend(priv->dev);
116+
117+
return ret;
81118
}
82119

83120
static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
84121
unsigned sel)
85122
{
86123
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
87-
u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
124+
u32 val;
125+
int ret;
88126

127+
ret = pm_runtime_get_sync(priv->dev);
128+
if (ret < 0) {
129+
pm_runtime_put_noidle(priv->dev);
130+
return ret;
131+
}
132+
133+
val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
89134
val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
90135
writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
91136

137+
pm_runtime_mark_last_busy(priv->dev);
138+
pm_runtime_put_autosuspend(priv->dev);
139+
92140
return 0;
93141
}
94142

95143
static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
96144
{
97145
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
98-
u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
146+
u32 val;
147+
int ret;
99148

100-
return FIELD_GET(STM32_VRS, val);
149+
ret = pm_runtime_get_sync(priv->dev);
150+
if (ret < 0) {
151+
pm_runtime_put_noidle(priv->dev);
152+
return ret;
153+
}
154+
155+
val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
156+
ret = FIELD_GET(STM32_VRS, val);
157+
158+
pm_runtime_mark_last_busy(priv->dev);
159+
pm_runtime_put_autosuspend(priv->dev);
160+
161+
return ret;
101162
}
102163

103164
static const struct regulator_ops stm32_vrefbuf_volt_ops = {
@@ -130,6 +191,7 @@ static int stm32_vrefbuf_probe(struct platform_device *pdev)
130191
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
131192
if (!priv)
132193
return -ENOMEM;
194+
priv->dev = &pdev->dev;
133195

134196
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135197
priv->base = devm_ioremap_resource(&pdev->dev, res);
@@ -140,10 +202,17 @@ static int stm32_vrefbuf_probe(struct platform_device *pdev)
140202
if (IS_ERR(priv->clk))
141203
return PTR_ERR(priv->clk);
142204

205+
pm_runtime_get_noresume(&pdev->dev);
206+
pm_runtime_set_active(&pdev->dev);
207+
pm_runtime_set_autosuspend_delay(&pdev->dev,
208+
STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS);
209+
pm_runtime_use_autosuspend(&pdev->dev);
210+
pm_runtime_enable(&pdev->dev);
211+
143212
ret = clk_prepare_enable(priv->clk);
144213
if (ret) {
145214
dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
146-
return ret;
215+
goto err_pm_stop;
147216
}
148217

149218
config.dev = &pdev->dev;
@@ -161,10 +230,17 @@ static int stm32_vrefbuf_probe(struct platform_device *pdev)
161230
}
162231
platform_set_drvdata(pdev, rdev);
163232

233+
pm_runtime_mark_last_busy(&pdev->dev);
234+
pm_runtime_put_autosuspend(&pdev->dev);
235+
164236
return 0;
165237

166238
err_clk_dis:
167239
clk_disable_unprepare(priv->clk);
240+
err_pm_stop:
241+
pm_runtime_disable(&pdev->dev);
242+
pm_runtime_set_suspended(&pdev->dev);
243+
pm_runtime_put_noidle(&pdev->dev);
168244

169245
return ret;
170246
}
@@ -174,12 +250,42 @@ static int stm32_vrefbuf_remove(struct platform_device *pdev)
174250
struct regulator_dev *rdev = platform_get_drvdata(pdev);
175251
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
176252

253+
pm_runtime_get_sync(&pdev->dev);
177254
regulator_unregister(rdev);
178255
clk_disable_unprepare(priv->clk);
256+
pm_runtime_disable(&pdev->dev);
257+
pm_runtime_set_suspended(&pdev->dev);
258+
pm_runtime_put_noidle(&pdev->dev);
179259

180260
return 0;
181261
};
182262

263+
static int __maybe_unused stm32_vrefbuf_runtime_suspend(struct device *dev)
264+
{
265+
struct regulator_dev *rdev = dev_get_drvdata(dev);
266+
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
267+
268+
clk_disable_unprepare(priv->clk);
269+
270+
return 0;
271+
}
272+
273+
static int __maybe_unused stm32_vrefbuf_runtime_resume(struct device *dev)
274+
{
275+
struct regulator_dev *rdev = dev_get_drvdata(dev);
276+
struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
277+
278+
return clk_prepare_enable(priv->clk);
279+
}
280+
281+
static const struct dev_pm_ops stm32_vrefbuf_pm_ops = {
282+
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
283+
pm_runtime_force_resume)
284+
SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend,
285+
stm32_vrefbuf_runtime_resume,
286+
NULL)
287+
};
288+
183289
static const struct of_device_id stm32_vrefbuf_of_match[] = {
184290
{ .compatible = "st,stm32-vrefbuf", },
185291
{},
@@ -192,6 +298,7 @@ static struct platform_driver stm32_vrefbuf_driver = {
192298
.driver = {
193299
.name = "stm32-vrefbuf",
194300
.of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
301+
.pm = &stm32_vrefbuf_pm_ops,
195302
},
196303
};
197304
module_platform_driver(stm32_vrefbuf_driver);

0 commit comments

Comments
 (0)