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 Initializes a clock configuration structure.
41  *
42  * @param clk_cfg pointer to the structure to be initialized.
43  * @param onoff_cnt number of clock configuration options handled
44  *                  handled by the structure.
45  * @param update_work_handler function that performs configuration update,
46  *                            called from the system work queue.
47  *
48  * @return 0 on success, negative value when onoff initialization fails.
49  */
50 int clock_config_init(void *clk_cfg, uint8_t onoff_cnt, k_work_handler_t update_work_handler);
51 
52 /**
53  * @brief Helper function for requesting a clock configuration handled by
54  *        a given on-off manager.
55  *
56  * If needed, the function resets the on-off service prior to making the new
57  * request.
58  *
59  * @param mgr pointer to the manager for which the request is to be done.
60  * @param cli pointer to a client state structure to be used for the request.
61  *
62  * @return result returned by onoff_request().
63  */
64 int clock_config_request(struct onoff_manager *mgr, struct onoff_client *cli);
65 
66 /**
67  * @brief Starts a clock configuration update.
68  *
69  * This function is supposed to be called by a specific clock control driver
70  * from its update work handler.
71  *
72  * @param work pointer to the work item received by the update work handler.
73  *
74  * @return index of the clock configuration onoff option to be activated.
75  */
76 uint8_t clock_config_update_begin(struct k_work *work);
77 
78 /**
79  * @brief Finalizes a clock configuration update.
80  *
81  * Notifies all relevant onoff managers about the update result.
82  * Only the first call after each clock_config_update_begin() performs
83  * the actual operation. Any further calls are simply no-ops.
84  *
85  * @param clk_cfg pointer to the clock configuration structure.
86  * @param status result to be passed to onoff managers.
87  */
88 void clock_config_update_end(void *clk_cfg, int status);
89 
90 int api_nosys_on_off(const struct device *dev, clock_control_subsys_t sys);
91 
92 #endif /* ZEPHYR_DRIVERS_CLOCK_CONTROL_NRF2_COMMON_H_ */
93