From 90465fe5372af05a00aa4efb51d21624e3e60755 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Thu, 26 Aug 2021 17:27:49 +0200 Subject: [PATCH] device: improve device data initialization The Z_DEVICE_STATE_DEFINE macro was conditioned by CONFIG_PM_DEVICE. This is a problem if one day we have other conditional fields in the device state field that need to be initialized. The approach has been changed to have an always existing initializer for the PM field, that is a no-op if device PM is not enabled. Signed-off-by: Gerard Marull-Paretas --- include/device.h | 22 +++++++++++----------- include/pm/device.h | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/device.h b/include/device.h index 79b5927b55c..1f4c6607313 100644 --- a/include/device.h +++ b/include/device.h @@ -831,22 +831,22 @@ BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts"); (&DEVICE_NAME_GET(dev_name)), level, prio) #ifdef CONFIG_PM_DEVICE +#define Z_DEVICE_STATE_PM_INIT(node_id, dev_name) \ + .pm = Z_PM_DEVICE_INIT(Z_DEVICE_STATE_NAME(dev_name).pm, node_id), +#define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) \ + .pm_control = (pm_control_fn), \ + .pm = &Z_DEVICE_STATE_NAME(dev_name).pm, +#else +#define Z_DEVICE_STATE_PM_INIT(node_id, dev_name) +#define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) +#endif + #define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \ static struct device_state Z_DEVICE_STATE_NAME(dev_name) \ __attribute__((__section__(".z_devstate"))) = { \ - .pm = Z_PM_DEVICE_INIT(Z_DEVICE_STATE_NAME(dev_name), node_id) \ + Z_DEVICE_STATE_PM_INIT(node_id, dev_name) \ }; -#define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) \ - .pm_control = (pm_control_fn), \ - .pm = &Z_DEVICE_STATE_NAME(dev_name).pm, -#else -#define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \ - __pinned_bss \ - static struct device_state Z_DEVICE_STATE_NAME(dev_name); -#define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) -#endif - #ifdef __cplusplus } #endif diff --git a/include/pm/device.h b/include/pm/device.h index 9a1c00ac8a2..fd54d8b60c9 100644 --- a/include/pm/device.h +++ b/include/pm/device.h @@ -126,8 +126,8 @@ struct pm_device { #define Z_PM_DEVICE_INIT(obj, node_id) \ { \ .usage = 0U, \ - .lock = Z_MUTEX_INITIALIZER(obj.pm.lock), \ - .condvar = Z_CONDVAR_INITIALIZER(obj.pm.condvar), \ + .lock = Z_MUTEX_INITIALIZER(obj.lock), \ + .condvar = Z_CONDVAR_INITIALIZER(obj.condvar), \ .state = PM_DEVICE_STATE_ACTIVE, \ .flags = ATOMIC_INIT(COND_CODE_1( \ DT_NODE_EXISTS(node_id), \