1 /*
2  * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include "sdkconfig.h"
9 #include "esp_phy_init.h"
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #define ESP_CAL_DATA_CHECK_FAIL 1
16 
17 typedef enum {
18     PHY_I2C_MST_CMD_TYPE_OFF = 0,
19     PHY_I2C_MST_CMD_TYPE_ON,
20     PHY_I2C_MST_CMD_TYPE_MAX
21 } phy_i2c_master_command_type_t;
22 
23 typedef struct {
24     struct {
25         uint8_t start, end; /* the start and end index of phy i2c master command memory */
26         uint8_t host_id;    /* phy i2c master host id */
27     } config[PHY_I2C_MST_CMD_TYPE_MAX];
28 } phy_i2c_master_command_attribute_t;
29 
30 /**
31  * @file phy.h
32  * @brief Declarations for functions provided by libphy.a
33  */
34 
35 /**
36  * @brief Return ROM function pointer table from PHY library.
37  */
38 void phy_get_romfunc_addr(void);
39 
40 /**
41  * @brief Initialize PHY module and do RF calibration
42  * @param[in] init_data Initialization parameters to be used by the PHY
43  * @param[inout] cal_data As input, calibration data previously obtained. As output, will contain new calibration data.
44  * @param[in] cal_mode  RF calibration mode
45  * @return ESP_CAL_DATA_CHECK_FAIL if the calibration data checksum fails or if the calibration data is outdated, other values are reserved for future use
46  */
47 int register_chipv7_phy(const esp_phy_init_data_t* init_data, esp_phy_calibration_data_t *cal_data, esp_phy_calibration_mode_t cal_mode);
48 
49 /**
50  * @brief Get the format version of calibration data used by PHY library.
51  * @return Format version number, OR'ed with BIT(16) if PHY is in WIFI only mode.
52  */
53 uint32_t phy_get_rf_cal_version(void);
54 
55 /**
56  * @brief Set RF/BB for only WIFI mode or coexist(WIFI & BT) mode
57  * @param[in] true is for only WIFI mode, false is for coexist mode. default is 0.
58  * @return NULL
59  */
60 void phy_set_wifi_mode_only(bool wifi_only);
61 
62 /**
63  * @brief Set BT the highest priority in coexist mode.
64  * @return NULL
65  */
66 void coex_bt_high_prio(void);
67 
68 /**
69  * @brief Open PHY and RF.
70  */
71 void phy_wakeup_init(void);
72 
73 /**
74  * @brief Shutdown PHY and RF.
75  */
76 void phy_close_rf(void);
77 
78 #if !CONFIG_IDF_TARGET_ESP32
79 /**
80  * @brief Disable PHY temperature sensor.
81  */
82 void phy_xpd_tsens(void);
83 #endif
84 
85 #if CONFIG_IDF_TARGET_ESP32C3
86 /**
87  * @brief Update internal state of PHY when wifi deinit powers off the wifi power domain.
88  */
89 void phy_init_flag(void);
90 #endif
91 
92 #if CONFIG_IDF_TARGET_ESP32C6
93 /**
94  * @brief Get the configuration info of PHY i2c master command memory.
95  *
96  * @param   attr the configuration info of PHY i2c master command memory
97  */
98 void phy_i2c_master_mem_cfg(phy_i2c_master_command_attribute_t *attr);
99 #endif
100 
101 /**
102  * @brief Store and load PHY digital registers.
103  *
104  * @param     backup_en  if backup_en is true, store PHY digital registers to memory. Otherwise load PHY digital registers from memory
105  * @param     mem_addr   Memory address to store and load PHY digital registers
106  *
107  * @return    memory size
108  */
109 uint8_t phy_dig_reg_backup(bool backup_en, uint32_t *mem_addr);
110 
111 #if CONFIG_MAC_BB_PD
112 /**
113  * @brief Store and load baseband registers.
114  */
115 void phy_freq_mem_backup(bool backup_en, uint32_t *mem);
116 #endif
117 
118 #if CONFIG_ESP_PHY_ENABLE_USB
119 /**
120  * @brief Enable or disable USB when phy init.
121  */
122 void phy_bbpll_en_usb(bool en);
123 #endif
124 
125 #if CONFIG_IDF_TARGET_ESP32S2
126 /**
127  * @brief Phy version select for ESP32S2
128  */
129 void phy_eco_version_sel(uint8_t chip_ver);
130 #endif
131 
132 #if CONFIG_ESP_PHY_IMPROVE_RX_11B
133 /**
134  * @brief Improve Wi-Fi receive 11b pkts when modules with high interference.
135  *
136  * @attention 1.This is a workaround to improve Wi-Fi receive 11b pkts for some modules using AC-DC power supply with high interference.
137  * @attention 2.Enable this will sacrifice Wi-Fi OFDM receive performance. But to guarantee 11b receive performance serves as a bottom line in this case.
138  *
139  * @param     enable  Enable or disable.
140  */
141 void phy_improve_rx_special(bool enable);
142 #endif
143 
144 /**
145  * @brief Enable phy track pll
146  *
147  */
148 void phy_track_pll_init(void);
149 
150 /**
151  * @brief Disable phy track pll
152  *
153  */
154 void phy_track_pll_deinit(void);
155 
156 /**
157  * @brief Set the flag recorded which modem has already enabled phy
158  *
159  */
160 void phy_set_modem_flag(esp_phy_modem_t modem);
161 
162 /**
163  * @brief Clear the flag to record which modem calls phy disenable
164  */
165 void phy_clr_modem_flag(esp_phy_modem_t modem);
166 
167 /**
168  * @brief Get the flag recorded which modem has already enabled phy
169  *
170  */
171 esp_phy_modem_t phy_get_modem_flag(void);
172 
173 /**
174  * @brief Get the PHY lock, only used in esp_phy, the user should not use this function.
175  *
176  */
177 struct k_mutex *phy_get_lock(void);
178 
179 /**
180  * @brief Call this funnction to track pll immediately.
181  *
182  */
183 void phy_track_pll(void);
184 
185 #if CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP
186 /**
187  * @brief On sleep->modem->active wakeup process, since RF has been turned on by hardware in
188  *        modem state, `sleep_modem_wifi_do_phy_retention` and `phy_wakeup_init` will be skipped
189  *        in `esp_phy_enable`, but there are still some configurations that need to be restored
190  *        by software, which are packed in this function.
191  */
192 void phy_wakeup_from_modem_state_extra_init(void);
193 #endif
194 #ifdef __cplusplus
195 }
196 #endif
197