forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/iio/dac/stm32-dac.c
....@@ -13,6 +13,7 @@
1313 #include <linux/kernel.h>
1414 #include <linux/module.h>
1515 #include <linux/platform_device.h>
16
+#include <linux/pm_runtime.h>
1617
1718 #include "stm32-dac-core.h"
1819
....@@ -20,12 +21,17 @@
2021 #define STM32_DAC_CHANNEL_2 2
2122 #define STM32_DAC_IS_CHAN_1(ch) ((ch) & STM32_DAC_CHANNEL_1)
2223
24
+#define STM32_DAC_AUTO_SUSPEND_DELAY_MS 2000
25
+
2326 /**
2427 * struct stm32_dac - private data of DAC driver
2528 * @common: reference to DAC common data
29
+ * @lock: lock to protect against potential races when reading
30
+ * and update CR, to keep it in sync with pm_runtime
2631 */
2732 struct stm32_dac {
2833 struct stm32_dac_common *common;
34
+ struct mutex lock;
2935 };
3036
3137 static int stm32_dac_is_enabled(struct iio_dev *indio_dev, int channel)
....@@ -49,15 +55,34 @@
4955 bool enable)
5056 {
5157 struct stm32_dac *dac = iio_priv(indio_dev);
58
+ struct device *dev = indio_dev->dev.parent;
5259 u32 msk = STM32_DAC_IS_CHAN_1(ch) ? STM32_DAC_CR_EN1 : STM32_DAC_CR_EN2;
5360 u32 en = enable ? msk : 0;
5461 int ret;
5562
63
+ /* already enabled / disabled ? */
64
+ mutex_lock(&dac->lock);
65
+ ret = stm32_dac_is_enabled(indio_dev, ch);
66
+ if (ret < 0 || enable == !!ret) {
67
+ mutex_unlock(&dac->lock);
68
+ return ret < 0 ? ret : 0;
69
+ }
70
+
71
+ if (enable) {
72
+ ret = pm_runtime_get_sync(dev);
73
+ if (ret < 0) {
74
+ pm_runtime_put_noidle(dev);
75
+ mutex_unlock(&dac->lock);
76
+ return ret;
77
+ }
78
+ }
79
+
5680 ret = regmap_update_bits(dac->common->regmap, STM32_DAC_CR, msk, en);
81
+ mutex_unlock(&dac->lock);
5782 if (ret < 0) {
5883 dev_err(&indio_dev->dev, "%s failed\n", en ?
5984 "Enable" : "Disable");
60
- return ret;
85
+ goto err_put_pm;
6186 }
6287
6388 /*
....@@ -68,7 +93,20 @@
6893 if (en && dac->common->hfsel)
6994 udelay(1);
7095
96
+ if (!enable) {
97
+ pm_runtime_mark_last_busy(dev);
98
+ pm_runtime_put_autosuspend(dev);
99
+ }
100
+
71101 return 0;
102
+
103
+err_put_pm:
104
+ if (enable) {
105
+ pm_runtime_mark_last_busy(dev);
106
+ pm_runtime_put_autosuspend(dev);
107
+ }
108
+
109
+ return ret;
72110 }
73111
74112 static int stm32_dac_get_value(struct stm32_dac *dac, int channel, int *val)
....@@ -272,6 +310,7 @@
272310 static int stm32_dac_probe(struct platform_device *pdev)
273311 {
274312 struct device_node *np = pdev->dev.of_node;
313
+ struct device *dev = &pdev->dev;
275314 struct iio_dev *indio_dev;
276315 struct stm32_dac *dac;
277316 int ret;
....@@ -287,17 +326,70 @@
287326 dac = iio_priv(indio_dev);
288327 dac->common = dev_get_drvdata(pdev->dev.parent);
289328 indio_dev->name = dev_name(&pdev->dev);
290
- indio_dev->dev.parent = &pdev->dev;
291329 indio_dev->dev.of_node = pdev->dev.of_node;
292330 indio_dev->info = &stm32_dac_iio_info;
293331 indio_dev->modes = INDIO_DIRECT_MODE;
332
+
333
+ mutex_init(&dac->lock);
294334
295335 ret = stm32_dac_chan_of_init(indio_dev);
296336 if (ret < 0)
297337 return ret;
298338
299
- return devm_iio_device_register(&pdev->dev, indio_dev);
339
+ /* Get stm32-dac-core PM online */
340
+ pm_runtime_get_noresume(dev);
341
+ pm_runtime_set_active(dev);
342
+ pm_runtime_set_autosuspend_delay(dev, STM32_DAC_AUTO_SUSPEND_DELAY_MS);
343
+ pm_runtime_use_autosuspend(dev);
344
+ pm_runtime_enable(dev);
345
+
346
+ ret = iio_device_register(indio_dev);
347
+ if (ret)
348
+ goto err_pm_put;
349
+
350
+ pm_runtime_mark_last_busy(dev);
351
+ pm_runtime_put_autosuspend(dev);
352
+
353
+ return 0;
354
+
355
+err_pm_put:
356
+ pm_runtime_disable(dev);
357
+ pm_runtime_set_suspended(dev);
358
+ pm_runtime_put_noidle(dev);
359
+
360
+ return ret;
300361 }
362
+
363
+static int stm32_dac_remove(struct platform_device *pdev)
364
+{
365
+ struct iio_dev *indio_dev = platform_get_drvdata(pdev);
366
+
367
+ pm_runtime_get_sync(&pdev->dev);
368
+ iio_device_unregister(indio_dev);
369
+ pm_runtime_disable(&pdev->dev);
370
+ pm_runtime_set_suspended(&pdev->dev);
371
+ pm_runtime_put_noidle(&pdev->dev);
372
+
373
+ return 0;
374
+}
375
+
376
+static int __maybe_unused stm32_dac_suspend(struct device *dev)
377
+{
378
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
379
+ int channel = indio_dev->channels[0].channel;
380
+ int ret;
381
+
382
+ /* Ensure DAC is disabled before suspend */
383
+ ret = stm32_dac_is_enabled(indio_dev, channel);
384
+ if (ret)
385
+ return ret < 0 ? ret : -EBUSY;
386
+
387
+ return pm_runtime_force_suspend(dev);
388
+}
389
+
390
+static const struct dev_pm_ops stm32_dac_pm_ops = {
391
+ SET_SYSTEM_SLEEP_PM_OPS(stm32_dac_suspend, pm_runtime_force_resume)
392
+};
301393
302394 static const struct of_device_id stm32_dac_of_match[] = {
303395 { .compatible = "st,stm32-dac", },
....@@ -307,9 +399,11 @@
307399
308400 static struct platform_driver stm32_dac_driver = {
309401 .probe = stm32_dac_probe,
402
+ .remove = stm32_dac_remove,
310403 .driver = {
311404 .name = "stm32-dac",
312405 .of_match_table = stm32_dac_of_match,
406
+ .pm = &stm32_dac_pm_ops,
313407 },
314408 };
315409 module_platform_driver(stm32_dac_driver);