|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Intel Corporation. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr.h> |
| 8 | +#include <ztest.h> |
| 9 | +#include <device.h> |
| 10 | +#include <pm/pm.h> |
| 11 | + |
| 12 | +#define DEV_NAME DT_NODELABEL(gpio0) |
| 13 | + |
| 14 | + |
| 15 | +static const struct device *dev; |
| 16 | +static uint8_t sleep_count; |
| 17 | + |
| 18 | + |
| 19 | +void pm_power_state_set(struct pm_state_info info) |
| 20 | +{ |
| 21 | + ARG_UNUSED(info); |
| 22 | + |
| 23 | + enum pm_device_state state; |
| 24 | + |
| 25 | + switch (sleep_count) { |
| 26 | + case 1: |
| 27 | + /* Just a sanity check that the system is the right state. |
| 28 | + * Devices are suspended before SoC on PM_STATE_SUSPEND_TO_RAM, that is why |
| 29 | + * we can check the device state here. |
| 30 | + */ |
| 31 | + zassert_equal(info.state, PM_STATE_SUSPEND_TO_RAM, "Wrong system state"); |
| 32 | + |
| 33 | + (void)pm_device_state_get(dev, &state); |
| 34 | + zassert_equal(state, PM_DEVICE_STATE_SUSPENDED, "Wrong device state"); |
| 35 | + |
| 36 | + /* Enable wakeup source. Next time the system is called |
| 37 | + * to sleep, this device will still be active. |
| 38 | + */ |
| 39 | + (void)pm_device_wakeup_enable((struct device *)dev, true); |
| 40 | + break; |
| 41 | + case 2: |
| 42 | + zassert_equal(info.state, PM_STATE_SUSPEND_TO_RAM, "Wrong system state"); |
| 43 | + |
| 44 | + /* Second time this function is called, the system is asked to standby |
| 45 | + * and devices were suspended. |
| 46 | + */ |
| 47 | + (void)pm_device_state_get(dev, &state); |
| 48 | + zassert_equal(state, PM_DEVICE_STATE_ACTIVE, "Wrong device state"); |
| 49 | + break; |
| 50 | + default: |
| 51 | + break; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void pm_power_state_exit_post_ops(struct pm_state_info info) |
| 56 | +{ |
| 57 | + irq_unlock(0); |
| 58 | +} |
| 59 | + |
| 60 | +struct pm_state_info pm_policy_next_state(int32_t ticks) |
| 61 | +{ |
| 62 | + while (sleep_count < 3) { |
| 63 | + sleep_count++; |
| 64 | + return (struct pm_state_info){PM_STATE_SUSPEND_TO_RAM, 0, 0, 0}; |
| 65 | + } |
| 66 | + |
| 67 | + return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0, 0}; |
| 68 | +} |
| 69 | + |
| 70 | +void test_wakeup_device_api(void) |
| 71 | +{ |
| 72 | + bool ret = false; |
| 73 | + |
| 74 | + dev = DEVICE_DT_GET(DEV_NAME); |
| 75 | + zassert_not_null(dev, "Failed to get device"); |
| 76 | + |
| 77 | + ret = pm_device_wakeup_is_capable(dev); |
| 78 | + zassert_true(ret, "Device marked as capable"); |
| 79 | + |
| 80 | + ret = pm_device_wakeup_enable((struct device *)dev, true); |
| 81 | + zassert_true(ret, "Could not enable wakeup source"); |
| 82 | + |
| 83 | + ret = pm_device_wakeup_is_enabled(dev); |
| 84 | + zassert_true(ret, "Wakeup source not enabled"); |
| 85 | + |
| 86 | + ret = pm_device_wakeup_enable((struct device *)dev, false); |
| 87 | + zassert_true(ret, "Could not disable wakeup source"); |
| 88 | + |
| 89 | + ret = pm_device_wakeup_is_enabled(dev); |
| 90 | + zassert_false(ret, "Wakeup source is enabled"); |
| 91 | +} |
| 92 | + |
| 93 | +void test_wakeup_device_system_pm(void) |
| 94 | +{ |
| 95 | + /* |
| 96 | + * Trigger system PM. The policy manager will return |
| 97 | + * PM_STATE_SUSPEND_TO_RAM and then the PM subsystem will |
| 98 | + * suspend all devices. As gpio is wakeup capability is not |
| 99 | + * enabled, the device will be suspended. This will be |
| 100 | + * confirmed in pm_power_state_set(). |
| 101 | + * |
| 102 | + * As the native posix implementation does not properly sleeps, |
| 103 | + * the idle thread will call several times the PM subsystem. This |
| 104 | + * test workaround this problem keeping track of the calls using |
| 105 | + * the sleep_count variable. |
| 106 | + */ |
| 107 | + k_sleep(K_SECONDS(1)); |
| 108 | +} |
| 109 | + |
| 110 | +void test_main(void) |
| 111 | +{ |
| 112 | + ztest_test_suite(wakeup_device_test, |
| 113 | + ztest_1cpu_unit_test(test_wakeup_device_api), |
| 114 | + ztest_1cpu_unit_test(test_wakeup_device_system_pm) |
| 115 | + ); |
| 116 | + ztest_run_test_suite(wakeup_device_test); |
| 117 | +} |
0 commit comments