From 71a0f39568eb85ee888bff7e715353c0011c77d0 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Wed, 9 Apr 2025 20:48:55 +1000 Subject: [PATCH] pm: device: correct state in `pm_device_driver_init` Set the value of `pm->state` as we move through the various stages of `pm_device_driver_init`. This ensures hat if any of the code inside the actions callbacks runs `pm_device_state_get` they get the correct state, instead of always getting `PM_DEVICE_STATE_ACTIVE` (0, the value at reset). Signed-off-by: Jordan Yates --- subsys/pm/device.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/subsys/pm/device.c b/subsys/pm/device.c index 518e20a8630..a1a7e92ee92 100644 --- a/subsys/pm/device.c +++ b/subsys/pm/device.c @@ -361,10 +361,13 @@ int pm_device_driver_init(const struct device *dev, struct pm_device_base *pm = dev->pm_base; int rc; + /* Device is currently in the OFF state */ + if (pm) { + pm->state = PM_DEVICE_STATE_OFF; + } + /* Work only needs to be performed if the device is powered */ if (!pm_device_is_powered(dev)) { - /* Start in off mode */ - pm_device_init_off(dev); return 0; } @@ -380,16 +383,21 @@ int pm_device_driver_init(const struct device *dev, return action_cb(dev, PM_DEVICE_ACTION_RESUME); } + /* Device is currently in the SUSPENDED state */ + pm->state = PM_DEVICE_STATE_SUSPENDED; + /* If device will have PM device runtime enabled */ if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME) && atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_AUTO)) { - /* Init into suspend mode. - * This saves a SUSPENDED->ACTIVE->SUSPENDED cycle. - */ - pm_device_init_suspended(dev); return 0; } /* Startup into active mode */ - return action_cb(dev, PM_DEVICE_ACTION_RESUME); + rc = action_cb(dev, PM_DEVICE_ACTION_RESUME); + + /* Device is now in the ACTIVE state */ + pm->state = PM_DEVICE_STATE_ACTIVE; + + /* Return the PM_DEVICE_ACTION_RESUME result */ + return rc; }