18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2012  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "../wifi.h"
58c2ecf20Sopenharmony_ci#include "../pci.h"
68c2ecf20Sopenharmony_ci#include "../ps.h"
78c2ecf20Sopenharmony_ci#include "../core.h"
88c2ecf20Sopenharmony_ci#include "reg.h"
98c2ecf20Sopenharmony_ci#include "def.h"
108c2ecf20Sopenharmony_ci#include "phy.h"
118c2ecf20Sopenharmony_ci#include "rf.h"
128c2ecf20Sopenharmony_ci#include "dm.h"
138c2ecf20Sopenharmony_ci#include "table.h"
148c2ecf20Sopenharmony_ci#include "sw.h"
158c2ecf20Sopenharmony_ci#include "hw.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define MAX_RF_IMR_INDEX			12
188c2ecf20Sopenharmony_ci#define MAX_RF_IMR_INDEX_NORMAL			13
198c2ecf20Sopenharmony_ci#define RF_REG_NUM_FOR_C_CUT_5G			6
208c2ecf20Sopenharmony_ci#define RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA	7
218c2ecf20Sopenharmony_ci#define RF_REG_NUM_FOR_C_CUT_2G			5
228c2ecf20Sopenharmony_ci#define RF_CHNL_NUM_5G				19
238c2ecf20Sopenharmony_ci#define RF_CHNL_NUM_5G_40M			17
248c2ecf20Sopenharmony_ci#define TARGET_CHNL_NUM_5G			221
258c2ecf20Sopenharmony_ci#define TARGET_CHNL_NUM_2G			14
268c2ecf20Sopenharmony_ci#define CV_CURVE_CNT				64
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic u32 rf_reg_for_5g_swchnl_normal[MAX_RF_IMR_INDEX_NORMAL] = {
298c2ecf20Sopenharmony_ci	0, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x0
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic u8 rf_reg_for_c_cut_5g[RF_REG_NUM_FOR_C_CUT_5G] = {
338c2ecf20Sopenharmony_ci	RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G4, RF_SYN_G5, RF_SYN_G6
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic u8 rf_reg_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
378c2ecf20Sopenharmony_ci	RF_SYN_G1, RF_SYN_G2, RF_SYN_G3, RF_SYN_G7, RF_SYN_G8
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic u8 rf_for_c_cut_5g_internal_pa[RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
418c2ecf20Sopenharmony_ci	0x0B, 0x48, 0x49, 0x4B, 0x03, 0x04, 0x0E
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic u32 rf_reg_mask_for_c_cut_2g[RF_REG_NUM_FOR_C_CUT_2G] = {
458c2ecf20Sopenharmony_ci	BIT(19) | BIT(18) | BIT(17) | BIT(14) | BIT(1),
468c2ecf20Sopenharmony_ci	BIT(10) | BIT(9),
478c2ecf20Sopenharmony_ci	BIT(18) | BIT(17) | BIT(16) | BIT(1),
488c2ecf20Sopenharmony_ci	BIT(2) | BIT(1),
498c2ecf20Sopenharmony_ci	BIT(15) | BIT(14) | BIT(13) | BIT(12) | BIT(11)
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic u8 rf_chnl_5g[RF_CHNL_NUM_5G] = {
538c2ecf20Sopenharmony_ci	36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108,
548c2ecf20Sopenharmony_ci	112, 116, 120, 124, 128, 132, 136, 140
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic u8 rf_chnl_5g_40m[RF_CHNL_NUM_5G_40M] = {
588c2ecf20Sopenharmony_ci	38, 42, 46, 50, 54, 58, 62, 102, 106, 110, 114,
598c2ecf20Sopenharmony_ci	118, 122, 126, 130, 134, 138
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_cistatic u32 rf_reg_pram_c_5g[5][RF_REG_NUM_FOR_C_CUT_5G] = {
628c2ecf20Sopenharmony_ci	{0xE43BE, 0xFC638, 0x77C0A, 0xDE471, 0xd7110, 0x8EB04},
638c2ecf20Sopenharmony_ci	{0xE43BE, 0xFC078, 0xF7C1A, 0xE0C71, 0xD7550, 0xAEB04},
648c2ecf20Sopenharmony_ci	{0xE43BF, 0xFF038, 0xF7C0A, 0xDE471, 0xE5550, 0xAEB04},
658c2ecf20Sopenharmony_ci	{0xE43BF, 0xFF079, 0xF7C1A, 0xDE471, 0xE5550, 0xAEB04},
668c2ecf20Sopenharmony_ci	{0xE43BF, 0xFF038, 0xF7C1A, 0xDE471, 0xd7550, 0xAEB04}
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic u32 rf_reg_param_for_c_cut_2g[3][RF_REG_NUM_FOR_C_CUT_2G] = {
708c2ecf20Sopenharmony_ci	{0x643BC, 0xFC038, 0x77C1A, 0x41289, 0x01840},
718c2ecf20Sopenharmony_ci	{0x643BC, 0xFC038, 0x07C1A, 0x41289, 0x01840},
728c2ecf20Sopenharmony_ci	{0x243BC, 0xFC438, 0x07C1A, 0x4128B, 0x0FC41}
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic u32 rf_syn_g4_for_c_cut_2g = 0xD1C31 & 0x7FF;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic u32 rf_pram_c_5g_int_pa[3][RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA] = {
788c2ecf20Sopenharmony_ci	{0x01a00, 0x40443, 0x00eb5, 0x89bec, 0x94a12, 0x94a12, 0x94a12},
798c2ecf20Sopenharmony_ci	{0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a52, 0x94a52, 0x94a52},
808c2ecf20Sopenharmony_ci	{0x01800, 0xc0443, 0x00730, 0x896ee, 0x94a12, 0x94a12, 0x94a12}
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* [mode][patha+b][reg] */
848c2ecf20Sopenharmony_cistatic u32 rf_imr_param_normal[1][3][MAX_RF_IMR_INDEX_NORMAL] = {
858c2ecf20Sopenharmony_ci	{
868c2ecf20Sopenharmony_ci		/* channel 1-14. */
878c2ecf20Sopenharmony_ci		{
888c2ecf20Sopenharmony_ci			0x70000, 0x00ff0, 0x4400f, 0x00ff0, 0x0, 0x0, 0x0,
898c2ecf20Sopenharmony_ci			0x0, 0x0, 0x64888, 0xe266c, 0x00090, 0x22fff
908c2ecf20Sopenharmony_ci		},
918c2ecf20Sopenharmony_ci		/* path 36-64 */
928c2ecf20Sopenharmony_ci		{
938c2ecf20Sopenharmony_ci			0x70000, 0x22880, 0x4470f, 0x55880, 0x00070, 0x88000,
948c2ecf20Sopenharmony_ci			0x0, 0x88080, 0x70000, 0x64a82, 0xe466c, 0x00090,
958c2ecf20Sopenharmony_ci			0x32c9a
968c2ecf20Sopenharmony_ci		},
978c2ecf20Sopenharmony_ci		/* 100 -165 */
988c2ecf20Sopenharmony_ci		{
998c2ecf20Sopenharmony_ci			0x70000, 0x44880, 0x4477f, 0x77880, 0x00070, 0x88000,
1008c2ecf20Sopenharmony_ci			0x0, 0x880b0, 0x0, 0x64b82, 0xe466c, 0x00090, 0x32c9a
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic u32 curveindex_5g[TARGET_CHNL_NUM_5G] = {0};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic u32 curveindex_2g[TARGET_CHNL_NUM_2G] = {0};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic u32 targetchnl_5g[TARGET_CHNL_NUM_5G] = {
1108c2ecf20Sopenharmony_ci	25141, 25116, 25091, 25066, 25041,
1118c2ecf20Sopenharmony_ci	25016, 24991, 24966, 24941, 24917,
1128c2ecf20Sopenharmony_ci	24892, 24867, 24843, 24818, 24794,
1138c2ecf20Sopenharmony_ci	24770, 24765, 24721, 24697, 24672,
1148c2ecf20Sopenharmony_ci	24648, 24624, 24600, 24576, 24552,
1158c2ecf20Sopenharmony_ci	24528, 24504, 24480, 24457, 24433,
1168c2ecf20Sopenharmony_ci	24409, 24385, 24362, 24338, 24315,
1178c2ecf20Sopenharmony_ci	24291, 24268, 24245, 24221, 24198,
1188c2ecf20Sopenharmony_ci	24175, 24151, 24128, 24105, 24082,
1198c2ecf20Sopenharmony_ci	24059, 24036, 24013, 23990, 23967,
1208c2ecf20Sopenharmony_ci	23945, 23922, 23899, 23876, 23854,
1218c2ecf20Sopenharmony_ci	23831, 23809, 23786, 23764, 23741,
1228c2ecf20Sopenharmony_ci	23719, 23697, 23674, 23652, 23630,
1238c2ecf20Sopenharmony_ci	23608, 23586, 23564, 23541, 23519,
1248c2ecf20Sopenharmony_ci	23498, 23476, 23454, 23432, 23410,
1258c2ecf20Sopenharmony_ci	23388, 23367, 23345, 23323, 23302,
1268c2ecf20Sopenharmony_ci	23280, 23259, 23237, 23216, 23194,
1278c2ecf20Sopenharmony_ci	23173, 23152, 23130, 23109, 23088,
1288c2ecf20Sopenharmony_ci	23067, 23046, 23025, 23003, 22982,
1298c2ecf20Sopenharmony_ci	22962, 22941, 22920, 22899, 22878,
1308c2ecf20Sopenharmony_ci	22857, 22837, 22816, 22795, 22775,
1318c2ecf20Sopenharmony_ci	22754, 22733, 22713, 22692, 22672,
1328c2ecf20Sopenharmony_ci	22652, 22631, 22611, 22591, 22570,
1338c2ecf20Sopenharmony_ci	22550, 22530, 22510, 22490, 22469,
1348c2ecf20Sopenharmony_ci	22449, 22429, 22409, 22390, 22370,
1358c2ecf20Sopenharmony_ci	22350, 22336, 22310, 22290, 22271,
1368c2ecf20Sopenharmony_ci	22251, 22231, 22212, 22192, 22173,
1378c2ecf20Sopenharmony_ci	22153, 22134, 22114, 22095, 22075,
1388c2ecf20Sopenharmony_ci	22056, 22037, 22017, 21998, 21979,
1398c2ecf20Sopenharmony_ci	21960, 21941, 21921, 21902, 21883,
1408c2ecf20Sopenharmony_ci	21864, 21845, 21826, 21807, 21789,
1418c2ecf20Sopenharmony_ci	21770, 21751, 21732, 21713, 21695,
1428c2ecf20Sopenharmony_ci	21676, 21657, 21639, 21620, 21602,
1438c2ecf20Sopenharmony_ci	21583, 21565, 21546, 21528, 21509,
1448c2ecf20Sopenharmony_ci	21491, 21473, 21454, 21436, 21418,
1458c2ecf20Sopenharmony_ci	21400, 21381, 21363, 21345, 21327,
1468c2ecf20Sopenharmony_ci	21309, 21291, 21273, 21255, 21237,
1478c2ecf20Sopenharmony_ci	21219, 21201, 21183, 21166, 21148,
1488c2ecf20Sopenharmony_ci	21130, 21112, 21095, 21077, 21059,
1498c2ecf20Sopenharmony_ci	21042, 21024, 21007, 20989, 20972,
1508c2ecf20Sopenharmony_ci	25679, 25653, 25627, 25601, 25575,
1518c2ecf20Sopenharmony_ci	25549, 25523, 25497, 25471, 25446,
1528c2ecf20Sopenharmony_ci	25420, 25394, 25369, 25343, 25318,
1538c2ecf20Sopenharmony_ci	25292, 25267, 25242, 25216, 25191,
1548c2ecf20Sopenharmony_ci	25166
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* channel 1~14 */
1588c2ecf20Sopenharmony_cistatic u32 targetchnl_2g[TARGET_CHNL_NUM_2G] = {
1598c2ecf20Sopenharmony_ci	26084, 26030, 25976, 25923, 25869, 25816, 25764,
1608c2ecf20Sopenharmony_ci	25711, 25658, 25606, 25554, 25502, 25451, 25328
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic const u8 channel_all[59] = {
1648c2ecf20Sopenharmony_ci	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1658c2ecf20Sopenharmony_ci	36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
1668c2ecf20Sopenharmony_ci	60, 62, 64, 100, 102, 104, 106, 108, 110, 112,
1678c2ecf20Sopenharmony_ci	114, 116, 118, 120, 122, 124, 126, 128,	130,
1688c2ecf20Sopenharmony_ci	132, 134, 136, 138, 140, 149, 151, 153, 155,
1698c2ecf20Sopenharmony_ci	157, 159, 161, 163, 165
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciu32 rtl92d_phy_query_bb_reg(struct ieee80211_hw *hw, u32 regaddr, u32 bitmask)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1758c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1768c2ecf20Sopenharmony_ci	u32 returnvalue, originalvalue, bitshift;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE, "regaddr(%#x), bitmask(%#x)\n",
1798c2ecf20Sopenharmony_ci		regaddr, bitmask);
1808c2ecf20Sopenharmony_ci	if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob) {
1818c2ecf20Sopenharmony_ci		u8 dbi_direct = 0;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci		/* mac1 use phy0 read radio_b. */
1848c2ecf20Sopenharmony_ci		/* mac0 use phy1 read radio_b. */
1858c2ecf20Sopenharmony_ci		if (rtlhal->during_mac1init_radioa)
1868c2ecf20Sopenharmony_ci			dbi_direct = BIT(3);
1878c2ecf20Sopenharmony_ci		else if (rtlhal->during_mac0init_radiob)
1888c2ecf20Sopenharmony_ci			dbi_direct = BIT(3) | BIT(2);
1898c2ecf20Sopenharmony_ci		originalvalue = rtl92de_read_dword_dbi(hw, (u16)regaddr,
1908c2ecf20Sopenharmony_ci			dbi_direct);
1918c2ecf20Sopenharmony_ci	} else {
1928c2ecf20Sopenharmony_ci		originalvalue = rtl_read_dword(rtlpriv, regaddr);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci	bitshift = calculate_bit_shift(bitmask);
1958c2ecf20Sopenharmony_ci	returnvalue = (originalvalue & bitmask) >> bitshift;
1968c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
1978c2ecf20Sopenharmony_ci		"BBR MASK=0x%x Addr[0x%x]=0x%x\n",
1988c2ecf20Sopenharmony_ci		bitmask, regaddr, originalvalue);
1998c2ecf20Sopenharmony_ci	return returnvalue;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_civoid rtl92d_phy_set_bb_reg(struct ieee80211_hw *hw,
2038c2ecf20Sopenharmony_ci			   u32 regaddr, u32 bitmask, u32 data)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2068c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2078c2ecf20Sopenharmony_ci	u8 dbi_direct = 0;
2088c2ecf20Sopenharmony_ci	u32 originalvalue, bitshift;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
2118c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x)\n",
2128c2ecf20Sopenharmony_ci		regaddr, bitmask, data);
2138c2ecf20Sopenharmony_ci	if (rtlhal->during_mac1init_radioa)
2148c2ecf20Sopenharmony_ci		dbi_direct = BIT(3);
2158c2ecf20Sopenharmony_ci	else if (rtlhal->during_mac0init_radiob)
2168c2ecf20Sopenharmony_ci		/* mac0 use phy1 write radio_b. */
2178c2ecf20Sopenharmony_ci		dbi_direct = BIT(3) | BIT(2);
2188c2ecf20Sopenharmony_ci	if (bitmask != MASKDWORD) {
2198c2ecf20Sopenharmony_ci		if (rtlhal->during_mac1init_radioa ||
2208c2ecf20Sopenharmony_ci		    rtlhal->during_mac0init_radiob)
2218c2ecf20Sopenharmony_ci			originalvalue = rtl92de_read_dword_dbi(hw,
2228c2ecf20Sopenharmony_ci					(u16) regaddr,
2238c2ecf20Sopenharmony_ci					dbi_direct);
2248c2ecf20Sopenharmony_ci		else
2258c2ecf20Sopenharmony_ci			originalvalue = rtl_read_dword(rtlpriv, regaddr);
2268c2ecf20Sopenharmony_ci		bitshift = calculate_bit_shift(bitmask);
2278c2ecf20Sopenharmony_ci		data = ((originalvalue & (~bitmask)) | (data << bitshift));
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci	if (rtlhal->during_mac1init_radioa || rtlhal->during_mac0init_radiob)
2308c2ecf20Sopenharmony_ci		rtl92de_write_dword_dbi(hw, (u16) regaddr, data, dbi_direct);
2318c2ecf20Sopenharmony_ci	else
2328c2ecf20Sopenharmony_ci		rtl_write_dword(rtlpriv, regaddr, data);
2338c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
2348c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x)\n",
2358c2ecf20Sopenharmony_ci		regaddr, bitmask, data);
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic u32 _rtl92d_phy_rf_serial_read(struct ieee80211_hw *hw,
2398c2ecf20Sopenharmony_ci				      enum radio_path rfpath, u32 offset)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2438c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
2448c2ecf20Sopenharmony_ci	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
2458c2ecf20Sopenharmony_ci	u32 newoffset;
2468c2ecf20Sopenharmony_ci	u32 tmplong, tmplong2;
2478c2ecf20Sopenharmony_ci	u8 rfpi_enable = 0;
2488c2ecf20Sopenharmony_ci	u32 retvalue;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	newoffset = offset;
2518c2ecf20Sopenharmony_ci	tmplong = rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD);
2528c2ecf20Sopenharmony_ci	if (rfpath == RF90_PATH_A)
2538c2ecf20Sopenharmony_ci		tmplong2 = tmplong;
2548c2ecf20Sopenharmony_ci	else
2558c2ecf20Sopenharmony_ci		tmplong2 = rtl_get_bbreg(hw, pphyreg->rfhssi_para2, MASKDWORD);
2568c2ecf20Sopenharmony_ci	tmplong2 = (tmplong2 & (~BLSSIREADADDRESS)) |
2578c2ecf20Sopenharmony_ci		(newoffset << 23) | BLSSIREADEDGE;
2588c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD,
2598c2ecf20Sopenharmony_ci		tmplong & (~BLSSIREADEDGE));
2608c2ecf20Sopenharmony_ci	udelay(10);
2618c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, MASKDWORD, tmplong2);
2628c2ecf20Sopenharmony_ci	udelay(50);
2638c2ecf20Sopenharmony_ci	udelay(50);
2648c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XA_HSSIPARAMETER2, MASKDWORD,
2658c2ecf20Sopenharmony_ci		tmplong | BLSSIREADEDGE);
2668c2ecf20Sopenharmony_ci	udelay(10);
2678c2ecf20Sopenharmony_ci	if (rfpath == RF90_PATH_A)
2688c2ecf20Sopenharmony_ci		rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XA_HSSIPARAMETER1,
2698c2ecf20Sopenharmony_ci			      BIT(8));
2708c2ecf20Sopenharmony_ci	else if (rfpath == RF90_PATH_B)
2718c2ecf20Sopenharmony_ci		rfpi_enable = (u8) rtl_get_bbreg(hw, RFPGA0_XB_HSSIPARAMETER1,
2728c2ecf20Sopenharmony_ci			      BIT(8));
2738c2ecf20Sopenharmony_ci	if (rfpi_enable)
2748c2ecf20Sopenharmony_ci		retvalue = rtl_get_bbreg(hw, pphyreg->rf_rbpi,
2758c2ecf20Sopenharmony_ci			BLSSIREADBACKDATA);
2768c2ecf20Sopenharmony_ci	else
2778c2ecf20Sopenharmony_ci		retvalue = rtl_get_bbreg(hw, pphyreg->rf_rb,
2788c2ecf20Sopenharmony_ci			BLSSIREADBACKDATA);
2798c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE, "RFR-%d Addr[0x%x] = 0x%x\n",
2808c2ecf20Sopenharmony_ci		rfpath, pphyreg->rf_rb, retvalue);
2818c2ecf20Sopenharmony_ci	return retvalue;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void _rtl92d_phy_rf_serial_write(struct ieee80211_hw *hw,
2858c2ecf20Sopenharmony_ci					enum radio_path rfpath,
2868c2ecf20Sopenharmony_ci					u32 offset, u32 data)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	u32 data_and_addr;
2898c2ecf20Sopenharmony_ci	u32 newoffset;
2908c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2918c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
2928c2ecf20Sopenharmony_ci	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	newoffset = offset;
2958c2ecf20Sopenharmony_ci	/* T65 RF */
2968c2ecf20Sopenharmony_ci	data_and_addr = ((newoffset << 20) | (data & 0x000fffff)) & 0x0fffffff;
2978c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rf3wire_offset, MASKDWORD, data_and_addr);
2988c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE, "RFW-%d Addr[0x%x]=0x%x\n",
2998c2ecf20Sopenharmony_ci		rfpath, pphyreg->rf3wire_offset, data_and_addr);
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ciu32 rtl92d_phy_query_rf_reg(struct ieee80211_hw *hw,
3038c2ecf20Sopenharmony_ci			    enum radio_path rfpath, u32 regaddr, u32 bitmask)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3068c2ecf20Sopenharmony_ci	u32 original_value, readback_value, bitshift;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
3098c2ecf20Sopenharmony_ci		"regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
3108c2ecf20Sopenharmony_ci		regaddr, rfpath, bitmask);
3118c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.rf_lock);
3128c2ecf20Sopenharmony_ci	original_value = _rtl92d_phy_rf_serial_read(hw, rfpath, regaddr);
3138c2ecf20Sopenharmony_ci	bitshift = calculate_bit_shift(bitmask);
3148c2ecf20Sopenharmony_ci	readback_value = (original_value & bitmask) >> bitshift;
3158c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.rf_lock);
3168c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
3178c2ecf20Sopenharmony_ci		"regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
3188c2ecf20Sopenharmony_ci		regaddr, rfpath, bitmask, original_value);
3198c2ecf20Sopenharmony_ci	return readback_value;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_civoid rtl92d_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
3238c2ecf20Sopenharmony_ci	u32 regaddr, u32 bitmask, u32 data)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3268c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
3278c2ecf20Sopenharmony_ci	u32 original_value, bitshift;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
3308c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
3318c2ecf20Sopenharmony_ci		regaddr, bitmask, data, rfpath);
3328c2ecf20Sopenharmony_ci	if (bitmask == 0)
3338c2ecf20Sopenharmony_ci		return;
3348c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.rf_lock);
3358c2ecf20Sopenharmony_ci	if (rtlphy->rf_mode != RF_OP_BY_FW) {
3368c2ecf20Sopenharmony_ci		if (bitmask != RFREG_OFFSET_MASK) {
3378c2ecf20Sopenharmony_ci			original_value = _rtl92d_phy_rf_serial_read(hw,
3388c2ecf20Sopenharmony_ci				rfpath, regaddr);
3398c2ecf20Sopenharmony_ci			bitshift = calculate_bit_shift(bitmask);
3408c2ecf20Sopenharmony_ci			data = ((original_value & (~bitmask)) |
3418c2ecf20Sopenharmony_ci				(data << bitshift));
3428c2ecf20Sopenharmony_ci		}
3438c2ecf20Sopenharmony_ci		_rtl92d_phy_rf_serial_write(hw, rfpath, regaddr, data);
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.rf_lock);
3468c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
3478c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
3488c2ecf20Sopenharmony_ci		regaddr, bitmask, data, rfpath);
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cibool rtl92d_phy_mac_config(struct ieee80211_hw *hw)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3548c2ecf20Sopenharmony_ci	u32 i;
3558c2ecf20Sopenharmony_ci	u32 arraylength;
3568c2ecf20Sopenharmony_ci	u32 *ptrarray;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Read Rtl819XMACPHY_Array\n");
3598c2ecf20Sopenharmony_ci	arraylength = MAC_2T_ARRAYLENGTH;
3608c2ecf20Sopenharmony_ci	ptrarray = rtl8192de_mac_2tarray;
3618c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Img:Rtl819XMAC_Array\n");
3628c2ecf20Sopenharmony_ci	for (i = 0; i < arraylength; i = i + 2)
3638c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, ptrarray[i], (u8) ptrarray[i + 1]);
3648c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY) {
3658c2ecf20Sopenharmony_ci		/* improve 2-stream TX EVM */
3668c2ecf20Sopenharmony_ci		/* rtl_write_byte(rtlpriv, 0x14,0x71); */
3678c2ecf20Sopenharmony_ci		/* AMPDU aggregation number 9 */
3688c2ecf20Sopenharmony_ci		/* rtl_write_word(rtlpriv, REG_MAX_AGGR_NUM, MAX_AGGR_NUM); */
3698c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x0B);
3708c2ecf20Sopenharmony_ci	} else {
3718c2ecf20Sopenharmony_ci		/* 92D need to test to decide the num. */
3728c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAX_AGGR_NUM, 0x07);
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci	return true;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic void _rtl92d_phy_init_bb_rf_register_definition(struct ieee80211_hw *hw)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3808c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* RF Interface Sowrtware Control */
3838c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x870 */
3848c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfintfs = RFPGA0_XAB_RFINTERFACESW;
3858c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x870 (16-bit for 0x872) */
3868c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfintfs = RFPGA0_XAB_RFINTERFACESW;
3878c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x874 */
3888c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfintfs = RFPGA0_XCD_RFINTERFACESW;
3898c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x874 (16-bit for 0x876) */
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfintfs = RFPGA0_XCD_RFINTERFACESW;
3928c2ecf20Sopenharmony_ci	/* RF Interface Readback Value */
3938c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x8E0 */
3948c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfintfi = RFPGA0_XAB_RFINTERFACERB;
3958c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x8E0 (16-bit for 0x8E2) */
3968c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfintfi = RFPGA0_XAB_RFINTERFACERB;
3978c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x8E4 */
3988c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfintfi = RFPGA0_XCD_RFINTERFACERB;
3998c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x8E4 (16-bit for 0x8E6) */
4008c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfintfi = RFPGA0_XCD_RFINTERFACERB;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* RF Interface Output (and Enable) */
4038c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x860 */
4048c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfintfo = RFPGA0_XA_RFINTERFACEOE;
4058c2ecf20Sopenharmony_ci	/* 16 LSBs if read 32-bit from 0x864 */
4068c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfintfo = RFPGA0_XB_RFINTERFACEOE;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* RF Interface (Output and)  Enable */
4098c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x860 (16-bit for 0x862) */
4108c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfintfe = RFPGA0_XA_RFINTERFACEOE;
4118c2ecf20Sopenharmony_ci	/* 16 MSBs if read 32-bit from 0x864 (16-bit for 0x866) */
4128c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfintfe = RFPGA0_XB_RFINTERFACEOE;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Addr of LSSI. Wirte RF register by driver */
4158c2ecf20Sopenharmony_ci	/* LSSI Parameter */
4168c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rf3wire_offset =
4178c2ecf20Sopenharmony_ci				 RFPGA0_XA_LSSIPARAMETER;
4188c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rf3wire_offset =
4198c2ecf20Sopenharmony_ci				 RFPGA0_XB_LSSIPARAMETER;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* RF parameter */
4228c2ecf20Sopenharmony_ci	/* BB Band Select */
4238c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rflssi_select = RFPGA0_XAB_RFPARAMETER;
4248c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rflssi_select = RFPGA0_XAB_RFPARAMETER;
4258c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rflssi_select = RFPGA0_XCD_RFPARAMETER;
4268c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rflssi_select = RFPGA0_XCD_RFPARAMETER;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/* Tx AGC Gain Stage (same for all path. Should we remove this?) */
4298c2ecf20Sopenharmony_ci	/* Tx gain stage */
4308c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rftxgain_stage = RFPGA0_TXGAINSTAGE;
4318c2ecf20Sopenharmony_ci	/* Tx gain stage */
4328c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rftxgain_stage = RFPGA0_TXGAINSTAGE;
4338c2ecf20Sopenharmony_ci	/* Tx gain stage */
4348c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rftxgain_stage = RFPGA0_TXGAINSTAGE;
4358c2ecf20Sopenharmony_ci	/* Tx gain stage */
4368c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rftxgain_stage = RFPGA0_TXGAINSTAGE;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	/* Tranceiver A~D HSSI Parameter-1 */
4398c2ecf20Sopenharmony_ci	/* wire control parameter1 */
4408c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para1 = RFPGA0_XA_HSSIPARAMETER1;
4418c2ecf20Sopenharmony_ci	/* wire control parameter1 */
4428c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para1 = RFPGA0_XB_HSSIPARAMETER1;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* Tranceiver A~D HSSI Parameter-2 */
4458c2ecf20Sopenharmony_ci	/* wire control parameter2 */
4468c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para2 = RFPGA0_XA_HSSIPARAMETER2;
4478c2ecf20Sopenharmony_ci	/* wire control parameter2 */
4488c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para2 = RFPGA0_XB_HSSIPARAMETER2;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* RF switch Control */
4518c2ecf20Sopenharmony_ci	/* TR/Ant switch control */
4528c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfsw_ctrl = RFPGA0_XAB_SWITCHCONTROL;
4538c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfsw_ctrl = RFPGA0_XAB_SWITCHCONTROL;
4548c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfsw_ctrl = RFPGA0_XCD_SWITCHCONTROL;
4558c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfsw_ctrl = RFPGA0_XCD_SWITCHCONTROL;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* AGC control 1 */
4588c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfagc_control1 = ROFDM0_XAAGCCORE1;
4598c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfagc_control1 = ROFDM0_XBAGCCORE1;
4608c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfagc_control1 = ROFDM0_XCAGCCORE1;
4618c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfagc_control1 = ROFDM0_XDAGCCORE1;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* AGC control 2  */
4648c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfagc_control2 = ROFDM0_XAAGCCORE2;
4658c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfagc_control2 = ROFDM0_XBAGCCORE2;
4668c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfagc_control2 = ROFDM0_XCAGCCORE2;
4678c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfagc_control2 = ROFDM0_XDAGCCORE2;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* RX AFE control 1 */
4708c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfrxiq_imbal = ROFDM0_XARXIQIMBALANCE;
4718c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfrxiq_imbal = ROFDM0_XBRXIQIMBALANCE;
4728c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfrxiq_imbal = ROFDM0_XCRXIQIMBALANCE;
4738c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfrxiq_imbal = ROFDM0_XDRXIQIMBALANCE;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/*RX AFE control 1 */
4768c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rfrx_afe = ROFDM0_XARXAFE;
4778c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rfrx_afe = ROFDM0_XBRXAFE;
4788c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rfrx_afe = ROFDM0_XCRXAFE;
4798c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rfrx_afe = ROFDM0_XDRXAFE;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* Tx AFE control 1 */
4828c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rftxiq_imbal = ROFDM0_XATXIQIMBALANCE;
4838c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rftxiq_imbal = ROFDM0_XBTXIQIMBALANCE;
4848c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rftxiq_imbal = ROFDM0_XCTXIQIMBALANCE;
4858c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rftxiq_imbal = ROFDM0_XDTXIQIMBALANCE;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/* Tx AFE control 2 */
4888c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rftx_afe = ROFDM0_XATXAFE;
4898c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rftx_afe = ROFDM0_XBTXAFE;
4908c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rftx_afe = ROFDM0_XCTXAFE;
4918c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rftx_afe = ROFDM0_XDTXAFE;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* Tranceiver LSSI Readback SI mode */
4948c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rf_rb = RFPGA0_XA_LSSIREADBACK;
4958c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rf_rb = RFPGA0_XB_LSSIREADBACK;
4968c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_C].rf_rb = RFPGA0_XC_LSSIREADBACK;
4978c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_D].rf_rb = RFPGA0_XD_LSSIREADBACK;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/* Tranceiver LSSI Readback PI mode */
5008c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_A].rf_rbpi = TRANSCEIVERA_HSPI_READBACK;
5018c2ecf20Sopenharmony_ci	rtlphy->phyreg_def[RF90_PATH_B].rf_rbpi = TRANSCEIVERB_HSPI_READBACK;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
5058c2ecf20Sopenharmony_ci	u8 configtype)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	int i;
5088c2ecf20Sopenharmony_ci	u32 *phy_regarray_table;
5098c2ecf20Sopenharmony_ci	u32 *agctab_array_table = NULL;
5108c2ecf20Sopenharmony_ci	u32 *agctab_5garray_table;
5118c2ecf20Sopenharmony_ci	u16 phy_reg_arraylen, agctab_arraylen = 0, agctab_5garraylen;
5128c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
5138c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Normal chip,Mac0 use AGC_TAB.txt for 2G and 5G band. */
5168c2ecf20Sopenharmony_ci	if (rtlhal->interfaceindex == 0) {
5178c2ecf20Sopenharmony_ci		agctab_arraylen = AGCTAB_ARRAYLENGTH;
5188c2ecf20Sopenharmony_ci		agctab_array_table = rtl8192de_agctab_array;
5198c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5208c2ecf20Sopenharmony_ci			" ===> phy:MAC0, Rtl819XAGCTAB_Array\n");
5218c2ecf20Sopenharmony_ci	} else {
5228c2ecf20Sopenharmony_ci		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
5238c2ecf20Sopenharmony_ci			agctab_arraylen = AGCTAB_2G_ARRAYLENGTH;
5248c2ecf20Sopenharmony_ci			agctab_array_table = rtl8192de_agctab_2garray;
5258c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5268c2ecf20Sopenharmony_ci				" ===> phy:MAC1, Rtl819XAGCTAB_2GArray\n");
5278c2ecf20Sopenharmony_ci		} else {
5288c2ecf20Sopenharmony_ci			agctab_5garraylen = AGCTAB_5G_ARRAYLENGTH;
5298c2ecf20Sopenharmony_ci			agctab_5garray_table = rtl8192de_agctab_5garray;
5308c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5318c2ecf20Sopenharmony_ci				" ===> phy:MAC1, Rtl819XAGCTAB_5GArray\n");
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci		}
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci	phy_reg_arraylen = PHY_REG_2T_ARRAYLENGTH;
5368c2ecf20Sopenharmony_ci	phy_regarray_table = rtl8192de_phy_reg_2tarray;
5378c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5388c2ecf20Sopenharmony_ci		" ===> phy:Rtl819XPHY_REG_Array_PG\n");
5398c2ecf20Sopenharmony_ci	if (configtype == BASEBAND_CONFIG_PHY_REG) {
5408c2ecf20Sopenharmony_ci		for (i = 0; i < phy_reg_arraylen; i = i + 2) {
5418c2ecf20Sopenharmony_ci			rtl_addr_delay(phy_regarray_table[i]);
5428c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, phy_regarray_table[i], MASKDWORD,
5438c2ecf20Sopenharmony_ci				      phy_regarray_table[i + 1]);
5448c2ecf20Sopenharmony_ci			udelay(1);
5458c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
5468c2ecf20Sopenharmony_ci				"The phy_regarray_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
5478c2ecf20Sopenharmony_ci				phy_regarray_table[i],
5488c2ecf20Sopenharmony_ci				phy_regarray_table[i + 1]);
5498c2ecf20Sopenharmony_ci		}
5508c2ecf20Sopenharmony_ci	} else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
5518c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 0) {
5528c2ecf20Sopenharmony_ci			for (i = 0; i < agctab_arraylen; i = i + 2) {
5538c2ecf20Sopenharmony_ci				rtl_set_bbreg(hw, agctab_array_table[i],
5548c2ecf20Sopenharmony_ci					MASKDWORD,
5558c2ecf20Sopenharmony_ci					agctab_array_table[i + 1]);
5568c2ecf20Sopenharmony_ci				/* Add 1us delay between BB/RF register
5578c2ecf20Sopenharmony_ci				 * setting. */
5588c2ecf20Sopenharmony_ci				udelay(1);
5598c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
5608c2ecf20Sopenharmony_ci					"The Rtl819XAGCTAB_Array_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
5618c2ecf20Sopenharmony_ci					agctab_array_table[i],
5628c2ecf20Sopenharmony_ci					agctab_array_table[i + 1]);
5638c2ecf20Sopenharmony_ci			}
5648c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5658c2ecf20Sopenharmony_ci				"Normal Chip, MAC0, load Rtl819XAGCTAB_Array\n");
5668c2ecf20Sopenharmony_ci		} else {
5678c2ecf20Sopenharmony_ci			if (rtlhal->current_bandtype == BAND_ON_2_4G) {
5688c2ecf20Sopenharmony_ci				for (i = 0; i < agctab_arraylen; i = i + 2) {
5698c2ecf20Sopenharmony_ci					rtl_set_bbreg(hw, agctab_array_table[i],
5708c2ecf20Sopenharmony_ci						MASKDWORD,
5718c2ecf20Sopenharmony_ci						agctab_array_table[i + 1]);
5728c2ecf20Sopenharmony_ci					/* Add 1us delay between BB/RF register
5738c2ecf20Sopenharmony_ci					 * setting. */
5748c2ecf20Sopenharmony_ci					udelay(1);
5758c2ecf20Sopenharmony_ci					rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
5768c2ecf20Sopenharmony_ci						"The Rtl819XAGCTAB_Array_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
5778c2ecf20Sopenharmony_ci						agctab_array_table[i],
5788c2ecf20Sopenharmony_ci						agctab_array_table[i + 1]);
5798c2ecf20Sopenharmony_ci				}
5808c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5818c2ecf20Sopenharmony_ci					"Load Rtl819XAGCTAB_2GArray\n");
5828c2ecf20Sopenharmony_ci			} else {
5838c2ecf20Sopenharmony_ci				for (i = 0; i < agctab_5garraylen; i = i + 2) {
5848c2ecf20Sopenharmony_ci					rtl_set_bbreg(hw,
5858c2ecf20Sopenharmony_ci						agctab_5garray_table[i],
5868c2ecf20Sopenharmony_ci						MASKDWORD,
5878c2ecf20Sopenharmony_ci						agctab_5garray_table[i + 1]);
5888c2ecf20Sopenharmony_ci					/* Add 1us delay between BB/RF registeri
5898c2ecf20Sopenharmony_ci					 * setting. */
5908c2ecf20Sopenharmony_ci					udelay(1);
5918c2ecf20Sopenharmony_ci					rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
5928c2ecf20Sopenharmony_ci						"The Rtl819XAGCTAB_5GArray_Table[0] is %u Rtl819XPHY_REGArray[1] is %u\n",
5938c2ecf20Sopenharmony_ci						agctab_5garray_table[i],
5948c2ecf20Sopenharmony_ci						agctab_5garray_table[i + 1]);
5958c2ecf20Sopenharmony_ci				}
5968c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
5978c2ecf20Sopenharmony_ci					"Load Rtl819XAGCTAB_5GArray\n");
5988c2ecf20Sopenharmony_ci			}
5998c2ecf20Sopenharmony_ci		}
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci	return true;
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_cistatic void _rtl92d_store_pwrindex_diffrate_offset(struct ieee80211_hw *hw,
6058c2ecf20Sopenharmony_ci						   u32 regaddr, u32 bitmask,
6068c2ecf20Sopenharmony_ci						   u32 data)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6098c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
6108c2ecf20Sopenharmony_ci	int index;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (regaddr == RTXAGC_A_RATE18_06)
6138c2ecf20Sopenharmony_ci		index = 0;
6148c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_RATE54_24)
6158c2ecf20Sopenharmony_ci		index = 1;
6168c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_CCK1_MCS32)
6178c2ecf20Sopenharmony_ci		index = 6;
6188c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0xffffff00)
6198c2ecf20Sopenharmony_ci		index = 7;
6208c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_MCS03_MCS00)
6218c2ecf20Sopenharmony_ci		index = 2;
6228c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_MCS07_MCS04)
6238c2ecf20Sopenharmony_ci		index = 3;
6248c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_MCS11_MCS08)
6258c2ecf20Sopenharmony_ci		index = 4;
6268c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_A_MCS15_MCS12)
6278c2ecf20Sopenharmony_ci		index = 5;
6288c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_RATE18_06)
6298c2ecf20Sopenharmony_ci		index = 8;
6308c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_RATE54_24)
6318c2ecf20Sopenharmony_ci		index = 9;
6328c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_CCK1_55_MCS32)
6338c2ecf20Sopenharmony_ci		index = 14;
6348c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_CCK11_A_CCK2_11 && bitmask == 0x000000ff)
6358c2ecf20Sopenharmony_ci		index = 15;
6368c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_MCS03_MCS00)
6378c2ecf20Sopenharmony_ci		index = 10;
6388c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_MCS07_MCS04)
6398c2ecf20Sopenharmony_ci		index = 11;
6408c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_MCS11_MCS08)
6418c2ecf20Sopenharmony_ci		index = 12;
6428c2ecf20Sopenharmony_ci	else if (regaddr == RTXAGC_B_MCS15_MCS12)
6438c2ecf20Sopenharmony_ci		index = 13;
6448c2ecf20Sopenharmony_ci	else
6458c2ecf20Sopenharmony_ci		return;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	rtlphy->mcs_offset[rtlphy->pwrgroup_cnt][index] = data;
6488c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
6498c2ecf20Sopenharmony_ci		"MCSTxPowerLevelOriginalOffset[%d][%d] = 0x%x\n",
6508c2ecf20Sopenharmony_ci		rtlphy->pwrgroup_cnt, index,
6518c2ecf20Sopenharmony_ci		rtlphy->mcs_offset[rtlphy->pwrgroup_cnt][index]);
6528c2ecf20Sopenharmony_ci	if (index == 13)
6538c2ecf20Sopenharmony_ci		rtlphy->pwrgroup_cnt++;
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
6578c2ecf20Sopenharmony_ci	u8 configtype)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6608c2ecf20Sopenharmony_ci	int i;
6618c2ecf20Sopenharmony_ci	u32 *phy_regarray_table_pg;
6628c2ecf20Sopenharmony_ci	u16 phy_regarray_pg_len;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	phy_regarray_pg_len = PHY_REG_ARRAY_PG_LENGTH;
6658c2ecf20Sopenharmony_ci	phy_regarray_table_pg = rtl8192de_phy_reg_array_pg;
6668c2ecf20Sopenharmony_ci	if (configtype == BASEBAND_CONFIG_PHY_REG) {
6678c2ecf20Sopenharmony_ci		for (i = 0; i < phy_regarray_pg_len; i = i + 3) {
6688c2ecf20Sopenharmony_ci			rtl_addr_delay(phy_regarray_table_pg[i]);
6698c2ecf20Sopenharmony_ci			_rtl92d_store_pwrindex_diffrate_offset(hw,
6708c2ecf20Sopenharmony_ci				phy_regarray_table_pg[i],
6718c2ecf20Sopenharmony_ci				phy_regarray_table_pg[i + 1],
6728c2ecf20Sopenharmony_ci				phy_regarray_table_pg[i + 2]);
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci	} else {
6758c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
6768c2ecf20Sopenharmony_ci			"configtype != BaseBand_Config_PHY_REG\n");
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci	return true;
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_bb_config(struct ieee80211_hw *hw)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6848c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
6858c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
6868c2ecf20Sopenharmony_ci	bool rtstatus = true;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "==>\n");
6898c2ecf20Sopenharmony_ci	rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
6908c2ecf20Sopenharmony_ci		BASEBAND_CONFIG_PHY_REG);
6918c2ecf20Sopenharmony_ci	if (!rtstatus) {
6928c2ecf20Sopenharmony_ci		pr_err("Write BB Reg Fail!!\n");
6938c2ecf20Sopenharmony_ci		return false;
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* if (rtlphy->rf_type == RF_1T2R) {
6978c2ecf20Sopenharmony_ci	 *      _rtl92c_phy_bb_config_1t(hw);
6988c2ecf20Sopenharmony_ci	 *     rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Config to 1T!!\n");
6998c2ecf20Sopenharmony_ci	 *} */
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	if (rtlefuse->autoload_failflag == false) {
7028c2ecf20Sopenharmony_ci		rtlphy->pwrgroup_cnt = 0;
7038c2ecf20Sopenharmony_ci		rtstatus = _rtl92d_phy_config_bb_with_pgheaderfile(hw,
7048c2ecf20Sopenharmony_ci			BASEBAND_CONFIG_PHY_REG);
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci	if (!rtstatus) {
7078c2ecf20Sopenharmony_ci		pr_err("BB_PG Reg Fail!!\n");
7088c2ecf20Sopenharmony_ci		return false;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci	rtstatus = _rtl92d_phy_config_bb_with_headerfile(hw,
7118c2ecf20Sopenharmony_ci		BASEBAND_CONFIG_AGC_TAB);
7128c2ecf20Sopenharmony_ci	if (!rtstatus) {
7138c2ecf20Sopenharmony_ci		pr_err("AGC Table Fail\n");
7148c2ecf20Sopenharmony_ci		return false;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci	rtlphy->cck_high_power = (bool) (rtl_get_bbreg(hw,
7178c2ecf20Sopenharmony_ci		RFPGA0_XA_HSSIPARAMETER2, 0x200));
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	return true;
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cibool rtl92d_phy_bb_config(struct ieee80211_hw *hw)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7258c2ecf20Sopenharmony_ci	u16 regval;
7268c2ecf20Sopenharmony_ci	u32 regvaldw;
7278c2ecf20Sopenharmony_ci	u8 value;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	_rtl92d_phy_init_bb_rf_register_definition(hw);
7308c2ecf20Sopenharmony_ci	regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
7318c2ecf20Sopenharmony_ci	rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
7328c2ecf20Sopenharmony_ci		       regval | BIT(13) | BIT(0) | BIT(1));
7338c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL, 0x83);
7348c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_AFE_PLL_CTRL + 1, 0xdb);
7358c2ecf20Sopenharmony_ci	/* 0x1f bit7 bit6 represent for mac0/mac1 driver ready */
7368c2ecf20Sopenharmony_ci	value = rtl_read_byte(rtlpriv, REG_RF_CTRL);
7378c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_RF_CTRL, value | RF_EN | RF_RSTB |
7388c2ecf20Sopenharmony_ci		RF_SDMRSTB);
7398c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, FEN_PPLL | FEN_PCIEA |
7408c2ecf20Sopenharmony_ci		FEN_DIO_PCIE | FEN_BB_GLB_RSTN | FEN_BBRSTB);
7418c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
7428c2ecf20Sopenharmony_ci	if (!(IS_92D_SINGLEPHY(rtlpriv->rtlhal.version))) {
7438c2ecf20Sopenharmony_ci		regvaldw = rtl_read_dword(rtlpriv, REG_LEDCFG0);
7448c2ecf20Sopenharmony_ci		rtl_write_dword(rtlpriv, REG_LEDCFG0, regvaldw | BIT(23));
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	return _rtl92d_phy_bb_config(hw);
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cibool rtl92d_phy_rf_config(struct ieee80211_hw *hw)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	return rtl92d_phy_rf6052_config(hw);
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cibool rtl92d_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
7568c2ecf20Sopenharmony_ci					  enum rf_content content,
7578c2ecf20Sopenharmony_ci					  enum radio_path rfpath)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	int i;
7608c2ecf20Sopenharmony_ci	u32 *radioa_array_table;
7618c2ecf20Sopenharmony_ci	u32 *radiob_array_table;
7628c2ecf20Sopenharmony_ci	u16 radioa_arraylen, radiob_arraylen;
7638c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	radioa_arraylen = RADIOA_2T_ARRAYLENGTH;
7668c2ecf20Sopenharmony_ci	radioa_array_table = rtl8192de_radioa_2tarray;
7678c2ecf20Sopenharmony_ci	radiob_arraylen = RADIOB_2T_ARRAYLENGTH;
7688c2ecf20Sopenharmony_ci	radiob_array_table = rtl8192de_radiob_2tarray;
7698c2ecf20Sopenharmony_ci	if (rtlpriv->efuse.internal_pa_5g[0]) {
7708c2ecf20Sopenharmony_ci		radioa_arraylen = RADIOA_2T_INT_PA_ARRAYLENGTH;
7718c2ecf20Sopenharmony_ci		radioa_array_table = rtl8192de_radioa_2t_int_paarray;
7728c2ecf20Sopenharmony_ci	}
7738c2ecf20Sopenharmony_ci	if (rtlpriv->efuse.internal_pa_5g[1]) {
7748c2ecf20Sopenharmony_ci		radiob_arraylen = RADIOB_2T_INT_PA_ARRAYLENGTH;
7758c2ecf20Sopenharmony_ci		radiob_array_table = rtl8192de_radiob_2t_int_paarray;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
7788c2ecf20Sopenharmony_ci		"PHY_ConfigRFWithHeaderFile() Radio_A:Rtl819XRadioA_1TArray\n");
7798c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
7808c2ecf20Sopenharmony_ci		"PHY_ConfigRFWithHeaderFile() Radio_B:Rtl819XRadioB_1TArray\n");
7818c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Radio No %x\n", rfpath);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	/* this only happens when DMDP, mac0 start on 2.4G,
7848c2ecf20Sopenharmony_ci	 * mac1 start on 5G, mac 0 has to set phy0&phy1
7858c2ecf20Sopenharmony_ci	 * pathA or mac1 has to set phy0&phy1 pathA */
7868c2ecf20Sopenharmony_ci	if ((content == radiob_txt) && (rfpath == RF90_PATH_A)) {
7878c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
7888c2ecf20Sopenharmony_ci			" ===> althougth Path A, we load radiob.txt\n");
7898c2ecf20Sopenharmony_ci		radioa_arraylen = radiob_arraylen;
7908c2ecf20Sopenharmony_ci		radioa_array_table = radiob_array_table;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci	switch (rfpath) {
7938c2ecf20Sopenharmony_ci	case RF90_PATH_A:
7948c2ecf20Sopenharmony_ci		for (i = 0; i < radioa_arraylen; i = i + 2) {
7958c2ecf20Sopenharmony_ci			rtl_rfreg_delay(hw, rfpath, radioa_array_table[i],
7968c2ecf20Sopenharmony_ci					RFREG_OFFSET_MASK,
7978c2ecf20Sopenharmony_ci					radioa_array_table[i + 1]);
7988c2ecf20Sopenharmony_ci		}
7998c2ecf20Sopenharmony_ci		break;
8008c2ecf20Sopenharmony_ci	case RF90_PATH_B:
8018c2ecf20Sopenharmony_ci		for (i = 0; i < radiob_arraylen; i = i + 2) {
8028c2ecf20Sopenharmony_ci			rtl_rfreg_delay(hw, rfpath, radiob_array_table[i],
8038c2ecf20Sopenharmony_ci					RFREG_OFFSET_MASK,
8048c2ecf20Sopenharmony_ci					radiob_array_table[i + 1]);
8058c2ecf20Sopenharmony_ci		}
8068c2ecf20Sopenharmony_ci		break;
8078c2ecf20Sopenharmony_ci	case RF90_PATH_C:
8088c2ecf20Sopenharmony_ci	case RF90_PATH_D:
8098c2ecf20Sopenharmony_ci		pr_err("switch case %#x not processed\n", rfpath);
8108c2ecf20Sopenharmony_ci		break;
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci	return true;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_civoid rtl92d_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
8188c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[0] =
8218c2ecf20Sopenharmony_ci	    (u8) rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
8228c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[1] =
8238c2ecf20Sopenharmony_ci	    (u8) rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
8248c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[2] =
8258c2ecf20Sopenharmony_ci	    (u8) rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
8268c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[3] =
8278c2ecf20Sopenharmony_ci	    (u8) rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
8288c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
8298c2ecf20Sopenharmony_ci		"Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
8308c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[0],
8318c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[1],
8328c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[2],
8338c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[3]);
8348c2ecf20Sopenharmony_ci	rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
8358c2ecf20Sopenharmony_ci					      MASKBYTE0);
8368c2ecf20Sopenharmony_ci	rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
8378c2ecf20Sopenharmony_ci					      MASKDWORD);
8388c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
8398c2ecf20Sopenharmony_ci		"Default framesync (0x%x) = 0x%x\n",
8408c2ecf20Sopenharmony_ci		ROFDM0_RXDETECTOR3, rtlphy->framesync);
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_cistatic void _rtl92d_get_txpower_index(struct ieee80211_hw *hw, u8 channel,
8448c2ecf20Sopenharmony_ci	u8 *cckpowerlevel, u8 *ofdmpowerlevel)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
8478c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
8488c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
8498c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
8508c2ecf20Sopenharmony_ci	u8 index = (channel - 1);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	/* 1. CCK */
8538c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
8548c2ecf20Sopenharmony_ci		/* RF-A */
8558c2ecf20Sopenharmony_ci		cckpowerlevel[RF90_PATH_A] =
8568c2ecf20Sopenharmony_ci				 rtlefuse->txpwrlevel_cck[RF90_PATH_A][index];
8578c2ecf20Sopenharmony_ci		/* RF-B */
8588c2ecf20Sopenharmony_ci		cckpowerlevel[RF90_PATH_B] =
8598c2ecf20Sopenharmony_ci				 rtlefuse->txpwrlevel_cck[RF90_PATH_B][index];
8608c2ecf20Sopenharmony_ci	} else {
8618c2ecf20Sopenharmony_ci		cckpowerlevel[RF90_PATH_A] = 0;
8628c2ecf20Sopenharmony_ci		cckpowerlevel[RF90_PATH_B] = 0;
8638c2ecf20Sopenharmony_ci	}
8648c2ecf20Sopenharmony_ci	/* 2. OFDM for 1S or 2S */
8658c2ecf20Sopenharmony_ci	if (rtlphy->rf_type == RF_1T2R || rtlphy->rf_type == RF_1T1R) {
8668c2ecf20Sopenharmony_ci		/*  Read HT 40 OFDM TX power */
8678c2ecf20Sopenharmony_ci		ofdmpowerlevel[RF90_PATH_A] =
8688c2ecf20Sopenharmony_ci		    rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_A][index];
8698c2ecf20Sopenharmony_ci		ofdmpowerlevel[RF90_PATH_B] =
8708c2ecf20Sopenharmony_ci		    rtlefuse->txpwrlevel_ht40_1s[RF90_PATH_B][index];
8718c2ecf20Sopenharmony_ci	} else if (rtlphy->rf_type == RF_2T2R) {
8728c2ecf20Sopenharmony_ci		/* Read HT 40 OFDM TX power */
8738c2ecf20Sopenharmony_ci		ofdmpowerlevel[RF90_PATH_A] =
8748c2ecf20Sopenharmony_ci		    rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_A][index];
8758c2ecf20Sopenharmony_ci		ofdmpowerlevel[RF90_PATH_B] =
8768c2ecf20Sopenharmony_ci		    rtlefuse->txpwrlevel_ht40_2s[RF90_PATH_B][index];
8778c2ecf20Sopenharmony_ci	}
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_cistatic void _rtl92d_ccxpower_index_check(struct ieee80211_hw *hw,
8818c2ecf20Sopenharmony_ci	u8 channel, u8 *cckpowerlevel, u8 *ofdmpowerlevel)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
8848c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	rtlphy->cur_cck_txpwridx = cckpowerlevel[0];
8878c2ecf20Sopenharmony_ci	rtlphy->cur_ofdm24g_txpwridx = ofdmpowerlevel[0];
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_cistatic u8 _rtl92c_phy_get_rightchnlplace(u8 chnl)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	u8 place = chnl;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	if (chnl > 14) {
8958c2ecf20Sopenharmony_ci		for (place = 14; place < sizeof(channel5g); place++) {
8968c2ecf20Sopenharmony_ci			if (channel5g[place] == chnl) {
8978c2ecf20Sopenharmony_ci				place++;
8988c2ecf20Sopenharmony_ci				break;
8998c2ecf20Sopenharmony_ci			}
9008c2ecf20Sopenharmony_ci		}
9018c2ecf20Sopenharmony_ci	}
9028c2ecf20Sopenharmony_ci	return place;
9038c2ecf20Sopenharmony_ci}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_civoid rtl92d_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
9088c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
9098c2ecf20Sopenharmony_ci	u8 cckpowerlevel[2], ofdmpowerlevel[2];
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	if (!rtlefuse->txpwr_fromeprom)
9128c2ecf20Sopenharmony_ci		return;
9138c2ecf20Sopenharmony_ci	channel = _rtl92c_phy_get_rightchnlplace(channel);
9148c2ecf20Sopenharmony_ci	_rtl92d_get_txpower_index(hw, channel, &cckpowerlevel[0],
9158c2ecf20Sopenharmony_ci		&ofdmpowerlevel[0]);
9168c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
9178c2ecf20Sopenharmony_ci		_rtl92d_ccxpower_index_check(hw, channel, &cckpowerlevel[0],
9188c2ecf20Sopenharmony_ci				&ofdmpowerlevel[0]);
9198c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G)
9208c2ecf20Sopenharmony_ci		rtl92d_phy_rf6052_set_cck_txpower(hw, &cckpowerlevel[0]);
9218c2ecf20Sopenharmony_ci	rtl92d_phy_rf6052_set_ofdm_txpower(hw, &ofdmpowerlevel[0], channel);
9228c2ecf20Sopenharmony_ci}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_civoid rtl92d_phy_set_bw_mode(struct ieee80211_hw *hw,
9258c2ecf20Sopenharmony_ci			    enum nl80211_channel_type ch_type)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
9288c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
9298c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
9308c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
9318c2ecf20Sopenharmony_ci	unsigned long flag = 0;
9328c2ecf20Sopenharmony_ci	u8 reg_prsr_rsc;
9338c2ecf20Sopenharmony_ci	u8 reg_bw_opmode;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	if (rtlphy->set_bwmode_inprogress)
9368c2ecf20Sopenharmony_ci		return;
9378c2ecf20Sopenharmony_ci	if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
9388c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
9398c2ecf20Sopenharmony_ci			"FALSE driver sleep or unload\n");
9408c2ecf20Sopenharmony_ci		return;
9418c2ecf20Sopenharmony_ci	}
9428c2ecf20Sopenharmony_ci	rtlphy->set_bwmode_inprogress = true;
9438c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "Switch to %s bandwidth\n",
9448c2ecf20Sopenharmony_ci		rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
9458c2ecf20Sopenharmony_ci		"20MHz" : "40MHz");
9468c2ecf20Sopenharmony_ci	reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
9478c2ecf20Sopenharmony_ci	reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
9488c2ecf20Sopenharmony_ci	switch (rtlphy->current_chan_bw) {
9498c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
9508c2ecf20Sopenharmony_ci		reg_bw_opmode |= BW_OPMODE_20MHZ;
9518c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
9528c2ecf20Sopenharmony_ci		break;
9538c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
9548c2ecf20Sopenharmony_ci		reg_bw_opmode &= ~BW_OPMODE_20MHZ;
9558c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci		reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
9588c2ecf20Sopenharmony_ci			(mac->cur_40_prime_sc << 5);
9598c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
9608c2ecf20Sopenharmony_ci		break;
9618c2ecf20Sopenharmony_ci	default:
9628c2ecf20Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n",
9638c2ecf20Sopenharmony_ci		       rtlphy->current_chan_bw);
9648c2ecf20Sopenharmony_ci		break;
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci	switch (rtlphy->current_chan_bw) {
9678c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
9688c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
9698c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
9708c2ecf20Sopenharmony_ci		/* SET BIT10 BIT11  for receive cck */
9718c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
9728c2ecf20Sopenharmony_ci			      BIT(11), 3);
9738c2ecf20Sopenharmony_ci		break;
9748c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
9758c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
9768c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
9778c2ecf20Sopenharmony_ci		/* Set Control channel to upper or lower.
9788c2ecf20Sopenharmony_ci		 * These settings are required only for 40MHz */
9798c2ecf20Sopenharmony_ci		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
9808c2ecf20Sopenharmony_ci			rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
9818c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCKSIDEBAND,
9828c2ecf20Sopenharmony_ci				(mac->cur_40_prime_sc >> 1));
9838c2ecf20Sopenharmony_ci			rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
9848c2ecf20Sopenharmony_ci		}
9858c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
9868c2ecf20Sopenharmony_ci		/* SET BIT10 BIT11  for receive cck */
9878c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10) |
9888c2ecf20Sopenharmony_ci			      BIT(11), 0);
9898c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
9908c2ecf20Sopenharmony_ci			(mac->cur_40_prime_sc ==
9918c2ecf20Sopenharmony_ci			HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
9928c2ecf20Sopenharmony_ci		break;
9938c2ecf20Sopenharmony_ci	default:
9948c2ecf20Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n",
9958c2ecf20Sopenharmony_ci		       rtlphy->current_chan_bw);
9968c2ecf20Sopenharmony_ci		break;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci	rtl92d_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
10008c2ecf20Sopenharmony_ci	rtlphy->set_bwmode_inprogress = false;
10018c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
10028c2ecf20Sopenharmony_ci}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_cistatic void _rtl92d_phy_stop_trx_before_changeband(struct ieee80211_hw *hw)
10058c2ecf20Sopenharmony_ci{
10068c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0);
10078c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0);
10088c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x00);
10098c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x0);
10108c2ecf20Sopenharmony_ci}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_cistatic void rtl92d_phy_switch_wirelessband(struct ieee80211_hw *hw, u8 band)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
10158c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
10168c2ecf20Sopenharmony_ci	u8 value8;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
10198c2ecf20Sopenharmony_ci	rtlhal->bandset = band;
10208c2ecf20Sopenharmony_ci	rtlhal->current_bandtype = band;
10218c2ecf20Sopenharmony_ci	if (IS_92D_SINGLEPHY(rtlhal->version))
10228c2ecf20Sopenharmony_ci		rtlhal->bandset = BAND_ON_BOTH;
10238c2ecf20Sopenharmony_ci	/* stop RX/Tx */
10248c2ecf20Sopenharmony_ci	_rtl92d_phy_stop_trx_before_changeband(hw);
10258c2ecf20Sopenharmony_ci	/* reconfig BB/RF according to wireless mode */
10268c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
10278c2ecf20Sopenharmony_ci		/* BB & RF Config */
10288c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_DMESG, "====>2.4G\n");
10298c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 1)
10308c2ecf20Sopenharmony_ci			_rtl92d_phy_config_bb_with_headerfile(hw,
10318c2ecf20Sopenharmony_ci				BASEBAND_CONFIG_AGC_TAB);
10328c2ecf20Sopenharmony_ci	} else {
10338c2ecf20Sopenharmony_ci		/* 5G band */
10348c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_DMESG, "====>5G\n");
10358c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 1)
10368c2ecf20Sopenharmony_ci			_rtl92d_phy_config_bb_with_headerfile(hw,
10378c2ecf20Sopenharmony_ci				BASEBAND_CONFIG_AGC_TAB);
10388c2ecf20Sopenharmony_ci	}
10398c2ecf20Sopenharmony_ci	rtl92d_update_bbrf_configuration(hw);
10408c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G)
10418c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0x1);
10428c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0x1);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	/* 20M BW. */
10458c2ecf20Sopenharmony_ci	/* rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1); */
10468c2ecf20Sopenharmony_ci	rtlhal->reloadtxpowerindex = true;
10478c2ecf20Sopenharmony_ci	/* notice fw know band status  0x81[1]/0x53[1] = 0: 5G, 1: 2G */
10488c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
10498c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv,	(rtlhal->interfaceindex ==
10508c2ecf20Sopenharmony_ci			0 ? REG_MAC0 : REG_MAC1));
10518c2ecf20Sopenharmony_ci		value8 |= BIT(1);
10528c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
10538c2ecf20Sopenharmony_ci			0 ? REG_MAC0 : REG_MAC1), value8);
10548c2ecf20Sopenharmony_ci	} else {
10558c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv, (rtlhal->interfaceindex ==
10568c2ecf20Sopenharmony_ci			0 ? REG_MAC0 : REG_MAC1));
10578c2ecf20Sopenharmony_ci		value8 &= (~BIT(1));
10588c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, (rtlhal->interfaceindex ==
10598c2ecf20Sopenharmony_ci			0 ? REG_MAC0 : REG_MAC1), value8);
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci	mdelay(1);
10628c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "<==Switch Band OK\n");
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic void _rtl92d_phy_reload_imr_setting(struct ieee80211_hw *hw,
10668c2ecf20Sopenharmony_ci	u8 channel, u8 rfpath)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
10698c2ecf20Sopenharmony_ci	u32 imr_num = MAX_RF_IMR_INDEX;
10708c2ecf20Sopenharmony_ci	u32 rfmask = RFREG_OFFSET_MASK;
10718c2ecf20Sopenharmony_ci	u8 group, i;
10728c2ecf20Sopenharmony_ci	unsigned long flag = 0;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>path %d\n", rfpath);
10758c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {
10768c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
10778c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
10788c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0xf);
10798c2ecf20Sopenharmony_ci		/* fc area 0xd2c */
10808c2ecf20Sopenharmony_ci		if (channel > 99)
10818c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
10828c2ecf20Sopenharmony_ci				      BIT(14), 2);
10838c2ecf20Sopenharmony_ci		else
10848c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(13) |
10858c2ecf20Sopenharmony_ci				      BIT(14), 1);
10868c2ecf20Sopenharmony_ci		/* leave 0 for channel1-14. */
10878c2ecf20Sopenharmony_ci		group = channel <= 64 ? 1 : 2;
10888c2ecf20Sopenharmony_ci		imr_num = MAX_RF_IMR_INDEX_NORMAL;
10898c2ecf20Sopenharmony_ci		for (i = 0; i < imr_num; i++)
10908c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)rfpath,
10918c2ecf20Sopenharmony_ci				      rf_reg_for_5g_swchnl_normal[i], rfmask,
10928c2ecf20Sopenharmony_ci				      rf_imr_param_normal[0][group][i]);
10938c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0x00f00000, 0);
10948c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 1);
10958c2ecf20Sopenharmony_ci	} else {
10968c2ecf20Sopenharmony_ci		/* G band. */
10978c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
10988c2ecf20Sopenharmony_ci			"Load RF IMR parameters for G band. IMR already setting %d\n",
10998c2ecf20Sopenharmony_ci			rtlpriv->rtlhal.load_imrandiqk_setting_for2g);
11008c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
11018c2ecf20Sopenharmony_ci		if (!rtlpriv->rtlhal.load_imrandiqk_setting_for2g) {
11028c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
11038c2ecf20Sopenharmony_ci				"Load RF IMR parameters for G band. %d\n",
11048c2ecf20Sopenharmony_ci				rfpath);
11058c2ecf20Sopenharmony_ci			rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
11068c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(25) | BIT(24), 0);
11078c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
11088c2ecf20Sopenharmony_ci				      0x00f00000, 0xf);
11098c2ecf20Sopenharmony_ci			imr_num = MAX_RF_IMR_INDEX_NORMAL;
11108c2ecf20Sopenharmony_ci			for (i = 0; i < imr_num; i++) {
11118c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)rfpath,
11128c2ecf20Sopenharmony_ci					      rf_reg_for_5g_swchnl_normal[i],
11138c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK,
11148c2ecf20Sopenharmony_ci					      rf_imr_param_normal[0][0][i]);
11158c2ecf20Sopenharmony_ci			}
11168c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4,
11178c2ecf20Sopenharmony_ci				      0x00f00000, 0);
11188c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN | BCCKEN, 3);
11198c2ecf20Sopenharmony_ci			rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
11208c2ecf20Sopenharmony_ci		}
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
11238c2ecf20Sopenharmony_ci}
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_cistatic void _rtl92d_phy_enable_rf_env(struct ieee80211_hw *hw,
11268c2ecf20Sopenharmony_ci	u8 rfpath, u32 *pu4_regval)
11278c2ecf20Sopenharmony_ci{
11288c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11298c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
11308c2ecf20Sopenharmony_ci	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD, "====>\n");
11338c2ecf20Sopenharmony_ci	/*----Store original RFENV control type----*/
11348c2ecf20Sopenharmony_ci	switch (rfpath) {
11358c2ecf20Sopenharmony_ci	case RF90_PATH_A:
11368c2ecf20Sopenharmony_ci	case RF90_PATH_C:
11378c2ecf20Sopenharmony_ci		*pu4_regval = rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV);
11388c2ecf20Sopenharmony_ci		break;
11398c2ecf20Sopenharmony_ci	case RF90_PATH_B:
11408c2ecf20Sopenharmony_ci	case RF90_PATH_D:
11418c2ecf20Sopenharmony_ci		*pu4_regval =
11428c2ecf20Sopenharmony_ci		    rtl_get_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16);
11438c2ecf20Sopenharmony_ci		break;
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci	/*----Set RF_ENV enable----*/
11468c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
11478c2ecf20Sopenharmony_ci	udelay(1);
11488c2ecf20Sopenharmony_ci	/*----Set RF_ENV output high----*/
11498c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
11508c2ecf20Sopenharmony_ci	udelay(1);
11518c2ecf20Sopenharmony_ci	/* Set bit number of Address and Data for RF register */
11528c2ecf20Sopenharmony_ci	/* Set 1 to 4 bits for 8255 */
11538c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREADDRESSLENGTH, 0x0);
11548c2ecf20Sopenharmony_ci	udelay(1);
11558c2ecf20Sopenharmony_ci	/*Set 0 to 12 bits for 8255 */
11568c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
11578c2ecf20Sopenharmony_ci	udelay(1);
11588c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD, "<====\n");
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_cistatic void _rtl92d_phy_restore_rf_env(struct ieee80211_hw *hw, u8 rfpath,
11628c2ecf20Sopenharmony_ci				       u32 *pu4_regval)
11638c2ecf20Sopenharmony_ci{
11648c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11658c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
11668c2ecf20Sopenharmony_ci	struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD, "=====>\n");
11698c2ecf20Sopenharmony_ci	/*----Restore RFENV control type----*/
11708c2ecf20Sopenharmony_ci	switch (rfpath) {
11718c2ecf20Sopenharmony_ci	case RF90_PATH_A:
11728c2ecf20Sopenharmony_ci	case RF90_PATH_C:
11738c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV, *pu4_regval);
11748c2ecf20Sopenharmony_ci		break;
11758c2ecf20Sopenharmony_ci	case RF90_PATH_B:
11768c2ecf20Sopenharmony_ci	case RF90_PATH_D:
11778c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfs, BRFSI_RFENV << 16,
11788c2ecf20Sopenharmony_ci			      *pu4_regval);
11798c2ecf20Sopenharmony_ci		break;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD, "<=====\n");
11828c2ecf20Sopenharmony_ci}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_cistatic void _rtl92d_phy_switch_rf_setting(struct ieee80211_hw *hw, u8 channel)
11858c2ecf20Sopenharmony_ci{
11868c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11878c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
11888c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
11898c2ecf20Sopenharmony_ci	u8 path = rtlhal->current_bandtype ==
11908c2ecf20Sopenharmony_ci	    BAND_ON_5G ? RF90_PATH_A : RF90_PATH_B;
11918c2ecf20Sopenharmony_ci	u8 index = 0, i = 0, rfpath = RF90_PATH_A;
11928c2ecf20Sopenharmony_ci	bool need_pwr_down = false, internal_pa = false;
11938c2ecf20Sopenharmony_ci	u32 u4regvalue, mask = 0x1C000, value = 0, u4tmp, u4tmp2;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>\n");
11968c2ecf20Sopenharmony_ci	/* config path A for 5G */
11978c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_5G) {
11988c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>5G\n");
11998c2ecf20Sopenharmony_ci		u4tmp = curveindex_5g[channel - 1];
12008c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
12018c2ecf20Sopenharmony_ci			"ver 1 set RF-A, 5G, 0x28 = 0x%x !!\n", u4tmp);
12028c2ecf20Sopenharmony_ci		for (i = 0; i < RF_CHNL_NUM_5G; i++) {
12038c2ecf20Sopenharmony_ci			if (channel == rf_chnl_5g[i] && channel <= 140)
12048c2ecf20Sopenharmony_ci				index = 0;
12058c2ecf20Sopenharmony_ci		}
12068c2ecf20Sopenharmony_ci		for (i = 0; i < RF_CHNL_NUM_5G_40M; i++) {
12078c2ecf20Sopenharmony_ci			if (channel == rf_chnl_5g_40m[i] && channel <= 140)
12088c2ecf20Sopenharmony_ci				index = 1;
12098c2ecf20Sopenharmony_ci		}
12108c2ecf20Sopenharmony_ci		if (channel == 149 || channel == 155 || channel == 161)
12118c2ecf20Sopenharmony_ci			index = 2;
12128c2ecf20Sopenharmony_ci		else if (channel == 151 || channel == 153 || channel == 163
12138c2ecf20Sopenharmony_ci			 || channel == 165)
12148c2ecf20Sopenharmony_ci			index = 3;
12158c2ecf20Sopenharmony_ci		else if (channel == 157 || channel == 159)
12168c2ecf20Sopenharmony_ci			index = 4;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci		if (rtlhal->macphymode == DUALMAC_DUALPHY
12198c2ecf20Sopenharmony_ci		    && rtlhal->interfaceindex == 1) {
12208c2ecf20Sopenharmony_ci			need_pwr_down = rtl92d_phy_enable_anotherphy(hw, false);
12218c2ecf20Sopenharmony_ci			rtlhal->during_mac1init_radioa = true;
12228c2ecf20Sopenharmony_ci			/* asume no this case */
12238c2ecf20Sopenharmony_ci			if (need_pwr_down)
12248c2ecf20Sopenharmony_ci				_rtl92d_phy_enable_rf_env(hw, path,
12258c2ecf20Sopenharmony_ci							  &u4regvalue);
12268c2ecf20Sopenharmony_ci		}
12278c2ecf20Sopenharmony_ci		for (i = 0; i < RF_REG_NUM_FOR_C_CUT_5G; i++) {
12288c2ecf20Sopenharmony_ci			if (i == 0 && (rtlhal->macphymode == DUALMAC_DUALPHY)) {
12298c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)path,
12308c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_5g[i],
12318c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK, 0xE439D);
12328c2ecf20Sopenharmony_ci			} else if (rf_reg_for_c_cut_5g[i] == RF_SYN_G4) {
12338c2ecf20Sopenharmony_ci				u4tmp2 = (rf_reg_pram_c_5g[index][i] &
12348c2ecf20Sopenharmony_ci				     0x7FF) | (u4tmp << 11);
12358c2ecf20Sopenharmony_ci				if (channel == 36)
12368c2ecf20Sopenharmony_ci					u4tmp2 &= ~(BIT(7) | BIT(6));
12378c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)path,
12388c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_5g[i],
12398c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK, u4tmp2);
12408c2ecf20Sopenharmony_ci			} else {
12418c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)path,
12428c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_5g[i],
12438c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK,
12448c2ecf20Sopenharmony_ci					      rf_reg_pram_c_5g[index][i]);
12458c2ecf20Sopenharmony_ci			}
12468c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
12478c2ecf20Sopenharmony_ci				"offset 0x%x value 0x%x path %d index %d readback 0x%x\n",
12488c2ecf20Sopenharmony_ci				rf_reg_for_c_cut_5g[i],
12498c2ecf20Sopenharmony_ci				rf_reg_pram_c_5g[index][i],
12508c2ecf20Sopenharmony_ci				path, index,
12518c2ecf20Sopenharmony_ci				rtl_get_rfreg(hw, (enum radio_path)path,
12528c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_5g[i],
12538c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK));
12548c2ecf20Sopenharmony_ci		}
12558c2ecf20Sopenharmony_ci		if (need_pwr_down)
12568c2ecf20Sopenharmony_ci			_rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
12578c2ecf20Sopenharmony_ci		if (rtlhal->during_mac1init_radioa)
12588c2ecf20Sopenharmony_ci			rtl92d_phy_powerdown_anotherphy(hw, false);
12598c2ecf20Sopenharmony_ci		if (channel < 149)
12608c2ecf20Sopenharmony_ci			value = 0x07;
12618c2ecf20Sopenharmony_ci		else if (channel >= 149)
12628c2ecf20Sopenharmony_ci			value = 0x02;
12638c2ecf20Sopenharmony_ci		if (channel >= 36 && channel <= 64)
12648c2ecf20Sopenharmony_ci			index = 0;
12658c2ecf20Sopenharmony_ci		else if (channel >= 100 && channel <= 140)
12668c2ecf20Sopenharmony_ci			index = 1;
12678c2ecf20Sopenharmony_ci		else
12688c2ecf20Sopenharmony_ci			index = 2;
12698c2ecf20Sopenharmony_ci		for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
12708c2ecf20Sopenharmony_ci			rfpath++) {
12718c2ecf20Sopenharmony_ci			if (rtlhal->macphymode == DUALMAC_DUALPHY &&
12728c2ecf20Sopenharmony_ci				rtlhal->interfaceindex == 1)	/* MAC 1 5G */
12738c2ecf20Sopenharmony_ci				internal_pa = rtlpriv->efuse.internal_pa_5g[1];
12748c2ecf20Sopenharmony_ci			else
12758c2ecf20Sopenharmony_ci				internal_pa =
12768c2ecf20Sopenharmony_ci					 rtlpriv->efuse.internal_pa_5g[rfpath];
12778c2ecf20Sopenharmony_ci			if (internal_pa) {
12788c2ecf20Sopenharmony_ci				for (i = 0;
12798c2ecf20Sopenharmony_ci				     i < RF_REG_NUM_FOR_C_CUT_5G_INTERNALPA;
12808c2ecf20Sopenharmony_ci				     i++) {
12818c2ecf20Sopenharmony_ci					rtl_set_rfreg(hw, rfpath,
12828c2ecf20Sopenharmony_ci						rf_for_c_cut_5g_internal_pa[i],
12838c2ecf20Sopenharmony_ci						RFREG_OFFSET_MASK,
12848c2ecf20Sopenharmony_ci						rf_pram_c_5g_int_pa[index][i]);
12858c2ecf20Sopenharmony_ci					rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD,
12868c2ecf20Sopenharmony_ci						"offset 0x%x value 0x%x path %d index %d\n",
12878c2ecf20Sopenharmony_ci						rf_for_c_cut_5g_internal_pa[i],
12888c2ecf20Sopenharmony_ci						rf_pram_c_5g_int_pa[index][i],
12898c2ecf20Sopenharmony_ci						rfpath, index);
12908c2ecf20Sopenharmony_ci				}
12918c2ecf20Sopenharmony_ci			} else {
12928c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
12938c2ecf20Sopenharmony_ci					      mask, value);
12948c2ecf20Sopenharmony_ci			}
12958c2ecf20Sopenharmony_ci		}
12968c2ecf20Sopenharmony_ci	} else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
12978c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "====>2.4G\n");
12988c2ecf20Sopenharmony_ci		u4tmp = curveindex_2g[channel - 1];
12998c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
13008c2ecf20Sopenharmony_ci			"ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", u4tmp);
13018c2ecf20Sopenharmony_ci		if (channel == 1 || channel == 2 || channel == 4 || channel == 9
13028c2ecf20Sopenharmony_ci		    || channel == 10 || channel == 11 || channel == 12)
13038c2ecf20Sopenharmony_ci			index = 0;
13048c2ecf20Sopenharmony_ci		else if (channel == 3 || channel == 13 || channel == 14)
13058c2ecf20Sopenharmony_ci			index = 1;
13068c2ecf20Sopenharmony_ci		else if (channel >= 5 && channel <= 8)
13078c2ecf20Sopenharmony_ci			index = 2;
13088c2ecf20Sopenharmony_ci		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
13098c2ecf20Sopenharmony_ci			path = RF90_PATH_A;
13108c2ecf20Sopenharmony_ci			if (rtlhal->interfaceindex == 0) {
13118c2ecf20Sopenharmony_ci				need_pwr_down =
13128c2ecf20Sopenharmony_ci					 rtl92d_phy_enable_anotherphy(hw, true);
13138c2ecf20Sopenharmony_ci				rtlhal->during_mac0init_radiob = true;
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci				if (need_pwr_down)
13168c2ecf20Sopenharmony_ci					_rtl92d_phy_enable_rf_env(hw, path,
13178c2ecf20Sopenharmony_ci								  &u4regvalue);
13188c2ecf20Sopenharmony_ci			}
13198c2ecf20Sopenharmony_ci		}
13208c2ecf20Sopenharmony_ci		for (i = 0; i < RF_REG_NUM_FOR_C_CUT_2G; i++) {
13218c2ecf20Sopenharmony_ci			if (rf_reg_for_c_cut_2g[i] == RF_SYN_G7)
13228c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)path,
13238c2ecf20Sopenharmony_ci					rf_reg_for_c_cut_2g[i],
13248c2ecf20Sopenharmony_ci					RFREG_OFFSET_MASK,
13258c2ecf20Sopenharmony_ci					(rf_reg_param_for_c_cut_2g[index][i] |
13268c2ecf20Sopenharmony_ci					BIT(17)));
13278c2ecf20Sopenharmony_ci			else
13288c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)path,
13298c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_2g[i],
13308c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK,
13318c2ecf20Sopenharmony_ci					      rf_reg_param_for_c_cut_2g
13328c2ecf20Sopenharmony_ci					      [index][i]);
13338c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
13348c2ecf20Sopenharmony_ci				"offset 0x%x value 0x%x mak 0x%x path %d index %d readback 0x%x\n",
13358c2ecf20Sopenharmony_ci				rf_reg_for_c_cut_2g[i],
13368c2ecf20Sopenharmony_ci				rf_reg_param_for_c_cut_2g[index][i],
13378c2ecf20Sopenharmony_ci				rf_reg_mask_for_c_cut_2g[i], path, index,
13388c2ecf20Sopenharmony_ci				rtl_get_rfreg(hw, (enum radio_path)path,
13398c2ecf20Sopenharmony_ci					      rf_reg_for_c_cut_2g[i],
13408c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK));
13418c2ecf20Sopenharmony_ci		}
13428c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
13438c2ecf20Sopenharmony_ci			"cosa ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n",
13448c2ecf20Sopenharmony_ci			rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, (enum radio_path)path, RF_SYN_G4,
13478c2ecf20Sopenharmony_ci			      RFREG_OFFSET_MASK,
13488c2ecf20Sopenharmony_ci			      rf_syn_g4_for_c_cut_2g | (u4tmp << 11));
13498c2ecf20Sopenharmony_ci		if (need_pwr_down)
13508c2ecf20Sopenharmony_ci			_rtl92d_phy_restore_rf_env(hw, path, &u4regvalue);
13518c2ecf20Sopenharmony_ci		if (rtlhal->during_mac0init_radiob)
13528c2ecf20Sopenharmony_ci			rtl92d_phy_powerdown_anotherphy(hw, true);
13538c2ecf20Sopenharmony_ci	}
13548c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
13558c2ecf20Sopenharmony_ci}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ciu8 rtl92d_get_rightchnlplace_for_iqk(u8 chnl)
13588c2ecf20Sopenharmony_ci{
13598c2ecf20Sopenharmony_ci	u8 place = chnl;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	if (chnl > 14) {
13628c2ecf20Sopenharmony_ci		for (place = 14; place < sizeof(channel_all); place++) {
13638c2ecf20Sopenharmony_ci			if (channel_all[place] == chnl)
13648c2ecf20Sopenharmony_ci				return place - 13;
13658c2ecf20Sopenharmony_ci		}
13668c2ecf20Sopenharmony_ci	}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	return 0;
13698c2ecf20Sopenharmony_ci}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci#define MAX_TOLERANCE		5
13728c2ecf20Sopenharmony_ci#define IQK_DELAY_TIME		1	/* ms */
13738c2ecf20Sopenharmony_ci#define MAX_TOLERANCE_92D	3
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
13768c2ecf20Sopenharmony_cistatic u8 _rtl92d_phy_patha_iqk(struct ieee80211_hw *hw, bool configpathb)
13778c2ecf20Sopenharmony_ci{
13788c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
13798c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
13808c2ecf20Sopenharmony_ci	u32 regeac, rege94, rege9c, regea4;
13818c2ecf20Sopenharmony_ci	u8 result = 0;
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK!\n");
13848c2ecf20Sopenharmony_ci	/* path-A IQK setting */
13858c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
13868c2ecf20Sopenharmony_ci	if (rtlhal->interfaceindex == 0) {
13878c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x10008c1f);
13888c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x10008c1f);
13898c2ecf20Sopenharmony_ci	} else {
13908c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x10008c22);
13918c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x10008c22);
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82140102);
13948c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x28160206);
13958c2ecf20Sopenharmony_ci	/* path-B IQK setting */
13968c2ecf20Sopenharmony_ci	if (configpathb) {
13978c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x10008c22);
13988c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x10008c22);
13998c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82140102);
14008c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x28160206);
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci	/* LO calibration setting */
14038c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
14048c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
14058c2ecf20Sopenharmony_ci	/* One shot, path A LOK & IQK */
14068c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "One shot, path A LOK & IQK!\n");
14078c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf9000000);
14088c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
14098c2ecf20Sopenharmony_ci	/* delay x ms */
14108c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
14118c2ecf20Sopenharmony_ci		"Delay %d ms for One shot, path A LOK & IQK\n",
14128c2ecf20Sopenharmony_ci		IQK_DELAY_TIME);
14138c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
14148c2ecf20Sopenharmony_ci	/* Check failed */
14158c2ecf20Sopenharmony_ci	regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
14168c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
14178c2ecf20Sopenharmony_ci	rege94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
14188c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe94 = 0x%x\n", rege94);
14198c2ecf20Sopenharmony_ci	rege9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
14208c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe9c = 0x%x\n", rege9c);
14218c2ecf20Sopenharmony_ci	regea4 = rtl_get_bbreg(hw, 0xea4, MASKDWORD);
14228c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xea4 = 0x%x\n", regea4);
14238c2ecf20Sopenharmony_ci	if (!(regeac & BIT(28)) && (((rege94 & 0x03FF0000) >> 16) != 0x142) &&
14248c2ecf20Sopenharmony_ci	    (((rege9c & 0x03FF0000) >> 16) != 0x42))
14258c2ecf20Sopenharmony_ci		result |= 0x01;
14268c2ecf20Sopenharmony_ci	else			/* if Tx not OK, ignore Rx */
14278c2ecf20Sopenharmony_ci		return result;
14288c2ecf20Sopenharmony_ci	/* if Tx is OK, check whether Rx is OK */
14298c2ecf20Sopenharmony_ci	if (!(regeac & BIT(27)) && (((regea4 & 0x03FF0000) >> 16) != 0x132) &&
14308c2ecf20Sopenharmony_ci	    (((regeac & 0x03FF0000) >> 16) != 0x36))
14318c2ecf20Sopenharmony_ci		result |= 0x02;
14328c2ecf20Sopenharmony_ci	else
14338c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A Rx IQK fail!!\n");
14348c2ecf20Sopenharmony_ci	return result;
14358c2ecf20Sopenharmony_ci}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
14388c2ecf20Sopenharmony_cistatic u8 _rtl92d_phy_patha_iqk_5g_normal(struct ieee80211_hw *hw,
14398c2ecf20Sopenharmony_ci					  bool configpathb)
14408c2ecf20Sopenharmony_ci{
14418c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
14428c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
14438c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
14448c2ecf20Sopenharmony_ci	u32 regeac, rege94, rege9c, regea4;
14458c2ecf20Sopenharmony_ci	u8 result = 0;
14468c2ecf20Sopenharmony_ci	u8 i;
14478c2ecf20Sopenharmony_ci	u8 retrycount = 2;
14488c2ecf20Sopenharmony_ci	u32 TXOKBIT = BIT(28), RXOKBIT = BIT(27);
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	if (rtlhal->interfaceindex == 1) {	/* PHY1 */
14518c2ecf20Sopenharmony_ci		TXOKBIT = BIT(31);
14528c2ecf20Sopenharmony_ci		RXOKBIT = BIT(30);
14538c2ecf20Sopenharmony_ci	}
14548c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK!\n");
14558c2ecf20Sopenharmony_ci	/* path-A IQK setting */
14568c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
14578c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x18008c1f);
14588c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x18008c1f);
14598c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82140307);
14608c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x68160960);
14618c2ecf20Sopenharmony_ci	/* path-B IQK setting */
14628c2ecf20Sopenharmony_ci	if (configpathb) {
14638c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x18008c2f);
14648c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x18008c2f);
14658c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82110000);
14668c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x68110000);
14678c2ecf20Sopenharmony_ci	}
14688c2ecf20Sopenharmony_ci	/* LO calibration setting */
14698c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
14708c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
14718c2ecf20Sopenharmony_ci	/* path-A PA on */
14728c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD, 0x07000f60);
14738c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, MASKDWORD, 0x66e60e30);
14748c2ecf20Sopenharmony_ci	for (i = 0; i < retrycount; i++) {
14758c2ecf20Sopenharmony_ci		/* One shot, path A LOK & IQK */
14768c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
14778c2ecf20Sopenharmony_ci			"One shot, path A LOK & IQK!\n");
14788c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf9000000);
14798c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
14808c2ecf20Sopenharmony_ci		/* delay x ms */
14818c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
14828c2ecf20Sopenharmony_ci			"Delay %d ms for One shot, path A LOK & IQK.\n",
14838c2ecf20Sopenharmony_ci			IQK_DELAY_TIME);
14848c2ecf20Sopenharmony_ci		mdelay(IQK_DELAY_TIME * 10);
14858c2ecf20Sopenharmony_ci		/* Check failed */
14868c2ecf20Sopenharmony_ci		regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
14878c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
14888c2ecf20Sopenharmony_ci		rege94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
14898c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe94 = 0x%x\n", rege94);
14908c2ecf20Sopenharmony_ci		rege9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
14918c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xe9c = 0x%x\n", rege9c);
14928c2ecf20Sopenharmony_ci		regea4 = rtl_get_bbreg(hw, 0xea4, MASKDWORD);
14938c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xea4 = 0x%x\n", regea4);
14948c2ecf20Sopenharmony_ci		if (!(regeac & TXOKBIT) &&
14958c2ecf20Sopenharmony_ci		     (((rege94 & 0x03FF0000) >> 16) != 0x142)) {
14968c2ecf20Sopenharmony_ci			result |= 0x01;
14978c2ecf20Sopenharmony_ci		} else { /* if Tx not OK, ignore Rx */
14988c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
14998c2ecf20Sopenharmony_ci				"Path A Tx IQK fail!!\n");
15008c2ecf20Sopenharmony_ci			continue;
15018c2ecf20Sopenharmony_ci		}
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci		/* if Tx is OK, check whether Rx is OK */
15048c2ecf20Sopenharmony_ci		if (!(regeac & RXOKBIT) &&
15058c2ecf20Sopenharmony_ci		    (((regea4 & 0x03FF0000) >> 16) != 0x132)) {
15068c2ecf20Sopenharmony_ci			result |= 0x02;
15078c2ecf20Sopenharmony_ci			break;
15088c2ecf20Sopenharmony_ci		} else {
15098c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
15108c2ecf20Sopenharmony_ci				"Path A Rx IQK fail!!\n");
15118c2ecf20Sopenharmony_ci		}
15128c2ecf20Sopenharmony_ci	}
15138c2ecf20Sopenharmony_ci	/* path A PA off */
15148c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD,
15158c2ecf20Sopenharmony_ci		      rtlphy->iqk_bb_backup[0]);
15168c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, MASKDWORD,
15178c2ecf20Sopenharmony_ci		      rtlphy->iqk_bb_backup[1]);
15188c2ecf20Sopenharmony_ci	return result;
15198c2ecf20Sopenharmony_ci}
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
15228c2ecf20Sopenharmony_cistatic u8 _rtl92d_phy_pathb_iqk(struct ieee80211_hw *hw)
15238c2ecf20Sopenharmony_ci{
15248c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
15258c2ecf20Sopenharmony_ci	u32 regeac, regeb4, regebc, regec4, regecc;
15268c2ecf20Sopenharmony_ci	u8 result = 0;
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B IQK!\n");
15298c2ecf20Sopenharmony_ci	/* One shot, path B LOK & IQK */
15308c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "One shot, path A LOK & IQK!\n");
15318c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000002);
15328c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0x00000000);
15338c2ecf20Sopenharmony_ci	/* delay x ms  */
15348c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
15358c2ecf20Sopenharmony_ci		"Delay %d ms for One shot, path B LOK & IQK\n", IQK_DELAY_TIME);
15368c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
15378c2ecf20Sopenharmony_ci	/* Check failed */
15388c2ecf20Sopenharmony_ci	regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
15398c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
15408c2ecf20Sopenharmony_ci	regeb4 = rtl_get_bbreg(hw, 0xeb4, MASKDWORD);
15418c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeb4 = 0x%x\n", regeb4);
15428c2ecf20Sopenharmony_ci	regebc = rtl_get_bbreg(hw, 0xebc, MASKDWORD);
15438c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xebc = 0x%x\n", regebc);
15448c2ecf20Sopenharmony_ci	regec4 = rtl_get_bbreg(hw, 0xec4, MASKDWORD);
15458c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xec4 = 0x%x\n", regec4);
15468c2ecf20Sopenharmony_ci	regecc = rtl_get_bbreg(hw, 0xecc, MASKDWORD);
15478c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xecc = 0x%x\n", regecc);
15488c2ecf20Sopenharmony_ci	if (!(regeac & BIT(31)) && (((regeb4 & 0x03FF0000) >> 16) != 0x142) &&
15498c2ecf20Sopenharmony_ci	    (((regebc & 0x03FF0000) >> 16) != 0x42))
15508c2ecf20Sopenharmony_ci		result |= 0x01;
15518c2ecf20Sopenharmony_ci	else
15528c2ecf20Sopenharmony_ci		return result;
15538c2ecf20Sopenharmony_ci	if (!(regeac & BIT(30)) && (((regec4 & 0x03FF0000) >> 16) != 0x132) &&
15548c2ecf20Sopenharmony_ci	    (((regecc & 0x03FF0000) >> 16) != 0x36))
15558c2ecf20Sopenharmony_ci		result |= 0x02;
15568c2ecf20Sopenharmony_ci	else
15578c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B Rx IQK fail!!\n");
15588c2ecf20Sopenharmony_ci	return result;
15598c2ecf20Sopenharmony_ci}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
15628c2ecf20Sopenharmony_cistatic u8 _rtl92d_phy_pathb_iqk_5g_normal(struct ieee80211_hw *hw)
15638c2ecf20Sopenharmony_ci{
15648c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
15658c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
15668c2ecf20Sopenharmony_ci	u32 regeac, regeb4, regebc, regec4, regecc;
15678c2ecf20Sopenharmony_ci	u8 result = 0;
15688c2ecf20Sopenharmony_ci	u8 i;
15698c2ecf20Sopenharmony_ci	u8 retrycount = 2;
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path B IQK!\n");
15728c2ecf20Sopenharmony_ci	/* path-A IQK setting */
15738c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A IQK setting!\n");
15748c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x18008c1f);
15758c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x18008c1f);
15768c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe38, MASKDWORD, 0x82110000);
15778c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe3c, MASKDWORD, 0x68110000);
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	/* path-B IQK setting */
15808c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe50, MASKDWORD, 0x18008c2f);
15818c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe54, MASKDWORD, 0x18008c2f);
15828c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe58, MASKDWORD, 0x82140307);
15838c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe5c, MASKDWORD, 0x68160960);
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	/* LO calibration setting */
15868c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LO calibration setting!\n");
15878c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe4c, MASKDWORD, 0x00462911);
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	/* path-B PA on */
15908c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD, 0x0f600700);
15918c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, MASKDWORD, 0x061f0d30);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	for (i = 0; i < retrycount; i++) {
15948c2ecf20Sopenharmony_ci		/* One shot, path B LOK & IQK */
15958c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
15968c2ecf20Sopenharmony_ci			"One shot, path A LOK & IQK!\n");
15978c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xfa000000);
15988c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe48, MASKDWORD, 0xf8000000);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci		/* delay x ms */
16018c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
16028c2ecf20Sopenharmony_ci			"Delay %d ms for One shot, path B LOK & IQK.\n", 10);
16038c2ecf20Sopenharmony_ci		mdelay(IQK_DELAY_TIME * 10);
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci		/* Check failed */
16068c2ecf20Sopenharmony_ci		regeac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
16078c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeac = 0x%x\n", regeac);
16088c2ecf20Sopenharmony_ci		regeb4 = rtl_get_bbreg(hw, 0xeb4, MASKDWORD);
16098c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xeb4 = 0x%x\n", regeb4);
16108c2ecf20Sopenharmony_ci		regebc = rtl_get_bbreg(hw, 0xebc, MASKDWORD);
16118c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xebc = 0x%x\n", regebc);
16128c2ecf20Sopenharmony_ci		regec4 = rtl_get_bbreg(hw, 0xec4, MASKDWORD);
16138c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xec4 = 0x%x\n", regec4);
16148c2ecf20Sopenharmony_ci		regecc = rtl_get_bbreg(hw, 0xecc, MASKDWORD);
16158c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "0xecc = 0x%x\n", regecc);
16168c2ecf20Sopenharmony_ci		if (!(regeac & BIT(31)) &&
16178c2ecf20Sopenharmony_ci		    (((regeb4 & 0x03FF0000) >> 16) != 0x142))
16188c2ecf20Sopenharmony_ci			result |= 0x01;
16198c2ecf20Sopenharmony_ci		else
16208c2ecf20Sopenharmony_ci			continue;
16218c2ecf20Sopenharmony_ci		if (!(regeac & BIT(30)) &&
16228c2ecf20Sopenharmony_ci		    (((regec4 & 0x03FF0000) >> 16) != 0x132)) {
16238c2ecf20Sopenharmony_ci			result |= 0x02;
16248c2ecf20Sopenharmony_ci			break;
16258c2ecf20Sopenharmony_ci		} else {
16268c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
16278c2ecf20Sopenharmony_ci				"Path B Rx IQK fail!!\n");
16288c2ecf20Sopenharmony_ci		}
16298c2ecf20Sopenharmony_ci	}
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	/* path B PA off */
16328c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW, MASKDWORD,
16338c2ecf20Sopenharmony_ci		      rtlphy->iqk_bb_backup[0]);
16348c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, MASKDWORD,
16358c2ecf20Sopenharmony_ci		      rtlphy->iqk_bb_backup[2]);
16368c2ecf20Sopenharmony_ci	return result;
16378c2ecf20Sopenharmony_ci}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_cistatic void _rtl92d_phy_save_adda_registers(struct ieee80211_hw *hw,
16408c2ecf20Sopenharmony_ci					    u32 *adda_reg, u32 *adda_backup,
16418c2ecf20Sopenharmony_ci					    u32 regnum)
16428c2ecf20Sopenharmony_ci{
16438c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16448c2ecf20Sopenharmony_ci	u32 i;
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Save ADDA parameters.\n");
16478c2ecf20Sopenharmony_ci	for (i = 0; i < regnum; i++)
16488c2ecf20Sopenharmony_ci		adda_backup[i] = rtl_get_bbreg(hw, adda_reg[i], MASKDWORD);
16498c2ecf20Sopenharmony_ci}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_cistatic void _rtl92d_phy_save_mac_registers(struct ieee80211_hw *hw,
16528c2ecf20Sopenharmony_ci	u32 *macreg, u32 *macbackup)
16538c2ecf20Sopenharmony_ci{
16548c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16558c2ecf20Sopenharmony_ci	u32 i;
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Save MAC parameters.\n");
16588c2ecf20Sopenharmony_ci	for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
16598c2ecf20Sopenharmony_ci		macbackup[i] = rtl_read_byte(rtlpriv, macreg[i]);
16608c2ecf20Sopenharmony_ci	macbackup[i] = rtl_read_dword(rtlpriv, macreg[i]);
16618c2ecf20Sopenharmony_ci}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_cistatic void _rtl92d_phy_reload_adda_registers(struct ieee80211_hw *hw,
16648c2ecf20Sopenharmony_ci					      u32 *adda_reg, u32 *adda_backup,
16658c2ecf20Sopenharmony_ci					      u32 regnum)
16668c2ecf20Sopenharmony_ci{
16678c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16688c2ecf20Sopenharmony_ci	u32 i;
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
16718c2ecf20Sopenharmony_ci		"Reload ADDA power saving parameters !\n");
16728c2ecf20Sopenharmony_ci	for (i = 0; i < regnum; i++)
16738c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, adda_reg[i], MASKDWORD, adda_backup[i]);
16748c2ecf20Sopenharmony_ci}
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_cistatic void _rtl92d_phy_reload_mac_registers(struct ieee80211_hw *hw,
16778c2ecf20Sopenharmony_ci					     u32 *macreg, u32 *macbackup)
16788c2ecf20Sopenharmony_ci{
16798c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16808c2ecf20Sopenharmony_ci	u32 i;
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Reload MAC parameters !\n");
16838c2ecf20Sopenharmony_ci	for (i = 0; i < (IQK_MAC_REG_NUM - 1); i++)
16848c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, macreg[i], (u8) macbackup[i]);
16858c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, macreg[i], macbackup[i]);
16868c2ecf20Sopenharmony_ci}
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_cistatic void _rtl92d_phy_path_adda_on(struct ieee80211_hw *hw,
16898c2ecf20Sopenharmony_ci		u32 *adda_reg, bool patha_on, bool is2t)
16908c2ecf20Sopenharmony_ci{
16918c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
16928c2ecf20Sopenharmony_ci	u32 pathon;
16938c2ecf20Sopenharmony_ci	u32 i;
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "ADDA ON.\n");
16968c2ecf20Sopenharmony_ci	pathon = patha_on ? 0x04db25a4 : 0x0b1b25a4;
16978c2ecf20Sopenharmony_ci	if (patha_on)
16988c2ecf20Sopenharmony_ci		pathon = rtlpriv->rtlhal.interfaceindex == 0 ?
16998c2ecf20Sopenharmony_ci		    0x04db25a4 : 0x0b1b25a4;
17008c2ecf20Sopenharmony_ci	for (i = 0; i < IQK_ADDA_REG_NUM; i++)
17018c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, adda_reg[i], MASKDWORD, pathon);
17028c2ecf20Sopenharmony_ci}
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_cistatic void _rtl92d_phy_mac_setting_calibration(struct ieee80211_hw *hw,
17058c2ecf20Sopenharmony_ci						u32 *macreg, u32 *macbackup)
17068c2ecf20Sopenharmony_ci{
17078c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17088c2ecf20Sopenharmony_ci	u32 i;
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "MAC settings for Calibration.\n");
17118c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, macreg[0], 0x3F);
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	for (i = 1; i < (IQK_MAC_REG_NUM - 1); i++)
17148c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, macreg[i], (u8)(macbackup[i] &
17158c2ecf20Sopenharmony_ci			       (~BIT(3))));
17168c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, macreg[i], (u8) (macbackup[i] & (~BIT(5))));
17178c2ecf20Sopenharmony_ci}
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_cistatic void _rtl92d_phy_patha_standby(struct ieee80211_hw *hw)
17208c2ecf20Sopenharmony_ci{
17218c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17228c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path-A standby mode!\n");
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x0);
17258c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, MASKDWORD, 0x00010000);
17268c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
17278c2ecf20Sopenharmony_ci}
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_cistatic void _rtl92d_phy_pimode_switch(struct ieee80211_hw *hw, bool pi_mode)
17308c2ecf20Sopenharmony_ci{
17318c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17328c2ecf20Sopenharmony_ci	u32 mode;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
17358c2ecf20Sopenharmony_ci		"BB Switch to %s mode!\n", pi_mode ? "PI" : "SI");
17368c2ecf20Sopenharmony_ci	mode = pi_mode ? 0x01000100 : 0x01000000;
17378c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x820, MASKDWORD, mode);
17388c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x828, MASKDWORD, mode);
17398c2ecf20Sopenharmony_ci}
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_cistatic void _rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw, long result[][8],
17428c2ecf20Sopenharmony_ci				     u8 t, bool is2t)
17438c2ecf20Sopenharmony_ci{
17448c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17458c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
17468c2ecf20Sopenharmony_ci	u32 i;
17478c2ecf20Sopenharmony_ci	u8 patha_ok, pathb_ok;
17488c2ecf20Sopenharmony_ci	static u32 adda_reg[IQK_ADDA_REG_NUM] = {
17498c2ecf20Sopenharmony_ci		RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
17508c2ecf20Sopenharmony_ci		0xe78, 0xe7c, 0xe80, 0xe84,
17518c2ecf20Sopenharmony_ci		0xe88, 0xe8c, 0xed0, 0xed4,
17528c2ecf20Sopenharmony_ci		0xed8, 0xedc, 0xee0, 0xeec
17538c2ecf20Sopenharmony_ci	};
17548c2ecf20Sopenharmony_ci	static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
17558c2ecf20Sopenharmony_ci		0x522, 0x550, 0x551, 0x040
17568c2ecf20Sopenharmony_ci	};
17578c2ecf20Sopenharmony_ci	static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
17588c2ecf20Sopenharmony_ci		RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
17598c2ecf20Sopenharmony_ci		RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
17608c2ecf20Sopenharmony_ci		RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
17618c2ecf20Sopenharmony_ci		RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
17628c2ecf20Sopenharmony_ci		ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
17638c2ecf20Sopenharmony_ci	};
17648c2ecf20Sopenharmony_ci	const u32 retrycount = 2;
17658c2ecf20Sopenharmony_ci	u32 bbvalue;
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK for 2.4G :Start!!!\n");
17688c2ecf20Sopenharmony_ci	if (t == 0) {
17698c2ecf20Sopenharmony_ci		bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, MASKDWORD);
17708c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "==>0x%08x\n", bbvalue);
17718c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
17728c2ecf20Sopenharmony_ci			is2t ? "2T2R" : "1T1R");
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci		/*  Save ADDA parameters, turn Path A ADDA on */
17758c2ecf20Sopenharmony_ci		_rtl92d_phy_save_adda_registers(hw, adda_reg,
17768c2ecf20Sopenharmony_ci			rtlphy->adda_backup, IQK_ADDA_REG_NUM);
17778c2ecf20Sopenharmony_ci		_rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
17788c2ecf20Sopenharmony_ci			rtlphy->iqk_mac_backup);
17798c2ecf20Sopenharmony_ci		_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
17808c2ecf20Sopenharmony_ci			rtlphy->iqk_bb_backup, IQK_BB_REG_NUM);
17818c2ecf20Sopenharmony_ci	}
17828c2ecf20Sopenharmony_ci	_rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
17838c2ecf20Sopenharmony_ci	if (t == 0)
17848c2ecf20Sopenharmony_ci		rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
17858c2ecf20Sopenharmony_ci				RFPGA0_XA_HSSIPARAMETER1, BIT(8));
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci	/*  Switch BB to PI mode to do IQ Calibration. */
17888c2ecf20Sopenharmony_ci	if (!rtlphy->rfpi_enable)
17898c2ecf20Sopenharmony_ci		_rtl92d_phy_pimode_switch(hw, true);
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
17928c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKDWORD, 0x03a05600);
17938c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, MASKDWORD, 0x000800e4);
17948c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, MASKDWORD, 0x22204000);
17958c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);
17968c2ecf20Sopenharmony_ci	if (is2t) {
17978c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XA_LSSIPARAMETER, MASKDWORD,
17988c2ecf20Sopenharmony_ci			      0x00010000);
17998c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XB_LSSIPARAMETER, MASKDWORD,
18008c2ecf20Sopenharmony_ci			      0x00010000);
18018c2ecf20Sopenharmony_ci	}
18028c2ecf20Sopenharmony_ci	/* MAC settings */
18038c2ecf20Sopenharmony_ci	_rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
18048c2ecf20Sopenharmony_ci					    rtlphy->iqk_mac_backup);
18058c2ecf20Sopenharmony_ci	/* Page B init */
18068c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xb68, MASKDWORD, 0x0f600000);
18078c2ecf20Sopenharmony_ci	if (is2t)
18088c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xb6c, MASKDWORD, 0x0f600000);
18098c2ecf20Sopenharmony_ci	/* IQ calibration setting */
18108c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK setting!\n");
18118c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
18128c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe40, MASKDWORD, 0x01007c00);
18138c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe44, MASKDWORD, 0x01004800);
18148c2ecf20Sopenharmony_ci	for (i = 0; i < retrycount; i++) {
18158c2ecf20Sopenharmony_ci		patha_ok = _rtl92d_phy_patha_iqk(hw, is2t);
18168c2ecf20Sopenharmony_ci		if (patha_ok == 0x03) {
18178c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
18188c2ecf20Sopenharmony_ci				"Path A IQK Success!!\n");
18198c2ecf20Sopenharmony_ci			result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
18208c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18218c2ecf20Sopenharmony_ci			result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
18228c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18238c2ecf20Sopenharmony_ci			result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
18248c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18258c2ecf20Sopenharmony_ci			result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
18268c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18278c2ecf20Sopenharmony_ci			break;
18288c2ecf20Sopenharmony_ci		} else if (i == (retrycount - 1) && patha_ok == 0x01) {
18298c2ecf20Sopenharmony_ci			/* Tx IQK OK */
18308c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
18318c2ecf20Sopenharmony_ci				"Path A IQK Only  Tx Success!!\n");
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci			result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
18348c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18358c2ecf20Sopenharmony_ci			result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
18368c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
18378c2ecf20Sopenharmony_ci		}
18388c2ecf20Sopenharmony_ci	}
18398c2ecf20Sopenharmony_ci	if (0x00 == patha_ok)
18408c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK failed!!\n");
18418c2ecf20Sopenharmony_ci	if (is2t) {
18428c2ecf20Sopenharmony_ci		_rtl92d_phy_patha_standby(hw);
18438c2ecf20Sopenharmony_ci		/* Turn Path B ADDA on */
18448c2ecf20Sopenharmony_ci		_rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
18458c2ecf20Sopenharmony_ci		for (i = 0; i < retrycount; i++) {
18468c2ecf20Sopenharmony_ci			pathb_ok = _rtl92d_phy_pathb_iqk(hw);
18478c2ecf20Sopenharmony_ci			if (pathb_ok == 0x03) {
18488c2ecf20Sopenharmony_ci				RTPRINT(rtlpriv, FINIT, INIT_IQK,
18498c2ecf20Sopenharmony_ci					"Path B IQK Success!!\n");
18508c2ecf20Sopenharmony_ci				result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
18518c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18528c2ecf20Sopenharmony_ci				result[t][5] = (rtl_get_bbreg(hw, 0xebc,
18538c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18548c2ecf20Sopenharmony_ci				result[t][6] = (rtl_get_bbreg(hw, 0xec4,
18558c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18568c2ecf20Sopenharmony_ci				result[t][7] = (rtl_get_bbreg(hw, 0xecc,
18578c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18588c2ecf20Sopenharmony_ci				break;
18598c2ecf20Sopenharmony_ci			} else if (i == (retrycount - 1) && pathb_ok == 0x01) {
18608c2ecf20Sopenharmony_ci				/* Tx IQK OK */
18618c2ecf20Sopenharmony_ci				RTPRINT(rtlpriv, FINIT, INIT_IQK,
18628c2ecf20Sopenharmony_ci					"Path B Only Tx IQK Success!!\n");
18638c2ecf20Sopenharmony_ci				result[t][4] = (rtl_get_bbreg(hw, 0xeb4,
18648c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18658c2ecf20Sopenharmony_ci				result[t][5] = (rtl_get_bbreg(hw, 0xebc,
18668c2ecf20Sopenharmony_ci					       MASKDWORD) & 0x3FF0000) >> 16;
18678c2ecf20Sopenharmony_ci			}
18688c2ecf20Sopenharmony_ci		}
18698c2ecf20Sopenharmony_ci		if (0x00 == pathb_ok)
18708c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
18718c2ecf20Sopenharmony_ci				"Path B IQK failed!!\n");
18728c2ecf20Sopenharmony_ci	}
18738c2ecf20Sopenharmony_ci
18748c2ecf20Sopenharmony_ci	/* Back to BB mode, load original value */
18758c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
18768c2ecf20Sopenharmony_ci		"IQK:Back to BB mode, load original value!\n");
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0);
18798c2ecf20Sopenharmony_ci	if (t != 0) {
18808c2ecf20Sopenharmony_ci		/* Switch back BB to SI mode after finish IQ Calibration. */
18818c2ecf20Sopenharmony_ci		if (!rtlphy->rfpi_enable)
18828c2ecf20Sopenharmony_ci			_rtl92d_phy_pimode_switch(hw, false);
18838c2ecf20Sopenharmony_ci		/* Reload ADDA power saving parameters */
18848c2ecf20Sopenharmony_ci		_rtl92d_phy_reload_adda_registers(hw, adda_reg,
18858c2ecf20Sopenharmony_ci				rtlphy->adda_backup, IQK_ADDA_REG_NUM);
18868c2ecf20Sopenharmony_ci		/* Reload MAC parameters */
18878c2ecf20Sopenharmony_ci		_rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
18888c2ecf20Sopenharmony_ci					rtlphy->iqk_mac_backup);
18898c2ecf20Sopenharmony_ci		if (is2t)
18908c2ecf20Sopenharmony_ci			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
18918c2ecf20Sopenharmony_ci							  rtlphy->iqk_bb_backup,
18928c2ecf20Sopenharmony_ci							  IQK_BB_REG_NUM);
18938c2ecf20Sopenharmony_ci		else
18948c2ecf20Sopenharmony_ci			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
18958c2ecf20Sopenharmony_ci							  rtlphy->iqk_bb_backup,
18968c2ecf20Sopenharmony_ci							  IQK_BB_REG_NUM - 1);
18978c2ecf20Sopenharmony_ci		/* load 0xe30 IQC default value */
18988c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x01008c00);
18998c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x01008c00);
19008c2ecf20Sopenharmony_ci	}
19018c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "<==\n");
19028c2ecf20Sopenharmony_ci}
19038c2ecf20Sopenharmony_ci
19048c2ecf20Sopenharmony_cistatic void _rtl92d_phy_iq_calibrate_5g_normal(struct ieee80211_hw *hw,
19058c2ecf20Sopenharmony_ci					       long result[][8], u8 t)
19068c2ecf20Sopenharmony_ci{
19078c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
19088c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
19098c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
19108c2ecf20Sopenharmony_ci	u8 patha_ok, pathb_ok;
19118c2ecf20Sopenharmony_ci	static u32 adda_reg[IQK_ADDA_REG_NUM] = {
19128c2ecf20Sopenharmony_ci		RFPGA0_XCD_SWITCHCONTROL, 0xe6c, 0xe70, 0xe74,
19138c2ecf20Sopenharmony_ci		0xe78, 0xe7c, 0xe80, 0xe84,
19148c2ecf20Sopenharmony_ci		0xe88, 0xe8c, 0xed0, 0xed4,
19158c2ecf20Sopenharmony_ci		0xed8, 0xedc, 0xee0, 0xeec
19168c2ecf20Sopenharmony_ci	};
19178c2ecf20Sopenharmony_ci	static u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
19188c2ecf20Sopenharmony_ci		0x522, 0x550, 0x551, 0x040
19198c2ecf20Sopenharmony_ci	};
19208c2ecf20Sopenharmony_ci	static u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
19218c2ecf20Sopenharmony_ci		RFPGA0_XAB_RFINTERFACESW, RFPGA0_XA_RFINTERFACEOE,
19228c2ecf20Sopenharmony_ci		RFPGA0_XB_RFINTERFACEOE, ROFDM0_TRMUXPAR,
19238c2ecf20Sopenharmony_ci		RFPGA0_XCD_RFINTERFACESW, ROFDM0_TRXPATHENABLE,
19248c2ecf20Sopenharmony_ci		RFPGA0_RFMOD, RFPGA0_ANALOGPARAMETER4,
19258c2ecf20Sopenharmony_ci		ROFDM0_XAAGCCORE1, ROFDM0_XBAGCCORE1
19268c2ecf20Sopenharmony_ci	};
19278c2ecf20Sopenharmony_ci	u32 bbvalue;
19288c2ecf20Sopenharmony_ci	bool is2t = IS_92D_SINGLEPHY(rtlhal->version);
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci	/* Note: IQ calibration must be performed after loading
19318c2ecf20Sopenharmony_ci	 * PHY_REG.txt , and radio_a, radio_b.txt */
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK for 5G NORMAL:Start!!!\n");
19348c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME * 20);
19358c2ecf20Sopenharmony_ci	if (t == 0) {
19368c2ecf20Sopenharmony_ci		bbvalue = rtl_get_bbreg(hw, RFPGA0_RFMOD, MASKDWORD);
19378c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "==>0x%08x\n", bbvalue);
19388c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "IQ Calibration for %s\n",
19398c2ecf20Sopenharmony_ci			is2t ? "2T2R" : "1T1R");
19408c2ecf20Sopenharmony_ci		/* Save ADDA parameters, turn Path A ADDA on */
19418c2ecf20Sopenharmony_ci		_rtl92d_phy_save_adda_registers(hw, adda_reg,
19428c2ecf20Sopenharmony_ci						rtlphy->adda_backup,
19438c2ecf20Sopenharmony_ci						IQK_ADDA_REG_NUM);
19448c2ecf20Sopenharmony_ci		_rtl92d_phy_save_mac_registers(hw, iqk_mac_reg,
19458c2ecf20Sopenharmony_ci					       rtlphy->iqk_mac_backup);
19468c2ecf20Sopenharmony_ci		if (is2t)
19478c2ecf20Sopenharmony_ci			_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
19488c2ecf20Sopenharmony_ci							rtlphy->iqk_bb_backup,
19498c2ecf20Sopenharmony_ci							IQK_BB_REG_NUM);
19508c2ecf20Sopenharmony_ci		else
19518c2ecf20Sopenharmony_ci			_rtl92d_phy_save_adda_registers(hw, iqk_bb_reg,
19528c2ecf20Sopenharmony_ci							rtlphy->iqk_bb_backup,
19538c2ecf20Sopenharmony_ci							IQK_BB_REG_NUM - 1);
19548c2ecf20Sopenharmony_ci	}
19558c2ecf20Sopenharmony_ci	_rtl92d_phy_path_adda_on(hw, adda_reg, true, is2t);
19568c2ecf20Sopenharmony_ci	/* MAC settings */
19578c2ecf20Sopenharmony_ci	_rtl92d_phy_mac_setting_calibration(hw, iqk_mac_reg,
19588c2ecf20Sopenharmony_ci			rtlphy->iqk_mac_backup);
19598c2ecf20Sopenharmony_ci	if (t == 0)
19608c2ecf20Sopenharmony_ci		rtlphy->rfpi_enable = (u8) rtl_get_bbreg(hw,
19618c2ecf20Sopenharmony_ci			RFPGA0_XA_HSSIPARAMETER1, BIT(8));
19628c2ecf20Sopenharmony_ci	/*  Switch BB to PI mode to do IQ Calibration. */
19638c2ecf20Sopenharmony_ci	if (!rtlphy->rfpi_enable)
19648c2ecf20Sopenharmony_ci		_rtl92d_phy_pimode_switch(hw, true);
19658c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_RFMOD, BIT(24), 0x00);
19668c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKDWORD, 0x03a05600);
19678c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_TRMUXPAR, MASKDWORD, 0x000800e4);
19688c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_XCD_RFINTERFACESW, MASKDWORD, 0x22208000);
19698c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xf00000, 0x0f);
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_ci	/* Page B init */
19728c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xb68, MASKDWORD, 0x0f600000);
19738c2ecf20Sopenharmony_ci	if (is2t)
19748c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xb6c, MASKDWORD, 0x0f600000);
19758c2ecf20Sopenharmony_ci	/* IQ calibration setting  */
19768c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "IQK setting!\n");
19778c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0x80800000);
19788c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe40, MASKDWORD, 0x10007c00);
19798c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe44, MASKDWORD, 0x01004800);
19808c2ecf20Sopenharmony_ci	patha_ok = _rtl92d_phy_patha_iqk_5g_normal(hw, is2t);
19818c2ecf20Sopenharmony_ci	if (patha_ok == 0x03) {
19828c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK Success!!\n");
19838c2ecf20Sopenharmony_ci		result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
19848c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19858c2ecf20Sopenharmony_ci		result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
19868c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19878c2ecf20Sopenharmony_ci		result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
19888c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19898c2ecf20Sopenharmony_ci		result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
19908c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19918c2ecf20Sopenharmony_ci	} else if (patha_ok == 0x01) {	/* Tx IQK OK */
19928c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
19938c2ecf20Sopenharmony_ci			"Path A IQK Only  Tx Success!!\n");
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci		result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
19968c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19978c2ecf20Sopenharmony_ci		result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
19988c2ecf20Sopenharmony_ci				0x3FF0000) >> 16;
19998c2ecf20Sopenharmony_ci	} else {
20008c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "Path A IQK Fail!!\n");
20018c2ecf20Sopenharmony_ci	}
20028c2ecf20Sopenharmony_ci	if (is2t) {
20038c2ecf20Sopenharmony_ci		/* _rtl92d_phy_patha_standby(hw); */
20048c2ecf20Sopenharmony_ci		/* Turn Path B ADDA on  */
20058c2ecf20Sopenharmony_ci		_rtl92d_phy_path_adda_on(hw, adda_reg, false, is2t);
20068c2ecf20Sopenharmony_ci		pathb_ok = _rtl92d_phy_pathb_iqk_5g_normal(hw);
20078c2ecf20Sopenharmony_ci		if (pathb_ok == 0x03) {
20088c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
20098c2ecf20Sopenharmony_ci				"Path B IQK Success!!\n");
20108c2ecf20Sopenharmony_ci			result[t][4] = (rtl_get_bbreg(hw, 0xeb4, MASKDWORD) &
20118c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20128c2ecf20Sopenharmony_ci			result[t][5] = (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
20138c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20148c2ecf20Sopenharmony_ci			result[t][6] = (rtl_get_bbreg(hw, 0xec4, MASKDWORD) &
20158c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20168c2ecf20Sopenharmony_ci			result[t][7] = (rtl_get_bbreg(hw, 0xecc, MASKDWORD) &
20178c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20188c2ecf20Sopenharmony_ci		} else if (pathb_ok == 0x01) { /* Tx IQK OK */
20198c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
20208c2ecf20Sopenharmony_ci				"Path B Only Tx IQK Success!!\n");
20218c2ecf20Sopenharmony_ci			result[t][4] = (rtl_get_bbreg(hw, 0xeb4, MASKDWORD) &
20228c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20238c2ecf20Sopenharmony_ci			result[t][5] = (rtl_get_bbreg(hw, 0xebc, MASKDWORD) &
20248c2ecf20Sopenharmony_ci			     0x3FF0000) >> 16;
20258c2ecf20Sopenharmony_ci		} else {
20268c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
20278c2ecf20Sopenharmony_ci				"Path B IQK failed!!\n");
20288c2ecf20Sopenharmony_ci		}
20298c2ecf20Sopenharmony_ci	}
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	/* Back to BB mode, load original value */
20328c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
20338c2ecf20Sopenharmony_ci		"IQK:Back to BB mode, load original value!\n");
20348c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xe28, MASKDWORD, 0);
20358c2ecf20Sopenharmony_ci	if (t != 0) {
20368c2ecf20Sopenharmony_ci		if (is2t)
20378c2ecf20Sopenharmony_ci			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
20388c2ecf20Sopenharmony_ci							  rtlphy->iqk_bb_backup,
20398c2ecf20Sopenharmony_ci							  IQK_BB_REG_NUM);
20408c2ecf20Sopenharmony_ci		else
20418c2ecf20Sopenharmony_ci			_rtl92d_phy_reload_adda_registers(hw, iqk_bb_reg,
20428c2ecf20Sopenharmony_ci							  rtlphy->iqk_bb_backup,
20438c2ecf20Sopenharmony_ci							  IQK_BB_REG_NUM - 1);
20448c2ecf20Sopenharmony_ci		/* Reload MAC parameters */
20458c2ecf20Sopenharmony_ci		_rtl92d_phy_reload_mac_registers(hw, iqk_mac_reg,
20468c2ecf20Sopenharmony_ci				rtlphy->iqk_mac_backup);
20478c2ecf20Sopenharmony_ci		/*  Switch back BB to SI mode after finish IQ Calibration. */
20488c2ecf20Sopenharmony_ci		if (!rtlphy->rfpi_enable)
20498c2ecf20Sopenharmony_ci			_rtl92d_phy_pimode_switch(hw, false);
20508c2ecf20Sopenharmony_ci		/* Reload ADDA power saving parameters */
20518c2ecf20Sopenharmony_ci		_rtl92d_phy_reload_adda_registers(hw, adda_reg,
20528c2ecf20Sopenharmony_ci						  rtlphy->adda_backup,
20538c2ecf20Sopenharmony_ci						  IQK_ADDA_REG_NUM);
20548c2ecf20Sopenharmony_ci	}
20558c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "<==\n");
20568c2ecf20Sopenharmony_ci}
20578c2ecf20Sopenharmony_ci
20588c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_simularity_compare(struct ieee80211_hw *hw,
20598c2ecf20Sopenharmony_ci	long result[][8], u8 c1, u8 c2)
20608c2ecf20Sopenharmony_ci{
20618c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
20628c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
20638c2ecf20Sopenharmony_ci	u32 i, j, diff, sim_bitmap, bound;
20648c2ecf20Sopenharmony_ci	u8 final_candidate[2] = {0xFF, 0xFF};	/* for path A and path B */
20658c2ecf20Sopenharmony_ci	bool bresult = true;
20668c2ecf20Sopenharmony_ci	bool is2t = IS_92D_SINGLEPHY(rtlhal->version);
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci	if (is2t)
20698c2ecf20Sopenharmony_ci		bound = 8;
20708c2ecf20Sopenharmony_ci	else
20718c2ecf20Sopenharmony_ci		bound = 4;
20728c2ecf20Sopenharmony_ci	sim_bitmap = 0;
20738c2ecf20Sopenharmony_ci	for (i = 0; i < bound; i++) {
20748c2ecf20Sopenharmony_ci		diff = (result[c1][i] > result[c2][i]) ? (result[c1][i] -
20758c2ecf20Sopenharmony_ci		       result[c2][i]) : (result[c2][i] - result[c1][i]);
20768c2ecf20Sopenharmony_ci		if (diff > MAX_TOLERANCE_92D) {
20778c2ecf20Sopenharmony_ci			if ((i == 2 || i == 6) && !sim_bitmap) {
20788c2ecf20Sopenharmony_ci				if (result[c1][i] + result[c1][i + 1] == 0)
20798c2ecf20Sopenharmony_ci					final_candidate[(i / 4)] = c2;
20808c2ecf20Sopenharmony_ci				else if (result[c2][i] + result[c2][i + 1] == 0)
20818c2ecf20Sopenharmony_ci					final_candidate[(i / 4)] = c1;
20828c2ecf20Sopenharmony_ci				else
20838c2ecf20Sopenharmony_ci					sim_bitmap = sim_bitmap | (1 << i);
20848c2ecf20Sopenharmony_ci			} else {
20858c2ecf20Sopenharmony_ci				sim_bitmap = sim_bitmap | (1 << i);
20868c2ecf20Sopenharmony_ci			}
20878c2ecf20Sopenharmony_ci		}
20888c2ecf20Sopenharmony_ci	}
20898c2ecf20Sopenharmony_ci	if (sim_bitmap == 0) {
20908c2ecf20Sopenharmony_ci		for (i = 0; i < (bound / 4); i++) {
20918c2ecf20Sopenharmony_ci			if (final_candidate[i] != 0xFF) {
20928c2ecf20Sopenharmony_ci				for (j = i * 4; j < (i + 1) * 4 - 2; j++)
20938c2ecf20Sopenharmony_ci					result[3][j] =
20948c2ecf20Sopenharmony_ci						 result[final_candidate[i]][j];
20958c2ecf20Sopenharmony_ci				bresult = false;
20968c2ecf20Sopenharmony_ci			}
20978c2ecf20Sopenharmony_ci		}
20988c2ecf20Sopenharmony_ci		return bresult;
20998c2ecf20Sopenharmony_ci	}
21008c2ecf20Sopenharmony_ci	if (!(sim_bitmap & 0x0F)) { /* path A OK */
21018c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++)
21028c2ecf20Sopenharmony_ci			result[3][i] = result[c1][i];
21038c2ecf20Sopenharmony_ci	} else if (!(sim_bitmap & 0x03)) { /* path A, Tx OK */
21048c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++)
21058c2ecf20Sopenharmony_ci			result[3][i] = result[c1][i];
21068c2ecf20Sopenharmony_ci	}
21078c2ecf20Sopenharmony_ci	if (!(sim_bitmap & 0xF0) && is2t) { /* path B OK */
21088c2ecf20Sopenharmony_ci		for (i = 4; i < 8; i++)
21098c2ecf20Sopenharmony_ci			result[3][i] = result[c1][i];
21108c2ecf20Sopenharmony_ci	} else if (!(sim_bitmap & 0x30)) { /* path B, Tx OK */
21118c2ecf20Sopenharmony_ci		for (i = 4; i < 6; i++)
21128c2ecf20Sopenharmony_ci			result[3][i] = result[c1][i];
21138c2ecf20Sopenharmony_ci	}
21148c2ecf20Sopenharmony_ci	return false;
21158c2ecf20Sopenharmony_ci}
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_cistatic void _rtl92d_phy_patha_fill_iqk_matrix(struct ieee80211_hw *hw,
21188c2ecf20Sopenharmony_ci					      bool iqk_ok, long result[][8],
21198c2ecf20Sopenharmony_ci					      u8 final_candidate, bool txonly)
21208c2ecf20Sopenharmony_ci{
21218c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
21228c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
21238c2ecf20Sopenharmony_ci	u32 oldval_0, val_x, tx0_a, reg;
21248c2ecf20Sopenharmony_ci	long val_y, tx0_c;
21258c2ecf20Sopenharmony_ci	bool is2t = IS_92D_SINGLEPHY(rtlhal->version) ||
21268c2ecf20Sopenharmony_ci	    rtlhal->macphymode == DUALMAC_DUALPHY;
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
21298c2ecf20Sopenharmony_ci		"Path A IQ Calibration %s !\n", iqk_ok ? "Success" : "Failed");
21308c2ecf20Sopenharmony_ci	if (final_candidate == 0xFF) {
21318c2ecf20Sopenharmony_ci		return;
21328c2ecf20Sopenharmony_ci	} else if (iqk_ok) {
21338c2ecf20Sopenharmony_ci		oldval_0 = (rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
21348c2ecf20Sopenharmony_ci			MASKDWORD) >> 22) & 0x3FF;	/* OFDM0_D */
21358c2ecf20Sopenharmony_ci		val_x = result[final_candidate][0];
21368c2ecf20Sopenharmony_ci		if ((val_x & 0x00000200) != 0)
21378c2ecf20Sopenharmony_ci			val_x = val_x | 0xFFFFFC00;
21388c2ecf20Sopenharmony_ci		tx0_a = (val_x * oldval_0) >> 8;
21398c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
21408c2ecf20Sopenharmony_ci			"X = 0x%x, tx0_a = 0x%x, oldval_0 0x%x\n",
21418c2ecf20Sopenharmony_ci			val_x, tx0_a, oldval_0);
21428c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE, 0x3FF, tx0_a);
21438c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(24),
21448c2ecf20Sopenharmony_ci			      ((val_x * oldval_0 >> 7) & 0x1));
21458c2ecf20Sopenharmony_ci		val_y = result[final_candidate][1];
21468c2ecf20Sopenharmony_ci		if ((val_y & 0x00000200) != 0)
21478c2ecf20Sopenharmony_ci			val_y = val_y | 0xFFFFFC00;
21488c2ecf20Sopenharmony_ci		/* path B IQK result + 3 */
21498c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 1 &&
21508c2ecf20Sopenharmony_ci			rtlhal->current_bandtype == BAND_ON_5G)
21518c2ecf20Sopenharmony_ci			val_y += 3;
21528c2ecf20Sopenharmony_ci		tx0_c = (val_y * oldval_0) >> 8;
21538c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
21548c2ecf20Sopenharmony_ci			"Y = 0x%lx, tx0_c = 0x%lx\n",
21558c2ecf20Sopenharmony_ci			val_y, tx0_c);
21568c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XCTXAFE, 0xF0000000,
21578c2ecf20Sopenharmony_ci			      ((tx0_c & 0x3C0) >> 6));
21588c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE, 0x003F0000,
21598c2ecf20Sopenharmony_ci			      (tx0_c & 0x3F));
21608c2ecf20Sopenharmony_ci		if (is2t)
21618c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(26),
21628c2ecf20Sopenharmony_ci				      ((val_y * oldval_0 >> 7) & 0x1));
21638c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "0xC80 = 0x%x\n",
21648c2ecf20Sopenharmony_ci			rtl_get_bbreg(hw, ROFDM0_XATXIQIMBALANCE,
21658c2ecf20Sopenharmony_ci				      MASKDWORD));
21668c2ecf20Sopenharmony_ci		if (txonly) {
21678c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,  "only Tx OK\n");
21688c2ecf20Sopenharmony_ci			return;
21698c2ecf20Sopenharmony_ci		}
21708c2ecf20Sopenharmony_ci		reg = result[final_candidate][2];
21718c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0x3FF, reg);
21728c2ecf20Sopenharmony_ci		reg = result[final_candidate][3] & 0x3F;
21738c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, 0xFC00, reg);
21748c2ecf20Sopenharmony_ci		reg = (result[final_candidate][3] >> 6) & 0xF;
21758c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg);
21768c2ecf20Sopenharmony_ci	}
21778c2ecf20Sopenharmony_ci}
21788c2ecf20Sopenharmony_ci
21798c2ecf20Sopenharmony_cistatic void _rtl92d_phy_pathb_fill_iqk_matrix(struct ieee80211_hw *hw,
21808c2ecf20Sopenharmony_ci	bool iqk_ok, long result[][8], u8 final_candidate, bool txonly)
21818c2ecf20Sopenharmony_ci{
21828c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
21838c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
21848c2ecf20Sopenharmony_ci	u32 oldval_1, val_x, tx1_a, reg;
21858c2ecf20Sopenharmony_ci	long val_y, tx1_c;
21868c2ecf20Sopenharmony_ci
21878c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK, "Path B IQ Calibration %s !\n",
21888c2ecf20Sopenharmony_ci		iqk_ok ? "Success" : "Failed");
21898c2ecf20Sopenharmony_ci	if (final_candidate == 0xFF) {
21908c2ecf20Sopenharmony_ci		return;
21918c2ecf20Sopenharmony_ci	} else if (iqk_ok) {
21928c2ecf20Sopenharmony_ci		oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
21938c2ecf20Sopenharmony_ci					  MASKDWORD) >> 22) & 0x3FF;
21948c2ecf20Sopenharmony_ci		val_x = result[final_candidate][4];
21958c2ecf20Sopenharmony_ci		if ((val_x & 0x00000200) != 0)
21968c2ecf20Sopenharmony_ci			val_x = val_x | 0xFFFFFC00;
21978c2ecf20Sopenharmony_ci		tx1_a = (val_x * oldval_1) >> 8;
21988c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "X = 0x%x, tx1_a = 0x%x\n",
21998c2ecf20Sopenharmony_ci			val_x, tx1_a);
22008c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x3FF, tx1_a);
22018c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(28),
22028c2ecf20Sopenharmony_ci			      ((val_x * oldval_1 >> 7) & 0x1));
22038c2ecf20Sopenharmony_ci		val_y = result[final_candidate][5];
22048c2ecf20Sopenharmony_ci		if ((val_y & 0x00000200) != 0)
22058c2ecf20Sopenharmony_ci			val_y = val_y | 0xFFFFFC00;
22068c2ecf20Sopenharmony_ci		if (rtlhal->current_bandtype == BAND_ON_5G)
22078c2ecf20Sopenharmony_ci			val_y += 3;
22088c2ecf20Sopenharmony_ci		tx1_c = (val_y * oldval_1) >> 8;
22098c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "Y = 0x%lx, tx1_c = 0x%lx\n",
22108c2ecf20Sopenharmony_ci			val_y, tx1_c);
22118c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XDTXAFE, 0xF0000000,
22128c2ecf20Sopenharmony_ci			      ((tx1_c & 0x3C0) >> 6));
22138c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x003F0000,
22148c2ecf20Sopenharmony_ci			      (tx1_c & 0x3F));
22158c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30),
22168c2ecf20Sopenharmony_ci			      ((val_y * oldval_1 >> 7) & 0x1));
22178c2ecf20Sopenharmony_ci		if (txonly)
22188c2ecf20Sopenharmony_ci			return;
22198c2ecf20Sopenharmony_ci		reg = result[final_candidate][6];
22208c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
22218c2ecf20Sopenharmony_ci		reg = result[final_candidate][7] & 0x3F;
22228c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
22238c2ecf20Sopenharmony_ci		reg = (result[final_candidate][7] >> 6) & 0xF;
22248c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, reg);
22258c2ecf20Sopenharmony_ci	}
22268c2ecf20Sopenharmony_ci}
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_civoid rtl92d_phy_iq_calibrate(struct ieee80211_hw *hw)
22298c2ecf20Sopenharmony_ci{
22308c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
22318c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
22328c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
22338c2ecf20Sopenharmony_ci	long result[4][8];
22348c2ecf20Sopenharmony_ci	u8 i, final_candidate, indexforchannel;
22358c2ecf20Sopenharmony_ci	bool patha_ok, pathb_ok;
22368c2ecf20Sopenharmony_ci	long rege94, rege9c, regea4, regeac, regeb4;
22378c2ecf20Sopenharmony_ci	long regebc, regec4, regecc, regtmp = 0;
22388c2ecf20Sopenharmony_ci	bool is12simular, is13simular, is23simular;
22398c2ecf20Sopenharmony_ci	unsigned long flag = 0;
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
22428c2ecf20Sopenharmony_ci		"IQK:Start!!!channel %d\n", rtlphy->current_channel);
22438c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
22448c2ecf20Sopenharmony_ci		result[0][i] = 0;
22458c2ecf20Sopenharmony_ci		result[1][i] = 0;
22468c2ecf20Sopenharmony_ci		result[2][i] = 0;
22478c2ecf20Sopenharmony_ci		result[3][i] = 0;
22488c2ecf20Sopenharmony_ci	}
22498c2ecf20Sopenharmony_ci	final_candidate = 0xff;
22508c2ecf20Sopenharmony_ci	patha_ok = false;
22518c2ecf20Sopenharmony_ci	pathb_ok = false;
22528c2ecf20Sopenharmony_ci	is12simular = false;
22538c2ecf20Sopenharmony_ci	is23simular = false;
22548c2ecf20Sopenharmony_ci	is13simular = false;
22558c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
22568c2ecf20Sopenharmony_ci		"IQK !!!currentband %d\n", rtlhal->current_bandtype);
22578c2ecf20Sopenharmony_ci	rtl92d_acquire_cckandrw_pagea_ctl(hw, &flag);
22588c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
22598c2ecf20Sopenharmony_ci		if (rtlhal->current_bandtype == BAND_ON_5G) {
22608c2ecf20Sopenharmony_ci			_rtl92d_phy_iq_calibrate_5g_normal(hw, result, i);
22618c2ecf20Sopenharmony_ci		} else if (rtlhal->current_bandtype == BAND_ON_2_4G) {
22628c2ecf20Sopenharmony_ci			if (IS_92D_SINGLEPHY(rtlhal->version))
22638c2ecf20Sopenharmony_ci				_rtl92d_phy_iq_calibrate(hw, result, i, true);
22648c2ecf20Sopenharmony_ci			else
22658c2ecf20Sopenharmony_ci				_rtl92d_phy_iq_calibrate(hw, result, i, false);
22668c2ecf20Sopenharmony_ci		}
22678c2ecf20Sopenharmony_ci		if (i == 1) {
22688c2ecf20Sopenharmony_ci			is12simular = _rtl92d_phy_simularity_compare(hw, result,
22698c2ecf20Sopenharmony_ci								     0, 1);
22708c2ecf20Sopenharmony_ci			if (is12simular) {
22718c2ecf20Sopenharmony_ci				final_candidate = 0;
22728c2ecf20Sopenharmony_ci				break;
22738c2ecf20Sopenharmony_ci			}
22748c2ecf20Sopenharmony_ci		}
22758c2ecf20Sopenharmony_ci		if (i == 2) {
22768c2ecf20Sopenharmony_ci			is13simular = _rtl92d_phy_simularity_compare(hw, result,
22778c2ecf20Sopenharmony_ci								     0, 2);
22788c2ecf20Sopenharmony_ci			if (is13simular) {
22798c2ecf20Sopenharmony_ci				final_candidate = 0;
22808c2ecf20Sopenharmony_ci				break;
22818c2ecf20Sopenharmony_ci			}
22828c2ecf20Sopenharmony_ci			is23simular = _rtl92d_phy_simularity_compare(hw, result,
22838c2ecf20Sopenharmony_ci								     1, 2);
22848c2ecf20Sopenharmony_ci			if (is23simular) {
22858c2ecf20Sopenharmony_ci				final_candidate = 1;
22868c2ecf20Sopenharmony_ci			} else {
22878c2ecf20Sopenharmony_ci				for (i = 0; i < 8; i++)
22888c2ecf20Sopenharmony_ci					regtmp += result[3][i];
22898c2ecf20Sopenharmony_ci
22908c2ecf20Sopenharmony_ci				if (regtmp != 0)
22918c2ecf20Sopenharmony_ci					final_candidate = 3;
22928c2ecf20Sopenharmony_ci				else
22938c2ecf20Sopenharmony_ci					final_candidate = 0xFF;
22948c2ecf20Sopenharmony_ci			}
22958c2ecf20Sopenharmony_ci		}
22968c2ecf20Sopenharmony_ci	}
22978c2ecf20Sopenharmony_ci	rtl92d_release_cckandrw_pagea_ctl(hw, &flag);
22988c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
22998c2ecf20Sopenharmony_ci		rege94 = result[i][0];
23008c2ecf20Sopenharmony_ci		rege9c = result[i][1];
23018c2ecf20Sopenharmony_ci		regea4 = result[i][2];
23028c2ecf20Sopenharmony_ci		regeac = result[i][3];
23038c2ecf20Sopenharmony_ci		regeb4 = result[i][4];
23048c2ecf20Sopenharmony_ci		regebc = result[i][5];
23058c2ecf20Sopenharmony_ci		regec4 = result[i][6];
23068c2ecf20Sopenharmony_ci		regecc = result[i][7];
23078c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
23088c2ecf20Sopenharmony_ci			"IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
23098c2ecf20Sopenharmony_ci			rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
23108c2ecf20Sopenharmony_ci			regecc);
23118c2ecf20Sopenharmony_ci	}
23128c2ecf20Sopenharmony_ci	if (final_candidate != 0xff) {
23138c2ecf20Sopenharmony_ci		rtlphy->reg_e94 = rege94 = result[final_candidate][0];
23148c2ecf20Sopenharmony_ci		rtlphy->reg_e9c = rege9c = result[final_candidate][1];
23158c2ecf20Sopenharmony_ci		regea4 = result[final_candidate][2];
23168c2ecf20Sopenharmony_ci		regeac = result[final_candidate][3];
23178c2ecf20Sopenharmony_ci		rtlphy->reg_eb4 = regeb4 = result[final_candidate][4];
23188c2ecf20Sopenharmony_ci		rtlphy->reg_ebc = regebc = result[final_candidate][5];
23198c2ecf20Sopenharmony_ci		regec4 = result[final_candidate][6];
23208c2ecf20Sopenharmony_ci		regecc = result[final_candidate][7];
23218c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
23228c2ecf20Sopenharmony_ci			"IQK: final_candidate is %x\n", final_candidate);
23238c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
23248c2ecf20Sopenharmony_ci			"IQK: rege94=%lx rege9c=%lx regea4=%lx regeac=%lx regeb4=%lx regebc=%lx regec4=%lx regecc=%lx\n",
23258c2ecf20Sopenharmony_ci			rege94, rege9c, regea4, regeac, regeb4, regebc, regec4,
23268c2ecf20Sopenharmony_ci			regecc);
23278c2ecf20Sopenharmony_ci		patha_ok = pathb_ok = true;
23288c2ecf20Sopenharmony_ci	} else {
23298c2ecf20Sopenharmony_ci		rtlphy->reg_e94 = rtlphy->reg_eb4 = 0x100; /* X default value */
23308c2ecf20Sopenharmony_ci		rtlphy->reg_e9c = rtlphy->reg_ebc = 0x0;   /* Y default value */
23318c2ecf20Sopenharmony_ci	}
23328c2ecf20Sopenharmony_ci	if ((rege94 != 0) /*&&(regea4 != 0) */)
23338c2ecf20Sopenharmony_ci		_rtl92d_phy_patha_fill_iqk_matrix(hw, patha_ok, result,
23348c2ecf20Sopenharmony_ci				final_candidate, (regea4 == 0));
23358c2ecf20Sopenharmony_ci	if (IS_92D_SINGLEPHY(rtlhal->version)) {
23368c2ecf20Sopenharmony_ci		if ((regeb4 != 0) /*&&(regec4 != 0) */)
23378c2ecf20Sopenharmony_ci			_rtl92d_phy_pathb_fill_iqk_matrix(hw, pathb_ok, result,
23388c2ecf20Sopenharmony_ci						final_candidate, (regec4 == 0));
23398c2ecf20Sopenharmony_ci	}
23408c2ecf20Sopenharmony_ci	if (final_candidate != 0xFF) {
23418c2ecf20Sopenharmony_ci		indexforchannel = rtl92d_get_rightchnlplace_for_iqk(
23428c2ecf20Sopenharmony_ci				  rtlphy->current_channel);
23438c2ecf20Sopenharmony_ci
23448c2ecf20Sopenharmony_ci		for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
23458c2ecf20Sopenharmony_ci			rtlphy->iqk_matrix[indexforchannel].
23468c2ecf20Sopenharmony_ci				value[0][i] = result[final_candidate][i];
23478c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[indexforchannel].iqk_done =
23488c2ecf20Sopenharmony_ci			true;
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_SCAN | COMP_MLME, DBG_LOUD,
23518c2ecf20Sopenharmony_ci			"IQK OK indexforchannel %d\n", indexforchannel);
23528c2ecf20Sopenharmony_ci	}
23538c2ecf20Sopenharmony_ci}
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_civoid rtl92d_phy_reload_iqk_setting(struct ieee80211_hw *hw, u8 channel)
23568c2ecf20Sopenharmony_ci{
23578c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
23588c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
23598c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
23608c2ecf20Sopenharmony_ci	u8 indexforchannel;
23618c2ecf20Sopenharmony_ci
23628c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "channel %d\n", channel);
23638c2ecf20Sopenharmony_ci	/*------Do IQK for normal chip and test chip 5G band------- */
23648c2ecf20Sopenharmony_ci	indexforchannel = rtl92d_get_rightchnlplace_for_iqk(channel);
23658c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "indexforchannel %d done %d\n",
23668c2ecf20Sopenharmony_ci		indexforchannel,
23678c2ecf20Sopenharmony_ci		 rtlphy->iqk_matrix[indexforchannel].iqk_done);
23688c2ecf20Sopenharmony_ci	if (0 && !rtlphy->iqk_matrix[indexforchannel].iqk_done &&
23698c2ecf20Sopenharmony_ci		rtlphy->need_iqk) {
23708c2ecf20Sopenharmony_ci		/* Re Do IQK. */
23718c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_SCAN | COMP_INIT, DBG_LOUD,
23728c2ecf20Sopenharmony_ci			"Do IQK Matrix reg for channel:%d....\n", channel);
23738c2ecf20Sopenharmony_ci		rtl92d_phy_iq_calibrate(hw);
23748c2ecf20Sopenharmony_ci	} else {
23758c2ecf20Sopenharmony_ci		/* Just load the value. */
23768c2ecf20Sopenharmony_ci		/* 2G band just load once. */
23778c2ecf20Sopenharmony_ci		if (((!rtlhal->load_imrandiqk_setting_for2g) &&
23788c2ecf20Sopenharmony_ci		    indexforchannel == 0) || indexforchannel > 0) {
23798c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
23808c2ecf20Sopenharmony_ci				"Just Read IQK Matrix reg for channel:%d....\n",
23818c2ecf20Sopenharmony_ci				channel);
23828c2ecf20Sopenharmony_ci			if (rtlphy->iqk_matrix[indexforchannel].value[0][0] != 0)
23838c2ecf20Sopenharmony_ci				_rtl92d_phy_patha_fill_iqk_matrix(hw, true,
23848c2ecf20Sopenharmony_ci					rtlphy->iqk_matrix[indexforchannel].value, 0,
23858c2ecf20Sopenharmony_ci					rtlphy->iqk_matrix[indexforchannel].value[0][2] == 0);
23868c2ecf20Sopenharmony_ci			if (IS_92D_SINGLEPHY(rtlhal->version)) {
23878c2ecf20Sopenharmony_ci				if ((rtlphy->iqk_matrix[
23888c2ecf20Sopenharmony_ci					indexforchannel].value[0][4] != 0)
23898c2ecf20Sopenharmony_ci					/*&&(regec4 != 0) */)
23908c2ecf20Sopenharmony_ci					_rtl92d_phy_pathb_fill_iqk_matrix(hw,
23918c2ecf20Sopenharmony_ci						true,
23928c2ecf20Sopenharmony_ci						rtlphy->iqk_matrix[
23938c2ecf20Sopenharmony_ci						indexforchannel].value, 0,
23948c2ecf20Sopenharmony_ci						(rtlphy->iqk_matrix[
23958c2ecf20Sopenharmony_ci						indexforchannel].value[0][6]
23968c2ecf20Sopenharmony_ci						== 0));
23978c2ecf20Sopenharmony_ci			}
23988c2ecf20Sopenharmony_ci		}
23998c2ecf20Sopenharmony_ci	}
24008c2ecf20Sopenharmony_ci	rtlphy->need_iqk = false;
24018c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
24028c2ecf20Sopenharmony_ci}
24038c2ecf20Sopenharmony_ci
24048c2ecf20Sopenharmony_cistatic u32 _rtl92d_phy_get_abs(u32 val1, u32 val2)
24058c2ecf20Sopenharmony_ci{
24068c2ecf20Sopenharmony_ci	u32 ret;
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci	if (val1 >= val2)
24098c2ecf20Sopenharmony_ci		ret = val1 - val2;
24108c2ecf20Sopenharmony_ci	else
24118c2ecf20Sopenharmony_ci		ret = val2 - val1;
24128c2ecf20Sopenharmony_ci	return ret;
24138c2ecf20Sopenharmony_ci}
24148c2ecf20Sopenharmony_ci
24158c2ecf20Sopenharmony_cistatic bool _rtl92d_is_legal_5g_channel(struct ieee80211_hw *hw, u8 channel)
24168c2ecf20Sopenharmony_ci{
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci	int i;
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(channel5g); i++)
24218c2ecf20Sopenharmony_ci		if (channel == channel5g[i])
24228c2ecf20Sopenharmony_ci			return true;
24238c2ecf20Sopenharmony_ci	return false;
24248c2ecf20Sopenharmony_ci}
24258c2ecf20Sopenharmony_ci
24268c2ecf20Sopenharmony_cistatic void _rtl92d_phy_calc_curvindex(struct ieee80211_hw *hw,
24278c2ecf20Sopenharmony_ci				       u32 *targetchnl, u32 * curvecount_val,
24288c2ecf20Sopenharmony_ci				       bool is5g, u32 *curveindex)
24298c2ecf20Sopenharmony_ci{
24308c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24318c2ecf20Sopenharmony_ci	u32 smallest_abs_val = 0xffffffff, u4tmp;
24328c2ecf20Sopenharmony_ci	u8 i, j;
24338c2ecf20Sopenharmony_ci	u8 chnl_num = is5g ? TARGET_CHNL_NUM_5G : TARGET_CHNL_NUM_2G;
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_ci	for (i = 0; i < chnl_num; i++) {
24368c2ecf20Sopenharmony_ci		if (is5g && !_rtl92d_is_legal_5g_channel(hw, i + 1))
24378c2ecf20Sopenharmony_ci			continue;
24388c2ecf20Sopenharmony_ci		curveindex[i] = 0;
24398c2ecf20Sopenharmony_ci		for (j = 0; j < (CV_CURVE_CNT * 2); j++) {
24408c2ecf20Sopenharmony_ci			u4tmp = _rtl92d_phy_get_abs(targetchnl[i],
24418c2ecf20Sopenharmony_ci				curvecount_val[j]);
24428c2ecf20Sopenharmony_ci
24438c2ecf20Sopenharmony_ci			if (u4tmp < smallest_abs_val) {
24448c2ecf20Sopenharmony_ci				curveindex[i] = j;
24458c2ecf20Sopenharmony_ci				smallest_abs_val = u4tmp;
24468c2ecf20Sopenharmony_ci			}
24478c2ecf20Sopenharmony_ci		}
24488c2ecf20Sopenharmony_ci		smallest_abs_val = 0xffffffff;
24498c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK, "curveindex[%d] = %x\n",
24508c2ecf20Sopenharmony_ci			i, curveindex[i]);
24518c2ecf20Sopenharmony_ci	}
24528c2ecf20Sopenharmony_ci}
24538c2ecf20Sopenharmony_ci
24548c2ecf20Sopenharmony_cistatic void _rtl92d_phy_reload_lck_setting(struct ieee80211_hw *hw,
24558c2ecf20Sopenharmony_ci		u8 channel)
24568c2ecf20Sopenharmony_ci{
24578c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24588c2ecf20Sopenharmony_ci	u8 erfpath = rtlpriv->rtlhal.current_bandtype ==
24598c2ecf20Sopenharmony_ci		BAND_ON_5G ? RF90_PATH_A :
24608c2ecf20Sopenharmony_ci		IS_92D_SINGLEPHY(rtlpriv->rtlhal.version) ?
24618c2ecf20Sopenharmony_ci		RF90_PATH_B : RF90_PATH_A;
24628c2ecf20Sopenharmony_ci	u32 u4tmp = 0, u4regvalue = 0;
24638c2ecf20Sopenharmony_ci	bool bneed_powerdown_radio = false;
24648c2ecf20Sopenharmony_ci
24658c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "path %d\n", erfpath);
24668c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK, "band type = %d\n",
24678c2ecf20Sopenharmony_ci		rtlpriv->rtlhal.current_bandtype);
24688c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "channel = %d\n", channel);
24698c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G) {/* Path-A for 5G */
24708c2ecf20Sopenharmony_ci		u4tmp = curveindex_5g[channel-1];
24718c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
24728c2ecf20Sopenharmony_ci			"ver 1 set RF-A, 5G,	0x28 = 0x%x !!\n", u4tmp);
24738c2ecf20Sopenharmony_ci		if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
24748c2ecf20Sopenharmony_ci			rtlpriv->rtlhal.interfaceindex == 1) {
24758c2ecf20Sopenharmony_ci			bneed_powerdown_radio =
24768c2ecf20Sopenharmony_ci				rtl92d_phy_enable_anotherphy(hw, false);
24778c2ecf20Sopenharmony_ci			rtlpriv->rtlhal.during_mac1init_radioa = true;
24788c2ecf20Sopenharmony_ci			/* asume no this case */
24798c2ecf20Sopenharmony_ci			if (bneed_powerdown_radio)
24808c2ecf20Sopenharmony_ci				_rtl92d_phy_enable_rf_env(hw, erfpath,
24818c2ecf20Sopenharmony_ci							  &u4regvalue);
24828c2ecf20Sopenharmony_ci		}
24838c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
24848c2ecf20Sopenharmony_ci		if (bneed_powerdown_radio)
24858c2ecf20Sopenharmony_ci			_rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
24868c2ecf20Sopenharmony_ci		if (rtlpriv->rtlhal.during_mac1init_radioa)
24878c2ecf20Sopenharmony_ci			rtl92d_phy_powerdown_anotherphy(hw, false);
24888c2ecf20Sopenharmony_ci	} else if (rtlpriv->rtlhal.current_bandtype == BAND_ON_2_4G) {
24898c2ecf20Sopenharmony_ci		u4tmp = curveindex_2g[channel-1];
24908c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
24918c2ecf20Sopenharmony_ci			"ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", u4tmp);
24928c2ecf20Sopenharmony_ci		if (rtlpriv->rtlhal.macphymode == DUALMAC_DUALPHY &&
24938c2ecf20Sopenharmony_ci			rtlpriv->rtlhal.interfaceindex == 0) {
24948c2ecf20Sopenharmony_ci			bneed_powerdown_radio =
24958c2ecf20Sopenharmony_ci				rtl92d_phy_enable_anotherphy(hw, true);
24968c2ecf20Sopenharmony_ci			rtlpriv->rtlhal.during_mac0init_radiob = true;
24978c2ecf20Sopenharmony_ci			if (bneed_powerdown_radio)
24988c2ecf20Sopenharmony_ci				_rtl92d_phy_enable_rf_env(hw, erfpath,
24998c2ecf20Sopenharmony_ci							  &u4regvalue);
25008c2ecf20Sopenharmony_ci		}
25018c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, erfpath, RF_SYN_G4, 0x3f800, u4tmp);
25028c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
25038c2ecf20Sopenharmony_ci			"ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n",
25048c2ecf20Sopenharmony_ci			rtl_get_rfreg(hw,  erfpath, RF_SYN_G4, 0x3f800));
25058c2ecf20Sopenharmony_ci		if (bneed_powerdown_radio)
25068c2ecf20Sopenharmony_ci			_rtl92d_phy_restore_rf_env(hw, erfpath, &u4regvalue);
25078c2ecf20Sopenharmony_ci		if (rtlpriv->rtlhal.during_mac0init_radiob)
25088c2ecf20Sopenharmony_ci			rtl92d_phy_powerdown_anotherphy(hw, true);
25098c2ecf20Sopenharmony_ci	}
25108c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "<====\n");
25118c2ecf20Sopenharmony_ci}
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_cistatic void _rtl92d_phy_lc_calibrate_sw(struct ieee80211_hw *hw, bool is2t)
25148c2ecf20Sopenharmony_ci{
25158c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
25168c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
25178c2ecf20Sopenharmony_ci	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
25188c2ecf20Sopenharmony_ci	u8 tmpreg, index, rf_mode[2];
25198c2ecf20Sopenharmony_ci	u8 path = is2t ? 2 : 1;
25208c2ecf20Sopenharmony_ci	u8 i;
25218c2ecf20Sopenharmony_ci	u32 u4tmp, offset;
25228c2ecf20Sopenharmony_ci	u32 curvecount_val[CV_CURVE_CNT * 2] = {0};
25238c2ecf20Sopenharmony_ci	u16 timeout = 800, timecount = 0;
25248c2ecf20Sopenharmony_ci
25258c2ecf20Sopenharmony_ci	/* Check continuous TX and Packet TX */
25268c2ecf20Sopenharmony_ci	tmpreg = rtl_read_byte(rtlpriv, 0xd03);
25278c2ecf20Sopenharmony_ci	/* if Deal with contisuous TX case, disable all continuous TX */
25288c2ecf20Sopenharmony_ci	/* if Deal with Packet TX case, block all queues */
25298c2ecf20Sopenharmony_ci	if ((tmpreg & 0x70) != 0)
25308c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
25318c2ecf20Sopenharmony_ci	else
25328c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
25338c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x0F);
25348c2ecf20Sopenharmony_ci	for (index = 0; index < path; index++) {
25358c2ecf20Sopenharmony_ci		/* 1. Read original RF mode */
25368c2ecf20Sopenharmony_ci		offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
25378c2ecf20Sopenharmony_ci		rf_mode[index] = rtl_read_byte(rtlpriv, offset);
25388c2ecf20Sopenharmony_ci		/* 2. Set RF mode = standby mode */
25398c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, (enum radio_path)index, RF_AC,
25408c2ecf20Sopenharmony_ci			      RFREG_OFFSET_MASK, 0x010000);
25418c2ecf20Sopenharmony_ci		if (rtlpci->init_ready) {
25428c2ecf20Sopenharmony_ci			/* switch CV-curve control by LC-calibration */
25438c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
25448c2ecf20Sopenharmony_ci				      BIT(17), 0x0);
25458c2ecf20Sopenharmony_ci			/* 4. Set LC calibration begin */
25468c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
25478c2ecf20Sopenharmony_ci				      0x08000, 0x01);
25488c2ecf20Sopenharmony_ci		}
25498c2ecf20Sopenharmony_ci		u4tmp = rtl_get_rfreg(hw, (enum radio_path)index, RF_SYN_G6,
25508c2ecf20Sopenharmony_ci				  RFREG_OFFSET_MASK);
25518c2ecf20Sopenharmony_ci		while ((!(u4tmp & BIT(11))) && timecount <= timeout) {
25528c2ecf20Sopenharmony_ci			mdelay(50);
25538c2ecf20Sopenharmony_ci			timecount += 50;
25548c2ecf20Sopenharmony_ci			u4tmp = rtl_get_rfreg(hw, (enum radio_path)index,
25558c2ecf20Sopenharmony_ci					      RF_SYN_G6, RFREG_OFFSET_MASK);
25568c2ecf20Sopenharmony_ci		}
25578c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,
25588c2ecf20Sopenharmony_ci			"PHY_LCK finish delay for %d ms=2\n", timecount);
25598c2ecf20Sopenharmony_ci		u4tmp = rtl_get_rfreg(hw, index, RF_SYN_G4, RFREG_OFFSET_MASK);
25608c2ecf20Sopenharmony_ci		if (index == 0 && rtlhal->interfaceindex == 0) {
25618c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
25628c2ecf20Sopenharmony_ci				"path-A / 5G LCK\n");
25638c2ecf20Sopenharmony_ci		} else {
25648c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FINIT, INIT_IQK,
25658c2ecf20Sopenharmony_ci				"path-B / 2.4G LCK\n");
25668c2ecf20Sopenharmony_ci		}
25678c2ecf20Sopenharmony_ci		memset(&curvecount_val[0], 0, CV_CURVE_CNT * 2);
25688c2ecf20Sopenharmony_ci		/* Set LC calibration off */
25698c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, (enum radio_path)index, RF_CHNLBW,
25708c2ecf20Sopenharmony_ci			      0x08000, 0x0);
25718c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FINIT, INIT_IQK,  "set RF 0x18[15] = 0\n");
25728c2ecf20Sopenharmony_ci		/* save Curve-counting number */
25738c2ecf20Sopenharmony_ci		for (i = 0; i < CV_CURVE_CNT; i++) {
25748c2ecf20Sopenharmony_ci			u32 readval = 0, readval2 = 0;
25758c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)index, 0x3F,
25768c2ecf20Sopenharmony_ci				      0x7f, i);
25778c2ecf20Sopenharmony_ci
25788c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)index, 0x4D,
25798c2ecf20Sopenharmony_ci				RFREG_OFFSET_MASK, 0x0);
25808c2ecf20Sopenharmony_ci			readval = rtl_get_rfreg(hw, (enum radio_path)index,
25818c2ecf20Sopenharmony_ci					  0x4F, RFREG_OFFSET_MASK);
25828c2ecf20Sopenharmony_ci			curvecount_val[2 * i + 1] = (readval & 0xfffe0) >> 5;
25838c2ecf20Sopenharmony_ci			/* reg 0x4f [4:0] */
25848c2ecf20Sopenharmony_ci			/* reg 0x50 [19:10] */
25858c2ecf20Sopenharmony_ci			readval2 = rtl_get_rfreg(hw, (enum radio_path)index,
25868c2ecf20Sopenharmony_ci						 0x50, 0xffc00);
25878c2ecf20Sopenharmony_ci			curvecount_val[2 * i] = (((readval & 0x1F) << 10) |
25888c2ecf20Sopenharmony_ci						 readval2);
25898c2ecf20Sopenharmony_ci		}
25908c2ecf20Sopenharmony_ci		if (index == 0 && rtlhal->interfaceindex == 0)
25918c2ecf20Sopenharmony_ci			_rtl92d_phy_calc_curvindex(hw, targetchnl_5g,
25928c2ecf20Sopenharmony_ci						   curvecount_val,
25938c2ecf20Sopenharmony_ci						   true, curveindex_5g);
25948c2ecf20Sopenharmony_ci		else
25958c2ecf20Sopenharmony_ci			_rtl92d_phy_calc_curvindex(hw, targetchnl_2g,
25968c2ecf20Sopenharmony_ci						   curvecount_val,
25978c2ecf20Sopenharmony_ci						   false, curveindex_2g);
25988c2ecf20Sopenharmony_ci		/* switch CV-curve control mode */
25998c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, (enum radio_path)index, RF_SYN_G7,
26008c2ecf20Sopenharmony_ci			      BIT(17), 0x1);
26018c2ecf20Sopenharmony_ci	}
26028c2ecf20Sopenharmony_ci
26038c2ecf20Sopenharmony_ci	/* Restore original situation  */
26048c2ecf20Sopenharmony_ci	for (index = 0; index < path; index++) {
26058c2ecf20Sopenharmony_ci		offset = index == 0 ? ROFDM0_XAAGCCORE1 : ROFDM0_XBAGCCORE1;
26068c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, offset, 0x50);
26078c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, offset, rf_mode[index]);
26088c2ecf20Sopenharmony_ci	}
26098c2ecf20Sopenharmony_ci	if ((tmpreg & 0x70) != 0)
26108c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, 0xd03, tmpreg);
26118c2ecf20Sopenharmony_ci	else /*Deal with Packet TX case */
26128c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
26138c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER4, 0xF00000, 0x00);
26148c2ecf20Sopenharmony_ci	_rtl92d_phy_reload_lck_setting(hw, rtlpriv->phy.current_channel);
26158c2ecf20Sopenharmony_ci}
26168c2ecf20Sopenharmony_ci
26178c2ecf20Sopenharmony_cistatic void _rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
26188c2ecf20Sopenharmony_ci{
26198c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
26208c2ecf20Sopenharmony_ci
26218c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "cosa PHY_LCK ver=2\n");
26228c2ecf20Sopenharmony_ci	_rtl92d_phy_lc_calibrate_sw(hw, is2t);
26238c2ecf20Sopenharmony_ci}
26248c2ecf20Sopenharmony_ci
26258c2ecf20Sopenharmony_civoid rtl92d_phy_lc_calibrate(struct ieee80211_hw *hw)
26268c2ecf20Sopenharmony_ci{
26278c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
26288c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
26298c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &(rtlpriv->rtlhal);
26308c2ecf20Sopenharmony_ci	u32 timeout = 2000, timecount = 0;
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci	while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
26338c2ecf20Sopenharmony_ci		udelay(50);
26348c2ecf20Sopenharmony_ci		timecount += 50;
26358c2ecf20Sopenharmony_ci	}
26368c2ecf20Sopenharmony_ci
26378c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = true;
26388c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
26398c2ecf20Sopenharmony_ci		"LCK:Start!!! currentband %x delay %d ms\n",
26408c2ecf20Sopenharmony_ci		rtlhal->current_bandtype, timecount);
26418c2ecf20Sopenharmony_ci	if (IS_92D_SINGLEPHY(rtlhal->version)) {
26428c2ecf20Sopenharmony_ci		_rtl92d_phy_lc_calibrate(hw, true);
26438c2ecf20Sopenharmony_ci	} else {
26448c2ecf20Sopenharmony_ci		/* For 1T1R */
26458c2ecf20Sopenharmony_ci		_rtl92d_phy_lc_calibrate(hw, false);
26468c2ecf20Sopenharmony_ci	}
26478c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = false;
26488c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,  "LCK:Finish!!!\n");
26498c2ecf20Sopenharmony_ci}
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_civoid rtl92d_phy_ap_calibrate(struct ieee80211_hw *hw, s8 delta)
26528c2ecf20Sopenharmony_ci{
26538c2ecf20Sopenharmony_ci	return;
26548c2ecf20Sopenharmony_ci}
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_set_sw_chnl_cmdarray(struct swchnlcmd *cmdtable,
26578c2ecf20Sopenharmony_ci		u32 cmdtableidx, u32 cmdtablesz, enum swchnlcmd_id cmdid,
26588c2ecf20Sopenharmony_ci		u32 para1, u32 para2, u32 msdelay)
26598c2ecf20Sopenharmony_ci{
26608c2ecf20Sopenharmony_ci	struct swchnlcmd *pcmd;
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci	if (cmdtable == NULL) {
26638c2ecf20Sopenharmony_ci		WARN_ONCE(true, "rtl8192de: cmdtable cannot be NULL\n");
26648c2ecf20Sopenharmony_ci		return false;
26658c2ecf20Sopenharmony_ci	}
26668c2ecf20Sopenharmony_ci	if (cmdtableidx >= cmdtablesz)
26678c2ecf20Sopenharmony_ci		return false;
26688c2ecf20Sopenharmony_ci
26698c2ecf20Sopenharmony_ci	pcmd = cmdtable + cmdtableidx;
26708c2ecf20Sopenharmony_ci	pcmd->cmdid = cmdid;
26718c2ecf20Sopenharmony_ci	pcmd->para1 = para1;
26728c2ecf20Sopenharmony_ci	pcmd->para2 = para2;
26738c2ecf20Sopenharmony_ci	pcmd->msdelay = msdelay;
26748c2ecf20Sopenharmony_ci	return true;
26758c2ecf20Sopenharmony_ci}
26768c2ecf20Sopenharmony_ci
26778c2ecf20Sopenharmony_civoid rtl92d_phy_reset_iqk_result(struct ieee80211_hw *hw)
26788c2ecf20Sopenharmony_ci{
26798c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
26808c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
26818c2ecf20Sopenharmony_ci	u8 i;
26828c2ecf20Sopenharmony_ci
26838c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
26848c2ecf20Sopenharmony_ci		"settings regs %d default regs %d\n",
26858c2ecf20Sopenharmony_ci		(int)(sizeof(rtlphy->iqk_matrix) /
26868c2ecf20Sopenharmony_ci		      sizeof(struct iqk_matrix_regs)),
26878c2ecf20Sopenharmony_ci		IQK_MATRIX_REG_NUM);
26888c2ecf20Sopenharmony_ci	/* 0xe94, 0xe9c, 0xea4, 0xeac, 0xeb4, 0xebc, 0xec4, 0xecc */
26898c2ecf20Sopenharmony_ci	for (i = 0; i < IQK_MATRIX_SETTINGS_NUM; i++) {
26908c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][0] = 0x100;
26918c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][2] = 0x100;
26928c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][4] = 0x100;
26938c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][6] = 0x100;
26948c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][1] = 0x0;
26958c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][3] = 0x0;
26968c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][5] = 0x0;
26978c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].value[0][7] = 0x0;
26988c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[i].iqk_done = false;
26998c2ecf20Sopenharmony_ci	}
27008c2ecf20Sopenharmony_ci}
27018c2ecf20Sopenharmony_ci
27028c2ecf20Sopenharmony_cistatic bool _rtl92d_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
27038c2ecf20Sopenharmony_ci					     u8 channel, u8 *stage, u8 *step,
27048c2ecf20Sopenharmony_ci					     u32 *delay)
27058c2ecf20Sopenharmony_ci{
27068c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
27078c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
27088c2ecf20Sopenharmony_ci	struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
27098c2ecf20Sopenharmony_ci	u32 precommoncmdcnt;
27108c2ecf20Sopenharmony_ci	struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
27118c2ecf20Sopenharmony_ci	u32 postcommoncmdcnt;
27128c2ecf20Sopenharmony_ci	struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
27138c2ecf20Sopenharmony_ci	u32 rfdependcmdcnt;
27148c2ecf20Sopenharmony_ci	struct swchnlcmd *currentcmd = NULL;
27158c2ecf20Sopenharmony_ci	u8 rfpath;
27168c2ecf20Sopenharmony_ci	u8 num_total_rfpath = rtlphy->num_total_rfpath;
27178c2ecf20Sopenharmony_ci
27188c2ecf20Sopenharmony_ci	precommoncmdcnt = 0;
27198c2ecf20Sopenharmony_ci	_rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
27208c2ecf20Sopenharmony_ci					 MAX_PRECMD_CNT,
27218c2ecf20Sopenharmony_ci					 CMDID_SET_TXPOWEROWER_LEVEL, 0, 0, 0);
27228c2ecf20Sopenharmony_ci	_rtl92d_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
27238c2ecf20Sopenharmony_ci					 MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
27248c2ecf20Sopenharmony_ci	postcommoncmdcnt = 0;
27258c2ecf20Sopenharmony_ci	_rtl92d_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
27268c2ecf20Sopenharmony_ci					 MAX_POSTCMD_CNT, CMDID_END, 0, 0, 0);
27278c2ecf20Sopenharmony_ci	rfdependcmdcnt = 0;
27288c2ecf20Sopenharmony_ci	_rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
27298c2ecf20Sopenharmony_ci					 MAX_RFDEPENDCMD_CNT, CMDID_RF_WRITEREG,
27308c2ecf20Sopenharmony_ci					 RF_CHNLBW, channel, 0);
27318c2ecf20Sopenharmony_ci	_rtl92d_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
27328c2ecf20Sopenharmony_ci					 MAX_RFDEPENDCMD_CNT, CMDID_END,
27338c2ecf20Sopenharmony_ci					 0, 0, 0);
27348c2ecf20Sopenharmony_ci
27358c2ecf20Sopenharmony_ci	do {
27368c2ecf20Sopenharmony_ci		switch (*stage) {
27378c2ecf20Sopenharmony_ci		case 0:
27388c2ecf20Sopenharmony_ci			currentcmd = &precommoncmd[*step];
27398c2ecf20Sopenharmony_ci			break;
27408c2ecf20Sopenharmony_ci		case 1:
27418c2ecf20Sopenharmony_ci			currentcmd = &rfdependcmd[*step];
27428c2ecf20Sopenharmony_ci			break;
27438c2ecf20Sopenharmony_ci		case 2:
27448c2ecf20Sopenharmony_ci			currentcmd = &postcommoncmd[*step];
27458c2ecf20Sopenharmony_ci			break;
27468c2ecf20Sopenharmony_ci		}
27478c2ecf20Sopenharmony_ci		if (currentcmd->cmdid == CMDID_END) {
27488c2ecf20Sopenharmony_ci			if ((*stage) == 2) {
27498c2ecf20Sopenharmony_ci				return true;
27508c2ecf20Sopenharmony_ci			} else {
27518c2ecf20Sopenharmony_ci				(*stage)++;
27528c2ecf20Sopenharmony_ci				(*step) = 0;
27538c2ecf20Sopenharmony_ci				continue;
27548c2ecf20Sopenharmony_ci			}
27558c2ecf20Sopenharmony_ci		}
27568c2ecf20Sopenharmony_ci		switch (currentcmd->cmdid) {
27578c2ecf20Sopenharmony_ci		case CMDID_SET_TXPOWEROWER_LEVEL:
27588c2ecf20Sopenharmony_ci			rtl92d_phy_set_txpower_level(hw, channel);
27598c2ecf20Sopenharmony_ci			break;
27608c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_ULONG:
27618c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, currentcmd->para1,
27628c2ecf20Sopenharmony_ci					currentcmd->para2);
27638c2ecf20Sopenharmony_ci			break;
27648c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_USHORT:
27658c2ecf20Sopenharmony_ci			rtl_write_word(rtlpriv, currentcmd->para1,
27668c2ecf20Sopenharmony_ci				       (u16)currentcmd->para2);
27678c2ecf20Sopenharmony_ci			break;
27688c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_UCHAR:
27698c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, currentcmd->para1,
27708c2ecf20Sopenharmony_ci				       (u8)currentcmd->para2);
27718c2ecf20Sopenharmony_ci			break;
27728c2ecf20Sopenharmony_ci		case CMDID_RF_WRITEREG:
27738c2ecf20Sopenharmony_ci			for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
27748c2ecf20Sopenharmony_ci				rtlphy->rfreg_chnlval[rfpath] =
27758c2ecf20Sopenharmony_ci					((rtlphy->rfreg_chnlval[rfpath] &
27768c2ecf20Sopenharmony_ci					0xffffff00) | currentcmd->para2);
27778c2ecf20Sopenharmony_ci				if (rtlpriv->rtlhal.current_bandtype ==
27788c2ecf20Sopenharmony_ci				    BAND_ON_5G) {
27798c2ecf20Sopenharmony_ci					if (currentcmd->para2 > 99)
27808c2ecf20Sopenharmony_ci						rtlphy->rfreg_chnlval[rfpath] =
27818c2ecf20Sopenharmony_ci						    rtlphy->rfreg_chnlval
27828c2ecf20Sopenharmony_ci						    [rfpath] | (BIT(18));
27838c2ecf20Sopenharmony_ci					else
27848c2ecf20Sopenharmony_ci						rtlphy->rfreg_chnlval[rfpath] =
27858c2ecf20Sopenharmony_ci						    rtlphy->rfreg_chnlval
27868c2ecf20Sopenharmony_ci						    [rfpath] & (~BIT(18));
27878c2ecf20Sopenharmony_ci					rtlphy->rfreg_chnlval[rfpath] |=
27888c2ecf20Sopenharmony_ci						 (BIT(16) | BIT(8));
27898c2ecf20Sopenharmony_ci				} else {
27908c2ecf20Sopenharmony_ci					rtlphy->rfreg_chnlval[rfpath] &=
27918c2ecf20Sopenharmony_ci						~(BIT(8) | BIT(16) | BIT(18));
27928c2ecf20Sopenharmony_ci				}
27938c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)rfpath,
27948c2ecf20Sopenharmony_ci					      currentcmd->para1,
27958c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK,
27968c2ecf20Sopenharmony_ci					      rtlphy->rfreg_chnlval[rfpath]);
27978c2ecf20Sopenharmony_ci				_rtl92d_phy_reload_imr_setting(hw, channel,
27988c2ecf20Sopenharmony_ci							       rfpath);
27998c2ecf20Sopenharmony_ci			}
28008c2ecf20Sopenharmony_ci			_rtl92d_phy_switch_rf_setting(hw, channel);
28018c2ecf20Sopenharmony_ci			/* do IQK when all parameters are ready */
28028c2ecf20Sopenharmony_ci			rtl92d_phy_reload_iqk_setting(hw, channel);
28038c2ecf20Sopenharmony_ci			break;
28048c2ecf20Sopenharmony_ci		default:
28058c2ecf20Sopenharmony_ci			pr_err("switch case %#x not processed\n",
28068c2ecf20Sopenharmony_ci			       currentcmd->cmdid);
28078c2ecf20Sopenharmony_ci			break;
28088c2ecf20Sopenharmony_ci		}
28098c2ecf20Sopenharmony_ci		break;
28108c2ecf20Sopenharmony_ci	} while (true);
28118c2ecf20Sopenharmony_ci	(*delay) = currentcmd->msdelay;
28128c2ecf20Sopenharmony_ci	(*step)++;
28138c2ecf20Sopenharmony_ci	return false;
28148c2ecf20Sopenharmony_ci}
28158c2ecf20Sopenharmony_ci
28168c2ecf20Sopenharmony_ciu8 rtl92d_phy_sw_chnl(struct ieee80211_hw *hw)
28178c2ecf20Sopenharmony_ci{
28188c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
28198c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
28208c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
28218c2ecf20Sopenharmony_ci	u32 delay;
28228c2ecf20Sopenharmony_ci	u32 timeout = 1000, timecount = 0;
28238c2ecf20Sopenharmony_ci	u8 channel = rtlphy->current_channel;
28248c2ecf20Sopenharmony_ci	u32 ret_value;
28258c2ecf20Sopenharmony_ci
28268c2ecf20Sopenharmony_ci	if (rtlphy->sw_chnl_inprogress)
28278c2ecf20Sopenharmony_ci		return 0;
28288c2ecf20Sopenharmony_ci	if (rtlphy->set_bwmode_inprogress)
28298c2ecf20Sopenharmony_ci		return 0;
28308c2ecf20Sopenharmony_ci
28318c2ecf20Sopenharmony_ci	if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
28328c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
28338c2ecf20Sopenharmony_ci			"sw_chnl_inprogress false driver sleep or unload\n");
28348c2ecf20Sopenharmony_ci		return 0;
28358c2ecf20Sopenharmony_ci	}
28368c2ecf20Sopenharmony_ci	while (rtlphy->lck_inprogress && timecount < timeout) {
28378c2ecf20Sopenharmony_ci		mdelay(50);
28388c2ecf20Sopenharmony_ci		timecount += 50;
28398c2ecf20Sopenharmony_ci	}
28408c2ecf20Sopenharmony_ci	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
28418c2ecf20Sopenharmony_ci	    rtlhal->bandset == BAND_ON_BOTH) {
28428c2ecf20Sopenharmony_ci		ret_value = rtl_get_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
28438c2ecf20Sopenharmony_ci					  MASKDWORD);
28448c2ecf20Sopenharmony_ci		if (rtlphy->current_channel > 14 && !(ret_value & BIT(0)))
28458c2ecf20Sopenharmony_ci			rtl92d_phy_switch_wirelessband(hw, BAND_ON_5G);
28468c2ecf20Sopenharmony_ci		else if (rtlphy->current_channel <= 14 && (ret_value & BIT(0)))
28478c2ecf20Sopenharmony_ci			rtl92d_phy_switch_wirelessband(hw, BAND_ON_2_4G);
28488c2ecf20Sopenharmony_ci	}
28498c2ecf20Sopenharmony_ci	switch (rtlhal->current_bandtype) {
28508c2ecf20Sopenharmony_ci	case BAND_ON_5G:
28518c2ecf20Sopenharmony_ci		/* Get first channel error when change between
28528c2ecf20Sopenharmony_ci		 * 5G and 2.4G band. */
28538c2ecf20Sopenharmony_ci		if (channel <= 14)
28548c2ecf20Sopenharmony_ci			return 0;
28558c2ecf20Sopenharmony_ci		WARN_ONCE((channel <= 14), "rtl8192de: 5G but channel<=14\n");
28568c2ecf20Sopenharmony_ci		break;
28578c2ecf20Sopenharmony_ci	case BAND_ON_2_4G:
28588c2ecf20Sopenharmony_ci		/* Get first channel error when change between
28598c2ecf20Sopenharmony_ci		 * 5G and 2.4G band. */
28608c2ecf20Sopenharmony_ci		if (channel > 14)
28618c2ecf20Sopenharmony_ci			return 0;
28628c2ecf20Sopenharmony_ci		WARN_ONCE((channel > 14), "rtl8192de: 2G but channel>14\n");
28638c2ecf20Sopenharmony_ci		break;
28648c2ecf20Sopenharmony_ci	default:
28658c2ecf20Sopenharmony_ci		WARN_ONCE(true, "rtl8192de: Invalid WirelessMode(%#x)!!\n",
28668c2ecf20Sopenharmony_ci			  rtlpriv->mac80211.mode);
28678c2ecf20Sopenharmony_ci		break;
28688c2ecf20Sopenharmony_ci	}
28698c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_inprogress = true;
28708c2ecf20Sopenharmony_ci	if (channel == 0)
28718c2ecf20Sopenharmony_ci		channel = 1;
28728c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_stage = 0;
28738c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_step = 0;
28748c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
28758c2ecf20Sopenharmony_ci		"switch to channel%d\n", rtlphy->current_channel);
28768c2ecf20Sopenharmony_ci
28778c2ecf20Sopenharmony_ci	do {
28788c2ecf20Sopenharmony_ci		if (!rtlphy->sw_chnl_inprogress)
28798c2ecf20Sopenharmony_ci			break;
28808c2ecf20Sopenharmony_ci		if (!_rtl92d_phy_sw_chnl_step_by_step(hw,
28818c2ecf20Sopenharmony_ci						      rtlphy->current_channel,
28828c2ecf20Sopenharmony_ci		    &rtlphy->sw_chnl_stage, &rtlphy->sw_chnl_step, &delay)) {
28838c2ecf20Sopenharmony_ci			if (delay > 0)
28848c2ecf20Sopenharmony_ci				mdelay(delay);
28858c2ecf20Sopenharmony_ci			else
28868c2ecf20Sopenharmony_ci				continue;
28878c2ecf20Sopenharmony_ci		} else {
28888c2ecf20Sopenharmony_ci			rtlphy->sw_chnl_inprogress = false;
28898c2ecf20Sopenharmony_ci		}
28908c2ecf20Sopenharmony_ci		break;
28918c2ecf20Sopenharmony_ci	} while (true);
28928c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "<==\n");
28938c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_inprogress = false;
28948c2ecf20Sopenharmony_ci	return 1;
28958c2ecf20Sopenharmony_ci}
28968c2ecf20Sopenharmony_ci
28978c2ecf20Sopenharmony_cistatic void rtl92d_phy_set_io(struct ieee80211_hw *hw)
28988c2ecf20Sopenharmony_ci{
28998c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
29008c2ecf20Sopenharmony_ci	struct dig_t *de_digtable = &rtlpriv->dm_digtable;
29018c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
29048c2ecf20Sopenharmony_ci		"--->Cmd(%#x), set_io_inprogress(%d)\n",
29058c2ecf20Sopenharmony_ci		rtlphy->current_io_type, rtlphy->set_io_inprogress);
29068c2ecf20Sopenharmony_ci	switch (rtlphy->current_io_type) {
29078c2ecf20Sopenharmony_ci	case IO_CMD_RESUME_DM_BY_SCAN:
29088c2ecf20Sopenharmony_ci		de_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
29098c2ecf20Sopenharmony_ci		rtl92d_dm_write_dig(hw);
29108c2ecf20Sopenharmony_ci		rtl92d_phy_set_txpower_level(hw, rtlphy->current_channel);
29118c2ecf20Sopenharmony_ci		break;
29128c2ecf20Sopenharmony_ci	case IO_CMD_PAUSE_DM_BY_SCAN:
29138c2ecf20Sopenharmony_ci		rtlphy->initgain_backup.xaagccore1 = de_digtable->cur_igvalue;
29148c2ecf20Sopenharmony_ci		de_digtable->cur_igvalue = 0x37;
29158c2ecf20Sopenharmony_ci		rtl92d_dm_write_dig(hw);
29168c2ecf20Sopenharmony_ci		break;
29178c2ecf20Sopenharmony_ci	default:
29188c2ecf20Sopenharmony_ci		pr_err("switch case %#x not processed\n",
29198c2ecf20Sopenharmony_ci		       rtlphy->current_io_type);
29208c2ecf20Sopenharmony_ci		break;
29218c2ecf20Sopenharmony_ci	}
29228c2ecf20Sopenharmony_ci	rtlphy->set_io_inprogress = false;
29238c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE, "<---(%#x)\n",
29248c2ecf20Sopenharmony_ci		rtlphy->current_io_type);
29258c2ecf20Sopenharmony_ci}
29268c2ecf20Sopenharmony_ci
29278c2ecf20Sopenharmony_cibool rtl92d_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
29288c2ecf20Sopenharmony_ci{
29298c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
29308c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
29318c2ecf20Sopenharmony_ci	bool postprocessing = false;
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
29348c2ecf20Sopenharmony_ci		"-->IO Cmd(%#x), set_io_inprogress(%d)\n",
29358c2ecf20Sopenharmony_ci		 iotype, rtlphy->set_io_inprogress);
29368c2ecf20Sopenharmony_ci	do {
29378c2ecf20Sopenharmony_ci		switch (iotype) {
29388c2ecf20Sopenharmony_ci		case IO_CMD_RESUME_DM_BY_SCAN:
29398c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
29408c2ecf20Sopenharmony_ci				"[IO CMD] Resume DM after scan\n");
29418c2ecf20Sopenharmony_ci			postprocessing = true;
29428c2ecf20Sopenharmony_ci			break;
29438c2ecf20Sopenharmony_ci		case IO_CMD_PAUSE_DM_BY_SCAN:
29448c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
29458c2ecf20Sopenharmony_ci				"[IO CMD] Pause DM before scan\n");
29468c2ecf20Sopenharmony_ci			postprocessing = true;
29478c2ecf20Sopenharmony_ci			break;
29488c2ecf20Sopenharmony_ci		default:
29498c2ecf20Sopenharmony_ci			pr_err("switch case %#x not processed\n",
29508c2ecf20Sopenharmony_ci			       iotype);
29518c2ecf20Sopenharmony_ci			break;
29528c2ecf20Sopenharmony_ci		}
29538c2ecf20Sopenharmony_ci	} while (false);
29548c2ecf20Sopenharmony_ci	if (postprocessing && !rtlphy->set_io_inprogress) {
29558c2ecf20Sopenharmony_ci		rtlphy->set_io_inprogress = true;
29568c2ecf20Sopenharmony_ci		rtlphy->current_io_type = iotype;
29578c2ecf20Sopenharmony_ci	} else {
29588c2ecf20Sopenharmony_ci		return false;
29598c2ecf20Sopenharmony_ci	}
29608c2ecf20Sopenharmony_ci	rtl92d_phy_set_io(hw);
29618c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE, "<--IO Type(%#x)\n", iotype);
29628c2ecf20Sopenharmony_ci	return true;
29638c2ecf20Sopenharmony_ci}
29648c2ecf20Sopenharmony_ci
29658c2ecf20Sopenharmony_cistatic void _rtl92d_phy_set_rfon(struct ieee80211_hw *hw)
29668c2ecf20Sopenharmony_ci{
29678c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
29688c2ecf20Sopenharmony_ci
29698c2ecf20Sopenharmony_ci	/* a.  SYS_CLKR 0x08[11] = 1  restore MAC clock */
29708c2ecf20Sopenharmony_ci	/* b.  SPS_CTRL 0x11[7:0] = 0x2b */
29718c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
29728c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
29738c2ecf20Sopenharmony_ci	/* c.  For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE3 enable BB TRX function */
29748c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
29758c2ecf20Sopenharmony_ci	/* RF_ON_EXCEP(d~g): */
29768c2ecf20Sopenharmony_ci	/* d.  APSD_CTRL 0x600[7:0] = 0x00 */
29778c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
29788c2ecf20Sopenharmony_ci	/* e.  SYS_FUNC_EN 0x02[7:0] = 0xE2  reset BB TRX function again */
29798c2ecf20Sopenharmony_ci	/* f.  SYS_FUNC_EN 0x02[7:0] = 0xE3  enable BB TRX function*/
29808c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
29818c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
29828c2ecf20Sopenharmony_ci	/* g.   txpause 0x522[7:0] = 0x00  enable mac tx queue */
29838c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
29848c2ecf20Sopenharmony_ci}
29858c2ecf20Sopenharmony_ci
29868c2ecf20Sopenharmony_cistatic void _rtl92d_phy_set_rfsleep(struct ieee80211_hw *hw)
29878c2ecf20Sopenharmony_ci{
29888c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
29898c2ecf20Sopenharmony_ci	u32 u4btmp;
29908c2ecf20Sopenharmony_ci	u8 delay = 5;
29918c2ecf20Sopenharmony_ci
29928c2ecf20Sopenharmony_ci	/* a.   TXPAUSE 0x522[7:0] = 0xFF  Pause MAC TX queue  */
29938c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
29948c2ecf20Sopenharmony_ci	/* b.   RF path 0 offset 0x00 = 0x00  disable RF  */
29958c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
29968c2ecf20Sopenharmony_ci	/* c.   APSD_CTRL 0x600[7:0] = 0x40 */
29978c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
29988c2ecf20Sopenharmony_ci	/* d. APSD_CTRL 0x600[7:0] = 0x00
29998c2ecf20Sopenharmony_ci	 * APSD_CTRL 0x600[7:0] = 0x00
30008c2ecf20Sopenharmony_ci	 * RF path 0 offset 0x00 = 0x00
30018c2ecf20Sopenharmony_ci	 * APSD_CTRL 0x600[7:0] = 0x40
30028c2ecf20Sopenharmony_ci	 * */
30038c2ecf20Sopenharmony_ci	u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
30048c2ecf20Sopenharmony_ci	while (u4btmp != 0 && delay > 0) {
30058c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x0);
30068c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
30078c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x40);
30088c2ecf20Sopenharmony_ci		u4btmp = rtl_get_rfreg(hw, RF90_PATH_A, 0, RFREG_OFFSET_MASK);
30098c2ecf20Sopenharmony_ci		delay--;
30108c2ecf20Sopenharmony_ci	}
30118c2ecf20Sopenharmony_ci	if (delay == 0) {
30128c2ecf20Sopenharmony_ci		/* Jump out the LPS turn off sequence to RF_ON_EXCEP */
30138c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_APSD_CTRL, 0x00);
30148c2ecf20Sopenharmony_ci
30158c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
30168c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
30178c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
30188c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
30198c2ecf20Sopenharmony_ci			"Fail !!! Switch RF timeout\n");
30208c2ecf20Sopenharmony_ci		return;
30218c2ecf20Sopenharmony_ci	}
30228c2ecf20Sopenharmony_ci	/* e.   For PCIE: SYS_FUNC_EN 0x02[7:0] = 0xE2 reset BB TRX function */
30238c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
30248c2ecf20Sopenharmony_ci	/* f.   SPS_CTRL 0x11[7:0] = 0x22 */
30258c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.macphymode == SINGLEMAC_SINGLEPHY)
30268c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
30278c2ecf20Sopenharmony_ci	/* g.    SYS_CLKR 0x08[11] = 0  gated MAC clock */
30288c2ecf20Sopenharmony_ci}
30298c2ecf20Sopenharmony_ci
30308c2ecf20Sopenharmony_cibool rtl92d_phy_set_rf_power_state(struct ieee80211_hw *hw,
30318c2ecf20Sopenharmony_ci				   enum rf_pwrstate rfpwr_state)
30328c2ecf20Sopenharmony_ci{
30338c2ecf20Sopenharmony_ci
30348c2ecf20Sopenharmony_ci	bool bresult = true;
30358c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
30368c2ecf20Sopenharmony_ci	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
30378c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
30388c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
30398c2ecf20Sopenharmony_ci	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
30408c2ecf20Sopenharmony_ci	u8 i, queue_id;
30418c2ecf20Sopenharmony_ci	struct rtl8192_tx_ring *ring = NULL;
30428c2ecf20Sopenharmony_ci
30438c2ecf20Sopenharmony_ci	if (rfpwr_state == ppsc->rfpwr_state)
30448c2ecf20Sopenharmony_ci		return false;
30458c2ecf20Sopenharmony_ci	switch (rfpwr_state) {
30468c2ecf20Sopenharmony_ci	case ERFON:
30478c2ecf20Sopenharmony_ci		if ((ppsc->rfpwr_state == ERFOFF) &&
30488c2ecf20Sopenharmony_ci		    RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
30498c2ecf20Sopenharmony_ci			bool rtstatus;
30508c2ecf20Sopenharmony_ci			u32 initializecount = 0;
30518c2ecf20Sopenharmony_ci			do {
30528c2ecf20Sopenharmony_ci				initializecount++;
30538c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
30548c2ecf20Sopenharmony_ci					"IPS Set eRf nic enable\n");
30558c2ecf20Sopenharmony_ci				rtstatus = rtl_ps_enable_nic(hw);
30568c2ecf20Sopenharmony_ci			} while (!rtstatus && (initializecount < 10));
30578c2ecf20Sopenharmony_ci
30588c2ecf20Sopenharmony_ci			RT_CLEAR_PS_LEVEL(ppsc,
30598c2ecf20Sopenharmony_ci					  RT_RF_OFF_LEVL_HALT_NIC);
30608c2ecf20Sopenharmony_ci		} else {
30618c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG,
30628c2ecf20Sopenharmony_ci				"awake, slept:%d ms state_inap:%x\n",
30638c2ecf20Sopenharmony_ci				jiffies_to_msecs(jiffies -
30648c2ecf20Sopenharmony_ci						 ppsc->last_sleep_jiffies),
30658c2ecf20Sopenharmony_ci				 rtlpriv->psc.state_inap);
30668c2ecf20Sopenharmony_ci			ppsc->last_awake_jiffies = jiffies;
30678c2ecf20Sopenharmony_ci			_rtl92d_phy_set_rfon(hw);
30688c2ecf20Sopenharmony_ci		}
30698c2ecf20Sopenharmony_ci
30708c2ecf20Sopenharmony_ci		if (mac->link_state == MAC80211_LINKED)
30718c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->led_control(hw,
30728c2ecf20Sopenharmony_ci					 LED_CTL_LINK);
30738c2ecf20Sopenharmony_ci		else
30748c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->led_control(hw,
30758c2ecf20Sopenharmony_ci					 LED_CTL_NO_LINK);
30768c2ecf20Sopenharmony_ci		break;
30778c2ecf20Sopenharmony_ci	case ERFOFF:
30788c2ecf20Sopenharmony_ci		if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
30798c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
30808c2ecf20Sopenharmony_ci				"IPS Set eRf nic disable\n");
30818c2ecf20Sopenharmony_ci			rtl_ps_disable_nic(hw);
30828c2ecf20Sopenharmony_ci			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
30838c2ecf20Sopenharmony_ci		} else {
30848c2ecf20Sopenharmony_ci			if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS)
30858c2ecf20Sopenharmony_ci				rtlpriv->cfg->ops->led_control(hw,
30868c2ecf20Sopenharmony_ci						 LED_CTL_NO_LINK);
30878c2ecf20Sopenharmony_ci			else
30888c2ecf20Sopenharmony_ci				rtlpriv->cfg->ops->led_control(hw,
30898c2ecf20Sopenharmony_ci						 LED_CTL_POWER_OFF);
30908c2ecf20Sopenharmony_ci		}
30918c2ecf20Sopenharmony_ci		break;
30928c2ecf20Sopenharmony_ci	case ERFSLEEP:
30938c2ecf20Sopenharmony_ci		if (ppsc->rfpwr_state == ERFOFF)
30948c2ecf20Sopenharmony_ci			return false;
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_ci		for (queue_id = 0, i = 0;
30978c2ecf20Sopenharmony_ci		     queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
30988c2ecf20Sopenharmony_ci			ring = &pcipriv->dev.tx_ring[queue_id];
30998c2ecf20Sopenharmony_ci			if (skb_queue_len(&ring->queue) == 0 ||
31008c2ecf20Sopenharmony_ci			    queue_id == BEACON_QUEUE) {
31018c2ecf20Sopenharmony_ci				queue_id++;
31028c2ecf20Sopenharmony_ci				continue;
31038c2ecf20Sopenharmony_ci			} else if (rtlpci->pdev->current_state != PCI_D0) {
31048c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
31058c2ecf20Sopenharmony_ci					"eRf Off/Sleep: %d times TcbBusyQueue[%d] !=0 but lower power state!\n",
31068c2ecf20Sopenharmony_ci					i + 1, queue_id);
31078c2ecf20Sopenharmony_ci				break;
31088c2ecf20Sopenharmony_ci			} else {
31098c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
31108c2ecf20Sopenharmony_ci					"eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
31118c2ecf20Sopenharmony_ci					i + 1, queue_id,
31128c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
31138c2ecf20Sopenharmony_ci				udelay(10);
31148c2ecf20Sopenharmony_ci				i++;
31158c2ecf20Sopenharmony_ci			}
31168c2ecf20Sopenharmony_ci
31178c2ecf20Sopenharmony_ci			if (i >= MAX_DOZE_WAITING_TIMES_9x) {
31188c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
31198c2ecf20Sopenharmony_ci					"ERFOFF: %d times TcbBusyQueue[%d] = %d !\n",
31208c2ecf20Sopenharmony_ci					MAX_DOZE_WAITING_TIMES_9x, queue_id,
31218c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
31228c2ecf20Sopenharmony_ci				break;
31238c2ecf20Sopenharmony_ci			}
31248c2ecf20Sopenharmony_ci		}
31258c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG,
31268c2ecf20Sopenharmony_ci			"Set rfsleep awakened:%d ms\n",
31278c2ecf20Sopenharmony_ci			 jiffies_to_msecs(jiffies - ppsc->last_awake_jiffies));
31288c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_DMESG,
31298c2ecf20Sopenharmony_ci			"sleep awakened:%d ms state_inap:%x\n",
31308c2ecf20Sopenharmony_ci			jiffies_to_msecs(jiffies -
31318c2ecf20Sopenharmony_ci					 ppsc->last_awake_jiffies),
31328c2ecf20Sopenharmony_ci			rtlpriv->psc.state_inap);
31338c2ecf20Sopenharmony_ci		ppsc->last_sleep_jiffies = jiffies;
31348c2ecf20Sopenharmony_ci		_rtl92d_phy_set_rfsleep(hw);
31358c2ecf20Sopenharmony_ci		break;
31368c2ecf20Sopenharmony_ci	default:
31378c2ecf20Sopenharmony_ci		pr_err("switch case %#x not processed\n",
31388c2ecf20Sopenharmony_ci		       rfpwr_state);
31398c2ecf20Sopenharmony_ci		bresult = false;
31408c2ecf20Sopenharmony_ci		break;
31418c2ecf20Sopenharmony_ci	}
31428c2ecf20Sopenharmony_ci	if (bresult)
31438c2ecf20Sopenharmony_ci		ppsc->rfpwr_state = rfpwr_state;
31448c2ecf20Sopenharmony_ci	return bresult;
31458c2ecf20Sopenharmony_ci}
31468c2ecf20Sopenharmony_ci
31478c2ecf20Sopenharmony_civoid rtl92d_phy_config_macphymode(struct ieee80211_hw *hw)
31488c2ecf20Sopenharmony_ci{
31498c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
31508c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
31518c2ecf20Sopenharmony_ci	u8 offset = REG_MAC_PHY_CTRL_NORMAL;
31528c2ecf20Sopenharmony_ci
31538c2ecf20Sopenharmony_ci	switch (rtlhal->macphymode) {
31548c2ecf20Sopenharmony_ci	case DUALMAC_DUALPHY:
31558c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
31568c2ecf20Sopenharmony_ci			"MacPhyMode: DUALMAC_DUALPHY\n");
31578c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, offset, 0xF3);
31588c2ecf20Sopenharmony_ci		break;
31598c2ecf20Sopenharmony_ci	case SINGLEMAC_SINGLEPHY:
31608c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
31618c2ecf20Sopenharmony_ci			"MacPhyMode: SINGLEMAC_SINGLEPHY\n");
31628c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, offset, 0xF4);
31638c2ecf20Sopenharmony_ci		break;
31648c2ecf20Sopenharmony_ci	case DUALMAC_SINGLEPHY:
31658c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
31668c2ecf20Sopenharmony_ci			"MacPhyMode: DUALMAC_SINGLEPHY\n");
31678c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, offset, 0xF1);
31688c2ecf20Sopenharmony_ci		break;
31698c2ecf20Sopenharmony_ci	}
31708c2ecf20Sopenharmony_ci}
31718c2ecf20Sopenharmony_ci
31728c2ecf20Sopenharmony_civoid rtl92d_phy_config_macphymode_info(struct ieee80211_hw *hw)
31738c2ecf20Sopenharmony_ci{
31748c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
31758c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
31768c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
31778c2ecf20Sopenharmony_ci
31788c2ecf20Sopenharmony_ci	switch (rtlhal->macphymode) {
31798c2ecf20Sopenharmony_ci	case DUALMAC_SINGLEPHY:
31808c2ecf20Sopenharmony_ci		rtlphy->rf_type = RF_2T2R;
31818c2ecf20Sopenharmony_ci		rtlhal->version |= RF_TYPE_2T2R;
31828c2ecf20Sopenharmony_ci		rtlhal->bandset = BAND_ON_BOTH;
31838c2ecf20Sopenharmony_ci		rtlhal->current_bandtype = BAND_ON_2_4G;
31848c2ecf20Sopenharmony_ci		break;
31858c2ecf20Sopenharmony_ci
31868c2ecf20Sopenharmony_ci	case SINGLEMAC_SINGLEPHY:
31878c2ecf20Sopenharmony_ci		rtlphy->rf_type = RF_2T2R;
31888c2ecf20Sopenharmony_ci		rtlhal->version |= RF_TYPE_2T2R;
31898c2ecf20Sopenharmony_ci		rtlhal->bandset = BAND_ON_BOTH;
31908c2ecf20Sopenharmony_ci		rtlhal->current_bandtype = BAND_ON_2_4G;
31918c2ecf20Sopenharmony_ci		break;
31928c2ecf20Sopenharmony_ci
31938c2ecf20Sopenharmony_ci	case DUALMAC_DUALPHY:
31948c2ecf20Sopenharmony_ci		rtlphy->rf_type = RF_1T1R;
31958c2ecf20Sopenharmony_ci		rtlhal->version &= RF_TYPE_1T1R;
31968c2ecf20Sopenharmony_ci		/* Now we let MAC0 run on 5G band. */
31978c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 0) {
31988c2ecf20Sopenharmony_ci			rtlhal->bandset = BAND_ON_5G;
31998c2ecf20Sopenharmony_ci			rtlhal->current_bandtype = BAND_ON_5G;
32008c2ecf20Sopenharmony_ci		} else {
32018c2ecf20Sopenharmony_ci			rtlhal->bandset = BAND_ON_2_4G;
32028c2ecf20Sopenharmony_ci			rtlhal->current_bandtype = BAND_ON_2_4G;
32038c2ecf20Sopenharmony_ci		}
32048c2ecf20Sopenharmony_ci		break;
32058c2ecf20Sopenharmony_ci	default:
32068c2ecf20Sopenharmony_ci		break;
32078c2ecf20Sopenharmony_ci	}
32088c2ecf20Sopenharmony_ci}
32098c2ecf20Sopenharmony_ci
32108c2ecf20Sopenharmony_ciu8 rtl92d_get_chnlgroup_fromarray(u8 chnl)
32118c2ecf20Sopenharmony_ci{
32128c2ecf20Sopenharmony_ci	u8 group;
32138c2ecf20Sopenharmony_ci
32148c2ecf20Sopenharmony_ci	if (channel_all[chnl] <= 3)
32158c2ecf20Sopenharmony_ci		group = 0;
32168c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 9)
32178c2ecf20Sopenharmony_ci		group = 1;
32188c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 14)
32198c2ecf20Sopenharmony_ci		group = 2;
32208c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 44)
32218c2ecf20Sopenharmony_ci		group = 3;
32228c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 54)
32238c2ecf20Sopenharmony_ci		group = 4;
32248c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 64)
32258c2ecf20Sopenharmony_ci		group = 5;
32268c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 112)
32278c2ecf20Sopenharmony_ci		group = 6;
32288c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 126)
32298c2ecf20Sopenharmony_ci		group = 7;
32308c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 140)
32318c2ecf20Sopenharmony_ci		group = 8;
32328c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 153)
32338c2ecf20Sopenharmony_ci		group = 9;
32348c2ecf20Sopenharmony_ci	else if (channel_all[chnl] <= 159)
32358c2ecf20Sopenharmony_ci		group = 10;
32368c2ecf20Sopenharmony_ci	else
32378c2ecf20Sopenharmony_ci		group = 11;
32388c2ecf20Sopenharmony_ci	return group;
32398c2ecf20Sopenharmony_ci}
32408c2ecf20Sopenharmony_ci
32418c2ecf20Sopenharmony_civoid rtl92d_phy_set_poweron(struct ieee80211_hw *hw)
32428c2ecf20Sopenharmony_ci{
32438c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
32448c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
32458c2ecf20Sopenharmony_ci	unsigned long flags;
32468c2ecf20Sopenharmony_ci	u8 value8;
32478c2ecf20Sopenharmony_ci	u16 i;
32488c2ecf20Sopenharmony_ci	u32 mac_reg = (rtlhal->interfaceindex == 0 ? REG_MAC0 : REG_MAC1);
32498c2ecf20Sopenharmony_ci
32508c2ecf20Sopenharmony_ci	/* notice fw know band status  0x81[1]/0x53[1] = 0: 5G, 1: 2G */
32518c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
32528c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv, mac_reg);
32538c2ecf20Sopenharmony_ci		value8 |= BIT(1);
32548c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, mac_reg, value8);
32558c2ecf20Sopenharmony_ci	} else {
32568c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv, mac_reg);
32578c2ecf20Sopenharmony_ci		value8 &= (~BIT(1));
32588c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, mac_reg, value8);
32598c2ecf20Sopenharmony_ci	}
32608c2ecf20Sopenharmony_ci
32618c2ecf20Sopenharmony_ci	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
32628c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv, REG_MAC0);
32638c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
32648c2ecf20Sopenharmony_ci	} else {
32658c2ecf20Sopenharmony_ci		spin_lock_irqsave(&globalmutex_power, flags);
32668c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 0) {
32678c2ecf20Sopenharmony_ci			value8 = rtl_read_byte(rtlpriv, REG_MAC0);
32688c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_MAC0, value8 | MAC0_ON);
32698c2ecf20Sopenharmony_ci		} else {
32708c2ecf20Sopenharmony_ci			value8 = rtl_read_byte(rtlpriv, REG_MAC1);
32718c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_MAC1, value8 | MAC1_ON);
32728c2ecf20Sopenharmony_ci		}
32738c2ecf20Sopenharmony_ci		value8 = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
32748c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&globalmutex_power, flags);
32758c2ecf20Sopenharmony_ci		for (i = 0; i < 200; i++) {
32768c2ecf20Sopenharmony_ci			if ((value8 & BIT(7)) == 0) {
32778c2ecf20Sopenharmony_ci				break;
32788c2ecf20Sopenharmony_ci			} else {
32798c2ecf20Sopenharmony_ci				udelay(500);
32808c2ecf20Sopenharmony_ci				spin_lock_irqsave(&globalmutex_power, flags);
32818c2ecf20Sopenharmony_ci				value8 = rtl_read_byte(rtlpriv,
32828c2ecf20Sopenharmony_ci						    REG_POWER_OFF_IN_PROCESS);
32838c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&globalmutex_power,
32848c2ecf20Sopenharmony_ci						       flags);
32858c2ecf20Sopenharmony_ci			}
32868c2ecf20Sopenharmony_ci		}
32878c2ecf20Sopenharmony_ci		if (i == 200)
32888c2ecf20Sopenharmony_ci			WARN_ONCE(true, "rtl8192de: Another mac power off over time\n");
32898c2ecf20Sopenharmony_ci	}
32908c2ecf20Sopenharmony_ci}
32918c2ecf20Sopenharmony_ci
32928c2ecf20Sopenharmony_civoid rtl92d_phy_config_maccoexist_rfpage(struct ieee80211_hw *hw)
32938c2ecf20Sopenharmony_ci{
32948c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
32958c2ecf20Sopenharmony_ci
32968c2ecf20Sopenharmony_ci	switch (rtlpriv->rtlhal.macphymode) {
32978c2ecf20Sopenharmony_ci	case DUALMAC_DUALPHY:
32988c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_DMC, 0x0);
32998c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
33008c2ecf20Sopenharmony_ci		rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
33018c2ecf20Sopenharmony_ci		break;
33028c2ecf20Sopenharmony_ci	case DUALMAC_SINGLEPHY:
33038c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_DMC, 0xf8);
33048c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x08);
33058c2ecf20Sopenharmony_ci		rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x13ff);
33068c2ecf20Sopenharmony_ci		break;
33078c2ecf20Sopenharmony_ci	case SINGLEMAC_SINGLEPHY:
33088c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_DMC, 0x0);
33098c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x10);
33108c2ecf20Sopenharmony_ci		rtl_write_word(rtlpriv, (REG_TRXFF_BNDY + 2), 0x27FF);
33118c2ecf20Sopenharmony_ci		break;
33128c2ecf20Sopenharmony_ci	default:
33138c2ecf20Sopenharmony_ci		break;
33148c2ecf20Sopenharmony_ci	}
33158c2ecf20Sopenharmony_ci}
33168c2ecf20Sopenharmony_ci
33178c2ecf20Sopenharmony_civoid rtl92d_update_bbrf_configuration(struct ieee80211_hw *hw)
33188c2ecf20Sopenharmony_ci{
33198c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
33208c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
33218c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
33228c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
33238c2ecf20Sopenharmony_ci	u8 rfpath, i;
33248c2ecf20Sopenharmony_ci
33258c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "==>\n");
33268c2ecf20Sopenharmony_ci	/* r_select_5G for path_A/B 0 for 2.4G, 1 for 5G */
33278c2ecf20Sopenharmony_ci	if (rtlhal->current_bandtype == BAND_ON_2_4G) {
33288c2ecf20Sopenharmony_ci		/* r_select_5G for path_A/B,0x878 */
33298c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x0);
33308c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x0);
33318c2ecf20Sopenharmony_ci		if (rtlhal->macphymode != DUALMAC_DUALPHY) {
33328c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x0);
33338c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x0);
33348c2ecf20Sopenharmony_ci		}
33358c2ecf20Sopenharmony_ci		/* rssi_table_select:index 0 for 2.4G.1~3 for 5G,0xc78 */
33368c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x0);
33378c2ecf20Sopenharmony_ci		/* fc_area  0xd2c */
33388c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x0);
33398c2ecf20Sopenharmony_ci		/* 5G LAN ON */
33408c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0xa);
33418c2ecf20Sopenharmony_ci		/* TX BB gain shift*1,Just for testchip,0xc80,0xc88 */
33428c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE, MASKDWORD,
33438c2ecf20Sopenharmony_ci			      0x40000100);
33448c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, MASKDWORD,
33458c2ecf20Sopenharmony_ci			      0x40000100);
33468c2ecf20Sopenharmony_ci		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
33478c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
33488c2ecf20Sopenharmony_ci				      BIT(10) | BIT(6) | BIT(5),
33498c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
33508c2ecf20Sopenharmony_ci				      (rtlefuse->eeprom_c9 & BIT(1)) |
33518c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(1)) << 4));
33528c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
33538c2ecf20Sopenharmony_ci				      BIT(10) | BIT(6) | BIT(5),
33548c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
33558c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
33568c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(0)) << 5));
33578c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0);
33588c2ecf20Sopenharmony_ci		} else {
33598c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
33608c2ecf20Sopenharmony_ci				      BIT(26) | BIT(22) | BIT(21) | BIT(10) |
33618c2ecf20Sopenharmony_ci				      BIT(6) | BIT(5),
33628c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(3)) >> 3) |
33638c2ecf20Sopenharmony_ci				      (rtlefuse->eeprom_c9 & BIT(1)) |
33648c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(1)) << 4) |
33658c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(7)) << 9) |
33668c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(5)) << 12) |
33678c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(3)) << 18));
33688c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE,
33698c2ecf20Sopenharmony_ci				      BIT(10) | BIT(6) | BIT(5),
33708c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(2)) >> 2) |
33718c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(0)) << 1) |
33728c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(0)) << 5));
33738c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE,
33748c2ecf20Sopenharmony_ci				      BIT(10) | BIT(6) | BIT(5),
33758c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(6)) >> 6) |
33768c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_c9 & BIT(4)) >> 3) |
33778c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(2)) << 3));
33788c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
33798c2ecf20Sopenharmony_ci				      BIT(31) | BIT(15), 0);
33808c2ecf20Sopenharmony_ci		}
33818c2ecf20Sopenharmony_ci		/* 1.5V_LDO */
33828c2ecf20Sopenharmony_ci	} else {
33838c2ecf20Sopenharmony_ci		/* r_select_5G for path_A/B */
33848c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(0), 0x1);
33858c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15), 0x1);
33868c2ecf20Sopenharmony_ci		if (rtlhal->macphymode != DUALMAC_DUALPHY) {
33878c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(16), 0x1);
33888c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(31), 0x1);
33898c2ecf20Sopenharmony_ci		}
33908c2ecf20Sopenharmony_ci		/* rssi_table_select:index 0 for 2.4G.1~3 for 5G */
33918c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, BIT(6) | BIT(7), 0x1);
33928c2ecf20Sopenharmony_ci		/* fc_area */
33938c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(14) | BIT(13), 0x1);
33948c2ecf20Sopenharmony_ci		/* 5G LAN ON */
33958c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xB30, 0x00F00000, 0x0);
33968c2ecf20Sopenharmony_ci		/* TX BB gain shift,Just for testchip,0xc80,0xc88 */
33978c2ecf20Sopenharmony_ci		if (rtlefuse->internal_pa_5g[0])
33988c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE, MASKDWORD,
33998c2ecf20Sopenharmony_ci				      0x2d4000b5);
34008c2ecf20Sopenharmony_ci		else
34018c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XATXIQIMBALANCE, MASKDWORD,
34028c2ecf20Sopenharmony_ci				      0x20000080);
34038c2ecf20Sopenharmony_ci		if (rtlefuse->internal_pa_5g[1])
34048c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, MASKDWORD,
34058c2ecf20Sopenharmony_ci				      0x2d4000b5);
34068c2ecf20Sopenharmony_ci		else
34078c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, MASKDWORD,
34088c2ecf20Sopenharmony_ci				      0x20000080);
34098c2ecf20Sopenharmony_ci		if (rtlhal->macphymode == DUALMAC_DUALPHY) {
34108c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
34118c2ecf20Sopenharmony_ci				      BIT(10) | BIT(6) | BIT(5),
34128c2ecf20Sopenharmony_ci				      (rtlefuse->eeprom_cc & BIT(5)));
34138c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
34148c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
34158c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER, BIT(15),
34168c2ecf20Sopenharmony_ci				      (rtlefuse->eeprom_cc & BIT(4)) >> 4);
34178c2ecf20Sopenharmony_ci		} else {
34188c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFINTERFACESW,
34198c2ecf20Sopenharmony_ci				      BIT(26) | BIT(22) | BIT(21) | BIT(10) |
34208c2ecf20Sopenharmony_ci				      BIT(6) | BIT(5),
34218c2ecf20Sopenharmony_ci				      (rtlefuse->eeprom_cc & BIT(5)) |
34228c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(7)) << 14));
34238c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XA_RFINTERFACEOE, BIT(10),
34248c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4));
34258c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XB_RFINTERFACEOE, BIT(10),
34268c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(6)) >> 6));
34278c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_XAB_RFPARAMETER,
34288c2ecf20Sopenharmony_ci				      BIT(31) | BIT(15),
34298c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(4)) >> 4) |
34308c2ecf20Sopenharmony_ci				      ((rtlefuse->eeprom_cc & BIT(6)) << 10));
34318c2ecf20Sopenharmony_ci		}
34328c2ecf20Sopenharmony_ci	}
34338c2ecf20Sopenharmony_ci	/* update IQK related settings */
34348c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_XARXIQIMBALANCE, MASKDWORD, 0x40000100);
34358c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, MASKDWORD, 0x40000100);
34368c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_XCTXAFE, 0xF0000000, 0x00);
34378c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(30) | BIT(28) |
34388c2ecf20Sopenharmony_ci		      BIT(26) | BIT(24), 0x00);
34398c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_XDTXAFE, 0xF0000000, 0x00);
34408c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xca0, 0xF0000000, 0x00);
34418c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, ROFDM0_AGCRSSITABLE, 0x0000F000, 0x00);
34428c2ecf20Sopenharmony_ci
34438c2ecf20Sopenharmony_ci	/* Update RF */
34448c2ecf20Sopenharmony_ci	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
34458c2ecf20Sopenharmony_ci	     rfpath++) {
34468c2ecf20Sopenharmony_ci		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
34478c2ecf20Sopenharmony_ci			/* MOD_AG for RF path_A 0x18 BIT8,BIT16 */
34488c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) | BIT(16) |
34498c2ecf20Sopenharmony_ci				      BIT(18), 0);
34508c2ecf20Sopenharmony_ci			/* RF0x0b[16:14] =3b'111 */
34518c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, (enum radio_path)rfpath, 0x0B,
34528c2ecf20Sopenharmony_ci				      0x1c000, 0x07);
34538c2ecf20Sopenharmony_ci		} else {
34548c2ecf20Sopenharmony_ci			/* MOD_AG for RF path_A 0x18 BIT8,BIT16 */
34558c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, rfpath, RF_CHNLBW, BIT(8) |
34568c2ecf20Sopenharmony_ci				      BIT(16) | BIT(18),
34578c2ecf20Sopenharmony_ci				      (BIT(16) | BIT(8)) >> 8);
34588c2ecf20Sopenharmony_ci		}
34598c2ecf20Sopenharmony_ci	}
34608c2ecf20Sopenharmony_ci	/* Update for all band. */
34618c2ecf20Sopenharmony_ci	/* DMDP */
34628c2ecf20Sopenharmony_ci	if (rtlphy->rf_type == RF_1T1R) {
34638c2ecf20Sopenharmony_ci		/* Use antenna 0,0xc04,0xd04 */
34648c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x11);
34658c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x1);
34668c2ecf20Sopenharmony_ci
34678c2ecf20Sopenharmony_ci		/* enable ad/da clock1 for dual-phy reg0x888 */
34688c2ecf20Sopenharmony_ci		if (rtlhal->interfaceindex == 0) {
34698c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) |
34708c2ecf20Sopenharmony_ci				      BIT(13), 0x3);
34718c2ecf20Sopenharmony_ci		} else {
34728c2ecf20Sopenharmony_ci			rtl92d_phy_enable_anotherphy(hw, false);
34738c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
34748c2ecf20Sopenharmony_ci				"MAC1 use DBI to update 0x888\n");
34758c2ecf20Sopenharmony_ci			/* 0x888 */
34768c2ecf20Sopenharmony_ci			rtl92de_write_dword_dbi(hw, RFPGA0_ADDALLOCKEN,
34778c2ecf20Sopenharmony_ci						rtl92de_read_dword_dbi(hw,
34788c2ecf20Sopenharmony_ci						RFPGA0_ADDALLOCKEN,
34798c2ecf20Sopenharmony_ci						BIT(3)) | BIT(12) | BIT(13),
34808c2ecf20Sopenharmony_ci						BIT(3));
34818c2ecf20Sopenharmony_ci			rtl92d_phy_powerdown_anotherphy(hw, false);
34828c2ecf20Sopenharmony_ci		}
34838c2ecf20Sopenharmony_ci	} else {
34848c2ecf20Sopenharmony_ci		/* Single PHY */
34858c2ecf20Sopenharmony_ci		/* Use antenna 0 & 1,0xc04,0xd04 */
34868c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_TRXPATHENABLE, MASKBYTE0, 0x33);
34878c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_TRXPATHENABLE, BDWORD, 0x3);
34888c2ecf20Sopenharmony_ci		/* disable ad/da clock1,0x888 */
34898c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_ADDALLOCKEN, BIT(12) | BIT(13), 0);
34908c2ecf20Sopenharmony_ci	}
34918c2ecf20Sopenharmony_ci	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
34928c2ecf20Sopenharmony_ci	     rfpath++) {
34938c2ecf20Sopenharmony_ci		rtlphy->rfreg_chnlval[rfpath] = rtl_get_rfreg(hw, rfpath,
34948c2ecf20Sopenharmony_ci						RF_CHNLBW, RFREG_OFFSET_MASK);
34958c2ecf20Sopenharmony_ci		rtlphy->reg_rf3c[rfpath] = rtl_get_rfreg(hw, rfpath, 0x3C,
34968c2ecf20Sopenharmony_ci			RFREG_OFFSET_MASK);
34978c2ecf20Sopenharmony_ci	}
34988c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++)
34998c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_RF, DBG_LOUD, "RF 0x18 = 0x%x\n",
35008c2ecf20Sopenharmony_ci			rtlphy->rfreg_chnlval[i]);
35018c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "<==\n");
35028c2ecf20Sopenharmony_ci
35038c2ecf20Sopenharmony_ci}
35048c2ecf20Sopenharmony_ci
35058c2ecf20Sopenharmony_cibool rtl92d_phy_check_poweroff(struct ieee80211_hw *hw)
35068c2ecf20Sopenharmony_ci{
35078c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
35088c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
35098c2ecf20Sopenharmony_ci	u8 u1btmp;
35108c2ecf20Sopenharmony_ci	unsigned long flags;
35118c2ecf20Sopenharmony_ci
35128c2ecf20Sopenharmony_ci	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY) {
35138c2ecf20Sopenharmony_ci		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
35148c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
35158c2ecf20Sopenharmony_ci		return true;
35168c2ecf20Sopenharmony_ci	}
35178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&globalmutex_power, flags);
35188c2ecf20Sopenharmony_ci	if (rtlhal->interfaceindex == 0) {
35198c2ecf20Sopenharmony_ci		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
35208c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAC0, u1btmp & (~MAC0_ON));
35218c2ecf20Sopenharmony_ci		u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
35228c2ecf20Sopenharmony_ci		u1btmp &= MAC1_ON;
35238c2ecf20Sopenharmony_ci	} else {
35248c2ecf20Sopenharmony_ci		u1btmp = rtl_read_byte(rtlpriv, REG_MAC1);
35258c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MAC1, u1btmp & (~MAC1_ON));
35268c2ecf20Sopenharmony_ci		u1btmp = rtl_read_byte(rtlpriv, REG_MAC0);
35278c2ecf20Sopenharmony_ci		u1btmp &= MAC0_ON;
35288c2ecf20Sopenharmony_ci	}
35298c2ecf20Sopenharmony_ci	if (u1btmp) {
35308c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&globalmutex_power, flags);
35318c2ecf20Sopenharmony_ci		return false;
35328c2ecf20Sopenharmony_ci	}
35338c2ecf20Sopenharmony_ci	u1btmp = rtl_read_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS);
35348c2ecf20Sopenharmony_ci	u1btmp |= BIT(7);
35358c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_POWER_OFF_IN_PROCESS, u1btmp);
35368c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&globalmutex_power, flags);
35378c2ecf20Sopenharmony_ci	return true;
35388c2ecf20Sopenharmony_ci}
3539