aboutsummaryrefslogtreecommitdiffhomepage
path: root/app
diff options
context:
space:
mode:
authorPeter Johanson <[email protected]>2024-02-19 02:18:23 -0800
committerPete Johanson <[email protected]>2024-03-27 20:59:26 -0700
commit8d54e287f019baa44540c8d8120eac594a630b03 (patch)
tree929c03126c4b43c30954166681f81c79dc2ea234 /app
parentbd21f41412eaff5822a17efa86ce1dadf831cbcc (diff)
downloadzmk-8d54e287f019baa44540c8d8120eac594a630b03.tar.gz
zmk-8d54e287f019baa44540c8d8120eac594a630b03.zip
fix: Adjustments for Zephyr 3.5.
Diffstat (limited to 'app')
-rw-r--r--app/Kconfig6
-rw-r--r--app/include/zmk/pm.h3
-rw-r--r--app/src/activity.c61
-rw-r--r--app/src/behaviors/behavior_soft_off.c2
-rw-r--r--app/src/pm.c68
5 files changed, 79 insertions, 61 deletions
diff --git a/app/Kconfig b/app/Kconfig
index 28d3c22ac5..5068e91fdf 100644
--- a/app/Kconfig
+++ b/app/Kconfig
@@ -405,6 +405,7 @@ config ZMK_SLEEP
bool "Enable deep sleep support"
depends on HAS_POWEROFF
select POWEROFF
+ select ZMK_PM_DEVICE_SUSPEND_RESUME
imply USB
if ZMK_SLEEP
@@ -426,10 +427,15 @@ config ZMK_EXT_POWER
config ZMK_PM
bool
+config ZMK_PM_DEVICE_SUSPEND_RESUME
+ bool
+ select ZMK_PM
+
config ZMK_PM_SOFT_OFF
bool "Soft-off support"
select ZMK_PM
select PM_DEVICE
+ select ZMK_PM_DEVICE_SUSPEND_RESUME
config ZMK_GPIO_KEY_WAKEUP_TRIGGER
bool "Hardware supported wakeup (GPIO)"
diff --git a/app/include/zmk/pm.h b/app/include/zmk/pm.h
index dff217afd2..a733856d09 100644
--- a/app/include/zmk/pm.h
+++ b/app/include/zmk/pm.h
@@ -6,4 +6,7 @@
#pragma once
+int zmk_pm_suspend_devices(void);
+void zmk_pm_resume_devices(void);
+
int zmk_pm_soft_off(void); \ No newline at end of file
diff --git a/app/src/activity.c b/app/src/activity.c
index 8f421f85d0..454e91e5da 100644
--- a/app/src/activity.c
+++ b/app/src/activity.c
@@ -7,8 +7,6 @@
#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/sys/poweroff.h>
#include <zephyr/logging/log.h>
@@ -20,69 +18,14 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/events/position_state_changed.h>
#include <zmk/events/sensor_event.h>
+#include <zmk/pm.h>
+
#include <zmk/activity.h>
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
#include <zmk/usb.h>
#endif
-// Reimplement some of the device work from Zephyr PM to work with the new `sys_poweroff` API.
-// TODO: Tweak this to smarter runtime PM of subsystems on sleep.
-
-#ifdef CONFIG_PM_DEVICE
-TYPE_SECTION_START_EXTERN(const struct device *, zmk_pm_device_slots);
-
-#if !defined(CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE)
-/* Number of devices successfully suspended. */
-static size_t zmk_num_susp;
-
-static int zmk_pm_suspend_devices(void) {
- const struct device *devs;
- size_t devc;
-
- devc = z_device_get_all_static(&devs);
-
- zmk_num_susp = 0;
-
- for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) {
- int ret;
-
- /*
- * Ignore uninitialized devices, busy devices, wake up sources, and
- * devices with runtime PM enabled.
- */
- if (!device_is_ready(dev) || pm_device_is_busy(dev) || pm_device_state_is_locked(dev) ||
- pm_device_wakeup_is_enabled(dev) || pm_device_runtime_is_enabled(dev)) {
- continue;
- }
-
- ret = pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND);
- /* ignore devices not supporting or already at the given state */
- if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
- continue;
- } else if (ret < 0) {
- LOG_ERR("Device %s did not enter %s state (%d)", dev->name,
- pm_device_state_str(PM_DEVICE_STATE_SUSPENDED), ret);
- return ret;
- }
-
- TYPE_SECTION_START(zmk_pm_device_slots)[zmk_num_susp] = dev;
- zmk_num_susp++;
- }
-
- return 0;
-}
-
-static void zmk_pm_resume_devices(void) {
- for (int i = (zmk_num_susp - 1); i >= 0; i--) {
- pm_device_action_run(TYPE_SECTION_START(zmk_pm_device_slots)[i], PM_DEVICE_ACTION_RESUME);
- }
-
- zmk_num_susp = 0;
-}
-#endif /* !CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE */
-#endif /* CONFIG_PM_DEVICE */
-
bool is_usb_power_present(void) {
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
return zmk_usb_is_powered();
diff --git a/app/src/behaviors/behavior_soft_off.c b/app/src/behaviors/behavior_soft_off.c
index e6096bb484..878a2fc586 100644
--- a/app/src/behaviors/behavior_soft_off.c
+++ b/app/src/behaviors/behavior_soft_off.c
@@ -64,7 +64,7 @@ static const struct behavior_driver_api behavior_soft_off_driver_api = {
}; \
static struct behavior_soft_off_data bso_data_##n = {}; \
BEHAVIOR_DT_INST_DEFINE(0, behavior_soft_off_init, NULL, &bso_data_##n, &bso_config_##n, \
- APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
+ POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
&behavior_soft_off_driver_api);
DT_INST_FOREACH_STATUS_OKAY(BSO_INST)
diff --git a/app/src/pm.c b/app/src/pm.c
index 8525046f04..4f151875ba 100644
--- a/app/src/pm.c
+++ b/app/src/pm.c
@@ -8,13 +8,72 @@
#include <zephyr/devicetree.h>
#include <zephyr/init.h>
#include <zephyr/pm/device.h>
+#include <zephyr/pm/device_runtime.h>
#include <zephyr/pm/pm.h>
+#include <zephyr/sys/poweroff.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/endpoints.h>
+// Reimplement some of the device work from Zephyr PM to work with the new `sys_poweroff` API.
+// TODO: Tweak this to smarter runtime PM of subsystems on sleep.
+
+#ifdef CONFIG_ZMK_PM_DEVICE_SUSPEND_RESUME
+TYPE_SECTION_START_EXTERN(const struct device *, zmk_pm_device_slots);
+
+#if !defined(CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE)
+/* Number of devices successfully suspended. */
+static size_t zmk_num_susp;
+
+int zmk_pm_suspend_devices(void) {
+ const struct device *devs;
+ size_t devc;
+
+ devc = z_device_get_all_static(&devs);
+
+ zmk_num_susp = 0;
+
+ for (const struct device *dev = devs + devc - 1; dev >= devs; dev--) {
+ int ret;
+
+ /*
+ * Ignore uninitialized devices, busy devices, wake up sources, and
+ * devices with runtime PM enabled.
+ */
+ if (!device_is_ready(dev) || pm_device_is_busy(dev) || pm_device_state_is_locked(dev) ||
+ pm_device_wakeup_is_enabled(dev) || pm_device_runtime_is_enabled(dev)) {
+ continue;
+ }
+
+ ret = pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND);
+ /* ignore devices not supporting or already at the given state */
+ if ((ret == -ENOSYS) || (ret == -ENOTSUP) || (ret == -EALREADY)) {
+ continue;
+ } else if (ret < 0) {
+ LOG_ERR("Device %s did not enter %s state (%d)", dev->name,
+ pm_device_state_str(PM_DEVICE_STATE_SUSPENDED), ret);
+ return ret;
+ }
+
+ TYPE_SECTION_START(zmk_pm_device_slots)[zmk_num_susp] = dev;
+ zmk_num_susp++;
+ }
+
+ return 0;
+}
+
+void zmk_pm_resume_devices(void) {
+ for (int i = (zmk_num_susp - 1); i >= 0; i--) {
+ pm_device_action_run(TYPE_SECTION_START(zmk_pm_device_slots)[i], PM_DEVICE_ACTION_RESUME);
+ }
+
+ zmk_num_susp = 0;
+}
+#endif /* !CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE */
+#endif /* CONFIG_ZMK_PM_DEVICE_SUSPEND_RESUME */
+
#if IS_ENABLED(CONFIG_ZMK_PM_SOFT_OFF)
#define HAS_WAKERS DT_HAS_COMPAT_STATUS_OKAY(zmk_soft_off_wakeup_sources)
@@ -64,8 +123,15 @@ int zmk_pm_soft_off(void) {
}
#endif // HAS_WAKERS
+ int err = zmk_pm_suspend_devices();
+ if (err < 0) {
+ zmk_pm_resume_devices();
+ return err;
+ }
+
LOG_DBG("soft-off: go to sleep");
- return pm_state_force(0U, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});
+ sys_poweroff();
+ return 0;
}
#endif // IS_ENABLED(CONFIG_ZMK_PM_SOFT_OFF) \ No newline at end of file