Skip to content

Commit 7e7b222

Browse files
gmarullcfriedt
authored andcommitted
pm: device: simplify suspend checks
The shared _pm_devices function used should_suspend check function to see if a device had to be suspended or not. Some of the logic inside that function was redundant since the pm_device_state_set function already performs similar checks, e.g. if the device is already at the given state or the state transition is not supported it will return error codes appropriately. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent f93778f commit 7e7b222

File tree

1 file changed

+19
-50
lines changed

1 file changed

+19
-50
lines changed

subsys/pm/device.c

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,31 @@ extern const struct device *__pm_device_slots_start[];
2323
/* Number of devices successfully suspended. */
2424
static size_t num_susp;
2525

26-
static bool should_suspend(const struct device *dev, enum pm_device_state state)
27-
{
28-
int rc;
29-
enum pm_device_state current_state;
30-
31-
if (pm_device_is_busy(dev) != 0) {
32-
return false;
33-
}
34-
35-
rc = pm_device_state_get(dev, &current_state);
36-
if ((rc == -ENOSYS) || (rc != 0)) {
37-
LOG_DBG("Was not possible to get device %s state: %d",
38-
dev->name, rc);
39-
return false;
40-
}
41-
42-
/*
43-
* If the device is currently powered off or the request was
44-
* to go to the same state, just ignore it.
45-
*/
46-
if ((current_state == PM_DEVICE_STATE_OFF) ||
47-
(current_state == state)) {
48-
return false;
49-
}
50-
51-
return true;
52-
}
53-
54-
static int _pm_devices(uint32_t state)
26+
static int _pm_devices(enum pm_device_state state)
5527
{
5628
const struct device *dev;
5729
num_susp = 0;
5830

5931
for (dev = (__device_end - 1); dev > __device_start; dev--) {
60-
bool suspend;
61-
int rc;
62-
63-
suspend = should_suspend(dev, state);
64-
if (suspend) {
65-
/*
66-
* Don't bother the device if it is currently
67-
* in the right state.
68-
*/
69-
rc = pm_device_state_set(dev, state);
70-
if (rc == -ENOTSUP) {
71-
continue;
72-
} else if ((rc != -ENOSYS) && (rc != 0)) {
73-
LOG_DBG("%s did not enter %s state: %d",
74-
dev->name, pm_device_state_str(state),
75-
rc);
76-
return rc;
77-
}
78-
79-
__pm_device_slots_start[num_susp] = dev;
80-
num_susp++;
32+
int ret;
33+
34+
/* ignore busy devices */
35+
if (pm_device_is_busy(dev)) {
36+
continue;
8137
}
38+
39+
ret = pm_device_state_set(dev, state);
40+
/* ignore devices not supporting or already at the given state */
41+
if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
42+
continue;
43+
} else if (ret < 0) {
44+
LOG_ERR("Device %s did not enter %s state (%d)",
45+
dev->name, pm_device_state_str(state), ret);
46+
return ret;
47+
}
48+
49+
__pm_device_slots_start[num_susp] = dev;
50+
num_susp++;
8251
}
8352

8453
return 0;

0 commit comments

Comments
 (0)