Skip to content

Commit a4cfc1e

Browse files
Jordan Yatescarlescufi
authored andcommitted
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 <[email protected]>
1 parent b82bbf5 commit a4cfc1e

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2023, CSIRO.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(device_driver_init)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/ {
2+
test_reg: test_reg {
3+
compatible = "power-domain-gpio";
4+
enable-gpios = <&gpio0 0 0>;
5+
};
6+
7+
test_reg_chained: test_reg_chained {
8+
compatible = "power-domain-gpio";
9+
enable-gpios = <&gpio0 1 0>;
10+
power-domain = <&test_reg>;
11+
};
12+
13+
test_reg_chained_auto: test_reg_chained_auto {
14+
compatible = "power-domain-gpio";
15+
enable-gpios = <&gpio0 2 0>;
16+
power-domain = <&test_reg>;
17+
zephyr,pm-device-runtime-auto;
18+
};
19+
20+
test_reg_auto: test_reg_auto {
21+
compatible = "power-domain-gpio";
22+
enable-gpios = <&gpio0 3 0>;
23+
zephyr,pm-device-runtime-auto;
24+
};
25+
26+
test_reg_auto_chained: test_reg_auto_chained {
27+
compatible = "power-domain-gpio";
28+
enable-gpios = <&gpio0 4 0>;
29+
power-domain = <&test_reg_auto>;
30+
};
31+
32+
test_reg_auto_chained_auto: test_reg_auto_chained_auto {
33+
compatible = "power-domain-gpio";
34+
enable-gpios = <&gpio0 5 0>;
35+
power-domain = <&test_reg_auto>;
36+
zephyr,pm-device-runtime-auto;
37+
};
38+
39+
test_reg_disabled: test_reg_disabled {
40+
compatible = "power-domain-gpio";
41+
enable-gpios = <&gpio0 6 0>;
42+
status = "disabled";
43+
};
44+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2023, Commonwealth Scientific and Industrial Research
2+
# Organisation (CSIRO) ABN 41 687 119 230.
3+
CONFIG_ZTEST=y
4+
CONFIG_MP_MAX_NUM_CPUS=1
5+
6+
CONFIG_GPIO=y
7+
CONFIG_GPIO_GET_CONFIG=y
8+
CONFIG_POWER_DOMAIN=y
9+
CONFIG_ZTEST_NEW_API=y
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2023, Commonwealth Scientific and Industrial Research
3+
* Organisation (CSIRO) ABN 41 687 119 230.
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* State checking in this test is done via the GPIO state instead of
8+
* the PM API as this test runs without the PM api enabled.
9+
*/
10+
11+
#include <zephyr/ztest.h>
12+
#include <zephyr/drivers/gpio.h>
13+
#include <zephyr/pm/device.h>
14+
#include <zephyr/pm/device_runtime.h>
15+
16+
#define POWER_GPIO_CONFIG_IS(node_id, config)\
17+
{\
18+
const struct gpio_dt_spec gpio = GPIO_DT_SPEC_GET(node_id, enable_gpios);\
19+
gpio_flags_t gpio_config; \
20+
int rc = gpio_pin_get_config_dt(&gpio, &gpio_config); \
21+
zassert_equal(rc, 0, "GPIO config retrieval failed"); \
22+
zassert_equal(gpio_config, config, "Unexpected config");\
23+
}
24+
25+
#define DEVICE_STATE_IS(node_id, value) \
26+
rc = pm_device_state_get(DEVICE_DT_GET(node_id), &state); \
27+
zassert_equal(rc, 0, "Device state retrieval failed"); \
28+
zassert_equal(state, value, "Unexpected device state");
29+
30+
ZTEST(device_driver_init, test_demo)
31+
{
32+
#if !IS_ENABLED(CONFIG_PM) || !IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)
33+
/* Every regulator should be in "active" mode automatically.
34+
* State checking via GPIO as PM API is disabled.
35+
*/
36+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH);
37+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH);
38+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_HIGH);
39+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_HIGH);
40+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_OUTPUT_HIGH);
41+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_OUTPUT_HIGH);
42+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_disabled), GPIO_DISCONNECTED);
43+
#endif
44+
45+
#if IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)
46+
enum pm_device_state state;
47+
int rc;
48+
49+
/* No device runtime PM, starts on */
50+
DEVICE_STATE_IS(DT_NODELABEL(test_reg), PM_DEVICE_STATE_ACTIVE);
51+
DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained), PM_DEVICE_STATE_ACTIVE);
52+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH);
53+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH);
54+
55+
/* Device powered, zephyr,pm-device-runtime-auto, starts suspended */
56+
DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained_auto), PM_DEVICE_STATE_SUSPENDED);
57+
DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto), PM_DEVICE_STATE_SUSPENDED);
58+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_LOW);
59+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_LOW);
60+
/* Device not powered, starts off */
61+
DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained), PM_DEVICE_STATE_OFF);
62+
DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained_auto), PM_DEVICE_STATE_OFF);
63+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_DISCONNECTED);
64+
POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_DISCONNECTED);
65+
#endif
66+
}
67+
68+
ZTEST_SUITE(device_driver_init, NULL, NULL, NULL, NULL, NULL);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests:
2+
pm.device_driver_init:
3+
tags: pm
4+
platform_allow: qemu_cortex_m3
5+
pm.device_driver_init.pm:
6+
tags: pm
7+
platform_allow: qemu_cortex_m3
8+
extra_configs:
9+
- CONFIG_PM=y
10+
- CONFIG_PM_DEVICE=y
11+
- CONFIG_PM_DEVICE_POWER_DOMAIN=y
12+
pm.device_driver_init.pm_device_runtime:
13+
tags: pm
14+
platform_allow: qemu_cortex_m3
15+
extra_configs:
16+
- CONFIG_PM=y
17+
- CONFIG_PM_DEVICE=y
18+
- CONFIG_PM_DEVICE_POWER_DOMAIN=y
19+
- CONFIG_PM_DEVICE_RUNTIME=y

0 commit comments

Comments
 (0)