pm: refactor policy_device_ get/put

Refactor this code so it has less nesting and is able to share some code
between functions, by making a function to find the device constraints
object which can be called before doing operations using it.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
This commit is contained in:
Declan Snyder 2025-05-09 16:16:05 -05:00 committed by Benjamin Cabé
commit 65ebecbf78

View file

@ -87,38 +87,54 @@ static struct pm_state_device_constraint _devices_constraints[] = {
DT_FOREACH_STATUS_OKAY_NODE(PM_STATE_DEVICE_CONSTRAINT_DEFINE) 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 DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state)
if (dev == NULL) {
return NULL;
}
for (size_t i = 0; i < ARRAY_SIZE(_devices_constraints); i++) { for (size_t i = 0; i < ARRAY_SIZE(_devices_constraints); i++) {
const struct device *device = device_get_binding(_devices_constraints[i].dev); const struct device *device = device_get_binding(_devices_constraints[i].dev);
if ((device != NULL) && (device == dev)) { if (device == dev) {
for (size_t j = 0; j < _devices_constraints[i].pm_constraints_size; j++) { return &_devices_constraints[i];
pm_policy_state_lock_get(
_devices_constraints[i].constraints[j].state,
_devices_constraints[i].constraints[j].substate_id);
}
break;
} }
} }
#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 #endif
} }
void pm_policy_device_power_lock_put(const struct device *dev) void pm_policy_device_power_lock_put(const struct device *dev)
{ {
#if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state) #if DT_HAS_COMPAT_STATUS_OKAY(zephyr_power_state)
for (size_t i = 0; i < ARRAY_SIZE(_devices_constraints); i++) { struct pm_state_device_constraint *constraints =
const struct device *device = device_get_binding(_devices_constraints[i].dev); pm_policy_priv_device_find_device_constraints(dev);
if (constraints == NULL) {
return;
}
if ((device != NULL) && (device == dev)) { for (size_t j = 0; j < constraints->pm_constraints_size; j++) {
for (size_t j = 0; j < _devices_constraints[i].pm_constraints_size; j++) { pm_policy_state_lock_put(constraints->constraints[j].state,
pm_policy_state_lock_put( constraints->constraints[j].substate_id);
_devices_constraints[i].constraints[j].state,
_devices_constraints[i].constraints[j].substate_id);
}
break;
}
} }
#endif #endif
} }