[PATCH] PM: Move to_device() out of CONFIG_PM_SLEEP protection

From: Pengpeng Hou

Date: Mon Mar 02 2026 - 06:00:04 EST


The helper function to_device() is used to convert a list_head structure
(specifically from dev->power.entry) back to the corresponding struct
device. Currently, this function is only available when CONFIG_PM_SLEEP
is enabled. However, some generic power management code may need to iterate
through the device list even if sleep states (suspend/hibernate) are not
supported.

There are currently two approaches for moving the function.The first one:
judging from the current conditions where the function is called, all calls
are not wrapped by any macros, so it is appropriate to move the function
definition to the global scope.The second one: if the compilation of the
files where the function is called is controlled by CONFIG_PM, it is more
appropriate to move the function definition into CONFIG_PM.

To ensure that the availability of the function is consistent with its
usage scenarios and to avoid undefined reference compilation errors, the
first approach is adopted.

Signed-off-by: Pengpeng Hou <pengpeng.hou@xxxxxxxxxxxxxxxx>
---
drivers/base/power/power.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index 922ed457d..0a40bb546 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -106,10 +106,6 @@ extern int pm_async_enabled;
/* drivers/base/power/main.c */
extern struct list_head dpm_list; /* The active device list */

-static inline struct device *to_device(struct list_head *entry)
-{
- return container_of(entry, struct device, power.entry);
-}

extern void device_pm_sleep_init(struct device *dev);
extern void device_pm_add(struct device *);
@@ -162,6 +158,11 @@ static inline int pm_wakeup_source_sysfs_add(struct device *parent)

#endif /* !CONFIG_PM_SLEEP */

+static inline struct device *to_device(struct list_head *entry)
+{
+ return container_of(entry, struct device, power.entry);
+}
+
static inline void device_pm_init(struct device *dev)
{
device_pm_init_common(dev);
--
2.25.1