diff --git a/include/device.h b/include/device.h index 69465790442..79b5927b55c 100644 --- a/include/device.h +++ b/include/device.h @@ -831,27 +831,10 @@ BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts"); (&DEVICE_NAME_GET(dev_name)), level, prio) #ifdef CONFIG_PM_DEVICE -/* Use of DT_PROP_OR here is because we cant assume that 'wakeup-source` - * will be a defined property for the binding of the devicetree node that - * is associated with the device - */ #define Z_DEVICE_STATE_DEFINE(node_id, dev_name) \ static struct device_state Z_DEVICE_STATE_NAME(dev_name) \ __attribute__((__section__(".z_devstate"))) = { \ - .pm = { \ - .usage = 0U, \ - .lock = Z_MUTEX_INITIALIZER( \ - Z_DEVICE_STATE_NAME(dev_name).pm.lock), \ - .condvar = Z_CONDVAR_INITIALIZER( \ - Z_DEVICE_STATE_NAME(dev_name).pm.condvar),\ - .state = PM_DEVICE_STATE_ACTIVE, \ - .flags = ATOMIC_INIT(COND_CODE_1( \ - DT_NODE_EXISTS(node_id), \ - (DT_PROP_OR( \ - node_id, wakeup_source, 0)), \ - (0)) << \ - PM_DEVICE_FLAGS_WS_CAPABLE), \ - }, \ + .pm = Z_PM_DEVICE_INIT(Z_DEVICE_STATE_NAME(dev_name), node_id) \ }; #define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) \ diff --git a/include/pm/device.h b/include/pm/device.h index 831d0930836..9a1c00ac8a2 100644 --- a/include/pm/device.h +++ b/include/pm/device.h @@ -114,6 +114,27 @@ struct pm_device { struct k_condvar condvar; }; +/** + * @brief Utility macro to initialize #pm_device. + * + * @note DT_PROP_OR is used to retrieve the wakeup_source property because + * it may not be defined on all devices. + * + * @param obj Name of the #pm_device structure being initialized. + * @param node_id Devicetree node for the initialized device (can be invalid). + */ +#define Z_PM_DEVICE_INIT(obj, node_id) \ + { \ + .usage = 0U, \ + .lock = Z_MUTEX_INITIALIZER(obj.pm.lock), \ + .condvar = Z_CONDVAR_INITIALIZER(obj.pm.condvar), \ + .state = PM_DEVICE_STATE_ACTIVE, \ + .flags = ATOMIC_INIT(COND_CODE_1( \ + DT_NODE_EXISTS(node_id), \ + (DT_PROP_OR(node_id, wakeup_source, 0)),\ + (0)) << PM_DEVICE_FLAGS_WS_CAPABLE), \ + } + /** * @brief Device power management control function callback. *