1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
18 union phy_table_tile
{
19 struct rtw_phy_cond cond
;
20 struct phy_cfg_pair cfg
;
23 static const u32 db_invert_table
[12][8] = {
29 1007, 1268, 1596, 2010},
31 794, 1000, 1259, 1585},
32 {1995, 2512, 3162, 3981,
33 5012, 6310, 7943, 10000},
34 {12589, 15849, 19953, 25119,
35 31623, 39811, 50119, 63098},
36 {79433, 100000, 125893, 158489,
37 199526, 251189, 316228, 398107},
38 {501187, 630957, 794328, 1000000,
39 1258925, 1584893, 1995262, 2511886},
40 {3162278, 3981072, 5011872, 6309573,
41 7943282, 1000000, 12589254, 15848932},
42 {19952623, 25118864, 31622777, 39810717,
43 50118723, 63095734, 79432823, 100000000},
44 {125892541, 158489319, 199526232, 251188643,
45 316227766, 398107171, 501187234, 630957345},
46 {794328235, 1000000000, 1258925412, 1584893192,
47 1995262315, 2511886432U, 3162277660U, 3981071706U}
50 u8 rtw_cck_rates
[] = { DESC_RATE1M
, DESC_RATE2M
, DESC_RATE5_5M
, DESC_RATE11M
};
51 u8 rtw_ofdm_rates
[] = {
52 DESC_RATE6M
, DESC_RATE9M
, DESC_RATE12M
,
53 DESC_RATE18M
, DESC_RATE24M
, DESC_RATE36M
,
54 DESC_RATE48M
, DESC_RATE54M
56 u8 rtw_ht_1s_rates
[] = {
57 DESC_RATEMCS0
, DESC_RATEMCS1
, DESC_RATEMCS2
,
58 DESC_RATEMCS3
, DESC_RATEMCS4
, DESC_RATEMCS5
,
59 DESC_RATEMCS6
, DESC_RATEMCS7
61 u8 rtw_ht_2s_rates
[] = {
62 DESC_RATEMCS8
, DESC_RATEMCS9
, DESC_RATEMCS10
,
63 DESC_RATEMCS11
, DESC_RATEMCS12
, DESC_RATEMCS13
,
64 DESC_RATEMCS14
, DESC_RATEMCS15
66 u8 rtw_vht_1s_rates
[] = {
67 DESC_RATEVHT1SS_MCS0
, DESC_RATEVHT1SS_MCS1
,
68 DESC_RATEVHT1SS_MCS2
, DESC_RATEVHT1SS_MCS3
,
69 DESC_RATEVHT1SS_MCS4
, DESC_RATEVHT1SS_MCS5
,
70 DESC_RATEVHT1SS_MCS6
, DESC_RATEVHT1SS_MCS7
,
71 DESC_RATEVHT1SS_MCS8
, DESC_RATEVHT1SS_MCS9
73 u8 rtw_vht_2s_rates
[] = {
74 DESC_RATEVHT2SS_MCS0
, DESC_RATEVHT2SS_MCS1
,
75 DESC_RATEVHT2SS_MCS2
, DESC_RATEVHT2SS_MCS3
,
76 DESC_RATEVHT2SS_MCS4
, DESC_RATEVHT2SS_MCS5
,
77 DESC_RATEVHT2SS_MCS6
, DESC_RATEVHT2SS_MCS7
,
78 DESC_RATEVHT2SS_MCS8
, DESC_RATEVHT2SS_MCS9
80 u8
*rtw_rate_section
[RTW_RATE_SECTION_MAX
] = {
81 rtw_cck_rates
, rtw_ofdm_rates
,
82 rtw_ht_1s_rates
, rtw_ht_2s_rates
,
83 rtw_vht_1s_rates
, rtw_vht_2s_rates
85 u8 rtw_rate_size
[RTW_RATE_SECTION_MAX
] = {
86 ARRAY_SIZE(rtw_cck_rates
),
87 ARRAY_SIZE(rtw_ofdm_rates
),
88 ARRAY_SIZE(rtw_ht_1s_rates
),
89 ARRAY_SIZE(rtw_ht_2s_rates
),
90 ARRAY_SIZE(rtw_vht_1s_rates
),
91 ARRAY_SIZE(rtw_vht_2s_rates
)
93 static const u8 rtw_cck_size
= ARRAY_SIZE(rtw_cck_rates
);
94 static const u8 rtw_ofdm_size
= ARRAY_SIZE(rtw_ofdm_rates
);
95 static const u8 rtw_ht_1s_size
= ARRAY_SIZE(rtw_ht_1s_rates
);
96 static const u8 rtw_ht_2s_size
= ARRAY_SIZE(rtw_ht_2s_rates
);
97 static const u8 rtw_vht_1s_size
= ARRAY_SIZE(rtw_vht_1s_rates
);
98 static const u8 rtw_vht_2s_size
= ARRAY_SIZE(rtw_vht_2s_rates
);
100 enum rtw_phy_band_type
{
105 static void rtw_phy_cck_pd_init(struct rtw_dev
*rtwdev
)
107 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
110 for (i
= 0; i
<= RTW_CHANNEL_WIDTH_40
; i
++) {
111 for (j
= 0; j
< RTW_RF_PATH_MAX
; j
++)
112 dm_info
->cck_pd_lv
[i
][j
] = CCK_PD_LV0
;
115 dm_info
->cck_fa_avg
= CCK_FA_AVG_RESET
;
118 void rtw_phy_init(struct rtw_dev
*rtwdev
)
120 struct rtw_chip_info
*chip
= rtwdev
->chip
;
121 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
124 dm_info
->fa_history
[3] = 0;
125 dm_info
->fa_history
[2] = 0;
126 dm_info
->fa_history
[1] = 0;
127 dm_info
->fa_history
[0] = 0;
128 dm_info
->igi_bitmap
= 0;
129 dm_info
->igi_history
[3] = 0;
130 dm_info
->igi_history
[2] = 0;
131 dm_info
->igi_history
[1] = 0;
133 addr
= chip
->dig
[0].addr
;
134 mask
= chip
->dig
[0].mask
;
135 dm_info
->igi_history
[0] = rtw_read32_mask(rtwdev
, addr
, mask
);
136 rtw_phy_cck_pd_init(rtwdev
);
139 void rtw_phy_dig_write(struct rtw_dev
*rtwdev
, u8 igi
)
141 struct rtw_chip_info
*chip
= rtwdev
->chip
;
142 struct rtw_hal
*hal
= &rtwdev
->hal
;
146 for (path
= 0; path
< hal
->rf_path_num
; path
++) {
147 addr
= chip
->dig
[path
].addr
;
148 mask
= chip
->dig
[path
].mask
;
149 rtw_write32_mask(rtwdev
, addr
, mask
, igi
);
153 static void rtw_phy_stat_false_alarm(struct rtw_dev
*rtwdev
)
155 struct rtw_chip_info
*chip
= rtwdev
->chip
;
157 chip
->ops
->false_alarm_statistics(rtwdev
);
160 #define RA_FLOOR_TABLE_SIZE 7
161 #define RA_FLOOR_UP_GAP 3
163 static u8
rtw_phy_get_rssi_level(u8 old_level
, u8 rssi
)
165 u8 table
[RA_FLOOR_TABLE_SIZE
] = {20, 34, 38, 42, 46, 50, 100};
169 for (i
= 0; i
< RA_FLOOR_TABLE_SIZE
; i
++)
171 table
[i
] += RA_FLOOR_UP_GAP
;
173 for (i
= 0; i
< RA_FLOOR_TABLE_SIZE
; i
++) {
174 if (rssi
< table
[i
]) {
183 struct rtw_phy_stat_iter_data
{
184 struct rtw_dev
*rtwdev
;
188 static void rtw_phy_stat_rssi_iter(void *data
, struct ieee80211_sta
*sta
)
190 struct rtw_phy_stat_iter_data
*iter_data
= data
;
191 struct rtw_dev
*rtwdev
= iter_data
->rtwdev
;
192 struct rtw_sta_info
*si
= (struct rtw_sta_info
*)sta
->drv_priv
;
195 rssi
= ewma_rssi_read(&si
->avg_rssi
);
196 si
->rssi_level
= rtw_phy_get_rssi_level(si
->rssi_level
, rssi
);
198 rtw_fw_send_rssi_info(rtwdev
, si
);
200 iter_data
->min_rssi
= min_t(u8
, rssi
, iter_data
->min_rssi
);
203 static void rtw_phy_stat_rssi(struct rtw_dev
*rtwdev
)
205 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
206 struct rtw_phy_stat_iter_data data
= {};
208 data
.rtwdev
= rtwdev
;
209 data
.min_rssi
= U8_MAX
;
210 rtw_iterate_stas_atomic(rtwdev
, rtw_phy_stat_rssi_iter
, &data
);
212 dm_info
->pre_min_rssi
= dm_info
->min_rssi
;
213 dm_info
->min_rssi
= data
.min_rssi
;
216 static void rtw_phy_stat_rate_cnt(struct rtw_dev
*rtwdev
)
218 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
220 dm_info
->last_pkt_count
= dm_info
->cur_pkt_count
;
221 memset(&dm_info
->cur_pkt_count
, 0, sizeof(dm_info
->cur_pkt_count
));
224 static void rtw_phy_statistics(struct rtw_dev
*rtwdev
)
226 rtw_phy_stat_rssi(rtwdev
);
227 rtw_phy_stat_false_alarm(rtwdev
);
228 rtw_phy_stat_rate_cnt(rtwdev
);
231 #define DIG_PERF_FA_TH_LOW 250
232 #define DIG_PERF_FA_TH_HIGH 500
233 #define DIG_PERF_FA_TH_EXTRA_HIGH 750
234 #define DIG_PERF_MAX 0x5a
235 #define DIG_PERF_MID 0x40
236 #define DIG_CVRG_FA_TH_LOW 2000
237 #define DIG_CVRG_FA_TH_HIGH 4000
238 #define DIG_CVRG_FA_TH_EXTRA_HIGH 5000
239 #define DIG_CVRG_MAX 0x2a
240 #define DIG_CVRG_MID 0x26
241 #define DIG_CVRG_MIN 0x1c
242 #define DIG_RSSI_GAIN_OFFSET 15
245 rtw_phy_dig_check_damping(struct rtw_dm_info
*dm_info
)
247 u16 fa_lo
= DIG_PERF_FA_TH_LOW
;
248 u16 fa_hi
= DIG_PERF_FA_TH_HIGH
;
255 bool damping
= false;
257 min_rssi
= dm_info
->min_rssi
;
258 if (dm_info
->damping
) {
259 damping_rssi
= dm_info
->damping_rssi
;
260 diff
= min_rssi
> damping_rssi
? min_rssi
- damping_rssi
:
261 damping_rssi
- min_rssi
;
262 if (diff
> 3 || dm_info
->damping_cnt
++ > 20) {
263 dm_info
->damping
= false;
270 igi_history
= dm_info
->igi_history
;
271 fa_history
= dm_info
->fa_history
;
272 igi_bitmap
= dm_info
->igi_bitmap
& 0xf;
273 switch (igi_bitmap
) {
275 /* down -> up -> down -> up */
276 if (igi_history
[0] > igi_history
[1] &&
277 igi_history
[2] > igi_history
[3] &&
278 igi_history
[0] - igi_history
[1] >= 2 &&
279 igi_history
[2] - igi_history
[3] >= 2 &&
280 fa_history
[0] > fa_hi
&& fa_history
[1] < fa_lo
&&
281 fa_history
[2] > fa_hi
&& fa_history
[3] < fa_lo
)
285 /* up -> down -> down -> up */
286 if (igi_history
[0] > igi_history
[1] &&
287 igi_history
[3] > igi_history
[2] &&
288 igi_history
[0] - igi_history
[1] >= 4 &&
289 igi_history
[3] - igi_history
[2] >= 2 &&
290 fa_history
[0] > fa_hi
&& fa_history
[1] < fa_lo
&&
291 fa_history
[2] < fa_lo
&& fa_history
[3] > fa_hi
)
299 dm_info
->damping
= true;
300 dm_info
->damping_cnt
= 0;
301 dm_info
->damping_rssi
= min_rssi
;
307 static void rtw_phy_dig_get_boundary(struct rtw_dm_info
*dm_info
,
308 u8
*upper
, u8
*lower
, bool linked
)
310 u8 dig_max
, dig_min
, dig_mid
;
314 dig_max
= DIG_PERF_MAX
;
315 dig_mid
= DIG_PERF_MID
;
316 /* 22B=0x1c, 22C=0x20 */
318 min_rssi
= max_t(u8
, dm_info
->min_rssi
, dig_min
);
320 dig_max
= DIG_CVRG_MAX
;
321 dig_mid
= DIG_CVRG_MID
;
322 dig_min
= DIG_CVRG_MIN
;
326 /* DIG MAX should be bounded by minimum RSSI with offset +15 */
327 dig_max
= min_t(u8
, dig_max
, min_rssi
+ DIG_RSSI_GAIN_OFFSET
);
329 *lower
= clamp_t(u8
, min_rssi
, dig_min
, dig_mid
);
330 *upper
= clamp_t(u8
, *lower
+ DIG_RSSI_GAIN_OFFSET
, dig_min
, dig_max
);
333 static void rtw_phy_dig_get_threshold(struct rtw_dm_info
*dm_info
,
334 u16
*fa_th
, u8
*step
, bool linked
)
336 u8 min_rssi
, pre_min_rssi
;
338 min_rssi
= dm_info
->min_rssi
;
339 pre_min_rssi
= dm_info
->pre_min_rssi
;
345 fa_th
[0] = DIG_PERF_FA_TH_EXTRA_HIGH
;
346 fa_th
[1] = DIG_PERF_FA_TH_HIGH
;
347 fa_th
[2] = DIG_PERF_FA_TH_LOW
;
348 if (pre_min_rssi
> min_rssi
) {
354 fa_th
[0] = DIG_CVRG_FA_TH_EXTRA_HIGH
;
355 fa_th
[1] = DIG_CVRG_FA_TH_HIGH
;
356 fa_th
[2] = DIG_CVRG_FA_TH_LOW
;
360 static void rtw_phy_dig_recorder(struct rtw_dm_info
*dm_info
, u8 igi
, u16 fa
)
367 igi_bitmap
= dm_info
->igi_bitmap
<< 1 & 0xfe;
368 igi_history
= dm_info
->igi_history
;
369 fa_history
= dm_info
->fa_history
;
371 up
= igi
> igi_history
[0];
374 igi_history
[3] = igi_history
[2];
375 igi_history
[2] = igi_history
[1];
376 igi_history
[1] = igi_history
[0];
377 igi_history
[0] = igi
;
379 fa_history
[3] = fa_history
[2];
380 fa_history
[2] = fa_history
[1];
381 fa_history
[1] = fa_history
[0];
384 dm_info
->igi_bitmap
= igi_bitmap
;
387 static void rtw_phy_dig(struct rtw_dev
*rtwdev
)
389 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
390 u8 upper_bound
, lower_bound
;
392 u16 fa_th
[3], fa_cnt
;
397 if (test_bit(RTW_FLAG_DIG_DISABLE
, rtwdev
->flags
))
400 if (rtw_phy_dig_check_damping(dm_info
))
403 linked
= !!rtwdev
->sta_cnt
;
405 fa_cnt
= dm_info
->total_fa_cnt
;
406 pre_igi
= dm_info
->igi_history
[0];
408 rtw_phy_dig_get_threshold(dm_info
, fa_th
, step
, linked
);
410 /* test the false alarm count from the highest threshold level first,
411 * and increase it by corresponding step size
413 * note that the step size is offset by -2, compensate it afterall
416 for (level
= 0; level
< 3; level
++) {
417 if (fa_cnt
> fa_th
[level
]) {
418 cur_igi
+= step
[level
];
424 /* calculate the upper/lower bound by the minimum rssi we have among
425 * the peers connected with us, meanwhile make sure the igi value does
426 * not beyond the hardware limitation
428 rtw_phy_dig_get_boundary(dm_info
, &upper_bound
, &lower_bound
, linked
);
429 cur_igi
= clamp_t(u8
, cur_igi
, lower_bound
, upper_bound
);
431 /* record current igi value and false alarm statistics for further
432 * damping checks, and record the trend of igi values
434 rtw_phy_dig_recorder(dm_info
, cur_igi
, fa_cnt
);
436 if (cur_igi
!= pre_igi
)
437 rtw_phy_dig_write(rtwdev
, cur_igi
);
440 static void rtw_phy_ra_info_update_iter(void *data
, struct ieee80211_sta
*sta
)
442 struct rtw_dev
*rtwdev
= data
;
443 struct rtw_sta_info
*si
= (struct rtw_sta_info
*)sta
->drv_priv
;
445 rtw_update_sta_info(rtwdev
, si
);
448 static void rtw_phy_ra_info_update(struct rtw_dev
*rtwdev
)
450 if (rtwdev
->watch_dog_cnt
& 0x3)
453 rtw_iterate_stas_atomic(rtwdev
, rtw_phy_ra_info_update_iter
, rtwdev
);
456 static void rtw_phy_dpk_track(struct rtw_dev
*rtwdev
)
458 struct rtw_chip_info
*chip
= rtwdev
->chip
;
460 if (chip
->ops
->dpk_track
)
461 chip
->ops
->dpk_track(rtwdev
);
464 #define CCK_PD_FA_LV1_MIN 1000
465 #define CCK_PD_FA_LV0_MAX 500
467 static u8
rtw_phy_cck_pd_lv_unlink(struct rtw_dev
*rtwdev
)
469 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
470 u32 cck_fa_avg
= dm_info
->cck_fa_avg
;
472 if (cck_fa_avg
> CCK_PD_FA_LV1_MIN
)
475 if (cck_fa_avg
< CCK_PD_FA_LV0_MAX
)
478 return CCK_PD_LV_MAX
;
481 #define CCK_PD_IGI_LV4_VAL 0x38
482 #define CCK_PD_IGI_LV3_VAL 0x2a
483 #define CCK_PD_IGI_LV2_VAL 0x24
484 #define CCK_PD_RSSI_LV4_VAL 32
485 #define CCK_PD_RSSI_LV3_VAL 32
486 #define CCK_PD_RSSI_LV2_VAL 24
488 static u8
rtw_phy_cck_pd_lv_link(struct rtw_dev
*rtwdev
)
490 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
491 u8 igi
= dm_info
->igi_history
[0];
492 u8 rssi
= dm_info
->min_rssi
;
493 u32 cck_fa_avg
= dm_info
->cck_fa_avg
;
495 if (igi
> CCK_PD_IGI_LV4_VAL
&& rssi
> CCK_PD_RSSI_LV4_VAL
)
497 if (igi
> CCK_PD_IGI_LV3_VAL
&& rssi
> CCK_PD_RSSI_LV3_VAL
)
499 if (igi
> CCK_PD_IGI_LV2_VAL
|| rssi
> CCK_PD_RSSI_LV2_VAL
)
501 if (cck_fa_avg
> CCK_PD_FA_LV1_MIN
)
503 if (cck_fa_avg
< CCK_PD_FA_LV0_MAX
)
506 return CCK_PD_LV_MAX
;
509 static u8
rtw_phy_cck_pd_lv(struct rtw_dev
*rtwdev
)
511 if (!rtw_is_assoc(rtwdev
))
512 return rtw_phy_cck_pd_lv_unlink(rtwdev
);
514 return rtw_phy_cck_pd_lv_link(rtwdev
);
517 static void rtw_phy_cck_pd(struct rtw_dev
*rtwdev
)
519 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
520 struct rtw_chip_info
*chip
= rtwdev
->chip
;
521 u32 cck_fa
= dm_info
->cck_fa_cnt
;
524 if (rtwdev
->hal
.current_band_type
!= RTW_BAND_2G
)
527 if (dm_info
->cck_fa_avg
== CCK_FA_AVG_RESET
)
528 dm_info
->cck_fa_avg
= cck_fa
;
530 dm_info
->cck_fa_avg
= (dm_info
->cck_fa_avg
* 3 + cck_fa
) >> 2;
532 level
= rtw_phy_cck_pd_lv(rtwdev
);
534 if (level
>= CCK_PD_LV_MAX
)
537 if (chip
->ops
->cck_pd_set
)
538 chip
->ops
->cck_pd_set(rtwdev
, level
);
541 static void rtw_phy_pwr_track(struct rtw_dev
*rtwdev
)
543 rtwdev
->chip
->ops
->pwr_track(rtwdev
);
546 void rtw_phy_dynamic_mechanism(struct rtw_dev
*rtwdev
)
548 /* for further calculation */
549 rtw_phy_statistics(rtwdev
);
551 rtw_phy_cck_pd(rtwdev
);
552 rtw_phy_ra_info_update(rtwdev
);
553 rtw_phy_dpk_track(rtwdev
);
554 rtw_phy_pwr_track(rtwdev
);
559 static u8
rtw_phy_power_2_db(s8 power
)
561 if (power
<= -100 || power
>= 20)
569 static u64
rtw_phy_db_2_linear(u8 power_db
)
576 else if (power_db
< 1)
580 i
= (power_db
- 1) >> 3;
581 j
= (power_db
- 1) - (i
<< 3);
583 linear
= db_invert_table
[i
][j
];
584 linear
= i
> 2 ? linear
<< FRAC_BITS
: linear
;
589 static u8
rtw_phy_linear_2_db(u64 linear
)
595 if (linear
>= db_invert_table
[11][7])
596 return 96; /* maximum 96 dB */
598 for (i
= 0; i
< 12; i
++) {
599 if (i
<= 2 && (linear
<< FRAC_BITS
) <= db_invert_table
[i
][7])
601 else if (i
> 2 && linear
<= db_invert_table
[i
][7])
605 for (j
= 0; j
< 8; j
++) {
606 if (i
<= 2 && (linear
<< FRAC_BITS
) <= db_invert_table
[i
][j
])
608 else if (i
> 2 && linear
<= db_invert_table
[i
][j
])
612 if (j
== 0 && i
== 0)
617 if (db_invert_table
[i
][0] - linear
>
618 linear
- db_invert_table
[i
- 1][7]) {
623 if (db_invert_table
[3][0] - linear
>
624 linear
- db_invert_table
[2][7]) {
630 if (db_invert_table
[i
][j
] - linear
>
631 linear
- db_invert_table
[i
][j
- 1]) {
636 dB
= (i
<< 3) + j
+ 1;
641 u8
rtw_phy_rf_power_2_rssi(s8
*rf_power
, u8 path_num
)
649 for (path
= 0; path
< path_num
; path
++) {
650 power
= rf_power
[path
];
651 power_db
= rtw_phy_power_2_db(power
);
652 linear
= rtw_phy_db_2_linear(power_db
);
656 sum
= (sum
+ (1 << (FRAC_BITS
- 1))) >> FRAC_BITS
;
662 sum
= ((sum
) + ((sum
) << 1) + ((sum
) << 3)) >> 5;
671 return rtw_phy_linear_2_db(sum
);
674 u32
rtw_phy_read_rf(struct rtw_dev
*rtwdev
, enum rtw_rf_path rf_path
,
677 struct rtw_hal
*hal
= &rtwdev
->hal
;
678 struct rtw_chip_info
*chip
= rtwdev
->chip
;
679 const u32
*base_addr
= chip
->rf_base_addr
;
680 u32 val
, direct_addr
;
682 if (rf_path
>= hal
->rf_path_num
) {
683 rtw_err(rtwdev
, "unsupported rf path (%d)\n", rf_path
);
688 direct_addr
= base_addr
[rf_path
] + (addr
<< 2);
691 val
= rtw_read32_mask(rtwdev
, direct_addr
, mask
);
696 bool rtw_phy_write_rf_reg_sipi(struct rtw_dev
*rtwdev
, enum rtw_rf_path rf_path
,
697 u32 addr
, u32 mask
, u32 data
)
699 struct rtw_hal
*hal
= &rtwdev
->hal
;
700 struct rtw_chip_info
*chip
= rtwdev
->chip
;
701 u32
*sipi_addr
= chip
->rf_sipi_addr
;
706 if (rf_path
>= hal
->rf_path_num
) {
707 rtw_err(rtwdev
, "unsupported rf path (%d)\n", rf_path
);
714 if (mask
!= RFREG_MASK
) {
715 old_data
= rtw_phy_read_rf(rtwdev
, rf_path
, addr
, RFREG_MASK
);
717 if (old_data
== INV_RF_DATA
) {
718 rtw_err(rtwdev
, "Write fail, rf is disabled\n");
723 data
= ((old_data
) & (~mask
)) | (data
<< shift
);
726 data_and_addr
= ((addr
<< 20) | (data
& 0x000fffff)) & 0x0fffffff;
728 rtw_write32(rtwdev
, sipi_addr
[rf_path
], data_and_addr
);
735 bool rtw_phy_write_rf_reg(struct rtw_dev
*rtwdev
, enum rtw_rf_path rf_path
,
736 u32 addr
, u32 mask
, u32 data
)
738 struct rtw_hal
*hal
= &rtwdev
->hal
;
739 struct rtw_chip_info
*chip
= rtwdev
->chip
;
740 const u32
*base_addr
= chip
->rf_base_addr
;
743 if (rf_path
>= hal
->rf_path_num
) {
744 rtw_err(rtwdev
, "unsupported rf path (%d)\n", rf_path
);
749 direct_addr
= base_addr
[rf_path
] + (addr
<< 2);
752 rtw_write32_mask(rtwdev
, direct_addr
, mask
, data
);
759 bool rtw_phy_write_rf_reg_mix(struct rtw_dev
*rtwdev
, enum rtw_rf_path rf_path
,
760 u32 addr
, u32 mask
, u32 data
)
763 return rtw_phy_write_rf_reg(rtwdev
, rf_path
, addr
, mask
, data
);
765 return rtw_phy_write_rf_reg_sipi(rtwdev
, rf_path
, addr
, mask
, data
);
768 void rtw_phy_setup_phy_cond(struct rtw_dev
*rtwdev
, u32 pkg
)
770 struct rtw_hal
*hal
= &rtwdev
->hal
;
771 struct rtw_efuse
*efuse
= &rtwdev
->efuse
;
772 struct rtw_phy_cond cond
= {0};
774 cond
.cut
= hal
->cut_version
? hal
->cut_version
: 15;
775 cond
.pkg
= pkg
? pkg
: 15;
777 cond
.rfe
= efuse
->rfe_option
;
779 switch (rtw_hci_type(rtwdev
)) {
780 case RTW_HCI_TYPE_USB
:
781 cond
.intf
= INTF_USB
;
783 case RTW_HCI_TYPE_SDIO
:
784 cond
.intf
= INTF_SDIO
;
786 case RTW_HCI_TYPE_PCIE
:
788 cond
.intf
= INTF_PCIE
;
792 hal
->phy_cond
= cond
;
794 rtw_dbg(rtwdev
, RTW_DBG_PHY
, "phy cond=0x%08x\n", *((u32
*)&hal
->phy_cond
));
797 static bool check_positive(struct rtw_dev
*rtwdev
, struct rtw_phy_cond cond
)
799 struct rtw_hal
*hal
= &rtwdev
->hal
;
800 struct rtw_phy_cond drv_cond
= hal
->phy_cond
;
802 if (cond
.cut
&& cond
.cut
!= drv_cond
.cut
)
805 if (cond
.pkg
&& cond
.pkg
!= drv_cond
.pkg
)
808 if (cond
.intf
&& cond
.intf
!= drv_cond
.intf
)
811 if (cond
.rfe
!= drv_cond
.rfe
)
817 void rtw_parse_tbl_phy_cond(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
)
819 const union phy_table_tile
*p
= tbl
->data
;
820 const union phy_table_tile
*end
= p
+ tbl
->size
/ 2;
821 struct rtw_phy_cond pos_cond
= {0};
822 bool is_matched
= true, is_skipped
= false;
824 BUILD_BUG_ON(sizeof(union phy_table_tile
) != sizeof(struct phy_cfg_pair
));
826 for (; p
< end
; p
++) {
828 switch (p
->cond
.branch
) {
834 is_matched
= is_skipped
? false : true;
842 } else if (p
->cond
.neg
) {
844 if (check_positive(rtwdev
, pos_cond
)) {
854 } else if (is_matched
) {
855 (*tbl
->do_cfg
)(rtwdev
, tbl
, p
->cfg
.addr
, p
->cfg
.data
);
860 #define bcd_to_dec_pwr_by_rate(val, i) bcd2bin(val >> (i * 8))
862 static u8
tbl_to_dec_pwr_by_rate(struct rtw_dev
*rtwdev
, u32 hex
, u8 i
)
864 if (rtwdev
->chip
->is_pwr_by_rate_dec
)
865 return bcd_to_dec_pwr_by_rate(hex
, i
);
867 return (hex
>> (i
* 8)) & 0xFF;
871 rtw_phy_get_rate_values_of_txpwr_by_rate(struct rtw_dev
*rtwdev
,
872 u32 addr
, u32 mask
, u32 val
, u8
*rate
,
873 u8
*pwr_by_rate
, u8
*rate_num
)
880 rate
[0] = DESC_RATE6M
;
881 rate
[1] = DESC_RATE9M
;
882 rate
[2] = DESC_RATE12M
;
883 rate
[3] = DESC_RATE18M
;
884 for (i
= 0; i
< 4; ++i
)
885 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
890 rate
[0] = DESC_RATE24M
;
891 rate
[1] = DESC_RATE36M
;
892 rate
[2] = DESC_RATE48M
;
893 rate
[3] = DESC_RATE54M
;
894 for (i
= 0; i
< 4; ++i
)
895 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
899 rate
[0] = DESC_RATE1M
;
900 pwr_by_rate
[0] = bcd_to_dec_pwr_by_rate(val
, 1);
904 if (mask
== 0xffffff00) {
905 rate
[0] = DESC_RATE2M
;
906 rate
[1] = DESC_RATE5_5M
;
907 rate
[2] = DESC_RATE11M
;
908 for (i
= 1; i
< 4; ++i
)
910 tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
912 } else if (mask
== 0x000000ff) {
913 rate
[0] = DESC_RATE11M
;
914 pwr_by_rate
[0] = bcd_to_dec_pwr_by_rate(val
, 0);
920 rate
[0] = DESC_RATEMCS0
;
921 rate
[1] = DESC_RATEMCS1
;
922 rate
[2] = DESC_RATEMCS2
;
923 rate
[3] = DESC_RATEMCS3
;
924 for (i
= 0; i
< 4; ++i
)
925 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
930 rate
[0] = DESC_RATEMCS4
;
931 rate
[1] = DESC_RATEMCS5
;
932 rate
[2] = DESC_RATEMCS6
;
933 rate
[3] = DESC_RATEMCS7
;
934 for (i
= 0; i
< 4; ++i
)
935 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
940 rate
[0] = DESC_RATEMCS8
;
941 rate
[1] = DESC_RATEMCS9
;
942 rate
[2] = DESC_RATEMCS10
;
943 rate
[3] = DESC_RATEMCS11
;
944 for (i
= 0; i
< 4; ++i
)
945 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
950 rate
[0] = DESC_RATEMCS12
;
951 rate
[1] = DESC_RATEMCS13
;
952 rate
[2] = DESC_RATEMCS14
;
953 rate
[3] = DESC_RATEMCS15
;
954 for (i
= 0; i
< 4; ++i
)
955 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
959 rate
[0] = DESC_RATE1M
;
960 rate
[1] = DESC_RATE2M
;
961 rate
[2] = DESC_RATE5_5M
;
962 for (i
= 1; i
< 4; ++i
)
963 pwr_by_rate
[i
- 1] = tbl_to_dec_pwr_by_rate(rtwdev
,
971 rate
[0] = DESC_RATE1M
;
972 rate
[1] = DESC_RATE2M
;
973 rate
[2] = DESC_RATE5_5M
;
974 rate
[3] = DESC_RATE11M
;
975 for (i
= 0; i
< 4; ++i
)
976 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
983 rate
[0] = DESC_RATE6M
;
984 rate
[1] = DESC_RATE9M
;
985 rate
[2] = DESC_RATE12M
;
986 rate
[3] = DESC_RATE18M
;
987 for (i
= 0; i
< 4; ++i
)
988 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
995 rate
[0] = DESC_RATE24M
;
996 rate
[1] = DESC_RATE36M
;
997 rate
[2] = DESC_RATE48M
;
998 rate
[3] = DESC_RATE54M
;
999 for (i
= 0; i
< 4; ++i
)
1000 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1007 rate
[0] = DESC_RATEMCS0
;
1008 rate
[1] = DESC_RATEMCS1
;
1009 rate
[2] = DESC_RATEMCS2
;
1010 rate
[3] = DESC_RATEMCS3
;
1011 for (i
= 0; i
< 4; ++i
)
1012 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1019 rate
[0] = DESC_RATEMCS4
;
1020 rate
[1] = DESC_RATEMCS5
;
1021 rate
[2] = DESC_RATEMCS6
;
1022 rate
[3] = DESC_RATEMCS7
;
1023 for (i
= 0; i
< 4; ++i
)
1024 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1031 rate
[0] = DESC_RATEMCS8
;
1032 rate
[1] = DESC_RATEMCS9
;
1033 rate
[2] = DESC_RATEMCS10
;
1034 rate
[3] = DESC_RATEMCS11
;
1035 for (i
= 0; i
< 4; ++i
)
1036 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1043 rate
[0] = DESC_RATEMCS12
;
1044 rate
[1] = DESC_RATEMCS13
;
1045 rate
[2] = DESC_RATEMCS14
;
1046 rate
[3] = DESC_RATEMCS15
;
1047 for (i
= 0; i
< 4; ++i
)
1048 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1055 rate
[0] = DESC_RATEVHT1SS_MCS0
;
1056 rate
[1] = DESC_RATEVHT1SS_MCS1
;
1057 rate
[2] = DESC_RATEVHT1SS_MCS2
;
1058 rate
[3] = DESC_RATEVHT1SS_MCS3
;
1059 for (i
= 0; i
< 4; ++i
)
1060 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1067 rate
[0] = DESC_RATEVHT1SS_MCS4
;
1068 rate
[1] = DESC_RATEVHT1SS_MCS5
;
1069 rate
[2] = DESC_RATEVHT1SS_MCS6
;
1070 rate
[3] = DESC_RATEVHT1SS_MCS7
;
1071 for (i
= 0; i
< 4; ++i
)
1072 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1079 rate
[0] = DESC_RATEVHT1SS_MCS8
;
1080 rate
[1] = DESC_RATEVHT1SS_MCS9
;
1081 rate
[2] = DESC_RATEVHT2SS_MCS0
;
1082 rate
[3] = DESC_RATEVHT2SS_MCS1
;
1083 for (i
= 0; i
< 4; ++i
)
1084 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1091 rate
[0] = DESC_RATEVHT2SS_MCS2
;
1092 rate
[1] = DESC_RATEVHT2SS_MCS3
;
1093 rate
[2] = DESC_RATEVHT2SS_MCS4
;
1094 rate
[3] = DESC_RATEVHT2SS_MCS5
;
1095 for (i
= 0; i
< 4; ++i
)
1096 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1103 rate
[0] = DESC_RATEVHT2SS_MCS6
;
1104 rate
[1] = DESC_RATEVHT2SS_MCS7
;
1105 rate
[2] = DESC_RATEVHT2SS_MCS8
;
1106 rate
[3] = DESC_RATEVHT2SS_MCS9
;
1107 for (i
= 0; i
< 4; ++i
)
1108 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1115 rate
[0] = DESC_RATEMCS16
;
1116 rate
[1] = DESC_RATEMCS17
;
1117 rate
[2] = DESC_RATEMCS18
;
1118 rate
[3] = DESC_RATEMCS19
;
1119 for (i
= 0; i
< 4; ++i
)
1120 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1127 rate
[0] = DESC_RATEMCS20
;
1128 rate
[1] = DESC_RATEMCS21
;
1129 rate
[2] = DESC_RATEMCS22
;
1130 rate
[3] = DESC_RATEMCS23
;
1131 for (i
= 0; i
< 4; ++i
)
1132 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1139 rate
[0] = DESC_RATEVHT3SS_MCS0
;
1140 rate
[1] = DESC_RATEVHT3SS_MCS1
;
1141 rate
[2] = DESC_RATEVHT3SS_MCS2
;
1142 rate
[3] = DESC_RATEVHT3SS_MCS3
;
1143 for (i
= 0; i
< 4; ++i
)
1144 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1151 rate
[0] = DESC_RATEVHT3SS_MCS4
;
1152 rate
[1] = DESC_RATEVHT3SS_MCS5
;
1153 rate
[2] = DESC_RATEVHT3SS_MCS6
;
1154 rate
[3] = DESC_RATEVHT3SS_MCS7
;
1155 for (i
= 0; i
< 4; ++i
)
1156 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1163 rate
[0] = DESC_RATEVHT3SS_MCS8
;
1164 rate
[1] = DESC_RATEVHT3SS_MCS9
;
1165 for (i
= 0; i
< 2; ++i
)
1166 pwr_by_rate
[i
] = tbl_to_dec_pwr_by_rate(rtwdev
, val
, i
);
1170 rtw_warn(rtwdev
, "invalid tx power index addr 0x%08x\n", addr
);
1175 static void rtw_phy_store_tx_power_by_rate(struct rtw_dev
*rtwdev
,
1176 u32 band
, u32 rfpath
, u32 txnum
,
1177 u32 regaddr
, u32 bitmask
, u32 data
)
1179 struct rtw_hal
*hal
= &rtwdev
->hal
;
1182 u8 rates
[RTW_RF_PATH_MAX
] = {0};
1184 s8 pwr_by_rate
[RTW_RF_PATH_MAX
] = {0};
1187 rtw_phy_get_rate_values_of_txpwr_by_rate(rtwdev
, regaddr
, bitmask
, data
,
1188 rates
, pwr_by_rate
, &rate_num
);
1190 if (WARN_ON(rfpath
>= RTW_RF_PATH_MAX
||
1191 (band
!= PHY_BAND_2G
&& band
!= PHY_BAND_5G
) ||
1192 rate_num
> RTW_RF_PATH_MAX
))
1195 for (i
= 0; i
< rate_num
; i
++) {
1196 offset
= pwr_by_rate
[i
];
1198 if (band
== PHY_BAND_2G
)
1199 hal
->tx_pwr_by_rate_offset_2g
[rfpath
][rate
] = offset
;
1200 else if (band
== PHY_BAND_5G
)
1201 hal
->tx_pwr_by_rate_offset_5g
[rfpath
][rate
] = offset
;
1207 void rtw_parse_tbl_bb_pg(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
)
1209 const struct rtw_phy_pg_cfg_pair
*p
= tbl
->data
;
1210 const struct rtw_phy_pg_cfg_pair
*end
= p
+ tbl
->size
;
1212 for (; p
< end
; p
++) {
1213 if (p
->addr
== 0xfe || p
->addr
== 0xffe) {
1217 rtw_phy_store_tx_power_by_rate(rtwdev
, p
->band
, p
->rf_path
,
1218 p
->tx_num
, p
->addr
, p
->bitmask
,
1223 static const u8 rtw_channel_idx_5g
[RTW_MAX_CHANNEL_NUM_5G
] = {
1224 36, 38, 40, 42, 44, 46, 48, /* Band 1 */
1225 52, 54, 56, 58, 60, 62, 64, /* Band 2 */
1226 100, 102, 104, 106, 108, 110, 112, /* Band 3 */
1227 116, 118, 120, 122, 124, 126, 128, /* Band 3 */
1228 132, 134, 136, 138, 140, 142, 144, /* Band 3 */
1229 149, 151, 153, 155, 157, 159, 161, /* Band 4 */
1230 165, 167, 169, 171, 173, 175, 177}; /* Band 4 */
1232 static int rtw_channel_to_idx(u8 band
, u8 channel
)
1237 if (band
== PHY_BAND_2G
) {
1238 ch_idx
= channel
- 1;
1239 n_channel
= RTW_MAX_CHANNEL_NUM_2G
;
1240 } else if (band
== PHY_BAND_5G
) {
1241 n_channel
= RTW_MAX_CHANNEL_NUM_5G
;
1242 for (ch_idx
= 0; ch_idx
< n_channel
; ch_idx
++)
1243 if (rtw_channel_idx_5g
[ch_idx
] == channel
)
1249 if (ch_idx
>= n_channel
)
1255 static void rtw_phy_set_tx_power_limit(struct rtw_dev
*rtwdev
, u8 regd
, u8 band
,
1256 u8 bw
, u8 rs
, u8 ch
, s8 pwr_limit
)
1258 struct rtw_hal
*hal
= &rtwdev
->hal
;
1259 u8 max_power_index
= rtwdev
->chip
->max_power_index
;
1263 pwr_limit
= clamp_t(s8
, pwr_limit
,
1264 -max_power_index
, max_power_index
);
1265 ch_idx
= rtw_channel_to_idx(band
, ch
);
1267 if (regd
>= RTW_REGD_MAX
|| bw
>= RTW_CHANNEL_WIDTH_MAX
||
1268 rs
>= RTW_RATE_SECTION_MAX
|| ch_idx
< 0) {
1270 "wrong txpwr_lmt regd=%u, band=%u bw=%u, rs=%u, ch_idx=%u, pwr_limit=%d\n",
1271 regd
, band
, bw
, rs
, ch_idx
, pwr_limit
);
1275 if (band
== PHY_BAND_2G
) {
1276 hal
->tx_pwr_limit_2g
[regd
][bw
][rs
][ch_idx
] = pwr_limit
;
1277 ww
= hal
->tx_pwr_limit_2g
[RTW_REGD_WW
][bw
][rs
][ch_idx
];
1278 ww
= min_t(s8
, ww
, pwr_limit
);
1279 hal
->tx_pwr_limit_2g
[RTW_REGD_WW
][bw
][rs
][ch_idx
] = ww
;
1280 } else if (band
== PHY_BAND_5G
) {
1281 hal
->tx_pwr_limit_5g
[regd
][bw
][rs
][ch_idx
] = pwr_limit
;
1282 ww
= hal
->tx_pwr_limit_5g
[RTW_REGD_WW
][bw
][rs
][ch_idx
];
1283 ww
= min_t(s8
, ww
, pwr_limit
);
1284 hal
->tx_pwr_limit_5g
[RTW_REGD_WW
][bw
][rs
][ch_idx
] = ww
;
1288 /* cross-reference 5G power limits if values are not assigned */
1290 rtw_xref_5g_txpwr_lmt(struct rtw_dev
*rtwdev
, u8 regd
,
1291 u8 bw
, u8 ch_idx
, u8 rs_ht
, u8 rs_vht
)
1293 struct rtw_hal
*hal
= &rtwdev
->hal
;
1294 u8 max_power_index
= rtwdev
->chip
->max_power_index
;
1295 s8 lmt_ht
= hal
->tx_pwr_limit_5g
[regd
][bw
][rs_ht
][ch_idx
];
1296 s8 lmt_vht
= hal
->tx_pwr_limit_5g
[regd
][bw
][rs_vht
][ch_idx
];
1298 if (lmt_ht
== lmt_vht
)
1301 if (lmt_ht
== max_power_index
)
1302 hal
->tx_pwr_limit_5g
[regd
][bw
][rs_ht
][ch_idx
] = lmt_vht
;
1304 else if (lmt_vht
== max_power_index
)
1305 hal
->tx_pwr_limit_5g
[regd
][bw
][rs_vht
][ch_idx
] = lmt_ht
;
1308 /* cross-reference power limits for ht and vht */
1310 rtw_xref_txpwr_lmt_by_rs(struct rtw_dev
*rtwdev
, u8 regd
, u8 bw
, u8 ch_idx
)
1312 u8 rs_idx
, rs_ht
, rs_vht
;
1313 u8 rs_cmp
[2][2] = {{RTW_RATE_SECTION_HT_1S
, RTW_RATE_SECTION_VHT_1S
},
1314 {RTW_RATE_SECTION_HT_2S
, RTW_RATE_SECTION_VHT_2S
} };
1316 for (rs_idx
= 0; rs_idx
< 2; rs_idx
++) {
1317 rs_ht
= rs_cmp
[rs_idx
][0];
1318 rs_vht
= rs_cmp
[rs_idx
][1];
1320 rtw_xref_5g_txpwr_lmt(rtwdev
, regd
, bw
, ch_idx
, rs_ht
, rs_vht
);
1324 /* cross-reference power limits for 5G channels */
1326 rtw_xref_5g_txpwr_lmt_by_ch(struct rtw_dev
*rtwdev
, u8 regd
, u8 bw
)
1330 for (ch_idx
= 0; ch_idx
< RTW_MAX_CHANNEL_NUM_5G
; ch_idx
++)
1331 rtw_xref_txpwr_lmt_by_rs(rtwdev
, regd
, bw
, ch_idx
);
1334 /* cross-reference power limits for 20/40M bandwidth */
1336 rtw_xref_txpwr_lmt_by_bw(struct rtw_dev
*rtwdev
, u8 regd
)
1340 for (bw
= RTW_CHANNEL_WIDTH_20
; bw
<= RTW_CHANNEL_WIDTH_40
; bw
++)
1341 rtw_xref_5g_txpwr_lmt_by_ch(rtwdev
, regd
, bw
);
1344 /* cross-reference power limits */
1345 static void rtw_xref_txpwr_lmt(struct rtw_dev
*rtwdev
)
1349 for (regd
= 0; regd
< RTW_REGD_MAX
; regd
++)
1350 rtw_xref_txpwr_lmt_by_bw(rtwdev
, regd
);
1353 void rtw_parse_tbl_txpwr_lmt(struct rtw_dev
*rtwdev
,
1354 const struct rtw_table
*tbl
)
1356 const struct rtw_txpwr_lmt_cfg_pair
*p
= tbl
->data
;
1357 const struct rtw_txpwr_lmt_cfg_pair
*end
= p
+ tbl
->size
;
1359 for (; p
< end
; p
++) {
1360 rtw_phy_set_tx_power_limit(rtwdev
, p
->regd
, p
->band
,
1361 p
->bw
, p
->rs
, p
->ch
, p
->txpwr_lmt
);
1364 rtw_xref_txpwr_lmt(rtwdev
);
1367 void rtw_phy_cfg_mac(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
,
1370 rtw_write8(rtwdev
, addr
, data
);
1373 void rtw_phy_cfg_agc(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
,
1376 rtw_write32(rtwdev
, addr
, data
);
1379 void rtw_phy_cfg_bb(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
,
1384 else if (addr
== 0xfd)
1386 else if (addr
== 0xfc)
1388 else if (addr
== 0xfb)
1389 usleep_range(50, 60);
1390 else if (addr
== 0xfa)
1392 else if (addr
== 0xf9)
1395 rtw_write32(rtwdev
, addr
, data
);
1398 void rtw_phy_cfg_rf(struct rtw_dev
*rtwdev
, const struct rtw_table
*tbl
,
1401 if (addr
== 0xffe) {
1403 } else if (addr
== 0xfe) {
1404 usleep_range(100, 110);
1406 rtw_write_rf(rtwdev
, tbl
->rf_path
, addr
, RFREG_MASK
, data
);
1411 static void rtw_load_rfk_table(struct rtw_dev
*rtwdev
)
1413 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1414 struct rtw_dpk_info
*dpk_info
= &rtwdev
->dm_info
.dpk_info
;
1416 if (!chip
->rfk_init_tbl
)
1419 rtw_write32_mask(rtwdev
, 0x1e24, BIT(17), 0x1);
1420 rtw_write32_mask(rtwdev
, 0x1cd0, BIT(28), 0x1);
1421 rtw_write32_mask(rtwdev
, 0x1cd0, BIT(29), 0x1);
1422 rtw_write32_mask(rtwdev
, 0x1cd0, BIT(30), 0x1);
1423 rtw_write32_mask(rtwdev
, 0x1cd0, BIT(31), 0x0);
1425 rtw_load_table(rtwdev
, chip
->rfk_init_tbl
);
1427 dpk_info
->is_dpk_pwr_on
= true;
1430 void rtw_phy_load_tables(struct rtw_dev
*rtwdev
)
1432 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1435 rtw_load_table(rtwdev
, chip
->mac_tbl
);
1436 rtw_load_table(rtwdev
, chip
->bb_tbl
);
1437 rtw_load_table(rtwdev
, chip
->agc_tbl
);
1438 rtw_load_rfk_table(rtwdev
);
1440 for (rf_path
= 0; rf_path
< rtwdev
->hal
.rf_path_num
; rf_path
++) {
1441 const struct rtw_table
*tbl
;
1443 tbl
= chip
->rf_tbl
[rf_path
];
1444 rtw_load_table(rtwdev
, tbl
);
1448 static u8
rtw_get_channel_group(u8 channel
)
1537 static s8
rtw_phy_get_dis_dpd_by_rate_diff(struct rtw_dev
*rtwdev
, u16 rate
)
1539 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1542 if (!chip
->en_dis_dpd
)
1545 #define RTW_DPD_RATE_CHECK(_rate) \
1546 case DESC_RATE ## _rate: \
1547 if (DIS_DPD_RATE ## _rate & chip->dpd_ratemask) \
1548 dpd_diff = -6 * chip->txgi_factor; \
1552 RTW_DPD_RATE_CHECK(6M
);
1553 RTW_DPD_RATE_CHECK(9M
);
1554 RTW_DPD_RATE_CHECK(MCS0
);
1555 RTW_DPD_RATE_CHECK(MCS1
);
1556 RTW_DPD_RATE_CHECK(MCS8
);
1557 RTW_DPD_RATE_CHECK(MCS9
);
1558 RTW_DPD_RATE_CHECK(VHT1SS_MCS0
);
1559 RTW_DPD_RATE_CHECK(VHT1SS_MCS1
);
1560 RTW_DPD_RATE_CHECK(VHT2SS_MCS0
);
1561 RTW_DPD_RATE_CHECK(VHT2SS_MCS1
);
1563 #undef RTW_DPD_RATE_CHECK
1568 static u8
rtw_phy_get_2g_tx_power_index(struct rtw_dev
*rtwdev
,
1569 struct rtw_2g_txpwr_idx
*pwr_idx_2g
,
1570 enum rtw_bandwidth bandwidth
,
1573 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1577 u8 factor
= chip
->txgi_factor
;
1579 if (rate
<= DESC_RATE11M
)
1580 tx_power
= pwr_idx_2g
->cck_base
[group
];
1582 tx_power
= pwr_idx_2g
->bw40_base
[group
];
1584 if (rate
>= DESC_RATE6M
&& rate
<= DESC_RATE54M
)
1585 tx_power
+= pwr_idx_2g
->ht_1s_diff
.ofdm
* factor
;
1587 mcs_rate
= (rate
>= DESC_RATEMCS0
&& rate
<= DESC_RATEMCS15
) ||
1588 (rate
>= DESC_RATEVHT1SS_MCS0
&&
1589 rate
<= DESC_RATEVHT2SS_MCS9
);
1590 above_2ss
= (rate
>= DESC_RATEMCS8
&& rate
<= DESC_RATEMCS15
) ||
1591 (rate
>= DESC_RATEVHT2SS_MCS0
);
1596 switch (bandwidth
) {
1600 case RTW_CHANNEL_WIDTH_20
:
1601 tx_power
+= pwr_idx_2g
->ht_1s_diff
.bw20
* factor
;
1603 tx_power
+= pwr_idx_2g
->ht_2s_diff
.bw20
* factor
;
1605 case RTW_CHANNEL_WIDTH_40
:
1606 /* bw40 is the base power */
1608 tx_power
+= pwr_idx_2g
->ht_2s_diff
.bw40
* factor
;
1615 static u8
rtw_phy_get_5g_tx_power_index(struct rtw_dev
*rtwdev
,
1616 struct rtw_5g_txpwr_idx
*pwr_idx_5g
,
1617 enum rtw_bandwidth bandwidth
,
1620 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1625 u8 factor
= chip
->txgi_factor
;
1627 tx_power
= pwr_idx_5g
->bw40_base
[group
];
1629 mcs_rate
= (rate
>= DESC_RATEMCS0
&& rate
<= DESC_RATEMCS15
) ||
1630 (rate
>= DESC_RATEVHT1SS_MCS0
&&
1631 rate
<= DESC_RATEVHT2SS_MCS9
);
1632 above_2ss
= (rate
>= DESC_RATEMCS8
&& rate
<= DESC_RATEMCS15
) ||
1633 (rate
>= DESC_RATEVHT2SS_MCS0
);
1636 tx_power
+= pwr_idx_5g
->ht_1s_diff
.ofdm
* factor
;
1640 switch (bandwidth
) {
1644 case RTW_CHANNEL_WIDTH_20
:
1645 tx_power
+= pwr_idx_5g
->ht_1s_diff
.bw20
* factor
;
1647 tx_power
+= pwr_idx_5g
->ht_2s_diff
.bw20
* factor
;
1649 case RTW_CHANNEL_WIDTH_40
:
1650 /* bw40 is the base power */
1652 tx_power
+= pwr_idx_5g
->ht_2s_diff
.bw40
* factor
;
1654 case RTW_CHANNEL_WIDTH_80
:
1655 /* the base idx of bw80 is the average of bw40+/bw40- */
1656 lower
= pwr_idx_5g
->bw40_base
[group
];
1657 upper
= pwr_idx_5g
->bw40_base
[group
+ 1];
1659 tx_power
= (lower
+ upper
) / 2;
1660 tx_power
+= pwr_idx_5g
->vht_1s_diff
.bw80
* factor
;
1662 tx_power
+= pwr_idx_5g
->vht_2s_diff
.bw80
* factor
;
1669 static s8
rtw_phy_get_tx_power_limit(struct rtw_dev
*rtwdev
, u8 band
,
1670 enum rtw_bandwidth bw
, u8 rf_path
,
1671 u8 rate
, u8 channel
, u8 regd
)
1673 struct rtw_hal
*hal
= &rtwdev
->hal
;
1674 u8
*cch_by_bw
= hal
->cch_by_bw
;
1675 s8 power_limit
= (s8
)rtwdev
->chip
->max_power_index
;
1681 if (regd
> RTW_REGD_WW
)
1684 if (rate
>= DESC_RATE1M
&& rate
<= DESC_RATE11M
)
1685 rs
= RTW_RATE_SECTION_CCK
;
1686 else if (rate
>= DESC_RATE6M
&& rate
<= DESC_RATE54M
)
1687 rs
= RTW_RATE_SECTION_OFDM
;
1688 else if (rate
>= DESC_RATEMCS0
&& rate
<= DESC_RATEMCS7
)
1689 rs
= RTW_RATE_SECTION_HT_1S
;
1690 else if (rate
>= DESC_RATEMCS8
&& rate
<= DESC_RATEMCS15
)
1691 rs
= RTW_RATE_SECTION_HT_2S
;
1692 else if (rate
>= DESC_RATEVHT1SS_MCS0
&& rate
<= DESC_RATEVHT1SS_MCS9
)
1693 rs
= RTW_RATE_SECTION_VHT_1S
;
1694 else if (rate
>= DESC_RATEVHT2SS_MCS0
&& rate
<= DESC_RATEVHT2SS_MCS9
)
1695 rs
= RTW_RATE_SECTION_VHT_2S
;
1699 /* only 20M BW with cck and ofdm */
1700 if (rs
== RTW_RATE_SECTION_CCK
|| rs
== RTW_RATE_SECTION_OFDM
)
1701 bw
= RTW_CHANNEL_WIDTH_20
;
1703 /* only 20/40M BW with ht */
1704 if (rs
== RTW_RATE_SECTION_HT_1S
|| rs
== RTW_RATE_SECTION_HT_2S
)
1705 bw
= min_t(u8
, bw
, RTW_CHANNEL_WIDTH_40
);
1707 /* select min power limit among [20M BW ~ current BW] */
1708 for (cur_bw
= RTW_CHANNEL_WIDTH_20
; cur_bw
<= bw
; cur_bw
++) {
1709 cur_ch
= cch_by_bw
[cur_bw
];
1711 ch_idx
= rtw_channel_to_idx(band
, cur_ch
);
1715 cur_lmt
= cur_ch
<= RTW_MAX_CHANNEL_NUM_2G
?
1716 hal
->tx_pwr_limit_2g
[regd
][cur_bw
][rs
][ch_idx
] :
1717 hal
->tx_pwr_limit_5g
[regd
][cur_bw
][rs
][ch_idx
];
1719 power_limit
= min_t(s8
, cur_lmt
, power_limit
);
1725 WARN(1, "invalid arguments, band=%d, bw=%d, path=%d, rate=%d, ch=%d\n",
1726 band
, bw
, rf_path
, rate
, channel
);
1727 return (s8
)rtwdev
->chip
->max_power_index
;
1730 void rtw_get_tx_power_params(struct rtw_dev
*rtwdev
, u8 path
, u8 rate
, u8 bw
,
1731 u8 ch
, u8 regd
, struct rtw_power_params
*pwr_param
)
1733 struct rtw_hal
*hal
= &rtwdev
->hal
;
1734 struct rtw_txpwr_idx
*pwr_idx
;
1736 u8
*base
= &pwr_param
->pwr_base
;
1737 s8
*offset
= &pwr_param
->pwr_offset
;
1738 s8
*limit
= &pwr_param
->pwr_limit
;
1740 pwr_idx
= &rtwdev
->efuse
.txpwr_idx_table
[path
];
1741 group
= rtw_get_channel_group(ch
);
1743 /* base power index for 2.4G/5G */
1744 if (IS_CH_2G_BAND(ch
)) {
1746 *base
= rtw_phy_get_2g_tx_power_index(rtwdev
,
1747 &pwr_idx
->pwr_idx_2g
,
1749 *offset
= hal
->tx_pwr_by_rate_offset_2g
[path
][rate
];
1752 *base
= rtw_phy_get_5g_tx_power_index(rtwdev
,
1753 &pwr_idx
->pwr_idx_5g
,
1755 *offset
= hal
->tx_pwr_by_rate_offset_5g
[path
][rate
];
1758 *limit
= rtw_phy_get_tx_power_limit(rtwdev
, band
, bw
, path
,
1763 rtw_phy_get_tx_power_index(struct rtw_dev
*rtwdev
, u8 rf_path
, u8 rate
,
1764 enum rtw_bandwidth bandwidth
, u8 channel
, u8 regd
)
1766 struct rtw_power_params pwr_param
= {0};
1770 rtw_get_tx_power_params(rtwdev
, rf_path
, rate
, bandwidth
,
1771 channel
, regd
, &pwr_param
);
1773 tx_power
= pwr_param
.pwr_base
;
1774 offset
= min_t(s8
, pwr_param
.pwr_offset
, pwr_param
.pwr_limit
);
1776 if (rtwdev
->chip
->en_dis_dpd
)
1777 offset
+= rtw_phy_get_dis_dpd_by_rate_diff(rtwdev
, rate
);
1781 if (tx_power
> rtwdev
->chip
->max_power_index
)
1782 tx_power
= rtwdev
->chip
->max_power_index
;
1787 static void rtw_phy_set_tx_power_index_by_rs(struct rtw_dev
*rtwdev
,
1788 u8 ch
, u8 path
, u8 rs
)
1790 struct rtw_hal
*hal
= &rtwdev
->hal
;
1791 u8 regd
= rtwdev
->regd
.txpwr_regd
;
1799 if (rs
>= RTW_RATE_SECTION_MAX
)
1802 rates
= rtw_rate_section
[rs
];
1803 size
= rtw_rate_size
[rs
];
1804 bw
= hal
->current_band_width
;
1805 for (i
= 0; i
< size
; i
++) {
1807 pwr_idx
= rtw_phy_get_tx_power_index(rtwdev
, path
, rate
,
1809 hal
->tx_pwr_tbl
[path
][rate
] = pwr_idx
;
1813 /* set tx power level by path for each rates, note that the order of the rates
1814 * are *very* important, bacause 8822B/8821C combines every four bytes of tx
1815 * power index into a four-byte power index register, and calls set_tx_agc to
1816 * write these values into hardware
1818 static void rtw_phy_set_tx_power_level_by_path(struct rtw_dev
*rtwdev
,
1821 struct rtw_hal
*hal
= &rtwdev
->hal
;
1824 /* do not need cck rates if we are not in 2.4G */
1825 if (hal
->current_band_type
== RTW_BAND_2G
)
1826 rs
= RTW_RATE_SECTION_CCK
;
1828 rs
= RTW_RATE_SECTION_OFDM
;
1830 for (; rs
< RTW_RATE_SECTION_MAX
; rs
++)
1831 rtw_phy_set_tx_power_index_by_rs(rtwdev
, ch
, path
, rs
);
1834 void rtw_phy_set_tx_power_level(struct rtw_dev
*rtwdev
, u8 channel
)
1836 struct rtw_chip_info
*chip
= rtwdev
->chip
;
1837 struct rtw_hal
*hal
= &rtwdev
->hal
;
1840 mutex_lock(&hal
->tx_power_mutex
);
1842 for (path
= 0; path
< hal
->rf_path_num
; path
++)
1843 rtw_phy_set_tx_power_level_by_path(rtwdev
, channel
, path
);
1845 chip
->ops
->set_tx_power_index(rtwdev
);
1846 mutex_unlock(&hal
->tx_power_mutex
);
1850 rtw_phy_tx_power_by_rate_config_by_path(struct rtw_hal
*hal
, u8 path
,
1851 u8 rs
, u8 size
, u8
*rates
)
1854 u8 base_idx
, rate_idx
;
1855 s8 base_2g
, base_5g
;
1857 if (rs
>= RTW_RATE_SECTION_VHT_1S
)
1858 base_idx
= rates
[size
- 3];
1860 base_idx
= rates
[size
- 1];
1861 base_2g
= hal
->tx_pwr_by_rate_offset_2g
[path
][base_idx
];
1862 base_5g
= hal
->tx_pwr_by_rate_offset_5g
[path
][base_idx
];
1863 hal
->tx_pwr_by_rate_base_2g
[path
][rs
] = base_2g
;
1864 hal
->tx_pwr_by_rate_base_5g
[path
][rs
] = base_5g
;
1865 for (rate
= 0; rate
< size
; rate
++) {
1866 rate_idx
= rates
[rate
];
1867 hal
->tx_pwr_by_rate_offset_2g
[path
][rate_idx
] -= base_2g
;
1868 hal
->tx_pwr_by_rate_offset_5g
[path
][rate_idx
] -= base_5g
;
1872 void rtw_phy_tx_power_by_rate_config(struct rtw_hal
*hal
)
1876 for (path
= 0; path
< RTW_RF_PATH_MAX
; path
++) {
1877 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1878 RTW_RATE_SECTION_CCK
,
1879 rtw_cck_size
, rtw_cck_rates
);
1880 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1881 RTW_RATE_SECTION_OFDM
,
1882 rtw_ofdm_size
, rtw_ofdm_rates
);
1883 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1884 RTW_RATE_SECTION_HT_1S
,
1885 rtw_ht_1s_size
, rtw_ht_1s_rates
);
1886 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1887 RTW_RATE_SECTION_HT_2S
,
1888 rtw_ht_2s_size
, rtw_ht_2s_rates
);
1889 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1890 RTW_RATE_SECTION_VHT_1S
,
1891 rtw_vht_1s_size
, rtw_vht_1s_rates
);
1892 rtw_phy_tx_power_by_rate_config_by_path(hal
, path
,
1893 RTW_RATE_SECTION_VHT_2S
,
1894 rtw_vht_2s_size
, rtw_vht_2s_rates
);
1899 __rtw_phy_tx_power_limit_config(struct rtw_hal
*hal
, u8 regd
, u8 bw
, u8 rs
)
1904 for (ch
= 0; ch
< RTW_MAX_CHANNEL_NUM_2G
; ch
++) {
1905 base
= hal
->tx_pwr_by_rate_base_2g
[0][rs
];
1906 hal
->tx_pwr_limit_2g
[regd
][bw
][rs
][ch
] -= base
;
1909 for (ch
= 0; ch
< RTW_MAX_CHANNEL_NUM_5G
; ch
++) {
1910 base
= hal
->tx_pwr_by_rate_base_5g
[0][rs
];
1911 hal
->tx_pwr_limit_5g
[regd
][bw
][rs
][ch
] -= base
;
1915 void rtw_phy_tx_power_limit_config(struct rtw_hal
*hal
)
1919 /* default at channel 1 */
1920 hal
->cch_by_bw
[RTW_CHANNEL_WIDTH_20
] = 1;
1922 for (regd
= 0; regd
< RTW_REGD_MAX
; regd
++)
1923 for (bw
= 0; bw
< RTW_CHANNEL_WIDTH_MAX
; bw
++)
1924 for (rs
= 0; rs
< RTW_RATE_SECTION_MAX
; rs
++)
1925 __rtw_phy_tx_power_limit_config(hal
, regd
, bw
, rs
);
1928 static void rtw_phy_init_tx_power_limit(struct rtw_dev
*rtwdev
,
1929 u8 regd
, u8 bw
, u8 rs
)
1931 struct rtw_hal
*hal
= &rtwdev
->hal
;
1932 s8 max_power_index
= (s8
)rtwdev
->chip
->max_power_index
;
1936 for (ch
= 0; ch
< RTW_MAX_CHANNEL_NUM_2G
; ch
++)
1937 hal
->tx_pwr_limit_2g
[regd
][bw
][rs
][ch
] = max_power_index
;
1940 for (ch
= 0; ch
< RTW_MAX_CHANNEL_NUM_5G
; ch
++)
1941 hal
->tx_pwr_limit_5g
[regd
][bw
][rs
][ch
] = max_power_index
;
1944 void rtw_phy_init_tx_power(struct rtw_dev
*rtwdev
)
1946 struct rtw_hal
*hal
= &rtwdev
->hal
;
1947 u8 regd
, path
, rate
, rs
, bw
;
1949 /* init tx power by rate offset */
1950 for (path
= 0; path
< RTW_RF_PATH_MAX
; path
++) {
1951 for (rate
= 0; rate
< DESC_RATE_MAX
; rate
++) {
1952 hal
->tx_pwr_by_rate_offset_2g
[path
][rate
] = 0;
1953 hal
->tx_pwr_by_rate_offset_5g
[path
][rate
] = 0;
1957 /* init tx power limit */
1958 for (regd
= 0; regd
< RTW_REGD_MAX
; regd
++)
1959 for (bw
= 0; bw
< RTW_CHANNEL_WIDTH_MAX
; bw
++)
1960 for (rs
= 0; rs
< RTW_RATE_SECTION_MAX
; rs
++)
1961 rtw_phy_init_tx_power_limit(rtwdev
, regd
, bw
,
1965 void rtw_phy_config_swing_table(struct rtw_dev
*rtwdev
,
1966 struct rtw_swing_table
*swing_table
)
1968 const struct rtw_pwr_track_tbl
*tbl
= rtwdev
->chip
->pwr_track_tbl
;
1969 u8 channel
= rtwdev
->hal
.current_channel
;
1971 if (IS_CH_2G_BAND(channel
)) {
1972 if (rtwdev
->dm_info
.tx_rate
<= DESC_RATE11M
) {
1973 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_2g_ccka_p
;
1974 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_2g_ccka_n
;
1975 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_2g_cckb_p
;
1976 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_2g_cckb_n
;
1978 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_2ga_p
;
1979 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_2ga_n
;
1980 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_2gb_p
;
1981 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_2gb_n
;
1983 } else if (IS_CH_5G_BAND_1(channel
) || IS_CH_5G_BAND_2(channel
)) {
1984 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_5ga_p
[RTW_PWR_TRK_5G_1
];
1985 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_5ga_n
[RTW_PWR_TRK_5G_1
];
1986 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_5gb_p
[RTW_PWR_TRK_5G_1
];
1987 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_5gb_n
[RTW_PWR_TRK_5G_1
];
1988 } else if (IS_CH_5G_BAND_3(channel
)) {
1989 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_5ga_p
[RTW_PWR_TRK_5G_2
];
1990 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_5ga_n
[RTW_PWR_TRK_5G_2
];
1991 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_5gb_p
[RTW_PWR_TRK_5G_2
];
1992 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_5gb_n
[RTW_PWR_TRK_5G_2
];
1993 } else if (IS_CH_5G_BAND_4(channel
)) {
1994 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_5ga_p
[RTW_PWR_TRK_5G_3
];
1995 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_5ga_n
[RTW_PWR_TRK_5G_3
];
1996 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_5gb_p
[RTW_PWR_TRK_5G_3
];
1997 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_5gb_n
[RTW_PWR_TRK_5G_3
];
1999 swing_table
->p
[RF_PATH_A
] = tbl
->pwrtrk_2ga_p
;
2000 swing_table
->n
[RF_PATH_A
] = tbl
->pwrtrk_2ga_n
;
2001 swing_table
->p
[RF_PATH_B
] = tbl
->pwrtrk_2gb_p
;
2002 swing_table
->n
[RF_PATH_B
] = tbl
->pwrtrk_2gb_n
;
2006 void rtw_phy_pwrtrack_avg(struct rtw_dev
*rtwdev
, u8 thermal
, u8 path
)
2008 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
2010 ewma_thermal_add(&dm_info
->avg_thermal
[path
], thermal
);
2011 dm_info
->thermal_avg
[path
] =
2012 ewma_thermal_read(&dm_info
->avg_thermal
[path
]);
2015 bool rtw_phy_pwrtrack_thermal_changed(struct rtw_dev
*rtwdev
, u8 thermal
,
2018 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
2019 u8 avg
= ewma_thermal_read(&dm_info
->avg_thermal
[path
]);
2027 u8
rtw_phy_pwrtrack_get_delta(struct rtw_dev
*rtwdev
, u8 path
)
2029 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
2030 u8 therm_avg
, therm_efuse
, therm_delta
;
2032 therm_avg
= dm_info
->thermal_avg
[path
];
2033 therm_efuse
= rtwdev
->efuse
.thermal_meter
[path
];
2034 therm_delta
= abs(therm_avg
- therm_efuse
);
2036 return min_t(u8
, therm_delta
, RTW_PWR_TRK_TBL_SZ
- 1);
2039 s8
rtw_phy_pwrtrack_get_pwridx(struct rtw_dev
*rtwdev
,
2040 struct rtw_swing_table
*swing_table
,
2041 u8 tbl_path
, u8 therm_path
, u8 delta
)
2043 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
2044 const u8
*delta_swing_table_idx_pos
;
2045 const u8
*delta_swing_table_idx_neg
;
2047 if (delta
>= RTW_PWR_TRK_TBL_SZ
) {
2048 rtw_warn(rtwdev
, "power track table overflow\n");
2053 rtw_warn(rtwdev
, "swing table not configured\n");
2057 delta_swing_table_idx_pos
= swing_table
->p
[tbl_path
];
2058 delta_swing_table_idx_neg
= swing_table
->n
[tbl_path
];
2060 if (!delta_swing_table_idx_pos
|| !delta_swing_table_idx_neg
) {
2061 rtw_warn(rtwdev
, "invalid swing table index\n");
2065 if (dm_info
->thermal_avg
[therm_path
] >
2066 rtwdev
->efuse
.thermal_meter
[therm_path
])
2067 return delta_swing_table_idx_pos
[delta
];
2069 return -delta_swing_table_idx_neg
[delta
];
2072 bool rtw_phy_pwrtrack_need_iqk(struct rtw_dev
*rtwdev
)
2074 struct rtw_dm_info
*dm_info
= &rtwdev
->dm_info
;
2077 delta_iqk
= abs(dm_info
->thermal_avg
[0] - dm_info
->thermal_meter_k
);
2078 if (delta_iqk
>= rtwdev
->chip
->iqk_threshold
) {
2079 dm_info
->thermal_meter_k
= dm_info
->thermal_avg
[0];