|
| 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); |
0 commit comments