18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2014  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "../wifi.h"
58c2ecf20Sopenharmony_ci#include "reg.h"
68c2ecf20Sopenharmony_ci#include "def.h"
78c2ecf20Sopenharmony_ci#include "phy.h"
88c2ecf20Sopenharmony_ci#include "rf.h"
98c2ecf20Sopenharmony_ci#include "dm.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civoid rtl8723be_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
168c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	switch (bandwidth) {
198c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
208c2ecf20Sopenharmony_ci		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
218c2ecf20Sopenharmony_ci					     0xfffff3ff) | BIT(10) | BIT(11));
228c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
238c2ecf20Sopenharmony_ci			      rtlphy->rfreg_chnlval[0]);
248c2ecf20Sopenharmony_ci		break;
258c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
268c2ecf20Sopenharmony_ci		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
278c2ecf20Sopenharmony_ci					     0xfffff3ff) | BIT(10));
288c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
298c2ecf20Sopenharmony_ci			      rtlphy->rfreg_chnlval[0]);
308c2ecf20Sopenharmony_ci		break;
318c2ecf20Sopenharmony_ci	default:
328c2ecf20Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n", bandwidth);
338c2ecf20Sopenharmony_ci		break;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_civoid rtl8723be_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
388c2ecf20Sopenharmony_ci					  u8 *ppowerlevel)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
418c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
428c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
438c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
448c2ecf20Sopenharmony_ci	u32 tx_agc[2] = {0, 0}, tmpval;
458c2ecf20Sopenharmony_ci	bool turbo_scanoff = false;
468c2ecf20Sopenharmony_ci	u8 idx1, idx2;
478c2ecf20Sopenharmony_ci	u8 *ptr;
488c2ecf20Sopenharmony_ci	u8 direction;
498c2ecf20Sopenharmony_ci	u32 pwrtrac_value;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	if (rtlefuse->eeprom_regulatory != 0)
528c2ecf20Sopenharmony_ci		turbo_scanoff = true;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (mac->act_scanning) {
558c2ecf20Sopenharmony_ci		tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
568c2ecf20Sopenharmony_ci		tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		if (turbo_scanoff) {
598c2ecf20Sopenharmony_ci			for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
608c2ecf20Sopenharmony_ci				tx_agc[idx1] = ppowerlevel[idx1] |
618c2ecf20Sopenharmony_ci					       (ppowerlevel[idx1] << 8) |
628c2ecf20Sopenharmony_ci					       (ppowerlevel[idx1] << 16) |
638c2ecf20Sopenharmony_ci					       (ppowerlevel[idx1] << 24);
648c2ecf20Sopenharmony_ci			}
658c2ecf20Sopenharmony_ci		}
668c2ecf20Sopenharmony_ci	} else {
678c2ecf20Sopenharmony_ci		for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
688c2ecf20Sopenharmony_ci			tx_agc[idx1] = ppowerlevel[idx1] |
698c2ecf20Sopenharmony_ci				       (ppowerlevel[idx1] << 8) |
708c2ecf20Sopenharmony_ci				       (ppowerlevel[idx1] << 16) |
718c2ecf20Sopenharmony_ci				       (ppowerlevel[idx1] << 24);
728c2ecf20Sopenharmony_ci		}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		if (rtlefuse->eeprom_regulatory == 0) {
758c2ecf20Sopenharmony_ci			tmpval =
768c2ecf20Sopenharmony_ci			    (rtlphy->mcs_txpwrlevel_origoffset[0][6]) +
778c2ecf20Sopenharmony_ci			    (rtlphy->mcs_txpwrlevel_origoffset[0][7] << 8);
788c2ecf20Sopenharmony_ci			tx_agc[RF90_PATH_A] += tmpval;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci			tmpval = (rtlphy->mcs_txpwrlevel_origoffset[0][14]) +
818c2ecf20Sopenharmony_ci				 (rtlphy->mcs_txpwrlevel_origoffset[0][15] <<
828c2ecf20Sopenharmony_ci				  24);
838c2ecf20Sopenharmony_ci			tx_agc[RF90_PATH_B] += tmpval;
848c2ecf20Sopenharmony_ci		}
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
888c2ecf20Sopenharmony_ci		ptr = (u8 *)(&(tx_agc[idx1]));
898c2ecf20Sopenharmony_ci		for (idx2 = 0; idx2 < 4; idx2++) {
908c2ecf20Sopenharmony_ci			if (*ptr > RF6052_MAX_TX_PWR)
918c2ecf20Sopenharmony_ci				*ptr = RF6052_MAX_TX_PWR;
928c2ecf20Sopenharmony_ci			ptr++;
938c2ecf20Sopenharmony_ci		}
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci	rtl8723be_dm_txpower_track_adjust(hw, 1, &direction, &pwrtrac_value);
968c2ecf20Sopenharmony_ci	if (direction == 1) {
978c2ecf20Sopenharmony_ci		tx_agc[0] += pwrtrac_value;
988c2ecf20Sopenharmony_ci		tx_agc[1] += pwrtrac_value;
998c2ecf20Sopenharmony_ci	} else if (direction == 2) {
1008c2ecf20Sopenharmony_ci		tx_agc[0] -= pwrtrac_value;
1018c2ecf20Sopenharmony_ci		tx_agc[1] -= pwrtrac_value;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_A] & 0xff;
1048c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_A_CCK1_MCS32, MASKBYTE1, tmpval);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1078c2ecf20Sopenharmony_ci		"CCK PWR 1M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
1088c2ecf20Sopenharmony_ci		 RTXAGC_A_CCK1_MCS32);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_A] >> 8;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/*tmpval = tmpval & 0xff00ffff;*/
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1178c2ecf20Sopenharmony_ci		"CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n", tmpval,
1188c2ecf20Sopenharmony_ci		 RTXAGC_B_CCK11_A_CCK2_11);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_B] >> 24;
1218c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1248c2ecf20Sopenharmony_ci		"CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
1258c2ecf20Sopenharmony_ci		 RTXAGC_B_CCK11_A_CCK2_11);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
1288c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1318c2ecf20Sopenharmony_ci		"CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n", tmpval,
1328c2ecf20Sopenharmony_ci		 RTXAGC_B_CCK1_55_MCS32);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic void rtl8723be_phy_get_power_base(struct ieee80211_hw *hw,
1368c2ecf20Sopenharmony_ci					 u8 *ppowerlevel_ofdm,
1378c2ecf20Sopenharmony_ci					 u8 *ppowerlevel_bw20,
1388c2ecf20Sopenharmony_ci					 u8 *ppowerlevel_bw40,
1398c2ecf20Sopenharmony_ci					 u8 channel, u32 *ofdmbase,
1408c2ecf20Sopenharmony_ci					 u32 *mcsbase)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1438c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1448c2ecf20Sopenharmony_ci	u32 powerbase0, powerbase1;
1458c2ecf20Sopenharmony_ci	u8 i, powerlevel[2];
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1488c2ecf20Sopenharmony_ci		powerbase0 = ppowerlevel_ofdm[i];
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		powerbase0 = (powerbase0 << 24) | (powerbase0 << 16) |
1518c2ecf20Sopenharmony_ci		    (powerbase0 << 8) | powerbase0;
1528c2ecf20Sopenharmony_ci		*(ofdmbase + i) = powerbase0;
1538c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1548c2ecf20Sopenharmony_ci			" [OFDM power base index rf(%c) = 0x%x]\n",
1558c2ecf20Sopenharmony_ci			 ((i == 0) ? 'A' : 'B'), *(ofdmbase + i));
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1598c2ecf20Sopenharmony_ci		if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20)
1608c2ecf20Sopenharmony_ci			powerlevel[i] = ppowerlevel_bw20[i];
1618c2ecf20Sopenharmony_ci		else
1628c2ecf20Sopenharmony_ci			powerlevel[i] = ppowerlevel_bw40[i];
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		powerbase1 = powerlevel[i];
1658c2ecf20Sopenharmony_ci		powerbase1 = (powerbase1 << 24) | (powerbase1 << 16) |
1668c2ecf20Sopenharmony_ci			     (powerbase1 << 8) | powerbase1;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		*(mcsbase + i) = powerbase1;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
1718c2ecf20Sopenharmony_ci			" [MCS power base index rf(%c) = 0x%x]\n",
1728c2ecf20Sopenharmony_ci			 ((i == 0) ? 'A' : 'B'), *(mcsbase + i));
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void _rtl8723be_get_txpower_writeval_by_regulatory(
1778c2ecf20Sopenharmony_ci							struct ieee80211_hw *hw,
1788c2ecf20Sopenharmony_ci							u8 channel, u8 index,
1798c2ecf20Sopenharmony_ci							u32 *powerbase0,
1808c2ecf20Sopenharmony_ci							u32 *powerbase1,
1818c2ecf20Sopenharmony_ci							u32 *p_outwriteval)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1848c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1858c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1868c2ecf20Sopenharmony_ci	u8 i, chnlgroup = 0, pwr_diff_limit[4], pwr_diff = 0, customer_pwr_diff;
1878c2ecf20Sopenharmony_ci	u32 writeval, customer_limit, rf;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	for (rf = 0; rf < 2; rf++) {
1908c2ecf20Sopenharmony_ci		switch (rtlefuse->eeprom_regulatory) {
1918c2ecf20Sopenharmony_ci		case 0:
1928c2ecf20Sopenharmony_ci			chnlgroup = 0;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci			writeval =
1958c2ecf20Sopenharmony_ci			    rtlphy->mcs_txpwrlevel_origoffset[chnlgroup][index +
1968c2ecf20Sopenharmony_ci								(rf ? 8 : 0)]
1978c2ecf20Sopenharmony_ci			    + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2008c2ecf20Sopenharmony_ci				"RTK better performance, writeval(%c) = 0x%x\n",
2018c2ecf20Sopenharmony_ci				((rf == 0) ? 'A' : 'B'), writeval);
2028c2ecf20Sopenharmony_ci			break;
2038c2ecf20Sopenharmony_ci		case 1:
2048c2ecf20Sopenharmony_ci			if (rtlphy->pwrgroup_cnt == 1) {
2058c2ecf20Sopenharmony_ci				chnlgroup = 0;
2068c2ecf20Sopenharmony_ci			} else {
2078c2ecf20Sopenharmony_ci				if (channel < 3)
2088c2ecf20Sopenharmony_ci					chnlgroup = 0;
2098c2ecf20Sopenharmony_ci				else if (channel < 6)
2108c2ecf20Sopenharmony_ci					chnlgroup = 1;
2118c2ecf20Sopenharmony_ci				else if (channel < 9)
2128c2ecf20Sopenharmony_ci					chnlgroup = 2;
2138c2ecf20Sopenharmony_ci				else if (channel < 12)
2148c2ecf20Sopenharmony_ci					chnlgroup = 3;
2158c2ecf20Sopenharmony_ci				else if (channel < 14)
2168c2ecf20Sopenharmony_ci					chnlgroup = 4;
2178c2ecf20Sopenharmony_ci				else if (channel == 14)
2188c2ecf20Sopenharmony_ci					chnlgroup = 5;
2198c2ecf20Sopenharmony_ci			}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci			writeval =
2228c2ecf20Sopenharmony_ci			    rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
2238c2ecf20Sopenharmony_ci			    [index + (rf ? 8 : 0)] + ((index < 2) ?
2248c2ecf20Sopenharmony_ci						      powerbase0[rf] :
2258c2ecf20Sopenharmony_ci						      powerbase1[rf]);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2288c2ecf20Sopenharmony_ci				"Realtek regulatory, 20MHz, writeval(%c) = 0x%x\n",
2298c2ecf20Sopenharmony_ci				((rf == 0) ? 'A' : 'B'), writeval);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci			break;
2328c2ecf20Sopenharmony_ci		case 2:
2338c2ecf20Sopenharmony_ci			writeval =
2348c2ecf20Sopenharmony_ci			    ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2378c2ecf20Sopenharmony_ci				"Better regulatory, writeval(%c) = 0x%x\n",
2388c2ecf20Sopenharmony_ci				((rf == 0) ? 'A' : 'B'), writeval);
2398c2ecf20Sopenharmony_ci			break;
2408c2ecf20Sopenharmony_ci		case 3:
2418c2ecf20Sopenharmony_ci			chnlgroup = 0;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
2448c2ecf20Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2458c2ecf20Sopenharmony_ci					"customer's limit, 40MHz rf(%c) = 0x%x\n",
2468c2ecf20Sopenharmony_ci					((rf == 0) ? 'A' : 'B'),
2478c2ecf20Sopenharmony_ci					rtlefuse->pwrgroup_ht40
2488c2ecf20Sopenharmony_ci					[rf][channel - 1]);
2498c2ecf20Sopenharmony_ci			} else {
2508c2ecf20Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2518c2ecf20Sopenharmony_ci					"customer's limit, 20MHz rf(%c) = 0x%x\n",
2528c2ecf20Sopenharmony_ci					((rf == 0) ? 'A' : 'B'),
2538c2ecf20Sopenharmony_ci					rtlefuse->pwrgroup_ht20
2548c2ecf20Sopenharmony_ci					[rf][channel - 1]);
2558c2ecf20Sopenharmony_ci			}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci			if (index < 2)
2588c2ecf20Sopenharmony_ci				pwr_diff =
2598c2ecf20Sopenharmony_ci				    rtlefuse->txpwr_legacyhtdiff[rf][channel-1];
2608c2ecf20Sopenharmony_ci			else if (rtlphy->current_chan_bw ==
2618c2ecf20Sopenharmony_ci				 HT_CHANNEL_WIDTH_20)
2628c2ecf20Sopenharmony_ci				pwr_diff =
2638c2ecf20Sopenharmony_ci				    rtlefuse->txpwr_ht20diff[rf][channel-1];
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40)
2668c2ecf20Sopenharmony_ci				customer_pwr_diff =
2678c2ecf20Sopenharmony_ci					rtlefuse->pwrgroup_ht40[rf][channel-1];
2688c2ecf20Sopenharmony_ci			else
2698c2ecf20Sopenharmony_ci				customer_pwr_diff =
2708c2ecf20Sopenharmony_ci					rtlefuse->pwrgroup_ht20[rf][channel-1];
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci			if (pwr_diff > customer_pwr_diff)
2738c2ecf20Sopenharmony_ci				pwr_diff = 0;
2748c2ecf20Sopenharmony_ci			else
2758c2ecf20Sopenharmony_ci				pwr_diff = customer_pwr_diff - pwr_diff;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci			for (i = 0; i < 4; i++) {
2788c2ecf20Sopenharmony_ci				pwr_diff_limit[i] =
2798c2ecf20Sopenharmony_ci				    (u8)((rtlphy->mcs_txpwrlevel_origoffset
2808c2ecf20Sopenharmony_ci					   [chnlgroup][index + (rf ? 8 : 0)] &
2818c2ecf20Sopenharmony_ci					      (0x7f << (i * 8))) >> (i * 8));
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci				if (pwr_diff_limit[i] > pwr_diff)
2848c2ecf20Sopenharmony_ci					pwr_diff_limit[i] = pwr_diff;
2858c2ecf20Sopenharmony_ci			}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci			customer_limit = (pwr_diff_limit[3] << 24) |
2888c2ecf20Sopenharmony_ci					 (pwr_diff_limit[2] << 16) |
2898c2ecf20Sopenharmony_ci					 (pwr_diff_limit[1] << 8) |
2908c2ecf20Sopenharmony_ci					 (pwr_diff_limit[0]);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
2938c2ecf20Sopenharmony_ci				"Customer's limit rf(%c) = 0x%x\n",
2948c2ecf20Sopenharmony_ci				 ((rf == 0) ? 'A' : 'B'), customer_limit);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci			writeval = customer_limit + ((index < 2) ?
2978c2ecf20Sopenharmony_ci						      powerbase0[rf] :
2988c2ecf20Sopenharmony_ci						      powerbase1[rf]);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
3018c2ecf20Sopenharmony_ci				"Customer, writeval rf(%c)= 0x%x\n",
3028c2ecf20Sopenharmony_ci				 ((rf == 0) ? 'A' : 'B'), writeval);
3038c2ecf20Sopenharmony_ci			break;
3048c2ecf20Sopenharmony_ci		default:
3058c2ecf20Sopenharmony_ci			chnlgroup = 0;
3068c2ecf20Sopenharmony_ci			writeval =
3078c2ecf20Sopenharmony_ci			    rtlphy->mcs_txpwrlevel_origoffset[chnlgroup]
3088c2ecf20Sopenharmony_ci			    [index + (rf ? 8 : 0)]
3098c2ecf20Sopenharmony_ci			    + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
3128c2ecf20Sopenharmony_ci				"RTK better performance, writeval rf(%c) = 0x%x\n",
3138c2ecf20Sopenharmony_ci				((rf == 0) ? 'A' : 'B'), writeval);
3148c2ecf20Sopenharmony_ci			break;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
3188c2ecf20Sopenharmony_ci			writeval = writeval - 0x06060606;
3198c2ecf20Sopenharmony_ci		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
3208c2ecf20Sopenharmony_ci			 TXHIGHPWRLEVEL_BT2)
3218c2ecf20Sopenharmony_ci			writeval = writeval - 0x0c0c0c0c;
3228c2ecf20Sopenharmony_ci		*(p_outwriteval + rf) = writeval;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic void _rtl8723be_write_ofdm_power_reg(struct ieee80211_hw *hw,
3278c2ecf20Sopenharmony_ci					 u8 index, u32 *pvalue)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3308c2ecf20Sopenharmony_ci	u16 regoffset_a[6] = {
3318c2ecf20Sopenharmony_ci		RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
3328c2ecf20Sopenharmony_ci		RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
3338c2ecf20Sopenharmony_ci		RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
3348c2ecf20Sopenharmony_ci	};
3358c2ecf20Sopenharmony_ci	u16 regoffset_b[6] = {
3368c2ecf20Sopenharmony_ci		RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
3378c2ecf20Sopenharmony_ci		RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
3388c2ecf20Sopenharmony_ci		RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
3398c2ecf20Sopenharmony_ci	};
3408c2ecf20Sopenharmony_ci	u8 i, rf, pwr_val[4];
3418c2ecf20Sopenharmony_ci	u32 writeval;
3428c2ecf20Sopenharmony_ci	u16 regoffset;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	for (rf = 0; rf < 2; rf++) {
3458c2ecf20Sopenharmony_ci		writeval = pvalue[rf];
3468c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++) {
3478c2ecf20Sopenharmony_ci			pwr_val[i] = (u8)((writeval & (0x7f <<
3488c2ecf20Sopenharmony_ci							(i * 8))) >> (i * 8));
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci			if (pwr_val[i] > RF6052_MAX_TX_PWR)
3518c2ecf20Sopenharmony_ci				pwr_val[i] = RF6052_MAX_TX_PWR;
3528c2ecf20Sopenharmony_ci		}
3538c2ecf20Sopenharmony_ci		writeval = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
3548c2ecf20Sopenharmony_ci		    (pwr_val[1] << 8) | pwr_val[0];
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci		if (rf == 0)
3578c2ecf20Sopenharmony_ci			regoffset = regoffset_a[index];
3588c2ecf20Sopenharmony_ci		else
3598c2ecf20Sopenharmony_ci			regoffset = regoffset_b[index];
3608c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, regoffset, MASKDWORD, writeval);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
3638c2ecf20Sopenharmony_ci			"Set 0x%x = %08x\n", regoffset, writeval);
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_civoid rtl8723be_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
3688c2ecf20Sopenharmony_ci					   u8 *ppowerlevel_ofdm,
3698c2ecf20Sopenharmony_ci					   u8 *ppowerlevel_bw20,
3708c2ecf20Sopenharmony_ci					   u8 *ppowerlevel_bw40, u8 channel)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	u32 writeval[2], powerbase0[2], powerbase1[2];
3738c2ecf20Sopenharmony_ci	u8 index;
3748c2ecf20Sopenharmony_ci	u8 direction;
3758c2ecf20Sopenharmony_ci	u32 pwrtrac_value;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	rtl8723be_phy_get_power_base(hw, ppowerlevel_ofdm, ppowerlevel_bw20,
3788c2ecf20Sopenharmony_ci				     ppowerlevel_bw40, channel,
3798c2ecf20Sopenharmony_ci				     &powerbase0[0], &powerbase1[0]);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	rtl8723be_dm_txpower_track_adjust(hw, 1, &direction, &pwrtrac_value);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	for (index = 0; index < 6; index++) {
3848c2ecf20Sopenharmony_ci		_rtl8723be_get_txpower_writeval_by_regulatory(hw,
3858c2ecf20Sopenharmony_ci							      channel, index,
3868c2ecf20Sopenharmony_ci							      &powerbase0[0],
3878c2ecf20Sopenharmony_ci							      &powerbase1[0],
3888c2ecf20Sopenharmony_ci							      &writeval[0]);
3898c2ecf20Sopenharmony_ci		if (direction == 1) {
3908c2ecf20Sopenharmony_ci			writeval[0] += pwrtrac_value;
3918c2ecf20Sopenharmony_ci			writeval[1] += pwrtrac_value;
3928c2ecf20Sopenharmony_ci		} else if (direction == 2) {
3938c2ecf20Sopenharmony_ci			writeval[0] -= pwrtrac_value;
3948c2ecf20Sopenharmony_ci			writeval[1] -= pwrtrac_value;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci		_rtl8723be_write_ofdm_power_reg(hw, index, &writeval[0]);
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cibool rtl8723be_phy_rf6052_config(struct ieee80211_hw *hw)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4038c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if (rtlphy->rf_type == RF_1T1R)
4068c2ecf20Sopenharmony_ci		rtlphy->num_total_rfpath = 1;
4078c2ecf20Sopenharmony_ci	else
4088c2ecf20Sopenharmony_ci		rtlphy->num_total_rfpath = 2;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	return _rtl8723be_phy_rf6052_config_parafile(hw);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4178c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
4188c2ecf20Sopenharmony_ci	u32 u4_regvalue = 0;
4198c2ecf20Sopenharmony_ci	u8 rfpath;
4208c2ecf20Sopenharmony_ci	bool rtstatus = true;
4218c2ecf20Sopenharmony_ci	struct bb_reg_def *pphyreg;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
4248c2ecf20Sopenharmony_ci		pphyreg = &rtlphy->phyreg_def[rfpath];
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci		switch (rfpath) {
4278c2ecf20Sopenharmony_ci		case RF90_PATH_A:
4288c2ecf20Sopenharmony_ci		case RF90_PATH_C:
4298c2ecf20Sopenharmony_ci			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
4308c2ecf20Sopenharmony_ci						    BRFSI_RFENV);
4318c2ecf20Sopenharmony_ci			break;
4328c2ecf20Sopenharmony_ci		case RF90_PATH_B:
4338c2ecf20Sopenharmony_ci		case RF90_PATH_D:
4348c2ecf20Sopenharmony_ci			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
4358c2ecf20Sopenharmony_ci						    BRFSI_RFENV << 16);
4368c2ecf20Sopenharmony_ci			break;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
4408c2ecf20Sopenharmony_ci		udelay(1);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
4438c2ecf20Sopenharmony_ci		udelay(1);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
4468c2ecf20Sopenharmony_ci			      B3WIREADDREAALENGTH, 0x0);
4478c2ecf20Sopenharmony_ci		udelay(1);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
4508c2ecf20Sopenharmony_ci		udelay(1);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		switch (rfpath) {
4538c2ecf20Sopenharmony_ci		case RF90_PATH_A:
4548c2ecf20Sopenharmony_ci			rtstatus = rtl8723be_phy_config_rf_with_headerfile(hw,
4558c2ecf20Sopenharmony_ci						      (enum radio_path)rfpath);
4568c2ecf20Sopenharmony_ci			break;
4578c2ecf20Sopenharmony_ci		case RF90_PATH_B:
4588c2ecf20Sopenharmony_ci			rtstatus = rtl8723be_phy_config_rf_with_headerfile(hw,
4598c2ecf20Sopenharmony_ci						      (enum radio_path)rfpath);
4608c2ecf20Sopenharmony_ci			break;
4618c2ecf20Sopenharmony_ci		case RF90_PATH_C:
4628c2ecf20Sopenharmony_ci			break;
4638c2ecf20Sopenharmony_ci		case RF90_PATH_D:
4648c2ecf20Sopenharmony_ci			break;
4658c2ecf20Sopenharmony_ci		}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci		switch (rfpath) {
4688c2ecf20Sopenharmony_ci		case RF90_PATH_A:
4698c2ecf20Sopenharmony_ci		case RF90_PATH_C:
4708c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, pphyreg->rfintfs,
4718c2ecf20Sopenharmony_ci				      BRFSI_RFENV, u4_regvalue);
4728c2ecf20Sopenharmony_ci			break;
4738c2ecf20Sopenharmony_ci		case RF90_PATH_B:
4748c2ecf20Sopenharmony_ci		case RF90_PATH_D:
4758c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, pphyreg->rfintfs,
4768c2ecf20Sopenharmony_ci				      BRFSI_RFENV << 16, u4_regvalue);
4778c2ecf20Sopenharmony_ci			break;
4788c2ecf20Sopenharmony_ci		}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci		if (!rtstatus) {
4818c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
4828c2ecf20Sopenharmony_ci				"Radio[%d] Fail!!\n", rfpath);
4838c2ecf20Sopenharmony_ci			return false;
4848c2ecf20Sopenharmony_ci		}
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "\n");
4888c2ecf20Sopenharmony_ci	return rtstatus;
4898c2ecf20Sopenharmony_ci}
490