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

drivers: sensor: nrf: qdec: Add device runtime power management #2087

Merged
merged 4 commits into from
Oct 22, 2024
Merged
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
87 changes: 35 additions & 52 deletions drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,81 +192,64 @@
.trigger_set = qdec_nrfx_trigger_set,
};

#ifdef CONFIG_PM_DEVICE
static int qdec_nrfx_pm_action(const struct device *dev,
enum pm_device_action action)
static void qdec_pm_suspend(const struct device *dev)
{
const struct qdec_nrfx_config *config = dev->config;
int ret = 0;

nrfx_qdec_disable(&config->qdec);
qdec_nrfx_gpio_ctrl(dev, false);

(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
}

static void qdec_pm_resume(const struct device *dev)
{
const struct qdec_nrfx_config *config = dev->config;

(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
qdec_nrfx_gpio_ctrl(dev, true);
nrfx_qdec_enable(&config->qdec);
}

static int qdec_nrfx_pm_action(const struct device *dev, enum pm_device_action action)
{
switch (action) {
case PM_DEVICE_ACTION_RESUME:
ret = pinctrl_apply_state(config->pcfg,
PINCTRL_STATE_DEFAULT);
if (ret < 0) {
return ret;
}
qdec_nrfx_gpio_ctrl(dev, true);
nrfx_qdec_enable(&config->qdec);
break;

case PM_DEVICE_ACTION_TURN_OFF:
/* device must be uninitialized */
nrfx_qdec_uninit(&config->qdec);
ret = pinctrl_apply_state(config->pcfg,
PINCTRL_STATE_SLEEP);
if (ret < 0) {
return ret;
}
qdec_pm_resume(dev);
break;

case PM_DEVICE_ACTION_SUSPEND:
/* device must be suspended */
nrfx_qdec_disable(&config->qdec);
qdec_nrfx_gpio_ctrl(dev, false);
ret = pinctrl_apply_state(config->pcfg,
PINCTRL_STATE_SLEEP);
if (ret < 0) {
return ret;
if (IS_ENABLED(CONFIG_PM_DEVICE)) {
qdec_pm_suspend(dev);
}
break;
default:
return -ENOTSUP;
break;
}

return ret;
return 0;
}
#endif /* CONFIG_PM_DEVICE */

static int qdec_nrfx_init(const struct device *dev)
{
const struct qdec_nrfx_config *dev_config = dev->config;

dev_config->irq_connect();
const struct qdec_nrfx_config *config = dev->config;
nrfx_err_t nerr;

int err = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
config->irq_connect();

if (err < 0) {
return err;
nerr = nrfx_qdec_init(&config->qdec, &config->config, qdec_nrfx_event_handler, (void *)dev);
if (nerr != NRFX_SUCCESS) {
return (nerr == NRFX_ERROR_INVALID_STATE) ? -EBUSY : -EFAULT;
}

nrfx_err_t nerr = nrfx_qdec_init(&dev_config->qdec,
&dev_config->config,
qdec_nrfx_event_handler,
(void *)dev);

if (nerr == NRFX_ERROR_INVALID_STATE) {
LOG_ERR("qdec already in use");
return -EBUSY;
} else if (nerr != NRFX_SUCCESS) {
LOG_ERR("failed to initialize qdec");
return -EFAULT;
/* End up in suspend state. */
qdec_nrfx_gpio_ctrl(dev, false);
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
}

qdec_nrfx_gpio_ctrl(dev, true);
nrfx_qdec_enable(&dev_config->qdec);

return 0;
return pm_device_driver_init(dev, qdec_nrfx_pm_action);
}

#define QDEC(idx) DT_NODELABEL(qdec##idx)
Expand Down Expand Up @@ -301,7 +284,7 @@
.enable_pin = DT_PROP_OR(QDEC(idx), enable_pin, NRF_QDEC_PIN_NOT_CONNECTED), \
.steps = QDEC_PROP(idx, steps), \
}; \
PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action); \
PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action, PM_DEVICE_ISR_SAFE); \
SENSOR_DEVICE_DT_DEFINE(QDEC(idx), \
qdec_nrfx_init, \
PM_DEVICE_DT_GET(QDEC(idx)), \
Expand All @@ -310,7 +293,7 @@
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&qdec_nrfx_driver_api)

Check notice on line 296 in drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c:296 -#define SENSOR_NRFX_QDEC_DEVICE(idx) \ - NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(QDEC(idx)); \ - BUILD_ASSERT(QDEC_PROP(idx, steps) > 0, \ - "Wrong QDEC"#idx" steps setting in dts. Only positive number valid"); \ - BUILD_ASSERT(QDEC_PROP(idx, steps) <= 2048, \ - "Wrong QDEC"#idx" steps setting in dts. Overflow possible"); \ - static void irq_connect##idx(void) \ - { \ - IRQ_CONNECT(DT_IRQN(QDEC(idx)), DT_IRQ(QDEC(idx), priority), \ - nrfx_isr, nrfx_qdec_##idx##_irq_handler, 0); \ - } \ - static struct qdec_nrfx_data qdec_##idx##_data; \ - PINCTRL_DT_DEFINE(QDEC(idx)); \ - static struct qdec_nrfx_config qdec_##idx##_config = { \ - .qdec = NRFX_QDEC_INSTANCE(idx), \ - .config = { \ - .reportper = NRF_QDEC_REPORTPER_40, \ - .sampleper = NRF_QDEC_SAMPLEPER_2048US, \ - .skip_gpio_cfg = true, \ - .skip_psel_cfg = true, \ - .ledpre = QDEC_PROP(idx, led_pre), \ - .ledpol = NRF_QDEC_LEPOL_ACTIVE_HIGH, \ - .reportper_inten = true, \ - }, \ - .irq_connect = irq_connect##idx, \ - .pcfg = PINCTRL_DT_DEV_CONFIG_GET(QDEC(idx)), \ - .enable_pin = DT_PROP_OR(QDEC(idx), enable_pin, NRF_QDEC_PIN_NOT_CONNECTED), \ - .steps = QDEC_PROP(idx, steps), \ - }; \ - PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action, PM_DEVICE_ISR_SAFE); \ - SENSOR_DEVICE_DT_DEFINE(QDEC(idx), \ - qdec_nrfx_init, \ - PM_DEVICE_DT_GET(QDEC(idx)), \ - &qdec_##idx##_data, \ - &qdec_##idx##_config, \ - POST_KERNEL, \ - CONFIG_SENSOR_INIT_PRIORITY, \ - &qdec_nrfx_driver_api) +#define SENSOR_NRFX_QDEC_DEVICE(idx) \ + NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(QDEC(idx)); \ + BUILD_ASSERT(QDEC_PROP(idx, steps) > 0, \ + "Wrong QDEC" #idx " steps setting in dts. Only positive number valid"); \ + BUILD_ASSERT(QDEC_PROP(idx, steps) <= 2048, \ + "Wrong QDEC" #idx " steps setting in dts. Overflow possible"); \ + static void irq_connect##idx(void) \ + { \ + IRQ_CONNECT(DT_IRQN(QDEC(idx)), DT_IRQ(QDEC(idx), priority), nrfx_isr, \ + nrfx_qdec_##idx##_irq_handler, 0); \ + } \ + static struct qdec_nrfx_data qdec_##idx##_data; \ + PINCTRL_DT_DEFINE(QDEC(idx)); \ + static struct qdec_nrfx_config qdec_##idx##_config = { \ + .qdec = NRFX_QDEC_INSTANCE(idx), \ + .config = \ + { \ + .reportper = NRF_QDEC_REPORTPER_40, \ + .sampleper = NRF_QDEC_SAMPLEPER_2048US, \ + .skip_gpio_cfg = true, \ + .skip_psel_cfg = true, \ + .ledpre = QDEC_PROP(idx, led_pre), \ + .ledpol = NRF_QDEC_LEPOL_ACTIVE_HIGH, \ + .reportper_inten = true, \ + }, \ + .irq_connect = irq_connect##idx,
#ifdef CONFIG_HAS_HW_NRF_QDEC0
SENSOR_NRFX_QDEC_DEVICE(0);
#endif
Expand Down
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,27 @@
<NRF_PSEL(QDEC_B, 1, 3)>; /* Arduino D2 */
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 1)>, /* Ardiuno D0 */
<NRF_PSEL(QDEC_B, 1, 3)>; /* Arduino D2 */
low-power-enable;
};
};
};

