12a59fe29SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
22a59fe29SPing-Ke Shih /* Copyright(c) 2022-2023  Realtek Corporation
32a59fe29SPing-Ke Shih  */
42a59fe29SPing-Ke Shih 
52a59fe29SPing-Ke Shih #include "coex.h"
62a59fe29SPing-Ke Shih #include "debug.h"
72a59fe29SPing-Ke Shih #include "mac.h"
82a59fe29SPing-Ke Shih #include "phy.h"
92a59fe29SPing-Ke Shih #include "reg.h"
102a59fe29SPing-Ke Shih #include "rtw8851b.h"
112a59fe29SPing-Ke Shih #include "rtw8851b_rfk.h"
122a59fe29SPing-Ke Shih #include "rtw8851b_rfk_table.h"
132a59fe29SPing-Ke Shih #include "rtw8851b_table.h"
142a59fe29SPing-Ke Shih 
150194a95cSPing-Ke Shih #define DPK_VER_8851B 0x5
160194a95cSPing-Ke Shih #define DPK_KIP_REG_NUM_8851B 7
170194a95cSPing-Ke Shih #define DPK_RF_REG_NUM_8851B 4
180194a95cSPing-Ke Shih #define DPK_KSET_NUM 4
19a83c6bb2SPing-Ke Shih #define RTW8851B_RXK_GROUP_NR 4
20*76a7c7acSPing-Ke Shih #define RTW8851B_RXK_GROUP_IDX_NR 2
21a83c6bb2SPing-Ke Shih #define RTW8851B_TXK_GROUP_NR 1
22a83c6bb2SPing-Ke Shih #define RTW8851B_IQK_VER 0x2a
23a83c6bb2SPing-Ke Shih #define RTW8851B_IQK_SS 1
24a83c6bb2SPing-Ke Shih #define RTW8851B_LOK_GRAM 10
253f2da9fcSPing-Ke Shih #define RTW8851B_TSSI_PATH_NR 1
263f2da9fcSPing-Ke Shih 
273f2da9fcSPing-Ke Shih #define _TSSI_DE_MASK GENMASK(21, 12)
28a83c6bb2SPing-Ke Shih 
290194a95cSPing-Ke Shih enum dpk_id {
300194a95cSPing-Ke Shih 	LBK_RXIQK	= 0x06,
310194a95cSPing-Ke Shih 	SYNC		= 0x10,
320194a95cSPing-Ke Shih 	MDPK_IDL	= 0x11,
330194a95cSPing-Ke Shih 	MDPK_MPA	= 0x12,
340194a95cSPing-Ke Shih 	GAIN_LOSS	= 0x13,
350194a95cSPing-Ke Shih 	GAIN_CAL	= 0x14,
360194a95cSPing-Ke Shih 	DPK_RXAGC	= 0x15,
370194a95cSPing-Ke Shih 	KIP_PRESET	= 0x16,
380194a95cSPing-Ke Shih 	KIP_RESTORE	= 0x17,
390194a95cSPing-Ke Shih 	DPK_TXAGC	= 0x19,
400194a95cSPing-Ke Shih 	D_KIP_PRESET	= 0x28,
410194a95cSPing-Ke Shih 	D_TXAGC		= 0x29,
420194a95cSPing-Ke Shih 	D_RXAGC		= 0x2a,
430194a95cSPing-Ke Shih 	D_SYNC		= 0x2b,
440194a95cSPing-Ke Shih 	D_GAIN_LOSS	= 0x2c,
450194a95cSPing-Ke Shih 	D_MDPK_IDL	= 0x2d,
460194a95cSPing-Ke Shih 	D_MDPK_LDL	= 0x2e,
470194a95cSPing-Ke Shih 	D_GAIN_NORM	= 0x2f,
480194a95cSPing-Ke Shih 	D_KIP_THERMAL	= 0x30,
490194a95cSPing-Ke Shih 	D_KIP_RESTORE	= 0x31
500194a95cSPing-Ke Shih };
510194a95cSPing-Ke Shih 
520194a95cSPing-Ke Shih enum dpk_agc_step {
530194a95cSPing-Ke Shih 	DPK_AGC_STEP_SYNC_DGAIN,
540194a95cSPing-Ke Shih 	DPK_AGC_STEP_GAIN_LOSS_IDX,
550194a95cSPing-Ke Shih 	DPK_AGC_STEP_GL_GT_CRITERION,
560194a95cSPing-Ke Shih 	DPK_AGC_STEP_GL_LT_CRITERION,
570194a95cSPing-Ke Shih 	DPK_AGC_STEP_SET_TX_GAIN,
580194a95cSPing-Ke Shih };
590194a95cSPing-Ke Shih 
60a83c6bb2SPing-Ke Shih enum rtw8851b_iqk_type {
61a83c6bb2SPing-Ke Shih 	ID_TXAGC = 0x0,
62a83c6bb2SPing-Ke Shih 	ID_FLOK_COARSE = 0x1,
63a83c6bb2SPing-Ke Shih 	ID_FLOK_FINE = 0x2,
64a83c6bb2SPing-Ke Shih 	ID_TXK = 0x3,
65a83c6bb2SPing-Ke Shih 	ID_RXAGC = 0x4,
66a83c6bb2SPing-Ke Shih 	ID_RXK = 0x5,
67a83c6bb2SPing-Ke Shih 	ID_NBTXK = 0x6,
68a83c6bb2SPing-Ke Shih 	ID_NBRXK = 0x7,
69a83c6bb2SPing-Ke Shih 	ID_FLOK_VBUFFER = 0x8,
70a83c6bb2SPing-Ke Shih 	ID_A_FLOK_COARSE = 0x9,
71a83c6bb2SPing-Ke Shih 	ID_G_FLOK_COARSE = 0xa,
72a83c6bb2SPing-Ke Shih 	ID_A_FLOK_FINE = 0xb,
73a83c6bb2SPing-Ke Shih 	ID_G_FLOK_FINE = 0xc,
74a83c6bb2SPing-Ke Shih 	ID_IQK_RESTORE = 0x10,
75a83c6bb2SPing-Ke Shih };
76a83c6bb2SPing-Ke Shih 
77fe8a1682SPing-Ke Shih enum rf_mode {
78fe8a1682SPing-Ke Shih 	RF_SHUT_DOWN = 0x0,
79fe8a1682SPing-Ke Shih 	RF_STANDBY = 0x1,
80fe8a1682SPing-Ke Shih 	RF_TX = 0x2,
81fe8a1682SPing-Ke Shih 	RF_RX = 0x3,
82fe8a1682SPing-Ke Shih 	RF_TXIQK = 0x4,
83fe8a1682SPing-Ke Shih 	RF_DPK = 0x5,
84fe8a1682SPing-Ke Shih 	RF_RXK1 = 0x6,
85fe8a1682SPing-Ke Shih 	RF_RXK2 = 0x7,
86fe8a1682SPing-Ke Shih };
87fe8a1682SPing-Ke Shih 
883f2da9fcSPing-Ke Shih static const u32 _tssi_de_cck_long[RF_PATH_NUM_8851B] = {0x5858};
893f2da9fcSPing-Ke Shih static const u32 _tssi_de_cck_short[RF_PATH_NUM_8851B] = {0x5860};
903f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_20m[RF_PATH_NUM_8851B] = {0x5838};
913f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_40m[RF_PATH_NUM_8851B] = {0x5840};
923f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_80m[RF_PATH_NUM_8851B] = {0x5848};
933f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_80m_80m[RF_PATH_NUM_8851B] = {0x5850};
943f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_5m[RF_PATH_NUM_8851B] = {0x5828};
953f2da9fcSPing-Ke Shih static const u32 _tssi_de_mcs_10m[RF_PATH_NUM_8851B] = {0x5830};
96a83c6bb2SPing-Ke Shih static const u32 g_idxrxgain[RTW8851B_RXK_GROUP_NR] = {0x10e, 0x116, 0x28e, 0x296};
97a83c6bb2SPing-Ke Shih static const u32 g_idxattc2[RTW8851B_RXK_GROUP_NR] = {0x0, 0xf, 0x0, 0xf};
98a83c6bb2SPing-Ke Shih static const u32 g_idxrxagc[RTW8851B_RXK_GROUP_NR] = {0x0, 0x1, 0x2, 0x3};
99*76a7c7acSPing-Ke Shih static const u32 a_idxrxgain[RTW8851B_RXK_GROUP_IDX_NR] = {0x10C, 0x28c};
100*76a7c7acSPing-Ke Shih static const u32 a_idxattc2[RTW8851B_RXK_GROUP_IDX_NR] = {0xf, 0xf};
101*76a7c7acSPing-Ke Shih static const u32 a_idxrxagc[RTW8851B_RXK_GROUP_IDX_NR] = {0x4, 0x6};
102a83c6bb2SPing-Ke Shih static const u32 a_power_range[RTW8851B_TXK_GROUP_NR] = {0x0};
103a83c6bb2SPing-Ke Shih static const u32 a_track_range[RTW8851B_TXK_GROUP_NR] = {0x6};
104a83c6bb2SPing-Ke Shih static const u32 a_gain_bb[RTW8851B_TXK_GROUP_NR] = {0x0a};
105a83c6bb2SPing-Ke Shih static const u32 a_itqt[RTW8851B_TXK_GROUP_NR] = {0x12};
106a83c6bb2SPing-Ke Shih static const u32 g_power_range[RTW8851B_TXK_GROUP_NR] = {0x0};
107a83c6bb2SPing-Ke Shih static const u32 g_track_range[RTW8851B_TXK_GROUP_NR] = {0x6};
108a83c6bb2SPing-Ke Shih static const u32 g_gain_bb[RTW8851B_TXK_GROUP_NR] = {0x10};
109a83c6bb2SPing-Ke Shih static const u32 g_itqt[RTW8851B_TXK_GROUP_NR] = {0x12};
110a83c6bb2SPing-Ke Shih 
111*76a7c7acSPing-Ke Shih static const u32 rtw8851b_backup_bb_regs[] = {0xc0d4, 0xc0d8, 0xc0c4, 0xc0ec, 0xc0e8};
112a83c6bb2SPing-Ke Shih static const u32 rtw8851b_backup_rf_regs[] = {
113a83c6bb2SPing-Ke Shih 	0xef, 0xde, 0x0, 0x1e, 0x2, 0x85, 0x90, 0x5};
114a83c6bb2SPing-Ke Shih 
115a83c6bb2SPing-Ke Shih #define BACKUP_BB_REGS_NR ARRAY_SIZE(rtw8851b_backup_bb_regs)
116a83c6bb2SPing-Ke Shih #define BACKUP_RF_REGS_NR ARRAY_SIZE(rtw8851b_backup_rf_regs)
117a83c6bb2SPing-Ke Shih 
1180194a95cSPing-Ke Shih static const u32 dpk_kip_reg[DPK_KIP_REG_NUM_8851B] = {
1190194a95cSPing-Ke Shih 	0x813c, 0x8124, 0xc0ec, 0xc0e8, 0xc0c4, 0xc0d4, 0xc0d8};
1200194a95cSPing-Ke Shih static const u32 dpk_rf_reg[DPK_RF_REG_NUM_8851B] = {0xde, 0x8f, 0x5, 0x10005};
1210194a95cSPing-Ke Shih 
122b686bc67SPing-Ke Shih static void _set_ch(struct rtw89_dev *rtwdev, u32 val);
123b686bc67SPing-Ke Shih 
_rxk_5ghz_group_from_idx(u8 idx)124*76a7c7acSPing-Ke Shih static u8 _rxk_5ghz_group_from_idx(u8 idx)
125*76a7c7acSPing-Ke Shih {
126*76a7c7acSPing-Ke Shih 	/* There are four RXK groups (RTW8851B_RXK_GROUP_NR), but only group 0
127*76a7c7acSPing-Ke Shih 	 * and 2 are used in 5 GHz band, so reduce elements to 2.
128*76a7c7acSPing-Ke Shih 	 */
129*76a7c7acSPing-Ke Shih 	if (idx < RTW8851B_RXK_GROUP_IDX_NR)
130*76a7c7acSPing-Ke Shih 		return idx * 2;
131*76a7c7acSPing-Ke Shih 
132*76a7c7acSPing-Ke Shih 	return 0;
133*76a7c7acSPing-Ke Shih }
134*76a7c7acSPing-Ke Shih 
_kpath(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)1352a59fe29SPing-Ke Shih static u8 _kpath(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
1362a59fe29SPing-Ke Shih {
1372a59fe29SPing-Ke Shih 	return RF_A;
1382a59fe29SPing-Ke Shih }
1392a59fe29SPing-Ke Shih 
_adc_fifo_rst(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)140a83c6bb2SPing-Ke Shih static void _adc_fifo_rst(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
141a83c6bb2SPing-Ke Shih 			  u8 path)
142a83c6bb2SPing-Ke Shih {
143a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0101);
144a83c6bb2SPing-Ke Shih 	fsleep(10);
145a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x1111);
146a83c6bb2SPing-Ke Shih }
147a83c6bb2SPing-Ke Shih 
_rfk_rf_direct_cntrl(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool is_bybb)1480194a95cSPing-Ke Shih static void _rfk_rf_direct_cntrl(struct rtw89_dev *rtwdev,
1490194a95cSPing-Ke Shih 				 enum rtw89_rf_path path, bool is_bybb)
1500194a95cSPing-Ke Shih {
1510194a95cSPing-Ke Shih 	if (is_bybb)
1520194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
1530194a95cSPing-Ke Shih 	else
1540194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1550194a95cSPing-Ke Shih }
1560194a95cSPing-Ke Shih 
_rfk_drf_direct_cntrl(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool is_bybb)1570194a95cSPing-Ke Shih static void _rfk_drf_direct_cntrl(struct rtw89_dev *rtwdev,
1580194a95cSPing-Ke Shih 				  enum rtw89_rf_path path, bool is_bybb)
1590194a95cSPing-Ke Shih {
1600194a95cSPing-Ke Shih 	if (is_bybb)
1610194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_BBDC, RR_BBDC_SEL, 0x1);
1620194a95cSPing-Ke Shih 	else
1630194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_BBDC, RR_BBDC_SEL, 0x0);
1640194a95cSPing-Ke Shih }
1650194a95cSPing-Ke Shih 
_wait_rx_mode(struct rtw89_dev * rtwdev,u8 kpath)166a83c6bb2SPing-Ke Shih static void _wait_rx_mode(struct rtw89_dev *rtwdev, u8 kpath)
167a83c6bb2SPing-Ke Shih {
168a83c6bb2SPing-Ke Shih 	u32 rf_mode;
169a83c6bb2SPing-Ke Shih 	u8 path;
170a83c6bb2SPing-Ke Shih 	int ret;
171a83c6bb2SPing-Ke Shih 
172a83c6bb2SPing-Ke Shih 	for (path = 0; path < RF_PATH_MAX; path++) {
173a83c6bb2SPing-Ke Shih 		if (!(kpath & BIT(path)))
174a83c6bb2SPing-Ke Shih 			continue;
175a83c6bb2SPing-Ke Shih 
176a83c6bb2SPing-Ke Shih 		ret = read_poll_timeout_atomic(rtw89_read_rf, rf_mode,
177a83c6bb2SPing-Ke Shih 					       rf_mode != 2, 2, 5000, false,
178a83c6bb2SPing-Ke Shih 					       rtwdev, path, 0x00, RR_MOD_MASK);
179a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
180a83c6bb2SPing-Ke Shih 			    "[RFK] Wait S%d to Rx mode!! (ret = %d)\n",
181a83c6bb2SPing-Ke Shih 			    path, ret);
182a83c6bb2SPing-Ke Shih 	}
183a83c6bb2SPing-Ke Shih }
184a83c6bb2SPing-Ke Shih 
_dack_reset(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)18593fbbeedSPing-Ke Shih static void _dack_reset(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
18693fbbeedSPing-Ke Shih {
18793fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_RST, 0x0);
18893fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_RST, 0x1);
18993fbbeedSPing-Ke Shih }
19093fbbeedSPing-Ke Shih 
_drck(struct rtw89_dev * rtwdev)19193fbbeedSPing-Ke Shih static void _drck(struct rtw89_dev *rtwdev)
19293fbbeedSPing-Ke Shih {
19393fbbeedSPing-Ke Shih 	u32 rck_d;
19493fbbeedSPing-Ke Shih 	u32 val;
19593fbbeedSPing-Ke Shih 	int ret;
19693fbbeedSPing-Ke Shih 
19793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]Ddie RCK start!!!\n");
19893fbbeedSPing-Ke Shih 
19993fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x1);
20093fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x1);
20193fbbeedSPing-Ke Shih 
20293fbbeedSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
20393fbbeedSPing-Ke Shih 				       1, 10000, false,
20493fbbeedSPing-Ke Shih 				       rtwdev, R_DRCK_RES, B_DRCK_POL);
20593fbbeedSPing-Ke Shih 	if (ret)
20693fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DRCK timeout\n");
20793fbbeedSPing-Ke Shih 
20893fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x0);
20993fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK_FH, B_DRCK_LAT, 0x1);
21093fbbeedSPing-Ke Shih 	udelay(1);
21193fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK_FH, B_DRCK_LAT, 0x0);
21293fbbeedSPing-Ke Shih 
21393fbbeedSPing-Ke Shih 	rck_d = rtw89_phy_read32_mask(rtwdev, R_DRCK_RES, 0x7c00);
21493fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x0);
21593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_VAL, rck_d);
21693fbbeedSPing-Ke Shih 
21793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0xc0c4 = 0x%x\n",
21893fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DRCK, MASKDWORD));
21993fbbeedSPing-Ke Shih }
22093fbbeedSPing-Ke Shih 
_addck_backup(struct rtw89_dev * rtwdev)22193fbbeedSPing-Ke Shih static void _addck_backup(struct rtw89_dev *rtwdev)
22293fbbeedSPing-Ke Shih {
22393fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
22493fbbeedSPing-Ke Shih 
22593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x0);
22693fbbeedSPing-Ke Shih 
22793fbbeedSPing-Ke Shih 	dack->addck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_A0);
22893fbbeedSPing-Ke Shih 	dack->addck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_A1);
22993fbbeedSPing-Ke Shih }
23093fbbeedSPing-Ke Shih 
_addck_reload(struct rtw89_dev * rtwdev)23193fbbeedSPing-Ke Shih static void _addck_reload(struct rtw89_dev *rtwdev)
23293fbbeedSPing-Ke Shih {
23393fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
23493fbbeedSPing-Ke Shih 
23593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL1, dack->addck_d[0][0]);
23693fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL0, dack->addck_d[0][1]);
23793fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RLS, 0x3);
23893fbbeedSPing-Ke Shih }
23993fbbeedSPing-Ke Shih 
_dack_backup_s0(struct rtw89_dev * rtwdev)24093fbbeedSPing-Ke Shih static void _dack_backup_s0(struct rtw89_dev *rtwdev)
24193fbbeedSPing-Ke Shih {
24293fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
24393fbbeedSPing-Ke Shih 	u8 i;
24493fbbeedSPing-Ke Shih 
24593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x1);
24693fbbeedSPing-Ke Shih 
24793fbbeedSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
24893fbbeedSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_V, i);
24993fbbeedSPing-Ke Shih 		dack->msbk_d[0][0][i] =
25093fbbeedSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0M0);
25193fbbeedSPing-Ke Shih 
25293fbbeedSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DCOF8, B_DCOF8_V, i);
25393fbbeedSPing-Ke Shih 		dack->msbk_d[0][1][i] =
25493fbbeedSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0M1);
25593fbbeedSPing-Ke Shih 	}
25693fbbeedSPing-Ke Shih 
25793fbbeedSPing-Ke Shih 	dack->biask_d[0][0] =
25893fbbeedSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS00, B_DACK_BIAS00);
25993fbbeedSPing-Ke Shih 	dack->biask_d[0][1] =
26093fbbeedSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS01, B_DACK_BIAS01);
26193fbbeedSPing-Ke Shih 	dack->dadck_d[0][0] =
26293fbbeedSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK00, B_DACK_DADCK00) + 24;
26393fbbeedSPing-Ke Shih 	dack->dadck_d[0][1] =
26493fbbeedSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK01, B_DACK_DADCK01) + 24;
26593fbbeedSPing-Ke Shih }
26693fbbeedSPing-Ke Shih 
_dack_reload_by_path(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 index)26793fbbeedSPing-Ke Shih static void _dack_reload_by_path(struct rtw89_dev *rtwdev,
26893fbbeedSPing-Ke Shih 				 enum rtw89_rf_path path, u8 index)
26993fbbeedSPing-Ke Shih {
27093fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
27193fbbeedSPing-Ke Shih 	u32 idx_offset, path_offset;
27293fbbeedSPing-Ke Shih 	u32 offset, reg;
27393fbbeedSPing-Ke Shih 	u32 tmp;
27493fbbeedSPing-Ke Shih 	u8 i;
27593fbbeedSPing-Ke Shih 
27693fbbeedSPing-Ke Shih 	if (index == 0)
27793fbbeedSPing-Ke Shih 		idx_offset = 0;
27893fbbeedSPing-Ke Shih 	else
27993fbbeedSPing-Ke Shih 		idx_offset = 0x14;
28093fbbeedSPing-Ke Shih 
28193fbbeedSPing-Ke Shih 	if (path == RF_PATH_A)
28293fbbeedSPing-Ke Shih 		path_offset = 0;
28393fbbeedSPing-Ke Shih 	else
28493fbbeedSPing-Ke Shih 		path_offset = 0x28;
28593fbbeedSPing-Ke Shih 
28693fbbeedSPing-Ke Shih 	offset = idx_offset + path_offset;
28793fbbeedSPing-Ke Shih 
28893fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_RST, 0x1);
28993fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF9, B_DCOF9_RST, 0x1);
29093fbbeedSPing-Ke Shih 
29193fbbeedSPing-Ke Shih 	/* msbk_d: 15/14/13/12 */
29293fbbeedSPing-Ke Shih 	tmp = 0x0;
29393fbbeedSPing-Ke Shih 	for (i = 0; i < 4; i++)
29493fbbeedSPing-Ke Shih 		tmp |= dack->msbk_d[path][index][i + 12] << (i * 8);
29593fbbeedSPing-Ke Shih 	reg = 0xc200 + offset;
29693fbbeedSPing-Ke Shih 	rtw89_phy_write32(rtwdev, reg, tmp);
29793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
29893fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
29993fbbeedSPing-Ke Shih 
30093fbbeedSPing-Ke Shih 	/* msbk_d: 11/10/9/8 */
30193fbbeedSPing-Ke Shih 	tmp = 0x0;
30293fbbeedSPing-Ke Shih 	for (i = 0; i < 4; i++)
30393fbbeedSPing-Ke Shih 		tmp |= dack->msbk_d[path][index][i + 8] << (i * 8);
30493fbbeedSPing-Ke Shih 	reg = 0xc204 + offset;
30593fbbeedSPing-Ke Shih 	rtw89_phy_write32(rtwdev, reg, tmp);
30693fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
30793fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
30893fbbeedSPing-Ke Shih 
30993fbbeedSPing-Ke Shih 	/* msbk_d: 7/6/5/4 */
31093fbbeedSPing-Ke Shih 	tmp = 0x0;
31193fbbeedSPing-Ke Shih 	for (i = 0; i < 4; i++)
31293fbbeedSPing-Ke Shih 		tmp |= dack->msbk_d[path][index][i + 4] << (i * 8);
31393fbbeedSPing-Ke Shih 	reg = 0xc208 + offset;
31493fbbeedSPing-Ke Shih 	rtw89_phy_write32(rtwdev, reg, tmp);
31593fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
31693fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
31793fbbeedSPing-Ke Shih 
31893fbbeedSPing-Ke Shih 	/* msbk_d: 3/2/1/0 */
31993fbbeedSPing-Ke Shih 	tmp = 0x0;
32093fbbeedSPing-Ke Shih 	for (i = 0; i < 4; i++)
32193fbbeedSPing-Ke Shih 		tmp |= dack->msbk_d[path][index][i] << (i * 8);
32293fbbeedSPing-Ke Shih 	reg = 0xc20c + offset;
32393fbbeedSPing-Ke Shih 	rtw89_phy_write32(rtwdev, reg, tmp);
32493fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
32593fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
32693fbbeedSPing-Ke Shih 
32793fbbeedSPing-Ke Shih 	/* dadak_d/biask_d */
32893fbbeedSPing-Ke Shih 	tmp = 0x0;
32993fbbeedSPing-Ke Shih 	tmp = (dack->biask_d[path][index] << 22) |
33093fbbeedSPing-Ke Shih 	      (dack->dadck_d[path][index] << 14);
33193fbbeedSPing-Ke Shih 	reg = 0xc210 + offset;
33293fbbeedSPing-Ke Shih 	rtw89_phy_write32(rtwdev, reg, tmp);
33393fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", reg,
33493fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, reg, MASKDWORD));
33593fbbeedSPing-Ke Shih 
33693fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DACKN0_CTL + offset, B_DACKN0_EN, 0x1);
33793fbbeedSPing-Ke Shih }
33893fbbeedSPing-Ke Shih 
_dack_reload(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)33993fbbeedSPing-Ke Shih static void _dack_reload(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
34093fbbeedSPing-Ke Shih {
34193fbbeedSPing-Ke Shih 	u8 index;
34293fbbeedSPing-Ke Shih 
34393fbbeedSPing-Ke Shih 	for (index = 0; index < 2; index++)
34493fbbeedSPing-Ke Shih 		_dack_reload_by_path(rtwdev, path, index);
34593fbbeedSPing-Ke Shih }
34693fbbeedSPing-Ke Shih 
_addck(struct rtw89_dev * rtwdev)34793fbbeedSPing-Ke Shih static void _addck(struct rtw89_dev *rtwdev)
34893fbbeedSPing-Ke Shih {
34993fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
35093fbbeedSPing-Ke Shih 	u32 val;
35193fbbeedSPing-Ke Shih 	int ret;
35293fbbeedSPing-Ke Shih 
35393fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x1);
35493fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x1);
35593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x0);
35693fbbeedSPing-Ke Shih 	udelay(1);
35793fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x1);
35893fbbeedSPing-Ke Shih 
35993fbbeedSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
36093fbbeedSPing-Ke Shih 				       1, 10000, false,
36193fbbeedSPing-Ke Shih 				       rtwdev, R_ADDCKR0, BIT(0));
36293fbbeedSPing-Ke Shih 	if (ret) {
36393fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADDCK timeout\n");
36493fbbeedSPing-Ke Shih 		dack->addck_timeout[0] = true;
36593fbbeedSPing-Ke Shih 	}
36693fbbeedSPing-Ke Shih 
36793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]ADDCK ret = %d\n", ret);
36893fbbeedSPing-Ke Shih 
36993fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x0);
37093fbbeedSPing-Ke Shih }
37193fbbeedSPing-Ke Shih 
_new_dadck(struct rtw89_dev * rtwdev)37293fbbeedSPing-Ke Shih static void _new_dadck(struct rtw89_dev *rtwdev)
37393fbbeedSPing-Ke Shih {
37493fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
37593fbbeedSPing-Ke Shih 	u32 i_dc, q_dc, ic, qc;
37693fbbeedSPing-Ke Shih 	u32 val;
37793fbbeedSPing-Ke Shih 	int ret;
37893fbbeedSPing-Ke Shih 
37993fbbeedSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_dadck_setup_defs_tbl);
38093fbbeedSPing-Ke Shih 
38193fbbeedSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
38293fbbeedSPing-Ke Shih 				       1, 10000, false,
38393fbbeedSPing-Ke Shih 				       rtwdev, R_ADDCKR0, BIT(0));
38493fbbeedSPing-Ke Shih 	if (ret) {
38593fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DADCK timeout\n");
38693fbbeedSPing-Ke Shih 		dack->addck_timeout[0] = true;
38793fbbeedSPing-Ke Shih 	}
38893fbbeedSPing-Ke Shih 
38993fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DADCK ret = %d\n", ret);
39093fbbeedSPing-Ke Shih 
39193fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_IQ, 0x0);
39293fbbeedSPing-Ke Shih 	i_dc = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_DC);
39393fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_IQ, 0x1);
39493fbbeedSPing-Ke Shih 	q_dc = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0, B_ADDCKR0_DC);
39593fbbeedSPing-Ke Shih 
39693fbbeedSPing-Ke Shih 	ic = 0x80 - sign_extend32(i_dc, 11) * 6;
39793fbbeedSPing-Ke Shih 	qc = 0x80 - sign_extend32(q_dc, 11) * 6;
39893fbbeedSPing-Ke Shih 
39993fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
40093fbbeedSPing-Ke Shih 		    "[DACK]before DADCK, i_dc=0x%x, q_dc=0x%x\n", i_dc, q_dc);
40193fbbeedSPing-Ke Shih 
40293fbbeedSPing-Ke Shih 	dack->dadck_d[0][0] = ic;
40393fbbeedSPing-Ke Shih 	dack->dadck_d[0][1] = qc;
40493fbbeedSPing-Ke Shih 
40593fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DACKN0_CTL, B_DACKN0_V, dack->dadck_d[0][0]);
40693fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DACKN1_CTL, B_DACKN1_V, dack->dadck_d[0][1]);
40793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
40893fbbeedSPing-Ke Shih 		    "[DACK]after DADCK, 0xc210=0x%x, 0xc224=0x%x\n",
40993fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACKN0_CTL, MASKDWORD),
41093fbbeedSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACKN1_CTL, MASKDWORD));
41193fbbeedSPing-Ke Shih 
41293fbbeedSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_dadck_post_defs_tbl);
41393fbbeedSPing-Ke Shih }
41493fbbeedSPing-Ke Shih 
_dack_s0_poll(struct rtw89_dev * rtwdev)41593fbbeedSPing-Ke Shih static bool _dack_s0_poll(struct rtw89_dev *rtwdev)
41693fbbeedSPing-Ke Shih {
41793fbbeedSPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_DACK_S0P0, B_DACK_S0P0_OK) == 0 ||
41893fbbeedSPing-Ke Shih 	    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P1, B_DACK_S0P1_OK) == 0 ||
41993fbbeedSPing-Ke Shih 	    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0P2_OK) == 0 ||
42093fbbeedSPing-Ke Shih 	    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0P3_OK) == 0)
42193fbbeedSPing-Ke Shih 		return false;
42293fbbeedSPing-Ke Shih 
42393fbbeedSPing-Ke Shih 	return true;
42493fbbeedSPing-Ke Shih }
42593fbbeedSPing-Ke Shih 
_dack_s0(struct rtw89_dev * rtwdev)42693fbbeedSPing-Ke Shih static void _dack_s0(struct rtw89_dev *rtwdev)
42793fbbeedSPing-Ke Shih {
42893fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
42993fbbeedSPing-Ke Shih 	bool done;
43093fbbeedSPing-Ke Shih 	int ret;
43193fbbeedSPing-Ke Shih 
43293fbbeedSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_dack_s0_1_defs_tbl);
43393fbbeedSPing-Ke Shih 	_dack_reset(rtwdev, RF_PATH_A);
43493fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x1);
43593fbbeedSPing-Ke Shih 
43693fbbeedSPing-Ke Shih 	ret = read_poll_timeout_atomic(_dack_s0_poll, done, done,
43793fbbeedSPing-Ke Shih 				       1, 10000, false, rtwdev);
43893fbbeedSPing-Ke Shih 	if (ret) {
43993fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DACK timeout\n");
44093fbbeedSPing-Ke Shih 		dack->msbk_timeout[0] = true;
44193fbbeedSPing-Ke Shih 	}
44293fbbeedSPing-Ke Shih 
44393fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK ret = %d\n", ret);
44493fbbeedSPing-Ke Shih 
44593fbbeedSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_dack_s0_2_defs_tbl);
44693fbbeedSPing-Ke Shih 
44793fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S0 DADCK\n");
44893fbbeedSPing-Ke Shih 
44993fbbeedSPing-Ke Shih 	_dack_backup_s0(rtwdev);
45093fbbeedSPing-Ke Shih 	_dack_reload(rtwdev, RF_PATH_A);
45193fbbeedSPing-Ke Shih 
45293fbbeedSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x0);
45393fbbeedSPing-Ke Shih }
45493fbbeedSPing-Ke Shih 
_dack(struct rtw89_dev * rtwdev)45593fbbeedSPing-Ke Shih static void _dack(struct rtw89_dev *rtwdev)
45693fbbeedSPing-Ke Shih {
45793fbbeedSPing-Ke Shih 	_dack_s0(rtwdev);
45893fbbeedSPing-Ke Shih }
45993fbbeedSPing-Ke Shih 
_dack_dump(struct rtw89_dev * rtwdev)46093fbbeedSPing-Ke Shih static void _dack_dump(struct rtw89_dev *rtwdev)
46193fbbeedSPing-Ke Shih {
46293fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
46393fbbeedSPing-Ke Shih 	u8 i;
46493fbbeedSPing-Ke Shih 	u8 t;
46593fbbeedSPing-Ke Shih 
46693fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADC_DCK ic = 0x%x, qc = 0x%x\n",
46793fbbeedSPing-Ke Shih 		    dack->addck_d[0][0], dack->addck_d[0][1]);
46893fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DAC_DCK ic = 0x%x, qc = 0x%x\n",
46993fbbeedSPing-Ke Shih 		    dack->dadck_d[0][0], dack->dadck_d[0][1]);
47093fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 biask ic = 0x%x, qc = 0x%x\n",
47193fbbeedSPing-Ke Shih 		    dack->biask_d[0][0], dack->biask_d[0][1]);
47293fbbeedSPing-Ke Shih 
47393fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK ic:\n");
47493fbbeedSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
47593fbbeedSPing-Ke Shih 		t = dack->msbk_d[0][0][i];
47693fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
47793fbbeedSPing-Ke Shih 	}
47893fbbeedSPing-Ke Shih 
47993fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK qc:\n");
48093fbbeedSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
48193fbbeedSPing-Ke Shih 		t = dack->msbk_d[0][1][i];
48293fbbeedSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
48393fbbeedSPing-Ke Shih 	}
48493fbbeedSPing-Ke Shih }
48593fbbeedSPing-Ke Shih 
_dack_manual_off(struct rtw89_dev * rtwdev)48693fbbeedSPing-Ke Shih static void _dack_manual_off(struct rtw89_dev *rtwdev)
48793fbbeedSPing-Ke Shih {
48893fbbeedSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_dack_manual_off_defs_tbl);
48993fbbeedSPing-Ke Shih }
49093fbbeedSPing-Ke Shih 
_dac_cal(struct rtw89_dev * rtwdev,bool force)49193fbbeedSPing-Ke Shih static void _dac_cal(struct rtw89_dev *rtwdev, bool force)
49293fbbeedSPing-Ke Shih {
49393fbbeedSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
49493fbbeedSPing-Ke Shih 	u32 rf0_0;
49593fbbeedSPing-Ke Shih 
49693fbbeedSPing-Ke Shih 	dack->dack_done = false;
49793fbbeedSPing-Ke Shih 
49893fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK 0x2\n");
49993fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK start!!!\n");
50093fbbeedSPing-Ke Shih 	rf0_0 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK);
50193fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]RF0=0x%x\n", rf0_0);
50293fbbeedSPing-Ke Shih 
50393fbbeedSPing-Ke Shih 	_drck(rtwdev);
50493fbbeedSPing-Ke Shih 	_dack_manual_off(rtwdev);
50593fbbeedSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x337e1);
50693fbbeedSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x0);
50793fbbeedSPing-Ke Shih 
50893fbbeedSPing-Ke Shih 	_addck(rtwdev);
50993fbbeedSPing-Ke Shih 	_addck_backup(rtwdev);
51093fbbeedSPing-Ke Shih 	_addck_reload(rtwdev);
51193fbbeedSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x40001);
51293fbbeedSPing-Ke Shih 
51393fbbeedSPing-Ke Shih 	_dack(rtwdev);
51493fbbeedSPing-Ke Shih 	_new_dadck(rtwdev);
51593fbbeedSPing-Ke Shih 	_dack_dump(rtwdev);
51693fbbeedSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x1);
51793fbbeedSPing-Ke Shih 
51893fbbeedSPing-Ke Shih 	dack->dack_done = true;
51993fbbeedSPing-Ke Shih 	dack->dack_cnt++;
52093fbbeedSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK finish!!!\n");
52193fbbeedSPing-Ke Shih }
52293fbbeedSPing-Ke Shih 
_rx_dck_info(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,bool is_afe)523fe8a1682SPing-Ke Shih static void _rx_dck_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
524fe8a1682SPing-Ke Shih 			 enum rtw89_rf_path path, bool is_afe)
525fe8a1682SPing-Ke Shih {
526fe8a1682SPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
527fe8a1682SPing-Ke Shih 
528fe8a1682SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
529fe8a1682SPing-Ke Shih 		    "[RX_DCK] ==== S%d RX DCK (%s / CH%d / %s / by %s)====\n", path,
530fe8a1682SPing-Ke Shih 		    chan->band_type == RTW89_BAND_2G ? "2G" :
531fe8a1682SPing-Ke Shih 		    chan->band_type == RTW89_BAND_5G ? "5G" : "6G",
532fe8a1682SPing-Ke Shih 		    chan->channel,
533fe8a1682SPing-Ke Shih 		    chan->band_width == RTW89_CHANNEL_WIDTH_20 ? "20M" :
534fe8a1682SPing-Ke Shih 		    chan->band_width == RTW89_CHANNEL_WIDTH_40 ? "40M" : "80M",
535fe8a1682SPing-Ke Shih 		    is_afe ? "AFE" : "RFC");
536fe8a1682SPing-Ke Shih }
537fe8a1682SPing-Ke Shih 
_rxbb_ofst_swap(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 rf_mode)538fe8a1682SPing-Ke Shih static void _rxbb_ofst_swap(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 rf_mode)
539fe8a1682SPing-Ke Shih {
540fe8a1682SPing-Ke Shih 	u32 val, val_i, val_q;
541fe8a1682SPing-Ke Shih 
542fe8a1682SPing-Ke Shih 	val_i = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_S1);
543fe8a1682SPing-Ke Shih 	val_q = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_S1);
544fe8a1682SPing-Ke Shih 
545fe8a1682SPing-Ke Shih 	val = val_q << 4 | val_i;
546fe8a1682SPing-Ke Shih 
547fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_DIS, 0x1);
548fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWA, RFREG_MASK, rf_mode);
549fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, val);
550fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_DIS, 0x0);
551fe8a1682SPing-Ke Shih 
552fe8a1682SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
553fe8a1682SPing-Ke Shih 		    "[RX_DCK] val_i = 0x%x, val_q = 0x%x, 0x3F = 0x%x\n",
554fe8a1682SPing-Ke Shih 		    val_i, val_q, val);
555fe8a1682SPing-Ke Shih }
556fe8a1682SPing-Ke Shih 
_set_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 rf_mode)557fe8a1682SPing-Ke Shih static void _set_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 rf_mode)
558fe8a1682SPing-Ke Shih {
559fe8a1682SPing-Ke Shih 	u32 val;
560fe8a1682SPing-Ke Shih 	int ret;
561fe8a1682SPing-Ke Shih 
562fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
563fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x1);
564fe8a1682SPing-Ke Shih 
565fe8a1682SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val,
566fe8a1682SPing-Ke Shih 				       2, 2000, false,
567fe8a1682SPing-Ke Shih 				       rtwdev, path, RR_DCK, BIT(8));
568fe8a1682SPing-Ke Shih 
569fe8a1682SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
570fe8a1682SPing-Ke Shih 
571fe8a1682SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] S%d RXDCK finish (ret = %d)\n",
572fe8a1682SPing-Ke Shih 		    path, ret);
573fe8a1682SPing-Ke Shih 
574fe8a1682SPing-Ke Shih 	_rxbb_ofst_swap(rtwdev, path, rf_mode);
575fe8a1682SPing-Ke Shih }
576fe8a1682SPing-Ke Shih 
_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool is_afe)577fe8a1682SPing-Ke Shih static void _rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool is_afe)
578fe8a1682SPing-Ke Shih {
579fe8a1682SPing-Ke Shih 	u32 rf_reg5;
580fe8a1682SPing-Ke Shih 	u8 path;
581fe8a1682SPing-Ke Shih 
582fe8a1682SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
583fe8a1682SPing-Ke Shih 		    "[RX_DCK] ****** RXDCK Start (Ver: 0x%x, Cv: %d) ******\n",
584fe8a1682SPing-Ke Shih 		    0x2, rtwdev->hal.cv);
585fe8a1682SPing-Ke Shih 
586fe8a1682SPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
587fe8a1682SPing-Ke Shih 		_rx_dck_info(rtwdev, phy, path, is_afe);
588fe8a1682SPing-Ke Shih 
589fe8a1682SPing-Ke Shih 		rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
590fe8a1682SPing-Ke Shih 
591fe8a1682SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
592fe8a1682SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
593fe8a1682SPing-Ke Shih 					       R_P0_TSSI_TRK + (path << 13),
594fe8a1682SPing-Ke Shih 					       B_P0_TSSI_TRK_EN, 0x1);
595fe8a1682SPing-Ke Shih 
596fe8a1682SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
597fe8a1682SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
598fe8a1682SPing-Ke Shih 		_set_rx_dck(rtwdev, path, RF_RX);
599fe8a1682SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
600fe8a1682SPing-Ke Shih 
601fe8a1682SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
602fe8a1682SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
603fe8a1682SPing-Ke Shih 					       R_P0_TSSI_TRK + (path << 13),
604fe8a1682SPing-Ke Shih 					       B_P0_TSSI_TRK_EN, 0x0);
605fe8a1682SPing-Ke Shih 	}
606fe8a1682SPing-Ke Shih }
607fe8a1682SPing-Ke Shih 
_iqk_sram(struct rtw89_dev * rtwdev,u8 path)608a83c6bb2SPing-Ke Shih static void _iqk_sram(struct rtw89_dev *rtwdev, u8 path)
609a83c6bb2SPing-Ke Shih {
610a83c6bb2SPing-Ke Shih 	u32 i;
611a83c6bb2SPing-Ke Shih 
612a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
613a83c6bb2SPing-Ke Shih 
614a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKDWORD, 0x00020000);
615a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, MASKDWORD, 0x80000000);
616a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX2, MASKDWORD, 0x00000080);
617a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00010000);
618a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x009);
619a83c6bb2SPing-Ke Shih 
620a83c6bb2SPing-Ke Shih 	for (i = 0; i <= 0x9f; i++) {
621a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD,
622a83c6bb2SPing-Ke Shih 				       0x00010000 + i);
623a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]0x%x\n",
624a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI));
625a83c6bb2SPing-Ke Shih 	}
626a83c6bb2SPing-Ke Shih 
627a83c6bb2SPing-Ke Shih 	for (i = 0; i <= 0x9f; i++) {
628a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD,
629a83c6bb2SPing-Ke Shih 				       0x00010000 + i);
630a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]0x%x\n",
631a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ));
632a83c6bb2SPing-Ke Shih 	}
633a83c6bb2SPing-Ke Shih 
634a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX2, MASKDWORD, 0x00000000);
635a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00000000);
636a83c6bb2SPing-Ke Shih }
637a83c6bb2SPing-Ke Shih 
_iqk_rxk_setting(struct rtw89_dev * rtwdev,u8 path)638a83c6bb2SPing-Ke Shih static void _iqk_rxk_setting(struct rtw89_dev *rtwdev, u8 path)
639a83c6bb2SPing-Ke Shih {
640a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
641a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_POW, 0x0);
642a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_POW, 0x1);
643a83c6bb2SPing-Ke Shih }
644a83c6bb2SPing-Ke Shih 
_iqk_check_cal(struct rtw89_dev * rtwdev,u8 path)645a83c6bb2SPing-Ke Shih static bool _iqk_check_cal(struct rtw89_dev *rtwdev, u8 path)
646a83c6bb2SPing-Ke Shih {
647a83c6bb2SPing-Ke Shih 	bool fail1 = false, fail2 = false;
648a83c6bb2SPing-Ke Shih 	u32 val;
649a83c6bb2SPing-Ke Shih 	int ret;
650a83c6bb2SPing-Ke Shih 
651a83c6bb2SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
652a83c6bb2SPing-Ke Shih 				       10, 8200, false,
653a83c6bb2SPing-Ke Shih 				       rtwdev, 0xbff8, MASKBYTE0);
654a83c6bb2SPing-Ke Shih 	if (ret) {
655a83c6bb2SPing-Ke Shih 		fail1 = true;
656a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
657a83c6bb2SPing-Ke Shih 			    "[IQK]NCTL1 IQK timeout!!!\n");
658a83c6bb2SPing-Ke Shih 	}
659a83c6bb2SPing-Ke Shih 
660a83c6bb2SPing-Ke Shih 	fsleep(10);
661a83c6bb2SPing-Ke Shih 
662a83c6bb2SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x8000,
663a83c6bb2SPing-Ke Shih 				       10, 200, false,
664a83c6bb2SPing-Ke Shih 				       rtwdev, R_RPT_COM, B_RPT_COM_RDY);
665a83c6bb2SPing-Ke Shih 	if (ret) {
666a83c6bb2SPing-Ke Shih 		fail2 = true;
667a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
668a83c6bb2SPing-Ke Shih 			    "[IQK]NCTL2 IQK timeout!!!\n");
669a83c6bb2SPing-Ke Shih 	}
670a83c6bb2SPing-Ke Shih 
671a83c6bb2SPing-Ke Shih 	fsleep(10);
672a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, MASKBYTE0, 0x0);
673a83c6bb2SPing-Ke Shih 
674a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
675a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, ret = %d, notready = %x fail=%d,%d\n",
676a83c6bb2SPing-Ke Shih 		    path, ret, fail1 || fail2, fail1, fail2);
677a83c6bb2SPing-Ke Shih 
678a83c6bb2SPing-Ke Shih 	return fail1 || fail2;
679a83c6bb2SPing-Ke Shih }
680a83c6bb2SPing-Ke Shih 
_iqk_one_shot(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path,u8 ktype)681a83c6bb2SPing-Ke Shih static bool _iqk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
682a83c6bb2SPing-Ke Shih 			  u8 path, u8 ktype)
683a83c6bb2SPing-Ke Shih {
684a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
685a83c6bb2SPing-Ke Shih 	bool notready;
686a83c6bb2SPing-Ke Shih 	u32 iqk_cmd;
687a83c6bb2SPing-Ke Shih 
688a83c6bb2SPing-Ke Shih 	switch (ktype) {
689a83c6bb2SPing-Ke Shih 	case ID_A_FLOK_COARSE:
690a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
691a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_A_FLOK_COARSE ============\n", path);
692a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
693a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x108 | (1 << (4 + path));
694a83c6bb2SPing-Ke Shih 		break;
695a83c6bb2SPing-Ke Shih 	case ID_G_FLOK_COARSE:
696a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
697a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_G_FLOK_COARSE ============\n", path);
698a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
699a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x108 | (1 << (4 + path));
700a83c6bb2SPing-Ke Shih 		break;
701a83c6bb2SPing-Ke Shih 	case ID_A_FLOK_FINE:
702a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
703a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_A_FLOK_FINE ============\n", path);
704a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
705a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x308 | (1 << (4 + path));
706a83c6bb2SPing-Ke Shih 		break;
707a83c6bb2SPing-Ke Shih 	case ID_G_FLOK_FINE:
708a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
709a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_G_FLOK_FINE ============\n", path);
710a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
711a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x308 | (1 << (4 + path));
712a83c6bb2SPing-Ke Shih 		break;
713a83c6bb2SPing-Ke Shih 	case ID_TXK:
714a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
715a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_TXK ============\n", path);
716a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
717a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (path + 4)) |
718a83c6bb2SPing-Ke Shih 			  (((0x8 + iqk_info->iqk_bw[path]) & 0xf) << 8);
719a83c6bb2SPing-Ke Shih 		break;
720a83c6bb2SPing-Ke Shih 	case ID_RXAGC:
721a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
722a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_RXAGC ============\n", path);
723a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
724a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x708 | (1 << (4 + path)) | (path << 1);
725a83c6bb2SPing-Ke Shih 		break;
726a83c6bb2SPing-Ke Shih 	case ID_RXK:
727a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
728a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_RXK ============\n", path);
729a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
730a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (path + 4)) |
731a83c6bb2SPing-Ke Shih 			  (((0xc + iqk_info->iqk_bw[path]) & 0xf) << 8);
732a83c6bb2SPing-Ke Shih 		break;
733a83c6bb2SPing-Ke Shih 	case ID_NBTXK:
734a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
735a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_NBTXK ============\n", path);
736a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
737a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT,
738a83c6bb2SPing-Ke Shih 				       0x00b);
739a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x408 | (1 << (4 + path));
740a83c6bb2SPing-Ke Shih 		break;
741a83c6bb2SPing-Ke Shih 	case ID_NBRXK:
742a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
743a83c6bb2SPing-Ke Shih 			    "[IQK]============ S%d ID_NBRXK ============\n", path);
744a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
745a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT,
746a83c6bb2SPing-Ke Shih 				       0x011);
747a83c6bb2SPing-Ke Shih 		iqk_cmd = 0x608 | (1 << (4 + path));
748a83c6bb2SPing-Ke Shih 		break;
749a83c6bb2SPing-Ke Shih 	default:
750a83c6bb2SPing-Ke Shih 		return false;
751a83c6bb2SPing-Ke Shih 	}
752a83c6bb2SPing-Ke Shih 
753a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, iqk_cmd + 1);
754a83c6bb2SPing-Ke Shih 	notready = _iqk_check_cal(rtwdev, path);
755a83c6bb2SPing-Ke Shih 	if (iqk_info->iqk_sram_en &&
756a83c6bb2SPing-Ke Shih 	    (ktype == ID_NBRXK || ktype == ID_RXK))
757a83c6bb2SPing-Ke Shih 		_iqk_sram(rtwdev, path);
758a83c6bb2SPing-Ke Shih 
759a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
760a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
761a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, ktype= %x, id = %x, notready = %x\n",
762a83c6bb2SPing-Ke Shih 		    path, ktype, iqk_cmd + 1, notready);
763a83c6bb2SPing-Ke Shih 
764a83c6bb2SPing-Ke Shih 	return notready;
765a83c6bb2SPing-Ke Shih }
766a83c6bb2SPing-Ke Shih 
_rxk_2g_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)767a83c6bb2SPing-Ke Shih static bool _rxk_2g_group_sel(struct rtw89_dev *rtwdev,
768a83c6bb2SPing-Ke Shih 			      enum rtw89_phy_idx phy_idx, u8 path)
769a83c6bb2SPing-Ke Shih {
770a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
771a83c6bb2SPing-Ke Shih 	bool kfail = false;
772a83c6bb2SPing-Ke Shih 	bool notready;
773a83c6bb2SPing-Ke Shih 	u32 rf_0;
774a83c6bb2SPing-Ke Shih 	u8 gp;
775a83c6bb2SPing-Ke Shih 
776a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
777a83c6bb2SPing-Ke Shih 
778a83c6bb2SPing-Ke Shih 	for (gp = 0; gp < RTW8851B_RXK_GROUP_NR; gp++) {
779a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
780a83c6bb2SPing-Ke Shih 
781a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RGM, g_idxrxgain[gp]);
782a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_C2, g_idxattc2[gp]);
783a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
784a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
785a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
786a83c6bb2SPing-Ke Shih 
787a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
788a83c6bb2SPing-Ke Shih 		fsleep(10);
789a83c6bb2SPing-Ke Shih 		rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
790a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
791a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, g_idxrxagc[gp]);
792a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
793a83c6bb2SPing-Ke Shih 
794a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
795a83c6bb2SPing-Ke Shih 
796a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
797a83c6bb2SPing-Ke Shih 			    "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
798a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
799a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
800a83c6bb2SPing-Ke Shih 
801a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
802a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
803a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
804a83c6bb2SPing-Ke Shih 		iqk_info->nb_rxcfir[path] =
805a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
806a83c6bb2SPing-Ke Shih 
807a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
808a83c6bb2SPing-Ke Shih 
809a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
810a83c6bb2SPing-Ke Shih 			    "[IQK]S%x, WBRXK 0x8008 = 0x%x\n", path,
811a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
812a83c6bb2SPing-Ke Shih 	}
813a83c6bb2SPing-Ke Shih 
814a83c6bb2SPing-Ke Shih 	if (!notready)
815a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
816a83c6bb2SPing-Ke Shih 
817a83c6bb2SPing-Ke Shih 	if (kfail)
818a83c6bb2SPing-Ke Shih 		_iqk_sram(rtwdev, path);
819a83c6bb2SPing-Ke Shih 
820a83c6bb2SPing-Ke Shih 	if (kfail) {
821a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
822a83c6bb2SPing-Ke Shih 				       MASKDWORD, iqk_info->nb_rxcfir[path] | 0x2);
823a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = false;
824a83c6bb2SPing-Ke Shih 	} else {
825a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
826a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000000);
827a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = true;
828a83c6bb2SPing-Ke Shih 	}
829a83c6bb2SPing-Ke Shih 
830a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
831a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
832a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_rxcfir[path]);
833a83c6bb2SPing-Ke Shih 	return kfail;
834a83c6bb2SPing-Ke Shih }
835a83c6bb2SPing-Ke Shih 
_rxk_5g_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)836a83c6bb2SPing-Ke Shih static bool _rxk_5g_group_sel(struct rtw89_dev *rtwdev,
837a83c6bb2SPing-Ke Shih 			      enum rtw89_phy_idx phy_idx, u8 path)
838a83c6bb2SPing-Ke Shih {
839a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
840a83c6bb2SPing-Ke Shih 	bool kfail = false;
841a83c6bb2SPing-Ke Shih 	bool notready;
842a83c6bb2SPing-Ke Shih 	u32 rf_0;
843*76a7c7acSPing-Ke Shih 	u8 idx;
844a83c6bb2SPing-Ke Shih 	u8 gp;
845a83c6bb2SPing-Ke Shih 
846a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
847a83c6bb2SPing-Ke Shih 
848*76a7c7acSPing-Ke Shih 	for (idx = 0; idx < RTW8851B_RXK_GROUP_IDX_NR; idx++) {
849*76a7c7acSPing-Ke Shih 		gp = _rxk_5ghz_group_from_idx(idx);
850*76a7c7acSPing-Ke Shih 
851a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
852a83c6bb2SPing-Ke Shih 
853*76a7c7acSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_RGM, a_idxrxgain[idx]);
854*76a7c7acSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_RXA2, RR_RXA2_ATT, a_idxattc2[idx]);
855a83c6bb2SPing-Ke Shih 
856a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
857a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
858a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
859a83c6bb2SPing-Ke Shih 
860a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
861a83c6bb2SPing-Ke Shih 		fsleep(100);
862a83c6bb2SPing-Ke Shih 		rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
863a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
864*76a7c7acSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, a_idxrxagc[idx]);
865a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
866a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
867a83c6bb2SPing-Ke Shih 
868a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
869a83c6bb2SPing-Ke Shih 			    "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
870a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
871a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB));
872a83c6bb2SPing-Ke Shih 
873a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
874a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
875a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
876a83c6bb2SPing-Ke Shih 		iqk_info->nb_rxcfir[path] =
877a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
878a83c6bb2SPing-Ke Shih 
879a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
880a83c6bb2SPing-Ke Shih 			    "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
881a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
882a83c6bb2SPing-Ke Shih 
883a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
884a83c6bb2SPing-Ke Shih 
885a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
886a83c6bb2SPing-Ke Shih 			    "[IQK]S%x, WBRXK 0x8008 = 0x%x\n", path,
887a83c6bb2SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
888a83c6bb2SPing-Ke Shih 	}
889a83c6bb2SPing-Ke Shih 
890a83c6bb2SPing-Ke Shih 	if (!notready)
891a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
892a83c6bb2SPing-Ke Shih 
893a83c6bb2SPing-Ke Shih 	if (kfail)
894a83c6bb2SPing-Ke Shih 		_iqk_sram(rtwdev, path);
895a83c6bb2SPing-Ke Shih 
896a83c6bb2SPing-Ke Shih 	if (kfail) {
897a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
898a83c6bb2SPing-Ke Shih 				       iqk_info->nb_rxcfir[path] | 0x2);
899a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = false;
900a83c6bb2SPing-Ke Shih 	} else {
901a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
902a83c6bb2SPing-Ke Shih 				       0x40000000);
903a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = true;
904a83c6bb2SPing-Ke Shih 	}
905a83c6bb2SPing-Ke Shih 
906a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
907a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
908a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_rxcfir[path]);
909a83c6bb2SPing-Ke Shih 	return kfail;
910a83c6bb2SPing-Ke Shih }
911a83c6bb2SPing-Ke Shih 
_iqk_5g_nbrxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)912a83c6bb2SPing-Ke Shih static bool _iqk_5g_nbrxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
913a83c6bb2SPing-Ke Shih 			  u8 path)
914a83c6bb2SPing-Ke Shih {
915a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
916a83c6bb2SPing-Ke Shih 	bool kfail = false;
917a83c6bb2SPing-Ke Shih 	bool notready;
918*76a7c7acSPing-Ke Shih 	u8 idx = 0x1;
919a83c6bb2SPing-Ke Shih 	u32 rf_0;
920*76a7c7acSPing-Ke Shih 	u8 gp;
921*76a7c7acSPing-Ke Shih 
922*76a7c7acSPing-Ke Shih 	gp = _rxk_5ghz_group_from_idx(idx);
923a83c6bb2SPing-Ke Shih 
924a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
925a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
926a83c6bb2SPing-Ke Shih 
927*76a7c7acSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_RGM, a_idxrxgain[idx]);
928*76a7c7acSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RXA2, RR_RXA2_ATT, a_idxattc2[idx]);
929a83c6bb2SPing-Ke Shih 
930a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
931a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
932a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
933a83c6bb2SPing-Ke Shih 
934a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
935a83c6bb2SPing-Ke Shih 	fsleep(100);
936a83c6bb2SPing-Ke Shih 	rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
937a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
938*76a7c7acSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, a_idxrxagc[idx]);
939a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
940a83c6bb2SPing-Ke Shih 	notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
941a83c6bb2SPing-Ke Shih 
942a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
943a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n", path,
944a83c6bb2SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
945a83c6bb2SPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
946a83c6bb2SPing-Ke Shih 
947a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
948a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
949a83c6bb2SPing-Ke Shih 	notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
950a83c6bb2SPing-Ke Shih 	iqk_info->nb_rxcfir[path] =
951a83c6bb2SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
952a83c6bb2SPing-Ke Shih 
953a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
954a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
955a83c6bb2SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
956a83c6bb2SPing-Ke Shih 
957a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, WBRXK 0x8008 = 0x%x\n",
958a83c6bb2SPing-Ke Shih 		    path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
959a83c6bb2SPing-Ke Shih 
960a83c6bb2SPing-Ke Shih 	if (!notready)
961a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
962a83c6bb2SPing-Ke Shih 
963a83c6bb2SPing-Ke Shih 	if (kfail) {
964a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
965a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000002);
966a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
967a83c6bb2SPing-Ke Shih 	} else {
968a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
969a83c6bb2SPing-Ke Shih 	}
970a83c6bb2SPing-Ke Shih 
971a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
972a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
973a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_rxcfir[path]);
974a83c6bb2SPing-Ke Shih 
975a83c6bb2SPing-Ke Shih 	return kfail;
976a83c6bb2SPing-Ke Shih }
977a83c6bb2SPing-Ke Shih 
_iqk_2g_nbrxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)978a83c6bb2SPing-Ke Shih static bool _iqk_2g_nbrxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
979a83c6bb2SPing-Ke Shih 			  u8 path)
980a83c6bb2SPing-Ke Shih {
981a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
982a83c6bb2SPing-Ke Shih 	bool kfail = false;
983a83c6bb2SPing-Ke Shih 	bool notready;
984a83c6bb2SPing-Ke Shih 	u8 gp = 0x3;
985a83c6bb2SPing-Ke Shih 	u32 rf_0;
986a83c6bb2SPing-Ke Shih 
987a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
988a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, gp = %x\n", path, gp);
989a83c6bb2SPing-Ke Shih 
990a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RGM, g_idxrxgain[gp]);
991a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_C2, g_idxattc2[gp]);
992a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
993a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x0);
994a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP_V1, gp);
995a83c6bb2SPing-Ke Shih 
996a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RFREG_MASK, 0x80013);
997a83c6bb2SPing-Ke Shih 	fsleep(10);
998a83c6bb2SPing-Ke Shih 	rf_0 = rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK);
999a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF2, B_IQK_DIF2_RXPI, rf_0);
1000a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_RXA, B_IQK_RXAGC, g_idxrxagc[gp]);
1001a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x11);
1002a83c6bb2SPing-Ke Shih 	notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXAGC);
1003a83c6bb2SPing-Ke Shih 
1004a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1005a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, RXAGC 0x8008 = 0x%x, rxbb = %x\n",
1006a83c6bb2SPing-Ke Shih 		    path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD),
1007a83c6bb2SPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_MOD, 0x003e0));
1008a83c6bb2SPing-Ke Shih 
1009a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXKPLL, RR_RXKPLL_OFF, 0x13);
1010a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_RXT, 0x011);
1011a83c6bb2SPing-Ke Shih 	notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBRXK);
1012a83c6bb2SPing-Ke Shih 	iqk_info->nb_rxcfir[path] =
1013a83c6bb2SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_RXIQC, MASKDWORD) | 0x2;
1014a83c6bb2SPing-Ke Shih 
1015a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1016a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, NBRXK 0x8008 = 0x%x\n", path,
1017a83c6bb2SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
1018a83c6bb2SPing-Ke Shih 
1019a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, WBRXK 0x8008 = 0x%x\n",
1020a83c6bb2SPing-Ke Shih 		    path, rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD));
1021a83c6bb2SPing-Ke Shih 
1022a83c6bb2SPing-Ke Shih 	if (!notready)
1023a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
1024a83c6bb2SPing-Ke Shih 
1025a83c6bb2SPing-Ke Shih 	if (kfail) {
1026a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8),
1027a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000002);
1028a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1029a83c6bb2SPing-Ke Shih 	} else {
1030a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1031a83c6bb2SPing-Ke Shih 	}
1032a83c6bb2SPing-Ke Shih 
1033a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1034a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x3c = 0x%x\n", path, kfail,
1035a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_rxcfir[path]);
1036a83c6bb2SPing-Ke Shih 	return kfail;
1037a83c6bb2SPing-Ke Shih }
1038a83c6bb2SPing-Ke Shih 
_iqk_rxclk_setting(struct rtw89_dev * rtwdev,u8 path)1039a83c6bb2SPing-Ke Shih static void _iqk_rxclk_setting(struct rtw89_dev *rtwdev, u8 path)
1040a83c6bb2SPing-Ke Shih {
1041a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1042a83c6bb2SPing-Ke Shih 
1043a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
1044a83c6bb2SPing-Ke Shih 
1045a83c6bb2SPing-Ke Shih 	if (iqk_info->iqk_bw[path] == RTW89_CHANNEL_WIDTH_80)
1046a83c6bb2SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_rxclk_80_defs_tbl);
1047a83c6bb2SPing-Ke Shih 	else
1048a83c6bb2SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_rxclk_others_defs_tbl);
1049a83c6bb2SPing-Ke Shih }
1050a83c6bb2SPing-Ke Shih 
_txk_5g_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1051a83c6bb2SPing-Ke Shih static bool _txk_5g_group_sel(struct rtw89_dev *rtwdev,
1052a83c6bb2SPing-Ke Shih 			      enum rtw89_phy_idx phy_idx, u8 path)
1053a83c6bb2SPing-Ke Shih {
1054a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1055a83c6bb2SPing-Ke Shih 	bool kfail = false;
1056a83c6bb2SPing-Ke Shih 	bool notready;
1057a83c6bb2SPing-Ke Shih 	u8 gp;
1058a83c6bb2SPing-Ke Shih 
1059a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1060a83c6bb2SPing-Ke Shih 
1061a83c6bb2SPing-Ke Shih 	for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
1062a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, a_power_range[gp]);
1063a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, a_track_range[gp]);
1064a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, a_gain_bb[gp]);
1065a83c6bb2SPing-Ke Shih 
1066a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
1067a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
1068a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
1069a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
1070a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1071a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, a_itqt[gp]);
1072a83c6bb2SPing-Ke Shih 
1073a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
1074a83c6bb2SPing-Ke Shih 		iqk_info->nb_txcfir[path] =
1075a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD)  | 0x2;
1076a83c6bb2SPing-Ke Shih 
1077a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1078a83c6bb2SPing-Ke Shih 				       MASKDWORD, a_itqt[gp]);
1079a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
1080a83c6bb2SPing-Ke Shih 	}
1081a83c6bb2SPing-Ke Shih 
1082a83c6bb2SPing-Ke Shih 	if (!notready)
1083a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
1084a83c6bb2SPing-Ke Shih 
1085a83c6bb2SPing-Ke Shih 	if (kfail) {
1086a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1087a83c6bb2SPing-Ke Shih 				       MASKDWORD, iqk_info->nb_txcfir[path] | 0x2);
1088a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = false;
1089a83c6bb2SPing-Ke Shih 	} else {
1090a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1091a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000000);
1092a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = true;
1093a83c6bb2SPing-Ke Shih 	}
1094a83c6bb2SPing-Ke Shih 
1095a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1096a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
1097a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_txcfir[path]);
1098a83c6bb2SPing-Ke Shih 	return kfail;
1099a83c6bb2SPing-Ke Shih }
1100a83c6bb2SPing-Ke Shih 
_txk_2g_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1101a83c6bb2SPing-Ke Shih static bool _txk_2g_group_sel(struct rtw89_dev *rtwdev,
1102a83c6bb2SPing-Ke Shih 			      enum rtw89_phy_idx phy_idx, u8 path)
1103a83c6bb2SPing-Ke Shih {
1104a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1105a83c6bb2SPing-Ke Shih 	bool kfail = false;
1106a83c6bb2SPing-Ke Shih 	bool notready;
1107a83c6bb2SPing-Ke Shih 	u8 gp;
1108a83c6bb2SPing-Ke Shih 
1109a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1110a83c6bb2SPing-Ke Shih 
1111a83c6bb2SPing-Ke Shih 	for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
1112a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, g_power_range[gp]);
1113a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, g_track_range[gp]);
1114a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, g_gain_bb[gp]);
1115a83c6bb2SPing-Ke Shih 
1116a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, g_itqt[gp]);
1117a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
1118a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
1119a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
1120a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
1121a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1122a83c6bb2SPing-Ke Shih 
1123a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
1124a83c6bb2SPing-Ke Shih 		iqk_info->nb_txcfir[path] =
1125a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD)  | 0x2;
1126a83c6bb2SPing-Ke Shih 
1127a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1128a83c6bb2SPing-Ke Shih 				       MASKDWORD, g_itqt[gp]);
1129a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
1130a83c6bb2SPing-Ke Shih 	}
1131a83c6bb2SPing-Ke Shih 
1132a83c6bb2SPing-Ke Shih 	if (!notready)
1133a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
1134a83c6bb2SPing-Ke Shih 
1135a83c6bb2SPing-Ke Shih 	if (kfail) {
1136a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1137a83c6bb2SPing-Ke Shih 				       MASKDWORD, iqk_info->nb_txcfir[path] | 0x2);
1138a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = false;
1139a83c6bb2SPing-Ke Shih 	} else {
1140a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1141a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000000);
1142a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = true;
1143a83c6bb2SPing-Ke Shih 	}
1144a83c6bb2SPing-Ke Shih 
1145a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1146a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
1147a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_txcfir[path]);
1148a83c6bb2SPing-Ke Shih 	return kfail;
1149a83c6bb2SPing-Ke Shih }
1150a83c6bb2SPing-Ke Shih 
_iqk_5g_nbtxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1151a83c6bb2SPing-Ke Shih static bool _iqk_5g_nbtxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1152a83c6bb2SPing-Ke Shih 			  u8 path)
1153a83c6bb2SPing-Ke Shih {
1154a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1155a83c6bb2SPing-Ke Shih 	bool kfail = false;
1156a83c6bb2SPing-Ke Shih 	bool notready;
1157a83c6bb2SPing-Ke Shih 	u8 gp;
1158a83c6bb2SPing-Ke Shih 
1159a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1160a83c6bb2SPing-Ke Shih 
1161a83c6bb2SPing-Ke Shih 	for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
1162a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, a_power_range[gp]);
1163a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, a_track_range[gp]);
1164a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, a_gain_bb[gp]);
1165a83c6bb2SPing-Ke Shih 
1166a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
1167a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
1168a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
1169a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
1170a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1171a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, a_itqt[gp]);
1172a83c6bb2SPing-Ke Shih 
1173a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
1174a83c6bb2SPing-Ke Shih 		iqk_info->nb_txcfir[path] =
1175a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXIQC, MASKDWORD)  | 0x2;
1176a83c6bb2SPing-Ke Shih 	}
1177a83c6bb2SPing-Ke Shih 
1178a83c6bb2SPing-Ke Shih 	if (!notready)
1179a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
1180a83c6bb2SPing-Ke Shih 
1181a83c6bb2SPing-Ke Shih 	if (kfail) {
1182a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1183a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000002);
1184a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1185a83c6bb2SPing-Ke Shih 	} else {
1186a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1187a83c6bb2SPing-Ke Shih 	}
1188a83c6bb2SPing-Ke Shih 
1189a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1190a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
1191a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_txcfir[path]);
1192a83c6bb2SPing-Ke Shih 	return kfail;
1193a83c6bb2SPing-Ke Shih }
1194a83c6bb2SPing-Ke Shih 
_iqk_2g_nbtxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1195a83c6bb2SPing-Ke Shih static bool _iqk_2g_nbtxk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1196a83c6bb2SPing-Ke Shih 			  u8 path)
1197a83c6bb2SPing-Ke Shih {
1198a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1199a83c6bb2SPing-Ke Shih 	bool kfail = false;
1200a83c6bb2SPing-Ke Shih 	bool notready;
1201a83c6bb2SPing-Ke Shih 	u8 gp;
1202a83c6bb2SPing-Ke Shih 
1203a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1204a83c6bb2SPing-Ke Shih 
1205a83c6bb2SPing-Ke Shih 	for (gp = 0x0; gp < RTW8851B_TXK_GROUP_NR; gp++) {
1206a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, g_power_range[gp]);
1207a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, g_track_range[gp]);
1208a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, g_gain_bb[gp]);
1209a83c6bb2SPing-Ke Shih 
1210a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, MASKDWORD, g_itqt[gp]);
1211a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_SEL, 0x1);
1212a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G3, 0x1);
1213a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_G2, 0x0);
1214a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT, B_CFIR_LUT_GP, gp);
1215a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1216a83c6bb2SPing-Ke Shih 
1217a83c6bb2SPing-Ke Shih 		notready = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
1218a83c6bb2SPing-Ke Shih 		iqk_info->nb_txcfir[path] =
1219a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8),
1220a83c6bb2SPing-Ke Shih 					      MASKDWORD)  | 0x2;
1221a83c6bb2SPing-Ke Shih 	}
1222a83c6bb2SPing-Ke Shih 
1223a83c6bb2SPing-Ke Shih 	if (!notready)
1224a83c6bb2SPing-Ke Shih 		kfail = !!rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, B_NCTL_RPT_FLG);
1225a83c6bb2SPing-Ke Shih 
1226a83c6bb2SPing-Ke Shih 	if (kfail) {
1227a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8),
1228a83c6bb2SPing-Ke Shih 				       MASKDWORD, 0x40000002);
1229a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1230a83c6bb2SPing-Ke Shih 	} else {
1231a83c6bb2SPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
1232a83c6bb2SPing-Ke Shih 	}
1233a83c6bb2SPing-Ke Shih 
1234a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1235a83c6bb2SPing-Ke Shih 		    "[IQK]S%x, kfail = 0x%x, 0x8%x38 = 0x%x\n", path, kfail,
1236a83c6bb2SPing-Ke Shih 		    1 << path, iqk_info->nb_txcfir[path]);
1237a83c6bb2SPing-Ke Shih 	return kfail;
1238a83c6bb2SPing-Ke Shih }
1239a83c6bb2SPing-Ke Shih 
_iqk_2g_lok(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1240a83c6bb2SPing-Ke Shih static bool _iqk_2g_lok(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1241a83c6bb2SPing-Ke Shih 			u8 path)
1242a83c6bb2SPing-Ke Shih {
1243a83c6bb2SPing-Ke Shih 	static const u32 g_txbb[RTW8851B_LOK_GRAM] = {
1244a83c6bb2SPing-Ke Shih 		0x02, 0x06, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
1245a83c6bb2SPing-Ke Shih 	static const u32 g_itqt[RTW8851B_LOK_GRAM] = {
1246a83c6bb2SPing-Ke Shih 		0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x12, 0x12, 0x12, 0x1b};
1247a83c6bb2SPing-Ke Shih 	static const u32 g_wa[RTW8851B_LOK_GRAM] = {
1248a83c6bb2SPing-Ke Shih 		0x00, 0x04, 0x08, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
1249a83c6bb2SPing-Ke Shih 	bool fail = false;
1250a83c6bb2SPing-Ke Shih 	u8 i;
1251a83c6bb2SPing-Ke Shih 
1252a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1253a83c6bb2SPing-Ke Shih 
1254a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_LOK, 0x0);
1255a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR0, 0x0);
1256a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR1, 0x6);
1257a83c6bb2SPing-Ke Shih 
1258a83c6bb2SPing-Ke Shih 	for (i = 0; i < RTW8851B_LOK_GRAM; i++) {
1259a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_TG, g_txbb[i]);
1260a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWA, RR_LUTWA_M1, g_wa[i]);
1261a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
1262a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, g_itqt[i]);
1263a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
1264a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
1265a83c6bb2SPing-Ke Shih 				       0x00000109 | (1 << (4 + path)));
1266a83c6bb2SPing-Ke Shih 		fail |= _iqk_check_cal(rtwdev, path);
1267a83c6bb2SPing-Ke Shih 
1268a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1269a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, g_itqt[i]);
1270a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
1271a83c6bb2SPing-Ke Shih 				       0x00000309 | (1 << (4 + path)));
1272a83c6bb2SPing-Ke Shih 		fail |= _iqk_check_cal(rtwdev, path);
1273a83c6bb2SPing-Ke Shih 
1274a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1275a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
1276a83c6bb2SPing-Ke Shih 
1277a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1278a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x8[19:15] = 0x%x,0x8[09:05] = 0x%x\n", i,
1279a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0xf8000),
1280a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0x003e0));
1281a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1282a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x9[19:16] = 0x%x,0x9[09:06] = 0x%x\n", i,
1283a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0xf0000),
1284a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0x003c0));
1285a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1286a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x58 = %x\n", i,
1287a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_TXMO, RFREG_MASK));
1288a83c6bb2SPing-Ke Shih 	}
1289a83c6bb2SPing-Ke Shih 
1290a83c6bb2SPing-Ke Shih 	return fail;
1291a83c6bb2SPing-Ke Shih }
1292a83c6bb2SPing-Ke Shih 
_iqk_5g_lok(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1293a83c6bb2SPing-Ke Shih static bool _iqk_5g_lok(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1294a83c6bb2SPing-Ke Shih 			u8 path)
1295a83c6bb2SPing-Ke Shih {
1296a83c6bb2SPing-Ke Shih 	static const u32 a_txbb[RTW8851B_LOK_GRAM] = {
1297a83c6bb2SPing-Ke Shih 		0x02, 0x06, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16, 0x17};
1298a83c6bb2SPing-Ke Shih 	static const u32 a_itqt[RTW8851B_LOK_GRAM] = {
1299a83c6bb2SPing-Ke Shih 		0x09, 0x09, 0x09, 0x12, 0x12, 0x12, 0x1b, 0x1b, 0x1b, 0x1b};
1300a83c6bb2SPing-Ke Shih 	static const u32 a_wa[RTW8851B_LOK_GRAM] = {
1301a83c6bb2SPing-Ke Shih 		0x80, 0x84, 0x88, 0x8c, 0x8e, 0x90, 0x92, 0x94, 0x96, 0x97};
1302a83c6bb2SPing-Ke Shih 	bool fail = false;
1303a83c6bb2SPing-Ke Shih 	u8 i;
1304a83c6bb2SPing-Ke Shih 
1305a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1306a83c6bb2SPing-Ke Shih 
1307a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_LOK, 0x0);
1308a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR0, 0x0);
1309a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_GR1, 0x7);
1310a83c6bb2SPing-Ke Shih 
1311a83c6bb2SPing-Ke Shih 	for (i = 0; i < RTW8851B_LOK_GRAM; i++) {
1312a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXIG, RR_TXIG_TG, a_txbb[i]);
1313a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWA, RR_LUTWA_M1, a_wa[i]);
1314a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x1);
1315a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, a_itqt[i]);
1316a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
1317a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
1318a83c6bb2SPing-Ke Shih 				       0x00000109 | (1 << (4 + path)));
1319a83c6bb2SPing-Ke Shih 		fail |= _iqk_check_cal(rtwdev, path);
1320a83c6bb2SPing-Ke Shih 
1321a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1322a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP, B_KIP_IQP_IQSW, a_itqt[i]);
1323a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x021);
1324a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
1325a83c6bb2SPing-Ke Shih 				       0x00000309 | (1 << (4 + path)));
1326a83c6bb2SPing-Ke Shih 		fail |= _iqk_check_cal(rtwdev, path);
1327a83c6bb2SPing-Ke Shih 
1328a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1329a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK, B_IQK_RFC_ON, 0x0);
1330a83c6bb2SPing-Ke Shih 
1331a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1332a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x8[19:15] = 0x%x,0x8[09:05] = 0x%x\n", i,
1333a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0xf8000),
1334a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_DTXLOK, 0x003e0));
1335a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1336a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x9[19:16] = 0x%x,0x9[09:06] = 0x%x\n", i,
1337a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0xf0000),
1338a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV2, 0x003c0));
1339a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1340a83c6bb2SPing-Ke Shih 			    "[IQK]S0, i = %x, 0x58 = %x\n", i,
1341a83c6bb2SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_TXMO, RFREG_MASK));
1342a83c6bb2SPing-Ke Shih 	}
1343a83c6bb2SPing-Ke Shih 
1344a83c6bb2SPing-Ke Shih 	return fail;
1345a83c6bb2SPing-Ke Shih }
1346a83c6bb2SPing-Ke Shih 
_iqk_txk_setting(struct rtw89_dev * rtwdev,u8 path)1347a83c6bb2SPing-Ke Shih static void _iqk_txk_setting(struct rtw89_dev *rtwdev, u8 path)
1348a83c6bb2SPing-Ke Shih {
1349a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1350a83c6bb2SPing-Ke Shih 
1351a83c6bb2SPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
1352a83c6bb2SPing-Ke Shih 	case RTW89_BAND_2G:
1353a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]RTW89_BAND_2G\n");
1354a83c6bb2SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_txk_2ghz_defs_tbl);
1355a83c6bb2SPing-Ke Shih 		break;
1356a83c6bb2SPing-Ke Shih 	case RTW89_BAND_5G:
1357a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]RTW89_BAND_5G\n");
1358a83c6bb2SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_txk_5ghz_defs_tbl);
1359a83c6bb2SPing-Ke Shih 		break;
1360a83c6bb2SPing-Ke Shih 	default:
1361a83c6bb2SPing-Ke Shih 		break;
1362a83c6bb2SPing-Ke Shih 	}
1363a83c6bb2SPing-Ke Shih }
1364a83c6bb2SPing-Ke Shih 
1365a83c6bb2SPing-Ke Shih #define IQK_LOK_RETRY 1
1366a83c6bb2SPing-Ke Shih 
_iqk_by_path(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1367a83c6bb2SPing-Ke Shih static void _iqk_by_path(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1368a83c6bb2SPing-Ke Shih 			 u8 path)
1369a83c6bb2SPing-Ke Shih {
1370a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1371a83c6bb2SPing-Ke Shih 	bool lok_is_fail;
1372a83c6bb2SPing-Ke Shih 	u8 i;
1373a83c6bb2SPing-Ke Shih 
1374a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1375a83c6bb2SPing-Ke Shih 
1376a83c6bb2SPing-Ke Shih 	for (i = 0; i < IQK_LOK_RETRY; i++) {
1377a83c6bb2SPing-Ke Shih 		_iqk_txk_setting(rtwdev, path);
1378a83c6bb2SPing-Ke Shih 		if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
1379a83c6bb2SPing-Ke Shih 			lok_is_fail = _iqk_2g_lok(rtwdev, phy_idx, path);
1380a83c6bb2SPing-Ke Shih 		else
1381a83c6bb2SPing-Ke Shih 			lok_is_fail = _iqk_5g_lok(rtwdev, phy_idx, path);
1382a83c6bb2SPing-Ke Shih 
1383a83c6bb2SPing-Ke Shih 		if (!lok_is_fail)
1384a83c6bb2SPing-Ke Shih 			break;
1385a83c6bb2SPing-Ke Shih 	}
1386a83c6bb2SPing-Ke Shih 
1387a83c6bb2SPing-Ke Shih 	if (iqk_info->is_nbiqk) {
1388a83c6bb2SPing-Ke Shih 		if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
1389a83c6bb2SPing-Ke Shih 			iqk_info->iqk_tx_fail[0][path] =
1390a83c6bb2SPing-Ke Shih 				_iqk_2g_nbtxk(rtwdev, phy_idx, path);
1391a83c6bb2SPing-Ke Shih 		else
1392a83c6bb2SPing-Ke Shih 			iqk_info->iqk_tx_fail[0][path] =
1393a83c6bb2SPing-Ke Shih 				_iqk_5g_nbtxk(rtwdev, phy_idx, path);
1394a83c6bb2SPing-Ke Shih 	} else {
1395a83c6bb2SPing-Ke Shih 		if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
1396a83c6bb2SPing-Ke Shih 			iqk_info->iqk_tx_fail[0][path] =
1397a83c6bb2SPing-Ke Shih 				_txk_2g_group_sel(rtwdev, phy_idx, path);
1398a83c6bb2SPing-Ke Shih 		else
1399a83c6bb2SPing-Ke Shih 			iqk_info->iqk_tx_fail[0][path] =
1400a83c6bb2SPing-Ke Shih 				_txk_5g_group_sel(rtwdev, phy_idx, path);
1401a83c6bb2SPing-Ke Shih 	}
1402a83c6bb2SPing-Ke Shih 
1403a83c6bb2SPing-Ke Shih 	_iqk_rxclk_setting(rtwdev, path);
1404a83c6bb2SPing-Ke Shih 	_iqk_rxk_setting(rtwdev, path);
1405a83c6bb2SPing-Ke Shih 	_adc_fifo_rst(rtwdev, phy_idx, path);
1406a83c6bb2SPing-Ke Shih 
1407a83c6bb2SPing-Ke Shih 	if (iqk_info->is_nbiqk) {
1408a83c6bb2SPing-Ke Shih 		if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
1409a83c6bb2SPing-Ke Shih 			iqk_info->iqk_rx_fail[0][path] =
1410a83c6bb2SPing-Ke Shih 				_iqk_2g_nbrxk(rtwdev, phy_idx, path);
1411a83c6bb2SPing-Ke Shih 		else
1412a83c6bb2SPing-Ke Shih 			iqk_info->iqk_rx_fail[0][path] =
1413a83c6bb2SPing-Ke Shih 				_iqk_5g_nbrxk(rtwdev, phy_idx, path);
1414a83c6bb2SPing-Ke Shih 	} else {
1415a83c6bb2SPing-Ke Shih 		if (iqk_info->iqk_band[path] == RTW89_BAND_2G)
1416a83c6bb2SPing-Ke Shih 			iqk_info->iqk_rx_fail[0][path] =
1417a83c6bb2SPing-Ke Shih 				_rxk_2g_group_sel(rtwdev, phy_idx, path);
1418a83c6bb2SPing-Ke Shih 		else
1419a83c6bb2SPing-Ke Shih 			iqk_info->iqk_rx_fail[0][path] =
1420a83c6bb2SPing-Ke Shih 				_rxk_5g_group_sel(rtwdev, phy_idx, path);
1421a83c6bb2SPing-Ke Shih 	}
1422a83c6bb2SPing-Ke Shih }
1423a83c6bb2SPing-Ke Shih 
_rfk_backup_bb_reg(struct rtw89_dev * rtwdev,u32 backup_bb_reg_val[])1424a83c6bb2SPing-Ke Shih static void _rfk_backup_bb_reg(struct rtw89_dev *rtwdev,
1425a83c6bb2SPing-Ke Shih 			       u32 backup_bb_reg_val[])
1426a83c6bb2SPing-Ke Shih {
1427a83c6bb2SPing-Ke Shih 	u32 i;
1428a83c6bb2SPing-Ke Shih 
1429a83c6bb2SPing-Ke Shih 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
1430a83c6bb2SPing-Ke Shih 		backup_bb_reg_val[i] =
1431a83c6bb2SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, rtw8851b_backup_bb_regs[i],
1432a83c6bb2SPing-Ke Shih 					      MASKDWORD);
1433a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1434a83c6bb2SPing-Ke Shih 			    "[RFK]backup bb reg : %x, value =%x\n",
1435a83c6bb2SPing-Ke Shih 			    rtw8851b_backup_bb_regs[i], backup_bb_reg_val[i]);
1436a83c6bb2SPing-Ke Shih 	}
1437a83c6bb2SPing-Ke Shih }
1438a83c6bb2SPing-Ke Shih 
_rfk_backup_rf_reg(struct rtw89_dev * rtwdev,u32 backup_rf_reg_val[],u8 rf_path)1439a83c6bb2SPing-Ke Shih static void _rfk_backup_rf_reg(struct rtw89_dev *rtwdev,
1440a83c6bb2SPing-Ke Shih 			       u32 backup_rf_reg_val[], u8 rf_path)
1441a83c6bb2SPing-Ke Shih {
1442a83c6bb2SPing-Ke Shih 	u32 i;
1443a83c6bb2SPing-Ke Shih 
1444a83c6bb2SPing-Ke Shih 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
1445a83c6bb2SPing-Ke Shih 		backup_rf_reg_val[i] =
1446a83c6bb2SPing-Ke Shih 			rtw89_read_rf(rtwdev, rf_path,
1447a83c6bb2SPing-Ke Shih 				      rtw8851b_backup_rf_regs[i], RFREG_MASK);
1448a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1449a83c6bb2SPing-Ke Shih 			    "[RFK]backup rf S%d reg : %x, value =%x\n", rf_path,
1450a83c6bb2SPing-Ke Shih 			    rtw8851b_backup_rf_regs[i], backup_rf_reg_val[i]);
1451a83c6bb2SPing-Ke Shih 	}
1452a83c6bb2SPing-Ke Shih }
1453a83c6bb2SPing-Ke Shih 
_rfk_restore_bb_reg(struct rtw89_dev * rtwdev,const u32 backup_bb_reg_val[])1454a83c6bb2SPing-Ke Shih static void _rfk_restore_bb_reg(struct rtw89_dev *rtwdev,
1455a83c6bb2SPing-Ke Shih 				const u32 backup_bb_reg_val[])
1456a83c6bb2SPing-Ke Shih {
1457a83c6bb2SPing-Ke Shih 	u32 i;
1458a83c6bb2SPing-Ke Shih 
1459a83c6bb2SPing-Ke Shih 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
1460a83c6bb2SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, rtw8851b_backup_bb_regs[i],
1461a83c6bb2SPing-Ke Shih 				       MASKDWORD, backup_bb_reg_val[i]);
1462a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1463a83c6bb2SPing-Ke Shih 			    "[RFK]restore bb reg : %x, value =%x\n",
1464a83c6bb2SPing-Ke Shih 			    rtw8851b_backup_bb_regs[i], backup_bb_reg_val[i]);
1465a83c6bb2SPing-Ke Shih 	}
1466a83c6bb2SPing-Ke Shih }
1467a83c6bb2SPing-Ke Shih 
_rfk_restore_rf_reg(struct rtw89_dev * rtwdev,const u32 backup_rf_reg_val[],u8 rf_path)1468a83c6bb2SPing-Ke Shih static void _rfk_restore_rf_reg(struct rtw89_dev *rtwdev,
1469a83c6bb2SPing-Ke Shih 				const u32 backup_rf_reg_val[], u8 rf_path)
1470a83c6bb2SPing-Ke Shih {
1471a83c6bb2SPing-Ke Shih 	u32 i;
1472a83c6bb2SPing-Ke Shih 
1473a83c6bb2SPing-Ke Shih 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
1474a83c6bb2SPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, rtw8851b_backup_rf_regs[i],
1475a83c6bb2SPing-Ke Shih 			       RFREG_MASK, backup_rf_reg_val[i]);
1476a83c6bb2SPing-Ke Shih 
1477a83c6bb2SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1478a83c6bb2SPing-Ke Shih 			    "[RFK]restore rf S%d reg: %x, value =%x\n", rf_path,
1479a83c6bb2SPing-Ke Shih 			    rtw8851b_backup_rf_regs[i], backup_rf_reg_val[i]);
1480a83c6bb2SPing-Ke Shih 	}
1481a83c6bb2SPing-Ke Shih }
1482a83c6bb2SPing-Ke Shih 
_iqk_get_ch_info(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 path)1483a83c6bb2SPing-Ke Shih static void _iqk_get_ch_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
1484a83c6bb2SPing-Ke Shih 			     u8 path)
1485a83c6bb2SPing-Ke Shih {
1486a83c6bb2SPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1487a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1488a83c6bb2SPing-Ke Shih 	u8 idx = 0;
1489a83c6bb2SPing-Ke Shih 
1490a83c6bb2SPing-Ke Shih 	iqk_info->iqk_band[path] = chan->band_type;
1491a83c6bb2SPing-Ke Shih 	iqk_info->iqk_bw[path] = chan->band_width;
1492a83c6bb2SPing-Ke Shih 	iqk_info->iqk_ch[path] = chan->channel;
1493a83c6bb2SPing-Ke Shih 	iqk_info->iqk_table_idx[path] = idx;
1494a83c6bb2SPing-Ke Shih 
1495a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d (PHY%d): / DBCC %s/ %s/ CH%d/ %s\n",
1496a83c6bb2SPing-Ke Shih 		    path, phy, rtwdev->dbcc_en ? "on" : "off",
1497a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_band[path] == 0 ? "2G" :
1498a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_band[path] == 1 ? "5G" : "6G",
1499a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_ch[path],
1500a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_bw[path] == 0 ? "20M" :
1501a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_bw[path] == 1 ? "40M" : "80M");
1502a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]times = 0x%x, ch =%x\n",
1503a83c6bb2SPing-Ke Shih 		    iqk_info->iqk_times, idx);
1504a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, iqk_info->syn1to2= 0x%x\n",
1505a83c6bb2SPing-Ke Shih 		    path, iqk_info->syn1to2);
1506a83c6bb2SPing-Ke Shih }
1507a83c6bb2SPing-Ke Shih 
_iqk_start_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1508a83c6bb2SPing-Ke Shih static void _iqk_start_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1509a83c6bb2SPing-Ke Shih 			   u8 path)
1510a83c6bb2SPing-Ke Shih {
1511a83c6bb2SPing-Ke Shih 	_iqk_by_path(rtwdev, phy_idx, path);
1512a83c6bb2SPing-Ke Shih }
1513a83c6bb2SPing-Ke Shih 
_iqk_restore(struct rtw89_dev * rtwdev,u8 path)1514a83c6bb2SPing-Ke Shih static void _iqk_restore(struct rtw89_dev *rtwdev, u8 path)
1515a83c6bb2SPing-Ke Shih {
1516a83c6bb2SPing-Ke Shih 	bool fail;
1517a83c6bb2SPing-Ke Shih 
1518a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1519a83c6bb2SPing-Ke Shih 
1520a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, 0x00001219);
1521a83c6bb2SPing-Ke Shih 	fsleep(10);
1522a83c6bb2SPing-Ke Shih 	fail = _iqk_check_cal(rtwdev, path);
1523a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK] restore fail=%d\n", fail);
1524a83c6bb2SPing-Ke Shih 
1525*76a7c7acSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTWE, RR_LUTWE_LOK, 0x0);
1526*76a7c7acSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LUTDBG, RR_LUTDBG_TIA, 0x0);
1527*76a7c7acSPing-Ke Shih 
1528a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1529a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
1530a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
1531a83c6bb2SPing-Ke Shih }
1532a83c6bb2SPing-Ke Shih 
_iqk_afebb_restore(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1533a83c6bb2SPing-Ke Shih static void _iqk_afebb_restore(struct rtw89_dev *rtwdev,
1534a83c6bb2SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx, u8 path)
1535a83c6bb2SPing-Ke Shih {
1536a83c6bb2SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_afebb_restore_defs_tbl);
1537a83c6bb2SPing-Ke Shih }
1538a83c6bb2SPing-Ke Shih 
_iqk_preset(struct rtw89_dev * rtwdev,u8 path)1539a83c6bb2SPing-Ke Shih static void _iqk_preset(struct rtw89_dev *rtwdev, u8 path)
1540a83c6bb2SPing-Ke Shih {
1541a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1542a83c6bb2SPing-Ke Shih 
1543a83c6bb2SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1544a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
1545a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x81ff010a);
1546a83c6bb2SPing-Ke Shih }
1547a83c6bb2SPing-Ke Shih 
_iqk_macbb_setting(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)1548a83c6bb2SPing-Ke Shih static void _iqk_macbb_setting(struct rtw89_dev *rtwdev,
1549a83c6bb2SPing-Ke Shih 			       enum rtw89_phy_idx phy_idx, u8 path)
1550a83c6bb2SPing-Ke Shih {
1551a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1552a83c6bb2SPing-Ke Shih 
1553a83c6bb2SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_macbb_defs_tbl);
1554a83c6bb2SPing-Ke Shih }
1555a83c6bb2SPing-Ke Shih 
_iqk_init(struct rtw89_dev * rtwdev)1556a83c6bb2SPing-Ke Shih static void _iqk_init(struct rtw89_dev *rtwdev)
1557a83c6bb2SPing-Ke Shih {
1558a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1559a83c6bb2SPing-Ke Shih 	u8 idx, path;
1560a83c6bb2SPing-Ke Shih 
1561a83c6bb2SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, MASKDWORD, 0x0);
1562a83c6bb2SPing-Ke Shih 
1563a83c6bb2SPing-Ke Shih 	if (iqk_info->is_iqk_init)
1564a83c6bb2SPing-Ke Shih 		return;
1565a83c6bb2SPing-Ke Shih 
1566a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1567a83c6bb2SPing-Ke Shih 
1568a83c6bb2SPing-Ke Shih 	iqk_info->is_iqk_init = true;
1569a83c6bb2SPing-Ke Shih 	iqk_info->is_nbiqk = false;
1570a83c6bb2SPing-Ke Shih 	iqk_info->iqk_fft_en = false;
1571a83c6bb2SPing-Ke Shih 	iqk_info->iqk_sram_en = false;
1572a83c6bb2SPing-Ke Shih 	iqk_info->iqk_cfir_en = false;
1573a83c6bb2SPing-Ke Shih 	iqk_info->iqk_xym_en = false;
1574a83c6bb2SPing-Ke Shih 	iqk_info->iqk_times = 0x0;
1575a83c6bb2SPing-Ke Shih 
1576a83c6bb2SPing-Ke Shih 	for (idx = 0; idx < RTW89_IQK_CHS_NR; idx++) {
1577a83c6bb2SPing-Ke Shih 		iqk_info->iqk_channel[idx] = 0x0;
1578a83c6bb2SPing-Ke Shih 		for (path = 0; path < RF_PATH_NUM_8851B; path++) {
1579a83c6bb2SPing-Ke Shih 			iqk_info->lok_cor_fail[idx][path] = false;
1580a83c6bb2SPing-Ke Shih 			iqk_info->lok_fin_fail[idx][path] = false;
1581a83c6bb2SPing-Ke Shih 			iqk_info->iqk_tx_fail[idx][path] = false;
1582a83c6bb2SPing-Ke Shih 			iqk_info->iqk_rx_fail[idx][path] = false;
1583a83c6bb2SPing-Ke Shih 			iqk_info->iqk_table_idx[path] = 0x0;
1584a83c6bb2SPing-Ke Shih 		}
1585a83c6bb2SPing-Ke Shih 	}
1586a83c6bb2SPing-Ke Shih }
1587a83c6bb2SPing-Ke Shih 
_doiqk(struct rtw89_dev * rtwdev,bool force,enum rtw89_phy_idx phy_idx,u8 path)1588a83c6bb2SPing-Ke Shih static void _doiqk(struct rtw89_dev *rtwdev, bool force,
1589a83c6bb2SPing-Ke Shih 		   enum rtw89_phy_idx phy_idx, u8 path)
1590a83c6bb2SPing-Ke Shih {
1591a83c6bb2SPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1592a83c6bb2SPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, RF_AB);
1593a83c6bb2SPing-Ke Shih 	u32 backup_rf_val[RTW8851B_IQK_SS][BACKUP_RF_REGS_NR];
1594a83c6bb2SPing-Ke Shih 	u32 backup_bb_val[BACKUP_BB_REGS_NR];
1595a83c6bb2SPing-Ke Shih 
1596a83c6bb2SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK,
1597a83c6bb2SPing-Ke Shih 			      BTC_WRFK_ONESHOT_START);
1598a83c6bb2SPing-Ke Shih 
1599a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
16005bc9a34cSDmitry Antipov 		    "[IQK]==========IQK start!!!!!==========\n");
1601a83c6bb2SPing-Ke Shih 	iqk_info->iqk_times++;
1602a83c6bb2SPing-Ke Shih 	iqk_info->version = RTW8851B_IQK_VER;
1603a83c6bb2SPing-Ke Shih 
1604a83c6bb2SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]Test Ver 0x%x\n", iqk_info->version);
1605a83c6bb2SPing-Ke Shih 	_iqk_get_ch_info(rtwdev, phy_idx, path);
1606a83c6bb2SPing-Ke Shih 
1607a83c6bb2SPing-Ke Shih 	_rfk_backup_bb_reg(rtwdev, &backup_bb_val[0]);
1608a83c6bb2SPing-Ke Shih 	_rfk_backup_rf_reg(rtwdev, &backup_rf_val[path][0], path);
1609a83c6bb2SPing-Ke Shih 	_iqk_macbb_setting(rtwdev, phy_idx, path);
1610a83c6bb2SPing-Ke Shih 	_iqk_preset(rtwdev, path);
1611a83c6bb2SPing-Ke Shih 	_iqk_start_iqk(rtwdev, phy_idx, path);
1612a83c6bb2SPing-Ke Shih 	_iqk_restore(rtwdev, path);
1613a83c6bb2SPing-Ke Shih 	_iqk_afebb_restore(rtwdev, phy_idx, path);
1614a83c6bb2SPing-Ke Shih 	_rfk_restore_bb_reg(rtwdev, &backup_bb_val[0]);
1615a83c6bb2SPing-Ke Shih 	_rfk_restore_rf_reg(rtwdev, &backup_rf_val[path][0], path);
1616a83c6bb2SPing-Ke Shih 
1617a83c6bb2SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK,
1618a83c6bb2SPing-Ke Shih 			      BTC_WRFK_ONESHOT_STOP);
1619a83c6bb2SPing-Ke Shih }
1620a83c6bb2SPing-Ke Shih 
_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,bool force)1621a83c6bb2SPing-Ke Shih static void _iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, bool force)
1622a83c6bb2SPing-Ke Shih {
1623a83c6bb2SPing-Ke Shih 	_doiqk(rtwdev, force, phy_idx, RF_PATH_A);
1624a83c6bb2SPing-Ke Shih }
1625a83c6bb2SPing-Ke Shih 
_dpk_bkup_kip(struct rtw89_dev * rtwdev,const u32 * reg,u32 reg_bkup[][DPK_KIP_REG_NUM_8851B],u8 path)16260194a95cSPing-Ke Shih static void _dpk_bkup_kip(struct rtw89_dev *rtwdev, const u32 *reg,
16270194a95cSPing-Ke Shih 			  u32 reg_bkup[][DPK_KIP_REG_NUM_8851B], u8 path)
16280194a95cSPing-Ke Shih {
16290194a95cSPing-Ke Shih 	u8 i;
16300194a95cSPing-Ke Shih 
16310194a95cSPing-Ke Shih 	for (i = 0; i < DPK_KIP_REG_NUM_8851B; i++) {
16320194a95cSPing-Ke Shih 		reg_bkup[path][i] =
16330194a95cSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD);
16340194a95cSPing-Ke Shih 
16350194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup 0x%x = %x\n",
16360194a95cSPing-Ke Shih 			    reg[i] + (path << 8), reg_bkup[path][i]);
16370194a95cSPing-Ke Shih 	}
16380194a95cSPing-Ke Shih }
16390194a95cSPing-Ke Shih 
_dpk_bkup_rf(struct rtw89_dev * rtwdev,const u32 * rf_reg,u32 rf_bkup[][DPK_RF_REG_NUM_8851B],u8 path)16400194a95cSPing-Ke Shih static void _dpk_bkup_rf(struct rtw89_dev *rtwdev, const u32 *rf_reg,
16410194a95cSPing-Ke Shih 			 u32 rf_bkup[][DPK_RF_REG_NUM_8851B], u8 path)
16420194a95cSPing-Ke Shih {
16430194a95cSPing-Ke Shih 	u8 i;
16440194a95cSPing-Ke Shih 
16450194a95cSPing-Ke Shih 	for (i = 0; i < DPK_RF_REG_NUM_8851B; i++) {
16460194a95cSPing-Ke Shih 		rf_bkup[path][i] = rtw89_read_rf(rtwdev, path, rf_reg[i], RFREG_MASK);
16470194a95cSPing-Ke Shih 
16480194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup RF S%d 0x%x = %x\n",
16490194a95cSPing-Ke Shih 			    path, rf_reg[i], rf_bkup[path][i]);
16500194a95cSPing-Ke Shih 	}
16510194a95cSPing-Ke Shih }
16520194a95cSPing-Ke Shih 
_dpk_reload_kip(struct rtw89_dev * rtwdev,const u32 * reg,u32 reg_bkup[][DPK_KIP_REG_NUM_8851B],u8 path)16530194a95cSPing-Ke Shih static void _dpk_reload_kip(struct rtw89_dev *rtwdev, const u32 *reg,
16540194a95cSPing-Ke Shih 			    u32 reg_bkup[][DPK_KIP_REG_NUM_8851B], u8 path)
16550194a95cSPing-Ke Shih {
16560194a95cSPing-Ke Shih 	u8 i;
16570194a95cSPing-Ke Shih 
16580194a95cSPing-Ke Shih 	for (i = 0; i < DPK_KIP_REG_NUM_8851B; i++) {
16590194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD,
16600194a95cSPing-Ke Shih 				       reg_bkup[path][i]);
16610194a95cSPing-Ke Shih 
16620194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16630194a95cSPing-Ke Shih 			    "[DPK] Reload 0x%x = %x\n",
16640194a95cSPing-Ke Shih 			    reg[i] + (path << 8), reg_bkup[path][i]);
16650194a95cSPing-Ke Shih 	}
16660194a95cSPing-Ke Shih }
16670194a95cSPing-Ke Shih 
_dpk_reload_rf(struct rtw89_dev * rtwdev,const u32 * rf_reg,u32 rf_bkup[][DPK_RF_REG_NUM_8851B],u8 path)16680194a95cSPing-Ke Shih static void _dpk_reload_rf(struct rtw89_dev *rtwdev, const u32 *rf_reg,
16690194a95cSPing-Ke Shih 			   u32 rf_bkup[][DPK_RF_REG_NUM_8851B], u8 path)
16700194a95cSPing-Ke Shih {
16710194a95cSPing-Ke Shih 	u8 i;
16720194a95cSPing-Ke Shih 
16730194a95cSPing-Ke Shih 	for (i = 0; i < DPK_RF_REG_NUM_8851B; i++) {
16740194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, rf_reg[i], RFREG_MASK, rf_bkup[path][i]);
16750194a95cSPing-Ke Shih 
16760194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16770194a95cSPing-Ke Shih 			    "[DPK] Reload RF S%d 0x%x = %x\n", path,
16780194a95cSPing-Ke Shih 			    rf_reg[i], rf_bkup[path][i]);
16790194a95cSPing-Ke Shih 	}
16800194a95cSPing-Ke Shih }
16810194a95cSPing-Ke Shih 
_dpk_one_shot(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,enum dpk_id id)16820194a95cSPing-Ke Shih static void _dpk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
16830194a95cSPing-Ke Shih 			  enum rtw89_rf_path path, enum dpk_id id)
16840194a95cSPing-Ke Shih {
16850194a95cSPing-Ke Shih 	u16 dpk_cmd;
16860194a95cSPing-Ke Shih 	u32 val;
16870194a95cSPing-Ke Shih 	int ret;
16880194a95cSPing-Ke Shih 
16890194a95cSPing-Ke Shih 	dpk_cmd = ((id << 8) | (0x19 + path * 0x12));
16900194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, dpk_cmd);
16910194a95cSPing-Ke Shih 
16920194a95cSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
16930194a95cSPing-Ke Shih 				       10, 20000, false,
16940194a95cSPing-Ke Shih 				       rtwdev, 0xbff8, MASKBYTE0);
16950194a95cSPing-Ke Shih 	if (ret)
16960194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] one-shot 1 timeout\n");
16970194a95cSPing-Ke Shih 
16980194a95cSPing-Ke Shih 	udelay(1);
16990194a95cSPing-Ke Shih 
17000194a95cSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x8000,
17010194a95cSPing-Ke Shih 				       1, 2000, false,
17020194a95cSPing-Ke Shih 				       rtwdev, R_RPT_COM, MASKLWORD);
17030194a95cSPing-Ke Shih 	if (ret)
17040194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] one-shot 2 timeout\n");
17050194a95cSPing-Ke Shih 
17060194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, MASKBYTE0, 0x0);
17070194a95cSPing-Ke Shih 
17080194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
17090194a95cSPing-Ke Shih 		    "[DPK] one-shot for %s = 0x%04x\n",
17100194a95cSPing-Ke Shih 		    id == 0x28 ? "KIP_PRESET" :
17110194a95cSPing-Ke Shih 		    id == 0x29 ? "DPK_TXAGC" :
17120194a95cSPing-Ke Shih 		    id == 0x2a ? "DPK_RXAGC" :
17130194a95cSPing-Ke Shih 		    id == 0x2b ? "SYNC" :
17140194a95cSPing-Ke Shih 		    id == 0x2c ? "GAIN_LOSS" :
17150194a95cSPing-Ke Shih 		    id == 0x2d ? "MDPK_IDL" :
17160194a95cSPing-Ke Shih 		    id == 0x2f ? "DPK_GAIN_NORM" :
171756fc4d48SColin Ian King 		    id == 0x31 ? "KIP_RESTORE" :
17180194a95cSPing-Ke Shih 		    id == 0x6 ? "LBK_RXIQK" : "Unknown id",
17190194a95cSPing-Ke Shih 		    dpk_cmd);
17200194a95cSPing-Ke Shih }
17210194a95cSPing-Ke Shih 
_dpk_onoff(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool off)17220194a95cSPing-Ke Shih static void _dpk_onoff(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
17230194a95cSPing-Ke Shih 		       bool off)
17240194a95cSPing-Ke Shih {
17250194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
17260194a95cSPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
17270194a95cSPing-Ke Shih 	u8 off_reverse = off ? 0 : 1;
17280194a95cSPing-Ke Shih 	u8 val;
17290194a95cSPing-Ke Shih 
17300194a95cSPing-Ke Shih 	val = dpk->is_dpk_enable * off_reverse * dpk->bp[path][kidx].path_ok;
17310194a95cSPing-Ke Shih 
17320194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
17330194a95cSPing-Ke Shih 			       0xf0000000, val);
17340194a95cSPing-Ke Shih 
17350194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] DPK %s !!!\n", path,
17360194a95cSPing-Ke Shih 		    kidx, val == 0 ? "disable" : "enable");
17370194a95cSPing-Ke Shih }
17380194a95cSPing-Ke Shih 
_dpk_init(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)17390194a95cSPing-Ke Shih static void _dpk_init(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
17400194a95cSPing-Ke Shih {
17410194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
17420194a95cSPing-Ke Shih 
17430194a95cSPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
17440194a95cSPing-Ke Shih 
17450194a95cSPing-Ke Shih 	dpk->bp[path][kidx].path_ok = 0;
17460194a95cSPing-Ke Shih }
17470194a95cSPing-Ke Shih 
_dpk_information(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)17480194a95cSPing-Ke Shih static void _dpk_information(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
17490194a95cSPing-Ke Shih 			     enum rtw89_rf_path path)
17500194a95cSPing-Ke Shih {
17510194a95cSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
17520194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
17530194a95cSPing-Ke Shih 
17540194a95cSPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
17550194a95cSPing-Ke Shih 
17560194a95cSPing-Ke Shih 	dpk->bp[path][kidx].band = chan->band_type;
17570194a95cSPing-Ke Shih 	dpk->bp[path][kidx].ch = chan->band_width;
17580194a95cSPing-Ke Shih 	dpk->bp[path][kidx].bw = chan->channel;
17590194a95cSPing-Ke Shih 
17600194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
17610194a95cSPing-Ke Shih 		    "[DPK] S%d[%d] (PHY%d): TSSI %s/ DBCC %s/ %s/ CH%d/ %s\n",
17620194a95cSPing-Ke Shih 		    path, dpk->cur_idx[path], phy,
17630194a95cSPing-Ke Shih 		    rtwdev->is_tssi_mode[path] ? "on" : "off",
17640194a95cSPing-Ke Shih 		    rtwdev->dbcc_en ? "on" : "off",
17650194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].band == 0 ? "2G" :
17660194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].band == 1 ? "5G" : "6G",
17670194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].ch,
17680194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].bw == 0 ? "20M" :
17690194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].bw == 1 ? "40M" :
17700194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].bw == 2 ? "80M" : "160M");
17710194a95cSPing-Ke Shih }
17720194a95cSPing-Ke Shih 
_dpk_rxagc_onoff(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool turn_on)17730194a95cSPing-Ke Shih static void _dpk_rxagc_onoff(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
17740194a95cSPing-Ke Shih 			     bool turn_on)
17750194a95cSPing-Ke Shih {
17760194a95cSPing-Ke Shih 	if (path == RF_PATH_A)
17770194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_AGC_CTL, B_P0_AGC_EN, turn_on);
17780194a95cSPing-Ke Shih 	else
17790194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_AGC_CTL, B_P1_AGC_EN, turn_on);
17800194a95cSPing-Ke Shih 
17810194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d RXAGC is %s\n", path,
17820194a95cSPing-Ke Shih 		    turn_on ? "turn_on" : "turn_off");
17830194a95cSPing-Ke Shih }
17840194a95cSPing-Ke Shih 
_dpk_bb_afe_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)17850194a95cSPing-Ke Shih static void _dpk_bb_afe_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
17860194a95cSPing-Ke Shih {
17870194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x1);
17880194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x0);
17890194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x1);
17900194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x0);
17910194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0xd801dffd);
17920194a95cSPing-Ke Shih 
17930194a95cSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_iqk_bb_afe_defs_tbl);
17940194a95cSPing-Ke Shih 
17950194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x1);
17960194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x1);
17970194a95cSPing-Ke Shih 
17980194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE setting\n", path);
17990194a95cSPing-Ke Shih }
18000194a95cSPing-Ke Shih 
_dpk_bb_afe_restore(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)18010194a95cSPing-Ke Shih static void _dpk_bb_afe_restore(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
18020194a95cSPing-Ke Shih {
18030194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x0);
18040194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x1);
18050194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(20 + path), 0x0);
18060194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x1);
18070194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(28 + path), 0x0);
18080194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0x00000000);
18090194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK + (path << 13), B_P0_TXCK_ALL, 0x00);
18100194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(16 + path), 0x0);
18110194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, BIT(24 + path), 0x0);
18120194a95cSPing-Ke Shih 
18130194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE restore\n", path);
18140194a95cSPing-Ke Shih }
18150194a95cSPing-Ke Shih 
_dpk_tssi_pause(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool is_pause)18160194a95cSPing-Ke Shih static void _dpk_tssi_pause(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
18170194a95cSPing-Ke Shih 			    bool is_pause)
18180194a95cSPing-Ke Shih {
18190194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
18200194a95cSPing-Ke Shih 			       B_P0_TSSI_TRK_EN, is_pause);
18210194a95cSPing-Ke Shih 
18220194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d TSSI %s\n", path,
18230194a95cSPing-Ke Shih 		    is_pause ? "pause" : "resume");
18240194a95cSPing-Ke Shih }
18250194a95cSPing-Ke Shih 
_dpk_tpg_sel(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)18260194a95cSPing-Ke Shih static void _dpk_tpg_sel(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
18270194a95cSPing-Ke Shih {
18280194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
18290194a95cSPing-Ke Shih 
18300194a95cSPing-Ke Shih 	if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80) {
18310194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x0);
18320194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xffe0fa00);
18330194a95cSPing-Ke Shih 	} else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40) {
18340194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x2);
18350194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xff4009e0);
18360194a95cSPing-Ke Shih 	} else {
18370194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x1);
18380194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xf9f007d0);
18390194a95cSPing-Ke Shih 	}
18400194a95cSPing-Ke Shih 
18410194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] TPG Select for %s\n",
18420194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80 ? "80M" :
18430194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ? "40M" : "20M");
18440194a95cSPing-Ke Shih }
18450194a95cSPing-Ke Shih 
_dpk_txpwr_bb_force(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool force)18460194a95cSPing-Ke Shih static void _dpk_txpwr_bb_force(struct rtw89_dev *rtwdev,
18470194a95cSPing-Ke Shih 				enum rtw89_rf_path path, bool force)
18480194a95cSPing-Ke Shih {
18490194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_ON, force);
18500194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB_H + (path << 13), B_TXPWRB_RDY, force);
18510194a95cSPing-Ke Shih 
18520194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d txpwr_bb_force %s\n",
18530194a95cSPing-Ke Shih 		    path, force ? "on" : "off");
18540194a95cSPing-Ke Shih }
18550194a95cSPing-Ke Shih 
_dpk_kip_pwr_clk_onoff(struct rtw89_dev * rtwdev,bool turn_on)18560194a95cSPing-Ke Shih static void _dpk_kip_pwr_clk_onoff(struct rtw89_dev *rtwdev, bool turn_on)
18570194a95cSPing-Ke Shih {
18580194a95cSPing-Ke Shih 	if (turn_on) {
18590194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
18600194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x807f030a);
18610194a95cSPing-Ke Shih 	} else {
18620194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
18630194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
18640194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_WR, BIT(18), 0x1);
18650194a95cSPing-Ke Shih 	}
18660194a95cSPing-Ke Shih }
18670194a95cSPing-Ke Shih 
_dpk_kip_control_rfc(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool ctrl_by_kip)18680194a95cSPing-Ke Shih static void _dpk_kip_control_rfc(struct rtw89_dev *rtwdev,
18690194a95cSPing-Ke Shih 				 enum rtw89_rf_path path, bool ctrl_by_kip)
18700194a95cSPing-Ke Shih {
18710194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13),
18720194a95cSPing-Ke Shih 			       B_IQK_RFC_ON, ctrl_by_kip);
18730194a95cSPing-Ke Shih 
18740194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RFC is controlled by %s\n",
18750194a95cSPing-Ke Shih 		    ctrl_by_kip ? "KIP" : "BB");
18760194a95cSPing-Ke Shih }
18770194a95cSPing-Ke Shih 
_dpk_kip_preset(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)18780194a95cSPing-Ke Shih static void _dpk_kip_preset(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
18790194a95cSPing-Ke Shih 			    enum rtw89_rf_path path, u8 kidx)
18800194a95cSPing-Ke Shih {
18810194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
18820194a95cSPing-Ke Shih 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
18830194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
18840194a95cSPing-Ke Shih 			       B_DPD_SEL, 0x01);
18850194a95cSPing-Ke Shih 
18860194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
18870194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_KIP_PRESET);
18880194a95cSPing-Ke Shih }
18890194a95cSPing-Ke Shih 
_dpk_kip_restore(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)18900194a95cSPing-Ke Shih static void _dpk_kip_restore(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
18910194a95cSPing-Ke Shih 			     enum rtw89_rf_path path)
18920194a95cSPing-Ke Shih {
18930194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_KIP_RESTORE);
18940194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
18950194a95cSPing-Ke Shih 	_dpk_txpwr_bb_force(rtwdev, path, false);
18960194a95cSPing-Ke Shih 
18970194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d restore KIP\n", path);
18980194a95cSPing-Ke Shih }
18990194a95cSPing-Ke Shih 
_dpk_kset_query(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)19000194a95cSPing-Ke Shih static void _dpk_kset_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
19010194a95cSPing-Ke Shih {
19020194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
19030194a95cSPing-Ke Shih 
19040194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0x10);
19050194a95cSPing-Ke Shih 
19060194a95cSPing-Ke Shih 	dpk->cur_k_set =
19070194a95cSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_KSET) - 1;
19080194a95cSPing-Ke Shih }
19090194a95cSPing-Ke Shih 
_dpk_para_query(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)19100194a95cSPing-Ke Shih static void _dpk_para_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
19110194a95cSPing-Ke Shih {
19120194a95cSPing-Ke Shih 	static const u32 reg[RTW89_DPK_BKUP_NUM][DPK_KSET_NUM] = {
19130194a95cSPing-Ke Shih 		{0x8190, 0x8194, 0x8198, 0x81a4},
19140194a95cSPing-Ke Shih 		{0x81a8, 0x81c4, 0x81c8, 0x81e8}
19150194a95cSPing-Ke Shih 	};
19160194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
19170194a95cSPing-Ke Shih 	u8 cur_k_set = dpk->cur_k_set;
19180194a95cSPing-Ke Shih 	u32 para;
19190194a95cSPing-Ke Shih 
19200194a95cSPing-Ke Shih 	if (cur_k_set >= DPK_KSET_NUM) {
19210194a95cSPing-Ke Shih 		rtw89_warn(rtwdev, "DPK cur_k_set = %d\n", cur_k_set);
19220194a95cSPing-Ke Shih 		cur_k_set = 2;
19230194a95cSPing-Ke Shih 	}
19240194a95cSPing-Ke Shih 
19250194a95cSPing-Ke Shih 	para = rtw89_phy_read32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
19260194a95cSPing-Ke Shih 				     MASKDWORD);
19270194a95cSPing-Ke Shih 
19280194a95cSPing-Ke Shih 	dpk->bp[path][kidx].txagc_dpk = (para >> 10) & 0x3f;
19290194a95cSPing-Ke Shih 	dpk->bp[path][kidx].ther_dpk = (para >> 26) & 0x3f;
19300194a95cSPing-Ke Shih 
19310194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
19320194a95cSPing-Ke Shih 		    "[DPK] thermal/ txagc_RF (K%d) = 0x%x/ 0x%x\n",
19330194a95cSPing-Ke Shih 		    dpk->cur_k_set, dpk->bp[path][kidx].ther_dpk,
19340194a95cSPing-Ke Shih 		    dpk->bp[path][kidx].txagc_dpk);
19350194a95cSPing-Ke Shih }
19360194a95cSPing-Ke Shih 
_dpk_sync_check(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)19370194a95cSPing-Ke Shih static bool _dpk_sync_check(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
19380194a95cSPing-Ke Shih {
19390194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
19400194a95cSPing-Ke Shih 	u8 corr_val, corr_idx, rxbb;
19410194a95cSPing-Ke Shih 	u16 dc_i, dc_q;
19420194a95cSPing-Ke Shih 	u8 rxbb_ov;
19430194a95cSPing-Ke Shih 
19440194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
19450194a95cSPing-Ke Shih 
19460194a95cSPing-Ke Shih 	corr_idx = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORI);
19470194a95cSPing-Ke Shih 	corr_val = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORV);
19480194a95cSPing-Ke Shih 	dpk->corr_idx[path][kidx] = corr_idx;
19490194a95cSPing-Ke Shih 	dpk->corr_val[path][kidx] = corr_val;
19500194a95cSPing-Ke Shih 
19510194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x9);
19520194a95cSPing-Ke Shih 
19530194a95cSPing-Ke Shih 	dc_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
19540194a95cSPing-Ke Shih 	dc_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ);
19550194a95cSPing-Ke Shih 
19560194a95cSPing-Ke Shih 	dc_i = abs(sign_extend32(dc_i, 11));
19570194a95cSPing-Ke Shih 	dc_q = abs(sign_extend32(dc_q, 11));
19580194a95cSPing-Ke Shih 
19590194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
19600194a95cSPing-Ke Shih 		    "[DPK] S%d Corr_idx/ Corr_val /DC I/Q, = %d / %d / %d / %d\n",
19610194a95cSPing-Ke Shih 		    path, corr_idx, corr_val, dc_i, dc_q);
19620194a95cSPing-Ke Shih 
19630194a95cSPing-Ke Shih 	dpk->dc_i[path][kidx] = dc_i;
19640194a95cSPing-Ke Shih 	dpk->dc_q[path][kidx] = dc_q;
19650194a95cSPing-Ke Shih 
19660194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x8);
19670194a95cSPing-Ke Shih 	rxbb = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXBB);
19680194a95cSPing-Ke Shih 
19690194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x31);
19700194a95cSPing-Ke Shih 	rxbb_ov = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXOV);
19710194a95cSPing-Ke Shih 
19720194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
19730194a95cSPing-Ke Shih 		    "[DPK] S%d RXBB/ RXAGC_done /RXBB_ovlmt = %d / %d / %d\n",
19740194a95cSPing-Ke Shih 		    path, rxbb,
19750194a95cSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DONE),
19760194a95cSPing-Ke Shih 		    rxbb_ov);
19770194a95cSPing-Ke Shih 
19780194a95cSPing-Ke Shih 	if (dc_i > 200 || dc_q > 200 || corr_val < 170)
19790194a95cSPing-Ke Shih 		return true;
19800194a95cSPing-Ke Shih 	else
19810194a95cSPing-Ke Shih 		return false;
19820194a95cSPing-Ke Shih }
19830194a95cSPing-Ke Shih 
_dpk_kip_set_txagc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 dbm,bool set_from_bb)19840194a95cSPing-Ke Shih static void _dpk_kip_set_txagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
19850194a95cSPing-Ke Shih 			       enum rtw89_rf_path path, u8 dbm,
19860194a95cSPing-Ke Shih 			       bool set_from_bb)
19870194a95cSPing-Ke Shih {
19880194a95cSPing-Ke Shih 	if (set_from_bb) {
19890194a95cSPing-Ke Shih 		dbm = clamp_t(u8, dbm, 7, 24);
19900194a95cSPing-Ke Shih 
19910194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
19920194a95cSPing-Ke Shih 			    "[DPK] set S%d txagc to %ddBm\n", path, dbm);
19930194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13),
19940194a95cSPing-Ke Shih 				       B_TXPWRB_VAL, dbm << 2);
19950194a95cSPing-Ke Shih 	}
19960194a95cSPing-Ke Shih 
19970194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_TXAGC);
19980194a95cSPing-Ke Shih 	_dpk_kset_query(rtwdev, path);
19990194a95cSPing-Ke Shih }
20000194a95cSPing-Ke Shih 
_dpk_kip_set_rxagc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)20010194a95cSPing-Ke Shih static bool _dpk_kip_set_rxagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
20020194a95cSPing-Ke Shih 			       enum rtw89_rf_path path, u8 kidx)
20030194a95cSPing-Ke Shih {
20040194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
20050194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
20060194a95cSPing-Ke Shih 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
20070194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
20080194a95cSPing-Ke Shih 
20090194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_RXAGC);
20100194a95cSPing-Ke Shih 	return _dpk_sync_check(rtwdev, path, kidx);
20110194a95cSPing-Ke Shih }
20120194a95cSPing-Ke Shih 
_dpk_lbk_rxiqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)20130194a95cSPing-Ke Shih static void _dpk_lbk_rxiqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
20140194a95cSPing-Ke Shih 			   enum rtw89_rf_path path)
20150194a95cSPing-Ke Shih {
20160194a95cSPing-Ke Shih 	u32 rf_11, reg_81cc;
20170194a95cSPing-Ke Shih 	u8 cur_rxbb;
20180194a95cSPing-Ke Shih 
20190194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
20200194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x1);
20210194a95cSPing-Ke Shih 
20220194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
20230194a95cSPing-Ke Shih 
20240194a95cSPing-Ke Shih 	cur_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB);
20250194a95cSPing-Ke Shih 	rf_11 = rtw89_read_rf(rtwdev, path, RR_TXIG, RFREG_MASK);
20260194a95cSPing-Ke Shih 	reg_81cc = rtw89_phy_read32_mask(rtwdev, R_KIP_IQP + (path << 8),
20270194a95cSPing-Ke Shih 					 B_KIP_IQP_SW);
20280194a95cSPing-Ke Shih 
20290194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
20300194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x3);
20310194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0xd);
20320194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, 0x1f);
20330194a95cSPing-Ke Shih 
20340194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_IQSW, 0x12);
20350194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, 0x3);
20360194a95cSPing-Ke Shih 
20370194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
20380194a95cSPing-Ke Shih 
20390194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, MASKDWORD, 0x00250025);
20400194a95cSPing-Ke Shih 
20410194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, LBK_RXIQK);
20420194a95cSPing-Ke Shih 
20430194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d LBK RXIQC = 0x%x\n", path,
20440194a95cSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD));
20450194a95cSPing-Ke Shih 
20460194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
20470194a95cSPing-Ke Shih 
20480194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RFREG_MASK, rf_11);
20490194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, cur_rxbb);
20500194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, reg_81cc);
20510194a95cSPing-Ke Shih 
20520194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x0);
20530194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KPATH_CFG, B_KPATH_CFG_ED, 0x0);
20540194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_DI, 0x1);
20550194a95cSPing-Ke Shih 
20560194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
20570194a95cSPing-Ke Shih }
20580194a95cSPing-Ke Shih 
_dpk_rf_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)20590194a95cSPing-Ke Shih static void _dpk_rf_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
20600194a95cSPing-Ke Shih {
20610194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
20620194a95cSPing-Ke Shih 
20630194a95cSPing-Ke Shih 	if (dpk->bp[path][kidx].band == RTW89_BAND_2G) {
20640194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK, 0x50521);
20650194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
20660194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTC, 0x0);
20670194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTR, 0x7);
20680194a95cSPing-Ke Shih 	} else {
20690194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
20700194a95cSPing-Ke Shih 			       0x50521 | BIT(rtwdev->dbcc_en));
20710194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
20720194a95cSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RAA2_SATT, 0x3);
20730194a95cSPing-Ke Shih 	}
20740194a95cSPing-Ke Shih 
20750194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RCKD, RR_RCKD_BW, 0x1);
20760194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_BTC, RR_BTC_TXBB, dpk->bp[path][kidx].bw + 1);
20770194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_BTC, RR_BTC_RXBB, 0x0);
20780194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_EBW, 0x0);
20790194a95cSPing-Ke Shih }
20800194a95cSPing-Ke Shih 
_dpk_bypass_rxiqc(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)20810194a95cSPing-Ke Shih static void _dpk_bypass_rxiqc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
20820194a95cSPing-Ke Shih {
20830194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
20840194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD, 0x40000002);
20850194a95cSPing-Ke Shih 
20860194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Bypass RXIQC\n");
20870194a95cSPing-Ke Shih }
20880194a95cSPing-Ke Shih 
_dpk_dgain_read(struct rtw89_dev * rtwdev)20890194a95cSPing-Ke Shih static u16 _dpk_dgain_read(struct rtw89_dev *rtwdev)
20900194a95cSPing-Ke Shih {
20910194a95cSPing-Ke Shih 	u16 dgain;
20920194a95cSPing-Ke Shih 
20930194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
20940194a95cSPing-Ke Shih 	dgain = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
20950194a95cSPing-Ke Shih 
20960194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] DGain = 0x%x\n", dgain);
20970194a95cSPing-Ke Shih 
20980194a95cSPing-Ke Shih 	return dgain;
20990194a95cSPing-Ke Shih }
21000194a95cSPing-Ke Shih 
_dpk_gainloss_read(struct rtw89_dev * rtwdev)21010194a95cSPing-Ke Shih static u8 _dpk_gainloss_read(struct rtw89_dev *rtwdev)
21020194a95cSPing-Ke Shih {
21030194a95cSPing-Ke Shih 	u8 result;
21040194a95cSPing-Ke Shih 
21050194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x6);
21060194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x1);
21070194a95cSPing-Ke Shih 	result = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_GL);
21080194a95cSPing-Ke Shih 
21090194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] tmp GL = %d\n", result);
21100194a95cSPing-Ke Shih 
21110194a95cSPing-Ke Shih 	return result;
21120194a95cSPing-Ke Shih }
21130194a95cSPing-Ke Shih 
_dpk_gainloss(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)21140194a95cSPing-Ke Shih static u8 _dpk_gainloss(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
21150194a95cSPing-Ke Shih 			enum rtw89_rf_path path, u8 kidx)
21160194a95cSPing-Ke Shih {
21170194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_GAIN_LOSS);
21180194a95cSPing-Ke Shih 	_dpk_kip_set_txagc(rtwdev, phy, path, 0xff, false);
21190194a95cSPing-Ke Shih 
21200194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A1, 0xf078);
21210194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A0, 0x0);
21220194a95cSPing-Ke Shih 
21230194a95cSPing-Ke Shih 	return _dpk_gainloss_read(rtwdev);
21240194a95cSPing-Ke Shih }
21250194a95cSPing-Ke Shih 
_dpk_pas_read(struct rtw89_dev * rtwdev,u8 is_check)21260194a95cSPing-Ke Shih static u8 _dpk_pas_read(struct rtw89_dev *rtwdev, u8 is_check)
21270194a95cSPing-Ke Shih {
21280194a95cSPing-Ke Shih 	u32 val1_i = 0, val1_q = 0, val2_i = 0, val2_q = 0;
21290194a95cSPing-Ke Shih 	u32 val1_sqrt_sum, val2_sqrt_sum;
21300194a95cSPing-Ke Shih 	u8 i;
21310194a95cSPing-Ke Shih 
21320194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKBYTE2, 0x06);
21330194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x0);
21340194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE2, 0x08);
21350194a95cSPing-Ke Shih 
21360194a95cSPing-Ke Shih 	if (is_check) {
21370194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x00);
21380194a95cSPing-Ke Shih 		val1_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
21390194a95cSPing-Ke Shih 		val1_i = abs(sign_extend32(val1_i, 11));
21400194a95cSPing-Ke Shih 		val1_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
21410194a95cSPing-Ke Shih 		val1_q = abs(sign_extend32(val1_q, 11));
21420194a95cSPing-Ke Shih 
21430194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x1f);
21440194a95cSPing-Ke Shih 		val2_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
21450194a95cSPing-Ke Shih 		val2_i = abs(sign_extend32(val2_i, 11));
21460194a95cSPing-Ke Shih 		val2_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
21470194a95cSPing-Ke Shih 		val2_q = abs(sign_extend32(val2_q, 11));
21480194a95cSPing-Ke Shih 
21490194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_delta = 0x%x\n",
21500194a95cSPing-Ke Shih 			    phy_div(val1_i * val1_i + val1_q * val1_q,
21510194a95cSPing-Ke Shih 				    val2_i * val2_i + val2_q * val2_q));
21520194a95cSPing-Ke Shih 	} else {
21530194a95cSPing-Ke Shih 		for (i = 0; i < 32; i++) {
21540194a95cSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, i);
21550194a95cSPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK,
21560194a95cSPing-Ke Shih 				    "[DPK] PAS_Read[%02d]= 0x%08x\n", i,
21570194a95cSPing-Ke Shih 				    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
21580194a95cSPing-Ke Shih 		}
21590194a95cSPing-Ke Shih 	}
21600194a95cSPing-Ke Shih 
21610194a95cSPing-Ke Shih 	val1_sqrt_sum = val1_i * val1_i + val1_q * val1_q;
21620194a95cSPing-Ke Shih 	val2_sqrt_sum = val2_i * val2_i + val2_q * val2_q;
21630194a95cSPing-Ke Shih 
21640194a95cSPing-Ke Shih 	if (val1_sqrt_sum < val2_sqrt_sum)
21650194a95cSPing-Ke Shih 		return 2;
21660194a95cSPing-Ke Shih 	else if (val1_sqrt_sum >= val2_sqrt_sum * 8 / 5)
21670194a95cSPing-Ke Shih 		return 1;
21680194a95cSPing-Ke Shih 	else
21690194a95cSPing-Ke Shih 		return 0;
21700194a95cSPing-Ke Shih }
21710194a95cSPing-Ke Shih 
_dpk_agc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx,u8 init_xdbm,u8 loss_only)21720194a95cSPing-Ke Shih static u8 _dpk_agc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
21730194a95cSPing-Ke Shih 		   enum rtw89_rf_path path, u8 kidx, u8 init_xdbm, u8 loss_only)
21740194a95cSPing-Ke Shih {
21750194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
21760194a95cSPing-Ke Shih 	u8 tmp_dbm = init_xdbm, tmp_gl_idx = 0;
21770194a95cSPing-Ke Shih 	u8 step = DPK_AGC_STEP_SYNC_DGAIN;
21780194a95cSPing-Ke Shih 	u8 goout = 0, agc_cnt = 0;
21790194a95cSPing-Ke Shih 	bool is_fail = false;
21800194a95cSPing-Ke Shih 	int limit = 200;
21810194a95cSPing-Ke Shih 	u8 tmp_rxbb;
21820194a95cSPing-Ke Shih 	u16 dgain;
21830194a95cSPing-Ke Shih 
21840194a95cSPing-Ke Shih 	do {
21850194a95cSPing-Ke Shih 		switch (step) {
21860194a95cSPing-Ke Shih 		case DPK_AGC_STEP_SYNC_DGAIN:
21870194a95cSPing-Ke Shih 			is_fail = _dpk_kip_set_rxagc(rtwdev, phy, path, kidx);
21880194a95cSPing-Ke Shih 
21890194a95cSPing-Ke Shih 			if (is_fail) {
21900194a95cSPing-Ke Shih 				goout = 1;
21910194a95cSPing-Ke Shih 				break;
21920194a95cSPing-Ke Shih 			}
21930194a95cSPing-Ke Shih 
21940194a95cSPing-Ke Shih 			dgain = _dpk_dgain_read(rtwdev);
21950194a95cSPing-Ke Shih 
21960194a95cSPing-Ke Shih 			if (dgain > 0x5fc || dgain < 0x556) {
21970194a95cSPing-Ke Shih 				_dpk_one_shot(rtwdev, phy, path, D_SYNC);
21980194a95cSPing-Ke Shih 				dgain = _dpk_dgain_read(rtwdev);
21990194a95cSPing-Ke Shih 			}
22000194a95cSPing-Ke Shih 
22010194a95cSPing-Ke Shih 			if (agc_cnt == 0) {
22020194a95cSPing-Ke Shih 				if (dpk->bp[path][kidx].band == RTW89_BAND_2G)
22030194a95cSPing-Ke Shih 					_dpk_bypass_rxiqc(rtwdev, path);
22040194a95cSPing-Ke Shih 				else
22050194a95cSPing-Ke Shih 					_dpk_lbk_rxiqk(rtwdev, phy, path);
22060194a95cSPing-Ke Shih 			}
22070194a95cSPing-Ke Shih 			step = DPK_AGC_STEP_GAIN_LOSS_IDX;
22080194a95cSPing-Ke Shih 			break;
22090194a95cSPing-Ke Shih 
22100194a95cSPing-Ke Shih 		case DPK_AGC_STEP_GAIN_LOSS_IDX:
22110194a95cSPing-Ke Shih 			tmp_gl_idx = _dpk_gainloss(rtwdev, phy, path, kidx);
22120194a95cSPing-Ke Shih 
22130194a95cSPing-Ke Shih 			if (_dpk_pas_read(rtwdev, true) == 2 && tmp_gl_idx > 0)
22140194a95cSPing-Ke Shih 				step = DPK_AGC_STEP_GL_LT_CRITERION;
22150194a95cSPing-Ke Shih 			else if ((tmp_gl_idx == 0 && _dpk_pas_read(rtwdev, true) == 1) ||
22160194a95cSPing-Ke Shih 				 tmp_gl_idx >= 7)
22170194a95cSPing-Ke Shih 				step = DPK_AGC_STEP_GL_GT_CRITERION;
22180194a95cSPing-Ke Shih 			else if (tmp_gl_idx == 0)
22190194a95cSPing-Ke Shih 				step = DPK_AGC_STEP_GL_LT_CRITERION;
22200194a95cSPing-Ke Shih 			else
22210194a95cSPing-Ke Shih 				step = DPK_AGC_STEP_SET_TX_GAIN;
22220194a95cSPing-Ke Shih 			break;
22230194a95cSPing-Ke Shih 
22240194a95cSPing-Ke Shih 		case DPK_AGC_STEP_GL_GT_CRITERION:
22250194a95cSPing-Ke Shih 			if (tmp_dbm <= 7) {
22260194a95cSPing-Ke Shih 				goout = 1;
22270194a95cSPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_RFK,
22280194a95cSPing-Ke Shih 					    "[DPK] Txagc@lower bound!!\n");
22290194a95cSPing-Ke Shih 			} else {
22300194a95cSPing-Ke Shih 				tmp_dbm = max_t(u8, tmp_dbm - 3, 7);
22310194a95cSPing-Ke Shih 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
22320194a95cSPing-Ke Shih 			}
22330194a95cSPing-Ke Shih 			step = DPK_AGC_STEP_SYNC_DGAIN;
22340194a95cSPing-Ke Shih 			agc_cnt++;
22350194a95cSPing-Ke Shih 			break;
22360194a95cSPing-Ke Shih 
22370194a95cSPing-Ke Shih 		case DPK_AGC_STEP_GL_LT_CRITERION:
22380194a95cSPing-Ke Shih 			if (tmp_dbm >= 24) {
22390194a95cSPing-Ke Shih 				goout = 1;
22400194a95cSPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_RFK,
22410194a95cSPing-Ke Shih 					    "[DPK] Txagc@upper bound!!\n");
22420194a95cSPing-Ke Shih 			} else {
22430194a95cSPing-Ke Shih 				tmp_dbm = min_t(u8, tmp_dbm + 2, 24);
22440194a95cSPing-Ke Shih 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
22450194a95cSPing-Ke Shih 			}
22460194a95cSPing-Ke Shih 			step = DPK_AGC_STEP_SYNC_DGAIN;
22470194a95cSPing-Ke Shih 			agc_cnt++;
22480194a95cSPing-Ke Shih 			break;
22490194a95cSPing-Ke Shih 
22500194a95cSPing-Ke Shih 		case DPK_AGC_STEP_SET_TX_GAIN:
22510194a95cSPing-Ke Shih 			_dpk_kip_control_rfc(rtwdev, path, false);
22520194a95cSPing-Ke Shih 			tmp_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_RXB);
22530194a95cSPing-Ke Shih 			tmp_rxbb = min_t(u8, tmp_rxbb + tmp_gl_idx, 0x1f);
22540194a95cSPing-Ke Shih 
22550194a95cSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_RXB, tmp_rxbb);
22560194a95cSPing-Ke Shih 
22570194a95cSPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK,
22580194a95cSPing-Ke Shih 				    "[DPK] Adjust RXBB (%+d) = 0x%x\n",
22590194a95cSPing-Ke Shih 				    tmp_gl_idx, tmp_rxbb);
22600194a95cSPing-Ke Shih 			_dpk_kip_control_rfc(rtwdev, path, true);
22610194a95cSPing-Ke Shih 			goout = 1;
22620194a95cSPing-Ke Shih 			break;
22630194a95cSPing-Ke Shih 		default:
22640194a95cSPing-Ke Shih 			goout = 1;
22650194a95cSPing-Ke Shih 			break;
22660194a95cSPing-Ke Shih 		}
22670194a95cSPing-Ke Shih 	} while (!goout && agc_cnt < 6 && limit-- > 0);
22680194a95cSPing-Ke Shih 
22690194a95cSPing-Ke Shih 	return is_fail;
22700194a95cSPing-Ke Shih }
22710194a95cSPing-Ke Shih 
_dpk_set_mdpd_para(struct rtw89_dev * rtwdev,u8 order)22720194a95cSPing-Ke Shih static void _dpk_set_mdpd_para(struct rtw89_dev *rtwdev, u8 order)
22730194a95cSPing-Ke Shih {
22740194a95cSPing-Ke Shih 	switch (order) {
22750194a95cSPing-Ke Shih 	case 0: /* (5,3,1) */
22760194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x0);
22770194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x2);
22780194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x4);
22790194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x1);
22800194a95cSPing-Ke Shih 		break;
22810194a95cSPing-Ke Shih 	case 1: /* (5,3,0) */
22820194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x1);
22830194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x1);
22840194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x0);
22850194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x0);
22860194a95cSPing-Ke Shih 		break;
22870194a95cSPing-Ke Shih 	case 2: /* (5,0,0) */
22880194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x2);
22890194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x0);
22900194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x0);
22910194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x0);
22920194a95cSPing-Ke Shih 		break;
22930194a95cSPing-Ke Shih 	case 3: /* (7,3,1) */
22940194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP, 0x3);
22950194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL_SEL, 0x3);
22960194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_PN, 0x4);
22970194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_MDPK_SYNC, B_MDPK_SYNC_DMAN, 0x1);
22980194a95cSPing-Ke Shih 		break;
22990194a95cSPing-Ke Shih 	default:
23000194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
23010194a95cSPing-Ke Shih 			    "[DPK] Wrong MDPD order!!(0x%x)\n", order);
23020194a95cSPing-Ke Shih 		break;
23030194a95cSPing-Ke Shih 	}
23040194a95cSPing-Ke Shih 
23050194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Set %s for IDL\n",
23060194a95cSPing-Ke Shih 		    order == 0x0 ? "(5,3,1)" :
23070194a95cSPing-Ke Shih 		    order == 0x1 ? "(5,3,0)" :
23080194a95cSPing-Ke Shih 		    order == 0x2 ? "(5,0,0)" : "(7,3,1)");
23090194a95cSPing-Ke Shih }
23100194a95cSPing-Ke Shih 
_dpk_idl_mpa(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)23110194a95cSPing-Ke Shih static void _dpk_idl_mpa(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
23120194a95cSPing-Ke Shih 			 enum rtw89_rf_path path, u8 kidx)
23130194a95cSPing-Ke Shih {
23140194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_MA, 0x1);
23150194a95cSPing-Ke Shih 
23160194a95cSPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_MD500) == 0x1)
23170194a95cSPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x2);
23180194a95cSPing-Ke Shih 	else if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_MD530) == 0x1)
23190194a95cSPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x1);
23200194a95cSPing-Ke Shih 	else
23210194a95cSPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x0);
23220194a95cSPing-Ke Shih 
23230194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL, 0x0);
23240194a95cSPing-Ke Shih 	fsleep(1000);
23250194a95cSPing-Ke Shih 
23260194a95cSPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
23270194a95cSPing-Ke Shih }
23280194a95cSPing-Ke Shih 
_dpk_order_convert(struct rtw89_dev * rtwdev)23290194a95cSPing-Ke Shih static u8 _dpk_order_convert(struct rtw89_dev *rtwdev)
23300194a95cSPing-Ke Shih {
23310194a95cSPing-Ke Shih 	u32 order;
23320194a95cSPing-Ke Shih 	u8 val;
23330194a95cSPing-Ke Shih 
23340194a95cSPing-Ke Shih 	order = rtw89_phy_read32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP);
23350194a95cSPing-Ke Shih 
23360194a95cSPing-Ke Shih 	switch (order) {
23370194a95cSPing-Ke Shih 	case 0: /* (5,3,1) */
23380194a95cSPing-Ke Shih 		val = 0x6;
23390194a95cSPing-Ke Shih 		break;
23400194a95cSPing-Ke Shih 	case 1: /* (5,3,0) */
23410194a95cSPing-Ke Shih 		val = 0x2;
23420194a95cSPing-Ke Shih 		break;
23430194a95cSPing-Ke Shih 	case 2: /* (5,0,0) */
23440194a95cSPing-Ke Shih 		val = 0x0;
23450194a95cSPing-Ke Shih 		break;
23460194a95cSPing-Ke Shih 	default:
23470194a95cSPing-Ke Shih 		val = 0xff;
23480194a95cSPing-Ke Shih 		break;
23490194a95cSPing-Ke Shih 	}
23500194a95cSPing-Ke Shih 
23510194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] convert MDPD order to 0x%x\n", val);
23520194a95cSPing-Ke Shih 
23530194a95cSPing-Ke Shih 	return val;
23540194a95cSPing-Ke Shih }
23550194a95cSPing-Ke Shih 
_dpk_gain_normalize(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx,bool is_execute)23560194a95cSPing-Ke Shih static void _dpk_gain_normalize(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
23570194a95cSPing-Ke Shih 				enum rtw89_rf_path path, u8 kidx, bool is_execute)
23580194a95cSPing-Ke Shih {
23590194a95cSPing-Ke Shih 	static const u32 reg[RTW89_DPK_BKUP_NUM][DPK_KSET_NUM] = {
23600194a95cSPing-Ke Shih 		{0x8190, 0x8194, 0x8198, 0x81a4},
23610194a95cSPing-Ke Shih 		{0x81a8, 0x81c4, 0x81c8, 0x81e8}
23620194a95cSPing-Ke Shih 	};
23630194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
23640194a95cSPing-Ke Shih 	u8 cur_k_set = dpk->cur_k_set;
23650194a95cSPing-Ke Shih 
23660194a95cSPing-Ke Shih 	if (cur_k_set >= DPK_KSET_NUM) {
23670194a95cSPing-Ke Shih 		rtw89_warn(rtwdev, "DPK cur_k_set = %d\n", cur_k_set);
23680194a95cSPing-Ke Shih 		cur_k_set = 2;
23690194a95cSPing-Ke Shih 	}
23700194a95cSPing-Ke Shih 
23710194a95cSPing-Ke Shih 	if (is_execute) {
23720194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8),
23730194a95cSPing-Ke Shih 				       B_DPK_GN_AG, 0x200);
23740194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8),
23750194a95cSPing-Ke Shih 				       B_DPK_GN_EN, 0x3);
23760194a95cSPing-Ke Shih 
23770194a95cSPing-Ke Shih 		_dpk_one_shot(rtwdev, phy, path, D_GAIN_NORM);
23780194a95cSPing-Ke Shih 	} else {
23790194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
23800194a95cSPing-Ke Shih 				       0x0000007F, 0x5b);
23810194a95cSPing-Ke Shih 	}
23820194a95cSPing-Ke Shih 
23830194a95cSPing-Ke Shih 	dpk->bp[path][kidx].gs =
23840194a95cSPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, reg[kidx][cur_k_set] + (path << 8),
23850194a95cSPing-Ke Shih 				      0x0000007F);
23860194a95cSPing-Ke Shih }
23870194a95cSPing-Ke Shih 
_dpk_on(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)23880194a95cSPing-Ke Shih static void _dpk_on(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
23890194a95cSPing-Ke Shih 		    enum rtw89_rf_path path, u8 kidx)
23900194a95cSPing-Ke Shih {
23910194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
23920194a95cSPing-Ke Shih 
23930194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x1);
23940194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x0);
23950194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
23960194a95cSPing-Ke Shih 			       B_DPD_ORDER, _dpk_order_convert(rtwdev));
23970194a95cSPing-Ke Shih 
23980194a95cSPing-Ke Shih 	dpk->bp[path][kidx].path_ok =
23990194a95cSPing-Ke Shih 		dpk->bp[path][kidx].path_ok | BIT(dpk->cur_k_set);
24000194a95cSPing-Ke Shih 
24010194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] path_ok = 0x%x\n",
24020194a95cSPing-Ke Shih 		    path, kidx, dpk->bp[path][kidx].path_ok);
24030194a95cSPing-Ke Shih 
24040194a95cSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
24050194a95cSPing-Ke Shih 			       B_DPD_MEN, dpk->bp[path][kidx].path_ok);
24060194a95cSPing-Ke Shih 
24070194a95cSPing-Ke Shih 	_dpk_gain_normalize(rtwdev, phy, path, kidx, false);
24080194a95cSPing-Ke Shih }
24090194a95cSPing-Ke Shih 
_dpk_main(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)24100194a95cSPing-Ke Shih static bool _dpk_main(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
24110194a95cSPing-Ke Shih 		      enum rtw89_rf_path path)
24120194a95cSPing-Ke Shih {
24130194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
24140194a95cSPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
24150194a95cSPing-Ke Shih 	u8 init_xdbm = 17;
24160194a95cSPing-Ke Shih 	bool is_fail;
24170194a95cSPing-Ke Shih 
24180194a95cSPing-Ke Shih 	if (dpk->bp[path][kidx].band != RTW89_BAND_2G)
24190194a95cSPing-Ke Shih 		init_xdbm = 15;
24200194a95cSPing-Ke Shih 
24210194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
24220194a95cSPing-Ke Shih 	_rfk_rf_direct_cntrl(rtwdev, path, false);
24230194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_BBDC, RFREG_MASK, 0x03ffd);
24240194a95cSPing-Ke Shih 
24250194a95cSPing-Ke Shih 	_dpk_rf_setting(rtwdev, path, kidx);
24260194a95cSPing-Ke Shih 	_set_rx_dck(rtwdev, path, RF_DPK);
24270194a95cSPing-Ke Shih 
24280194a95cSPing-Ke Shih 	_dpk_kip_pwr_clk_onoff(rtwdev, true);
24290194a95cSPing-Ke Shih 	_dpk_kip_preset(rtwdev, phy, path, kidx);
24300194a95cSPing-Ke Shih 	_dpk_txpwr_bb_force(rtwdev, path, true);
24310194a95cSPing-Ke Shih 	_dpk_kip_set_txagc(rtwdev, phy, path, init_xdbm, true);
24320194a95cSPing-Ke Shih 	_dpk_tpg_sel(rtwdev, path, kidx);
24330194a95cSPing-Ke Shih 	is_fail = _dpk_agc(rtwdev, phy, path, kidx, init_xdbm, false);
24340194a95cSPing-Ke Shih 	if (is_fail)
24350194a95cSPing-Ke Shih 		goto _error;
24360194a95cSPing-Ke Shih 
24370194a95cSPing-Ke Shih 	_dpk_idl_mpa(rtwdev, phy, path, kidx);
24380194a95cSPing-Ke Shih 	_dpk_para_query(rtwdev, path, kidx);
24390194a95cSPing-Ke Shih 
24400194a95cSPing-Ke Shih 	_dpk_on(rtwdev, phy, path, kidx);
24410194a95cSPing-Ke Shih _error:
24420194a95cSPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
24430194a95cSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
24440194a95cSPing-Ke Shih 
24450194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d]_K%d %s\n", path, kidx,
24460194a95cSPing-Ke Shih 		    dpk->cur_k_set, is_fail ? "need Check" : "is Success");
24470194a95cSPing-Ke Shih 
24480194a95cSPing-Ke Shih 	return is_fail;
24490194a95cSPing-Ke Shih }
24500194a95cSPing-Ke Shih 
_dpk_cal_select(struct rtw89_dev * rtwdev,bool force,enum rtw89_phy_idx phy,u8 kpath)24510194a95cSPing-Ke Shih static void _dpk_cal_select(struct rtw89_dev *rtwdev, bool force,
24520194a95cSPing-Ke Shih 			    enum rtw89_phy_idx phy, u8 kpath)
24530194a95cSPing-Ke Shih {
24540194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
24550194a95cSPing-Ke Shih 	u32 kip_bkup[RF_PATH_NUM_8851B][DPK_KIP_REG_NUM_8851B] = {};
24560194a95cSPing-Ke Shih 	u32 rf_bkup[RF_PATH_NUM_8851B][DPK_RF_REG_NUM_8851B] = {};
24570194a95cSPing-Ke Shih 	bool is_fail;
24580194a95cSPing-Ke Shih 	u8 path;
24590194a95cSPing-Ke Shih 
24600194a95cSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++)
24610194a95cSPing-Ke Shih 		dpk->cur_idx[path] = 0;
24620194a95cSPing-Ke Shih 
24630194a95cSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
24640194a95cSPing-Ke Shih 		if (!(kpath & BIT(path)))
24650194a95cSPing-Ke Shih 			continue;
24660194a95cSPing-Ke Shih 		_dpk_bkup_kip(rtwdev, dpk_kip_reg, kip_bkup, path);
24670194a95cSPing-Ke Shih 		_dpk_bkup_rf(rtwdev, dpk_rf_reg, rf_bkup, path);
24680194a95cSPing-Ke Shih 		_dpk_information(rtwdev, phy, path);
24690194a95cSPing-Ke Shih 		_dpk_init(rtwdev, path);
24700194a95cSPing-Ke Shih 
24710194a95cSPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
24720194a95cSPing-Ke Shih 			_dpk_tssi_pause(rtwdev, path, true);
24730194a95cSPing-Ke Shih 	}
24740194a95cSPing-Ke Shih 
24750194a95cSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
24760194a95cSPing-Ke Shih 		if (!(kpath & BIT(path)))
24770194a95cSPing-Ke Shih 			continue;
24780194a95cSPing-Ke Shih 
24790194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
24800194a95cSPing-Ke Shih 			    "[DPK] ========= S%d[%d] DPK Start =========\n",
24810194a95cSPing-Ke Shih 			    path, dpk->cur_idx[path]);
24820194a95cSPing-Ke Shih 
24830194a95cSPing-Ke Shih 		_dpk_rxagc_onoff(rtwdev, path, false);
24840194a95cSPing-Ke Shih 		_rfk_drf_direct_cntrl(rtwdev, path, false);
24850194a95cSPing-Ke Shih 		_dpk_bb_afe_setting(rtwdev, path);
24860194a95cSPing-Ke Shih 
24870194a95cSPing-Ke Shih 		is_fail = _dpk_main(rtwdev, phy, path);
24880194a95cSPing-Ke Shih 		_dpk_onoff(rtwdev, path, is_fail);
24890194a95cSPing-Ke Shih 	}
24900194a95cSPing-Ke Shih 
24910194a95cSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
24920194a95cSPing-Ke Shih 		if (!(kpath & BIT(path)))
24930194a95cSPing-Ke Shih 			continue;
24940194a95cSPing-Ke Shih 
24950194a95cSPing-Ke Shih 		_dpk_kip_restore(rtwdev, phy, path);
24960194a95cSPing-Ke Shih 		_dpk_reload_kip(rtwdev, dpk_kip_reg, kip_bkup, path);
24970194a95cSPing-Ke Shih 		_dpk_reload_rf(rtwdev, dpk_rf_reg, rf_bkup, path);
24980194a95cSPing-Ke Shih 		_dpk_bb_afe_restore(rtwdev, path);
24990194a95cSPing-Ke Shih 		_dpk_rxagc_onoff(rtwdev, path, true);
25000194a95cSPing-Ke Shih 
25010194a95cSPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
25020194a95cSPing-Ke Shih 			_dpk_tssi_pause(rtwdev, path, false);
25030194a95cSPing-Ke Shih 	}
25040194a95cSPing-Ke Shih 
25050194a95cSPing-Ke Shih 	_dpk_kip_pwr_clk_onoff(rtwdev, false);
25060194a95cSPing-Ke Shih }
25070194a95cSPing-Ke Shih 
_dpk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool force)25080194a95cSPing-Ke Shih static void _dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool force)
25090194a95cSPing-Ke Shih {
25100194a95cSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
25110194a95cSPing-Ke Shih 		    "[DPK] ****** 8851B DPK Start (Ver: 0x%x, Cv: %d) ******\n",
25120194a95cSPing-Ke Shih 		    DPK_VER_8851B, rtwdev->hal.cv);
25130194a95cSPing-Ke Shih 
25140194a95cSPing-Ke Shih 	_dpk_cal_select(rtwdev, force, phy, _kpath(rtwdev, phy));
25150194a95cSPing-Ke Shih }
25160194a95cSPing-Ke Shih 
_dpk_track(struct rtw89_dev * rtwdev)25170194a95cSPing-Ke Shih static void _dpk_track(struct rtw89_dev *rtwdev)
25180194a95cSPing-Ke Shih {
25190194a95cSPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
25200194a95cSPing-Ke Shih 	s8 txagc_bb, txagc_bb_tp, txagc_ofst;
25210194a95cSPing-Ke Shih 	s16 pwsf_tssi_ofst;
25220194a95cSPing-Ke Shih 	s8 delta_ther = 0;
25230194a95cSPing-Ke Shih 	u8 path, kidx;
25240194a95cSPing-Ke Shih 	u8 txagc_rf;
25250194a95cSPing-Ke Shih 	u8 cur_ther;
25260194a95cSPing-Ke Shih 
25270194a95cSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
25280194a95cSPing-Ke Shih 		kidx = dpk->cur_idx[path];
25290194a95cSPing-Ke Shih 
25300194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25310194a95cSPing-Ke Shih 			    "[DPK_TRK] ================[S%d[%d] (CH %d)]================\n",
25320194a95cSPing-Ke Shih 			    path, kidx, dpk->bp[path][kidx].ch);
25330194a95cSPing-Ke Shih 
25340194a95cSPing-Ke Shih 		txagc_rf = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13),
25350194a95cSPing-Ke Shih 						 B_TXAGC_RF);
25360194a95cSPing-Ke Shih 		txagc_bb = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13),
25370194a95cSPing-Ke Shih 						 MASKBYTE2);
25380194a95cSPing-Ke Shih 		txagc_bb_tp = rtw89_phy_read32_mask(rtwdev, R_TXAGC_BTP + (path << 13),
25390194a95cSPing-Ke Shih 						    B_TXAGC_BTP);
25400194a95cSPing-Ke Shih 
25410194a95cSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8),
25420194a95cSPing-Ke Shih 				       B_KIP_RPT_SEL, 0xf);
25430194a95cSPing-Ke Shih 		cur_ther = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
25440194a95cSPing-Ke Shih 						 B_RPT_PER_TH);
25450194a95cSPing-Ke Shih 		txagc_ofst = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
25460194a95cSPing-Ke Shih 						   B_RPT_PER_OF);
25470194a95cSPing-Ke Shih 		pwsf_tssi_ofst = rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8),
25480194a95cSPing-Ke Shih 						       B_RPT_PER_TSSI);
25490194a95cSPing-Ke Shih 		pwsf_tssi_ofst = sign_extend32(pwsf_tssi_ofst, 12);
25500194a95cSPing-Ke Shih 
25510194a95cSPing-Ke Shih 		delta_ther = cur_ther - dpk->bp[path][kidx].ther_dpk;
25520194a95cSPing-Ke Shih 
25530194a95cSPing-Ke Shih 		delta_ther = delta_ther * 2 / 3;
25540194a95cSPing-Ke Shih 
25550194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25560194a95cSPing-Ke Shih 			    "[DPK_TRK] extra delta_ther = %d (0x%x / 0x%x@k)\n",
25570194a95cSPing-Ke Shih 			    delta_ther, cur_ther, dpk->bp[path][kidx].ther_dpk);
25580194a95cSPing-Ke Shih 
25590194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25600194a95cSPing-Ke Shih 			    "[DPK_TRK] delta_txagc = %d (0x%x / 0x%x@k)\n",
25610194a95cSPing-Ke Shih 			    txagc_rf - dpk->bp[path][kidx].txagc_dpk,
25620194a95cSPing-Ke Shih 			    txagc_rf, dpk->bp[path][kidx].txagc_dpk);
25630194a95cSPing-Ke Shih 
25640194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25650194a95cSPing-Ke Shih 			    "[DPK_TRK] txagc_offset / pwsf_tssi_ofst = 0x%x / %+d\n",
25660194a95cSPing-Ke Shih 			    txagc_ofst, pwsf_tssi_ofst);
25670194a95cSPing-Ke Shih 
25680194a95cSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25690194a95cSPing-Ke Shih 			    "[DPK_TRK] txagc_bb_tp / txagc_bb = 0x%x / 0x%x\n",
25700194a95cSPing-Ke Shih 			    txagc_bb_tp, txagc_bb);
25710194a95cSPing-Ke Shih 
25720194a95cSPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_IDL_MPA, B_IDL_DN) == 0x0 &&
25730194a95cSPing-Ke Shih 		    txagc_rf != 0) {
25740194a95cSPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
25750194a95cSPing-Ke Shih 				    "[DPK_TRK] New pwsf = 0x%x\n", 0x78 - delta_ther);
25760194a95cSPing-Ke Shih 
25770194a95cSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
25780194a95cSPing-Ke Shih 					       R_DPD_BND + (path << 8) + (kidx << 2),
25790194a95cSPing-Ke Shih 					       0x07FC0000, 0x78 - delta_ther);
25800194a95cSPing-Ke Shih 		}
25810194a95cSPing-Ke Shih 	}
25820194a95cSPing-Ke Shih }
25830194a95cSPing-Ke Shih 
_rck(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)2584ae546f0aSPing-Ke Shih static void _rck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
2585ae546f0aSPing-Ke Shih {
2586ae546f0aSPing-Ke Shih 	u32 rf_reg5;
2587ae546f0aSPing-Ke Shih 	u32 rck_val;
2588ae546f0aSPing-Ke Shih 	u32 val;
2589ae546f0aSPing-Ke Shih 	int ret;
2590ae546f0aSPing-Ke Shih 
2591ae546f0aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] ====== S%d RCK ======\n", path);
2592ae546f0aSPing-Ke Shih 
2593ae546f0aSPing-Ke Shih 	rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
2594ae546f0aSPing-Ke Shih 
2595ae546f0aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
2596ae546f0aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
2597ae546f0aSPing-Ke Shih 
2598ae546f0aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF0x00 = 0x%05x\n",
2599ae546f0aSPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
2600ae546f0aSPing-Ke Shih 
2601ae546f0aSPing-Ke Shih 	/* RCK trigger */
2602ae546f0aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, 0x00240);
2603ae546f0aSPing-Ke Shih 
2604ae546f0aSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val, 2, 30,
2605ae546f0aSPing-Ke Shih 				       false, rtwdev, path, RR_RCKS, BIT(3));
2606ae546f0aSPing-Ke Shih 
2607ae546f0aSPing-Ke Shih 	rck_val = rtw89_read_rf(rtwdev, path, RR_RCKC, RR_RCKC_CA);
2608ae546f0aSPing-Ke Shih 
2609ae546f0aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] rck_val = 0x%x, ret = %d\n",
2610ae546f0aSPing-Ke Shih 		    rck_val, ret);
2611ae546f0aSPing-Ke Shih 
2612ae546f0aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, rck_val);
2613ae546f0aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
2614ae546f0aSPing-Ke Shih 
2615ae546f0aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF 0x1b = 0x%x\n",
2616ae546f0aSPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_RCKC, RFREG_MASK));
2617ae546f0aSPing-Ke Shih }
2618ae546f0aSPing-Ke Shih 
_tssi_set_sys(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)26193f2da9fcSPing-Ke Shih static void _tssi_set_sys(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
26203f2da9fcSPing-Ke Shih 			  enum rtw89_rf_path path)
26213f2da9fcSPing-Ke Shih {
26223f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
26233f2da9fcSPing-Ke Shih 	enum rtw89_band band = chan->band_type;
26243f2da9fcSPing-Ke Shih 
26253f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_sys_defs_tbl);
26263f2da9fcSPing-Ke Shih 
26273f2da9fcSPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
26283f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_sys_a_defs_2g_tbl,
26293f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_sys_a_defs_5g_tbl);
26303f2da9fcSPing-Ke Shih }
26313f2da9fcSPing-Ke Shih 
_tssi_ini_txpwr_ctrl_bb(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)26323f2da9fcSPing-Ke Shih static void _tssi_ini_txpwr_ctrl_bb(struct rtw89_dev *rtwdev,
26333f2da9fcSPing-Ke Shih 				    enum rtw89_phy_idx phy,
26343f2da9fcSPing-Ke Shih 				    enum rtw89_rf_path path)
26353f2da9fcSPing-Ke Shih {
26363f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_init_txpwr_defs_a_tbl);
26373f2da9fcSPing-Ke Shih }
26383f2da9fcSPing-Ke Shih 
_tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)26393f2da9fcSPing-Ke Shih static void _tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev *rtwdev,
26403f2da9fcSPing-Ke Shih 					  enum rtw89_phy_idx phy,
26413f2da9fcSPing-Ke Shih 					  enum rtw89_rf_path path)
26423f2da9fcSPing-Ke Shih {
26433f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_init_txpwr_he_tb_defs_a_tbl);
26443f2da9fcSPing-Ke Shih }
26453f2da9fcSPing-Ke Shih 
_tssi_set_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)26463f2da9fcSPing-Ke Shih static void _tssi_set_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
26473f2da9fcSPing-Ke Shih 			  enum rtw89_rf_path path)
26483f2da9fcSPing-Ke Shih {
26493f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_dck_defs_a_tbl);
26503f2da9fcSPing-Ke Shih }
26513f2da9fcSPing-Ke Shih 
_tssi_set_tmeter_tbl(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)26523f2da9fcSPing-Ke Shih static void _tssi_set_tmeter_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
26533f2da9fcSPing-Ke Shih 				 enum rtw89_rf_path path)
26543f2da9fcSPing-Ke Shih {
26553f2da9fcSPing-Ke Shih #define RTW8851B_TSSI_GET_VAL(ptr, idx)			\
26563f2da9fcSPing-Ke Shih ({							\
26573f2da9fcSPing-Ke Shih 	s8 *__ptr = (ptr);				\
26583f2da9fcSPing-Ke Shih 	u8 __idx = (idx), __i, __v;			\
26593f2da9fcSPing-Ke Shih 	u32 __val = 0;					\
26603f2da9fcSPing-Ke Shih 	for (__i = 0; __i < 4; __i++) {			\
26613f2da9fcSPing-Ke Shih 		__v = (__ptr[__idx + __i]);		\
26623f2da9fcSPing-Ke Shih 		__val |= (__v << (8 * __i));		\
26633f2da9fcSPing-Ke Shih 	}						\
26643f2da9fcSPing-Ke Shih 	__val;						\
26653f2da9fcSPing-Ke Shih })
26663f2da9fcSPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
26673f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
26683f2da9fcSPing-Ke Shih 	u8 ch = chan->channel;
26693f2da9fcSPing-Ke Shih 	u8 subband = chan->subband_type;
26703f2da9fcSPing-Ke Shih 	const s8 *thm_up_a = NULL;
26713f2da9fcSPing-Ke Shih 	const s8 *thm_down_a = NULL;
26723f2da9fcSPing-Ke Shih 	u8 thermal = 0xff;
26733f2da9fcSPing-Ke Shih 	s8 thm_ofst[64] = {0};
26743f2da9fcSPing-Ke Shih 	u32 tmp = 0;
26753f2da9fcSPing-Ke Shih 	u8 i, j;
26763f2da9fcSPing-Ke Shih 
26773f2da9fcSPing-Ke Shih 	switch (subband) {
26783f2da9fcSPing-Ke Shih 	default:
26793f2da9fcSPing-Ke Shih 	case RTW89_CH_2G:
26803f2da9fcSPing-Ke Shih 		thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_2ga_p;
26813f2da9fcSPing-Ke Shih 		thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_2ga_n;
26823f2da9fcSPing-Ke Shih 		break;
26833f2da9fcSPing-Ke Shih 	case RTW89_CH_5G_BAND_1:
26843f2da9fcSPing-Ke Shih 		thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[0];
26853f2da9fcSPing-Ke Shih 		thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[0];
26863f2da9fcSPing-Ke Shih 		break;
26873f2da9fcSPing-Ke Shih 	case RTW89_CH_5G_BAND_3:
26883f2da9fcSPing-Ke Shih 		thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[1];
26893f2da9fcSPing-Ke Shih 		thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[1];
26903f2da9fcSPing-Ke Shih 		break;
26913f2da9fcSPing-Ke Shih 	case RTW89_CH_5G_BAND_4:
26923f2da9fcSPing-Ke Shih 		thm_up_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_p[2];
26933f2da9fcSPing-Ke Shih 		thm_down_a = rtw89_8851b_trk_cfg.delta_swingidx_5ga_n[2];
26943f2da9fcSPing-Ke Shih 		break;
26953f2da9fcSPing-Ke Shih 	}
26963f2da9fcSPing-Ke Shih 
26973f2da9fcSPing-Ke Shih 	if (path == RF_PATH_A) {
26983f2da9fcSPing-Ke Shih 		thermal = tssi_info->thermal[RF_PATH_A];
26993f2da9fcSPing-Ke Shih 
27003f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
27013f2da9fcSPing-Ke Shih 			    "[TSSI] ch=%d thermal_pathA=0x%x\n", ch, thermal);
27023f2da9fcSPing-Ke Shih 
27033f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_DIS, 0x0);
27043f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_TRK, 0x1);
27053f2da9fcSPing-Ke Shih 
27063f2da9fcSPing-Ke Shih 		if (thermal == 0xff) {
27073f2da9fcSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, 32);
27083f2da9fcSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL, 32);
27093f2da9fcSPing-Ke Shih 
27103f2da9fcSPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
27113f2da9fcSPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, 0x0);
27123f2da9fcSPing-Ke Shih 
27133f2da9fcSPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
27143f2da9fcSPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
27153f2da9fcSPing-Ke Shih 					    R_P0_TSSI_BASE + i, 0x0);
27163f2da9fcSPing-Ke Shih 			}
27173f2da9fcSPing-Ke Shih 
27183f2da9fcSPing-Ke Shih 		} else {
27193f2da9fcSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER,
27203f2da9fcSPing-Ke Shih 					       thermal);
27213f2da9fcSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL,
27223f2da9fcSPing-Ke Shih 					       thermal);
27233f2da9fcSPing-Ke Shih 
27243f2da9fcSPing-Ke Shih 			i = 0;
27253f2da9fcSPing-Ke Shih 			for (j = 0; j < 32; j++)
27263f2da9fcSPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
27273f2da9fcSPing-Ke Shih 					      -thm_down_a[i++] :
27283f2da9fcSPing-Ke Shih 					      -thm_down_a[DELTA_SWINGIDX_SIZE - 1];
27293f2da9fcSPing-Ke Shih 
27303f2da9fcSPing-Ke Shih 			i = 1;
27313f2da9fcSPing-Ke Shih 			for (j = 63; j >= 32; j--)
27323f2da9fcSPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
27333f2da9fcSPing-Ke Shih 					      thm_up_a[i++] :
27343f2da9fcSPing-Ke Shih 					      thm_up_a[DELTA_SWINGIDX_SIZE - 1];
27353f2da9fcSPing-Ke Shih 
27363f2da9fcSPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
27373f2da9fcSPing-Ke Shih 				tmp = RTW8851B_TSSI_GET_VAL(thm_ofst, i);
27383f2da9fcSPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, tmp);
27393f2da9fcSPing-Ke Shih 
27403f2da9fcSPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
27413f2da9fcSPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
27423f2da9fcSPing-Ke Shih 					    0x5c00 + i, tmp);
27433f2da9fcSPing-Ke Shih 			}
27443f2da9fcSPing-Ke Shih 		}
27453f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x1);
27463f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x0);
27473f2da9fcSPing-Ke Shih 	}
27483f2da9fcSPing-Ke Shih #undef RTW8851B_TSSI_GET_VAL
27493f2da9fcSPing-Ke Shih }
27503f2da9fcSPing-Ke Shih 
_tssi_set_dac_gain_tbl(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)27513f2da9fcSPing-Ke Shih static void _tssi_set_dac_gain_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
27523f2da9fcSPing-Ke Shih 				   enum rtw89_rf_path path)
27533f2da9fcSPing-Ke Shih {
27543f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_dac_gain_defs_a_tbl);
27553f2da9fcSPing-Ke Shih }
27563f2da9fcSPing-Ke Shih 
_tssi_slope_cal_org(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)27573f2da9fcSPing-Ke Shih static void _tssi_slope_cal_org(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
27583f2da9fcSPing-Ke Shih 				enum rtw89_rf_path path)
27593f2da9fcSPing-Ke Shih {
27603f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
27613f2da9fcSPing-Ke Shih 	enum rtw89_band band = chan->band_type;
27623f2da9fcSPing-Ke Shih 
27633f2da9fcSPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
27643f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_slope_a_defs_2g_tbl,
27653f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_slope_a_defs_5g_tbl);
27663f2da9fcSPing-Ke Shih }
27673f2da9fcSPing-Ke Shih 
_tssi_alignment_default(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,bool all)27683f2da9fcSPing-Ke Shih static void _tssi_alignment_default(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
27693f2da9fcSPing-Ke Shih 				    enum rtw89_rf_path path, bool all)
27703f2da9fcSPing-Ke Shih {
27713f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
27723f2da9fcSPing-Ke Shih 	enum rtw89_band band = chan->band_type;
27733f2da9fcSPing-Ke Shih 
27743f2da9fcSPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
27753f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_align_a_2g_defs_tbl,
27763f2da9fcSPing-Ke Shih 				 &rtw8851b_tssi_align_a_5g_defs_tbl);
27773f2da9fcSPing-Ke Shih }
27783f2da9fcSPing-Ke Shih 
_tssi_set_tssi_slope(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)27793f2da9fcSPing-Ke Shih static void _tssi_set_tssi_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
27803f2da9fcSPing-Ke Shih 				 enum rtw89_rf_path path)
27813f2da9fcSPing-Ke Shih {
27823f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_slope_defs_a_tbl);
27833f2da9fcSPing-Ke Shih }
27843f2da9fcSPing-Ke Shih 
_tssi_set_tssi_track(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)27853f2da9fcSPing-Ke Shih static void _tssi_set_tssi_track(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
27863f2da9fcSPing-Ke Shih 				 enum rtw89_rf_path path)
27873f2da9fcSPing-Ke Shih {
27883f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_track_defs_a_tbl);
27893f2da9fcSPing-Ke Shih }
27903f2da9fcSPing-Ke Shih 
_tssi_set_txagc_offset_mv_avg(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)27913f2da9fcSPing-Ke Shih static void _tssi_set_txagc_offset_mv_avg(struct rtw89_dev *rtwdev,
27923f2da9fcSPing-Ke Shih 					  enum rtw89_phy_idx phy,
27933f2da9fcSPing-Ke Shih 					  enum rtw89_rf_path path)
27943f2da9fcSPing-Ke Shih {
27953f2da9fcSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8851b_tssi_mv_avg_defs_a_tbl);
27963f2da9fcSPing-Ke Shih }
27973f2da9fcSPing-Ke Shih 
_tssi_enable(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)27983f2da9fcSPing-Ke Shih static void _tssi_enable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
27993f2da9fcSPing-Ke Shih {
28003f2da9fcSPing-Ke Shih 	_tssi_set_tssi_track(rtwdev, phy, RF_PATH_A);
28013f2da9fcSPing-Ke Shih 	_tssi_set_txagc_offset_mv_avg(rtwdev, phy, RF_PATH_A);
28023f2da9fcSPing-Ke Shih 
28033f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_MV_AVG, B_P0_TSSI_MV_CLR, 0x0);
28043f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x0);
28053f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x1);
28063f2da9fcSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_TXGA_V1, RR_TXGA_V1_TRK_EN, 0x1);
28073f2da9fcSPing-Ke Shih 
28083f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
28093f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_RFC, 0x3);
28103f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT, 0xc0);
28113f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
28123f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
28133f2da9fcSPing-Ke Shih 
28143f2da9fcSPing-Ke Shih 	rtwdev->is_tssi_mode[RF_PATH_A] = true;
28153f2da9fcSPing-Ke Shih }
28163f2da9fcSPing-Ke Shih 
_tssi_disable(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)28173f2da9fcSPing-Ke Shih static void _tssi_disable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
28183f2da9fcSPing-Ke Shih {
28193f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_AVG, B_P0_TSSI_EN, 0x0);
28203f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
28213f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
28223f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
28233f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_MV_AVG, B_P0_TSSI_MV_CLR, 0x1);
28243f2da9fcSPing-Ke Shih 
28253f2da9fcSPing-Ke Shih 	rtwdev->is_tssi_mode[RF_PATH_A] = false;
28263f2da9fcSPing-Ke Shih }
28273f2da9fcSPing-Ke Shih 
_tssi_get_cck_group(struct rtw89_dev * rtwdev,u8 ch)28283f2da9fcSPing-Ke Shih static u32 _tssi_get_cck_group(struct rtw89_dev *rtwdev, u8 ch)
28293f2da9fcSPing-Ke Shih {
28303f2da9fcSPing-Ke Shih 	switch (ch) {
28313f2da9fcSPing-Ke Shih 	case 1 ... 2:
28323f2da9fcSPing-Ke Shih 		return 0;
28333f2da9fcSPing-Ke Shih 	case 3 ... 5:
28343f2da9fcSPing-Ke Shih 		return 1;
28353f2da9fcSPing-Ke Shih 	case 6 ... 8:
28363f2da9fcSPing-Ke Shih 		return 2;
28373f2da9fcSPing-Ke Shih 	case 9 ... 11:
28383f2da9fcSPing-Ke Shih 		return 3;
28393f2da9fcSPing-Ke Shih 	case 12 ... 13:
28403f2da9fcSPing-Ke Shih 		return 4;
28413f2da9fcSPing-Ke Shih 	case 14:
28423f2da9fcSPing-Ke Shih 		return 5;
28433f2da9fcSPing-Ke Shih 	}
28443f2da9fcSPing-Ke Shih 
28453f2da9fcSPing-Ke Shih 	return 0;
28463f2da9fcSPing-Ke Shih }
28473f2da9fcSPing-Ke Shih 
28483f2da9fcSPing-Ke Shih #define TSSI_EXTRA_GROUP_BIT (BIT(31))
28493f2da9fcSPing-Ke Shih #define TSSI_EXTRA_GROUP(idx) (TSSI_EXTRA_GROUP_BIT | (idx))
28503f2da9fcSPing-Ke Shih #define IS_TSSI_EXTRA_GROUP(group) ((group) & TSSI_EXTRA_GROUP_BIT)
28513f2da9fcSPing-Ke Shih #define TSSI_EXTRA_GET_GROUP_IDX1(group) ((group) & ~TSSI_EXTRA_GROUP_BIT)
28523f2da9fcSPing-Ke Shih #define TSSI_EXTRA_GET_GROUP_IDX2(group) (TSSI_EXTRA_GET_GROUP_IDX1(group) + 1)
28533f2da9fcSPing-Ke Shih 
_tssi_get_ofdm_group(struct rtw89_dev * rtwdev,u8 ch)28543f2da9fcSPing-Ke Shih static u32 _tssi_get_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
28553f2da9fcSPing-Ke Shih {
28563f2da9fcSPing-Ke Shih 	switch (ch) {
28573f2da9fcSPing-Ke Shih 	case 1 ... 2:
28583f2da9fcSPing-Ke Shih 		return 0;
28593f2da9fcSPing-Ke Shih 	case 3 ... 5:
28603f2da9fcSPing-Ke Shih 		return 1;
28613f2da9fcSPing-Ke Shih 	case 6 ... 8:
28623f2da9fcSPing-Ke Shih 		return 2;
28633f2da9fcSPing-Ke Shih 	case 9 ... 11:
28643f2da9fcSPing-Ke Shih 		return 3;
28653f2da9fcSPing-Ke Shih 	case 12 ... 14:
28663f2da9fcSPing-Ke Shih 		return 4;
28673f2da9fcSPing-Ke Shih 	case 36 ... 40:
28683f2da9fcSPing-Ke Shih 		return 5;
28693f2da9fcSPing-Ke Shih 	case 41 ... 43:
28703f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(5);
28713f2da9fcSPing-Ke Shih 	case 44 ... 48:
28723f2da9fcSPing-Ke Shih 		return 6;
28733f2da9fcSPing-Ke Shih 	case 49 ... 51:
28743f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(6);
28753f2da9fcSPing-Ke Shih 	case 52 ... 56:
28763f2da9fcSPing-Ke Shih 		return 7;
28773f2da9fcSPing-Ke Shih 	case 57 ... 59:
28783f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(7);
28793f2da9fcSPing-Ke Shih 	case 60 ... 64:
28803f2da9fcSPing-Ke Shih 		return 8;
28813f2da9fcSPing-Ke Shih 	case 100 ... 104:
28823f2da9fcSPing-Ke Shih 		return 9;
28833f2da9fcSPing-Ke Shih 	case 105 ... 107:
28843f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(9);
28853f2da9fcSPing-Ke Shih 	case 108 ... 112:
28863f2da9fcSPing-Ke Shih 		return 10;
28873f2da9fcSPing-Ke Shih 	case 113 ... 115:
28883f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(10);
28893f2da9fcSPing-Ke Shih 	case 116 ... 120:
28903f2da9fcSPing-Ke Shih 		return 11;
28913f2da9fcSPing-Ke Shih 	case 121 ... 123:
28923f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(11);
28933f2da9fcSPing-Ke Shih 	case 124 ... 128:
28943f2da9fcSPing-Ke Shih 		return 12;
28953f2da9fcSPing-Ke Shih 	case 129 ... 131:
28963f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(12);
28973f2da9fcSPing-Ke Shih 	case 132 ... 136:
28983f2da9fcSPing-Ke Shih 		return 13;
28993f2da9fcSPing-Ke Shih 	case 137 ... 139:
29003f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(13);
29013f2da9fcSPing-Ke Shih 	case 140 ... 144:
29023f2da9fcSPing-Ke Shih 		return 14;
29033f2da9fcSPing-Ke Shih 	case 149 ... 153:
29043f2da9fcSPing-Ke Shih 		return 15;
29053f2da9fcSPing-Ke Shih 	case 154 ... 156:
29063f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(15);
29073f2da9fcSPing-Ke Shih 	case 157 ... 161:
29083f2da9fcSPing-Ke Shih 		return 16;
29093f2da9fcSPing-Ke Shih 	case 162 ... 164:
29103f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(16);
29113f2da9fcSPing-Ke Shih 	case 165 ... 169:
29123f2da9fcSPing-Ke Shih 		return 17;
29133f2da9fcSPing-Ke Shih 	case 170 ... 172:
29143f2da9fcSPing-Ke Shih 		return TSSI_EXTRA_GROUP(17);
29153f2da9fcSPing-Ke Shih 	case 173 ... 177:
29163f2da9fcSPing-Ke Shih 		return 18;
29173f2da9fcSPing-Ke Shih 	}
29183f2da9fcSPing-Ke Shih 
29193f2da9fcSPing-Ke Shih 	return 0;
29203f2da9fcSPing-Ke Shih }
29213f2da9fcSPing-Ke Shih 
_tssi_get_trim_group(struct rtw89_dev * rtwdev,u8 ch)29223f2da9fcSPing-Ke Shih static u32 _tssi_get_trim_group(struct rtw89_dev *rtwdev, u8 ch)
29233f2da9fcSPing-Ke Shih {
29243f2da9fcSPing-Ke Shih 	switch (ch) {
29253f2da9fcSPing-Ke Shih 	case 1 ... 8:
29263f2da9fcSPing-Ke Shih 		return 0;
29273f2da9fcSPing-Ke Shih 	case 9 ... 14:
29283f2da9fcSPing-Ke Shih 		return 1;
29293f2da9fcSPing-Ke Shih 	case 36 ... 48:
29303f2da9fcSPing-Ke Shih 		return 2;
29313f2da9fcSPing-Ke Shih 	case 52 ... 64:
29323f2da9fcSPing-Ke Shih 		return 3;
29333f2da9fcSPing-Ke Shih 	case 100 ... 112:
29343f2da9fcSPing-Ke Shih 		return 4;
29353f2da9fcSPing-Ke Shih 	case 116 ... 128:
29363f2da9fcSPing-Ke Shih 		return 5;
29373f2da9fcSPing-Ke Shih 	case 132 ... 144:
29383f2da9fcSPing-Ke Shih 		return 6;
29393f2da9fcSPing-Ke Shih 	case 149 ... 177:
29403f2da9fcSPing-Ke Shih 		return 7;
29413f2da9fcSPing-Ke Shih 	}
29423f2da9fcSPing-Ke Shih 
29433f2da9fcSPing-Ke Shih 	return 0;
29443f2da9fcSPing-Ke Shih }
29453f2da9fcSPing-Ke Shih 
_tssi_get_ofdm_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)29463f2da9fcSPing-Ke Shih static s8 _tssi_get_ofdm_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
29473f2da9fcSPing-Ke Shih 			    enum rtw89_rf_path path)
29483f2da9fcSPing-Ke Shih {
29493f2da9fcSPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
29503f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
29513f2da9fcSPing-Ke Shih 	u32 gidx, gidx_1st, gidx_2nd;
29523f2da9fcSPing-Ke Shih 	u8 ch = chan->channel;
29533f2da9fcSPing-Ke Shih 	s8 de_1st;
29543f2da9fcSPing-Ke Shih 	s8 de_2nd;
29553f2da9fcSPing-Ke Shih 	s8 val;
29563f2da9fcSPing-Ke Shih 
29573f2da9fcSPing-Ke Shih 	gidx = _tssi_get_ofdm_group(rtwdev, ch);
29583f2da9fcSPing-Ke Shih 
29593f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI,
29603f2da9fcSPing-Ke Shih 		    "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n", path, gidx);
29613f2da9fcSPing-Ke Shih 
29623f2da9fcSPing-Ke Shih 	if (IS_TSSI_EXTRA_GROUP(gidx)) {
29633f2da9fcSPing-Ke Shih 		gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
29643f2da9fcSPing-Ke Shih 		gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
29653f2da9fcSPing-Ke Shih 		de_1st = tssi_info->tssi_mcs[path][gidx_1st];
29663f2da9fcSPing-Ke Shih 		de_2nd = tssi_info->tssi_mcs[path][gidx_2nd];
29673f2da9fcSPing-Ke Shih 		val = (de_1st + de_2nd) / 2;
29683f2da9fcSPing-Ke Shih 
29693f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
29703f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
29713f2da9fcSPing-Ke Shih 			    path, val, de_1st, de_2nd);
29723f2da9fcSPing-Ke Shih 	} else {
29733f2da9fcSPing-Ke Shih 		val = tssi_info->tssi_mcs[path][gidx];
29743f2da9fcSPing-Ke Shih 
29753f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
29763f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
29773f2da9fcSPing-Ke Shih 	}
29783f2da9fcSPing-Ke Shih 
29793f2da9fcSPing-Ke Shih 	return val;
29803f2da9fcSPing-Ke Shih }
29813f2da9fcSPing-Ke Shih 
_tssi_get_ofdm_trim_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)29823f2da9fcSPing-Ke Shih static s8 _tssi_get_ofdm_trim_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
29833f2da9fcSPing-Ke Shih 				 enum rtw89_rf_path path)
29843f2da9fcSPing-Ke Shih {
29853f2da9fcSPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
29863f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
29873f2da9fcSPing-Ke Shih 	u32 tgidx, tgidx_1st, tgidx_2nd;
29883f2da9fcSPing-Ke Shih 	u8 ch = chan->channel;
29893f2da9fcSPing-Ke Shih 	s8 tde_1st;
29903f2da9fcSPing-Ke Shih 	s8 tde_2nd;
29913f2da9fcSPing-Ke Shih 	s8 val;
29923f2da9fcSPing-Ke Shih 
29933f2da9fcSPing-Ke Shih 	tgidx = _tssi_get_trim_group(rtwdev, ch);
29943f2da9fcSPing-Ke Shih 
29953f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI,
29963f2da9fcSPing-Ke Shih 		    "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
29973f2da9fcSPing-Ke Shih 		    path, tgidx);
29983f2da9fcSPing-Ke Shih 
29993f2da9fcSPing-Ke Shih 	if (IS_TSSI_EXTRA_GROUP(tgidx)) {
30003f2da9fcSPing-Ke Shih 		tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
30013f2da9fcSPing-Ke Shih 		tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
30023f2da9fcSPing-Ke Shih 		tde_1st = tssi_info->tssi_trim[path][tgidx_1st];
30033f2da9fcSPing-Ke Shih 		tde_2nd = tssi_info->tssi_trim[path][tgidx_2nd];
30043f2da9fcSPing-Ke Shih 		val = (tde_1st + tde_2nd) / 2;
30053f2da9fcSPing-Ke Shih 
30063f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30073f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
30083f2da9fcSPing-Ke Shih 			    path, val, tde_1st, tde_2nd);
30093f2da9fcSPing-Ke Shih 	} else {
30103f2da9fcSPing-Ke Shih 		val = tssi_info->tssi_trim[path][tgidx];
30113f2da9fcSPing-Ke Shih 
30123f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30133f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
30143f2da9fcSPing-Ke Shih 			    path, val);
30153f2da9fcSPing-Ke Shih 	}
30163f2da9fcSPing-Ke Shih 
30173f2da9fcSPing-Ke Shih 	return val;
30183f2da9fcSPing-Ke Shih }
30193f2da9fcSPing-Ke Shih 
_tssi_set_efuse_to_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)30203f2da9fcSPing-Ke Shih static void _tssi_set_efuse_to_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
30213f2da9fcSPing-Ke Shih {
30223f2da9fcSPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
30233f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
30243f2da9fcSPing-Ke Shih 	u8 ch = chan->channel;
30253f2da9fcSPing-Ke Shih 	u8 gidx;
30263f2da9fcSPing-Ke Shih 	s8 ofdm_de;
30273f2da9fcSPing-Ke Shih 	s8 trim_de;
30283f2da9fcSPing-Ke Shih 	s32 val;
30293f2da9fcSPing-Ke Shih 	u32 i;
30303f2da9fcSPing-Ke Shih 
30313f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI][TRIM]: phy=%d ch=%d\n",
30323f2da9fcSPing-Ke Shih 		    phy, ch);
30333f2da9fcSPing-Ke Shih 
30343f2da9fcSPing-Ke Shih 	for (i = RF_PATH_A; i < RTW8851B_TSSI_PATH_NR; i++) {
30353f2da9fcSPing-Ke Shih 		gidx = _tssi_get_cck_group(rtwdev, ch);
30363f2da9fcSPing-Ke Shih 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
30373f2da9fcSPing-Ke Shih 		val = tssi_info->tssi_cck[i][gidx] + trim_de;
30383f2da9fcSPing-Ke Shih 
30393f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30403f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d cck[%d]=0x%x trim=0x%x\n",
30413f2da9fcSPing-Ke Shih 			    i, gidx, tssi_info->tssi_cck[i][gidx], trim_de);
30423f2da9fcSPing-Ke Shih 
30433f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_long[i], _TSSI_DE_MASK, val);
30443f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_short[i], _TSSI_DE_MASK, val);
30453f2da9fcSPing-Ke Shih 
30463f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30473f2da9fcSPing-Ke Shih 			    "[TSSI] Set TSSI CCK DE 0x%x[21:12]=0x%x\n",
30483f2da9fcSPing-Ke Shih 			    _tssi_de_cck_long[i],
30493f2da9fcSPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_cck_long[i],
30503f2da9fcSPing-Ke Shih 						  _TSSI_DE_MASK));
30513f2da9fcSPing-Ke Shih 
30523f2da9fcSPing-Ke Shih 		ofdm_de = _tssi_get_ofdm_de(rtwdev, phy, i);
30533f2da9fcSPing-Ke Shih 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
30543f2da9fcSPing-Ke Shih 		val = ofdm_de + trim_de;
30553f2da9fcSPing-Ke Shih 
30563f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30573f2da9fcSPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs=0x%x trim=0x%x\n",
30583f2da9fcSPing-Ke Shih 			    i, ofdm_de, trim_de);
30593f2da9fcSPing-Ke Shih 
30603f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_20m[i], _TSSI_DE_MASK, val);
30613f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_40m[i], _TSSI_DE_MASK, val);
30623f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m[i], _TSSI_DE_MASK, val);
30633f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m_80m[i], _TSSI_DE_MASK, val);
30643f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_5m[i], _TSSI_DE_MASK, val);
30653f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_10m[i], _TSSI_DE_MASK, val);
30663f2da9fcSPing-Ke Shih 
30673f2da9fcSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
30683f2da9fcSPing-Ke Shih 			    "[TSSI] Set TSSI MCS DE 0x%x[21:12]=0x%x\n",
30693f2da9fcSPing-Ke Shih 			    _tssi_de_mcs_20m[i],
30703f2da9fcSPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_mcs_20m[i],
30713f2da9fcSPing-Ke Shih 						  _TSSI_DE_MASK));
30723f2da9fcSPing-Ke Shih 	}
30733f2da9fcSPing-Ke Shih }
30743f2da9fcSPing-Ke Shih 
_tssi_alimentk_dump_result(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)30753f2da9fcSPing-Ke Shih static void _tssi_alimentk_dump_result(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
30763f2da9fcSPing-Ke Shih {
30773f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
30783f2da9fcSPing-Ke Shih 		    "[TSSI PA K]\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n"
30793f2da9fcSPing-Ke Shih 		    "0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n0x%x = 0x%08x\n",
30803f2da9fcSPing-Ke Shih 		    R_TSSI_PA_K1 + (path << 13),
30813f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K1 + (path << 13), MASKDWORD),
30823f2da9fcSPing-Ke Shih 		    R_TSSI_PA_K2 + (path << 13),
30833f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K2 + (path << 13), MASKDWORD),
30843f2da9fcSPing-Ke Shih 		    R_P0_TSSI_ALIM1 + (path << 13),
30853f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM1 + (path << 13), MASKDWORD),
30863f2da9fcSPing-Ke Shih 		    R_P0_TSSI_ALIM3 + (path << 13),
30873f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM3 + (path << 13), MASKDWORD),
30883f2da9fcSPing-Ke Shih 		    R_TSSI_PA_K5 + (path << 13),
30893f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K5 + (path << 13), MASKDWORD),
30903f2da9fcSPing-Ke Shih 		    R_P0_TSSI_ALIM2 + (path << 13),
30913f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM2 + (path << 13), MASKDWORD),
30923f2da9fcSPing-Ke Shih 		    R_P0_TSSI_ALIM4 + (path << 13),
30933f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_ALIM4 + (path << 13), MASKDWORD),
30943f2da9fcSPing-Ke Shih 		    R_TSSI_PA_K8 + (path << 13),
30953f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TSSI_PA_K8 + (path << 13), MASKDWORD));
30963f2da9fcSPing-Ke Shih }
30973f2da9fcSPing-Ke Shih 
_tssi_alimentk_done(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)30983f2da9fcSPing-Ke Shih static void _tssi_alimentk_done(struct rtw89_dev *rtwdev,
30993f2da9fcSPing-Ke Shih 				enum rtw89_phy_idx phy, enum rtw89_rf_path path)
31003f2da9fcSPing-Ke Shih {
31013f2da9fcSPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
31023f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
31033f2da9fcSPing-Ke Shih 	u8 channel = chan->channel;
31043f2da9fcSPing-Ke Shih 	u8 band;
31053f2da9fcSPing-Ke Shih 
31063f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
31073f2da9fcSPing-Ke Shih 		    "======>%s   phy=%d   path=%d\n", __func__, phy, path);
31083f2da9fcSPing-Ke Shih 
31093f2da9fcSPing-Ke Shih 	if (channel >= 1 && channel <= 14)
31103f2da9fcSPing-Ke Shih 		band = TSSI_ALIMK_2G;
31113f2da9fcSPing-Ke Shih 	else if (channel >= 36 && channel <= 64)
31123f2da9fcSPing-Ke Shih 		band = TSSI_ALIMK_5GL;
31133f2da9fcSPing-Ke Shih 	else if (channel >= 100 && channel <= 144)
31143f2da9fcSPing-Ke Shih 		band = TSSI_ALIMK_5GM;
31153f2da9fcSPing-Ke Shih 	else if (channel >= 149 && channel <= 177)
31163f2da9fcSPing-Ke Shih 		band = TSSI_ALIMK_5GH;
31173f2da9fcSPing-Ke Shih 	else
31183f2da9fcSPing-Ke Shih 		band = TSSI_ALIMK_2G;
31193f2da9fcSPing-Ke Shih 
31203f2da9fcSPing-Ke Shih 	if (tssi_info->alignment_done[path][band]) {
31213f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM1 + (path << 13), MASKDWORD,
31223f2da9fcSPing-Ke Shih 				       tssi_info->alignment_value[path][band][0]);
31233f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM3 + (path << 13), MASKDWORD,
31243f2da9fcSPing-Ke Shih 				       tssi_info->alignment_value[path][band][1]);
31253f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM2 + (path << 13), MASKDWORD,
31263f2da9fcSPing-Ke Shih 				       tssi_info->alignment_value[path][band][2]);
31273f2da9fcSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_ALIM4 + (path << 13), MASKDWORD,
31283f2da9fcSPing-Ke Shih 				       tssi_info->alignment_value[path][band][3]);
31293f2da9fcSPing-Ke Shih 	}
31303f2da9fcSPing-Ke Shih 
31313f2da9fcSPing-Ke Shih 	_tssi_alimentk_dump_result(rtwdev, path);
31323f2da9fcSPing-Ke Shih }
31333f2da9fcSPing-Ke Shih 
rtw8851b_by_rate_dpd(struct rtw89_dev * rtwdev)31340194a95cSPing-Ke Shih static void rtw8851b_by_rate_dpd(struct rtw89_dev *rtwdev)
31350194a95cSPing-Ke Shih {
31360194a95cSPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_PWR_SWING_OTHER_CTRL0,
31370194a95cSPing-Ke Shih 			   B_AX_CFIR_BY_RATE_OFF_MASK, 0x21861);
31380194a95cSPing-Ke Shih }
31390194a95cSPing-Ke Shih 
rtw8851b_dpk_init(struct rtw89_dev * rtwdev)31400194a95cSPing-Ke Shih void rtw8851b_dpk_init(struct rtw89_dev *rtwdev)
31410194a95cSPing-Ke Shih {
31420194a95cSPing-Ke Shih 	rtw8851b_by_rate_dpd(rtwdev);
31430194a95cSPing-Ke Shih }
31440194a95cSPing-Ke Shih 
rtw8851b_aack(struct rtw89_dev * rtwdev)314527d5559fSPing-Ke Shih void rtw8851b_aack(struct rtw89_dev *rtwdev)
314627d5559fSPing-Ke Shih {
3147b686bc67SPing-Ke Shih 	u32 tmp05, tmpd3, ib[4];
314827d5559fSPing-Ke Shih 	u32 tmp;
314927d5559fSPing-Ke Shih 	int ret;
315027d5559fSPing-Ke Shih 	int rek;
315127d5559fSPing-Ke Shih 	int i;
315227d5559fSPing-Ke Shih 
315327d5559fSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]DO AACK\n");
315427d5559fSPing-Ke Shih 
315527d5559fSPing-Ke Shih 	tmp05 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK);
3156b686bc67SPing-Ke Shih 	tmpd3 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK);
315727d5559fSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_MASK, 0x3);
315827d5559fSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, 0x0);
3159b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_ST, 0x0);
316027d5559fSPing-Ke Shih 
316127d5559fSPing-Ke Shih 	for (rek = 0; rek < 4; rek++) {
316227d5559fSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_AACK, RFREG_MASK, 0x8201e);
316327d5559fSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_AACK, RFREG_MASK, 0x8201f);
316427d5559fSPing-Ke Shih 		fsleep(100);
316527d5559fSPing-Ke Shih 
316627d5559fSPing-Ke Shih 		ret = read_poll_timeout_atomic(rtw89_read_rf, tmp, tmp,
316727d5559fSPing-Ke Shih 					       1, 1000, false,
316827d5559fSPing-Ke Shih 					       rtwdev, RF_PATH_A, 0xd0, BIT(16));
316927d5559fSPing-Ke Shih 		if (ret)
317027d5559fSPing-Ke Shih 			rtw89_warn(rtwdev, "[LCK]AACK timeout\n");
317127d5559fSPing-Ke Shih 
317227d5559fSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCI, RR_VCI_ON, 0x1);
317327d5559fSPing-Ke Shih 		for (i = 0; i < 4; i++) {
317427d5559fSPing-Ke Shih 			rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCO, RR_VCO_SEL, i);
317527d5559fSPing-Ke Shih 			ib[i] = rtw89_read_rf(rtwdev, RF_PATH_A, RR_IBD, RR_IBD_VAL);
317627d5559fSPing-Ke Shih 		}
317727d5559fSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_VCI, RR_VCI_ON, 0x0);
317827d5559fSPing-Ke Shih 
317927d5559fSPing-Ke Shih 		if (ib[0] != 0 && ib[1] != 0 && ib[2] != 0 && ib[3] != 0)
318027d5559fSPing-Ke Shih 			break;
318127d5559fSPing-Ke Shih 	}
318227d5559fSPing-Ke Shih 
318327d5559fSPing-Ke Shih 	if (rek != 0)
318427d5559fSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]AACK rek = %d\n", rek);
318527d5559fSPing-Ke Shih 
318627d5559fSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, tmp05);
3187b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK, tmpd3);
3188b686bc67SPing-Ke Shih }
3189b686bc67SPing-Ke Shih 
_lck_keep_thermal(struct rtw89_dev * rtwdev)3190b686bc67SPing-Ke Shih static void _lck_keep_thermal(struct rtw89_dev *rtwdev)
3191b686bc67SPing-Ke Shih {
3192b686bc67SPing-Ke Shih 	struct rtw89_lck_info *lck = &rtwdev->lck;
3193b686bc67SPing-Ke Shih 
3194b686bc67SPing-Ke Shih 	lck->thermal[RF_PATH_A] =
3195b686bc67SPing-Ke Shih 		ewma_thermal_read(&rtwdev->phystat.avg_thermal[RF_PATH_A]);
3196b686bc67SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3197b686bc67SPing-Ke Shih 		    "[LCK] path=%d thermal=0x%x", RF_PATH_A, lck->thermal[RF_PATH_A]);
3198b686bc67SPing-Ke Shih }
3199b686bc67SPing-Ke Shih 
rtw8851b_lck(struct rtw89_dev * rtwdev)3200b686bc67SPing-Ke Shih static void rtw8851b_lck(struct rtw89_dev *rtwdev)
3201b686bc67SPing-Ke Shih {
3202b686bc67SPing-Ke Shih 	u32 tmp05, tmp18, tmpd3;
3203b686bc67SPing-Ke Shih 
3204b686bc67SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]DO LCK\n");
3205b686bc67SPing-Ke Shih 
3206b686bc67SPing-Ke Shih 	tmp05 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK);
3207b686bc67SPing-Ke Shih 	tmp18 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
3208b686bc67SPing-Ke Shih 	tmpd3 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK);
3209b686bc67SPing-Ke Shih 
3210b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RR_MOD_MASK, 0x3);
3211b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, 0x0);
3212b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
3213b686bc67SPing-Ke Shih 
3214b686bc67SPing-Ke Shih 	_set_ch(rtwdev, tmp18);
3215b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RFREG_MASK, tmpd3);
3216b686bc67SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RFREG_MASK, tmp05);
3217b686bc67SPing-Ke Shih 
3218b686bc67SPing-Ke Shih 	_lck_keep_thermal(rtwdev);
3219b686bc67SPing-Ke Shih }
3220b686bc67SPing-Ke Shih 
3221b686bc67SPing-Ke Shih #define RTW8851B_LCK_TH 8
3222b686bc67SPing-Ke Shih 
rtw8851b_lck_track(struct rtw89_dev * rtwdev)3223b686bc67SPing-Ke Shih void rtw8851b_lck_track(struct rtw89_dev *rtwdev)
3224b686bc67SPing-Ke Shih {
3225b686bc67SPing-Ke Shih 	struct rtw89_lck_info *lck = &rtwdev->lck;
3226b686bc67SPing-Ke Shih 	u8 cur_thermal;
3227b686bc67SPing-Ke Shih 	int delta;
3228b686bc67SPing-Ke Shih 
3229b686bc67SPing-Ke Shih 	cur_thermal =
3230b686bc67SPing-Ke Shih 		ewma_thermal_read(&rtwdev->phystat.avg_thermal[RF_PATH_A]);
3231b686bc67SPing-Ke Shih 	delta = abs((int)cur_thermal - lck->thermal[RF_PATH_A]);
3232b686bc67SPing-Ke Shih 
3233b686bc67SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3234b686bc67SPing-Ke Shih 		    "[LCK] path=%d current thermal=0x%x delta=0x%x\n",
3235b686bc67SPing-Ke Shih 		    RF_PATH_A, cur_thermal, delta);
3236b686bc67SPing-Ke Shih 
3237b686bc67SPing-Ke Shih 	if (delta >= RTW8851B_LCK_TH) {
3238b686bc67SPing-Ke Shih 		rtw8851b_aack(rtwdev);
3239b686bc67SPing-Ke Shih 		rtw8851b_lck(rtwdev);
3240b686bc67SPing-Ke Shih 	}
3241b686bc67SPing-Ke Shih }
3242b686bc67SPing-Ke Shih 
rtw8851b_lck_init(struct rtw89_dev * rtwdev)3243b686bc67SPing-Ke Shih void rtw8851b_lck_init(struct rtw89_dev *rtwdev)
3244b686bc67SPing-Ke Shih {
3245b686bc67SPing-Ke Shih 	_lck_keep_thermal(rtwdev);
324627d5559fSPing-Ke Shih }
324727d5559fSPing-Ke Shih 
rtw8851b_rck(struct rtw89_dev * rtwdev)3248ae546f0aSPing-Ke Shih void rtw8851b_rck(struct rtw89_dev *rtwdev)
3249ae546f0aSPing-Ke Shih {
3250ae546f0aSPing-Ke Shih 	_rck(rtwdev, RF_PATH_A);
3251ae546f0aSPing-Ke Shih }
3252ae546f0aSPing-Ke Shih 
rtw8851b_dack(struct rtw89_dev * rtwdev)325393fbbeedSPing-Ke Shih void rtw8851b_dack(struct rtw89_dev *rtwdev)
325493fbbeedSPing-Ke Shih {
325593fbbeedSPing-Ke Shih 	_dac_cal(rtwdev, false);
325693fbbeedSPing-Ke Shih }
325793fbbeedSPing-Ke Shih 
rtw8851b_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)3258a83c6bb2SPing-Ke Shih void rtw8851b_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
3259a83c6bb2SPing-Ke Shih {
3260a83c6bb2SPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
3261a83c6bb2SPing-Ke Shih 	u32 tx_en;
3262a83c6bb2SPing-Ke Shih 
3263a83c6bb2SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_START);
3264a83c6bb2SPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
3265a83c6bb2SPing-Ke Shih 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
3266a83c6bb2SPing-Ke Shih 
3267a83c6bb2SPing-Ke Shih 	_iqk_init(rtwdev);
3268a83c6bb2SPing-Ke Shih 	_iqk(rtwdev, phy_idx, false);
3269a83c6bb2SPing-Ke Shih 
3270a83c6bb2SPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
3271a83c6bb2SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_STOP);
3272a83c6bb2SPing-Ke Shih }
3273a83c6bb2SPing-Ke Shih 
rtw8851b_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)3274fe8a1682SPing-Ke Shih void rtw8851b_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
3275fe8a1682SPing-Ke Shih {
3276fe8a1682SPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
3277fe8a1682SPing-Ke Shih 	u32 tx_en;
3278fe8a1682SPing-Ke Shih 
3279fe8a1682SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_START);
3280fe8a1682SPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
3281fe8a1682SPing-Ke Shih 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
3282fe8a1682SPing-Ke Shih 
3283fe8a1682SPing-Ke Shih 	_rx_dck(rtwdev, phy_idx, false);
3284fe8a1682SPing-Ke Shih 
3285fe8a1682SPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
3286fe8a1682SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_STOP);
3287fe8a1682SPing-Ke Shih }
3288fe8a1682SPing-Ke Shih 
rtw8851b_dpk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)32890194a95cSPing-Ke Shih void rtw8851b_dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
32900194a95cSPing-Ke Shih {
32910194a95cSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
32920194a95cSPing-Ke Shih 	u32 tx_en;
32930194a95cSPing-Ke Shih 
32940194a95cSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_START);
32950194a95cSPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
32960194a95cSPing-Ke Shih 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
32970194a95cSPing-Ke Shih 
32980194a95cSPing-Ke Shih 	rtwdev->dpk.is_dpk_enable = true;
32990194a95cSPing-Ke Shih 	rtwdev->dpk.is_dpk_reload_en = false;
33000194a95cSPing-Ke Shih 	_dpk(rtwdev, phy_idx, false);
33010194a95cSPing-Ke Shih 
33020194a95cSPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
33030194a95cSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_STOP);
33040194a95cSPing-Ke Shih }
33050194a95cSPing-Ke Shih 
rtw8851b_dpk_track(struct rtw89_dev * rtwdev)33060194a95cSPing-Ke Shih void rtw8851b_dpk_track(struct rtw89_dev *rtwdev)
33070194a95cSPing-Ke Shih {
33080194a95cSPing-Ke Shih 	_dpk_track(rtwdev);
33090194a95cSPing-Ke Shih }
33100194a95cSPing-Ke Shih 
rtw8851b_tssi(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool hwtx_en)33113f2da9fcSPing-Ke Shih void rtw8851b_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool hwtx_en)
33123f2da9fcSPing-Ke Shih {
33133f2da9fcSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy, RF_A);
33143f2da9fcSPing-Ke Shih 	u8 i;
33153f2da9fcSPing-Ke Shih 
33163f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n", __func__, phy);
33173f2da9fcSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_START);
33183f2da9fcSPing-Ke Shih 
33193f2da9fcSPing-Ke Shih 	_tssi_disable(rtwdev, phy);
33203f2da9fcSPing-Ke Shih 
33213f2da9fcSPing-Ke Shih 	for (i = RF_PATH_A; i < RF_PATH_NUM_8851B; i++) {
33223f2da9fcSPing-Ke Shih 		_tssi_set_sys(rtwdev, phy, i);
33233f2da9fcSPing-Ke Shih 		_tssi_ini_txpwr_ctrl_bb(rtwdev, phy, i);
33243f2da9fcSPing-Ke Shih 		_tssi_ini_txpwr_ctrl_bb_he_tb(rtwdev, phy, i);
33253f2da9fcSPing-Ke Shih 		_tssi_set_dck(rtwdev, phy, i);
33263f2da9fcSPing-Ke Shih 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
33273f2da9fcSPing-Ke Shih 		_tssi_set_dac_gain_tbl(rtwdev, phy, i);
33283f2da9fcSPing-Ke Shih 		_tssi_slope_cal_org(rtwdev, phy, i);
33293f2da9fcSPing-Ke Shih 		_tssi_alignment_default(rtwdev, phy, i, true);
33303f2da9fcSPing-Ke Shih 		_tssi_set_tssi_slope(rtwdev, phy, i);
33313f2da9fcSPing-Ke Shih 	}
33323f2da9fcSPing-Ke Shih 
33333f2da9fcSPing-Ke Shih 	_tssi_enable(rtwdev, phy);
33343f2da9fcSPing-Ke Shih 	_tssi_set_efuse_to_de(rtwdev, phy);
33353f2da9fcSPing-Ke Shih 
33363f2da9fcSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_STOP);
33373f2da9fcSPing-Ke Shih }
33383f2da9fcSPing-Ke Shih 
rtw8851b_tssi_scan(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)33393f2da9fcSPing-Ke Shih void rtw8851b_tssi_scan(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
33403f2da9fcSPing-Ke Shih {
33413f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
33423f2da9fcSPing-Ke Shih 	u8 channel = chan->channel;
33433f2da9fcSPing-Ke Shih 	u32 i;
33443f2da9fcSPing-Ke Shih 
33453f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
33463f2da9fcSPing-Ke Shih 		    "======>%s   phy=%d  channel=%d\n", __func__, phy, channel);
33473f2da9fcSPing-Ke Shih 
33483f2da9fcSPing-Ke Shih 	_tssi_disable(rtwdev, phy);
33493f2da9fcSPing-Ke Shih 
33503f2da9fcSPing-Ke Shih 	for (i = RF_PATH_A; i < RF_PATH_NUM_8851B; i++) {
33513f2da9fcSPing-Ke Shih 		_tssi_set_sys(rtwdev, phy, i);
33523f2da9fcSPing-Ke Shih 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
33533f2da9fcSPing-Ke Shih 		_tssi_slope_cal_org(rtwdev, phy, i);
33543f2da9fcSPing-Ke Shih 		_tssi_alignment_default(rtwdev, phy, i, true);
33553f2da9fcSPing-Ke Shih 	}
33563f2da9fcSPing-Ke Shih 
33573f2da9fcSPing-Ke Shih 	_tssi_enable(rtwdev, phy);
33583f2da9fcSPing-Ke Shih 	_tssi_set_efuse_to_de(rtwdev, phy);
33593f2da9fcSPing-Ke Shih }
33603f2da9fcSPing-Ke Shih 
rtw8851b_tssi_default_txagc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool enable)33613f2da9fcSPing-Ke Shih static void rtw8851b_tssi_default_txagc(struct rtw89_dev *rtwdev,
33623f2da9fcSPing-Ke Shih 					enum rtw89_phy_idx phy, bool enable)
33633f2da9fcSPing-Ke Shih {
33643f2da9fcSPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
33653f2da9fcSPing-Ke Shih 	u8 channel = chan->channel;
33663f2da9fcSPing-Ke Shih 
33673f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "======> %s   ch=%d\n",
33683f2da9fcSPing-Ke Shih 		    __func__, channel);
33693f2da9fcSPing-Ke Shih 
33703f2da9fcSPing-Ke Shih 	if (enable)
33713f2da9fcSPing-Ke Shih 		return;
33723f2da9fcSPing-Ke Shih 
33733f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
33743f2da9fcSPing-Ke Shih 		    "======>%s 1 SCAN_END Set 0x5818[7:0]=0x%x\n",
33753f2da9fcSPing-Ke Shih 		    __func__,
33763f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT));
33773f2da9fcSPing-Ke Shih 
33783f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT, 0xc0);
33793f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
33803f2da9fcSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
33813f2da9fcSPing-Ke Shih 
33823f2da9fcSPing-Ke Shih 	_tssi_alimentk_done(rtwdev, phy, RF_PATH_A);
33833f2da9fcSPing-Ke Shih 
33843f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
33853f2da9fcSPing-Ke Shih 		    "======>%s 2 SCAN_END Set 0x5818[7:0]=0x%x\n",
33863f2da9fcSPing-Ke Shih 		    __func__,
33873f2da9fcSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT));
33883f2da9fcSPing-Ke Shih 
33893f2da9fcSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
33903f2da9fcSPing-Ke Shih 		    "======> %s   SCAN_END\n", __func__);
33913f2da9fcSPing-Ke Shih }
33923f2da9fcSPing-Ke Shih 
rtw8851b_wifi_scan_notify(struct rtw89_dev * rtwdev,bool scan_start,enum rtw89_phy_idx phy_idx)33933f2da9fcSPing-Ke Shih void rtw8851b_wifi_scan_notify(struct rtw89_dev *rtwdev, bool scan_start,
33943f2da9fcSPing-Ke Shih 			       enum rtw89_phy_idx phy_idx)
33953f2da9fcSPing-Ke Shih {
33963f2da9fcSPing-Ke Shih 	if (scan_start)
33973f2da9fcSPing-Ke Shih 		rtw8851b_tssi_default_txagc(rtwdev, phy_idx, true);
33983f2da9fcSPing-Ke Shih 	else
33993f2da9fcSPing-Ke Shih 		rtw8851b_tssi_default_txagc(rtwdev, phy_idx, false);
34003f2da9fcSPing-Ke Shih }
34013f2da9fcSPing-Ke Shih 
_bw_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,enum rtw89_bandwidth bw,bool dav)34022a59fe29SPing-Ke Shih static void _bw_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
34032a59fe29SPing-Ke Shih 			enum rtw89_bandwidth bw, bool dav)
34042a59fe29SPing-Ke Shih {
34052a59fe29SPing-Ke Shih 	u32 reg18_addr = dav ? RR_CFGCH : RR_CFGCH_V1;
34062a59fe29SPing-Ke Shih 	u32 rf_reg18;
34072a59fe29SPing-Ke Shih 
34082a59fe29SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===> %s\n", __func__);
34092a59fe29SPing-Ke Shih 
34102a59fe29SPing-Ke Shih 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK);
34112a59fe29SPing-Ke Shih 	if (rf_reg18 == INV_RF_DATA) {
34122a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
34132a59fe29SPing-Ke Shih 			    "[RFK]Invalid RF_0x18 for Path-%d\n", path);
34142a59fe29SPing-Ke Shih 		return;
34152a59fe29SPing-Ke Shih 	}
34162a59fe29SPing-Ke Shih 	rf_reg18 &= ~RR_CFGCH_BW;
34172a59fe29SPing-Ke Shih 
34182a59fe29SPing-Ke Shih 	switch (bw) {
34192a59fe29SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_5:
34202a59fe29SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_10:
34212a59fe29SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
34222a59fe29SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_20M);
34232a59fe29SPing-Ke Shih 		break;
34242a59fe29SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
34252a59fe29SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_40M);
34262a59fe29SPing-Ke Shih 		break;
34272a59fe29SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
34282a59fe29SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_80M);
34292a59fe29SPing-Ke Shih 		break;
34302a59fe29SPing-Ke Shih 	default:
34312a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]Fail to set CH\n");
34322a59fe29SPing-Ke Shih 	}
34332a59fe29SPing-Ke Shih 
34342a59fe29SPing-Ke Shih 	rf_reg18 &= ~(RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH | RR_CFGCH_BCN |
34352a59fe29SPing-Ke Shih 		      RR_CFGCH_BW2) & RFREG_MASK;
34362a59fe29SPing-Ke Shih 	rf_reg18 |= RR_CFGCH_BW2;
34372a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, reg18_addr, RFREG_MASK, rf_reg18);
34382a59fe29SPing-Ke Shih 
34392a59fe29SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK] set %x at path%d, %x =0x%x\n",
34402a59fe29SPing-Ke Shih 		    bw, path, reg18_addr,
34412a59fe29SPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK));
34422a59fe29SPing-Ke Shih }
34432a59fe29SPing-Ke Shih 
_ctrl_bw(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_bandwidth bw)34442a59fe29SPing-Ke Shih static void _ctrl_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
34452a59fe29SPing-Ke Shih 		     enum rtw89_bandwidth bw)
34462a59fe29SPing-Ke Shih {
34472a59fe29SPing-Ke Shih 	_bw_setting(rtwdev, RF_PATH_A, bw, true);
34482a59fe29SPing-Ke Shih 	_bw_setting(rtwdev, RF_PATH_A, bw, false);
34492a59fe29SPing-Ke Shih }
34502a59fe29SPing-Ke Shih 
_set_s0_arfc18(struct rtw89_dev * rtwdev,u32 val)34512a59fe29SPing-Ke Shih static bool _set_s0_arfc18(struct rtw89_dev *rtwdev, u32 val)
34522a59fe29SPing-Ke Shih {
34532a59fe29SPing-Ke Shih 	u32 bak;
34542a59fe29SPing-Ke Shih 	u32 tmp;
34552a59fe29SPing-Ke Shih 	int ret;
34562a59fe29SPing-Ke Shih 
34572a59fe29SPing-Ke Shih 	bak = rtw89_read_rf(rtwdev, RF_PATH_A, RR_LDO, RFREG_MASK);
34582a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LDO, RR_LDO_SEL, 0x1);
34592a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK, val);
34602a59fe29SPing-Ke Shih 
34612a59fe29SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read_rf, tmp, tmp == 0, 1, 1000,
34622a59fe29SPing-Ke Shih 				       false, rtwdev, RF_PATH_A, RR_LPF, RR_LPF_BUSY);
34632a59fe29SPing-Ke Shih 	if (ret)
34642a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]LCK timeout\n");
34652a59fe29SPing-Ke Shih 
34662a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_LDO, RFREG_MASK, bak);
34672a59fe29SPing-Ke Shih 
34682a59fe29SPing-Ke Shih 	return !!ret;
34692a59fe29SPing-Ke Shih }
34702a59fe29SPing-Ke Shih 
_lck_check(struct rtw89_dev * rtwdev)34712a59fe29SPing-Ke Shih static void _lck_check(struct rtw89_dev *rtwdev)
34722a59fe29SPing-Ke Shih {
34732a59fe29SPing-Ke Shih 	u32 tmp;
34742a59fe29SPing-Ke Shih 
34752a59fe29SPing-Ke Shih 	if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
34762a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]SYN MMD reset\n");
34772a59fe29SPing-Ke Shih 
34782a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_EN, 0x1);
34792a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_SYN, 0x0);
34802a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_SYN, 0x1);
34812a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_MMD, RR_MMD_RST_EN, 0x0);
34822a59fe29SPing-Ke Shih 	}
34832a59fe29SPing-Ke Shih 
34842a59fe29SPing-Ke Shih 	udelay(10);
34852a59fe29SPing-Ke Shih 
34862a59fe29SPing-Ke Shih 	if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
34872a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]re-set RF 0x18\n");
34882a59fe29SPing-Ke Shih 
34892a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
34902a59fe29SPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
34912a59fe29SPing-Ke Shih 		_set_s0_arfc18(rtwdev, tmp);
34922a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
34932a59fe29SPing-Ke Shih 	}
34942a59fe29SPing-Ke Shih 
34952a59fe29SPing-Ke Shih 	if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RR_SYNFB_LK) == 0) {
34962a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]SYN off/on\n");
34972a59fe29SPing-Ke Shih 
34982a59fe29SPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_POW, RFREG_MASK);
34992a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RFREG_MASK, tmp);
35002a59fe29SPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_SX, RFREG_MASK);
35012a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_SX, RFREG_MASK, tmp);
35022a59fe29SPing-Ke Shih 
35032a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_SYNLUT, RR_SYNLUT_MOD, 0x1);
35042a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RR_POW_SYN, 0x0);
35052a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_POW, RR_POW_SYN, 0x3);
35062a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_SYNLUT, RR_SYNLUT_MOD, 0x0);
35072a59fe29SPing-Ke Shih 
35082a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
35092a59fe29SPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
35102a59fe29SPing-Ke Shih 		_set_s0_arfc18(rtwdev, tmp);
35112a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_A, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
35122a59fe29SPing-Ke Shih 
35132a59fe29SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[LCK]0xb2=%x, 0xc5=%x\n",
35142a59fe29SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_VCO, RFREG_MASK),
35152a59fe29SPing-Ke Shih 			    rtw89_read_rf(rtwdev, RF_PATH_A, RR_SYNFB, RFREG_MASK));
35162a59fe29SPing-Ke Shih 	}
35172a59fe29SPing-Ke Shih }
35182a59fe29SPing-Ke Shih 
_set_ch(struct rtw89_dev * rtwdev,u32 val)35192a59fe29SPing-Ke Shih static void _set_ch(struct rtw89_dev *rtwdev, u32 val)
35202a59fe29SPing-Ke Shih {
35212a59fe29SPing-Ke Shih 	bool timeout;
35222a59fe29SPing-Ke Shih 
35232a59fe29SPing-Ke Shih 	timeout = _set_s0_arfc18(rtwdev, val);
35242a59fe29SPing-Ke Shih 	if (!timeout)
35252a59fe29SPing-Ke Shih 		_lck_check(rtwdev);
35262a59fe29SPing-Ke Shih }
35272a59fe29SPing-Ke Shih 
_ch_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 central_ch,bool dav)35282a59fe29SPing-Ke Shih static void _ch_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
35292a59fe29SPing-Ke Shih 			u8 central_ch, bool dav)
35302a59fe29SPing-Ke Shih {
35312a59fe29SPing-Ke Shih 	u32 reg18_addr = dav ? RR_CFGCH : RR_CFGCH_V1;
35322a59fe29SPing-Ke Shih 	bool is_2g_ch = central_ch <= 14;
35332a59fe29SPing-Ke Shih 	u32 rf_reg18;
35342a59fe29SPing-Ke Shih 
35352a59fe29SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===> %s\n", __func__);
35362a59fe29SPing-Ke Shih 
35372a59fe29SPing-Ke Shih 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK);
35382a59fe29SPing-Ke Shih 	rf_reg18 &= ~(RR_CFGCH_BAND1 | RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH |
35392a59fe29SPing-Ke Shih 		      RR_CFGCH_BCN | RR_CFGCH_BAND0 | RR_CFGCH_CH);
35402a59fe29SPing-Ke Shih 	rf_reg18 |= FIELD_PREP(RR_CFGCH_CH, central_ch);
35412a59fe29SPing-Ke Shih 
35422a59fe29SPing-Ke Shih 	if (!is_2g_ch)
35432a59fe29SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_5G) |
35442a59fe29SPing-Ke Shih 			    FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_5G);
35452a59fe29SPing-Ke Shih 
35462a59fe29SPing-Ke Shih 	rf_reg18 &= ~(RR_CFGCH_POW_LCK | RR_CFGCH_TRX_AH | RR_CFGCH_BCN |
35472a59fe29SPing-Ke Shih 		      RR_CFGCH_BW2) & RFREG_MASK;
35482a59fe29SPing-Ke Shih 	rf_reg18 |= RR_CFGCH_BW2;
35492a59fe29SPing-Ke Shih 
35502a59fe29SPing-Ke Shih 	if (path == RF_PATH_A && dav)
35512a59fe29SPing-Ke Shih 		_set_ch(rtwdev, rf_reg18);
35522a59fe29SPing-Ke Shih 	else
35532a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, reg18_addr, RFREG_MASK, rf_reg18);
35542a59fe29SPing-Ke Shih 
35552a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LCKST, RR_LCKST_BIN, 0);
35562a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LCKST, RR_LCKST_BIN, 1);
35572a59fe29SPing-Ke Shih 
35582a59fe29SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
35592a59fe29SPing-Ke Shih 		    "[RFK]CH: %d for Path-%d, reg0x%x = 0x%x\n",
35602a59fe29SPing-Ke Shih 		    central_ch, path, reg18_addr,
35612a59fe29SPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, reg18_addr, RFREG_MASK));
35622a59fe29SPing-Ke Shih }
35632a59fe29SPing-Ke Shih 
_ctrl_ch(struct rtw89_dev * rtwdev,u8 central_ch)35642a59fe29SPing-Ke Shih static void _ctrl_ch(struct rtw89_dev *rtwdev, u8 central_ch)
35652a59fe29SPing-Ke Shih {
35662a59fe29SPing-Ke Shih 	_ch_setting(rtwdev, RF_PATH_A, central_ch, true);
35672a59fe29SPing-Ke Shih 	_ch_setting(rtwdev, RF_PATH_A, central_ch, false);
35682a59fe29SPing-Ke Shih }
35692a59fe29SPing-Ke Shih 
_set_rxbb_bw(struct rtw89_dev * rtwdev,enum rtw89_bandwidth bw,enum rtw89_rf_path path)35702a59fe29SPing-Ke Shih static void _set_rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_bandwidth bw,
35712a59fe29SPing-Ke Shih 			 enum rtw89_rf_path path)
35722a59fe29SPing-Ke Shih {
35732a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x1);
35742a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWA, RR_LUTWA_M2, 0x12);
35752a59fe29SPing-Ke Shih 
35762a59fe29SPing-Ke Shih 	if (bw == RTW89_CHANNEL_WIDTH_20)
35772a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x1b);
35782a59fe29SPing-Ke Shih 	else if (bw == RTW89_CHANNEL_WIDTH_40)
35792a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x13);
35802a59fe29SPing-Ke Shih 	else if (bw == RTW89_CHANNEL_WIDTH_80)
35812a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0xb);
35822a59fe29SPing-Ke Shih 	else
35832a59fe29SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, 0x3);
35842a59fe29SPing-Ke Shih 
35852a59fe29SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK] set S%d RXBB BW 0x3F = 0x%x\n", path,
35862a59fe29SPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB));
35872a59fe29SPing-Ke Shih 
35882a59fe29SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x0);
35892a59fe29SPing-Ke Shih }
35902a59fe29SPing-Ke Shih 
_rxbb_bw(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_bandwidth bw)35912a59fe29SPing-Ke Shih static void _rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
35922a59fe29SPing-Ke Shih 		     enum rtw89_bandwidth bw)
35932a59fe29SPing-Ke Shih {
35942a59fe29SPing-Ke Shih 	u8 kpath, path;
35952a59fe29SPing-Ke Shih 
35962a59fe29SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
35972a59fe29SPing-Ke Shih 
35982a59fe29SPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8851B; path++) {
35992a59fe29SPing-Ke Shih 		if (!(kpath & BIT(path)))
36002a59fe29SPing-Ke Shih 			continue;
36012a59fe29SPing-Ke Shih 
36022a59fe29SPing-Ke Shih 		_set_rxbb_bw(rtwdev, bw, path);
36032a59fe29SPing-Ke Shih 	}
36042a59fe29SPing-Ke Shih }
36052a59fe29SPing-Ke Shih 
rtw8851b_ctrl_bw_ch(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 central_ch,enum rtw89_band band,enum rtw89_bandwidth bw)36062a59fe29SPing-Ke Shih static void rtw8851b_ctrl_bw_ch(struct rtw89_dev *rtwdev,
36072a59fe29SPing-Ke Shih 				enum rtw89_phy_idx phy, u8 central_ch,
36082a59fe29SPing-Ke Shih 				enum rtw89_band band, enum rtw89_bandwidth bw)
36092a59fe29SPing-Ke Shih {
36102a59fe29SPing-Ke Shih 	_ctrl_ch(rtwdev, central_ch);
36112a59fe29SPing-Ke Shih 	_ctrl_bw(rtwdev, phy, bw);
36122a59fe29SPing-Ke Shih 	_rxbb_bw(rtwdev, phy, bw);
36132a59fe29SPing-Ke Shih }
36142a59fe29SPing-Ke Shih 
rtw8851b_set_channel_rf(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)36152a59fe29SPing-Ke Shih void rtw8851b_set_channel_rf(struct rtw89_dev *rtwdev,
36162a59fe29SPing-Ke Shih 			     const struct rtw89_chan *chan,
36172a59fe29SPing-Ke Shih 			     enum rtw89_phy_idx phy_idx)
36182a59fe29SPing-Ke Shih {
36192a59fe29SPing-Ke Shih 	rtw8851b_ctrl_bw_ch(rtwdev, phy_idx, chan->channel, chan->band_type,
36202a59fe29SPing-Ke Shih 			    chan->band_width);
36212a59fe29SPing-Ke Shih }
3622