1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
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/device.h>
11 
12 struct test_driver_data {
13 	size_t count;
14 	bool ongoing;
15 	bool async;
16 	struct k_sem sync;
17 	int ret;
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 	struct test_driver_data *data = dev->data;
24 
25 	if (!IS_ENABLED(CONFIG_TEST_PM_DEVICE_ISR_SAFE)) {
26 		data->ongoing = true;
27 
28 		if (data->async) {
29 			k_sem_take(&data->sync, K_FOREVER);
30 			data->async = false;
31 		}
32 
33 		data->ongoing = false;
34 	}
35 
36 	data->count++;
37 
38 	return data->ret;
39 }
40 
test_driver_pm_async(const struct device * dev)41 void test_driver_pm_async(const struct device *dev)
42 {
43 	struct test_driver_data *data = dev->data;
44 
45 	data->async = true;
46 }
47 
test_driver_pm_done(const struct device * dev)48 void test_driver_pm_done(const struct device *dev)
49 {
50 	struct test_driver_data *data = dev->data;
51 
52 	k_sem_give(&data->sync);
53 }
54 
test_driver_pm_ongoing(const struct device * dev)55 bool test_driver_pm_ongoing(const struct device *dev)
56 {
57 	struct test_driver_data *data = dev->data;
58 
59 	return data->ongoing;
60 }
61 
test_driver_pm_count(const struct device * dev)62 size_t test_driver_pm_count(const struct device *dev)
63 {
64 	struct test_driver_data *data = dev->data;
65 
66 	return data->count;
67 }
68 
test_driver_pm_retval(const struct device * dev,int ret)69 void test_driver_pm_retval(const struct device *dev, int ret)
70 {
71 	struct test_driver_data *data = dev->data;
72 
73 	data->ret = ret;
74 }
75 
test_driver_init(const struct device * dev)76 int test_driver_init(const struct device *dev)
77 {
78 	struct test_driver_data *data = dev->data;
79 
80 	k_sem_init(&data->sync, 0, 1);
81 
82 	return 0;
83 }
84 
85 #define PM_DEVICE_TYPE COND_CODE_1(CONFIG_TEST_PM_DEVICE_ISR_SAFE, (PM_DEVICE_ISR_SAFE), (0))
86 
87 PM_DEVICE_DEFINE(test_driver, test_driver_action, PM_DEVICE_TYPE);
88 
89 static struct test_driver_data data;
90 
91 DEVICE_DEFINE(test_driver, "test_driver", &test_driver_init,
92 	      PM_DEVICE_GET(test_driver), &data, NULL, POST_KERNEL,
93 	      CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
94