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