1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2010 Realtek Corporation.*/
14 #include "../btcoexist/halbt_precomp.h"
18 #define READ_NEXT_PAIR(array_table, v1, v2, i) \
21 v1 = array_table[i]; \
22 v2 = array_table[i+1]; \
25 static u32 _rtl8821ae_phy_rf_serial_read(struct ieee80211_hw *hw,
26 enum radio_path rfpath, u32 offset);
27 static void _rtl8821ae_phy_rf_serial_write(struct ieee80211_hw *hw,
28 enum radio_path rfpath, u32 offset,
30 static u32 _rtl8821ae_phy_calculate_bit_shift(u32 bitmask);
31 static bool _rtl8821ae_phy_bb8821a_config_parafile(struct ieee80211_hw *hw);
32 /*static bool _rtl8812ae_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);*/
33 static bool _rtl8821ae_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);
34 static bool _rtl8821ae_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
36 static bool _rtl8821ae_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
38 static void phy_init_bb_rf_register_definition(struct ieee80211_hw *hw);
40 static long _rtl8821ae_phy_txpwr_idx_to_dbm(struct ieee80211_hw *hw,
41 enum wireless_mode wirelessmode,
43 static void rtl8821ae_phy_set_rf_on(struct ieee80211_hw *hw);
44 static void rtl8821ae_phy_set_io(struct ieee80211_hw *hw);
46 static void rtl8812ae_fixspur(struct ieee80211_hw *hw,
47 enum ht_channel_width band_width, u8 channel)
49 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
51 /*C cut Item12 ADC FIFO CLOCK*/
52 if (IS_VENDOR_8812A_C_CUT(rtlhal->version)) {
53 if (band_width == HT_CHANNEL_WIDTH_20_40 && channel == 11)
54 rtl_set_bbreg(hw, RRFMOD, 0xC00, 0x3);
55 /* 0x8AC[11:10] = 2'b11*/
57 rtl_set_bbreg(hw, RRFMOD, 0xC00, 0x2);
58 /* 0x8AC[11:10] = 2'b10*/
60 /* <20120914, Kordan> A workarould to resolve
61 * 2480Mhz spur by setting ADC clock as 160M. (Asked by Binson)
63 if (band_width == HT_CHANNEL_WIDTH_20 &&
64 (channel == 13 || channel == 14)) {
65 rtl_set_bbreg(hw, RRFMOD, 0x300, 0x3);
66 /*0x8AC[9:8] = 2'b11*/
67 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 1);
69 } else if (band_width == HT_CHANNEL_WIDTH_20_40 &&
71 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 1);
73 } else if (band_width != HT_CHANNEL_WIDTH_80) {
74 rtl_set_bbreg(hw, RRFMOD, 0x300, 0x2);
75 /*0x8AC[9:8] = 2'b10*/
76 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 0);
79 } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
80 /* <20120914, Kordan> A workarould to resolve
81 * 2480Mhz spur by setting ADC clock as 160M.
83 if (band_width == HT_CHANNEL_WIDTH_20 &&
84 (channel == 13 || channel == 14))
85 rtl_set_bbreg(hw, RRFMOD, 0x300, 0x3);
87 else if (channel <= 14) /*2.4G only*/
88 rtl_set_bbreg(hw, RRFMOD, 0x300, 0x2);
93 u32 rtl8821ae_phy_query_bb_reg(struct ieee80211_hw *hw, u32 regaddr,
96 struct rtl_priv *rtlpriv = rtl_priv(hw);
97 u32 returnvalue, originalvalue, bitshift;
99 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
100 "regaddr(%#x), bitmask(%#x)\n",
102 originalvalue = rtl_read_dword(rtlpriv, regaddr);
103 bitshift = _rtl8821ae_phy_calculate_bit_shift(bitmask);
104 returnvalue = (originalvalue & bitmask) >> bitshift;
106 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
107 "BBR MASK=0x%x Addr[0x%x]=0x%x\n",
108 bitmask, regaddr, originalvalue);
112 void rtl8821ae_phy_set_bb_reg(struct ieee80211_hw *hw,
113 u32 regaddr, u32 bitmask, u32 data)
115 struct rtl_priv *rtlpriv = rtl_priv(hw);
116 u32 originalvalue, bitshift;
118 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
119 "regaddr(%#x), bitmask(%#x), data(%#x)\n",
120 regaddr, bitmask, data);
122 if (bitmask != MASKDWORD) {
123 originalvalue = rtl_read_dword(rtlpriv, regaddr);
124 bitshift = _rtl8821ae_phy_calculate_bit_shift(bitmask);
125 data = ((originalvalue & (~bitmask)) |
126 ((data << bitshift) & bitmask));
129 rtl_write_dword(rtlpriv, regaddr, data);
131 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
132 "regaddr(%#x), bitmask(%#x), data(%#x)\n",
133 regaddr, bitmask, data);
136 u32 rtl8821ae_phy_query_rf_reg(struct ieee80211_hw *hw,
137 enum radio_path rfpath, u32 regaddr,
140 struct rtl_priv *rtlpriv = rtl_priv(hw);
141 u32 original_value, readback_value, bitshift;
143 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
144 "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
145 regaddr, rfpath, bitmask);
147 spin_lock(&rtlpriv->locks.rf_lock);
149 original_value = _rtl8821ae_phy_rf_serial_read(hw, rfpath, regaddr);
150 bitshift = _rtl8821ae_phy_calculate_bit_shift(bitmask);
151 readback_value = (original_value & bitmask) >> bitshift;
153 spin_unlock(&rtlpriv->locks.rf_lock);
155 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
156 "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
157 regaddr, rfpath, bitmask, original_value);
159 return readback_value;
162 void rtl8821ae_phy_set_rf_reg(struct ieee80211_hw *hw,
163 enum radio_path rfpath,
164 u32 regaddr, u32 bitmask, u32 data)
166 struct rtl_priv *rtlpriv = rtl_priv(hw);
167 u32 original_value, bitshift;
169 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
170 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
171 regaddr, bitmask, data, rfpath);
173 spin_lock(&rtlpriv->locks.rf_lock);
175 if (bitmask != RFREG_OFFSET_MASK) {
177 _rtl8821ae_phy_rf_serial_read(hw, rfpath, regaddr);
178 bitshift = _rtl8821ae_phy_calculate_bit_shift(bitmask);
179 data = ((original_value & (~bitmask)) | (data << bitshift));
182 _rtl8821ae_phy_rf_serial_write(hw, rfpath, regaddr, data);
184 spin_unlock(&rtlpriv->locks.rf_lock);
186 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
187 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
188 regaddr, bitmask, data, rfpath);
191 static u32 _rtl8821ae_phy_rf_serial_read(struct ieee80211_hw *hw,
192 enum radio_path rfpath, u32 offset)
194 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
195 bool is_pi_mode = false;
198 /* 2009/06/17 MH We can not execute IO for power
199 save or other accident mode.*/
200 if (RT_CANNOT_IO(hw)) {
201 pr_err("return all one\n");
204 /* <20120809, Kordan> CCA OFF(when entering),
205 asked by James to avoid reading the wrong value.
206 <20120828, Kordan> Toggling CCA would affect RF 0x0, skip it!*/
208 !((rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) ||
209 (IS_VENDOR_8812A_C_CUT(rtlhal->version))))
210 rtl_set_bbreg(hw, RCCAONSEC, 0x8, 1);
213 if (rfpath == RF90_PATH_A)
214 is_pi_mode = (bool)rtl_get_bbreg(hw, 0xC00, 0x4);
215 else if (rfpath == RF90_PATH_B)
216 is_pi_mode = (bool)rtl_get_bbreg(hw, 0xE00, 0x4);
218 rtl_set_bbreg(hw, RHSSIREAD_8821AE, 0xff, offset);
220 if ((rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) ||
221 (IS_VENDOR_8812A_C_CUT(rtlhal->version)))
225 if (rfpath == RF90_PATH_A)
227 rtl_get_bbreg(hw, RA_PIREAD_8821A, BLSSIREADBACKDATA);
228 else if (rfpath == RF90_PATH_B)
230 rtl_get_bbreg(hw, RB_PIREAD_8821A, BLSSIREADBACKDATA);
232 if (rfpath == RF90_PATH_A)
234 rtl_get_bbreg(hw, RA_SIREAD_8821A, BLSSIREADBACKDATA);
235 else if (rfpath == RF90_PATH_B)
237 rtl_get_bbreg(hw, RB_SIREAD_8821A, BLSSIREADBACKDATA);
240 /*<20120809, Kordan> CCA ON(when exiting),
241 * asked by James to avoid reading the wrong value.
242 * <20120828, Kordan> Toggling CCA would affect RF 0x0, skip it!
245 !((rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) ||
246 (IS_VENDOR_8812A_C_CUT(rtlhal->version))))
247 rtl_set_bbreg(hw, RCCAONSEC, 0x8, 0);
251 static void _rtl8821ae_phy_rf_serial_write(struct ieee80211_hw *hw,
252 enum radio_path rfpath, u32 offset,
255 struct rtl_priv *rtlpriv = rtl_priv(hw);
256 struct rtl_phy *rtlphy = &rtlpriv->phy;
257 struct bb_reg_def *pphyreg = &rtlphy->phyreg_def[rfpath];
261 if (RT_CANNOT_IO(hw)) {
267 data_and_addr = ((newoffset << 20) |
268 (data & 0x000fffff)) & 0x0fffffff;
269 rtl_set_bbreg(hw, pphyreg->rf3wire_offset, MASKDWORD, data_and_addr);
270 rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
271 "RFW-%d Addr[0x%x]=0x%x\n",
272 rfpath, pphyreg->rf3wire_offset, data_and_addr);
275 static u32 _rtl8821ae_phy_calculate_bit_shift(u32 bitmask)
279 for (i = 0; i <= 31; i++) {
280 if (((bitmask >> i) & 0x1) == 1)
286 bool rtl8821ae_phy_mac_config(struct ieee80211_hw *hw)
290 rtstatus = _rtl8821ae_phy_config_mac_with_headerfile(hw);
295 bool rtl8821ae_phy_bb_config(struct ieee80211_hw *hw)
297 bool rtstatus = true;
298 struct rtl_priv *rtlpriv = rtl_priv(hw);
299 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
300 struct rtl_phy *rtlphy = &rtlpriv->phy;
301 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
305 phy_init_bb_rf_register_definition(hw);
307 regval = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN);
309 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, regval);
310 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN,
311 regval | FEN_BB_GLB_RSTN | FEN_BBRSTB);
313 rtl_write_byte(rtlpriv, REG_RF_CTRL, 0x7);
314 rtl_write_byte(rtlpriv, REG_OPT_CTRL + 2, 0x7);
316 rtstatus = _rtl8821ae_phy_bb8821a_config_parafile(hw);
318 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
319 crystal_cap = rtlefuse->crystalcap & 0x3F;
320 rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0x7FF80000,
321 (crystal_cap | (crystal_cap << 6)));
323 crystal_cap = rtlefuse->crystalcap & 0x3F;
324 rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
325 (crystal_cap | (crystal_cap << 6)));
327 rtlphy->reg_837 = rtl_read_byte(rtlpriv, 0x837);
332 bool rtl8821ae_phy_rf_config(struct ieee80211_hw *hw)
334 return rtl8821ae_phy_rf6052_config(hw);
337 static void _rtl8812ae_phy_set_rfe_reg_24g(struct ieee80211_hw *hw)
339 struct rtl_priv *rtlpriv = rtl_priv(hw);
340 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
343 switch (rtlhal->rfe_type) {
345 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x54337770);
346 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x54337770);
347 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x010);
348 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x010);
349 rtl_set_bbreg(hw, 0x900, 0x00000303, 0x1);
352 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x77777777);
353 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77777777);
354 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x001);
355 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x001);
358 rtl_write_byte(rtlpriv, RA_RFE_PINMUX + 2, 0x77);
359 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77777777);
360 tmp = rtl_read_byte(rtlpriv, RA_RFE_INV + 3);
361 rtl_write_byte(rtlpriv, RA_RFE_INV + 3, tmp & ~0x1);
362 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x000);
365 if (rtlpriv->btcoexist.bt_coexistence) {
366 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xffffff, 0x777777);
367 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD,
369 rtl_set_bbreg(hw, RA_RFE_INV, 0x33f00000, 0x000);
370 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x000);
377 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x77777777);
378 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77777777);
379 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x000);
380 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x000);
385 static void _rtl8812ae_phy_set_rfe_reg_5g(struct ieee80211_hw *hw)
387 struct rtl_priv *rtlpriv = rtl_priv(hw);
388 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
391 switch (rtlhal->rfe_type) {
393 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x77337717);
394 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77337717);
395 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x010);
396 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x010);
399 if (rtlpriv->btcoexist.bt_coexistence) {
400 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xffffff, 0x337717);
401 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD,
403 rtl_set_bbreg(hw, RA_RFE_INV, 0x33f00000, 0x000);
404 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x000);
406 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD,
408 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD,
410 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x000);
411 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x000);
415 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x54337717);
416 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x54337717);
417 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x010);
418 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x010);
419 rtl_set_bbreg(hw, 0x900, 0x00000303, 0x1);
422 rtl_write_byte(rtlpriv, RA_RFE_PINMUX + 2, 0x33);
423 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77337777);
424 tmp = rtl_read_byte(rtlpriv, RA_RFE_INV + 3);
425 rtl_write_byte(rtlpriv, RA_RFE_INV + 3, tmp | 0x1);
426 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x010);
431 rtl_set_bbreg(hw, RA_RFE_PINMUX, BMASKDWORD, 0x77337777);
432 rtl_set_bbreg(hw, RB_RFE_PINMUX, BMASKDWORD, 0x77337777);
433 rtl_set_bbreg(hw, RA_RFE_INV, BMASKRFEINV, 0x010);
434 rtl_set_bbreg(hw, RB_RFE_INV, BMASKRFEINV, 0x010);
439 u32 phy_get_tx_swing_8812A(struct ieee80211_hw *hw, u8 band,
442 struct rtl_priv *rtlpriv = rtl_priv(hw);
443 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
444 struct rtl_dm *rtldm = rtl_dm(rtlpriv);
445 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
446 s8 reg_swing_2g = -1;/* 0xff; */
447 s8 reg_swing_5g = -1;/* 0xff; */
448 s8 swing_2g = -1 * reg_swing_2g;
449 s8 swing_5g = -1 * reg_swing_5g;
451 const s8 auto_temp = -1;
453 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
454 "===> PHY_GetTXBBSwing_8812A, bbSwing_2G: %d, bbSwing_5G: %d,autoload_failflag=%d.\n",
455 (int)swing_2g, (int)swing_5g,
456 (int)rtlefuse->autoload_failflag);
458 if (rtlefuse->autoload_failflag) {
459 if (band == BAND_ON_2_4G) {
460 rtldm->swing_diff_2g = swing_2g;
462 out = 0x200; /* 0 dB */
463 } else if (swing_2g == -3) {
464 out = 0x16A; /* -3 dB */
465 } else if (swing_2g == -6) {
466 out = 0x101; /* -6 dB */
467 } else if (swing_2g == -9) {
468 out = 0x0B6; /* -9 dB */
470 rtldm->swing_diff_2g = 0;
473 } else if (band == BAND_ON_5G) {
474 rtldm->swing_diff_5g = swing_5g;
476 out = 0x200; /* 0 dB */
477 } else if (swing_5g == -3) {
478 out = 0x16A; /* -3 dB */
479 } else if (swing_5g == -6) {
480 out = 0x101; /* -6 dB */
481 } else if (swing_5g == -9) {
482 out = 0x0B6; /* -9 dB */
484 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
485 rtldm->swing_diff_5g = -3;
488 rtldm->swing_diff_5g = 0;
493 rtldm->swing_diff_2g = -3;
494 rtldm->swing_diff_5g = -3;
495 out = 0x16A; /* -3 dB */
498 u32 swing = 0, swing_a = 0, swing_b = 0;
500 if (band == BAND_ON_2_4G) {
501 if (reg_swing_2g == auto_temp) {
502 efuse_shadow_read(hw, 1, 0xC6, (u32 *)&swing);
503 swing = (swing == 0xFF) ? 0x00 : swing;
504 } else if (swing_2g == 0) {
505 swing = 0x00; /* 0 dB */
506 } else if (swing_2g == -3) {
507 swing = 0x05; /* -3 dB */
508 } else if (swing_2g == -6) {
509 swing = 0x0A; /* -6 dB */
510 } else if (swing_2g == -9) {
511 swing = 0xFF; /* -9 dB */
516 if (reg_swing_5g == auto_temp) {
517 efuse_shadow_read(hw, 1, 0xC7, (u32 *)&swing);
518 swing = (swing == 0xFF) ? 0x00 : swing;
519 } else if (swing_5g == 0) {
520 swing = 0x00; /* 0 dB */
521 } else if (swing_5g == -3) {
522 swing = 0x05; /* -3 dB */
523 } else if (swing_5g == -6) {
524 swing = 0x0A; /* -6 dB */
525 } else if (swing_5g == -9) {
526 swing = 0xFF; /* -9 dB */
532 swing_a = (swing & 0x3) >> 0; /* 0xC6/C7[1:0] */
533 swing_b = (swing & 0xC) >> 2; /* 0xC6/C7[3:2] */
534 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
535 "===> PHY_GetTXBBSwing_8812A, swingA: 0x%X, swingB: 0x%X\n",
539 if (swing_a == 0x0) {
540 if (band == BAND_ON_2_4G)
541 rtldm->swing_diff_2g = 0;
543 rtldm->swing_diff_5g = 0;
544 out = 0x200; /* 0 dB */
545 } else if (swing_a == 0x1) {
546 if (band == BAND_ON_2_4G)
547 rtldm->swing_diff_2g = -3;
549 rtldm->swing_diff_5g = -3;
550 out = 0x16A; /* -3 dB */
551 } else if (swing_a == 0x2) {
552 if (band == BAND_ON_2_4G)
553 rtldm->swing_diff_2g = -6;
555 rtldm->swing_diff_5g = -6;
556 out = 0x101; /* -6 dB */
557 } else if (swing_a == 0x3) {
558 if (band == BAND_ON_2_4G)
559 rtldm->swing_diff_2g = -9;
561 rtldm->swing_diff_5g = -9;
562 out = 0x0B6; /* -9 dB */
565 if (swing_b == 0x0) {
566 if (band == BAND_ON_2_4G)
567 rtldm->swing_diff_2g = 0;
569 rtldm->swing_diff_5g = 0;
570 out = 0x200; /* 0 dB */
571 } else if (swing_b == 0x1) {
572 if (band == BAND_ON_2_4G)
573 rtldm->swing_diff_2g = -3;
575 rtldm->swing_diff_5g = -3;
576 out = 0x16A; /* -3 dB */
577 } else if (swing_b == 0x2) {
578 if (band == BAND_ON_2_4G)
579 rtldm->swing_diff_2g = -6;
581 rtldm->swing_diff_5g = -6;
582 out = 0x101; /* -6 dB */
583 } else if (swing_b == 0x3) {
584 if (band == BAND_ON_2_4G)
585 rtldm->swing_diff_2g = -9;
587 rtldm->swing_diff_5g = -9;
588 out = 0x0B6; /* -9 dB */
592 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
593 "<=== PHY_GetTXBBSwing_8812A, out = 0x%X\n", out);
597 void rtl8821ae_phy_switch_wirelessband(struct ieee80211_hw *hw, u8 band)
599 struct rtl_priv *rtlpriv = rtl_priv(hw);
600 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
601 struct rtl_dm *rtldm = rtl_dm(rtlpriv);
602 u8 current_band = rtlhal->current_bandtype;
604 s8 bb_diff_between_band;
606 txpath = rtl8821ae_phy_query_bb_reg(hw, RTXPATH, 0xf0);
607 rxpath = rtl8821ae_phy_query_bb_reg(hw, RCCK_RX, 0x0f000000);
608 rtlhal->current_bandtype = (enum band_type) band;
609 /* reconfig BB/RF according to wireless mode */
610 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
612 rtl_set_bbreg(hw, ROFDMCCKEN, BOFDMEN|BCCKEN, 0x03);
614 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
615 /* 0xCB0[15:12] = 0x7 (LNA_On)*/
616 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xF000, 0x7);
617 /* 0xCB0[7:4] = 0x7 (PAPE_A)*/
618 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xF0, 0x7);
621 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
623 rtl_set_bbreg(hw, 0x834, 0x3, 0x1);
626 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
627 /* 0xC1C[11:8] = 0 */
628 rtl_set_bbreg(hw, RA_TXSCALE, 0xF00, 0);
630 /* 0x82C[1:0] = 2b'00 */
631 rtl_set_bbreg(hw, 0x82c, 0x3, 0);
634 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
635 _rtl8812ae_phy_set_rfe_reg_24g(hw);
637 rtl_set_bbreg(hw, RTXPATH, 0xf0, 0x1);
638 rtl_set_bbreg(hw, RCCK_RX, 0x0f000000, 0x1);
640 rtl_write_byte(rtlpriv, REG_CCK_CHECK, 0x0);
641 } else {/* 5G band */
644 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
645 /*0xCB0[15:12] = 0x5 (LNA_On)*/
646 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xF000, 0x5);
647 /*0xCB0[7:4] = 0x4 (PAPE_A)*/
648 rtl_set_bbreg(hw, RA_RFE_PINMUX, 0xF0, 0x4);
651 rtl_write_byte(rtlpriv, REG_CCK_CHECK, 0x80);
654 reg_41a = rtl_read_word(rtlpriv, REG_TXPKT_EMPTY);
655 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
656 "Reg41A value %d\n", reg_41a);
658 while ((reg_41a != 0x30) && (count < 50)) {
660 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD, "Delay 50us\n");
662 reg_41a = rtl_read_word(rtlpriv, REG_TXPKT_EMPTY);
665 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
666 "Reg41A value %d\n", reg_41a);
669 rtl_dbg(rtlpriv, COMP_MLME, DBG_LOUD,
670 "PHY_SwitchWirelessBand8812(): Switch to 5G Band. Count = %d reg41A=0x%x\n",
673 /* 2012/02/01, Sinda add registry to switch workaround
674 without long-run verification for scan issue. */
675 rtl_set_bbreg(hw, ROFDMCCKEN, BOFDMEN|BCCKEN, 0x03);
677 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
679 rtl_set_bbreg(hw, 0x834, 0x3, 0x2);
682 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
683 /* AGC table select */
685 rtl_set_bbreg(hw, RA_TXSCALE, 0xF00, 1);
687 /* 0x82C[1:0] = 2'b00 */
688 rtl_set_bbreg(hw, 0x82c, 0x3, 1);
690 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
691 _rtl8812ae_phy_set_rfe_reg_5g(hw);
693 rtl_set_bbreg(hw, RTXPATH, 0xf0, 0);
694 rtl_set_bbreg(hw, RCCK_RX, 0x0f000000, 0xf);
696 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
697 "==>PHY_SwitchWirelessBand8812() BAND_ON_5G settings OFDM index 0x%x\n",
698 rtlpriv->dm.ofdm_index[RF90_PATH_A]);
701 if ((rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) ||
702 (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)) {
704 rtl_set_bbreg(hw, RA_TXSCALE, 0xFFE00000,
705 phy_get_tx_swing_8812A(hw, band, RF90_PATH_A));
707 rtl_set_bbreg(hw, RB_TXSCALE, 0xFFE00000,
708 phy_get_tx_swing_8812A(hw, band, RF90_PATH_B));
710 /* <20121005, Kordan> When TxPowerTrack is ON,
711 * we should take care of the change of BB swing.
712 * That is, reset all info to trigger Tx power tracking.
714 if (band != current_band) {
715 bb_diff_between_band =
716 (rtldm->swing_diff_2g - rtldm->swing_diff_5g);
717 bb_diff_between_band = (band == BAND_ON_2_4G) ?
718 bb_diff_between_band :
719 (-1 * bb_diff_between_band);
720 rtldm->default_ofdm_index += bb_diff_between_band * 2;
722 rtl8821ae_dm_clear_txpower_tracking_state(hw);
725 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
726 "<==%s():Switch Band OK.\n", __func__);
730 static bool _rtl8821ae_check_positive(struct ieee80211_hw *hw,
731 const u32 condition1,
732 const u32 condition2)
734 struct rtl_priv *rtlpriv = rtl_priv(hw);
735 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
736 u32 cut_ver = ((rtlhal->version & CHIP_VER_RTL_MASK)
737 >> CHIP_VER_RTL_SHIFT);
738 u32 intf = (rtlhal->interface == INTF_USB ? BIT(1) : BIT(0));
740 u8 board_type = ((rtlhal->board_type & BIT(4)) >> 4) << 0 | /* _GLNA */
741 ((rtlhal->board_type & BIT(3)) >> 3) << 1 | /* _GPA */
742 ((rtlhal->board_type & BIT(7)) >> 7) << 2 | /* _ALNA */
743 ((rtlhal->board_type & BIT(6)) >> 6) << 3 | /* _APA */
744 ((rtlhal->board_type & BIT(2)) >> 2) << 4; /* _BT */
746 u32 cond1 = condition1, cond2 = condition2;
747 u32 driver1 = cut_ver << 24 | /* CUT ver */
748 0 << 20 | /* interface 2/2 */
749 0x04 << 16 | /* platform */
750 rtlhal->package_type << 12 |
751 intf << 8 | /* interface 1/2 */
754 u32 driver2 = rtlhal->type_glna << 0 |
755 rtlhal->type_gpa << 8 |
756 rtlhal->type_alna << 16 |
757 rtlhal->type_apa << 24;
759 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
760 "===> [8812A] CheckPositive (cond1, cond2) = (0x%X 0x%X)\n",
762 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
763 "===> [8812A] CheckPositive (driver1, driver2) = (0x%X 0x%X)\n",
766 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
767 " (Platform, Interface) = (0x%X, 0x%X)\n", 0x04, intf);
768 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
769 " (Board, Package) = (0x%X, 0x%X)\n",
770 rtlhal->board_type, rtlhal->package_type);
772 /*============== Value Defined Check ===============*/
773 /*QFN Type [15:12] and Cut Version [27:24] need to do value check*/
775 if (((cond1 & 0x0000F000) != 0) && ((cond1 & 0x0000F000) !=
776 (driver1 & 0x0000F000)))
778 if (((cond1 & 0x0F000000) != 0) && ((cond1 & 0x0F000000) !=
779 (driver1 & 0x0F000000)))
782 /*=============== Bit Defined Check ================*/
783 /* We don't care [31:28] */
786 driver1 &= 0x00FF0FFF;
788 if ((cond1 & driver1) == cond1) {
791 if ((cond1 & 0x0F) == 0) /* BoardType is DONTCARE*/
794 if ((cond1 & BIT(0)) != 0) /*GLNA*/
796 if ((cond1 & BIT(1)) != 0) /*GPA*/
798 if ((cond1 & BIT(2)) != 0) /*ALNA*/
800 if ((cond1 & BIT(3)) != 0) /*APA*/
803 /* BoardType of each RF path is matched*/
804 if ((cond2 & mask) == (driver2 & mask))
812 static bool _rtl8821ae_check_condition(struct ieee80211_hw *hw,
815 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
816 u32 _board = rtlefuse->board_type; /*need efuse define*/
817 u32 _interface = 0x01; /* ODM_ITRF_PCIE */
818 u32 _platform = 0x08;/* ODM_WIN */
819 u32 cond = condition;
821 if (condition == 0xCDCDCDCD)
824 cond = condition & 0xFF;
825 if ((_board != cond) && cond != 0xFF)
828 cond = condition & 0xFF00;
830 if ((_interface & cond) == 0 && cond != 0x07)
833 cond = condition & 0xFF0000;
835 if ((_platform & cond) == 0 && cond != 0x0F)
840 static void _rtl8821ae_config_rf_reg(struct ieee80211_hw *hw,
842 enum radio_path rfpath, u32 regaddr)
844 if (addr == 0xfe || addr == 0xffe) {
845 /* In order not to disturb BT music when
846 * wifi init.(1ant NIC only)
850 rtl_set_rfreg(hw, rfpath, regaddr, RFREG_OFFSET_MASK, data);
855 static void _rtl8821ae_config_rf_radio_a(struct ieee80211_hw *hw,
858 u32 content = 0x1000; /*RF Content: radio_a_txt*/
859 u32 maskforphyset = (u32)(content & 0xE000);
861 _rtl8821ae_config_rf_reg(hw, addr, data,
862 RF90_PATH_A, addr | maskforphyset);
865 static void _rtl8821ae_config_rf_radio_b(struct ieee80211_hw *hw,
868 u32 content = 0x1001; /*RF Content: radio_b_txt*/
869 u32 maskforphyset = (u32)(content & 0xE000);
871 _rtl8821ae_config_rf_reg(hw, addr, data,
872 RF90_PATH_B, addr | maskforphyset);
875 static void _rtl8821ae_config_bb_reg(struct ieee80211_hw *hw,
880 else if (addr == 0xfd)
882 else if (addr == 0xfc)
884 else if (addr == 0xfb)
886 else if (addr == 0xfa)
888 else if (addr == 0xf9)
891 rtl_set_bbreg(hw, addr, MASKDWORD, data);
896 static void _rtl8821ae_phy_init_tx_power_by_rate(struct ieee80211_hw *hw)
898 struct rtl_priv *rtlpriv = rtl_priv(hw);
899 struct rtl_phy *rtlphy = &rtlpriv->phy;
900 u8 band, rfpath, txnum, rate_section;
902 for (band = BAND_ON_2_4G; band <= BAND_ON_5G; ++band)
903 for (rfpath = 0; rfpath < TX_PWR_BY_RATE_NUM_RF; ++rfpath)
904 for (txnum = 0; txnum < TX_PWR_BY_RATE_NUM_RF; ++txnum)
905 for (rate_section = 0;
906 rate_section < TX_PWR_BY_RATE_NUM_SECTION;
908 rtlphy->tx_power_by_rate_offset[band]
909 [rfpath][txnum][rate_section] = 0;
912 static void _rtl8821ae_phy_set_txpower_by_rate_base(struct ieee80211_hw *hw,
917 struct rtl_priv *rtlpriv = rtl_priv(hw);
918 struct rtl_phy *rtlphy = &rtlpriv->phy;
920 if (path > RF90_PATH_D) {
921 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
922 "Invalid Rf Path %d in phy_SetTxPowerByRatBase()\n", path);
926 if (band == BAND_ON_2_4G) {
927 switch (rate_section) {
929 rtlphy->txpwr_by_rate_base_24g[path][txnum][0] = value;
932 rtlphy->txpwr_by_rate_base_24g[path][txnum][1] = value;
935 rtlphy->txpwr_by_rate_base_24g[path][txnum][2] = value;
938 rtlphy->txpwr_by_rate_base_24g[path][txnum][3] = value;
940 case VHT_1SSMCS0_1SSMCS9:
941 rtlphy->txpwr_by_rate_base_24g[path][txnum][4] = value;
943 case VHT_2SSMCS0_2SSMCS9:
944 rtlphy->txpwr_by_rate_base_24g[path][txnum][5] = value;
947 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
948 "Invalid RateSection %d in Band 2.4G,Rf Path %d, %dTx in PHY_SetTxPowerByRateBase()\n",
949 rate_section, path, txnum);
952 } else if (band == BAND_ON_5G) {
953 switch (rate_section) {
955 rtlphy->txpwr_by_rate_base_5g[path][txnum][0] = value;
958 rtlphy->txpwr_by_rate_base_5g[path][txnum][1] = value;
961 rtlphy->txpwr_by_rate_base_5g[path][txnum][2] = value;
963 case VHT_1SSMCS0_1SSMCS9:
964 rtlphy->txpwr_by_rate_base_5g[path][txnum][3] = value;
966 case VHT_2SSMCS0_2SSMCS9:
967 rtlphy->txpwr_by_rate_base_5g[path][txnum][4] = value;
970 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
971 "Invalid RateSection %d in Band 5G, Rf Path %d, %dTx in PHY_SetTxPowerByRateBase()\n",
972 rate_section, path, txnum);
976 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
977 "Invalid Band %d in PHY_SetTxPowerByRateBase()\n", band);
981 static u8 _rtl8821ae_phy_get_txpower_by_rate_base(struct ieee80211_hw *hw,
983 u8 txnum, u8 rate_section)
985 struct rtl_priv *rtlpriv = rtl_priv(hw);
986 struct rtl_phy *rtlphy = &rtlpriv->phy;
989 if (path > RF90_PATH_D) {
990 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
991 "Invalid Rf Path %d in PHY_GetTxPowerByRateBase()\n",
996 if (band == BAND_ON_2_4G) {
997 switch (rate_section) {
999 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][0];
1002 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][1];
1005 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][2];
1008 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][3];
1010 case VHT_1SSMCS0_1SSMCS9:
1011 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][4];
1013 case VHT_2SSMCS0_2SSMCS9:
1014 value = rtlphy->txpwr_by_rate_base_24g[path][txnum][5];
1017 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1018 "Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_GetTxPowerByRateBase()\n",
1019 rate_section, path, txnum);
1022 } else if (band == BAND_ON_5G) {
1023 switch (rate_section) {
1025 value = rtlphy->txpwr_by_rate_base_5g[path][txnum][0];
1028 value = rtlphy->txpwr_by_rate_base_5g[path][txnum][1];
1031 value = rtlphy->txpwr_by_rate_base_5g[path][txnum][2];
1033 case VHT_1SSMCS0_1SSMCS9:
1034 value = rtlphy->txpwr_by_rate_base_5g[path][txnum][3];
1036 case VHT_2SSMCS0_2SSMCS9:
1037 value = rtlphy->txpwr_by_rate_base_5g[path][txnum][4];
1040 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1041 "Invalid RateSection %d in Band 5G, Rf Path %d, %dTx in PHY_GetTxPowerByRateBase()\n",
1042 rate_section, path, txnum);
1046 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1047 "Invalid Band %d in PHY_GetTxPowerByRateBase()\n", band);
1053 static void _rtl8821ae_phy_store_txpower_by_rate_base(struct ieee80211_hw *hw)
1055 struct rtl_priv *rtlpriv = rtl_priv(hw);
1056 struct rtl_phy *rtlphy = &rtlpriv->phy;
1058 u8 base = 0, path = 0;
1060 for (path = RF90_PATH_A; path <= RF90_PATH_B; ++path) {
1061 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_1TX][0] >> 24) & 0xFF;
1062 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1063 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, CCK, RF_1TX, base);
1065 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_1TX][2] >> 24) & 0xFF;
1066 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1067 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, OFDM, RF_1TX, base);
1069 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_1TX][4] >> 24) & 0xFF;
1070 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1071 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, HT_MCS0_MCS7, RF_1TX, base);
1073 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_2TX][6] >> 24) & 0xFF;
1074 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1075 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, HT_MCS8_MCS15, RF_2TX, base);
1077 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_1TX][8] >> 24) & 0xFF;
1078 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1079 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, VHT_1SSMCS0_1SSMCS9, RF_1TX, base);
1081 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][path][RF_2TX][11] >> 8) & 0xFF;
1082 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1083 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G, path, VHT_2SSMCS0_2SSMCS9, RF_2TX, base);
1085 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_5G][path][RF_1TX][2] >> 24) & 0xFF;
1086 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1087 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_5G, path, OFDM, RF_1TX, base);
1089 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_5G][path][RF_1TX][4] >> 24) & 0xFF;
1090 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1091 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_5G, path, HT_MCS0_MCS7, RF_1TX, base);
1093 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_5G][path][RF_2TX][6] >> 24) & 0xFF;
1094 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1095 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_5G, path, HT_MCS8_MCS15, RF_2TX, base);
1097 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_5G][path][RF_1TX][8] >> 24) & 0xFF;
1098 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1099 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_5G, path, VHT_1SSMCS0_1SSMCS9, RF_1TX, base);
1101 rawvalue = (u16)(rtlphy->tx_power_by_rate_offset[BAND_ON_5G][path][RF_2TX][11] >> 8) & 0xFF;
1102 base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
1103 _rtl8821ae_phy_set_txpower_by_rate_base(hw, BAND_ON_5G, path, VHT_2SSMCS0_2SSMCS9, RF_2TX, base);
1107 static void _phy_convert_txpower_dbm_to_relative_value(u32 *data, u8 start,
1108 u8 end, u8 base_val)
1114 for (i = 3; i >= 0; --i) {
1115 if (i >= start && i <= end) {
1116 /* Get the exact value */
1117 temp_value = (u8)(*data >> (i * 8)) & 0xF;
1118 temp_value += ((u8)((*data >> (i * 8 + 4)) & 0xF)) * 10;
1120 /* Change the value to a relative value */
1121 temp_value = (temp_value > base_val) ? temp_value -
1122 base_val : base_val - temp_value;
1124 temp_value = (u8)(*data >> (i * 8)) & 0xFF;
1127 temp_data |= temp_value;
1132 static void _rtl8812ae_phy_cross_reference_ht_and_vht_txpower_limit(struct ieee80211_hw *hw)
1134 struct rtl_priv *rtlpriv = rtl_priv(hw);
1135 struct rtl_phy *rtlphy = &rtlpriv->phy;
1136 u8 regulation, bw, channel, rate_section;
1139 for (regulation = 0; regulation < MAX_REGULATION_NUM; ++regulation) {
1140 for (bw = 0; bw < MAX_5G_BANDWIDTH_NUM; ++bw) {
1141 for (channel = 0; channel < CHANNEL_MAX_NUMBER_5G; ++channel) {
1142 for (rate_section = 0; rate_section < MAX_RATE_SECTION_NUM; ++rate_section) {
1143 temp_pwrlmt = rtlphy->txpwr_limit_5g[regulation]
1144 [bw][rate_section][channel][RF90_PATH_A];
1145 if (temp_pwrlmt == MAX_POWER_INDEX) {
1146 if (bw == 0 || bw == 1) { /*5G 20M 40M VHT and HT can cross reference*/
1147 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1148 "No power limit table of the specified band %d, bandwidth %d, ratesection %d, channel %d, rf path %d\n",
1149 1, bw, rate_section, channel, RF90_PATH_A);
1150 if (rate_section == 2) {
1151 rtlphy->txpwr_limit_5g[regulation][bw][2][channel][RF90_PATH_A] =
1152 rtlphy->txpwr_limit_5g[regulation][bw][4][channel][RF90_PATH_A];
1153 } else if (rate_section == 4) {
1154 rtlphy->txpwr_limit_5g[regulation][bw][4][channel][RF90_PATH_A] =
1155 rtlphy->txpwr_limit_5g[regulation][bw][2][channel][RF90_PATH_A];
1156 } else if (rate_section == 3) {
1157 rtlphy->txpwr_limit_5g[regulation][bw][3][channel][RF90_PATH_A] =
1158 rtlphy->txpwr_limit_5g[regulation][bw][5][channel][RF90_PATH_A];
1159 } else if (rate_section == 5) {
1160 rtlphy->txpwr_limit_5g[regulation][bw][5][channel][RF90_PATH_A] =
1161 rtlphy->txpwr_limit_5g[regulation][bw][3][channel][RF90_PATH_A];
1164 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1165 "use other value %d\n",
1175 static u8 _rtl8812ae_phy_get_txpower_by_rate_base_index(struct ieee80211_hw *hw,
1176 enum band_type band, u8 rate)
1178 struct rtl_priv *rtlpriv = rtl_priv(hw);
1180 if (band == BAND_ON_2_4G) {
1223 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1224 "Wrong rate 0x%x to obtain index in 2.4G in PHY_GetTxPowerByRateBaseIndex()\n",
1228 } else if (band == BAND_ON_5G) {
1263 case MGN_VHT1SS_MCS0:
1264 case MGN_VHT1SS_MCS1:
1265 case MGN_VHT1SS_MCS2:
1266 case MGN_VHT1SS_MCS3:
1267 case MGN_VHT1SS_MCS4:
1268 case MGN_VHT1SS_MCS5:
1269 case MGN_VHT1SS_MCS6:
1270 case MGN_VHT1SS_MCS7:
1271 case MGN_VHT1SS_MCS8:
1272 case MGN_VHT1SS_MCS9:
1276 case MGN_VHT2SS_MCS0:
1277 case MGN_VHT2SS_MCS1:
1278 case MGN_VHT2SS_MCS2:
1279 case MGN_VHT2SS_MCS3:
1280 case MGN_VHT2SS_MCS4:
1281 case MGN_VHT2SS_MCS5:
1282 case MGN_VHT2SS_MCS6:
1283 case MGN_VHT2SS_MCS7:
1284 case MGN_VHT2SS_MCS8:
1285 case MGN_VHT2SS_MCS9:
1290 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1291 "Wrong rate 0x%x to obtain index in 5G in PHY_GetTxPowerByRateBaseIndex()\n",
1300 static void _rtl8812ae_phy_convert_txpower_limit_to_power_index(struct ieee80211_hw *hw)
1302 struct rtl_priv *rtlpriv = rtl_priv(hw);
1303 struct rtl_phy *rtlphy = &rtlpriv->phy;
1304 u8 bw40_pwr_base_dbm2_4G, bw40_pwr_base_dbm5G;
1305 u8 regulation, bw, channel, rate_section;
1306 u8 base_index2_4G = 0;
1307 u8 base_index5G = 0;
1308 s8 temp_value = 0, temp_pwrlmt = 0;
1311 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1312 "=====> _rtl8812ae_phy_convert_txpower_limit_to_power_index()\n");
1314 _rtl8812ae_phy_cross_reference_ht_and_vht_txpower_limit(hw);
1316 for (regulation = 0; regulation < MAX_REGULATION_NUM; ++regulation) {
1317 for (bw = 0; bw < MAX_2_4G_BANDWIDTH_NUM; ++bw) {
1318 for (channel = 0; channel < CHANNEL_MAX_NUMBER_2G; ++channel) {
1319 for (rate_section = 0; rate_section < MAX_RATE_SECTION_NUM; ++rate_section) {
1320 /* obtain the base dBm values in 2.4G band
1321 CCK => 11M, OFDM => 54M, HT 1T => MCS7, HT 2T => MCS15*/
1322 if (rate_section == 0) { /*CCK*/
1324 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1325 BAND_ON_2_4G, MGN_11M);
1326 } else if (rate_section == 1) { /*OFDM*/
1328 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1329 BAND_ON_2_4G, MGN_54M);
1330 } else if (rate_section == 2) { /*HT IT*/
1332 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1333 BAND_ON_2_4G, MGN_MCS7);
1334 } else if (rate_section == 3) { /*HT 2T*/
1336 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1337 BAND_ON_2_4G, MGN_MCS15);
1340 temp_pwrlmt = rtlphy->txpwr_limit_2_4g[regulation]
1341 [bw][rate_section][channel][RF90_PATH_A];
1343 for (rf_path = RF90_PATH_A;
1344 rf_path < MAX_RF_PATH_NUM;
1346 if (rate_section == 3)
1347 bw40_pwr_base_dbm2_4G =
1348 rtlphy->txpwr_by_rate_base_24g[rf_path][RF_2TX][base_index2_4G];
1350 bw40_pwr_base_dbm2_4G =
1351 rtlphy->txpwr_by_rate_base_24g[rf_path][RF_1TX][base_index2_4G];
1353 if (temp_pwrlmt != MAX_POWER_INDEX) {
1354 temp_value = temp_pwrlmt - bw40_pwr_base_dbm2_4G;
1355 rtlphy->txpwr_limit_2_4g[regulation]
1356 [bw][rate_section][channel][rf_path] =
1360 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1361 "TxPwrLimit_2_4G[regulation %d][bw %d][rateSection %d][channel %d] = %d\n(TxPwrLimit in dBm %d - BW40PwrLmt2_4G[channel %d][rfpath %d] %d)\n",
1362 regulation, bw, rate_section, channel,
1363 rtlphy->txpwr_limit_2_4g[regulation][bw]
1364 [rate_section][channel][rf_path], (temp_pwrlmt == 63)
1365 ? 0 : temp_pwrlmt/2, channel, rf_path,
1366 bw40_pwr_base_dbm2_4G);
1372 for (regulation = 0; regulation < MAX_REGULATION_NUM; ++regulation) {
1373 for (bw = 0; bw < MAX_5G_BANDWIDTH_NUM; ++bw) {
1374 for (channel = 0; channel < CHANNEL_MAX_NUMBER_5G; ++channel) {
1375 for (rate_section = 0; rate_section < MAX_RATE_SECTION_NUM; ++rate_section) {
1376 /* obtain the base dBm values in 5G band
1377 OFDM => 54M, HT 1T => MCS7, HT 2T => MCS15,
1378 VHT => 1SSMCS7, VHT 2T => 2SSMCS7*/
1379 if (rate_section == 1) { /*OFDM*/
1381 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1382 BAND_ON_5G, MGN_54M);
1383 } else if (rate_section == 2) { /*HT 1T*/
1385 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1386 BAND_ON_5G, MGN_MCS7);
1387 } else if (rate_section == 3) { /*HT 2T*/
1389 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1390 BAND_ON_5G, MGN_MCS15);
1391 } else if (rate_section == 4) { /*VHT 1T*/
1393 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1394 BAND_ON_5G, MGN_VHT1SS_MCS7);
1395 } else if (rate_section == 5) { /*VHT 2T*/
1397 _rtl8812ae_phy_get_txpower_by_rate_base_index(hw,
1398 BAND_ON_5G, MGN_VHT2SS_MCS7);
1401 temp_pwrlmt = rtlphy->txpwr_limit_5g[regulation]
1402 [bw][rate_section][channel]
1405 for (rf_path = RF90_PATH_A;
1406 rf_path < MAX_RF_PATH_NUM;
1408 if (rate_section == 3 || rate_section == 5)
1409 bw40_pwr_base_dbm5G =
1410 rtlphy->txpwr_by_rate_base_5g[rf_path]
1411 [RF_2TX][base_index5G];
1413 bw40_pwr_base_dbm5G =
1414 rtlphy->txpwr_by_rate_base_5g[rf_path]
1415 [RF_1TX][base_index5G];
1417 if (temp_pwrlmt != MAX_POWER_INDEX) {
1419 temp_pwrlmt - bw40_pwr_base_dbm5G;
1420 rtlphy->txpwr_limit_5g[regulation]
1421 [bw][rate_section][channel]
1422 [rf_path] = temp_value;
1425 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1426 "TxPwrLimit_5G[regulation %d][bw %d][rateSection %d][channel %d] =%d\n(TxPwrLimit in dBm %d - BW40PwrLmt5G[chnl group %d][rfpath %d] %d)\n",
1427 regulation, bw, rate_section,
1428 channel, rtlphy->txpwr_limit_5g[regulation]
1429 [bw][rate_section][channel][rf_path],
1430 temp_pwrlmt, channel, rf_path, bw40_pwr_base_dbm5G);
1436 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1437 "<===== %s()\n", __func__);
1440 static void _rtl8821ae_phy_init_txpower_limit(struct ieee80211_hw *hw)
1442 struct rtl_priv *rtlpriv = rtl_priv(hw);
1443 struct rtl_phy *rtlphy = &rtlpriv->phy;
1446 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1447 "=====>`%s()!\n", __func__);
1449 for (i = 0; i < MAX_REGULATION_NUM; ++i) {
1450 for (j = 0; j < MAX_2_4G_BANDWIDTH_NUM; ++j)
1451 for (k = 0; k < MAX_RATE_SECTION_NUM; ++k)
1452 for (m = 0; m < CHANNEL_MAX_NUMBER_2G; ++m)
1453 for (l = 0; l < MAX_RF_PATH_NUM; ++l)
1454 rtlphy->txpwr_limit_2_4g
1458 for (i = 0; i < MAX_REGULATION_NUM; ++i) {
1459 for (j = 0; j < MAX_5G_BANDWIDTH_NUM; ++j)
1460 for (k = 0; k < MAX_RATE_SECTION_NUM; ++k)
1461 for (m = 0; m < CHANNEL_MAX_NUMBER_5G; ++m)
1462 for (l = 0; l < MAX_RF_PATH_NUM; ++l)
1463 rtlphy->txpwr_limit_5g
1468 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1469 "<===== %s()!\n", __func__);
1472 static void _rtl8821ae_phy_convert_txpower_dbm_to_relative_value(struct ieee80211_hw *hw)
1474 struct rtl_priv *rtlpriv = rtl_priv(hw);
1475 struct rtl_phy *rtlphy = &rtlpriv->phy;
1476 u8 base = 0, rfpath = 0;
1478 for (rfpath = RF90_PATH_A; rfpath <= RF90_PATH_B; ++rfpath) {
1479 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_1TX, CCK);
1480 _phy_convert_txpower_dbm_to_relative_value(
1481 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][0],
1484 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_1TX, OFDM);
1485 _phy_convert_txpower_dbm_to_relative_value(
1486 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][1],
1488 _phy_convert_txpower_dbm_to_relative_value(
1489 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][2],
1492 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_1TX, HT_MCS0_MCS7);
1493 _phy_convert_txpower_dbm_to_relative_value(
1494 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][3],
1496 _phy_convert_txpower_dbm_to_relative_value(
1497 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][4],
1500 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_2TX, HT_MCS8_MCS15);
1502 _phy_convert_txpower_dbm_to_relative_value(
1503 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][5],
1506 _phy_convert_txpower_dbm_to_relative_value(
1507 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][6],
1510 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_1TX, VHT_1SSMCS0_1SSMCS9);
1511 _phy_convert_txpower_dbm_to_relative_value(
1512 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][7],
1514 _phy_convert_txpower_dbm_to_relative_value(
1515 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][8],
1517 _phy_convert_txpower_dbm_to_relative_value(
1518 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][9],
1521 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath, RF_2TX, VHT_2SSMCS0_2SSMCS9);
1522 _phy_convert_txpower_dbm_to_relative_value(
1523 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][9],
1525 _phy_convert_txpower_dbm_to_relative_value(
1526 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][10],
1528 _phy_convert_txpower_dbm_to_relative_value(
1529 &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][11],
1532 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_5G, rfpath, RF_1TX, OFDM);
1533 _phy_convert_txpower_dbm_to_relative_value(
1534 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][1],
1536 _phy_convert_txpower_dbm_to_relative_value(
1537 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][2],
1540 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_5G, rfpath, RF_1TX, HT_MCS0_MCS7);
1541 _phy_convert_txpower_dbm_to_relative_value(
1542 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][3],
1544 _phy_convert_txpower_dbm_to_relative_value(
1545 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][4],
1548 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_5G, rfpath, RF_2TX, HT_MCS8_MCS15);
1549 _phy_convert_txpower_dbm_to_relative_value(
1550 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_2TX][5],
1552 _phy_convert_txpower_dbm_to_relative_value(
1553 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_2TX][6],
1556 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_5G, rfpath, RF_1TX, VHT_1SSMCS0_1SSMCS9);
1557 _phy_convert_txpower_dbm_to_relative_value(
1558 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][7],
1560 _phy_convert_txpower_dbm_to_relative_value(
1561 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][8],
1563 _phy_convert_txpower_dbm_to_relative_value(
1564 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][9],
1567 base = _rtl8821ae_phy_get_txpower_by_rate_base(hw, BAND_ON_5G, rfpath, RF_2TX, VHT_2SSMCS0_2SSMCS9);
1568 _phy_convert_txpower_dbm_to_relative_value(
1569 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_1TX][9],
1571 _phy_convert_txpower_dbm_to_relative_value(
1572 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_2TX][10],
1574 _phy_convert_txpower_dbm_to_relative_value(
1575 &rtlphy->tx_power_by_rate_offset[BAND_ON_5G][rfpath][RF_2TX][11],
1579 rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
1580 "<===_rtl8821ae_phy_convert_txpower_dbm_to_relative_value()\n");
1583 static void _rtl8821ae_phy_txpower_by_rate_configuration(struct ieee80211_hw *hw)
1585 _rtl8821ae_phy_store_txpower_by_rate_base(hw);
1586 _rtl8821ae_phy_convert_txpower_dbm_to_relative_value(hw);
1589 /* string is in decimal */
1590 static bool _rtl8812ae_get_integer_from_string(char *str, u8 *pint)
1595 while (str[i] != '\0') {
1596 if (str[i] >= '0' && str[i] <= '9') {
1598 *pint += (str[i] - '0');
1608 static bool _rtl8812ae_eq_n_byte(u8 *str1, u8 *str2, u32 num)
1614 if (str1[num] != str2[num])
1620 static s8 _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(struct ieee80211_hw *hw,
1621 u8 band, u8 channel)
1623 struct rtl_priv *rtlpriv = rtl_priv(hw);
1624 s8 channel_index = -1;
1627 if (band == BAND_ON_2_4G)
1628 channel_index = channel - 1;
1629 else if (band == BAND_ON_5G) {
1630 for (i = 0; i < sizeof(channel5g)/sizeof(u8); ++i) {
1631 if (channel5g[i] == channel)
1635 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid Band %d in %s\n",
1638 if (channel_index == -1)
1639 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
1640 "Invalid Channel %d of Band %d in %s\n", channel,
1643 return channel_index;
1646 static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw, u8 *pregulation,
1647 u8 *pband, u8 *pbandwidth,
1648 u8 *prate_section, u8 *prf_path,
1649 u8 *pchannel, u8 *ppower_limit)
1651 struct rtl_priv *rtlpriv = rtl_priv(hw);
1652 struct rtl_phy *rtlphy = &rtlpriv->phy;
1653 u8 regulation = 0, bandwidth = 0, rate_section = 0, channel;
1655 s8 power_limit = 0, prev_power_limit, ret;
1657 if (!_rtl8812ae_get_integer_from_string((char *)pchannel, &channel) ||
1658 !_rtl8812ae_get_integer_from_string((char *)ppower_limit,
1660 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1661 "Illegal index of pwr_lmt table [chnl %d][val %d]\n",
1662 channel, power_limit);
1665 power_limit = power_limit > MAX_POWER_INDEX ?
1666 MAX_POWER_INDEX : power_limit;
1668 if (_rtl8812ae_eq_n_byte(pregulation, (u8 *)("FCC"), 3))
1670 else if (_rtl8812ae_eq_n_byte(pregulation, (u8 *)("MKK"), 3))
1672 else if (_rtl8812ae_eq_n_byte(pregulation, (u8 *)("ETSI"), 4))
1674 else if (_rtl8812ae_eq_n_byte(pregulation, (u8 *)("WW13"), 4))
1677 if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("CCK"), 3))
1679 else if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("OFDM"), 4))
1681 else if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("HT"), 2) &&
1682 _rtl8812ae_eq_n_byte(prf_path, (u8 *)("1T"), 2))
1684 else if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("HT"), 2) &&
1685 _rtl8812ae_eq_n_byte(prf_path, (u8 *)("2T"), 2))
1687 else if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("VHT"), 3) &&
1688 _rtl8812ae_eq_n_byte(prf_path, (u8 *)("1T"), 2))
1690 else if (_rtl8812ae_eq_n_byte(prate_section, (u8 *)("VHT"), 3) &&
1691 _rtl8812ae_eq_n_byte(prf_path, (u8 *)("2T"), 2))
1694 if (_rtl8812ae_eq_n_byte(pbandwidth, (u8 *)("20M"), 3))
1696 else if (_rtl8812ae_eq_n_byte(pbandwidth, (u8 *)("40M"), 3))
1698 else if (_rtl8812ae_eq_n_byte(pbandwidth, (u8 *)("80M"), 3))
1700 else if (_rtl8812ae_eq_n_byte(pbandwidth, (u8 *)("160M"), 4))
1703 if (_rtl8812ae_eq_n_byte(pband, (u8 *)("2.4G"), 4)) {
1704 ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
1711 channel_index = ret;
1713 prev_power_limit = rtlphy->txpwr_limit_2_4g[regulation]
1714 [bandwidth][rate_section]
1715 [channel_index][RF90_PATH_A];
1717 if (power_limit < prev_power_limit)
1718 rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
1719 [rate_section][channel_index][RF90_PATH_A] =
1722 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1723 "2.4G [regula %d][bw %d][sec %d][chnl %d][val %d]\n",
1724 regulation, bandwidth, rate_section, channel_index,
1725 rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
1726 [rate_section][channel_index][RF90_PATH_A]);
1727 } else if (_rtl8812ae_eq_n_byte(pband, (u8 *)("5G"), 2)) {
1728 ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
1735 channel_index = ret;
1737 prev_power_limit = rtlphy->txpwr_limit_5g[regulation][bandwidth]
1738 [rate_section][channel_index]
1741 if (power_limit < prev_power_limit)
1742 rtlphy->txpwr_limit_5g[regulation][bandwidth]
1743 [rate_section][channel_index][RF90_PATH_A] = power_limit;
1745 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1746 "5G: [regul %d][bw %d][sec %d][chnl %d][val %d]\n",
1747 regulation, bandwidth, rate_section, channel,
1748 rtlphy->txpwr_limit_5g[regulation][bandwidth]
1749 [rate_section][channel_index][RF90_PATH_A]);
1751 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1752 "Cannot recognize the band info in %s\n", pband);
1757 static void _rtl8812ae_phy_config_bb_txpwr_lmt(struct ieee80211_hw *hw,
1758 u8 *regulation, u8 *band,
1759 u8 *bandwidth, u8 *rate_section,
1760 u8 *rf_path, u8 *channel,
1763 _rtl8812ae_phy_set_txpower_limit(hw, regulation, band, bandwidth,
1764 rate_section, rf_path, channel,
1768 static void _rtl8821ae_phy_read_and_config_txpwr_lmt(struct ieee80211_hw *hw)
1770 struct rtl_priv *rtlpriv = rtl_priv(hw);
1771 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1776 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1777 array_len = RTL8812AE_TXPWR_LMT_ARRAY_LEN;
1778 array = RTL8812AE_TXPWR_LMT;
1780 array_len = RTL8821AE_TXPWR_LMT_ARRAY_LEN;
1781 array = RTL8821AE_TXPWR_LMT;
1784 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
1786 for (i = 0; i < array_len; i += 7) {
1787 u8 *regulation = array[i];
1788 u8 *band = array[i+1];
1789 u8 *bandwidth = array[i+2];
1790 u8 *rate = array[i+3];
1791 u8 *rf_path = array[i+4];
1792 u8 *chnl = array[i+5];
1793 u8 *val = array[i+6];
1795 _rtl8812ae_phy_config_bb_txpwr_lmt(hw, regulation, band,
1796 bandwidth, rate, rf_path,
1801 static bool _rtl8821ae_phy_bb8821a_config_parafile(struct ieee80211_hw *hw)
1803 struct rtl_priv *rtlpriv = rtl_priv(hw);
1804 struct rtl_phy *rtlphy = &rtlpriv->phy;
1805 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1808 _rtl8821ae_phy_init_txpower_limit(hw);
1810 /* RegEnableTxPowerLimit == 1 for 8812a & 8821a */
1811 if (rtlefuse->eeprom_regulatory != 2)
1812 _rtl8821ae_phy_read_and_config_txpwr_lmt(hw);
1814 rtstatus = _rtl8821ae_phy_config_bb_with_headerfile(hw,
1815 BASEBAND_CONFIG_PHY_REG);
1817 pr_err("Write BB Reg Fail!!\n");
1820 _rtl8821ae_phy_init_tx_power_by_rate(hw);
1821 if (rtlefuse->autoload_failflag == false) {
1822 rtstatus = _rtl8821ae_phy_config_bb_with_pgheaderfile(hw,
1823 BASEBAND_CONFIG_PHY_REG);
1826 pr_err("BB_PG Reg Fail!!\n");
1830 _rtl8821ae_phy_txpower_by_rate_configuration(hw);
1832 /* RegEnableTxPowerLimit == 1 for 8812a & 8821a */
1833 if (rtlefuse->eeprom_regulatory != 2)
1834 _rtl8812ae_phy_convert_txpower_limit_to_power_index(hw);
1836 rtstatus = _rtl8821ae_phy_config_bb_with_headerfile(hw,
1837 BASEBAND_CONFIG_AGC_TAB);
1840 pr_err("AGC Table Fail\n");
1843 rtlphy->cck_high_power = (bool)(rtl_get_bbreg(hw,
1844 RFPGA0_XA_HSSIPARAMETER2, 0x200));
1849 __rtl8821ae_phy_config_with_headerfile(struct ieee80211_hw *hw,
1850 u32 *array_table, u16 arraylen,
1851 void (*set_reg)(struct ieee80211_hw *hw,
1852 u32 regaddr, u32 data))
1855 #define COND_ENDIF 3
1859 bool matched = true, skipped = false;
1861 while ((i + 1) < arraylen) {
1862 u32 v1 = array_table[i];
1863 u32 v2 = array_table[i + 1];
1865 if (v1 & (BIT(31) | BIT(30))) {/*positive & negative condition*/
1866 if (v1 & BIT(31)) {/* positive condition*/
1867 cond = (u8)((v1 & (BIT(29) | BIT(28))) >> 28);
1868 if (cond == COND_ENDIF) {/*end*/
1871 } else if (cond == COND_ELSE) /*else*/
1872 matched = skipped ? false : true;
1873 else {/*if , else if*/
1877 if (_rtl8821ae_check_positive(
1887 } else if (v1 & BIT(30)) { /*negative condition*/
1892 set_reg(hw, v1, v2);
1900 static bool _rtl8821ae_phy_config_mac_with_headerfile(struct ieee80211_hw *hw)
1902 struct rtl_priv *rtlpriv = rtl_priv(hw);
1903 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1907 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Read MAC_REG_Array\n");
1908 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1909 arraylength = RTL8821AE_MAC_1T_ARRAYLEN;
1910 ptrarray = RTL8821AE_MAC_REG_ARRAY;
1912 arraylength = RTL8812AE_MAC_1T_ARRAYLEN;
1913 ptrarray = RTL8812AE_MAC_REG_ARRAY;
1915 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1916 "Img: MAC_REG_ARRAY LEN %d\n", arraylength);
1918 return __rtl8821ae_phy_config_with_headerfile(hw,
1919 ptrarray, arraylength, rtl_write_byte_with_val32);
1922 static bool _rtl8821ae_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
1925 struct rtl_priv *rtlpriv = rtl_priv(hw);
1926 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1930 if (configtype == BASEBAND_CONFIG_PHY_REG) {
1931 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1932 arraylen = RTL8812AE_PHY_REG_1TARRAYLEN;
1933 array_table = RTL8812AE_PHY_REG_ARRAY;
1935 arraylen = RTL8821AE_PHY_REG_1TARRAYLEN;
1936 array_table = RTL8821AE_PHY_REG_ARRAY;
1939 return __rtl8821ae_phy_config_with_headerfile(hw,
1940 array_table, arraylen,
1941 _rtl8821ae_config_bb_reg);
1942 } else if (configtype == BASEBAND_CONFIG_AGC_TAB) {
1943 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1944 arraylen = RTL8812AE_AGC_TAB_1TARRAYLEN;
1945 array_table = RTL8812AE_AGC_TAB_ARRAY;
1947 arraylen = RTL8821AE_AGC_TAB_1TARRAYLEN;
1948 array_table = RTL8821AE_AGC_TAB_ARRAY;
1951 return __rtl8821ae_phy_config_with_headerfile(hw,
1952 array_table, arraylen,
1953 rtl_set_bbreg_with_dwmask);
1958 static u8 _rtl8821ae_get_rate_section_index(u32 regaddr)
1962 if (regaddr >= 0xC20 && regaddr <= 0xC4C)
1963 index = (u8)((regaddr - 0xC20) / 4);
1964 else if (regaddr >= 0xE20 && regaddr <= 0xE4C)
1965 index = (u8)((regaddr - 0xE20) / 4);
1968 "rtl8821ae: Invalid RegAddr 0x%x\n", regaddr);
1972 static void _rtl8821ae_store_tx_power_by_rate(struct ieee80211_hw *hw,
1973 u32 band, u32 rfpath,
1974 u32 txnum, u32 regaddr,
1975 u32 bitmask, u32 data)
1977 struct rtl_priv *rtlpriv = rtl_priv(hw);
1978 struct rtl_phy *rtlphy = &rtlpriv->phy;
1979 u8 rate_section = _rtl8821ae_get_rate_section_index(regaddr);
1981 if (band != BAND_ON_2_4G && band != BAND_ON_5G) {
1982 rtl_dbg(rtlpriv, COMP_INIT, DBG_WARNING, "Invalid Band %d\n", band);
1983 band = BAND_ON_2_4G;
1985 if (rfpath >= MAX_RF_PATH) {
1986 rtl_dbg(rtlpriv, COMP_INIT, DBG_WARNING, "Invalid RfPath %d\n", rfpath);
1987 rfpath = MAX_RF_PATH - 1;
1989 if (txnum >= MAX_RF_PATH) {
1990 rtl_dbg(rtlpriv, COMP_INIT, DBG_WARNING, "Invalid TxNum %d\n", txnum);
1991 txnum = MAX_RF_PATH - 1;
1993 rtlphy->tx_power_by_rate_offset[band][rfpath][txnum][rate_section] = data;
1994 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
1995 "TxPwrByRateOffset[Band %d][RfPath %d][TxNum %d][RateSection %d] = 0x%x\n",
1996 band, rfpath, txnum, rate_section,
1997 rtlphy->tx_power_by_rate_offset[band][rfpath][txnum][rate_section]);
2000 static bool _rtl8821ae_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
2003 struct rtl_priv *rtlpriv = rtl_priv(hw);
2004 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2008 u32 v1, v2, v3, v4, v5, v6;
2010 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
2011 arraylen = RTL8812AE_PHY_REG_ARRAY_PGLEN;
2012 array = RTL8812AE_PHY_REG_ARRAY_PG;
2014 arraylen = RTL8821AE_PHY_REG_ARRAY_PGLEN;
2015 array = RTL8821AE_PHY_REG_ARRAY_PG;
2018 if (configtype != BASEBAND_CONFIG_PHY_REG) {
2019 rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
2020 "configtype != BaseBand_Config_PHY_REG\n");
2023 for (i = 0; i < arraylen; i += 6) {
2031 if (v1 < 0xCDCDCDCD) {
2032 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE &&
2033 (v4 == 0xfe || v4 == 0xffe)) {
2038 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
2041 else if (v4 == 0xfd)
2043 else if (v4 == 0xfc)
2045 else if (v4 == 0xfb)
2047 else if (v4 == 0xfa)
2049 else if (v4 == 0xf9)
2052 _rtl8821ae_store_tx_power_by_rate(hw, v1, v2, v3,
2056 /*don't need the hw_body*/
2057 if (!_rtl8821ae_check_condition(hw, v1)) {
2058 i += 2; /* skip the pair of expression*/
2062 while (v2 != 0xDEAD) {
2075 bool rtl8812ae_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
2076 enum radio_path rfpath)
2078 u32 *radioa_array_table_a, *radioa_array_table_b;
2079 u16 radioa_arraylen_a, radioa_arraylen_b;
2080 struct rtl_priv *rtlpriv = rtl_priv(hw);
2082 radioa_arraylen_a = RTL8812AE_RADIOA_1TARRAYLEN;
2083 radioa_array_table_a = RTL8812AE_RADIOA_ARRAY;
2084 radioa_arraylen_b = RTL8812AE_RADIOB_1TARRAYLEN;
2085 radioa_array_table_b = RTL8812AE_RADIOB_ARRAY;
2086 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2087 "Radio_A:RTL8821AE_RADIOA_ARRAY %d\n", radioa_arraylen_a);
2088 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Radio No %x\n", rfpath);
2091 return __rtl8821ae_phy_config_with_headerfile(hw,
2092 radioa_array_table_a, radioa_arraylen_a,
2093 _rtl8821ae_config_rf_radio_a);
2096 return __rtl8821ae_phy_config_with_headerfile(hw,
2097 radioa_array_table_b, radioa_arraylen_b,
2098 _rtl8821ae_config_rf_radio_b);
2102 pr_err("switch case %#x not processed\n", rfpath);
2108 bool rtl8821ae_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
2109 enum radio_path rfpath)
2111 u32 *radioa_array_table;
2112 u16 radioa_arraylen;
2113 struct rtl_priv *rtlpriv = rtl_priv(hw);
2115 radioa_arraylen = RTL8821AE_RADIOA_1TARRAYLEN;
2116 radioa_array_table = RTL8821AE_RADIOA_ARRAY;
2117 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2118 "Radio_A:RTL8821AE_RADIOA_ARRAY %d\n", radioa_arraylen);
2119 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Radio No %x\n", rfpath);
2122 return __rtl8821ae_phy_config_with_headerfile(hw,
2123 radioa_array_table, radioa_arraylen,
2124 _rtl8821ae_config_rf_radio_a);
2130 pr_err("switch case %#x not processed\n", rfpath);
2136 void rtl8821ae_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
2138 struct rtl_priv *rtlpriv = rtl_priv(hw);
2139 struct rtl_phy *rtlphy = &rtlpriv->phy;
2141 rtlphy->default_initialgain[0] =
2142 (u8)rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
2143 rtlphy->default_initialgain[1] =
2144 (u8)rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
2145 rtlphy->default_initialgain[2] =
2146 (u8)rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
2147 rtlphy->default_initialgain[3] =
2148 (u8)rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
2150 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
2151 "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
2152 rtlphy->default_initialgain[0],
2153 rtlphy->default_initialgain[1],
2154 rtlphy->default_initialgain[2],
2155 rtlphy->default_initialgain[3]);
2157 rtlphy->framesync = (u8)rtl_get_bbreg(hw,
2158 ROFDM0_RXDETECTOR3, MASKBYTE0);
2159 rtlphy->framesync_c34 = rtl_get_bbreg(hw,
2160 ROFDM0_RXDETECTOR2, MASKDWORD);
2162 rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
2163 "Default framesync (0x%x) = 0x%x\n",
2164 ROFDM0_RXDETECTOR3, rtlphy->framesync);
2167 static void phy_init_bb_rf_register_definition(struct ieee80211_hw *hw)
2169 struct rtl_priv *rtlpriv = rtl_priv(hw);
2170 struct rtl_phy *rtlphy = &rtlpriv->phy;
2172 rtlphy->phyreg_def[RF90_PATH_A].rfintfs = RFPGA0_XAB_RFINTERFACESW;
2173 rtlphy->phyreg_def[RF90_PATH_B].rfintfs = RFPGA0_XAB_RFINTERFACESW;
2175 rtlphy->phyreg_def[RF90_PATH_A].rfintfo = RFPGA0_XA_RFINTERFACEOE;
2176 rtlphy->phyreg_def[RF90_PATH_B].rfintfo = RFPGA0_XB_RFINTERFACEOE;
2178 rtlphy->phyreg_def[RF90_PATH_A].rfintfe = RFPGA0_XA_RFINTERFACEOE;
2179 rtlphy->phyreg_def[RF90_PATH_B].rfintfe = RFPGA0_XB_RFINTERFACEOE;
2181 rtlphy->phyreg_def[RF90_PATH_A].rf3wire_offset = RA_LSSIWRITE_8821A;
2182 rtlphy->phyreg_def[RF90_PATH_B].rf3wire_offset = RB_LSSIWRITE_8821A;
2184 rtlphy->phyreg_def[RF90_PATH_A].rfhssi_para2 = RHSSIREAD_8821AE;
2185 rtlphy->phyreg_def[RF90_PATH_B].rfhssi_para2 = RHSSIREAD_8821AE;
2187 rtlphy->phyreg_def[RF90_PATH_A].rf_rb = RA_SIREAD_8821A;
2188 rtlphy->phyreg_def[RF90_PATH_B].rf_rb = RB_SIREAD_8821A;
2190 rtlphy->phyreg_def[RF90_PATH_A].rf_rbpi = RA_PIREAD_8821A;
2191 rtlphy->phyreg_def[RF90_PATH_B].rf_rbpi = RB_PIREAD_8821A;
2194 void rtl8821ae_phy_get_txpower_level(struct ieee80211_hw *hw, long *powerlevel)
2196 struct rtl_priv *rtlpriv = rtl_priv(hw);
2197 struct rtl_phy *rtlphy = &rtlpriv->phy;
2201 txpwr_level = rtlphy->cur_cck_txpwridx;
2202 txpwr_dbm = _rtl8821ae_phy_txpwr_idx_to_dbm(hw,
2203 WIRELESS_MODE_B, txpwr_level);
2204 txpwr_level = rtlphy->cur_ofdm24g_txpwridx;
2205 if (_rtl8821ae_phy_txpwr_idx_to_dbm(hw,
2207 txpwr_level) > txpwr_dbm)
2209 _rtl8821ae_phy_txpwr_idx_to_dbm(hw, WIRELESS_MODE_G,
2211 txpwr_level = rtlphy->cur_ofdm24g_txpwridx;
2212 if (_rtl8821ae_phy_txpwr_idx_to_dbm(hw,
2213 WIRELESS_MODE_N_24G,
2214 txpwr_level) > txpwr_dbm)
2216 _rtl8821ae_phy_txpwr_idx_to_dbm(hw, WIRELESS_MODE_N_24G,
2218 *powerlevel = txpwr_dbm;
2221 static bool _rtl8821ae_phy_get_chnl_index(u8 channel, u8 *chnl_index)
2226 if (channel <= 14) {
2228 *chnl_index = channel - 1;
2232 for (i = 0; i < CHANNEL_MAX_NUMBER_5G; ++i) {
2233 if (channel5g[i] == channel) {
2242 static s8 _rtl8821ae_phy_get_ratesection_intxpower_byrate(u8 path, u8 rate)
2244 s8 rate_section = 0;
2278 case DESC_RATEMCS10:
2279 case DESC_RATEMCS11:
2282 case DESC_RATEMCS12:
2283 case DESC_RATEMCS13:
2284 case DESC_RATEMCS14:
2285 case DESC_RATEMCS15:
2288 case DESC_RATEVHT1SS_MCS0:
2289 case DESC_RATEVHT1SS_MCS1:
2290 case DESC_RATEVHT1SS_MCS2:
2291 case DESC_RATEVHT1SS_MCS3:
2294 case DESC_RATEVHT1SS_MCS4:
2295 case DESC_RATEVHT1SS_MCS5:
2296 case DESC_RATEVHT1SS_MCS6:
2297 case DESC_RATEVHT1SS_MCS7:
2300 case DESC_RATEVHT1SS_MCS8:
2301 case DESC_RATEVHT1SS_MCS9:
2302 case DESC_RATEVHT2SS_MCS0:
2303 case DESC_RATEVHT2SS_MCS1:
2306 case DESC_RATEVHT2SS_MCS2:
2307 case DESC_RATEVHT2SS_MCS3:
2308 case DESC_RATEVHT2SS_MCS4:
2309 case DESC_RATEVHT2SS_MCS5:
2312 case DESC_RATEVHT2SS_MCS6:
2313 case DESC_RATEVHT2SS_MCS7:
2314 case DESC_RATEVHT2SS_MCS8:
2315 case DESC_RATEVHT2SS_MCS9:
2319 WARN_ONCE(true, "rtl8821ae: Rate_Section is Illegal\n");
2323 return rate_section;
2326 static s8 _rtl8812ae_phy_get_world_wide_limit(s8 *limit_table)
2328 s8 min = limit_table[0];
2331 for (i = 0; i < MAX_REGULATION_NUM; ++i) {
2332 if (limit_table[i] < min)
2333 min = limit_table[i];
2338 static s8 _rtl8812ae_phy_get_txpower_limit(struct ieee80211_hw *hw,
2340 enum ht_channel_width bandwidth,
2341 enum radio_path rf_path,
2342 u8 rate, u8 channel)
2344 struct rtl_priv *rtlpriv = rtl_priv(hw);
2345 struct rtl_efuse *rtlefuse = rtl_efuse(rtlpriv);
2346 struct rtl_phy *rtlphy = &rtlpriv->phy;
2347 short band_temp = -1, regulation = -1, bandwidth_temp = -1,
2348 rate_section = -1, channel_temp = -1;
2349 u16 regu, bdwidth, sec, chnl;
2350 s8 power_limit = MAX_POWER_INDEX;
2352 if (rtlefuse->eeprom_regulatory == 2)
2353 return MAX_POWER_INDEX;
2355 regulation = TXPWR_LMT_WW;
2357 if (band == BAND_ON_2_4G)
2359 else if (band == BAND_ON_5G)
2362 if (bandwidth == HT_CHANNEL_WIDTH_20)
2364 else if (bandwidth == HT_CHANNEL_WIDTH_20_40)
2366 else if (bandwidth == HT_CHANNEL_WIDTH_80)
2398 case DESC_RATEMCS10:
2399 case DESC_RATEMCS11:
2400 case DESC_RATEMCS12:
2401 case DESC_RATEMCS13:
2402 case DESC_RATEMCS14:
2403 case DESC_RATEMCS15:
2406 case DESC_RATEVHT1SS_MCS0:
2407 case DESC_RATEVHT1SS_MCS1:
2408 case DESC_RATEVHT1SS_MCS2:
2409 case DESC_RATEVHT1SS_MCS3:
2410 case DESC_RATEVHT1SS_MCS4:
2411 case DESC_RATEVHT1SS_MCS5:
2412 case DESC_RATEVHT1SS_MCS6:
2413 case DESC_RATEVHT1SS_MCS7:
2414 case DESC_RATEVHT1SS_MCS8:
2415 case DESC_RATEVHT1SS_MCS9:
2418 case DESC_RATEVHT2SS_MCS0:
2419 case DESC_RATEVHT2SS_MCS1:
2420 case DESC_RATEVHT2SS_MCS2:
2421 case DESC_RATEVHT2SS_MCS3:
2422 case DESC_RATEVHT2SS_MCS4:
2423 case DESC_RATEVHT2SS_MCS5:
2424 case DESC_RATEVHT2SS_MCS6:
2425 case DESC_RATEVHT2SS_MCS7:
2426 case DESC_RATEVHT2SS_MCS8:
2427 case DESC_RATEVHT2SS_MCS9:
2431 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
2432 "Wrong rate 0x%x\n", rate);
2436 if (band_temp == BAND_ON_5G && rate_section == 0)
2437 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
2438 "Wrong rate 0x%x: No CCK in 5G Band\n", rate);
2440 /*workaround for wrong index combination to obtain tx power limit,
2441 OFDM only exists in BW 20M*/
2442 if (rate_section == 1)
2445 /*workaround for wrong index combination to obtain tx power limit,
2446 *HT on 80M will reference to HT on 40M
2448 if ((rate_section == 2 || rate_section == 3) && band == BAND_ON_5G &&
2449 bandwidth_temp == 2)
2452 if (band == BAND_ON_2_4G)
2453 channel_temp = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
2454 BAND_ON_2_4G, channel);
2455 else if (band == BAND_ON_5G)
2456 channel_temp = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
2457 BAND_ON_5G, channel);
2458 else if (band == BAND_ON_BOTH)
2459 ;/* BAND_ON_BOTH don't care temporarily */
2461 if (band_temp == -1 || regulation == -1 || bandwidth_temp == -1 ||
2462 rate_section == -1 || channel_temp == -1) {
2463 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
2464 "Wrong index value to access power limit table [band %d][regulation %d][bandwidth %d][rf_path %d][rate_section %d][chnl %d]\n",
2465 band_temp, regulation, bandwidth_temp, rf_path,
2466 rate_section, channel_temp);
2467 return MAX_POWER_INDEX;
2471 bdwidth = bandwidth_temp;
2473 chnl = channel_temp;
2475 if (band == BAND_ON_2_4G) {
2476 s8 limits[10] = {0};
2479 for (i = 0; i < 4; ++i)
2480 limits[i] = rtlphy->txpwr_limit_2_4g[i][bdwidth]
2481 [sec][chnl][rf_path];
2483 power_limit = (regulation == TXPWR_LMT_WW) ?
2484 _rtl8812ae_phy_get_world_wide_limit(limits) :
2485 rtlphy->txpwr_limit_2_4g[regu][bdwidth]
2486 [sec][chnl][rf_path];
2487 } else if (band == BAND_ON_5G) {
2488 s8 limits[10] = {0};
2491 for (i = 0; i < MAX_REGULATION_NUM; ++i)
2492 limits[i] = rtlphy->txpwr_limit_5g[i][bdwidth]
2493 [sec][chnl][rf_path];
2495 power_limit = (regulation == TXPWR_LMT_WW) ?
2496 _rtl8812ae_phy_get_world_wide_limit(limits) :
2497 rtlphy->txpwr_limit_5g[regu][chnl]
2498 [sec][chnl][rf_path];
2500 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2501 "No power limit table of the specified band\n");
2506 static s8 _rtl8821ae_phy_get_txpower_by_rate(struct ieee80211_hw *hw,
2507 u8 band, u8 path, u8 rate)
2509 struct rtl_priv *rtlpriv = rtl_priv(hw);
2510 struct rtl_phy *rtlphy = &rtlpriv->phy;
2511 u8 shift = 0, rate_section, tx_num;
2515 rate_section = _rtl8821ae_phy_get_ratesection_intxpower_byrate(path, rate);
2516 tx_num = RF_TX_NUM_NONIMPLEMENT;
2518 if (tx_num == RF_TX_NUM_NONIMPLEMENT) {
2519 if ((rate >= DESC_RATEMCS8 && rate <= DESC_RATEMCS15) ||
2520 (rate >= DESC_RATEVHT2SS_MCS2 && rate <= DESC_RATEVHT2SS_MCS9))
2533 case DESC_RATEMCS12:
2534 case DESC_RATEVHT1SS_MCS0:
2535 case DESC_RATEVHT1SS_MCS4:
2536 case DESC_RATEVHT1SS_MCS8:
2537 case DESC_RATEVHT2SS_MCS2:
2538 case DESC_RATEVHT2SS_MCS6:
2547 case DESC_RATEMCS13:
2548 case DESC_RATEVHT1SS_MCS1:
2549 case DESC_RATEVHT1SS_MCS5:
2550 case DESC_RATEVHT1SS_MCS9:
2551 case DESC_RATEVHT2SS_MCS3:
2552 case DESC_RATEVHT2SS_MCS7:
2560 case DESC_RATEMCS10:
2561 case DESC_RATEMCS14:
2562 case DESC_RATEVHT1SS_MCS2:
2563 case DESC_RATEVHT1SS_MCS6:
2564 case DESC_RATEVHT2SS_MCS0:
2565 case DESC_RATEVHT2SS_MCS4:
2566 case DESC_RATEVHT2SS_MCS8:
2574 case DESC_RATEMCS11:
2575 case DESC_RATEMCS15:
2576 case DESC_RATEVHT1SS_MCS3:
2577 case DESC_RATEVHT1SS_MCS7:
2578 case DESC_RATEVHT2SS_MCS1:
2579 case DESC_RATEVHT2SS_MCS5:
2580 case DESC_RATEVHT2SS_MCS9:
2584 WARN_ONCE(true, "rtl8821ae: Rate_Section is Illegal\n");
2588 tx_pwr_diff = (u8)(rtlphy->tx_power_by_rate_offset[band][path]
2589 [tx_num][rate_section] >> shift) & 0xff;
2591 /* RegEnableTxPowerLimit == 1 for 8812a & 8821a */
2592 if (rtlpriv->efuse.eeprom_regulatory != 2) {
2593 limit = _rtl8812ae_phy_get_txpower_limit(hw, band,
2594 rtlphy->current_chan_bw, path, rate,
2595 rtlphy->current_channel);
2597 if (rate == DESC_RATEVHT1SS_MCS8 || rate == DESC_RATEVHT1SS_MCS9 ||
2598 rate == DESC_RATEVHT2SS_MCS8 || rate == DESC_RATEVHT2SS_MCS9) {
2600 if (tx_pwr_diff < (-limit))
2601 tx_pwr_diff = -limit;
2605 tx_pwr_diff = limit;
2607 tx_pwr_diff = tx_pwr_diff > limit ? limit : tx_pwr_diff;
2609 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
2610 "Maximum power by rate %d, final power by rate %d\n",
2611 limit, tx_pwr_diff);
2617 static u8 _rtl8821ae_get_txpower_index(struct ieee80211_hw *hw, u8 path,
2618 u8 rate, u8 bandwidth, u8 channel)
2620 struct rtl_priv *rtlpriv = rtl_priv(hw);
2621 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2622 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2623 u8 index = (channel - 1);
2625 bool in_24g = false;
2626 s8 powerdiff_byrate = 0;
2628 if (((rtlhal->current_bandtype == BAND_ON_2_4G) &&
2629 (channel > 14 || channel < 1)) ||
2630 ((rtlhal->current_bandtype == BAND_ON_5G) && (channel <= 14))) {
2632 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
2633 "Illegal channel!!\n");
2636 in_24g = _rtl8821ae_phy_get_chnl_index(channel, &index);
2638 if (RTL8821AE_RX_HAL_IS_CCK_RATE(rate))
2639 txpower = rtlefuse->txpwrlevel_cck[path][index];
2640 else if (DESC_RATE6M <= rate)
2641 txpower = rtlefuse->txpwrlevel_ht40_1s[path][index];
2643 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD, "invalid rate\n");
2645 if (DESC_RATE6M <= rate && rate <= DESC_RATE54M &&
2646 !RTL8821AE_RX_HAL_IS_CCK_RATE(rate))
2647 txpower += rtlefuse->txpwr_legacyhtdiff[path][TX_1S];
2649 if (bandwidth == HT_CHANNEL_WIDTH_20) {
2650 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2651 (DESC_RATEVHT1SS_MCS0 <= rate && rate <= DESC_RATEVHT2SS_MCS9))
2652 txpower += rtlefuse->txpwr_ht20diff[path][TX_1S];
2653 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2654 (DESC_RATEVHT2SS_MCS0 <= rate && rate <= DESC_RATEVHT2SS_MCS9))
2655 txpower += rtlefuse->txpwr_ht20diff[path][TX_2S];
2656 } else if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
2657 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2658 (DESC_RATEVHT1SS_MCS0 <= rate && rate <= DESC_RATEVHT2SS_MCS9))
2659 txpower += rtlefuse->txpwr_ht40diff[path][TX_1S];
2660 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2661 (DESC_RATEVHT2SS_MCS0 <= rate && rate <= DESC_RATEVHT2SS_MCS9))
2662 txpower += rtlefuse->txpwr_ht40diff[path][TX_2S];
2663 } else if (bandwidth == HT_CHANNEL_WIDTH_80) {
2664 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2665 (DESC_RATEVHT1SS_MCS0 <= rate &&
2666 rate <= DESC_RATEVHT2SS_MCS9))
2667 txpower += rtlefuse->txpwr_ht40diff[path][TX_1S];
2668 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2669 (DESC_RATEVHT2SS_MCS0 <= rate &&
2670 rate <= DESC_RATEVHT2SS_MCS9))
2671 txpower += rtlefuse->txpwr_ht40diff[path][TX_2S];
2674 if (DESC_RATE6M <= rate)
2675 txpower = rtlefuse->txpwr_5g_bw40base[path][index];
2677 rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_WARNING,
2680 if (DESC_RATE6M <= rate && rate <= DESC_RATE54M &&
2681 !RTL8821AE_RX_HAL_IS_CCK_RATE(rate))
2682 txpower += rtlefuse->txpwr_5g_ofdmdiff[path][TX_1S];
2684 if (bandwidth == HT_CHANNEL_WIDTH_20) {
2685 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2686 (DESC_RATEVHT1SS_MCS0 <= rate &&
2687 rate <= DESC_RATEVHT2SS_MCS9))
2688 txpower += rtlefuse->txpwr_5g_bw20diff[path][TX_1S];
2689 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2690 (DESC_RATEVHT2SS_MCS0 <= rate &&
2691 rate <= DESC_RATEVHT2SS_MCS9))
2692 txpower += rtlefuse->txpwr_5g_bw20diff[path][TX_2S];
2693 } else if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
2694 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2695 (DESC_RATEVHT1SS_MCS0 <= rate &&
2696 rate <= DESC_RATEVHT2SS_MCS9))
2697 txpower += rtlefuse->txpwr_5g_bw40diff[path][TX_1S];
2698 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2699 (DESC_RATEVHT2SS_MCS0 <= rate &&
2700 rate <= DESC_RATEVHT2SS_MCS9))
2701 txpower += rtlefuse->txpwr_5g_bw40diff[path][TX_2S];
2702 } else if (bandwidth == HT_CHANNEL_WIDTH_80) {
2705 for (i = 0; i < sizeof(channel5g_80m) / sizeof(u8); ++i)
2706 if (channel5g_80m[i] == channel)
2709 if ((DESC_RATEMCS0 <= rate && rate <= DESC_RATEMCS15) ||
2710 (DESC_RATEVHT1SS_MCS0 <= rate &&
2711 rate <= DESC_RATEVHT2SS_MCS9))
2712 txpower = rtlefuse->txpwr_5g_bw80base[path][index]
2713 + rtlefuse->txpwr_5g_bw80diff[path][TX_1S];
2714 if ((DESC_RATEMCS8 <= rate && rate <= DESC_RATEMCS15) ||
2715 (DESC_RATEVHT2SS_MCS0 <= rate &&
2716 rate <= DESC_RATEVHT2SS_MCS9))
2717 txpower = rtlefuse->txpwr_5g_bw80base[path][index]
2718 + rtlefuse->txpwr_5g_bw80diff[path][TX_1S]
2719 + rtlefuse->txpwr_5g_bw80diff[path][TX_2S];
2722 if (rtlefuse->eeprom_regulatory != 2)
2724 _rtl8821ae_phy_get_txpower_by_rate(hw, (u8)(!in_24g),
2727 if (rate == DESC_RATEVHT1SS_MCS8 || rate == DESC_RATEVHT1SS_MCS9 ||
2728 rate == DESC_RATEVHT2SS_MCS8 || rate == DESC_RATEVHT2SS_MCS9)
2729 txpower -= powerdiff_byrate;
2731 txpower += powerdiff_byrate;
2733 if (rate > DESC_RATE11M)
2734 txpower += rtlpriv->dm.remnant_ofdm_swing_idx[path];
2736 txpower += rtlpriv->dm.remnant_cck_idx;
2738 if (txpower > MAX_POWER_INDEX)
2739 txpower = MAX_POWER_INDEX;
2744 static void _rtl8821ae_phy_set_txpower_index(struct ieee80211_hw *hw,
2745 u8 power_index, u8 path, u8 rate)
2747 struct rtl_priv *rtlpriv = rtl_priv(hw);
2749 if (path == RF90_PATH_A) {
2752 rtl_set_bbreg(hw, RTXAGC_A_CCK11_CCK1,
2753 MASKBYTE0, power_index);
2756 rtl_set_bbreg(hw, RTXAGC_A_CCK11_CCK1,
2757 MASKBYTE1, power_index);
2760 rtl_set_bbreg(hw, RTXAGC_A_CCK11_CCK1,
2761 MASKBYTE2, power_index);
2764 rtl_set_bbreg(hw, RTXAGC_A_CCK11_CCK1,
2765 MASKBYTE3, power_index);
2768 rtl_set_bbreg(hw, RTXAGC_A_OFDM18_OFDM6,
2769 MASKBYTE0, power_index);
2772 rtl_set_bbreg(hw, RTXAGC_A_OFDM18_OFDM6,
2773 MASKBYTE1, power_index);
2776 rtl_set_bbreg(hw, RTXAGC_A_OFDM18_OFDM6,
2777 MASKBYTE2, power_index);
2780 rtl_set_bbreg(hw, RTXAGC_A_OFDM18_OFDM6,
2781 MASKBYTE3, power_index);
2784 rtl_set_bbreg(hw, RTXAGC_A_OFDM54_OFDM24,
2785 MASKBYTE0, power_index);
2788 rtl_set_bbreg(hw, RTXAGC_A_OFDM54_OFDM24,
2789 MASKBYTE1, power_index);
2792 rtl_set_bbreg(hw, RTXAGC_A_OFDM54_OFDM24,
2793 MASKBYTE2, power_index);
2796 rtl_set_bbreg(hw, RTXAGC_A_OFDM54_OFDM24,
2797 MASKBYTE3, power_index);
2800 rtl_set_bbreg(hw, RTXAGC_A_MCS03_MCS00,
2801 MASKBYTE0, power_index);
2804 rtl_set_bbreg(hw, RTXAGC_A_MCS03_MCS00,
2805 MASKBYTE1, power_index);
2808 rtl_set_bbreg(hw, RTXAGC_A_MCS03_MCS00,
2809 MASKBYTE2, power_index);
2812 rtl_set_bbreg(hw, RTXAGC_A_MCS03_MCS00,
2813 MASKBYTE3, power_index);
2816 rtl_set_bbreg(hw, RTXAGC_A_MCS07_MCS04,
2817 MASKBYTE0, power_index);
2820 rtl_set_bbreg(hw, RTXAGC_A_MCS07_MCS04,
2821 MASKBYTE1, power_index);
2824 rtl_set_bbreg(hw, RTXAGC_A_MCS07_MCS04,
2825 MASKBYTE2, power_index);
2828 rtl_set_bbreg(hw, RTXAGC_A_MCS07_MCS04,
2829 MASKBYTE3, power_index);
2832 rtl_set_bbreg(hw, RTXAGC_A_MCS11_MCS08,
2833 MASKBYTE0, power_index);
2836 rtl_set_bbreg(hw, RTXAGC_A_MCS11_MCS08,
2837 MASKBYTE1, power_index);
2839 case DESC_RATEMCS10:
2840 rtl_set_bbreg(hw, RTXAGC_A_MCS11_MCS08,
2841 MASKBYTE2, power_index);
2843 case DESC_RATEMCS11:
2844 rtl_set_bbreg(hw, RTXAGC_A_MCS11_MCS08,
2845 MASKBYTE3, power_index);
2847 case DESC_RATEMCS12:
2848 rtl_set_bbreg(hw, RTXAGC_A_MCS15_MCS12,
2849 MASKBYTE0, power_index);
2851 case DESC_RATEMCS13:
2852 rtl_set_bbreg(hw, RTXAGC_A_MCS15_MCS12,
2853 MASKBYTE1, power_index);
2855 case DESC_RATEMCS14:
2856 rtl_set_bbreg(hw, RTXAGC_A_MCS15_MCS12,
2857 MASKBYTE2, power_index);
2859 case DESC_RATEMCS15:
2860 rtl_set_bbreg(hw, RTXAGC_A_MCS15_MCS12,
2861 MASKBYTE3, power_index);
2863 case DESC_RATEVHT1SS_MCS0:
2864 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX3_NSS1INDEX0,
2865 MASKBYTE0, power_index);
2867 case DESC_RATEVHT1SS_MCS1:
2868 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX3_NSS1INDEX0,
2869 MASKBYTE1, power_index);
2871 case DESC_RATEVHT1SS_MCS2:
2872 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX3_NSS1INDEX0,
2873 MASKBYTE2, power_index);
2875 case DESC_RATEVHT1SS_MCS3:
2876 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX3_NSS1INDEX0,
2877 MASKBYTE3, power_index);
2879 case DESC_RATEVHT1SS_MCS4:
2880 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX7_NSS1INDEX4,
2881 MASKBYTE0, power_index);
2883 case DESC_RATEVHT1SS_MCS5:
2884 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX7_NSS1INDEX4,
2885 MASKBYTE1, power_index);
2887 case DESC_RATEVHT1SS_MCS6:
2888 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX7_NSS1INDEX4,
2889 MASKBYTE2, power_index);
2891 case DESC_RATEVHT1SS_MCS7:
2892 rtl_set_bbreg(hw, RTXAGC_A_NSS1INDEX7_NSS1INDEX4,
2893 MASKBYTE3, power_index);
2895 case DESC_RATEVHT1SS_MCS8:
2896 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX1_NSS1INDEX8,
2897 MASKBYTE0, power_index);
2899 case DESC_RATEVHT1SS_MCS9:
2900 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX1_NSS1INDEX8,
2901 MASKBYTE1, power_index);
2903 case DESC_RATEVHT2SS_MCS0:
2904 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX1_NSS1INDEX8,
2905 MASKBYTE2, power_index);
2907 case DESC_RATEVHT2SS_MCS1:
2908 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX1_NSS1INDEX8,
2909 MASKBYTE3, power_index);
2911 case DESC_RATEVHT2SS_MCS2:
2912 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX5_NSS2INDEX2,
2913 MASKBYTE0, power_index);
2915 case DESC_RATEVHT2SS_MCS3:
2916 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX5_NSS2INDEX2,
2917 MASKBYTE1, power_index);
2919 case DESC_RATEVHT2SS_MCS4:
2920 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX5_NSS2INDEX2,
2921 MASKBYTE2, power_index);
2923 case DESC_RATEVHT2SS_MCS5:
2924 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX5_NSS2INDEX2,
2925 MASKBYTE3, power_index);
2927 case DESC_RATEVHT2SS_MCS6:
2928 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX9_NSS2INDEX6,
2929 MASKBYTE0, power_index);
2931 case DESC_RATEVHT2SS_MCS7:
2932 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX9_NSS2INDEX6,
2933 MASKBYTE1, power_index);
2935 case DESC_RATEVHT2SS_MCS8:
2936 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX9_NSS2INDEX6,
2937 MASKBYTE2, power_index);
2939 case DESC_RATEVHT2SS_MCS9:
2940 rtl_set_bbreg(hw, RTXAGC_A_NSS2INDEX9_NSS2INDEX6,
2941 MASKBYTE3, power_index);
2944 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
2945 "Invalid Rate!!\n");
2948 } else if (path == RF90_PATH_B) {
2951 rtl_set_bbreg(hw, RTXAGC_B_CCK11_CCK1,
2952 MASKBYTE0, power_index);
2955 rtl_set_bbreg(hw, RTXAGC_B_CCK11_CCK1,
2956 MASKBYTE1, power_index);
2959 rtl_set_bbreg(hw, RTXAGC_B_CCK11_CCK1,
2960 MASKBYTE2, power_index);
2963 rtl_set_bbreg(hw, RTXAGC_B_CCK11_CCK1,
2964 MASKBYTE3, power_index);
2967 rtl_set_bbreg(hw, RTXAGC_B_OFDM18_OFDM6,
2968 MASKBYTE0, power_index);
2971 rtl_set_bbreg(hw, RTXAGC_B_OFDM18_OFDM6,
2972 MASKBYTE1, power_index);
2975 rtl_set_bbreg(hw, RTXAGC_B_OFDM18_OFDM6,
2976 MASKBYTE2, power_index);
2979 rtl_set_bbreg(hw, RTXAGC_B_OFDM18_OFDM6,
2980 MASKBYTE3, power_index);
2983 rtl_set_bbreg(hw, RTXAGC_B_OFDM54_OFDM24,
2984 MASKBYTE0, power_index);
2987 rtl_set_bbreg(hw, RTXAGC_B_OFDM54_OFDM24,
2988 MASKBYTE1, power_index);
2991 rtl_set_bbreg(hw, RTXAGC_B_OFDM54_OFDM24,
2992 MASKBYTE2, power_index);
2995 rtl_set_bbreg(hw, RTXAGC_B_OFDM54_OFDM24,
2996 MASKBYTE3, power_index);
2999 rtl_set_bbreg(hw, RTXAGC_B_MCS03_MCS00,
3000 MASKBYTE0, power_index);
3003 rtl_set_bbreg(hw, RTXAGC_B_MCS03_MCS00,
3004 MASKBYTE1, power_index);
3007 rtl_set_bbreg(hw, RTXAGC_B_MCS03_MCS00,
3008 MASKBYTE2, power_index);
3011 rtl_set_bbreg(hw, RTXAGC_B_MCS03_MCS00,
3012 MASKBYTE3, power_index);
3015 rtl_set_bbreg(hw, RTXAGC_B_MCS07_MCS04,
3016 MASKBYTE0, power_index);
3019 rtl_set_bbreg(hw, RTXAGC_B_MCS07_MCS04,
3020 MASKBYTE1, power_index);
3023 rtl_set_bbreg(hw, RTXAGC_B_MCS07_MCS04,
3024 MASKBYTE2, power_index);
3027 rtl_set_bbreg(hw, RTXAGC_B_MCS07_MCS04,
3028 MASKBYTE3, power_index);
3031 rtl_set_bbreg(hw, RTXAGC_B_MCS11_MCS08,
3032 MASKBYTE0, power_index);
3035 rtl_set_bbreg(hw, RTXAGC_B_MCS11_MCS08,
3036 MASKBYTE1, power_index);
3038 case DESC_RATEMCS10:
3039 rtl_set_bbreg(hw, RTXAGC_B_MCS11_MCS08,
3040 MASKBYTE2, power_index);
3042 case DESC_RATEMCS11:
3043 rtl_set_bbreg(hw, RTXAGC_B_MCS11_MCS08,
3044 MASKBYTE3, power_index);
3046 case DESC_RATEMCS12:
3047 rtl_set_bbreg(hw, RTXAGC_B_MCS15_MCS12,
3048 MASKBYTE0, power_index);
3050 case DESC_RATEMCS13:
3051 rtl_set_bbreg(hw, RTXAGC_B_MCS15_MCS12,
3052 MASKBYTE1, power_index);
3054 case DESC_RATEMCS14:
3055 rtl_set_bbreg(hw, RTXAGC_B_MCS15_MCS12,
3056 MASKBYTE2, power_index);
3058 case DESC_RATEMCS15:
3059 rtl_set_bbreg(hw, RTXAGC_B_MCS15_MCS12,
3060 MASKBYTE3, power_index);
3062 case DESC_RATEVHT1SS_MCS0:
3063 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX3_NSS1INDEX0,
3064 MASKBYTE0, power_index);
3066 case DESC_RATEVHT1SS_MCS1:
3067 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX3_NSS1INDEX0,
3068 MASKBYTE1, power_index);
3070 case DESC_RATEVHT1SS_MCS2:
3071 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX3_NSS1INDEX0,
3072 MASKBYTE2, power_index);
3074 case DESC_RATEVHT1SS_MCS3:
3075 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX3_NSS1INDEX0,
3076 MASKBYTE3, power_index);
3078 case DESC_RATEVHT1SS_MCS4:
3079 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX7_NSS1INDEX4,
3080 MASKBYTE0, power_index);
3082 case DESC_RATEVHT1SS_MCS5:
3083 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX7_NSS1INDEX4,
3084 MASKBYTE1, power_index);
3086 case DESC_RATEVHT1SS_MCS6:
3087 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX7_NSS1INDEX4,
3088 MASKBYTE2, power_index);
3090 case DESC_RATEVHT1SS_MCS7:
3091 rtl_set_bbreg(hw, RTXAGC_B_NSS1INDEX7_NSS1INDEX4,
3092 MASKBYTE3, power_index);
3094 case DESC_RATEVHT1SS_MCS8:
3095 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX1_NSS1INDEX8,
3096 MASKBYTE0, power_index);
3098 case DESC_RATEVHT1SS_MCS9:
3099 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX1_NSS1INDEX8,
3100 MASKBYTE1, power_index);
3102 case DESC_RATEVHT2SS_MCS0:
3103 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX1_NSS1INDEX8,
3104 MASKBYTE2, power_index);
3106 case DESC_RATEVHT2SS_MCS1:
3107 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX1_NSS1INDEX8,
3108 MASKBYTE3, power_index);
3110 case DESC_RATEVHT2SS_MCS2:
3111 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX5_NSS2INDEX2,
3112 MASKBYTE0, power_index);
3114 case DESC_RATEVHT2SS_MCS3:
3115 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX5_NSS2INDEX2,
3116 MASKBYTE1, power_index);
3118 case DESC_RATEVHT2SS_MCS4:
3119 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX5_NSS2INDEX2,
3120 MASKBYTE2, power_index);
3122 case DESC_RATEVHT2SS_MCS5:
3123 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX5_NSS2INDEX2,
3124 MASKBYTE3, power_index);
3126 case DESC_RATEVHT2SS_MCS6:
3127 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX9_NSS2INDEX6,
3128 MASKBYTE0, power_index);
3130 case DESC_RATEVHT2SS_MCS7:
3131 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX9_NSS2INDEX6,
3132 MASKBYTE1, power_index);
3134 case DESC_RATEVHT2SS_MCS8:
3135 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX9_NSS2INDEX6,
3136 MASKBYTE2, power_index);
3138 case DESC_RATEVHT2SS_MCS9:
3139 rtl_set_bbreg(hw, RTXAGC_B_NSS2INDEX9_NSS2INDEX6,
3140 MASKBYTE3, power_index);
3143 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
3144 "Invalid Rate!!\n");
3148 rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
3149 "Invalid RFPath!!\n");
3153 static void _rtl8821ae_phy_set_txpower_level_by_path(struct ieee80211_hw *hw,
3155 u8 channel, u8 size)
3157 struct rtl_priv *rtlpriv = rtl_priv(hw);
3158 struct rtl_phy *rtlphy = &rtlpriv->phy;
3162 for (i = 0; i < size; i++) {
3164 _rtl8821ae_get_txpower_index(hw, path, array[i],
3165 rtlphy->current_chan_bw,
3167 _rtl8821ae_phy_set_txpower_index(hw, power_index, path,
3172 static void _rtl8821ae_phy_txpower_training_by_path(struct ieee80211_hw *hw,
3173 u8 bw, u8 channel, u8 path)
3175 struct rtl_priv *rtlpriv = rtl_priv(hw);
3176 struct rtl_phy *rtlphy = &rtlpriv->phy;
3179 u32 power_level, data, offset;
3181 if (path >= rtlphy->num_total_rfpath)
3185 if (path == RF90_PATH_A) {
3187 _rtl8821ae_get_txpower_index(hw, RF90_PATH_A,
3188 DESC_RATEMCS7, bw, channel);
3189 offset = RA_TXPWRTRAING;
3192 _rtl8821ae_get_txpower_index(hw, RF90_PATH_B,
3193 DESC_RATEMCS7, bw, channel);
3194 offset = RB_TXPWRTRAING;
3197 for (i = 0; i < 3; i++) {
3199 power_level = power_level - 10;
3201 power_level = power_level - 8;
3203 power_level = power_level - 6;
3205 data |= (((power_level > 2) ? (power_level) : 2) << (i * 8));
3207 rtl_set_bbreg(hw, offset, 0xffffff, data);
3210 void rtl8821ae_phy_set_txpower_level_by_path(struct ieee80211_hw *hw,
3211 u8 channel, u8 path)
3213 /* struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); */
3214 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3215 struct rtl_priv *rtlpriv = rtl_priv(hw);
3216 struct rtl_phy *rtlphy = &rtlpriv->phy;
3217 u8 cck_rates[] = {DESC_RATE1M, DESC_RATE2M, DESC_RATE5_5M,
3219 u8 sizes_of_cck_retes = 4;
3220 u8 ofdm_rates[] = {DESC_RATE6M, DESC_RATE9M, DESC_RATE12M,
3221 DESC_RATE18M, DESC_RATE24M, DESC_RATE36M,
3222 DESC_RATE48M, DESC_RATE54M};
3223 u8 sizes_of_ofdm_retes = 8;
3224 u8 ht_rates_1t[] = {DESC_RATEMCS0, DESC_RATEMCS1, DESC_RATEMCS2,
3225 DESC_RATEMCS3, DESC_RATEMCS4, DESC_RATEMCS5,
3226 DESC_RATEMCS6, DESC_RATEMCS7};
3227 u8 sizes_of_ht_retes_1t = 8;
3228 u8 ht_rates_2t[] = {DESC_RATEMCS8, DESC_RATEMCS9,
3229 DESC_RATEMCS10, DESC_RATEMCS11,
3230 DESC_RATEMCS12, DESC_RATEMCS13,
3231 DESC_RATEMCS14, DESC_RATEMCS15};
3232 u8 sizes_of_ht_retes_2t = 8;
3233 u8 vht_rates_1t[] = {DESC_RATEVHT1SS_MCS0, DESC_RATEVHT1SS_MCS1,
3234 DESC_RATEVHT1SS_MCS2, DESC_RATEVHT1SS_MCS3,
3235 DESC_RATEVHT1SS_MCS4, DESC_RATEVHT1SS_MCS5,
3236 DESC_RATEVHT1SS_MCS6, DESC_RATEVHT1SS_MCS7,
3237 DESC_RATEVHT1SS_MCS8, DESC_RATEVHT1SS_MCS9};
3238 u8 vht_rates_2t[] = {DESC_RATEVHT2SS_MCS0, DESC_RATEVHT2SS_MCS1,
3239 DESC_RATEVHT2SS_MCS2, DESC_RATEVHT2SS_MCS3,
3240 DESC_RATEVHT2SS_MCS4, DESC_RATEVHT2SS_MCS5,
3241 DESC_RATEVHT2SS_MCS6, DESC_RATEVHT2SS_MCS7,
3242 DESC_RATEVHT2SS_MCS8, DESC_RATEVHT2SS_MCS9};
3243 u8 sizes_of_vht_retes = 10;
3245 if (rtlhal->current_bandtype == BAND_ON_2_4G)
3246 _rtl8821ae_phy_set_txpower_level_by_path(hw, cck_rates, path, channel,
3247 sizes_of_cck_retes);
3249 _rtl8821ae_phy_set_txpower_level_by_path(hw, ofdm_rates, path, channel,
3250 sizes_of_ofdm_retes);
3251 _rtl8821ae_phy_set_txpower_level_by_path(hw, ht_rates_1t, path, channel,
3252 sizes_of_ht_retes_1t);
3253 _rtl8821ae_phy_set_txpower_level_by_path(hw, vht_rates_1t, path, channel,
3254 sizes_of_vht_retes);
3256 if (rtlphy->num_total_rfpath >= 2) {
3257 _rtl8821ae_phy_set_txpower_level_by_path(hw, ht_rates_2t, path,
3259 sizes_of_ht_retes_2t);
3260 _rtl8821ae_phy_set_txpower_level_by_path(hw, vht_rates_2t, path,
3262 sizes_of_vht_retes);
3265 _rtl8821ae_phy_txpower_training_by_path(hw, rtlphy->current_chan_bw,
3269 /*just in case, write txpower in DW, to reduce time*/
3270 void rtl8821ae_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
3272 struct rtl_priv *rtlpriv = rtl_priv(hw);
3273 struct rtl_phy *rtlphy = &rtlpriv->phy;
3276 for (path = RF90_PATH_A; path < rtlphy->num_total_rfpath; ++path)
3277 rtl8821ae_phy_set_txpower_level_by_path(hw, channel, path);
3280 static long _rtl8821ae_phy_txpwr_idx_to_dbm(struct ieee80211_hw *hw,
3281 enum wireless_mode wirelessmode,
3287 switch (wirelessmode) {
3288 case WIRELESS_MODE_B:
3291 case WIRELESS_MODE_G:
3292 case WIRELESS_MODE_N_24G:
3299 pwrout_dbm = txpwridx / 2 + offset;
3303 void rtl8821ae_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
3305 struct rtl_priv *rtlpriv = rtl_priv(hw);
3306 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3307 enum io_type iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
3309 if (!is_hal_stop(rtlhal)) {
3310 switch (operation) {
3311 case SCAN_OPT_BACKUP_BAND0:
3312 iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
3313 rtlpriv->cfg->ops->set_hw_reg(hw,
3318 case SCAN_OPT_BACKUP_BAND1:
3319 iotype = IO_CMD_PAUSE_BAND1_DM_BY_SCAN;
3320 rtlpriv->cfg->ops->set_hw_reg(hw,
3325 case SCAN_OPT_RESTORE:
3326 iotype = IO_CMD_RESUME_DM_BY_SCAN;
3327 rtlpriv->cfg->ops->set_hw_reg(hw,
3332 pr_err("Unknown Scan Backup operation.\n");
3338 static void _rtl8821ae_phy_set_reg_bw(struct rtl_priv *rtlpriv, u8 bw)
3340 u16 reg_rf_mode_bw, tmp = 0;
3342 reg_rf_mode_bw = rtl_read_word(rtlpriv, REG_TRXPTCL_CTL);
3344 case HT_CHANNEL_WIDTH_20:
3345 rtl_write_word(rtlpriv, REG_TRXPTCL_CTL, reg_rf_mode_bw & 0xFE7F);
3347 case HT_CHANNEL_WIDTH_20_40:
3348 tmp = reg_rf_mode_bw | BIT(7);
3349 rtl_write_word(rtlpriv, REG_TRXPTCL_CTL, tmp & 0xFEFF);
3351 case HT_CHANNEL_WIDTH_80:
3352 tmp = reg_rf_mode_bw | BIT(8);
3353 rtl_write_word(rtlpriv, REG_TRXPTCL_CTL, tmp & 0xFF7F);
3356 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING, "unknown Bandwidth: 0x%x\n", bw);
3361 static u8 _rtl8821ae_phy_get_secondary_chnl(struct rtl_priv *rtlpriv)
3363 struct rtl_phy *rtlphy = &rtlpriv->phy;
3364 struct rtl_mac *mac = rtl_mac(rtlpriv);
3365 u8 sc_set_40 = 0, sc_set_20 = 0;
3367 if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
3368 if (mac->cur_80_prime_sc == PRIME_CHNL_OFFSET_LOWER)
3369 sc_set_40 = VHT_DATA_SC_40_LOWER_OF_80MHZ;
3370 else if (mac->cur_80_prime_sc == PRIME_CHNL_OFFSET_UPPER)
3371 sc_set_40 = VHT_DATA_SC_40_UPPER_OF_80MHZ;
3373 pr_err("SCMapping: Not Correct Primary40MHz Setting\n");
3375 if ((mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_LOWER) &&
3376 (mac->cur_80_prime_sc == HAL_PRIME_CHNL_OFFSET_LOWER))
3377 sc_set_20 = VHT_DATA_SC_20_LOWEST_OF_80MHZ;
3378 else if ((mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_UPPER) &&
3379 (mac->cur_80_prime_sc == HAL_PRIME_CHNL_OFFSET_LOWER))
3380 sc_set_20 = VHT_DATA_SC_20_LOWER_OF_80MHZ;
3381 else if ((mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_LOWER) &&
3382 (mac->cur_80_prime_sc == HAL_PRIME_CHNL_OFFSET_UPPER))
3383 sc_set_20 = VHT_DATA_SC_20_UPPER_OF_80MHZ;
3384 else if ((mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_UPPER) &&
3385 (mac->cur_80_prime_sc == HAL_PRIME_CHNL_OFFSET_UPPER))
3386 sc_set_20 = VHT_DATA_SC_20_UPPERST_OF_80MHZ;
3388 pr_err("SCMapping: Not Correct Primary40MHz Setting\n");
3389 } else if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
3390 if (mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_UPPER)
3391 sc_set_20 = VHT_DATA_SC_20_UPPER_OF_80MHZ;
3392 else if (mac->cur_40_prime_sc == PRIME_CHNL_OFFSET_LOWER)
3393 sc_set_20 = VHT_DATA_SC_20_LOWER_OF_80MHZ;
3395 pr_err("SCMapping: Not Correct Primary40MHz Setting\n");
3397 return (sc_set_40 << 4) | sc_set_20;
3400 void rtl8821ae_phy_set_bw_mode_callback(struct ieee80211_hw *hw)
3402 struct rtl_priv *rtlpriv = rtl_priv(hw);
3403 struct rtl_phy *rtlphy = &rtlpriv->phy;
3407 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
3408 "Switch to %s bandwidth\n",
3409 (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
3411 (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40 ?
3412 "40MHz" : "80MHz")));
3414 _rtl8821ae_phy_set_reg_bw(rtlpriv, rtlphy->current_chan_bw);
3415 sub_chnl = _rtl8821ae_phy_get_secondary_chnl(rtlpriv);
3416 rtl_write_byte(rtlpriv, 0x0483, sub_chnl);
3418 switch (rtlphy->current_chan_bw) {
3419 case HT_CHANNEL_WIDTH_20:
3420 rtl_set_bbreg(hw, RRFMOD, 0x003003C3, 0x00300200);
3421 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 0);
3423 if (rtlphy->rf_type == RF_2T2R)
3424 rtl_set_bbreg(hw, RL1PEAKTH, 0x03C00000, 7);
3426 rtl_set_bbreg(hw, RL1PEAKTH, 0x03C00000, 8);
3428 case HT_CHANNEL_WIDTH_20_40:
3429 rtl_set_bbreg(hw, RRFMOD, 0x003003C3, 0x00300201);
3430 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 0);
3431 rtl_set_bbreg(hw, RRFMOD, 0x3C, sub_chnl);
3432 rtl_set_bbreg(hw, RCCAONSEC, 0xf0000000, sub_chnl);
3434 if (rtlphy->reg_837 & BIT(2))
3437 if (rtlphy->rf_type == RF_2T2R)
3442 /* 0x848[25:22] = 0x6 */
3443 rtl_set_bbreg(hw, RL1PEAKTH, 0x03C00000, l1pk_val);
3445 if (sub_chnl == VHT_DATA_SC_20_UPPER_OF_80MHZ)
3446 rtl_set_bbreg(hw, RCCK_SYSTEM, BCCK_SYSTEM, 1);
3448 rtl_set_bbreg(hw, RCCK_SYSTEM, BCCK_SYSTEM, 0);
3451 case HT_CHANNEL_WIDTH_80:
3452 /* 0x8ac[21,20,9:6,1,0]=8'b11100010 */
3453 rtl_set_bbreg(hw, RRFMOD, 0x003003C3, 0x00300202);
3455 rtl_set_bbreg(hw, RADC_BUF_CLK, BIT(30), 1);
3456 rtl_set_bbreg(hw, RRFMOD, 0x3C, sub_chnl);
3457 rtl_set_bbreg(hw, RCCAONSEC, 0xf0000000, sub_chnl);
3459 if (rtlphy->reg_837 & BIT(2))
3462 if (rtlphy->rf_type == RF_2T2R)
3467 rtl_set_bbreg(hw, RL1PEAKTH, 0x03C00000, l1pk_val);
3471 pr_err("unknown bandwidth: %#X\n",
3472 rtlphy->current_chan_bw);
3476 rtl8812ae_fixspur(hw, rtlphy->current_chan_bw, rtlphy->current_channel);
3478 rtl8821ae_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
3479 rtlphy->set_bwmode_inprogress = false;
3481 rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD, "\n");
3484 void rtl8821ae_phy_set_bw_mode(struct ieee80211_hw *hw,
3485 enum nl80211_channel_type ch_type)
3487 struct rtl_priv *rtlpriv = rtl_priv(hw);
3488 struct rtl_phy *rtlphy = &rtlpriv->phy;
3489 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3490 u8 tmp_bw = rtlphy->current_chan_bw;
3492 if (rtlphy->set_bwmode_inprogress)
3494 rtlphy->set_bwmode_inprogress = true;
3495 if ((!is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw)))
3496 rtl8821ae_phy_set_bw_mode_callback(hw);
3498 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
3499 "FALSE driver sleep or unload\n");
3500 rtlphy->set_bwmode_inprogress = false;
3501 rtlphy->current_chan_bw = tmp_bw;
3505 void rtl8821ae_phy_sw_chnl_callback(struct ieee80211_hw *hw)
3507 struct rtl_priv *rtlpriv = rtl_priv(hw);
3508 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3509 struct rtl_phy *rtlphy = &rtlpriv->phy;
3510 u8 channel = rtlphy->current_channel;
3514 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
3515 "switch to channel%d\n", rtlphy->current_channel);
3516 if (is_hal_stop(rtlhal))
3519 if (36 <= channel && channel <= 48)
3521 else if (50 <= channel && channel <= 64)
3523 else if (100 <= channel && channel <= 116)
3525 else if (118 <= channel)
3529 rtl_set_bbreg(hw, RFC_AREA, 0x1ffe0000, data);
3531 for (path = RF90_PATH_A; path < rtlphy->num_total_rfpath; path++) {
3532 if (36 <= channel && channel <= 64)
3534 else if (100 <= channel && channel <= 140)
3536 else if (140 < channel)
3540 rtl8821ae_phy_set_rf_reg(hw, path, RF_CHNLBW,
3541 BIT(18)|BIT(17)|BIT(16)|BIT(9)|BIT(8), data);
3543 rtl8821ae_phy_set_rf_reg(hw, path, RF_CHNLBW,
3544 BMASKBYTE0, channel);
3547 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
3548 if (36 <= channel && channel <= 64)
3552 rtl8821ae_phy_set_rf_reg(hw, path, RF_APK,
3553 BRFREGOFFSETMASK, data);
3557 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
3560 u8 rtl8821ae_phy_sw_chnl(struct ieee80211_hw *hw)
3562 struct rtl_priv *rtlpriv = rtl_priv(hw);
3563 struct rtl_phy *rtlphy = &rtlpriv->phy;
3564 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3565 u32 timeout = 1000, timecount = 0;
3566 u8 channel = rtlphy->current_channel;
3568 if (rtlphy->sw_chnl_inprogress)
3570 if (rtlphy->set_bwmode_inprogress)
3573 if ((is_hal_stop(rtlhal)) || (RT_CANNOT_IO(hw))) {
3574 rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
3575 "sw_chnl_inprogress false driver sleep or unload\n");
3578 while (rtlphy->lck_inprogress && timecount < timeout) {
3583 if (rtlphy->current_channel > 14 && rtlhal->current_bandtype != BAND_ON_5G)
3584 rtl8821ae_phy_switch_wirelessband(hw, BAND_ON_5G);
3585 else if (rtlphy->current_channel <= 14 && rtlhal->current_bandtype != BAND_ON_2_4G)
3586 rtl8821ae_phy_switch_wirelessband(hw, BAND_ON_2_4G);
3588 rtlphy->sw_chnl_inprogress = true;
3592 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
3593 "switch to channel%d, band type is %d\n",
3594 rtlphy->current_channel, rtlhal->current_bandtype);
3596 rtl8821ae_phy_sw_chnl_callback(hw);
3598 rtl8821ae_dm_clear_txpower_tracking_state(hw);
3599 rtl8821ae_phy_set_txpower_level(hw, rtlphy->current_channel);
3601 rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
3602 rtlphy->sw_chnl_inprogress = false;
3606 u8 _rtl8812ae_get_right_chnl_place_for_iqk(u8 chnl)
3608 static const u8 channel_all[TARGET_CHNL_NUM_2G_5G_8812] = {
3609 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
3610 14, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54,
3611 56, 58, 60, 62, 64, 100, 102, 104, 106, 108,
3612 110, 112, 114, 116, 118, 120, 122, 124, 126,
3613 128, 130, 132, 134, 136, 138, 140, 149, 151,
3614 153, 155, 157, 159, 161, 163, 165};
3618 for (place = 14; place < sizeof(channel_all); place++)
3619 if (channel_all[place] == chnl)
3626 #define MACBB_REG_NUM 10
3627 #define AFE_REG_NUM 14
3628 #define RF_REG_NUM 3
3630 static void _rtl8821ae_iqk_backup_macbb(struct ieee80211_hw *hw,
3632 u32 *backup_macbb_reg, u32 mac_bb_num)
3634 struct rtl_priv *rtlpriv = rtl_priv(hw);
3637 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /*[31] = 0 --> Page C*/
3638 /*save MACBB default value*/
3639 for (i = 0; i < mac_bb_num; i++)
3640 macbb_backup[i] = rtl_read_dword(rtlpriv, backup_macbb_reg[i]);
3642 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "BackupMacBB Success!!!!\n");
3645 static void _rtl8821ae_iqk_backup_afe(struct ieee80211_hw *hw, u32 *afe_backup,
3646 u32 *backup_afe_REG, u32 afe_num)
3648 struct rtl_priv *rtlpriv = rtl_priv(hw);
3651 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /*[31] = 0 --> Page C*/
3652 /*Save AFE Parameters */
3653 for (i = 0; i < afe_num; i++)
3654 afe_backup[i] = rtl_read_dword(rtlpriv, backup_afe_REG[i]);
3655 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "BackupAFE Success!!!!\n");
3658 static void _rtl8821ae_iqk_backup_rf(struct ieee80211_hw *hw, u32 *rfa_backup,
3659 u32 *rfb_backup, u32 *backup_rf_reg,
3662 struct rtl_priv *rtlpriv = rtl_priv(hw);
3665 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /*[31] = 0 --> Page C*/
3666 /*Save RF Parameters*/
3667 for (i = 0; i < rf_num; i++) {
3668 rfa_backup[i] = rtl_get_rfreg(hw, RF90_PATH_A, backup_rf_reg[i],
3670 rfb_backup[i] = rtl_get_rfreg(hw, RF90_PATH_B, backup_rf_reg[i],
3673 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "BackupRF Success!!!!\n");
3676 static void _rtl8821ae_iqk_configure_mac(
3677 struct ieee80211_hw *hw
3680 struct rtl_priv *rtlpriv = rtl_priv(hw);
3681 /* ========MAC register setting========*/
3682 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /*[31] = 0 --> Page C*/
3683 rtl_write_byte(rtlpriv, 0x522, 0x3f);
3684 rtl_set_bbreg(hw, 0x550, BIT(11) | BIT(3), 0x0);
3685 rtl_write_byte(rtlpriv, 0x808, 0x00); /*RX ante off*/
3686 rtl_set_bbreg(hw, 0x838, 0xf, 0xc); /*CCA off*/
3689 static void _rtl8821ae_iqk_tx_fill_iqc(struct ieee80211_hw *hw,
3690 enum radio_path path, u32 tx_x, u32 tx_y)
3692 struct rtl_priv *rtlpriv = rtl_priv(hw);
3695 /* [31] = 1 --> Page C1 */
3696 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1);
3697 rtl_write_dword(rtlpriv, 0xc90, 0x00000080);
3698 rtl_write_dword(rtlpriv, 0xcc4, 0x20040000);
3699 rtl_write_dword(rtlpriv, 0xcc8, 0x20000000);
3700 rtl_set_bbreg(hw, 0xccc, 0x000007ff, tx_y);
3701 rtl_set_bbreg(hw, 0xcd4, 0x000007ff, tx_x);
3702 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3703 "TX_X = %x;;TX_Y = %x =====> fill to IQC\n",
3705 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3706 "0xcd4 = %x;;0xccc = %x ====>fill to IQC\n",
3707 rtl_get_bbreg(hw, 0xcd4, 0x000007ff),
3708 rtl_get_bbreg(hw, 0xccc, 0x000007ff));
3715 static void _rtl8821ae_iqk_rx_fill_iqc(struct ieee80211_hw *hw,
3716 enum radio_path path, u32 rx_x, u32 rx_y)
3718 struct rtl_priv *rtlpriv = rtl_priv(hw);
3721 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
3722 rtl_set_bbreg(hw, 0xc10, 0x000003ff, rx_x>>1);
3723 rtl_set_bbreg(hw, 0xc10, 0x03ff0000, rx_y>>1);
3724 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3725 "rx_x = %x;;rx_y = %x ====>fill to IQC\n",
3726 rx_x >> 1, rx_y >> 1);
3727 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3728 "0xc10 = %x ====>fill to IQC\n",
3729 rtl_read_dword(rtlpriv, 0xc10));
3738 static void _rtl8821ae_iqk_tx(struct ieee80211_hw *hw, enum radio_path path)
3740 struct rtl_priv *rtlpriv = rtl_priv(hw);
3741 struct rtl_phy *rtlphy = &rtlpriv->phy;
3742 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3744 u32 tx_fail, rx_fail, delay_count, iqk_ready, cal_retry, cal = 0, temp_reg65;
3745 int tx_x = 0, tx_y = 0, rx_x = 0, rx_y = 0, tx_average = 0, rx_average = 0;
3746 int tx_x0[cal_num], tx_y0[cal_num], tx_x0_rxk[cal_num],
3747 tx_y0_rxk[cal_num], rx_x0[cal_num], rx_y0[cal_num],
3748 tx_dt[cal_num], rx_dt[cal_num];
3749 bool tx0iqkok = false, rx0iqkok = false;
3750 bool vdf_enable = false;
3751 int i, k, vdf_y[3], vdf_x[3],
3752 ii, dx = 0, dy = 0, tx_finish = 0, rx_finish = 0;
3754 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3755 "BandWidth = %d.\n",
3756 rtlphy->current_chan_bw);
3757 if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80)
3760 while (cal < cal_num) {
3763 temp_reg65 = rtl_get_rfreg(hw, path, 0x65, 0xffffffff);
3765 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /*[31] = 0 --> Page C*/
3766 /*========Path-A AFE all on========*/
3767 /*Port 0 DAC/ADC on*/
3768 rtl_write_dword(rtlpriv, 0xc60, 0x77777777);
3769 rtl_write_dword(rtlpriv, 0xc64, 0x77777777);
3770 rtl_write_dword(rtlpriv, 0xc68, 0x19791979);
3771 rtl_write_dword(rtlpriv, 0xc6c, 0x19791979);
3772 rtl_write_dword(rtlpriv, 0xc70, 0x19791979);
3773 rtl_write_dword(rtlpriv, 0xc74, 0x19791979);
3774 rtl_write_dword(rtlpriv, 0xc78, 0x19791979);
3775 rtl_write_dword(rtlpriv, 0xc7c, 0x19791979);
3776 rtl_write_dword(rtlpriv, 0xc80, 0x19791979);
3777 rtl_write_dword(rtlpriv, 0xc84, 0x19791979);
3779 rtl_set_bbreg(hw, 0xc00, 0xf, 0x4); /*hardware 3-wire off*/
3782 /* ====== LOK ====== */
3783 /*DAC/ADC sampling rate (160 MHz)*/
3784 rtl_set_bbreg(hw, 0xc5c, BIT(26) | BIT(25) | BIT(24), 0x7);
3786 /* 2. LoK RF Setting (at BW = 20M) */
3787 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80002);
3788 rtl_set_rfreg(hw, path, 0x18, 0x00c00, 0x3); /* BW 20M */
3789 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x20000);
3790 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x0003f);
3791 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xf3fc3);
3792 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, 0x931d5);
3793 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x8a001);
3794 rtl_set_bbreg(hw, 0xcb8, 0xf, 0xd);
3795 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
3796 rtl_write_dword(rtlpriv, 0xb00, 0x03000100);
3797 rtl_set_bbreg(hw, 0xc94, BIT(0), 0x1);
3798 rtl_write_dword(rtlpriv, 0x978, 0x29002000);/* TX (X,Y) */
3799 rtl_write_dword(rtlpriv, 0x97c, 0xa9002000);/* RX (X,Y) */
3800 rtl_write_dword(rtlpriv, 0x984, 0x00462910);/* [0]:AGC_en, [15]:idac_K_Mask */
3802 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
3803 rtl_write_dword(rtlpriv, 0xc88, 0x821403f4);
3805 if (rtlhal->current_bandtype)
3806 rtl_write_dword(rtlpriv, 0xc8c, 0x68163e96);
3808 rtl_write_dword(rtlpriv, 0xc8c, 0x28163e96);
3810 rtl_write_dword(rtlpriv, 0xc80, 0x18008c10);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
3811 rtl_write_dword(rtlpriv, 0xc84, 0x38008c10);/* RX_TONE_idx[9:0], RxK_Mask[29] */
3812 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
3813 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
3814 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
3816 mdelay(10); /* Delay 10ms */
3817 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
3819 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
3820 rtl_set_rfreg(hw, path, 0x58, 0x7fe00, rtl_get_rfreg(hw, path, 0x8, 0xffc00)); /* Load LOK */
3822 switch (rtlphy->current_chan_bw) {
3824 rtl_set_rfreg(hw, path, 0x18, 0x00c00, 0x1);
3827 rtl_set_rfreg(hw, path, 0x18, 0x00c00, 0x0);
3833 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
3835 /* 3. TX RF Setting */
3836 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
3837 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80000);
3838 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x20000);
3839 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x0003f);
3840 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xf3fc3);
3841 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, 0x931d5);
3842 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x8a001);
3843 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x00000);
3844 /* ODM_SetBBReg(pDM_Odm, 0xcb8, 0xf, 0xd); */
3845 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
3846 rtl_write_dword(rtlpriv, 0xb00, 0x03000100);
3847 rtl_set_bbreg(hw, 0xc94, BIT(0), 0x1);
3848 rtl_write_dword(rtlpriv, 0x978, 0x29002000);/* TX (X,Y) */
3849 rtl_write_dword(rtlpriv, 0x97c, 0xa9002000);/* RX (X,Y) */
3850 rtl_write_dword(rtlpriv, 0x984, 0x0046a910);/* [0]:AGC_en, [15]:idac_K_Mask */
3852 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
3853 rtl_write_dword(rtlpriv, 0xc88, 0x821403f1);
3854 if (rtlhal->current_bandtype)
3855 rtl_write_dword(rtlpriv, 0xc8c, 0x40163e96);
3857 rtl_write_dword(rtlpriv, 0xc8c, 0x00163e96);
3859 if (vdf_enable == 1) {
3860 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "VDF_enable\n");
3861 for (k = 0; k <= 2; k++) {
3864 rtl_write_dword(rtlpriv, 0xc80, 0x18008c38);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
3865 rtl_write_dword(rtlpriv, 0xc84, 0x38008c38);/* RX_TONE_idx[9:0], RxK_Mask[29] */
3866 rtl_set_bbreg(hw, 0xce8, BIT(31), 0x0);
3869 rtl_set_bbreg(hw, 0xc80, BIT(28), 0x0);
3870 rtl_set_bbreg(hw, 0xc84, BIT(28), 0x0);
3871 rtl_set_bbreg(hw, 0xce8, BIT(31), 0x0);
3874 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3875 "vdf_y[1] = %x;;;vdf_y[0] = %x\n", vdf_y[1]>>21 & 0x00007ff, vdf_y[0]>>21 & 0x00007ff);
3876 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
3877 "vdf_x[1] = %x;;;vdf_x[0] = %x\n", vdf_x[1]>>21 & 0x00007ff, vdf_x[0]>>21 & 0x00007ff);
3878 tx_dt[cal] = (vdf_y[1]>>20)-(vdf_y[0]>>20);
3879 tx_dt[cal] = ((16*tx_dt[cal])*10000/15708);
3880 tx_dt[cal] = (tx_dt[cal] >> 1)+(tx_dt[cal] & BIT(0));
3881 rtl_write_dword(rtlpriv, 0xc80, 0x18008c20);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
3882 rtl_write_dword(rtlpriv, 0xc84, 0x38008c20);/* RX_TONE_idx[9:0], RxK_Mask[29] */
3883 rtl_set_bbreg(hw, 0xce8, BIT(31), 0x1);
3884 rtl_set_bbreg(hw, 0xce8, 0x3fff0000, tx_dt[cal] & 0x00003fff);
3889 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
3893 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
3894 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
3896 mdelay(10); /* Delay 10ms */
3897 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
3900 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
3901 if ((~iqk_ready) || (delay_count > 20))
3909 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
3910 /* ============TXIQK Check============== */
3911 tx_fail = rtl_get_bbreg(hw, 0xd00, BIT(12));
3914 rtl_write_dword(rtlpriv, 0xcb8, 0x02000000);
3915 vdf_x[k] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
3916 rtl_write_dword(rtlpriv, 0xcb8, 0x04000000);
3917 vdf_y[k] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
3921 rtl_set_bbreg(hw, 0xccc, 0x000007ff, 0x0);
3922 rtl_set_bbreg(hw, 0xcd4, 0x000007ff, 0x200);
3925 if (cal_retry == 10)
3931 if (cal_retry == 10)
3937 tx_x0[cal] = vdf_x[k-1];
3938 tx_y0[cal] = vdf_y[k-1];
3941 rtl_write_dword(rtlpriv, 0xc80, 0x18008c10);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
3942 rtl_write_dword(rtlpriv, 0xc84, 0x38008c10);/* RX_TONE_idx[9:0], RxK_Mask[29] */
3943 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
3947 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
3948 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
3950 mdelay(10); /* Delay 10ms */
3951 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
3954 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
3955 if ((~iqk_ready) || (delay_count > 20))
3963 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
3964 /* ============TXIQK Check============== */
3965 tx_fail = rtl_get_bbreg(hw, 0xd00, BIT(12));
3968 rtl_write_dword(rtlpriv, 0xcb8, 0x02000000);
3969 tx_x0[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
3970 rtl_write_dword(rtlpriv, 0xcb8, 0x04000000);
3971 tx_y0[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
3975 rtl_set_bbreg(hw, 0xccc, 0x000007ff, 0x0);
3976 rtl_set_bbreg(hw, 0xcd4, 0x000007ff, 0x200);
3979 if (cal_retry == 10)
3985 if (cal_retry == 10)
3991 if (tx0iqkok == false)
3992 break; /* TXK fail, Don't do RXK */
3994 if (vdf_enable == 1) {
3995 rtl_set_bbreg(hw, 0xce8, BIT(31), 0x0); /* TX VDF Disable */
3996 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "RXVDF Start\n");
3997 for (k = 0; k <= 2; k++) {
3998 /* ====== RX mode TXK (RXK Step 1) ====== */
3999 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4000 /* 1. TX RF Setting */
4001 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80000);
4002 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x30000);
4003 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x00029);
4004 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xd7ffb);
4005 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, temp_reg65);
4006 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x8a001);
4007 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x00000);
4009 rtl_set_bbreg(hw, 0xcb8, 0xf, 0xd);
4010 rtl_write_dword(rtlpriv, 0x978, 0x29002000);/* TX (X,Y) */
4011 rtl_write_dword(rtlpriv, 0x97c, 0xa9002000);/* RX (X,Y) */
4012 rtl_write_dword(rtlpriv, 0x984, 0x0046a910);/* [0]:AGC_en, [15]:idac_K_Mask */
4013 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
4014 rtl_write_dword(rtlpriv, 0xb00, 0x03000100);
4015 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
4019 rtl_write_dword(rtlpriv, 0xc80, 0x18008c38);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
4020 rtl_write_dword(rtlpriv, 0xc84, 0x38008c38);/* RX_TONE_idx[9:0], RxK_Mask[29] */
4021 rtl_set_bbreg(hw, 0xce8, BIT(30), 0x0);
4026 rtl_write_dword(rtlpriv, 0xc80, 0x08008c38);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
4027 rtl_write_dword(rtlpriv, 0xc84, 0x28008c38);/* RX_TONE_idx[9:0], RxK_Mask[29] */
4028 rtl_set_bbreg(hw, 0xce8, BIT(30), 0x0);
4033 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4034 "VDF_Y[1] = %x;;;VDF_Y[0] = %x\n",
4035 vdf_y[1] >> 21 & 0x00007ff,
4036 vdf_y[0] >> 21 & 0x00007ff);
4037 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4038 "VDF_X[1] = %x;;;VDF_X[0] = %x\n",
4039 vdf_x[1] >> 21 & 0x00007ff,
4040 vdf_x[0] >> 21 & 0x00007ff);
4041 rx_dt[cal] = (vdf_y[1]>>20)-(vdf_y[0]>>20);
4042 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "Rx_dt = %d\n",
4044 rx_dt[cal] = ((16*rx_dt[cal])*10000/13823);
4045 rx_dt[cal] = (rx_dt[cal] >> 1)+(rx_dt[cal] & BIT(0));
4046 rtl_write_dword(rtlpriv, 0xc80, 0x18008c20);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
4047 rtl_write_dword(rtlpriv, 0xc84, 0x38008c20);/* RX_TONE_idx[9:0], RxK_Mask[29] */
4048 rtl_set_bbreg(hw, 0xce8, 0x00003fff, rx_dt[cal] & 0x00003fff);
4054 rtl_write_dword(rtlpriv, 0xc88, 0x821603e0);
4055 rtl_write_dword(rtlpriv, 0xc8c, 0x68163e96);
4056 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
4060 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
4061 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
4063 mdelay(10); /* Delay 10ms */
4064 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
4067 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
4068 if ((~iqk_ready) || (delay_count > 20))
4076 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
4077 /* ============TXIQK Check============== */
4078 tx_fail = rtl_get_bbreg(hw, 0xd00, BIT(12));
4081 rtl_write_dword(rtlpriv, 0xcb8, 0x02000000);
4082 tx_x0_rxk[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4083 rtl_write_dword(rtlpriv, 0xcb8, 0x04000000);
4084 tx_y0_rxk[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4090 if (cal_retry == 10)
4096 if (cal_retry == 10)
4101 if (tx0iqkok == false) { /* If RX mode TXK fail, then take TXK Result */
4102 tx_x0_rxk[cal] = tx_x0[cal];
4103 tx_y0_rxk[cal] = tx_y0[cal];
4108 "RXK Step 1 fail\n");
4111 /* ====== RX IQK ====== */
4112 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4113 /* 1. RX RF Setting */
4114 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80000);
4115 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x30000);
4116 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x0002f);
4117 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xfffbb);
4118 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x88001);
4119 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, 0x931d8);
4120 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x00000);
4122 rtl_set_bbreg(hw, 0x978, 0x03FF8000, (tx_x0_rxk[cal])>>21&0x000007ff);
4123 rtl_set_bbreg(hw, 0x978, 0x000007FF, (tx_y0_rxk[cal])>>21&0x000007ff);
4124 rtl_set_bbreg(hw, 0x978, BIT(31), 0x1);
4125 rtl_set_bbreg(hw, 0x97c, BIT(31), 0x0);
4126 rtl_set_bbreg(hw, 0xcb8, 0xF, 0xe);
4127 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
4128 rtl_write_dword(rtlpriv, 0x984, 0x0046a911);
4130 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
4131 rtl_set_bbreg(hw, 0xc80, BIT(29), 0x1);
4132 rtl_set_bbreg(hw, 0xc84, BIT(29), 0x0);
4133 rtl_write_dword(rtlpriv, 0xc88, 0x02140119);
4135 rtl_write_dword(rtlpriv, 0xc8c, 0x28160d00); /* pDM_Odm->SupportInterface == 1 */
4138 rtl_set_bbreg(hw, 0xce8, BIT(30), 0x1); /* RX VDF Enable */
4139 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
4144 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
4145 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
4147 mdelay(10); /* Delay 10ms */
4148 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
4151 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
4152 if ((~iqk_ready) || (delay_count > 20))
4160 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
4161 /* ============RXIQK Check============== */
4162 rx_fail = rtl_get_bbreg(hw, 0xd00, BIT(11));
4164 rtl_write_dword(rtlpriv, 0xcb8, 0x06000000);
4165 vdf_x[k] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4166 rtl_write_dword(rtlpriv, 0xcb8, 0x08000000);
4167 vdf_y[k] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4171 rtl_set_bbreg(hw, 0xc10, 0x000003ff, 0x200>>1);
4172 rtl_set_bbreg(hw, 0xc10, 0x03ff0000, 0x0>>1);
4175 if (cal_retry == 10)
4182 if (cal_retry == 10)
4189 rx_x0[cal] = vdf_x[k-1];
4190 rx_y0[cal] = vdf_y[k-1];
4192 rtl_set_bbreg(hw, 0xce8, BIT(31), 0x1); /* TX VDF Enable */
4196 /* ====== RX mode TXK (RXK Step 1) ====== */
4197 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4198 /* 1. TX RF Setting */
4199 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80000);
4200 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x30000);
4201 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x00029);
4202 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xd7ffb);
4203 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, temp_reg65);
4204 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x8a001);
4205 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x00000);
4206 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
4207 rtl_write_dword(rtlpriv, 0xb00, 0x03000100);
4208 rtl_write_dword(rtlpriv, 0x984, 0x0046a910);/* [0]:AGC_en, [15]:idac_K_Mask */
4210 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
4211 rtl_write_dword(rtlpriv, 0xc80, 0x18008c10);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
4212 rtl_write_dword(rtlpriv, 0xc84, 0x38008c10);/* RX_TONE_idx[9:0], RxK_Mask[29] */
4213 rtl_write_dword(rtlpriv, 0xc88, 0x821603e0);
4214 /* ODM_Write4Byte(pDM_Odm, 0xc8c, 0x68163e96); */
4215 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
4219 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
4220 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
4222 mdelay(10); /* Delay 10ms */
4223 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
4226 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
4227 if ((~iqk_ready) || (delay_count > 20))
4235 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
4236 /* ============TXIQK Check============== */
4237 tx_fail = rtl_get_bbreg(hw, 0xd00, BIT(12));
4240 rtl_write_dword(rtlpriv, 0xcb8, 0x02000000);
4241 tx_x0_rxk[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4242 rtl_write_dword(rtlpriv, 0xcb8, 0x04000000);
4243 tx_y0_rxk[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4249 if (cal_retry == 10)
4255 if (cal_retry == 10)
4260 if (tx0iqkok == false) { /* If RX mode TXK fail, then take TXK Result */
4261 tx_x0_rxk[cal] = tx_x0[cal];
4262 tx_y0_rxk[cal] = tx_y0[cal];
4264 rtl_dbg(rtlpriv, COMP_IQK,
4268 /* ====== RX IQK ====== */
4269 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4270 /* 1. RX RF Setting */
4271 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x80000);
4272 rtl_set_rfreg(hw, path, 0x30, RFREG_OFFSET_MASK, 0x30000);
4273 rtl_set_rfreg(hw, path, 0x31, RFREG_OFFSET_MASK, 0x0002f);
4274 rtl_set_rfreg(hw, path, 0x32, RFREG_OFFSET_MASK, 0xfffbb);
4275 rtl_set_rfreg(hw, path, 0x8f, RFREG_OFFSET_MASK, 0x88001);
4276 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, 0x931d8);
4277 rtl_set_rfreg(hw, path, 0xef, RFREG_OFFSET_MASK, 0x00000);
4279 rtl_set_bbreg(hw, 0x978, 0x03FF8000, (tx_x0_rxk[cal])>>21&0x000007ff);
4280 rtl_set_bbreg(hw, 0x978, 0x000007FF, (tx_y0_rxk[cal])>>21&0x000007ff);
4281 rtl_set_bbreg(hw, 0x978, BIT(31), 0x1);
4282 rtl_set_bbreg(hw, 0x97c, BIT(31), 0x0);
4283 /* ODM_SetBBReg(pDM_Odm, 0xcb8, 0xF, 0xe); */
4284 rtl_write_dword(rtlpriv, 0x90c, 0x00008000);
4285 rtl_write_dword(rtlpriv, 0x984, 0x0046a911);
4287 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
4288 rtl_write_dword(rtlpriv, 0xc80, 0x38008c10);/* TX_TONE_idx[9:0], TxK_Mask[29] TX_Tone = 16 */
4289 rtl_write_dword(rtlpriv, 0xc84, 0x18008c10);/* RX_TONE_idx[9:0], RxK_Mask[29] */
4290 rtl_write_dword(rtlpriv, 0xc88, 0x02140119);
4292 rtl_write_dword(rtlpriv, 0xc8c, 0x28160d00); /*pDM_Odm->SupportInterface == 1*/
4294 rtl_write_dword(rtlpriv, 0xcb8, 0x00100000);/* cb8[20] \B1N SI/PI \A8Ï¥\CE\C5v\A4\C1\B5\B9 iqk_dpk module */
4299 rtl_write_dword(rtlpriv, 0x980, 0xfa000000);
4300 rtl_write_dword(rtlpriv, 0x980, 0xf8000000);
4302 mdelay(10); /* Delay 10ms */
4303 rtl_write_dword(rtlpriv, 0xcb8, 0x00000000);
4306 iqk_ready = rtl_get_bbreg(hw, 0xd00, BIT(10));
4307 if ((~iqk_ready) || (delay_count > 20))
4315 if (delay_count < 20) { /* If 20ms No Result, then cal_retry++ */
4316 /* ============RXIQK Check============== */
4317 rx_fail = rtl_get_bbreg(hw, 0xd00, BIT(11));
4319 rtl_write_dword(rtlpriv, 0xcb8, 0x06000000);
4320 rx_x0[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4321 rtl_write_dword(rtlpriv, 0xcb8, 0x08000000);
4322 rx_y0[cal] = rtl_get_bbreg(hw, 0xd00, 0x07ff0000)<<21;
4326 rtl_set_bbreg(hw, 0xc10, 0x000003ff, 0x200>>1);
4327 rtl_set_bbreg(hw, 0xc10, 0x03ff0000, 0x0>>1);
4330 if (cal_retry == 10)
4337 if (cal_retry == 10)
4347 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4348 rtl_set_rfreg(hw, path, 0x65, RFREG_OFFSET_MASK, temp_reg65);
4356 /* FillIQK Result */
4359 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4360 "========Path_A =======\n");
4361 if (tx_average == 0)
4364 for (i = 0; i < tx_average; i++) {
4365 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4366 "TX_X0_RXK[%d] = %x ;; TX_Y0_RXK[%d] = %x\n", i,
4367 (tx_x0_rxk[i]) >> 21 & 0x000007ff, i,
4368 (tx_y0_rxk[i]) >> 21 & 0x000007ff);
4369 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4370 "TX_X0[%d] = %x ;; TX_Y0[%d] = %x\n", i,
4371 (tx_x0[i]) >> 21 & 0x000007ff, i,
4372 (tx_y0[i]) >> 21 & 0x000007ff);
4374 for (i = 0; i < tx_average; i++) {
4375 for (ii = i+1; ii < tx_average; ii++) {
4376 dx = (tx_x0[i]>>21) - (tx_x0[ii]>>21);
4377 if (dx < 3 && dx > -3) {
4378 dy = (tx_y0[i]>>21) - (tx_y0[ii]>>21);
4379 if (dy < 3 && dy > -3) {
4380 tx_x = ((tx_x0[i]>>21) + (tx_x0[ii]>>21))/2;
4381 tx_y = ((tx_y0[i]>>21) + (tx_y0[ii]>>21))/2;
4392 _rtl8821ae_iqk_tx_fill_iqc(hw, path, tx_x, tx_y); /* ? */
4394 _rtl8821ae_iqk_tx_fill_iqc(hw, path, 0x200, 0x0);
4396 if (rx_average == 0)
4399 for (i = 0; i < rx_average; i++)
4400 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4401 "RX_X0[%d] = %x ;; RX_Y0[%d] = %x\n", i,
4402 (rx_x0[i])>>21&0x000007ff, i,
4403 (rx_y0[i])>>21&0x000007ff);
4404 for (i = 0; i < rx_average; i++) {
4405 for (ii = i+1; ii < rx_average; ii++) {
4406 dx = (rx_x0[i]>>21) - (rx_x0[ii]>>21);
4407 if (dx < 4 && dx > -4) {
4408 dy = (rx_y0[i]>>21) - (rx_y0[ii]>>21);
4409 if (dy < 4 && dy > -4) {
4410 rx_x = ((rx_x0[i]>>21) + (rx_x0[ii]>>21))/2;
4411 rx_y = ((rx_y0[i]>>21) + (rx_y0[ii]>>21))/2;
4422 _rtl8821ae_iqk_rx_fill_iqc(hw, path, rx_x, rx_y);
4424 _rtl8821ae_iqk_rx_fill_iqc(hw, path, 0x200, 0x0);
4431 static void _rtl8821ae_iqk_restore_rf(struct ieee80211_hw *hw,
4432 enum radio_path path,
4434 u32 *rf_backup, u32 rf_reg_num)
4436 struct rtl_priv *rtlpriv = rtl_priv(hw);
4439 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4440 for (i = 0; i < RF_REG_NUM; i++)
4441 rtl_set_rfreg(hw, path, backup_rf_reg[i], RFREG_OFFSET_MASK,
4446 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4447 "RestoreRF Path A Success!!!!\n");
4454 static void _rtl8821ae_iqk_restore_afe(struct ieee80211_hw *hw,
4455 u32 *afe_backup, u32 *backup_afe_reg,
4459 struct rtl_priv *rtlpriv = rtl_priv(hw);
4461 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4462 /* Reload AFE Parameters */
4463 for (i = 0; i < afe_num; i++)
4464 rtl_write_dword(rtlpriv, backup_afe_reg[i], afe_backup[i]);
4465 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x1); /* [31] = 1 --> Page C1 */
4466 rtl_write_dword(rtlpriv, 0xc80, 0x0);
4467 rtl_write_dword(rtlpriv, 0xc84, 0x0);
4468 rtl_write_dword(rtlpriv, 0xc88, 0x0);
4469 rtl_write_dword(rtlpriv, 0xc8c, 0x3c000000);
4470 rtl_write_dword(rtlpriv, 0xc90, 0x00000080);
4471 rtl_write_dword(rtlpriv, 0xc94, 0x00000000);
4472 rtl_write_dword(rtlpriv, 0xcc4, 0x20040000);
4473 rtl_write_dword(rtlpriv, 0xcc8, 0x20000000);
4474 rtl_write_dword(rtlpriv, 0xcb8, 0x0);
4475 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "RestoreAFE Success!!!!\n");
4478 static void _rtl8821ae_iqk_restore_macbb(struct ieee80211_hw *hw,
4480 u32 *backup_macbb_reg,
4484 struct rtl_priv *rtlpriv = rtl_priv(hw);
4486 rtl_set_bbreg(hw, 0x82c, BIT(31), 0x0); /* [31] = 0 --> Page C */
4487 /* Reload MacBB Parameters */
4488 for (i = 0; i < macbb_num; i++)
4489 rtl_write_dword(rtlpriv, backup_macbb_reg[i], macbb_backup[i]);
4490 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD, "RestoreMacBB Success!!!!\n");
4493 #undef MACBB_REG_NUM
4497 #define MACBB_REG_NUM 11
4498 #define AFE_REG_NUM 12
4499 #define RF_REG_NUM 3
4501 static void _rtl8821ae_phy_iq_calibrate(struct ieee80211_hw *hw)
4503 u32 macbb_backup[MACBB_REG_NUM];
4504 u32 afe_backup[AFE_REG_NUM];
4505 u32 rfa_backup[RF_REG_NUM];
4506 u32 rfb_backup[RF_REG_NUM];
4507 u32 backup_macbb_reg[MACBB_REG_NUM] = {
4508 0xb00, 0x520, 0x550, 0x808, 0x90c, 0xc00, 0xc50,
4509 0xe00, 0xe50, 0x838, 0x82c
4511 u32 backup_afe_reg[AFE_REG_NUM] = {
4512 0xc5c, 0xc60, 0xc64, 0xc68, 0xc6c, 0xc70, 0xc74,
4513 0xc78, 0xc7c, 0xc80, 0xc84, 0xcb8
4515 u32 backup_rf_reg[RF_REG_NUM] = {0x65, 0x8f, 0x0};
4517 _rtl8821ae_iqk_backup_macbb(hw, macbb_backup, backup_macbb_reg,
4519 _rtl8821ae_iqk_backup_afe(hw, afe_backup, backup_afe_reg, AFE_REG_NUM);
4520 _rtl8821ae_iqk_backup_rf(hw, rfa_backup, rfb_backup, backup_rf_reg,
4523 _rtl8821ae_iqk_configure_mac(hw);
4524 _rtl8821ae_iqk_tx(hw, RF90_PATH_A);
4525 _rtl8821ae_iqk_restore_rf(hw, RF90_PATH_A, backup_rf_reg, rfa_backup,
4528 _rtl8821ae_iqk_restore_afe(hw, afe_backup, backup_afe_reg, AFE_REG_NUM);
4529 _rtl8821ae_iqk_restore_macbb(hw, macbb_backup, backup_macbb_reg,
4533 static void _rtl8821ae_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool main)
4535 struct rtl_priv *rtlpriv = rtl_priv(hw);
4536 /* struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw)); */
4537 /* struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw)); */
4538 rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
4541 rtl_set_bbreg(hw, RA_RFE_PINMUX + 4, BIT(29) | BIT(28), 0x1);
4543 rtl_set_bbreg(hw, RA_RFE_PINMUX + 4, BIT(29) | BIT(28), 0x2);
4546 #undef IQK_ADDA_REG_NUM
4547 #undef IQK_DELAY_TIME
4549 void rtl8812ae_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
4553 void rtl8812ae_do_iqk(struct ieee80211_hw *hw, u8 delta_thermal_index,
4554 u8 thermal_value, u8 threshold)
4556 struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
4558 rtldm->thermalvalue_iqk = thermal_value;
4559 rtl8812ae_phy_iq_calibrate(hw, false);
4562 void rtl8821ae_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
4564 struct rtl_priv *rtlpriv = rtl_priv(hw);
4565 struct rtl_phy *rtlphy = &rtlpriv->phy;
4567 if (!rtlphy->lck_inprogress) {
4568 spin_lock(&rtlpriv->locks.iqk_lock);
4569 rtlphy->lck_inprogress = true;
4570 spin_unlock(&rtlpriv->locks.iqk_lock);
4572 _rtl8821ae_phy_iq_calibrate(hw);
4574 spin_lock(&rtlpriv->locks.iqk_lock);
4575 rtlphy->lck_inprogress = false;
4576 spin_unlock(&rtlpriv->locks.iqk_lock);
4580 void rtl8821ae_reset_iqk_result(struct ieee80211_hw *hw)
4582 struct rtl_priv *rtlpriv = rtl_priv(hw);
4583 struct rtl_phy *rtlphy = &rtlpriv->phy;
4586 rtl_dbg(rtlpriv, COMP_IQK, DBG_LOUD,
4587 "rtl8812ae_dm_reset_iqk_result:: settings regs %d default regs %d\n",
4588 (int)(sizeof(rtlphy->iqk_matrix) /
4589 sizeof(struct iqk_matrix_regs)),
4590 IQK_MATRIX_SETTINGS_NUM);
4592 for (i = 0; i < IQK_MATRIX_SETTINGS_NUM; i++) {
4593 rtlphy->iqk_matrix[i].value[0][0] = 0x100;
4594 rtlphy->iqk_matrix[i].value[0][2] = 0x100;
4595 rtlphy->iqk_matrix[i].value[0][4] = 0x100;
4596 rtlphy->iqk_matrix[i].value[0][6] = 0x100;
4598 rtlphy->iqk_matrix[i].value[0][1] = 0x0;
4599 rtlphy->iqk_matrix[i].value[0][3] = 0x0;
4600 rtlphy->iqk_matrix[i].value[0][5] = 0x0;
4601 rtlphy->iqk_matrix[i].value[0][7] = 0x0;
4603 rtlphy->iqk_matrix[i].iqk_done = false;
4607 void rtl8821ae_do_iqk(struct ieee80211_hw *hw, u8 delta_thermal_index,
4608 u8 thermal_value, u8 threshold)
4610 struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
4612 rtl8821ae_reset_iqk_result(hw);
4614 rtldm->thermalvalue_iqk = thermal_value;
4615 rtl8821ae_phy_iq_calibrate(hw, false);
4618 void rtl8821ae_phy_lc_calibrate(struct ieee80211_hw *hw)
4622 void rtl8821ae_phy_ap_calibrate(struct ieee80211_hw *hw, s8 delta)
4626 void rtl8821ae_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool bmain)
4628 _rtl8821ae_phy_set_rfpath_switch(hw, bmain);
4631 bool rtl8821ae_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
4633 struct rtl_priv *rtlpriv = rtl_priv(hw);
4634 struct rtl_phy *rtlphy = &rtlpriv->phy;
4635 bool postprocessing = false;
4637 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
4638 "-->IO Cmd(%#x), set_io_inprogress(%d)\n",
4639 iotype, rtlphy->set_io_inprogress);
4642 case IO_CMD_RESUME_DM_BY_SCAN:
4643 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
4644 "[IO CMD] Resume DM after scan.\n");
4645 postprocessing = true;
4647 case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
4648 case IO_CMD_PAUSE_BAND1_DM_BY_SCAN:
4649 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
4650 "[IO CMD] Pause DM before scan.\n");
4651 postprocessing = true;
4654 pr_err("switch case %#x not processed\n",
4659 if (postprocessing && !rtlphy->set_io_inprogress) {
4660 rtlphy->set_io_inprogress = true;
4661 rtlphy->current_io_type = iotype;
4665 rtl8821ae_phy_set_io(hw);
4666 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE, "IO Type(%#x)\n", iotype);
4670 static void rtl8821ae_phy_set_io(struct ieee80211_hw *hw)
4672 struct rtl_priv *rtlpriv = rtl_priv(hw);
4673 struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
4674 struct rtl_phy *rtlphy = &rtlpriv->phy;
4676 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
4677 "--->Cmd(%#x), set_io_inprogress(%d)\n",
4678 rtlphy->current_io_type, rtlphy->set_io_inprogress);
4679 switch (rtlphy->current_io_type) {
4680 case IO_CMD_RESUME_DM_BY_SCAN:
4681 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
4682 _rtl8821ae_resume_tx_beacon(hw);
4683 rtl8821ae_dm_write_dig(hw, rtlphy->initgain_backup.xaagccore1);
4684 rtl8821ae_dm_write_cck_cca_thres(hw,
4685 rtlphy->initgain_backup.cca);
4687 case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
4688 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
4689 _rtl8821ae_stop_tx_beacon(hw);
4690 rtlphy->initgain_backup.xaagccore1 = dm_digtable->cur_igvalue;
4691 rtl8821ae_dm_write_dig(hw, 0x17);
4692 rtlphy->initgain_backup.cca = dm_digtable->cur_cck_cca_thres;
4693 rtl8821ae_dm_write_cck_cca_thres(hw, 0x40);
4695 case IO_CMD_PAUSE_BAND1_DM_BY_SCAN:
4698 pr_err("switch case %#x not processed\n",
4699 rtlphy->current_io_type);
4702 rtlphy->set_io_inprogress = false;
4703 rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
4704 "(%#x)\n", rtlphy->current_io_type);
4707 static void rtl8821ae_phy_set_rf_on(struct ieee80211_hw *hw)
4709 struct rtl_priv *rtlpriv = rtl_priv(hw);
4711 rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
4712 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
4713 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
4714 rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
4715 rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
4718 static bool _rtl8821ae_phy_set_rf_power_state(struct ieee80211_hw *hw,
4719 enum rf_pwrstate rfpwr_state)
4721 struct rtl_priv *rtlpriv = rtl_priv(hw);
4722 struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
4723 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
4724 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
4725 bool bresult = true;
4727 struct rtl8192_tx_ring *ring = NULL;
4729 switch (rfpwr_state) {
4731 if ((ppsc->rfpwr_state == ERFOFF) &&
4732 RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
4733 bool rtstatus = false;
4734 u32 initializecount = 0;
4738 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
4739 "IPS Set eRf nic enable\n");
4740 rtstatus = rtl_ps_enable_nic(hw);
4741 } while (!rtstatus && (initializecount < 10));
4742 RT_CLEAR_PS_LEVEL(ppsc,
4743 RT_RF_OFF_LEVL_HALT_NIC);
4745 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
4746 "Set ERFON slept:%d ms\n",
4747 jiffies_to_msecs(jiffies -
4748 ppsc->last_sleep_jiffies));
4749 ppsc->last_awake_jiffies = jiffies;
4750 rtl8821ae_phy_set_rf_on(hw);
4752 if (mac->link_state == MAC80211_LINKED) {
4753 rtlpriv->cfg->ops->led_control(hw,
4756 rtlpriv->cfg->ops->led_control(hw,
4761 for (queue_id = 0, i = 0;
4762 queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
4763 ring = &pcipriv->dev.tx_ring[queue_id];
4764 if (queue_id == BEACON_QUEUE ||
4765 skb_queue_len(&ring->queue) == 0) {
4769 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
4770 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
4772 skb_queue_len(&ring->queue));
4777 if (i >= MAX_DOZE_WAITING_TIMES_9x) {
4778 rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
4779 "\n ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
4780 MAX_DOZE_WAITING_TIMES_9x,
4782 skb_queue_len(&ring->queue));
4787 if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
4788 rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
4789 "IPS Set eRf nic disable\n");
4790 rtl_ps_disable_nic(hw);
4791 RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
4793 if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS) {
4794 rtlpriv->cfg->ops->led_control(hw,
4797 rtlpriv->cfg->ops->led_control(hw,
4803 pr_err("switch case %#x not processed\n",
4809 ppsc->rfpwr_state = rfpwr_state;
4813 bool rtl8821ae_phy_set_rf_power_state(struct ieee80211_hw *hw,
4814 enum rf_pwrstate rfpwr_state)
4816 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
4818 bool bresult = false;
4820 if (rfpwr_state == ppsc->rfpwr_state)
4822 bresult = _rtl8821ae_phy_set_rf_power_state(hw, rfpwr_state);