1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_driver.h"
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/pm/policy.h>
11 #include <zephyr/pm/device.h>
12 #include <zephyr/ztest.h>
13 
14 struct test_driver_data {
15 	const struct device *self;
16 	struct k_timer timer;
17 	bool ongoing;
18 };
19 
test_driver_action(const struct device * dev,enum pm_device_action action)20 static int test_driver_action(const struct device *dev,
21 			      enum pm_device_action action)
22 {
23 	ARG_UNUSED(dev);
24 	ARG_UNUSED(action);
25 
26 	return 0;
27 }
28 
timer_expire_cb(struct k_timer * timer)29 static void timer_expire_cb(struct k_timer *timer)
30 {
31 	struct test_driver_data *data = k_timer_user_data_get(timer);
32 
33 	data->ongoing = false;
34 	k_timer_stop(timer);
35 	pm_policy_device_power_lock_put(data->self);
36 }
37 
test_driver_async_operation(const struct device * dev)38 void test_driver_async_operation(const struct device *dev)
39 {
40 	struct test_driver_data *data = dev->data;
41 
42 	data->ongoing = true;
43 	pm_policy_device_power_lock_get(dev);
44 
45 	/** Lets set a timer big enough to ensure that any deep
46 	 *  sleep state would be suitable but constraints will
47 	 *  make only state0 (suspend-to-idle) will be used.
48 	 */
49 	k_timer_start(&data->timer, K_MSEC(500), K_NO_WAIT);
50 }
51 
test_driver_init(const struct device * dev)52 int test_driver_init(const struct device *dev)
53 {
54 	struct test_driver_data *data = dev->data;
55 
56 	data->self = dev;
57 
58 	k_timer_init(&data->timer, timer_expire_cb, NULL);
59 	k_timer_user_data_set(&data->timer, data);
60 
61 	return 0;
62 }
63 
64 PM_DEVICE_DT_DEFINE(DT_NODELABEL(test_dev), test_driver_action);
65 
66 static struct test_driver_data data;
67 
68 DEVICE_DT_DEFINE(DT_NODELABEL(test_dev), test_driver_init,
69 	      PM_DEVICE_DT_GET(DT_NODELABEL(test_dev)), &data, NULL, POST_KERNEL,
70 	      CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
71