From 18b932f10d323aaec9f9cce2147d47a04aff2acf Mon Sep 17 00:00:00 2001 From: Flavio Ceolin Date: Tue, 14 Dec 2021 09:47:00 -0800 Subject: [PATCH] pm: device_runtime: Return possible error on enable Change the function pm_device_runtime_enable() to return 0 on success or an error code in case of error. Signed-off-by: Flavio Ceolin --- doc/guides/pm/device_runtime.rst | 5 ++++- drivers/gpio/gpio_stm32.c | 4 +--- include/pm/device_runtime.h | 8 ++++++-- include/tracing/tracing.h | 3 ++- samples/subsys/pm/device_pm/src/dummy_driver.c | 4 +--- samples/subsys/pm/device_pm/src/dummy_parent.c | 3 +-- subsys/pm/device_runtime.c | 7 +++++-- subsys/tracing/ctf/tracing_ctf.h | 2 +- subsys/tracing/sysview/SYSVIEW_Zephyr.txt | 2 +- subsys/tracing/sysview/tracing_sysview.h | 5 +++-- subsys/tracing/test/tracing_test.h | 2 +- tests/subsys/pm/power_mgmt/src/dummy_driver.c | 3 +-- tests/subsys/pm/power_mgmt/src/main.c | 3 ++- 13 files changed, 29 insertions(+), 22 deletions(-) diff --git a/doc/guides/pm/device_runtime.rst b/doc/guides/pm/device_runtime.rst index 38745934289..16a40928213 100644 --- a/doc/guides/pm/device_runtime.rst +++ b/doc/guides/pm/device_runtime.rst @@ -154,7 +154,10 @@ must perform the necessary operations to suspend the device. ... /* make sure the device physically is suspended */ /* enable device runtime power management */ - pm_device_runtime_enable(dev); + ret = pm_device_runtime_enable(dev); + if (ret < 0) { + return ret; + } ... } diff --git a/drivers/gpio/gpio_stm32.c b/drivers/gpio/gpio_stm32.c index 5315b82cf04..29746d3e9f7 100644 --- a/drivers/gpio/gpio_stm32.c +++ b/drivers/gpio/gpio_stm32.c @@ -612,9 +612,7 @@ static int gpio_stm32_init(const struct device *dev) #endif #ifdef CONFIG_PM_DEVICE_RUNTIME - pm_device_runtime_enable(dev); - - return 0; + return pm_device_runtime_enable(dev); #else return gpio_stm32_clock_request(dev, true); #endif diff --git a/include/pm/device_runtime.h b/include/pm/device_runtime.h index ba8c3f40311..6e227a4e410 100644 --- a/include/pm/device_runtime.h +++ b/include/pm/device_runtime.h @@ -28,8 +28,12 @@ extern "C" { * @funcprops \pre_kernel_ok * * @param dev Device instance. + * + * @retval 0 If the device runtime PM is enabled successfully. + * @retval -EPERM If device has power state locked. + * @retval -ENOSYS If the functionality is not available. */ -void pm_device_runtime_enable(const struct device *dev); +int pm_device_runtime_enable(const struct device *dev); /** * @brief Disable device runtime PM @@ -131,7 +135,7 @@ int pm_device_runtime_put_async(const struct device *dev); bool pm_device_runtime_is_enabled(const struct device *dev); #else -static inline void pm_device_runtime_enable(const struct device *dev) { } +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; } static inline int pm_device_runtime_get(const struct device *dev) { return -ENOSYS; } static inline int pm_device_runtime_put(const struct device *dev) { return -ENOSYS; } diff --git a/include/tracing/tracing.h b/include/tracing/tracing.h index 8e09907369e..9461427f62f 100644 --- a/include/tracing/tracing.h +++ b/include/tracing/tracing.h @@ -1985,8 +1985,9 @@ void sys_trace_idle(void); /** * @brief Trace enabling device runtime PM call exit. * @param dev Device instance. + * @param ret Return value. */ -#define sys_port_trace_pm_device_runtime_enable_exit(dev) +#define sys_port_trace_pm_device_runtime_enable_exit(dev, ret) /** * @brief Trace disabling device runtime PM call entry. diff --git a/samples/subsys/pm/device_pm/src/dummy_driver.c b/samples/subsys/pm/device_pm/src/dummy_driver.c index 4df43c9022f..9cb769e49e3 100644 --- a/samples/subsys/pm/device_pm/src/dummy_driver.c +++ b/samples/subsys/pm/device_pm/src/dummy_driver.c @@ -109,9 +109,7 @@ int dummy_init(const struct device *dev) printk("parent not found\n"); } - pm_device_runtime_enable(dev); - - return 0; + return pm_device_runtime_enable(dev); } PM_DEVICE_DEFINE(dummy_driver, dummy_device_pm_action); diff --git a/samples/subsys/pm/device_pm/src/dummy_parent.c b/samples/subsys/pm/device_pm/src/dummy_parent.c index fb3586c7f1c..80776bee5b0 100644 --- a/samples/subsys/pm/device_pm/src/dummy_parent.c +++ b/samples/subsys/pm/device_pm/src/dummy_parent.c @@ -48,8 +48,7 @@ static const struct dummy_parent_api funcs = { int dummy_parent_init(const struct device *dev) { - pm_device_runtime_enable(dev); - return 0; + return pm_device_runtime_enable(dev); } PM_DEVICE_DEFINE(dummy_parent, dummy_parent_pm_action); diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index 74b65bda352..ec6cf4365e0 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -166,13 +166,15 @@ int pm_device_runtime_put_async(const struct device *dev) return ret; } -void pm_device_runtime_enable(const struct device *dev) +int pm_device_runtime_enable(const struct device *dev) { + int ret = 0; struct pm_device *pm = dev->pm; SYS_PORT_TRACING_FUNC_ENTER(pm, device_runtime_enable, dev); if (pm_device_state_is_locked(dev)) { + ret = -EPERM; goto end; } @@ -200,7 +202,8 @@ unlock: } end: - SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_enable, dev); + SYS_PORT_TRACING_FUNC_EXIT(pm, device_runtime_enable, dev, ret); + return ret; } int pm_device_runtime_disable(const struct device *dev) diff --git a/subsys/tracing/ctf/tracing_ctf.h b/subsys/tracing/ctf/tracing_ctf.h index 6370614fa3b..85845701945 100644 --- a/subsys/tracing/ctf/tracing_ctf.h +++ b/subsys/tracing/ctf/tracing_ctf.h @@ -331,7 +331,7 @@ extern "C" { #define sys_port_trace_pm_device_runtime_put_async_enter(dev) #define sys_port_trace_pm_device_runtime_put_async_exit(dev, ret) #define sys_port_trace_pm_device_runtime_enable_enter(dev) -#define sys_port_trace_pm_device_runtime_enable_exit(dev) +#define sys_port_trace_pm_device_runtime_enable_exit(dev, ret) #define sys_port_trace_pm_device_runtime_disable_enter(dev) #define sys_port_trace_pm_device_runtime_disable_exit(dev, ret) diff --git a/subsys/tracing/sysview/SYSVIEW_Zephyr.txt b/subsys/tracing/sysview/SYSVIEW_Zephyr.txt index a39fe4a98e3..9be847a11b2 100644 --- a/subsys/tracing/sysview/SYSVIEW_Zephyr.txt +++ b/subsys/tracing/sysview/SYSVIEW_Zephyr.txt @@ -166,5 +166,5 @@ TaskState 0xBF 1=dummy, 2=Waiting, 4=New, 8=Terminated, 16=Suspended, 32=Termina 156 pm_device_runtime_get dev=%I | Returns %u 157 pm_device_runtime_put dev=%I | Returns %u 158 pm_device_runtime_put_async dev=%I | Returns %u -159 pm_device_runtime_enable dev=%I +159 pm_device_runtime_enable dev=%I | Returns %u 160 pm_device_runtime_disable dev=%I | Returns %u diff --git a/subsys/tracing/sysview/tracing_sysview.h b/subsys/tracing/sysview/tracing_sysview.h index 22042d8eeec..b27d09ac45a 100644 --- a/subsys/tracing/sysview/tracing_sysview.h +++ b/subsys/tracing/sysview/tracing_sysview.h @@ -639,8 +639,9 @@ void sys_trace_k_thread_info(struct k_thread *thread); #define sys_port_trace_pm_device_runtime_enable_enter(dev) \ SEGGER_SYSVIEW_RecordU32(TID_PM_DEVICE_RUNTIME_ENABLE, \ (uint32_t)(uintptr_t)dev) -#define sys_port_trace_pm_device_runtime_enable_exit(dev) \ - SEGGER_SYSVIEW_RecordEndCall(TID_PM_DEVICE_RUNTIME_ENABLE) +#define sys_port_trace_pm_device_runtime_enable_exit(dev, ret) \ + SEGGER_SYSVIEW_RecordEndCall(TID_PM_DEVICE_RUNTIME_ENABLE, \ + (uint32_t)ret) #define sys_port_trace_pm_device_runtime_disable_enter(dev) \ SEGGER_SYSVIEW_RecordU32(TID_PM_DEVICE_RUNTIME_DISABLE, \ (uint32_t)(uintptr_t)dev) diff --git a/subsys/tracing/test/tracing_test.h b/subsys/tracing/test/tracing_test.h index e3bb3b89722..3f9f3d281bb 100644 --- a/subsys/tracing/test/tracing_test.h +++ b/subsys/tracing/test/tracing_test.h @@ -439,7 +439,7 @@ #define sys_port_trace_pm_device_runtime_put_async_enter(dev) #define sys_port_trace_pm_device_runtime_put_async_exit(dev, ret) #define sys_port_trace_pm_device_runtime_enable_enter(dev) -#define sys_port_trace_pm_device_runtime_enable_exit(dev) +#define sys_port_trace_pm_device_runtime_enable_exit(dev, ret) #define sys_port_trace_pm_device_runtime_disable_enter(dev) #define sys_port_trace_pm_device_runtime_disable_exit(dev, ret) diff --git a/tests/subsys/pm/power_mgmt/src/dummy_driver.c b/tests/subsys/pm/power_mgmt/src/dummy_driver.c index e8241a8890d..b21b93962d3 100644 --- a/tests/subsys/pm/power_mgmt/src/dummy_driver.c +++ b/tests/subsys/pm/power_mgmt/src/dummy_driver.c @@ -33,8 +33,7 @@ static const struct dummy_driver_api funcs = { int dummy_init(const struct device *dev) { - pm_device_runtime_enable(dev); - return 0; + return pm_device_runtime_enable(dev); } PM_DEVICE_DEFINE(dummy_driver, dummy_device_pm_action); diff --git a/tests/subsys/pm/power_mgmt/src/main.c b/tests/subsys/pm/power_mgmt/src/main.c index 2615d0ef985..216f3969dbf 100644 --- a/tests/subsys/pm/power_mgmt/src/main.c +++ b/tests/subsys/pm/power_mgmt/src/main.c @@ -304,7 +304,8 @@ void test_power_state_trans(void) k_sleep(SLEEP_TIMEOUT); zassert_true(leave_idle, NULL); - pm_device_runtime_enable(device_dummy); + ret = pm_device_runtime_enable(device_dummy); + zassert_true(ret == 0, "Failed to enable device runtime PM"); pm_notifier_unregister(¬ifier); }