From 62cc467acd4f2eb33091c4dec1009e2fdabdc197 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 3 Jan 2024 11:11:24 -0800 Subject: [PATCH] fix(pm): Fix deep sleep with sideband behaviors. * Properly implement the PM hook needed for sideband behavior kscan device to have wakeup source enabled on it. --- .../kscan/zmk,kscan-sideband-behaviors.yaml | 2 +- app/src/kscan.c | 5 ++-- app/src/kscan_sideband_behaviors.c | 29 +++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/dts/bindings/kscan/zmk,kscan-sideband-behaviors.yaml b/app/dts/bindings/kscan/zmk,kscan-sideband-behaviors.yaml index 7289b9e1692..57b54a6041c 100644 --- a/app/dts/bindings/kscan/zmk,kscan-sideband-behaviors.yaml +++ b/app/dts/bindings/kscan/zmk,kscan-sideband-behaviors.yaml @@ -7,7 +7,7 @@ description: | compatible: "zmk,kscan-sideband-behaviors" -include: [kscan.yaml] +include: kscan.yaml properties: kscan: diff --git a/app/src/kscan.c b/app/src/kscan.c index c04ce2d8799..5c7a5535a5b 100644 --- a/app/src/kscan.c +++ b/app/src/kscan.c @@ -74,13 +74,14 @@ int zmk_kscan_init(const struct device *dev) { k_work_init(&msg_processor.work, zmk_kscan_process_msgq); - kscan_config(dev, zmk_kscan_callback); - kscan_enable_callback(dev); #if IS_ENABLED(CONFIG_PM_DEVICE) if (pm_device_wakeup_is_capable(dev)) { pm_device_wakeup_enable(dev, true); } #endif // IS_ENABLED(CONFIG_PM_DEVICE) + kscan_config(dev, zmk_kscan_callback); + kscan_enable_callback(dev); + return 0; } diff --git a/app/src/kscan_sideband_behaviors.c b/app/src/kscan_sideband_behaviors.c index 3a03a293839..cff28e49d18 100644 --- a/app/src/kscan_sideband_behaviors.c +++ b/app/src/kscan_sideband_behaviors.c @@ -75,6 +75,7 @@ void ksbb_inner_kscan_callback(const struct device *dev, uint32_t row, uint32_t } static int ksbb_configure(const struct device *dev, kscan_callback_t callback) { + const struct ksbb_config *cfg = dev->config; struct ksbb_data *data = dev->data; if (!callback) { @@ -82,6 +83,13 @@ static int ksbb_configure(const struct device *dev, kscan_callback_t callback) { } data->callback = callback; + +#if IS_ENABLED(CONFIG_PM_DEVICE) + if (pm_device_wakeup_is_enabled(dev) && pm_device_wakeup_is_capable(cfg->kscan)) { + pm_device_wakeup_enable(cfg->kscan, true); + } +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + return 0; } @@ -119,6 +127,21 @@ static const struct kscan_driver_api ksbb_api = { .disable_callback = ksbb_disable, }; +#if IS_ENABLED(CONFIG_PM_DEVICE) + +static int ksbb_pm_action(const struct device *dev, enum pm_device_action action) { + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + return ksbb_disable(dev); + case PM_DEVICE_ACTION_RESUME: + return ksbb_disable(dev); + default: + return -ENOTSUP; + } +} + +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + #define JUST_ONE(_id) 1 #define ENTRY(e) \ @@ -136,7 +159,9 @@ static const struct kscan_driver_api ksbb_api = { .entries_len = DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(n, JUST_ONE, (+)), \ }; \ struct ksbb_data ksbb_data_##n = {}; \ - DEVICE_DT_INST_DEFINE(n, ksbb_init, NULL, &ksbb_data_##n, &ksbb_config_##n, APPLICATION, \ - CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &ksbb_api); + PM_DEVICE_DT_INST_DEFINE(n, ksbb_pm_action); \ + DEVICE_DT_INST_DEFINE(n, ksbb_init, PM_DEVICE_DT_INST_GET(n), &ksbb_data_##n, \ + &ksbb_config_##n, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &ksbb_api); DT_INST_FOREACH_STATUS_OKAY(KSBB_INST)