xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192ce/rf.c (revision 0770f718458ed93fe593b887fb7481c30999bea5)
1*0770f718SLarry Finger // SPDX-License-Identifier: GPL-2.0
2*0770f718SLarry Finger /* Copyright(c) 2009-2012  Realtek Corporation.*/
3f1d2b4d3SLarry Finger 
4f1d2b4d3SLarry Finger #include "../wifi.h"
5f1d2b4d3SLarry Finger #include "reg.h"
6f1d2b4d3SLarry Finger #include "def.h"
7f1d2b4d3SLarry Finger #include "phy.h"
8f1d2b4d3SLarry Finger #include "rf.h"
9f1d2b4d3SLarry Finger #include "dm.h"
10f1d2b4d3SLarry Finger 
11f1d2b4d3SLarry Finger static bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
12f1d2b4d3SLarry Finger 
13f1d2b4d3SLarry Finger void rtl92ce_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
14f1d2b4d3SLarry Finger {
15f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
16f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
17f1d2b4d3SLarry Finger 
18f1d2b4d3SLarry Finger 	switch (bandwidth) {
19f1d2b4d3SLarry Finger 	case HT_CHANNEL_WIDTH_20:
20f1d2b4d3SLarry Finger 		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
21f1d2b4d3SLarry Finger 					     0xfffff3ff) | 0x0400);
22f1d2b4d3SLarry Finger 		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
23f1d2b4d3SLarry Finger 			      rtlphy->rfreg_chnlval[0]);
24f1d2b4d3SLarry Finger 		break;
25f1d2b4d3SLarry Finger 	case HT_CHANNEL_WIDTH_20_40:
26f1d2b4d3SLarry Finger 		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
27f1d2b4d3SLarry Finger 					     0xfffff3ff));
28f1d2b4d3SLarry Finger 		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
29f1d2b4d3SLarry Finger 			      rtlphy->rfreg_chnlval[0]);
30f1d2b4d3SLarry Finger 		break;
31f1d2b4d3SLarry Finger 	default:
32e40a0056SLarry Finger 		pr_err("unknown bandwidth: %#X\n", bandwidth);
33f1d2b4d3SLarry Finger 		break;
34f1d2b4d3SLarry Finger 	}
35f1d2b4d3SLarry Finger }
36f1d2b4d3SLarry Finger 
37f1d2b4d3SLarry Finger void rtl92ce_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
38f1d2b4d3SLarry Finger 					u8 *ppowerlevel)
39f1d2b4d3SLarry Finger {
40f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
41f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
42f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
43f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
44f1d2b4d3SLarry Finger 	u32 tx_agc[2] = {0, 0}, tmpval;
45f1d2b4d3SLarry Finger 	bool turbo_scanoff = false;
46f1d2b4d3SLarry Finger 	u8 idx1, idx2;
47f1d2b4d3SLarry Finger 	u8 *ptr;
48f1d2b4d3SLarry Finger 
49f1d2b4d3SLarry Finger 	if (rtlefuse->eeprom_regulatory != 0)
50f1d2b4d3SLarry Finger 		turbo_scanoff = true;
51f1d2b4d3SLarry Finger 
52f1d2b4d3SLarry Finger 	if (mac->act_scanning) {
53f1d2b4d3SLarry Finger 		tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
54f1d2b4d3SLarry Finger 		tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
55f1d2b4d3SLarry Finger 
56f1d2b4d3SLarry Finger 		if (turbo_scanoff) {
57f1d2b4d3SLarry Finger 			for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
58f1d2b4d3SLarry Finger 				tx_agc[idx1] = ppowerlevel[idx1] |
59f1d2b4d3SLarry Finger 				    (ppowerlevel[idx1] << 8) |
60f1d2b4d3SLarry Finger 				    (ppowerlevel[idx1] << 16) |
61f1d2b4d3SLarry Finger 				    (ppowerlevel[idx1] << 24);
62f1d2b4d3SLarry Finger 			}
63f1d2b4d3SLarry Finger 		}
64f1d2b4d3SLarry Finger 	} else {
65f1d2b4d3SLarry Finger 		for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
66f1d2b4d3SLarry Finger 			tx_agc[idx1] = ppowerlevel[idx1] |
67f1d2b4d3SLarry Finger 			    (ppowerlevel[idx1] << 8) |
68f1d2b4d3SLarry Finger 			    (ppowerlevel[idx1] << 16) |
69f1d2b4d3SLarry Finger 			    (ppowerlevel[idx1] << 24);
70f1d2b4d3SLarry Finger 		}
71f1d2b4d3SLarry Finger 
72f1d2b4d3SLarry Finger 		if (rtlefuse->eeprom_regulatory == 0) {
73f1d2b4d3SLarry Finger 			tmpval = (rtlphy->mcs_offset[0][6]) +
74f1d2b4d3SLarry Finger 			    (rtlphy->mcs_offset[0][7] << 8);
75f1d2b4d3SLarry Finger 			tx_agc[RF90_PATH_A] += tmpval;
76f1d2b4d3SLarry Finger 
77f1d2b4d3SLarry Finger 			tmpval = (rtlphy->mcs_offset[0][14]) +
78f1d2b4d3SLarry Finger 				 (rtlphy->mcs_offset[0][15] << 24);
79f1d2b4d3SLarry Finger 			tx_agc[RF90_PATH_B] += tmpval;
80f1d2b4d3SLarry Finger 		}
81f1d2b4d3SLarry Finger 	}
82f1d2b4d3SLarry Finger 
83f1d2b4d3SLarry Finger 	for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
84f1d2b4d3SLarry Finger 		ptr = (u8 *) (&(tx_agc[idx1]));
85f1d2b4d3SLarry Finger 		for (idx2 = 0; idx2 < 4; idx2++) {
86f1d2b4d3SLarry Finger 			if (*ptr > RF6052_MAX_TX_PWR)
87f1d2b4d3SLarry Finger 				*ptr = RF6052_MAX_TX_PWR;
88f1d2b4d3SLarry Finger 			ptr++;
89f1d2b4d3SLarry Finger 		}
90f1d2b4d3SLarry Finger 	}
91f1d2b4d3SLarry Finger 
92f1d2b4d3SLarry Finger 	tmpval = tx_agc[RF90_PATH_A] & 0xff;
93f1d2b4d3SLarry Finger 	rtl_set_bbreg(hw, RTXAGC_A_CCK1_MCS32, MASKBYTE1, tmpval);
94f1d2b4d3SLarry Finger 
95f1d2b4d3SLarry Finger 	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
96f1d2b4d3SLarry Finger 		"CCK PWR 1M (rf-A) = 0x%x (reg 0x%x)\n",
97f1d2b4d3SLarry Finger 		tmpval, RTXAGC_A_CCK1_MCS32);
98f1d2b4d3SLarry Finger 
99f1d2b4d3SLarry Finger 	tmpval = tx_agc[RF90_PATH_A] >> 8;
100f1d2b4d3SLarry Finger 
101f1d2b4d3SLarry Finger 	tmpval = tmpval & 0xff00ffff;
102f1d2b4d3SLarry Finger 
103f1d2b4d3SLarry Finger 	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
104f1d2b4d3SLarry Finger 
105f1d2b4d3SLarry Finger 	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
106f1d2b4d3SLarry Finger 		"CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n",
107f1d2b4d3SLarry Finger 		tmpval, RTXAGC_B_CCK11_A_CCK2_11);
108f1d2b4d3SLarry Finger 
109f1d2b4d3SLarry Finger 	tmpval = tx_agc[RF90_PATH_B] >> 24;
110f1d2b4d3SLarry Finger 	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
111f1d2b4d3SLarry Finger 
112f1d2b4d3SLarry Finger 	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
113f1d2b4d3SLarry Finger 		"CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n",
114f1d2b4d3SLarry Finger 		tmpval, RTXAGC_B_CCK11_A_CCK2_11);
115f1d2b4d3SLarry Finger 
116f1d2b4d3SLarry Finger 	tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
117f1d2b4d3SLarry Finger 	rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
118f1d2b4d3SLarry Finger 
119f1d2b4d3SLarry Finger 	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
120f1d2b4d3SLarry Finger 		"CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n",
121f1d2b4d3SLarry Finger 		tmpval, RTXAGC_B_CCK1_55_MCS32);
122f1d2b4d3SLarry Finger }
123f1d2b4d3SLarry Finger 
124f1d2b4d3SLarry Finger static void rtl92c_phy_get_power_base(struct ieee80211_hw *hw,
125f1d2b4d3SLarry Finger 				      u8 *ppowerlevel, u8 channel,
126f1d2b4d3SLarry Finger 				      u32 *ofdmbase, u32 *mcsbase)
127f1d2b4d3SLarry Finger {
128f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
129f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
130f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
131f1d2b4d3SLarry Finger 	u32 powerBase0, powerBase1;
132f1d2b4d3SLarry Finger 	u8 legacy_pwrdiff, ht20_pwrdiff;
133f1d2b4d3SLarry Finger 	u8 i, powerlevel[2];
134f1d2b4d3SLarry Finger 
135f1d2b4d3SLarry Finger 	for (i = 0; i < 2; i++) {
136f1d2b4d3SLarry Finger 		powerlevel[i] = ppowerlevel[i];
137f1d2b4d3SLarry Finger 		legacy_pwrdiff = rtlefuse->txpwr_legacyhtdiff[i][channel - 1];
138f1d2b4d3SLarry Finger 		powerBase0 = powerlevel[i] + legacy_pwrdiff;
139f1d2b4d3SLarry Finger 
140f1d2b4d3SLarry Finger 		powerBase0 = (powerBase0 << 24) | (powerBase0 << 16) |
141f1d2b4d3SLarry Finger 		    (powerBase0 << 8) | powerBase0;
142f1d2b4d3SLarry Finger 		*(ofdmbase + i) = powerBase0;
143f1d2b4d3SLarry Finger 		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
144f1d2b4d3SLarry Finger 			" [OFDM power base index rf(%c) = 0x%x]\n",
145f1d2b4d3SLarry Finger 			i == 0 ? 'A' : 'B', *(ofdmbase + i));
146f1d2b4d3SLarry Finger 	}
147f1d2b4d3SLarry Finger 
148f1d2b4d3SLarry Finger 	for (i = 0; i < 2; i++) {
149f1d2b4d3SLarry Finger 		if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20) {
150f1d2b4d3SLarry Finger 			ht20_pwrdiff = rtlefuse->txpwr_ht20diff[i][channel - 1];
151f1d2b4d3SLarry Finger 			powerlevel[i] += ht20_pwrdiff;
152f1d2b4d3SLarry Finger 		}
153f1d2b4d3SLarry Finger 		powerBase1 = powerlevel[i];
154f1d2b4d3SLarry Finger 		powerBase1 = (powerBase1 << 24) |
155f1d2b4d3SLarry Finger 		    (powerBase1 << 16) | (powerBase1 << 8) | powerBase1;
156f1d2b4d3SLarry Finger 
157f1d2b4d3SLarry Finger 		*(mcsbase + i) = powerBase1;
158f1d2b4d3SLarry Finger 
159f1d2b4d3SLarry Finger 		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
160f1d2b4d3SLarry Finger 			" [MCS power base index rf(%c) = 0x%x]\n",
161f1d2b4d3SLarry Finger 			i == 0 ? 'A' : 'B', *(mcsbase + i));
162f1d2b4d3SLarry Finger 	}
163f1d2b4d3SLarry Finger }
164f1d2b4d3SLarry Finger 
165f1d2b4d3SLarry Finger static void _rtl92c_get_txpower_writeval_by_regulatory(struct ieee80211_hw *hw,
166f1d2b4d3SLarry Finger 						       u8 channel, u8 index,
167f1d2b4d3SLarry Finger 						       u32 *powerBase0,
168f1d2b4d3SLarry Finger 						       u32 *powerBase1,
169f1d2b4d3SLarry Finger 						       u32 *p_outwriteval)
170f1d2b4d3SLarry Finger {
171f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
172f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
173f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
174f1d2b4d3SLarry Finger 	u8 i, chnlgroup = 0, pwr_diff_limit[4];
175f1d2b4d3SLarry Finger 	u32 writeVal, customer_limit, rf;
176f1d2b4d3SLarry Finger 
177f1d2b4d3SLarry Finger 	for (rf = 0; rf < 2; rf++) {
178f1d2b4d3SLarry Finger 		switch (rtlefuse->eeprom_regulatory) {
179f1d2b4d3SLarry Finger 		case 0:
180f1d2b4d3SLarry Finger 			chnlgroup = 0;
181f1d2b4d3SLarry Finger 
182f1d2b4d3SLarry Finger 			writeVal = rtlphy->mcs_offset[chnlgroup][index +
183f1d2b4d3SLarry Finger 			    (rf ? 8 : 0)]
184f1d2b4d3SLarry Finger 			    + ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
185f1d2b4d3SLarry Finger 
186f1d2b4d3SLarry Finger 			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
187f1d2b4d3SLarry Finger 				"RTK better performance, writeVal(%c) = 0x%x\n",
188f1d2b4d3SLarry Finger 				rf == 0 ? 'A' : 'B', writeVal);
189f1d2b4d3SLarry Finger 			break;
190f1d2b4d3SLarry Finger 		case 1:
191f1d2b4d3SLarry Finger 			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
192f1d2b4d3SLarry Finger 				writeVal = ((index < 2) ? powerBase0[rf] :
193f1d2b4d3SLarry Finger 					    powerBase1[rf]);
194f1d2b4d3SLarry Finger 
195f1d2b4d3SLarry Finger 				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
196f1d2b4d3SLarry Finger 					"Realtek regulatory, 40MHz, writeVal(%c) = 0x%x\n",
197f1d2b4d3SLarry Finger 					rf == 0 ? 'A' : 'B', writeVal);
198f1d2b4d3SLarry Finger 			} else {
199f1d2b4d3SLarry Finger 				if (rtlphy->pwrgroup_cnt == 1)
200f1d2b4d3SLarry Finger 					chnlgroup = 0;
201f1d2b4d3SLarry Finger 				if (rtlphy->pwrgroup_cnt >= 3) {
202f1d2b4d3SLarry Finger 					if (channel <= 3)
203f1d2b4d3SLarry Finger 						chnlgroup = 0;
204f1d2b4d3SLarry Finger 					else if (channel >= 4 && channel <= 9)
205f1d2b4d3SLarry Finger 						chnlgroup = 1;
206f1d2b4d3SLarry Finger 					else if (channel > 9)
207f1d2b4d3SLarry Finger 						chnlgroup = 2;
208f1d2b4d3SLarry Finger 					if (rtlphy->pwrgroup_cnt == 4)
209f1d2b4d3SLarry Finger 						chnlgroup++;
210f1d2b4d3SLarry Finger 				}
211f1d2b4d3SLarry Finger 
212f1d2b4d3SLarry Finger 				writeVal = rtlphy->mcs_offset[chnlgroup]
213f1d2b4d3SLarry Finger 				    [index + (rf ? 8 : 0)] + ((index < 2) ?
214f1d2b4d3SLarry Finger 							      powerBase0[rf] :
215f1d2b4d3SLarry Finger 							      powerBase1[rf]);
216f1d2b4d3SLarry Finger 
217f1d2b4d3SLarry Finger 				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
218f1d2b4d3SLarry Finger 					"Realtek regulatory, 20MHz, writeVal(%c) = 0x%x\n",
219f1d2b4d3SLarry Finger 					rf == 0 ? 'A' : 'B', writeVal);
220f1d2b4d3SLarry Finger 			}
221f1d2b4d3SLarry Finger 			break;
222f1d2b4d3SLarry Finger 		case 2:
223f1d2b4d3SLarry Finger 			writeVal =
224f1d2b4d3SLarry Finger 			    ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
225f1d2b4d3SLarry Finger 
226f1d2b4d3SLarry Finger 			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
227f1d2b4d3SLarry Finger 				"Better regulatory, writeVal(%c) = 0x%x\n",
228f1d2b4d3SLarry Finger 				rf == 0 ? 'A' : 'B', writeVal);
229f1d2b4d3SLarry Finger 			break;
230f1d2b4d3SLarry Finger 		case 3:
231f1d2b4d3SLarry Finger 			chnlgroup = 0;
232f1d2b4d3SLarry Finger 
233f1d2b4d3SLarry Finger 			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
234f1d2b4d3SLarry Finger 				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
235f1d2b4d3SLarry Finger 					"customer's limit, 40MHz rf(%c) = 0x%x\n",
236f1d2b4d3SLarry Finger 					rf == 0 ? 'A' : 'B',
237f1d2b4d3SLarry Finger 					rtlefuse->pwrgroup_ht40[rf][channel -
238f1d2b4d3SLarry Finger 								    1]);
239f1d2b4d3SLarry Finger 			} else {
240f1d2b4d3SLarry Finger 				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
241f1d2b4d3SLarry Finger 					"customer's limit, 20MHz rf(%c) = 0x%x\n",
242f1d2b4d3SLarry Finger 					rf == 0 ? 'A' : 'B',
243f1d2b4d3SLarry Finger 					rtlefuse->pwrgroup_ht20[rf][channel -
244f1d2b4d3SLarry Finger 								    1]);
245f1d2b4d3SLarry Finger 			}
246f1d2b4d3SLarry Finger 			for (i = 0; i < 4; i++) {
247f1d2b4d3SLarry Finger 				pwr_diff_limit[i] = (u8) ((rtlphy->mcs_offset
248f1d2b4d3SLarry Finger 					  [chnlgroup][index +
249f1d2b4d3SLarry Finger 					  (rf ? 8 : 0)] & (0x7f << (i * 8))) >>
250f1d2b4d3SLarry Finger 					  (i * 8));
251f1d2b4d3SLarry Finger 
252f1d2b4d3SLarry Finger 				if (rtlphy->current_chan_bw ==
253f1d2b4d3SLarry Finger 				    HT_CHANNEL_WIDTH_20_40) {
254f1d2b4d3SLarry Finger 					if (pwr_diff_limit[i] >
255f1d2b4d3SLarry Finger 					    rtlefuse->
256f1d2b4d3SLarry Finger 					    pwrgroup_ht40[rf][channel - 1])
257f1d2b4d3SLarry Finger 						pwr_diff_limit[i] =
258f1d2b4d3SLarry Finger 						    rtlefuse->pwrgroup_ht40[rf]
259f1d2b4d3SLarry Finger 						    [channel - 1];
260f1d2b4d3SLarry Finger 				} else {
261f1d2b4d3SLarry Finger 					if (pwr_diff_limit[i] >
262f1d2b4d3SLarry Finger 					    rtlefuse->
263f1d2b4d3SLarry Finger 					    pwrgroup_ht20[rf][channel - 1])
264f1d2b4d3SLarry Finger 						pwr_diff_limit[i] =
265f1d2b4d3SLarry Finger 						    rtlefuse->pwrgroup_ht20[rf]
266f1d2b4d3SLarry Finger 						    [channel - 1];
267f1d2b4d3SLarry Finger 				}
268f1d2b4d3SLarry Finger 			}
269f1d2b4d3SLarry Finger 
270f1d2b4d3SLarry Finger 			customer_limit = (pwr_diff_limit[3] << 24) |
271f1d2b4d3SLarry Finger 			    (pwr_diff_limit[2] << 16) |
272f1d2b4d3SLarry Finger 			    (pwr_diff_limit[1] << 8) | (pwr_diff_limit[0]);
273f1d2b4d3SLarry Finger 
274f1d2b4d3SLarry Finger 			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
275f1d2b4d3SLarry Finger 				"Customer's limit rf(%c) = 0x%x\n",
276f1d2b4d3SLarry Finger 				rf == 0 ? 'A' : 'B', customer_limit);
277f1d2b4d3SLarry Finger 
278f1d2b4d3SLarry Finger 			writeVal = customer_limit +
279f1d2b4d3SLarry Finger 			    ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
280f1d2b4d3SLarry Finger 
281f1d2b4d3SLarry Finger 			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
282f1d2b4d3SLarry Finger 				"Customer, writeVal rf(%c)= 0x%x\n",
283f1d2b4d3SLarry Finger 				rf == 0 ? 'A' : 'B', writeVal);
284f1d2b4d3SLarry Finger 			break;
285f1d2b4d3SLarry Finger 		default:
286f1d2b4d3SLarry Finger 			chnlgroup = 0;
287f1d2b4d3SLarry Finger 			writeVal = rtlphy->mcs_offset[chnlgroup]
288f1d2b4d3SLarry Finger 			    [index + (rf ? 8 : 0)]
289f1d2b4d3SLarry Finger 			    + ((index < 2) ? powerBase0[rf] : powerBase1[rf]);
290f1d2b4d3SLarry Finger 
291f1d2b4d3SLarry Finger 			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
292f1d2b4d3SLarry Finger 				"RTK better performance, writeVal rf(%c) = 0x%x\n",
293f1d2b4d3SLarry Finger 				rf == 0 ? 'A' : 'B', writeVal);
294f1d2b4d3SLarry Finger 			break;
295f1d2b4d3SLarry Finger 		}
296f1d2b4d3SLarry Finger 
297f1d2b4d3SLarry Finger 		if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
298f1d2b4d3SLarry Finger 			writeVal = writeVal - 0x06060606;
299f1d2b4d3SLarry Finger 		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
300f1d2b4d3SLarry Finger 			 TXHIGHPWRLEVEL_BT2)
301f1d2b4d3SLarry Finger 			writeVal = writeVal - 0x0c0c0c0c;
302f1d2b4d3SLarry Finger 		*(p_outwriteval + rf) = writeVal;
303f1d2b4d3SLarry Finger 	}
304f1d2b4d3SLarry Finger }
305f1d2b4d3SLarry Finger 
306f1d2b4d3SLarry Finger static void _rtl92c_write_ofdm_power_reg(struct ieee80211_hw *hw,
307f1d2b4d3SLarry Finger 					 u8 index, u32 *pValue)
308f1d2b4d3SLarry Finger {
309f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
310f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
311f1d2b4d3SLarry Finger 
312f1d2b4d3SLarry Finger 	u16 regoffset_a[6] = {
313f1d2b4d3SLarry Finger 		RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
314f1d2b4d3SLarry Finger 		RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
315f1d2b4d3SLarry Finger 		RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
316f1d2b4d3SLarry Finger 	};
317f1d2b4d3SLarry Finger 	u16 regoffset_b[6] = {
318f1d2b4d3SLarry Finger 		RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
319f1d2b4d3SLarry Finger 		RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
320f1d2b4d3SLarry Finger 		RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
321f1d2b4d3SLarry Finger 	};
322f1d2b4d3SLarry Finger 	u8 i, rf, pwr_val[4];
323f1d2b4d3SLarry Finger 	u32 writeVal;
324f1d2b4d3SLarry Finger 	u16 regoffset;
325f1d2b4d3SLarry Finger 
326f1d2b4d3SLarry Finger 	for (rf = 0; rf < 2; rf++) {
327f1d2b4d3SLarry Finger 		writeVal = pValue[rf];
328f1d2b4d3SLarry Finger 		for (i = 0; i < 4; i++) {
329f1d2b4d3SLarry Finger 			pwr_val[i] = (u8) ((writeVal & (0x7f <<
330f1d2b4d3SLarry Finger 							(i * 8))) >> (i * 8));
331f1d2b4d3SLarry Finger 
332f1d2b4d3SLarry Finger 			if (pwr_val[i] > RF6052_MAX_TX_PWR)
333f1d2b4d3SLarry Finger 				pwr_val[i] = RF6052_MAX_TX_PWR;
334f1d2b4d3SLarry Finger 		}
335f1d2b4d3SLarry Finger 		writeVal = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
336f1d2b4d3SLarry Finger 		    (pwr_val[1] << 8) | pwr_val[0];
337f1d2b4d3SLarry Finger 
338f1d2b4d3SLarry Finger 		if (rf == 0)
339f1d2b4d3SLarry Finger 			regoffset = regoffset_a[index];
340f1d2b4d3SLarry Finger 		else
341f1d2b4d3SLarry Finger 			regoffset = regoffset_b[index];
342f1d2b4d3SLarry Finger 		rtl_set_bbreg(hw, regoffset, MASKDWORD, writeVal);
343f1d2b4d3SLarry Finger 
344f1d2b4d3SLarry Finger 		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
345f1d2b4d3SLarry Finger 			"Set 0x%x = %08x\n", regoffset, writeVal);
346f1d2b4d3SLarry Finger 
347f1d2b4d3SLarry Finger 		if (((get_rf_type(rtlphy) == RF_2T2R) &&
348f1d2b4d3SLarry Finger 		     (regoffset == RTXAGC_A_MCS15_MCS12 ||
349f1d2b4d3SLarry Finger 		      regoffset == RTXAGC_B_MCS15_MCS12)) ||
350f1d2b4d3SLarry Finger 		    ((get_rf_type(rtlphy) != RF_2T2R) &&
351f1d2b4d3SLarry Finger 		     (regoffset == RTXAGC_A_MCS07_MCS04 ||
352f1d2b4d3SLarry Finger 		      regoffset == RTXAGC_B_MCS07_MCS04))) {
353f1d2b4d3SLarry Finger 
354f1d2b4d3SLarry Finger 			writeVal = pwr_val[3];
355f1d2b4d3SLarry Finger 			if (regoffset == RTXAGC_A_MCS15_MCS12 ||
356f1d2b4d3SLarry Finger 			    regoffset == RTXAGC_A_MCS07_MCS04)
357f1d2b4d3SLarry Finger 				regoffset = 0xc90;
358f1d2b4d3SLarry Finger 			if (regoffset == RTXAGC_B_MCS15_MCS12 ||
359f1d2b4d3SLarry Finger 			    regoffset == RTXAGC_B_MCS07_MCS04)
360f1d2b4d3SLarry Finger 				regoffset = 0xc98;
361f1d2b4d3SLarry Finger 
362f1d2b4d3SLarry Finger 			for (i = 0; i < 3; i++) {
363f1d2b4d3SLarry Finger 				writeVal = (writeVal > 6) ? (writeVal - 6) : 0;
364f1d2b4d3SLarry Finger 				rtl_write_byte(rtlpriv, (u32) (regoffset + i),
365f1d2b4d3SLarry Finger 					       (u8) writeVal);
366f1d2b4d3SLarry Finger 			}
367f1d2b4d3SLarry Finger 		}
368f1d2b4d3SLarry Finger 	}
369f1d2b4d3SLarry Finger }
370f1d2b4d3SLarry Finger 
371f1d2b4d3SLarry Finger void rtl92ce_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
372f1d2b4d3SLarry Finger 					u8 *ppowerlevel, u8 channel)
373f1d2b4d3SLarry Finger {
374f1d2b4d3SLarry Finger 	u32 writeVal[2], powerBase0[2], powerBase1[2];
375f1d2b4d3SLarry Finger 	u8 index;
376f1d2b4d3SLarry Finger 
377f1d2b4d3SLarry Finger 	rtl92c_phy_get_power_base(hw, ppowerlevel,
378f1d2b4d3SLarry Finger 				  channel, &powerBase0[0], &powerBase1[0]);
379f1d2b4d3SLarry Finger 
380f1d2b4d3SLarry Finger 	for (index = 0; index < 6; index++) {
381f1d2b4d3SLarry Finger 		_rtl92c_get_txpower_writeval_by_regulatory(hw,
382f1d2b4d3SLarry Finger 							   channel, index,
383f1d2b4d3SLarry Finger 							   &powerBase0[0],
384f1d2b4d3SLarry Finger 							   &powerBase1[0],
385f1d2b4d3SLarry Finger 							   &writeVal[0]);
386f1d2b4d3SLarry Finger 
387f1d2b4d3SLarry Finger 		_rtl92c_write_ofdm_power_reg(hw, index, &writeVal[0]);
388f1d2b4d3SLarry Finger 	}
389f1d2b4d3SLarry Finger }
390f1d2b4d3SLarry Finger 
391f1d2b4d3SLarry Finger bool rtl92ce_phy_rf6052_config(struct ieee80211_hw *hw)
392f1d2b4d3SLarry Finger {
393f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
394f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
395f1d2b4d3SLarry Finger 
396f1d2b4d3SLarry Finger 	if (rtlphy->rf_type == RF_1T1R)
397f1d2b4d3SLarry Finger 		rtlphy->num_total_rfpath = 1;
398f1d2b4d3SLarry Finger 	else
399f1d2b4d3SLarry Finger 		rtlphy->num_total_rfpath = 2;
400f1d2b4d3SLarry Finger 
401f1d2b4d3SLarry Finger 	return _rtl92ce_phy_rf6052_config_parafile(hw);
402f1d2b4d3SLarry Finger 
403f1d2b4d3SLarry Finger }
404f1d2b4d3SLarry Finger 
405f1d2b4d3SLarry Finger static bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
406f1d2b4d3SLarry Finger {
407f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
408f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
409f1d2b4d3SLarry Finger 	u32 u4_regvalue = 0;
410f1d2b4d3SLarry Finger 	u8 rfpath;
411f1d2b4d3SLarry Finger 	bool rtstatus = true;
412f1d2b4d3SLarry Finger 	struct bb_reg_def *pphyreg;
413f1d2b4d3SLarry Finger 
414f1d2b4d3SLarry Finger 	for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
415f1d2b4d3SLarry Finger 
416f1d2b4d3SLarry Finger 		pphyreg = &rtlphy->phyreg_def[rfpath];
417f1d2b4d3SLarry Finger 
418f1d2b4d3SLarry Finger 		switch (rfpath) {
419f1d2b4d3SLarry Finger 		case RF90_PATH_A:
420f1d2b4d3SLarry Finger 		case RF90_PATH_C:
421f1d2b4d3SLarry Finger 			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
422f1d2b4d3SLarry Finger 						    BRFSI_RFENV);
423f1d2b4d3SLarry Finger 			break;
424f1d2b4d3SLarry Finger 		case RF90_PATH_B:
425f1d2b4d3SLarry Finger 		case RF90_PATH_D:
426f1d2b4d3SLarry Finger 			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
427f1d2b4d3SLarry Finger 						    BRFSI_RFENV << 16);
428f1d2b4d3SLarry Finger 			break;
429f1d2b4d3SLarry Finger 		}
430f1d2b4d3SLarry Finger 
431f1d2b4d3SLarry Finger 		rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
432f1d2b4d3SLarry Finger 		udelay(1);
433f1d2b4d3SLarry Finger 
434f1d2b4d3SLarry Finger 		rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
435f1d2b4d3SLarry Finger 		udelay(1);
436f1d2b4d3SLarry Finger 
437f1d2b4d3SLarry Finger 		rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
438f1d2b4d3SLarry Finger 			      B3WIREADDREAALENGTH, 0x0);
439f1d2b4d3SLarry Finger 		udelay(1);
440f1d2b4d3SLarry Finger 
441f1d2b4d3SLarry Finger 		rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
442f1d2b4d3SLarry Finger 		udelay(1);
443f1d2b4d3SLarry Finger 
444f1d2b4d3SLarry Finger 		switch (rfpath) {
445f1d2b4d3SLarry Finger 		case RF90_PATH_A:
446f1d2b4d3SLarry Finger 			rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
447f1d2b4d3SLarry Finger 						(enum radio_path)rfpath);
448f1d2b4d3SLarry Finger 			break;
449f1d2b4d3SLarry Finger 		case RF90_PATH_B:
450f1d2b4d3SLarry Finger 			rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
451f1d2b4d3SLarry Finger 						(enum radio_path)rfpath);
452f1d2b4d3SLarry Finger 			break;
453f1d2b4d3SLarry Finger 		case RF90_PATH_C:
454f1d2b4d3SLarry Finger 			break;
455f1d2b4d3SLarry Finger 		case RF90_PATH_D:
456f1d2b4d3SLarry Finger 			break;
457f1d2b4d3SLarry Finger 		}
458f1d2b4d3SLarry Finger 
459f1d2b4d3SLarry Finger 		switch (rfpath) {
460f1d2b4d3SLarry Finger 		case RF90_PATH_A:
461f1d2b4d3SLarry Finger 		case RF90_PATH_C:
462f1d2b4d3SLarry Finger 			rtl_set_bbreg(hw, pphyreg->rfintfs,
463f1d2b4d3SLarry Finger 				      BRFSI_RFENV, u4_regvalue);
464f1d2b4d3SLarry Finger 			break;
465f1d2b4d3SLarry Finger 		case RF90_PATH_B:
466f1d2b4d3SLarry Finger 		case RF90_PATH_D:
467f1d2b4d3SLarry Finger 			rtl_set_bbreg(hw, pphyreg->rfintfs,
468f1d2b4d3SLarry Finger 				      BRFSI_RFENV << 16, u4_regvalue);
469f1d2b4d3SLarry Finger 			break;
470f1d2b4d3SLarry Finger 		}
471f1d2b4d3SLarry Finger 
472f1d2b4d3SLarry Finger 		if (!rtstatus) {
473f1d2b4d3SLarry Finger 			RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
474f1d2b4d3SLarry Finger 				 "Radio[%d] Fail!!\n", rfpath);
475f1d2b4d3SLarry Finger 			return false;
476f1d2b4d3SLarry Finger 		}
477f1d2b4d3SLarry Finger 
478f1d2b4d3SLarry Finger 	}
479f1d2b4d3SLarry Finger 
480f1d2b4d3SLarry Finger 	RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "<---\n");
481f1d2b4d3SLarry Finger 	return rtstatus;
482f1d2b4d3SLarry Finger }
483