From 8d771f1d8ebb295339975736a2eb69a48fd89ae9 Mon Sep 17 00:00:00 2001 From: Peter Bigot Date: Tue, 2 Feb 2021 10:09:14 -0600 Subject: [PATCH] device: move device power management state into common dynamic state This avoids the need for distinct object that uses flash to store its initializer. Instead the state is initialized when the kernel is starting up, before anything can reference it. In future refactoring the PM state could be accessed directly without storing an extra pointer in the static device state. Signed-off-by: Peter Bigot --- include/device.h | 20 +++++--------------- kernel/device.c | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/include/device.h b/include/device.h index 40331b30ea9..89c5589d2d5 100644 --- a/include/device.h +++ b/include/device.h @@ -293,6 +293,10 @@ struct device_pm { * before they are accessed. */ struct device_state { +#ifdef CONFIG_PM_DEVICE + /* Power management data */ + struct device_pm pm; +#endif /* CONFIG_PM_DEVICE */ }; /** @@ -752,7 +756,6 @@ static inline int device_pm_put_sync(const struct device *dev) { return -ENOTSUP #define Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, pm_control_fn, \ data_ptr, cfg_ptr, level, prio, api_ptr) \ - Z_DEVICE_DEFINE_PM(dev_name) \ static struct device_state Z_DEVICE_STATE_NAME(dev_name); \ COND_CODE_1(DT_NODE_EXISTS(node_id), (), (static)) \ const Z_DECL_ALIGN(struct device) \ @@ -771,23 +774,10 @@ static inline int device_pm_put_sync(const struct device *dev) { return -ENOTSUP (&DEVICE_NAME_GET(dev_name)), level, prio) #ifdef CONFIG_PM_DEVICE -#define Z_DEVICE_DEFINE_PM(dev_name) \ - static struct device_pm _CONCAT(__pm_, dev_name) __used = { \ - .usage = ATOMIC_INIT(0), \ - .lock = Z_SEM_INITIALIZER( \ - _CONCAT(__pm_, dev_name).lock, 1, 1), \ - .signal = K_POLL_SIGNAL_INITIALIZER( \ - _CONCAT(__pm_, dev_name).signal), \ - .event = K_POLL_EVENT_INITIALIZER( \ - K_POLL_TYPE_SIGNAL, \ - K_POLL_MODE_NOTIFY_ONLY, \ - &_CONCAT(__pm_, dev_name).signal), \ - }; #define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) \ .device_pm_control = (pm_control_fn), \ - .pm = &_CONCAT(__pm_, dev_name), + .pm = &Z_DEVICE_STATE_NAME(dev_name).pm, #else -#define Z_DEVICE_DEFINE_PM(dev_name) #define Z_DEVICE_DEFINE_PM_INIT(dev_name, pm_control_fn) #endif diff --git a/kernel/device.c b/kernel/device.c index b95feaafb4e..ae08ddaa934 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -31,6 +31,21 @@ extern uint32_t __device_busy_end[]; #define DEVICE_BUSY_SIZE (__device_busy_end - __device_busy_start) #endif +static inline void device_pm_state_init(const struct device *dev) +{ +#ifdef CONFIG_PM_DEVICE + *dev->pm = (struct device_pm){ + .usage = ATOMIC_INIT(0), + .lock = Z_SEM_INITIALIZER(dev->pm->lock, 1, 1), + .signal = K_POLL_SIGNAL_INITIALIZER(dev->pm->signal), + .event = K_POLL_EVENT_INITIALIZER( + K_POLL_TYPE_SIGNAL, + K_POLL_MODE_NOTIFY_ONLY, + &dev->pm->signal), + }; +#endif /* CONFIG_PM_DEVICE */ +} + /** * @brief Initialize state for all static devices. * @@ -42,6 +57,7 @@ void z_device_state_init(void) const struct device *dev = __device_start; while (dev < __device_end) { + device_pm_state_init(dev); z_object_init(dev); ++dev; }