|
| 1 | +/* |
| 2 | + * Copyright 2024-25 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <zephyr/kernel.h> |
| 7 | +#include <zephyr/kernel_structs.h> |
| 8 | +#include <zephyr/pm/device.h> |
| 9 | +#include <zephyr/pm/device_runtime.h> |
| 10 | +#include <zephyr/pm/pm.h> |
| 11 | + |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | +LOG_MODULE_REGISTER(power_domain_soc_state_change, CONFIG_POWER_DOMAIN_LOG_LEVEL); |
| 14 | + |
| 15 | +/* Indicates the end of the onoff_power_states array */ |
| 16 | +#define POWER_DOMAIN_DEVICE_ONOFF_STATE_MARKER 0xFF |
| 17 | + |
| 18 | +struct pd_deviceonoff_config { |
| 19 | + uint8_t *onoff_power_states; |
| 20 | +}; |
| 21 | + |
| 22 | +struct pd_visitor_context { |
| 23 | + const struct device *domain; |
| 24 | + enum pm_device_action action; |
| 25 | +}; |
| 26 | + |
| 27 | +static int pd_domain_visitor(const struct device *dev, void *context) |
| 28 | +{ |
| 29 | + struct pd_visitor_context *visitor_context = context; |
| 30 | + |
| 31 | + /* Only run action if the device is on the specified domain */ |
| 32 | + if (!dev->pm || (dev->pm_base->domain != visitor_context->domain)) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + |
| 36 | + /* In case device is active, first suspend it before turning it off */ |
| 37 | + if ((visitor_context->action == PM_DEVICE_ACTION_TURN_OFF) && |
| 38 | + (dev->pm_base->state == PM_DEVICE_STATE_ACTIVE)) { |
| 39 | + (void)pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND); |
| 40 | + } |
| 41 | + (void)pm_device_action_run(dev, visitor_context->action); |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +static int pd_pm_action(const struct device *dev, enum pm_device_action action) |
| 46 | +{ |
| 47 | + const struct pd_deviceonoff_config *config = dev->config; |
| 48 | + uint8_t i = 0; |
| 49 | + /* Get the next power state that will be used */ |
| 50 | + enum pm_state state = pm_state_next_get(_current_cpu->id)->state; |
| 51 | + struct pd_visitor_context context = {.domain = dev}; |
| 52 | + |
| 53 | + switch (action) { |
| 54 | + case PM_DEVICE_ACTION_RESUME: |
| 55 | + LOG_DBG("%s: resuming", dev->name); |
| 56 | + while (config->onoff_power_states[i] != POWER_DOMAIN_DEVICE_ONOFF_STATE_MARKER) { |
| 57 | + /* Check if we need do the turn on action for this state */ |
| 58 | + if (state == config->onoff_power_states[i]) { |
| 59 | + /* Notify devices on the domain they are now powered */ |
| 60 | + context.action = PM_DEVICE_ACTION_TURN_ON; |
| 61 | + (void)device_supported_foreach(dev, pd_domain_visitor, &context); |
| 62 | + /* No need to go through the rest of the array of states */ |
| 63 | + break; |
| 64 | + } |
| 65 | + i++; |
| 66 | + } |
| 67 | + break; |
| 68 | + case PM_DEVICE_ACTION_SUSPEND: |
| 69 | + LOG_DBG("%s: suspending", dev->name); |
| 70 | + while (config->onoff_power_states[i] != POWER_DOMAIN_DEVICE_ONOFF_STATE_MARKER) { |
| 71 | + /* Check if need to do the turn off action for this state */ |
| 72 | + if (state == config->onoff_power_states[i]) { |
| 73 | + /* Notify devices on the domain that power is going down */ |
| 74 | + context.action = PM_DEVICE_ACTION_TURN_OFF; |
| 75 | + (void)device_supported_foreach(dev, pd_domain_visitor, &context); |
| 76 | + /* No need to go through the rest of the array of states */ |
| 77 | + break; |
| 78 | + } |
| 79 | + i++; |
| 80 | + } |
| 81 | + break; |
| 82 | + case PM_DEVICE_ACTION_TURN_ON: |
| 83 | + break; |
| 84 | + case PM_DEVICE_ACTION_TURN_OFF: |
| 85 | + break; |
| 86 | + default: |
| 87 | + return -ENOTSUP; |
| 88 | + } |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +#define DT_DRV_COMPAT power_domain_soc_state_change |
| 94 | + |
| 95 | +#define PM_STATE_FROM_DT(i, node_id, prop_name) \ |
| 96 | + COND_CODE_1(DT_NODE_HAS_STATUS(DT_PHANDLE_BY_IDX(node_id, prop_name, i), okay), \ |
| 97 | + (PM_STATE_DT_INIT(DT_PHANDLE_BY_IDX(node_id, prop_name, i)),), ()) |
| 98 | + |
| 99 | +#define POWER_DOMAIN_DEVICE_ONOFF_STATES(id, node_id) \ |
| 100 | + uint8_t onoff_states_##id[] = { \ |
| 101 | + LISTIFY(DT_PROP_LEN_OR(node_id, onoff_power_states, 0), \ |
| 102 | + PM_STATE_FROM_DT, (), node_id, onoff_power_states) \ |
| 103 | + POWER_DOMAIN_DEVICE_ONOFF_STATE_MARKER \ |
| 104 | + }; |
| 105 | + |
| 106 | +#define POWER_DOMAIN_DEVICE(id) \ |
| 107 | + POWER_DOMAIN_DEVICE_ONOFF_STATES(id, DT_DRV_INST(id)) \ |
| 108 | + \ |
| 109 | + static const struct pd_deviceonoff_config pd_deviceonoff_##id##_cfg = { \ |
| 110 | + .onoff_power_states = onoff_states_##id, \ |
| 111 | + }; \ |
| 112 | + PM_DEVICE_DT_INST_DEFINE(id, pd_pm_action); \ |
| 113 | + DEVICE_DT_INST_DEFINE(id, NULL, PM_DEVICE_DT_INST_GET(id), \ |
| 114 | + NULL, &pd_deviceonoff_##id##_cfg, PRE_KERNEL_1, \ |
| 115 | + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); |
| 116 | + |
| 117 | +DT_INST_FOREACH_STATUS_OKAY(POWER_DOMAIN_DEVICE) |
0 commit comments