Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add required changes for zmk power domain support #8

Open
wants to merge 7 commits into
base: v3.0.0+zmk-fixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,12 @@ zephyr_get_include_directories_for_lang(C
STRIP_PREFIX # Don't use a -I prefix
)

if(CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC)
set(number_of_dynamic_devices ${CONFIG_PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM})
else()
set(number_of_dynamic_devices 0)
endif()

if(CONFIG_HAS_DTS)
# dev_handles.c is generated from ${ZEPHYR_LINK_STAGE_EXECUTABLE} by
# gen_handles.py
Expand All @@ -810,6 +816,7 @@ if(CONFIG_HAS_DTS)
${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/gen_handles.py
--output-source dev_handles.c
--num-dynamic-devices ${number_of_dynamic_devices}
--kernel $<TARGET_FILE:${ZEPHYR_LINK_STAGE_EXECUTABLE}>
--zephyr-base ${ZEPHYR_BASE}
--start-symbol "$<TARGET_PROPERTY:linker,devices_start_symbol>"
Expand Down
31 changes: 30 additions & 1 deletion drivers/display/ssd1306.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LOG_MODULE_REGISTER(ssd1306, CONFIG_DISPLAY_LOG_LEVEL);
#include <drivers/gpio.h>
#include <drivers/i2c.h>
#include <drivers/spi.h>
#include <pm/device.h>

#include "ssd1306_regs.h"
#include <display/cfb.h>
Expand Down Expand Up @@ -425,6 +426,34 @@ static int ssd1306_init(const struct device *dev)
return 0;
}

#ifdef CONFIG_PM_DEVICE
static int ssd1306_pm_action(const struct device *dev,
enum pm_device_action action)
{
int ret = 0;

switch (action) {
case PM_DEVICE_ACTION_RESUME:
LOG_DBG("Initializing display...");
ssd1306_init(dev);

break;

case PM_DEVICE_ACTION_TURN_OFF:
// We don't need to do anything when turning off, but we also
// don't want to return -ENOTSUP
// So we keep this empty on purpose.
break;

default:
ret = -ENOTSUP;
}

return ret;
}
PM_DEVICE_DT_INST_DEFINE(0, ssd1306_pm_action);
#endif /* CONFIG_PM_DEVICE */

