hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/iio/dac/stm32-dac-core.c
....@@ -11,6 +11,7 @@
1111 #include <linux/delay.h>
1212 #include <linux/module.h>
1313 #include <linux/of_platform.h>
14
+#include <linux/pm_runtime.h>
1415 #include <linux/regulator/consumer.h>
1516 #include <linux/reset.h>
1617
....@@ -19,13 +20,11 @@
1920 /**
2021 * struct stm32_dac_priv - stm32 DAC core private data
2122 * @pclk: peripheral clock common for all DACs
22
- * @rst: peripheral reset control
2323 * @vref: regulator reference
2424 * @common: Common data for all DAC instances
2525 */
2626 struct stm32_dac_priv {
2727 struct clk *pclk;
28
- struct reset_control *rst;
2928 struct regulator *vref;
3029 struct stm32_dac_common common;
3130 };
....@@ -50,6 +49,41 @@
5049 .max_register = 0x3fc,
5150 };
5251
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
+
5387 static int stm32_dac_probe(struct platform_device *pdev)
5488 {
5589 struct device *dev = &pdev->dev;
....@@ -58,6 +92,7 @@
5892 struct regmap *regmap;
5993 struct resource *res;
6094 void __iomem *mmio;
95
+ struct reset_control *rst;
6196 int ret;
6297
6398 if (!dev->of_node)
....@@ -66,6 +101,8 @@
66101 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
67102 if (!priv)
68103 return -ENOMEM;
104
+ platform_set_drvdata(pdev, &priv->common);
105
+
69106 cfg = (const struct stm32_dac_cfg *)
70107 of_match_device(dev->driver->of_match_table, dev)->data;
71108
....@@ -74,10 +111,18 @@
74111 if (IS_ERR(mmio))
75112 return PTR_ERR(mmio);
76113
77
- regmap = devm_regmap_init_mmio(dev, mmio, &stm32_dac_regmap_cfg);
114
+ regmap = devm_regmap_init_mmio_clk(dev, "pclk", mmio,
115
+ &stm32_dac_regmap_cfg);
78116 if (IS_ERR(regmap))
79117 return PTR_ERR(regmap);
80118 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
+ }
81126
82127 priv->vref = devm_regulator_get(dev, "vref");
83128 if (IS_ERR(priv->vref)) {
....@@ -86,38 +131,32 @@
86131 return ret;
87132 }
88133
89
- ret = regulator_enable(priv->vref);
90
- if (ret < 0) {
91
- dev_err(dev, "vref enable failed\n");
92
- return ret;
93
- }
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;
94141
95142 ret = regulator_get_voltage(priv->vref);
96143 if (ret < 0) {
97144 dev_err(dev, "vref get voltage failed, %d\n", ret);
98
- goto err_vref;
145
+ goto err_hw_stop;
99146 }
100147 priv->common.vref_mv = ret / 1000;
101148 dev_dbg(dev, "vref+=%dmV\n", priv->common.vref_mv);
102149
103
- priv->pclk = devm_clk_get(dev, "pclk");
104
- if (IS_ERR(priv->pclk)) {
105
- ret = PTR_ERR(priv->pclk);
106
- dev_err(dev, "pclk get failed\n");
107
- goto err_vref;
108
- }
150
+ rst = devm_reset_control_get_optional_exclusive(dev, NULL);
151
+ if (rst) {
152
+ if (IS_ERR(rst)) {
153
+ ret = dev_err_probe(dev, PTR_ERR(rst), "reset get failed\n");
154
+ goto err_hw_stop;
155
+ }
109156
110
- ret = clk_prepare_enable(priv->pclk);
111
- if (ret < 0) {
112
- dev_err(dev, "pclk enable failed\n");
113
- goto err_vref;
114
- }
115
-
116
- priv->rst = devm_reset_control_get_exclusive(dev, NULL);
117
- if (!IS_ERR(priv->rst)) {
118
- reset_control_assert(priv->rst);
157
+ reset_control_assert(rst);
119158 udelay(2);
120
- reset_control_deassert(priv->rst);
159
+ reset_control_deassert(rst);
121160 }
122161
123162 if (cfg && cfg->has_hfsel) {
....@@ -128,38 +167,78 @@
128167 priv->common.hfsel ?
129168 STM32H7_DAC_CR_HFSEL : 0);
130169 if (ret)
131
- goto err_pclk;
170
+ goto err_hw_stop;
132171 }
133172
134
- platform_set_drvdata(pdev, &priv->common);
135173
136174 ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, dev);
137175 if (ret < 0) {
138176 dev_err(dev, "failed to populate DT children\n");
139
- goto err_pclk;
177
+ goto err_hw_stop;
140178 }
179
+
180
+ pm_runtime_put(dev);
141181
142182 return 0;
143183
144
-err_pclk:
145
- clk_disable_unprepare(priv->pclk);
146
-err_vref:
147
- regulator_disable(priv->vref);
184
+err_hw_stop:
185
+ stm32_dac_core_hw_stop(dev);
186
+err_pm_stop:
187
+ pm_runtime_disable(dev);
188
+ pm_runtime_set_suspended(dev);
189
+ pm_runtime_put_noidle(dev);
148190
149191 return ret;
150192 }
151193
152194 static int stm32_dac_remove(struct platform_device *pdev)
153195 {
154
- struct stm32_dac_common *common = platform_get_drvdata(pdev);
155
- struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
156
-
196
+ pm_runtime_get_sync(&pdev->dev);
157197 of_platform_depopulate(&pdev->dev);
158
- clk_disable_unprepare(priv->pclk);
159
- regulator_disable(priv->vref);
198
+ stm32_dac_core_hw_stop(&pdev->dev);
199
+ pm_runtime_disable(&pdev->dev);
200
+ pm_runtime_set_suspended(&pdev->dev);
201
+ pm_runtime_put_noidle(&pdev->dev);
160202
161203 return 0;
162204 }
205
+
206
+static int __maybe_unused stm32_dac_core_resume(struct device *dev)
207
+{
208
+ struct stm32_dac_common *common = dev_get_drvdata(dev);
209
+ struct stm32_dac_priv *priv = to_stm32_dac_priv(common);
210
+ int ret;
211
+
212
+ if (priv->common.hfsel) {
213
+ /* restore hfsel (maybe lost under low power state) */
214
+ ret = regmap_update_bits(priv->common.regmap, STM32_DAC_CR,
215
+ STM32H7_DAC_CR_HFSEL,
216
+ STM32H7_DAC_CR_HFSEL);
217
+ if (ret)
218
+ return ret;
219
+ }
220
+
221
+ return pm_runtime_force_resume(dev);
222
+}
223
+
224
+static int __maybe_unused stm32_dac_core_runtime_suspend(struct device *dev)
225
+{
226
+ stm32_dac_core_hw_stop(dev);
227
+
228
+ return 0;
229
+}
230
+
231
+static int __maybe_unused stm32_dac_core_runtime_resume(struct device *dev)
232
+{
233
+ return stm32_dac_core_hw_start(dev);
234
+}
235
+
236
+static const struct dev_pm_ops stm32_dac_core_pm_ops = {
237
+ SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, stm32_dac_core_resume)
238
+ SET_RUNTIME_PM_OPS(stm32_dac_core_runtime_suspend,
239
+ stm32_dac_core_runtime_resume,
240
+ NULL)
241
+};
163242
164243 static const struct stm32_dac_cfg stm32h7_dac_cfg = {
165244 .has_hfsel = true,
....@@ -182,6 +261,7 @@
182261 .driver = {
183262 .name = "stm32-dac-core",
184263 .of_match_table = stm32_dac_of_match,
264
+ .pm = &stm32_dac_core_pm_ops,
185265 },
186266 };
187267 module_platform_driver(stm32_dac_driver);