&qdec0 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = < 127 >;
led-pre = < 500 >;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart0 {
disable-rx;
};
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf5340dk_nrf5340_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@
<NRF_PSEL(QDEC_B, 0, 6)>; /* Arduino A2 */
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 0, 4)>,
<NRF_PSEL(QDEC_B, 0, 6)>;
low-power-enable;
};
};
};

&qdec1 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = < 127 >;
led-pre = < 500 >;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart0 {
disable-rx;
};
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf54h20dk_nrf54h20_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
<NRF_PSEL(QDEC_B, 1, 2)>;
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 0)>,
<NRF_PSEL(QDEC_B, 1, 2)>;
low-power-enable;
};
};
};

&gpio1 {
Expand All @@ -41,7 +49,14 @@
&qdec130 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = <127>;
led-pre = <500>;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart136 {
disable-rx;
};
40 changes: 4 additions & 36 deletions tests/boards/nrf/qdec/boards/nrf54l15dk_nrf54l15_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
qdec0 = &qdec20;
qenca = &phase_a;
qencb = &phase_b;
};
#include "nrf54l15pdk_nrf54l15_common.dtsi"

encoder-emulate {
compatible = "gpio-leds";
phase_a: phase_a {
gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>;
};
phase_b: phase_b {
gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;
};
};
};

