From d6ebab55f30cf96da7c7a8f23cd5f40f79ea677e Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Thu, 25 Nov 2021 13:07:50 +0100 Subject: [PATCH] pm: device_runtime: add pm_device_runtime_init_suspended By default device state is initialized to PM_DEVICE_STATE_ACTIVE. In general, this makes sense because the device initialization function will resume and configure a device, leaving it operational. However, when device runtime PM is enabled, the device may not be resumed and the init function will just enable device runtime PM. If that is the case, this function can be used to set the initial device state to PM_DEVICE_STATE_SUSPENDED. Documentation has been updated to comment on this case, and example code has been adjusted accordingly. Signed-off-by: Gerard Marull-Paretas --- include/pm/device_runtime.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/pm/device_runtime.h b/include/pm/device_runtime.h index 2f467e8b1c5..5424d2ae654 100644 --- a/include/pm/device_runtime.h +++ b/include/pm/device_runtime.h @@ -131,6 +131,25 @@ int pm_device_runtime_put_async(const struct device *dev); */ bool pm_device_runtime_is_enabled(const struct device *dev); +/** + * @brief Initialize a device state to #PM_DEVICE_STATE_SUSPENDED. + * + * By default device state is initialized to #PM_DEVICE_STATE_ACTIVE. In + * general, this makes sense because the device initialization function will + * resume and configure a device, leaving it operational. However, when device + * runtime PM is enabled, the device may not be resumed and the init function + * will just enable device runtime PM. If that is the case, this function can be + * used to set the initial device state to #PM_DEVICE_STATE_SUSPENDED. + * + * @param dev Device instance. + */ +static inline void pm_device_runtime_init_suspended(const struct device *dev) +{ + struct pm_device *pm = dev->pm; + + pm->state = PM_DEVICE_STATE_SUSPENDED; +} + #else static inline int pm_device_runtime_enable(const struct device *dev) { return -ENOSYS; } static inline int pm_device_runtime_disable(const struct device *dev) { return -ENOSYS; } @@ -138,6 +157,7 @@ static inline int pm_device_runtime_get(const struct device *dev) { return 0; } static inline int pm_device_runtime_put(const struct device *dev) { return 0; } static inline int pm_device_runtime_put_async(const struct device *dev) { return 0; } static inline bool pm_device_runtime_is_enabled(const struct device *dev) { return false; } +static inline void pm_device_runtime_init_suspended(const struct device *dev) { } #endif /** @} */