static const struct ssd1306_config ssd1306_config = {
#if DT_INST_ON_BUS(0, i2c)
.bus = I2C_DT_SPEC_INST_GET(0),
Expand All @@ -451,7 +480,7 @@ static struct display_driver_api ssd1306_driver_api = {
.set_orientation = ssd1306_set_orientation,
};

DEVICE_DT_INST_DEFINE(0, ssd1306_init, NULL,
DEVICE_DT_INST_DEFINE(0, ssd1306_init, PM_DEVICE_DT_INST_GET(0),
&ssd1306_driver, &ssd1306_config,
POST_KERNEL, CONFIG_DISPLAY_INIT_PRIORITY,
&ssd1306_driver_api);
8 changes: 8 additions & 0 deletions drivers/i2c/i2c_nrfx_twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,18 @@ static int twi_nrfx_pm_action(const struct device *dev,

switch (action) {
case PM_DEVICE_ACTION_RESUME:
LOG_DBG("Initializing i2c-twi...");
init_twi(dev);
if (data->dev_config) {
i2c_nrfx_twi_configure(dev, data->dev_config);
}

break;

case PM_DEVICE_ACTION_TURN_OFF:
// We don't want to uninit as it causes issues on re-init
// But we also don't want to return -ENOTSUP
// So we keep this empty.
break;

case PM_DEVICE_ACTION_SUSPEND:
Expand Down
51 changes: 45 additions & 6 deletions drivers/power_domain/power_domain_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <pm/device_runtime.h>

#include <logging/log.h>
LOG_MODULE_REGISTER(power_domain_gpio, LOG_LEVEL_INF);
LOG_MODULE_REGISTER(power_domain_gpio, CONFIG_PM_DEVICE_LOG_LEVEL);

struct pd_gpio_config {
struct gpio_dt_spec enable;
Expand All @@ -36,33 +36,64 @@ const char *actions[] = {
static int pd_gpio_pm_action(const struct device *dev,
enum pm_device_action action)
{
LOG_DBG("In pd_gpio_pm_action for power domain %s with action: %s", dev->name, pm_device_action_str(action));

const struct pd_gpio_config *cfg = dev->config;
int rc = 0;

switch (action) {
case PM_DEVICE_ACTION_RESUME:
/* Switch power on */
gpio_pin_set_dt(&cfg->enable, 1);

if(cfg->startup_delay_us > 0) {
k_sleep(K_USEC(cfg->startup_delay_us));
}

LOG_DBG("%s is now ON", dev->name);

/* Notify supported devices they are now powered */
pm_device_children_action_run(dev, PM_DEVICE_ACTION_TURN_ON, NULL);
pm_device_children_action_run(dev, action, NULL);
break;
case PM_DEVICE_ACTION_SUSPEND:
/* Notify supported devices power is going down */
pm_device_children_action_run(dev, PM_DEVICE_ACTION_TURN_OFF, NULL);
pm_device_children_action_run(dev, action, NULL);

if(cfg->off_on_delay_us > 0) {
k_sleep(K_USEC(cfg->off_on_delay_us));
}

/* Switch power off */
gpio_pin_set_dt(&cfg->enable, 0);
LOG_DBG("%s is now OFF and powered", dev->name);
break;
case PM_DEVICE_ACTION_TURN_ON:
/* Actively control the enable pin now that the device is powered */
gpio_pin_configure_dt(&cfg->enable, GPIO_OUTPUT_INACTIVE);
gpio_pin_set_dt(&cfg->enable, 1);

if(cfg->startup_delay_us > 0) {
k_sleep(K_USEC(cfg->startup_delay_us));
}

LOG_DBG("%s is OFF and powered", dev->name);

/* Notify supported devices they are now powered */
pm_device_children_action_run(dev, action, NULL);

break;
case PM_DEVICE_ACTION_TURN_OFF:
/* Notify supported devices power is going down */
pm_device_children_action_run(dev, action, NULL);

if(cfg->off_on_delay_us > 0) {
k_sleep(K_USEC(cfg->off_on_delay_us));
}

/* Let the enable pin float while device is not powered */
gpio_pin_configure_dt(&cfg->enable, GPIO_DISCONNECTED);
gpio_pin_set_dt(&cfg->enable, 0);

LOG_DBG("%s is OFF and not powered", dev->name);

break;
default:
rc = -ENOTSUP;
Expand All @@ -73,6 +104,8 @@ static int pd_gpio_pm_action(const struct device *dev,

static int pd_gpio_init(const struct device *dev)
{
LOG_DBG("Initing power-domain-gpio: %s", dev->name);

const struct pd_gpio_config *cfg = dev->config;
int rc;

Expand All @@ -85,12 +118,18 @@ static int pd_gpio_init(const struct device *dev)
/* Device is unpowered */
pm_device_runtime_init_off(dev);
rc = gpio_pin_configure_dt(&cfg->enable, GPIO_DISCONNECTED);
if(rc != 0) {
LOG_WRN("Could not configure pin to GPIO_DISCONNECTED: %d", rc);
}
} else {
pm_device_runtime_init_suspended(dev);
rc = gpio_pin_configure_dt(&cfg->enable, GPIO_OUTPUT_INACTIVE);
if(rc != 0) {
LOG_WRN("Could not configure pin to GPIO_OUTPUT_INACTIVE: %d", rc);
}
}

return rc;
return 0;
}

#define POWER_DOMAIN_DEVICE(id) \
Expand Down
13 changes: 10 additions & 3 deletions include/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ struct device_state {

struct pm_device;

#ifdef CONFIG_HAS_DYNAMIC_DEVICE_HANDLES
#define Z_DEVICE_HANDLES_CONST
#else
#define Z_DEVICE_HANDLES_CONST const
#endif

/**
* @brief Runtime device structure (in ROM) per driver instance
*/
Expand All @@ -465,7 +471,8 @@ struct device {
* extracted with dedicated API, such as
* device_required_handles_get().
*/
const device_handle_t *const handles;
Z_DEVICE_HANDLES_CONST device_handle_t * const handles;

#ifdef CONFIG_PM_DEVICE
/** Reference to the device PM resources. */
struct pm_device * const pm;
Expand Down Expand Up @@ -877,9 +884,9 @@ __deprecated static inline int device_usable_check(const struct device *dev)
*/
BUILD_ASSERT(sizeof(device_handle_t) == 2, "fix the linker scripts");
#define Z_DEVICE_DEFINE_HANDLES(node_id, dev_name, ...) \
extern const device_handle_t \
extern Z_DEVICE_HANDLES_CONST device_handle_t \
Z_DEVICE_HANDLE_NAME(node_id, dev_name)[]; \
const device_handle_t \
Z_DEVICE_HANDLES_CONST device_handle_t \
__aligned(sizeof(device_handle_t)) \
__attribute__((__weak__, \
__section__(".__device_handles_pass1"))) \
Expand Down
13 changes: 13 additions & 0 deletions include/linker/common-ram.ld
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
#endif

#if defined(CONFIG_HAS_DYNAMIC_DEVICE_HANDLES)
SECTION_DATA_PROLOGUE(device_handles,,)
{
__device_handles_start = .;
#ifdef LINKER_DEVICE_HANDLES_PASS1
KEEP(*(SORT(.__device_handles_pass1*)));
#else
KEEP(*(SORT(.__device_handles_pass2*)));
#endif /* LINKER_DEVICE_HANDLES_PASS1 */
__device_handles_end = .;
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
#endif /* CONFIG_HAS_DYNAMIC_DEVICE_HANDLES */

SECTION_DATA_PROLOGUE(initshell,,)
{
/* link in shell initialization objects for all modules that
Expand Down
2 changes: 2 additions & 0 deletions include/linker/common-rom.ld
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
KEEP(*(".dbg_thread_info"));
} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

#if !defined(CONFIG_HAS_DYNAMIC_DEVICE_HANDLES)
SECTION_DATA_PROLOGUE(device_handles,,)
{
__device_handles_start = .;
Expand All @@ -226,3 +227,4 @@
#endif /* LINKER_DEVICE_HANDLES_PASS1 */
__device_handles_end = .;
} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
#endif /* !CONFIG_HAS_DYNAMIC_DEVICE_HANDLES */
50 changes: 50 additions & 0 deletions include/pm/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ struct pm_device {
*/
const char *pm_device_state_str(enum pm_device_state state);

/**
* @brief Get name of device PM action
*
* @param action Action id which name should be returned
*/
const char *pm_device_action_str(enum pm_device_action action);

/**
* @brief Set the power state of a device.
*
Expand Down Expand Up @@ -507,6 +514,36 @@ bool pm_device_state_is_locked(const struct device *dev);
*/
bool pm_device_on_power_domain(const struct device *dev);

/**
* @brief Add a device to a power domain.
*
* This function adds a device to a given power domain.
*
* @param dev Device to be added to the power domain.
* @param domain Power domain.
*
* @retval 0 If successful.
* @retval -EALREADY If device is already part of the power domain.
* @retval -ENOSYS If the application was built without power domain support.
* @retval -ENOSPC If there is no space available in the power domain to add the device.
*/
int pm_device_power_domain_add(const struct device *dev,
const struct device *domain);

/**
* @brief Remove a device from a power domain.
*
* This function removes a device from a given power domain.
*
* @param dev Device to be removed from the power domain.
* @param domain Power domain.
*
* @retval 0 If successful.
* @retval -ENOSYS If the application was built without power domain support.
* @retval -ENOENT If device is not in the given domain.
*/
int pm_device_power_domain_remove(const struct device *dev,
const struct device *domain);
#else
static inline void pm_device_busy_set(const struct device *dev)
{
Expand Down Expand Up @@ -556,6 +593,19 @@ static inline bool pm_device_on_power_domain(const struct device *dev)
ARG_UNUSED(dev);
return false;
}

static inline int pm_device_power_domain_add(const struct device *dev,
const struct device *domain)
{
return -ENOSYS;
}

static inline int pm_device_power_domain_remove(const struct device *dev,
const struct device *domain)
{
return -ENOSYS;
}

#endif /* CONFIG_PM_DEVICE */

/**
Expand Down
10 changes: 10 additions & 0 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,14 @@ config THREAD_LOCAL_STORAGE

endmenu

menu "Device Options"

config HAS_DYNAMIC_DEVICE_HANDLES
bool
help
Hidden option that makes possible to manipulate device handles at
runtime.

endmenu

rsource "Kconfig.vm"
4 changes: 4 additions & 0 deletions scripts/gen_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def parse_args():

parser.add_argument("-k", "--kernel", required=True,
help="Input zephyr ELF binary")
parser.add_argument("-d", "--num-dynamic-devices", required=False, default=0,
type=int, help="Input number of dynamic devices allowed")
parser.add_argument("-o", "--output-source", required=True,
help="Output source file")

Expand Down Expand Up @@ -112,6 +114,7 @@ def symbol_handle_data(elf, sym):
# These match the corresponding constants in <device.h>
DEVICE_HANDLE_SEP = -32768
DEVICE_HANDLE_ENDS = 32767
DEVICE_HANDLE_NULL = 0
def handle_name(hdl):
if hdl == DEVICE_HANDLE_SEP:
return "DEVICE_HANDLE_SEP"
Expand Down Expand Up @@ -336,6 +339,7 @@ def main():
else:
sup_paths.append('(%s)' % dn.path)
hdls.extend(dn.__device.dev_handle for dn in sn.__supports)
hdls.extend(DEVICE_HANDLE_NULL for dn in range(args.num_dynamic_devices))

# Terminate the array with the end symbol
hdls.append(DEVICE_HANDLE_ENDS)
Expand Down
14 changes: 14 additions & 0 deletions subsys/pm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ config PM_DEVICE_POWER_DOMAIN
devices that depend on a domain will be notified when this
domain is suspended or resumed.

config PM_DEVICE_POWER_DOMAIN_DYNAMIC
bool "Dynamically bind devices to a Power Pomain"
depends on PM_DEVICE_POWER_DOMAIN
select HAS_DYNAMIC_DEVICE_HANDLES
help
Enable support for dynamically bind devices to a Power Domain.

config PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM
int "Number of devices that can dynamically be bind to a Power Domain"
depends on PM_DEVICE_POWER_DOMAIN_DYNAMIC
default 1
help
The number of devices that can dynamically be bind to a Power Domain.

config PM_DEVICE_RUNTIME
bool "Runtime Device Power Management"
help
Expand Down
Loading