Skip to content

Commit

Permalink
feat(power): Add power domain config
Browse files Browse the repository at this point in the history
* High level ZMK_POWER_DOMAINS for the feature.
* ZMK_POWER_DOMAINS_DYNAMIC_DEFAULT config setting
   to have various subsystems add their respective devices to
   the chosen `zmk,default-power-domain` on init.
  • Loading branch information
petejohanson committed Oct 21, 2023
1 parent 9964a82 commit 81a7efb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
29 changes: 21 additions & 8 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -323,22 +323,35 @@ config ZMK_IDLE_TIMEOUT
config ZMK_SLEEP
bool "Enable deep sleep support"
imply USB

if ZMK_SLEEP

config PM_DEVICE
default y
select PM_DEVICE

config ZMK_IDLE_SLEEP_TIMEOUT
int "Milliseconds of inactivity before entering deep sleep"
depends on ZMK_SLEEP
default 900000

#ZMK_SLEEP
endif
config ZMK_POWER_DOMAINS
bool "Enable automatic power domain handling"
default n
select PM_DEVICE
select PM_DEVICE_POWER_DOMAIN
select PM_DEVICE_RUNTIME
select POWER_DOMAIN
select POWER_DOMAIN_GPIO

config ZMK_POWER_DOMAINS_DYNAMIC_DEFAULT
bool "Enable assigning peripherals to the chosen default power domain"
default n
depends on ZMK_POWER_DOMAINS
select PM_DEVICE_POWER_DOMAIN_DYNAMIC

# Default this to 2, the most common scenario for this for ZMK is RGB + OLED
config PM_DEVICE_POWER_DOMAIN_DYNAMIC_NUM
default 2

config ZMK_EXT_POWER
bool "Enable support to control external power output"
default y
default y if !ZMK_POWER_DOMAINS

#Power Management
endmenu
Expand Down
14 changes: 14 additions & 0 deletions app/src/display/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/device.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/devicetree.h>

Expand Down Expand Up @@ -118,6 +119,19 @@ void initialize_display(struct k_work *work) {
return;
}

#if IS_ENABLED(CONFIG_ZMK_POWER_DOMAINS) && DT_HAS_CHOSEN(zmk_default_power_domain)

pm_device_runtime_enable(display);
if (!pm_device_on_power_domain(display)) {
int rc =
pm_device_power_domain_add(display, DEVICE_DT_GET(DT_CHOSEN(zmk_default_power_domain)));
if (rc < 0) {
LOG_ERR("Failed to add the display to the default power domain (0x%02x)", -rc);
}
}

#endif

initialized = true;

initialize_theme();
Expand Down
14 changes: 14 additions & 0 deletions app/src/rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/settings/settings.h>

Expand Down Expand Up @@ -250,6 +251,19 @@ static int zmk_rgb_underglow_init(const struct device *_arg) {
}
#endif

#if IS_ENABLED(CONFIG_ZMK_POWER_DOMAINS) && DT_HAS_CHOSEN(zmk_default_power_domain)

pm_device_runtime_enable(led_strip);
if (!pm_device_on_power_domain(led_strip)) {
int rc = pm_device_power_domain_add(led_strip,
DEVICE_DT_GET(DT_CHOSEN(zmk_default_power_domain)));
if (rc < 0) {
LOG_ERR("Failed to add the LED strip to the default power domain (0x%02x)", -rc);
}
}

#endif

state = (struct rgb_underglow_state){
color : {
h : CONFIG_ZMK_RGB_UNDERGLOW_HUE_START,
Expand Down

0 comments on commit 81a7efb

Please sign in to comment.