Skip to content

Commit b579a90

Browse files
mmahadevan108kartben
authored andcommitted
drivers: power_domain: Add a driver to trigger TURN_ON/TURN_OFF actions
This driver triggers the TURN_ON and TURN_OFF actions for certain power states. These power states are specified via device tree. Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 5ebb04f commit b579a90

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

drivers/power_domain/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ zephyr_library_sources_ifdef(CONFIG_POWER_DOMAIN_GPIO power_domain_gpio.c)
77
zephyr_library_sources_ifdef(CONFIG_POWER_DOMAIN_GPIO_MONITOR power_domain_gpio_monitor.c)
88
zephyr_library_sources_ifdef(CONFIG_POWER_DOMAIN_INTEL_ADSP power_domain_intel_adsp.c)
99
zephyr_library_sources_ifdef(CONFIG_POWER_DOMAIN_NXP_SCU power_domain_nxp_scu.c)
10+
zephyr_library_sources_ifdef(CONFIG_POWER_DOMAIN_SOC_PM_STATE power_domain_soc_state_change.c)

drivers/power_domain/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,13 @@ config POWER_DOMAIN_NXP_SCU_INIT_PRIORITY
9090

9191
endif #POWER_DOMAIN_NXP_SCU
9292

93+
config POWER_DOMAIN_SOC_PM_STATE
94+
bool "SoC PM state power domain"
95+
default y
96+
depends on DT_HAS_POWER_DOMAIN_SOC_STATE_CHANGE_ENABLED
97+
select DEVICE_DEPS
98+
help
99+
Generic power domain control to turn on/off devices when the
100+
PM subsystem transitions in and out certain power states.
101+
93102
endif
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2024-25 NXP
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
This power domain will Turn On and Off devices when transitioning
6+
in and out a specified Power State. Power States that trigger this
7+
action is specified via the onoff-power-states property.
8+
9+
compatible: "power-domain-soc-state-change"
10+
11+
include: power-domain.yaml
12+
13+
properties:
14+
onoff-power-states:
15+
type: phandles
16+
description: |
17+
List of power management states.
18+
The registered devices will be turned off before the PM subsystem
19+
enters these PM states. The devices will be turned on after the
20+
PM subsystem exits these PM states.
21+
22+
"#power-domain-cells":
23+
const: 0

0 commit comments

Comments
 (0)