xref: /openbmc/linux/drivers/net/wireless/realtek/rtw89/rtw8852c_rfk.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1bb865ba6SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2bb865ba6SPing-Ke Shih /* Copyright(c) 2019-2022  Realtek Corporation
3bb865ba6SPing-Ke Shih  */
4bb865ba6SPing-Ke Shih 
576599a8dSPing-Ke Shih #include "coex.h"
6bb865ba6SPing-Ke Shih #include "debug.h"
7bb865ba6SPing-Ke Shih #include "phy.h"
8bb865ba6SPing-Ke Shih #include "reg.h"
9bb865ba6SPing-Ke Shih #include "rtw8852c.h"
10bb865ba6SPing-Ke Shih #include "rtw8852c_rfk.h"
1176599a8dSPing-Ke Shih #include "rtw8852c_rfk_table.h"
12e5efc4d5SPing-Ke Shih #include "rtw8852c_table.h"
13e5efc4d5SPing-Ke Shih 
143aa83062SChih-Kang Chang struct rxck_def {
153aa83062SChih-Kang Chang 	u32 ctl;
163aa83062SChih-Kang Chang 	u32 en;
173aa83062SChih-Kang Chang 	u32 bw0;
183aa83062SChih-Kang Chang 	u32 bw1;
193aa83062SChih-Kang Chang 	u32 mul;
203aa83062SChih-Kang Chang 	u32 lp;
213aa83062SChih-Kang Chang };
223aa83062SChih-Kang Chang 
23e5efc4d5SPing-Ke Shih #define _TSSI_DE_MASK GENMASK(21, 12)
24e5efc4d5SPing-Ke Shih static const u32 _tssi_de_cck_long[RF_PATH_NUM_8852C] = {0x5858, 0x7858};
25e5efc4d5SPing-Ke Shih static const u32 _tssi_de_cck_short[RF_PATH_NUM_8852C] = {0x5860, 0x7860};
26e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_20m[RF_PATH_NUM_8852C] = {0x5838, 0x7838};
27e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_40m[RF_PATH_NUM_8852C] = {0x5840, 0x7840};
28e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_80m[RF_PATH_NUM_8852C] = {0x5848, 0x7848};
29e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_80m_80m[RF_PATH_NUM_8852C] = {0x5850, 0x7850};
30e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_5m[RF_PATH_NUM_8852C] = {0x5828, 0x7828};
31e5efc4d5SPing-Ke Shih static const u32 _tssi_de_mcs_10m[RF_PATH_NUM_8852C] = {0x5830, 0x7830};
32bb865ba6SPing-Ke Shih 
332da8109dSPing-Ke Shih static const u32 rtw8852c_backup_bb_regs[] = {
3468b0ce5bSPing-Ke Shih 	0x8120, 0xc0d4, 0xc0d8, 0xc0e8, 0x8220, 0xc1d4, 0xc1d8, 0xc1e8
352da8109dSPing-Ke Shih };
362da8109dSPing-Ke Shih 
372da8109dSPing-Ke Shih static const u32 rtw8852c_backup_rf_regs[] = {
38ba1a6905SChih-Kang Chang 	0xdf, 0x5f, 0x8f, 0x97, 0xa3, 0x5, 0x10005
392da8109dSPing-Ke Shih };
402da8109dSPing-Ke Shih 
412da8109dSPing-Ke Shih #define BACKUP_BB_REGS_NR ARRAY_SIZE(rtw8852c_backup_bb_regs)
422da8109dSPing-Ke Shih #define BACKUP_RF_REGS_NR ARRAY_SIZE(rtw8852c_backup_rf_regs)
432da8109dSPing-Ke Shih 
442da8109dSPing-Ke Shih #define RXK_GROUP_NR 4
452da8109dSPing-Ke Shih static const u32 _rxk_a6_idxrxgain[RXK_GROUP_NR] = {0x190, 0x196, 0x290, 0x316};
462da8109dSPing-Ke Shih static const u32 _rxk_a6_idxattc2[RXK_GROUP_NR] = {0x00, 0x0, 0x00, 0x00};
472da8109dSPing-Ke Shih static const u32 _rxk_a_idxrxgain[RXK_GROUP_NR] = {0x190, 0x198, 0x310, 0x318};
482da8109dSPing-Ke Shih static const u32 _rxk_a_idxattc2[RXK_GROUP_NR] = {0x00, 0x00, 0x00, 0x00};
492da8109dSPing-Ke Shih static const u32 _rxk_g_idxrxgain[RXK_GROUP_NR] = {0x252, 0x26c, 0x350, 0x360};
502da8109dSPing-Ke Shih static const u32 _rxk_g_idxattc2[RXK_GROUP_NR] = {0x00, 0x07, 0x00, 0x3};
512da8109dSPing-Ke Shih 
522da8109dSPing-Ke Shih #define TXK_GROUP_NR 3
532da8109dSPing-Ke Shih static const u32 _txk_a6_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
542da8109dSPing-Ke Shih static const u32 _txk_a6_track_range[TXK_GROUP_NR] = {0x6, 0x7, 0x7};
552da8109dSPing-Ke Shih static const u32 _txk_a6_gain_bb[TXK_GROUP_NR] = {0x12, 0x09, 0x0e};
562da8109dSPing-Ke Shih static const u32 _txk_a6_itqt[TXK_GROUP_NR] = {0x12, 0x12, 0x12};
572da8109dSPing-Ke Shih static const u32 _txk_a_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
582da8109dSPing-Ke Shih static const u32 _txk_a_track_range[TXK_GROUP_NR] = {0x5, 0x6, 0x7};
592da8109dSPing-Ke Shih static const u32 _txk_a_gain_bb[TXK_GROUP_NR] = {0x12, 0x09, 0x0e};
602da8109dSPing-Ke Shih static const u32 _txk_a_itqt[TXK_GROUP_NR] = {0x12, 0x12, 0x12};
612da8109dSPing-Ke Shih static const u32 _txk_g_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
622da8109dSPing-Ke Shih static const u32 _txk_g_track_range[TXK_GROUP_NR] = {0x5, 0x6, 0x6};
632da8109dSPing-Ke Shih static const u32 _txk_g_gain_bb[TXK_GROUP_NR] = {0x0e, 0x0a, 0x0e};
642da8109dSPing-Ke Shih static const u32 _txk_g_itqt[TXK_GROUP_NR] = { 0x12, 0x12, 0x12};
652da8109dSPing-Ke Shih 
66da4cea16SPing-Ke Shih static const u32 dpk_par_regs[RTW89_DPK_RF_PATH][4] = {
67da4cea16SPing-Ke Shih 	{0x8190, 0x8194, 0x8198, 0x81a4},
68da4cea16SPing-Ke Shih 	{0x81a8, 0x81c4, 0x81c8, 0x81e8},
69da4cea16SPing-Ke Shih };
70da4cea16SPing-Ke Shih 
719c22d603SPing-Ke Shih static const u8 _dck_addr_bs[RF_PATH_NUM_8852C] = {0x0, 0x10};
729c22d603SPing-Ke Shih static const u8 _dck_addr[RF_PATH_NUM_8852C] = {0xc, 0x1c};
739c22d603SPing-Ke Shih 
743aa83062SChih-Kang Chang static const struct rxck_def _ck480M = {0x8, 0x2, 0x3, 0xf, 0x0, 0x9};
753aa83062SChih-Kang Chang static const struct rxck_def _ck960M = {0x8, 0x2, 0x2, 0x8, 0x0, 0x9};
763aa83062SChih-Kang Chang static const struct rxck_def _ck1920M = {0x8, 0x0, 0x2, 0x4, 0x6, 0x9};
773aa83062SChih-Kang Chang 
_kpath(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)78bb865ba6SPing-Ke Shih static u8 _kpath(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
79bb865ba6SPing-Ke Shih {
80bb865ba6SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]dbcc_en: %x,  PHY%d\n",
81bb865ba6SPing-Ke Shih 		    rtwdev->dbcc_en, phy_idx);
82bb865ba6SPing-Ke Shih 
83bb865ba6SPing-Ke Shih 	if (!rtwdev->dbcc_en)
84bb865ba6SPing-Ke Shih 		return RF_AB;
85bb865ba6SPing-Ke Shih 
86bb865ba6SPing-Ke Shih 	if (phy_idx == RTW89_PHY_0)
87bb865ba6SPing-Ke Shih 		return RF_A;
88bb865ba6SPing-Ke Shih 	else
89bb865ba6SPing-Ke Shih 		return RF_B;
90bb865ba6SPing-Ke Shih }
91bb865ba6SPing-Ke Shih 
_rfk_backup_bb_reg(struct rtw89_dev * rtwdev,u32 backup_bb_reg_val[])922da8109dSPing-Ke Shih static void _rfk_backup_bb_reg(struct rtw89_dev *rtwdev, u32 backup_bb_reg_val[])
932da8109dSPing-Ke Shih {
942da8109dSPing-Ke Shih 	u32 i;
952da8109dSPing-Ke Shih 
962da8109dSPing-Ke Shih 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
972da8109dSPing-Ke Shih 		backup_bb_reg_val[i] =
982da8109dSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, rtw8852c_backup_bb_regs[i],
992da8109dSPing-Ke Shih 					      MASKDWORD);
1002da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1012da8109dSPing-Ke Shih 			    "[IQK]backup bb reg : %x, value =%x\n",
1022da8109dSPing-Ke Shih 			    rtw8852c_backup_bb_regs[i], backup_bb_reg_val[i]);
1032da8109dSPing-Ke Shih 	}
1042da8109dSPing-Ke Shih }
1052da8109dSPing-Ke Shih 
_rfk_backup_rf_reg(struct rtw89_dev * rtwdev,u32 backup_rf_reg_val[],u8 rf_path)1062da8109dSPing-Ke Shih static void _rfk_backup_rf_reg(struct rtw89_dev *rtwdev, u32 backup_rf_reg_val[],
1072da8109dSPing-Ke Shih 			       u8 rf_path)
1082da8109dSPing-Ke Shih {
1092da8109dSPing-Ke Shih 	u32 i;
1102da8109dSPing-Ke Shih 
1112da8109dSPing-Ke Shih 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
1122da8109dSPing-Ke Shih 		backup_rf_reg_val[i] =
1132da8109dSPing-Ke Shih 			rtw89_read_rf(rtwdev, rf_path,
1142da8109dSPing-Ke Shih 				      rtw8852c_backup_rf_regs[i], RFREG_MASK);
1152da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1162da8109dSPing-Ke Shih 			    "[IQK]backup rf S%d reg : %x, value =%x\n", rf_path,
1172da8109dSPing-Ke Shih 			    rtw8852c_backup_rf_regs[i], backup_rf_reg_val[i]);
1182da8109dSPing-Ke Shih 	}
1192da8109dSPing-Ke Shih }
1202da8109dSPing-Ke Shih 
_rfk_restore_bb_reg(struct rtw89_dev * rtwdev,u32 backup_bb_reg_val[])1212da8109dSPing-Ke Shih static void _rfk_restore_bb_reg(struct rtw89_dev *rtwdev, u32 backup_bb_reg_val[])
1222da8109dSPing-Ke Shih {
1232da8109dSPing-Ke Shih 	u32 i;
1242da8109dSPing-Ke Shih 
1252da8109dSPing-Ke Shih 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
1262da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, rtw8852c_backup_bb_regs[i],
1272da8109dSPing-Ke Shih 				       MASKDWORD, backup_bb_reg_val[i]);
1282da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1292da8109dSPing-Ke Shih 			    "[IQK]restore bb reg : %x, value =%x\n",
1302da8109dSPing-Ke Shih 			    rtw8852c_backup_bb_regs[i], backup_bb_reg_val[i]);
1312da8109dSPing-Ke Shih 	}
1322da8109dSPing-Ke Shih }
1332da8109dSPing-Ke Shih 
_rfk_restore_rf_reg(struct rtw89_dev * rtwdev,u32 backup_rf_reg_val[],u8 rf_path)1342da8109dSPing-Ke Shih static void _rfk_restore_rf_reg(struct rtw89_dev *rtwdev, u32 backup_rf_reg_val[],
1352da8109dSPing-Ke Shih 				u8 rf_path)
1362da8109dSPing-Ke Shih {
1372da8109dSPing-Ke Shih 	u32 i;
1382da8109dSPing-Ke Shih 
1392da8109dSPing-Ke Shih 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
1402da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, rf_path, rtw8852c_backup_rf_regs[i],
1412da8109dSPing-Ke Shih 			       RFREG_MASK, backup_rf_reg_val[i]);
1422da8109dSPing-Ke Shih 
1432da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1442da8109dSPing-Ke Shih 			    "[IQK]restore rf S%d reg: %x, value =%x\n", rf_path,
1452da8109dSPing-Ke Shih 			    rtw8852c_backup_rf_regs[i], backup_rf_reg_val[i]);
1462da8109dSPing-Ke Shih 	}
1472da8109dSPing-Ke Shih }
1482da8109dSPing-Ke Shih 
_wait_rx_mode(struct rtw89_dev * rtwdev,u8 kpath)1492da8109dSPing-Ke Shih static void _wait_rx_mode(struct rtw89_dev *rtwdev, u8 kpath)
1502da8109dSPing-Ke Shih {
1512da8109dSPing-Ke Shih 	u8 path;
1522da8109dSPing-Ke Shih 	u32 rf_mode;
1532da8109dSPing-Ke Shih 	int ret;
1542da8109dSPing-Ke Shih 
1552da8109dSPing-Ke Shih 	for (path = 0; path < RF_PATH_MAX; path++) {
1562da8109dSPing-Ke Shih 		if (!(kpath & BIT(path)))
1572da8109dSPing-Ke Shih 			continue;
1582da8109dSPing-Ke Shih 
1592da8109dSPing-Ke Shih 		ret = read_poll_timeout_atomic(rtw89_read_rf, rf_mode, rf_mode != 2,
1602da8109dSPing-Ke Shih 					       2, 5000, false, rtwdev, path, 0x00,
1612da8109dSPing-Ke Shih 					       RR_MOD_MASK);
1622da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1632da8109dSPing-Ke Shih 			    "[RFK] Wait S%d to Rx mode!! (ret = %d)\n",
1642da8109dSPing-Ke Shih 			    path, ret);
1652da8109dSPing-Ke Shih 	}
1662da8109dSPing-Ke Shih }
1672da8109dSPing-Ke Shih 
_dack_dump(struct rtw89_dev * rtwdev)16876599a8dSPing-Ke Shih static void _dack_dump(struct rtw89_dev *rtwdev)
16976599a8dSPing-Ke Shih {
17076599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
17176599a8dSPing-Ke Shih 	u8 i;
17276599a8dSPing-Ke Shih 	u8 t;
17376599a8dSPing-Ke Shih 
17476599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
17576599a8dSPing-Ke Shih 		    "[DACK]S0 ADC_DCK ic = 0x%x, qc = 0x%x\n",
17676599a8dSPing-Ke Shih 		    dack->addck_d[0][0], dack->addck_d[0][1]);
17776599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
17876599a8dSPing-Ke Shih 		    "[DACK]S1 ADC_DCK ic = 0x%x, qc = 0x%x\n",
17976599a8dSPing-Ke Shih 		    dack->addck_d[1][0], dack->addck_d[1][1]);
18076599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
18176599a8dSPing-Ke Shih 		    "[DACK]S0 DAC_DCK ic = 0x%x, qc = 0x%x\n",
18276599a8dSPing-Ke Shih 		    dack->dadck_d[0][0], dack->dadck_d[0][1]);
18376599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
18476599a8dSPing-Ke Shih 		    "[DACK]S1 DAC_DCK ic = 0x%x, qc = 0x%x\n",
18576599a8dSPing-Ke Shih 		    dack->dadck_d[1][0], dack->dadck_d[1][1]);
18676599a8dSPing-Ke Shih 
18776599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
18876599a8dSPing-Ke Shih 		    "[DACK]S0 biask ic = 0x%x, qc = 0x%x\n",
18976599a8dSPing-Ke Shih 		    dack->biask_d[0][0], dack->biask_d[0][1]);
19076599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
19176599a8dSPing-Ke Shih 		    "[DACK]S1 biask ic = 0x%x, qc = 0x%x\n",
19276599a8dSPing-Ke Shih 		    dack->biask_d[1][0], dack->biask_d[1][1]);
19376599a8dSPing-Ke Shih 
19476599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK ic:\n");
19576599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
19676599a8dSPing-Ke Shih 		t = dack->msbk_d[0][0][i];
19776599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
19876599a8dSPing-Ke Shih 	}
19976599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK qc:\n");
20076599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
20176599a8dSPing-Ke Shih 		t = dack->msbk_d[0][1][i];
20276599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
20376599a8dSPing-Ke Shih 	}
20476599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK ic:\n");
20576599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
20676599a8dSPing-Ke Shih 		t = dack->msbk_d[1][0][i];
20776599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
20876599a8dSPing-Ke Shih 	}
20976599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK qc:\n");
21076599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
21176599a8dSPing-Ke Shih 		t = dack->msbk_d[1][1][i];
21276599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
21376599a8dSPing-Ke Shih 	}
21476599a8dSPing-Ke Shih }
21576599a8dSPing-Ke Shih 
_addck_backup(struct rtw89_dev * rtwdev)21676599a8dSPing-Ke Shih static void _addck_backup(struct rtw89_dev *rtwdev)
21776599a8dSPing-Ke Shih {
21876599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
21976599a8dSPing-Ke Shih 
22076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x0);
22176599a8dSPing-Ke Shih 	dack->addck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0,
22276599a8dSPing-Ke Shih 						    B_ADDCKR0_A0);
22376599a8dSPing-Ke Shih 	dack->addck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0,
22476599a8dSPing-Ke Shih 						    B_ADDCKR0_A1);
22576599a8dSPing-Ke Shih 
22676599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1, 0x0);
22776599a8dSPing-Ke Shih 	dack->addck_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR1,
22876599a8dSPing-Ke Shih 						    B_ADDCKR1_A0);
22976599a8dSPing-Ke Shih 	dack->addck_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR1,
23076599a8dSPing-Ke Shih 						    B_ADDCKR1_A1);
23176599a8dSPing-Ke Shih }
23276599a8dSPing-Ke Shih 
_addck_reload(struct rtw89_dev * rtwdev)23376599a8dSPing-Ke Shih static void _addck_reload(struct rtw89_dev *rtwdev)
23476599a8dSPing-Ke Shih {
23576599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
23676599a8dSPing-Ke Shih 
23776599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL1,
23876599a8dSPing-Ke Shih 			       dack->addck_d[0][0]);
23976599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL0,
24076599a8dSPing-Ke Shih 			       dack->addck_d[0][1]);
24176599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RLS, 0x3);
24276599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RL1,
24376599a8dSPing-Ke Shih 			       dack->addck_d[1][0]);
24476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RL0,
24576599a8dSPing-Ke Shih 			       dack->addck_d[1][1]);
24676599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RLS, 0x3);
24776599a8dSPing-Ke Shih }
24876599a8dSPing-Ke Shih 
_dack_backup_s0(struct rtw89_dev * rtwdev)24976599a8dSPing-Ke Shih static void _dack_backup_s0(struct rtw89_dev *rtwdev)
25076599a8dSPing-Ke Shih {
25176599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
25276599a8dSPing-Ke Shih 	u8 i;
25376599a8dSPing-Ke Shih 
25476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x1);
25576599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
25676599a8dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_V, i);
25776599a8dSPing-Ke Shih 		dack->msbk_d[0][0][i] = rtw89_phy_read32_mask(rtwdev,
25876599a8dSPing-Ke Shih 							      R_DACK_S0P2,
25976599a8dSPing-Ke Shih 							      B_DACK_S0M0);
26076599a8dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DCOF8, B_DCOF8_V, i);
26176599a8dSPing-Ke Shih 		dack->msbk_d[0][1][i] = rtw89_phy_read32_mask(rtwdev,
26276599a8dSPing-Ke Shih 							      R_DACK_S0P3,
26376599a8dSPing-Ke Shih 							      B_DACK_S0M1);
26476599a8dSPing-Ke Shih 	}
26576599a8dSPing-Ke Shih 	dack->biask_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS00,
26676599a8dSPing-Ke Shih 						    B_DACK_BIAS00);
26776599a8dSPing-Ke Shih 	dack->biask_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS01,
26876599a8dSPing-Ke Shih 						    B_DACK_BIAS01);
26976599a8dSPing-Ke Shih 	dack->dadck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK00,
27076599a8dSPing-Ke Shih 						    B_DACK_DADCK00);
27176599a8dSPing-Ke Shih 	dack->dadck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK01,
27276599a8dSPing-Ke Shih 						    B_DACK_DADCK01);
27376599a8dSPing-Ke Shih }
27476599a8dSPing-Ke Shih 
_dack_backup_s1(struct rtw89_dev * rtwdev)27576599a8dSPing-Ke Shih static void _dack_backup_s1(struct rtw89_dev *rtwdev)
27676599a8dSPing-Ke Shih {
27776599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
27876599a8dSPing-Ke Shih 	u8 i;
27976599a8dSPing-Ke Shih 
28076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P1_DBGMOD, B_P1_DBGMOD_ON, 0x1);
28176599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
28276599a8dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DACK10, B_DACK10, i);
28376599a8dSPing-Ke Shih 		dack->msbk_d[1][0][i] = rtw89_phy_read32_mask(rtwdev,
28476599a8dSPing-Ke Shih 							      R_DACK10S,
28576599a8dSPing-Ke Shih 							      B_DACK10S);
28676599a8dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DACK11, B_DACK11, i);
28776599a8dSPing-Ke Shih 		dack->msbk_d[1][1][i] = rtw89_phy_read32_mask(rtwdev,
28876599a8dSPing-Ke Shih 							      R_DACK11S,
28976599a8dSPing-Ke Shih 							      B_DACK11S);
29076599a8dSPing-Ke Shih 	}
29176599a8dSPing-Ke Shih 	dack->biask_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS10,
29276599a8dSPing-Ke Shih 						    B_DACK_BIAS10);
29376599a8dSPing-Ke Shih 	dack->biask_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS11,
29476599a8dSPing-Ke Shih 						    B_DACK_BIAS11);
29576599a8dSPing-Ke Shih 	dack->dadck_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK10,
29676599a8dSPing-Ke Shih 						    B_DACK_DADCK10);
29776599a8dSPing-Ke Shih 	dack->dadck_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK11,
29876599a8dSPing-Ke Shih 						    B_DACK_DADCK11);
29976599a8dSPing-Ke Shih }
30076599a8dSPing-Ke Shih 
_dack_reload_by_path(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 index)30176599a8dSPing-Ke Shih static void _dack_reload_by_path(struct rtw89_dev *rtwdev,
30276599a8dSPing-Ke Shih 				 enum rtw89_rf_path path, u8 index)
30376599a8dSPing-Ke Shih {
30476599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
30576599a8dSPing-Ke Shih 	u32 idx_offset, path_offset;
30676599a8dSPing-Ke Shih 	u32 val32, offset, addr;
30776599a8dSPing-Ke Shih 	u8 i;
30876599a8dSPing-Ke Shih 
30976599a8dSPing-Ke Shih 	idx_offset = (index == 0 ? 0 : 0x14);
31076599a8dSPing-Ke Shih 	path_offset = (path == RF_PATH_A ? 0 : 0x28);
31176599a8dSPing-Ke Shih 	offset = idx_offset + path_offset;
31276599a8dSPing-Ke Shih 
31376599a8dSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_reload_defs_tbl);
31476599a8dSPing-Ke Shih 
31576599a8dSPing-Ke Shih 	/* msbk_d: 15/14/13/12 */
31676599a8dSPing-Ke Shih 	val32 = 0x0;
31776599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
31876599a8dSPing-Ke Shih 		val32 |= dack->msbk_d[path][index][i + 12] << (i * 8);
31976599a8dSPing-Ke Shih 	addr = 0xc200 + offset;
32076599a8dSPing-Ke Shih 	rtw89_phy_write32(rtwdev, addr, val32);
32176599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
32276599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
32376599a8dSPing-Ke Shih 
32476599a8dSPing-Ke Shih 	/* msbk_d: 11/10/9/8 */
32576599a8dSPing-Ke Shih 	val32 = 0x0;
32676599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
32776599a8dSPing-Ke Shih 		val32 |= dack->msbk_d[path][index][i + 8] << (i * 8);
32876599a8dSPing-Ke Shih 	addr = 0xc204 + offset;
32976599a8dSPing-Ke Shih 	rtw89_phy_write32(rtwdev, addr, val32);
33076599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
33176599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
33276599a8dSPing-Ke Shih 
33376599a8dSPing-Ke Shih 	/* msbk_d: 7/6/5/4 */
33476599a8dSPing-Ke Shih 	val32 = 0x0;
33576599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
33676599a8dSPing-Ke Shih 		val32 |= dack->msbk_d[path][index][i + 4] << (i * 8);
33776599a8dSPing-Ke Shih 	addr = 0xc208 + offset;
33876599a8dSPing-Ke Shih 	rtw89_phy_write32(rtwdev, addr, val32);
33976599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
34076599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
34176599a8dSPing-Ke Shih 
34276599a8dSPing-Ke Shih 	/* msbk_d: 3/2/1/0 */
34376599a8dSPing-Ke Shih 	val32 = 0x0;
34476599a8dSPing-Ke Shih 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
34576599a8dSPing-Ke Shih 		val32 |= dack->msbk_d[path][index][i] << (i * 8);
34676599a8dSPing-Ke Shih 	addr = 0xc20c + offset;
34776599a8dSPing-Ke Shih 	rtw89_phy_write32(rtwdev, addr, val32);
34876599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
34976599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
35076599a8dSPing-Ke Shih 
35176599a8dSPing-Ke Shih 	/* dadak_d/biask_d */
35276599a8dSPing-Ke Shih 	val32 = (dack->biask_d[path][index] << 22) |
35376599a8dSPing-Ke Shih 		(dack->dadck_d[path][index] << 14);
35476599a8dSPing-Ke Shih 	addr = 0xc210 + offset;
35576599a8dSPing-Ke Shih 	rtw89_phy_write32(rtwdev, addr, val32);
356b2bab7b1SPing-Ke Shih 	rtw89_phy_write32_set(rtwdev, addr, BIT(0));
35776599a8dSPing-Ke Shih }
35876599a8dSPing-Ke Shih 
_dack_reload(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)35976599a8dSPing-Ke Shih static void _dack_reload(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
36076599a8dSPing-Ke Shih {
36176599a8dSPing-Ke Shih 	u8 i;
36276599a8dSPing-Ke Shih 
36376599a8dSPing-Ke Shih 	for (i = 0; i < 2; i++)
36476599a8dSPing-Ke Shih 		_dack_reload_by_path(rtwdev, path, i);
36576599a8dSPing-Ke Shih }
36676599a8dSPing-Ke Shih 
_addck(struct rtw89_dev * rtwdev)36776599a8dSPing-Ke Shih static void _addck(struct rtw89_dev *rtwdev)
36876599a8dSPing-Ke Shih {
36976599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
37076599a8dSPing-Ke Shih 	u32 val;
37176599a8dSPing-Ke Shih 	int ret;
37276599a8dSPing-Ke Shih 
37376599a8dSPing-Ke Shih 	/* S0 */
37476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x1);
37576599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x1);
37676599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x0);
37776599a8dSPing-Ke Shih 	fsleep(1);
37876599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x1);
37976599a8dSPing-Ke Shih 
38076599a8dSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
38176599a8dSPing-Ke Shih 				       1, 10000, false, rtwdev, 0xc0fc, BIT(0));
38276599a8dSPing-Ke Shih 	if (ret) {
38376599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADDCK timeout\n");
38476599a8dSPing-Ke Shih 		dack->addck_timeout[0] = true;
38576599a8dSPing-Ke Shih 	}
38676599a8dSPing-Ke Shih 
38776599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x0);
38876599a8dSPing-Ke Shih 
38976599a8dSPing-Ke Shih 	/* S1 */
39076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_RST, 0x1);
39176599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_EN, 0x1);
39276599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_EN, 0x0);
39376599a8dSPing-Ke Shih 	udelay(1);
39476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1, 0x1);
39576599a8dSPing-Ke Shih 
39676599a8dSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
39776599a8dSPing-Ke Shih 				       1, 10000, false, rtwdev, 0xc1fc, BIT(0));
39876599a8dSPing-Ke Shih 	if (ret) {
39976599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 ADDCK timeout\n");
40076599a8dSPing-Ke Shih 		dack->addck_timeout[0] = true;
40176599a8dSPing-Ke Shih 	}
40276599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_RST, 0x0);
40376599a8dSPing-Ke Shih }
40476599a8dSPing-Ke Shih 
_dack_reset(struct rtw89_dev * rtwdev,u8 path)40576599a8dSPing-Ke Shih static void _dack_reset(struct rtw89_dev *rtwdev, u8 path)
40676599a8dSPing-Ke Shih {
40776599a8dSPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
40876599a8dSPing-Ke Shih 				 &rtw8852c_dack_reset_defs_a_tbl,
40976599a8dSPing-Ke Shih 				 &rtw8852c_dack_reset_defs_b_tbl);
41076599a8dSPing-Ke Shih }
41176599a8dSPing-Ke Shih 
41276599a8dSPing-Ke Shih enum adc_ck {
41376599a8dSPing-Ke Shih 	ADC_NA = 0,
41476599a8dSPing-Ke Shih 	ADC_480M = 1,
41576599a8dSPing-Ke Shih 	ADC_960M = 2,
41676599a8dSPing-Ke Shih 	ADC_1920M = 3,
41776599a8dSPing-Ke Shih };
41876599a8dSPing-Ke Shih 
41976599a8dSPing-Ke Shih enum dac_ck {
42076599a8dSPing-Ke Shih 	DAC_40M = 0,
42176599a8dSPing-Ke Shih 	DAC_80M = 1,
42276599a8dSPing-Ke Shih 	DAC_120M = 2,
42376599a8dSPing-Ke Shih 	DAC_160M = 3,
42476599a8dSPing-Ke Shih 	DAC_240M = 4,
42576599a8dSPing-Ke Shih 	DAC_320M = 5,
42676599a8dSPing-Ke Shih 	DAC_480M = 6,
42776599a8dSPing-Ke Shih 	DAC_960M = 7,
42876599a8dSPing-Ke Shih };
42976599a8dSPing-Ke Shih 
43076599a8dSPing-Ke Shih enum rf_mode {
43176599a8dSPing-Ke Shih 	RF_SHUT_DOWN = 0x0,
43276599a8dSPing-Ke Shih 	RF_STANDBY = 0x1,
43376599a8dSPing-Ke Shih 	RF_TX = 0x2,
43476599a8dSPing-Ke Shih 	RF_RX = 0x3,
43576599a8dSPing-Ke Shih 	RF_TXIQK = 0x4,
43676599a8dSPing-Ke Shih 	RF_DPK = 0x5,
43776599a8dSPing-Ke Shih 	RF_RXK1 = 0x6,
43876599a8dSPing-Ke Shih 	RF_RXK2 = 0x7,
43976599a8dSPing-Ke Shih };
44076599a8dSPing-Ke Shih 
rtw8852c_txck_force(struct rtw89_dev * rtwdev,u8 path,bool force,enum dac_ck ck)44176599a8dSPing-Ke Shih static void rtw8852c_txck_force(struct rtw89_dev *rtwdev, u8 path, bool force,
44276599a8dSPing-Ke Shih 				enum dac_ck ck)
44376599a8dSPing-Ke Shih {
44476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_ON, 0x0);
44576599a8dSPing-Ke Shih 
44676599a8dSPing-Ke Shih 	if (!force)
44776599a8dSPing-Ke Shih 		return;
44876599a8dSPing-Ke Shih 
44976599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_VAL, ck);
45076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_ON, 0x1);
45176599a8dSPing-Ke Shih }
45276599a8dSPing-Ke Shih 
rtw8852c_rxck_force(struct rtw89_dev * rtwdev,u8 path,bool force,enum adc_ck ck)45376599a8dSPing-Ke Shih static void rtw8852c_rxck_force(struct rtw89_dev *rtwdev, u8 path, bool force,
45476599a8dSPing-Ke Shih 				enum adc_ck ck)
45576599a8dSPing-Ke Shih {
4563aa83062SChih-Kang Chang 	const struct rxck_def *def;
4573aa83062SChih-Kang Chang 
45876599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_ON, 0x0);
45976599a8dSPing-Ke Shih 
46076599a8dSPing-Ke Shih 	if (!force)
46176599a8dSPing-Ke Shih 		return;
46276599a8dSPing-Ke Shih 
46376599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_VAL, ck);
46476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_ON, 0x1);
4653aa83062SChih-Kang Chang 
4663aa83062SChih-Kang Chang 	switch (ck) {
4673aa83062SChih-Kang Chang 	case ADC_480M:
4683aa83062SChih-Kang Chang 		def = &_ck480M;
4693aa83062SChih-Kang Chang 		break;
4703aa83062SChih-Kang Chang 	case ADC_960M:
4713aa83062SChih-Kang Chang 		def = &_ck960M;
4723aa83062SChih-Kang Chang 		break;
4733aa83062SChih-Kang Chang 	case ADC_1920M:
4743aa83062SChih-Kang Chang 	default:
4753aa83062SChih-Kang Chang 		def = &_ck1920M;
4763aa83062SChih-Kang Chang 		break;
4773aa83062SChih-Kang Chang 	}
4783aa83062SChih-Kang Chang 
4793aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_CTL, def->ctl);
4803aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_EN, def->en);
4813aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, def->bw0);
4823aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, def->bw1);
4833aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_DRCK | (path << 8), B_DRCK_MUL, def->mul);
4843aa83062SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_ADCMOD | (path << 8), B_ADCMOD_LP, def->lp);
48576599a8dSPing-Ke Shih }
48676599a8dSPing-Ke Shih 
_check_dack_done(struct rtw89_dev * rtwdev,bool s0)48776599a8dSPing-Ke Shih static bool _check_dack_done(struct rtw89_dev *rtwdev, bool s0)
48876599a8dSPing-Ke Shih {
48976599a8dSPing-Ke Shih 	if (s0) {
49076599a8dSPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_DACK_S0P0, B_DACK_S0P0_OK) == 0 ||
49176599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P1, B_DACK_S0P1_OK) == 0 ||
49276599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0P2_OK) == 0 ||
49376599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0P3_OK) == 0)
49476599a8dSPing-Ke Shih 			return false;
49576599a8dSPing-Ke Shih 	} else {
49676599a8dSPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_DACK_S1P0, B_DACK_S1P0_OK) == 0 ||
49776599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P1, B_DACK_S1P1_OK) == 0 ||
49876599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P2, B_DACK_S1P2_OK) == 0 ||
49976599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P3, B_DACK_S1P3_OK) == 0)
50076599a8dSPing-Ke Shih 			return false;
50176599a8dSPing-Ke Shih 	}
50276599a8dSPing-Ke Shih 
50376599a8dSPing-Ke Shih 	return true;
50476599a8dSPing-Ke Shih }
50576599a8dSPing-Ke Shih 
_dack_s0(struct rtw89_dev * rtwdev)50676599a8dSPing-Ke Shih static void _dack_s0(struct rtw89_dev *rtwdev)
50776599a8dSPing-Ke Shih {
50876599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
50976599a8dSPing-Ke Shih 	bool done;
51076599a8dSPing-Ke Shih 	int ret;
51176599a8dSPing-Ke Shih 
51276599a8dSPing-Ke Shih 	rtw8852c_txck_force(rtwdev, RF_PATH_A, true, DAC_160M);
51376599a8dSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_defs_s0_tbl);
51476599a8dSPing-Ke Shih 
51576599a8dSPing-Ke Shih 	_dack_reset(rtwdev, RF_PATH_A);
51676599a8dSPing-Ke Shih 
51776599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x1);
51876599a8dSPing-Ke Shih 	ret = read_poll_timeout_atomic(_check_dack_done, done, done,
51976599a8dSPing-Ke Shih 				       1, 10000, false, rtwdev, true);
52076599a8dSPing-Ke Shih 	if (ret) {
52176599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DACK timeout\n");
52276599a8dSPing-Ke Shih 		dack->msbk_timeout[0] = true;
52376599a8dSPing-Ke Shih 	}
52476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x0);
52576599a8dSPing-Ke Shih 	rtw8852c_txck_force(rtwdev, RF_PATH_A, false, DAC_960M);
52676599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S0 DADCK\n");
52776599a8dSPing-Ke Shih 
52876599a8dSPing-Ke Shih 	_dack_backup_s0(rtwdev);
52976599a8dSPing-Ke Shih 	_dack_reload(rtwdev, RF_PATH_A);
53076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x0);
53176599a8dSPing-Ke Shih }
53276599a8dSPing-Ke Shih 
_dack_s1(struct rtw89_dev * rtwdev)53376599a8dSPing-Ke Shih static void _dack_s1(struct rtw89_dev *rtwdev)
53476599a8dSPing-Ke Shih {
53576599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
53676599a8dSPing-Ke Shih 	bool done;
53776599a8dSPing-Ke Shih 	int ret;
53876599a8dSPing-Ke Shih 
53976599a8dSPing-Ke Shih 	rtw8852c_txck_force(rtwdev, RF_PATH_B, true, DAC_160M);
54076599a8dSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_defs_s1_tbl);
54176599a8dSPing-Ke Shih 
54276599a8dSPing-Ke Shih 	_dack_reset(rtwdev, RF_PATH_B);
54376599a8dSPing-Ke Shih 
54476599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DACK1_K, B_DACK1_EN, 0x1);
54576599a8dSPing-Ke Shih 	ret = read_poll_timeout_atomic(_check_dack_done, done, done,
54676599a8dSPing-Ke Shih 				       1, 10000, false, rtwdev, false);
54776599a8dSPing-Ke Shih 	if (ret) {
54876599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 DACK timeout\n");
54976599a8dSPing-Ke Shih 		dack->msbk_timeout[0] = true;
55076599a8dSPing-Ke Shih 	}
55176599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DACK1_K, B_DACK1_EN, 0x0);
55276599a8dSPing-Ke Shih 	rtw8852c_txck_force(rtwdev, RF_PATH_B, false, DAC_960M);
55376599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S1 DADCK\n");
55476599a8dSPing-Ke Shih 
55576599a8dSPing-Ke Shih 	_dack_backup_s1(rtwdev);
55676599a8dSPing-Ke Shih 	_dack_reload(rtwdev, RF_PATH_B);
55776599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P1_DBGMOD, B_P1_DBGMOD_ON, 0x0);
55876599a8dSPing-Ke Shih }
55976599a8dSPing-Ke Shih 
_dack(struct rtw89_dev * rtwdev)56076599a8dSPing-Ke Shih static void _dack(struct rtw89_dev *rtwdev)
56176599a8dSPing-Ke Shih {
56276599a8dSPing-Ke Shih 	_dack_s0(rtwdev);
56376599a8dSPing-Ke Shih 	_dack_s1(rtwdev);
56476599a8dSPing-Ke Shih }
56576599a8dSPing-Ke Shih 
_drck(struct rtw89_dev * rtwdev)56676599a8dSPing-Ke Shih static void _drck(struct rtw89_dev *rtwdev)
56776599a8dSPing-Ke Shih {
56876599a8dSPing-Ke Shih 	u32 val;
56976599a8dSPing-Ke Shih 	int ret;
57076599a8dSPing-Ke Shih 
57176599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x1);
57276599a8dSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
57376599a8dSPing-Ke Shih 				       1, 10000, false, rtwdev, 0xc0c8, BIT(3));
57476599a8dSPing-Ke Shih 	if (ret)
57576599a8dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,  "[DACK]DRCK timeout\n");
57676599a8dSPing-Ke Shih 
57776599a8dSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_drck_defs_tbl);
57876599a8dSPing-Ke Shih 
57976599a8dSPing-Ke Shih 	val = rtw89_phy_read32_mask(rtwdev, R_DRCK_RES, B_DRCK_RES);
58076599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x0);
58176599a8dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_VAL, val);
58276599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0xc0c4 = 0x%x\n",
58376599a8dSPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_DRCK, MASKDWORD));
58476599a8dSPing-Ke Shih }
58576599a8dSPing-Ke Shih 
_dac_cal(struct rtw89_dev * rtwdev,bool force)58676599a8dSPing-Ke Shih static void _dac_cal(struct rtw89_dev *rtwdev, bool force)
58776599a8dSPing-Ke Shih {
58876599a8dSPing-Ke Shih 	struct rtw89_dack_info *dack = &rtwdev->dack;
58976599a8dSPing-Ke Shih 	u32 rf0_0, rf1_0;
59076599a8dSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, RTW89_PHY_0, RF_AB);
59176599a8dSPing-Ke Shih 
59276599a8dSPing-Ke Shih 	dack->dack_done = false;
59376599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK b\n");
59476599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK start!!!\n");
59576599a8dSPing-Ke Shih 	rf0_0 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK);
59676599a8dSPing-Ke Shih 	rf1_0 = rtw89_read_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK);
59776599a8dSPing-Ke Shih 	_drck(rtwdev);
59876599a8dSPing-Ke Shih 
59976599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x0);
60076599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x0);
60176599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x337e1);
60276599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK, 0x337e1);
60376599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_START);
60476599a8dSPing-Ke Shih 	_addck(rtwdev);
60576599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_STOP);
60676599a8dSPing-Ke Shih 
60776599a8dSPing-Ke Shih 	_addck_backup(rtwdev);
60876599a8dSPing-Ke Shih 	_addck_reload(rtwdev);
60976599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MODOPT, RFREG_MASK, 0x0);
61076599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MODOPT, RFREG_MASK, 0x0);
61176599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_START);
61276599a8dSPing-Ke Shih 	_dack(rtwdev);
61376599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_STOP);
61476599a8dSPing-Ke Shih 
61576599a8dSPing-Ke Shih 	_dack_dump(rtwdev);
61676599a8dSPing-Ke Shih 	dack->dack_done = true;
61776599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, rf0_0);
61876599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK, rf1_0);
61976599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x1);
62076599a8dSPing-Ke Shih 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x1);
62176599a8dSPing-Ke Shih 	dack->dack_cnt++;
62276599a8dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK finish!!!\n");
62376599a8dSPing-Ke Shih }
62476599a8dSPing-Ke Shih 
6252da8109dSPing-Ke Shih #define RTW8852C_NCTL_VER 0xd
6262da8109dSPing-Ke Shih #define RTW8852C_IQK_VER 0x2a
6272da8109dSPing-Ke Shih #define RTW8852C_IQK_SS 2
6282da8109dSPing-Ke Shih #define RTW8852C_IQK_THR_REK 8
6292da8109dSPing-Ke Shih #define RTW8852C_IQK_CFIR_GROUP_NR 4
6302da8109dSPing-Ke Shih 
6312da8109dSPing-Ke Shih enum rtw8852c_iqk_type {
6322da8109dSPing-Ke Shih 	ID_TXAGC,
6332da8109dSPing-Ke Shih 	ID_G_FLOK_COARSE,
6342da8109dSPing-Ke Shih 	ID_A_FLOK_COARSE,
6352da8109dSPing-Ke Shih 	ID_G_FLOK_FINE,
6362da8109dSPing-Ke Shih 	ID_A_FLOK_FINE,
6372da8109dSPing-Ke Shih 	ID_FLOK_VBUFFER,
6382da8109dSPing-Ke Shih 	ID_TXK,
6392da8109dSPing-Ke Shih 	ID_RXAGC,
6402da8109dSPing-Ke Shih 	ID_RXK,
6412da8109dSPing-Ke Shih 	ID_NBTXK,
6422da8109dSPing-Ke Shih 	ID_NBRXK,
6432da8109dSPing-Ke Shih };
6442da8109dSPing-Ke Shih 
rtw8852c_disable_rxagc(struct rtw89_dev * rtwdev,u8 path,u8 en_rxgac)6452da8109dSPing-Ke Shih static void rtw8852c_disable_rxagc(struct rtw89_dev *rtwdev, u8 path, u8 en_rxgac)
6462da8109dSPing-Ke Shih {
6472da8109dSPing-Ke Shih 	if (path == RF_PATH_A)
6482da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_AGC_CTL, B_P0_AGC_EN, en_rxgac);
6492da8109dSPing-Ke Shih 	else
6502da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_AGC_CTL, B_P1_AGC_EN, en_rxgac);
6512da8109dSPing-Ke Shih }
6522da8109dSPing-Ke Shih 
_iqk_rxk_setting(struct rtw89_dev * rtwdev,u8 path)6532da8109dSPing-Ke Shih static void _iqk_rxk_setting(struct rtw89_dev *rtwdev, u8 path)
6542da8109dSPing-Ke Shih {
6552da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
6562da8109dSPing-Ke Shih 
6572da8109dSPing-Ke Shih 	if (path == RF_PATH_A)
6582da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0101);
6592da8109dSPing-Ke Shih 	else
6602da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0202);
6612da8109dSPing-Ke Shih 
6622da8109dSPing-Ke Shih 	switch (iqk_info->iqk_bw[path]) {
6632da8109dSPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
6642da8109dSPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
6652da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
6662da8109dSPing-Ke Shih 		rtw8852c_rxck_force(rtwdev, path, true, ADC_480M);
6672da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x0);
6682da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
6692da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
6702da8109dSPing-Ke Shih 		break;
6712da8109dSPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
6722da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
6732da8109dSPing-Ke Shih 		rtw8852c_rxck_force(rtwdev, path, true, ADC_960M);
6742da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x1);
6752da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
6762da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
6772da8109dSPing-Ke Shih 	break;
6782da8109dSPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
6792da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
6802da8109dSPing-Ke Shih 		rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
6812da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x2);
6822da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
6832da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
6842da8109dSPing-Ke Shih 		break;
6852da8109dSPing-Ke Shih 	default:
6862da8109dSPing-Ke Shih 		break;
6872da8109dSPing-Ke Shih 	}
6882da8109dSPing-Ke Shih 
6892da8109dSPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_iqk_rxk_cfg_defs_tbl);
6902da8109dSPing-Ke Shih 
6912da8109dSPing-Ke Shih 	if (path == RF_PATH_A)
6922da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x1101);
6932da8109dSPing-Ke Shih 	else
6942da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x2202);
6952da8109dSPing-Ke Shih }
6962da8109dSPing-Ke Shih 
_iqk_check_cal(struct rtw89_dev * rtwdev,u8 path,u8 ktype)6972da8109dSPing-Ke Shih static bool _iqk_check_cal(struct rtw89_dev *rtwdev, u8 path, u8 ktype)
6982da8109dSPing-Ke Shih {
6992da8109dSPing-Ke Shih 	u32 tmp;
7002da8109dSPing-Ke Shih 	u32 val;
7012da8109dSPing-Ke Shih 	int ret;
7022da8109dSPing-Ke Shih 
7032da8109dSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
7042da8109dSPing-Ke Shih 				       1, 8200, false, rtwdev, 0xbff8, MASKBYTE0);
7052da8109dSPing-Ke Shih 	if (ret)
7062da8109dSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]IQK timeout!!!\n");
7072da8109dSPing-Ke Shih 
7082da8109dSPing-Ke Shih 	rtw89_phy_write32_clr(rtwdev, R_NCTL_N1, MASKBYTE0);
7092da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, ret=%d\n", path, ret);
7102da8109dSPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD);
7112da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
7122da8109dSPing-Ke Shih 		    "[IQK]S%x, type= %x, 0x8008 = 0x%x\n", path, ktype, tmp);
7132da8109dSPing-Ke Shih 
7142da8109dSPing-Ke Shih 	return false;
7152da8109dSPing-Ke Shih }
7162da8109dSPing-Ke Shih 
_iqk_one_shot(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path,u8 ktype)7172da8109dSPing-Ke Shih static bool _iqk_one_shot(struct rtw89_dev *rtwdev,
7182da8109dSPing-Ke Shih 			  enum rtw89_phy_idx phy_idx, u8 path, u8 ktype)
7192da8109dSPing-Ke Shih {
7202da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
7212da8109dSPing-Ke Shih 	u32 addr_rfc_ctl = R_UPD_CLK + (path << 13);
7222da8109dSPing-Ke Shih 	u32 iqk_cmd;
7232da8109dSPing-Ke Shih 	bool fail;
7242da8109dSPing-Ke Shih 
7252da8109dSPing-Ke Shih 	switch (ktype) {
7262da8109dSPing-Ke Shih 	case ID_TXAGC:
7272da8109dSPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (4 + path)) | (path << 1);
7282da8109dSPing-Ke Shih 		break;
7292da8109dSPing-Ke Shih 	case ID_A_FLOK_COARSE:
7302da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7312da8109dSPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (4 + path));
7322da8109dSPing-Ke Shih 		break;
7332da8109dSPing-Ke Shih 	case ID_G_FLOK_COARSE:
7342da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7352da8109dSPing-Ke Shih 		iqk_cmd = 0x108 | (1 << (4 + path));
7362da8109dSPing-Ke Shih 		break;
7372da8109dSPing-Ke Shih 	case ID_A_FLOK_FINE:
7382da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7392da8109dSPing-Ke Shih 		iqk_cmd = 0x508 | (1 << (4 + path));
7402da8109dSPing-Ke Shih 		break;
7412da8109dSPing-Ke Shih 	case ID_G_FLOK_FINE:
7422da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7432da8109dSPing-Ke Shih 		iqk_cmd = 0x208 | (1 << (4 + path));
7442da8109dSPing-Ke Shih 		break;
7452da8109dSPing-Ke Shih 	case ID_FLOK_VBUFFER:
7462da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7472da8109dSPing-Ke Shih 		iqk_cmd = 0x308 | (1 << (4 + path));
7482da8109dSPing-Ke Shih 		break;
7492da8109dSPing-Ke Shih 	case ID_TXK:
7502da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
7512da8109dSPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (4 + path)) | ((0x8 + iqk_info->iqk_bw[path]) << 8);
7522da8109dSPing-Ke Shih 		break;
7532da8109dSPing-Ke Shih 	case ID_RXAGC:
7542da8109dSPing-Ke Shih 		iqk_cmd = 0x508 | (1 << (4 + path)) | (path << 1);
7552da8109dSPing-Ke Shih 		break;
7562da8109dSPing-Ke Shih 	case ID_RXK:
7572da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7582da8109dSPing-Ke Shih 		iqk_cmd = 0x008 | (1 << (4 + path)) | ((0xc + iqk_info->iqk_bw[path]) << 8);
7592da8109dSPing-Ke Shih 		break;
7602da8109dSPing-Ke Shih 	case ID_NBTXK:
7612da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
7622da8109dSPing-Ke Shih 		iqk_cmd = 0x408 | (1 << (4 + path));
7632da8109dSPing-Ke Shih 		break;
7642da8109dSPing-Ke Shih 	case ID_NBRXK:
7652da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
7662da8109dSPing-Ke Shih 		iqk_cmd = 0x608 | (1 << (4 + path));
7672da8109dSPing-Ke Shih 		break;
7682da8109dSPing-Ke Shih 	default:
7692da8109dSPing-Ke Shih 		return false;
7702da8109dSPing-Ke Shih 	}
7712da8109dSPing-Ke Shih 
7722da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, iqk_cmd + 1);
7732da8109dSPing-Ke Shih 	fsleep(15);
7742da8109dSPing-Ke Shih 	fail = _iqk_check_cal(rtwdev, path, ktype);
7752da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
7762da8109dSPing-Ke Shih 
7772da8109dSPing-Ke Shih 	return fail;
7782da8109dSPing-Ke Shih }
7792da8109dSPing-Ke Shih 
_rxk_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)7802da8109dSPing-Ke Shih static bool _rxk_group_sel(struct rtw89_dev *rtwdev,
7812da8109dSPing-Ke Shih 			   enum rtw89_phy_idx phy_idx, u8 path)
7822da8109dSPing-Ke Shih {
7832da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
7842da8109dSPing-Ke Shih 	bool fail;
7852da8109dSPing-Ke Shih 	u32 tmp;
7862da8109dSPing-Ke Shih 	u32 bkrf0;
7872da8109dSPing-Ke Shih 	u8 gp;
7882da8109dSPing-Ke Shih 
7892da8109dSPing-Ke Shih 	bkrf0 = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_NBW);
7902da8109dSPing-Ke Shih 	if (path == RF_PATH_B) {
7912da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_IQKPLL, RR_IQKPLL_MOD, 0x3);
7922da8109dSPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_MOD);
7932da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_AGH, tmp);
7942da8109dSPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_TXRX);
7952da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_PLLCH, tmp);
7962da8109dSPing-Ke Shih 	}
7972da8109dSPing-Ke Shih 
7982da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
7992da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
8002da8109dSPing-Ke Shih 	default:
8012da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8022da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8032da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXG, RR_RXG_IQKMOD, 0x9);
8042da8109dSPing-Ke Shih 		break;
8052da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
8062da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8072da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8082da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x8);
8092da8109dSPing-Ke Shih 		break;
8102da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
8112da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8122da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8132da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x9);
8142da8109dSPing-Ke Shih 		break;
8152da8109dSPing-Ke Shih 	}
8162da8109dSPing-Ke Shih 
8172da8109dSPing-Ke Shih 	fsleep(10);
8182da8109dSPing-Ke Shih 
8192da8109dSPing-Ke Shih 	for (gp = 0; gp < RXK_GROUP_NR; gp++) {
8202da8109dSPing-Ke Shih 		switch (iqk_info->iqk_band[path]) {
8212da8109dSPing-Ke Shih 		case RTW89_BAND_2G:
8222da8109dSPing-Ke Shih 		default:
8232da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
8242da8109dSPing-Ke Shih 				       _rxk_g_idxrxgain[gp]);
8252da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_VOBUF,
8262da8109dSPing-Ke Shih 				       _rxk_g_idxattc2[gp]);
8272da8109dSPing-Ke Shih 			break;
8282da8109dSPing-Ke Shih 		case RTW89_BAND_5G:
8292da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
8302da8109dSPing-Ke Shih 				       _rxk_a_idxrxgain[gp]);
8312da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT,
8322da8109dSPing-Ke Shih 				       _rxk_a_idxattc2[gp]);
8332da8109dSPing-Ke Shih 			break;
8342da8109dSPing-Ke Shih 		case RTW89_BAND_6G:
8352da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
8362da8109dSPing-Ke Shih 				       _rxk_a6_idxrxgain[gp]);
8372da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT,
8382da8109dSPing-Ke Shih 				       _rxk_a6_idxattc2[gp]);
8392da8109dSPing-Ke Shih 			break;
8402da8109dSPing-Ke Shih 		}
8412da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
8422da8109dSPing-Ke Shih 				       B_CFIR_LUT_SEL, 0x1);
8432da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
8442da8109dSPing-Ke Shih 				       B_CFIR_LUT_SET, 0x0);
8452da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
8462da8109dSPing-Ke Shih 				       B_CFIR_LUT_GP_V1, gp);
8472da8109dSPing-Ke Shih 		fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
8482da8109dSPing-Ke Shih 	}
8492da8109dSPing-Ke Shih 
8502da8109dSPing-Ke Shih 	if (path == RF_PATH_B)
8512da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_IQKPLL, RR_IQKPLL_MOD, 0x0);
8522da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, bkrf0);
8532da8109dSPing-Ke Shih 
8542da8109dSPing-Ke Shih 	if (fail) {
8552da8109dSPing-Ke Shih 		iqk_info->nb_rxcfir[path] = 0x40000002;
8562da8109dSPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = false;
8572da8109dSPing-Ke Shih 	} else {
8582da8109dSPing-Ke Shih 		iqk_info->nb_rxcfir[path] = 0x40000000;
8592da8109dSPing-Ke Shih 		iqk_info->is_wb_rxiqk[path] = true;
8602da8109dSPing-Ke Shih 	}
8612da8109dSPing-Ke Shih 
8622da8109dSPing-Ke Shih 	return false;
8632da8109dSPing-Ke Shih }
8642da8109dSPing-Ke Shih 
_iqk_nbrxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)8652da8109dSPing-Ke Shih static bool _iqk_nbrxk(struct rtw89_dev *rtwdev,
8662da8109dSPing-Ke Shih 		       enum rtw89_phy_idx phy_idx, u8 path)
8672da8109dSPing-Ke Shih {
8682da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
8692da8109dSPing-Ke Shih 	bool fail;
8702da8109dSPing-Ke Shih 	u32 tmp;
8712da8109dSPing-Ke Shih 	u32 bkrf0;
8722da8109dSPing-Ke Shih 	u8 gp = 0x2;
8732da8109dSPing-Ke Shih 
8742da8109dSPing-Ke Shih 	bkrf0 = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_NBW);
8752da8109dSPing-Ke Shih 	if (path == RF_PATH_B) {
8762da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_IQKPLL, RR_IQKPLL_MOD, 0x3);
8772da8109dSPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_MOD);
8782da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_AGH, tmp);
8792da8109dSPing-Ke Shih 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_TXRX);
8802da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_PLLCH, tmp);
8812da8109dSPing-Ke Shih 	}
8822da8109dSPing-Ke Shih 
8832da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
8842da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
8852da8109dSPing-Ke Shih 	default:
8862da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8872da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8882da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXG, RR_RXG_IQKMOD, 0x9);
8892da8109dSPing-Ke Shih 		break;
8902da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
8912da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8922da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8932da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x8);
8942da8109dSPing-Ke Shih 		break;
8952da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
8962da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
8972da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
8982da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x9);
8992da8109dSPing-Ke Shih 		break;
9002da8109dSPing-Ke Shih 	}
9012da8109dSPing-Ke Shih 
9022da8109dSPing-Ke Shih 	fsleep(10);
9032da8109dSPing-Ke Shih 
9042da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
9052da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
9062da8109dSPing-Ke Shih 	default:
9072da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_g_idxrxgain[gp]);
9082da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_VOBUF, _rxk_g_idxattc2[gp]);
9092da8109dSPing-Ke Shih 		break;
9102da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
9112da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_a_idxrxgain[gp]);
9122da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT, _rxk_a_idxattc2[gp]);
9132da8109dSPing-Ke Shih 		break;
9142da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
9152da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_a6_idxrxgain[gp]);
9162da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT, _rxk_a6_idxattc2[gp]);
9172da8109dSPing-Ke Shih 		break;
9182da8109dSPing-Ke Shih 	}
9192da8109dSPing-Ke Shih 
9202da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SEL, 0x1);
9212da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SET, 0x0);
9222da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_GP_V1, gp);
9232da8109dSPing-Ke Shih 	fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
9242da8109dSPing-Ke Shih 
9252da8109dSPing-Ke Shih 	if (path == RF_PATH_B)
9262da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_IQKPLL, RR_IQKPLL_MOD, 0x0);
9272da8109dSPing-Ke Shih 
9282da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, bkrf0);
9292da8109dSPing-Ke Shih 
9302da8109dSPing-Ke Shih 	if (fail)
9312da8109dSPing-Ke Shih 		iqk_info->nb_rxcfir[path] =
9322da8109dSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8),
9332da8109dSPing-Ke Shih 					      MASKDWORD) | 0x2;
9342da8109dSPing-Ke Shih 	else
9352da8109dSPing-Ke Shih 		iqk_info->nb_rxcfir[path] = 0x40000002;
9362da8109dSPing-Ke Shih 
9372da8109dSPing-Ke Shih 	iqk_info->is_wb_rxiqk[path] = false;
9382da8109dSPing-Ke Shih 	return fail;
9392da8109dSPing-Ke Shih }
9402da8109dSPing-Ke Shih 
_txk_group_sel(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)9412da8109dSPing-Ke Shih static bool _txk_group_sel(struct rtw89_dev *rtwdev,
9422da8109dSPing-Ke Shih 			   enum rtw89_phy_idx phy_idx, u8 path)
9432da8109dSPing-Ke Shih {
9442da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
9452da8109dSPing-Ke Shih 	bool fail;
9462da8109dSPing-Ke Shih 	u8 gp;
9472da8109dSPing-Ke Shih 
9482da8109dSPing-Ke Shih 	for (gp = 0; gp < TXK_GROUP_NR; gp++) {
9492da8109dSPing-Ke Shih 		switch (iqk_info->iqk_band[path]) {
9502da8109dSPing-Ke Shih 		case RTW89_BAND_2G:
9512da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
9522da8109dSPing-Ke Shih 				       _txk_g_power_range[gp]);
9532da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
9542da8109dSPing-Ke Shih 				       _txk_g_track_range[gp]);
9552da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
9562da8109dSPing-Ke Shih 				       _txk_g_gain_bb[gp]);
9572da8109dSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
9582da8109dSPing-Ke Shih 					       R_KIP_IQP + (path << 8),
9592da8109dSPing-Ke Shih 					       MASKDWORD, _txk_g_itqt[gp]);
9602da8109dSPing-Ke Shih 			break;
9612da8109dSPing-Ke Shih 		case RTW89_BAND_5G:
9622da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
9632da8109dSPing-Ke Shih 				       _txk_a_power_range[gp]);
9642da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
9652da8109dSPing-Ke Shih 				       _txk_a_track_range[gp]);
9662da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
9672da8109dSPing-Ke Shih 				       _txk_a_gain_bb[gp]);
9682da8109dSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
9692da8109dSPing-Ke Shih 					       R_KIP_IQP + (path << 8),
9702da8109dSPing-Ke Shih 					       MASKDWORD, _txk_a_itqt[gp]);
9712da8109dSPing-Ke Shih 			break;
9722da8109dSPing-Ke Shih 		case RTW89_BAND_6G:
9732da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
9742da8109dSPing-Ke Shih 				       _txk_a6_power_range[gp]);
9752da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
9762da8109dSPing-Ke Shih 				       _txk_a6_track_range[gp]);
9772da8109dSPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
9782da8109dSPing-Ke Shih 				       _txk_a6_gain_bb[gp]);
9792da8109dSPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev,
9802da8109dSPing-Ke Shih 					       R_KIP_IQP + (path << 8),
9812da8109dSPing-Ke Shih 					       MASKDWORD, _txk_a6_itqt[gp]);
9822da8109dSPing-Ke Shih 			break;
9832da8109dSPing-Ke Shih 		default:
9842da8109dSPing-Ke Shih 			break;
9852da8109dSPing-Ke Shih 		}
9862da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
9872da8109dSPing-Ke Shih 				       B_CFIR_LUT_SEL, 0x1);
9882da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
9892da8109dSPing-Ke Shih 				       B_CFIR_LUT_SET, 0x1);
9902da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
9912da8109dSPing-Ke Shih 				       B_CFIR_LUT_G2, 0x0);
9922da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
9932da8109dSPing-Ke Shih 				       B_CFIR_LUT_GP, gp + 1);
9942da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x00b);
9952da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
9962da8109dSPing-Ke Shih 		fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
9972da8109dSPing-Ke Shih 	}
9982da8109dSPing-Ke Shih 
9992da8109dSPing-Ke Shih 	if (fail) {
10002da8109dSPing-Ke Shih 		iqk_info->nb_txcfir[path] = 0x40000002;
10012da8109dSPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = false;
10022da8109dSPing-Ke Shih 	} else {
10032da8109dSPing-Ke Shih 		iqk_info->nb_txcfir[path] = 0x40000000;
10042da8109dSPing-Ke Shih 		iqk_info->is_wb_txiqk[path] = true;
10052da8109dSPing-Ke Shih 	}
10062da8109dSPing-Ke Shih 
10072da8109dSPing-Ke Shih 	return fail;
10082da8109dSPing-Ke Shih }
10092da8109dSPing-Ke Shih 
_iqk_nbtxk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)10102da8109dSPing-Ke Shih static bool _iqk_nbtxk(struct rtw89_dev *rtwdev,
10112da8109dSPing-Ke Shih 		       enum rtw89_phy_idx phy_idx, u8 path)
10122da8109dSPing-Ke Shih {
10132da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
10142da8109dSPing-Ke Shih 	bool fail;
10152da8109dSPing-Ke Shih 	u8 gp = 0x2;
10162da8109dSPing-Ke Shih 
10172da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
10182da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
10192da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_g_power_range[gp]);
10202da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_g_track_range[gp]);
10212da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_g_gain_bb[gp]);
10222da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
10232da8109dSPing-Ke Shih 				       MASKDWORD, _txk_g_itqt[gp]);
10242da8109dSPing-Ke Shih 		break;
10252da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
10262da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_a_power_range[gp]);
10272da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_a_track_range[gp]);
10282da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_a_gain_bb[gp]);
10292da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
10302da8109dSPing-Ke Shih 				       MASKDWORD, _txk_a_itqt[gp]);
10312da8109dSPing-Ke Shih 		break;
10322da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
10332da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_a6_power_range[gp]);
10342da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_a6_track_range[gp]);
10352da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_a6_gain_bb[gp]);
10362da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
10372da8109dSPing-Ke Shih 				       MASKDWORD, _txk_a6_itqt[gp]);
10382da8109dSPing-Ke Shih 	break;
10392da8109dSPing-Ke Shih 	default:
10402da8109dSPing-Ke Shih 		break;
10412da8109dSPing-Ke Shih 	}
10422da8109dSPing-Ke Shih 
10432da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SEL, 0x1);
10442da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SET, 0x1);
10452da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_G2, 0x0);
10462da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_GP, gp + 1);
10472da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x00b);
10482da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
10492da8109dSPing-Ke Shih 	fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
10502da8109dSPing-Ke Shih 
10512da8109dSPing-Ke Shih 	if (!fail)
10522da8109dSPing-Ke Shih 		iqk_info->nb_txcfir[path] =
10532da8109dSPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8),
10542da8109dSPing-Ke Shih 					      MASKDWORD) | 0x2;
10552da8109dSPing-Ke Shih 	else
10562da8109dSPing-Ke Shih 		iqk_info->nb_txcfir[path] = 0x40000002;
10572da8109dSPing-Ke Shih 
10582da8109dSPing-Ke Shih 	iqk_info->is_wb_txiqk[path] = false;
10592da8109dSPing-Ke Shih 
10602da8109dSPing-Ke Shih 	return fail;
10612da8109dSPing-Ke Shih }
10622da8109dSPing-Ke Shih 
_lok_finetune_check(struct rtw89_dev * rtwdev,u8 path)10632da8109dSPing-Ke Shih static bool _lok_finetune_check(struct rtw89_dev *rtwdev, u8 path)
10642da8109dSPing-Ke Shih {
106538f25decSZong-Zhe Yang 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
10662da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
106738f25decSZong-Zhe Yang 	u8 idx = rfk_mcc->table_idx;
10682da8109dSPing-Ke Shih 	bool is_fail1,  is_fail2;
10692da8109dSPing-Ke Shih 	u32 val;
10702da8109dSPing-Ke Shih 	u32 core_i;
10712da8109dSPing-Ke Shih 	u32 core_q;
10722da8109dSPing-Ke Shih 	u32 vbuff_i;
10732da8109dSPing-Ke Shih 	u32 vbuff_q;
10742da8109dSPing-Ke Shih 
10752da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
10762da8109dSPing-Ke Shih 	val = rtw89_read_rf(rtwdev,  path, RR_TXMO, RFREG_MASK);
10772da8109dSPing-Ke Shih 	core_i = FIELD_GET(RR_TXMO_COI, val);
10782da8109dSPing-Ke Shih 	core_q = FIELD_GET(RR_TXMO_COQ, val);
10792da8109dSPing-Ke Shih 
10802da8109dSPing-Ke Shih 	if (core_i < 0x2 || core_i > 0x1d || core_q < 0x2 || core_q > 0x1d)
10812da8109dSPing-Ke Shih 		is_fail1 = true;
10822da8109dSPing-Ke Shih 	else
10832da8109dSPing-Ke Shih 		is_fail1 = false;
10842da8109dSPing-Ke Shih 
10852da8109dSPing-Ke Shih 	iqk_info->lok_idac[idx][path] = val;
10862da8109dSPing-Ke Shih 
10872da8109dSPing-Ke Shih 	val = rtw89_read_rf(rtwdev, path, RR_LOKVB, RFREG_MASK);
10882da8109dSPing-Ke Shih 	vbuff_i = FIELD_GET(RR_LOKVB_COI, val);
10892da8109dSPing-Ke Shih 	vbuff_q = FIELD_GET(RR_LOKVB_COQ, val);
10902da8109dSPing-Ke Shih 
10912da8109dSPing-Ke Shih 	if (vbuff_i < 0x2 || vbuff_i > 0x3d || vbuff_q < 0x2 || vbuff_q > 0x3d)
10922da8109dSPing-Ke Shih 		is_fail2 = true;
10932da8109dSPing-Ke Shih 	else
10942da8109dSPing-Ke Shih 		is_fail2 = false;
10952da8109dSPing-Ke Shih 
10962da8109dSPing-Ke Shih 	iqk_info->lok_vbuf[idx][path] = val;
10972da8109dSPing-Ke Shih 
10982da8109dSPing-Ke Shih 	return is_fail1 || is_fail2;
10992da8109dSPing-Ke Shih }
11002da8109dSPing-Ke Shih 
_iqk_lok(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)11012da8109dSPing-Ke Shih static bool _iqk_lok(struct rtw89_dev *rtwdev,
11022da8109dSPing-Ke Shih 		     enum rtw89_phy_idx phy_idx, u8 path)
11032da8109dSPing-Ke Shih {
11042da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
11052da8109dSPing-Ke Shih 	u8 tmp_id = 0x0;
11062da8109dSPing-Ke Shih 	bool fail = false;
11072da8109dSPing-Ke Shih 	bool tmp = false;
11082da8109dSPing-Ke Shih 
11092da8109dSPing-Ke Shih 	/* Step 0: Init RF gain & tone idx= 8.25Mhz */
11102da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, IQK_DF4_TXT_8_25MHZ);
11112da8109dSPing-Ke Shih 
11122da8109dSPing-Ke Shih 	/* Step 1  START: _lok_coarse_fine_wi_swap */
11132da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
11142da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
11152da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11162da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11172da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11182da8109dSPing-Ke Shih 		tmp_id = ID_G_FLOK_COARSE;
11192da8109dSPing-Ke Shih 		break;
11202da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
11212da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11222da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11232da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11242da8109dSPing-Ke Shih 		tmp_id = ID_A_FLOK_COARSE;
11252da8109dSPing-Ke Shih 		break;
11262da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
11272da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11282da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11292da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11302da8109dSPing-Ke Shih 		tmp_id = ID_A_FLOK_COARSE;
11312da8109dSPing-Ke Shih 		break;
11322da8109dSPing-Ke Shih 	default:
11332da8109dSPing-Ke Shih 		break;
11342da8109dSPing-Ke Shih 	}
11352da8109dSPing-Ke Shih 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, tmp_id);
11362da8109dSPing-Ke Shih 	iqk_info->lok_cor_fail[0][path] = tmp;
11372da8109dSPing-Ke Shih 
11382da8109dSPing-Ke Shih 	/* Step 2 */
11392da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
11402da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
11412da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
11422da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11432da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
11442da8109dSPing-Ke Shih 		break;
11452da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
11462da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
11472da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11482da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
11492da8109dSPing-Ke Shih 		break;
11502da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
11512da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
11522da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11532da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
11542da8109dSPing-Ke Shih 		break;
11552da8109dSPing-Ke Shih 	default:
11562da8109dSPing-Ke Shih 		break;
11572da8109dSPing-Ke Shih 	}
11582da8109dSPing-Ke Shih 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, ID_FLOK_VBUFFER);
11592da8109dSPing-Ke Shih 
11602da8109dSPing-Ke Shih 	/* Step 3 */
11612da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
11622da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
11632da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11642da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11652da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11662da8109dSPing-Ke Shih 		tmp_id = ID_G_FLOK_FINE;
11672da8109dSPing-Ke Shih 		break;
11682da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
11692da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11702da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11712da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11722da8109dSPing-Ke Shih 		tmp_id = ID_A_FLOK_FINE;
11732da8109dSPing-Ke Shih 		break;
11742da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
11752da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
11762da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11772da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x9);
11782da8109dSPing-Ke Shih 		tmp_id = ID_A_FLOK_FINE;
11792da8109dSPing-Ke Shih 		break;
11802da8109dSPing-Ke Shih 	default:
11812da8109dSPing-Ke Shih 		break;
11822da8109dSPing-Ke Shih 	}
11832da8109dSPing-Ke Shih 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, tmp_id);
11842da8109dSPing-Ke Shih 	iqk_info->lok_fin_fail[0][path] = tmp;
11852da8109dSPing-Ke Shih 
11862da8109dSPing-Ke Shih 	/* Step 4 large rf gain */
11872da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
11882da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
11892da8109dSPing-Ke Shih 	default:
11902da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
11912da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11922da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
11932da8109dSPing-Ke Shih 		break;
11942da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
11952da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
11962da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
11972da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
11982da8109dSPing-Ke Shih 		break;
11992da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
12002da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
12012da8109dSPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
12022da8109dSPing-Ke Shih 				       B_KIP_IQP_IQSW, 0x1b);
12032da8109dSPing-Ke Shih 		break;
12042da8109dSPing-Ke Shih 	}
12052da8109dSPing-Ke Shih 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, ID_FLOK_VBUFFER);
12062da8109dSPing-Ke Shih 	fail = _lok_finetune_check(rtwdev, path);
12072da8109dSPing-Ke Shih 
12082da8109dSPing-Ke Shih 	return fail;
12092da8109dSPing-Ke Shih }
12102da8109dSPing-Ke Shih 
_iqk_txk_setting(struct rtw89_dev * rtwdev,u8 path)12112da8109dSPing-Ke Shih static void _iqk_txk_setting(struct rtw89_dev *rtwdev, u8 path)
12122da8109dSPing-Ke Shih {
12132da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
12142da8109dSPing-Ke Shih 
12152da8109dSPing-Ke Shih 	switch (iqk_info->iqk_band[path]) {
12162da8109dSPing-Ke Shih 	case RTW89_BAND_2G:
12172da8109dSPing-Ke Shih 	default:
12182da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXG1, RR_TXG1_ATT2, 0x0);
12192da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXG1, RR_TXG1_ATT1, 0x0);
12202da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXG2, RR_TXG2_ATT0, 0x1);
12212da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
12222da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
12232da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
12242da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
12252da8109dSPing-Ke Shih 			       0x403e0 | iqk_info->syn1to2);
12262da8109dSPing-Ke Shih 		fsleep(10);
12272da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
12282da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
12292da8109dSPing-Ke Shih 		break;
12302da8109dSPing-Ke Shih 	case RTW89_BAND_5G:
12312da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXATANK, RR_TXATANK_LBSW2, 0x0);
12322da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXPOW, RR_TXPOW_TXAS, 0x1);
12332da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
12342da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
12352da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
12362da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
12372da8109dSPing-Ke Shih 			       0x403e0 | iqk_info->syn1to2);
12382da8109dSPing-Ke Shih 		fsleep(10);
12392da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
12402da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
12412da8109dSPing-Ke Shih 		break;
12422da8109dSPing-Ke Shih 	case RTW89_BAND_6G:
12432da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXATANK, RR_TXATANK_LBSW2, 0x0);
12442da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXPOW, RR_TXPOW_TXAS, 0x1);
12452da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
12462da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
12472da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
12482da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
12492da8109dSPing-Ke Shih 			       0x403e0  | iqk_info->syn1to2);
12502da8109dSPing-Ke Shih 		fsleep(10);
12512da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
12522da8109dSPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
12532da8109dSPing-Ke Shih 		break;
12542da8109dSPing-Ke Shih 	}
12552da8109dSPing-Ke Shih }
12562da8109dSPing-Ke Shih 
_iqk_info_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)12572da8109dSPing-Ke Shih static void _iqk_info_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
12582da8109dSPing-Ke Shih 			  u8 path)
12592da8109dSPing-Ke Shih {
12602da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
12612da8109dSPing-Ke Shih 	u32 tmp;
12622da8109dSPing-Ke Shih 	bool flag;
12632da8109dSPing-Ke Shih 
1264686317a2SDmitry Antipov 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_thermal = %lu\n", path,
1265686317a2SDmitry Antipov 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]));
12662da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_LOK_COR_fail= %d\n", path,
12672da8109dSPing-Ke Shih 		    iqk_info->lok_cor_fail[0][path]);
12682da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_LOK_FIN_fail= %d\n", path,
12692da8109dSPing-Ke Shih 		    iqk_info->lok_fin_fail[0][path]);
12702da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_TXIQK_fail = %d\n", path,
12712da8109dSPing-Ke Shih 		    iqk_info->iqk_tx_fail[0][path]);
12722da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_RXIQK_fail= %d,\n", path,
12732da8109dSPing-Ke Shih 		    iqk_info->iqk_rx_fail[0][path]);
12742da8109dSPing-Ke Shih 
12752da8109dSPing-Ke Shih 	flag = iqk_info->lok_cor_fail[0][path];
12762da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FCOR << (path * 4), flag);
12772da8109dSPing-Ke Shih 	flag = iqk_info->lok_fin_fail[0][path];
12782da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FFIN << (path * 4), flag);
12792da8109dSPing-Ke Shih 	flag = iqk_info->iqk_tx_fail[0][path];
12802da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FTX << (path * 4), flag);
12812da8109dSPing-Ke Shih 	flag = iqk_info->iqk_rx_fail[0][path];
12822da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_F_RX << (path * 4), flag);
12832da8109dSPing-Ke Shih 
12842da8109dSPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_IQK_RES + (path << 8), MASKDWORD);
12852da8109dSPing-Ke Shih 	iqk_info->bp_iqkenable[path] = tmp;
12862da8109dSPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8), MASKDWORD);
12872da8109dSPing-Ke Shih 	iqk_info->bp_txkresult[path] = tmp;
12882da8109dSPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD);
12892da8109dSPing-Ke Shih 	iqk_info->bp_rxkresult[path] = tmp;
12902da8109dSPing-Ke Shih 
12912da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_KCNT,
12922da8109dSPing-Ke Shih 			       iqk_info->iqk_times);
12932da8109dSPing-Ke Shih 
12942da8109dSPing-Ke Shih 	tmp = rtw89_phy_read32_mask(rtwdev, R_IQKINF, B_IQKINF_FAIL << (path * 4));
12952da8109dSPing-Ke Shih 	if (tmp != 0x0)
12962da8109dSPing-Ke Shih 		iqk_info->iqk_fail_cnt++;
12972da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_FCNT << (path * 4),
12982da8109dSPing-Ke Shih 			       iqk_info->iqk_fail_cnt);
12992da8109dSPing-Ke Shih }
13002da8109dSPing-Ke Shih 
_iqk_by_path(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)13012da8109dSPing-Ke Shih static void _iqk_by_path(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, u8 path)
13022da8109dSPing-Ke Shih {
13032da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
13042da8109dSPing-Ke Shih 
13052da8109dSPing-Ke Shih 	_iqk_txk_setting(rtwdev, path);
13062da8109dSPing-Ke Shih 	iqk_info->lok_fail[path] = _iqk_lok(rtwdev, phy_idx, path);
13072da8109dSPing-Ke Shih 
13082da8109dSPing-Ke Shih 	if (iqk_info->is_nbiqk)
13092da8109dSPing-Ke Shih 		iqk_info->iqk_tx_fail[0][path] = _iqk_nbtxk(rtwdev, phy_idx, path);
13102da8109dSPing-Ke Shih 	else
13112da8109dSPing-Ke Shih 		iqk_info->iqk_tx_fail[0][path] = _txk_group_sel(rtwdev, phy_idx, path);
13122da8109dSPing-Ke Shih 
13132da8109dSPing-Ke Shih 	_iqk_rxk_setting(rtwdev, path);
13142da8109dSPing-Ke Shih 	if (iqk_info->is_nbiqk)
13152da8109dSPing-Ke Shih 		iqk_info->iqk_rx_fail[0][path] = _iqk_nbrxk(rtwdev, phy_idx, path);
13162da8109dSPing-Ke Shih 	else
13172da8109dSPing-Ke Shih 		iqk_info->iqk_rx_fail[0][path] = _rxk_group_sel(rtwdev, phy_idx, path);
13182da8109dSPing-Ke Shih 
13192da8109dSPing-Ke Shih 	_iqk_info_iqk(rtwdev, phy_idx, path);
13202da8109dSPing-Ke Shih }
13212da8109dSPing-Ke Shih 
_iqk_get_ch_info(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 path)13222da8109dSPing-Ke Shih static void _iqk_get_ch_info(struct rtw89_dev *rtwdev,
13232da8109dSPing-Ke Shih 			     enum rtw89_phy_idx phy, u8 path)
13242da8109dSPing-Ke Shih {
1325cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
13262da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
13272da8109dSPing-Ke Shih 
13282da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
13292da8109dSPing-Ke Shih 
1330cbb145b9SZong-Zhe Yang 	iqk_info->iqk_band[path] = chan->band_type;
1331cbb145b9SZong-Zhe Yang 	iqk_info->iqk_bw[path] = chan->band_width;
1332cbb145b9SZong-Zhe Yang 	iqk_info->iqk_ch[path] = chan->channel;
13332da8109dSPing-Ke Shih 
13342da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
13352da8109dSPing-Ke Shih 		    "[IQK]iqk_info->iqk_band[%x] = 0x%x\n", path,
13362da8109dSPing-Ke Shih 		    iqk_info->iqk_band[path]);
13372da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]iqk_info->iqk_bw[%x] = 0x%x\n",
13382da8109dSPing-Ke Shih 		    path, iqk_info->iqk_bw[path]);
13392da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]iqk_info->iqk_ch[%x] = 0x%x\n",
13402da8109dSPing-Ke Shih 		    path, iqk_info->iqk_ch[path]);
13412da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
13422da8109dSPing-Ke Shih 		    "[IQK]S%d (PHY%d): / DBCC %s/ %s/ CH%d/ %s\n", path, phy,
13432da8109dSPing-Ke Shih 		    rtwdev->dbcc_en ? "on" : "off",
13442da8109dSPing-Ke Shih 		    iqk_info->iqk_band[path] == 0 ? "2G" :
13452da8109dSPing-Ke Shih 		    iqk_info->iqk_band[path] == 1 ? "5G" : "6G",
13462da8109dSPing-Ke Shih 		    iqk_info->iqk_ch[path],
13472da8109dSPing-Ke Shih 		    iqk_info->iqk_bw[path] == 0 ? "20M" :
13482da8109dSPing-Ke Shih 		    iqk_info->iqk_bw[path] == 1 ? "40M" : "80M");
13492da8109dSPing-Ke Shih 	if (!rtwdev->dbcc_en)
13502da8109dSPing-Ke Shih 		iqk_info->syn1to2 = 0x1;
13512da8109dSPing-Ke Shih 	else
13522da8109dSPing-Ke Shih 		iqk_info->syn1to2 = 0x3;
13532da8109dSPing-Ke Shih 
13542da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_VER, RTW8852C_IQK_VER);
13552da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_BAND << (path * 16),
13562da8109dSPing-Ke Shih 			       iqk_info->iqk_band[path]);
13572da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_BW << (path * 16),
13582da8109dSPing-Ke Shih 			       iqk_info->iqk_bw[path]);
13592da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_CH << (path * 16),
13602da8109dSPing-Ke Shih 			       iqk_info->iqk_ch[path]);
13612da8109dSPing-Ke Shih 
13622da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_NCTLV, RTW8852C_NCTL_VER);
13632da8109dSPing-Ke Shih }
13642da8109dSPing-Ke Shih 
_iqk_start_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)13652da8109dSPing-Ke Shih static void _iqk_start_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
13662da8109dSPing-Ke Shih 			   u8 path)
13672da8109dSPing-Ke Shih {
13682da8109dSPing-Ke Shih 	_iqk_by_path(rtwdev, phy_idx, path);
13692da8109dSPing-Ke Shih }
13702da8109dSPing-Ke Shih 
_iqk_restore(struct rtw89_dev * rtwdev,u8 path)13712da8109dSPing-Ke Shih static void _iqk_restore(struct rtw89_dev *rtwdev, u8 path)
13722da8109dSPing-Ke Shih {
13732da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
13742da8109dSPing-Ke Shih 	bool fail;
13752da8109dSPing-Ke Shih 
13762da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8), MASKDWORD,
13772da8109dSPing-Ke Shih 			       iqk_info->nb_txcfir[path]);
13782da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
13792da8109dSPing-Ke Shih 			       iqk_info->nb_rxcfir[path]);
13802da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
13812da8109dSPing-Ke Shih 			       0x00001219 + (path << 4));
13822da8109dSPing-Ke Shih 	fsleep(200);
13832da8109dSPing-Ke Shih 	fail = _iqk_check_cal(rtwdev, path, 0x12);
13842da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK] restore fail  = %x\n", fail);
13852da8109dSPing-Ke Shih 
13862da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
13872da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
13882da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
13892da8109dSPing-Ke Shih 
13902da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x0);
13912da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
13922da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
13932da8109dSPing-Ke Shih }
13942da8109dSPing-Ke Shih 
_iqk_afebb_restore(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)13952da8109dSPing-Ke Shih static void _iqk_afebb_restore(struct rtw89_dev *rtwdev,
13962da8109dSPing-Ke Shih 			       enum rtw89_phy_idx phy_idx, u8 path)
13972da8109dSPing-Ke Shih {
13982da8109dSPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
13992da8109dSPing-Ke Shih 				 &rtw8852c_iqk_afebb_restore_defs_a_tbl,
14002da8109dSPing-Ke Shih 				 &rtw8852c_iqk_afebb_restore_defs_b_tbl);
14012da8109dSPing-Ke Shih 
14022da8109dSPing-Ke Shih 	rtw8852c_disable_rxagc(rtwdev, path, 0x1);
14032da8109dSPing-Ke Shih }
14042da8109dSPing-Ke Shih 
_iqk_preset(struct rtw89_dev * rtwdev,u8 path)14052da8109dSPing-Ke Shih static void _iqk_preset(struct rtw89_dev *rtwdev, u8 path)
14062da8109dSPing-Ke Shih {
140738f25decSZong-Zhe Yang 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
14082da8109dSPing-Ke Shih 	u8 idx = 0;
14092da8109dSPing-Ke Shih 
141038f25decSZong-Zhe Yang 	idx = rfk_mcc->table_idx;
14112da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8), B_COEF_SEL_IQC, idx);
14122da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_G3, idx);
14132da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
14142da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
14152da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x81ff010a);
14162da8109dSPing-Ke Shih }
14172da8109dSPing-Ke Shih 
_iqk_macbb_setting(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,u8 path)14182da8109dSPing-Ke Shih static void _iqk_macbb_setting(struct rtw89_dev *rtwdev,
14192da8109dSPing-Ke Shih 			       enum rtw89_phy_idx phy_idx, u8 path)
14202da8109dSPing-Ke Shih {
14212da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===> %s\n", __func__);
14222da8109dSPing-Ke Shih 
14232da8109dSPing-Ke Shih 	/* 01_BB_AFE_for DPK_S0_20210820 */
14242da8109dSPing-Ke Shih 	rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x0);
14252da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
14262da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
14272da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
14282da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
14292da8109dSPing-Ke Shih 
14302da8109dSPing-Ke Shih 	/* disable rxgac */
14312da8109dSPing-Ke Shih 	rtw8852c_disable_rxagc(rtwdev, path, 0x0);
14322da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), MASKDWORD, 0xf801fffd);
14332da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DPD_DIS, 0x1);
14342da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DAC_VAL, 0x1);
14352da8109dSPing-Ke Shih 
14362da8109dSPing-Ke Shih 	rtw8852c_txck_force(rtwdev, path, true, DAC_960M);
14372da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DPD_GDIS, 0x1);
14382da8109dSPing-Ke Shih 
14392da8109dSPing-Ke Shih 	rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
14402da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_ACK_VAL, 0x2);
14412da8109dSPing-Ke Shih 
14422da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW | (path << 13), B_P0_NRBW_DBG, 0x1);
14432da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, B_ANAPAR_PW15, 0x1f);
14442da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, B_ANAPAR_PW15, 0x13);
14452da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, B_ANAPAR_15, 0x0001);
14462da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, B_ANAPAR_15, 0x0041);
14472da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x1);
14482da8109dSPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x1);
14492da8109dSPing-Ke Shih }
14502da8109dSPing-Ke Shih 
_rck(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)145130052c5aSPing-Ke Shih static void _rck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
145230052c5aSPing-Ke Shih {
145330052c5aSPing-Ke Shih 	u32 rf_reg5, rck_val = 0;
145430052c5aSPing-Ke Shih 	u32 val;
145530052c5aSPing-Ke Shih 	int ret;
145630052c5aSPing-Ke Shih 
145730052c5aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] ====== S%d RCK ======\n", path);
145830052c5aSPing-Ke Shih 
145930052c5aSPing-Ke Shih 	rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
146030052c5aSPing-Ke Shih 
146130052c5aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
146230052c5aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
146330052c5aSPing-Ke Shih 
146430052c5aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF0x00 = 0x%x\n",
146530052c5aSPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
146630052c5aSPing-Ke Shih 
146730052c5aSPing-Ke Shih 	/* RCK trigger */
146830052c5aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, 0x00240);
146930052c5aSPing-Ke Shih 
147030052c5aSPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val, 2, 20,
147130052c5aSPing-Ke Shih 				       false, rtwdev, path, 0x1c, BIT(3));
147230052c5aSPing-Ke Shih 	if (ret)
147330052c5aSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RCK timeout\n");
147430052c5aSPing-Ke Shih 
147530052c5aSPing-Ke Shih 	rck_val = rtw89_read_rf(rtwdev, path, RR_RCKC, RR_RCKC_CA);
147630052c5aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, rck_val);
147730052c5aSPing-Ke Shih 
147830052c5aSPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
147930052c5aSPing-Ke Shih 
148030052c5aSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
148130052c5aSPing-Ke Shih 		    "[RCK] RF 0x1b / 0x1c = 0x%x / 0x%x\n",
148230052c5aSPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_RCKC, RFREG_MASK),
148330052c5aSPing-Ke Shih 		    rtw89_read_rf(rtwdev, path, RR_RCKS, RFREG_MASK));
148430052c5aSPing-Ke Shih }
148530052c5aSPing-Ke Shih 
_iqk_init(struct rtw89_dev * rtwdev)14862da8109dSPing-Ke Shih static void _iqk_init(struct rtw89_dev *rtwdev)
14872da8109dSPing-Ke Shih {
14882da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
14892da8109dSPing-Ke Shih 	u8 ch, path;
14902da8109dSPing-Ke Shih 
14912da8109dSPing-Ke Shih 	rtw89_phy_write32_clr(rtwdev, R_IQKINF, MASKDWORD);
14922da8109dSPing-Ke Shih 	if (iqk_info->is_iqk_init)
14932da8109dSPing-Ke Shih 		return;
14942da8109dSPing-Ke Shih 
14952da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
14962da8109dSPing-Ke Shih 	iqk_info->is_iqk_init = true;
14972da8109dSPing-Ke Shih 	iqk_info->is_nbiqk = false;
14982da8109dSPing-Ke Shih 	iqk_info->iqk_fft_en = false;
14992da8109dSPing-Ke Shih 	iqk_info->iqk_sram_en = false;
15002da8109dSPing-Ke Shih 	iqk_info->iqk_cfir_en = false;
15012da8109dSPing-Ke Shih 	iqk_info->iqk_xym_en = false;
15022da8109dSPing-Ke Shih 	iqk_info->iqk_times = 0x0;
15032da8109dSPing-Ke Shih 
15042da8109dSPing-Ke Shih 	for (ch = 0; ch < RTW89_IQK_CHS_NR; ch++) {
15052da8109dSPing-Ke Shih 		iqk_info->iqk_channel[ch] = 0x0;
15062da8109dSPing-Ke Shih 		for (path = 0; path < RTW8852C_IQK_SS; path++) {
15072da8109dSPing-Ke Shih 			iqk_info->lok_cor_fail[ch][path] = false;
15082da8109dSPing-Ke Shih 			iqk_info->lok_fin_fail[ch][path] = false;
15092da8109dSPing-Ke Shih 			iqk_info->iqk_tx_fail[ch][path] = false;
15102da8109dSPing-Ke Shih 			iqk_info->iqk_rx_fail[ch][path] = false;
15112da8109dSPing-Ke Shih 			iqk_info->iqk_mcc_ch[ch][path] = 0x0;
15122da8109dSPing-Ke Shih 			iqk_info->iqk_table_idx[path] = 0x0;
15132da8109dSPing-Ke Shih 		}
15142da8109dSPing-Ke Shih 	}
15152da8109dSPing-Ke Shih }
15162da8109dSPing-Ke Shih 
_doiqk(struct rtw89_dev * rtwdev,bool force,enum rtw89_phy_idx phy_idx,u8 path)15172da8109dSPing-Ke Shih static void _doiqk(struct rtw89_dev *rtwdev, bool force,
15182da8109dSPing-Ke Shih 		   enum rtw89_phy_idx phy_idx, u8 path)
15192da8109dSPing-Ke Shih {
15202da8109dSPing-Ke Shih 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
15212da8109dSPing-Ke Shih 	u32 backup_bb_val[BACKUP_BB_REGS_NR];
15222da8109dSPing-Ke Shih 	u32 backup_rf_val[RTW8852C_IQK_SS][BACKUP_RF_REGS_NR];
15232da8109dSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, RF_AB);
15242da8109dSPing-Ke Shih 
15252da8109dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_START);
15262da8109dSPing-Ke Shih 
15272da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1528*5bc9a34cSDmitry Antipov 		    "[IQK]==========IQK start!!!!!==========\n");
15292da8109dSPing-Ke Shih 	iqk_info->iqk_times++;
15302da8109dSPing-Ke Shih 	iqk_info->version = RTW8852C_IQK_VER;
15312da8109dSPing-Ke Shih 
15322da8109dSPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]Test Ver 0x%x\n", iqk_info->version);
15332da8109dSPing-Ke Shih 	_iqk_get_ch_info(rtwdev, phy_idx, path);
15342da8109dSPing-Ke Shih 	_rfk_backup_bb_reg(rtwdev, backup_bb_val);
15352da8109dSPing-Ke Shih 	_rfk_backup_rf_reg(rtwdev, backup_rf_val[path], path);
15362da8109dSPing-Ke Shih 	_iqk_macbb_setting(rtwdev, phy_idx, path);
15372da8109dSPing-Ke Shih 	_iqk_preset(rtwdev, path);
15382da8109dSPing-Ke Shih 	_iqk_start_iqk(rtwdev, phy_idx, path);
15392da8109dSPing-Ke Shih 	_iqk_restore(rtwdev, path);
15402da8109dSPing-Ke Shih 	_iqk_afebb_restore(rtwdev, phy_idx, path);
15412da8109dSPing-Ke Shih 	_rfk_restore_bb_reg(rtwdev, backup_bb_val);
15422da8109dSPing-Ke Shih 	_rfk_restore_rf_reg(rtwdev, backup_rf_val[path], path);
15432da8109dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_STOP);
15442da8109dSPing-Ke Shih }
15452da8109dSPing-Ke Shih 
_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx,bool force)15462da8109dSPing-Ke Shih static void _iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, bool force)
15472da8109dSPing-Ke Shih {
15482da8109dSPing-Ke Shih 	switch (_kpath(rtwdev, phy_idx)) {
15492da8109dSPing-Ke Shih 	case RF_A:
15502da8109dSPing-Ke Shih 		_doiqk(rtwdev, force, phy_idx, RF_PATH_A);
15512da8109dSPing-Ke Shih 		break;
15522da8109dSPing-Ke Shih 	case RF_B:
15532da8109dSPing-Ke Shih 		_doiqk(rtwdev, force, phy_idx, RF_PATH_B);
15542da8109dSPing-Ke Shih 		break;
15552da8109dSPing-Ke Shih 	case RF_AB:
15562da8109dSPing-Ke Shih 		_doiqk(rtwdev, force, phy_idx, RF_PATH_A);
15572da8109dSPing-Ke Shih 		_doiqk(rtwdev, force, phy_idx, RF_PATH_B);
15582da8109dSPing-Ke Shih 		break;
15592da8109dSPing-Ke Shih 	default:
15602da8109dSPing-Ke Shih 		break;
15612da8109dSPing-Ke Shih 	}
15622da8109dSPing-Ke Shih }
15632da8109dSPing-Ke Shih 
_rx_dck_value_rewrite(struct rtw89_dev * rtwdev,u8 path,u8 addr,u8 val_i,u8 val_q)15649c22d603SPing-Ke Shih static void _rx_dck_value_rewrite(struct rtw89_dev *rtwdev, u8 path, u8 addr,
15659c22d603SPing-Ke Shih 				  u8 val_i, u8 val_q)
15669c22d603SPing-Ke Shih {
15679c22d603SPing-Ke Shih 	u32 ofst_val;
15689c22d603SPing-Ke Shih 
15699c22d603SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
15709c22d603SPing-Ke Shih 		    "[RX_DCK] rewrite val_i = 0x%x, val_q = 0x%x\n", val_i, val_q);
15719c22d603SPing-Ke Shih 
15729c22d603SPing-Ke Shih 	/* val_i and val_q are 7 bits, and target is 6 bits. */
15739c22d603SPing-Ke Shih 	ofst_val = u32_encode_bits(val_q >> 1, RR_LUTWD0_MB) |
15749c22d603SPing-Ke Shih 		   u32_encode_bits(val_i >> 1, RR_LUTWD0_LB);
15759c22d603SPing-Ke Shih 
15769c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTPLL, RR_CAL_RW, 0x1);
15779c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RFC, RR_WCAL, 0x1);
15789c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_FINE, 0x1);
15799c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWA, MASKBYTE0, addr);
15809c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, ofst_val);
15819c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, ofst_val);
15829c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_FINE, 0x0);
15839c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_RFC, RR_WCAL, 0x0);
15849c22d603SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_LUTPLL, RR_CAL_RW, 0x0);
15859c22d603SPing-Ke Shih 
15869c22d603SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] Final val_i = 0x%x, val_q = 0x%x\n",
15879c22d603SPing-Ke Shih 		    u32_get_bits(ofst_val, RR_LUTWD0_LB) << 1,
15889c22d603SPing-Ke Shih 		    u32_get_bits(ofst_val, RR_LUTWD0_MB) << 1);
15899c22d603SPing-Ke Shih }
15909c22d603SPing-Ke Shih 
_rx_dck_rek_check(struct rtw89_dev * rtwdev,u8 path)15919c22d603SPing-Ke Shih static bool _rx_dck_rek_check(struct rtw89_dev *rtwdev, u8 path)
15929c22d603SPing-Ke Shih {
15939c22d603SPing-Ke Shih 	u8 i_even_bs, q_even_bs;
15949c22d603SPing-Ke Shih 	u8 i_odd_bs, q_odd_bs;
15959c22d603SPing-Ke Shih 	u8 i_even, q_even;
15969c22d603SPing-Ke Shih 	u8 i_odd, q_odd;
15979c22d603SPing-Ke Shih 	const u8 th = 10;
15989c22d603SPing-Ke Shih 	u8 i;
15999c22d603SPing-Ke Shih 
16009c22d603SPing-Ke Shih 	for (i = 0; i < RF_PATH_NUM_8852C; i++) {
16019c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i]);
16029c22d603SPing-Ke Shih 		i_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16039c22d603SPing-Ke Shih 		q_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16049c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16059c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_even_bs/ q_even_bs = 0x%x/ 0x%x\n",
16069c22d603SPing-Ke Shih 			    _dck_addr_bs[i], i_even_bs, q_even_bs);
16079c22d603SPing-Ke Shih 
16089c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i]);
16099c22d603SPing-Ke Shih 		i_even = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16109c22d603SPing-Ke Shih 		q_even = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16119c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16129c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_even/ q_even = 0x%x/ 0x%x\n",
16139c22d603SPing-Ke Shih 			    _dck_addr[i], i_even, q_even);
16149c22d603SPing-Ke Shih 
16159c22d603SPing-Ke Shih 		if (abs(i_even_bs - i_even) > th || abs(q_even_bs - q_even) > th)
16169c22d603SPing-Ke Shih 			return true;
16179c22d603SPing-Ke Shih 
16189c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i] + 1);
16199c22d603SPing-Ke Shih 		i_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16209c22d603SPing-Ke Shih 		q_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16219c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16229c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_odd_bs/ q_odd_bs = 0x%x/ 0x%x\n",
16239c22d603SPing-Ke Shih 			    _dck_addr_bs[i] + 1, i_odd_bs, q_odd_bs);
16249c22d603SPing-Ke Shih 
16259c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i] + 1);
16269c22d603SPing-Ke Shih 		i_odd = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16279c22d603SPing-Ke Shih 		q_odd = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16289c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16299c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_odd/ q_odd = 0x%x/ 0x%x\n",
16309c22d603SPing-Ke Shih 			    _dck_addr[i] + 1, i_odd, q_odd);
16319c22d603SPing-Ke Shih 
16329c22d603SPing-Ke Shih 		if (abs(i_odd_bs - i_odd) > th || abs(q_odd_bs - q_odd) > th)
16339c22d603SPing-Ke Shih 			return true;
16349c22d603SPing-Ke Shih 	}
16359c22d603SPing-Ke Shih 
16369c22d603SPing-Ke Shih 	return false;
16379c22d603SPing-Ke Shih }
16389c22d603SPing-Ke Shih 
_rx_dck_fix_if_need(struct rtw89_dev * rtwdev,u8 path,u8 addr,u8 val_i_bs,u8 val_q_bs,u8 val_i,u8 val_q)16399c22d603SPing-Ke Shih static void _rx_dck_fix_if_need(struct rtw89_dev *rtwdev, u8 path, u8 addr,
16409c22d603SPing-Ke Shih 				u8 val_i_bs, u8 val_q_bs, u8 val_i, u8 val_q)
16419c22d603SPing-Ke Shih {
16429c22d603SPing-Ke Shih 	const u8 th = 10;
16439c22d603SPing-Ke Shih 
16449c22d603SPing-Ke Shih 	if ((abs(val_i_bs - val_i) < th) && (abs(val_q_bs - val_q) <= th)) {
16459c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] offset check PASS!!\n");
16469c22d603SPing-Ke Shih 		return;
16479c22d603SPing-Ke Shih 	}
16489c22d603SPing-Ke Shih 
16499c22d603SPing-Ke Shih 	if (abs(val_i_bs - val_i) > th) {
16509c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16519c22d603SPing-Ke Shih 			    "[RX_DCK] val_i over TH (0x%x / 0x%x)\n", val_i_bs, val_i);
16529c22d603SPing-Ke Shih 		val_i = val_i_bs;
16539c22d603SPing-Ke Shih 	}
16549c22d603SPing-Ke Shih 
16559c22d603SPing-Ke Shih 	if (abs(val_q_bs - val_q) > th) {
16569c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16579c22d603SPing-Ke Shih 			    "[RX_DCK] val_q over TH (0x%x / 0x%x)\n", val_q_bs, val_q);
16589c22d603SPing-Ke Shih 		val_q = val_q_bs;
16599c22d603SPing-Ke Shih 	}
16609c22d603SPing-Ke Shih 
16619c22d603SPing-Ke Shih 	_rx_dck_value_rewrite(rtwdev, path, addr, val_i, val_q);
16629c22d603SPing-Ke Shih }
16639c22d603SPing-Ke Shih 
_rx_dck_recover(struct rtw89_dev * rtwdev,u8 path)16649c22d603SPing-Ke Shih static void _rx_dck_recover(struct rtw89_dev *rtwdev, u8 path)
16659c22d603SPing-Ke Shih {
16669c22d603SPing-Ke Shih 	u8 i_even_bs, q_even_bs;
16679c22d603SPing-Ke Shih 	u8 i_odd_bs, q_odd_bs;
16689c22d603SPing-Ke Shih 	u8 i_even, q_even;
16699c22d603SPing-Ke Shih 	u8 i_odd, q_odd;
16709c22d603SPing-Ke Shih 	u8 i;
16719c22d603SPing-Ke Shih 
16729c22d603SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] ===> recovery\n");
16739c22d603SPing-Ke Shih 
16749c22d603SPing-Ke Shih 	for (i = 0; i < RF_PATH_NUM_8852C; i++) {
16759c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i]);
16769c22d603SPing-Ke Shih 		i_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16779c22d603SPing-Ke Shih 		q_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16789c22d603SPing-Ke Shih 
16799c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i] + 1);
16809c22d603SPing-Ke Shih 		i_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16819c22d603SPing-Ke Shih 		q_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16829c22d603SPing-Ke Shih 
16839c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16849c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_even_bs/ q_even_bs = 0x%x/ 0x%x\n",
16859c22d603SPing-Ke Shih 			    _dck_addr_bs[i], i_even_bs, q_even_bs);
16869c22d603SPing-Ke Shih 
16879c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i]);
16889c22d603SPing-Ke Shih 		i_even = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
16899c22d603SPing-Ke Shih 		q_even = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
16909c22d603SPing-Ke Shih 
16919c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16929c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_even/ q_even = 0x%x/ 0x%x\n",
16939c22d603SPing-Ke Shih 			    _dck_addr[i], i_even, q_even);
16949c22d603SPing-Ke Shih 		_rx_dck_fix_if_need(rtwdev, path, _dck_addr[i],
16959c22d603SPing-Ke Shih 				    i_even_bs, q_even_bs, i_even, q_even);
16969c22d603SPing-Ke Shih 
16979c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
16989c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_odd_bs/ q_odd_bs = 0x%x/ 0x%x\n",
16999c22d603SPing-Ke Shih 			    _dck_addr_bs[i] + 1, i_odd_bs, q_odd_bs);
17009c22d603SPing-Ke Shih 
17019c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i] + 1);
17029c22d603SPing-Ke Shih 		i_odd = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
17039c22d603SPing-Ke Shih 		q_odd = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
17049c22d603SPing-Ke Shih 
17059c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
17069c22d603SPing-Ke Shih 			    "[RX_DCK] Gain[0x%x] i_odd/ q_odd = 0x%x/ 0x%x\n",
17079c22d603SPing-Ke Shih 			    _dck_addr[i] + 1, i_odd, q_odd);
17089c22d603SPing-Ke Shih 		_rx_dck_fix_if_need(rtwdev, path, _dck_addr[i] + 1,
17099c22d603SPing-Ke Shih 				    i_odd_bs, q_odd_bs, i_odd, q_odd);
17109c22d603SPing-Ke Shih 	}
17119c22d603SPing-Ke Shih }
17129c22d603SPing-Ke Shih 
_rx_dck_toggle(struct rtw89_dev * rtwdev,u8 path)1713ac91be97SPing-Ke Shih static void _rx_dck_toggle(struct rtw89_dev *rtwdev, u8 path)
1714ac91be97SPing-Ke Shih {
1715ac91be97SPing-Ke Shih 	int ret;
1716ac91be97SPing-Ke Shih 	u32 val;
1717ac91be97SPing-Ke Shih 
1718ac91be97SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
1719ac91be97SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x1);
1720ac91be97SPing-Ke Shih 
1721ac91be97SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val,
17222449ca71SPing-Ke Shih 				       2, 2000, false, rtwdev, path,
17232449ca71SPing-Ke Shih 				       RR_DCK1, RR_DCK1_DONE);
1724ac91be97SPing-Ke Shih 	if (ret)
1725ac91be97SPing-Ke Shih 		rtw89_warn(rtwdev, "[RX_DCK] S%d RXDCK timeout\n", path);
1726ac91be97SPing-Ke Shih 	else
1727ac91be97SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] S%d RXDCK finish\n", path);
1728ac91be97SPing-Ke Shih 
1729ac91be97SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
1730ac91be97SPing-Ke Shih }
1731ac91be97SPing-Ke Shih 
_set_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 path,bool is_afe)1732ac91be97SPing-Ke Shih static void _set_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, u8 path,
1733ac91be97SPing-Ke Shih 			bool is_afe)
1734ac91be97SPing-Ke Shih {
1735ac91be97SPing-Ke Shih 	u8 res;
1736ac91be97SPing-Ke Shih 
1737ac91be97SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_DCK1, RR_DCK1_CLR, 0x0);
1738ac91be97SPing-Ke Shih 
1739ac91be97SPing-Ke Shih 	_rx_dck_toggle(rtwdev, path);
1740ac91be97SPing-Ke Shih 	if (rtw89_read_rf(rtwdev, path, RR_DCKC, RR_DCKC_CHK) == 0)
1741ac91be97SPing-Ke Shih 		return;
1742ac91be97SPing-Ke Shih 	res = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_DONE);
1743ac91be97SPing-Ke Shih 	if (res > 1) {
1744ac91be97SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_IDAC, res);
1745ac91be97SPing-Ke Shih 		_rx_dck_toggle(rtwdev, path);
1746ac91be97SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_IDAC, 0x1);
1747ac91be97SPing-Ke Shih 	}
1748ac91be97SPing-Ke Shih }
1749ac91be97SPing-Ke Shih 
17509c22d603SPing-Ke Shih static
_rx_dck_channel_calc(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan)17519c22d603SPing-Ke Shih u8 _rx_dck_channel_calc(struct rtw89_dev *rtwdev, const struct rtw89_chan *chan)
17529c22d603SPing-Ke Shih {
17539c22d603SPing-Ke Shih 	u8 target_ch = 0;
17549c22d603SPing-Ke Shih 
17559c22d603SPing-Ke Shih 	if (chan->band_type == RTW89_BAND_5G) {
17569c22d603SPing-Ke Shih 		if (chan->channel >= 36 && chan->channel <= 64) {
17579c22d603SPing-Ke Shih 			target_ch = 100;
17589c22d603SPing-Ke Shih 		} else if (chan->channel >= 100 && chan->channel <= 144) {
17599c22d603SPing-Ke Shih 			target_ch = chan->channel + 32;
17609c22d603SPing-Ke Shih 			if (target_ch > 144)
17619c22d603SPing-Ke Shih 				target_ch = chan->channel + 33;
17629c22d603SPing-Ke Shih 		} else if (chan->channel >= 149 && chan->channel <= 177) {
17639c22d603SPing-Ke Shih 			target_ch = chan->channel - 33;
17649c22d603SPing-Ke Shih 		}
17659c22d603SPing-Ke Shih 	} else if (chan->band_type == RTW89_BAND_6G) {
17669c22d603SPing-Ke Shih 		if (chan->channel >= 1 && chan->channel <= 125)
17679c22d603SPing-Ke Shih 			target_ch = chan->channel + 32;
17689c22d603SPing-Ke Shih 		else
17699c22d603SPing-Ke Shih 			target_ch = chan->channel - 32;
17709c22d603SPing-Ke Shih 	} else {
17719c22d603SPing-Ke Shih 		target_ch = chan->channel;
17729c22d603SPing-Ke Shih 	}
17739c22d603SPing-Ke Shih 
17749c22d603SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
17759c22d603SPing-Ke Shih 		    "[RX_DCK] cur_ch / target_ch = %d / %d\n",
17769c22d603SPing-Ke Shih 		    chan->channel, target_ch);
17779c22d603SPing-Ke Shih 
17789c22d603SPing-Ke Shih 	return target_ch;
17799c22d603SPing-Ke Shih }
17809c22d603SPing-Ke Shih 
1781da4cea16SPing-Ke Shih #define RTW8852C_RF_REL_VERSION 34
1782ba1a6905SChih-Kang Chang #define RTW8852C_DPK_VER 0xf
1783da4cea16SPing-Ke Shih #define RTW8852C_DPK_TH_AVG_NUM 4
1784da4cea16SPing-Ke Shih #define RTW8852C_DPK_RF_PATH 2
17853aa83062SChih-Kang Chang #define RTW8852C_DPK_KIP_REG_NUM 7
1786da4cea16SPing-Ke Shih #define RTW8852C_DPK_RXSRAM_DBG 0
1787da4cea16SPing-Ke Shih 
1788da4cea16SPing-Ke Shih enum rtw8852c_dpk_id {
1789da4cea16SPing-Ke Shih 	LBK_RXIQK	= 0x06,
1790da4cea16SPing-Ke Shih 	SYNC		= 0x10,
1791da4cea16SPing-Ke Shih 	MDPK_IDL	= 0x11,
1792da4cea16SPing-Ke Shih 	MDPK_MPA	= 0x12,
1793da4cea16SPing-Ke Shih 	GAIN_LOSS	= 0x13,
1794da4cea16SPing-Ke Shih 	GAIN_CAL	= 0x14,
1795da4cea16SPing-Ke Shih 	DPK_RXAGC	= 0x15,
1796da4cea16SPing-Ke Shih 	KIP_PRESET	= 0x16,
1797da4cea16SPing-Ke Shih 	KIP_RESTORE	= 0x17,
1798da4cea16SPing-Ke Shih 	DPK_TXAGC	= 0x19,
1799da4cea16SPing-Ke Shih 	D_KIP_PRESET	= 0x28,
1800da4cea16SPing-Ke Shih 	D_TXAGC		= 0x29,
1801da4cea16SPing-Ke Shih 	D_RXAGC		= 0x2a,
1802da4cea16SPing-Ke Shih 	D_SYNC		= 0x2b,
1803da4cea16SPing-Ke Shih 	D_GAIN_LOSS	= 0x2c,
1804da4cea16SPing-Ke Shih 	D_MDPK_IDL	= 0x2d,
1805da4cea16SPing-Ke Shih 	D_GAIN_NORM	= 0x2f,
1806da4cea16SPing-Ke Shih 	D_KIP_THERMAL	= 0x30,
1807da4cea16SPing-Ke Shih 	D_KIP_RESTORE	= 0x31
1808da4cea16SPing-Ke Shih };
1809da4cea16SPing-Ke Shih 
1810da4cea16SPing-Ke Shih #define DPK_TXAGC_LOWER 0x2e
1811da4cea16SPing-Ke Shih #define DPK_TXAGC_UPPER 0x3f
1812da4cea16SPing-Ke Shih #define DPK_TXAGC_INVAL 0xff
1813da4cea16SPing-Ke Shih 
1814da4cea16SPing-Ke Shih enum dpk_agc_step {
1815da4cea16SPing-Ke Shih 	DPK_AGC_STEP_SYNC_DGAIN,
1816da4cea16SPing-Ke Shih 	DPK_AGC_STEP_GAIN_LOSS_IDX,
1817da4cea16SPing-Ke Shih 	DPK_AGC_STEP_GL_GT_CRITERION,
1818da4cea16SPing-Ke Shih 	DPK_AGC_STEP_GL_LT_CRITERION,
1819da4cea16SPing-Ke Shih 	DPK_AGC_STEP_SET_TX_GAIN,
1820da4cea16SPing-Ke Shih };
1821da4cea16SPing-Ke Shih 
1822ba1a6905SChih-Kang Chang enum dpk_pas_result {
1823ba1a6905SChih-Kang Chang 	DPK_PAS_NOR,
1824ba1a6905SChih-Kang Chang 	DPK_PAS_GT,
1825ba1a6905SChih-Kang Chang 	DPK_PAS_LT,
1826ba1a6905SChih-Kang Chang };
1827ba1a6905SChih-Kang Chang 
_rf_direct_cntrl(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool is_bybb)1828da4cea16SPing-Ke Shih static void _rf_direct_cntrl(struct rtw89_dev *rtwdev,
1829da4cea16SPing-Ke Shih 			     enum rtw89_rf_path path, bool is_bybb)
1830da4cea16SPing-Ke Shih {
1831da4cea16SPing-Ke Shih 	if (is_bybb)
1832da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
1833da4cea16SPing-Ke Shih 	else
1834da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1835da4cea16SPing-Ke Shih }
1836da4cea16SPing-Ke Shih 
1837da4cea16SPing-Ke Shih static void _dpk_onoff(struct rtw89_dev *rtwdev,
1838da4cea16SPing-Ke Shih 		       enum rtw89_rf_path path, bool off);
1839da4cea16SPing-Ke Shih 
_dpk_bkup_kip(struct rtw89_dev * rtwdev,const u32 reg[],u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM],u8 path)1840da4cea16SPing-Ke Shih static void _dpk_bkup_kip(struct rtw89_dev *rtwdev, const u32 reg[],
1841da4cea16SPing-Ke Shih 			  u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM], u8 path)
1842da4cea16SPing-Ke Shih {
1843da4cea16SPing-Ke Shih 	u8 i;
1844da4cea16SPing-Ke Shih 
1845da4cea16SPing-Ke Shih 	for (i = 0; i < RTW8852C_DPK_KIP_REG_NUM; i++) {
1846da4cea16SPing-Ke Shih 		reg_bkup[path][i] =
1847da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD);
1848da4cea16SPing-Ke Shih 
1849da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup 0x%x = %x\n",
1850da4cea16SPing-Ke Shih 			    reg[i] + (path << 8), reg_bkup[path][i]);
1851da4cea16SPing-Ke Shih 	}
1852da4cea16SPing-Ke Shih }
1853da4cea16SPing-Ke Shih 
_dpk_reload_kip(struct rtw89_dev * rtwdev,const u32 reg[],u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM],u8 path)1854da4cea16SPing-Ke Shih static void _dpk_reload_kip(struct rtw89_dev *rtwdev, const u32 reg[],
1855da4cea16SPing-Ke Shih 			    u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM], u8 path)
1856da4cea16SPing-Ke Shih {
1857da4cea16SPing-Ke Shih 	u8 i;
1858da4cea16SPing-Ke Shih 
1859da4cea16SPing-Ke Shih 	for (i = 0; i < RTW8852C_DPK_KIP_REG_NUM; i++) {
1860da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, reg[i] + (path << 8),
1861da4cea16SPing-Ke Shih 				       MASKDWORD, reg_bkup[path][i]);
1862da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Reload 0x%x = %x\n",
1863da4cea16SPing-Ke Shih 			    reg[i] + (path << 8), reg_bkup[path][i]);
1864da4cea16SPing-Ke Shih 	}
1865da4cea16SPing-Ke Shih }
1866da4cea16SPing-Ke Shih 
_dpk_one_shot(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,enum rtw8852c_dpk_id id)1867da4cea16SPing-Ke Shih static u8 _dpk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
1868da4cea16SPing-Ke Shih 			enum rtw89_rf_path path, enum rtw8852c_dpk_id id)
1869da4cea16SPing-Ke Shih {
1870da4cea16SPing-Ke Shih 	u16 dpk_cmd;
1871da4cea16SPing-Ke Shih 	u32 val;
1872da4cea16SPing-Ke Shih 	int ret;
1873da4cea16SPing-Ke Shih 
1874da4cea16SPing-Ke Shih 	dpk_cmd = (u16)((id << 8) | (0x19 + path * 0x12));
1875da4cea16SPing-Ke Shih 
1876da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, dpk_cmd);
1877da4cea16SPing-Ke Shih 
1878da4cea16SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
1879da4cea16SPing-Ke Shih 				       10, 20000, false, rtwdev, 0xbff8, MASKBYTE0);
18803be11416SPing-Ke Shih 	udelay(10);
1881da4cea16SPing-Ke Shih 	rtw89_phy_write32_clr(rtwdev, R_NCTL_N1, MASKBYTE0);
1882da4cea16SPing-Ke Shih 
1883da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1884da4cea16SPing-Ke Shih 		    "[DPK] one-shot for %s = 0x%x (ret=%d)\n",
1885da4cea16SPing-Ke Shih 		    id == 0x06 ? "LBK_RXIQK" :
1886da4cea16SPing-Ke Shih 		    id == 0x10 ? "SYNC" :
1887da4cea16SPing-Ke Shih 		    id == 0x11 ? "MDPK_IDL" :
1888da4cea16SPing-Ke Shih 		    id == 0x12 ? "MDPK_MPA" :
1889da4cea16SPing-Ke Shih 		    id == 0x13 ? "GAIN_LOSS" : "PWR_CAL",
1890da4cea16SPing-Ke Shih 		    dpk_cmd, ret);
1891da4cea16SPing-Ke Shih 
1892da4cea16SPing-Ke Shih 	if (ret) {
1893da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1894da4cea16SPing-Ke Shih 			    "[DPK] one-shot over 20ms!!!!\n");
1895da4cea16SPing-Ke Shih 		return 1;
1896da4cea16SPing-Ke Shih 	}
1897da4cea16SPing-Ke Shih 
1898da4cea16SPing-Ke Shih 	return 0;
1899da4cea16SPing-Ke Shih }
1900da4cea16SPing-Ke Shih 
_dpk_information(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)1901da4cea16SPing-Ke Shih static void _dpk_information(struct rtw89_dev *rtwdev,
1902da4cea16SPing-Ke Shih 			     enum rtw89_phy_idx phy,
1903da4cea16SPing-Ke Shih 			     enum rtw89_rf_path path)
1904da4cea16SPing-Ke Shih {
1905cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1906da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
1907da4cea16SPing-Ke Shih 
1908da4cea16SPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
1909da4cea16SPing-Ke Shih 
1910cbb145b9SZong-Zhe Yang 	dpk->bp[path][kidx].band = chan->band_type;
1911cbb145b9SZong-Zhe Yang 	dpk->bp[path][kidx].ch = chan->channel;
1912cbb145b9SZong-Zhe Yang 	dpk->bp[path][kidx].bw = chan->band_width;
1913da4cea16SPing-Ke Shih 
1914da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1915da4cea16SPing-Ke Shih 		    "[DPK] S%d[%d] (PHY%d): TSSI %s/ DBCC %s/ %s/ CH%d/ %s\n",
1916da4cea16SPing-Ke Shih 		    path, dpk->cur_idx[path], phy,
1917da4cea16SPing-Ke Shih 		    rtwdev->is_tssi_mode[path] ? "on" : "off",
1918da4cea16SPing-Ke Shih 		    rtwdev->dbcc_en ? "on" : "off",
1919da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].band == 0 ? "2G" :
1920da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].band == 1 ? "5G" : "6G",
1921da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].ch,
1922da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].bw == 0 ? "20M" :
1923da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].bw == 1 ? "40M" : "80M");
1924da4cea16SPing-Ke Shih }
1925da4cea16SPing-Ke Shih 
_dpk_bb_afe_setting(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kpath)1926da4cea16SPing-Ke Shih static void _dpk_bb_afe_setting(struct rtw89_dev *rtwdev,
1927da4cea16SPing-Ke Shih 				enum rtw89_phy_idx phy,
1928da4cea16SPing-Ke Shih 				enum rtw89_rf_path path, u8 kpath)
1929da4cea16SPing-Ke Shih {
1930da4cea16SPing-Ke Shih 	/*1. Keep ADC_fifo reset*/
1931da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
1932da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
1933da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
1934da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
1935da4cea16SPing-Ke Shih 
1936da4cea16SPing-Ke Shih 	/*2. BB for IQK DBG mode*/
1937da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0xd801dffd);
1938da4cea16SPing-Ke Shih 
1939da4cea16SPing-Ke Shih 	/*3.Set DAC clk*/
1940da4cea16SPing-Ke Shih 	rtw8852c_txck_force(rtwdev, path, true, DAC_960M);
1941da4cea16SPing-Ke Shih 
1942da4cea16SPing-Ke Shih 	/*4. Set ADC clk*/
1943da4cea16SPing-Ke Shih 	rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
1944da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13),
1945da4cea16SPing-Ke Shih 			       B_P0_NRBW_DBG, 0x1);
1946da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, MASKBYTE3, 0x1f);
1947da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, MASKBYTE3, 0x13);
1948da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, MASKHWORD, 0x0001);
1949da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, MASKHWORD, 0x0041);
1950da4cea16SPing-Ke Shih 
1951da4cea16SPing-Ke Shih 	/*5. ADDA fifo rst*/
1952da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x1);
1953da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x1);
1954da4cea16SPing-Ke Shih 
1955da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE setting\n", path);
1956da4cea16SPing-Ke Shih }
1957da4cea16SPing-Ke Shih 
_dpk_bb_afe_restore(struct rtw89_dev * rtwdev,u8 path)1958da4cea16SPing-Ke Shih static void _dpk_bb_afe_restore(struct rtw89_dev *rtwdev, u8 path)
1959da4cea16SPing-Ke Shih {
1960da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13),
1961da4cea16SPing-Ke Shih 			       B_P0_NRBW_DBG, 0x0);
1962da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
1963da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
1964da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
1965da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
1966da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0x00000000);
1967da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK + (path << 13), B_P0_TXCK_ALL, 0x00);
1968da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x0);
1969da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x0);
1970da4cea16SPing-Ke Shih 
1971da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE restore\n", path);
1972da4cea16SPing-Ke Shih }
1973da4cea16SPing-Ke Shih 
_dpk_tssi_pause(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool is_pause)1974da4cea16SPing-Ke Shih static void _dpk_tssi_pause(struct rtw89_dev *rtwdev,
1975da4cea16SPing-Ke Shih 			    enum rtw89_rf_path path, bool is_pause)
1976da4cea16SPing-Ke Shih {
1977da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
1978da4cea16SPing-Ke Shih 			       B_P0_TSSI_TRK_EN, is_pause);
1979da4cea16SPing-Ke Shih 
1980da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d TSSI %s\n", path,
1981da4cea16SPing-Ke Shih 		    is_pause ? "pause" : "resume");
1982da4cea16SPing-Ke Shih }
1983da4cea16SPing-Ke Shih 
_dpk_kip_control_rfc(struct rtw89_dev * rtwdev,u8 path,bool ctrl_by_kip)1984da4cea16SPing-Ke Shih static void _dpk_kip_control_rfc(struct rtw89_dev *rtwdev, u8 path, bool ctrl_by_kip)
1985da4cea16SPing-Ke Shih {
1986da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_IQK_RFC_ON, ctrl_by_kip);
1987da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RFC is controlled by %s\n",
1988da4cea16SPing-Ke Shih 		    ctrl_by_kip ? "KIP" : "BB");
1989da4cea16SPing-Ke Shih }
1990da4cea16SPing-Ke Shih 
_dpk_txpwr_bb_force(struct rtw89_dev * rtwdev,u8 path,bool force)1991da4cea16SPing-Ke Shih static void _dpk_txpwr_bb_force(struct rtw89_dev *rtwdev, u8 path, bool force)
1992da4cea16SPing-Ke Shih {
1993da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_ON, force);
1994da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB_H + (path << 13), B_TXPWRB_RDY, force);
1995da4cea16SPing-Ke Shih 
1996da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,  "[DPK] S%d txpwr_bb_force %s\n",
1997da4cea16SPing-Ke Shih 		    path, force ? "on" : "off");
1998da4cea16SPing-Ke Shih }
1999da4cea16SPing-Ke Shih 
_dpk_kip_restore(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2000da4cea16SPing-Ke Shih static void _dpk_kip_restore(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2001da4cea16SPing-Ke Shih 			     enum rtw89_rf_path path)
2002da4cea16SPing-Ke Shih {
2003da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_KIP_RESTORE);
2004da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
2005da4cea16SPing-Ke Shih 	_dpk_txpwr_bb_force(rtwdev, path, false);
2006da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d restore KIP\n", path);
2007da4cea16SPing-Ke Shih }
2008da4cea16SPing-Ke Shih 
_dpk_lbk_rxiqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2009da4cea16SPing-Ke Shih static void _dpk_lbk_rxiqk(struct rtw89_dev *rtwdev,
2010da4cea16SPing-Ke Shih 			   enum rtw89_phy_idx phy,
2011da4cea16SPing-Ke Shih 			   enum rtw89_rf_path path)
2012da4cea16SPing-Ke Shih {
2013da4cea16SPing-Ke Shih #define RX_TONE_IDX 0x00250025 /* Q.2 9.25MHz */
2014da4cea16SPing-Ke Shih 	u8 cur_rxbb;
2015da4cea16SPing-Ke Shih 	u32 rf_11, reg_81cc;
2016da4cea16SPing-Ke Shih 
2017da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
2018da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x1);
2019da4cea16SPing-Ke Shih 
2020da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
2021da4cea16SPing-Ke Shih 
2022da4cea16SPing-Ke Shih 	cur_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB);
2023da4cea16SPing-Ke Shih 	rf_11 = rtw89_read_rf(rtwdev, path, RR_TXIG, RFREG_MASK);
2024da4cea16SPing-Ke Shih 	reg_81cc = rtw89_phy_read32_mask(rtwdev, R_KIP_IQP + (path << 8),
2025da4cea16SPing-Ke Shih 					 B_KIP_IQP_SW);
2026da4cea16SPing-Ke Shih 
2027da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
2028da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x3);
2029da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0xd);
2030da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, 0x1f);
2031da4cea16SPing-Ke Shih 
2032da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_IQSW, 0x12);
2033da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, 0x3);
2034da4cea16SPing-Ke Shih 
2035da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
2036da4cea16SPing-Ke Shih 
2037da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, MASKDWORD, RX_TONE_IDX);
2038da4cea16SPing-Ke Shih 
2039da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, LBK_RXIQK);
2040da4cea16SPing-Ke Shih 
2041da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d LBK RXIQC = 0x%x\n", path,
2042da4cea16SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD));
2043da4cea16SPing-Ke Shih 
2044da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
2045da4cea16SPing-Ke Shih 
2046da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_TXIG, RFREG_MASK, rf_11);
2047da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, cur_rxbb);
2048da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, reg_81cc);
2049da4cea16SPing-Ke Shih 
2050da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x0);
2051da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KPATH_CFG, B_KPATH_CFG_ED, 0x0);
2052da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_DI, 0x1);
2053da4cea16SPing-Ke Shih 
2054da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
2055da4cea16SPing-Ke Shih }
2056da4cea16SPing-Ke Shih 
_dpk_rf_setting(struct rtw89_dev * rtwdev,u8 gain,enum rtw89_rf_path path,u8 kidx)2057da4cea16SPing-Ke Shih static void _dpk_rf_setting(struct rtw89_dev *rtwdev, u8 gain,
2058da4cea16SPing-Ke Shih 			    enum rtw89_rf_path path, u8 kidx)
2059da4cea16SPing-Ke Shih {
2060da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2061da4cea16SPing-Ke Shih 
2062da4cea16SPing-Ke Shih 	if (dpk->bp[path][kidx].band == RTW89_BAND_2G) {
2063da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
2064da4cea16SPing-Ke Shih 			       0x50121 | BIT(rtwdev->dbcc_en));
2065da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
2066da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTC, 0x2);
2067da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTR, 0x4);
2068da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTDBG, RR_LUTDBG_TIA, 0x1);
2069da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TIA, RR_TIA_N6, 0x1);
2070da4cea16SPing-Ke Shih 
2071da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2072da4cea16SPing-Ke Shih 			    "[DPK] RF 0x0/0x83/0x9e/0x1a/0xdf/0x1001a = 0x%x/ 0x%x/ 0x%x/ 0x%x/ 0x%x/ 0x%x\n",
2073da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK),
2074da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_RXBB, RFREG_MASK),
2075da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_TIA, RFREG_MASK),
2076da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_BTC, RFREG_MASK),
2077da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, RR_LUTDBG, RFREG_MASK),
2078da4cea16SPing-Ke Shih 			    rtw89_read_rf(rtwdev, path, 0x1001a, RFREG_MASK));
2079da4cea16SPing-Ke Shih 	} else {
2080da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
2081da4cea16SPing-Ke Shih 			       0x50101 | BIT(rtwdev->dbcc_en));
2082da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
2083da4cea16SPing-Ke Shih 
208421b5f159SPing-Ke Shih 		if (dpk->bp[path][kidx].band == RTW89_BAND_6G && dpk->bp[path][kidx].ch >= 161)
2085da4cea16SPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_IQGEN, RR_IQGEN_BIAS, 0x8);
208621b5f159SPing-Ke Shih 
2087da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LOGEN, RR_LOGEN_RPT, 0xd);
208821b5f159SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXAC, RR_TXAC_IQG, 0x8);
2089da4cea16SPing-Ke Shih 
2090da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_ATT, 0x0);
2091da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TXIQK, RR_TXIQK_ATT2, 0x3);
2092da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTDBG, RR_LUTDBG_TIA, 0x1);
2093da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_TIA, RR_TIA_N6, 0x1);
2094da4cea16SPing-Ke Shih 
2095da4cea16SPing-Ke Shih 		if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160)
2096da4cea16SPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_EBW, 0x0);
2097da4cea16SPing-Ke Shih 	}
2098da4cea16SPing-Ke Shih }
2099da4cea16SPing-Ke Shih 
_dpk_tpg_sel(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)2100da4cea16SPing-Ke Shih static void _dpk_tpg_sel(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2101da4cea16SPing-Ke Shih {
2102da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2103da4cea16SPing-Ke Shih 
2104da4cea16SPing-Ke Shih 	if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160) {
2105da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x3);
2106da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0x0180ff30);
2107da4cea16SPing-Ke Shih 	} else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80) {
2108da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x0);
2109da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xffe0fa00);
2110da4cea16SPing-Ke Shih 	} else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40) {
2111da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x2);
2112da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xff4009e0);
2113da4cea16SPing-Ke Shih 	} else {
2114da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x1);
2115da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xf9f007d0);
2116da4cea16SPing-Ke Shih 	}
2117da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] TPG_Select for %s\n",
2118da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160 ? "160M" :
2119da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80 ? "80M" :
2120da4cea16SPing-Ke Shih 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ? "40M" : "20M");
2121da4cea16SPing-Ke Shih }
2122da4cea16SPing-Ke Shih 
_dpk_sync_check(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)2123da4cea16SPing-Ke Shih static bool _dpk_sync_check(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2124da4cea16SPing-Ke Shih {
2125da4cea16SPing-Ke Shih #define DPK_SYNC_TH_DC_I 200
2126da4cea16SPing-Ke Shih #define DPK_SYNC_TH_DC_Q 200
2127da4cea16SPing-Ke Shih #define DPK_SYNC_TH_CORR 170
2128da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2129da4cea16SPing-Ke Shih 	u16 dc_i, dc_q;
2130da4cea16SPing-Ke Shih 	u8 corr_val, corr_idx, rxbb;
2131da4cea16SPing-Ke Shih 	u8 rxbb_ov;
2132da4cea16SPing-Ke Shih 
2133da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
2134da4cea16SPing-Ke Shih 
2135da4cea16SPing-Ke Shih 	corr_idx = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORI);
2136da4cea16SPing-Ke Shih 	corr_val = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORV);
2137da4cea16SPing-Ke Shih 
2138da4cea16SPing-Ke Shih 	dpk->corr_idx[path][kidx] = corr_idx;
2139da4cea16SPing-Ke Shih 	dpk->corr_val[path][kidx] = corr_val;
2140da4cea16SPing-Ke Shih 
2141da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x9);
2142da4cea16SPing-Ke Shih 
2143da4cea16SPing-Ke Shih 	dc_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
2144da4cea16SPing-Ke Shih 	dc_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ);
2145da4cea16SPing-Ke Shih 
2146da4cea16SPing-Ke Shih 	dc_i = abs(sign_extend32(dc_i, 11));
2147da4cea16SPing-Ke Shih 	dc_q = abs(sign_extend32(dc_q, 11));
2148da4cea16SPing-Ke Shih 
2149da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2150da4cea16SPing-Ke Shih 		    "[DPK] S%d Corr_idx/ Corr_val /DC I/Q, = %d / %d / %d / %d\n",
2151da4cea16SPing-Ke Shih 		    path, corr_idx, corr_val, dc_i, dc_q);
2152da4cea16SPing-Ke Shih 
2153da4cea16SPing-Ke Shih 	dpk->dc_i[path][kidx] = dc_i;
2154da4cea16SPing-Ke Shih 	dpk->dc_q[path][kidx] = dc_q;
2155da4cea16SPing-Ke Shih 
2156da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x8);
2157da4cea16SPing-Ke Shih 	rxbb = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXBB);
2158da4cea16SPing-Ke Shih 
2159da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x31);
2160da4cea16SPing-Ke Shih 	rxbb_ov = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXOV);
2161da4cea16SPing-Ke Shih 
2162da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2163da4cea16SPing-Ke Shih 		    "[DPK] S%d RXBB/ RXAGC_done /RXBB_ovlmt = %d / %d / %d\n",
2164da4cea16SPing-Ke Shih 		    path, rxbb,
2165da4cea16SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DONE),
2166da4cea16SPing-Ke Shih 		    rxbb_ov);
2167da4cea16SPing-Ke Shih 
2168da4cea16SPing-Ke Shih 	if (dc_i > DPK_SYNC_TH_DC_I || dc_q > DPK_SYNC_TH_DC_Q ||
2169da4cea16SPing-Ke Shih 	    corr_val < DPK_SYNC_TH_CORR)
2170da4cea16SPing-Ke Shih 		return true;
2171da4cea16SPing-Ke Shih 	else
2172da4cea16SPing-Ke Shih 		return false;
2173da4cea16SPing-Ke Shih }
2174da4cea16SPing-Ke Shih 
_dpk_dgain_read(struct rtw89_dev * rtwdev)2175da4cea16SPing-Ke Shih static u16 _dpk_dgain_read(struct rtw89_dev *rtwdev)
2176da4cea16SPing-Ke Shih {
2177da4cea16SPing-Ke Shih 	u16 dgain = 0x0;
2178da4cea16SPing-Ke Shih 
2179da4cea16SPing-Ke Shih 	rtw89_phy_write32_clr(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL);
2180da4cea16SPing-Ke Shih 
2181da4cea16SPing-Ke Shih 	dgain = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
2182da4cea16SPing-Ke Shih 
2183da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] DGain = 0x%x (%d)\n", dgain, dgain);
2184da4cea16SPing-Ke Shih 
2185da4cea16SPing-Ke Shih 	return dgain;
2186da4cea16SPing-Ke Shih }
2187da4cea16SPing-Ke Shih 
_dpk_gainloss_read(struct rtw89_dev * rtwdev)2188da4cea16SPing-Ke Shih static u8 _dpk_gainloss_read(struct rtw89_dev *rtwdev)
2189da4cea16SPing-Ke Shih {
2190da4cea16SPing-Ke Shih 	u8 result;
2191da4cea16SPing-Ke Shih 
2192da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x6);
2193da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x1);
2194da4cea16SPing-Ke Shih 
2195da4cea16SPing-Ke Shih 	result = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_GL);
2196da4cea16SPing-Ke Shih 
2197da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] tmp GL = %d\n", result);
2198da4cea16SPing-Ke Shih 
2199da4cea16SPing-Ke Shih 	return result;
2200da4cea16SPing-Ke Shih }
2201da4cea16SPing-Ke Shih 
_dpk_kset_query(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)2202da4cea16SPing-Ke Shih static void _dpk_kset_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
2203da4cea16SPing-Ke Shih {
2204da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2205da4cea16SPing-Ke Shih 
2206da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0x10);
2207da4cea16SPing-Ke Shih 	dpk->cur_k_set =
2208da4cea16SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), 0xE0000000) - 1;
2209da4cea16SPing-Ke Shih }
2210da4cea16SPing-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)2211da4cea16SPing-Ke Shih static void _dpk_kip_set_txagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2212da4cea16SPing-Ke Shih 			       enum rtw89_rf_path path, u8 dbm, bool set_from_bb)
2213da4cea16SPing-Ke Shih {
2214da4cea16SPing-Ke Shih 	if (set_from_bb) {
2215da4cea16SPing-Ke Shih 		dbm = clamp_t(u8, dbm, 7, 24);
2216da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] set S%d txagc to %ddBm\n", path, dbm);
2217da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_VAL, dbm << 2);
2218da4cea16SPing-Ke Shih 	}
2219da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_TXAGC);
2220da4cea16SPing-Ke Shih 	_dpk_kset_query(rtwdev, path);
2221da4cea16SPing-Ke Shih }
2222da4cea16SPing-Ke Shih 
_dpk_gainloss(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)2223da4cea16SPing-Ke Shih static u8 _dpk_gainloss(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2224da4cea16SPing-Ke Shih 			enum rtw89_rf_path path, u8 kidx)
2225da4cea16SPing-Ke Shih {
2226da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_GAIN_LOSS);
2227da4cea16SPing-Ke Shih 	_dpk_kip_set_txagc(rtwdev, phy, path, 0xff, false);
2228da4cea16SPing-Ke Shih 
2229da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A1, 0x0);
2230da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A0, 0x0);
2231da4cea16SPing-Ke Shih 
2232da4cea16SPing-Ke Shih 	return _dpk_gainloss_read(rtwdev);
2233da4cea16SPing-Ke Shih }
2234da4cea16SPing-Ke Shih 
_dpk_pas_read(struct rtw89_dev * rtwdev,bool is_check)2235ba1a6905SChih-Kang Chang static enum dpk_pas_result _dpk_pas_read(struct rtw89_dev *rtwdev, bool is_check)
2236da4cea16SPing-Ke Shih {
2237da4cea16SPing-Ke Shih 	u32 val1_i = 0, val1_q = 0, val2_i = 0, val2_q = 0;
2238ba1a6905SChih-Kang Chang 	u32 val1_sqrt_sum, val2_sqrt_sum;
2239da4cea16SPing-Ke Shih 	u8 i;
2240da4cea16SPing-Ke Shih 
2241da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKBYTE2, 0x06);
2242da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x0);
2243da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE2, 0x08);
2244da4cea16SPing-Ke Shih 
2245da4cea16SPing-Ke Shih 	if (is_check) {
2246da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x00);
2247da4cea16SPing-Ke Shih 		val1_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
2248da4cea16SPing-Ke Shih 		val1_i = abs(sign_extend32(val1_i, 11));
2249da4cea16SPing-Ke Shih 		val1_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
2250da4cea16SPing-Ke Shih 		val1_q = abs(sign_extend32(val1_q, 11));
2251da4cea16SPing-Ke Shih 
2252da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x1f);
2253da4cea16SPing-Ke Shih 		val2_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
2254da4cea16SPing-Ke Shih 		val2_i = abs(sign_extend32(val2_i, 11));
2255da4cea16SPing-Ke Shih 		val2_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
2256da4cea16SPing-Ke Shih 		val2_q = abs(sign_extend32(val2_q, 11));
2257da4cea16SPing-Ke Shih 
2258da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_delta = 0x%x\n",
2259da4cea16SPing-Ke Shih 			    phy_div(val1_i * val1_i + val1_q * val1_q,
2260da4cea16SPing-Ke Shih 				    val2_i * val2_i + val2_q * val2_q));
2261da4cea16SPing-Ke Shih 	} else {
2262da4cea16SPing-Ke Shih 		for (i = 0; i < 32; i++) {
2263da4cea16SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, i);
2264da4cea16SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_Read[%02d]= 0x%08x\n", i,
2265da4cea16SPing-Ke Shih 				    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
2266da4cea16SPing-Ke Shih 		}
2267da4cea16SPing-Ke Shih 	}
2268da4cea16SPing-Ke Shih 
2269ba1a6905SChih-Kang Chang 	val1_sqrt_sum = val1_i * val1_i + val1_q * val1_q;
2270ba1a6905SChih-Kang Chang 	val2_sqrt_sum = val2_i * val2_i + val2_q * val2_q;
2271ba1a6905SChih-Kang Chang 
2272ba1a6905SChih-Kang Chang 	if (val1_sqrt_sum < val2_sqrt_sum)
2273ba1a6905SChih-Kang Chang 		return DPK_PAS_LT;
2274ba1a6905SChih-Kang Chang 	else if (val1_sqrt_sum >= val2_sqrt_sum * 8 / 5)
2275ba1a6905SChih-Kang Chang 		return DPK_PAS_GT;
2276da4cea16SPing-Ke Shih 	else
2277ba1a6905SChih-Kang Chang 		return DPK_PAS_NOR;
2278da4cea16SPing-Ke Shih }
2279da4cea16SPing-Ke Shih 
_dpk_kip_set_rxagc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)2280da4cea16SPing-Ke Shih static bool _dpk_kip_set_rxagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2281da4cea16SPing-Ke Shih 			       enum rtw89_rf_path path, u8 kidx)
2282da4cea16SPing-Ke Shih {
2283ba1a6905SChih-Kang Chang 	_dpk_kip_control_rfc(rtwdev, path, false);
2284ba1a6905SChih-Kang Chang 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
2285ba1a6905SChih-Kang Chang 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
2286ba1a6905SChih-Kang Chang 	_dpk_kip_control_rfc(rtwdev, path, true);
2287ba1a6905SChih-Kang Chang 
2288da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_RXAGC);
2289da4cea16SPing-Ke Shih 
2290da4cea16SPing-Ke Shih 	return _dpk_sync_check(rtwdev, path, kidx);
2291da4cea16SPing-Ke Shih }
2292da4cea16SPing-Ke Shih 
_dpk_read_rxsram(struct rtw89_dev * rtwdev)2293da4cea16SPing-Ke Shih static void _dpk_read_rxsram(struct rtw89_dev *rtwdev)
2294da4cea16SPing-Ke Shih {
2295da4cea16SPing-Ke Shih 	u32 addr;
2296da4cea16SPing-Ke Shih 
2297da4cea16SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_read_rxsram_pre_defs_tbl);
2298da4cea16SPing-Ke Shih 
2299da4cea16SPing-Ke Shih 	for (addr = 0; addr < 0x200; addr++) {
2300da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00010000 | addr);
2301da4cea16SPing-Ke Shih 
2302da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RXSRAM[%03d] = 0x%07x\n", addr,
2303da4cea16SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
2304da4cea16SPing-Ke Shih 	}
2305da4cea16SPing-Ke Shih 
2306da4cea16SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_read_rxsram_post_defs_tbl);
2307da4cea16SPing-Ke Shih }
2308da4cea16SPing-Ke Shih 
_dpk_bypass_rxiqc(struct rtw89_dev * rtwdev,enum rtw89_rf_path path)2309da4cea16SPing-Ke Shih static void _dpk_bypass_rxiqc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
2310da4cea16SPing-Ke Shih {
2311da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
2312da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD, 0x40000002);
2313da4cea16SPing-Ke Shih 
2314da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Bypass RXIQC\n");
2315da4cea16SPing-Ke Shih }
2316da4cea16SPing-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)2317da4cea16SPing-Ke Shih static u8 _dpk_agc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2318da4cea16SPing-Ke Shih 		   enum rtw89_rf_path path, u8 kidx, u8 init_xdbm, u8 loss_only)
2319da4cea16SPing-Ke Shih {
2320da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2321da4cea16SPing-Ke Shih 	u8 step = DPK_AGC_STEP_SYNC_DGAIN;
2322da4cea16SPing-Ke Shih 	u8 tmp_dbm = init_xdbm, tmp_gl_idx = 0;
2323da4cea16SPing-Ke Shih 	u8 tmp_rxbb;
2324da4cea16SPing-Ke Shih 	u8 goout = 0, agc_cnt = 0;
2325ba1a6905SChih-Kang Chang 	enum dpk_pas_result pas;
2326da4cea16SPing-Ke Shih 	u16 dgain = 0;
2327da4cea16SPing-Ke Shih 	bool is_fail = false;
2328da4cea16SPing-Ke Shih 	int limit = 200;
2329da4cea16SPing-Ke Shih 
2330da4cea16SPing-Ke Shih 	do {
2331da4cea16SPing-Ke Shih 		switch (step) {
2332da4cea16SPing-Ke Shih 		case DPK_AGC_STEP_SYNC_DGAIN:
2333da4cea16SPing-Ke Shih 			is_fail = _dpk_kip_set_rxagc(rtwdev, phy, path, kidx);
2334da4cea16SPing-Ke Shih 
2335da4cea16SPing-Ke Shih 			if (RTW8852C_DPK_RXSRAM_DBG)
2336da4cea16SPing-Ke Shih 				_dpk_read_rxsram(rtwdev);
2337da4cea16SPing-Ke Shih 
2338da4cea16SPing-Ke Shih 			if (is_fail) {
2339da4cea16SPing-Ke Shih 				goout = 1;
2340da4cea16SPing-Ke Shih 				break;
2341da4cea16SPing-Ke Shih 			}
2342da4cea16SPing-Ke Shih 
2343da4cea16SPing-Ke Shih 			dgain = _dpk_dgain_read(rtwdev);
2344da4cea16SPing-Ke Shih 
2345da4cea16SPing-Ke Shih 			if (dgain > 0x5fc || dgain < 0x556) {
2346da4cea16SPing-Ke Shih 				_dpk_one_shot(rtwdev, phy, path, D_SYNC);
2347da4cea16SPing-Ke Shih 				dgain = _dpk_dgain_read(rtwdev);
2348da4cea16SPing-Ke Shih 			}
2349da4cea16SPing-Ke Shih 
2350da4cea16SPing-Ke Shih 			if (agc_cnt == 0) {
2351da4cea16SPing-Ke Shih 				if (dpk->bp[path][kidx].band == RTW89_BAND_2G)
2352da4cea16SPing-Ke Shih 					_dpk_bypass_rxiqc(rtwdev, path);
2353da4cea16SPing-Ke Shih 				else
2354da4cea16SPing-Ke Shih 					_dpk_lbk_rxiqk(rtwdev, phy, path);
2355da4cea16SPing-Ke Shih 			}
2356da4cea16SPing-Ke Shih 			step = DPK_AGC_STEP_GAIN_LOSS_IDX;
2357da4cea16SPing-Ke Shih 			break;
2358da4cea16SPing-Ke Shih 
2359da4cea16SPing-Ke Shih 		case DPK_AGC_STEP_GAIN_LOSS_IDX:
2360da4cea16SPing-Ke Shih 			tmp_gl_idx = _dpk_gainloss(rtwdev, phy, path, kidx);
2361ba1a6905SChih-Kang Chang 			pas = _dpk_pas_read(rtwdev, true);
2362da4cea16SPing-Ke Shih 
2363ba1a6905SChih-Kang Chang 			if (pas == DPK_PAS_LT && tmp_gl_idx > 0)
2364ba1a6905SChih-Kang Chang 				step = DPK_AGC_STEP_GL_LT_CRITERION;
2365ba1a6905SChih-Kang Chang 			else if (pas == DPK_PAS_GT && tmp_gl_idx == 0)
2366ba1a6905SChih-Kang Chang 				step = DPK_AGC_STEP_GL_GT_CRITERION;
2367ba1a6905SChih-Kang Chang 			else if (tmp_gl_idx >= 7)
2368da4cea16SPing-Ke Shih 				step = DPK_AGC_STEP_GL_GT_CRITERION;
2369da4cea16SPing-Ke Shih 			else if (tmp_gl_idx == 0)
2370da4cea16SPing-Ke Shih 				step = DPK_AGC_STEP_GL_LT_CRITERION;
2371da4cea16SPing-Ke Shih 			else
2372da4cea16SPing-Ke Shih 				step = DPK_AGC_STEP_SET_TX_GAIN;
2373da4cea16SPing-Ke Shih 			break;
2374da4cea16SPing-Ke Shih 
2375da4cea16SPing-Ke Shih 		case DPK_AGC_STEP_GL_GT_CRITERION:
2376da4cea16SPing-Ke Shih 			if (tmp_dbm <= 7) {
2377da4cea16SPing-Ke Shih 				goout = 1;
2378da4cea16SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Txagc@lower bound!!\n");
2379da4cea16SPing-Ke Shih 			} else {
2380da4cea16SPing-Ke Shih 				tmp_dbm = max_t(u8, tmp_dbm - 3, 7);
2381da4cea16SPing-Ke Shih 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
2382da4cea16SPing-Ke Shih 			}
2383da4cea16SPing-Ke Shih 			step = DPK_AGC_STEP_SYNC_DGAIN;
2384da4cea16SPing-Ke Shih 			agc_cnt++;
2385da4cea16SPing-Ke Shih 			break;
2386da4cea16SPing-Ke Shih 
2387da4cea16SPing-Ke Shih 		case DPK_AGC_STEP_GL_LT_CRITERION:
2388da4cea16SPing-Ke Shih 			if (tmp_dbm >= 24) {
2389da4cea16SPing-Ke Shih 				goout = 1;
2390da4cea16SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Txagc@upper bound!!\n");
2391da4cea16SPing-Ke Shih 			} else {
2392da4cea16SPing-Ke Shih 				tmp_dbm = min_t(u8, tmp_dbm + 2, 24);
2393da4cea16SPing-Ke Shih 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
2394da4cea16SPing-Ke Shih 			}
2395da4cea16SPing-Ke Shih 			step = DPK_AGC_STEP_SYNC_DGAIN;
2396da4cea16SPing-Ke Shih 			agc_cnt++;
2397da4cea16SPing-Ke Shih 			break;
2398da4cea16SPing-Ke Shih 
2399da4cea16SPing-Ke Shih 		case DPK_AGC_STEP_SET_TX_GAIN:
2400da4cea16SPing-Ke Shih 			_dpk_kip_control_rfc(rtwdev, path, false);
2401da4cea16SPing-Ke Shih 			tmp_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB);
2402da4cea16SPing-Ke Shih 			if (tmp_rxbb + tmp_gl_idx > 0x1f)
2403da4cea16SPing-Ke Shih 				tmp_rxbb = 0x1f;
2404da4cea16SPing-Ke Shih 			else
2405da4cea16SPing-Ke Shih 				tmp_rxbb = tmp_rxbb + tmp_gl_idx;
2406da4cea16SPing-Ke Shih 
2407da4cea16SPing-Ke Shih 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, tmp_rxbb);
2408da4cea16SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Adjust RXBB (%+d) = 0x%x\n",
2409da4cea16SPing-Ke Shih 				    tmp_gl_idx, tmp_rxbb);
2410da4cea16SPing-Ke Shih 			_dpk_kip_control_rfc(rtwdev, path, true);
2411da4cea16SPing-Ke Shih 			goout = 1;
2412da4cea16SPing-Ke Shih 			break;
2413da4cea16SPing-Ke Shih 		default:
2414da4cea16SPing-Ke Shih 			goout = 1;
2415da4cea16SPing-Ke Shih 			break;
2416da4cea16SPing-Ke Shih 		}
2417da4cea16SPing-Ke Shih 	} while (!goout && agc_cnt < 6 && --limit > 0);
2418da4cea16SPing-Ke Shih 
2419da4cea16SPing-Ke Shih 	if (limit <= 0)
2420da4cea16SPing-Ke Shih 		rtw89_warn(rtwdev, "[DPK] exceed loop limit\n");
2421da4cea16SPing-Ke Shih 
2422da4cea16SPing-Ke Shih 	return is_fail;
2423da4cea16SPing-Ke Shih }
2424da4cea16SPing-Ke Shih 
_dpk_set_mdpd_para(struct rtw89_dev * rtwdev,u8 order)2425da4cea16SPing-Ke Shih static void _dpk_set_mdpd_para(struct rtw89_dev *rtwdev, u8 order)
2426da4cea16SPing-Ke Shih {
2427da4cea16SPing-Ke Shih 	static const struct rtw89_rfk_tbl *order_tbls[] = {
2428da4cea16SPing-Ke Shih 		&rtw8852c_dpk_mdpd_order0_defs_tbl,
2429da4cea16SPing-Ke Shih 		&rtw8852c_dpk_mdpd_order1_defs_tbl,
2430da4cea16SPing-Ke Shih 		&rtw8852c_dpk_mdpd_order2_defs_tbl,
2431da4cea16SPing-Ke Shih 		&rtw8852c_dpk_mdpd_order3_defs_tbl,
2432da4cea16SPing-Ke Shih 	};
2433da4cea16SPing-Ke Shih 
2434da4cea16SPing-Ke Shih 	if (order >= ARRAY_SIZE(order_tbls)) {
2435da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Wrong MDPD order!!(0x%x)\n", order);
2436da4cea16SPing-Ke Shih 		return;
2437da4cea16SPing-Ke Shih 	}
2438da4cea16SPing-Ke Shih 
2439da4cea16SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, order_tbls[order]);
2440da4cea16SPing-Ke Shih 
2441da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Set %s for IDL\n",
2442da4cea16SPing-Ke Shih 		    order == 0x0 ? "(5,3,1)" :
2443da4cea16SPing-Ke Shih 		    order == 0x1 ? "(5,3,0)" :
2444da4cea16SPing-Ke Shih 		    order == 0x2 ? "(5,0,0)" : "(7,3,1)");
2445da4cea16SPing-Ke Shih }
2446da4cea16SPing-Ke Shih 
_dpk_idl_mpa(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)2447da4cea16SPing-Ke Shih static void _dpk_idl_mpa(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2448da4cea16SPing-Ke Shih 			 enum rtw89_rf_path path, u8 kidx)
2449da4cea16SPing-Ke Shih {
2450da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2451da4cea16SPing-Ke Shih 	u8 cnt;
2452da4cea16SPing-Ke Shih 	u8 ov_flag;
2453da4cea16SPing-Ke Shih 	u32 dpk_sync;
2454da4cea16SPing-Ke Shih 
2455da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_MA, 0x1);
2456da4cea16SPing-Ke Shih 
2457da4cea16SPing-Ke Shih 	if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T2) == 0x1)
2458da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x2);
2459da4cea16SPing-Ke Shih 	else if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T1) == 0x1)
2460da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x1);
2461da4cea16SPing-Ke Shih 	else if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T0) == 0x1)
2462da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x0);
2463da4cea16SPing-Ke Shih 	else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_5 ||
2464da4cea16SPing-Ke Shih 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_10 ||
2465da4cea16SPing-Ke Shih 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_20)
2466da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x2);
2467da4cea16SPing-Ke Shih 	else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ||
2468da4cea16SPing-Ke Shih 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80)
2469da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x1);
2470da4cea16SPing-Ke Shih 	else
2471da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x0);
2472da4cea16SPing-Ke Shih 
2473da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL, 0x0);
2474da4cea16SPing-Ke Shih 	fsleep(1000);
2475da4cea16SPing-Ke Shih 
2476da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2477da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
2478da4cea16SPing-Ke Shih 	dpk_sync = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD);
2479da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] dpk_sync = 0x%x\n", dpk_sync);
2480da4cea16SPing-Ke Shih 
2481da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0xf);
2482da4cea16SPing-Ke Shih 	ov_flag = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_SYNERR);
2483da4cea16SPing-Ke Shih 	for (cnt = 0; cnt < 5 && ov_flag == 0x1; cnt++) {
2484da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] ReK due to MDPK ov!!!\n");
2485da4cea16SPing-Ke Shih 		_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2486da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0xf);
2487da4cea16SPing-Ke Shih 		ov_flag = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_SYNERR);
2488da4cea16SPing-Ke Shih 	}
2489da4cea16SPing-Ke Shih 
2490da4cea16SPing-Ke Shih 	if (ov_flag) {
2491da4cea16SPing-Ke Shih 		_dpk_set_mdpd_para(rtwdev, 0x2);
2492da4cea16SPing-Ke Shih 		_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2493da4cea16SPing-Ke Shih 	}
2494da4cea16SPing-Ke Shih }
2495da4cea16SPing-Ke Shih 
_dpk_reload_check(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2496da4cea16SPing-Ke Shih static bool _dpk_reload_check(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2497da4cea16SPing-Ke Shih 			      enum rtw89_rf_path path)
2498da4cea16SPing-Ke Shih {
2499cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2500da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2501da4cea16SPing-Ke Shih 	bool is_reload = false;
2502da4cea16SPing-Ke Shih 	u8 idx, cur_band, cur_ch;
2503da4cea16SPing-Ke Shih 
2504cbb145b9SZong-Zhe Yang 	cur_band = chan->band_type;
2505cbb145b9SZong-Zhe Yang 	cur_ch = chan->channel;
2506da4cea16SPing-Ke Shih 
2507da4cea16SPing-Ke Shih 	for (idx = 0; idx < RTW89_DPK_BKUP_NUM; idx++) {
2508da4cea16SPing-Ke Shih 		if (cur_band != dpk->bp[path][idx].band ||
2509da4cea16SPing-Ke Shih 		    cur_ch != dpk->bp[path][idx].ch)
2510da4cea16SPing-Ke Shih 			continue;
2511da4cea16SPing-Ke Shih 
2512da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8),
2513da4cea16SPing-Ke Shih 				       B_COEF_SEL_MDPD, idx);
2514da4cea16SPing-Ke Shih 		dpk->cur_idx[path] = idx;
2515da4cea16SPing-Ke Shih 		is_reload = true;
2516da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2517da4cea16SPing-Ke Shih 			    "[DPK] reload S%d[%d] success\n", path, idx);
2518da4cea16SPing-Ke Shih 	}
2519da4cea16SPing-Ke Shih 
2520da4cea16SPing-Ke Shih 	return is_reload;
2521da4cea16SPing-Ke Shih }
2522da4cea16SPing-Ke Shih 
_dpk_kip_pwr_clk_onoff(struct rtw89_dev * rtwdev,bool turn_on)2523da4cea16SPing-Ke Shih static void _dpk_kip_pwr_clk_onoff(struct rtw89_dev *rtwdev, bool turn_on)
2524da4cea16SPing-Ke Shih {
2525da4cea16SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, turn_on ? &rtw8852c_dpk_kip_pwr_clk_on_defs_tbl :
2526da4cea16SPing-Ke Shih 					   &rtw8852c_dpk_kip_pwr_clk_off_defs_tbl);
2527da4cea16SPing-Ke Shih }
2528da4cea16SPing-Ke Shih 
_dpk_kip_preset_8852c(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)2529da4cea16SPing-Ke Shih static void _dpk_kip_preset_8852c(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2530da4cea16SPing-Ke Shih 				  enum rtw89_rf_path path, u8 kidx)
2531da4cea16SPing-Ke Shih {
2532da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
2533da4cea16SPing-Ke Shih 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
2534da4cea16SPing-Ke Shih 
2535da4cea16SPing-Ke Shih 	if (rtwdev->hal.cv == CHIP_CAV)
2536da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev,
2537da4cea16SPing-Ke Shih 				       R_DPD_CH0A + (path << 8) + (kidx << 2),
2538da4cea16SPing-Ke Shih 				       B_DPD_SEL, 0x01);
2539da4cea16SPing-Ke Shih 	else
2540da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev,
2541da4cea16SPing-Ke Shih 				       R_DPD_CH0A + (path << 8) + (kidx << 2),
2542da4cea16SPing-Ke Shih 				       B_DPD_SEL, 0x0c);
2543da4cea16SPing-Ke Shih 
2544da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, true);
2545da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8), B_COEF_SEL_MDPD, kidx);
2546da4cea16SPing-Ke Shih 
2547da4cea16SPing-Ke Shih 	_dpk_one_shot(rtwdev, phy, path, D_KIP_PRESET);
2548da4cea16SPing-Ke Shih }
2549da4cea16SPing-Ke Shih 
_dpk_para_query(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 kidx)2550da4cea16SPing-Ke Shih static void _dpk_para_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2551da4cea16SPing-Ke Shih {
2552da4cea16SPing-Ke Shih #define _DPK_PARA_TXAGC GENMASK(15, 10)
2553da4cea16SPing-Ke Shih #define _DPK_PARA_THER GENMASK(31, 26)
2554da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2555da4cea16SPing-Ke Shih 	u32 para;
2556da4cea16SPing-Ke Shih 
2557da4cea16SPing-Ke Shih 	para = rtw89_phy_read32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2558da4cea16SPing-Ke Shih 				     MASKDWORD);
2559da4cea16SPing-Ke Shih 
2560da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].txagc_dpk = FIELD_GET(_DPK_PARA_TXAGC, para);
2561da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].ther_dpk = FIELD_GET(_DPK_PARA_THER, para);
2562da4cea16SPing-Ke Shih 
2563da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] thermal/ txagc_RF (K%d) = 0x%x/ 0x%x\n",
2564da4cea16SPing-Ke Shih 		    dpk->cur_k_set, dpk->bp[path][kidx].ther_dpk, dpk->bp[path][kidx].txagc_dpk);
2565da4cea16SPing-Ke Shih }
2566da4cea16SPing-Ke Shih 
_dpk_gain_normalize_8852c(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx,bool is_execute)2567da4cea16SPing-Ke Shih static void _dpk_gain_normalize_8852c(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2568da4cea16SPing-Ke Shih 				      enum rtw89_rf_path path, u8 kidx, bool is_execute)
2569da4cea16SPing-Ke Shih {
2570da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2571da4cea16SPing-Ke Shih 
2572da4cea16SPing-Ke Shih 	if (is_execute) {
2573da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8), B_DPK_GN_AG, 0x200);
2574da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8), B_DPK_GN_EN, 0x3);
2575da4cea16SPing-Ke Shih 
2576da4cea16SPing-Ke Shih 		_dpk_one_shot(rtwdev, phy, path, D_GAIN_NORM);
2577da4cea16SPing-Ke Shih 	} else {
2578da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2579da4cea16SPing-Ke Shih 				       0x0000007F, 0x5b);
2580da4cea16SPing-Ke Shih 	}
2581da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].gs =
2582da4cea16SPing-Ke Shih 		rtw89_phy_read32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2583da4cea16SPing-Ke Shih 				      0x0000007F);
2584da4cea16SPing-Ke Shih }
2585da4cea16SPing-Ke Shih 
_dpk_order_convert(struct rtw89_dev * rtwdev)2586da4cea16SPing-Ke Shih static u8 _dpk_order_convert(struct rtw89_dev *rtwdev)
2587da4cea16SPing-Ke Shih {
2588da4cea16SPing-Ke Shih 	u32 val32 = rtw89_phy_read32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP);
2589da4cea16SPing-Ke Shih 	u8 val;
2590da4cea16SPing-Ke Shih 
2591da4cea16SPing-Ke Shih 	switch (val32) {
2592da4cea16SPing-Ke Shih 	case 0:
2593da4cea16SPing-Ke Shih 		val = 0x6;
2594da4cea16SPing-Ke Shih 		break;
2595da4cea16SPing-Ke Shih 	case 1:
2596da4cea16SPing-Ke Shih 		val = 0x2;
2597da4cea16SPing-Ke Shih 		break;
2598da4cea16SPing-Ke Shih 	case 2:
2599da4cea16SPing-Ke Shih 		val = 0x0;
2600da4cea16SPing-Ke Shih 		break;
2601da4cea16SPing-Ke Shih 	case 3:
2602da4cea16SPing-Ke Shih 		val = 0x7;
2603da4cea16SPing-Ke Shih 		break;
2604da4cea16SPing-Ke Shih 	default:
2605da4cea16SPing-Ke Shih 		val = 0xff;
2606da4cea16SPing-Ke Shih 		break;
2607da4cea16SPing-Ke Shih 	}
2608da4cea16SPing-Ke Shih 
2609da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] convert MDPD order to 0x%x\n", val);
2610da4cea16SPing-Ke Shih 
2611da4cea16SPing-Ke Shih 	return val;
2612da4cea16SPing-Ke Shih }
2613da4cea16SPing-Ke Shih 
_dpk_on(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 kidx)2614da4cea16SPing-Ke Shih static void _dpk_on(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2615da4cea16SPing-Ke Shih 		    enum rtw89_rf_path path, u8 kidx)
2616da4cea16SPing-Ke Shih {
2617da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2618da4cea16SPing-Ke Shih 
2619da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x1);
2620da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x0);
2621da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2622da4cea16SPing-Ke Shih 			       B_DPD_ORDER, _dpk_order_convert(rtwdev));
2623da4cea16SPing-Ke Shih 
2624da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].mdpd_en = BIT(dpk->cur_k_set);
2625da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].path_ok = true;
2626da4cea16SPing-Ke Shih 
2627da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] path_ok = 0x%x\n",
2628da4cea16SPing-Ke Shih 		    path, kidx, dpk->bp[path][kidx].mdpd_en);
2629da4cea16SPing-Ke Shih 
2630da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2631da4cea16SPing-Ke Shih 			       B_DPD_MEN, dpk->bp[path][kidx].mdpd_en);
2632da4cea16SPing-Ke Shih 
2633da4cea16SPing-Ke Shih 	_dpk_gain_normalize_8852c(rtwdev, phy, path, kidx, false);
2634da4cea16SPing-Ke Shih }
2635da4cea16SPing-Ke Shih 
_dpk_main(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path,u8 gain)2636da4cea16SPing-Ke Shih static bool _dpk_main(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2637da4cea16SPing-Ke Shih 		      enum rtw89_rf_path path, u8 gain)
2638da4cea16SPing-Ke Shih {
2639da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2640da4cea16SPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
2641da4cea16SPing-Ke Shih 	u8 init_xdbm = 15;
2642da4cea16SPing-Ke Shih 	bool is_fail;
2643da4cea16SPing-Ke Shih 
2644da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2645da4cea16SPing-Ke Shih 		    "[DPK] ========= S%d[%d] DPK Start =========\n", path, kidx);
2646da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
2647da4cea16SPing-Ke Shih 	_rf_direct_cntrl(rtwdev, path, false);
2648da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_BBDC, RFREG_MASK, 0x03ffd);
2649da4cea16SPing-Ke Shih 	_dpk_rf_setting(rtwdev, gain, path, kidx);
2650da4cea16SPing-Ke Shih 	_set_rx_dck(rtwdev, phy, path, false);
2651da4cea16SPing-Ke Shih 	_dpk_kip_pwr_clk_onoff(rtwdev, true);
2652da4cea16SPing-Ke Shih 	_dpk_kip_preset_8852c(rtwdev, phy, path, kidx);
2653da4cea16SPing-Ke Shih 	_dpk_txpwr_bb_force(rtwdev, path, true);
2654da4cea16SPing-Ke Shih 	_dpk_kip_set_txagc(rtwdev, phy, path, init_xdbm, true);
2655da4cea16SPing-Ke Shih 	_dpk_tpg_sel(rtwdev, path, kidx);
2656da4cea16SPing-Ke Shih 
2657da4cea16SPing-Ke Shih 	is_fail = _dpk_agc(rtwdev, phy, path, kidx, init_xdbm, false);
2658da4cea16SPing-Ke Shih 	if (is_fail)
2659da4cea16SPing-Ke Shih 		goto _error;
2660da4cea16SPing-Ke Shih 
2661da4cea16SPing-Ke Shih 	_dpk_idl_mpa(rtwdev, phy, path, kidx);
2662da4cea16SPing-Ke Shih 	_dpk_para_query(rtwdev, path, kidx);
2663da4cea16SPing-Ke Shih 	_dpk_on(rtwdev, phy, path, kidx);
2664da4cea16SPing-Ke Shih 
2665da4cea16SPing-Ke Shih _error:
2666da4cea16SPing-Ke Shih 	_dpk_kip_control_rfc(rtwdev, path, false);
2667da4cea16SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
2668da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d]_K%d %s\n", path, kidx,
2669da4cea16SPing-Ke Shih 		    dpk->cur_k_set, is_fail ? "need Check" : "is Success");
2670da4cea16SPing-Ke Shih 
2671da4cea16SPing-Ke Shih 	return is_fail;
2672da4cea16SPing-Ke Shih }
2673da4cea16SPing-Ke Shih 
_dpk_init(struct rtw89_dev * rtwdev,u8 path)2674da4cea16SPing-Ke Shih static void _dpk_init(struct rtw89_dev *rtwdev, u8 path)
2675da4cea16SPing-Ke Shih {
2676da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2677da4cea16SPing-Ke Shih 	u8 kidx = dpk->cur_idx[path];
2678da4cea16SPing-Ke Shih 
2679da4cea16SPing-Ke Shih 	dpk->bp[path][kidx].path_ok = false;
2680da4cea16SPing-Ke Shih }
2681da4cea16SPing-Ke Shih 
_dpk_drf_direct_cntrl(struct rtw89_dev * rtwdev,u8 path,bool is_bybb)2682da4cea16SPing-Ke Shih static void _dpk_drf_direct_cntrl(struct rtw89_dev *rtwdev, u8 path, bool is_bybb)
2683da4cea16SPing-Ke Shih {
2684da4cea16SPing-Ke Shih 	if (is_bybb)
2685da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x1);
2686da4cea16SPing-Ke Shih 	else
2687da4cea16SPing-Ke Shih 		rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x0);
2688da4cea16SPing-Ke Shih }
2689da4cea16SPing-Ke Shih 
_dpk_cal_select(struct rtw89_dev * rtwdev,bool force,enum rtw89_phy_idx phy,u8 kpath)2690da4cea16SPing-Ke Shih static void _dpk_cal_select(struct rtw89_dev *rtwdev, bool force,
2691da4cea16SPing-Ke Shih 			    enum rtw89_phy_idx phy, u8 kpath)
2692da4cea16SPing-Ke Shih {
2693da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
26943aa83062SChih-Kang Chang 	static const u32 kip_reg[] = {0x813c, 0x8124, 0x8120, 0xc0c4, 0xc0e8, 0xc0d4, 0xc0d8};
2695da4cea16SPing-Ke Shih 	u32 backup_rf_val[RTW8852C_DPK_RF_PATH][BACKUP_RF_REGS_NR];
2696da4cea16SPing-Ke Shih 	u32 kip_bkup[RTW8852C_DPK_RF_PATH][RTW8852C_DPK_KIP_REG_NUM] = {};
2697da4cea16SPing-Ke Shih 	u8 path;
2698da4cea16SPing-Ke Shih 	bool is_fail = true, reloaded[RTW8852C_DPK_RF_PATH] = {false};
2699da4cea16SPing-Ke Shih 
27003aa83062SChih-Kang Chang 	static_assert(ARRAY_SIZE(kip_reg) == RTW8852C_DPK_KIP_REG_NUM);
27013aa83062SChih-Kang Chang 
2702da4cea16SPing-Ke Shih 	if (dpk->is_dpk_reload_en) {
2703da4cea16SPing-Ke Shih 		for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2704da4cea16SPing-Ke Shih 			if (!(kpath & BIT(path)))
2705da4cea16SPing-Ke Shih 				continue;
2706da4cea16SPing-Ke Shih 
2707da4cea16SPing-Ke Shih 			reloaded[path] = _dpk_reload_check(rtwdev, phy, path);
2708da4cea16SPing-Ke Shih 			if (!reloaded[path] && dpk->bp[path][0].ch != 0)
2709da4cea16SPing-Ke Shih 				dpk->cur_idx[path] = !dpk->cur_idx[path];
2710da4cea16SPing-Ke Shih 			else
2711da4cea16SPing-Ke Shih 				_dpk_onoff(rtwdev, path, false);
2712da4cea16SPing-Ke Shih 		}
2713da4cea16SPing-Ke Shih 	} else {
2714da4cea16SPing-Ke Shih 		for (path = 0; path < RTW8852C_DPK_RF_PATH; path++)
2715da4cea16SPing-Ke Shih 			dpk->cur_idx[path] = 0;
2716da4cea16SPing-Ke Shih 	}
2717da4cea16SPing-Ke Shih 
2718da4cea16SPing-Ke Shih 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2719da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2720da4cea16SPing-Ke Shih 			    "[DPK] ========= S%d[%d] DPK Init =========\n",
2721da4cea16SPing-Ke Shih 			    path, dpk->cur_idx[path]);
2722da4cea16SPing-Ke Shih 		_dpk_bkup_kip(rtwdev, kip_reg, kip_bkup, path);
2723da4cea16SPing-Ke Shih 		_rfk_backup_rf_reg(rtwdev, backup_rf_val[path], path);
2724da4cea16SPing-Ke Shih 		_dpk_information(rtwdev, phy, path);
2725da4cea16SPing-Ke Shih 		_dpk_init(rtwdev, path);
2726da4cea16SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
2727da4cea16SPing-Ke Shih 			_dpk_tssi_pause(rtwdev, path, true);
2728da4cea16SPing-Ke Shih 	}
2729da4cea16SPing-Ke Shih 
2730da4cea16SPing-Ke Shih 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2731da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2732da4cea16SPing-Ke Shih 			    "[DPK] ========= S%d[%d] DPK Start =========\n",
2733da4cea16SPing-Ke Shih 			    path, dpk->cur_idx[path]);
2734da4cea16SPing-Ke Shih 		rtw8852c_disable_rxagc(rtwdev, path, 0x0);
2735da4cea16SPing-Ke Shih 		_dpk_drf_direct_cntrl(rtwdev, path, false);
2736da4cea16SPing-Ke Shih 		_dpk_bb_afe_setting(rtwdev, phy, path, kpath);
2737da4cea16SPing-Ke Shih 		is_fail = _dpk_main(rtwdev, phy, path, 1);
2738da4cea16SPing-Ke Shih 		_dpk_onoff(rtwdev, path, is_fail);
2739da4cea16SPing-Ke Shih 	}
2740da4cea16SPing-Ke Shih 
2741da4cea16SPing-Ke Shih 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2742da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2743da4cea16SPing-Ke Shih 			    "[DPK] ========= S%d[%d] DPK Restore =========\n",
2744da4cea16SPing-Ke Shih 			    path, dpk->cur_idx[path]);
2745da4cea16SPing-Ke Shih 		_dpk_kip_restore(rtwdev, phy, path);
2746da4cea16SPing-Ke Shih 		_dpk_reload_kip(rtwdev, kip_reg, kip_bkup, path);
2747da4cea16SPing-Ke Shih 		_rfk_restore_rf_reg(rtwdev, backup_rf_val[path], path);
2748da4cea16SPing-Ke Shih 		_dpk_bb_afe_restore(rtwdev, path);
2749da4cea16SPing-Ke Shih 		rtw8852c_disable_rxagc(rtwdev, path, 0x1);
2750da4cea16SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
2751da4cea16SPing-Ke Shih 			_dpk_tssi_pause(rtwdev, path, false);
2752da4cea16SPing-Ke Shih 	}
2753da4cea16SPing-Ke Shih 
2754da4cea16SPing-Ke Shih 	_dpk_kip_pwr_clk_onoff(rtwdev, false);
2755da4cea16SPing-Ke Shih }
2756da4cea16SPing-Ke Shih 
_dpk_bypass_check(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)2757da4cea16SPing-Ke Shih static bool _dpk_bypass_check(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
2758da4cea16SPing-Ke Shih {
2759da4cea16SPing-Ke Shih 	struct rtw89_fem_info *fem = &rtwdev->fem;
2760cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2761cbb145b9SZong-Zhe Yang 	u8 band = chan->band_type;
2762da4cea16SPing-Ke Shih 
2763cbb145b9SZong-Zhe Yang 	if (rtwdev->hal.cv == CHIP_CAV && band != RTW89_BAND_2G) {
2764da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to CAV & not 2G!!\n");
2765da4cea16SPing-Ke Shih 		return true;
2766cbb145b9SZong-Zhe Yang 	} else if (fem->epa_2g && band == RTW89_BAND_2G) {
2767da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 2G_ext_PA exist!!\n");
2768da4cea16SPing-Ke Shih 		return true;
2769cbb145b9SZong-Zhe Yang 	} else if (fem->epa_5g && band == RTW89_BAND_5G) {
2770da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 5G_ext_PA exist!!\n");
2771da4cea16SPing-Ke Shih 		return true;
2772cbb145b9SZong-Zhe Yang 	} else if (fem->epa_6g && band == RTW89_BAND_6G) {
2773da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 6G_ext_PA exist!!\n");
2774da4cea16SPing-Ke Shih 		return true;
2775da4cea16SPing-Ke Shih 	}
2776da4cea16SPing-Ke Shih 
2777da4cea16SPing-Ke Shih 	return false;
2778da4cea16SPing-Ke Shih }
2779da4cea16SPing-Ke Shih 
_dpk_force_bypass(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)2780da4cea16SPing-Ke Shih static void _dpk_force_bypass(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
2781da4cea16SPing-Ke Shih {
2782da4cea16SPing-Ke Shih 	u8 path, kpath;
2783da4cea16SPing-Ke Shih 
2784da4cea16SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
2785da4cea16SPing-Ke Shih 
2786da4cea16SPing-Ke Shih 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2787da4cea16SPing-Ke Shih 		if (kpath & BIT(path))
2788da4cea16SPing-Ke Shih 			_dpk_onoff(rtwdev, path, true);
2789da4cea16SPing-Ke Shih 	}
2790da4cea16SPing-Ke Shih }
2791da4cea16SPing-Ke Shih 
_dpk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool force)2792da4cea16SPing-Ke Shih static void _dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool force)
2793da4cea16SPing-Ke Shih {
2794da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2795da4cea16SPing-Ke Shih 		    "[DPK] ****** DPK Start (Ver: 0x%x, Cv: %d, RF_para: %d) ******\n",
2796da4cea16SPing-Ke Shih 		    RTW8852C_DPK_VER, rtwdev->hal.cv,
2797da4cea16SPing-Ke Shih 		    RTW8852C_RF_REL_VERSION);
2798da4cea16SPing-Ke Shih 
2799da4cea16SPing-Ke Shih 	if (_dpk_bypass_check(rtwdev, phy))
2800da4cea16SPing-Ke Shih 		_dpk_force_bypass(rtwdev, phy);
2801da4cea16SPing-Ke Shih 	else
2802da4cea16SPing-Ke Shih 		_dpk_cal_select(rtwdev, force, phy, _kpath(rtwdev, phy));
2803da4cea16SPing-Ke Shih 
2804da4cea16SPing-Ke Shih 	if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_DCKC, RR_DCKC_CHK) == 0x1)
2805da4cea16SPing-Ke Shih 		rtw8852c_rx_dck(rtwdev, phy, false);
2806da4cea16SPing-Ke Shih }
2807da4cea16SPing-Ke Shih 
_dpk_onoff(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,bool off)2808da4cea16SPing-Ke Shih static void _dpk_onoff(struct rtw89_dev *rtwdev,
2809da4cea16SPing-Ke Shih 		       enum rtw89_rf_path path, bool off)
2810da4cea16SPing-Ke Shih {
2811da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2812da4cea16SPing-Ke Shih 	u8 val, kidx = dpk->cur_idx[path];
2813da4cea16SPing-Ke Shih 
2814da4cea16SPing-Ke Shih 	val = dpk->is_dpk_enable && !off && dpk->bp[path][kidx].path_ok ?
2815da4cea16SPing-Ke Shih 	      dpk->bp[path][kidx].mdpd_en : 0;
2816da4cea16SPing-Ke Shih 
2817da4cea16SPing-Ke Shih 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2818da4cea16SPing-Ke Shih 			       B_DPD_MEN, val);
2819da4cea16SPing-Ke Shih 
2820da4cea16SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] DPK %s !!!\n", path,
2821da4cea16SPing-Ke Shih 		    kidx, dpk->is_dpk_enable && !off ? "enable" : "disable");
2822da4cea16SPing-Ke Shih }
2823da4cea16SPing-Ke Shih 
_dpk_track(struct rtw89_dev * rtwdev)2824da4cea16SPing-Ke Shih static void _dpk_track(struct rtw89_dev *rtwdev)
2825da4cea16SPing-Ke Shih {
2826da4cea16SPing-Ke Shih 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2827da4cea16SPing-Ke Shih 	u8 path, kidx;
2828da4cea16SPing-Ke Shih 	u8 txagc_rf = 0;
2829da4cea16SPing-Ke Shih 	s8 txagc_bb = 0, txagc_bb_tp = 0, txagc_ofst = 0;
2830da4cea16SPing-Ke Shih 	u8 cur_ther;
2831da4cea16SPing-Ke Shih 	s8 delta_ther = 0;
2832da4cea16SPing-Ke Shih 	s16 pwsf_tssi_ofst;
2833da4cea16SPing-Ke Shih 
2834da4cea16SPing-Ke Shih 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2835da4cea16SPing-Ke Shih 		kidx = dpk->cur_idx[path];
2836da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2837da4cea16SPing-Ke Shih 			    "[DPK_TRK] ================[S%d[%d] (CH %d)]================\n",
2838da4cea16SPing-Ke Shih 			    path, kidx, dpk->bp[path][kidx].ch);
2839da4cea16SPing-Ke Shih 
2840da4cea16SPing-Ke Shih 		txagc_rf =
2841da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13), 0x0000003f);
2842da4cea16SPing-Ke Shih 		txagc_bb =
2843da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13), MASKBYTE2);
2844da4cea16SPing-Ke Shih 		txagc_bb_tp =
2845da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BTP + (path << 13), B_TXAGC_BTP);
2846da4cea16SPing-Ke Shih 
2847da4cea16SPing-Ke Shih 		/* report from KIP */
2848da4cea16SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0xf);
2849da4cea16SPing-Ke Shih 		cur_ther =
2850da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_TH);
2851da4cea16SPing-Ke Shih 		txagc_ofst =
2852da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_OF);
2853da4cea16SPing-Ke Shih 		pwsf_tssi_ofst =
2854da4cea16SPing-Ke Shih 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_TSSI);
2855da4cea16SPing-Ke Shih 		pwsf_tssi_ofst = sign_extend32(pwsf_tssi_ofst, 12);
2856da4cea16SPing-Ke Shih 
2857da4cea16SPing-Ke Shih 		cur_ther = ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
2858da4cea16SPing-Ke Shih 
2859da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2860da4cea16SPing-Ke Shih 			    "[DPK_TRK] thermal now = %d\n", cur_ther);
2861da4cea16SPing-Ke Shih 
2862da4cea16SPing-Ke Shih 		if (dpk->bp[path][kidx].ch != 0 && cur_ther != 0)
2863da4cea16SPing-Ke Shih 			delta_ther = dpk->bp[path][kidx].ther_dpk - cur_ther;
2864da4cea16SPing-Ke Shih 
2865da4cea16SPing-Ke Shih 		delta_ther = delta_ther * 1 / 2;
2866da4cea16SPing-Ke Shih 
2867da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2868da4cea16SPing-Ke Shih 			    "[DPK_TRK] extra delta_ther = %d (0x%x / 0x%x@k)\n",
2869da4cea16SPing-Ke Shih 			    delta_ther, cur_ther, dpk->bp[path][kidx].ther_dpk);
2870da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2871da4cea16SPing-Ke Shih 			    "[DPK_TRK] delta_txagc = %d (0x%x / 0x%x@k)\n",
2872da4cea16SPing-Ke Shih 			    txagc_rf - dpk->bp[path][kidx].txagc_dpk, txagc_rf,
2873da4cea16SPing-Ke Shih 			    dpk->bp[path][kidx].txagc_dpk);
2874da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2875da4cea16SPing-Ke Shih 			    "[DPK_TRK] txagc_offset / pwsf_tssi_ofst = 0x%x / %+d\n",
2876da4cea16SPing-Ke Shih 			    txagc_ofst, pwsf_tssi_ofst);
2877da4cea16SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2878da4cea16SPing-Ke Shih 			    "[DPK_TRK] txagc_bb_tp / txagc_bb = 0x%x / 0x%x\n",
2879da4cea16SPing-Ke Shih 			    txagc_bb_tp, txagc_bb);
2880da4cea16SPing-Ke Shih 
2881da4cea16SPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_DPK_WR, B_DPK_WR_ST) == 0x0 &&
2882da4cea16SPing-Ke Shih 		    txagc_rf != 0 && rtwdev->hal.cv == CHIP_CAV) {
2883da4cea16SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2884da4cea16SPing-Ke Shih 				    "[DPK_TRK] New pwsf = 0x%x\n", 0x78 - delta_ther);
2885da4cea16SPing-Ke Shih 
2886da4cea16SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_DPD_BND + (path << 8) + (kidx << 2),
2887da4cea16SPing-Ke Shih 					       0x07FC0000, 0x78 - delta_ther);
2888da4cea16SPing-Ke Shih 		}
2889da4cea16SPing-Ke Shih 	}
2890da4cea16SPing-Ke Shih }
2891da4cea16SPing-Ke Shih 
_tssi_set_sys(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2892e5efc4d5SPing-Ke Shih static void _tssi_set_sys(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2893e5efc4d5SPing-Ke Shih 			  enum rtw89_rf_path path)
2894e5efc4d5SPing-Ke Shih {
2895cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2896cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
2897e5efc4d5SPing-Ke Shih 
2898e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_sys_defs_tbl);
2899e5efc4d5SPing-Ke Shih 
2900e5efc4d5SPing-Ke Shih 	if (path == RF_PATH_A)
2901e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2902e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_sys_defs_2g_a_tbl,
2903e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_sys_defs_5g_a_tbl);
2904e5efc4d5SPing-Ke Shih 	else
2905e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2906e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_sys_defs_2g_b_tbl,
2907e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_sys_defs_5g_b_tbl);
2908e5efc4d5SPing-Ke Shih }
2909e5efc4d5SPing-Ke Shih 
_tssi_ini_txpwr_ctrl_bb(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2910e5efc4d5SPing-Ke Shih static void _tssi_ini_txpwr_ctrl_bb(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2911e5efc4d5SPing-Ke Shih 				    enum rtw89_rf_path path)
2912e5efc4d5SPing-Ke Shih {
2913e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2914e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txpwr_ctrl_bb_defs_a_tbl,
2915e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txpwr_ctrl_bb_defs_b_tbl);
2916e5efc4d5SPing-Ke Shih }
2917e5efc4d5SPing-Ke Shih 
_tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2918e5efc4d5SPing-Ke Shih static void _tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev *rtwdev,
2919e5efc4d5SPing-Ke Shih 					  enum rtw89_phy_idx phy,
2920e5efc4d5SPing-Ke Shih 					  enum rtw89_rf_path path)
2921e5efc4d5SPing-Ke Shih {
2922e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2923e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txpwr_ctrl_bb_he_tb_defs_a_tbl,
2924e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txpwr_ctrl_bb_he_tb_defs_b_tbl);
2925e5efc4d5SPing-Ke Shih }
2926e5efc4d5SPing-Ke Shih 
_tssi_set_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2927e5efc4d5SPing-Ke Shih static void _tssi_set_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2928e5efc4d5SPing-Ke Shih 			  enum rtw89_rf_path path)
2929e5efc4d5SPing-Ke Shih {
2930cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2931cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
2932e5efc4d5SPing-Ke Shih 
2933e5efc4d5SPing-Ke Shih 	if (path == RF_PATH_A) {
2934e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_dck_defs_a_tbl);
2935e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2936e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_dck_defs_2g_a_tbl,
2937e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_dck_defs_5g_a_tbl);
2938e5efc4d5SPing-Ke Shih 	} else {
2939e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_dck_defs_b_tbl);
2940e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2941e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_dck_defs_2g_b_tbl,
2942e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_dck_defs_5g_b_tbl);
2943e5efc4d5SPing-Ke Shih 	}
2944e5efc4d5SPing-Ke Shih }
2945e5efc4d5SPing-Ke Shih 
_tssi_set_bbgain_split(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2946e5efc4d5SPing-Ke Shih static void _tssi_set_bbgain_split(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2947e5efc4d5SPing-Ke Shih 				   enum rtw89_rf_path path)
2948e5efc4d5SPing-Ke Shih {
2949e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2950e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_set_bbgain_split_a_tbl,
2951e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_set_bbgain_split_b_tbl);
2952e5efc4d5SPing-Ke Shih }
2953e5efc4d5SPing-Ke Shih 
_tssi_set_tmeter_tbl(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)2954e5efc4d5SPing-Ke Shih static void _tssi_set_tmeter_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2955e5efc4d5SPing-Ke Shih 				 enum rtw89_rf_path path)
2956e5efc4d5SPing-Ke Shih {
2957e5efc4d5SPing-Ke Shih #define RTW8852C_TSSI_GET_VAL(ptr, idx)			\
2958e5efc4d5SPing-Ke Shih ({							\
2959e5efc4d5SPing-Ke Shih 	s8 *__ptr = (ptr);				\
2960e5efc4d5SPing-Ke Shih 	u8 __idx = (idx), __i, __v;			\
2961e5efc4d5SPing-Ke Shih 	u32 __val = 0;					\
2962e5efc4d5SPing-Ke Shih 	for (__i = 0; __i < 4; __i++) {			\
2963e5efc4d5SPing-Ke Shih 		__v = (__ptr[__idx + __i]);		\
2964e5efc4d5SPing-Ke Shih 		__val |= (__v << (8 * __i));		\
2965e5efc4d5SPing-Ke Shih 	}						\
2966e5efc4d5SPing-Ke Shih 	__val;						\
2967e5efc4d5SPing-Ke Shih })
2968e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
2969cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2970cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
2971cbb145b9SZong-Zhe Yang 	u8 subband = chan->subband_type;
2972e5efc4d5SPing-Ke Shih 	const s8 *thm_up_a = NULL;
2973e5efc4d5SPing-Ke Shih 	const s8 *thm_down_a = NULL;
2974e5efc4d5SPing-Ke Shih 	const s8 *thm_up_b = NULL;
2975e5efc4d5SPing-Ke Shih 	const s8 *thm_down_b = NULL;
2976e5efc4d5SPing-Ke Shih 	u8 thermal = 0xff;
2977e5efc4d5SPing-Ke Shih 	s8 thm_ofst[64] = {0};
2978e5efc4d5SPing-Ke Shih 	u32 tmp = 0;
2979e5efc4d5SPing-Ke Shih 	u8 i, j;
2980e5efc4d5SPing-Ke Shih 
2981e5efc4d5SPing-Ke Shih 	switch (subband) {
2982e5efc4d5SPing-Ke Shih 	default:
2983e5efc4d5SPing-Ke Shih 	case RTW89_CH_2G:
2984e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_2ga_p;
2985e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_2ga_n;
2986e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_2gb_p;
2987e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_2gb_n;
2988e5efc4d5SPing-Ke Shih 		break;
2989e5efc4d5SPing-Ke Shih 	case RTW89_CH_5G_BAND_1:
2990e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[0];
2991e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[0];
2992e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[0];
2993e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[0];
2994e5efc4d5SPing-Ke Shih 		break;
2995e5efc4d5SPing-Ke Shih 	case RTW89_CH_5G_BAND_3:
2996e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[1];
2997e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[1];
2998e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[1];
2999e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[1];
3000e5efc4d5SPing-Ke Shih 		break;
3001e5efc4d5SPing-Ke Shih 	case RTW89_CH_5G_BAND_4:
3002e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[2];
3003e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[2];
3004e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[2];
3005e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[2];
3006e5efc4d5SPing-Ke Shih 		break;
3007e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX0:
3008e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX1:
3009e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[0];
3010e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[0];
3011e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[0];
3012e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[0];
3013e5efc4d5SPing-Ke Shih 		break;
3014e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX2:
3015e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX3:
3016e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[1];
3017e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[1];
3018e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[1];
3019e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[1];
3020e5efc4d5SPing-Ke Shih 		break;
3021e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX4:
3022e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX5:
3023e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[2];
3024e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[2];
3025e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[2];
3026e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[2];
3027e5efc4d5SPing-Ke Shih 		break;
3028e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX6:
3029e5efc4d5SPing-Ke Shih 	case RTW89_CH_6G_BAND_IDX7:
3030e5efc4d5SPing-Ke Shih 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[3];
3031e5efc4d5SPing-Ke Shih 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[3];
3032e5efc4d5SPing-Ke Shih 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[3];
3033e5efc4d5SPing-Ke Shih 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[3];
3034e5efc4d5SPing-Ke Shih 		break;
3035e5efc4d5SPing-Ke Shih 	}
3036e5efc4d5SPing-Ke Shih 
3037e5efc4d5SPing-Ke Shih 	if (path == RF_PATH_A) {
3038e5efc4d5SPing-Ke Shih 		thermal = tssi_info->thermal[RF_PATH_A];
3039e5efc4d5SPing-Ke Shih 
3040e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3041e5efc4d5SPing-Ke Shih 			    "[TSSI] ch=%d thermal_pathA=0x%x\n", ch, thermal);
3042e5efc4d5SPing-Ke Shih 
3043e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_DIS, 0x0);
3044e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_TRK, 0x1);
3045e5efc4d5SPing-Ke Shih 
3046e5efc4d5SPing-Ke Shih 		if (thermal == 0xff) {
3047e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, 32);
3048e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL, 32);
3049e5efc4d5SPing-Ke Shih 
3050e5efc4d5SPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
3051e5efc4d5SPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, 0x0);
3052e5efc4d5SPing-Ke Shih 
3053e5efc4d5SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3054e5efc4d5SPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
3055e5efc4d5SPing-Ke Shih 					    0x5c00 + i, 0x0);
3056e5efc4d5SPing-Ke Shih 			}
3057e5efc4d5SPing-Ke Shih 
3058e5efc4d5SPing-Ke Shih 		} else {
3059e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, thermal);
3060e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL,
3061e5efc4d5SPing-Ke Shih 					       thermal);
3062e5efc4d5SPing-Ke Shih 
3063e5efc4d5SPing-Ke Shih 			i = 0;
3064e5efc4d5SPing-Ke Shih 			for (j = 0; j < 32; j++)
3065e5efc4d5SPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3066e5efc4d5SPing-Ke Shih 					      -thm_down_a[i++] :
3067e5efc4d5SPing-Ke Shih 					      -thm_down_a[DELTA_SWINGIDX_SIZE - 1];
3068e5efc4d5SPing-Ke Shih 
3069e5efc4d5SPing-Ke Shih 			i = 1;
3070e5efc4d5SPing-Ke Shih 			for (j = 63; j >= 32; j--)
3071e5efc4d5SPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3072e5efc4d5SPing-Ke Shih 					      thm_up_a[i++] :
3073e5efc4d5SPing-Ke Shih 					      thm_up_a[DELTA_SWINGIDX_SIZE - 1];
3074e5efc4d5SPing-Ke Shih 
3075e5efc4d5SPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
3076e5efc4d5SPing-Ke Shih 				tmp = RTW8852C_TSSI_GET_VAL(thm_ofst, i);
3077e5efc4d5SPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, tmp);
3078e5efc4d5SPing-Ke Shih 
3079e5efc4d5SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3080e5efc4d5SPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
3081e5efc4d5SPing-Ke Shih 					    0x5c00 + i, tmp);
3082e5efc4d5SPing-Ke Shih 			}
3083e5efc4d5SPing-Ke Shih 		}
3084e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x1);
3085e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x0);
3086e5efc4d5SPing-Ke Shih 
3087e5efc4d5SPing-Ke Shih 	} else {
3088e5efc4d5SPing-Ke Shih 		thermal = tssi_info->thermal[RF_PATH_B];
3089e5efc4d5SPing-Ke Shih 
3090e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3091e5efc4d5SPing-Ke Shih 			    "[TSSI] ch=%d thermal_pathB=0x%x\n", ch, thermal);
3092e5efc4d5SPing-Ke Shih 
3093e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER_DIS, 0x0);
3094e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER_TRK, 0x1);
3095e5efc4d5SPing-Ke Shih 
3096e5efc4d5SPing-Ke Shih 		if (thermal == 0xff) {
3097e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER, 32);
3098e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, B_P1_RFCTM_VAL, 32);
3099e5efc4d5SPing-Ke Shih 
3100e5efc4d5SPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
3101e5efc4d5SPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_TSSI_THOF + i, 0x0);
3102e5efc4d5SPing-Ke Shih 
3103e5efc4d5SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3104e5efc4d5SPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
3105e5efc4d5SPing-Ke Shih 					    0x7c00 + i, 0x0);
3106e5efc4d5SPing-Ke Shih 			}
3107e5efc4d5SPing-Ke Shih 
3108e5efc4d5SPing-Ke Shih 		} else {
3109e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER, thermal);
3110e5efc4d5SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, B_P1_RFCTM_VAL,
3111e5efc4d5SPing-Ke Shih 					       thermal);
3112e5efc4d5SPing-Ke Shih 
3113e5efc4d5SPing-Ke Shih 			i = 0;
3114e5efc4d5SPing-Ke Shih 			for (j = 0; j < 32; j++)
3115e5efc4d5SPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3116e5efc4d5SPing-Ke Shih 					      -thm_down_b[i++] :
3117e5efc4d5SPing-Ke Shih 					      -thm_down_b[DELTA_SWINGIDX_SIZE - 1];
3118e5efc4d5SPing-Ke Shih 
3119e5efc4d5SPing-Ke Shih 			i = 1;
3120e5efc4d5SPing-Ke Shih 			for (j = 63; j >= 32; j--)
3121e5efc4d5SPing-Ke Shih 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3122e5efc4d5SPing-Ke Shih 					      thm_up_b[i++] :
3123e5efc4d5SPing-Ke Shih 					      thm_up_b[DELTA_SWINGIDX_SIZE - 1];
3124e5efc4d5SPing-Ke Shih 
3125e5efc4d5SPing-Ke Shih 			for (i = 0; i < 64; i += 4) {
3126e5efc4d5SPing-Ke Shih 				tmp = RTW8852C_TSSI_GET_VAL(thm_ofst, i);
3127e5efc4d5SPing-Ke Shih 				rtw89_phy_write32(rtwdev, R_TSSI_THOF + i, tmp);
3128e5efc4d5SPing-Ke Shih 
3129e5efc4d5SPing-Ke Shih 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3130e5efc4d5SPing-Ke Shih 					    "[TSSI] write 0x%x val=0x%08x\n",
3131e5efc4d5SPing-Ke Shih 					    0x7c00 + i, tmp);
3132e5efc4d5SPing-Ke Shih 			}
3133e5efc4d5SPing-Ke Shih 		}
3134e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, R_P1_RFCTM_RDY, 0x1);
3135e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, R_P1_RFCTM_RDY, 0x0);
3136e5efc4d5SPing-Ke Shih 	}
3137e5efc4d5SPing-Ke Shih #undef RTW8852C_TSSI_GET_VAL
3138e5efc4d5SPing-Ke Shih }
3139e5efc4d5SPing-Ke Shih 
_tssi_slope_cal_org(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3140e5efc4d5SPing-Ke Shih static void _tssi_slope_cal_org(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3141e5efc4d5SPing-Ke Shih 				enum rtw89_rf_path path)
3142e5efc4d5SPing-Ke Shih {
3143cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3144cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
3145e5efc4d5SPing-Ke Shih 
3146e5efc4d5SPing-Ke Shih 	if (path == RF_PATH_A) {
3147e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
3148e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_slope_cal_org_defs_2g_a_tbl,
3149e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_slope_cal_org_defs_5g_a_tbl);
3150e5efc4d5SPing-Ke Shih 	} else {
3151e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
3152e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_slope_cal_org_defs_2g_b_tbl,
3153e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_slope_cal_org_defs_5g_b_tbl);
3154e5efc4d5SPing-Ke Shih 	}
3155e5efc4d5SPing-Ke Shih }
3156e5efc4d5SPing-Ke Shih 
_tssi_set_aligk_default(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3157e5efc4d5SPing-Ke Shih static void _tssi_set_aligk_default(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3158e5efc4d5SPing-Ke Shih 				    enum rtw89_rf_path path)
3159e5efc4d5SPing-Ke Shih {
3160cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3161cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
3162e5efc4d5SPing-Ke Shih 	const struct rtw89_rfk_tbl *tbl;
3163e5efc4d5SPing-Ke Shih 
3164e5efc4d5SPing-Ke Shih 	if (path == RF_PATH_A) {
3165e5efc4d5SPing-Ke Shih 		if (band == RTW89_BAND_2G)
3166e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_2g_a_tbl;
3167e5efc4d5SPing-Ke Shih 		else if (band == RTW89_BAND_6G)
3168e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_6g_a_tbl;
3169e5efc4d5SPing-Ke Shih 		else
3170e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_5g_a_tbl;
3171e5efc4d5SPing-Ke Shih 	} else {
3172e5efc4d5SPing-Ke Shih 		if (band == RTW89_BAND_2G)
3173e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_2g_b_tbl;
3174e5efc4d5SPing-Ke Shih 		else if (band == RTW89_BAND_6G)
3175e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_6g_b_tbl;
3176e5efc4d5SPing-Ke Shih 		else
3177e5efc4d5SPing-Ke Shih 			tbl = &rtw8852c_tssi_set_aligk_default_defs_5g_b_tbl;
3178e5efc4d5SPing-Ke Shih 	}
3179e5efc4d5SPing-Ke Shih 
3180e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser(rtwdev, tbl);
3181e5efc4d5SPing-Ke Shih }
3182e5efc4d5SPing-Ke Shih 
_tssi_set_slope(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3183e5efc4d5SPing-Ke Shih static void _tssi_set_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3184e5efc4d5SPing-Ke Shih 			    enum rtw89_rf_path path)
3185e5efc4d5SPing-Ke Shih {
3186e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3187e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_slope_defs_a_tbl,
3188e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_slope_defs_b_tbl);
3189e5efc4d5SPing-Ke Shih }
3190e5efc4d5SPing-Ke Shih 
_tssi_run_slope(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3191e5efc4d5SPing-Ke Shih static void _tssi_run_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3192e5efc4d5SPing-Ke Shih 			    enum rtw89_rf_path path)
3193e5efc4d5SPing-Ke Shih {
3194e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3195e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_run_slope_defs_a_tbl,
3196e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_run_slope_defs_b_tbl);
3197e5efc4d5SPing-Ke Shih }
3198e5efc4d5SPing-Ke Shih 
_tssi_set_track(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3199e5efc4d5SPing-Ke Shih static void _tssi_set_track(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3200e5efc4d5SPing-Ke Shih 			    enum rtw89_rf_path path)
3201e5efc4d5SPing-Ke Shih {
3202e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3203e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_track_defs_a_tbl,
3204e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_track_defs_b_tbl);
3205e5efc4d5SPing-Ke Shih }
3206e5efc4d5SPing-Ke Shih 
_tssi_set_txagc_offset_mv_avg(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3207e5efc4d5SPing-Ke Shih static void _tssi_set_txagc_offset_mv_avg(struct rtw89_dev *rtwdev,
3208e5efc4d5SPing-Ke Shih 					  enum rtw89_phy_idx phy,
3209e5efc4d5SPing-Ke Shih 					  enum rtw89_rf_path path)
3210e5efc4d5SPing-Ke Shih {
3211e5efc4d5SPing-Ke Shih 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3212e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txagc_ofst_mv_avg_defs_a_tbl,
3213e5efc4d5SPing-Ke Shih 				 &rtw8852c_tssi_txagc_ofst_mv_avg_defs_b_tbl);
3214e5efc4d5SPing-Ke Shih }
3215e5efc4d5SPing-Ke Shih 
_tssi_enable(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)3216e5efc4d5SPing-Ke Shih static void _tssi_enable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
3217e5efc4d5SPing-Ke Shih {
3218e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3219e5efc4d5SPing-Ke Shih 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3220e5efc4d5SPing-Ke Shih 
3221e5efc4d5SPing-Ke Shih 	if (rtwdev->dbcc_en) {
3222e5efc4d5SPing-Ke Shih 		if (phy == RTW89_PHY_0) {
3223e5efc4d5SPing-Ke Shih 			path = RF_PATH_A;
3224e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_B;
3225e5efc4d5SPing-Ke Shih 		} else if (phy == RTW89_PHY_1) {
3226e5efc4d5SPing-Ke Shih 			path = RF_PATH_B;
3227e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_NUM_8852C;
3228e5efc4d5SPing-Ke Shih 		}
3229e5efc4d5SPing-Ke Shih 	}
3230e5efc4d5SPing-Ke Shih 
3231e5efc4d5SPing-Ke Shih 	for (i = path; i < path_max; i++) {
3232e5efc4d5SPing-Ke Shih 		_tssi_set_track(rtwdev, phy, i);
3233e5efc4d5SPing-Ke Shih 		_tssi_set_txagc_offset_mv_avg(rtwdev, phy, i);
3234e5efc4d5SPing-Ke Shih 
3235e5efc4d5SPing-Ke Shih 		rtw89_rfk_parser_by_cond(rtwdev, i == RF_PATH_A,
3236e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_enable_defs_a_tbl,
3237e5efc4d5SPing-Ke Shih 					 &rtw8852c_tssi_enable_defs_b_tbl);
3238e5efc4d5SPing-Ke Shih 
3239e5efc4d5SPing-Ke Shih 		tssi_info->base_thermal[i] =
3240e5efc4d5SPing-Ke Shih 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[i]);
3241e5efc4d5SPing-Ke Shih 		rtwdev->is_tssi_mode[i] = true;
3242e5efc4d5SPing-Ke Shih 	}
3243e5efc4d5SPing-Ke Shih }
3244e5efc4d5SPing-Ke Shih 
_tssi_disable(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)3245e5efc4d5SPing-Ke Shih static void _tssi_disable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
3246e5efc4d5SPing-Ke Shih {
3247e5efc4d5SPing-Ke Shih 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3248e5efc4d5SPing-Ke Shih 
3249e5efc4d5SPing-Ke Shih 	if (rtwdev->dbcc_en) {
3250e5efc4d5SPing-Ke Shih 		if (phy == RTW89_PHY_0) {
3251e5efc4d5SPing-Ke Shih 			path = RF_PATH_A;
3252e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_B;
3253e5efc4d5SPing-Ke Shih 		} else if (phy == RTW89_PHY_1) {
3254e5efc4d5SPing-Ke Shih 			path = RF_PATH_B;
3255e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_NUM_8852C;
3256e5efc4d5SPing-Ke Shih 		}
3257e5efc4d5SPing-Ke Shih 	}
3258e5efc4d5SPing-Ke Shih 
3259e5efc4d5SPing-Ke Shih 	for (i = path; i < path_max; i++) {
3260e5efc4d5SPing-Ke Shih 		if (i == RF_PATH_A) {
3261e5efc4d5SPing-Ke Shih 			rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_disable_defs_a_tbl);
3262e5efc4d5SPing-Ke Shih 			rtwdev->is_tssi_mode[RF_PATH_A] = false;
3263e5efc4d5SPing-Ke Shih 		}  else if (i == RF_PATH_B) {
3264e5efc4d5SPing-Ke Shih 			rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_disable_defs_b_tbl);
3265e5efc4d5SPing-Ke Shih 			rtwdev->is_tssi_mode[RF_PATH_B] = false;
3266e5efc4d5SPing-Ke Shih 		}
3267e5efc4d5SPing-Ke Shih 	}
3268e5efc4d5SPing-Ke Shih }
3269e5efc4d5SPing-Ke Shih 
_tssi_get_cck_group(struct rtw89_dev * rtwdev,u8 ch)3270e5efc4d5SPing-Ke Shih static u32 _tssi_get_cck_group(struct rtw89_dev *rtwdev, u8 ch)
3271e5efc4d5SPing-Ke Shih {
3272e5efc4d5SPing-Ke Shih 	switch (ch) {
3273e5efc4d5SPing-Ke Shih 	case 1 ... 2:
3274e5efc4d5SPing-Ke Shih 		return 0;
3275e5efc4d5SPing-Ke Shih 	case 3 ... 5:
3276e5efc4d5SPing-Ke Shih 		return 1;
3277e5efc4d5SPing-Ke Shih 	case 6 ... 8:
3278e5efc4d5SPing-Ke Shih 		return 2;
3279e5efc4d5SPing-Ke Shih 	case 9 ... 11:
3280e5efc4d5SPing-Ke Shih 		return 3;
3281e5efc4d5SPing-Ke Shih 	case 12 ... 13:
3282e5efc4d5SPing-Ke Shih 		return 4;
3283e5efc4d5SPing-Ke Shih 	case 14:
3284e5efc4d5SPing-Ke Shih 		return 5;
3285e5efc4d5SPing-Ke Shih 	}
3286e5efc4d5SPing-Ke Shih 
3287e5efc4d5SPing-Ke Shih 	return 0;
3288e5efc4d5SPing-Ke Shih }
3289e5efc4d5SPing-Ke Shih 
3290e5efc4d5SPing-Ke Shih #define TSSI_EXTRA_GROUP_BIT (BIT(31))
3291e5efc4d5SPing-Ke Shih #define TSSI_EXTRA_GROUP(idx) (TSSI_EXTRA_GROUP_BIT | (idx))
3292e5efc4d5SPing-Ke Shih #define IS_TSSI_EXTRA_GROUP(group) ((group) & TSSI_EXTRA_GROUP_BIT)
3293e5efc4d5SPing-Ke Shih #define TSSI_EXTRA_GET_GROUP_IDX1(group) ((group) & ~TSSI_EXTRA_GROUP_BIT)
3294e5efc4d5SPing-Ke Shih #define TSSI_EXTRA_GET_GROUP_IDX2(group) (TSSI_EXTRA_GET_GROUP_IDX1(group) + 1)
3295e5efc4d5SPing-Ke Shih 
_tssi_get_ofdm_group(struct rtw89_dev * rtwdev,u8 ch)3296e5efc4d5SPing-Ke Shih static u32 _tssi_get_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
3297e5efc4d5SPing-Ke Shih {
3298e5efc4d5SPing-Ke Shih 	switch (ch) {
3299e5efc4d5SPing-Ke Shih 	case 1 ... 2:
3300e5efc4d5SPing-Ke Shih 		return 0;
3301e5efc4d5SPing-Ke Shih 	case 3 ... 5:
3302e5efc4d5SPing-Ke Shih 		return 1;
3303e5efc4d5SPing-Ke Shih 	case 6 ... 8:
3304e5efc4d5SPing-Ke Shih 		return 2;
3305e5efc4d5SPing-Ke Shih 	case 9 ... 11:
3306e5efc4d5SPing-Ke Shih 		return 3;
3307e5efc4d5SPing-Ke Shih 	case 12 ... 14:
3308e5efc4d5SPing-Ke Shih 		return 4;
3309e5efc4d5SPing-Ke Shih 	case 36 ... 40:
3310e5efc4d5SPing-Ke Shih 		return 5;
3311e5efc4d5SPing-Ke Shih 	case 41 ... 43:
3312e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(5);
3313e5efc4d5SPing-Ke Shih 	case 44 ... 48:
3314e5efc4d5SPing-Ke Shih 		return 6;
3315e5efc4d5SPing-Ke Shih 	case 49 ... 51:
3316e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(6);
3317e5efc4d5SPing-Ke Shih 	case 52 ... 56:
3318e5efc4d5SPing-Ke Shih 		return 7;
3319e5efc4d5SPing-Ke Shih 	case 57 ... 59:
3320e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(7);
3321e5efc4d5SPing-Ke Shih 	case 60 ... 64:
3322e5efc4d5SPing-Ke Shih 		return 8;
3323e5efc4d5SPing-Ke Shih 	case 100 ... 104:
3324e5efc4d5SPing-Ke Shih 		return 9;
3325e5efc4d5SPing-Ke Shih 	case 105 ... 107:
3326e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(9);
3327e5efc4d5SPing-Ke Shih 	case 108 ... 112:
3328e5efc4d5SPing-Ke Shih 		return 10;
3329e5efc4d5SPing-Ke Shih 	case 113 ... 115:
3330e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(10);
3331e5efc4d5SPing-Ke Shih 	case 116 ... 120:
3332e5efc4d5SPing-Ke Shih 		return 11;
3333e5efc4d5SPing-Ke Shih 	case 121 ... 123:
3334e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(11);
3335e5efc4d5SPing-Ke Shih 	case 124 ... 128:
3336e5efc4d5SPing-Ke Shih 		return 12;
3337e5efc4d5SPing-Ke Shih 	case 129 ... 131:
3338e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(12);
3339e5efc4d5SPing-Ke Shih 	case 132 ... 136:
3340e5efc4d5SPing-Ke Shih 		return 13;
3341e5efc4d5SPing-Ke Shih 	case 137 ... 139:
3342e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(13);
3343e5efc4d5SPing-Ke Shih 	case 140 ... 144:
3344e5efc4d5SPing-Ke Shih 		return 14;
3345e5efc4d5SPing-Ke Shih 	case 149 ... 153:
3346e5efc4d5SPing-Ke Shih 		return 15;
3347e5efc4d5SPing-Ke Shih 	case 154 ... 156:
3348e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(15);
3349e5efc4d5SPing-Ke Shih 	case 157 ... 161:
3350e5efc4d5SPing-Ke Shih 		return 16;
3351e5efc4d5SPing-Ke Shih 	case 162 ... 164:
3352e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(16);
3353e5efc4d5SPing-Ke Shih 	case 165 ... 169:
3354e5efc4d5SPing-Ke Shih 		return 17;
3355e5efc4d5SPing-Ke Shih 	case 170 ... 172:
3356e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(17);
3357e5efc4d5SPing-Ke Shih 	case 173 ... 177:
3358e5efc4d5SPing-Ke Shih 		return 18;
3359e5efc4d5SPing-Ke Shih 	}
3360e5efc4d5SPing-Ke Shih 
3361e5efc4d5SPing-Ke Shih 	return 0;
3362e5efc4d5SPing-Ke Shih }
3363e5efc4d5SPing-Ke Shih 
_tssi_get_6g_ofdm_group(struct rtw89_dev * rtwdev,u8 ch)3364e5efc4d5SPing-Ke Shih static u32 _tssi_get_6g_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
3365e5efc4d5SPing-Ke Shih {
3366e5efc4d5SPing-Ke Shih 	switch (ch) {
3367e5efc4d5SPing-Ke Shih 	case 1 ... 5:
3368e5efc4d5SPing-Ke Shih 		return 0;
3369e5efc4d5SPing-Ke Shih 	case 6 ... 8:
3370e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(0);
3371e5efc4d5SPing-Ke Shih 	case 9 ... 13:
3372e5efc4d5SPing-Ke Shih 		return 1;
3373e5efc4d5SPing-Ke Shih 	case 14 ... 16:
3374e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(1);
3375e5efc4d5SPing-Ke Shih 	case 17 ... 21:
3376e5efc4d5SPing-Ke Shih 		return 2;
3377e5efc4d5SPing-Ke Shih 	case 22 ... 24:
3378e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(2);
3379e5efc4d5SPing-Ke Shih 	case 25 ... 29:
3380e5efc4d5SPing-Ke Shih 		return 3;
3381e5efc4d5SPing-Ke Shih 	case 33 ... 37:
3382e5efc4d5SPing-Ke Shih 		return 4;
3383e5efc4d5SPing-Ke Shih 	case 38 ... 40:
3384e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(4);
3385e5efc4d5SPing-Ke Shih 	case 41 ... 45:
3386e5efc4d5SPing-Ke Shih 		return 5;
3387e5efc4d5SPing-Ke Shih 	case 46 ... 48:
3388e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(5);
3389e5efc4d5SPing-Ke Shih 	case 49 ... 53:
3390e5efc4d5SPing-Ke Shih 		return 6;
3391e5efc4d5SPing-Ke Shih 	case 54 ... 56:
3392e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(6);
3393e5efc4d5SPing-Ke Shih 	case 57 ... 61:
3394e5efc4d5SPing-Ke Shih 		return 7;
3395e5efc4d5SPing-Ke Shih 	case 65 ... 69:
3396e5efc4d5SPing-Ke Shih 		return 8;
3397e5efc4d5SPing-Ke Shih 	case 70 ... 72:
3398e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(8);
3399e5efc4d5SPing-Ke Shih 	case 73 ... 77:
3400e5efc4d5SPing-Ke Shih 		return 9;
3401e5efc4d5SPing-Ke Shih 	case 78 ... 80:
3402e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(9);
3403e5efc4d5SPing-Ke Shih 	case 81 ... 85:
3404e5efc4d5SPing-Ke Shih 		return 10;
3405e5efc4d5SPing-Ke Shih 	case 86 ... 88:
3406e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(10);
3407e5efc4d5SPing-Ke Shih 	case 89 ... 93:
3408e5efc4d5SPing-Ke Shih 		return 11;
3409e5efc4d5SPing-Ke Shih 	case 97 ... 101:
3410e5efc4d5SPing-Ke Shih 		return 12;
3411e5efc4d5SPing-Ke Shih 	case 102 ... 104:
3412e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(12);
3413e5efc4d5SPing-Ke Shih 	case 105 ... 109:
3414e5efc4d5SPing-Ke Shih 		return 13;
3415e5efc4d5SPing-Ke Shih 	case 110 ... 112:
3416e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(13);
3417e5efc4d5SPing-Ke Shih 	case 113 ... 117:
3418e5efc4d5SPing-Ke Shih 		return 14;
3419e5efc4d5SPing-Ke Shih 	case 118 ... 120:
3420e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(14);
3421e5efc4d5SPing-Ke Shih 	case 121 ... 125:
3422e5efc4d5SPing-Ke Shih 		return 15;
3423e5efc4d5SPing-Ke Shih 	case 129 ... 133:
3424e5efc4d5SPing-Ke Shih 		return 16;
3425e5efc4d5SPing-Ke Shih 	case 134 ... 136:
3426e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(16);
3427e5efc4d5SPing-Ke Shih 	case 137 ... 141:
3428e5efc4d5SPing-Ke Shih 		return 17;
3429e5efc4d5SPing-Ke Shih 	case 142 ... 144:
3430e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(17);
3431e5efc4d5SPing-Ke Shih 	case 145 ... 149:
3432e5efc4d5SPing-Ke Shih 		return 18;
3433e5efc4d5SPing-Ke Shih 	case 150 ... 152:
3434e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(18);
3435e5efc4d5SPing-Ke Shih 	case 153 ... 157:
3436e5efc4d5SPing-Ke Shih 		return 19;
3437e5efc4d5SPing-Ke Shih 	case 161 ... 165:
3438e5efc4d5SPing-Ke Shih 		return 20;
3439e5efc4d5SPing-Ke Shih 	case 166 ... 168:
3440e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(20);
3441e5efc4d5SPing-Ke Shih 	case 169 ... 173:
3442e5efc4d5SPing-Ke Shih 		return 21;
3443e5efc4d5SPing-Ke Shih 	case 174 ... 176:
3444e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(21);
3445e5efc4d5SPing-Ke Shih 	case 177 ... 181:
3446e5efc4d5SPing-Ke Shih 		return 22;
3447e5efc4d5SPing-Ke Shih 	case 182 ... 184:
3448e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(22);
3449e5efc4d5SPing-Ke Shih 	case 185 ... 189:
3450e5efc4d5SPing-Ke Shih 		return 23;
3451e5efc4d5SPing-Ke Shih 	case 193 ... 197:
3452e5efc4d5SPing-Ke Shih 		return 24;
3453e5efc4d5SPing-Ke Shih 	case 198 ... 200:
3454e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(24);
3455e5efc4d5SPing-Ke Shih 	case 201 ... 205:
3456e5efc4d5SPing-Ke Shih 		return 25;
3457e5efc4d5SPing-Ke Shih 	case 206 ... 208:
3458e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(25);
3459e5efc4d5SPing-Ke Shih 	case 209 ... 213:
3460e5efc4d5SPing-Ke Shih 		return 26;
3461e5efc4d5SPing-Ke Shih 	case 214 ... 216:
3462e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(26);
3463e5efc4d5SPing-Ke Shih 	case 217 ... 221:
3464e5efc4d5SPing-Ke Shih 		return 27;
3465e5efc4d5SPing-Ke Shih 	case 225 ... 229:
3466e5efc4d5SPing-Ke Shih 		return 28;
3467e5efc4d5SPing-Ke Shih 	case 230 ... 232:
3468e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(28);
3469e5efc4d5SPing-Ke Shih 	case 233 ... 237:
3470e5efc4d5SPing-Ke Shih 		return 29;
3471e5efc4d5SPing-Ke Shih 	case 238 ... 240:
3472e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(29);
3473e5efc4d5SPing-Ke Shih 	case 241 ... 245:
3474e5efc4d5SPing-Ke Shih 		return 30;
3475e5efc4d5SPing-Ke Shih 	case 246 ... 248:
3476e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(30);
3477e5efc4d5SPing-Ke Shih 	case 249 ... 253:
3478e5efc4d5SPing-Ke Shih 		return 31;
3479e5efc4d5SPing-Ke Shih 	}
3480e5efc4d5SPing-Ke Shih 
3481e5efc4d5SPing-Ke Shih 	return 0;
3482e5efc4d5SPing-Ke Shih }
3483e5efc4d5SPing-Ke Shih 
_tssi_get_trim_group(struct rtw89_dev * rtwdev,u8 ch)3484e5efc4d5SPing-Ke Shih static u32 _tssi_get_trim_group(struct rtw89_dev *rtwdev, u8 ch)
3485e5efc4d5SPing-Ke Shih {
3486e5efc4d5SPing-Ke Shih 	switch (ch) {
3487e5efc4d5SPing-Ke Shih 	case 1 ... 8:
3488e5efc4d5SPing-Ke Shih 		return 0;
3489e5efc4d5SPing-Ke Shih 	case 9 ... 14:
3490e5efc4d5SPing-Ke Shih 		return 1;
3491e5efc4d5SPing-Ke Shih 	case 36 ... 48:
3492e5efc4d5SPing-Ke Shih 		return 2;
3493e5efc4d5SPing-Ke Shih 	case 49 ... 51:
3494e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(2);
3495e5efc4d5SPing-Ke Shih 	case 52 ... 64:
3496e5efc4d5SPing-Ke Shih 		return 3;
3497e5efc4d5SPing-Ke Shih 	case 100 ... 112:
3498e5efc4d5SPing-Ke Shih 		return 4;
3499e5efc4d5SPing-Ke Shih 	case 113 ... 115:
3500e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(4);
3501e5efc4d5SPing-Ke Shih 	case 116 ... 128:
3502e5efc4d5SPing-Ke Shih 		return 5;
3503e5efc4d5SPing-Ke Shih 	case 132 ... 144:
3504e5efc4d5SPing-Ke Shih 		return 6;
3505e5efc4d5SPing-Ke Shih 	case 149 ... 177:
3506e5efc4d5SPing-Ke Shih 		return 7;
3507e5efc4d5SPing-Ke Shih 	}
3508e5efc4d5SPing-Ke Shih 
3509e5efc4d5SPing-Ke Shih 	return 0;
3510e5efc4d5SPing-Ke Shih }
3511e5efc4d5SPing-Ke Shih 
_tssi_get_6g_trim_group(struct rtw89_dev * rtwdev,u8 ch)3512e5efc4d5SPing-Ke Shih static u32 _tssi_get_6g_trim_group(struct rtw89_dev *rtwdev, u8 ch)
3513e5efc4d5SPing-Ke Shih {
3514e5efc4d5SPing-Ke Shih 	switch (ch) {
3515e5efc4d5SPing-Ke Shih 	case 1 ... 13:
3516e5efc4d5SPing-Ke Shih 		return 0;
3517e5efc4d5SPing-Ke Shih 	case 14 ... 16:
3518e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(0);
3519e5efc4d5SPing-Ke Shih 	case 17 ... 29:
3520e5efc4d5SPing-Ke Shih 		return 1;
3521e5efc4d5SPing-Ke Shih 	case 33 ... 45:
3522e5efc4d5SPing-Ke Shih 		return 2;
3523e5efc4d5SPing-Ke Shih 	case 46 ... 48:
3524e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(2);
3525e5efc4d5SPing-Ke Shih 	case 49 ... 61:
3526e5efc4d5SPing-Ke Shih 		return 3;
3527e5efc4d5SPing-Ke Shih 	case 65 ... 77:
3528e5efc4d5SPing-Ke Shih 		return 4;
3529e5efc4d5SPing-Ke Shih 	case 78 ... 80:
3530e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(4);
3531e5efc4d5SPing-Ke Shih 	case 81 ... 93:
3532e5efc4d5SPing-Ke Shih 		return 5;
3533e5efc4d5SPing-Ke Shih 	case 97 ... 109:
3534e5efc4d5SPing-Ke Shih 		return 6;
3535e5efc4d5SPing-Ke Shih 	case 110 ... 112:
3536e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(6);
3537e5efc4d5SPing-Ke Shih 	case 113 ... 125:
3538e5efc4d5SPing-Ke Shih 		return 7;
3539e5efc4d5SPing-Ke Shih 	case 129 ... 141:
3540e5efc4d5SPing-Ke Shih 		return 8;
3541e5efc4d5SPing-Ke Shih 	case 142 ... 144:
3542e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(8);
3543e5efc4d5SPing-Ke Shih 	case 145 ... 157:
3544e5efc4d5SPing-Ke Shih 		return 9;
3545e5efc4d5SPing-Ke Shih 	case 161 ... 173:
3546e5efc4d5SPing-Ke Shih 		return 10;
3547e5efc4d5SPing-Ke Shih 	case 174 ... 176:
3548e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(10);
3549e5efc4d5SPing-Ke Shih 	case 177 ... 189:
3550e5efc4d5SPing-Ke Shih 		return 11;
3551e5efc4d5SPing-Ke Shih 	case 193 ... 205:
3552e5efc4d5SPing-Ke Shih 		return 12;
3553e5efc4d5SPing-Ke Shih 	case 206 ... 208:
3554e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(12);
3555e5efc4d5SPing-Ke Shih 	case 209 ... 221:
3556e5efc4d5SPing-Ke Shih 		return 13;
3557e5efc4d5SPing-Ke Shih 	case 225 ... 237:
3558e5efc4d5SPing-Ke Shih 		return 14;
3559e5efc4d5SPing-Ke Shih 	case 238 ... 240:
3560e5efc4d5SPing-Ke Shih 		return TSSI_EXTRA_GROUP(14);
3561e5efc4d5SPing-Ke Shih 	case 241 ... 253:
3562e5efc4d5SPing-Ke Shih 		return 15;
3563e5efc4d5SPing-Ke Shih 	}
3564e5efc4d5SPing-Ke Shih 
3565e5efc4d5SPing-Ke Shih 	return 0;
3566e5efc4d5SPing-Ke Shih }
3567e5efc4d5SPing-Ke Shih 
_tssi_get_ofdm_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3568e5efc4d5SPing-Ke Shih static s8 _tssi_get_ofdm_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3569e5efc4d5SPing-Ke Shih 			    enum rtw89_rf_path path)
3570e5efc4d5SPing-Ke Shih {
3571e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3572cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3573cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
3574cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
3575e5efc4d5SPing-Ke Shih 	u32 gidx, gidx_1st, gidx_2nd;
3576e5efc4d5SPing-Ke Shih 	s8 de_1st;
3577e5efc4d5SPing-Ke Shih 	s8 de_2nd;
3578e5efc4d5SPing-Ke Shih 	s8 val;
3579e5efc4d5SPing-Ke Shih 
3580e5efc4d5SPing-Ke Shih 	if (band == RTW89_BAND_2G || band == RTW89_BAND_5G) {
3581e5efc4d5SPing-Ke Shih 		gidx = _tssi_get_ofdm_group(rtwdev, ch);
3582e5efc4d5SPing-Ke Shih 
3583e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3584e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n",
3585e5efc4d5SPing-Ke Shih 			    path, gidx);
3586e5efc4d5SPing-Ke Shih 
3587e5efc4d5SPing-Ke Shih 		if (IS_TSSI_EXTRA_GROUP(gidx)) {
3588e5efc4d5SPing-Ke Shih 			gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
3589e5efc4d5SPing-Ke Shih 			gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
3590e5efc4d5SPing-Ke Shih 			de_1st = tssi_info->tssi_mcs[path][gidx_1st];
3591e5efc4d5SPing-Ke Shih 			de_2nd = tssi_info->tssi_mcs[path][gidx_2nd];
3592e5efc4d5SPing-Ke Shih 			val = (de_1st + de_2nd) / 2;
3593e5efc4d5SPing-Ke Shih 
3594e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3595e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
3596e5efc4d5SPing-Ke Shih 				    path, val, de_1st, de_2nd);
3597e5efc4d5SPing-Ke Shih 		} else {
3598e5efc4d5SPing-Ke Shih 			val = tssi_info->tssi_mcs[path][gidx];
3599e5efc4d5SPing-Ke Shih 
3600e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3601e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
3602e5efc4d5SPing-Ke Shih 		}
3603e5efc4d5SPing-Ke Shih 	} else {
3604e5efc4d5SPing-Ke Shih 		gidx = _tssi_get_6g_ofdm_group(rtwdev, ch);
3605e5efc4d5SPing-Ke Shih 
3606e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3607e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n",
3608e5efc4d5SPing-Ke Shih 			    path, gidx);
3609e5efc4d5SPing-Ke Shih 
3610e5efc4d5SPing-Ke Shih 		if (IS_TSSI_EXTRA_GROUP(gidx)) {
3611e5efc4d5SPing-Ke Shih 			gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
3612e5efc4d5SPing-Ke Shih 			gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
3613e5efc4d5SPing-Ke Shih 			de_1st = tssi_info->tssi_6g_mcs[path][gidx_1st];
3614e5efc4d5SPing-Ke Shih 			de_2nd = tssi_info->tssi_6g_mcs[path][gidx_2nd];
3615e5efc4d5SPing-Ke Shih 			val = (de_1st + de_2nd) / 2;
3616e5efc4d5SPing-Ke Shih 
3617e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3618e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
3619e5efc4d5SPing-Ke Shih 				    path, val, de_1st, de_2nd);
3620e5efc4d5SPing-Ke Shih 		} else {
3621e5efc4d5SPing-Ke Shih 			val = tssi_info->tssi_6g_mcs[path][gidx];
3622e5efc4d5SPing-Ke Shih 
3623e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3624e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
3625e5efc4d5SPing-Ke Shih 		}
3626e5efc4d5SPing-Ke Shih 	}
3627e5efc4d5SPing-Ke Shih 
3628e5efc4d5SPing-Ke Shih 	return val;
3629e5efc4d5SPing-Ke Shih }
3630e5efc4d5SPing-Ke Shih 
_tssi_get_ofdm_trim_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_rf_path path)3631e5efc4d5SPing-Ke Shih static s8 _tssi_get_ofdm_trim_de(struct rtw89_dev *rtwdev,
3632e5efc4d5SPing-Ke Shih 				 enum rtw89_phy_idx phy,
3633e5efc4d5SPing-Ke Shih 				 enum rtw89_rf_path path)
3634e5efc4d5SPing-Ke Shih {
3635e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3636cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3637cbb145b9SZong-Zhe Yang 	enum rtw89_band band = chan->band_type;
3638cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
3639e5efc4d5SPing-Ke Shih 	u32 tgidx, tgidx_1st, tgidx_2nd;
3640e5efc4d5SPing-Ke Shih 	s8 tde_1st = 0;
3641e5efc4d5SPing-Ke Shih 	s8 tde_2nd = 0;
3642e5efc4d5SPing-Ke Shih 	s8 val;
3643e5efc4d5SPing-Ke Shih 
3644e5efc4d5SPing-Ke Shih 	if (band == RTW89_BAND_2G || band == RTW89_BAND_5G) {
3645e5efc4d5SPing-Ke Shih 		tgidx = _tssi_get_trim_group(rtwdev, ch);
3646e5efc4d5SPing-Ke Shih 
3647e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3648e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
3649e5efc4d5SPing-Ke Shih 			    path, tgidx);
3650e5efc4d5SPing-Ke Shih 
3651e5efc4d5SPing-Ke Shih 		if (IS_TSSI_EXTRA_GROUP(tgidx)) {
3652e5efc4d5SPing-Ke Shih 			tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
3653e5efc4d5SPing-Ke Shih 			tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
3654e5efc4d5SPing-Ke Shih 			tde_1st = tssi_info->tssi_trim[path][tgidx_1st];
3655e5efc4d5SPing-Ke Shih 			tde_2nd = tssi_info->tssi_trim[path][tgidx_2nd];
3656e5efc4d5SPing-Ke Shih 			val = (tde_1st + tde_2nd) / 2;
3657e5efc4d5SPing-Ke Shih 
3658e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3659e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
3660e5efc4d5SPing-Ke Shih 				    path, val, tde_1st, tde_2nd);
3661e5efc4d5SPing-Ke Shih 		} else {
3662e5efc4d5SPing-Ke Shih 			val = tssi_info->tssi_trim[path][tgidx];
3663e5efc4d5SPing-Ke Shih 
3664e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3665e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
3666e5efc4d5SPing-Ke Shih 				    path, val);
3667e5efc4d5SPing-Ke Shih 		}
3668e5efc4d5SPing-Ke Shih 	} else {
3669e5efc4d5SPing-Ke Shih 		tgidx = _tssi_get_6g_trim_group(rtwdev, ch);
3670e5efc4d5SPing-Ke Shih 
3671e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3672e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
3673e5efc4d5SPing-Ke Shih 			    path, tgidx);
3674e5efc4d5SPing-Ke Shih 
3675e5efc4d5SPing-Ke Shih 		if (IS_TSSI_EXTRA_GROUP(tgidx)) {
3676e5efc4d5SPing-Ke Shih 			tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
3677e5efc4d5SPing-Ke Shih 			tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
3678e5efc4d5SPing-Ke Shih 			tde_1st = tssi_info->tssi_trim_6g[path][tgidx_1st];
3679e5efc4d5SPing-Ke Shih 			tde_2nd = tssi_info->tssi_trim_6g[path][tgidx_2nd];
3680e5efc4d5SPing-Ke Shih 			val = (tde_1st + tde_2nd) / 2;
3681e5efc4d5SPing-Ke Shih 
3682e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3683e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
3684e5efc4d5SPing-Ke Shih 				    path, val, tde_1st, tde_2nd);
3685e5efc4d5SPing-Ke Shih 		} else {
3686e5efc4d5SPing-Ke Shih 			val = tssi_info->tssi_trim_6g[path][tgidx];
3687e5efc4d5SPing-Ke Shih 
3688e5efc4d5SPing-Ke Shih 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3689e5efc4d5SPing-Ke Shih 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
3690e5efc4d5SPing-Ke Shih 				    path, val);
3691e5efc4d5SPing-Ke Shih 		}
3692e5efc4d5SPing-Ke Shih 	}
3693e5efc4d5SPing-Ke Shih 
3694e5efc4d5SPing-Ke Shih 	return val;
3695e5efc4d5SPing-Ke Shih }
3696e5efc4d5SPing-Ke Shih 
_tssi_set_efuse_to_de(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)3697e5efc4d5SPing-Ke Shih static void _tssi_set_efuse_to_de(struct rtw89_dev *rtwdev,
3698e5efc4d5SPing-Ke Shih 				  enum rtw89_phy_idx phy)
3699e5efc4d5SPing-Ke Shih {
3700e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3701cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3702cbb145b9SZong-Zhe Yang 	u8 ch = chan->channel;
3703e5efc4d5SPing-Ke Shih 	u8 gidx;
3704e5efc4d5SPing-Ke Shih 	s8 ofdm_de;
3705e5efc4d5SPing-Ke Shih 	s8 trim_de;
3706e5efc4d5SPing-Ke Shih 	s32 val;
3707e5efc4d5SPing-Ke Shih 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3708e5efc4d5SPing-Ke Shih 
3709e5efc4d5SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI][TRIM]: phy=%d ch=%d\n",
3710e5efc4d5SPing-Ke Shih 		    phy, ch);
3711e5efc4d5SPing-Ke Shih 
3712e5efc4d5SPing-Ke Shih 	if (rtwdev->dbcc_en) {
3713e5efc4d5SPing-Ke Shih 		if (phy == RTW89_PHY_0) {
3714e5efc4d5SPing-Ke Shih 			path = RF_PATH_A;
3715e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_B;
3716e5efc4d5SPing-Ke Shih 		} else if (phy == RTW89_PHY_1) {
3717e5efc4d5SPing-Ke Shih 			path = RF_PATH_B;
3718e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_NUM_8852C;
3719e5efc4d5SPing-Ke Shih 		}
3720e5efc4d5SPing-Ke Shih 	}
3721e5efc4d5SPing-Ke Shih 
3722e5efc4d5SPing-Ke Shih 	for (i = path; i < path_max; i++) {
3723e5efc4d5SPing-Ke Shih 		gidx = _tssi_get_cck_group(rtwdev, ch);
3724e5efc4d5SPing-Ke Shih 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
3725e5efc4d5SPing-Ke Shih 		val = tssi_info->tssi_cck[i][gidx] + trim_de;
3726e5efc4d5SPing-Ke Shih 
3727e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3728e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d cck[%d]=0x%x trim=0x%x\n",
3729e5efc4d5SPing-Ke Shih 			    i, gidx, tssi_info->tssi_cck[i][gidx], trim_de);
3730e5efc4d5SPing-Ke Shih 
3731e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_long[i], _TSSI_DE_MASK, val);
3732e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_short[i], _TSSI_DE_MASK, val);
3733e5efc4d5SPing-Ke Shih 
3734e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3735e5efc4d5SPing-Ke Shih 			    "[TSSI] Set TSSI CCK DE 0x%x[21:12]=0x%x\n",
3736e5efc4d5SPing-Ke Shih 			    _tssi_de_cck_long[i],
3737e5efc4d5SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_cck_long[i],
3738e5efc4d5SPing-Ke Shih 						  _TSSI_DE_MASK));
3739e5efc4d5SPing-Ke Shih 
3740e5efc4d5SPing-Ke Shih 		ofdm_de = _tssi_get_ofdm_de(rtwdev, phy, i);
3741e5efc4d5SPing-Ke Shih 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
3742e5efc4d5SPing-Ke Shih 		val = ofdm_de + trim_de;
3743e5efc4d5SPing-Ke Shih 
3744e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3745e5efc4d5SPing-Ke Shih 			    "[TSSI][TRIM]: path=%d mcs=0x%x trim=0x%x\n",
3746e5efc4d5SPing-Ke Shih 			    i, ofdm_de, trim_de);
3747e5efc4d5SPing-Ke Shih 
3748e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_20m[i], _TSSI_DE_MASK, val);
3749e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_40m[i], _TSSI_DE_MASK, val);
3750e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m[i], _TSSI_DE_MASK, val);
3751e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m_80m[i], _TSSI_DE_MASK, val);
3752e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_5m[i], _TSSI_DE_MASK, val);
3753e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_10m[i], _TSSI_DE_MASK, val);
3754e5efc4d5SPing-Ke Shih 
3755e5efc4d5SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3756e5efc4d5SPing-Ke Shih 			    "[TSSI] Set TSSI MCS DE 0x%x[21:12]=0x%x\n",
3757e5efc4d5SPing-Ke Shih 			    _tssi_de_mcs_20m[i],
3758e5efc4d5SPing-Ke Shih 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_mcs_20m[i],
3759e5efc4d5SPing-Ke Shih 						  _TSSI_DE_MASK));
3760e5efc4d5SPing-Ke Shih 	}
3761e5efc4d5SPing-Ke Shih }
3762e5efc4d5SPing-Ke Shih 
rtw8852c_tssi_cont_en(struct rtw89_dev * rtwdev,bool en,enum rtw89_rf_path path)3763e5efc4d5SPing-Ke Shih static void rtw8852c_tssi_cont_en(struct rtw89_dev *rtwdev, bool en,
3764e5efc4d5SPing-Ke Shih 				  enum rtw89_rf_path path)
3765e5efc4d5SPing-Ke Shih {
3766e5efc4d5SPing-Ke Shih 	static const u32 tssi_trk[2] = {0x5818, 0x7818};
3767e5efc4d5SPing-Ke Shih 	static const u32 tssi_en[2] = {0x5820, 0x7820};
3768e5efc4d5SPing-Ke Shih 
3769e5efc4d5SPing-Ke Shih 	if (en) {
3770e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, tssi_trk[path], BIT(30), 0x0);
3771e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, tssi_en[path], BIT(31), 0x0);
3772e5efc4d5SPing-Ke Shih 		if (rtwdev->dbcc_en && path == RF_PATH_B)
3773e5efc4d5SPing-Ke Shih 			_tssi_set_efuse_to_de(rtwdev, RTW89_PHY_1);
3774e5efc4d5SPing-Ke Shih 		else
3775e5efc4d5SPing-Ke Shih 			_tssi_set_efuse_to_de(rtwdev, RTW89_PHY_0);
3776e5efc4d5SPing-Ke Shih 	} else {
3777e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, tssi_trk[path], BIT(30), 0x1);
3778e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, tssi_en[path], BIT(31), 0x1);
3779e5efc4d5SPing-Ke Shih 	}
3780e5efc4d5SPing-Ke Shih }
3781e5efc4d5SPing-Ke Shih 
rtw8852c_tssi_cont_en_phyidx(struct rtw89_dev * rtwdev,bool en,u8 phy_idx)3782e5efc4d5SPing-Ke Shih void rtw8852c_tssi_cont_en_phyidx(struct rtw89_dev *rtwdev, bool en, u8 phy_idx)
3783e5efc4d5SPing-Ke Shih {
3784e5efc4d5SPing-Ke Shih 	if (!rtwdev->dbcc_en) {
3785e5efc4d5SPing-Ke Shih 		rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_A);
3786e5efc4d5SPing-Ke Shih 		rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_B);
3787e5efc4d5SPing-Ke Shih 	} else {
3788e5efc4d5SPing-Ke Shih 		if (phy_idx == RTW89_PHY_0)
3789e5efc4d5SPing-Ke Shih 			rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_A);
3790e5efc4d5SPing-Ke Shih 		else
3791e5efc4d5SPing-Ke Shih 			rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_B);
3792e5efc4d5SPing-Ke Shih 	}
3793e5efc4d5SPing-Ke Shih }
3794e5efc4d5SPing-Ke Shih 
_bw_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,enum rtw89_bandwidth bw,bool is_dav)3795bb865ba6SPing-Ke Shih static void _bw_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
3796bb865ba6SPing-Ke Shih 			enum rtw89_bandwidth bw, bool is_dav)
3797bb865ba6SPing-Ke Shih {
3798bb865ba6SPing-Ke Shih 	u32 rf_reg18;
3799bb865ba6SPing-Ke Shih 	u32 reg_reg18_addr;
3800bb865ba6SPing-Ke Shih 
3801bb865ba6SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3802bb865ba6SPing-Ke Shih 	if (is_dav)
3803bb865ba6SPing-Ke Shih 		reg_reg18_addr = RR_CFGCH;
3804bb865ba6SPing-Ke Shih 	else
3805bb865ba6SPing-Ke Shih 		reg_reg18_addr = RR_CFGCH_V1;
3806bb865ba6SPing-Ke Shih 
3807bb865ba6SPing-Ke Shih 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK);
3808bb865ba6SPing-Ke Shih 	rf_reg18 &= ~RR_CFGCH_BW;
3809bb865ba6SPing-Ke Shih 
3810bb865ba6SPing-Ke Shih 	switch (bw) {
3811bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_5:
3812bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_10:
3813bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_20:
3814bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_20M);
3815bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x3);
3816bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xf);
3817bb865ba6SPing-Ke Shih 		break;
3818bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_40:
3819bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_40M);
3820bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x3);
3821bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xf);
3822bb865ba6SPing-Ke Shih 		break;
3823bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_80:
3824bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_80M);
3825bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x2);
3826bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xd);
3827bb865ba6SPing-Ke Shih 		break;
3828bb865ba6SPing-Ke Shih 	case RTW89_CHANNEL_WIDTH_160:
3829bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_160M);
3830bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x1);
3831bb865ba6SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xb);
3832bb865ba6SPing-Ke Shih 		break;
3833bb865ba6SPing-Ke Shih 	default:
3834bb865ba6SPing-Ke Shih 		break;
3835bb865ba6SPing-Ke Shih 	}
3836bb865ba6SPing-Ke Shih 
3837bb865ba6SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK, rf_reg18);
3838bb865ba6SPing-Ke Shih }
3839bb865ba6SPing-Ke Shih 
_ctrl_bw(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_bandwidth bw)3840bb865ba6SPing-Ke Shih static void _ctrl_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3841bb865ba6SPing-Ke Shih 		     enum rtw89_bandwidth bw)
3842bb865ba6SPing-Ke Shih {
3843bb865ba6SPing-Ke Shih 	bool is_dav;
3844bb865ba6SPing-Ke Shih 	u8 kpath, path;
3845bb865ba6SPing-Ke Shih 	u32 tmp = 0;
3846bb865ba6SPing-Ke Shih 
3847bb865ba6SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3848bb865ba6SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
3849bb865ba6SPing-Ke Shih 
3850bb865ba6SPing-Ke Shih 	for (path = 0; path < 2; path++) {
3851bb865ba6SPing-Ke Shih 		if (!(kpath & BIT(path)))
3852bb865ba6SPing-Ke Shih 			continue;
3853bb865ba6SPing-Ke Shih 
3854bb865ba6SPing-Ke Shih 		is_dav = true;
3855bb865ba6SPing-Ke Shih 		_bw_setting(rtwdev, path, bw, is_dav);
3856bb865ba6SPing-Ke Shih 		is_dav = false;
3857bb865ba6SPing-Ke Shih 		_bw_setting(rtwdev, path, bw, is_dav);
3858bb865ba6SPing-Ke Shih 		if (rtwdev->dbcc_en)
3859bb865ba6SPing-Ke Shih 			continue;
3860bb865ba6SPing-Ke Shih 
3861bb865ba6SPing-Ke Shih 		if (path == RF_PATH_B && rtwdev->hal.cv == CHIP_CAV) {
3862bb865ba6SPing-Ke Shih 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x0);
3863bb865ba6SPing-Ke Shih 			tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
3864bb865ba6SPing-Ke Shih 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_APK, RR_APK_MOD, 0x3);
3865bb865ba6SPing-Ke Shih 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_CFGCH, RFREG_MASK, tmp);
3866bb865ba6SPing-Ke Shih 			fsleep(100);
3867bb865ba6SPing-Ke Shih 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x1);
3868bb865ba6SPing-Ke Shih 		}
3869bb865ba6SPing-Ke Shih 	}
3870bb865ba6SPing-Ke Shih }
3871bb865ba6SPing-Ke Shih 
_ch_setting(struct rtw89_dev * rtwdev,enum rtw89_rf_path path,u8 central_ch,enum rtw89_band band,bool is_dav)3872bb865ba6SPing-Ke Shih static void _ch_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
3873bb865ba6SPing-Ke Shih 			u8 central_ch, enum rtw89_band band, bool is_dav)
3874bb865ba6SPing-Ke Shih {
3875bb865ba6SPing-Ke Shih 	u32 rf_reg18;
3876bb865ba6SPing-Ke Shih 	u32 reg_reg18_addr;
3877bb865ba6SPing-Ke Shih 
3878bb865ba6SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3879bb865ba6SPing-Ke Shih 	if (is_dav)
3880bb865ba6SPing-Ke Shih 		reg_reg18_addr = 0x18;
3881bb865ba6SPing-Ke Shih 	else
3882bb865ba6SPing-Ke Shih 		reg_reg18_addr = 0x10018;
3883bb865ba6SPing-Ke Shih 
3884bb865ba6SPing-Ke Shih 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK);
3885bb865ba6SPing-Ke Shih 	rf_reg18 &= ~(RR_CFGCH_BAND1 | RR_CFGCH_BAND0 | RR_CFGCH_CH);
3886bb865ba6SPing-Ke Shih 	rf_reg18 |= FIELD_PREP(RR_CFGCH_CH, central_ch);
3887bb865ba6SPing-Ke Shih 
3888bb865ba6SPing-Ke Shih 	switch (band) {
3889bb865ba6SPing-Ke Shih 	case RTW89_BAND_2G:
3890bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_2G);
3891bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_2G);
3892bb865ba6SPing-Ke Shih 		break;
3893bb865ba6SPing-Ke Shih 	case RTW89_BAND_5G:
3894bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_5G);
3895bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_5G);
3896bb865ba6SPing-Ke Shih 		break;
3897bb865ba6SPing-Ke Shih 	case RTW89_BAND_6G:
3898bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_6G);
3899bb865ba6SPing-Ke Shih 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_6G);
3900bb865ba6SPing-Ke Shih 		break;
3901bb865ba6SPing-Ke Shih 	default:
3902bb865ba6SPing-Ke Shih 		break;
3903bb865ba6SPing-Ke Shih 	}
3904bb865ba6SPing-Ke Shih 	rtw89_write_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK, rf_reg18);
3905bb865ba6SPing-Ke Shih 	fsleep(100);
3906bb865ba6SPing-Ke Shih }
3907bb865ba6SPing-Ke Shih 
_ctrl_ch(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 central_ch,enum rtw89_band band)3908bb865ba6SPing-Ke Shih static void _ctrl_ch(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3909bb865ba6SPing-Ke Shih 		     u8 central_ch, enum rtw89_band band)
3910bb865ba6SPing-Ke Shih {
3911bb865ba6SPing-Ke Shih 	u8 kpath, path;
3912bb865ba6SPing-Ke Shih 
3913bb865ba6SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3914bb865ba6SPing-Ke Shih 	if (band != RTW89_BAND_6G) {
3915bb865ba6SPing-Ke Shih 		if ((central_ch > 14 && central_ch < 36) ||
3916bb865ba6SPing-Ke Shih 		    (central_ch > 64 && central_ch < 100) ||
3917bb865ba6SPing-Ke Shih 		    (central_ch > 144 && central_ch < 149) || central_ch > 177)
3918bb865ba6SPing-Ke Shih 			return;
3919bb865ba6SPing-Ke Shih 	} else {
3920bb865ba6SPing-Ke Shih 		if (central_ch > 253 || central_ch  == 2)
3921bb865ba6SPing-Ke Shih 			return;
3922bb865ba6SPing-Ke Shih 	}
3923bb865ba6SPing-Ke Shih 
3924bb865ba6SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
3925bb865ba6SPing-Ke Shih 
3926bb865ba6SPing-Ke Shih 	for (path = 0; path < 2; path++) {
3927bb865ba6SPing-Ke Shih 		if (kpath & BIT(path)) {
3928bb865ba6SPing-Ke Shih 			_ch_setting(rtwdev, path, central_ch, band, true);
3929bb865ba6SPing-Ke Shih 			_ch_setting(rtwdev, path, central_ch, band, false);
3930bb865ba6SPing-Ke Shih 		}
3931bb865ba6SPing-Ke Shih 	}
3932bb865ba6SPing-Ke Shih }
3933bb865ba6SPing-Ke Shih 
_rxbb_bw(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,enum rtw89_bandwidth bw)3934bb865ba6SPing-Ke Shih static void _rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3935bb865ba6SPing-Ke Shih 		     enum rtw89_bandwidth bw)
3936bb865ba6SPing-Ke Shih {
3937bb865ba6SPing-Ke Shih 	u8 kpath;
3938bb865ba6SPing-Ke Shih 	u8 path;
3939bb865ba6SPing-Ke Shih 	u32 val;
3940bb865ba6SPing-Ke Shih 
3941bb865ba6SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
3942bb865ba6SPing-Ke Shih 	for (path = 0; path < 2; path++) {
3943bb865ba6SPing-Ke Shih 		if (!(kpath & BIT(path)))
3944bb865ba6SPing-Ke Shih 			continue;
3945bb865ba6SPing-Ke Shih 
3946bb865ba6SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x1);
3947bb865ba6SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWA, RR_LUTWA_M2, 0xa);
3948bb865ba6SPing-Ke Shih 		switch (bw) {
3949bb865ba6SPing-Ke Shih 		case RTW89_CHANNEL_WIDTH_20:
3950bb865ba6SPing-Ke Shih 			val = 0x1b;
3951bb865ba6SPing-Ke Shih 			break;
3952bb865ba6SPing-Ke Shih 		case RTW89_CHANNEL_WIDTH_40:
3953bb865ba6SPing-Ke Shih 			val = 0x13;
3954bb865ba6SPing-Ke Shih 			break;
3955bb865ba6SPing-Ke Shih 		case RTW89_CHANNEL_WIDTH_80:
3956bb865ba6SPing-Ke Shih 			val = 0xb;
3957bb865ba6SPing-Ke Shih 			break;
3958bb865ba6SPing-Ke Shih 		case RTW89_CHANNEL_WIDTH_160:
3959bb865ba6SPing-Ke Shih 		default:
3960bb865ba6SPing-Ke Shih 			val = 0x3;
3961bb865ba6SPing-Ke Shih 			break;
3962bb865ba6SPing-Ke Shih 		}
3963bb865ba6SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, val);
3964bb865ba6SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x0);
3965bb865ba6SPing-Ke Shih 	}
3966bb865ba6SPing-Ke Shih }
3967bb865ba6SPing-Ke Shih 
_lck_keep_thermal(struct rtw89_dev * rtwdev)3968fb8177d7SPing-Ke Shih static void _lck_keep_thermal(struct rtw89_dev *rtwdev)
3969fb8177d7SPing-Ke Shih {
3970fb8177d7SPing-Ke Shih 	struct rtw89_lck_info *lck = &rtwdev->lck;
3971fb8177d7SPing-Ke Shih 	int path;
3972fb8177d7SPing-Ke Shih 
3973fb8177d7SPing-Ke Shih 	for (path = 0; path < rtwdev->chip->rf_path_num; path++) {
3974fb8177d7SPing-Ke Shih 		lck->thermal[path] =
3975fb8177d7SPing-Ke Shih 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
3976fb8177d7SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3977fb8177d7SPing-Ke Shih 			    "[LCK] path=%d thermal=0x%x", path, lck->thermal[path]);
3978fb8177d7SPing-Ke Shih 	}
3979fb8177d7SPing-Ke Shih }
3980fb8177d7SPing-Ke Shih 
_lck(struct rtw89_dev * rtwdev)3981fb8177d7SPing-Ke Shih static void _lck(struct rtw89_dev *rtwdev)
3982fb8177d7SPing-Ke Shih {
3983fb8177d7SPing-Ke Shih 	u32 tmp18[2];
3984fb8177d7SPing-Ke Shih 	int path = rtwdev->dbcc_en ? 2 : 1;
3985fb8177d7SPing-Ke Shih 	int i;
3986fb8177d7SPing-Ke Shih 
3987fb8177d7SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, "[LCK] DO LCK\n");
3988fb8177d7SPing-Ke Shih 
3989fb8177d7SPing-Ke Shih 	tmp18[0] = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
3990fb8177d7SPing-Ke Shih 	tmp18[1] = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CFGCH, RFREG_MASK);
3991fb8177d7SPing-Ke Shih 
3992fb8177d7SPing-Ke Shih 	for (i = 0; i < path; i++) {
3993fb8177d7SPing-Ke Shih 		rtw89_write_rf(rtwdev, i, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
3994fb8177d7SPing-Ke Shih 		rtw89_write_rf(rtwdev, i, RR_CFGCH, RFREG_MASK, tmp18[i]);
3995fb8177d7SPing-Ke Shih 		rtw89_write_rf(rtwdev, i, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
3996fb8177d7SPing-Ke Shih 	}
3997fb8177d7SPing-Ke Shih 
3998fb8177d7SPing-Ke Shih 	_lck_keep_thermal(rtwdev);
3999fb8177d7SPing-Ke Shih }
4000fb8177d7SPing-Ke Shih 
4001fb8177d7SPing-Ke Shih #define RTW8852C_LCK_TH 8
4002fb8177d7SPing-Ke Shih 
rtw8852c_lck_track(struct rtw89_dev * rtwdev)4003fb8177d7SPing-Ke Shih void rtw8852c_lck_track(struct rtw89_dev *rtwdev)
4004fb8177d7SPing-Ke Shih {
4005fb8177d7SPing-Ke Shih 	struct rtw89_lck_info *lck = &rtwdev->lck;
4006fb8177d7SPing-Ke Shih 	u8 cur_thermal;
4007fb8177d7SPing-Ke Shih 	int delta;
4008fb8177d7SPing-Ke Shih 	int path;
4009fb8177d7SPing-Ke Shih 
4010fb8177d7SPing-Ke Shih 	for (path = 0; path < rtwdev->chip->rf_path_num; path++) {
4011fb8177d7SPing-Ke Shih 		cur_thermal =
4012fb8177d7SPing-Ke Shih 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4013fb8177d7SPing-Ke Shih 		delta = abs((int)cur_thermal - lck->thermal[path]);
4014fb8177d7SPing-Ke Shih 
4015fb8177d7SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
4016fb8177d7SPing-Ke Shih 			    "[LCK] path=%d current thermal=0x%x delta=0x%x\n",
4017fb8177d7SPing-Ke Shih 			    path, cur_thermal, delta);
4018fb8177d7SPing-Ke Shih 
4019fb8177d7SPing-Ke Shih 		if (delta >= RTW8852C_LCK_TH) {
4020fb8177d7SPing-Ke Shih 			_lck(rtwdev);
4021fb8177d7SPing-Ke Shih 			return;
4022fb8177d7SPing-Ke Shih 		}
4023fb8177d7SPing-Ke Shih 	}
4024fb8177d7SPing-Ke Shih }
4025fb8177d7SPing-Ke Shih 
rtw8852c_lck_init(struct rtw89_dev * rtwdev)4026fb8177d7SPing-Ke Shih void rtw8852c_lck_init(struct rtw89_dev *rtwdev)
4027fb8177d7SPing-Ke Shih {
4028fb8177d7SPing-Ke Shih 	_lck_keep_thermal(rtwdev);
4029fb8177d7SPing-Ke Shih }
4030fb8177d7SPing-Ke Shih 
4031bb865ba6SPing-Ke Shih static
rtw8852c_ctrl_bw_ch(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,u8 central_ch,enum rtw89_band band,enum rtw89_bandwidth bw)4032bb865ba6SPing-Ke Shih void rtw8852c_ctrl_bw_ch(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
4033bb865ba6SPing-Ke Shih 			 u8 central_ch, enum rtw89_band band,
4034bb865ba6SPing-Ke Shih 			 enum rtw89_bandwidth bw)
4035bb865ba6SPing-Ke Shih {
4036bb865ba6SPing-Ke Shih 	_ctrl_ch(rtwdev, phy, central_ch, band);
4037bb865ba6SPing-Ke Shih 	_ctrl_bw(rtwdev, phy, bw);
4038bb865ba6SPing-Ke Shih 	_rxbb_bw(rtwdev, phy, bw);
4039bb865ba6SPing-Ke Shih }
4040bb865ba6SPing-Ke Shih 
rtw8852c_set_channel_rf(struct rtw89_dev * rtwdev,const struct rtw89_chan * chan,enum rtw89_phy_idx phy_idx)4041bb865ba6SPing-Ke Shih void rtw8852c_set_channel_rf(struct rtw89_dev *rtwdev,
40423e5831caSZong-Zhe Yang 			     const struct rtw89_chan *chan,
4043bb865ba6SPing-Ke Shih 			     enum rtw89_phy_idx phy_idx)
4044bb865ba6SPing-Ke Shih {
40453e5831caSZong-Zhe Yang 	rtw8852c_ctrl_bw_ch(rtwdev, phy_idx, chan->channel,
40463e5831caSZong-Zhe Yang 			    chan->band_type,
40473e5831caSZong-Zhe Yang 			    chan->band_width);
4048bb865ba6SPing-Ke Shih }
404976599a8dSPing-Ke Shih 
rtw8852c_mcc_get_ch_info(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)40505309cd5eSPing-Ke Shih void rtw8852c_mcc_get_ch_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
40515309cd5eSPing-Ke Shih {
4052cbb145b9SZong-Zhe Yang 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
405338f25decSZong-Zhe Yang 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
405438f25decSZong-Zhe Yang 	u8 idx = rfk_mcc->table_idx;
40555309cd5eSPing-Ke Shih 	int i;
40565309cd5eSPing-Ke Shih 
40575309cd5eSPing-Ke Shih 	for (i = 0; i < RTW89_IQK_CHS_NR; i++) {
405838f25decSZong-Zhe Yang 		if (rfk_mcc->ch[idx] == 0)
40595309cd5eSPing-Ke Shih 			break;
40605309cd5eSPing-Ke Shih 		if (++idx >= RTW89_IQK_CHS_NR)
40615309cd5eSPing-Ke Shih 			idx = 0;
40625309cd5eSPing-Ke Shih 	}
40635309cd5eSPing-Ke Shih 
406438f25decSZong-Zhe Yang 	rfk_mcc->table_idx = idx;
406538f25decSZong-Zhe Yang 	rfk_mcc->ch[idx] = chan->channel;
406638f25decSZong-Zhe Yang 	rfk_mcc->band[idx] = chan->band_type;
40675309cd5eSPing-Ke Shih }
40685309cd5eSPing-Ke Shih 
rtw8852c_rck(struct rtw89_dev * rtwdev)406930052c5aSPing-Ke Shih void rtw8852c_rck(struct rtw89_dev *rtwdev)
407030052c5aSPing-Ke Shih {
407130052c5aSPing-Ke Shih 	u8 path;
407230052c5aSPing-Ke Shih 
407330052c5aSPing-Ke Shih 	for (path = 0; path < 2; path++)
407430052c5aSPing-Ke Shih 		_rck(rtwdev, path);
407530052c5aSPing-Ke Shih }
407630052c5aSPing-Ke Shih 
rtw8852c_dack(struct rtw89_dev * rtwdev)407776599a8dSPing-Ke Shih void rtw8852c_dack(struct rtw89_dev *rtwdev)
407876599a8dSPing-Ke Shih {
407976599a8dSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, RTW89_PHY_0, 0);
408076599a8dSPing-Ke Shih 
408176599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_START);
408276599a8dSPing-Ke Shih 	_dac_cal(rtwdev, false);
408376599a8dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_STOP);
408476599a8dSPing-Ke Shih }
4085e5efc4d5SPing-Ke Shih 
rtw8852c_iqk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)40862da8109dSPing-Ke Shih void rtw8852c_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
40872da8109dSPing-Ke Shih {
40882da8109dSPing-Ke Shih 	u32 tx_en;
40892da8109dSPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
40902da8109dSPing-Ke Shih 
40912da8109dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_START);
40922da8109dSPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
40932da8109dSPing-Ke Shih 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
40942da8109dSPing-Ke Shih 
40952da8109dSPing-Ke Shih 	_iqk_init(rtwdev);
40962da8109dSPing-Ke Shih 	_iqk(rtwdev, phy_idx, false);
40972da8109dSPing-Ke Shih 
40982da8109dSPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
40992da8109dSPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_STOP);
41002da8109dSPing-Ke Shih }
41012da8109dSPing-Ke Shih 
4102ac91be97SPing-Ke Shih #define RXDCK_VER_8852C 0xe
4103ac91be97SPing-Ke Shih 
_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool is_afe,u8 retry_limit)41049c22d603SPing-Ke Shih static void _rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
41059c22d603SPing-Ke Shih 		    bool is_afe, u8 retry_limit)
4106ac91be97SPing-Ke Shih {
4107e3d365ffSPing-Ke Shih 	struct rtw89_rx_dck_info *rx_dck = &rtwdev->rx_dck;
4108ac91be97SPing-Ke Shih 	u8 path, kpath;
4109ac91be97SPing-Ke Shih 	u32 rf_reg5;
41109c22d603SPing-Ke Shih 	bool is_fail;
41119c22d603SPing-Ke Shih 	u8 rek_cnt;
4112ac91be97SPing-Ke Shih 
4113ac91be97SPing-Ke Shih 	kpath = _kpath(rtwdev, phy);
4114ac91be97SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
4115ac91be97SPing-Ke Shih 		    "[RX_DCK] ****** RXDCK Start (Ver: 0x%x, Cv: %d) ******\n",
4116ac91be97SPing-Ke Shih 		    RXDCK_VER_8852C, rtwdev->hal.cv);
4117ac91be97SPing-Ke Shih 
4118ac91be97SPing-Ke Shih 	for (path = 0; path < 2; path++) {
4119ac91be97SPing-Ke Shih 		rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
4120ac91be97SPing-Ke Shih 		if (!(kpath & BIT(path)))
4121ac91be97SPing-Ke Shih 			continue;
4122ac91be97SPing-Ke Shih 
4123ac91be97SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
4124ac91be97SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
4125ac91be97SPing-Ke Shih 					       B_P0_TSSI_TRK_EN, 0x1);
4126ac91be97SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
4127ac91be97SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
41289c22d603SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_LO_SEL, rtwdev->dbcc_en);
41299c22d603SPing-Ke Shih 
41309c22d603SPing-Ke Shih 		for (rek_cnt = 0; rek_cnt < retry_limit; rek_cnt++) {
4131ac91be97SPing-Ke Shih 			_set_rx_dck(rtwdev, phy, path, is_afe);
41329c22d603SPing-Ke Shih 
41339c22d603SPing-Ke Shih 			/* To reduce IO of dck_rek_check(), the last try is seen
41349c22d603SPing-Ke Shih 			 * as failure always, and then do recovery procedure.
41359c22d603SPing-Ke Shih 			 */
41369c22d603SPing-Ke Shih 			if (rek_cnt == retry_limit - 1) {
41379c22d603SPing-Ke Shih 				_rx_dck_recover(rtwdev, path);
41389c22d603SPing-Ke Shih 				break;
41399c22d603SPing-Ke Shih 			}
41409c22d603SPing-Ke Shih 
41419c22d603SPing-Ke Shih 			is_fail = _rx_dck_rek_check(rtwdev, path);
41429c22d603SPing-Ke Shih 			if (!is_fail)
41439c22d603SPing-Ke Shih 				break;
41449c22d603SPing-Ke Shih 		}
41459c22d603SPing-Ke Shih 
41469c22d603SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] rek_cnt[%d]=%d",
41479c22d603SPing-Ke Shih 			    path, rek_cnt);
41489c22d603SPing-Ke Shih 
4149e3d365ffSPing-Ke Shih 		rx_dck->thermal[path] = ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4150ac91be97SPing-Ke Shih 		rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
4151ac91be97SPing-Ke Shih 
4152ac91be97SPing-Ke Shih 		if (rtwdev->is_tssi_mode[path])
4153ac91be97SPing-Ke Shih 			rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
4154ac91be97SPing-Ke Shih 					       B_P0_TSSI_TRK_EN, 0x0);
4155ac91be97SPing-Ke Shih 	}
4156ac91be97SPing-Ke Shih }
4157ac91be97SPing-Ke Shih 
rtw8852c_rx_dck(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool is_afe)41589c22d603SPing-Ke Shih void rtw8852c_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool is_afe)
41599c22d603SPing-Ke Shih {
41609c22d603SPing-Ke Shih 	_rx_dck(rtwdev, phy, is_afe, 1);
41619c22d603SPing-Ke Shih }
41629c22d603SPing-Ke Shih 
41639c22d603SPing-Ke Shih #define RTW8852C_RX_DCK_TH 12
4164e3d365ffSPing-Ke Shih 
rtw8852c_rx_dck_track(struct rtw89_dev * rtwdev)4165e3d365ffSPing-Ke Shih void rtw8852c_rx_dck_track(struct rtw89_dev *rtwdev)
4166e3d365ffSPing-Ke Shih {
41679c22d603SPing-Ke Shih 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
4168e3d365ffSPing-Ke Shih 	struct rtw89_rx_dck_info *rx_dck = &rtwdev->rx_dck;
41699c22d603SPing-Ke Shih 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
41709c22d603SPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
41719c22d603SPing-Ke Shih 	u8 dck_channel;
4172e3d365ffSPing-Ke Shih 	u8 cur_thermal;
41739c22d603SPing-Ke Shih 	u32 tx_en;
4174e3d365ffSPing-Ke Shih 	int delta;
4175e3d365ffSPing-Ke Shih 	int path;
4176e3d365ffSPing-Ke Shih 
41779c22d603SPing-Ke Shih 	if (chan->band_type == RTW89_BAND_2G)
41789c22d603SPing-Ke Shih 		return;
41799c22d603SPing-Ke Shih 
41809c22d603SPing-Ke Shih 	if (rtwdev->scanning)
41819c22d603SPing-Ke Shih 		return;
41829c22d603SPing-Ke Shih 
4183e3d365ffSPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8852C; path++) {
4184e3d365ffSPing-Ke Shih 		cur_thermal =
4185e3d365ffSPing-Ke Shih 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4186e3d365ffSPing-Ke Shih 		delta = abs((int)cur_thermal - rx_dck->thermal[path]);
4187e3d365ffSPing-Ke Shih 
4188e3d365ffSPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
4189e3d365ffSPing-Ke Shih 			    "[RX_DCK] path=%d current thermal=0x%x delta=0x%x\n",
4190e3d365ffSPing-Ke Shih 			    path, cur_thermal, delta);
4191e3d365ffSPing-Ke Shih 
41929c22d603SPing-Ke Shih 		if (delta >= RTW8852C_RX_DCK_TH)
41939c22d603SPing-Ke Shih 			goto trigger_rx_dck;
41949c22d603SPing-Ke Shih 	}
41959c22d603SPing-Ke Shih 
4196e3d365ffSPing-Ke Shih 	return;
41979c22d603SPing-Ke Shih 
41989c22d603SPing-Ke Shih trigger_rx_dck:
41999c22d603SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_START);
42009c22d603SPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
42019c22d603SPing-Ke Shih 
42029c22d603SPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8852C; path++) {
42039c22d603SPing-Ke Shih 		dck_channel = _rx_dck_channel_calc(rtwdev, chan);
42049c22d603SPing-Ke Shih 		_ctrl_ch(rtwdev, RTW89_PHY_0, dck_channel, chan->band_type);
4205e3d365ffSPing-Ke Shih 	}
42069c22d603SPing-Ke Shih 
42079c22d603SPing-Ke Shih 	_rx_dck(rtwdev, RTW89_PHY_0, false, 20);
42089c22d603SPing-Ke Shih 
42099c22d603SPing-Ke Shih 	for (path = 0; path < RF_PATH_NUM_8852C; path++)
42109c22d603SPing-Ke Shih 		_ctrl_ch(rtwdev, RTW89_PHY_0, chan->channel, chan->band_type);
42119c22d603SPing-Ke Shih 
42129c22d603SPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
42139c22d603SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_STOP);
4214e3d365ffSPing-Ke Shih }
4215e3d365ffSPing-Ke Shih 
rtw8852c_dpk(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy_idx)4216da4cea16SPing-Ke Shih void rtw8852c_dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
4217da4cea16SPing-Ke Shih {
4218da4cea16SPing-Ke Shih 	u32 tx_en;
4219da4cea16SPing-Ke Shih 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
4220da4cea16SPing-Ke Shih 
4221da4cea16SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_START);
4222da4cea16SPing-Ke Shih 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
4223da4cea16SPing-Ke Shih 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
4224da4cea16SPing-Ke Shih 
4225da4cea16SPing-Ke Shih 	rtwdev->dpk.is_dpk_enable = true;
4226da4cea16SPing-Ke Shih 	rtwdev->dpk.is_dpk_reload_en = false;
4227da4cea16SPing-Ke Shih 	_dpk(rtwdev, phy_idx, false);
4228da4cea16SPing-Ke Shih 
4229da4cea16SPing-Ke Shih 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
4230da4cea16SPing-Ke Shih 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_STOP);
4231da4cea16SPing-Ke Shih }
4232da4cea16SPing-Ke Shih 
rtw8852c_dpk_track(struct rtw89_dev * rtwdev)4233da4cea16SPing-Ke Shih void rtw8852c_dpk_track(struct rtw89_dev *rtwdev)
4234da4cea16SPing-Ke Shih {
4235da4cea16SPing-Ke Shih 	_dpk_track(rtwdev);
4236da4cea16SPing-Ke Shih }
4237da4cea16SPing-Ke Shih 
rtw8852c_tssi(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)4238e5efc4d5SPing-Ke Shih void rtw8852c_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
4239e5efc4d5SPing-Ke Shih {
4240e5efc4d5SPing-Ke Shih 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
4241e5efc4d5SPing-Ke Shih 
4242e5efc4d5SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n", __func__, phy);
4243e5efc4d5SPing-Ke Shih 
4244e5efc4d5SPing-Ke Shih 	if (rtwdev->dbcc_en) {
4245e5efc4d5SPing-Ke Shih 		if (phy == RTW89_PHY_0) {
4246e5efc4d5SPing-Ke Shih 			path = RF_PATH_A;
4247e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_B;
4248e5efc4d5SPing-Ke Shih 		} else if (phy == RTW89_PHY_1) {
4249e5efc4d5SPing-Ke Shih 			path = RF_PATH_B;
4250e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_NUM_8852C;
4251e5efc4d5SPing-Ke Shih 		}
4252e5efc4d5SPing-Ke Shih 	}
4253e5efc4d5SPing-Ke Shih 
4254e5efc4d5SPing-Ke Shih 	_tssi_disable(rtwdev, phy);
4255e5efc4d5SPing-Ke Shih 
4256e5efc4d5SPing-Ke Shih 	for (i = path; i < path_max; i++) {
4257e5efc4d5SPing-Ke Shih 		_tssi_set_sys(rtwdev, phy, i);
4258e5efc4d5SPing-Ke Shih 		_tssi_ini_txpwr_ctrl_bb(rtwdev, phy, i);
4259e5efc4d5SPing-Ke Shih 		_tssi_ini_txpwr_ctrl_bb_he_tb(rtwdev, phy, i);
4260e5efc4d5SPing-Ke Shih 		_tssi_set_dck(rtwdev, phy, i);
4261e5efc4d5SPing-Ke Shih 		_tssi_set_bbgain_split(rtwdev, phy, i);
4262e5efc4d5SPing-Ke Shih 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
4263e5efc4d5SPing-Ke Shih 		_tssi_slope_cal_org(rtwdev, phy, i);
4264e5efc4d5SPing-Ke Shih 		_tssi_set_aligk_default(rtwdev, phy, i);
4265e5efc4d5SPing-Ke Shih 		_tssi_set_slope(rtwdev, phy, i);
4266e5efc4d5SPing-Ke Shih 		_tssi_run_slope(rtwdev, phy, i);
4267e5efc4d5SPing-Ke Shih 	}
4268e5efc4d5SPing-Ke Shih 
4269e5efc4d5SPing-Ke Shih 	_tssi_enable(rtwdev, phy);
4270e5efc4d5SPing-Ke Shih 	_tssi_set_efuse_to_de(rtwdev, phy);
4271e5efc4d5SPing-Ke Shih }
4272e5efc4d5SPing-Ke Shih 
rtw8852c_tssi_scan(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy)4273e5efc4d5SPing-Ke Shih void rtw8852c_tssi_scan(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
4274e5efc4d5SPing-Ke Shih {
4275e5efc4d5SPing-Ke Shih 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
4276e5efc4d5SPing-Ke Shih 
4277e5efc4d5SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n",
4278e5efc4d5SPing-Ke Shih 		    __func__, phy);
4279e5efc4d5SPing-Ke Shih 
4280e5efc4d5SPing-Ke Shih 	if (!rtwdev->is_tssi_mode[RF_PATH_A])
4281e5efc4d5SPing-Ke Shih 		return;
4282e5efc4d5SPing-Ke Shih 	if (!rtwdev->is_tssi_mode[RF_PATH_B])
4283e5efc4d5SPing-Ke Shih 		return;
4284e5efc4d5SPing-Ke Shih 
4285e5efc4d5SPing-Ke Shih 	if (rtwdev->dbcc_en) {
4286e5efc4d5SPing-Ke Shih 		if (phy == RTW89_PHY_0) {
4287e5efc4d5SPing-Ke Shih 			path = RF_PATH_A;
4288e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_B;
4289e5efc4d5SPing-Ke Shih 		} else if (phy == RTW89_PHY_1) {
4290e5efc4d5SPing-Ke Shih 			path = RF_PATH_B;
4291e5efc4d5SPing-Ke Shih 			path_max = RF_PATH_NUM_8852C;
4292e5efc4d5SPing-Ke Shih 		}
4293e5efc4d5SPing-Ke Shih 	}
4294e5efc4d5SPing-Ke Shih 
4295e5efc4d5SPing-Ke Shih 	_tssi_disable(rtwdev, phy);
4296e5efc4d5SPing-Ke Shih 
4297e5efc4d5SPing-Ke Shih 	for (i = path; i < path_max; i++) {
4298e5efc4d5SPing-Ke Shih 		_tssi_set_sys(rtwdev, phy, i);
4299e5efc4d5SPing-Ke Shih 		_tssi_set_dck(rtwdev, phy, i);
4300e5efc4d5SPing-Ke Shih 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
4301e5efc4d5SPing-Ke Shih 		_tssi_slope_cal_org(rtwdev, phy, i);
4302e5efc4d5SPing-Ke Shih 		_tssi_set_aligk_default(rtwdev, phy, i);
4303e5efc4d5SPing-Ke Shih 	}
4304e5efc4d5SPing-Ke Shih 
4305e5efc4d5SPing-Ke Shih 	_tssi_enable(rtwdev, phy);
4306e5efc4d5SPing-Ke Shih 	_tssi_set_efuse_to_de(rtwdev, phy);
4307e5efc4d5SPing-Ke Shih }
4308e5efc4d5SPing-Ke Shih 
rtw8852c_tssi_default_txagc(struct rtw89_dev * rtwdev,enum rtw89_phy_idx phy,bool enable)4309e5efc4d5SPing-Ke Shih static void rtw8852c_tssi_default_txagc(struct rtw89_dev *rtwdev,
4310e5efc4d5SPing-Ke Shih 					enum rtw89_phy_idx phy, bool enable)
4311e5efc4d5SPing-Ke Shih {
4312e5efc4d5SPing-Ke Shih 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
4313e5efc4d5SPing-Ke Shih 	u8 i;
4314e5efc4d5SPing-Ke Shih 
4315e5efc4d5SPing-Ke Shih 	if (!rtwdev->is_tssi_mode[RF_PATH_A] && !rtwdev->is_tssi_mode[RF_PATH_B])
4316e5efc4d5SPing-Ke Shih 		return;
4317e5efc4d5SPing-Ke Shih 
4318e5efc4d5SPing-Ke Shih 	if (enable) {
4319e5efc4d5SPing-Ke Shih 		/* SCAN_START */
4320e5efc4d5SPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB, B_TXAGC_BB_OFT) != 0xc000 &&
4321e5efc4d5SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB, B_TXAGC_BB_OFT) != 0x0) {
4322e5efc4d5SPing-Ke Shih 			for (i = 0; i < 6; i++) {
4323e5efc4d5SPing-Ke Shih 				tssi_info->default_txagc_offset[RF_PATH_A] =
4324e5efc4d5SPing-Ke Shih 					rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB,
4325e5efc4d5SPing-Ke Shih 							      B_TXAGC_BB);
4326e5efc4d5SPing-Ke Shih 				if (tssi_info->default_txagc_offset[RF_PATH_A])
4327e5efc4d5SPing-Ke Shih 					break;
4328e5efc4d5SPing-Ke Shih 			}
4329e5efc4d5SPing-Ke Shih 		}
4330e5efc4d5SPing-Ke Shih 
4331e5efc4d5SPing-Ke Shih 		if (rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1, B_TXAGC_BB_S1_OFT) != 0xc000 &&
4332e5efc4d5SPing-Ke Shih 		    rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1, B_TXAGC_BB_S1_OFT) != 0x0) {
4333e5efc4d5SPing-Ke Shih 			for (i = 0; i < 6; i++) {
4334e5efc4d5SPing-Ke Shih 				tssi_info->default_txagc_offset[RF_PATH_B] =
4335e5efc4d5SPing-Ke Shih 					rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1,
4336e5efc4d5SPing-Ke Shih 							      B_TXAGC_BB_S1);
4337e5efc4d5SPing-Ke Shih 				if (tssi_info->default_txagc_offset[RF_PATH_B])
4338e5efc4d5SPing-Ke Shih 					break;
4339e5efc4d5SPing-Ke Shih 			}
4340e5efc4d5SPing-Ke Shih 		}
4341e5efc4d5SPing-Ke Shih 	} else {
4342e5efc4d5SPing-Ke Shih 		/* SCAN_END */
4343e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT,
4344e5efc4d5SPing-Ke Shih 				       tssi_info->default_txagc_offset[RF_PATH_A]);
4345e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT,
4346e5efc4d5SPing-Ke Shih 				       tssi_info->default_txagc_offset[RF_PATH_B]);
4347e5efc4d5SPing-Ke Shih 
4348e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
4349e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
4350e5efc4d5SPing-Ke Shih 
4351e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT_EN, 0x0);
4352e5efc4d5SPing-Ke Shih 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT_EN, 0x1);
4353e5efc4d5SPing-Ke Shih 	}
4354e5efc4d5SPing-Ke Shih }
4355e5efc4d5SPing-Ke Shih 
rtw8852c_wifi_scan_notify(struct rtw89_dev * rtwdev,bool scan_start,enum rtw89_phy_idx phy_idx)4356e5efc4d5SPing-Ke Shih void rtw8852c_wifi_scan_notify(struct rtw89_dev *rtwdev,
4357e5efc4d5SPing-Ke Shih 			       bool scan_start, enum rtw89_phy_idx phy_idx)
4358e5efc4d5SPing-Ke Shih {
4359e5efc4d5SPing-Ke Shih 	if (scan_start)
4360e5efc4d5SPing-Ke Shih 		rtw8852c_tssi_default_txagc(rtwdev, phy_idx, true);
4361e5efc4d5SPing-Ke Shih 	else
4362e5efc4d5SPing-Ke Shih 		rtw8852c_tssi_default_txagc(rtwdev, phy_idx, false);
4363e5efc4d5SPing-Ke Shih }
4364