162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 2009-2012  Realtek Corporation.*/
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "../wifi.h"
562306a36Sopenharmony_ci#include "reg.h"
662306a36Sopenharmony_ci#include "def.h"
762306a36Sopenharmony_ci#include "phy.h"
862306a36Sopenharmony_ci#include "rf.h"
962306a36Sopenharmony_ci#include "dm.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistatic bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw);
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_civoid rtl92ce_phy_rf6052_set_bandwidth(struct ieee80211_hw *hw, u8 bandwidth)
1462306a36Sopenharmony_ci{
1562306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1662306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci	switch (bandwidth) {
1962306a36Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
2062306a36Sopenharmony_ci		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
2162306a36Sopenharmony_ci					     0xfffff3ff) | 0x0400);
2262306a36Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
2362306a36Sopenharmony_ci			      rtlphy->rfreg_chnlval[0]);
2462306a36Sopenharmony_ci		break;
2562306a36Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
2662306a36Sopenharmony_ci		rtlphy->rfreg_chnlval[0] = ((rtlphy->rfreg_chnlval[0] &
2762306a36Sopenharmony_ci					     0xfffff3ff));
2862306a36Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, RF_CHNLBW, RFREG_OFFSET_MASK,
2962306a36Sopenharmony_ci			      rtlphy->rfreg_chnlval[0]);
3062306a36Sopenharmony_ci		break;
3162306a36Sopenharmony_ci	default:
3262306a36Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n", bandwidth);
3362306a36Sopenharmony_ci		break;
3462306a36Sopenharmony_ci	}
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_civoid rtl92ce_phy_rf6052_set_cck_txpower(struct ieee80211_hw *hw,
3862306a36Sopenharmony_ci					u8 *ppowerlevel)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4162306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
4262306a36Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
4362306a36Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
4462306a36Sopenharmony_ci	u32 tx_agc[2] = {0, 0}, tmpval;
4562306a36Sopenharmony_ci	bool turbo_scanoff = false;
4662306a36Sopenharmony_ci	u8 idx1, idx2;
4762306a36Sopenharmony_ci	u8 *ptr;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	if (rtlefuse->eeprom_regulatory != 0)
5062306a36Sopenharmony_ci		turbo_scanoff = true;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	if (mac->act_scanning) {
5362306a36Sopenharmony_ci		tx_agc[RF90_PATH_A] = 0x3f3f3f3f;
5462306a36Sopenharmony_ci		tx_agc[RF90_PATH_B] = 0x3f3f3f3f;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci		if (turbo_scanoff) {
5762306a36Sopenharmony_ci			for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
5862306a36Sopenharmony_ci				tx_agc[idx1] = ppowerlevel[idx1] |
5962306a36Sopenharmony_ci				    (ppowerlevel[idx1] << 8) |
6062306a36Sopenharmony_ci				    (ppowerlevel[idx1] << 16) |
6162306a36Sopenharmony_ci				    (ppowerlevel[idx1] << 24);
6262306a36Sopenharmony_ci			}
6362306a36Sopenharmony_ci		}
6462306a36Sopenharmony_ci	} else {
6562306a36Sopenharmony_ci		for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
6662306a36Sopenharmony_ci			tx_agc[idx1] = ppowerlevel[idx1] |
6762306a36Sopenharmony_ci			    (ppowerlevel[idx1] << 8) |
6862306a36Sopenharmony_ci			    (ppowerlevel[idx1] << 16) |
6962306a36Sopenharmony_ci			    (ppowerlevel[idx1] << 24);
7062306a36Sopenharmony_ci		}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci		if (rtlefuse->eeprom_regulatory == 0) {
7362306a36Sopenharmony_ci			tmpval = (rtlphy->mcs_offset[0][6]) +
7462306a36Sopenharmony_ci			    (rtlphy->mcs_offset[0][7] << 8);
7562306a36Sopenharmony_ci			tx_agc[RF90_PATH_A] += tmpval;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci			tmpval = (rtlphy->mcs_offset[0][14]) +
7862306a36Sopenharmony_ci				 (rtlphy->mcs_offset[0][15] << 24);
7962306a36Sopenharmony_ci			tx_agc[RF90_PATH_B] += tmpval;
8062306a36Sopenharmony_ci		}
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	for (idx1 = RF90_PATH_A; idx1 <= RF90_PATH_B; idx1++) {
8462306a36Sopenharmony_ci		ptr = (u8 *) (&(tx_agc[idx1]));
8562306a36Sopenharmony_ci		for (idx2 = 0; idx2 < 4; idx2++) {
8662306a36Sopenharmony_ci			if (*ptr > RF6052_MAX_TX_PWR)
8762306a36Sopenharmony_ci				*ptr = RF6052_MAX_TX_PWR;
8862306a36Sopenharmony_ci			ptr++;
8962306a36Sopenharmony_ci		}
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_A] & 0xff;
9362306a36Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_A_CCK1_MCS32, MASKBYTE1, tmpval);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
9662306a36Sopenharmony_ci		"CCK PWR 1M (rf-A) = 0x%x (reg 0x%x)\n",
9762306a36Sopenharmony_ci		tmpval, RTXAGC_A_CCK1_MCS32);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_A] >> 8;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	tmpval = tmpval & 0xff00ffff;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, 0xffffff00, tmpval);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
10662306a36Sopenharmony_ci		"CCK PWR 2~11M (rf-A) = 0x%x (reg 0x%x)\n",
10762306a36Sopenharmony_ci		tmpval, RTXAGC_B_CCK11_A_CCK2_11);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_B] >> 24;
11062306a36Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK11_A_CCK2_11, MASKBYTE0, tmpval);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
11362306a36Sopenharmony_ci		"CCK PWR 11M (rf-B) = 0x%x (reg 0x%x)\n",
11462306a36Sopenharmony_ci		tmpval, RTXAGC_B_CCK11_A_CCK2_11);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	tmpval = tx_agc[RF90_PATH_B] & 0x00ffffff;
11762306a36Sopenharmony_ci	rtl_set_bbreg(hw, RTXAGC_B_CCK1_55_MCS32, 0xffffff00, tmpval);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
12062306a36Sopenharmony_ci		"CCK PWR 1~5.5M (rf-B) = 0x%x (reg 0x%x)\n",
12162306a36Sopenharmony_ci		tmpval, RTXAGC_B_CCK1_55_MCS32);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic void rtl92c_phy_get_power_base(struct ieee80211_hw *hw,
12562306a36Sopenharmony_ci				      u8 *ppowerlevel, u8 channel,
12662306a36Sopenharmony_ci				      u32 *ofdmbase, u32 *mcsbase)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12962306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
13062306a36Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
13162306a36Sopenharmony_ci	u32 powerbase0, powerbase1;
13262306a36Sopenharmony_ci	u8 legacy_pwrdiff, ht20_pwrdiff;
13362306a36Sopenharmony_ci	u8 i, powerlevel[2];
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	for (i = 0; i < 2; i++) {
13662306a36Sopenharmony_ci		powerlevel[i] = ppowerlevel[i];
13762306a36Sopenharmony_ci		legacy_pwrdiff = rtlefuse->txpwr_legacyhtdiff[i][channel - 1];
13862306a36Sopenharmony_ci		powerbase0 = powerlevel[i] + legacy_pwrdiff;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		powerbase0 = (powerbase0 << 24) | (powerbase0 << 16) |
14162306a36Sopenharmony_ci		    (powerbase0 << 8) | powerbase0;
14262306a36Sopenharmony_ci		*(ofdmbase + i) = powerbase0;
14362306a36Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
14462306a36Sopenharmony_ci			" [OFDM power base index rf(%c) = 0x%x]\n",
14562306a36Sopenharmony_ci			i == 0 ? 'A' : 'B', *(ofdmbase + i));
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	for (i = 0; i < 2; i++) {
14962306a36Sopenharmony_ci		if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20) {
15062306a36Sopenharmony_ci			ht20_pwrdiff = rtlefuse->txpwr_ht20diff[i][channel - 1];
15162306a36Sopenharmony_ci			powerlevel[i] += ht20_pwrdiff;
15262306a36Sopenharmony_ci		}
15362306a36Sopenharmony_ci		powerbase1 = powerlevel[i];
15462306a36Sopenharmony_ci		powerbase1 = (powerbase1 << 24) |
15562306a36Sopenharmony_ci		    (powerbase1 << 16) | (powerbase1 << 8) | powerbase1;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci		*(mcsbase + i) = powerbase1;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
16062306a36Sopenharmony_ci			" [MCS power base index rf(%c) = 0x%x]\n",
16162306a36Sopenharmony_ci			i == 0 ? 'A' : 'B', *(mcsbase + i));
16262306a36Sopenharmony_ci	}
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic void _rtl92c_get_txpower_writeval_by_regulatory(struct ieee80211_hw *hw,
16662306a36Sopenharmony_ci						       u8 channel, u8 index,
16762306a36Sopenharmony_ci						       u32 *powerbase0,
16862306a36Sopenharmony_ci						       u32 *powerbase1,
16962306a36Sopenharmony_ci						       u32 *p_outwriteval)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
17262306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
17362306a36Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
17462306a36Sopenharmony_ci	u8 i, chnlgroup = 0, pwr_diff_limit[4];
17562306a36Sopenharmony_ci	u32 writeval, customer_limit, rf;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	for (rf = 0; rf < 2; rf++) {
17862306a36Sopenharmony_ci		switch (rtlefuse->eeprom_regulatory) {
17962306a36Sopenharmony_ci		case 0:
18062306a36Sopenharmony_ci			chnlgroup = 0;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci			writeval = rtlphy->mcs_offset[chnlgroup][index +
18362306a36Sopenharmony_ci			    (rf ? 8 : 0)]
18462306a36Sopenharmony_ci			    + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
18762306a36Sopenharmony_ci				"RTK better performance, writeval(%c) = 0x%x\n",
18862306a36Sopenharmony_ci				rf == 0 ? 'A' : 'B', writeval);
18962306a36Sopenharmony_ci			break;
19062306a36Sopenharmony_ci		case 1:
19162306a36Sopenharmony_ci			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
19262306a36Sopenharmony_ci				writeval = ((index < 2) ? powerbase0[rf] :
19362306a36Sopenharmony_ci					    powerbase1[rf]);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
19662306a36Sopenharmony_ci					"Realtek regulatory, 40MHz, writeval(%c) = 0x%x\n",
19762306a36Sopenharmony_ci					rf == 0 ? 'A' : 'B', writeval);
19862306a36Sopenharmony_ci			} else {
19962306a36Sopenharmony_ci				if (rtlphy->pwrgroup_cnt == 1)
20062306a36Sopenharmony_ci					chnlgroup = 0;
20162306a36Sopenharmony_ci				if (rtlphy->pwrgroup_cnt >= 3) {
20262306a36Sopenharmony_ci					if (channel <= 3)
20362306a36Sopenharmony_ci						chnlgroup = 0;
20462306a36Sopenharmony_ci					else if (channel >= 4 && channel <= 9)
20562306a36Sopenharmony_ci						chnlgroup = 1;
20662306a36Sopenharmony_ci					else if (channel > 9)
20762306a36Sopenharmony_ci						chnlgroup = 2;
20862306a36Sopenharmony_ci					if (rtlphy->pwrgroup_cnt == 4)
20962306a36Sopenharmony_ci						chnlgroup++;
21062306a36Sopenharmony_ci				}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci				writeval = rtlphy->mcs_offset[chnlgroup]
21362306a36Sopenharmony_ci				    [index + (rf ? 8 : 0)] + ((index < 2) ?
21462306a36Sopenharmony_ci							      powerbase0[rf] :
21562306a36Sopenharmony_ci							      powerbase1[rf]);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
21862306a36Sopenharmony_ci					"Realtek regulatory, 20MHz, writeval(%c) = 0x%x\n",
21962306a36Sopenharmony_ci					rf == 0 ? 'A' : 'B', writeval);
22062306a36Sopenharmony_ci			}
22162306a36Sopenharmony_ci			break;
22262306a36Sopenharmony_ci		case 2:
22362306a36Sopenharmony_ci			writeval =
22462306a36Sopenharmony_ci			    ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
22762306a36Sopenharmony_ci				"Better regulatory, writeval(%c) = 0x%x\n",
22862306a36Sopenharmony_ci				rf == 0 ? 'A' : 'B', writeval);
22962306a36Sopenharmony_ci			break;
23062306a36Sopenharmony_ci		case 3:
23162306a36Sopenharmony_ci			chnlgroup = 0;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci			if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40) {
23462306a36Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
23562306a36Sopenharmony_ci					"customer's limit, 40MHz rf(%c) = 0x%x\n",
23662306a36Sopenharmony_ci					rf == 0 ? 'A' : 'B',
23762306a36Sopenharmony_ci					rtlefuse->pwrgroup_ht40[rf][channel -
23862306a36Sopenharmony_ci								    1]);
23962306a36Sopenharmony_ci			} else {
24062306a36Sopenharmony_ci				RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
24162306a36Sopenharmony_ci					"customer's limit, 20MHz rf(%c) = 0x%x\n",
24262306a36Sopenharmony_ci					rf == 0 ? 'A' : 'B',
24362306a36Sopenharmony_ci					rtlefuse->pwrgroup_ht20[rf][channel -
24462306a36Sopenharmony_ci								    1]);
24562306a36Sopenharmony_ci			}
24662306a36Sopenharmony_ci			for (i = 0; i < 4; i++) {
24762306a36Sopenharmony_ci				pwr_diff_limit[i] = (u8) ((rtlphy->mcs_offset
24862306a36Sopenharmony_ci					  [chnlgroup][index +
24962306a36Sopenharmony_ci					  (rf ? 8 : 0)] & (0x7f << (i * 8))) >>
25062306a36Sopenharmony_ci					  (i * 8));
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci				if (rtlphy->current_chan_bw ==
25362306a36Sopenharmony_ci				    HT_CHANNEL_WIDTH_20_40) {
25462306a36Sopenharmony_ci					if (pwr_diff_limit[i] >
25562306a36Sopenharmony_ci					    rtlefuse->
25662306a36Sopenharmony_ci					    pwrgroup_ht40[rf][channel - 1])
25762306a36Sopenharmony_ci						pwr_diff_limit[i] =
25862306a36Sopenharmony_ci						    rtlefuse->pwrgroup_ht40[rf]
25962306a36Sopenharmony_ci						    [channel - 1];
26062306a36Sopenharmony_ci				} else {
26162306a36Sopenharmony_ci					if (pwr_diff_limit[i] >
26262306a36Sopenharmony_ci					    rtlefuse->
26362306a36Sopenharmony_ci					    pwrgroup_ht20[rf][channel - 1])
26462306a36Sopenharmony_ci						pwr_diff_limit[i] =
26562306a36Sopenharmony_ci						    rtlefuse->pwrgroup_ht20[rf]
26662306a36Sopenharmony_ci						    [channel - 1];
26762306a36Sopenharmony_ci				}
26862306a36Sopenharmony_ci			}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci			customer_limit = (pwr_diff_limit[3] << 24) |
27162306a36Sopenharmony_ci			    (pwr_diff_limit[2] << 16) |
27262306a36Sopenharmony_ci			    (pwr_diff_limit[1] << 8) | (pwr_diff_limit[0]);
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
27562306a36Sopenharmony_ci				"Customer's limit rf(%c) = 0x%x\n",
27662306a36Sopenharmony_ci				rf == 0 ? 'A' : 'B', customer_limit);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci			writeval = customer_limit +
27962306a36Sopenharmony_ci			    ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
28262306a36Sopenharmony_ci				"Customer, writeval rf(%c)= 0x%x\n",
28362306a36Sopenharmony_ci				rf == 0 ? 'A' : 'B', writeval);
28462306a36Sopenharmony_ci			break;
28562306a36Sopenharmony_ci		default:
28662306a36Sopenharmony_ci			chnlgroup = 0;
28762306a36Sopenharmony_ci			writeval = rtlphy->mcs_offset[chnlgroup]
28862306a36Sopenharmony_ci			    [index + (rf ? 8 : 0)]
28962306a36Sopenharmony_ci			    + ((index < 2) ? powerbase0[rf] : powerbase1[rf]);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci			RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
29262306a36Sopenharmony_ci				"RTK better performance, writeval rf(%c) = 0x%x\n",
29362306a36Sopenharmony_ci				rf == 0 ? 'A' : 'B', writeval);
29462306a36Sopenharmony_ci			break;
29562306a36Sopenharmony_ci		}
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci		if (rtlpriv->dm.dynamic_txhighpower_lvl == TXHIGHPWRLEVEL_BT1)
29862306a36Sopenharmony_ci			writeval = writeval - 0x06060606;
29962306a36Sopenharmony_ci		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
30062306a36Sopenharmony_ci			 TXHIGHPWRLEVEL_BT2)
30162306a36Sopenharmony_ci			writeval = writeval - 0x0c0c0c0c;
30262306a36Sopenharmony_ci		*(p_outwriteval + rf) = writeval;
30362306a36Sopenharmony_ci	}
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic void _rtl92c_write_ofdm_power_reg(struct ieee80211_hw *hw,
30762306a36Sopenharmony_ci					 u8 index, u32 *value)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
31062306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	u16 regoffset_a[6] = {
31362306a36Sopenharmony_ci		RTXAGC_A_RATE18_06, RTXAGC_A_RATE54_24,
31462306a36Sopenharmony_ci		RTXAGC_A_MCS03_MCS00, RTXAGC_A_MCS07_MCS04,
31562306a36Sopenharmony_ci		RTXAGC_A_MCS11_MCS08, RTXAGC_A_MCS15_MCS12
31662306a36Sopenharmony_ci	};
31762306a36Sopenharmony_ci	u16 regoffset_b[6] = {
31862306a36Sopenharmony_ci		RTXAGC_B_RATE18_06, RTXAGC_B_RATE54_24,
31962306a36Sopenharmony_ci		RTXAGC_B_MCS03_MCS00, RTXAGC_B_MCS07_MCS04,
32062306a36Sopenharmony_ci		RTXAGC_B_MCS11_MCS08, RTXAGC_B_MCS15_MCS12
32162306a36Sopenharmony_ci	};
32262306a36Sopenharmony_ci	u8 i, rf, pwr_val[4];
32362306a36Sopenharmony_ci	u32 writeval;
32462306a36Sopenharmony_ci	u16 regoffset;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	for (rf = 0; rf < 2; rf++) {
32762306a36Sopenharmony_ci		writeval = value[rf];
32862306a36Sopenharmony_ci		for (i = 0; i < 4; i++) {
32962306a36Sopenharmony_ci			pwr_val[i] = (u8)((writeval & (0x7f <<
33062306a36Sopenharmony_ci							(i * 8))) >> (i * 8));
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci			if (pwr_val[i] > RF6052_MAX_TX_PWR)
33362306a36Sopenharmony_ci				pwr_val[i] = RF6052_MAX_TX_PWR;
33462306a36Sopenharmony_ci		}
33562306a36Sopenharmony_ci		writeval = (pwr_val[3] << 24) | (pwr_val[2] << 16) |
33662306a36Sopenharmony_ci		    (pwr_val[1] << 8) | pwr_val[0];
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci		if (rf == 0)
33962306a36Sopenharmony_ci			regoffset = regoffset_a[index];
34062306a36Sopenharmony_ci		else
34162306a36Sopenharmony_ci			regoffset = regoffset_b[index];
34262306a36Sopenharmony_ci		rtl_set_bbreg(hw, regoffset, MASKDWORD, writeval);
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci		RTPRINT(rtlpriv, FPHY, PHY_TXPWR,
34562306a36Sopenharmony_ci			"Set 0x%x = %08x\n", regoffset, writeval);
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci		if (((get_rf_type(rtlphy) == RF_2T2R) &&
34862306a36Sopenharmony_ci		     (regoffset == RTXAGC_A_MCS15_MCS12 ||
34962306a36Sopenharmony_ci		      regoffset == RTXAGC_B_MCS15_MCS12)) ||
35062306a36Sopenharmony_ci		    ((get_rf_type(rtlphy) != RF_2T2R) &&
35162306a36Sopenharmony_ci		     (regoffset == RTXAGC_A_MCS07_MCS04 ||
35262306a36Sopenharmony_ci		      regoffset == RTXAGC_B_MCS07_MCS04))) {
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci			writeval = pwr_val[3];
35562306a36Sopenharmony_ci			if (regoffset == RTXAGC_A_MCS15_MCS12 ||
35662306a36Sopenharmony_ci			    regoffset == RTXAGC_A_MCS07_MCS04)
35762306a36Sopenharmony_ci				regoffset = 0xc90;
35862306a36Sopenharmony_ci			if (regoffset == RTXAGC_B_MCS15_MCS12 ||
35962306a36Sopenharmony_ci			    regoffset == RTXAGC_B_MCS07_MCS04)
36062306a36Sopenharmony_ci				regoffset = 0xc98;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci			for (i = 0; i < 3; i++) {
36362306a36Sopenharmony_ci				writeval = (writeval > 6) ? (writeval - 6) : 0;
36462306a36Sopenharmony_ci				rtl_write_byte(rtlpriv, (u32) (regoffset + i),
36562306a36Sopenharmony_ci					       (u8)writeval);
36662306a36Sopenharmony_ci			}
36762306a36Sopenharmony_ci		}
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_civoid rtl92ce_phy_rf6052_set_ofdm_txpower(struct ieee80211_hw *hw,
37262306a36Sopenharmony_ci					u8 *ppowerlevel, u8 channel)
37362306a36Sopenharmony_ci{
37462306a36Sopenharmony_ci	u32 writeval[2], powerbase0[2], powerbase1[2];
37562306a36Sopenharmony_ci	u8 index;
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	rtl92c_phy_get_power_base(hw, ppowerlevel,
37862306a36Sopenharmony_ci				  channel, &powerbase0[0], &powerbase1[0]);
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	for (index = 0; index < 6; index++) {
38162306a36Sopenharmony_ci		_rtl92c_get_txpower_writeval_by_regulatory(hw,
38262306a36Sopenharmony_ci							   channel, index,
38362306a36Sopenharmony_ci							   &powerbase0[0],
38462306a36Sopenharmony_ci							   &powerbase1[0],
38562306a36Sopenharmony_ci							   &writeval[0]);
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci		_rtl92c_write_ofdm_power_reg(hw, index, &writeval[0]);
38862306a36Sopenharmony_ci	}
38962306a36Sopenharmony_ci}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_cibool rtl92ce_phy_rf6052_config(struct ieee80211_hw *hw)
39262306a36Sopenharmony_ci{
39362306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
39462306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	if (rtlphy->rf_type == RF_1T1R)
39762306a36Sopenharmony_ci		rtlphy->num_total_rfpath = 1;
39862306a36Sopenharmony_ci	else
39962306a36Sopenharmony_ci		rtlphy->num_total_rfpath = 2;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	return _rtl92ce_phy_rf6052_config_parafile(hw);
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci}
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_cistatic bool _rtl92ce_phy_rf6052_config_parafile(struct ieee80211_hw *hw)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
40862306a36Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
40962306a36Sopenharmony_ci	u32 u4_regvalue = 0;
41062306a36Sopenharmony_ci	u8 rfpath;
41162306a36Sopenharmony_ci	bool rtstatus = true;
41262306a36Sopenharmony_ci	struct bb_reg_def *pphyreg;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	for (rfpath = 0; rfpath < rtlphy->num_total_rfpath; rfpath++) {
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci		pphyreg = &rtlphy->phyreg_def[rfpath];
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci		switch (rfpath) {
41962306a36Sopenharmony_ci		case RF90_PATH_A:
42062306a36Sopenharmony_ci		case RF90_PATH_C:
42162306a36Sopenharmony_ci			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
42262306a36Sopenharmony_ci						    BRFSI_RFENV);
42362306a36Sopenharmony_ci			break;
42462306a36Sopenharmony_ci		case RF90_PATH_B:
42562306a36Sopenharmony_ci		case RF90_PATH_D:
42662306a36Sopenharmony_ci			u4_regvalue = rtl_get_bbreg(hw, pphyreg->rfintfs,
42762306a36Sopenharmony_ci						    BRFSI_RFENV << 16);
42862306a36Sopenharmony_ci			break;
42962306a36Sopenharmony_ci		}
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfe, BRFSI_RFENV << 16, 0x1);
43262306a36Sopenharmony_ci		udelay(1);
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfintfo, BRFSI_RFENV, 0x1);
43562306a36Sopenharmony_ci		udelay(1);
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfhssi_para2,
43862306a36Sopenharmony_ci			      B3WIREADDREAALENGTH, 0x0);
43962306a36Sopenharmony_ci		udelay(1);
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci		rtl_set_bbreg(hw, pphyreg->rfhssi_para2, B3WIREDATALENGTH, 0x0);
44262306a36Sopenharmony_ci		udelay(1);
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci		switch (rfpath) {
44562306a36Sopenharmony_ci		case RF90_PATH_A:
44662306a36Sopenharmony_ci			rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
44762306a36Sopenharmony_ci						(enum radio_path)rfpath);
44862306a36Sopenharmony_ci			break;
44962306a36Sopenharmony_ci		case RF90_PATH_B:
45062306a36Sopenharmony_ci			rtstatus = rtl92c_phy_config_rf_with_headerfile(hw,
45162306a36Sopenharmony_ci						(enum radio_path)rfpath);
45262306a36Sopenharmony_ci			break;
45362306a36Sopenharmony_ci		case RF90_PATH_C:
45462306a36Sopenharmony_ci			break;
45562306a36Sopenharmony_ci		case RF90_PATH_D:
45662306a36Sopenharmony_ci			break;
45762306a36Sopenharmony_ci		}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci		switch (rfpath) {
46062306a36Sopenharmony_ci		case RF90_PATH_A:
46162306a36Sopenharmony_ci		case RF90_PATH_C:
46262306a36Sopenharmony_ci			rtl_set_bbreg(hw, pphyreg->rfintfs,
46362306a36Sopenharmony_ci				      BRFSI_RFENV, u4_regvalue);
46462306a36Sopenharmony_ci			break;
46562306a36Sopenharmony_ci		case RF90_PATH_B:
46662306a36Sopenharmony_ci		case RF90_PATH_D:
46762306a36Sopenharmony_ci			rtl_set_bbreg(hw, pphyreg->rfintfs,
46862306a36Sopenharmony_ci				      BRFSI_RFENV << 16, u4_regvalue);
46962306a36Sopenharmony_ci			break;
47062306a36Sopenharmony_ci		}
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci		if (!rtstatus) {
47362306a36Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
47462306a36Sopenharmony_ci				"Radio[%d] Fail!!\n", rfpath);
47562306a36Sopenharmony_ci			return false;
47662306a36Sopenharmony_ci		}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "<---\n");
48162306a36Sopenharmony_ci	return rtstatus;
48262306a36Sopenharmony_ci}
483