|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#include <device.h> |
7 | 8 | #include <zephyr.h> |
8 | 9 | #include <kernel.h> |
9 | 10 | #include <timeout_q.h> |
10 | 11 | #include <init.h> |
11 | 12 | #include <string.h> |
| 13 | +#include <pm/device.h> |
12 | 14 | #include <pm/pm.h> |
13 | 15 | #include <pm/state.h> |
14 | 16 | #include <pm/policy.h> |
15 | 17 | #include <tracing/tracing.h> |
16 | 18 |
|
17 | | -#include "pm_priv.h" |
18 | | - |
19 | 19 | #define PM_STATES_LEN (1 + PM_STATE_SOFT_OFF - PM_STATE_ACTIVE) |
20 | 20 | #include <logging/log.h> |
21 | 21 | LOG_MODULE_REGISTER(pm, CONFIG_PM_LOG_LEVEL); |
@@ -99,6 +99,61 @@ static inline void pm_stop_timer(void) {} |
99 | 99 | static void pm_stats_update(enum pm_state state) {} |
100 | 100 | #endif |
101 | 101 |
|
| 102 | +#ifdef CONFIG_PM_DEVICE |
| 103 | +extern const struct device *__pm_device_slots_start[]; |
| 104 | + |
| 105 | +/* Number of devices successfully suspended. */ |
| 106 | +static size_t num_susp; |
| 107 | + |
| 108 | +static int pm_suspend_devices(void) |
| 109 | +{ |
| 110 | + const struct device *devs; |
| 111 | + size_t devc; |
| 112 | + |
| 113 | + devc = z_device_get_all_static(&devs); |
| 114 | + |
| 115 | + num_susp = 0; |
| 116 | + |
| 117 | + for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) { |
| 118 | + int ret; |
| 119 | + |
| 120 | + /* ignore busy devices */ |
| 121 | + if (pm_device_is_busy(dev) || pm_device_wakeup_is_enabled(dev)) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + ret = pm_device_state_set(dev, PM_DEVICE_STATE_SUSPENDED); |
| 126 | + /* ignore devices not supporting or already at the given state */ |
| 127 | + if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) { |
| 128 | + continue; |
| 129 | + } else if (ret < 0) { |
| 130 | + LOG_ERR("Device %s did not enter %s state (%d)", |
| 131 | + dev->name, |
| 132 | + pm_device_state_str(PM_DEVICE_STATE_SUSPENDED), |
| 133 | + ret); |
| 134 | + return ret; |
| 135 | + } |
| 136 | + |
| 137 | + __pm_device_slots_start[num_susp] = dev; |
| 138 | + num_susp++; |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
| 143 | + |
| 144 | +static void pm_resume_devices(void) |
| 145 | +{ |
| 146 | + size_t i; |
| 147 | + |
| 148 | + for (i = 0; i < num_susp; i++) { |
| 149 | + pm_device_state_set(__pm_device_slots_start[i], |
| 150 | + PM_DEVICE_STATE_ACTIVE); |
| 151 | + } |
| 152 | + |
| 153 | + num_susp = 0; |
| 154 | +} |
| 155 | +#endif /* CONFIG_PM_DEVICE */ |
| 156 | + |
102 | 157 | static inline void exit_pos_ops(struct pm_state_info info) |
103 | 158 | { |
104 | 159 | extern __weak void |
|
0 commit comments