From df819a860924fac82582ce34d85c256b9474cfa1 Mon Sep 17 00:00:00 2001 From: Fabio Baltieri Date: Tue, 23 Apr 2024 17:47:31 +0100 Subject: [PATCH] pm: device_runtime: add an access function to get the usage counter Add a pm_device_usage_get() to get the current usage counter. Signed-off-by: Fabio Baltieri --- include/zephyr/pm/device_runtime.h | 17 +++++++++++++++++ subsys/pm/device_runtime.c | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/zephyr/pm/device_runtime.h b/include/zephyr/pm/device_runtime.h index 25fea556e4e..b1404fa4ac4 100644 --- a/include/zephyr/pm/device_runtime.h +++ b/include/zephyr/pm/device_runtime.h @@ -159,6 +159,17 @@ int pm_device_runtime_put_async(const struct device *dev, k_timeout_t delay); */ bool pm_device_runtime_is_enabled(const struct device *dev); +/** + * @brief Return the current device usage counter. + * + * @param dev Device instance. + * + * @returns the current usage counter. + * @retval -ENOTSUP If the device is not using runtime PM. + * @retval -ENOSYS If the runtime PM is not enabled at all. + */ +int pm_device_runtime_usage(const struct device *dev); + #else static inline int pm_device_runtime_auto_enable(const struct device *dev) @@ -205,6 +216,12 @@ static inline bool pm_device_runtime_is_enabled(const struct device *dev) return false; } +static inline int pm_device_runtime_usage(const struct device *dev) +{ + ARG_UNUSED(dev); + return -ENOSYS; +} + #endif /** @} */ diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index b06b72242df..51cdd74e4a4 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -560,3 +560,19 @@ bool pm_device_runtime_is_enabled(const struct device *dev) return pm && atomic_test_bit(&pm->flags, PM_DEVICE_FLAG_RUNTIME_ENABLED); } + +int pm_device_runtime_usage(const struct device *dev) +{ + struct pm_device *pm = dev->pm; + uint32_t usage; + + if (!pm_device_runtime_is_enabled(dev)) { + return -ENOTSUP; + } + + (void)k_sem_take(&pm->lock, K_FOREVER); + usage = pm->base.usage; + k_sem_give(&pm->lock); + + return usage; +}