xref: /freebsd/sys/contrib/dev/rtw89/rtw8852c_rfk.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2019-2022  Realtek Corporation
3  */
4 
5 #include "coex.h"
6 #include "debug.h"
7 #include "phy.h"
8 #include "reg.h"
9 #include "rtw8852c.h"
10 #include "rtw8852c_rfk.h"
11 #include "rtw8852c_rfk_table.h"
12 #include "rtw8852c_table.h"
13 
14 struct rxck_def {
15 	u32 ctl;
16 	u32 en;
17 	u32 bw0;
18 	u32 bw1;
19 	u32 mul;
20 	u32 lp;
21 };
22 
23 #define _TSSI_DE_MASK GENMASK(21, 12)
24 static const u32 _tssi_de_cck_long[RF_PATH_NUM_8852C] = {0x5858, 0x7858};
25 static const u32 _tssi_de_cck_short[RF_PATH_NUM_8852C] = {0x5860, 0x7860};
26 static const u32 _tssi_de_mcs_20m[RF_PATH_NUM_8852C] = {0x5838, 0x7838};
27 static const u32 _tssi_de_mcs_40m[RF_PATH_NUM_8852C] = {0x5840, 0x7840};
28 static const u32 _tssi_de_mcs_80m[RF_PATH_NUM_8852C] = {0x5848, 0x7848};
29 static const u32 _tssi_de_mcs_80m_80m[RF_PATH_NUM_8852C] = {0x5850, 0x7850};
30 static const u32 _tssi_de_mcs_5m[RF_PATH_NUM_8852C] = {0x5828, 0x7828};
31 static const u32 _tssi_de_mcs_10m[RF_PATH_NUM_8852C] = {0x5830, 0x7830};
32 
33 static const u32 rtw8852c_backup_bb_regs[] = {
34 	0x8120, 0xc0d4, 0xc0d8, 0xc0e8, 0x8220, 0xc1d4, 0xc1d8, 0xc1e8
35 };
36 
37 static const u32 rtw8852c_backup_rf_regs[] = {
38 	0xdf, 0x5f, 0x8f, 0x97, 0xa3, 0x5, 0x10005
39 };
40 
41 #define BACKUP_BB_REGS_NR ARRAY_SIZE(rtw8852c_backup_bb_regs)
42 #define BACKUP_RF_REGS_NR ARRAY_SIZE(rtw8852c_backup_rf_regs)
43 
44 #define RXK_GROUP_NR 4
45 static const u32 _rxk_a6_idxrxgain[RXK_GROUP_NR] = {0x190, 0x196, 0x290, 0x316};
46 static const u32 _rxk_a6_idxattc2[RXK_GROUP_NR] = {0x00, 0x0, 0x00, 0x00};
47 static const u32 _rxk_a_idxrxgain[RXK_GROUP_NR] = {0x190, 0x198, 0x310, 0x318};
48 static const u32 _rxk_a_idxattc2[RXK_GROUP_NR] = {0x00, 0x00, 0x00, 0x00};
49 static const u32 _rxk_g_idxrxgain[RXK_GROUP_NR] = {0x252, 0x26c, 0x350, 0x360};
50 static const u32 _rxk_g_idxattc2[RXK_GROUP_NR] = {0x00, 0x07, 0x00, 0x3};
51 
52 #define TXK_GROUP_NR 3
53 static const u32 _txk_a6_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
54 static const u32 _txk_a6_track_range[TXK_GROUP_NR] = {0x6, 0x7, 0x7};
55 static const u32 _txk_a6_gain_bb[TXK_GROUP_NR] = {0x12, 0x09, 0x0e};
56 static const u32 _txk_a6_itqt[TXK_GROUP_NR] = {0x12, 0x12, 0x12};
57 static const u32 _txk_a_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
58 static const u32 _txk_a_track_range[TXK_GROUP_NR] = {0x5, 0x6, 0x7};
59 static const u32 _txk_a_gain_bb[TXK_GROUP_NR] = {0x12, 0x09, 0x0e};
60 static const u32 _txk_a_itqt[TXK_GROUP_NR] = {0x12, 0x12, 0x12};
61 static const u32 _txk_g_power_range[TXK_GROUP_NR] = {0x0, 0x0, 0x0};
62 static const u32 _txk_g_track_range[TXK_GROUP_NR] = {0x5, 0x6, 0x6};
63 static const u32 _txk_g_gain_bb[TXK_GROUP_NR] = {0x0e, 0x0a, 0x0e};
64 static const u32 _txk_g_itqt[TXK_GROUP_NR] = { 0x12, 0x12, 0x12};
65 
66 static const u32 dpk_par_regs[RTW89_DPK_RF_PATH][4] = {
67 	{0x8190, 0x8194, 0x8198, 0x81a4},
68 	{0x81a8, 0x81c4, 0x81c8, 0x81e8},
69 };
70 
71 static const u8 _dck_addr_bs[RF_PATH_NUM_8852C] = {0x0, 0x10};
72 static const u8 _dck_addr[RF_PATH_NUM_8852C] = {0xc, 0x1c};
73 
74 static const struct rxck_def _ck480M = {0x8, 0x2, 0x3, 0xf, 0x0, 0x9};
75 static const struct rxck_def _ck960M = {0x8, 0x2, 0x2, 0x8, 0x0, 0x9};
76 static const struct rxck_def _ck1920M = {0x8, 0x0, 0x2, 0x4, 0x6, 0x9};
77 
78 static u8 _kpath(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
79 {
80 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]dbcc_en: %x,  PHY%d\n",
81 		    rtwdev->dbcc_en, phy_idx);
82 
83 	if (!rtwdev->dbcc_en)
84 		return RF_AB;
85 
86 	if (phy_idx == RTW89_PHY_0)
87 		return RF_A;
88 	else
89 		return RF_B;
90 }
91 
92 static void _rfk_backup_bb_reg(struct rtw89_dev *rtwdev, u32 backup_bb_reg_val[])
93 {
94 	u32 i;
95 
96 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
97 		backup_bb_reg_val[i] =
98 			rtw89_phy_read32_mask(rtwdev, rtw8852c_backup_bb_regs[i],
99 					      MASKDWORD);
100 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
101 			    "[IQK]backup bb reg : %x, value =%x\n",
102 			    rtw8852c_backup_bb_regs[i], backup_bb_reg_val[i]);
103 	}
104 }
105 
106 static void _rfk_backup_rf_reg(struct rtw89_dev *rtwdev, u32 backup_rf_reg_val[],
107 			       u8 rf_path)
108 {
109 	u32 i;
110 
111 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
112 		backup_rf_reg_val[i] =
113 			rtw89_read_rf(rtwdev, rf_path,
114 				      rtw8852c_backup_rf_regs[i], RFREG_MASK);
115 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
116 			    "[IQK]backup rf S%d reg : %x, value =%x\n", rf_path,
117 			    rtw8852c_backup_rf_regs[i], backup_rf_reg_val[i]);
118 	}
119 }
120 
121 static void _rfk_restore_bb_reg(struct rtw89_dev *rtwdev, u32 backup_bb_reg_val[])
122 {
123 	u32 i;
124 
125 	for (i = 0; i < BACKUP_BB_REGS_NR; i++) {
126 		rtw89_phy_write32_mask(rtwdev, rtw8852c_backup_bb_regs[i],
127 				       MASKDWORD, backup_bb_reg_val[i]);
128 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
129 			    "[IQK]restore bb reg : %x, value =%x\n",
130 			    rtw8852c_backup_bb_regs[i], backup_bb_reg_val[i]);
131 	}
132 }
133 
134 static void _rfk_restore_rf_reg(struct rtw89_dev *rtwdev, u32 backup_rf_reg_val[],
135 				u8 rf_path)
136 {
137 	u32 i;
138 
139 	for (i = 0; i < BACKUP_RF_REGS_NR; i++) {
140 		rtw89_write_rf(rtwdev, rf_path, rtw8852c_backup_rf_regs[i],
141 			       RFREG_MASK, backup_rf_reg_val[i]);
142 
143 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
144 			    "[IQK]restore rf S%d reg: %x, value =%x\n", rf_path,
145 			    rtw8852c_backup_rf_regs[i], backup_rf_reg_val[i]);
146 	}
147 }
148 
149 static void _wait_rx_mode(struct rtw89_dev *rtwdev, u8 kpath)
150 {
151 	u8 path;
152 	u32 rf_mode;
153 	int ret;
154 
155 	for (path = 0; path < RF_PATH_MAX; path++) {
156 		if (!(kpath & BIT(path)))
157 			continue;
158 
159 		ret = read_poll_timeout_atomic(rtw89_read_rf, rf_mode, rf_mode != 2,
160 					       2, 5000, false, rtwdev, path, 0x00,
161 					       RR_MOD_MASK);
162 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
163 			    "[RFK] Wait S%d to Rx mode!! (ret = %d)\n",
164 			    path, ret);
165 	}
166 }
167 
168 static void _dack_dump(struct rtw89_dev *rtwdev)
169 {
170 	struct rtw89_dack_info *dack = &rtwdev->dack;
171 	u8 i;
172 	u8 t;
173 
174 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
175 		    "[DACK]S0 ADC_DCK ic = 0x%x, qc = 0x%x\n",
176 		    dack->addck_d[0][0], dack->addck_d[0][1]);
177 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
178 		    "[DACK]S1 ADC_DCK ic = 0x%x, qc = 0x%x\n",
179 		    dack->addck_d[1][0], dack->addck_d[1][1]);
180 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
181 		    "[DACK]S0 DAC_DCK ic = 0x%x, qc = 0x%x\n",
182 		    dack->dadck_d[0][0], dack->dadck_d[0][1]);
183 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
184 		    "[DACK]S1 DAC_DCK ic = 0x%x, qc = 0x%x\n",
185 		    dack->dadck_d[1][0], dack->dadck_d[1][1]);
186 
187 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
188 		    "[DACK]S0 biask ic = 0x%x, qc = 0x%x\n",
189 		    dack->biask_d[0][0], dack->biask_d[0][1]);
190 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
191 		    "[DACK]S1 biask ic = 0x%x, qc = 0x%x\n",
192 		    dack->biask_d[1][0], dack->biask_d[1][1]);
193 
194 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK ic:\n");
195 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
196 		t = dack->msbk_d[0][0][i];
197 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
198 	}
199 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 MSBK qc:\n");
200 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
201 		t = dack->msbk_d[0][1][i];
202 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
203 	}
204 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK ic:\n");
205 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
206 		t = dack->msbk_d[1][0][i];
207 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
208 	}
209 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 MSBK qc:\n");
210 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
211 		t = dack->msbk_d[1][1][i];
212 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x\n", t);
213 	}
214 }
215 
216 static void _addck_backup(struct rtw89_dev *rtwdev)
217 {
218 	struct rtw89_dack_info *dack = &rtwdev->dack;
219 
220 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x0);
221 	dack->addck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0,
222 						    B_ADDCKR0_A0);
223 	dack->addck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR0,
224 						    B_ADDCKR0_A1);
225 
226 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1, 0x0);
227 	dack->addck_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR1,
228 						    B_ADDCKR1_A0);
229 	dack->addck_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_ADDCKR1,
230 						    B_ADDCKR1_A1);
231 }
232 
233 static void _addck_reload(struct rtw89_dev *rtwdev)
234 {
235 	struct rtw89_dack_info *dack = &rtwdev->dack;
236 
237 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL1,
238 			       dack->addck_d[0][0]);
239 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RL0,
240 			       dack->addck_d[0][1]);
241 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0_RL, B_ADDCK0_RLS, 0x3);
242 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RL1,
243 			       dack->addck_d[1][0]);
244 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RL0,
245 			       dack->addck_d[1][1]);
246 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1_RL, B_ADDCK1_RLS, 0x3);
247 }
248 
249 static void _dack_backup_s0(struct rtw89_dev *rtwdev)
250 {
251 	struct rtw89_dack_info *dack = &rtwdev->dack;
252 	u8 i;
253 
254 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x1);
255 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
256 		rtw89_phy_write32_mask(rtwdev, R_DCOF0, B_DCOF0_V, i);
257 		dack->msbk_d[0][0][i] = rtw89_phy_read32_mask(rtwdev,
258 							      R_DACK_S0P2,
259 							      B_DACK_S0M0);
260 		rtw89_phy_write32_mask(rtwdev, R_DCOF8, B_DCOF8_V, i);
261 		dack->msbk_d[0][1][i] = rtw89_phy_read32_mask(rtwdev,
262 							      R_DACK_S0P3,
263 							      B_DACK_S0M1);
264 	}
265 	dack->biask_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS00,
266 						    B_DACK_BIAS00);
267 	dack->biask_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS01,
268 						    B_DACK_BIAS01);
269 	dack->dadck_d[0][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK00,
270 						    B_DACK_DADCK00);
271 	dack->dadck_d[0][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK01,
272 						    B_DACK_DADCK01);
273 }
274 
275 static void _dack_backup_s1(struct rtw89_dev *rtwdev)
276 {
277 	struct rtw89_dack_info *dack = &rtwdev->dack;
278 	u8 i;
279 
280 	rtw89_phy_write32_mask(rtwdev, R_P1_DBGMOD, B_P1_DBGMOD_ON, 0x1);
281 	for (i = 0; i < RTW89_DACK_MSBK_NR; i++) {
282 		rtw89_phy_write32_mask(rtwdev, R_DACK10, B_DACK10, i);
283 		dack->msbk_d[1][0][i] = rtw89_phy_read32_mask(rtwdev,
284 							      R_DACK10S,
285 							      B_DACK10S);
286 		rtw89_phy_write32_mask(rtwdev, R_DACK11, B_DACK11, i);
287 		dack->msbk_d[1][1][i] = rtw89_phy_read32_mask(rtwdev,
288 							      R_DACK11S,
289 							      B_DACK11S);
290 	}
291 	dack->biask_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS10,
292 						    B_DACK_BIAS10);
293 	dack->biask_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_BIAS11,
294 						    B_DACK_BIAS11);
295 	dack->dadck_d[1][0] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK10,
296 						    B_DACK_DADCK10);
297 	dack->dadck_d[1][1] = rtw89_phy_read32_mask(rtwdev, R_DACK_DADCK11,
298 						    B_DACK_DADCK11);
299 }
300 
301 static void _dack_reload_by_path(struct rtw89_dev *rtwdev,
302 				 enum rtw89_rf_path path, u8 index)
303 {
304 	struct rtw89_dack_info *dack = &rtwdev->dack;
305 	u32 idx_offset, path_offset;
306 	u32 val32, offset, addr;
307 	u8 i;
308 
309 	idx_offset = (index == 0 ? 0 : 0x14);
310 	path_offset = (path == RF_PATH_A ? 0 : 0x28);
311 	offset = idx_offset + path_offset;
312 
313 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_reload_defs_tbl);
314 
315 	/* msbk_d: 15/14/13/12 */
316 	val32 = 0x0;
317 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
318 		val32 |= dack->msbk_d[path][index][i + 12] << (i * 8);
319 	addr = 0xc200 + offset;
320 	rtw89_phy_write32(rtwdev, addr, val32);
321 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
322 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
323 
324 	/* msbk_d: 11/10/9/8 */
325 	val32 = 0x0;
326 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
327 		val32 |= dack->msbk_d[path][index][i + 8] << (i * 8);
328 	addr = 0xc204 + offset;
329 	rtw89_phy_write32(rtwdev, addr, val32);
330 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
331 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
332 
333 	/* msbk_d: 7/6/5/4 */
334 	val32 = 0x0;
335 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
336 		val32 |= dack->msbk_d[path][index][i + 4] << (i * 8);
337 	addr = 0xc208 + offset;
338 	rtw89_phy_write32(rtwdev, addr, val32);
339 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
340 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
341 
342 	/* msbk_d: 3/2/1/0 */
343 	val32 = 0x0;
344 	for (i = 0; i < RTW89_DACK_MSBK_NR / 4; i++)
345 		val32 |= dack->msbk_d[path][index][i] << (i * 8);
346 	addr = 0xc20c + offset;
347 	rtw89_phy_write32(rtwdev, addr, val32);
348 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0x%x=0x%x\n", addr,
349 		    rtw89_phy_read32_mask(rtwdev, addr, MASKDWORD));
350 
351 	/* dadak_d/biask_d */
352 	val32 = (dack->biask_d[path][index] << 22) |
353 		(dack->dadck_d[path][index] << 14);
354 	addr = 0xc210 + offset;
355 	rtw89_phy_write32(rtwdev, addr, val32);
356 	rtw89_phy_write32_set(rtwdev, addr, BIT(0));
357 }
358 
359 static void _dack_reload(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
360 {
361 	u8 i;
362 
363 	for (i = 0; i < 2; i++)
364 		_dack_reload_by_path(rtwdev, path, i);
365 }
366 
367 static void _addck(struct rtw89_dev *rtwdev)
368 {
369 	struct rtw89_dack_info *dack = &rtwdev->dack;
370 	u32 val;
371 	int ret;
372 
373 	/* S0 */
374 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x1);
375 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x1);
376 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_EN, 0x0);
377 	fsleep(1);
378 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0, 0x1);
379 
380 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
381 				       1, 10000, false, rtwdev, 0xc0fc, BIT(0));
382 	if (ret) {
383 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 ADDCK timeout\n");
384 		dack->addck_timeout[0] = true;
385 	}
386 
387 	rtw89_phy_write32_mask(rtwdev, R_ADDCK0, B_ADDCK0_RST, 0x0);
388 
389 	/* S1 */
390 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_RST, 0x1);
391 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_EN, 0x1);
392 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_EN, 0x0);
393 	udelay(1);
394 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1, 0x1);
395 
396 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
397 				       1, 10000, false, rtwdev, 0xc1fc, BIT(0));
398 	if (ret) {
399 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 ADDCK timeout\n");
400 		dack->addck_timeout[0] = true;
401 	}
402 	rtw89_phy_write32_mask(rtwdev, R_ADDCK1, B_ADDCK1_RST, 0x0);
403 }
404 
405 static void _dack_reset(struct rtw89_dev *rtwdev, u8 path)
406 {
407 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
408 				 &rtw8852c_dack_reset_defs_a_tbl,
409 				 &rtw8852c_dack_reset_defs_b_tbl);
410 }
411 
412 enum adc_ck {
413 	ADC_NA = 0,
414 	ADC_480M = 1,
415 	ADC_960M = 2,
416 	ADC_1920M = 3,
417 };
418 
419 enum dac_ck {
420 	DAC_40M = 0,
421 	DAC_80M = 1,
422 	DAC_120M = 2,
423 	DAC_160M = 3,
424 	DAC_240M = 4,
425 	DAC_320M = 5,
426 	DAC_480M = 6,
427 	DAC_960M = 7,
428 };
429 
430 enum rf_mode {
431 	RF_SHUT_DOWN = 0x0,
432 	RF_STANDBY = 0x1,
433 	RF_TX = 0x2,
434 	RF_RX = 0x3,
435 	RF_TXIQK = 0x4,
436 	RF_DPK = 0x5,
437 	RF_RXK1 = 0x6,
438 	RF_RXK2 = 0x7,
439 };
440 
441 static void rtw8852c_txck_force(struct rtw89_dev *rtwdev, u8 path, bool force,
442 				enum dac_ck ck)
443 {
444 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_ON, 0x0);
445 
446 	if (!force)
447 		return;
448 
449 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_VAL, ck);
450 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_TXCK_ON, 0x1);
451 }
452 
453 static void rtw8852c_rxck_force(struct rtw89_dev *rtwdev, u8 path, bool force,
454 				enum adc_ck ck)
455 {
456 	const struct rxck_def *def;
457 
458 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_ON, 0x0);
459 
460 	if (!force)
461 		return;
462 
463 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_VAL, ck);
464 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK | (path << 13), B_P0_RXCK_ON, 0x1);
465 
466 	switch (ck) {
467 	case ADC_480M:
468 		def = &_ck480M;
469 		break;
470 	case ADC_960M:
471 		def = &_ck960M;
472 		break;
473 	case ADC_1920M:
474 	default:
475 		def = &_ck1920M;
476 		break;
477 	}
478 
479 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_CTL, def->ctl);
480 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_EN, def->en);
481 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, def->bw0);
482 	rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, def->bw1);
483 	rtw89_phy_write32_mask(rtwdev, R_DRCK | (path << 8), B_DRCK_MUL, def->mul);
484 	rtw89_phy_write32_mask(rtwdev, R_ADCMOD | (path << 8), B_ADCMOD_LP, def->lp);
485 }
486 
487 static bool _check_dack_done(struct rtw89_dev *rtwdev, bool s0)
488 {
489 	if (s0) {
490 		if (rtw89_phy_read32_mask(rtwdev, R_DACK_S0P0, B_DACK_S0P0_OK) == 0 ||
491 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P1, B_DACK_S0P1_OK) == 0 ||
492 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P2, B_DACK_S0P2_OK) == 0 ||
493 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S0P3, B_DACK_S0P3_OK) == 0)
494 			return false;
495 	} else {
496 		if (rtw89_phy_read32_mask(rtwdev, R_DACK_S1P0, B_DACK_S1P0_OK) == 0 ||
497 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P1, B_DACK_S1P1_OK) == 0 ||
498 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P2, B_DACK_S1P2_OK) == 0 ||
499 		    rtw89_phy_read32_mask(rtwdev, R_DACK_S1P3, B_DACK_S1P3_OK) == 0)
500 			return false;
501 	}
502 
503 	return true;
504 }
505 
506 static void _dack_s0(struct rtw89_dev *rtwdev)
507 {
508 	struct rtw89_dack_info *dack = &rtwdev->dack;
509 	bool done;
510 	int ret;
511 
512 	rtw8852c_txck_force(rtwdev, RF_PATH_A, true, DAC_160M);
513 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_defs_s0_tbl);
514 
515 	_dack_reset(rtwdev, RF_PATH_A);
516 
517 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x1);
518 	ret = read_poll_timeout_atomic(_check_dack_done, done, done,
519 				       1, 10000, false, rtwdev, true);
520 	if (ret) {
521 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S0 DACK timeout\n");
522 		dack->msbk_timeout[0] = true;
523 	}
524 	rtw89_phy_write32_mask(rtwdev, R_DCOF1, B_DCOF1_S, 0x0);
525 	rtw8852c_txck_force(rtwdev, RF_PATH_A, false, DAC_960M);
526 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S0 DADCK\n");
527 
528 	_dack_backup_s0(rtwdev);
529 	_dack_reload(rtwdev, RF_PATH_A);
530 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW, B_P0_NRBW_DBG, 0x0);
531 }
532 
533 static void _dack_s1(struct rtw89_dev *rtwdev)
534 {
535 	struct rtw89_dack_info *dack = &rtwdev->dack;
536 	bool done;
537 	int ret;
538 
539 	rtw8852c_txck_force(rtwdev, RF_PATH_B, true, DAC_160M);
540 	rtw89_rfk_parser(rtwdev, &rtw8852c_dack_defs_s1_tbl);
541 
542 	_dack_reset(rtwdev, RF_PATH_B);
543 
544 	rtw89_phy_write32_mask(rtwdev, R_DACK1_K, B_DACK1_EN, 0x1);
545 	ret = read_poll_timeout_atomic(_check_dack_done, done, done,
546 				       1, 10000, false, rtwdev, false);
547 	if (ret) {
548 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]S1 DACK timeout\n");
549 		dack->msbk_timeout[0] = true;
550 	}
551 	rtw89_phy_write32_mask(rtwdev, R_DACK1_K, B_DACK1_EN, 0x0);
552 	rtw8852c_txck_force(rtwdev, RF_PATH_B, false, DAC_960M);
553 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]after S1 DADCK\n");
554 
555 	_dack_backup_s1(rtwdev);
556 	_dack_reload(rtwdev, RF_PATH_B);
557 	rtw89_phy_write32_mask(rtwdev, R_P1_DBGMOD, B_P1_DBGMOD_ON, 0x0);
558 }
559 
560 static void _dack(struct rtw89_dev *rtwdev)
561 {
562 	_dack_s0(rtwdev);
563 	_dack_s1(rtwdev);
564 }
565 
566 static void _drck(struct rtw89_dev *rtwdev)
567 {
568 	u32 val;
569 	int ret;
570 
571 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_EN, 0x1);
572 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val,
573 				       1, 10000, false, rtwdev, 0xc0c8, BIT(3));
574 	if (ret)
575 		rtw89_debug(rtwdev, RTW89_DBG_RFK,  "[DACK]DRCK timeout\n");
576 
577 	rtw89_rfk_parser(rtwdev, &rtw8852c_drck_defs_tbl);
578 
579 	val = rtw89_phy_read32_mask(rtwdev, R_DRCK_RES, B_DRCK_RES);
580 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_IDLE, 0x0);
581 	rtw89_phy_write32_mask(rtwdev, R_DRCK, B_DRCK_VAL, val);
582 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]0xc0c4 = 0x%x\n",
583 		    rtw89_phy_read32_mask(rtwdev, R_DRCK, MASKDWORD));
584 }
585 
586 static void _dac_cal(struct rtw89_dev *rtwdev, bool force)
587 {
588 	struct rtw89_dack_info *dack = &rtwdev->dack;
589 	u32 rf0_0, rf1_0;
590 	u8 phy_map = rtw89_btc_phymap(rtwdev, RTW89_PHY_0, RF_AB);
591 
592 	dack->dack_done = false;
593 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK b\n");
594 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK start!!!\n");
595 	rf0_0 = rtw89_read_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK);
596 	rf1_0 = rtw89_read_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK);
597 	_drck(rtwdev);
598 
599 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x0);
600 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x0);
601 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, 0x337e1);
602 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK, 0x337e1);
603 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_START);
604 	_addck(rtwdev);
605 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_STOP);
606 
607 	_addck_backup(rtwdev);
608 	_addck_reload(rtwdev);
609 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MODOPT, RFREG_MASK, 0x0);
610 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MODOPT, RFREG_MASK, 0x0);
611 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_START);
612 	_dack(rtwdev);
613 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_ONESHOT_STOP);
614 
615 	_dack_dump(rtwdev);
616 	dack->dack_done = true;
617 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_MOD, RFREG_MASK, rf0_0);
618 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_MOD, RFREG_MASK, rf1_0);
619 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_RSV1, RR_RSV1_RST, 0x1);
620 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x1);
621 	dack->dack_cnt++;
622 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DACK]DACK finish!!!\n");
623 }
624 
625 #define RTW8852C_NCTL_VER 0xd
626 #define RTW8852C_IQK_VER 0x2a
627 #define RTW8852C_IQK_SS 2
628 #define RTW8852C_IQK_THR_REK 8
629 #define RTW8852C_IQK_CFIR_GROUP_NR 4
630 
631 enum rtw8852c_iqk_type {
632 	ID_TXAGC,
633 	ID_G_FLOK_COARSE,
634 	ID_A_FLOK_COARSE,
635 	ID_G_FLOK_FINE,
636 	ID_A_FLOK_FINE,
637 	ID_FLOK_VBUFFER,
638 	ID_TXK,
639 	ID_RXAGC,
640 	ID_RXK,
641 	ID_NBTXK,
642 	ID_NBRXK,
643 };
644 
645 static void rtw8852c_disable_rxagc(struct rtw89_dev *rtwdev, u8 path, u8 en_rxgac)
646 {
647 	if (path == RF_PATH_A)
648 		rtw89_phy_write32_mask(rtwdev, R_P0_AGC_CTL, B_P0_AGC_EN, en_rxgac);
649 	else
650 		rtw89_phy_write32_mask(rtwdev, R_P1_AGC_CTL, B_P1_AGC_EN, en_rxgac);
651 }
652 
653 static void _iqk_rxk_setting(struct rtw89_dev *rtwdev, u8 path)
654 {
655 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
656 
657 	if (path == RF_PATH_A)
658 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0101);
659 	else
660 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x0202);
661 
662 	switch (iqk_info->iqk_bw[path]) {
663 	case RTW89_CHANNEL_WIDTH_20:
664 	case RTW89_CHANNEL_WIDTH_40:
665 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
666 		rtw8852c_rxck_force(rtwdev, path, true, ADC_480M);
667 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x0);
668 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
669 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
670 		break;
671 	case RTW89_CHANNEL_WIDTH_80:
672 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
673 		rtw8852c_rxck_force(rtwdev, path, true, ADC_960M);
674 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x1);
675 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
676 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
677 	break;
678 	case RTW89_CHANNEL_WIDTH_160:
679 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_DPD_GDIS, 0x1);
680 		rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
681 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_ACK_VAL, 0x2);
682 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_CKT, 0x1);
683 		rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13), B_P0_NRBW_DBG, 0x1);
684 		break;
685 	default:
686 		break;
687 	}
688 
689 	rtw89_rfk_parser(rtwdev, &rtw8852c_iqk_rxk_cfg_defs_tbl);
690 
691 	if (path == RF_PATH_A)
692 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x1101);
693 	else
694 		rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_RXK, 0x2202);
695 }
696 
697 static bool _iqk_check_cal(struct rtw89_dev *rtwdev, u8 path, u8 ktype)
698 {
699 	u32 tmp;
700 	u32 val;
701 	int ret;
702 
703 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
704 				       1, 8200, false, rtwdev, 0xbff8, MASKBYTE0);
705 	if (ret)
706 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]IQK timeout!!!\n");
707 
708 	rtw89_phy_write32_clr(rtwdev, R_NCTL_N1, MASKBYTE0);
709 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%x, ret=%d\n", path, ret);
710 	tmp = rtw89_phy_read32_mask(rtwdev, R_NCTL_RPT, MASKDWORD);
711 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
712 		    "[IQK]S%x, type= %x, 0x8008 = 0x%x\n", path, ktype, tmp);
713 
714 	return false;
715 }
716 
717 static bool _iqk_one_shot(struct rtw89_dev *rtwdev,
718 			  enum rtw89_phy_idx phy_idx, u8 path, u8 ktype)
719 {
720 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
721 	u32 addr_rfc_ctl = R_UPD_CLK + (path << 13);
722 	u32 iqk_cmd;
723 	bool fail;
724 
725 	switch (ktype) {
726 	case ID_TXAGC:
727 		iqk_cmd = 0x008 | (1 << (4 + path)) | (path << 1);
728 		break;
729 	case ID_A_FLOK_COARSE:
730 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
731 		iqk_cmd = 0x008 | (1 << (4 + path));
732 		break;
733 	case ID_G_FLOK_COARSE:
734 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
735 		iqk_cmd = 0x108 | (1 << (4 + path));
736 		break;
737 	case ID_A_FLOK_FINE:
738 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
739 		iqk_cmd = 0x508 | (1 << (4 + path));
740 		break;
741 	case ID_G_FLOK_FINE:
742 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
743 		iqk_cmd = 0x208 | (1 << (4 + path));
744 		break;
745 	case ID_FLOK_VBUFFER:
746 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
747 		iqk_cmd = 0x308 | (1 << (4 + path));
748 		break;
749 	case ID_TXK:
750 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
751 		iqk_cmd = 0x008 | (1 << (4 + path)) | ((0x8 + iqk_info->iqk_bw[path]) << 8);
752 		break;
753 	case ID_RXAGC:
754 		iqk_cmd = 0x508 | (1 << (4 + path)) | (path << 1);
755 		break;
756 	case ID_RXK:
757 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
758 		iqk_cmd = 0x008 | (1 << (4 + path)) | ((0xc + iqk_info->iqk_bw[path]) << 8);
759 		break;
760 	case ID_NBTXK:
761 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
762 		iqk_cmd = 0x408 | (1 << (4 + path));
763 		break;
764 	case ID_NBRXK:
765 		rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x1);
766 		iqk_cmd = 0x608 | (1 << (4 + path));
767 		break;
768 	default:
769 		return false;
770 	}
771 
772 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, iqk_cmd + 1);
773 	fsleep(15);
774 	fail = _iqk_check_cal(rtwdev, path, ktype);
775 	rtw89_phy_write32_mask(rtwdev, addr_rfc_ctl, 0x00000002, 0x0);
776 
777 	return fail;
778 }
779 
780 static bool _rxk_group_sel(struct rtw89_dev *rtwdev,
781 			   enum rtw89_phy_idx phy_idx, u8 path)
782 {
783 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
784 	bool fail;
785 	u32 tmp;
786 	u32 bkrf0;
787 	u8 gp;
788 
789 	bkrf0 = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_NBW);
790 	if (path == RF_PATH_B) {
791 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_IQKPLL, RR_IQKPLL_MOD, 0x3);
792 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_MOD);
793 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_AGH, tmp);
794 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_TXRX);
795 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_PLLCH, tmp);
796 	}
797 
798 	switch (iqk_info->iqk_band[path]) {
799 	case RTW89_BAND_2G:
800 	default:
801 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
802 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
803 		rtw89_write_rf(rtwdev, path, RR_RXG, RR_RXG_IQKMOD, 0x9);
804 		break;
805 	case RTW89_BAND_5G:
806 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
807 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
808 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x8);
809 		break;
810 	case RTW89_BAND_6G:
811 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
812 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
813 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x9);
814 		break;
815 	}
816 
817 	fsleep(10);
818 
819 	for (gp = 0; gp < RXK_GROUP_NR; gp++) {
820 		switch (iqk_info->iqk_band[path]) {
821 		case RTW89_BAND_2G:
822 		default:
823 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
824 				       _rxk_g_idxrxgain[gp]);
825 			rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_VOBUF,
826 				       _rxk_g_idxattc2[gp]);
827 			break;
828 		case RTW89_BAND_5G:
829 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
830 				       _rxk_a_idxrxgain[gp]);
831 			rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT,
832 				       _rxk_a_idxattc2[gp]);
833 			break;
834 		case RTW89_BAND_6G:
835 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG,
836 				       _rxk_a6_idxrxgain[gp]);
837 			rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT,
838 				       _rxk_a6_idxattc2[gp]);
839 			break;
840 		}
841 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
842 				       B_CFIR_LUT_SEL, 0x1);
843 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
844 				       B_CFIR_LUT_SET, 0x0);
845 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
846 				       B_CFIR_LUT_GP_V1, gp);
847 		fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
848 	}
849 
850 	if (path == RF_PATH_B)
851 		rtw89_write_rf(rtwdev, path, RR_IQKPLL, RR_IQKPLL_MOD, 0x0);
852 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, bkrf0);
853 
854 	if (fail) {
855 		iqk_info->nb_rxcfir[path] = 0x40000002;
856 		iqk_info->is_wb_rxiqk[path] = false;
857 	} else {
858 		iqk_info->nb_rxcfir[path] = 0x40000000;
859 		iqk_info->is_wb_rxiqk[path] = true;
860 	}
861 
862 	return false;
863 }
864 
865 static bool _iqk_nbrxk(struct rtw89_dev *rtwdev,
866 		       enum rtw89_phy_idx phy_idx, u8 path)
867 {
868 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
869 	bool fail;
870 	u32 tmp;
871 	u32 bkrf0;
872 	u8 gp = 0x2;
873 
874 	bkrf0 = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_NBW);
875 	if (path == RF_PATH_B) {
876 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_IQKPLL, RR_IQKPLL_MOD, 0x3);
877 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_MOD);
878 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_AGH, tmp);
879 		tmp = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CHTR, RR_CHTR_TXRX);
880 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV4, RR_RSV4_PLLCH, tmp);
881 	}
882 
883 	switch (iqk_info->iqk_band[path]) {
884 	case RTW89_BAND_2G:
885 	default:
886 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
887 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
888 		rtw89_write_rf(rtwdev, path, RR_RXG, RR_RXG_IQKMOD, 0x9);
889 		break;
890 	case RTW89_BAND_5G:
891 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
892 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
893 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x8);
894 		break;
895 	case RTW89_BAND_6G:
896 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, 0xc);
897 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, 0x0);
898 		rtw89_write_rf(rtwdev, path, RR_RXAE, RR_RXAE_IQKMOD, 0x9);
899 		break;
900 	}
901 
902 	fsleep(10);
903 
904 	switch (iqk_info->iqk_band[path]) {
905 	case RTW89_BAND_2G:
906 	default:
907 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_g_idxrxgain[gp]);
908 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_VOBUF, _rxk_g_idxattc2[gp]);
909 		break;
910 	case RTW89_BAND_5G:
911 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_a_idxrxgain[gp]);
912 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT, _rxk_a_idxattc2[gp]);
913 		break;
914 	case RTW89_BAND_6G:
915 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXG, _rxk_a6_idxrxgain[gp]);
916 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_IATT, _rxk_a6_idxattc2[gp]);
917 		break;
918 	}
919 
920 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SEL, 0x1);
921 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SET, 0x0);
922 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_GP_V1, gp);
923 	fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_RXK);
924 
925 	if (path == RF_PATH_B)
926 		rtw89_write_rf(rtwdev, path, RR_IQKPLL, RR_IQKPLL_MOD, 0x0);
927 
928 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_NBW, bkrf0);
929 
930 	if (fail)
931 		iqk_info->nb_rxcfir[path] =
932 			rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8),
933 					      MASKDWORD) | 0x2;
934 	else
935 		iqk_info->nb_rxcfir[path] = 0x40000002;
936 
937 	iqk_info->is_wb_rxiqk[path] = false;
938 	return fail;
939 }
940 
941 static bool _txk_group_sel(struct rtw89_dev *rtwdev,
942 			   enum rtw89_phy_idx phy_idx, u8 path)
943 {
944 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
945 	bool fail;
946 	u8 gp;
947 
948 	for (gp = 0; gp < TXK_GROUP_NR; gp++) {
949 		switch (iqk_info->iqk_band[path]) {
950 		case RTW89_BAND_2G:
951 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
952 				       _txk_g_power_range[gp]);
953 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
954 				       _txk_g_track_range[gp]);
955 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
956 				       _txk_g_gain_bb[gp]);
957 			rtw89_phy_write32_mask(rtwdev,
958 					       R_KIP_IQP + (path << 8),
959 					       MASKDWORD, _txk_g_itqt[gp]);
960 			break;
961 		case RTW89_BAND_5G:
962 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
963 				       _txk_a_power_range[gp]);
964 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
965 				       _txk_a_track_range[gp]);
966 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
967 				       _txk_a_gain_bb[gp]);
968 			rtw89_phy_write32_mask(rtwdev,
969 					       R_KIP_IQP + (path << 8),
970 					       MASKDWORD, _txk_a_itqt[gp]);
971 			break;
972 		case RTW89_BAND_6G:
973 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0,
974 				       _txk_a6_power_range[gp]);
975 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1,
976 				       _txk_a6_track_range[gp]);
977 			rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG,
978 				       _txk_a6_gain_bb[gp]);
979 			rtw89_phy_write32_mask(rtwdev,
980 					       R_KIP_IQP + (path << 8),
981 					       MASKDWORD, _txk_a6_itqt[gp]);
982 			break;
983 		default:
984 			break;
985 		}
986 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
987 				       B_CFIR_LUT_SEL, 0x1);
988 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
989 				       B_CFIR_LUT_SET, 0x1);
990 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
991 				       B_CFIR_LUT_G2, 0x0);
992 		rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8),
993 				       B_CFIR_LUT_GP, gp + 1);
994 		rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x00b);
995 		rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
996 		fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_TXK);
997 	}
998 
999 	if (fail) {
1000 		iqk_info->nb_txcfir[path] = 0x40000002;
1001 		iqk_info->is_wb_txiqk[path] = false;
1002 	} else {
1003 		iqk_info->nb_txcfir[path] = 0x40000000;
1004 		iqk_info->is_wb_txiqk[path] = true;
1005 	}
1006 
1007 	return fail;
1008 }
1009 
1010 static bool _iqk_nbtxk(struct rtw89_dev *rtwdev,
1011 		       enum rtw89_phy_idx phy_idx, u8 path)
1012 {
1013 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1014 	bool fail;
1015 	u8 gp = 0x2;
1016 
1017 	switch (iqk_info->iqk_band[path]) {
1018 	case RTW89_BAND_2G:
1019 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_g_power_range[gp]);
1020 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_g_track_range[gp]);
1021 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_g_gain_bb[gp]);
1022 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1023 				       MASKDWORD, _txk_g_itqt[gp]);
1024 		break;
1025 	case RTW89_BAND_5G:
1026 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_a_power_range[gp]);
1027 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_a_track_range[gp]);
1028 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_a_gain_bb[gp]);
1029 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1030 				       MASKDWORD, _txk_a_itqt[gp]);
1031 		break;
1032 	case RTW89_BAND_6G:
1033 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, _txk_a6_power_range[gp]);
1034 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, _txk_a6_track_range[gp]);
1035 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, _txk_a6_gain_bb[gp]);
1036 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1037 				       MASKDWORD, _txk_a6_itqt[gp]);
1038 	break;
1039 	default:
1040 		break;
1041 	}
1042 
1043 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SEL, 0x1);
1044 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_SET, 0x1);
1045 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_G2, 0x0);
1046 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_GP, gp + 1);
1047 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, 0x00b);
1048 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1049 	fail = _iqk_one_shot(rtwdev, phy_idx, path, ID_NBTXK);
1050 
1051 	if (!fail)
1052 		iqk_info->nb_txcfir[path] =
1053 			rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8),
1054 					      MASKDWORD) | 0x2;
1055 	else
1056 		iqk_info->nb_txcfir[path] = 0x40000002;
1057 
1058 	iqk_info->is_wb_txiqk[path] = false;
1059 
1060 	return fail;
1061 }
1062 
1063 static bool _lok_finetune_check(struct rtw89_dev *rtwdev, u8 path)
1064 {
1065 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
1066 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1067 	u8 idx = rfk_mcc->table_idx;
1068 	bool is_fail1,  is_fail2;
1069 	u32 val;
1070 	u32 core_i;
1071 	u32 core_q;
1072 	u32 vbuff_i;
1073 	u32 vbuff_q;
1074 
1075 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1076 	val = rtw89_read_rf(rtwdev,  path, RR_TXMO, RFREG_MASK);
1077 	core_i = FIELD_GET(RR_TXMO_COI, val);
1078 	core_q = FIELD_GET(RR_TXMO_COQ, val);
1079 
1080 	if (core_i < 0x2 || core_i > 0x1d || core_q < 0x2 || core_q > 0x1d)
1081 		is_fail1 = true;
1082 	else
1083 		is_fail1 = false;
1084 
1085 	iqk_info->lok_idac[idx][path] = val;
1086 
1087 	val = rtw89_read_rf(rtwdev, path, RR_LOKVB, RFREG_MASK);
1088 	vbuff_i = FIELD_GET(RR_LOKVB_COI, val);
1089 	vbuff_q = FIELD_GET(RR_LOKVB_COQ, val);
1090 
1091 	if (vbuff_i < 0x2 || vbuff_i > 0x3d || vbuff_q < 0x2 || vbuff_q > 0x3d)
1092 		is_fail2 = true;
1093 	else
1094 		is_fail2 = false;
1095 
1096 	iqk_info->lok_vbuf[idx][path] = val;
1097 
1098 	return is_fail1 || is_fail2;
1099 }
1100 
1101 static bool _iqk_lok(struct rtw89_dev *rtwdev,
1102 		     enum rtw89_phy_idx phy_idx, u8 path)
1103 {
1104 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1105 	u8 tmp_id = 0x0;
1106 	bool fail = false;
1107 	bool tmp = false;
1108 
1109 	/* Step 0: Init RF gain & tone idx= 8.25Mhz */
1110 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, B_IQK_DIF4_TXT, IQK_DF4_TXT_8_25MHZ);
1111 
1112 	/* Step 1  START: _lok_coarse_fine_wi_swap */
1113 	switch (iqk_info->iqk_band[path]) {
1114 	case RTW89_BAND_2G:
1115 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1116 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1117 				       B_KIP_IQP_IQSW, 0x9);
1118 		tmp_id = ID_G_FLOK_COARSE;
1119 		break;
1120 	case RTW89_BAND_5G:
1121 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1122 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1123 				       B_KIP_IQP_IQSW, 0x9);
1124 		tmp_id = ID_A_FLOK_COARSE;
1125 		break;
1126 	case RTW89_BAND_6G:
1127 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1128 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1129 				       B_KIP_IQP_IQSW, 0x9);
1130 		tmp_id = ID_A_FLOK_COARSE;
1131 		break;
1132 	default:
1133 		break;
1134 	}
1135 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, tmp_id);
1136 	iqk_info->lok_cor_fail[0][path] = tmp;
1137 
1138 	/* Step 2 */
1139 	switch (iqk_info->iqk_band[path]) {
1140 	case RTW89_BAND_2G:
1141 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1142 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1143 				       B_KIP_IQP_IQSW, 0x1b);
1144 		break;
1145 	case RTW89_BAND_5G:
1146 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1147 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1148 				       B_KIP_IQP_IQSW, 0x1b);
1149 		break;
1150 	case RTW89_BAND_6G:
1151 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1152 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1153 				       B_KIP_IQP_IQSW, 0x1b);
1154 		break;
1155 	default:
1156 		break;
1157 	}
1158 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, ID_FLOK_VBUFFER);
1159 
1160 	/* Step 3 */
1161 	switch (iqk_info->iqk_band[path]) {
1162 	case RTW89_BAND_2G:
1163 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1164 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1165 				       B_KIP_IQP_IQSW, 0x9);
1166 		tmp_id = ID_G_FLOK_FINE;
1167 		break;
1168 	case RTW89_BAND_5G:
1169 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1170 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1171 				       B_KIP_IQP_IQSW, 0x9);
1172 		tmp_id = ID_A_FLOK_FINE;
1173 		break;
1174 	case RTW89_BAND_6G:
1175 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x6);
1176 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1177 				       B_KIP_IQP_IQSW, 0x9);
1178 		tmp_id = ID_A_FLOK_FINE;
1179 		break;
1180 	default:
1181 		break;
1182 	}
1183 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, tmp_id);
1184 	iqk_info->lok_fin_fail[0][path] = tmp;
1185 
1186 	/* Step 4 large rf gain */
1187 	switch (iqk_info->iqk_band[path]) {
1188 	case RTW89_BAND_2G:
1189 	default:
1190 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1191 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1192 				       B_KIP_IQP_IQSW, 0x1b);
1193 		break;
1194 	case RTW89_BAND_5G:
1195 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1196 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1197 				       B_KIP_IQP_IQSW, 0x1b);
1198 		break;
1199 	case RTW89_BAND_6G:
1200 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0x12);
1201 		rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8),
1202 				       B_KIP_IQP_IQSW, 0x1b);
1203 		break;
1204 	}
1205 	tmp = _iqk_one_shot(rtwdev, phy_idx, path, ID_FLOK_VBUFFER);
1206 	fail = _lok_finetune_check(rtwdev, path);
1207 
1208 	return fail;
1209 }
1210 
1211 static void _iqk_txk_setting(struct rtw89_dev *rtwdev, u8 path)
1212 {
1213 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1214 
1215 	switch (iqk_info->iqk_band[path]) {
1216 	case RTW89_BAND_2G:
1217 	default:
1218 		rtw89_write_rf(rtwdev, path, RR_TXG1, RR_TXG1_ATT2, 0x0);
1219 		rtw89_write_rf(rtwdev, path, RR_TXG1, RR_TXG1_ATT1, 0x0);
1220 		rtw89_write_rf(rtwdev, path, RR_TXG2, RR_TXG2_ATT0, 0x1);
1221 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
1222 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
1223 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
1224 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
1225 			       0x403e0 | iqk_info->syn1to2);
1226 		fsleep(10);
1227 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
1228 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
1229 		break;
1230 	case RTW89_BAND_5G:
1231 		rtw89_write_rf(rtwdev, path, RR_TXATANK, RR_TXATANK_LBSW2, 0x0);
1232 		rtw89_write_rf(rtwdev, path, RR_TXPOW, RR_TXPOW_TXAS, 0x1);
1233 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
1234 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
1235 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
1236 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
1237 			       0x403e0 | iqk_info->syn1to2);
1238 		fsleep(10);
1239 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
1240 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
1241 		break;
1242 	case RTW89_BAND_6G:
1243 		rtw89_write_rf(rtwdev, path, RR_TXATANK, RR_TXATANK_LBSW2, 0x0);
1244 		rtw89_write_rf(rtwdev, path, RR_TXPOW, RR_TXPOW_TXAS, 0x1);
1245 		rtw89_write_rf(rtwdev, path, RR_TXA2, RR_TXA2_LDO, 0xf);
1246 		rtw89_write_rf(rtwdev, path, RR_TXGA, RR_TXGA_LOK_EXT, 0x0);
1247 		rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x1);
1248 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
1249 			       0x403e0  | iqk_info->syn1to2);
1250 		fsleep(10);
1251 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
1252 		rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x6);
1253 		break;
1254 	}
1255 }
1256 
1257 static void _iqk_info_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1258 			  u8 path)
1259 {
1260 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1261 	u32 tmp;
1262 	bool flag;
1263 
1264 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_thermal = %lu\n", path,
1265 		    ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]));
1266 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_LOK_COR_fail= %d\n", path,
1267 		    iqk_info->lok_cor_fail[0][path]);
1268 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_LOK_FIN_fail= %d\n", path,
1269 		    iqk_info->lok_fin_fail[0][path]);
1270 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_TXIQK_fail = %d\n", path,
1271 		    iqk_info->iqk_tx_fail[0][path]);
1272 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]S%d_RXIQK_fail= %d,\n", path,
1273 		    iqk_info->iqk_rx_fail[0][path]);
1274 
1275 	flag = iqk_info->lok_cor_fail[0][path];
1276 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FCOR << (path * 4), flag);
1277 	flag = iqk_info->lok_fin_fail[0][path];
1278 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FFIN << (path * 4), flag);
1279 	flag = iqk_info->iqk_tx_fail[0][path];
1280 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_FTX << (path * 4), flag);
1281 	flag = iqk_info->iqk_rx_fail[0][path];
1282 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_F_RX << (path * 4), flag);
1283 
1284 	tmp = rtw89_phy_read32_mask(rtwdev, R_IQK_RES + (path << 8), MASKDWORD);
1285 	iqk_info->bp_iqkenable[path] = tmp;
1286 	tmp = rtw89_phy_read32_mask(rtwdev, R_TXIQC + (path << 8), MASKDWORD);
1287 	iqk_info->bp_txkresult[path] = tmp;
1288 	tmp = rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD);
1289 	iqk_info->bp_rxkresult[path] = tmp;
1290 
1291 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_KCNT,
1292 			       iqk_info->iqk_times);
1293 
1294 	tmp = rtw89_phy_read32_mask(rtwdev, R_IQKINF, B_IQKINF_FAIL << (path * 4));
1295 	if (tmp != 0x0)
1296 		iqk_info->iqk_fail_cnt++;
1297 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_FCNT << (path * 4),
1298 			       iqk_info->iqk_fail_cnt);
1299 }
1300 
1301 static void _iqk_by_path(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, u8 path)
1302 {
1303 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1304 
1305 	_iqk_txk_setting(rtwdev, path);
1306 	iqk_info->lok_fail[path] = _iqk_lok(rtwdev, phy_idx, path);
1307 
1308 	if (iqk_info->is_nbiqk)
1309 		iqk_info->iqk_tx_fail[0][path] = _iqk_nbtxk(rtwdev, phy_idx, path);
1310 	else
1311 		iqk_info->iqk_tx_fail[0][path] = _txk_group_sel(rtwdev, phy_idx, path);
1312 
1313 	_iqk_rxk_setting(rtwdev, path);
1314 	if (iqk_info->is_nbiqk)
1315 		iqk_info->iqk_rx_fail[0][path] = _iqk_nbrxk(rtwdev, phy_idx, path);
1316 	else
1317 		iqk_info->iqk_rx_fail[0][path] = _rxk_group_sel(rtwdev, phy_idx, path);
1318 
1319 	_iqk_info_iqk(rtwdev, phy_idx, path);
1320 }
1321 
1322 static void _iqk_get_ch_info(struct rtw89_dev *rtwdev,
1323 			     enum rtw89_phy_idx phy, u8 path)
1324 {
1325 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1326 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1327 
1328 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1329 
1330 	iqk_info->iqk_band[path] = chan->band_type;
1331 	iqk_info->iqk_bw[path] = chan->band_width;
1332 	iqk_info->iqk_ch[path] = chan->channel;
1333 
1334 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1335 		    "[IQK]iqk_info->iqk_band[%x] = 0x%x\n", path,
1336 		    iqk_info->iqk_band[path]);
1337 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]iqk_info->iqk_bw[%x] = 0x%x\n",
1338 		    path, iqk_info->iqk_bw[path]);
1339 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]iqk_info->iqk_ch[%x] = 0x%x\n",
1340 		    path, iqk_info->iqk_ch[path]);
1341 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1342 		    "[IQK]S%d (PHY%d): / DBCC %s/ %s/ CH%d/ %s\n", path, phy,
1343 		    rtwdev->dbcc_en ? "on" : "off",
1344 		    iqk_info->iqk_band[path] == 0 ? "2G" :
1345 		    iqk_info->iqk_band[path] == 1 ? "5G" : "6G",
1346 		    iqk_info->iqk_ch[path],
1347 		    iqk_info->iqk_bw[path] == 0 ? "20M" :
1348 		    iqk_info->iqk_bw[path] == 1 ? "40M" : "80M");
1349 	if (!rtwdev->dbcc_en)
1350 		iqk_info->syn1to2 = 0x1;
1351 	else
1352 		iqk_info->syn1to2 = 0x3;
1353 
1354 	rtw89_phy_write32_mask(rtwdev, R_IQKINF, B_IQKINF_VER, RTW8852C_IQK_VER);
1355 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_BAND << (path * 16),
1356 			       iqk_info->iqk_band[path]);
1357 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_BW << (path * 16),
1358 			       iqk_info->iqk_bw[path]);
1359 	rtw89_phy_write32_mask(rtwdev, R_IQKCH, B_IQKCH_CH << (path * 16),
1360 			       iqk_info->iqk_ch[path]);
1361 
1362 	rtw89_phy_write32_mask(rtwdev, R_IQKINF2, B_IQKINF2_NCTLV, RTW8852C_NCTL_VER);
1363 }
1364 
1365 static void _iqk_start_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx,
1366 			   u8 path)
1367 {
1368 	_iqk_by_path(rtwdev, phy_idx, path);
1369 }
1370 
1371 static void _iqk_restore(struct rtw89_dev *rtwdev, u8 path)
1372 {
1373 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1374 	bool fail;
1375 
1376 	rtw89_phy_write32_mask(rtwdev, R_TXIQC + (path << 8), MASKDWORD,
1377 			       iqk_info->nb_txcfir[path]);
1378 	rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD,
1379 			       iqk_info->nb_rxcfir[path]);
1380 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD,
1381 			       0x00001219 + (path << 4));
1382 	fsleep(200);
1383 	fail = _iqk_check_cal(rtwdev, path, 0x12);
1384 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK] restore fail  = %x\n", fail);
1385 
1386 	rtw89_phy_write32_mask(rtwdev, R_NCTL_N1, B_NCTL_N1_CIP, 0x00);
1387 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000000);
1388 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x80000000);
1389 
1390 	rtw89_write_rf(rtwdev, path, RR_LUTWE, RR_LUTWE_LOK, 0x0);
1391 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
1392 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
1393 }
1394 
1395 static void _iqk_afebb_restore(struct rtw89_dev *rtwdev,
1396 			       enum rtw89_phy_idx phy_idx, u8 path)
1397 {
1398 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
1399 				 &rtw8852c_iqk_afebb_restore_defs_a_tbl,
1400 				 &rtw8852c_iqk_afebb_restore_defs_b_tbl);
1401 
1402 	rtw8852c_disable_rxagc(rtwdev, path, 0x1);
1403 }
1404 
1405 static void _iqk_preset(struct rtw89_dev *rtwdev, u8 path)
1406 {
1407 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
1408 	u8 idx = 0;
1409 
1410 	idx = rfk_mcc->table_idx;
1411 	rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8), B_COEF_SEL_IQC, idx);
1412 	rtw89_phy_write32_mask(rtwdev, R_CFIR_LUT + (path << 8), B_CFIR_LUT_G3, idx);
1413 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1414 	rtw89_phy_write32_mask(rtwdev, R_NCTL_RPT, MASKDWORD, 0x00000080);
1415 	rtw89_phy_write32_mask(rtwdev, R_KIP_SYSCFG, MASKDWORD, 0x81ff010a);
1416 }
1417 
1418 static void _iqk_macbb_setting(struct rtw89_dev *rtwdev,
1419 			       enum rtw89_phy_idx phy_idx, u8 path)
1420 {
1421 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===> %s\n", __func__);
1422 
1423 	/* 01_BB_AFE_for DPK_S0_20210820 */
1424 	rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x0);
1425 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
1426 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
1427 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
1428 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
1429 
1430 	/* disable rxgac */
1431 	rtw8852c_disable_rxagc(rtwdev, path, 0x0);
1432 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), MASKDWORD, 0xf801fffd);
1433 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DPD_DIS, 0x1);
1434 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DAC_VAL, 0x1);
1435 
1436 	rtw8852c_txck_force(rtwdev, path, true, DAC_960M);
1437 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_DPD_GDIS, 0x1);
1438 
1439 	rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
1440 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK | (path << 13), B_ACK_VAL, 0x2);
1441 
1442 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW | (path << 13), B_P0_NRBW_DBG, 0x1);
1443 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, B_ANAPAR_PW15, 0x1f);
1444 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, B_ANAPAR_PW15, 0x13);
1445 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, B_ANAPAR_15, 0x0001);
1446 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, B_ANAPAR_15, 0x0041);
1447 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x1);
1448 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x1);
1449 }
1450 
1451 static void _rck(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
1452 {
1453 	u32 rf_reg5, rck_val = 0;
1454 	u32 val;
1455 	int ret;
1456 
1457 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] ====== S%d RCK ======\n", path);
1458 
1459 	rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
1460 
1461 	rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1462 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
1463 
1464 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RF0x00 = 0x%x\n",
1465 		    rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
1466 
1467 	/* RCK trigger */
1468 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, 0x00240);
1469 
1470 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val, 2, 20,
1471 				       false, rtwdev, path, 0x1c, BIT(3));
1472 	if (ret)
1473 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RCK] RCK timeout\n");
1474 
1475 	rck_val = rtw89_read_rf(rtwdev, path, RR_RCKC, RR_RCKC_CA);
1476 	rtw89_write_rf(rtwdev, path, RR_RCKC, RFREG_MASK, rck_val);
1477 
1478 	rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
1479 
1480 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1481 		    "[RCK] RF 0x1b / 0x1c = 0x%x / 0x%x\n",
1482 		    rtw89_read_rf(rtwdev, path, RR_RCKC, RFREG_MASK),
1483 		    rtw89_read_rf(rtwdev, path, RR_RCKS, RFREG_MASK));
1484 }
1485 
1486 static void _iqk_init(struct rtw89_dev *rtwdev)
1487 {
1488 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1489 	u8 ch, path;
1490 
1491 	rtw89_phy_write32_clr(rtwdev, R_IQKINF, MASKDWORD);
1492 	if (iqk_info->is_iqk_init)
1493 		return;
1494 
1495 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]===>%s\n", __func__);
1496 	iqk_info->is_iqk_init = true;
1497 	iqk_info->is_nbiqk = false;
1498 	iqk_info->iqk_fft_en = false;
1499 	iqk_info->iqk_sram_en = false;
1500 	iqk_info->iqk_cfir_en = false;
1501 	iqk_info->iqk_xym_en = false;
1502 	iqk_info->iqk_times = 0x0;
1503 
1504 	for (ch = 0; ch < RTW89_IQK_CHS_NR; ch++) {
1505 		iqk_info->iqk_channel[ch] = 0x0;
1506 		for (path = 0; path < RTW8852C_IQK_SS; path++) {
1507 			iqk_info->lok_cor_fail[ch][path] = false;
1508 			iqk_info->lok_fin_fail[ch][path] = false;
1509 			iqk_info->iqk_tx_fail[ch][path] = false;
1510 			iqk_info->iqk_rx_fail[ch][path] = false;
1511 			iqk_info->iqk_mcc_ch[ch][path] = 0x0;
1512 			iqk_info->iqk_table_idx[path] = 0x0;
1513 		}
1514 	}
1515 }
1516 
1517 static void _doiqk(struct rtw89_dev *rtwdev, bool force,
1518 		   enum rtw89_phy_idx phy_idx, u8 path)
1519 {
1520 	struct rtw89_iqk_info *iqk_info = &rtwdev->iqk;
1521 	u32 backup_bb_val[BACKUP_BB_REGS_NR];
1522 	u32 backup_rf_val[RTW8852C_IQK_SS][BACKUP_RF_REGS_NR];
1523 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, RF_AB);
1524 
1525 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_START);
1526 
1527 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1528 		    "[IQK]==========IQK start!!!!!==========\n");
1529 	iqk_info->iqk_times++;
1530 	iqk_info->version = RTW8852C_IQK_VER;
1531 
1532 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[IQK]Test Ver 0x%x\n", iqk_info->version);
1533 	_iqk_get_ch_info(rtwdev, phy_idx, path);
1534 	_rfk_backup_bb_reg(rtwdev, backup_bb_val);
1535 	_rfk_backup_rf_reg(rtwdev, backup_rf_val[path], path);
1536 	_iqk_macbb_setting(rtwdev, phy_idx, path);
1537 	_iqk_preset(rtwdev, path);
1538 	_iqk_start_iqk(rtwdev, phy_idx, path);
1539 	_iqk_restore(rtwdev, path);
1540 	_iqk_afebb_restore(rtwdev, phy_idx, path);
1541 	_rfk_restore_bb_reg(rtwdev, backup_bb_val);
1542 	_rfk_restore_rf_reg(rtwdev, backup_rf_val[path], path);
1543 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_ONESHOT_STOP);
1544 }
1545 
1546 static void _iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx, bool force)
1547 {
1548 	switch (_kpath(rtwdev, phy_idx)) {
1549 	case RF_A:
1550 		_doiqk(rtwdev, force, phy_idx, RF_PATH_A);
1551 		break;
1552 	case RF_B:
1553 		_doiqk(rtwdev, force, phy_idx, RF_PATH_B);
1554 		break;
1555 	case RF_AB:
1556 		_doiqk(rtwdev, force, phy_idx, RF_PATH_A);
1557 		_doiqk(rtwdev, force, phy_idx, RF_PATH_B);
1558 		break;
1559 	default:
1560 		break;
1561 	}
1562 }
1563 
1564 static void _rx_dck_value_rewrite(struct rtw89_dev *rtwdev, u8 path, u8 addr,
1565 				  u8 val_i, u8 val_q)
1566 {
1567 	u32 ofst_val;
1568 
1569 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1570 		    "[RX_DCK] rewrite val_i = 0x%x, val_q = 0x%x\n", val_i, val_q);
1571 
1572 	/* val_i and val_q are 7 bits, and target is 6 bits. */
1573 	ofst_val = u32_encode_bits(val_q >> 1, RR_LUTWD0_MB) |
1574 		   u32_encode_bits(val_i >> 1, RR_LUTWD0_LB);
1575 
1576 	rtw89_write_rf(rtwdev, path, RR_LUTPLL, RR_CAL_RW, 0x1);
1577 	rtw89_write_rf(rtwdev, path, RR_RFC, RR_WCAL, 0x1);
1578 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_FINE, 0x1);
1579 	rtw89_write_rf(rtwdev, path, RR_LUTWA, MASKBYTE0, addr);
1580 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, ofst_val);
1581 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, ofst_val);
1582 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_FINE, 0x0);
1583 	rtw89_write_rf(rtwdev, path, RR_RFC, RR_WCAL, 0x0);
1584 	rtw89_write_rf(rtwdev, path, RR_LUTPLL, RR_CAL_RW, 0x0);
1585 
1586 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] Final val_i = 0x%x, val_q = 0x%x\n",
1587 		    u32_get_bits(ofst_val, RR_LUTWD0_LB) << 1,
1588 		    u32_get_bits(ofst_val, RR_LUTWD0_MB) << 1);
1589 }
1590 
1591 static bool _rx_dck_rek_check(struct rtw89_dev *rtwdev, u8 path)
1592 {
1593 	u8 i_even_bs, q_even_bs;
1594 	u8 i_odd_bs, q_odd_bs;
1595 	u8 i_even, q_even;
1596 	u8 i_odd, q_odd;
1597 	const u8 th = 10;
1598 	u8 i;
1599 
1600 	for (i = 0; i < RF_PATH_NUM_8852C; i++) {
1601 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i]);
1602 		i_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1603 		q_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1604 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1605 			    "[RX_DCK] Gain[0x%x] i_even_bs/ q_even_bs = 0x%x/ 0x%x\n",
1606 			    _dck_addr_bs[i], i_even_bs, q_even_bs);
1607 
1608 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i]);
1609 		i_even = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1610 		q_even = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1611 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1612 			    "[RX_DCK] Gain[0x%x] i_even/ q_even = 0x%x/ 0x%x\n",
1613 			    _dck_addr[i], i_even, q_even);
1614 
1615 		if (abs(i_even_bs - i_even) > th || abs(q_even_bs - q_even) > th)
1616 			return true;
1617 
1618 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i] + 1);
1619 		i_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1620 		q_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1621 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1622 			    "[RX_DCK] Gain[0x%x] i_odd_bs/ q_odd_bs = 0x%x/ 0x%x\n",
1623 			    _dck_addr_bs[i] + 1, i_odd_bs, q_odd_bs);
1624 
1625 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i] + 1);
1626 		i_odd = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1627 		q_odd = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1628 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1629 			    "[RX_DCK] Gain[0x%x] i_odd/ q_odd = 0x%x/ 0x%x\n",
1630 			    _dck_addr[i] + 1, i_odd, q_odd);
1631 
1632 		if (abs(i_odd_bs - i_odd) > th || abs(q_odd_bs - q_odd) > th)
1633 			return true;
1634 	}
1635 
1636 	return false;
1637 }
1638 
1639 static void _rx_dck_fix_if_need(struct rtw89_dev *rtwdev, u8 path, u8 addr,
1640 				u8 val_i_bs, u8 val_q_bs, u8 val_i, u8 val_q)
1641 {
1642 	const u8 th = 10;
1643 
1644 	if ((abs(val_i_bs - val_i) < th) && (abs(val_q_bs - val_q) <= th)) {
1645 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] offset check PASS!!\n");
1646 		return;
1647 	}
1648 
1649 	if (abs(val_i_bs - val_i) > th) {
1650 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1651 			    "[RX_DCK] val_i over TH (0x%x / 0x%x)\n", val_i_bs, val_i);
1652 		val_i = val_i_bs;
1653 	}
1654 
1655 	if (abs(val_q_bs - val_q) > th) {
1656 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1657 			    "[RX_DCK] val_q over TH (0x%x / 0x%x)\n", val_q_bs, val_q);
1658 		val_q = val_q_bs;
1659 	}
1660 
1661 	_rx_dck_value_rewrite(rtwdev, path, addr, val_i, val_q);
1662 }
1663 
1664 static void _rx_dck_recover(struct rtw89_dev *rtwdev, u8 path)
1665 {
1666 	u8 i_even_bs, q_even_bs;
1667 	u8 i_odd_bs, q_odd_bs;
1668 	u8 i_even, q_even;
1669 	u8 i_odd, q_odd;
1670 	u8 i;
1671 
1672 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] ===> recovery\n");
1673 
1674 	for (i = 0; i < RF_PATH_NUM_8852C; i++) {
1675 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i]);
1676 		i_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1677 		q_even_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1678 
1679 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr_bs[i] + 1);
1680 		i_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1681 		q_odd_bs = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1682 
1683 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1684 			    "[RX_DCK] Gain[0x%x] i_even_bs/ q_even_bs = 0x%x/ 0x%x\n",
1685 			    _dck_addr_bs[i], i_even_bs, q_even_bs);
1686 
1687 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i]);
1688 		i_even = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1689 		q_even = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1690 
1691 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1692 			    "[RX_DCK] Gain[0x%x] i_even/ q_even = 0x%x/ 0x%x\n",
1693 			    _dck_addr[i], i_even, q_even);
1694 		_rx_dck_fix_if_need(rtwdev, path, _dck_addr[i],
1695 				    i_even_bs, q_even_bs, i_even, q_even);
1696 
1697 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1698 			    "[RX_DCK] Gain[0x%x] i_odd_bs/ q_odd_bs = 0x%x/ 0x%x\n",
1699 			    _dck_addr_bs[i] + 1, i_odd_bs, q_odd_bs);
1700 
1701 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_DCK, _dck_addr[i] + 1);
1702 		i_odd = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_TIA);
1703 		q_odd = rtw89_read_rf(rtwdev, path, RR_DCK1, RR_DCK1_TIA);
1704 
1705 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1706 			    "[RX_DCK] Gain[0x%x] i_odd/ q_odd = 0x%x/ 0x%x\n",
1707 			    _dck_addr[i] + 1, i_odd, q_odd);
1708 		_rx_dck_fix_if_need(rtwdev, path, _dck_addr[i] + 1,
1709 				    i_odd_bs, q_odd_bs, i_odd, q_odd);
1710 	}
1711 }
1712 
1713 static void _rx_dck_toggle(struct rtw89_dev *rtwdev, u8 path)
1714 {
1715 	int ret;
1716 	u32 val;
1717 
1718 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
1719 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x1);
1720 
1721 	ret = read_poll_timeout_atomic(rtw89_read_rf, val, val,
1722 				       2, 2000, false, rtwdev, path,
1723 				       RR_DCK1, RR_DCK1_DONE);
1724 	if (ret)
1725 		rtw89_warn(rtwdev, "[RX_DCK] S%d RXDCK timeout\n", path);
1726 	else
1727 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] S%d RXDCK finish\n", path);
1728 
1729 	rtw89_write_rf(rtwdev, path, RR_DCK, RR_DCK_LV, 0x0);
1730 }
1731 
1732 static void _set_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, u8 path,
1733 			bool is_afe)
1734 {
1735 	u8 res;
1736 
1737 	rtw89_write_rf(rtwdev, path, RR_DCK1, RR_DCK1_CLR, 0x0);
1738 
1739 	_rx_dck_toggle(rtwdev, path);
1740 	if (rtw89_read_rf(rtwdev, path, RR_DCKC, RR_DCKC_CHK) == 0)
1741 		return;
1742 	res = rtw89_read_rf(rtwdev, path, RR_DCK, RR_DCK_DONE);
1743 	if (res > 1) {
1744 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_IDAC, res);
1745 		_rx_dck_toggle(rtwdev, path);
1746 		rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_IDAC, 0x1);
1747 	}
1748 }
1749 
1750 static
1751 u8 _rx_dck_channel_calc(struct rtw89_dev *rtwdev, const struct rtw89_chan *chan)
1752 {
1753 	u8 target_ch = 0;
1754 
1755 	if (chan->band_type == RTW89_BAND_5G) {
1756 		if (chan->channel >= 36 && chan->channel <= 64) {
1757 			target_ch = 100;
1758 		} else if (chan->channel >= 100 && chan->channel <= 144) {
1759 			target_ch = chan->channel + 32;
1760 			if (target_ch > 144)
1761 				target_ch = chan->channel + 33;
1762 		} else if (chan->channel >= 149 && chan->channel <= 177) {
1763 			target_ch = chan->channel - 33;
1764 		}
1765 	} else if (chan->band_type == RTW89_BAND_6G) {
1766 		if (chan->channel >= 1 && chan->channel <= 125)
1767 			target_ch = chan->channel + 32;
1768 		else
1769 			target_ch = chan->channel - 32;
1770 	} else {
1771 		target_ch = chan->channel;
1772 	}
1773 
1774 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1775 		    "[RX_DCK] cur_ch / target_ch = %d / %d\n",
1776 		    chan->channel, target_ch);
1777 
1778 	return target_ch;
1779 }
1780 
1781 #define RTW8852C_RF_REL_VERSION 34
1782 #define RTW8852C_DPK_VER 0xf
1783 #define RTW8852C_DPK_TH_AVG_NUM 4
1784 #define RTW8852C_DPK_RF_PATH 2
1785 #define RTW8852C_DPK_KIP_REG_NUM 7
1786 #define RTW8852C_DPK_RXSRAM_DBG 0
1787 
1788 enum rtw8852c_dpk_id {
1789 	LBK_RXIQK	= 0x06,
1790 	SYNC		= 0x10,
1791 	MDPK_IDL	= 0x11,
1792 	MDPK_MPA	= 0x12,
1793 	GAIN_LOSS	= 0x13,
1794 	GAIN_CAL	= 0x14,
1795 	DPK_RXAGC	= 0x15,
1796 	KIP_PRESET	= 0x16,
1797 	KIP_RESTORE	= 0x17,
1798 	DPK_TXAGC	= 0x19,
1799 	D_KIP_PRESET	= 0x28,
1800 	D_TXAGC		= 0x29,
1801 	D_RXAGC		= 0x2a,
1802 	D_SYNC		= 0x2b,
1803 	D_GAIN_LOSS	= 0x2c,
1804 	D_MDPK_IDL	= 0x2d,
1805 	D_GAIN_NORM	= 0x2f,
1806 	D_KIP_THERMAL	= 0x30,
1807 	D_KIP_RESTORE	= 0x31
1808 };
1809 
1810 #define DPK_TXAGC_LOWER 0x2e
1811 #define DPK_TXAGC_UPPER 0x3f
1812 #define DPK_TXAGC_INVAL 0xff
1813 
1814 enum dpk_agc_step {
1815 	DPK_AGC_STEP_SYNC_DGAIN,
1816 	DPK_AGC_STEP_GAIN_LOSS_IDX,
1817 	DPK_AGC_STEP_GL_GT_CRITERION,
1818 	DPK_AGC_STEP_GL_LT_CRITERION,
1819 	DPK_AGC_STEP_SET_TX_GAIN,
1820 };
1821 
1822 enum dpk_pas_result {
1823 	DPK_PAS_NOR,
1824 	DPK_PAS_GT,
1825 	DPK_PAS_LT,
1826 };
1827 
1828 static void _rf_direct_cntrl(struct rtw89_dev *rtwdev,
1829 			     enum rtw89_rf_path path, bool is_bybb)
1830 {
1831 	if (is_bybb)
1832 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x1);
1833 	else
1834 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
1835 }
1836 
1837 static void _dpk_onoff(struct rtw89_dev *rtwdev,
1838 		       enum rtw89_rf_path path, bool off);
1839 
1840 static void _dpk_bkup_kip(struct rtw89_dev *rtwdev, const u32 reg[],
1841 			  u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM], u8 path)
1842 {
1843 	u8 i;
1844 
1845 	for (i = 0; i < RTW8852C_DPK_KIP_REG_NUM; i++) {
1846 		reg_bkup[path][i] =
1847 			rtw89_phy_read32_mask(rtwdev, reg[i] + (path << 8), MASKDWORD);
1848 
1849 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Backup 0x%x = %x\n",
1850 			    reg[i] + (path << 8), reg_bkup[path][i]);
1851 	}
1852 }
1853 
1854 static void _dpk_reload_kip(struct rtw89_dev *rtwdev, const u32 reg[],
1855 			    u32 reg_bkup[][RTW8852C_DPK_KIP_REG_NUM], u8 path)
1856 {
1857 	u8 i;
1858 
1859 	for (i = 0; i < RTW8852C_DPK_KIP_REG_NUM; i++) {
1860 		rtw89_phy_write32_mask(rtwdev, reg[i] + (path << 8),
1861 				       MASKDWORD, reg_bkup[path][i]);
1862 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Reload 0x%x = %x\n",
1863 			    reg[i] + (path << 8), reg_bkup[path][i]);
1864 	}
1865 }
1866 
1867 static u8 _dpk_one_shot(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
1868 			enum rtw89_rf_path path, enum rtw8852c_dpk_id id)
1869 {
1870 	u16 dpk_cmd;
1871 	u32 val;
1872 	int ret;
1873 
1874 	dpk_cmd = (u16)((id << 8) | (0x19 + path * 0x12));
1875 
1876 	rtw89_phy_write32_mask(rtwdev, R_NCTL_CFG, MASKDWORD, dpk_cmd);
1877 
1878 	ret = read_poll_timeout_atomic(rtw89_phy_read32_mask, val, val == 0x55,
1879 				       10, 20000, false, rtwdev, 0xbff8, MASKBYTE0);
1880 	udelay(10);
1881 	rtw89_phy_write32_clr(rtwdev, R_NCTL_N1, MASKBYTE0);
1882 
1883 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1884 		    "[DPK] one-shot for %s = 0x%x (ret=%d)\n",
1885 		    id == 0x06 ? "LBK_RXIQK" :
1886 		    id == 0x10 ? "SYNC" :
1887 		    id == 0x11 ? "MDPK_IDL" :
1888 		    id == 0x12 ? "MDPK_MPA" :
1889 		    id == 0x13 ? "GAIN_LOSS" : "PWR_CAL",
1890 		    dpk_cmd, ret);
1891 
1892 	if (ret) {
1893 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
1894 			    "[DPK] one-shot over 20ms!!!!\n");
1895 		return 1;
1896 	}
1897 
1898 	return 0;
1899 }
1900 
1901 static void _dpk_information(struct rtw89_dev *rtwdev,
1902 			     enum rtw89_phy_idx phy,
1903 			     enum rtw89_rf_path path)
1904 {
1905 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
1906 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
1907 
1908 	u8 kidx = dpk->cur_idx[path];
1909 
1910 	dpk->bp[path][kidx].band = chan->band_type;
1911 	dpk->bp[path][kidx].ch = chan->channel;
1912 	dpk->bp[path][kidx].bw = chan->band_width;
1913 
1914 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
1915 		    "[DPK] S%d[%d] (PHY%d): TSSI %s/ DBCC %s/ %s/ CH%d/ %s\n",
1916 		    path, dpk->cur_idx[path], phy,
1917 		    rtwdev->is_tssi_mode[path] ? "on" : "off",
1918 		    rtwdev->dbcc_en ? "on" : "off",
1919 		    dpk->bp[path][kidx].band == 0 ? "2G" :
1920 		    dpk->bp[path][kidx].band == 1 ? "5G" : "6G",
1921 		    dpk->bp[path][kidx].ch,
1922 		    dpk->bp[path][kidx].bw == 0 ? "20M" :
1923 		    dpk->bp[path][kidx].bw == 1 ? "40M" : "80M");
1924 }
1925 
1926 static void _dpk_bb_afe_setting(struct rtw89_dev *rtwdev,
1927 				enum rtw89_phy_idx phy,
1928 				enum rtw89_rf_path path, u8 kpath)
1929 {
1930 	/*1. Keep ADC_fifo reset*/
1931 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
1932 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
1933 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
1934 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
1935 
1936 	/*2. BB for IQK DBG mode*/
1937 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0xd801dffd);
1938 
1939 	/*3.Set DAC clk*/
1940 	rtw8852c_txck_force(rtwdev, path, true, DAC_960M);
1941 
1942 	/*4. Set ADC clk*/
1943 	rtw8852c_rxck_force(rtwdev, path, true, ADC_1920M);
1944 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13),
1945 			       B_P0_NRBW_DBG, 0x1);
1946 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, MASKBYTE3, 0x1f);
1947 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR_PW15, MASKBYTE3, 0x13);
1948 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, MASKHWORD, 0x0001);
1949 	rtw89_phy_write32_mask(rtwdev, R_ANAPAR, MASKHWORD, 0x0041);
1950 
1951 	/*5. ADDA fifo rst*/
1952 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x1);
1953 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x1);
1954 
1955 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE setting\n", path);
1956 }
1957 
1958 static void _dpk_bb_afe_restore(struct rtw89_dev *rtwdev, u8 path)
1959 {
1960 	rtw89_phy_write32_mask(rtwdev, R_P0_NRBW + (path << 13),
1961 			       B_P0_NRBW_DBG, 0x0);
1962 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x1);
1963 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A1 << path, 0x0);
1964 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x1);
1965 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A3 << path, 0x0);
1966 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), MASKDWORD, 0x00000000);
1967 	rtw89_phy_write32_mask(rtwdev, R_P0_RXCK + (path << 13), B_P0_TXCK_ALL, 0x00);
1968 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A0 << path, 0x0);
1969 	rtw89_phy_write32_mask(rtwdev, R_ADC_FIFO, B_ADC_FIFO_A2 << path, 0x0);
1970 
1971 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d BB/AFE restore\n", path);
1972 }
1973 
1974 static void _dpk_tssi_pause(struct rtw89_dev *rtwdev,
1975 			    enum rtw89_rf_path path, bool is_pause)
1976 {
1977 	rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
1978 			       B_P0_TSSI_TRK_EN, is_pause);
1979 
1980 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d TSSI %s\n", path,
1981 		    is_pause ? "pause" : "resume");
1982 }
1983 
1984 static void _dpk_kip_control_rfc(struct rtw89_dev *rtwdev, u8 path, bool ctrl_by_kip)
1985 {
1986 	rtw89_phy_write32_mask(rtwdev, R_UPD_CLK + (path << 13), B_IQK_RFC_ON, ctrl_by_kip);
1987 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RFC is controlled by %s\n",
1988 		    ctrl_by_kip ? "KIP" : "BB");
1989 }
1990 
1991 static void _dpk_txpwr_bb_force(struct rtw89_dev *rtwdev, u8 path, bool force)
1992 {
1993 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_ON, force);
1994 	rtw89_phy_write32_mask(rtwdev, R_TXPWRB_H + (path << 13), B_TXPWRB_RDY, force);
1995 
1996 	rtw89_debug(rtwdev, RTW89_DBG_RFK,  "[DPK] S%d txpwr_bb_force %s\n",
1997 		    path, force ? "on" : "off");
1998 }
1999 
2000 static void _dpk_kip_restore(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2001 			     enum rtw89_rf_path path)
2002 {
2003 	_dpk_one_shot(rtwdev, phy, path, D_KIP_RESTORE);
2004 	_dpk_kip_control_rfc(rtwdev, path, false);
2005 	_dpk_txpwr_bb_force(rtwdev, path, false);
2006 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d restore KIP\n", path);
2007 }
2008 
2009 static void _dpk_lbk_rxiqk(struct rtw89_dev *rtwdev,
2010 			   enum rtw89_phy_idx phy,
2011 			   enum rtw89_rf_path path)
2012 {
2013 #define RX_TONE_IDX 0x00250025 /* Q.2 9.25MHz */
2014 	u8 cur_rxbb;
2015 	u32 rf_11, reg_81cc;
2016 
2017 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
2018 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x1);
2019 
2020 	_dpk_kip_control_rfc(rtwdev, path, false);
2021 
2022 	cur_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB);
2023 	rf_11 = rtw89_read_rf(rtwdev, path, RR_TXIG, RFREG_MASK);
2024 	reg_81cc = rtw89_phy_read32_mask(rtwdev, R_KIP_IQP + (path << 8),
2025 					 B_KIP_IQP_SW);
2026 
2027 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR0, 0x0);
2028 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_GR1, 0x3);
2029 	rtw89_write_rf(rtwdev, path, RR_TXIG, RR_TXIG_TG, 0xd);
2030 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, 0x1f);
2031 
2032 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_IQSW, 0x12);
2033 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, 0x3);
2034 
2035 	_dpk_kip_control_rfc(rtwdev, path, true);
2036 
2037 	rtw89_phy_write32_mask(rtwdev, R_IQK_DIF4, MASKDWORD, RX_TONE_IDX);
2038 
2039 	_dpk_one_shot(rtwdev, phy, path, LBK_RXIQK);
2040 
2041 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d LBK RXIQC = 0x%x\n", path,
2042 		    rtw89_phy_read32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD));
2043 
2044 	_dpk_kip_control_rfc(rtwdev, path, false);
2045 
2046 	rtw89_write_rf(rtwdev, path, RR_TXIG, RFREG_MASK, rf_11);
2047 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, cur_rxbb);
2048 	rtw89_phy_write32_mask(rtwdev, R_KIP_IQP + (path << 8), B_KIP_IQP_SW, reg_81cc);
2049 
2050 	rtw89_phy_write32_mask(rtwdev, R_MDPK_RX_DCK, B_MDPK_RX_DCK_EN, 0x0);
2051 	rtw89_phy_write32_mask(rtwdev, R_KPATH_CFG, B_KPATH_CFG_ED, 0x0);
2052 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_DI, 0x1);
2053 
2054 	_dpk_kip_control_rfc(rtwdev, path, true);
2055 }
2056 
2057 static void _dpk_rf_setting(struct rtw89_dev *rtwdev, u8 gain,
2058 			    enum rtw89_rf_path path, u8 kidx)
2059 {
2060 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2061 
2062 	if (dpk->bp[path][kidx].band == RTW89_BAND_2G) {
2063 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
2064 			       0x50121 | BIT(rtwdev->dbcc_en));
2065 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
2066 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTC, 0x2);
2067 		rtw89_write_rf(rtwdev, path, RR_RXBB, RR_RXBB_ATTR, 0x4);
2068 		rtw89_write_rf(rtwdev, path, RR_LUTDBG, RR_LUTDBG_TIA, 0x1);
2069 		rtw89_write_rf(rtwdev, path, RR_TIA, RR_TIA_N6, 0x1);
2070 
2071 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2072 			    "[DPK] RF 0x0/0x83/0x9e/0x1a/0xdf/0x1001a = 0x%x/ 0x%x/ 0x%x/ 0x%x/ 0x%x/ 0x%x\n",
2073 			    rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK),
2074 			    rtw89_read_rf(rtwdev, path, RR_RXBB, RFREG_MASK),
2075 			    rtw89_read_rf(rtwdev, path, RR_TIA, RFREG_MASK),
2076 			    rtw89_read_rf(rtwdev, path, RR_BTC, RFREG_MASK),
2077 			    rtw89_read_rf(rtwdev, path, RR_LUTDBG, RFREG_MASK),
2078 			    rtw89_read_rf(rtwdev, path, 0x1001a, RFREG_MASK));
2079 	} else {
2080 		rtw89_write_rf(rtwdev, path, RR_MOD, RFREG_MASK,
2081 			       0x50101 | BIT(rtwdev->dbcc_en));
2082 		rtw89_write_rf(rtwdev, path, RR_MOD_V1, RR_MOD_MASK, RF_DPK);
2083 
2084 		if (dpk->bp[path][kidx].band == RTW89_BAND_6G && dpk->bp[path][kidx].ch >= 161)
2085 			rtw89_write_rf(rtwdev, path, RR_IQGEN, RR_IQGEN_BIAS, 0x8);
2086 
2087 		rtw89_write_rf(rtwdev, path, RR_LOGEN, RR_LOGEN_RPT, 0xd);
2088 		rtw89_write_rf(rtwdev, path, RR_TXAC, RR_TXAC_IQG, 0x8);
2089 
2090 		rtw89_write_rf(rtwdev, path, RR_RXA2, RR_RXA2_ATT, 0x0);
2091 		rtw89_write_rf(rtwdev, path, RR_TXIQK, RR_TXIQK_ATT2, 0x3);
2092 		rtw89_write_rf(rtwdev, path, RR_LUTDBG, RR_LUTDBG_TIA, 0x1);
2093 		rtw89_write_rf(rtwdev, path, RR_TIA, RR_TIA_N6, 0x1);
2094 
2095 		if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160)
2096 			rtw89_write_rf(rtwdev, path, RR_RXBB2, RR_RXBB2_EBW, 0x0);
2097 	}
2098 }
2099 
2100 static void _dpk_tpg_sel(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2101 {
2102 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2103 
2104 	if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160) {
2105 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x3);
2106 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0x0180ff30);
2107 	} else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80) {
2108 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x0);
2109 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xffe0fa00);
2110 	} else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40) {
2111 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x2);
2112 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xff4009e0);
2113 	} else {
2114 		rtw89_phy_write32_mask(rtwdev, R_TPG_MOD, B_TPG_MOD_F, 0x1);
2115 		rtw89_phy_write32_mask(rtwdev, R_TPG_SEL, MASKDWORD, 0xf9f007d0);
2116 	}
2117 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] TPG_Select for %s\n",
2118 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_160 ? "160M" :
2119 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80 ? "80M" :
2120 		    dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ? "40M" : "20M");
2121 }
2122 
2123 static bool _dpk_sync_check(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2124 {
2125 #define DPK_SYNC_TH_DC_I 200
2126 #define DPK_SYNC_TH_DC_Q 200
2127 #define DPK_SYNC_TH_CORR 170
2128 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2129 	u16 dc_i, dc_q;
2130 	u8 corr_val, corr_idx, rxbb;
2131 	u8 rxbb_ov;
2132 
2133 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
2134 
2135 	corr_idx = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORI);
2136 	corr_val = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_CORV);
2137 
2138 	dpk->corr_idx[path][kidx] = corr_idx;
2139 	dpk->corr_val[path][kidx] = corr_val;
2140 
2141 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x9);
2142 
2143 	dc_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
2144 	dc_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCQ);
2145 
2146 	dc_i = abs(sign_extend32(dc_i, 11));
2147 	dc_q = abs(sign_extend32(dc_q, 11));
2148 
2149 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2150 		    "[DPK] S%d Corr_idx/ Corr_val /DC I/Q, = %d / %d / %d / %d\n",
2151 		    path, corr_idx, corr_val, dc_i, dc_q);
2152 
2153 	dpk->dc_i[path][kidx] = dc_i;
2154 	dpk->dc_q[path][kidx] = dc_q;
2155 
2156 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x8);
2157 	rxbb = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXBB);
2158 
2159 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x31);
2160 	rxbb_ov = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_RXOV);
2161 
2162 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2163 		    "[DPK] S%d RXBB/ RXAGC_done /RXBB_ovlmt = %d / %d / %d\n",
2164 		    path, rxbb,
2165 		    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DONE),
2166 		    rxbb_ov);
2167 
2168 	if (dc_i > DPK_SYNC_TH_DC_I || dc_q > DPK_SYNC_TH_DC_Q ||
2169 	    corr_val < DPK_SYNC_TH_CORR)
2170 		return true;
2171 	else
2172 		return false;
2173 }
2174 
2175 static u16 _dpk_dgain_read(struct rtw89_dev *rtwdev)
2176 {
2177 	u16 dgain = 0x0;
2178 
2179 	rtw89_phy_write32_clr(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL);
2180 
2181 	dgain = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_DCI);
2182 
2183 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] DGain = 0x%x (%d)\n", dgain, dgain);
2184 
2185 	return dgain;
2186 }
2187 
2188 static u8 _dpk_gainloss_read(struct rtw89_dev *rtwdev)
2189 {
2190 	u8 result;
2191 
2192 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x6);
2193 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x1);
2194 
2195 	result = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_GL);
2196 
2197 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] tmp GL = %d\n", result);
2198 
2199 	return result;
2200 }
2201 
2202 static void _dpk_kset_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
2203 {
2204 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2205 
2206 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0x10);
2207 	dpk->cur_k_set =
2208 		rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), 0xE0000000) - 1;
2209 }
2210 
2211 static void _dpk_kip_set_txagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2212 			       enum rtw89_rf_path path, u8 dbm, bool set_from_bb)
2213 {
2214 	if (set_from_bb) {
2215 		dbm = clamp_t(u8, dbm, 7, 24);
2216 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] set S%d txagc to %ddBm\n", path, dbm);
2217 		rtw89_phy_write32_mask(rtwdev, R_TXPWRB + (path << 13), B_TXPWRB_VAL, dbm << 2);
2218 	}
2219 	_dpk_one_shot(rtwdev, phy, path, D_TXAGC);
2220 	_dpk_kset_query(rtwdev, path);
2221 }
2222 
2223 static u8 _dpk_gainloss(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2224 			enum rtw89_rf_path path, u8 kidx)
2225 {
2226 	_dpk_one_shot(rtwdev, phy, path, D_GAIN_LOSS);
2227 	_dpk_kip_set_txagc(rtwdev, phy, path, 0xff, false);
2228 
2229 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A1, 0x0);
2230 	rtw89_phy_write32_mask(rtwdev, R_DPK_GL + (path << 8), B_DPK_GL_A0, 0x0);
2231 
2232 	return _dpk_gainloss_read(rtwdev);
2233 }
2234 
2235 static enum dpk_pas_result _dpk_pas_read(struct rtw89_dev *rtwdev, bool is_check)
2236 {
2237 	u32 val1_i = 0, val1_q = 0, val2_i = 0, val2_q = 0;
2238 	u32 val1_sqrt_sum, val2_sqrt_sum;
2239 	u8 i;
2240 
2241 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, MASKBYTE2, 0x06);
2242 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG2, B_DPK_CFG2_ST, 0x0);
2243 	rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE2, 0x08);
2244 
2245 	if (is_check) {
2246 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x00);
2247 		val1_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
2248 		val1_i = abs(sign_extend32(val1_i, 11));
2249 		val1_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
2250 		val1_q = abs(sign_extend32(val1_q, 11));
2251 
2252 		rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, 0x1f);
2253 		val2_i = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKHWORD);
2254 		val2_i = abs(sign_extend32(val2_i, 11));
2255 		val2_q = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKLWORD);
2256 		val2_q = abs(sign_extend32(val2_q, 11));
2257 
2258 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_delta = 0x%x\n",
2259 			    phy_div(val1_i * val1_i + val1_q * val1_q,
2260 				    val2_i * val2_i + val2_q * val2_q));
2261 	} else {
2262 		for (i = 0; i < 32; i++) {
2263 			rtw89_phy_write32_mask(rtwdev, R_DPK_CFG3, MASKBYTE3, i);
2264 			rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] PAS_Read[%02d]= 0x%08x\n", i,
2265 				    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
2266 		}
2267 	}
2268 
2269 	val1_sqrt_sum = val1_i * val1_i + val1_q * val1_q;
2270 	val2_sqrt_sum = val2_i * val2_i + val2_q * val2_q;
2271 
2272 	if (val1_sqrt_sum < val2_sqrt_sum)
2273 		return DPK_PAS_LT;
2274 	else if (val1_sqrt_sum >= val2_sqrt_sum * 8 / 5)
2275 		return DPK_PAS_GT;
2276 	else
2277 		return DPK_PAS_NOR;
2278 }
2279 
2280 static bool _dpk_kip_set_rxagc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2281 			       enum rtw89_rf_path path, u8 kidx)
2282 {
2283 	_dpk_kip_control_rfc(rtwdev, path, false);
2284 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
2285 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
2286 	_dpk_kip_control_rfc(rtwdev, path, true);
2287 
2288 	_dpk_one_shot(rtwdev, phy, path, D_RXAGC);
2289 
2290 	return _dpk_sync_check(rtwdev, path, kidx);
2291 }
2292 
2293 static void _dpk_read_rxsram(struct rtw89_dev *rtwdev)
2294 {
2295 	u32 addr;
2296 
2297 	rtw89_rfk_parser(rtwdev, &rtw8852c_read_rxsram_pre_defs_tbl);
2298 
2299 	for (addr = 0; addr < 0x200; addr++) {
2300 		rtw89_phy_write32_mask(rtwdev, R_SRAM_IQRX, MASKDWORD, 0x00010000 | addr);
2301 
2302 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] RXSRAM[%03d] = 0x%07x\n", addr,
2303 			    rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD));
2304 	}
2305 
2306 	rtw89_rfk_parser(rtwdev, &rtw8852c_read_rxsram_post_defs_tbl);
2307 }
2308 
2309 static void _dpk_bypass_rxiqc(struct rtw89_dev *rtwdev, enum rtw89_rf_path path)
2310 {
2311 	rtw89_phy_write32_mask(rtwdev, R_DPD_V1 + (path << 8), B_DPD_LBK, 0x1);
2312 	rtw89_phy_write32_mask(rtwdev, R_RXIQC + (path << 8), MASKDWORD, 0x40000002);
2313 
2314 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Bypass RXIQC\n");
2315 }
2316 
2317 static u8 _dpk_agc(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2318 		   enum rtw89_rf_path path, u8 kidx, u8 init_xdbm, u8 loss_only)
2319 {
2320 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2321 	u8 step = DPK_AGC_STEP_SYNC_DGAIN;
2322 	u8 tmp_dbm = init_xdbm, tmp_gl_idx = 0;
2323 	u8 tmp_rxbb;
2324 	u8 goout = 0, agc_cnt = 0;
2325 	enum dpk_pas_result pas;
2326 	u16 dgain = 0;
2327 	bool is_fail = false;
2328 	int limit = 200;
2329 
2330 	do {
2331 		switch (step) {
2332 		case DPK_AGC_STEP_SYNC_DGAIN:
2333 			is_fail = _dpk_kip_set_rxagc(rtwdev, phy, path, kidx);
2334 
2335 			if (RTW8852C_DPK_RXSRAM_DBG)
2336 				_dpk_read_rxsram(rtwdev);
2337 
2338 			if (is_fail) {
2339 				goout = 1;
2340 				break;
2341 			}
2342 
2343 			dgain = _dpk_dgain_read(rtwdev);
2344 
2345 			if (dgain > 0x5fc || dgain < 0x556) {
2346 				_dpk_one_shot(rtwdev, phy, path, D_SYNC);
2347 				dgain = _dpk_dgain_read(rtwdev);
2348 			}
2349 
2350 			if (agc_cnt == 0) {
2351 				if (dpk->bp[path][kidx].band == RTW89_BAND_2G)
2352 					_dpk_bypass_rxiqc(rtwdev, path);
2353 				else
2354 					_dpk_lbk_rxiqk(rtwdev, phy, path);
2355 			}
2356 			step = DPK_AGC_STEP_GAIN_LOSS_IDX;
2357 			break;
2358 
2359 		case DPK_AGC_STEP_GAIN_LOSS_IDX:
2360 			tmp_gl_idx = _dpk_gainloss(rtwdev, phy, path, kidx);
2361 			pas = _dpk_pas_read(rtwdev, true);
2362 
2363 			if (pas == DPK_PAS_LT && tmp_gl_idx > 0)
2364 				step = DPK_AGC_STEP_GL_LT_CRITERION;
2365 			else if (pas == DPK_PAS_GT && tmp_gl_idx == 0)
2366 				step = DPK_AGC_STEP_GL_GT_CRITERION;
2367 			else if (tmp_gl_idx >= 7)
2368 				step = DPK_AGC_STEP_GL_GT_CRITERION;
2369 			else if (tmp_gl_idx == 0)
2370 				step = DPK_AGC_STEP_GL_LT_CRITERION;
2371 			else
2372 				step = DPK_AGC_STEP_SET_TX_GAIN;
2373 			break;
2374 
2375 		case DPK_AGC_STEP_GL_GT_CRITERION:
2376 			if (tmp_dbm <= 7) {
2377 				goout = 1;
2378 				rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Txagc@lower bound!!\n");
2379 			} else {
2380 				tmp_dbm = max_t(u8, tmp_dbm - 3, 7);
2381 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
2382 			}
2383 			step = DPK_AGC_STEP_SYNC_DGAIN;
2384 			agc_cnt++;
2385 			break;
2386 
2387 		case DPK_AGC_STEP_GL_LT_CRITERION:
2388 			if (tmp_dbm >= 24) {
2389 				goout = 1;
2390 				rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Txagc@upper bound!!\n");
2391 			} else {
2392 				tmp_dbm = min_t(u8, tmp_dbm + 2, 24);
2393 				_dpk_kip_set_txagc(rtwdev, phy, path, tmp_dbm, true);
2394 			}
2395 			step = DPK_AGC_STEP_SYNC_DGAIN;
2396 			agc_cnt++;
2397 			break;
2398 
2399 		case DPK_AGC_STEP_SET_TX_GAIN:
2400 			_dpk_kip_control_rfc(rtwdev, path, false);
2401 			tmp_rxbb = rtw89_read_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB);
2402 			if (tmp_rxbb + tmp_gl_idx > 0x1f)
2403 				tmp_rxbb = 0x1f;
2404 			else
2405 				tmp_rxbb = tmp_rxbb + tmp_gl_idx;
2406 
2407 			rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_M_RXBB, tmp_rxbb);
2408 			rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Adjust RXBB (%+d) = 0x%x\n",
2409 				    tmp_gl_idx, tmp_rxbb);
2410 			_dpk_kip_control_rfc(rtwdev, path, true);
2411 			goout = 1;
2412 			break;
2413 		default:
2414 			goout = 1;
2415 			break;
2416 		}
2417 	} while (!goout && agc_cnt < 6 && --limit > 0);
2418 
2419 	if (limit <= 0)
2420 		rtw89_warn(rtwdev, "[DPK] exceed loop limit\n");
2421 
2422 	return is_fail;
2423 }
2424 
2425 static void _dpk_set_mdpd_para(struct rtw89_dev *rtwdev, u8 order)
2426 {
2427 	static const struct rtw89_rfk_tbl *order_tbls[] = {
2428 		&rtw8852c_dpk_mdpd_order0_defs_tbl,
2429 		&rtw8852c_dpk_mdpd_order1_defs_tbl,
2430 		&rtw8852c_dpk_mdpd_order2_defs_tbl,
2431 		&rtw8852c_dpk_mdpd_order3_defs_tbl,
2432 	};
2433 
2434 	if (order >= ARRAY_SIZE(order_tbls)) {
2435 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Wrong MDPD order!!(0x%x)\n", order);
2436 		return;
2437 	}
2438 
2439 	rtw89_rfk_parser(rtwdev, order_tbls[order]);
2440 
2441 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Set %s for IDL\n",
2442 		    order == 0x0 ? "(5,3,1)" :
2443 		    order == 0x1 ? "(5,3,0)" :
2444 		    order == 0x2 ? "(5,0,0)" : "(7,3,1)");
2445 }
2446 
2447 static void _dpk_idl_mpa(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2448 			 enum rtw89_rf_path path, u8 kidx)
2449 {
2450 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2451 	u8 cnt;
2452 	u8 ov_flag;
2453 	u32 dpk_sync;
2454 
2455 	rtw89_phy_write32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_MA, 0x1);
2456 
2457 	if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T2) == 0x1)
2458 		_dpk_set_mdpd_para(rtwdev, 0x2);
2459 	else if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T1) == 0x1)
2460 		_dpk_set_mdpd_para(rtwdev, 0x1);
2461 	else if (rtw89_phy_read32_mask(rtwdev, R_DPK_MPA, B_DPK_MPA_T0) == 0x1)
2462 		_dpk_set_mdpd_para(rtwdev, 0x0);
2463 	else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_5 ||
2464 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_10 ||
2465 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_20)
2466 		_dpk_set_mdpd_para(rtwdev, 0x2);
2467 	else if (dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_40 ||
2468 		 dpk->bp[path][kidx].bw == RTW89_CHANNEL_WIDTH_80)
2469 		_dpk_set_mdpd_para(rtwdev, 0x1);
2470 	else
2471 		_dpk_set_mdpd_para(rtwdev, 0x0);
2472 
2473 	rtw89_phy_write32_mask(rtwdev, R_DPK_IDL, B_DPK_IDL, 0x0);
2474 	fsleep(1000);
2475 
2476 	_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2477 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0x0);
2478 	dpk_sync = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, MASKDWORD);
2479 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] dpk_sync = 0x%x\n", dpk_sync);
2480 
2481 	rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0xf);
2482 	ov_flag = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_SYNERR);
2483 	for (cnt = 0; cnt < 5 && ov_flag == 0x1; cnt++) {
2484 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] ReK due to MDPK ov!!!\n");
2485 		_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2486 		rtw89_phy_write32_mask(rtwdev, R_KIP_RPT1, B_KIP_RPT1_SEL, 0xf);
2487 		ov_flag = rtw89_phy_read32_mask(rtwdev, R_RPT_COM, B_PRT_COM_SYNERR);
2488 	}
2489 
2490 	if (ov_flag) {
2491 		_dpk_set_mdpd_para(rtwdev, 0x2);
2492 		_dpk_one_shot(rtwdev, phy, path, D_MDPK_IDL);
2493 	}
2494 }
2495 
2496 static bool _dpk_reload_check(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2497 			      enum rtw89_rf_path path)
2498 {
2499 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2500 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2501 	bool is_reload = false;
2502 	u8 idx, cur_band, cur_ch;
2503 
2504 	cur_band = chan->band_type;
2505 	cur_ch = chan->channel;
2506 
2507 	for (idx = 0; idx < RTW89_DPK_BKUP_NUM; idx++) {
2508 		if (cur_band != dpk->bp[path][idx].band ||
2509 		    cur_ch != dpk->bp[path][idx].ch)
2510 			continue;
2511 
2512 		rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8),
2513 				       B_COEF_SEL_MDPD, idx);
2514 		dpk->cur_idx[path] = idx;
2515 		is_reload = true;
2516 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2517 			    "[DPK] reload S%d[%d] success\n", path, idx);
2518 	}
2519 
2520 	return is_reload;
2521 }
2522 
2523 static void _dpk_kip_pwr_clk_onoff(struct rtw89_dev *rtwdev, bool turn_on)
2524 {
2525 	rtw89_rfk_parser(rtwdev, turn_on ? &rtw8852c_dpk_kip_pwr_clk_on_defs_tbl :
2526 					   &rtw8852c_dpk_kip_pwr_clk_off_defs_tbl);
2527 }
2528 
2529 static void _dpk_kip_preset_8852c(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2530 				  enum rtw89_rf_path path, u8 kidx)
2531 {
2532 	rtw89_phy_write32_mask(rtwdev, R_KIP_MOD, B_KIP_MOD,
2533 			       rtw89_read_rf(rtwdev, path, RR_MOD, RFREG_MASK));
2534 
2535 	if (rtwdev->hal.cv == CHIP_CAV)
2536 		rtw89_phy_write32_mask(rtwdev,
2537 				       R_DPD_CH0A + (path << 8) + (kidx << 2),
2538 				       B_DPD_SEL, 0x01);
2539 	else
2540 		rtw89_phy_write32_mask(rtwdev,
2541 				       R_DPD_CH0A + (path << 8) + (kidx << 2),
2542 				       B_DPD_SEL, 0x0c);
2543 
2544 	_dpk_kip_control_rfc(rtwdev, path, true);
2545 	rtw89_phy_write32_mask(rtwdev, R_COEF_SEL + (path << 8), B_COEF_SEL_MDPD, kidx);
2546 
2547 	_dpk_one_shot(rtwdev, phy, path, D_KIP_PRESET);
2548 }
2549 
2550 static void _dpk_para_query(struct rtw89_dev *rtwdev, enum rtw89_rf_path path, u8 kidx)
2551 {
2552 #define _DPK_PARA_TXAGC GENMASK(15, 10)
2553 #define _DPK_PARA_THER GENMASK(31, 26)
2554 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2555 	u32 para;
2556 
2557 	para = rtw89_phy_read32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2558 				     MASKDWORD);
2559 
2560 	dpk->bp[path][kidx].txagc_dpk = FIELD_GET(_DPK_PARA_TXAGC, para);
2561 	dpk->bp[path][kidx].ther_dpk = FIELD_GET(_DPK_PARA_THER, para);
2562 
2563 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] thermal/ txagc_RF (K%d) = 0x%x/ 0x%x\n",
2564 		    dpk->cur_k_set, dpk->bp[path][kidx].ther_dpk, dpk->bp[path][kidx].txagc_dpk);
2565 }
2566 
2567 static void _dpk_gain_normalize_8852c(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2568 				      enum rtw89_rf_path path, u8 kidx, bool is_execute)
2569 {
2570 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2571 
2572 	if (is_execute) {
2573 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8), B_DPK_GN_AG, 0x200);
2574 		rtw89_phy_write32_mask(rtwdev, R_DPK_GN + (path << 8), B_DPK_GN_EN, 0x3);
2575 
2576 		_dpk_one_shot(rtwdev, phy, path, D_GAIN_NORM);
2577 	} else {
2578 		rtw89_phy_write32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2579 				       0x0000007F, 0x5b);
2580 	}
2581 	dpk->bp[path][kidx].gs =
2582 		rtw89_phy_read32_mask(rtwdev, dpk_par_regs[kidx][dpk->cur_k_set] + (path << 8),
2583 				      0x0000007F);
2584 }
2585 
2586 static u8 _dpk_order_convert(struct rtw89_dev *rtwdev)
2587 {
2588 	u32 val32 = rtw89_phy_read32_mask(rtwdev, R_LDL_NORM, B_LDL_NORM_OP);
2589 	u8 val;
2590 
2591 	switch (val32) {
2592 	case 0:
2593 		val = 0x6;
2594 		break;
2595 	case 1:
2596 		val = 0x2;
2597 		break;
2598 	case 2:
2599 		val = 0x0;
2600 		break;
2601 	case 3:
2602 		val = 0x7;
2603 		break;
2604 	default:
2605 		val = 0xff;
2606 		break;
2607 	}
2608 
2609 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] convert MDPD order to 0x%x\n", val);
2610 
2611 	return val;
2612 }
2613 
2614 static void _dpk_on(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2615 		    enum rtw89_rf_path path, u8 kidx)
2616 {
2617 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2618 
2619 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x1);
2620 	rtw89_phy_write32_mask(rtwdev, R_LOAD_COEF + (path << 8), B_LOAD_COEF_MDPD, 0x0);
2621 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2622 			       B_DPD_ORDER, _dpk_order_convert(rtwdev));
2623 
2624 	dpk->bp[path][kidx].mdpd_en = BIT(dpk->cur_k_set);
2625 	dpk->bp[path][kidx].path_ok = true;
2626 
2627 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] path_ok = 0x%x\n",
2628 		    path, kidx, dpk->bp[path][kidx].mdpd_en);
2629 
2630 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2631 			       B_DPD_MEN, dpk->bp[path][kidx].mdpd_en);
2632 
2633 	_dpk_gain_normalize_8852c(rtwdev, phy, path, kidx, false);
2634 }
2635 
2636 static bool _dpk_main(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2637 		      enum rtw89_rf_path path, u8 gain)
2638 {
2639 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2640 	u8 kidx = dpk->cur_idx[path];
2641 	u8 init_xdbm = 15;
2642 	bool is_fail;
2643 
2644 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2645 		    "[DPK] ========= S%d[%d] DPK Start =========\n", path, kidx);
2646 	_dpk_kip_control_rfc(rtwdev, path, false);
2647 	_rf_direct_cntrl(rtwdev, path, false);
2648 	rtw89_write_rf(rtwdev, path, RR_BBDC, RFREG_MASK, 0x03ffd);
2649 	_dpk_rf_setting(rtwdev, gain, path, kidx);
2650 	_set_rx_dck(rtwdev, phy, path, false);
2651 	_dpk_kip_pwr_clk_onoff(rtwdev, true);
2652 	_dpk_kip_preset_8852c(rtwdev, phy, path, kidx);
2653 	_dpk_txpwr_bb_force(rtwdev, path, true);
2654 	_dpk_kip_set_txagc(rtwdev, phy, path, init_xdbm, true);
2655 	_dpk_tpg_sel(rtwdev, path, kidx);
2656 
2657 	is_fail = _dpk_agc(rtwdev, phy, path, kidx, init_xdbm, false);
2658 	if (is_fail)
2659 		goto _error;
2660 
2661 	_dpk_idl_mpa(rtwdev, phy, path, kidx);
2662 	_dpk_para_query(rtwdev, path, kidx);
2663 	_dpk_on(rtwdev, phy, path, kidx);
2664 
2665 _error:
2666 	_dpk_kip_control_rfc(rtwdev, path, false);
2667 	rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RF_RX);
2668 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d]_K%d %s\n", path, kidx,
2669 		    dpk->cur_k_set, is_fail ? "need Check" : "is Success");
2670 
2671 	return is_fail;
2672 }
2673 
2674 static void _dpk_init(struct rtw89_dev *rtwdev, u8 path)
2675 {
2676 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2677 	u8 kidx = dpk->cur_idx[path];
2678 
2679 	dpk->bp[path][kidx].path_ok = false;
2680 }
2681 
2682 static void _dpk_drf_direct_cntrl(struct rtw89_dev *rtwdev, u8 path, bool is_bybb)
2683 {
2684 	if (is_bybb)
2685 		rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x1);
2686 	else
2687 		rtw89_write_rf(rtwdev,  path, RR_BBDC, RR_BBDC_SEL, 0x0);
2688 }
2689 
2690 static void _dpk_cal_select(struct rtw89_dev *rtwdev, bool force,
2691 			    enum rtw89_phy_idx phy, u8 kpath)
2692 {
2693 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2694 	static const u32 kip_reg[] = {0x813c, 0x8124, 0x8120, 0xc0c4, 0xc0e8, 0xc0d4, 0xc0d8};
2695 	u32 backup_rf_val[RTW8852C_DPK_RF_PATH][BACKUP_RF_REGS_NR];
2696 	u32 kip_bkup[RTW8852C_DPK_RF_PATH][RTW8852C_DPK_KIP_REG_NUM] = {};
2697 	u8 path;
2698 	bool is_fail = true, reloaded[RTW8852C_DPK_RF_PATH] = {false};
2699 
2700 #if defined(__linux__)
2701 	static_assert(ARRAY_SIZE(kip_reg) == RTW8852C_DPK_KIP_REG_NUM);
2702 #elif defined(__FreeBSD__)
2703 	rtw89_static_assert(ARRAY_SIZE(kip_reg) == RTW8852C_DPK_KIP_REG_NUM);
2704 #endif
2705 
2706 	if (dpk->is_dpk_reload_en) {
2707 		for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2708 			if (!(kpath & BIT(path)))
2709 				continue;
2710 
2711 			reloaded[path] = _dpk_reload_check(rtwdev, phy, path);
2712 			if (!reloaded[path] && dpk->bp[path][0].ch != 0)
2713 				dpk->cur_idx[path] = !dpk->cur_idx[path];
2714 			else
2715 				_dpk_onoff(rtwdev, path, false);
2716 		}
2717 	} else {
2718 		for (path = 0; path < RTW8852C_DPK_RF_PATH; path++)
2719 			dpk->cur_idx[path] = 0;
2720 	}
2721 
2722 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2723 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2724 			    "[DPK] ========= S%d[%d] DPK Init =========\n",
2725 			    path, dpk->cur_idx[path]);
2726 		_dpk_bkup_kip(rtwdev, kip_reg, kip_bkup, path);
2727 		_rfk_backup_rf_reg(rtwdev, backup_rf_val[path], path);
2728 		_dpk_information(rtwdev, phy, path);
2729 		_dpk_init(rtwdev, path);
2730 		if (rtwdev->is_tssi_mode[path])
2731 			_dpk_tssi_pause(rtwdev, path, true);
2732 	}
2733 
2734 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2735 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2736 			    "[DPK] ========= S%d[%d] DPK Start =========\n",
2737 			    path, dpk->cur_idx[path]);
2738 		rtw8852c_disable_rxagc(rtwdev, path, 0x0);
2739 		_dpk_drf_direct_cntrl(rtwdev, path, false);
2740 		_dpk_bb_afe_setting(rtwdev, phy, path, kpath);
2741 		is_fail = _dpk_main(rtwdev, phy, path, 1);
2742 		_dpk_onoff(rtwdev, path, is_fail);
2743 	}
2744 
2745 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2746 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
2747 			    "[DPK] ========= S%d[%d] DPK Restore =========\n",
2748 			    path, dpk->cur_idx[path]);
2749 		_dpk_kip_restore(rtwdev, phy, path);
2750 		_dpk_reload_kip(rtwdev, kip_reg, kip_bkup, path);
2751 		_rfk_restore_rf_reg(rtwdev, backup_rf_val[path], path);
2752 		_dpk_bb_afe_restore(rtwdev, path);
2753 		rtw8852c_disable_rxagc(rtwdev, path, 0x1);
2754 		if (rtwdev->is_tssi_mode[path])
2755 			_dpk_tssi_pause(rtwdev, path, false);
2756 	}
2757 
2758 	_dpk_kip_pwr_clk_onoff(rtwdev, false);
2759 }
2760 
2761 static bool _dpk_bypass_check(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
2762 {
2763 	struct rtw89_fem_info *fem = &rtwdev->fem;
2764 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2765 	u8 band = chan->band_type;
2766 
2767 	if (rtwdev->hal.cv == CHIP_CAV && band != RTW89_BAND_2G) {
2768 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to CAV & not 2G!!\n");
2769 		return true;
2770 	} else if (fem->epa_2g && band == RTW89_BAND_2G) {
2771 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 2G_ext_PA exist!!\n");
2772 		return true;
2773 	} else if (fem->epa_5g && band == RTW89_BAND_5G) {
2774 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 5G_ext_PA exist!!\n");
2775 		return true;
2776 	} else if (fem->epa_6g && band == RTW89_BAND_6G) {
2777 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] Skip DPK due to 6G_ext_PA exist!!\n");
2778 		return true;
2779 	}
2780 
2781 	return false;
2782 }
2783 
2784 static void _dpk_force_bypass(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
2785 {
2786 	u8 path, kpath;
2787 
2788 	kpath = _kpath(rtwdev, phy);
2789 
2790 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2791 		if (kpath & BIT(path))
2792 			_dpk_onoff(rtwdev, path, true);
2793 	}
2794 }
2795 
2796 static void _dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool force)
2797 {
2798 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
2799 		    "[DPK] ****** DPK Start (Ver: 0x%x, Cv: %d, RF_para: %d) ******\n",
2800 		    RTW8852C_DPK_VER, rtwdev->hal.cv,
2801 		    RTW8852C_RF_REL_VERSION);
2802 
2803 	if (_dpk_bypass_check(rtwdev, phy))
2804 		_dpk_force_bypass(rtwdev, phy);
2805 	else
2806 		_dpk_cal_select(rtwdev, force, phy, _kpath(rtwdev, phy));
2807 
2808 	if (rtw89_read_rf(rtwdev, RF_PATH_A, RR_DCKC, RR_DCKC_CHK) == 0x1)
2809 		rtw8852c_rx_dck(rtwdev, phy, false);
2810 }
2811 
2812 static void _dpk_onoff(struct rtw89_dev *rtwdev,
2813 		       enum rtw89_rf_path path, bool off)
2814 {
2815 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2816 	u8 val, kidx = dpk->cur_idx[path];
2817 
2818 	val = dpk->is_dpk_enable && !off && dpk->bp[path][kidx].path_ok ?
2819 	      dpk->bp[path][kidx].mdpd_en : 0;
2820 
2821 	rtw89_phy_write32_mask(rtwdev, R_DPD_CH0A + (path << 8) + (kidx << 2),
2822 			       B_DPD_MEN, val);
2823 
2824 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[DPK] S%d[%d] DPK %s !!!\n", path,
2825 		    kidx, dpk->is_dpk_enable && !off ? "enable" : "disable");
2826 }
2827 
2828 static void _dpk_track(struct rtw89_dev *rtwdev)
2829 {
2830 	struct rtw89_dpk_info *dpk = &rtwdev->dpk;
2831 	u8 path, kidx;
2832 	u8 txagc_rf = 0;
2833 	s8 txagc_bb = 0, txagc_bb_tp = 0, txagc_ofst = 0;
2834 	u8 cur_ther;
2835 	s8 delta_ther = 0;
2836 	s16 pwsf_tssi_ofst;
2837 
2838 	for (path = 0; path < RTW8852C_DPK_RF_PATH; path++) {
2839 		kidx = dpk->cur_idx[path];
2840 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2841 			    "[DPK_TRK] ================[S%d[%d] (CH %d)]================\n",
2842 			    path, kidx, dpk->bp[path][kidx].ch);
2843 
2844 		txagc_rf =
2845 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13), 0x0000003f);
2846 		txagc_bb =
2847 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB + (path << 13), MASKBYTE2);
2848 		txagc_bb_tp =
2849 			rtw89_phy_read32_mask(rtwdev, R_TXAGC_BTP + (path << 13), B_TXAGC_BTP);
2850 
2851 		/* report from KIP */
2852 		rtw89_phy_write32_mask(rtwdev, R_KIP_RPT + (path << 8), B_KIP_RPT_SEL, 0xf);
2853 		cur_ther =
2854 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_TH);
2855 		txagc_ofst =
2856 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_OF);
2857 		pwsf_tssi_ofst =
2858 			rtw89_phy_read32_mask(rtwdev, R_RPT_PER + (path << 8), B_RPT_PER_TSSI);
2859 		pwsf_tssi_ofst = sign_extend32(pwsf_tssi_ofst, 12);
2860 
2861 		cur_ther = ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
2862 
2863 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2864 			    "[DPK_TRK] thermal now = %d\n", cur_ther);
2865 
2866 		if (dpk->bp[path][kidx].ch != 0 && cur_ther != 0)
2867 			delta_ther = dpk->bp[path][kidx].ther_dpk - cur_ther;
2868 
2869 		delta_ther = delta_ther * 1 / 2;
2870 
2871 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2872 			    "[DPK_TRK] extra delta_ther = %d (0x%x / 0x%x@k)\n",
2873 			    delta_ther, cur_ther, dpk->bp[path][kidx].ther_dpk);
2874 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2875 			    "[DPK_TRK] delta_txagc = %d (0x%x / 0x%x@k)\n",
2876 			    txagc_rf - dpk->bp[path][kidx].txagc_dpk, txagc_rf,
2877 			    dpk->bp[path][kidx].txagc_dpk);
2878 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2879 			    "[DPK_TRK] txagc_offset / pwsf_tssi_ofst = 0x%x / %+d\n",
2880 			    txagc_ofst, pwsf_tssi_ofst);
2881 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2882 			    "[DPK_TRK] txagc_bb_tp / txagc_bb = 0x%x / 0x%x\n",
2883 			    txagc_bb_tp, txagc_bb);
2884 
2885 		if (rtw89_phy_read32_mask(rtwdev, R_DPK_WR, B_DPK_WR_ST) == 0x0 &&
2886 		    txagc_rf != 0 && rtwdev->hal.cv == CHIP_CAV) {
2887 			rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
2888 				    "[DPK_TRK] New pwsf = 0x%x\n", 0x78 - delta_ther);
2889 
2890 			rtw89_phy_write32_mask(rtwdev, R_DPD_BND + (path << 8) + (kidx << 2),
2891 					       0x07FC0000, 0x78 - delta_ther);
2892 		}
2893 	}
2894 }
2895 
2896 static void _tssi_set_sys(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2897 			  enum rtw89_rf_path path)
2898 {
2899 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2900 	enum rtw89_band band = chan->band_type;
2901 
2902 	rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_sys_defs_tbl);
2903 
2904 	if (path == RF_PATH_A)
2905 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2906 					 &rtw8852c_tssi_sys_defs_2g_a_tbl,
2907 					 &rtw8852c_tssi_sys_defs_5g_a_tbl);
2908 	else
2909 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2910 					 &rtw8852c_tssi_sys_defs_2g_b_tbl,
2911 					 &rtw8852c_tssi_sys_defs_5g_b_tbl);
2912 }
2913 
2914 static void _tssi_ini_txpwr_ctrl_bb(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2915 				    enum rtw89_rf_path path)
2916 {
2917 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2918 				 &rtw8852c_tssi_txpwr_ctrl_bb_defs_a_tbl,
2919 				 &rtw8852c_tssi_txpwr_ctrl_bb_defs_b_tbl);
2920 }
2921 
2922 static void _tssi_ini_txpwr_ctrl_bb_he_tb(struct rtw89_dev *rtwdev,
2923 					  enum rtw89_phy_idx phy,
2924 					  enum rtw89_rf_path path)
2925 {
2926 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2927 				 &rtw8852c_tssi_txpwr_ctrl_bb_he_tb_defs_a_tbl,
2928 				 &rtw8852c_tssi_txpwr_ctrl_bb_he_tb_defs_b_tbl);
2929 }
2930 
2931 static void _tssi_set_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2932 			  enum rtw89_rf_path path)
2933 {
2934 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2935 	enum rtw89_band band = chan->band_type;
2936 
2937 	if (path == RF_PATH_A) {
2938 		rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_dck_defs_a_tbl);
2939 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2940 					 &rtw8852c_tssi_dck_defs_2g_a_tbl,
2941 					 &rtw8852c_tssi_dck_defs_5g_a_tbl);
2942 	} else {
2943 		rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_dck_defs_b_tbl);
2944 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
2945 					 &rtw8852c_tssi_dck_defs_2g_b_tbl,
2946 					 &rtw8852c_tssi_dck_defs_5g_b_tbl);
2947 	}
2948 }
2949 
2950 static void _tssi_set_bbgain_split(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2951 				   enum rtw89_rf_path path)
2952 {
2953 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
2954 				 &rtw8852c_tssi_set_bbgain_split_a_tbl,
2955 				 &rtw8852c_tssi_set_bbgain_split_b_tbl);
2956 }
2957 
2958 static void _tssi_set_tmeter_tbl(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
2959 				 enum rtw89_rf_path path)
2960 {
2961 #define RTW8852C_TSSI_GET_VAL(ptr, idx)			\
2962 ({							\
2963 	s8 *__ptr = (ptr);				\
2964 	u8 __idx = (idx), __i, __v;			\
2965 	u32 __val = 0;					\
2966 	for (__i = 0; __i < 4; __i++) {			\
2967 		__v = (__ptr[__idx + __i]);		\
2968 		__val |= (__v << (8 * __i));		\
2969 	}						\
2970 	__val;						\
2971 })
2972 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
2973 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
2974 	u8 ch = chan->channel;
2975 	u8 subband = chan->subband_type;
2976 	const s8 *thm_up_a = NULL;
2977 	const s8 *thm_down_a = NULL;
2978 	const s8 *thm_up_b = NULL;
2979 	const s8 *thm_down_b = NULL;
2980 	u8 thermal = 0xff;
2981 	s8 thm_ofst[64] = {0};
2982 	u32 tmp = 0;
2983 	u8 i, j;
2984 
2985 	switch (subband) {
2986 	default:
2987 	case RTW89_CH_2G:
2988 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_2ga_p;
2989 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_2ga_n;
2990 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_2gb_p;
2991 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_2gb_n;
2992 		break;
2993 	case RTW89_CH_5G_BAND_1:
2994 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[0];
2995 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[0];
2996 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[0];
2997 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[0];
2998 		break;
2999 	case RTW89_CH_5G_BAND_3:
3000 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[1];
3001 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[1];
3002 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[1];
3003 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[1];
3004 		break;
3005 	case RTW89_CH_5G_BAND_4:
3006 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_p[2];
3007 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_5ga_n[2];
3008 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_p[2];
3009 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_5gb_n[2];
3010 		break;
3011 	case RTW89_CH_6G_BAND_IDX0:
3012 	case RTW89_CH_6G_BAND_IDX1:
3013 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[0];
3014 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[0];
3015 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[0];
3016 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[0];
3017 		break;
3018 	case RTW89_CH_6G_BAND_IDX2:
3019 	case RTW89_CH_6G_BAND_IDX3:
3020 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[1];
3021 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[1];
3022 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[1];
3023 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[1];
3024 		break;
3025 	case RTW89_CH_6G_BAND_IDX4:
3026 	case RTW89_CH_6G_BAND_IDX5:
3027 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[2];
3028 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[2];
3029 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[2];
3030 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[2];
3031 		break;
3032 	case RTW89_CH_6G_BAND_IDX6:
3033 	case RTW89_CH_6G_BAND_IDX7:
3034 		thm_up_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_p[3];
3035 		thm_down_a = rtw89_8852c_trk_cfg.delta_swingidx_6ga_n[3];
3036 		thm_up_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_p[3];
3037 		thm_down_b = rtw89_8852c_trk_cfg.delta_swingidx_6gb_n[3];
3038 		break;
3039 	}
3040 
3041 	if (path == RF_PATH_A) {
3042 		thermal = tssi_info->thermal[RF_PATH_A];
3043 
3044 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3045 			    "[TSSI] ch=%d thermal_pathA=0x%x\n", ch, thermal);
3046 
3047 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_DIS, 0x0);
3048 		rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER_TRK, 0x1);
3049 
3050 		if (thermal == 0xff) {
3051 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, 32);
3052 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL, 32);
3053 
3054 			for (i = 0; i < 64; i += 4) {
3055 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, 0x0);
3056 
3057 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3058 					    "[TSSI] write 0x%x val=0x%08x\n",
3059 					    0x5c00 + i, 0x0);
3060 			}
3061 
3062 		} else {
3063 			rtw89_phy_write32_mask(rtwdev, R_P0_TMETER, B_P0_TMETER, thermal);
3064 			rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, B_P0_RFCTM_VAL,
3065 					       thermal);
3066 
3067 			i = 0;
3068 			for (j = 0; j < 32; j++)
3069 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3070 					      -thm_down_a[i++] :
3071 					      -thm_down_a[DELTA_SWINGIDX_SIZE - 1];
3072 
3073 			i = 1;
3074 			for (j = 63; j >= 32; j--)
3075 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3076 					      thm_up_a[i++] :
3077 					      thm_up_a[DELTA_SWINGIDX_SIZE - 1];
3078 
3079 			for (i = 0; i < 64; i += 4) {
3080 				tmp = RTW8852C_TSSI_GET_VAL(thm_ofst, i);
3081 				rtw89_phy_write32(rtwdev, R_P0_TSSI_BASE + i, tmp);
3082 
3083 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3084 					    "[TSSI] write 0x%x val=0x%08x\n",
3085 					    0x5c00 + i, tmp);
3086 			}
3087 		}
3088 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x1);
3089 		rtw89_phy_write32_mask(rtwdev, R_P0_RFCTM, R_P0_RFCTM_RDY, 0x0);
3090 
3091 	} else {
3092 		thermal = tssi_info->thermal[RF_PATH_B];
3093 
3094 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3095 			    "[TSSI] ch=%d thermal_pathB=0x%x\n", ch, thermal);
3096 
3097 		rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER_DIS, 0x0);
3098 		rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER_TRK, 0x1);
3099 
3100 		if (thermal == 0xff) {
3101 			rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER, 32);
3102 			rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, B_P1_RFCTM_VAL, 32);
3103 
3104 			for (i = 0; i < 64; i += 4) {
3105 				rtw89_phy_write32(rtwdev, R_TSSI_THOF + i, 0x0);
3106 
3107 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3108 					    "[TSSI] write 0x%x val=0x%08x\n",
3109 					    0x7c00 + i, 0x0);
3110 			}
3111 
3112 		} else {
3113 			rtw89_phy_write32_mask(rtwdev, R_P1_TMETER, B_P1_TMETER, thermal);
3114 			rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, B_P1_RFCTM_VAL,
3115 					       thermal);
3116 
3117 			i = 0;
3118 			for (j = 0; j < 32; j++)
3119 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3120 					      -thm_down_b[i++] :
3121 					      -thm_down_b[DELTA_SWINGIDX_SIZE - 1];
3122 
3123 			i = 1;
3124 			for (j = 63; j >= 32; j--)
3125 				thm_ofst[j] = i < DELTA_SWINGIDX_SIZE ?
3126 					      thm_up_b[i++] :
3127 					      thm_up_b[DELTA_SWINGIDX_SIZE - 1];
3128 
3129 			for (i = 0; i < 64; i += 4) {
3130 				tmp = RTW8852C_TSSI_GET_VAL(thm_ofst, i);
3131 				rtw89_phy_write32(rtwdev, R_TSSI_THOF + i, tmp);
3132 
3133 				rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3134 					    "[TSSI] write 0x%x val=0x%08x\n",
3135 					    0x7c00 + i, tmp);
3136 			}
3137 		}
3138 		rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, R_P1_RFCTM_RDY, 0x1);
3139 		rtw89_phy_write32_mask(rtwdev, R_P1_RFCTM, R_P1_RFCTM_RDY, 0x0);
3140 	}
3141 #undef RTW8852C_TSSI_GET_VAL
3142 }
3143 
3144 static void _tssi_slope_cal_org(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3145 				enum rtw89_rf_path path)
3146 {
3147 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3148 	enum rtw89_band band = chan->band_type;
3149 
3150 	if (path == RF_PATH_A) {
3151 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
3152 					 &rtw8852c_tssi_slope_cal_org_defs_2g_a_tbl,
3153 					 &rtw8852c_tssi_slope_cal_org_defs_5g_a_tbl);
3154 	} else {
3155 		rtw89_rfk_parser_by_cond(rtwdev, band == RTW89_BAND_2G,
3156 					 &rtw8852c_tssi_slope_cal_org_defs_2g_b_tbl,
3157 					 &rtw8852c_tssi_slope_cal_org_defs_5g_b_tbl);
3158 	}
3159 }
3160 
3161 static void _tssi_set_aligk_default(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3162 				    enum rtw89_rf_path path)
3163 {
3164 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3165 	enum rtw89_band band = chan->band_type;
3166 	const struct rtw89_rfk_tbl *tbl;
3167 
3168 	if (path == RF_PATH_A) {
3169 		if (band == RTW89_BAND_2G)
3170 			tbl = &rtw8852c_tssi_set_aligk_default_defs_2g_a_tbl;
3171 		else if (band == RTW89_BAND_6G)
3172 			tbl = &rtw8852c_tssi_set_aligk_default_defs_6g_a_tbl;
3173 		else
3174 			tbl = &rtw8852c_tssi_set_aligk_default_defs_5g_a_tbl;
3175 	} else {
3176 		if (band == RTW89_BAND_2G)
3177 			tbl = &rtw8852c_tssi_set_aligk_default_defs_2g_b_tbl;
3178 		else if (band == RTW89_BAND_6G)
3179 			tbl = &rtw8852c_tssi_set_aligk_default_defs_6g_b_tbl;
3180 		else
3181 			tbl = &rtw8852c_tssi_set_aligk_default_defs_5g_b_tbl;
3182 	}
3183 
3184 	rtw89_rfk_parser(rtwdev, tbl);
3185 }
3186 
3187 static void _tssi_set_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3188 			    enum rtw89_rf_path path)
3189 {
3190 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3191 				 &rtw8852c_tssi_slope_defs_a_tbl,
3192 				 &rtw8852c_tssi_slope_defs_b_tbl);
3193 }
3194 
3195 static void _tssi_run_slope(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3196 			    enum rtw89_rf_path path)
3197 {
3198 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3199 				 &rtw8852c_tssi_run_slope_defs_a_tbl,
3200 				 &rtw8852c_tssi_run_slope_defs_b_tbl);
3201 }
3202 
3203 static void _tssi_set_track(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3204 			    enum rtw89_rf_path path)
3205 {
3206 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3207 				 &rtw8852c_tssi_track_defs_a_tbl,
3208 				 &rtw8852c_tssi_track_defs_b_tbl);
3209 }
3210 
3211 static void _tssi_set_txagc_offset_mv_avg(struct rtw89_dev *rtwdev,
3212 					  enum rtw89_phy_idx phy,
3213 					  enum rtw89_rf_path path)
3214 {
3215 	rtw89_rfk_parser_by_cond(rtwdev, path == RF_PATH_A,
3216 				 &rtw8852c_tssi_txagc_ofst_mv_avg_defs_a_tbl,
3217 				 &rtw8852c_tssi_txagc_ofst_mv_avg_defs_b_tbl);
3218 }
3219 
3220 static void _tssi_enable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
3221 {
3222 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3223 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3224 
3225 	if (rtwdev->dbcc_en) {
3226 		if (phy == RTW89_PHY_0) {
3227 			path = RF_PATH_A;
3228 			path_max = RF_PATH_B;
3229 		} else if (phy == RTW89_PHY_1) {
3230 			path = RF_PATH_B;
3231 			path_max = RF_PATH_NUM_8852C;
3232 		}
3233 	}
3234 
3235 	for (i = path; i < path_max; i++) {
3236 		_tssi_set_track(rtwdev, phy, i);
3237 		_tssi_set_txagc_offset_mv_avg(rtwdev, phy, i);
3238 
3239 		rtw89_rfk_parser_by_cond(rtwdev, i == RF_PATH_A,
3240 					 &rtw8852c_tssi_enable_defs_a_tbl,
3241 					 &rtw8852c_tssi_enable_defs_b_tbl);
3242 
3243 		tssi_info->base_thermal[i] =
3244 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[i]);
3245 		rtwdev->is_tssi_mode[i] = true;
3246 	}
3247 }
3248 
3249 static void _tssi_disable(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
3250 {
3251 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3252 
3253 	if (rtwdev->dbcc_en) {
3254 		if (phy == RTW89_PHY_0) {
3255 			path = RF_PATH_A;
3256 			path_max = RF_PATH_B;
3257 		} else if (phy == RTW89_PHY_1) {
3258 			path = RF_PATH_B;
3259 			path_max = RF_PATH_NUM_8852C;
3260 		}
3261 	}
3262 
3263 	for (i = path; i < path_max; i++) {
3264 		if (i == RF_PATH_A) {
3265 			rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_disable_defs_a_tbl);
3266 			rtwdev->is_tssi_mode[RF_PATH_A] = false;
3267 		}  else if (i == RF_PATH_B) {
3268 			rtw89_rfk_parser(rtwdev, &rtw8852c_tssi_disable_defs_b_tbl);
3269 			rtwdev->is_tssi_mode[RF_PATH_B] = false;
3270 		}
3271 	}
3272 }
3273 
3274 static u32 _tssi_get_cck_group(struct rtw89_dev *rtwdev, u8 ch)
3275 {
3276 	switch (ch) {
3277 	case 1 ... 2:
3278 		return 0;
3279 	case 3 ... 5:
3280 		return 1;
3281 	case 6 ... 8:
3282 		return 2;
3283 	case 9 ... 11:
3284 		return 3;
3285 	case 12 ... 13:
3286 		return 4;
3287 	case 14:
3288 		return 5;
3289 	}
3290 
3291 	return 0;
3292 }
3293 
3294 #define TSSI_EXTRA_GROUP_BIT (BIT(31))
3295 #define TSSI_EXTRA_GROUP(idx) (TSSI_EXTRA_GROUP_BIT | (idx))
3296 #define IS_TSSI_EXTRA_GROUP(group) ((group) & TSSI_EXTRA_GROUP_BIT)
3297 #define TSSI_EXTRA_GET_GROUP_IDX1(group) ((group) & ~TSSI_EXTRA_GROUP_BIT)
3298 #define TSSI_EXTRA_GET_GROUP_IDX2(group) (TSSI_EXTRA_GET_GROUP_IDX1(group) + 1)
3299 
3300 static u32 _tssi_get_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
3301 {
3302 	switch (ch) {
3303 	case 1 ... 2:
3304 		return 0;
3305 	case 3 ... 5:
3306 		return 1;
3307 	case 6 ... 8:
3308 		return 2;
3309 	case 9 ... 11:
3310 		return 3;
3311 	case 12 ... 14:
3312 		return 4;
3313 	case 36 ... 40:
3314 		return 5;
3315 	case 41 ... 43:
3316 		return TSSI_EXTRA_GROUP(5);
3317 	case 44 ... 48:
3318 		return 6;
3319 	case 49 ... 51:
3320 		return TSSI_EXTRA_GROUP(6);
3321 	case 52 ... 56:
3322 		return 7;
3323 	case 57 ... 59:
3324 		return TSSI_EXTRA_GROUP(7);
3325 	case 60 ... 64:
3326 		return 8;
3327 	case 100 ... 104:
3328 		return 9;
3329 	case 105 ... 107:
3330 		return TSSI_EXTRA_GROUP(9);
3331 	case 108 ... 112:
3332 		return 10;
3333 	case 113 ... 115:
3334 		return TSSI_EXTRA_GROUP(10);
3335 	case 116 ... 120:
3336 		return 11;
3337 	case 121 ... 123:
3338 		return TSSI_EXTRA_GROUP(11);
3339 	case 124 ... 128:
3340 		return 12;
3341 	case 129 ... 131:
3342 		return TSSI_EXTRA_GROUP(12);
3343 	case 132 ... 136:
3344 		return 13;
3345 	case 137 ... 139:
3346 		return TSSI_EXTRA_GROUP(13);
3347 	case 140 ... 144:
3348 		return 14;
3349 	case 149 ... 153:
3350 		return 15;
3351 	case 154 ... 156:
3352 		return TSSI_EXTRA_GROUP(15);
3353 	case 157 ... 161:
3354 		return 16;
3355 	case 162 ... 164:
3356 		return TSSI_EXTRA_GROUP(16);
3357 	case 165 ... 169:
3358 		return 17;
3359 	case 170 ... 172:
3360 		return TSSI_EXTRA_GROUP(17);
3361 	case 173 ... 177:
3362 		return 18;
3363 	}
3364 
3365 	return 0;
3366 }
3367 
3368 static u32 _tssi_get_6g_ofdm_group(struct rtw89_dev *rtwdev, u8 ch)
3369 {
3370 	switch (ch) {
3371 	case 1 ... 5:
3372 		return 0;
3373 	case 6 ... 8:
3374 		return TSSI_EXTRA_GROUP(0);
3375 	case 9 ... 13:
3376 		return 1;
3377 	case 14 ... 16:
3378 		return TSSI_EXTRA_GROUP(1);
3379 	case 17 ... 21:
3380 		return 2;
3381 	case 22 ... 24:
3382 		return TSSI_EXTRA_GROUP(2);
3383 	case 25 ... 29:
3384 		return 3;
3385 	case 33 ... 37:
3386 		return 4;
3387 	case 38 ... 40:
3388 		return TSSI_EXTRA_GROUP(4);
3389 	case 41 ... 45:
3390 		return 5;
3391 	case 46 ... 48:
3392 		return TSSI_EXTRA_GROUP(5);
3393 	case 49 ... 53:
3394 		return 6;
3395 	case 54 ... 56:
3396 		return TSSI_EXTRA_GROUP(6);
3397 	case 57 ... 61:
3398 		return 7;
3399 	case 65 ... 69:
3400 		return 8;
3401 	case 70 ... 72:
3402 		return TSSI_EXTRA_GROUP(8);
3403 	case 73 ... 77:
3404 		return 9;
3405 	case 78 ... 80:
3406 		return TSSI_EXTRA_GROUP(9);
3407 	case 81 ... 85:
3408 		return 10;
3409 	case 86 ... 88:
3410 		return TSSI_EXTRA_GROUP(10);
3411 	case 89 ... 93:
3412 		return 11;
3413 	case 97 ... 101:
3414 		return 12;
3415 	case 102 ... 104:
3416 		return TSSI_EXTRA_GROUP(12);
3417 	case 105 ... 109:
3418 		return 13;
3419 	case 110 ... 112:
3420 		return TSSI_EXTRA_GROUP(13);
3421 	case 113 ... 117:
3422 		return 14;
3423 	case 118 ... 120:
3424 		return TSSI_EXTRA_GROUP(14);
3425 	case 121 ... 125:
3426 		return 15;
3427 	case 129 ... 133:
3428 		return 16;
3429 	case 134 ... 136:
3430 		return TSSI_EXTRA_GROUP(16);
3431 	case 137 ... 141:
3432 		return 17;
3433 	case 142 ... 144:
3434 		return TSSI_EXTRA_GROUP(17);
3435 	case 145 ... 149:
3436 		return 18;
3437 	case 150 ... 152:
3438 		return TSSI_EXTRA_GROUP(18);
3439 	case 153 ... 157:
3440 		return 19;
3441 	case 161 ... 165:
3442 		return 20;
3443 	case 166 ... 168:
3444 		return TSSI_EXTRA_GROUP(20);
3445 	case 169 ... 173:
3446 		return 21;
3447 	case 174 ... 176:
3448 		return TSSI_EXTRA_GROUP(21);
3449 	case 177 ... 181:
3450 		return 22;
3451 	case 182 ... 184:
3452 		return TSSI_EXTRA_GROUP(22);
3453 	case 185 ... 189:
3454 		return 23;
3455 	case 193 ... 197:
3456 		return 24;
3457 	case 198 ... 200:
3458 		return TSSI_EXTRA_GROUP(24);
3459 	case 201 ... 205:
3460 		return 25;
3461 	case 206 ... 208:
3462 		return TSSI_EXTRA_GROUP(25);
3463 	case 209 ... 213:
3464 		return 26;
3465 	case 214 ... 216:
3466 		return TSSI_EXTRA_GROUP(26);
3467 	case 217 ... 221:
3468 		return 27;
3469 	case 225 ... 229:
3470 		return 28;
3471 	case 230 ... 232:
3472 		return TSSI_EXTRA_GROUP(28);
3473 	case 233 ... 237:
3474 		return 29;
3475 	case 238 ... 240:
3476 		return TSSI_EXTRA_GROUP(29);
3477 	case 241 ... 245:
3478 		return 30;
3479 	case 246 ... 248:
3480 		return TSSI_EXTRA_GROUP(30);
3481 	case 249 ... 253:
3482 		return 31;
3483 	}
3484 
3485 	return 0;
3486 }
3487 
3488 static u32 _tssi_get_trim_group(struct rtw89_dev *rtwdev, u8 ch)
3489 {
3490 	switch (ch) {
3491 	case 1 ... 8:
3492 		return 0;
3493 	case 9 ... 14:
3494 		return 1;
3495 	case 36 ... 48:
3496 		return 2;
3497 	case 49 ... 51:
3498 		return TSSI_EXTRA_GROUP(2);
3499 	case 52 ... 64:
3500 		return 3;
3501 	case 100 ... 112:
3502 		return 4;
3503 	case 113 ... 115:
3504 		return TSSI_EXTRA_GROUP(4);
3505 	case 116 ... 128:
3506 		return 5;
3507 	case 132 ... 144:
3508 		return 6;
3509 	case 149 ... 177:
3510 		return 7;
3511 	}
3512 
3513 	return 0;
3514 }
3515 
3516 static u32 _tssi_get_6g_trim_group(struct rtw89_dev *rtwdev, u8 ch)
3517 {
3518 	switch (ch) {
3519 	case 1 ... 13:
3520 		return 0;
3521 	case 14 ... 16:
3522 		return TSSI_EXTRA_GROUP(0);
3523 	case 17 ... 29:
3524 		return 1;
3525 	case 33 ... 45:
3526 		return 2;
3527 	case 46 ... 48:
3528 		return TSSI_EXTRA_GROUP(2);
3529 	case 49 ... 61:
3530 		return 3;
3531 	case 65 ... 77:
3532 		return 4;
3533 	case 78 ... 80:
3534 		return TSSI_EXTRA_GROUP(4);
3535 	case 81 ... 93:
3536 		return 5;
3537 	case 97 ... 109:
3538 		return 6;
3539 	case 110 ... 112:
3540 		return TSSI_EXTRA_GROUP(6);
3541 	case 113 ... 125:
3542 		return 7;
3543 	case 129 ... 141:
3544 		return 8;
3545 	case 142 ... 144:
3546 		return TSSI_EXTRA_GROUP(8);
3547 	case 145 ... 157:
3548 		return 9;
3549 	case 161 ... 173:
3550 		return 10;
3551 	case 174 ... 176:
3552 		return TSSI_EXTRA_GROUP(10);
3553 	case 177 ... 189:
3554 		return 11;
3555 	case 193 ... 205:
3556 		return 12;
3557 	case 206 ... 208:
3558 		return TSSI_EXTRA_GROUP(12);
3559 	case 209 ... 221:
3560 		return 13;
3561 	case 225 ... 237:
3562 		return 14;
3563 	case 238 ... 240:
3564 		return TSSI_EXTRA_GROUP(14);
3565 	case 241 ... 253:
3566 		return 15;
3567 	}
3568 
3569 	return 0;
3570 }
3571 
3572 static s8 _tssi_get_ofdm_de(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3573 			    enum rtw89_rf_path path)
3574 {
3575 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3576 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3577 	enum rtw89_band band = chan->band_type;
3578 	u8 ch = chan->channel;
3579 	u32 gidx, gidx_1st, gidx_2nd;
3580 	s8 de_1st;
3581 	s8 de_2nd;
3582 	s8 val;
3583 
3584 	if (band == RTW89_BAND_2G || band == RTW89_BAND_5G) {
3585 		gidx = _tssi_get_ofdm_group(rtwdev, ch);
3586 
3587 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3588 			    "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n",
3589 			    path, gidx);
3590 
3591 		if (IS_TSSI_EXTRA_GROUP(gidx)) {
3592 			gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
3593 			gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
3594 			de_1st = tssi_info->tssi_mcs[path][gidx_1st];
3595 			de_2nd = tssi_info->tssi_mcs[path][gidx_2nd];
3596 			val = (de_1st + de_2nd) / 2;
3597 
3598 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3599 				    "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
3600 				    path, val, de_1st, de_2nd);
3601 		} else {
3602 			val = tssi_info->tssi_mcs[path][gidx];
3603 
3604 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3605 				    "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
3606 		}
3607 	} else {
3608 		gidx = _tssi_get_6g_ofdm_group(rtwdev, ch);
3609 
3610 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3611 			    "[TSSI][TRIM]: path=%d mcs group_idx=0x%x\n",
3612 			    path, gidx);
3613 
3614 		if (IS_TSSI_EXTRA_GROUP(gidx)) {
3615 			gidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(gidx);
3616 			gidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(gidx);
3617 			de_1st = tssi_info->tssi_6g_mcs[path][gidx_1st];
3618 			de_2nd = tssi_info->tssi_6g_mcs[path][gidx_2nd];
3619 			val = (de_1st + de_2nd) / 2;
3620 
3621 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3622 				    "[TSSI][TRIM]: path=%d mcs de=%d 1st=%d 2nd=%d\n",
3623 				    path, val, de_1st, de_2nd);
3624 		} else {
3625 			val = tssi_info->tssi_6g_mcs[path][gidx];
3626 
3627 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3628 				    "[TSSI][TRIM]: path=%d mcs de=%d\n", path, val);
3629 		}
3630 	}
3631 
3632 	return val;
3633 }
3634 
3635 static s8 _tssi_get_ofdm_trim_de(struct rtw89_dev *rtwdev,
3636 				 enum rtw89_phy_idx phy,
3637 				 enum rtw89_rf_path path)
3638 {
3639 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3640 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3641 	enum rtw89_band band = chan->band_type;
3642 	u8 ch = chan->channel;
3643 	u32 tgidx, tgidx_1st, tgidx_2nd;
3644 	s8 tde_1st = 0;
3645 	s8 tde_2nd = 0;
3646 	s8 val;
3647 
3648 	if (band == RTW89_BAND_2G || band == RTW89_BAND_5G) {
3649 		tgidx = _tssi_get_trim_group(rtwdev, ch);
3650 
3651 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3652 			    "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
3653 			    path, tgidx);
3654 
3655 		if (IS_TSSI_EXTRA_GROUP(tgidx)) {
3656 			tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
3657 			tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
3658 			tde_1st = tssi_info->tssi_trim[path][tgidx_1st];
3659 			tde_2nd = tssi_info->tssi_trim[path][tgidx_2nd];
3660 			val = (tde_1st + tde_2nd) / 2;
3661 
3662 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3663 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
3664 				    path, val, tde_1st, tde_2nd);
3665 		} else {
3666 			val = tssi_info->tssi_trim[path][tgidx];
3667 
3668 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3669 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
3670 				    path, val);
3671 		}
3672 	} else {
3673 		tgidx = _tssi_get_6g_trim_group(rtwdev, ch);
3674 
3675 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3676 			    "[TSSI][TRIM]: path=%d mcs trim_group_idx=0x%x\n",
3677 			    path, tgidx);
3678 
3679 		if (IS_TSSI_EXTRA_GROUP(tgidx)) {
3680 			tgidx_1st = TSSI_EXTRA_GET_GROUP_IDX1(tgidx);
3681 			tgidx_2nd = TSSI_EXTRA_GET_GROUP_IDX2(tgidx);
3682 			tde_1st = tssi_info->tssi_trim_6g[path][tgidx_1st];
3683 			tde_2nd = tssi_info->tssi_trim_6g[path][tgidx_2nd];
3684 			val = (tde_1st + tde_2nd) / 2;
3685 
3686 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3687 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d 1st=%d 2nd=%d\n",
3688 				    path, val, tde_1st, tde_2nd);
3689 		} else {
3690 			val = tssi_info->tssi_trim_6g[path][tgidx];
3691 
3692 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3693 				    "[TSSI][TRIM]: path=%d mcs trim_de=%d\n",
3694 				    path, val);
3695 		}
3696 	}
3697 
3698 	return val;
3699 }
3700 
3701 static void _tssi_set_efuse_to_de(struct rtw89_dev *rtwdev,
3702 				  enum rtw89_phy_idx phy)
3703 {
3704 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
3705 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
3706 	u8 ch = chan->channel;
3707 	u8 gidx;
3708 	s8 ofdm_de;
3709 	s8 trim_de;
3710 	s32 val;
3711 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
3712 
3713 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI][TRIM]: phy=%d ch=%d\n",
3714 		    phy, ch);
3715 
3716 	if (rtwdev->dbcc_en) {
3717 		if (phy == RTW89_PHY_0) {
3718 			path = RF_PATH_A;
3719 			path_max = RF_PATH_B;
3720 		} else if (phy == RTW89_PHY_1) {
3721 			path = RF_PATH_B;
3722 			path_max = RF_PATH_NUM_8852C;
3723 		}
3724 	}
3725 
3726 	for (i = path; i < path_max; i++) {
3727 		gidx = _tssi_get_cck_group(rtwdev, ch);
3728 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
3729 		val = tssi_info->tssi_cck[i][gidx] + trim_de;
3730 
3731 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3732 			    "[TSSI][TRIM]: path=%d cck[%d]=0x%x trim=0x%x\n",
3733 			    i, gidx, tssi_info->tssi_cck[i][gidx], trim_de);
3734 
3735 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_long[i], _TSSI_DE_MASK, val);
3736 		rtw89_phy_write32_mask(rtwdev, _tssi_de_cck_short[i], _TSSI_DE_MASK, val);
3737 
3738 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3739 			    "[TSSI] Set TSSI CCK DE 0x%x[21:12]=0x%x\n",
3740 			    _tssi_de_cck_long[i],
3741 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_cck_long[i],
3742 						  _TSSI_DE_MASK));
3743 
3744 		ofdm_de = _tssi_get_ofdm_de(rtwdev, phy, i);
3745 		trim_de = _tssi_get_ofdm_trim_de(rtwdev, phy, i);
3746 		val = ofdm_de + trim_de;
3747 
3748 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3749 			    "[TSSI][TRIM]: path=%d mcs=0x%x trim=0x%x\n",
3750 			    i, ofdm_de, trim_de);
3751 
3752 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_20m[i], _TSSI_DE_MASK, val);
3753 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_40m[i], _TSSI_DE_MASK, val);
3754 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m[i], _TSSI_DE_MASK, val);
3755 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_80m_80m[i], _TSSI_DE_MASK, val);
3756 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_5m[i], _TSSI_DE_MASK, val);
3757 		rtw89_phy_write32_mask(rtwdev, _tssi_de_mcs_10m[i], _TSSI_DE_MASK, val);
3758 
3759 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
3760 			    "[TSSI] Set TSSI MCS DE 0x%x[21:12]=0x%x\n",
3761 			    _tssi_de_mcs_20m[i],
3762 			    rtw89_phy_read32_mask(rtwdev, _tssi_de_mcs_20m[i],
3763 						  _TSSI_DE_MASK));
3764 	}
3765 }
3766 
3767 static void rtw8852c_tssi_cont_en(struct rtw89_dev *rtwdev, bool en,
3768 				  enum rtw89_rf_path path)
3769 {
3770 	static const u32 tssi_trk[2] = {0x5818, 0x7818};
3771 	static const u32 tssi_en[2] = {0x5820, 0x7820};
3772 
3773 	if (en) {
3774 		rtw89_phy_write32_mask(rtwdev, tssi_trk[path], BIT(30), 0x0);
3775 		rtw89_phy_write32_mask(rtwdev, tssi_en[path], BIT(31), 0x0);
3776 		if (rtwdev->dbcc_en && path == RF_PATH_B)
3777 			_tssi_set_efuse_to_de(rtwdev, RTW89_PHY_1);
3778 		else
3779 			_tssi_set_efuse_to_de(rtwdev, RTW89_PHY_0);
3780 	} else {
3781 		rtw89_phy_write32_mask(rtwdev, tssi_trk[path], BIT(30), 0x1);
3782 		rtw89_phy_write32_mask(rtwdev, tssi_en[path], BIT(31), 0x1);
3783 	}
3784 }
3785 
3786 void rtw8852c_tssi_cont_en_phyidx(struct rtw89_dev *rtwdev, bool en, u8 phy_idx)
3787 {
3788 	if (!rtwdev->dbcc_en) {
3789 		rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_A);
3790 		rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_B);
3791 	} else {
3792 		if (phy_idx == RTW89_PHY_0)
3793 			rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_A);
3794 		else
3795 			rtw8852c_tssi_cont_en(rtwdev, en, RF_PATH_B);
3796 	}
3797 }
3798 
3799 static void _bw_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
3800 			enum rtw89_bandwidth bw, bool is_dav)
3801 {
3802 	u32 rf_reg18;
3803 	u32 reg_reg18_addr;
3804 
3805 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3806 	if (is_dav)
3807 		reg_reg18_addr = RR_CFGCH;
3808 	else
3809 		reg_reg18_addr = RR_CFGCH_V1;
3810 
3811 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK);
3812 	rf_reg18 &= ~RR_CFGCH_BW;
3813 
3814 	switch (bw) {
3815 	case RTW89_CHANNEL_WIDTH_5:
3816 	case RTW89_CHANNEL_WIDTH_10:
3817 	case RTW89_CHANNEL_WIDTH_20:
3818 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_20M);
3819 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x3);
3820 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xf);
3821 		break;
3822 	case RTW89_CHANNEL_WIDTH_40:
3823 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_40M);
3824 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x3);
3825 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xf);
3826 		break;
3827 	case RTW89_CHANNEL_WIDTH_80:
3828 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_80M);
3829 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x2);
3830 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xd);
3831 		break;
3832 	case RTW89_CHANNEL_WIDTH_160:
3833 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BW, CFGCH_BW_160M);
3834 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW0 | (path << 8), B_P0_CFCH_BW0, 0x1);
3835 		rtw89_phy_write32_mask(rtwdev, R_P0_CFCH_BW1 | (path << 8), B_P0_CFCH_BW1, 0xb);
3836 		break;
3837 	default:
3838 		break;
3839 	}
3840 
3841 	rtw89_write_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK, rf_reg18);
3842 }
3843 
3844 static void _ctrl_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3845 		     enum rtw89_bandwidth bw)
3846 {
3847 	bool is_dav;
3848 	u8 kpath, path;
3849 	u32 tmp = 0;
3850 
3851 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3852 	kpath = _kpath(rtwdev, phy);
3853 
3854 	for (path = 0; path < 2; path++) {
3855 		if (!(kpath & BIT(path)))
3856 			continue;
3857 
3858 		is_dav = true;
3859 		_bw_setting(rtwdev, path, bw, is_dav);
3860 		is_dav = false;
3861 		_bw_setting(rtwdev, path, bw, is_dav);
3862 		if (rtwdev->dbcc_en)
3863 			continue;
3864 
3865 		if (path == RF_PATH_B && rtwdev->hal.cv == CHIP_CAV) {
3866 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x0);
3867 			tmp = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
3868 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_APK, RR_APK_MOD, 0x3);
3869 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_CFGCH, RFREG_MASK, tmp);
3870 			fsleep(100);
3871 			rtw89_write_rf(rtwdev, RF_PATH_B, RR_RSV1, RR_RSV1_RST, 0x1);
3872 		}
3873 	}
3874 }
3875 
3876 static void _ch_setting(struct rtw89_dev *rtwdev, enum rtw89_rf_path path,
3877 			u8 central_ch, enum rtw89_band band, bool is_dav)
3878 {
3879 	u32 rf_reg18;
3880 	u32 reg_reg18_addr;
3881 
3882 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3883 	if (is_dav)
3884 		reg_reg18_addr = 0x18;
3885 	else
3886 		reg_reg18_addr = 0x10018;
3887 
3888 	rf_reg18 = rtw89_read_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK);
3889 	rf_reg18 &= ~(RR_CFGCH_BAND1 | RR_CFGCH_BAND0 | RR_CFGCH_CH);
3890 	rf_reg18 |= FIELD_PREP(RR_CFGCH_CH, central_ch);
3891 
3892 	switch (band) {
3893 	case RTW89_BAND_2G:
3894 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_2G);
3895 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_2G);
3896 		break;
3897 	case RTW89_BAND_5G:
3898 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_5G);
3899 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_5G);
3900 		break;
3901 	case RTW89_BAND_6G:
3902 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND1, CFGCH_BAND1_6G);
3903 		rf_reg18 |= FIELD_PREP(RR_CFGCH_BAND0, CFGCH_BAND0_6G);
3904 		break;
3905 	default:
3906 		break;
3907 	}
3908 	rtw89_write_rf(rtwdev, path, reg_reg18_addr, RFREG_MASK, rf_reg18);
3909 	fsleep(100);
3910 }
3911 
3912 static void _ctrl_ch(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3913 		     u8 central_ch, enum rtw89_band band)
3914 {
3915 	u8 kpath, path;
3916 
3917 	rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RFK]===>%s\n", __func__);
3918 	if (band != RTW89_BAND_6G) {
3919 		if ((central_ch > 14 && central_ch < 36) ||
3920 		    (central_ch > 64 && central_ch < 100) ||
3921 		    (central_ch > 144 && central_ch < 149) || central_ch > 177)
3922 			return;
3923 	} else {
3924 		if (central_ch > 253 || central_ch  == 2)
3925 			return;
3926 	}
3927 
3928 	kpath = _kpath(rtwdev, phy);
3929 
3930 	for (path = 0; path < 2; path++) {
3931 		if (kpath & BIT(path)) {
3932 			_ch_setting(rtwdev, path, central_ch, band, true);
3933 			_ch_setting(rtwdev, path, central_ch, band, false);
3934 		}
3935 	}
3936 }
3937 
3938 static void _rxbb_bw(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
3939 		     enum rtw89_bandwidth bw)
3940 {
3941 	u8 kpath;
3942 	u8 path;
3943 	u32 val;
3944 
3945 	kpath = _kpath(rtwdev, phy);
3946 	for (path = 0; path < 2; path++) {
3947 		if (!(kpath & BIT(path)))
3948 			continue;
3949 
3950 		rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x1);
3951 		rtw89_write_rf(rtwdev, path, RR_LUTWA, RR_LUTWA_M2, 0xa);
3952 		switch (bw) {
3953 		case RTW89_CHANNEL_WIDTH_20:
3954 			val = 0x1b;
3955 			break;
3956 		case RTW89_CHANNEL_WIDTH_40:
3957 			val = 0x13;
3958 			break;
3959 		case RTW89_CHANNEL_WIDTH_80:
3960 			val = 0xb;
3961 			break;
3962 		case RTW89_CHANNEL_WIDTH_160:
3963 		default:
3964 			val = 0x3;
3965 			break;
3966 		}
3967 		rtw89_write_rf(rtwdev, path, RR_LUTWD0, RR_LUTWD0_LB, val);
3968 		rtw89_write_rf(rtwdev, path, RR_LUTWE2, RR_LUTWE2_RTXBW, 0x0);
3969 	}
3970 }
3971 
3972 static void _lck_keep_thermal(struct rtw89_dev *rtwdev)
3973 {
3974 	struct rtw89_lck_info *lck = &rtwdev->lck;
3975 	int path;
3976 
3977 	for (path = 0; path < rtwdev->chip->rf_path_num; path++) {
3978 		lck->thermal[path] =
3979 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
3980 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
3981 			    "[LCK] path=%d thermal=0x%x", path, lck->thermal[path]);
3982 	}
3983 }
3984 
3985 static void _lck(struct rtw89_dev *rtwdev)
3986 {
3987 	u32 tmp18[2];
3988 	int path = rtwdev->dbcc_en ? 2 : 1;
3989 	int i;
3990 
3991 	rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK, "[LCK] DO LCK\n");
3992 
3993 	tmp18[0] = rtw89_read_rf(rtwdev, RF_PATH_A, RR_CFGCH, RFREG_MASK);
3994 	tmp18[1] = rtw89_read_rf(rtwdev, RF_PATH_B, RR_CFGCH, RFREG_MASK);
3995 
3996 	for (i = 0; i < path; i++) {
3997 		rtw89_write_rf(rtwdev, i, RR_LCK_TRG, RR_LCK_TRGSEL, 0x1);
3998 		rtw89_write_rf(rtwdev, i, RR_CFGCH, RFREG_MASK, tmp18[i]);
3999 		rtw89_write_rf(rtwdev, i, RR_LCK_TRG, RR_LCK_TRGSEL, 0x0);
4000 	}
4001 
4002 	_lck_keep_thermal(rtwdev);
4003 }
4004 
4005 #define RTW8852C_LCK_TH 8
4006 
4007 void rtw8852c_lck_track(struct rtw89_dev *rtwdev)
4008 {
4009 	struct rtw89_lck_info *lck = &rtwdev->lck;
4010 	u8 cur_thermal;
4011 	int delta;
4012 	int path;
4013 
4014 	for (path = 0; path < rtwdev->chip->rf_path_num; path++) {
4015 		cur_thermal =
4016 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4017 		delta = abs((int)cur_thermal - lck->thermal[path]);
4018 
4019 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
4020 			    "[LCK] path=%d current thermal=0x%x delta=0x%x\n",
4021 			    path, cur_thermal, delta);
4022 
4023 		if (delta >= RTW8852C_LCK_TH) {
4024 			_lck(rtwdev);
4025 			return;
4026 		}
4027 	}
4028 }
4029 
4030 void rtw8852c_lck_init(struct rtw89_dev *rtwdev)
4031 {
4032 	_lck_keep_thermal(rtwdev);
4033 }
4034 
4035 static
4036 void rtw8852c_ctrl_bw_ch(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
4037 			 u8 central_ch, enum rtw89_band band,
4038 			 enum rtw89_bandwidth bw)
4039 {
4040 	_ctrl_ch(rtwdev, phy, central_ch, band);
4041 	_ctrl_bw(rtwdev, phy, bw);
4042 	_rxbb_bw(rtwdev, phy, bw);
4043 }
4044 
4045 void rtw8852c_set_channel_rf(struct rtw89_dev *rtwdev,
4046 			     const struct rtw89_chan *chan,
4047 			     enum rtw89_phy_idx phy_idx)
4048 {
4049 	rtw8852c_ctrl_bw_ch(rtwdev, phy_idx, chan->channel,
4050 			    chan->band_type,
4051 			    chan->band_width);
4052 }
4053 
4054 void rtw8852c_mcc_get_ch_info(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
4055 {
4056 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
4057 	struct rtw89_rfk_mcc_info *rfk_mcc = &rtwdev->rfk_mcc;
4058 	u8 idx = rfk_mcc->table_idx;
4059 	int i;
4060 
4061 	for (i = 0; i < RTW89_IQK_CHS_NR; i++) {
4062 		if (rfk_mcc->ch[idx] == 0)
4063 			break;
4064 		if (++idx >= RTW89_IQK_CHS_NR)
4065 			idx = 0;
4066 	}
4067 
4068 	rfk_mcc->table_idx = idx;
4069 	rfk_mcc->ch[idx] = chan->channel;
4070 	rfk_mcc->band[idx] = chan->band_type;
4071 }
4072 
4073 void rtw8852c_rck(struct rtw89_dev *rtwdev)
4074 {
4075 	u8 path;
4076 
4077 	for (path = 0; path < 2; path++)
4078 		_rck(rtwdev, path);
4079 }
4080 
4081 void rtw8852c_dack(struct rtw89_dev *rtwdev)
4082 {
4083 	u8 phy_map = rtw89_btc_phymap(rtwdev, RTW89_PHY_0, 0);
4084 
4085 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_START);
4086 	_dac_cal(rtwdev, false);
4087 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DACK, BTC_WRFK_STOP);
4088 }
4089 
4090 void rtw8852c_iqk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
4091 {
4092 	u32 tx_en;
4093 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
4094 
4095 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_START);
4096 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
4097 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
4098 
4099 	_iqk_init(rtwdev);
4100 	_iqk(rtwdev, phy_idx, false);
4101 
4102 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
4103 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_IQK, BTC_WRFK_STOP);
4104 }
4105 
4106 #define RXDCK_VER_8852C 0xe
4107 
4108 static void _rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy,
4109 		    bool is_afe, u8 retry_limit)
4110 {
4111 	struct rtw89_rx_dck_info *rx_dck = &rtwdev->rx_dck;
4112 	u8 path, kpath;
4113 	u32 rf_reg5;
4114 	bool is_fail;
4115 	u8 rek_cnt;
4116 
4117 	kpath = _kpath(rtwdev, phy);
4118 	rtw89_debug(rtwdev, RTW89_DBG_RFK,
4119 		    "[RX_DCK] ****** RXDCK Start (Ver: 0x%x, Cv: %d) ******\n",
4120 		    RXDCK_VER_8852C, rtwdev->hal.cv);
4121 
4122 	for (path = 0; path < 2; path++) {
4123 		rf_reg5 = rtw89_read_rf(rtwdev, path, RR_RSV1, RFREG_MASK);
4124 		if (!(kpath & BIT(path)))
4125 			continue;
4126 
4127 		if (rtwdev->is_tssi_mode[path])
4128 			rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
4129 					       B_P0_TSSI_TRK_EN, 0x1);
4130 		rtw89_write_rf(rtwdev, path, RR_RSV1, RR_RSV1_RST, 0x0);
4131 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_MASK, RR_MOD_V_RX);
4132 		rtw89_write_rf(rtwdev, path, RR_MOD, RR_MOD_LO_SEL, rtwdev->dbcc_en);
4133 
4134 		for (rek_cnt = 0; rek_cnt < retry_limit; rek_cnt++) {
4135 			_set_rx_dck(rtwdev, phy, path, is_afe);
4136 
4137 			/* To reduce IO of dck_rek_check(), the last try is seen
4138 			 * as failure always, and then do recovery procedure.
4139 			 */
4140 			if (rek_cnt == retry_limit - 1) {
4141 				_rx_dck_recover(rtwdev, path);
4142 				break;
4143 			}
4144 
4145 			is_fail = _rx_dck_rek_check(rtwdev, path);
4146 			if (!is_fail)
4147 				break;
4148 		}
4149 
4150 		rtw89_debug(rtwdev, RTW89_DBG_RFK, "[RX_DCK] rek_cnt[%d]=%d",
4151 			    path, rek_cnt);
4152 
4153 		rx_dck->thermal[path] = ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4154 		rtw89_write_rf(rtwdev, path, RR_RSV1, RFREG_MASK, rf_reg5);
4155 
4156 		if (rtwdev->is_tssi_mode[path])
4157 			rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK + (path << 13),
4158 					       B_P0_TSSI_TRK_EN, 0x0);
4159 	}
4160 }
4161 
4162 void rtw8852c_rx_dck(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy, bool is_afe)
4163 {
4164 	_rx_dck(rtwdev, phy, is_afe, 1);
4165 }
4166 
4167 #define RTW8852C_RX_DCK_TH 12
4168 
4169 void rtw8852c_rx_dck_track(struct rtw89_dev *rtwdev)
4170 {
4171 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0);
4172 	struct rtw89_rx_dck_info *rx_dck = &rtwdev->rx_dck;
4173 	enum rtw89_phy_idx phy_idx = RTW89_PHY_0;
4174 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
4175 	u8 dck_channel;
4176 	u8 cur_thermal;
4177 	u32 tx_en;
4178 	int delta;
4179 	int path;
4180 
4181 	if (chan->band_type == RTW89_BAND_2G)
4182 		return;
4183 
4184 	if (rtwdev->scanning)
4185 		return;
4186 
4187 	for (path = 0; path < RF_PATH_NUM_8852C; path++) {
4188 		cur_thermal =
4189 			ewma_thermal_read(&rtwdev->phystat.avg_thermal[path]);
4190 		delta = abs((int)cur_thermal - rx_dck->thermal[path]);
4191 
4192 		rtw89_debug(rtwdev, RTW89_DBG_RFK_TRACK,
4193 			    "[RX_DCK] path=%d current thermal=0x%x delta=0x%x\n",
4194 			    path, cur_thermal, delta);
4195 
4196 		if (delta >= RTW8852C_RX_DCK_TH)
4197 			goto trigger_rx_dck;
4198 	}
4199 
4200 	return;
4201 
4202 trigger_rx_dck:
4203 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_START);
4204 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
4205 
4206 	for (path = 0; path < RF_PATH_NUM_8852C; path++) {
4207 		dck_channel = _rx_dck_channel_calc(rtwdev, chan);
4208 		_ctrl_ch(rtwdev, RTW89_PHY_0, dck_channel, chan->band_type);
4209 	}
4210 
4211 	_rx_dck(rtwdev, RTW89_PHY_0, false, 20);
4212 
4213 	for (path = 0; path < RF_PATH_NUM_8852C; path++)
4214 		_ctrl_ch(rtwdev, RTW89_PHY_0, chan->channel, chan->band_type);
4215 
4216 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
4217 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_RXDCK, BTC_WRFK_STOP);
4218 }
4219 
4220 void rtw8852c_dpk(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
4221 {
4222 	u32 tx_en;
4223 	u8 phy_map = rtw89_btc_phymap(rtwdev, phy_idx, 0);
4224 
4225 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_START);
4226 	rtw89_chip_stop_sch_tx(rtwdev, phy_idx, &tx_en, RTW89_SCH_TX_SEL_ALL);
4227 	_wait_rx_mode(rtwdev, _kpath(rtwdev, phy_idx));
4228 
4229 	rtwdev->dpk.is_dpk_enable = true;
4230 	rtwdev->dpk.is_dpk_reload_en = false;
4231 	_dpk(rtwdev, phy_idx, false);
4232 
4233 	rtw89_chip_resume_sch_tx(rtwdev, phy_idx, tx_en);
4234 	rtw89_btc_ntfy_wl_rfk(rtwdev, phy_map, BTC_WRFKT_DPK, BTC_WRFK_STOP);
4235 }
4236 
4237 void rtw8852c_dpk_track(struct rtw89_dev *rtwdev)
4238 {
4239 	_dpk_track(rtwdev);
4240 }
4241 
4242 void rtw8852c_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
4243 {
4244 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
4245 
4246 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n", __func__, phy);
4247 
4248 	if (rtwdev->dbcc_en) {
4249 		if (phy == RTW89_PHY_0) {
4250 			path = RF_PATH_A;
4251 			path_max = RF_PATH_B;
4252 		} else if (phy == RTW89_PHY_1) {
4253 			path = RF_PATH_B;
4254 			path_max = RF_PATH_NUM_8852C;
4255 		}
4256 	}
4257 
4258 	_tssi_disable(rtwdev, phy);
4259 
4260 	for (i = path; i < path_max; i++) {
4261 		_tssi_set_sys(rtwdev, phy, i);
4262 		_tssi_ini_txpwr_ctrl_bb(rtwdev, phy, i);
4263 		_tssi_ini_txpwr_ctrl_bb_he_tb(rtwdev, phy, i);
4264 		_tssi_set_dck(rtwdev, phy, i);
4265 		_tssi_set_bbgain_split(rtwdev, phy, i);
4266 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
4267 		_tssi_slope_cal_org(rtwdev, phy, i);
4268 		_tssi_set_aligk_default(rtwdev, phy, i);
4269 		_tssi_set_slope(rtwdev, phy, i);
4270 		_tssi_run_slope(rtwdev, phy, i);
4271 	}
4272 
4273 	_tssi_enable(rtwdev, phy);
4274 	_tssi_set_efuse_to_de(rtwdev, phy);
4275 }
4276 
4277 void rtw8852c_tssi_scan(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy)
4278 {
4279 	u32 i, path = RF_PATH_A, path_max = RF_PATH_NUM_8852C;
4280 
4281 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "[TSSI] %s: phy=%d\n",
4282 		    __func__, phy);
4283 
4284 	if (!rtwdev->is_tssi_mode[RF_PATH_A])
4285 		return;
4286 	if (!rtwdev->is_tssi_mode[RF_PATH_B])
4287 		return;
4288 
4289 	if (rtwdev->dbcc_en) {
4290 		if (phy == RTW89_PHY_0) {
4291 			path = RF_PATH_A;
4292 			path_max = RF_PATH_B;
4293 		} else if (phy == RTW89_PHY_1) {
4294 			path = RF_PATH_B;
4295 			path_max = RF_PATH_NUM_8852C;
4296 		}
4297 	}
4298 
4299 	_tssi_disable(rtwdev, phy);
4300 
4301 	for (i = path; i < path_max; i++) {
4302 		_tssi_set_sys(rtwdev, phy, i);
4303 		_tssi_set_dck(rtwdev, phy, i);
4304 		_tssi_set_tmeter_tbl(rtwdev, phy, i);
4305 		_tssi_slope_cal_org(rtwdev, phy, i);
4306 		_tssi_set_aligk_default(rtwdev, phy, i);
4307 	}
4308 
4309 	_tssi_enable(rtwdev, phy);
4310 	_tssi_set_efuse_to_de(rtwdev, phy);
4311 }
4312 
4313 static void rtw8852c_tssi_default_txagc(struct rtw89_dev *rtwdev,
4314 					enum rtw89_phy_idx phy, bool enable)
4315 {
4316 	struct rtw89_tssi_info *tssi_info = &rtwdev->tssi;
4317 	u8 i;
4318 
4319 	if (!rtwdev->is_tssi_mode[RF_PATH_A] && !rtwdev->is_tssi_mode[RF_PATH_B])
4320 		return;
4321 
4322 	if (enable) {
4323 		/* SCAN_START */
4324 		if (rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB, B_TXAGC_BB_OFT) != 0xc000 &&
4325 		    rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB, B_TXAGC_BB_OFT) != 0x0) {
4326 			for (i = 0; i < 6; i++) {
4327 				tssi_info->default_txagc_offset[RF_PATH_A] =
4328 					rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB,
4329 							      B_TXAGC_BB);
4330 				if (tssi_info->default_txagc_offset[RF_PATH_A])
4331 					break;
4332 			}
4333 		}
4334 
4335 		if (rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1, B_TXAGC_BB_S1_OFT) != 0xc000 &&
4336 		    rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1, B_TXAGC_BB_S1_OFT) != 0x0) {
4337 			for (i = 0; i < 6; i++) {
4338 				tssi_info->default_txagc_offset[RF_PATH_B] =
4339 					rtw89_phy_read32_mask(rtwdev, R_TXAGC_BB_S1,
4340 							      B_TXAGC_BB_S1);
4341 				if (tssi_info->default_txagc_offset[RF_PATH_B])
4342 					break;
4343 			}
4344 		}
4345 	} else {
4346 		/* SCAN_END */
4347 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT,
4348 				       tssi_info->default_txagc_offset[RF_PATH_A]);
4349 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT,
4350 				       tssi_info->default_txagc_offset[RF_PATH_B]);
4351 
4352 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x0);
4353 		rtw89_phy_write32_mask(rtwdev, R_P0_TSSI_TRK, B_P0_TSSI_OFT_EN, 0x1);
4354 
4355 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT_EN, 0x0);
4356 		rtw89_phy_write32_mask(rtwdev, R_P1_TSSI_TRK, B_P1_TSSI_OFT_EN, 0x1);
4357 	}
4358 }
4359 
4360 void rtw8852c_wifi_scan_notify(struct rtw89_dev *rtwdev,
4361 			       bool scan_start, enum rtw89_phy_idx phy_idx)
4362 {
4363 	if (scan_start)
4364 		rtw8852c_tssi_default_txagc(rtwdev, phy_idx, true);
4365 	else
4366 		rtw8852c_tssi_default_txagc(rtwdev, phy_idx, false);
4367 }
4368