&pinctrl {
qdec_pinctrl: qdec_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 8)>,
<NRF_PSEL(QDEC_B, 1, 10)>;
};
};
};

&gpio1 {
status = "okay";
};

&qdec20 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
steps = <127>;
led-pre = <500>;
/* To prevent enabling console receiver. */
&uart20 {
disable-rx;
};
40 changes: 4 additions & 36 deletions tests/boards/nrf/qdec/boards/nrf54l15dk_nrf54l15_cpuflpr.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
qdec0 = &qdec20;
qenca = &phase_a;
qencb = &phase_b;
};
#include "nrf54l15pdk_nrf54l15_common.dtsi"

encoder-emulate {
compatible = "gpio-leds";
phase_a: phase_a {
gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>;
};
phase_b: phase_b {
gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;
};
};
};

&pinctrl {
qdec_pinctrl: qdec_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 8)>,
<NRF_PSEL(QDEC_B, 1, 10)>;
};
};
};

&gpio1 {
status = "okay";
};

&qdec20 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
steps = <127>;
led-pre = <500>;
/* To prevent enabling console receiver. */
&uart30 {
disable-rx;
};
53 changes: 53 additions & 0 deletions tests/boards/nrf/qdec/boards/nrf54l15pdk_nrf54l15_common.dtsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
qdec0 = &qdec20;
qenca = &phase_a;
qencb = &phase_b;
};

encoder-emulate {
compatible = "gpio-leds";
phase_a: phase_a {
gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>;
};
phase_b: phase_b {
gpios = <&gpio1 11 GPIO_ACTIVE_HIGH>;
};
};
};

&pinctrl {
qdec_pinctrl: qdec_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 8)>,
<NRF_PSEL(QDEC_B, 1, 10)>;
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 8)>,
<NRF_PSEL(QDEC_B, 1, 10)>;
low-power-enable;
};
};
};

&gpio1 {
status = "okay";
};

&qdec20 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = <127>;
led-pre = <500>;
zephyr,pm-device-runtime-auto;
};
Loading
Loading