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 gpio_ret = gpio_pin_get_config_dt(&gpio, &gpio_config); \
21 zassert_equal(gpio_ret, 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
ZTEST(device_driver_init,test_device_driver_init)30 ZTEST(device_driver_init, test_device_driver_init)
31 {
32 #if defined(CONFIG_PM_DEVICE_RUNTIME)
33 enum pm_device_state state;
34 int rc;
35 state = -1;
36 zassert_str_equal("", pm_device_state_str(state),
37 "Invalid device state");
38 /* No device runtime PM, starts on */
39 DEVICE_STATE_IS(DT_NODELABEL(test_reg), PM_DEVICE_STATE_ACTIVE);
40 DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained), PM_DEVICE_STATE_ACTIVE);
41 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH);
42 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH);
43 zassert_str_equal("active", pm_device_state_str(state),
44 "Invalid device state");
45 /* Device powered, zephyr,pm-device-runtime-auto, starts suspended */
46 DEVICE_STATE_IS(DT_NODELABEL(test_reg_chained_auto), PM_DEVICE_STATE_SUSPENDED);
47 DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto), PM_DEVICE_STATE_SUSPENDED);
48 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_LOW);
49 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_LOW);
50 zassert_str_equal("suspended", pm_device_state_str(state),
51 "Invalid device state");
52 /* Device not powered, starts off */
53 DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained), PM_DEVICE_STATE_OFF);
54 DEVICE_STATE_IS(DT_NODELABEL(test_reg_auto_chained_auto), PM_DEVICE_STATE_OFF);
55 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_DISCONNECTED);
56 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_DISCONNECTED);
57 zassert_str_equal("off", pm_device_state_str(state),
58 "Invalid device state");
59 #else
60 /* Every regulator should be in "active" mode automatically.
61 * State checking via GPIO as PM API is disabled.
62 */
63 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg), GPIO_OUTPUT_HIGH);
64 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained), GPIO_OUTPUT_HIGH);
65 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_chained_auto), GPIO_OUTPUT_HIGH);
66 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto), GPIO_OUTPUT_HIGH);
67 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained), GPIO_OUTPUT_HIGH);
68 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_auto_chained_auto), GPIO_OUTPUT_HIGH);
69 POWER_GPIO_CONFIG_IS(DT_NODELABEL(test_reg_disabled), GPIO_DISCONNECTED);
70 #endif
71 }
72
73 ZTEST_SUITE(device_driver_init, NULL, NULL, NULL, NULL, NULL);
74