1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef ZEPHYR_DRIVERS_CLOCK_CONTROL_NRF2_COMMON_H_
7 #define ZEPHYR_DRIVERS_CLOCK_CONTROL_NRF2_COMMON_H_
8 
9 #include <zephyr/device.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/drivers/clock_control.h>
12 #include <zephyr/sys/atomic.h>
13 #include <zephyr/sys/onoff.h>
14 
15 #define FLAGS_COMMON_BITS 10
16 
17 struct clock_onoff {
18 	struct onoff_manager mgr;
19 	onoff_notify_fn notify;
20 	uint8_t idx;
21 };
22 
23 /**
24  * @brief Defines a type for specific clock configuration structure.
25  *
26  * @param type suffix added clock_config_ to form the type name.
27  * @param _onoff_cnt number of clock configuration options to be handled;
28  *                   for each one a separate onoff manager instance is used.
29  */
30 #define STRUCT_CLOCK_CONFIG(type, _onoff_cnt) \
31 	struct clock_config_##type { \
32 		atomic_t flags; \
33 		uint32_t flags_snapshot; \
34 		struct k_work work; \
35 		uint8_t onoff_cnt; \
36 		struct clock_onoff onoff[_onoff_cnt]; \
37 	}
38 
39 /**
40  * @brief Obtain LFOSC accuracy in ppm.
41  *
42  * @param[out] accuracy Accuracy in ppm.
43  *
44  * @retval 0 On success
45  * @retval -EINVAL If accuracy is not configured.
46  */
47 int lfosc_get_accuracy(uint16_t *accuracy);
48 
49 /**
50  * @brief Initializes a clock configuration structure.
51  *
52  * @param clk_cfg pointer to the structure to be initialized.
53  * @param onoff_cnt number of clock configuration options handled
54  *                  handled by the structure.
55  * @param update_work_handler function that performs configuration update,
56  *                            called from the system work queue.
57  *
58  * @return 0 on success, negative value when onoff initialization fails.
59  */
60 int clock_config_init(void *clk_cfg, uint8_t onoff_cnt, k_work_handler_t update_work_handler);
61 
62 /**
63  * @brief Starts a clock configuration update.
64  *
65  * This function is supposed to be called by a specific clock control driver
66  * from its update work handler.
67  *
68  * @param work pointer to the work item received by the update work handler.
69  *
70  * @return index of the clock configuration onoff option to be activated.
71  */
72 uint8_t clock_config_update_begin(struct k_work *work);
73 
74 /**
75  * @brief Finalizes a clock configuration update.
76  *
77  * Notifies all relevant onoff managers about the update result.
78  * Only the first call after each clock_config_update_begin() performs
79  * the actual operation. Any further calls are simply no-ops.
80  *
81  * @param clk_cfg pointer to the clock configuration structure.
82  * @param status result to be passed to onoff managers.
83  */
84 void clock_config_update_end(void *clk_cfg, int status);
85 
86 int api_nosys_on_off(const struct device *dev, clock_control_subsys_t sys);
87 
88 #endif /* ZEPHYR_DRIVERS_CLOCK_CONTROL_NRF2_COMMON_H_ */
89