1 /*
2  * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "esp_err.h"
14 #include "soc/soc_caps.h"
15 #include "soc/periph_defs.h"
16 #include "hal/modem_clock_types.h"
17 #include "esp_private/esp_pmu.h"
18 
19 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
20 #include "hal/modem_clock_hal.h"
21 #endif
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /**
28  * @brief Enable the clock of modem module
29  *
30  * Solve the clock dependency between modem modules, For example, the wifi
31  * module depends on the wifi mac, wifi baseband and FE, when wifi module
32  * clock is enabled, the wifi MAC, baseband and FE clocks will be enabled
33  *
34  * This interface and modem_clock_module_disable will jointly maintain the
35  * ref_cnt of each device clock source. The ref_cnt indicates how many modules
36  * are relying on the clock source. Each enable ops will add 1 to the ref_cnt of
37  * the clock source that the module depends on, and only when the ref_cnt of
38  * the module is from 0 to 1 will the clock enable be actually configured.
39  *
40  * !!! Do not use the hal/ll layer interface to configure the clock for the
41  * consistency of the hardware state maintained in the driver and the hardware
42  * actual state.
43  *
44  * @param module  modem module
45  */
46 void modem_clock_module_enable(periph_module_t module);
47 
48 /**
49  * @brief Disable the clock of modem module
50  *
51  * This interface and modem_clock_module_enable will jointly maintain the ref_cnt
52  * of each device clock source. The ref_cnt indicates how many modules are relying
53  * on the clock source. Each disable ops will minus 1 to the ref_cnt of the clock
54  * source that the module depends on, and only when the ref_cnt of the module is
55  * from 1 to 0 will the clock disable be actually configured.
56  *
57  * !!! Do not use the hal/ll layer interface to configure the clock for the
58  * consistency of the hardware state maintained in the driver and the hardware
59  * actual state.
60  *
61  * @param module  modem module
62  */
63 void modem_clock_module_disable(periph_module_t module);
64 
65 /**
66  * @brief Reset the mac of modem module
67  *
68  * @param module  modem module, must be one of
69  *    PERIPH_WIFI_MODULE / PERIPH_BT_MODULE /PERIPH_IEEE802154_MODULE
70  */
71 void modem_clock_module_mac_reset(periph_module_t module);
72 
73 #if SOC_PMU_SUPPORTED
74 /**
75  * @brief Enable modem clock domain clock gate to gate it's output
76  *
77  * @param domain modem module clock domain
78  * @param mode   PMU HP system ACTIVE, MODEM and SLEEP state
79  *
80  * @return
81  *      - ESP_OK on success
82  *      - ESP_ERR_INVALID_ARG if the argument value are not correct
83  */
84 esp_err_t modem_clock_domain_clk_gate_enable(modem_clock_domain_t domain, pmu_hp_icg_modem_mode_t mode);
85 
86 /**
87  * @brief Disable modem clock domain clock gate to ungate it's output
88  *
89  * @param domain modem module clock domain
90  * @param mode   PMU HP system ACTIVE, MODEM and SLEEP state
91  *
92  * @return
93  *      - ESP_OK on success
94  *      - ESP_ERR_INVALID_ARG if the argument value are not correct
95  */
96 esp_err_t modem_clock_domain_clk_gate_disable(modem_clock_domain_t domain, pmu_hp_icg_modem_mode_t mode);
97 #endif
98 
99 /**
100  * @brief Select the modem module lowpower clock source and configure the clock divider
101  *
102  * @param module  modem module
103  * @param src     lowpower clock source
104  * @param divider divider value to lowpower clock source
105  */
106 void modem_clock_select_lp_clock_source(periph_module_t module, modem_clock_lpclk_src_t src, uint32_t divider);
107 
108 /**
109  * @brief Disable lowpower clock source selection
110  * @param module  modem module
111  */
112 void modem_clock_deselect_lp_clock_source(periph_module_t module);
113 
114 /**
115 * @brief Disable all modem module's lowpower clock source selection
116  */
117 void modem_clock_deselect_all_module_lp_clock_source(void);
118 
119 /**
120  * @brief Reset wifi mac
121  */
122 void modem_clock_wifi_mac_reset(void);
123 
124 /**
125  * @brief Enable clock registers which shared by both modem and ADC. Need a ref count to enable/disable them
126  *
127  * @param enable true: enable; false: disable
128  */
129 void modem_clock_shared_enable(bool enable);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134