From a4cfc1eb37c5619fa10ffcc4cd8d45d12e8913a4 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Sat, 15 Jul 2023 17:17:32 +1000 Subject: [PATCH] tests: pm: test `pm_device_driver_init` Test that `pm_device_driver_init` puts devices into the appropriate state depending on the devicetree configuration. Signed-off-by: Jordan Yates --- .../pm/device_driver_init/CMakeLists.txt | 8 +++ .../subsys/pm/device_driver_init/app.overlay | 44 ++++++++++++ tests/subsys/pm/device_driver_init/prj.conf | 9 +++ tests/subsys/pm/device_driver_init/src/main.c | 68 +++++++++++++++++++ .../pm/device_driver_init/testcase.yaml | 19 ++++++ 5 files changed, 148 insertions(+) create mode 100644 tests/subsys/pm/device_driver_init/CMakeLists.txt create mode 100644 tests/subsys/pm/device_driver_init/app.overlay create mode 100644 tests/subsys/pm/device_driver_init/prj.conf create mode 100644 tests/subsys/pm/device_driver_init/src/main.c create mode 100644 tests/subsys/pm/device_driver_init/testcase.yaml diff --git a/tests/subsys/pm/device_driver_init/CMakeLists.txt b/tests/subsys/pm/device_driver_init/CMakeLists.txt new file mode 100644 index 00000000000..ddb4b524925 --- /dev/null +++ b/tests/subsys/pm/device_driver_init/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2023, CSIRO. +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(device_driver_init) + +target_sources(app PRIVATE src/main.c) diff --git a/tests/subsys/pm/device_driver_init/app.overlay b/tests/subsys/pm/device_driver_init/app.overlay new file mode 100644 index 00000000000..a5518b74be2 --- /dev/null +++ b/tests/subsys/pm/device_driver_init/app.overlay @@ -0,0 +1,44 @@ +/ { + test_reg: test_reg { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 0 0>; + }; + + test_reg_chained: test_reg_chained { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 1 0>; + power-domain = <&test_reg>; + }; + + test_reg_chained_auto: test_reg_chained_auto { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 2 0>; + power-domain = <&test_reg>; + zephyr,pm-device-runtime-auto; + }; + + test_reg_auto: test_reg_auto { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 3 0>; + zephyr,pm-device-runtime-auto; + }; + + test_reg_auto_chained: test_reg_auto_chained { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 4 0>; + power-domain = <&test_reg_auto>; + }; + + test_reg_auto_chained_auto: test_reg_auto_chained_auto { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 5 0>; + power-domain = <&test_reg_auto>; + zephyr,pm-device-runtime-auto; + }; + + test_reg_disabled: test_reg_disabled { + compatible = "power-domain-gpio"; + enable-gpios = <&gpio0 6 0>; + status = "disabled"; + }; +}; diff --git a/tests/subsys/pm/device_driver_init/prj.conf b/tests/subsys/pm/device_driver_init/prj.conf new file mode 100644 index 00000000000..e6d05f591ac --- /dev/null +++ b/tests/subsys/pm/device_driver_init/prj.conf @@ -0,0 +1,9 @@ +# Copyright (c) 2023, Commonwealth Scientific and Industrial Research +# Organisation (CSIRO) ABN 41 687 119 230. +CONFIG_ZTEST=y +CONFIG_MP_MAX_NUM_CPUS=1 + +CONFIG_GPIO=y +CONFIG_GPIO_GET_CONFIG=y +CONFIG_POWER_DOMAIN=y +CONFIG_ZTEST_NEW_API=y diff --git a/tests/subsys/pm/device_driver_init/src/main.c b/tests/subsys/pm/device_driver_init/src/main.c new file mode 100644 index 00000000000..bc004ab7a0c --- /dev/null +++ b/tests/subsys/pm/device_driver_init/src/main.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023, Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * SPDX-License-Identifier: Apache-2.0 + * + * State checking in this test is done via the GPIO state instead of + * the PM API as this test runs without the PM api enabled. + */ + +#include +#include +#include +#include + +#define POWER_GPIO_CONFIG_IS(node_id, config)\ + {\ + const struct gpio_dt_spec gpio = GPIO_DT_SPEC_GET(node_id, enable_gpios);\ + gpio_flags_t gpio_config; \ + int rc = gpio_pin_get_config_dt(&gpio, &gpio_config); \ + zassert_equal(rc, 0, "GPIO config retrieval failed"); \ + zassert_equal(gpio_config, config, "Unexpected config");\ + } + +#define DEVICE_STATE_IS(node_id, value) \ + rc = pm_device_state_get(DEVICE_DT_GET(node_id), &state); \ + zassert_equal(rc, 0, "Device state retrieval failed"); \ + zassert_equal(state, value, "Unexpected device state"); + +ZTEST(device_driver_init, test_demo) +{ +#if !IS_ENABLED(CONFIG_PM) || !IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME) + /* Every regulator should be in "active" mode automatically. + * State checking via GPIO as PM API is disabled. + */ + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_disabled), GPIO_DISCONNECTED); +#endif + +#if IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME) + enum pm_device_state state; + int rc; + + /* No device runtime PM, starts on */ + DEVICE_STATE_IS(DT_NODELABEL(test_reg), PM_DEVICE_STATE_ACTIVE); + DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained), PM_DEVICE_STATE_ACTIVE); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH); + + /* Device powered, zephyr,pm-device-runtime-auto, starts suspended */ + DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained_auto), PM_DEVICE_STATE_SUSPENDED); + DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto), PM_DEVICE_STATE_SUSPENDED); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_LOW); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_LOW); + /* Device not powered, starts off */ + DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained), PM_DEVICE_STATE_OFF); + DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained_auto), PM_DEVICE_STATE_OFF); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_DISCONNECTED); + POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_DISCONNECTED); +#endif +} + +ZTEST_SUITE(device_driver_init, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/subsys/pm/device_driver_init/testcase.yaml b/tests/subsys/pm/device_driver_init/testcase.yaml new file mode 100644 index 00000000000..c7e549b5400 --- /dev/null +++ b/tests/subsys/pm/device_driver_init/testcase.yaml @@ -0,0 +1,19 @@ +tests: + pm.device_driver_init: + tags: pm + platform_allow: qemu_cortex_m3 + pm.device_driver_init.pm: + tags: pm + platform_allow: qemu_cortex_m3 + extra_configs: + - CONFIG_PM=y + - CONFIG_PM_DEVICE=y + - CONFIG_PM_DEVICE_POWER_DOMAIN=y + pm.device_driver_init.pm_device_runtime: + tags: pm + platform_allow: qemu_cortex_m3 + extra_configs: + - CONFIG_PM=y + - CONFIG_PM_DEVICE=y + - CONFIG_PM_DEVICE_POWER_DOMAIN=y + - CONFIG_PM_DEVICE_RUNTIME=y