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 };
18 
test_driver_action(const struct device * dev,enum pm_device_action action)19 static int test_driver_action(const struct device *dev,
20 			      enum pm_device_action action)
21 {
22 	struct test_driver_data *data = dev->data;
23 
24 	if (!IS_ENABLED(CONFIG_TEST_PM_DEVICE_ISR_SAFE)) {
25 		data->ongoing = true;
26 
27 		if (data->async) {
28 			k_sem_take(&data->sync, K_FOREVER);
29 			data->async = false;
30 		}
31 
32 		data->ongoing = false;
33 	}
34 
35 	data->count++;
36 
37 	return 0;
38 }
39 
test_driver_pm_async(const struct device * dev)40 void test_driver_pm_async(const struct device *dev)
41 {
42 	struct test_driver_data *data = dev->data;
43 
44 	data->async = true;
45 }
46 
test_driver_pm_done(const struct device * dev)47 void test_driver_pm_done(const struct device *dev)
48 {
49 	struct test_driver_data *data = dev->data;
50 
51 	k_sem_give(&data->sync);
52 }
53 
test_driver_pm_ongoing(const struct device * dev)54 bool test_driver_pm_ongoing(const struct device *dev)
55 {
56 	struct test_driver_data *data = dev->data;
57 
58 	return data->ongoing;
59 }
60 
test_driver_pm_count(const struct device * dev)61 size_t test_driver_pm_count(const struct device *dev)
62 {
63 	struct test_driver_data *data = dev->data;
64 
65 	return data->count;
66 }
67 
test_driver_init(const struct device * dev)68 int test_driver_init(const struct device *dev)
69 {
70 	struct test_driver_data *data = dev->data;
71 
72 	k_sem_init(&data->sync, 0, 1);
73 
74 	return 0;
75 }
76 
77 #define PM_DEVICE_TYPE COND_CODE_1(CONFIG_TEST_PM_DEVICE_ISR_SAFE, (PM_DEVICE_ISR_SAFE), (0))
78 
79 PM_DEVICE_DEFINE(test_driver, test_driver_action, PM_DEVICE_TYPE);
80 
81 static struct test_driver_data data;
82 
83 DEVICE_DEFINE(test_driver, "test_driver", &test_driver_init,
84 	      PM_DEVICE_GET(test_driver), &data, NULL, POST_KERNEL,
85 	      CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
86