diff --git a/subsys/pm/policy/policy_device_lock.c b/subsys/pm/policy/policy_device_lock.c index b1bc92b9fd7..8cf4618a9c7 100644 --- a/subsys/pm/policy/policy_device_lock.c +++ b/subsys/pm/policy/policy_device_lock.c @@ -87,38 +87,54 @@ static struct pm_state_device_constraint _devices_constraints[] = { DT_FOREACH_STATUS_OKAY_NODE(PM_STATE_DEVICE_CONSTRAINT_DEFINE) }; -void pm_policy_device_power_lock_get(const struct device *dev) +/* returns device's constraints in _devices_constraints, NULL if not found */ +static struct pm_state_device_constraint * +pm_policy_priv_device_find_device_constraints(const struct device *dev) { #if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state) + if (dev == NULL) { + return NULL; + } + for (size_t i = 0; i < ARRAY_SIZE(_devices_constraints); i++) { const struct device *device = device_get_binding(_devices_constraints[i].dev); - if ((device != NULL) && (device == dev)) { - for (size_t j = 0; j < _devices_constraints[i].pm_constraints_size; j++) { - pm_policy_state_lock_get( - _devices_constraints[i].constraints[j].state, - _devices_constraints[i].constraints[j].substate_id); - } - break; + if (device == dev) { + return &_devices_constraints[i]; } } +#endif + return NULL; +} + +void pm_policy_device_power_lock_get(const struct device *dev) +{ +#if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state) + struct pm_state_device_constraint *constraints = + pm_policy_priv_device_find_device_constraints(dev); + if (constraints == NULL) { + return; + } + + for (size_t j = 0; j < constraints->pm_constraints_size; j++) { + pm_policy_state_lock_get(constraints->constraints[j].state, + constraints->constraints[j].substate_id); + } #endif } void pm_policy_device_power_lock_put(const struct device *dev) { #if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state) - for (size_t i = 0; i < ARRAY_SIZE(_devices_constraints); i++) { - const struct device *device = device_get_binding(_devices_constraints[i].dev); + struct pm_state_device_constraint *constraints = + pm_policy_priv_device_find_device_constraints(dev); + if (constraints == NULL) { + return; + } - if ((device != NULL) && (device == dev)) { - for (size_t j = 0; j < _devices_constraints[i].pm_constraints_size; j++) { - pm_policy_state_lock_put( - _devices_constraints[i].constraints[j].state, - _devices_constraints[i].constraints[j].substate_id); - } - break; - } + for (size_t j = 0; j < constraints->pm_constraints_size; j++) { + pm_policy_state_lock_put(constraints->constraints[j].state, + constraints->constraints[j].substate_id); } #endif }