hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/regulator/stm32-vrefbuf.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) STMicroelectronics 2017
34 *
45 * Author: Fabrice Gasnier <fabrice.gasnier@st.com>
5
- *
6
- * License terms: GNU General Public License (GPL), version 2
76 */
87
98 #include <linux/bitfield.h>
....@@ -15,6 +14,7 @@
1514 #include <linux/platform_device.h>
1615 #include <linux/regulator/driver.h>
1716 #include <linux/regulator/of_regulator.h>
17
+#include <linux/pm_runtime.h>
1818
1919 /* STM32 VREFBUF registers */
2020 #define STM32_VREFBUF_CSR 0x00
....@@ -25,9 +25,12 @@
2525 #define STM32_HIZ BIT(1)
2626 #define STM32_ENVR BIT(0)
2727
28
+#define STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS 10
29
+
2830 struct stm32_vrefbuf {
2931 void __iomem *base;
3032 struct clk *clk;
33
+ struct device *dev;
3134 };
3235
3336 static const unsigned int stm32_vrefbuf_voltages[] = {
....@@ -38,9 +41,16 @@
3841 static int stm32_vrefbuf_enable(struct regulator_dev *rdev)
3942 {
4043 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
41
- u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
44
+ u32 val;
4245 int ret;
4346
47
+ ret = pm_runtime_get_sync(priv->dev);
48
+ if (ret < 0) {
49
+ pm_runtime_put_noidle(priv->dev);
50
+ return ret;
51
+ }
52
+
53
+ val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
4454 val = (val & ~STM32_HIZ) | STM32_ENVR;
4555 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
4656
....@@ -59,16 +69,30 @@
5969 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
6070 }
6171
72
+ pm_runtime_mark_last_busy(priv->dev);
73
+ pm_runtime_put_autosuspend(priv->dev);
74
+
6275 return ret;
6376 }
6477
6578 static int stm32_vrefbuf_disable(struct regulator_dev *rdev)
6679 {
6780 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
68
- u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
81
+ u32 val;
82
+ int ret;
6983
70
- val = (val & ~STM32_ENVR) | STM32_HIZ;
84
+ ret = pm_runtime_get_sync(priv->dev);
85
+ if (ret < 0) {
86
+ pm_runtime_put_noidle(priv->dev);
87
+ return ret;
88
+ }
89
+
90
+ val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
91
+ val &= ~STM32_ENVR;
7192 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
93
+
94
+ pm_runtime_mark_last_busy(priv->dev);
95
+ pm_runtime_put_autosuspend(priv->dev);
7296
7397 return 0;
7498 }
....@@ -76,18 +100,41 @@
76100 static int stm32_vrefbuf_is_enabled(struct regulator_dev *rdev)
77101 {
78102 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
103
+ int ret;
79104
80
- return readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
105
+ ret = pm_runtime_get_sync(priv->dev);
106
+ if (ret < 0) {
107
+ pm_runtime_put_noidle(priv->dev);
108
+ return ret;
109
+ }
110
+
111
+ ret = readl_relaxed(priv->base + STM32_VREFBUF_CSR) & STM32_ENVR;
112
+
113
+ pm_runtime_mark_last_busy(priv->dev);
114
+ pm_runtime_put_autosuspend(priv->dev);
115
+
116
+ return ret;
81117 }
82118
83119 static int stm32_vrefbuf_set_voltage_sel(struct regulator_dev *rdev,
84120 unsigned sel)
85121 {
86122 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
87
- u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
123
+ u32 val;
124
+ int ret;
88125
126
+ ret = pm_runtime_get_sync(priv->dev);
127
+ if (ret < 0) {
128
+ pm_runtime_put_noidle(priv->dev);
129
+ return ret;
130
+ }
131
+
132
+ val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
89133 val = (val & ~STM32_VRS) | FIELD_PREP(STM32_VRS, sel);
90134 writel_relaxed(val, priv->base + STM32_VREFBUF_CSR);
135
+
136
+ pm_runtime_mark_last_busy(priv->dev);
137
+ pm_runtime_put_autosuspend(priv->dev);
91138
92139 return 0;
93140 }
....@@ -95,9 +142,22 @@
95142 static int stm32_vrefbuf_get_voltage_sel(struct regulator_dev *rdev)
96143 {
97144 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
98
- u32 val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
145
+ u32 val;
146
+ int ret;
99147
100
- return FIELD_GET(STM32_VRS, val);
148
+ ret = pm_runtime_get_sync(priv->dev);
149
+ if (ret < 0) {
150
+ pm_runtime_put_noidle(priv->dev);
151
+ return ret;
152
+ }
153
+
154
+ val = readl_relaxed(priv->base + STM32_VREFBUF_CSR);
155
+ ret = FIELD_GET(STM32_VRS, val);
156
+
157
+ pm_runtime_mark_last_busy(priv->dev);
158
+ pm_runtime_put_autosuspend(priv->dev);
159
+
160
+ return ret;
101161 }
102162
103163 static const struct regulator_ops stm32_vrefbuf_volt_ops = {
....@@ -115,13 +175,13 @@
115175 .volt_table = stm32_vrefbuf_voltages,
116176 .n_voltages = ARRAY_SIZE(stm32_vrefbuf_voltages),
117177 .ops = &stm32_vrefbuf_volt_ops,
178
+ .off_on_delay = 1000,
118179 .type = REGULATOR_VOLTAGE,
119180 .owner = THIS_MODULE,
120181 };
121182
122183 static int stm32_vrefbuf_probe(struct platform_device *pdev)
123184 {
124
- struct resource *res;
125185 struct stm32_vrefbuf *priv;
126186 struct regulator_config config = { };
127187 struct regulator_dev *rdev;
....@@ -130,9 +190,9 @@
130190 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
131191 if (!priv)
132192 return -ENOMEM;
193
+ priv->dev = &pdev->dev;
133194
134
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135
- priv->base = devm_ioremap_resource(&pdev->dev, res);
195
+ priv->base = devm_platform_ioremap_resource(pdev, 0);
136196 if (IS_ERR(priv->base))
137197 return PTR_ERR(priv->base);
138198
....@@ -140,10 +200,17 @@
140200 if (IS_ERR(priv->clk))
141201 return PTR_ERR(priv->clk);
142202
203
+ pm_runtime_get_noresume(&pdev->dev);
204
+ pm_runtime_set_active(&pdev->dev);
205
+ pm_runtime_set_autosuspend_delay(&pdev->dev,
206
+ STM32_VREFBUF_AUTO_SUSPEND_DELAY_MS);
207
+ pm_runtime_use_autosuspend(&pdev->dev);
208
+ pm_runtime_enable(&pdev->dev);
209
+
143210 ret = clk_prepare_enable(priv->clk);
144211 if (ret) {
145212 dev_err(&pdev->dev, "clk prepare failed with error %d\n", ret);
146
- return ret;
213
+ goto err_pm_stop;
147214 }
148215
149216 config.dev = &pdev->dev;
....@@ -161,10 +228,17 @@
161228 }
162229 platform_set_drvdata(pdev, rdev);
163230
231
+ pm_runtime_mark_last_busy(&pdev->dev);
232
+ pm_runtime_put_autosuspend(&pdev->dev);
233
+
164234 return 0;
165235
166236 err_clk_dis:
167237 clk_disable_unprepare(priv->clk);
238
+err_pm_stop:
239
+ pm_runtime_disable(&pdev->dev);
240
+ pm_runtime_set_suspended(&pdev->dev);
241
+ pm_runtime_put_noidle(&pdev->dev);
168242
169243 return ret;
170244 }
....@@ -174,13 +248,43 @@
174248 struct regulator_dev *rdev = platform_get_drvdata(pdev);
175249 struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
176250
251
+ pm_runtime_get_sync(&pdev->dev);
177252 regulator_unregister(rdev);
178253 clk_disable_unprepare(priv->clk);
254
+ pm_runtime_disable(&pdev->dev);
255
+ pm_runtime_set_suspended(&pdev->dev);
256
+ pm_runtime_put_noidle(&pdev->dev);
179257
180258 return 0;
181259 };
182260
183
-static const struct of_device_id stm32_vrefbuf_of_match[] = {
261
+static int __maybe_unused stm32_vrefbuf_runtime_suspend(struct device *dev)
262
+{
263
+ struct regulator_dev *rdev = dev_get_drvdata(dev);
264
+ struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
265
+
266
+ clk_disable_unprepare(priv->clk);
267
+
268
+ return 0;
269
+}
270
+
271
+static int __maybe_unused stm32_vrefbuf_runtime_resume(struct device *dev)
272
+{
273
+ struct regulator_dev *rdev = dev_get_drvdata(dev);
274
+ struct stm32_vrefbuf *priv = rdev_get_drvdata(rdev);
275
+
276
+ return clk_prepare_enable(priv->clk);
277
+}
278
+
279
+static const struct dev_pm_ops stm32_vrefbuf_pm_ops = {
280
+ SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
281
+ pm_runtime_force_resume)
282
+ SET_RUNTIME_PM_OPS(stm32_vrefbuf_runtime_suspend,
283
+ stm32_vrefbuf_runtime_resume,
284
+ NULL)
285
+};
286
+
287
+static const struct of_device_id __maybe_unused stm32_vrefbuf_of_match[] = {
184288 { .compatible = "st,stm32-vrefbuf", },
185289 {},
186290 };
....@@ -192,6 +296,7 @@
192296 .driver = {
193297 .name = "stm32-vrefbuf",
194298 .of_match_table = of_match_ptr(stm32_vrefbuf_of_match),
299
+ .pm = &stm32_vrefbuf_pm_ops,
195300 },
196301 };
197302 module_platform_driver(stm32_vrefbuf_driver);