xref: /linux/drivers/net/wireless/realtek/rtw89/rtw8852b_common.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2024  Realtek Corporation
3  */
4 
5 #include "coex.h"
6 #include "debug.h"
7 #include "mac.h"
8 #include "phy.h"
9 #include "reg.h"
10 #include "rtw8852b_common.h"
11 #include "util.h"
12 
13 static const struct rtw89_reg3_def rtw8852bx_pmac_ht20_mcs7_tbl[] = {
14 	{0x4580, 0x0000ffff, 0x0},
15 	{0x4580, 0xffff0000, 0x0},
16 	{0x4584, 0x0000ffff, 0x0},
17 	{0x4584, 0xffff0000, 0x0},
18 	{0x4580, 0x0000ffff, 0x1},
19 	{0x4578, 0x00ffffff, 0x2018b},
20 	{0x4570, 0x03ffffff, 0x7},
21 	{0x4574, 0x03ffffff, 0x32407},
22 	{0x45b8, 0x00000010, 0x0},
23 	{0x45b8, 0x00000100, 0x0},
24 	{0x45b8, 0x00000080, 0x0},
25 	{0x45b8, 0x00000008, 0x0},
26 	{0x45a0, 0x0000ff00, 0x0},
27 	{0x45a0, 0xff000000, 0x1},
28 	{0x45a4, 0x0000ff00, 0x2},
29 	{0x45a4, 0xff000000, 0x3},
30 	{0x45b8, 0x00000020, 0x0},
31 	{0x4568, 0xe0000000, 0x0},
32 	{0x45b8, 0x00000002, 0x1},
33 	{0x456c, 0xe0000000, 0x0},
34 	{0x45b4, 0x00006000, 0x0},
35 	{0x45b4, 0x00001800, 0x1},
36 	{0x45b8, 0x00000040, 0x0},
37 	{0x45b8, 0x00000004, 0x0},
38 	{0x45b8, 0x00000200, 0x0},
39 	{0x4598, 0xf8000000, 0x0},
40 	{0x45b8, 0x00100000, 0x0},
41 	{0x45a8, 0x00000fc0, 0x0},
42 	{0x45b8, 0x00200000, 0x0},
43 	{0x45b0, 0x00000038, 0x0},
44 	{0x45b0, 0x000001c0, 0x0},
45 	{0x45a0, 0x000000ff, 0x0},
46 	{0x45b8, 0x00400000, 0x0},
47 	{0x4590, 0x000007ff, 0x0},
48 	{0x45b0, 0x00000e00, 0x0},
49 	{0x45ac, 0x0000001f, 0x0},
50 	{0x45b8, 0x00800000, 0x0},
51 	{0x45a8, 0x0003f000, 0x0},
52 	{0x45b8, 0x01000000, 0x0},
53 	{0x45b0, 0x00007000, 0x0},
54 	{0x45b0, 0x00038000, 0x0},
55 	{0x45a0, 0x00ff0000, 0x0},
56 	{0x45b8, 0x02000000, 0x0},
57 	{0x4590, 0x003ff800, 0x0},
58 	{0x45b0, 0x001c0000, 0x0},
59 	{0x45ac, 0x000003e0, 0x0},
60 	{0x45b8, 0x04000000, 0x0},
61 	{0x45a8, 0x00fc0000, 0x0},
62 	{0x45b8, 0x08000000, 0x0},
63 	{0x45b0, 0x00e00000, 0x0},
64 	{0x45b0, 0x07000000, 0x0},
65 	{0x45a4, 0x000000ff, 0x0},
66 	{0x45b8, 0x10000000, 0x0},
67 	{0x4594, 0x000007ff, 0x0},
68 	{0x45b0, 0x38000000, 0x0},
69 	{0x45ac, 0x00007c00, 0x0},
70 	{0x45b8, 0x20000000, 0x0},
71 	{0x45a8, 0x3f000000, 0x0},
72 	{0x45b8, 0x40000000, 0x0},
73 	{0x45b4, 0x00000007, 0x0},
74 	{0x45b4, 0x00000038, 0x0},
75 	{0x45a4, 0x00ff0000, 0x0},
76 	{0x45b8, 0x80000000, 0x0},
77 	{0x4594, 0x003ff800, 0x0},
78 	{0x45b4, 0x000001c0, 0x0},
79 	{0x4598, 0xf8000000, 0x0},
80 	{0x45b8, 0x00100000, 0x0},
81 	{0x45a8, 0x00000fc0, 0x7},
82 	{0x45b8, 0x00200000, 0x0},
83 	{0x45b0, 0x00000038, 0x0},
84 	{0x45b0, 0x000001c0, 0x0},
85 	{0x45a0, 0x000000ff, 0x0},
86 	{0x45b4, 0x06000000, 0x0},
87 	{0x45b0, 0x00000007, 0x0},
88 	{0x45b8, 0x00080000, 0x0},
89 	{0x45a8, 0x0000003f, 0x0},
90 	{0x457c, 0xffe00000, 0x1},
91 	{0x4530, 0xffffffff, 0x0},
92 	{0x4588, 0x00003fff, 0x0},
93 	{0x4598, 0x000001ff, 0x0},
94 	{0x4534, 0xffffffff, 0x0},
95 	{0x4538, 0xffffffff, 0x0},
96 	{0x453c, 0xffffffff, 0x0},
97 	{0x4588, 0x0fffc000, 0x0},
98 	{0x4598, 0x0003fe00, 0x0},
99 	{0x4540, 0xffffffff, 0x0},
100 	{0x4544, 0xffffffff, 0x0},
101 	{0x4548, 0xffffffff, 0x0},
102 	{0x458c, 0x00003fff, 0x0},
103 	{0x4598, 0x07fc0000, 0x0},
104 	{0x454c, 0xffffffff, 0x0},
105 	{0x4550, 0xffffffff, 0x0},
106 	{0x4554, 0xffffffff, 0x0},
107 	{0x458c, 0x0fffc000, 0x0},
108 	{0x459c, 0x000001ff, 0x0},
109 	{0x4558, 0xffffffff, 0x0},
110 	{0x455c, 0xffffffff, 0x0},
111 	{0x4530, 0xffffffff, 0x4e790001},
112 	{0x4588, 0x00003fff, 0x0},
113 	{0x4598, 0x000001ff, 0x1},
114 	{0x4534, 0xffffffff, 0x0},
115 	{0x4538, 0xffffffff, 0x4b},
116 	{0x45ac, 0x38000000, 0x7},
117 	{0x4588, 0xf0000000, 0x0},
118 	{0x459c, 0x7e000000, 0x0},
119 	{0x45b8, 0x00040000, 0x0},
120 	{0x45b8, 0x00020000, 0x0},
121 	{0x4590, 0xffc00000, 0x0},
122 	{0x45b8, 0x00004000, 0x0},
123 	{0x4578, 0xff000000, 0x0},
124 	{0x45b8, 0x00000400, 0x0},
125 	{0x45b8, 0x00000800, 0x0},
126 	{0x45b8, 0x00001000, 0x0},
127 	{0x45b8, 0x00002000, 0x0},
128 	{0x45b4, 0x00018000, 0x0},
129 	{0x45ac, 0x07800000, 0x0},
130 	{0x45b4, 0x00000600, 0x2},
131 	{0x459c, 0x0001fe00, 0x80},
132 	{0x45ac, 0x00078000, 0x3},
133 	{0x459c, 0x01fe0000, 0x1},
134 };
135 
136 static const struct rtw89_reg3_def rtw8852bx_btc_preagc_en_defs[] = {
137 	{0x46D0, GENMASK(1, 0), 0x3},
138 	{0x4790, GENMASK(1, 0), 0x3},
139 	{0x4AD4, GENMASK(31, 0), 0xf},
140 	{0x4AE0, GENMASK(31, 0), 0xf},
141 	{0x4688, GENMASK(31, 24), 0x80},
142 	{0x476C, GENMASK(31, 24), 0x80},
143 	{0x4694, GENMASK(7, 0), 0x80},
144 	{0x4694, GENMASK(15, 8), 0x80},
145 	{0x4778, GENMASK(7, 0), 0x80},
146 	{0x4778, GENMASK(15, 8), 0x80},
147 	{0x4AE4, GENMASK(23, 0), 0x780D1E},
148 	{0x4AEC, GENMASK(23, 0), 0x780D1E},
149 	{0x469C, GENMASK(31, 26), 0x34},
150 	{0x49F0, GENMASK(31, 26), 0x34},
151 };
152 
153 static DECLARE_PHY_REG3_TBL(rtw8852bx_btc_preagc_en_defs);
154 
155 static const struct rtw89_reg3_def rtw8852bx_btc_preagc_dis_defs[] = {
156 	{0x46D0, GENMASK(1, 0), 0x0},
157 	{0x4790, GENMASK(1, 0), 0x0},
158 	{0x4AD4, GENMASK(31, 0), 0x60},
159 	{0x4AE0, GENMASK(31, 0), 0x60},
160 	{0x4688, GENMASK(31, 24), 0x1a},
161 	{0x476C, GENMASK(31, 24), 0x1a},
162 	{0x4694, GENMASK(7, 0), 0x2a},
163 	{0x4694, GENMASK(15, 8), 0x2a},
164 	{0x4778, GENMASK(7, 0), 0x2a},
165 	{0x4778, GENMASK(15, 8), 0x2a},
166 	{0x4AE4, GENMASK(23, 0), 0x79E99E},
167 	{0x4AEC, GENMASK(23, 0), 0x79E99E},
168 	{0x469C, GENMASK(31, 26), 0x26},
169 	{0x49F0, GENMASK(31, 26), 0x26},
170 };
171 
172 static DECLARE_PHY_REG3_TBL(rtw8852bx_btc_preagc_dis_defs);
173 
174 static void rtw8852be_efuse_parsing(struct rtw89_efuse *efuse,
175 				    struct rtw8852bx_efuse *map)
176 {
177 	ether_addr_copy(efuse->addr, map->e.mac_addr);
178 	efuse->rfe_type = map->rfe_type;
179 	efuse->xtal_cap = map->xtal_k;
180 }
181 
182 static void rtw8852bx_efuse_parsing_tssi(struct rtw89_dev *rtwdev,
183 					 struct rtw8852bx_efuse *map)
184 {
185 	struct rtw89_tssi_info *tssi = &rtwdev->tssi;
186 	struct rtw8852bx_tssi_offset *ofst[] = {&map->path_a_tssi, &map->path_b_tssi};
187 	u8 i, j;
188 
189 	tssi->thermal[RF_PATH_A] = map->path_a_therm;
190 	tssi->thermal[RF_PATH_B] = map->path_b_therm;
191 
192 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
193 		memcpy(tssi->tssi_cck[i], ofst[i]->cck_tssi,
194 		       sizeof(ofst[i]->cck_tssi));
195 
196 		for (j = 0; j < TSSI_CCK_CH_GROUP_NUM; j++)
197 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
198 				    "[TSSI][EFUSE] path=%d cck[%d]=0x%x\n",
199 				    i, j, tssi->tssi_cck[i][j]);
200 
201 		memcpy(tssi->tssi_mcs[i], ofst[i]->bw40_tssi,
202 		       sizeof(ofst[i]->bw40_tssi));
203 		memcpy(tssi->tssi_mcs[i] + TSSI_MCS_2G_CH_GROUP_NUM,
204 		       ofst[i]->bw40_1s_tssi_5g, sizeof(ofst[i]->bw40_1s_tssi_5g));
205 
206 		for (j = 0; j < TSSI_MCS_CH_GROUP_NUM; j++)
207 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
208 				    "[TSSI][EFUSE] path=%d mcs[%d]=0x%x\n",
209 				    i, j, tssi->tssi_mcs[i][j]);
210 	}
211 }
212 
213 static bool _decode_efuse_gain(u8 data, s8 *high, s8 *low)
214 {
215 	if (high)
216 		*high = sign_extend32(FIELD_GET(GENMASK(7,  4), data), 3);
217 	if (low)
218 		*low = sign_extend32(FIELD_GET(GENMASK(3,  0), data), 3);
219 
220 	return data != 0xff;
221 }
222 
223 static void rtw8852bx_efuse_parsing_gain_offset(struct rtw89_dev *rtwdev,
224 						struct rtw8852bx_efuse *map)
225 {
226 	struct rtw89_phy_efuse_gain *gain = &rtwdev->efuse_gain;
227 	bool valid = false;
228 
229 	valid |= _decode_efuse_gain(map->rx_gain_2g_cck,
230 				    &gain->offset[RF_PATH_A][RTW89_GAIN_OFFSET_2G_CCK],
231 				    &gain->offset[RF_PATH_B][RTW89_GAIN_OFFSET_2G_CCK]);
232 	valid |= _decode_efuse_gain(map->rx_gain_2g_ofdm,
233 				    &gain->offset[RF_PATH_A][RTW89_GAIN_OFFSET_2G_OFDM],
234 				    &gain->offset[RF_PATH_B][RTW89_GAIN_OFFSET_2G_OFDM]);
235 	valid |= _decode_efuse_gain(map->rx_gain_5g_low,
236 				    &gain->offset[RF_PATH_A][RTW89_GAIN_OFFSET_5G_LOW],
237 				    &gain->offset[RF_PATH_B][RTW89_GAIN_OFFSET_5G_LOW]);
238 	valid |= _decode_efuse_gain(map->rx_gain_5g_mid,
239 				    &gain->offset[RF_PATH_A][RTW89_GAIN_OFFSET_5G_MID],
240 				    &gain->offset[RF_PATH_B][RTW89_GAIN_OFFSET_5G_MID]);
241 	valid |= _decode_efuse_gain(map->rx_gain_5g_high,
242 				    &gain->offset[RF_PATH_A][RTW89_GAIN_OFFSET_5G_HIGH],
243 				    &gain->offset[RF_PATH_B][RTW89_GAIN_OFFSET_5G_HIGH]);
244 
245 	gain->offset_valid = valid;
246 }
247 
248 static int __rtw8852bx_read_efuse(struct rtw89_dev *rtwdev, u8 *log_map,
249 				  enum rtw89_efuse_block block)
250 {
251 	struct rtw89_efuse *efuse = &rtwdev->efuse;
252 	struct rtw8852bx_efuse *map;
253 
254 	map = (struct rtw8852bx_efuse *)log_map;
255 
256 	efuse->country_code[0] = map->country_code[0];
257 	efuse->country_code[1] = map->country_code[1];
258 	rtw8852bx_efuse_parsing_tssi(rtwdev, map);
259 	rtw8852bx_efuse_parsing_gain_offset(rtwdev, map);
260 
261 	switch (rtwdev->hci.type) {
262 	case RTW89_HCI_TYPE_PCIE:
263 		rtw8852be_efuse_parsing(efuse, map);
264 		break;
265 	default:
266 		return -EOPNOTSUPP;
267 	}
268 
269 	rtw89_info(rtwdev, "chip rfe_type is %d\n", efuse->rfe_type);
270 
271 	return 0;
272 }
273 
274 static void rtw8852bx_phycap_parsing_power_cal(struct rtw89_dev *rtwdev, u8 *phycap_map)
275 {
276 #define PWR_K_CHK_OFFSET 0x5E9
277 #define PWR_K_CHK_VALUE 0xAA
278 	u32 offset = PWR_K_CHK_OFFSET - rtwdev->chip->phycap_addr;
279 
280 	if (phycap_map[offset] == PWR_K_CHK_VALUE)
281 		rtwdev->efuse.power_k_valid = true;
282 }
283 
284 static void rtw8852bx_phycap_parsing_tssi(struct rtw89_dev *rtwdev, u8 *phycap_map)
285 {
286 	struct rtw89_tssi_info *tssi = &rtwdev->tssi;
287 	static const u32 tssi_trim_addr[RF_PATH_NUM_8852BX] = {0x5D6, 0x5AB};
288 	u32 addr = rtwdev->chip->phycap_addr;
289 	bool pg = false;
290 	u32 ofst;
291 	u8 i, j;
292 
293 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
294 		for (j = 0; j < TSSI_TRIM_CH_GROUP_NUM; j++) {
295 			/* addrs are in decreasing order */
296 			ofst = tssi_trim_addr[i] - addr - j;
297 			tssi->tssi_trim[i][j] = phycap_map[ofst];
298 
299 			if (phycap_map[ofst] != 0xff)
300 				pg = true;
301 		}
302 	}
303 
304 	if (!pg) {
305 		memset(tssi->tssi_trim, 0, sizeof(tssi->tssi_trim));
306 		rtw89_debug(rtwdev, RTW89_DBG_TSSI,
307 			    "[TSSI][TRIM] no PG, set all trim info to 0\n");
308 	}
309 
310 	for (i = 0; i < RF_PATH_NUM_8852BX; i++)
311 		for (j = 0; j < TSSI_TRIM_CH_GROUP_NUM; j++)
312 			rtw89_debug(rtwdev, RTW89_DBG_TSSI,
313 				    "[TSSI] path=%d idx=%d trim=0x%x addr=0x%x\n",
314 				    i, j, tssi->tssi_trim[i][j],
315 				    tssi_trim_addr[i] - j);
316 }
317 
318 static void rtw8852bx_phycap_parsing_thermal_trim(struct rtw89_dev *rtwdev,
319 						  u8 *phycap_map)
320 {
321 	struct rtw89_power_trim_info *info = &rtwdev->pwr_trim;
322 	static const u32 thm_trim_addr[RF_PATH_NUM_8852BX] = {0x5DF, 0x5DC};
323 	u32 addr = rtwdev->chip->phycap_addr;
324 	u8 i;
325 
326 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
327 		info->thermal_trim[i] = phycap_map[thm_trim_addr[i] - addr];
328 
329 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
330 			    "[THERMAL][TRIM] path=%d thermal_trim=0x%x\n",
331 			    i, info->thermal_trim[i]);
332 
333 		if (info->thermal_trim[i] != 0xff)
334 			info->pg_thermal_trim = true;
335 	}
336 }
337 
338 static void rtw8852bx_thermal_trim(struct rtw89_dev *rtwdev)
339 {
340 #define __thm_setting(raw)				\
341 ({							\
342 	u8 __v = (raw);					\
343 	((__v & 0x1) << 3) | ((__v & 0x1f) >> 1);	\
344 })
345 	struct rtw89_power_trim_info *info = &rtwdev->pwr_trim;
346 	u8 i, val;
347 
348 	if (!info->pg_thermal_trim) {
349 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
350 			    "[THERMAL][TRIM] no PG, do nothing\n");
351 
352 		return;
353 	}
354 
355 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
356 		val = __thm_setting(info->thermal_trim[i]);
357 		rtw89_write_rf(rtwdev, i, RR_TM2, RR_TM2_OFF, val);
358 
359 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
360 			    "[THERMAL][TRIM] path=%d thermal_setting=0x%x\n",
361 			    i, val);
362 	}
363 #undef __thm_setting
364 }
365 
366 static void rtw8852bx_phycap_parsing_pa_bias_trim(struct rtw89_dev *rtwdev,
367 						  u8 *phycap_map)
368 {
369 	struct rtw89_power_trim_info *info = &rtwdev->pwr_trim;
370 	static const u32 pabias_trim_addr[RF_PATH_NUM_8852BX] = {0x5DE, 0x5DB};
371 	u32 addr = rtwdev->chip->phycap_addr;
372 	u8 i;
373 
374 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
375 		info->pa_bias_trim[i] = phycap_map[pabias_trim_addr[i] - addr];
376 
377 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
378 			    "[PA_BIAS][TRIM] path=%d pa_bias_trim=0x%x\n",
379 			    i, info->pa_bias_trim[i]);
380 
381 		if (info->pa_bias_trim[i] != 0xff)
382 			info->pg_pa_bias_trim = true;
383 	}
384 }
385 
386 static void rtw8852bx_pa_bias_trim(struct rtw89_dev *rtwdev)
387 {
388 	struct rtw89_power_trim_info *info = &rtwdev->pwr_trim;
389 	u8 pabias_2g, pabias_5g;
390 	u8 i;
391 
392 	if (!info->pg_pa_bias_trim) {
393 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
394 			    "[PA_BIAS][TRIM] no PG, do nothing\n");
395 
396 		return;
397 	}
398 
399 	for (i = 0; i < RF_PATH_NUM_8852BX; i++) {
400 		pabias_2g = FIELD_GET(GENMASK(3, 0), info->pa_bias_trim[i]);
401 		pabias_5g = FIELD_GET(GENMASK(7, 4), info->pa_bias_trim[i]);
402 
403 		rtw89_debug(rtwdev, RTW89_DBG_RFK,
404 			    "[PA_BIAS][TRIM] path=%d 2G=0x%x 5G=0x%x\n",
405 			    i, pabias_2g, pabias_5g);
406 
407 		rtw89_write_rf(rtwdev, i, RR_BIASA, RR_BIASA_TXG, pabias_2g);
408 		rtw89_write_rf(rtwdev, i, RR_BIASA, RR_BIASA_TXA, pabias_5g);
409 	}
410 }
411 
412 static void rtw8852bx_phycap_parsing_gain_comp(struct rtw89_dev *rtwdev, u8 *phycap_map)
413 {
414 	static const u32 comp_addrs[][RTW89_SUBBAND_2GHZ_5GHZ_NR] = {
415 		{0x5BB, 0x5BA, 0, 0x5B9, 0x5B8},
416 		{0x590, 0x58F, 0, 0x58E, 0x58D},
417 	};
418 	struct rtw89_phy_efuse_gain *gain = &rtwdev->efuse_gain;
419 	u32 phycap_addr = rtwdev->chip->phycap_addr;
420 	bool valid = false;
421 	int path, i;
422 	u8 data;
423 
424 	for (path = 0; path < 2; path++)
425 		for (i = 0; i < RTW89_SUBBAND_2GHZ_5GHZ_NR; i++) {
426 			if (comp_addrs[path][i] == 0)
427 				continue;
428 
429 			data = phycap_map[comp_addrs[path][i] - phycap_addr];
430 			valid |= _decode_efuse_gain(data, NULL,
431 						    &gain->comp[path][i]);
432 		}
433 
434 	gain->comp_valid = valid;
435 }
436 
437 static int __rtw8852bx_read_phycap(struct rtw89_dev *rtwdev, u8 *phycap_map)
438 {
439 	rtw8852bx_phycap_parsing_power_cal(rtwdev, phycap_map);
440 	rtw8852bx_phycap_parsing_tssi(rtwdev, phycap_map);
441 	rtw8852bx_phycap_parsing_thermal_trim(rtwdev, phycap_map);
442 	rtw8852bx_phycap_parsing_pa_bias_trim(rtwdev, phycap_map);
443 	rtw8852bx_phycap_parsing_gain_comp(rtwdev, phycap_map);
444 
445 	return 0;
446 }
447 
448 static void __rtw8852bx_power_trim(struct rtw89_dev *rtwdev)
449 {
450 	rtw8852bx_thermal_trim(rtwdev);
451 	rtw8852bx_pa_bias_trim(rtwdev);
452 }
453 
454 static void __rtw8852bx_set_channel_mac(struct rtw89_dev *rtwdev,
455 					const struct rtw89_chan *chan,
456 					u8 mac_idx)
457 {
458 	u32 rf_mod = rtw89_mac_reg_by_idx(rtwdev, R_AX_WMAC_RFMOD, mac_idx);
459 	u32 sub_carr = rtw89_mac_reg_by_idx(rtwdev, R_AX_TX_SUB_CARRIER_VALUE, mac_idx);
460 	u32 chk_rate = rtw89_mac_reg_by_idx(rtwdev, R_AX_TXRATE_CHK, mac_idx);
461 	u8 txsc20 = 0, txsc40 = 0;
462 
463 	switch (chan->band_width) {
464 	case RTW89_CHANNEL_WIDTH_80:
465 		txsc40 = rtw89_phy_get_txsc(rtwdev, chan, RTW89_CHANNEL_WIDTH_40);
466 		fallthrough;
467 	case RTW89_CHANNEL_WIDTH_40:
468 		txsc20 = rtw89_phy_get_txsc(rtwdev, chan, RTW89_CHANNEL_WIDTH_20);
469 		break;
470 	default:
471 		break;
472 	}
473 
474 	switch (chan->band_width) {
475 	case RTW89_CHANNEL_WIDTH_80:
476 		rtw89_write8_mask(rtwdev, rf_mod, B_AX_WMAC_RFMOD_MASK, BIT(1));
477 		rtw89_write32(rtwdev, sub_carr, txsc20 | (txsc40 << 4));
478 		break;
479 	case RTW89_CHANNEL_WIDTH_40:
480 		rtw89_write8_mask(rtwdev, rf_mod, B_AX_WMAC_RFMOD_MASK, BIT(0));
481 		rtw89_write32(rtwdev, sub_carr, txsc20);
482 		break;
483 	case RTW89_CHANNEL_WIDTH_20:
484 		rtw89_write8_clr(rtwdev, rf_mod, B_AX_WMAC_RFMOD_MASK);
485 		rtw89_write32(rtwdev, sub_carr, 0);
486 		break;
487 	default:
488 		break;
489 	}
490 
491 	if (chan->channel > 14) {
492 		rtw89_write8_clr(rtwdev, chk_rate, B_AX_BAND_MODE);
493 		rtw89_write8_set(rtwdev, chk_rate,
494 				 B_AX_CHECK_CCK_EN | B_AX_RTS_LIMIT_IN_OFDM6);
495 	} else {
496 		rtw89_write8_set(rtwdev, chk_rate, B_AX_BAND_MODE);
497 		rtw89_write8_clr(rtwdev, chk_rate,
498 				 B_AX_CHECK_CCK_EN | B_AX_RTS_LIMIT_IN_OFDM6);
499 	}
500 }
501 
502 static const u32 rtw8852bx_sco_barker_threshold[14] = {
503 	0x1cfea, 0x1d0e1, 0x1d1d7, 0x1d2cd, 0x1d3c3, 0x1d4b9, 0x1d5b0, 0x1d6a6,
504 	0x1d79c, 0x1d892, 0x1d988, 0x1da7f, 0x1db75, 0x1ddc4
505 };
506 
507 static const u32 rtw8852bx_sco_cck_threshold[14] = {
508 	0x27de3, 0x27f35, 0x28088, 0x281da, 0x2832d, 0x2847f, 0x285d2, 0x28724,
509 	0x28877, 0x289c9, 0x28b1c, 0x28c6e, 0x28dc1, 0x290ed
510 };
511 
512 static void rtw8852bx_ctrl_sco_cck(struct rtw89_dev *rtwdev, u8 primary_ch)
513 {
514 	u8 ch_element = primary_ch - 1;
515 
516 	rtw89_phy_write32_mask(rtwdev, R_RXSCOBC, B_RXSCOBC_TH,
517 			       rtw8852bx_sco_barker_threshold[ch_element]);
518 	rtw89_phy_write32_mask(rtwdev, R_RXSCOCCK, B_RXSCOCCK_TH,
519 			       rtw8852bx_sco_cck_threshold[ch_element]);
520 }
521 
522 static u8 rtw8852bx_sco_mapping(u8 central_ch)
523 {
524 	if (central_ch == 1)
525 		return 109;
526 	else if (central_ch >= 2 && central_ch <= 6)
527 		return 108;
528 	else if (central_ch >= 7 && central_ch <= 10)
529 		return 107;
530 	else if (central_ch >= 11 && central_ch <= 14)
531 		return 106;
532 	else if (central_ch == 36 || central_ch == 38)
533 		return 51;
534 	else if (central_ch >= 40 && central_ch <= 58)
535 		return 50;
536 	else if (central_ch >= 60 && central_ch <= 64)
537 		return 49;
538 	else if (central_ch == 100 || central_ch == 102)
539 		return 48;
540 	else if (central_ch >= 104 && central_ch <= 126)
541 		return 47;
542 	else if (central_ch >= 128 && central_ch <= 151)
543 		return 46;
544 	else if (central_ch >= 153 && central_ch <= 177)
545 		return 45;
546 	else
547 		return 0;
548 }
549 
550 struct rtw8852bx_bb_gain {
551 	u32 gain_g[BB_PATH_NUM_8852BX];
552 	u32 gain_a[BB_PATH_NUM_8852BX];
553 	u32 gain_mask;
554 };
555 
556 static const struct rtw8852bx_bb_gain bb_gain_lna[LNA_GAIN_NUM] = {
557 	{ .gain_g = {0x4678, 0x475C}, .gain_a = {0x45DC, 0x4740},
558 	  .gain_mask = 0x00ff0000 },
559 	{ .gain_g = {0x4678, 0x475C}, .gain_a = {0x45DC, 0x4740},
560 	  .gain_mask = 0xff000000 },
561 	{ .gain_g = {0x467C, 0x4760}, .gain_a = {0x4660, 0x4744},
562 	  .gain_mask = 0x000000ff },
563 	{ .gain_g = {0x467C, 0x4760}, .gain_a = {0x4660, 0x4744},
564 	  .gain_mask = 0x0000ff00 },
565 	{ .gain_g = {0x467C, 0x4760}, .gain_a = {0x4660, 0x4744},
566 	  .gain_mask = 0x00ff0000 },
567 	{ .gain_g = {0x467C, 0x4760}, .gain_a = {0x4660, 0x4744},
568 	  .gain_mask = 0xff000000 },
569 	{ .gain_g = {0x4680, 0x4764}, .gain_a = {0x4664, 0x4748},
570 	  .gain_mask = 0x000000ff },
571 };
572 
573 static const struct rtw8852bx_bb_gain bb_gain_tia[TIA_GAIN_NUM] = {
574 	{ .gain_g = {0x4680, 0x4764}, .gain_a = {0x4664, 0x4748},
575 	  .gain_mask = 0x00ff0000 },
576 	{ .gain_g = {0x4680, 0x4764}, .gain_a = {0x4664, 0x4748},
577 	  .gain_mask = 0xff000000 },
578 };
579 
580 static void rtw8852bx_set_gain_error(struct rtw89_dev *rtwdev,
581 				     enum rtw89_subband subband,
582 				     enum rtw89_rf_path path)
583 {
584 	const struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax;
585 	u8 gain_band = rtw89_subband_to_bb_gain_band(subband);
586 	s32 val;
587 	u32 reg;
588 	u32 mask;
589 	int i;
590 
591 	for (i = 0; i < LNA_GAIN_NUM; i++) {
592 		if (subband == RTW89_CH_2G)
593 			reg = bb_gain_lna[i].gain_g[path];
594 		else
595 			reg = bb_gain_lna[i].gain_a[path];
596 
597 		mask = bb_gain_lna[i].gain_mask;
598 		val = gain->lna_gain[gain_band][path][i];
599 		rtw89_phy_write32_mask(rtwdev, reg, mask, val);
600 	}
601 
602 	for (i = 0; i < TIA_GAIN_NUM; i++) {
603 		if (subband == RTW89_CH_2G)
604 			reg = bb_gain_tia[i].gain_g[path];
605 		else
606 			reg = bb_gain_tia[i].gain_a[path];
607 
608 		mask = bb_gain_tia[i].gain_mask;
609 		val = gain->tia_gain[gain_band][path][i];
610 		rtw89_phy_write32_mask(rtwdev, reg, mask, val);
611 	}
612 }
613 
614 static void rtw8852bt_ext_loss_avg_update(struct rtw89_dev *rtwdev,
615 					  s8 ext_loss_a, s8 ext_loss_b)
616 {
617 	s8 ext_loss_avg;
618 	u64 linear;
619 	u8 pwrofst;
620 
621 	if (ext_loss_a == ext_loss_b) {
622 		ext_loss_avg = ext_loss_a;
623 	} else {
624 		linear = rtw89_db_2_linear(abs(ext_loss_a - ext_loss_b)) + 1;
625 		linear = DIV_ROUND_CLOSEST_ULL(linear / 2, 1 << RTW89_LINEAR_FRAC_BITS);
626 		ext_loss_avg = rtw89_linear_2_db(linear);
627 		ext_loss_avg += min(ext_loss_a, ext_loss_b);
628 	}
629 
630 	pwrofst = max(DIV_ROUND_CLOSEST(ext_loss_avg, 4) + 16, EDCCA_PWROFST_DEFAULT);
631 
632 	rtw89_phy_write32_mask(rtwdev, R_PWOFST, B_PWOFST, pwrofst);
633 }
634 
635 static void rtw8852bx_set_gain_offset(struct rtw89_dev *rtwdev,
636 				      enum rtw89_subband subband,
637 				      enum rtw89_phy_idx phy_idx)
638 {
639 	static const u32 gain_err_addr[2] = {R_P0_AGC_RSVD, R_P1_AGC_RSVD};
640 	static const u32 rssi_ofst_addr[2] = {R_PATH0_G_TIA1_LNA6_OP1DB_V1,
641 					      R_PATH1_G_TIA1_LNA6_OP1DB_V1};
642 	struct rtw89_hal *hal = &rtwdev->hal;
643 	struct rtw89_phy_efuse_gain *efuse_gain = &rtwdev->efuse_gain;
644 	enum rtw89_gain_offset gain_ofdm_band;
645 	s8 ext_loss_a = 0, ext_loss_b = 0;
646 	s32 offset_a, offset_b;
647 	s32 offset_ofdm, offset_cck;
648 	s32 tmp;
649 	u8 path;
650 
651 	if (!efuse_gain->comp_valid)
652 		goto next;
653 
654 	for (path = RF_PATH_A; path < BB_PATH_NUM_8852BX; path++) {
655 		tmp = efuse_gain->comp[path][subband];
656 		tmp = clamp_t(s32, tmp << 2, S8_MIN, S8_MAX);
657 		rtw89_phy_write32_mask(rtwdev, gain_err_addr[path], MASKBYTE0, tmp);
658 	}
659 
660 next:
661 	if (!efuse_gain->offset_valid)
662 		goto ext_loss;
663 
664 	gain_ofdm_band = rtw89_subband_to_gain_offset_band_of_ofdm(subband);
665 
666 	offset_a = -efuse_gain->offset[RF_PATH_A][gain_ofdm_band];
667 	offset_b = -efuse_gain->offset[RF_PATH_B][gain_ofdm_band];
668 
669 	tmp = -((offset_a << 2) + (efuse_gain->offset_base[RTW89_PHY_0] >> 2));
670 	tmp = clamp_t(s32, tmp, S8_MIN, S8_MAX);
671 	rtw89_phy_write32_mask(rtwdev, rssi_ofst_addr[RF_PATH_A], B_PATH0_R_G_OFST_MASK, tmp);
672 
673 	tmp = -((offset_b << 2) + (efuse_gain->offset_base[RTW89_PHY_0] >> 2));
674 	tmp = clamp_t(s32, tmp, S8_MIN, S8_MAX);
675 	rtw89_phy_write32_mask(rtwdev, rssi_ofst_addr[RF_PATH_B], B_PATH0_R_G_OFST_MASK, tmp);
676 
677 	if (hal->antenna_rx == RF_B) {
678 		offset_ofdm = -efuse_gain->offset[RF_PATH_B][gain_ofdm_band];
679 		offset_cck = -efuse_gain->offset[RF_PATH_B][0];
680 	} else {
681 		offset_ofdm = -efuse_gain->offset[RF_PATH_A][gain_ofdm_band];
682 		offset_cck = -efuse_gain->offset[RF_PATH_A][0];
683 	}
684 
685 	tmp = (offset_ofdm << 4) + efuse_gain->offset_base[RTW89_PHY_0];
686 	tmp = clamp_t(s32, tmp, S8_MIN, S8_MAX);
687 	rtw89_phy_write32_idx(rtwdev, R_P0_RPL1, B_P0_RPL1_BIAS_MASK, tmp, phy_idx);
688 
689 	tmp = (offset_ofdm << 4) + efuse_gain->rssi_base[RTW89_PHY_0];
690 	tmp = clamp_t(s32, tmp, S8_MIN, S8_MAX);
691 	rtw89_phy_write32_idx(rtwdev, R_P1_RPL1, B_P0_RPL1_BIAS_MASK, tmp, phy_idx);
692 
693 	if (subband == RTW89_CH_2G) {
694 		tmp = (offset_cck << 3) + (efuse_gain->offset_base[RTW89_PHY_0] >> 1);
695 		tmp = clamp_t(s32, tmp, S8_MIN >> 1, S8_MAX >> 1);
696 		rtw89_phy_write32_mask(rtwdev, R_RX_RPL_OFST,
697 				       B_RX_RPL_OFST_CCK_MASK, tmp);
698 	}
699 
700 	ext_loss_a = (offset_a << 2) + (efuse_gain->offset_base[RTW89_PHY_0] >> 2);
701 	ext_loss_b = (offset_b << 2) + (efuse_gain->offset_base[RTW89_PHY_0] >> 2);
702 
703 ext_loss:
704 	if (rtwdev->chip->chip_id == RTL8852BT)
705 		rtw8852bt_ext_loss_avg_update(rtwdev, ext_loss_a, ext_loss_b);
706 }
707 
708 static
709 void rtw8852bx_set_rxsc_rpl_comp(struct rtw89_dev *rtwdev, enum rtw89_subband subband)
710 {
711 	const struct rtw89_phy_bb_gain_info *gain = &rtwdev->bb_gain.ax;
712 	u8 band = rtw89_subband_to_bb_gain_band(subband);
713 	u32 val;
714 
715 	val = u32_encode_bits((gain->rpl_ofst_20[band][RF_PATH_A] +
716 			       gain->rpl_ofst_20[band][RF_PATH_B]) >> 1, B_P0_RPL1_20_MASK) |
717 	      u32_encode_bits((gain->rpl_ofst_40[band][RF_PATH_A][0] +
718 			       gain->rpl_ofst_40[band][RF_PATH_B][0]) >> 1, B_P0_RPL1_40_MASK) |
719 	      u32_encode_bits((gain->rpl_ofst_40[band][RF_PATH_A][1] +
720 			       gain->rpl_ofst_40[band][RF_PATH_B][1]) >> 1, B_P0_RPL1_41_MASK);
721 	val >>= B_P0_RPL1_SHIFT;
722 	rtw89_phy_write32_mask(rtwdev, R_P0_RPL1, B_P0_RPL1_MASK, val);
723 	rtw89_phy_write32_mask(rtwdev, R_P1_RPL1, B_P0_RPL1_MASK, val);
724 
725 	val = u32_encode_bits((gain->rpl_ofst_40[band][RF_PATH_A][2] +
726 			       gain->rpl_ofst_40[band][RF_PATH_B][2]) >> 1, B_P0_RTL2_42_MASK) |
727 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][0] +
728 			       gain->rpl_ofst_80[band][RF_PATH_B][0]) >> 1, B_P0_RTL2_80_MASK) |
729 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][1] +
730 			       gain->rpl_ofst_80[band][RF_PATH_B][1]) >> 1, B_P0_RTL2_81_MASK) |
731 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][10] +
732 			       gain->rpl_ofst_80[band][RF_PATH_B][10]) >> 1, B_P0_RTL2_8A_MASK);
733 	rtw89_phy_write32(rtwdev, R_P0_RPL2, val);
734 	rtw89_phy_write32(rtwdev, R_P1_RPL2, val);
735 
736 	val = u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][2] +
737 			       gain->rpl_ofst_80[band][RF_PATH_B][2]) >> 1, B_P0_RTL3_82_MASK) |
738 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][3] +
739 			       gain->rpl_ofst_80[band][RF_PATH_B][3]) >> 1, B_P0_RTL3_83_MASK) |
740 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][4] +
741 			       gain->rpl_ofst_80[band][RF_PATH_B][4]) >> 1, B_P0_RTL3_84_MASK) |
742 	      u32_encode_bits((gain->rpl_ofst_80[band][RF_PATH_A][9] +
743 			       gain->rpl_ofst_80[band][RF_PATH_B][9]) >> 1, B_P0_RTL3_89_MASK);
744 	rtw89_phy_write32(rtwdev, R_P0_RPL3, val);
745 	rtw89_phy_write32(rtwdev, R_P1_RPL3, val);
746 }
747 
748 static void rtw8852bx_ctrl_ch(struct rtw89_dev *rtwdev,
749 			      const struct rtw89_chan *chan,
750 			      enum rtw89_phy_idx phy_idx)
751 {
752 	u8 central_ch = chan->channel;
753 	u8 subband = chan->subband_type;
754 	u8 sco_comp;
755 	bool is_2g = central_ch <= 14;
756 
757 	/* Path A */
758 	if (is_2g)
759 		rtw89_phy_write32_idx(rtwdev, R_PATH0_BAND_SEL_V1,
760 				      B_PATH0_BAND_SEL_MSK_V1, 1, phy_idx);
761 	else
762 		rtw89_phy_write32_idx(rtwdev, R_PATH0_BAND_SEL_V1,
763 				      B_PATH0_BAND_SEL_MSK_V1, 0, phy_idx);
764 
765 	/* Path B */
766 	if (is_2g)
767 		rtw89_phy_write32_idx(rtwdev, R_PATH1_BAND_SEL_V1,
768 				      B_PATH1_BAND_SEL_MSK_V1, 1, phy_idx);
769 	else
770 		rtw89_phy_write32_idx(rtwdev, R_PATH1_BAND_SEL_V1,
771 				      B_PATH1_BAND_SEL_MSK_V1, 0, phy_idx);
772 
773 	/* SCO compensate FC setting */
774 	sco_comp = rtw8852bx_sco_mapping(central_ch);
775 	rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_INV, sco_comp, phy_idx);
776 
777 	if (chan->band_type == RTW89_BAND_6G)
778 		return;
779 
780 	/* CCK parameters */
781 	if (central_ch == 14) {
782 		rtw89_phy_write32_mask(rtwdev, R_TXFIR0, B_TXFIR_C01, 0x3b13ff);
783 		rtw89_phy_write32_mask(rtwdev, R_TXFIR2, B_TXFIR_C23, 0x1c42de);
784 		rtw89_phy_write32_mask(rtwdev, R_TXFIR4, B_TXFIR_C45, 0xfdb0ad);
785 		rtw89_phy_write32_mask(rtwdev, R_TXFIR6, B_TXFIR_C67, 0xf60f6e);
786 		rtw89_phy_write32_mask(rtwdev, R_TXFIR8, B_TXFIR_C89, 0xfd8f92);
787 		rtw89_phy_write32_mask(rtwdev, R_TXFIRA, B_TXFIR_CAB, 0x2d011);
788 		rtw89_phy_write32_mask(rtwdev, R_TXFIRC, B_TXFIR_CCD, 0x1c02c);
789 		rtw89_phy_write32_mask(rtwdev, R_TXFIRE, B_TXFIR_CEF, 0xfff00a);
790 	} else {
791 		rtw89_phy_write32_mask(rtwdev, R_TXFIR0, B_TXFIR_C01, 0x3d23ff);
792 		rtw89_phy_write32_mask(rtwdev, R_TXFIR2, B_TXFIR_C23, 0x29b354);
793 		rtw89_phy_write32_mask(rtwdev, R_TXFIR4, B_TXFIR_C45, 0xfc1c8);
794 		rtw89_phy_write32_mask(rtwdev, R_TXFIR6, B_TXFIR_C67, 0xfdb053);
795 		rtw89_phy_write32_mask(rtwdev, R_TXFIR8, B_TXFIR_C89, 0xf86f9a);
796 		rtw89_phy_write32_mask(rtwdev, R_TXFIRA, B_TXFIR_CAB, 0xfaef92);
797 		rtw89_phy_write32_mask(rtwdev, R_TXFIRC, B_TXFIR_CCD, 0xfe5fcc);
798 		rtw89_phy_write32_mask(rtwdev, R_TXFIRE, B_TXFIR_CEF, 0xffdff5);
799 	}
800 
801 	rtw8852bx_set_gain_error(rtwdev, subband, RF_PATH_A);
802 	rtw8852bx_set_gain_error(rtwdev, subband, RF_PATH_B);
803 	rtw8852bx_set_gain_offset(rtwdev, subband, phy_idx);
804 	rtw8852bx_set_rxsc_rpl_comp(rtwdev, subband);
805 }
806 
807 static void rtw8852b_bw_setting(struct rtw89_dev *rtwdev, u8 bw, u8 path)
808 {
809 	static const u32 adc_sel[2] = {0xC0EC, 0xC1EC};
810 	static const u32 wbadc_sel[2] = {0xC0E4, 0xC1E4};
811 
812 	switch (bw) {
813 	case RTW89_CHANNEL_WIDTH_5:
814 		rtw89_phy_write32_mask(rtwdev, adc_sel[path], 0x6000, 0x1);
815 		rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], 0x30, 0x0);
816 		break;
817 	case RTW89_CHANNEL_WIDTH_10:
818 		rtw89_phy_write32_mask(rtwdev, adc_sel[path], 0x6000, 0x2);
819 		rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], 0x30, 0x1);
820 		break;
821 	case RTW89_CHANNEL_WIDTH_20:
822 		rtw89_phy_write32_mask(rtwdev, adc_sel[path], 0x6000, 0x0);
823 		rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], 0x30, 0x2);
824 		break;
825 	case RTW89_CHANNEL_WIDTH_40:
826 		rtw89_phy_write32_mask(rtwdev, adc_sel[path], 0x6000, 0x0);
827 		rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], 0x30, 0x2);
828 		break;
829 	case RTW89_CHANNEL_WIDTH_80:
830 		rtw89_phy_write32_mask(rtwdev, adc_sel[path], 0x6000, 0x0);
831 		rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], 0x30, 0x2);
832 		break;
833 	default:
834 		rtw89_warn(rtwdev, "Fail to set ADC\n");
835 	}
836 }
837 
838 static
839 void rtw8852bt_adc_cfg(struct rtw89_dev *rtwdev, u8 bw, u8 path)
840 {
841 	static const u32 rck_reset_count[2] = {0xC0E8, 0xC1E8};
842 	static const u32 adc_op5_bw_sel[2] = {0xC0D8, 0xC1D8};
843 	static const u32 adc_sample_td[2] = {0xC0D4, 0xC1D4};
844 	static const u32 adc_rst_cycle[2] = {0xC0EC, 0xC1EC};
845 	static const u32 decim_filter[2] = {0xC0EC, 0xC1EC};
846 	static const u32 rck_offset[2] = {0xC0C4, 0xC1C4};
847 	static const u32 rx_adc_clk[2] = {0x12A0, 0x32A0};
848 	static const u32 wbadc_sel[2] = {0xC0E4, 0xC1E4};
849 	static const u32 idac2_1[2] = {0xC0D4, 0xC1D4};
850 	static const u32 idac2[2] = {0xC0D4, 0xC1D4};
851 	static const u32 upd_clk_adc = {0x704};
852 
853 	if (rtwdev->chip->chip_id != RTL8852BT)
854 		return;
855 
856 	rtw89_phy_write32_mask(rtwdev, idac2[path], B_P0_CFCH_CTL, 0x8);
857 	rtw89_phy_write32_mask(rtwdev, rck_reset_count[path], B_ADCMOD_LP, 0x9);
858 	rtw89_phy_write32_mask(rtwdev, wbadc_sel[path], B_WDADC_SEL, 0x2);
859 	rtw89_phy_write32_mask(rtwdev, rx_adc_clk[path], B_P0_RXCK_ADJ, 0x49);
860 	rtw89_phy_write32_mask(rtwdev, decim_filter[path], B_DCIM_FR, 0x0);
861 
862 	switch (bw) {
863 	case RTW89_CHANNEL_WIDTH_5:
864 	case RTW89_CHANNEL_WIDTH_10:
865 	case RTW89_CHANNEL_WIDTH_20:
866 	case RTW89_CHANNEL_WIDTH_40:
867 		rtw89_phy_write32_mask(rtwdev, idac2_1[path], B_P0_CFCH_EN, 0x2);
868 		rtw89_phy_write32_mask(rtwdev, adc_sample_td[path], B_P0_CFCH_BW0, 0x3);
869 		rtw89_phy_write32_mask(rtwdev, adc_op5_bw_sel[path], B_P0_CFCH_BW1, 0xf);
870 		rtw89_phy_write32_mask(rtwdev, rck_offset[path], B_DRCK_MUL, 0x0);
871 		/* Tx TSSI ADC update */
872 		rtw89_phy_write32_mask(rtwdev, upd_clk_adc, B_RSTB_ASYNC_BW80, 0);
873 
874 		if (rtwdev->efuse.rfe_type >= 51)
875 			rtw89_phy_write32_mask(rtwdev, adc_rst_cycle[path], B_DCIM_RC, 0x2);
876 		else
877 			rtw89_phy_write32_mask(rtwdev, adc_rst_cycle[path], B_DCIM_RC, 0x3);
878 		break;
879 	case RTW89_CHANNEL_WIDTH_80:
880 		rtw89_phy_write32_mask(rtwdev, idac2_1[path], B_P0_CFCH_EN, 0x2);
881 		rtw89_phy_write32_mask(rtwdev, adc_sample_td[path], B_P0_CFCH_BW0, 0x2);
882 		rtw89_phy_write32_mask(rtwdev, adc_op5_bw_sel[path], B_P0_CFCH_BW1, 0x8);
883 		rtw89_phy_write32_mask(rtwdev, rck_offset[path], B_DRCK_MUL, 0x0);
884 		rtw89_phy_write32_mask(rtwdev, adc_rst_cycle[path], B_DCIM_RC, 0x3);
885 		/* Tx TSSI ADC update */
886 		rtw89_phy_write32_mask(rtwdev, upd_clk_adc, B_RSTB_ASYNC_BW80, 1);
887 		break;
888 	case RTW89_CHANNEL_WIDTH_160:
889 		rtw89_phy_write32_mask(rtwdev, idac2_1[path], B_P0_CFCH_EN, 0x0);
890 		rtw89_phy_write32_mask(rtwdev, adc_sample_td[path], B_P0_CFCH_BW0, 0x2);
891 		rtw89_phy_write32_mask(rtwdev, adc_op5_bw_sel[path], B_P0_CFCH_BW1, 0x4);
892 		rtw89_phy_write32_mask(rtwdev, rck_offset[path], B_DRCK_MUL, 0x6);
893 		rtw89_phy_write32_mask(rtwdev, adc_rst_cycle[path], B_DCIM_RC, 0x3);
894 		/* Tx TSSI ADC update */
895 		rtw89_phy_write32_mask(rtwdev, upd_clk_adc, B_RSTB_ASYNC_BW80, 2);
896 		break;
897 	default:
898 		rtw89_warn(rtwdev, "Fail to set ADC\n");
899 		break;
900 	}
901 }
902 
903 static void rtw8852bx_ctrl_bw(struct rtw89_dev *rtwdev, u8 pri_ch, u8 bw,
904 			      enum rtw89_phy_idx phy_idx)
905 {
906 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
907 	u32 rx_path_0;
908 
909 	rx_path_0 = rtw89_phy_read32_idx(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, phy_idx);
910 
911 	switch (bw) {
912 	case RTW89_CHANNEL_WIDTH_5:
913 		rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_SET, 0x0, phy_idx);
914 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_SBW, 0x1, phy_idx);
915 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_PRICH, 0x0, phy_idx);
916 
917 		/*Set RF mode at 3 */
918 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
919 				      B_P0_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
920 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
921 				      B_P1_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
922 		if (chip_id == RTL8852BT) {
923 			rtw89_phy_write32_idx(rtwdev, R_PATH0_BAND_SEL_V1,
924 					      B_PATH0_BAND_NRBW_EN_V1, 0x0, phy_idx);
925 			rtw89_phy_write32_idx(rtwdev, R_PATH1_BAND_SEL_V1,
926 					      B_PATH1_BAND_NRBW_EN_V1, 0x0, phy_idx);
927 		}
928 		break;
929 	case RTW89_CHANNEL_WIDTH_10:
930 		rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_SET, 0x0, phy_idx);
931 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_SBW, 0x2, phy_idx);
932 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_PRICH, 0x0, phy_idx);
933 
934 		/*Set RF mode at 3 */
935 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
936 				      B_P0_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
937 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
938 				      B_P1_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
939 		if (chip_id == RTL8852BT) {
940 			rtw89_phy_write32_idx(rtwdev, R_PATH0_BAND_SEL_V1,
941 					      B_PATH0_BAND_NRBW_EN_V1, 0x0, phy_idx);
942 			rtw89_phy_write32_idx(rtwdev, R_PATH1_BAND_SEL_V1,
943 					      B_PATH1_BAND_NRBW_EN_V1, 0x0, phy_idx);
944 		}
945 		break;
946 	case RTW89_CHANNEL_WIDTH_20:
947 		rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_SET, 0x0, phy_idx);
948 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_SBW, 0x0, phy_idx);
949 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_PRICH, 0x0, phy_idx);
950 
951 		/*Set RF mode at 3 */
952 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
953 				      B_P0_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
954 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
955 				      B_P1_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
956 		if (chip_id == RTL8852BT) {
957 			rtw89_phy_write32_idx(rtwdev, R_PATH0_BAND_SEL_V1,
958 					      B_PATH0_BAND_NRBW_EN_V1, 0x1, phy_idx);
959 			rtw89_phy_write32_idx(rtwdev, R_PATH1_BAND_SEL_V1,
960 					      B_PATH1_BAND_NRBW_EN_V1, 0x1, phy_idx);
961 		}
962 		break;
963 	case RTW89_CHANNEL_WIDTH_40:
964 		rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_SET, 0x1, phy_idx);
965 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_SBW, 0x0, phy_idx);
966 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_PRICH,
967 				      pri_ch, phy_idx);
968 
969 		/*Set RF mode at 3 */
970 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
971 				      B_P0_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
972 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
973 				      B_P1_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
974 		/*CCK primary channel */
975 		if (pri_ch == RTW89_SC_20_UPPER)
976 			rtw89_phy_write32_mask(rtwdev, R_RXSC, B_RXSC_EN, 1);
977 		else
978 			rtw89_phy_write32_mask(rtwdev, R_RXSC, B_RXSC_EN, 0);
979 
980 		break;
981 	case RTW89_CHANNEL_WIDTH_80:
982 		rtw89_phy_write32_idx(rtwdev, R_FC0_BW_V1, B_FC0_BW_SET, 0x2, phy_idx);
983 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_SBW, 0x0, phy_idx);
984 		rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_CHBW_MOD_PRICH,
985 				      pri_ch, phy_idx);
986 
987 		/*Set RF mode at 3 */
988 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
989 				      B_P0_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
990 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
991 				      B_P1_RFMODE_ORI_RX_ALL, 0x333, phy_idx);
992 		break;
993 	default:
994 		rtw89_warn(rtwdev, "Fail to switch bw (bw:%d, pri ch:%d)\n", bw,
995 			   pri_ch);
996 	}
997 
998 	if (chip_id == RTL8852B) {
999 		rtw8852b_bw_setting(rtwdev, bw, RF_PATH_A);
1000 		rtw8852b_bw_setting(rtwdev, bw, RF_PATH_B);
1001 	} else if (chip_id == RTL8852BT) {
1002 		rtw8852bt_adc_cfg(rtwdev, bw, RF_PATH_A);
1003 		rtw8852bt_adc_cfg(rtwdev, bw, RF_PATH_B);
1004 	}
1005 
1006 	if (rx_path_0 == 0x1)
1007 		rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_ORI_RX,
1008 				      B_P1_RFMODE_ORI_RX_ALL, 0x111, phy_idx);
1009 	else if (rx_path_0 == 0x2)
1010 		rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_ORI_RX,
1011 				      B_P0_RFMODE_ORI_RX_ALL, 0x111, phy_idx);
1012 }
1013 
1014 static void rtw8852bx_ctrl_cck_en(struct rtw89_dev *rtwdev, bool cck_en)
1015 {
1016 	if (cck_en) {
1017 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK_ADC, B_ENABLE_CCK, 1);
1018 		rtw89_phy_write32_mask(rtwdev, R_RXCCA, B_RXCCA_DIS, 0);
1019 	} else {
1020 		rtw89_phy_write32_mask(rtwdev, R_UPD_CLK_ADC, B_ENABLE_CCK, 0);
1021 		rtw89_phy_write32_mask(rtwdev, R_RXCCA, B_RXCCA_DIS, 1);
1022 	}
1023 }
1024 
1025 static void rtw8852bx_5m_mask(struct rtw89_dev *rtwdev, const struct rtw89_chan *chan,
1026 			      enum rtw89_phy_idx phy_idx)
1027 {
1028 	u8 pri_ch = chan->pri_ch_idx;
1029 	bool mask_5m_low;
1030 	bool mask_5m_en;
1031 
1032 	switch (chan->band_width) {
1033 	case RTW89_CHANNEL_WIDTH_40:
1034 		/* Prich=1: Mask 5M High, Prich=2: Mask 5M Low */
1035 		mask_5m_en = true;
1036 		mask_5m_low = pri_ch == RTW89_SC_20_LOWER;
1037 		break;
1038 	case RTW89_CHANNEL_WIDTH_80:
1039 		/* Prich=3: Mask 5M High, Prich=4: Mask 5M Low, Else: Disable */
1040 		mask_5m_en = pri_ch == RTW89_SC_20_UPMOST ||
1041 			     pri_ch == RTW89_SC_20_LOWEST;
1042 		mask_5m_low = pri_ch == RTW89_SC_20_LOWEST;
1043 		break;
1044 	default:
1045 		mask_5m_en = false;
1046 		break;
1047 	}
1048 
1049 	if (!mask_5m_en) {
1050 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_EN, 0x0);
1051 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_EN, 0x0);
1052 		rtw89_phy_write32_idx(rtwdev, R_ASSIGN_SBD_OPT_V1,
1053 				      B_ASSIGN_SBD_OPT_EN_V1, 0x0, phy_idx);
1054 		return;
1055 	}
1056 
1057 	if (mask_5m_low) {
1058 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_TH, 0x4);
1059 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_EN, 0x1);
1060 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_SB2, 0x0);
1061 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_SB0, 0x1);
1062 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_TH, 0x4);
1063 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_EN, 0x1);
1064 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_SB2, 0x0);
1065 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_SB0, 0x1);
1066 	} else {
1067 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_TH, 0x4);
1068 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_EN, 0x1);
1069 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_SB2, 0x1);
1070 		rtw89_phy_write32_mask(rtwdev, R_PATH0_5MDET_V1, B_PATH0_5MDET_SB0, 0x0);
1071 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_TH, 0x4);
1072 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_EN, 0x1);
1073 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_SB2, 0x1);
1074 		rtw89_phy_write32_mask(rtwdev, R_PATH1_5MDET_V1, B_PATH1_5MDET_SB0, 0x0);
1075 	}
1076 	rtw89_phy_write32_idx(rtwdev, R_ASSIGN_SBD_OPT_V1,
1077 			      B_ASSIGN_SBD_OPT_EN_V1, 0x1, phy_idx);
1078 }
1079 
1080 static void __rtw8852bx_bb_reset_all(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
1081 {
1082 	rtw89_phy_write32_idx(rtwdev, R_S0_HW_SI_DIS, B_S0_HW_SI_DIS_W_R_TRIG, 0x7, phy_idx);
1083 	rtw89_phy_write32_idx(rtwdev, R_S1_HW_SI_DIS, B_S1_HW_SI_DIS_W_R_TRIG, 0x7, phy_idx);
1084 	fsleep(1);
1085 	rtw89_phy_write32_idx(rtwdev, R_RSTB_ASYNC, B_RSTB_ASYNC_ALL, 1, phy_idx);
1086 	rtw89_phy_write32_idx(rtwdev, R_RSTB_ASYNC, B_RSTB_ASYNC_ALL, 0, phy_idx);
1087 	rtw89_phy_write32_idx(rtwdev, R_S0_HW_SI_DIS, B_S0_HW_SI_DIS_W_R_TRIG, 0x0, phy_idx);
1088 	rtw89_phy_write32_idx(rtwdev, R_S1_HW_SI_DIS, B_S1_HW_SI_DIS_W_R_TRIG, 0x0, phy_idx);
1089 	rtw89_phy_write32_idx(rtwdev, R_RSTB_ASYNC, B_RSTB_ASYNC_ALL, 1, phy_idx);
1090 }
1091 
1092 static void rtw8852bx_bb_macid_ctrl_init(struct rtw89_dev *rtwdev,
1093 					 enum rtw89_phy_idx phy_idx)
1094 {
1095 	u32 addr;
1096 
1097 	for (addr = R_AX_PWR_MACID_LMT_TABLE0;
1098 	     addr <= R_AX_PWR_MACID_LMT_TABLE127; addr += 4)
1099 		rtw89_mac_txpwr_write32(rtwdev, phy_idx, addr, 0);
1100 }
1101 
1102 static void __rtw8852bx_bb_sethw(struct rtw89_dev *rtwdev)
1103 {
1104 	struct rtw89_phy_efuse_gain *gain = &rtwdev->efuse_gain;
1105 
1106 	rtw89_phy_write32_clr(rtwdev, R_P0_EN_SOUND_WO_NDP, B_P0_EN_SOUND_WO_NDP);
1107 	rtw89_phy_write32_clr(rtwdev, R_P1_EN_SOUND_WO_NDP, B_P1_EN_SOUND_WO_NDP);
1108 
1109 	rtw8852bx_bb_macid_ctrl_init(rtwdev, RTW89_PHY_0);
1110 
1111 	/* read these registers after loading BB parameters */
1112 	gain->offset_base[RTW89_PHY_0] =
1113 		rtw89_phy_read32_mask(rtwdev, R_P0_RPL1, B_P0_RPL1_BIAS_MASK);
1114 	gain->rssi_base[RTW89_PHY_0] =
1115 		rtw89_phy_read32_mask(rtwdev, R_P1_RPL1, B_P0_RPL1_BIAS_MASK);
1116 }
1117 
1118 static void rtw8852bx_bb_set_pop(struct rtw89_dev *rtwdev)
1119 {
1120 	if (rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR)
1121 		rtw89_phy_write32_clr(rtwdev, R_PKT_CTRL, B_PKT_POP_EN);
1122 }
1123 
1124 static u32 rtw8852bt_spur_freq(struct rtw89_dev *rtwdev,
1125 			       const struct rtw89_chan *chan)
1126 {
1127 	u8 center_chan = chan->channel;
1128 
1129 	switch (chan->band_type) {
1130 	case RTW89_BAND_5G:
1131 		if (center_chan == 151 || center_chan == 153 ||
1132 		    center_chan == 155 || center_chan == 163)
1133 			return 5760;
1134 		break;
1135 	default:
1136 		break;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 #define CARRIER_SPACING_312_5 312500 /* 312.5 kHz */
1143 #define CARRIER_SPACING_78_125 78125 /* 78.125 kHz */
1144 #define MAX_TONE_NUM 2048
1145 
1146 static void rtw8852bt_set_csi_tone_idx(struct rtw89_dev *rtwdev,
1147 				       const struct rtw89_chan *chan,
1148 				       enum rtw89_phy_idx phy_idx)
1149 {
1150 	s32 freq_diff, csi_idx, csi_tone_idx;
1151 	u32 spur_freq;
1152 
1153 	spur_freq = rtw8852bt_spur_freq(rtwdev, chan);
1154 	if (spur_freq == 0) {
1155 		rtw89_phy_write32_idx(rtwdev, R_SEG0CSI_EN_V1, B_SEG0CSI_EN,
1156 				      0, phy_idx);
1157 		return;
1158 	}
1159 
1160 	freq_diff = (spur_freq - chan->freq) * 1000000;
1161 	csi_idx = s32_div_u32_round_closest(freq_diff, CARRIER_SPACING_78_125);
1162 	s32_div_u32_round_down(csi_idx, MAX_TONE_NUM, &csi_tone_idx);
1163 
1164 	rtw89_phy_write32_idx(rtwdev, R_SEG0CSI_V1, B_SEG0CSI_IDX,
1165 			      csi_tone_idx, phy_idx);
1166 	rtw89_phy_write32_idx(rtwdev, R_SEG0CSI_EN_V1, B_SEG0CSI_EN, 1, phy_idx);
1167 }
1168 
1169 static
1170 void __rtw8852bx_set_channel_bb(struct rtw89_dev *rtwdev, const struct rtw89_chan *chan,
1171 				enum rtw89_phy_idx phy_idx)
1172 {
1173 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1174 	bool cck_en = chan->channel <= 14;
1175 	u8 pri_ch_idx = chan->pri_ch_idx;
1176 	u8 band = chan->band_type, chan_idx;
1177 
1178 	if (cck_en)
1179 		rtw8852bx_ctrl_sco_cck(rtwdev,  chan->primary_channel);
1180 
1181 	rtw8852bx_ctrl_ch(rtwdev, chan, phy_idx);
1182 	rtw8852bx_ctrl_bw(rtwdev, pri_ch_idx, chan->band_width, phy_idx);
1183 	rtw8852bx_ctrl_cck_en(rtwdev, cck_en);
1184 	if (chip_id == RTL8852BT)
1185 		rtw8852bt_set_csi_tone_idx(rtwdev, chan, phy_idx);
1186 	if (chip_id == RTL8852B && chan->band_type == RTW89_BAND_5G) {
1187 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BT_SHARE_V1,
1188 				       B_PATH0_BT_SHARE_V1, 0x0);
1189 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BTG_PATH_V1,
1190 				       B_PATH0_BTG_PATH_V1, 0x0);
1191 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BT_SHARE_V1,
1192 				       B_PATH1_BT_SHARE_V1, 0x0);
1193 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BTG_PATH_V1,
1194 				       B_PATH1_BTG_PATH_V1, 0x0);
1195 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_BT_SHARE, 0x0);
1196 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_BT_SEG0, 0x0);
1197 		rtw89_phy_write32_mask(rtwdev, R_BT_DYN_DC_EST_EN_V1,
1198 				       B_BT_DYN_DC_EST_EN_MSK, 0x0);
1199 		rtw89_phy_write32_mask(rtwdev, R_GNT_BT_WGT_EN, B_GNT_BT_WGT_EN, 0x0);
1200 	}
1201 	chan_idx = rtw89_encode_chan_idx(rtwdev, chan->primary_channel, band);
1202 	rtw89_phy_write32_mask(rtwdev, R_MAC_PIN_SEL, B_CH_IDX_SEG0, chan_idx);
1203 	rtw8852bx_5m_mask(rtwdev, chan, phy_idx);
1204 	rtw8852bx_bb_set_pop(rtwdev);
1205 	__rtw8852bx_bb_reset_all(rtwdev, phy_idx);
1206 }
1207 
1208 static u32 rtw8852bx_bb_cal_txpwr_ref(struct rtw89_dev *rtwdev,
1209 				      enum rtw89_phy_idx phy_idx, s16 ref)
1210 {
1211 	const u16 tssi_16dbm_cw = 0x12c;
1212 	const u8 base_cw_0db = 0x27;
1213 	const s8 ofst_int = 0;
1214 	s16 pwr_s10_3;
1215 	s16 rf_pwr_cw;
1216 	u16 bb_pwr_cw;
1217 	u32 pwr_cw;
1218 	u32 tssi_ofst_cw;
1219 
1220 	pwr_s10_3 = (ref << 1) + (s16)(ofst_int) + (s16)(base_cw_0db << 3);
1221 	bb_pwr_cw = u16_get_bits(pwr_s10_3, GENMASK(2, 0));
1222 	rf_pwr_cw = u16_get_bits(pwr_s10_3, GENMASK(8, 3));
1223 	rf_pwr_cw = clamp_t(s16, rf_pwr_cw, 15, 63);
1224 	pwr_cw = (rf_pwr_cw << 3) | bb_pwr_cw;
1225 
1226 	tssi_ofst_cw = (u32)((s16)tssi_16dbm_cw + (ref << 1) - (16 << 3));
1227 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1228 		    "[TXPWR] tssi_ofst_cw=%d rf_cw=0x%x bb_cw=0x%x\n",
1229 		    tssi_ofst_cw, rf_pwr_cw, bb_pwr_cw);
1230 
1231 	return u32_encode_bits(tssi_ofst_cw, B_DPD_TSSI_CW) |
1232 	       u32_encode_bits(pwr_cw, B_DPD_PWR_CW) |
1233 	       u32_encode_bits(ref, B_DPD_REF);
1234 }
1235 
1236 static void rtw8852bx_set_txpwr_ref(struct rtw89_dev *rtwdev,
1237 				    enum rtw89_phy_idx phy_idx)
1238 {
1239 	static const u32 addr[RF_PATH_NUM_8852BX] = {0x5800, 0x7800};
1240 	const u32 mask = B_DPD_TSSI_CW | B_DPD_PWR_CW | B_DPD_REF;
1241 	const u8 ofst_ofdm = 0x4;
1242 	const u8 ofst_cck = 0x8;
1243 	const s16 ref_ofdm = 0;
1244 	const s16 ref_cck = 0;
1245 	u32 val;
1246 	u8 i;
1247 
1248 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set txpwr reference\n");
1249 
1250 	rtw89_mac_txpwr_write32_mask(rtwdev, phy_idx, R_AX_PWR_RATE_CTRL,
1251 				     B_AX_PWR_REF, 0x0);
1252 
1253 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set bb ofdm txpwr ref\n");
1254 	val = rtw8852bx_bb_cal_txpwr_ref(rtwdev, phy_idx, ref_ofdm);
1255 
1256 	for (i = 0; i < RF_PATH_NUM_8852BX; i++)
1257 		rtw89_phy_write32_idx(rtwdev, addr[i] + ofst_ofdm, mask, val,
1258 				      phy_idx);
1259 
1260 	rtw89_debug(rtwdev, RTW89_DBG_TXPWR, "[TXPWR] set bb cck txpwr ref\n");
1261 	val = rtw8852bx_bb_cal_txpwr_ref(rtwdev, phy_idx, ref_cck);
1262 
1263 	for (i = 0; i < RF_PATH_NUM_8852BX; i++)
1264 		rtw89_phy_write32_idx(rtwdev, addr[i] + ofst_cck, mask, val,
1265 				      phy_idx);
1266 }
1267 
1268 static void rtw8852bx_bb_set_tx_shape_dfir(struct rtw89_dev *rtwdev,
1269 					   const struct rtw89_chan *chan,
1270 					   u8 tx_shape_idx,
1271 					   enum rtw89_phy_idx phy_idx)
1272 {
1273 #define __DFIR_CFG_ADDR(i) (R_TXFIR0 + ((i) << 2))
1274 #define __DFIR_CFG_MASK 0xffffffff
1275 #define __DFIR_CFG_NR 8
1276 #define __DECL_DFIR_PARAM(_name, _val...) \
1277 	static const u32 param_ ## _name[] = {_val}; \
1278 	static_assert(ARRAY_SIZE(param_ ## _name) == __DFIR_CFG_NR)
1279 
1280 	__DECL_DFIR_PARAM(flat,
1281 			  0x023D23FF, 0x0029B354, 0x000FC1C8, 0x00FDB053,
1282 			  0x00F86F9A, 0x06FAEF92, 0x00FE5FCC, 0x00FFDFF5);
1283 	__DECL_DFIR_PARAM(sharp,
1284 			  0x023D83FF, 0x002C636A, 0x0013F204, 0x00008090,
1285 			  0x00F87FB0, 0x06F99F83, 0x00FDBFBA, 0x00003FF5);
1286 	__DECL_DFIR_PARAM(sharp_14,
1287 			  0x023B13FF, 0x001C42DE, 0x00FDB0AD, 0x00F60F6E,
1288 			  0x00FD8F92, 0x0602D011, 0x0001C02C, 0x00FFF00A);
1289 	u8 ch = chan->channel;
1290 	const u32 *param;
1291 	u32 addr;
1292 	int i;
1293 
1294 	if (ch > 14) {
1295 		rtw89_warn(rtwdev,
1296 			   "set tx shape dfir by unknown ch: %d on 2G\n", ch);
1297 		return;
1298 	}
1299 
1300 	if (ch == 14)
1301 		param = param_sharp_14;
1302 	else
1303 		param = tx_shape_idx == 0 ? param_flat : param_sharp;
1304 
1305 	for (i = 0; i < __DFIR_CFG_NR; i++) {
1306 		addr = __DFIR_CFG_ADDR(i);
1307 		rtw89_debug(rtwdev, RTW89_DBG_TXPWR,
1308 			    "set tx shape dfir: 0x%x: 0x%x\n", addr, param[i]);
1309 		rtw89_phy_write32_idx(rtwdev, addr, __DFIR_CFG_MASK, param[i],
1310 				      phy_idx);
1311 	}
1312 
1313 #undef __DECL_DFIR_PARAM
1314 #undef __DFIR_CFG_NR
1315 #undef __DFIR_CFG_MASK
1316 #undef __DECL_CFG_ADDR
1317 }
1318 
1319 static void rtw8852bx_set_tx_shape(struct rtw89_dev *rtwdev,
1320 				   const struct rtw89_chan *chan,
1321 				   enum rtw89_phy_idx phy_idx)
1322 {
1323 	const struct rtw89_rfe_parms *rfe_parms = rtwdev->rfe_parms;
1324 	u8 band = chan->band_type;
1325 	u8 regd = rtw89_regd_get(rtwdev, band);
1326 	u8 tx_shape_cck = (*rfe_parms->tx_shape.lmt)[band][RTW89_RS_CCK][regd];
1327 	u8 tx_shape_ofdm = (*rfe_parms->tx_shape.lmt)[band][RTW89_RS_OFDM][regd];
1328 
1329 	if (band == RTW89_BAND_2G)
1330 		rtw8852bx_bb_set_tx_shape_dfir(rtwdev, chan, tx_shape_cck, phy_idx);
1331 
1332 	rtw89_phy_write32_mask(rtwdev, R_DCFO_OPT, B_TXSHAPE_TRIANGULAR_CFG,
1333 			       tx_shape_ofdm);
1334 }
1335 
1336 static void __rtw8852bx_set_txpwr(struct rtw89_dev *rtwdev,
1337 				  const struct rtw89_chan *chan,
1338 				  enum rtw89_phy_idx phy_idx)
1339 {
1340 	rtw89_phy_set_txpwr_byrate(rtwdev, chan, phy_idx);
1341 	rtw89_phy_set_txpwr_offset(rtwdev, chan, phy_idx);
1342 	rtw8852bx_set_tx_shape(rtwdev, chan, phy_idx);
1343 	rtw89_phy_set_txpwr_limit(rtwdev, chan, phy_idx);
1344 	rtw89_phy_set_txpwr_limit_ru(rtwdev, chan, phy_idx);
1345 }
1346 
1347 static void __rtw8852bx_set_txpwr_ctrl(struct rtw89_dev *rtwdev,
1348 				       enum rtw89_phy_idx phy_idx)
1349 {
1350 	rtw8852bx_set_txpwr_ref(rtwdev, phy_idx);
1351 }
1352 
1353 static
1354 void __rtw8852bx_set_txpwr_ul_tb_offset(struct rtw89_dev *rtwdev,
1355 					s8 pw_ofst, enum rtw89_mac_idx mac_idx)
1356 {
1357 	u32 reg;
1358 
1359 	if (pw_ofst < -16 || pw_ofst > 15) {
1360 		rtw89_warn(rtwdev, "[ULTB] Err pwr_offset=%d\n", pw_ofst);
1361 		return;
1362 	}
1363 
1364 	reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PWR_UL_TB_CTRL, mac_idx);
1365 	rtw89_write32_set(rtwdev, reg, B_AX_PWR_UL_TB_CTRL_EN);
1366 
1367 	reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PWR_UL_TB_1T, mac_idx);
1368 	rtw89_write32_mask(rtwdev, reg, B_AX_PWR_UL_TB_1T_MASK, pw_ofst);
1369 
1370 	pw_ofst = max_t(s8, pw_ofst - 3, -16);
1371 	reg = rtw89_mac_reg_by_idx(rtwdev, R_AX_PWR_UL_TB_2T, mac_idx);
1372 	rtw89_write32_mask(rtwdev, reg, B_AX_PWR_UL_TB_2T_MASK, pw_ofst);
1373 }
1374 
1375 static int
1376 __rtw8852bx_init_txpwr_unit(struct rtw89_dev *rtwdev, enum rtw89_phy_idx phy_idx)
1377 {
1378 	int ret;
1379 
1380 	ret = rtw89_mac_txpwr_write32(rtwdev, phy_idx, R_AX_PWR_UL_CTRL2, 0x07763333);
1381 	if (ret)
1382 		return ret;
1383 
1384 	ret = rtw89_mac_txpwr_write32(rtwdev, phy_idx, R_AX_PWR_COEXT_CTRL, 0x01ebf000);
1385 	if (ret)
1386 		return ret;
1387 
1388 	ret = rtw89_mac_txpwr_write32(rtwdev, phy_idx, R_AX_PWR_UL_CTRL0, 0x0002f8ff);
1389 	if (ret)
1390 		return ret;
1391 
1392 	rtw8852bx_set_txpwr_ul_tb_offset(rtwdev, 0, phy_idx == RTW89_PHY_1 ?
1393 						   RTW89_MAC_1 : RTW89_MAC_0);
1394 
1395 	return 0;
1396 }
1397 
1398 static
1399 void __rtw8852bx_bb_set_plcp_tx(struct rtw89_dev *rtwdev)
1400 {
1401 	const struct rtw89_reg3_def *def = rtw8852bx_pmac_ht20_mcs7_tbl;
1402 	u8 i;
1403 
1404 	for (i = 0; i < ARRAY_SIZE(rtw8852bx_pmac_ht20_mcs7_tbl); i++, def++)
1405 		rtw89_phy_write32_mask(rtwdev, def->addr, def->mask, def->data);
1406 }
1407 
1408 static void rtw8852bx_stop_pmac_tx(struct rtw89_dev *rtwdev,
1409 				   struct rtw8852bx_bb_pmac_info *tx_info,
1410 				   enum rtw89_phy_idx idx)
1411 {
1412 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC Stop Tx");
1413 	if (tx_info->mode == CONT_TX)
1414 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_PRD, B_PMAC_CTX_EN, 0, idx);
1415 	else if (tx_info->mode == PKTS_TX)
1416 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_PRD, B_PMAC_PTX_EN, 0, idx);
1417 }
1418 
1419 static void rtw8852bx_start_pmac_tx(struct rtw89_dev *rtwdev,
1420 				    struct rtw8852bx_bb_pmac_info *tx_info,
1421 				    enum rtw89_phy_idx idx)
1422 {
1423 	enum rtw8852bx_pmac_mode mode = tx_info->mode;
1424 	u32 pkt_cnt = tx_info->tx_cnt;
1425 	u16 period = tx_info->period;
1426 
1427 	if (mode == CONT_TX && !tx_info->is_cck) {
1428 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_PRD, B_PMAC_CTX_EN, 1, idx);
1429 		rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC CTx Start");
1430 	} else if (mode == PKTS_TX) {
1431 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_PRD, B_PMAC_PTX_EN, 1, idx);
1432 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_PRD,
1433 				      B_PMAC_TX_PRD_MSK, period, idx);
1434 		rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_CNT, B_PMAC_TX_CNT_MSK,
1435 				      pkt_cnt, idx);
1436 		rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC PTx Start");
1437 	}
1438 
1439 	rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_CTRL, B_PMAC_TXEN_DIS, 1, idx);
1440 	rtw89_phy_write32_idx(rtwdev, R_PMAC_TX_CTRL, B_PMAC_TXEN_DIS, 0, idx);
1441 }
1442 
1443 static
1444 void rtw8852bx_bb_set_pmac_tx(struct rtw89_dev *rtwdev,
1445 			      struct rtw8852bx_bb_pmac_info *tx_info,
1446 			      enum rtw89_phy_idx idx, const struct rtw89_chan *chan)
1447 {
1448 	if (!tx_info->en_pmac_tx) {
1449 		rtw8852bx_stop_pmac_tx(rtwdev, tx_info, idx);
1450 		rtw89_phy_write32_idx(rtwdev, R_PD_CTRL, B_PD_HIT_DIS, 0, idx);
1451 		if (chan->band_type == RTW89_BAND_2G)
1452 			rtw89_phy_write32_clr(rtwdev, R_RXCCA, B_RXCCA_DIS);
1453 		return;
1454 	}
1455 
1456 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC Tx Enable");
1457 
1458 	rtw89_phy_write32_idx(rtwdev, R_PMAC_GNT, B_PMAC_GNT_TXEN, 1, idx);
1459 	rtw89_phy_write32_idx(rtwdev, R_PMAC_GNT, B_PMAC_GNT_RXEN, 1, idx);
1460 	rtw89_phy_write32_idx(rtwdev, R_PMAC_RX_CFG1, B_PMAC_OPT1_MSK, 0x3f, idx);
1461 	rtw89_phy_write32_idx(rtwdev, R_RSTB_ASYNC, B_RSTB_ASYNC_ALL, 0, idx);
1462 	rtw89_phy_write32_idx(rtwdev, R_PD_CTRL, B_PD_HIT_DIS, 1, idx);
1463 	rtw89_phy_write32_set(rtwdev, R_RXCCA, B_RXCCA_DIS);
1464 	rtw89_phy_write32_idx(rtwdev, R_RSTB_ASYNC, B_RSTB_ASYNC_ALL, 1, idx);
1465 
1466 	rtw8852bx_start_pmac_tx(rtwdev, tx_info, idx);
1467 }
1468 
1469 static
1470 void __rtw8852bx_bb_set_pmac_pkt_tx(struct rtw89_dev *rtwdev, u8 enable,
1471 				    u16 tx_cnt, u16 period, u16 tx_time,
1472 				    enum rtw89_phy_idx idx, const struct rtw89_chan *chan)
1473 {
1474 	struct rtw8852bx_bb_pmac_info tx_info = {0};
1475 
1476 	tx_info.en_pmac_tx = enable;
1477 	tx_info.is_cck = 0;
1478 	tx_info.mode = PKTS_TX;
1479 	tx_info.tx_cnt = tx_cnt;
1480 	tx_info.period = period;
1481 	tx_info.tx_time = tx_time;
1482 
1483 	rtw8852bx_bb_set_pmac_tx(rtwdev, &tx_info, idx, chan);
1484 }
1485 
1486 static
1487 void __rtw8852bx_bb_set_power(struct rtw89_dev *rtwdev, s16 pwr_dbm,
1488 			      enum rtw89_phy_idx idx)
1489 {
1490 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC CFG Tx PWR = %d", pwr_dbm);
1491 
1492 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_PWR_EN, 1, idx);
1493 	rtw89_phy_write32_idx(rtwdev, R_TXPWR, B_TXPWR_MSK, pwr_dbm, idx);
1494 }
1495 
1496 static
1497 void __rtw8852bx_bb_cfg_tx_path(struct rtw89_dev *rtwdev, u8 tx_path)
1498 {
1499 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_MOD, 7, RTW89_PHY_0);
1500 
1501 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "PMAC CFG Tx Path = %d", tx_path);
1502 
1503 	if (tx_path == RF_PATH_A) {
1504 		rtw89_phy_write32_mask(rtwdev, R_TXPATH_SEL, B_TXPATH_SEL_MSK, 1);
1505 		rtw89_phy_write32_mask(rtwdev, R_TXNSS_MAP, B_TXNSS_MAP_MSK, 0);
1506 	} else if (tx_path == RF_PATH_B) {
1507 		rtw89_phy_write32_mask(rtwdev, R_TXPATH_SEL, B_TXPATH_SEL_MSK, 2);
1508 		rtw89_phy_write32_mask(rtwdev, R_TXNSS_MAP, B_TXNSS_MAP_MSK, 0);
1509 	} else if (tx_path == RF_PATH_AB) {
1510 		rtw89_phy_write32_mask(rtwdev, R_TXPATH_SEL, B_TXPATH_SEL_MSK, 3);
1511 		rtw89_phy_write32_mask(rtwdev, R_TXNSS_MAP, B_TXNSS_MAP_MSK, 4);
1512 	} else {
1513 		rtw89_debug(rtwdev, RTW89_DBG_TSSI, "Error Tx Path");
1514 	}
1515 }
1516 
1517 static
1518 void __rtw8852bx_bb_tx_mode_switch(struct rtw89_dev *rtwdev,
1519 				   enum rtw89_phy_idx idx, u8 mode)
1520 {
1521 	if (mode != 0)
1522 		return;
1523 
1524 	rtw89_debug(rtwdev, RTW89_DBG_TSSI, "Tx mode switch");
1525 
1526 	rtw89_phy_write32_idx(rtwdev, R_PMAC_GNT, B_PMAC_GNT_TXEN, 0, idx);
1527 	rtw89_phy_write32_idx(rtwdev, R_PMAC_GNT, B_PMAC_GNT_RXEN, 0, idx);
1528 	rtw89_phy_write32_idx(rtwdev, R_PMAC_RX_CFG1, B_PMAC_OPT1_MSK, 0, idx);
1529 	rtw89_phy_write32_idx(rtwdev, R_PMAC_RXMOD, B_PMAC_RXMOD_MSK, 0, idx);
1530 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_DPD_EN, 0, idx);
1531 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_MOD, 0, idx);
1532 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_PWR_EN, 0, idx);
1533 }
1534 
1535 static
1536 void __rtw8852bx_bb_backup_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx idx,
1537 				struct rtw8852bx_bb_tssi_bak *bak)
1538 {
1539 	s32 tmp;
1540 
1541 	bak->tx_path = rtw89_phy_read32_idx(rtwdev, R_TXPATH_SEL, B_TXPATH_SEL_MSK, idx);
1542 	bak->rx_path = rtw89_phy_read32_idx(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, idx);
1543 	bak->p0_rfmode = rtw89_phy_read32_idx(rtwdev, R_P0_RFMODE, MASKDWORD, idx);
1544 	bak->p0_rfmode_ftm = rtw89_phy_read32_idx(rtwdev, R_P0_RFMODE_FTM_RX, MASKDWORD, idx);
1545 	bak->p1_rfmode = rtw89_phy_read32_idx(rtwdev, R_P1_RFMODE, MASKDWORD, idx);
1546 	bak->p1_rfmode_ftm = rtw89_phy_read32_idx(rtwdev, R_P1_RFMODE_FTM_RX, MASKDWORD, idx);
1547 	tmp = rtw89_phy_read32_idx(rtwdev, R_TXPWR, B_TXPWR_MSK, idx);
1548 	bak->tx_pwr = sign_extend32(tmp, 8);
1549 }
1550 
1551 static
1552 void __rtw8852bx_bb_restore_tssi(struct rtw89_dev *rtwdev, enum rtw89_phy_idx idx,
1553 				 const struct rtw8852bx_bb_tssi_bak *bak)
1554 {
1555 	rtw89_phy_write32_idx(rtwdev, R_TXPATH_SEL, B_TXPATH_SEL_MSK, bak->tx_path, idx);
1556 	if (bak->tx_path == RF_AB)
1557 		rtw89_phy_write32_mask(rtwdev, R_TXNSS_MAP, B_TXNSS_MAP_MSK, 0x4);
1558 	else
1559 		rtw89_phy_write32_mask(rtwdev, R_TXNSS_MAP, B_TXNSS_MAP_MSK, 0x0);
1560 	rtw89_phy_write32_idx(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, bak->rx_path, idx);
1561 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_PWR_EN, 1, idx);
1562 	rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE, MASKDWORD, bak->p0_rfmode, idx);
1563 	rtw89_phy_write32_idx(rtwdev, R_P0_RFMODE_FTM_RX, MASKDWORD, bak->p0_rfmode_ftm, idx);
1564 	rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE, MASKDWORD, bak->p1_rfmode, idx);
1565 	rtw89_phy_write32_idx(rtwdev, R_P1_RFMODE_FTM_RX, MASKDWORD, bak->p1_rfmode_ftm, idx);
1566 	rtw89_phy_write32_idx(rtwdev, R_TXPWR, B_TXPWR_MSK, bak->tx_pwr, idx);
1567 }
1568 
1569 static void __rtw8852bx_ctrl_nbtg_bt_tx(struct rtw89_dev *rtwdev, bool en,
1570 					enum rtw89_phy_idx phy_idx)
1571 {
1572 	rtw89_phy_write_reg3_tbl(rtwdev, en ? &rtw8852bx_btc_preagc_en_defs_tbl :
1573 						 &rtw8852bx_btc_preagc_dis_defs_tbl);
1574 }
1575 
1576 static void __rtw8852bx_ctrl_btg_bt_rx(struct rtw89_dev *rtwdev, bool en,
1577 				       enum rtw89_phy_idx phy_idx)
1578 {
1579 	if (en) {
1580 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BT_SHARE_V1,
1581 				       B_PATH0_BT_SHARE_V1, 0x1);
1582 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BTG_PATH_V1,
1583 				       B_PATH0_BTG_PATH_V1, 0x0);
1584 		rtw89_phy_write32_mask(rtwdev, R_PATH1_G_LNA6_OP1DB_V1,
1585 				       B_PATH1_G_LNA6_OP1DB_V1, 0x20);
1586 		rtw89_phy_write32_mask(rtwdev, R_PATH1_G_TIA0_LNA6_OP1DB_V1,
1587 				       B_PATH1_G_TIA0_LNA6_OP1DB_V1, 0x30);
1588 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BT_SHARE_V1,
1589 				       B_PATH1_BT_SHARE_V1, 0x1);
1590 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BTG_PATH_V1,
1591 				       B_PATH1_BTG_PATH_V1, 0x1);
1592 		rtw89_phy_write32_mask(rtwdev, R_PMAC_GNT, B_PMAC_GNT_P1, 0x0);
1593 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_BT_SHARE, 0x1);
1594 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_BT_SEG0, 0x2);
1595 		rtw89_phy_write32_mask(rtwdev, R_BT_DYN_DC_EST_EN_V1,
1596 				       B_BT_DYN_DC_EST_EN_MSK, 0x1);
1597 		rtw89_phy_write32_mask(rtwdev, R_GNT_BT_WGT_EN, B_GNT_BT_WGT_EN, 0x1);
1598 	} else {
1599 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BT_SHARE_V1,
1600 				       B_PATH0_BT_SHARE_V1, 0x0);
1601 		rtw89_phy_write32_mask(rtwdev, R_PATH0_BTG_PATH_V1,
1602 				       B_PATH0_BTG_PATH_V1, 0x0);
1603 		rtw89_phy_write32_mask(rtwdev, R_PATH1_G_LNA6_OP1DB_V1,
1604 				       B_PATH1_G_LNA6_OP1DB_V1, 0x1a);
1605 		rtw89_phy_write32_mask(rtwdev, R_PATH1_G_TIA0_LNA6_OP1DB_V1,
1606 				       B_PATH1_G_TIA0_LNA6_OP1DB_V1, 0x2a);
1607 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BT_SHARE_V1,
1608 				       B_PATH1_BT_SHARE_V1, 0x0);
1609 		rtw89_phy_write32_mask(rtwdev, R_PATH1_BTG_PATH_V1,
1610 				       B_PATH1_BTG_PATH_V1, 0x0);
1611 		rtw89_phy_write32_mask(rtwdev, R_PMAC_GNT, B_PMAC_GNT_P1, 0xc);
1612 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_BT_SHARE, 0x0);
1613 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_BT_SEG0, 0x0);
1614 		rtw89_phy_write32_mask(rtwdev, R_BT_DYN_DC_EST_EN_V1,
1615 				       B_BT_DYN_DC_EST_EN_MSK, 0x1);
1616 		rtw89_phy_write32_mask(rtwdev, R_GNT_BT_WGT_EN, B_GNT_BT_WGT_EN, 0x0);
1617 	}
1618 }
1619 
1620 static
1621 void __rtw8852bx_bb_ctrl_rx_path(struct rtw89_dev *rtwdev,
1622 				 enum rtw89_rf_path_bit rx_path,
1623 				 const struct rtw89_chan *chan)
1624 {
1625 	u32 rst_mask0;
1626 	u32 rst_mask1;
1627 
1628 	if (rx_path == RF_A) {
1629 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, 1);
1630 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG0, 1);
1631 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG1, 1);
1632 		rtw89_phy_write32_mask(rtwdev, R_RXHT_MCS_LIMIT, B_RXHT_MCS_LIMIT, 0);
1633 		rtw89_phy_write32_mask(rtwdev, R_RXVHT_MCS_LIMIT, B_RXVHT_MCS_LIMIT, 0);
1634 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_USER_MAX, 4);
1635 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_MAX_NSS, 0);
1636 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHETB_MAX_NSS, 0);
1637 	} else if (rx_path == RF_B) {
1638 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, 2);
1639 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG0, 2);
1640 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG1, 2);
1641 		rtw89_phy_write32_mask(rtwdev, R_RXHT_MCS_LIMIT, B_RXHT_MCS_LIMIT, 0);
1642 		rtw89_phy_write32_mask(rtwdev, R_RXVHT_MCS_LIMIT, B_RXVHT_MCS_LIMIT, 0);
1643 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_USER_MAX, 4);
1644 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_MAX_NSS, 0);
1645 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHETB_MAX_NSS, 0);
1646 	} else if (rx_path == RF_AB) {
1647 		rtw89_phy_write32_mask(rtwdev, R_CHBW_MOD_V1, B_ANT_RX_SEG0, 3);
1648 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG0, 3);
1649 		rtw89_phy_write32_mask(rtwdev, R_FC0_BW_V1, B_ANT_RX_1RCCA_SEG1, 3);
1650 		rtw89_phy_write32_mask(rtwdev, R_RXHT_MCS_LIMIT, B_RXHT_MCS_LIMIT, 1);
1651 		rtw89_phy_write32_mask(rtwdev, R_RXVHT_MCS_LIMIT, B_RXVHT_MCS_LIMIT, 1);
1652 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_USER_MAX, 4);
1653 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_MAX_NSS, 1);
1654 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHETB_MAX_NSS, 1);
1655 	}
1656 
1657 	rtw8852bx_set_gain_offset(rtwdev, chan->subband_type, RTW89_PHY_0);
1658 
1659 	if (chan->band_type == RTW89_BAND_2G &&
1660 	    (rx_path == RF_B || rx_path == RF_AB))
1661 		rtw8852bx_ctrl_btg_bt_rx(rtwdev, true, RTW89_PHY_0);
1662 	else
1663 		rtw8852bx_ctrl_btg_bt_rx(rtwdev, false, RTW89_PHY_0);
1664 
1665 	rst_mask0 = B_P0_TXPW_RSTB_MANON | B_P0_TXPW_RSTB_TSSI;
1666 	rst_mask1 = B_P1_TXPW_RSTB_MANON | B_P1_TXPW_RSTB_TSSI;
1667 	if (rx_path == RF_A) {
1668 		rtw89_phy_write32_mask(rtwdev, R_P0_TXPW_RSTB, rst_mask0, 1);
1669 		rtw89_phy_write32_mask(rtwdev, R_P0_TXPW_RSTB, rst_mask0, 3);
1670 	} else {
1671 		rtw89_phy_write32_mask(rtwdev, R_P1_TXPW_RSTB, rst_mask1, 1);
1672 		rtw89_phy_write32_mask(rtwdev, R_P1_TXPW_RSTB, rst_mask1, 3);
1673 	}
1674 }
1675 
1676 static void rtw8852bx_bb_ctrl_rf_mode_rx_path(struct rtw89_dev *rtwdev,
1677 					      enum rtw89_rf_path_bit rx_path)
1678 {
1679 	if (rx_path == RF_A) {
1680 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE,
1681 				       B_P0_RFMODE_ORI_TXRX_FTM_TX, 0x1233312);
1682 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE_FTM_RX,
1683 				       B_P0_RFMODE_FTM_RX, 0x333);
1684 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE,
1685 				       B_P1_RFMODE_ORI_TXRX_FTM_TX, 0x1111111);
1686 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE_FTM_RX,
1687 				       B_P1_RFMODE_FTM_RX, 0x111);
1688 	} else if (rx_path == RF_B) {
1689 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE,
1690 				       B_P0_RFMODE_ORI_TXRX_FTM_TX, 0x1111111);
1691 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE_FTM_RX,
1692 				       B_P0_RFMODE_FTM_RX, 0x111);
1693 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE,
1694 				       B_P1_RFMODE_ORI_TXRX_FTM_TX, 0x1233312);
1695 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE_FTM_RX,
1696 				       B_P1_RFMODE_FTM_RX, 0x333);
1697 	} else if (rx_path == RF_AB) {
1698 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE,
1699 				       B_P0_RFMODE_ORI_TXRX_FTM_TX, 0x1233312);
1700 		rtw89_phy_write32_mask(rtwdev, R_P0_RFMODE_FTM_RX,
1701 				       B_P0_RFMODE_FTM_RX, 0x333);
1702 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE,
1703 				       B_P1_RFMODE_ORI_TXRX_FTM_TX, 0x1233312);
1704 		rtw89_phy_write32_mask(rtwdev, R_P1_RFMODE_FTM_RX,
1705 				       B_P1_RFMODE_FTM_RX, 0x333);
1706 	}
1707 }
1708 
1709 static void __rtw8852bx_bb_cfg_txrx_path(struct rtw89_dev *rtwdev)
1710 {
1711 	struct rtw89_hal *hal = &rtwdev->hal;
1712 	const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_CHANCTX_0);
1713 	enum rtw89_rf_path_bit rx_path = hal->antenna_rx ? hal->antenna_rx : RF_AB;
1714 
1715 	rtw8852bx_bb_ctrl_rx_path(rtwdev, rx_path, chan);
1716 	rtw8852bx_bb_ctrl_rf_mode_rx_path(rtwdev, rx_path);
1717 
1718 	if (rtwdev->hal.rx_nss == 1) {
1719 		rtw89_phy_write32_mask(rtwdev, R_RXHT_MCS_LIMIT, B_RXHT_MCS_LIMIT, 0);
1720 		rtw89_phy_write32_mask(rtwdev, R_RXVHT_MCS_LIMIT, B_RXVHT_MCS_LIMIT, 0);
1721 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_MAX_NSS, 0);
1722 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHETB_MAX_NSS, 0);
1723 	} else {
1724 		rtw89_phy_write32_mask(rtwdev, R_RXHT_MCS_LIMIT, B_RXHT_MCS_LIMIT, 1);
1725 		rtw89_phy_write32_mask(rtwdev, R_RXVHT_MCS_LIMIT, B_RXVHT_MCS_LIMIT, 1);
1726 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHE_MAX_NSS, 1);
1727 		rtw89_phy_write32_mask(rtwdev, R_RXHE, B_RXHETB_MAX_NSS, 1);
1728 	}
1729 
1730 	rtw89_phy_write32_idx(rtwdev, R_MAC_SEL, B_MAC_SEL_MOD, 0x0, RTW89_PHY_0);
1731 }
1732 
1733 static u8 __rtw8852bx_get_thermal(struct rtw89_dev *rtwdev, enum rtw89_rf_path rf_path)
1734 {
1735 	if (rtwdev->is_tssi_mode[rf_path]) {
1736 		u32 addr = 0x1c10 + (rf_path << 13);
1737 
1738 		return rtw89_phy_read32_mask(rtwdev, addr, 0x3F000000);
1739 	}
1740 
1741 	rtw89_write_rf(rtwdev, rf_path, RR_TM, RR_TM_TRI, 0x1);
1742 	rtw89_write_rf(rtwdev, rf_path, RR_TM, RR_TM_TRI, 0x0);
1743 	rtw89_write_rf(rtwdev, rf_path, RR_TM, RR_TM_TRI, 0x1);
1744 
1745 	fsleep(200);
1746 
1747 	return rtw89_read_rf(rtwdev, rf_path, RR_TM, RR_TM_VAL);
1748 }
1749 
1750 static
1751 void rtw8852bx_set_trx_mask(struct rtw89_dev *rtwdev, u8 path, u8 group, u32 val)
1752 {
1753 	rtw89_write_rf(rtwdev, path, RR_LUTWE, RFREG_MASK, 0x20000);
1754 	rtw89_write_rf(rtwdev, path, RR_LUTWA, RFREG_MASK, group);
1755 	rtw89_write_rf(rtwdev, path, RR_LUTWD0, RFREG_MASK, val);
1756 	rtw89_write_rf(rtwdev, path, RR_LUTWE, RFREG_MASK, 0x0);
1757 }
1758 
1759 static void __rtw8852bx_btc_init_cfg(struct rtw89_dev *rtwdev)
1760 {
1761 	struct rtw89_btc *btc = &rtwdev->btc;
1762 	const struct rtw89_chip_info *chip = rtwdev->chip;
1763 	const struct rtw89_mac_ax_coex coex_params = {
1764 		.pta_mode = RTW89_MAC_AX_COEX_RTK_MODE,
1765 		.direction = RTW89_MAC_AX_COEX_INNER,
1766 	};
1767 
1768 	/* PTA init  */
1769 	rtw89_mac_coex_init(rtwdev, &coex_params);
1770 
1771 	/* set WL Tx response = Hi-Pri */
1772 	chip->ops->btc_set_wl_pri(rtwdev, BTC_PRI_MASK_TX_RESP, true);
1773 	chip->ops->btc_set_wl_pri(rtwdev, BTC_PRI_MASK_BEACON, true);
1774 
1775 	/* set rf gnt debug off */
1776 	rtw89_write_rf(rtwdev, RF_PATH_A, RR_WLSEL, RFREG_MASK, 0x0);
1777 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_WLSEL, RFREG_MASK, 0x0);
1778 
1779 	/* set WL Tx thru in TRX mask table if GNT_WL = 0 && BT_S1 = ss group */
1780 	if (btc->ant_type == BTC_ANT_SHARED) {
1781 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_A, BTC_BT_SS_GROUP, 0x5ff);
1782 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_B, BTC_BT_SS_GROUP, 0x5ff);
1783 		/* set path-A(S0) Tx/Rx no-mask if GNT_WL=0 && BT_S1=tx group */
1784 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_A, BTC_BT_TX_GROUP, 0x5ff);
1785 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_B, BTC_BT_TX_GROUP, 0x55f);
1786 	} else { /* set WL Tx stb if GNT_WL = 0 && BT_S1 = ss group for 3-ant */
1787 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_A, BTC_BT_SS_GROUP, 0x5df);
1788 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_B, BTC_BT_SS_GROUP, 0x5df);
1789 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_A, BTC_BT_TX_GROUP, 0x5ff);
1790 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_B, BTC_BT_TX_GROUP, 0x5ff);
1791 	}
1792 
1793 	if (rtwdev->chip->chip_id == RTL8852BT) {
1794 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_A, BTC_BT_RX_GROUP, 0x5df);
1795 		rtw8852bx_set_trx_mask(rtwdev, RF_PATH_B, BTC_BT_RX_GROUP, 0x5df);
1796 	}
1797 
1798 	/* set PTA break table */
1799 	rtw89_write32(rtwdev, R_BTC_BREAK_TABLE, BTC_BREAK_PARAM);
1800 
1801 	 /* enable BT counter 0xda40[16,2] = 2b'11 */
1802 	rtw89_write32_set(rtwdev, R_AX_CSR_MODE, B_AX_BT_CNT_RST | B_AX_STATIS_BT_EN);
1803 	btc->cx.wl.status.map.init_ok = true;
1804 }
1805 
1806 static
1807 void __rtw8852bx_btc_set_wl_pri(struct rtw89_dev *rtwdev, u8 map, bool state)
1808 {
1809 	u32 bitmap;
1810 	u32 reg;
1811 
1812 	switch (map) {
1813 	case BTC_PRI_MASK_TX_RESP:
1814 		reg = R_BTC_BT_COEX_MSK_TABLE;
1815 		bitmap = B_BTC_PRI_MASK_TX_RESP_V1;
1816 		break;
1817 	case BTC_PRI_MASK_BEACON:
1818 		reg = R_AX_WL_PRI_MSK;
1819 		bitmap = B_AX_PTA_WL_PRI_MASK_BCNQ;
1820 		break;
1821 	case BTC_PRI_MASK_RX_CCK:
1822 		reg = R_BTC_BT_COEX_MSK_TABLE;
1823 		bitmap = B_BTC_PRI_MASK_RXCCK_V1;
1824 		break;
1825 	default:
1826 		return;
1827 	}
1828 
1829 	if (state)
1830 		rtw89_write32_set(rtwdev, reg, bitmap);
1831 	else
1832 		rtw89_write32_clr(rtwdev, reg, bitmap);
1833 }
1834 
1835 static
1836 s8 __rtw8852bx_btc_get_bt_rssi(struct rtw89_dev *rtwdev, s8 val)
1837 {
1838 	/* +6 for compensate offset */
1839 	return clamp_t(s8, val + 6, -100, 0) + 100;
1840 }
1841 
1842 static
1843 void __rtw8852bx_btc_update_bt_cnt(struct rtw89_dev *rtwdev)
1844 {
1845 	/* Feature move to firmware */
1846 }
1847 
1848 static void __rtw8852bx_btc_wl_s1_standby(struct rtw89_dev *rtwdev, bool state)
1849 {
1850 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x80000);
1851 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x1);
1852 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD1, RFREG_MASK, 0x31);
1853 
1854 	/* set WL standby = Rx for GNT_BT_Tx = 1->0 settle issue */
1855 	if (state)
1856 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x179);
1857 	else
1858 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x20);
1859 
1860 	rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x0);
1861 }
1862 
1863 static void rtw8852bx_btc_set_wl_lna2(struct rtw89_dev *rtwdev, u8 level)
1864 {
1865 	switch (level) {
1866 	case 0: /* default */
1867 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x1000);
1868 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x0);
1869 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x15);
1870 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x1);
1871 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x17);
1872 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x2);
1873 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x15);
1874 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x3);
1875 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x17);
1876 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x0);
1877 		break;
1878 	case 1: /* Fix LNA2=5  */
1879 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x1000);
1880 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x0);
1881 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x15);
1882 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x1);
1883 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x5);
1884 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x2);
1885 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x15);
1886 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWA, RFREG_MASK, 0x3);
1887 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWD0, RFREG_MASK, 0x5);
1888 		rtw89_write_rf(rtwdev, RF_PATH_B, RR_LUTWE, RFREG_MASK, 0x0);
1889 		break;
1890 	}
1891 }
1892 
1893 static void __rtw8852bx_btc_set_wl_rx_gain(struct rtw89_dev *rtwdev, u32 level)
1894 {
1895 	struct rtw89_btc *btc = &rtwdev->btc;
1896 
1897 	switch (level) {
1898 	case 0: /* original */
1899 	default:
1900 		rtw8852bx_ctrl_nbtg_bt_tx(rtwdev, false, RTW89_PHY_0);
1901 		btc->dm.wl_lna2 = 0;
1902 		break;
1903 	case 1: /* for FDD free-run */
1904 		rtw8852bx_ctrl_nbtg_bt_tx(rtwdev, true, RTW89_PHY_0);
1905 		btc->dm.wl_lna2 = 0;
1906 		break;
1907 	case 2: /* for BTG Co-Rx*/
1908 		rtw8852bx_ctrl_nbtg_bt_tx(rtwdev, false, RTW89_PHY_0);
1909 		btc->dm.wl_lna2 = 1;
1910 		break;
1911 	}
1912 
1913 	rtw8852bx_btc_set_wl_lna2(rtwdev, btc->dm.wl_lna2);
1914 }
1915 
1916 static void rtw8852bx_fill_freq_with_ppdu(struct rtw89_dev *rtwdev,
1917 					  struct rtw89_rx_phy_ppdu *phy_ppdu,
1918 					  struct ieee80211_rx_status *status)
1919 {
1920 	u16 chan = phy_ppdu->chan_idx;
1921 	enum nl80211_band band;
1922 	u8 ch;
1923 
1924 	if (chan == 0)
1925 		return;
1926 
1927 	rtw89_decode_chan_idx(rtwdev, chan, &ch, &band);
1928 	status->freq = ieee80211_channel_to_frequency(ch, band);
1929 	status->band = band;
1930 }
1931 
1932 static void __rtw8852bx_query_ppdu(struct rtw89_dev *rtwdev,
1933 				   struct rtw89_rx_phy_ppdu *phy_ppdu,
1934 				   struct ieee80211_rx_status *status)
1935 {
1936 	u8 path;
1937 	u8 *rx_power = phy_ppdu->rssi;
1938 
1939 	status->signal = RTW89_RSSI_RAW_TO_DBM(max(rx_power[RF_PATH_A], rx_power[RF_PATH_B]));
1940 	for (path = 0; path < rtwdev->chip->rf_path_num; path++) {
1941 		status->chains |= BIT(path);
1942 		status->chain_signal[path] = RTW89_RSSI_RAW_TO_DBM(rx_power[path]);
1943 	}
1944 	if (phy_ppdu->valid)
1945 		rtw8852bx_fill_freq_with_ppdu(rtwdev, phy_ppdu, status);
1946 }
1947 
1948 static void __rtw8852bx_convert_rpl_to_rssi(struct rtw89_dev *rtwdev,
1949 					    struct rtw89_rx_phy_ppdu *phy_ppdu)
1950 {
1951 	u8 delta = phy_ppdu->rpl_avg - phy_ppdu->rssi_avg;
1952 	u8 *rssi = phy_ppdu->rssi;
1953 	u8 i;
1954 
1955 	for (i = 0; i < RF_PATH_NUM_8852BX; i++)
1956 		rssi[i] += delta;
1957 
1958 	phy_ppdu->rssi_avg = phy_ppdu->rpl_avg;
1959 }
1960 
1961 static int __rtw8852bx_mac_enable_bb_rf(struct rtw89_dev *rtwdev)
1962 {
1963 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1964 	u32 val32;
1965 	int ret;
1966 
1967 	rtw89_write8_set(rtwdev, R_AX_SYS_FUNC_EN,
1968 			 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN);
1969 	rtw89_write32_mask(rtwdev, R_AX_SPS_DIG_ON_CTRL0, B_AX_REG_ZCDC_H_MASK, 0x1);
1970 	rtw89_write32_set(rtwdev, R_AX_WLRF_CTRL, B_AX_AFC_AFEDIG);
1971 	rtw89_write32_clr(rtwdev, R_AX_WLRF_CTRL, B_AX_AFC_AFEDIG);
1972 	rtw89_write32_set(rtwdev, R_AX_WLRF_CTRL, B_AX_AFC_AFEDIG);
1973 
1974 	if (chip_id == RTL8852BT) {
1975 		val32 = rtw89_read32(rtwdev, R_AX_AFE_OFF_CTRL1);
1976 		val32 = u32_replace_bits(val32, 0x1, B_AX_S0_LDO_VSEL_F_MASK);
1977 		val32 = u32_replace_bits(val32, 0x1, B_AX_S1_LDO_VSEL_F_MASK);
1978 		rtw89_write32(rtwdev, R_AX_AFE_OFF_CTRL1, val32);
1979 	}
1980 
1981 	ret = rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_WL_RFC_S0, 0xC7,
1982 				      FULL_BIT_MASK);
1983 	if (ret)
1984 		return ret;
1985 
1986 	ret = rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_WL_RFC_S1, 0xC7,
1987 				      FULL_BIT_MASK);
1988 	if (ret)
1989 		return ret;
1990 
1991 	rtw89_write8(rtwdev, R_AX_PHYREG_SET, PHYREG_SET_XYN_CYCLE);
1992 
1993 	return 0;
1994 }
1995 
1996 static int __rtw8852bx_mac_disable_bb_rf(struct rtw89_dev *rtwdev)
1997 {
1998 	u8 wl_rfc_s0;
1999 	u8 wl_rfc_s1;
2000 	int ret;
2001 
2002 	rtw89_write32_clr(rtwdev, R_AX_WLRF_CTRL, B_AX_AFC_AFEDIG);
2003 	rtw89_write8_clr(rtwdev, R_AX_SYS_FUNC_EN,
2004 			 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN);
2005 
2006 	ret = rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_WL_RFC_S0, &wl_rfc_s0);
2007 	if (ret)
2008 		return ret;
2009 	wl_rfc_s0 &= ~XTAL_SI_RF00S_EN;
2010 	ret = rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_WL_RFC_S0, wl_rfc_s0,
2011 				      FULL_BIT_MASK);
2012 	if (ret)
2013 		return ret;
2014 
2015 	ret = rtw89_mac_read_xtal_si(rtwdev, XTAL_SI_WL_RFC_S1, &wl_rfc_s1);
2016 	if (ret)
2017 		return ret;
2018 	wl_rfc_s1 &= ~XTAL_SI_RF10S_EN;
2019 	ret = rtw89_mac_write_xtal_si(rtwdev, XTAL_SI_WL_RFC_S1, wl_rfc_s1,
2020 				      FULL_BIT_MASK);
2021 	return ret;
2022 }
2023 
2024 const struct rtw8852bx_info rtw8852bx_info = {
2025 	.mac_enable_bb_rf = __rtw8852bx_mac_enable_bb_rf,
2026 	.mac_disable_bb_rf = __rtw8852bx_mac_disable_bb_rf,
2027 	.bb_sethw = __rtw8852bx_bb_sethw,
2028 	.bb_reset_all = __rtw8852bx_bb_reset_all,
2029 	.bb_cfg_txrx_path = __rtw8852bx_bb_cfg_txrx_path,
2030 	.bb_cfg_tx_path = __rtw8852bx_bb_cfg_tx_path,
2031 	.bb_ctrl_rx_path = __rtw8852bx_bb_ctrl_rx_path,
2032 	.bb_set_plcp_tx = __rtw8852bx_bb_set_plcp_tx,
2033 	.bb_set_power = __rtw8852bx_bb_set_power,
2034 	.bb_set_pmac_pkt_tx = __rtw8852bx_bb_set_pmac_pkt_tx,
2035 	.bb_backup_tssi = __rtw8852bx_bb_backup_tssi,
2036 	.bb_restore_tssi = __rtw8852bx_bb_restore_tssi,
2037 	.bb_tx_mode_switch = __rtw8852bx_bb_tx_mode_switch,
2038 	.set_channel_mac = __rtw8852bx_set_channel_mac,
2039 	.set_channel_bb = __rtw8852bx_set_channel_bb,
2040 	.ctrl_nbtg_bt_tx = __rtw8852bx_ctrl_nbtg_bt_tx,
2041 	.ctrl_btg_bt_rx = __rtw8852bx_ctrl_btg_bt_rx,
2042 	.query_ppdu = __rtw8852bx_query_ppdu,
2043 	.convert_rpl_to_rssi = __rtw8852bx_convert_rpl_to_rssi,
2044 	.read_efuse = __rtw8852bx_read_efuse,
2045 	.read_phycap = __rtw8852bx_read_phycap,
2046 	.power_trim = __rtw8852bx_power_trim,
2047 	.set_txpwr = __rtw8852bx_set_txpwr,
2048 	.set_txpwr_ctrl = __rtw8852bx_set_txpwr_ctrl,
2049 	.init_txpwr_unit = __rtw8852bx_init_txpwr_unit,
2050 	.set_txpwr_ul_tb_offset = __rtw8852bx_set_txpwr_ul_tb_offset,
2051 	.get_thermal = __rtw8852bx_get_thermal,
2052 	.adc_cfg = rtw8852bt_adc_cfg,
2053 	.btc_init_cfg = __rtw8852bx_btc_init_cfg,
2054 	.btc_set_wl_pri = __rtw8852bx_btc_set_wl_pri,
2055 	.btc_get_bt_rssi = __rtw8852bx_btc_get_bt_rssi,
2056 	.btc_update_bt_cnt = __rtw8852bx_btc_update_bt_cnt,
2057 	.btc_wl_s1_standby = __rtw8852bx_btc_wl_s1_standby,
2058 	.btc_set_wl_rx_gain = __rtw8852bx_btc_set_wl_rx_gain,
2059 };
2060 EXPORT_SYMBOL(rtw8852bx_info);
2061 
2062 MODULE_AUTHOR("Realtek Corporation");
2063 MODULE_DESCRIPTION("Realtek 802.11ax wireless 8852B common routines");
2064 MODULE_LICENSE("Dual BSD/GPL");
2065