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 "../pci.h"
68c2ecf20Sopenharmony_ci#include "../ps.h"
78c2ecf20Sopenharmony_ci#include "reg.h"
88c2ecf20Sopenharmony_ci#include "def.h"
98c2ecf20Sopenharmony_ci#include "phy.h"
108c2ecf20Sopenharmony_ci#include "../rtl8723com/phy_common.h"
118c2ecf20Sopenharmony_ci#include "rf.h"
128c2ecf20Sopenharmony_ci#include "dm.h"
138c2ecf20Sopenharmony_ci#include "../rtl8723com/dm_common.h"
148c2ecf20Sopenharmony_ci#include "table.h"
158c2ecf20Sopenharmony_ci#include "trx.h"
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw);
198c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw);
208c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
218c2ecf20Sopenharmony_ci						     u8 configtype);
228c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
238c2ecf20Sopenharmony_ci						       u8 configtype);
248c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
258c2ecf20Sopenharmony_ci						u8 channel, u8 *stage,
268c2ecf20Sopenharmony_ci						u8 *step, u32 *delay);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw);
298c2ecf20Sopenharmony_cistatic void rtl8723be_phy_set_io(struct ieee80211_hw *hw);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciu32 rtl8723be_phy_query_rf_reg(struct ieee80211_hw *hw, enum radio_path rfpath,
328c2ecf20Sopenharmony_ci			       u32 regaddr, u32 bitmask)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
358c2ecf20Sopenharmony_ci	u32 original_value, readback_value, bitshift;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
388c2ecf20Sopenharmony_ci		"regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
398c2ecf20Sopenharmony_ci		regaddr, rfpath, bitmask);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.rf_lock);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	original_value = rtl8723_phy_rf_serial_read(hw, rfpath, regaddr);
448c2ecf20Sopenharmony_ci	bitshift = calculate_bit_shift(bitmask);
458c2ecf20Sopenharmony_ci	readback_value = (original_value & bitmask) >> bitshift;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.rf_lock);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
508c2ecf20Sopenharmony_ci		"regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
518c2ecf20Sopenharmony_ci		regaddr, rfpath, bitmask, original_value);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return readback_value;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_civoid rtl8723be_phy_set_rf_reg(struct ieee80211_hw *hw, enum radio_path path,
578c2ecf20Sopenharmony_ci			      u32 regaddr, u32 bitmask, u32 data)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
608c2ecf20Sopenharmony_ci	u32 original_value, bitshift;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
638c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
648c2ecf20Sopenharmony_ci		regaddr, bitmask, data, path);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.rf_lock);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (bitmask != RFREG_OFFSET_MASK) {
698c2ecf20Sopenharmony_ci			original_value = rtl8723_phy_rf_serial_read(hw, path,
708c2ecf20Sopenharmony_ci								    regaddr);
718c2ecf20Sopenharmony_ci			bitshift = calculate_bit_shift(bitmask);
728c2ecf20Sopenharmony_ci			data = ((original_value & (~bitmask)) |
738c2ecf20Sopenharmony_ci				(data << bitshift));
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	rtl8723_phy_rf_serial_write(hw, path, regaddr, data);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.rf_lock);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_RF, DBG_TRACE,
818c2ecf20Sopenharmony_ci		"regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
828c2ecf20Sopenharmony_ci		regaddr, bitmask, data, path);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cibool rtl8723be_phy_mac_config(struct ieee80211_hw *hw)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
898c2ecf20Sopenharmony_ci	bool rtstatus = _rtl8723be_phy_config_mac_with_headerfile(hw);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, 0x04CA, 0x0B);
928c2ecf20Sopenharmony_ci	return rtstatus;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cibool rtl8723be_phy_bb_config(struct ieee80211_hw *hw)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	bool rtstatus = true;
988c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
998c2ecf20Sopenharmony_ci	u16 regval;
1008c2ecf20Sopenharmony_ci	u8 b_reg_hwparafile = 1;
1018c2ecf20Sopenharmony_ci	u32 tmp;
1028c2ecf20Sopenharmony_ci	u8 crystalcap = rtlpriv->efuse.crystalcap;
1038c2ecf20Sopenharmony_ci	rtl8723_phy_init_bb_rf_reg_def(hw);
1048c2ecf20Sopenharmony_ci	regval = rtl_read_word(rtlpriv, REG_SYS_FUNC_EN);
1058c2ecf20Sopenharmony_ci	rtl_write_word(rtlpriv, REG_SYS_FUNC_EN,
1068c2ecf20Sopenharmony_ci		       regval | BIT(13) | BIT(0) | BIT(1));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_RF_CTRL, RF_EN | RF_RSTB | RF_SDMRSTB);
1098c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN,
1108c2ecf20Sopenharmony_ci		       FEN_PPLL | FEN_PCIEA | FEN_DIO_PCIE |
1118c2ecf20Sopenharmony_ci		       FEN_BB_GLB_RSTN | FEN_BBRSTB);
1128c2ecf20Sopenharmony_ci	tmp = rtl_read_dword(rtlpriv, 0x4c);
1138c2ecf20Sopenharmony_ci	rtl_write_dword(rtlpriv, 0x4c, tmp | BIT(23));
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_AFE_XTAL_CTRL + 1, 0x80);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (b_reg_hwparafile == 1)
1188c2ecf20Sopenharmony_ci		rtstatus = _rtl8723be_phy_bb8723b_config_parafile(hw);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	crystalcap = crystalcap & 0x3F;
1218c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
1228c2ecf20Sopenharmony_ci		      (crystalcap | crystalcap << 6));
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return rtstatus;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cibool rtl8723be_phy_rf_config(struct ieee80211_hw *hw)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	return rtl8723be_phy_rf6052_config(hw);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic bool _rtl8723be_check_positive(struct ieee80211_hw *hw,
1338c2ecf20Sopenharmony_ci				      const u32 condition1,
1348c2ecf20Sopenharmony_ci				      const u32 condition2)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1378c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1388c2ecf20Sopenharmony_ci	u32 cut_ver = ((rtlhal->version & CHIP_VER_RTL_MASK)
1398c2ecf20Sopenharmony_ci					>> CHIP_VER_RTL_SHIFT);
1408c2ecf20Sopenharmony_ci	u32 intf = (rtlhal->interface == INTF_USB ? BIT(1) : BIT(0));
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	u8  board_type = ((rtlhal->board_type & BIT(4)) >> 4) << 0 | /* _GLNA */
1438c2ecf20Sopenharmony_ci			 ((rtlhal->board_type & BIT(3)) >> 3) << 1 | /* _GPA  */
1448c2ecf20Sopenharmony_ci			 ((rtlhal->board_type & BIT(7)) >> 7) << 2 | /* _ALNA */
1458c2ecf20Sopenharmony_ci			 ((rtlhal->board_type & BIT(6)) >> 6) << 3 | /* _APA  */
1468c2ecf20Sopenharmony_ci			 ((rtlhal->board_type & BIT(2)) >> 2) << 4;  /* _BT   */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	u32 cond1 = condition1, cond2 = condition2;
1498c2ecf20Sopenharmony_ci	u32 driver1 = cut_ver << 24 |	/* CUT ver */
1508c2ecf20Sopenharmony_ci		      0 << 20 |			/* interface 2/2 */
1518c2ecf20Sopenharmony_ci		      0x04 << 16 |		/* platform */
1528c2ecf20Sopenharmony_ci		      rtlhal->package_type << 12 |
1538c2ecf20Sopenharmony_ci		      intf << 8 |			/* interface 1/2 */
1548c2ecf20Sopenharmony_ci		      board_type;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	u32 driver2 = rtlhal->type_glna <<  0 |
1578c2ecf20Sopenharmony_ci		      rtlhal->type_gpa  <<  8 |
1588c2ecf20Sopenharmony_ci		      rtlhal->type_alna << 16 |
1598c2ecf20Sopenharmony_ci		      rtlhal->type_apa  << 24;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1628c2ecf20Sopenharmony_ci		"===> [8812A] CheckPositive (cond1, cond2) = (0x%X 0x%X)\n",
1638c2ecf20Sopenharmony_ci		cond1, cond2);
1648c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1658c2ecf20Sopenharmony_ci		"===> [8812A] CheckPositive (driver1, driver2) = (0x%X 0x%X)\n",
1668c2ecf20Sopenharmony_ci		driver1, driver2);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1698c2ecf20Sopenharmony_ci		"(Platform, Interface) = (0x%X, 0x%X)\n", 0x04, intf);
1708c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
1718c2ecf20Sopenharmony_ci		"(Board, Package) = (0x%X, 0x%X)\n",
1728c2ecf20Sopenharmony_ci		rtlhal->board_type, rtlhal->package_type);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/*============== Value Defined Check ===============*/
1758c2ecf20Sopenharmony_ci	/*QFN Type [15:12] and Cut Version [27:24] need to do value check*/
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (((cond1 & 0x0000F000) != 0) && ((cond1 & 0x0000F000) !=
1788c2ecf20Sopenharmony_ci		(driver1 & 0x0000F000)))
1798c2ecf20Sopenharmony_ci		return false;
1808c2ecf20Sopenharmony_ci	if (((cond1 & 0x0F000000) != 0) && ((cond1 & 0x0F000000) !=
1818c2ecf20Sopenharmony_ci		(driver1 & 0x0F000000)))
1828c2ecf20Sopenharmony_ci		return false;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/*=============== Bit Defined Check ================*/
1858c2ecf20Sopenharmony_ci	/* We don't care [31:28] */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	cond1   &= 0x00FF0FFF;
1888c2ecf20Sopenharmony_ci	driver1 &= 0x00FF0FFF;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if ((cond1 & driver1) == cond1) {
1918c2ecf20Sopenharmony_ci		u32 mask = 0;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci		if ((cond1 & 0x0F) == 0) /* BoardType is DONTCARE*/
1948c2ecf20Sopenharmony_ci			return true;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci		if ((cond1 & BIT(0)) != 0) /*GLNA*/
1978c2ecf20Sopenharmony_ci			mask |= 0x000000FF;
1988c2ecf20Sopenharmony_ci		if ((cond1 & BIT(1)) != 0) /*GPA*/
1998c2ecf20Sopenharmony_ci			mask |= 0x0000FF00;
2008c2ecf20Sopenharmony_ci		if ((cond1 & BIT(2)) != 0) /*ALNA*/
2018c2ecf20Sopenharmony_ci			mask |= 0x00FF0000;
2028c2ecf20Sopenharmony_ci		if ((cond1 & BIT(3)) != 0) /*APA*/
2038c2ecf20Sopenharmony_ci			mask |= 0xFF000000;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		/* BoardType of each RF path is matched*/
2068c2ecf20Sopenharmony_ci		if ((cond2 & mask) == (driver2 & mask))
2078c2ecf20Sopenharmony_ci			return true;
2088c2ecf20Sopenharmony_ci		else
2098c2ecf20Sopenharmony_ci			return false;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	return false;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic void _rtl8723be_config_rf_reg(struct ieee80211_hw *hw, u32 addr,
2158c2ecf20Sopenharmony_ci				     u32 data, enum radio_path rfpath,
2168c2ecf20Sopenharmony_ci				     u32 regaddr)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	if (addr == 0xfe || addr == 0xffe) {
2198c2ecf20Sopenharmony_ci		/* In order not to disturb BT music
2208c2ecf20Sopenharmony_ci		 *	when wifi init.(1ant NIC only)
2218c2ecf20Sopenharmony_ci		 */
2228c2ecf20Sopenharmony_ci		mdelay(50);
2238c2ecf20Sopenharmony_ci	} else {
2248c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, rfpath, regaddr, RFREG_OFFSET_MASK, data);
2258c2ecf20Sopenharmony_ci		udelay(1);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_cistatic void _rtl8723be_config_rf_radio_a(struct ieee80211_hw *hw,
2298c2ecf20Sopenharmony_ci					 u32 addr, u32 data)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	u32 content = 0x1000; /*RF Content: radio_a_txt*/
2328c2ecf20Sopenharmony_ci	u32 maskforphyset = (u32)(content & 0xE000);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	_rtl8723be_config_rf_reg(hw, addr, data, RF90_PATH_A,
2358c2ecf20Sopenharmony_ci				 addr | maskforphyset);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_init_tx_power_by_rate(struct ieee80211_hw *hw)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2428c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	u8 band, path, txnum, section;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	for (band = BAND_ON_2_4G; band <= BAND_ON_5G; ++band)
2478c2ecf20Sopenharmony_ci		for (path = 0; path < TX_PWR_BY_RATE_NUM_RF; ++path)
2488c2ecf20Sopenharmony_ci			for (txnum = 0; txnum < TX_PWR_BY_RATE_NUM_RF; ++txnum)
2498c2ecf20Sopenharmony_ci				for (section = 0;
2508c2ecf20Sopenharmony_ci				     section < TX_PWR_BY_RATE_NUM_SECTION;
2518c2ecf20Sopenharmony_ci				     ++section)
2528c2ecf20Sopenharmony_ci					rtlphy->tx_power_by_rate_offset
2538c2ecf20Sopenharmony_ci					  [band][path][txnum][section] = 0;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic void _rtl8723be_config_bb_reg(struct ieee80211_hw *hw,
2578c2ecf20Sopenharmony_ci				     u32 addr, u32 data)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	if (addr == 0xfe) {
2608c2ecf20Sopenharmony_ci		mdelay(50);
2618c2ecf20Sopenharmony_ci	} else if (addr == 0xfd) {
2628c2ecf20Sopenharmony_ci		mdelay(5);
2638c2ecf20Sopenharmony_ci	} else if (addr == 0xfc) {
2648c2ecf20Sopenharmony_ci		mdelay(1);
2658c2ecf20Sopenharmony_ci	} else if (addr == 0xfb) {
2668c2ecf20Sopenharmony_ci		udelay(50);
2678c2ecf20Sopenharmony_ci	} else if (addr == 0xfa) {
2688c2ecf20Sopenharmony_ci		udelay(5);
2698c2ecf20Sopenharmony_ci	} else if (addr == 0xf9) {
2708c2ecf20Sopenharmony_ci		udelay(1);
2718c2ecf20Sopenharmony_ci	} else {
2728c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, addr, MASKDWORD, data);
2738c2ecf20Sopenharmony_ci		udelay(1);
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_set_txpower_by_rate_base(struct ieee80211_hw *hw,
2788c2ecf20Sopenharmony_ci						    u8 band,
2798c2ecf20Sopenharmony_ci						    u8 path, u8 rate_section,
2808c2ecf20Sopenharmony_ci						    u8 txnum, u8 value)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
2838c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (path > RF90_PATH_D) {
2868c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
2878c2ecf20Sopenharmony_ci			"Invalid Rf Path %d in phy_SetTxPowerByRatBase()\n",
2888c2ecf20Sopenharmony_ci			path);
2898c2ecf20Sopenharmony_ci		return;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (band == BAND_ON_2_4G) {
2938c2ecf20Sopenharmony_ci		switch (rate_section) {
2948c2ecf20Sopenharmony_ci		case CCK:
2958c2ecf20Sopenharmony_ci			rtlphy->txpwr_by_rate_base_24g[path][txnum][0] = value;
2968c2ecf20Sopenharmony_ci			break;
2978c2ecf20Sopenharmony_ci		case OFDM:
2988c2ecf20Sopenharmony_ci			rtlphy->txpwr_by_rate_base_24g[path][txnum][1] = value;
2998c2ecf20Sopenharmony_ci			break;
3008c2ecf20Sopenharmony_ci		case HT_MCS0_MCS7:
3018c2ecf20Sopenharmony_ci			rtlphy->txpwr_by_rate_base_24g[path][txnum][2] = value;
3028c2ecf20Sopenharmony_ci			break;
3038c2ecf20Sopenharmony_ci		case HT_MCS8_MCS15:
3048c2ecf20Sopenharmony_ci			rtlphy->txpwr_by_rate_base_24g[path][txnum][3] = value;
3058c2ecf20Sopenharmony_ci			break;
3068c2ecf20Sopenharmony_ci		default:
3078c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3088c2ecf20Sopenharmony_ci				"Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_SetTxPowerByRateBase()\n",
3098c2ecf20Sopenharmony_ci				rate_section, path, txnum);
3108c2ecf20Sopenharmony_ci			break;
3118c2ecf20Sopenharmony_ci		}
3128c2ecf20Sopenharmony_ci	} else {
3138c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3148c2ecf20Sopenharmony_ci			"Invalid Band %d in PHY_SetTxPowerByRateBase()\n",
3158c2ecf20Sopenharmony_ci			band);
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_get_txpower_by_rate_base(struct ieee80211_hw *hw,
3218c2ecf20Sopenharmony_ci						  u8 band, u8 path, u8 txnum,
3228c2ecf20Sopenharmony_ci						  u8 rate_section)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3258c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
3268c2ecf20Sopenharmony_ci	u8 value = 0;
3278c2ecf20Sopenharmony_ci	if (path > RF90_PATH_D) {
3288c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3298c2ecf20Sopenharmony_ci			"Invalid Rf Path %d in PHY_GetTxPowerByRateBase()\n",
3308c2ecf20Sopenharmony_ci			path);
3318c2ecf20Sopenharmony_ci		return 0;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (band == BAND_ON_2_4G) {
3358c2ecf20Sopenharmony_ci		switch (rate_section) {
3368c2ecf20Sopenharmony_ci		case CCK:
3378c2ecf20Sopenharmony_ci			value = rtlphy->txpwr_by_rate_base_24g[path][txnum][0];
3388c2ecf20Sopenharmony_ci			break;
3398c2ecf20Sopenharmony_ci		case OFDM:
3408c2ecf20Sopenharmony_ci			value = rtlphy->txpwr_by_rate_base_24g[path][txnum][1];
3418c2ecf20Sopenharmony_ci			break;
3428c2ecf20Sopenharmony_ci		case HT_MCS0_MCS7:
3438c2ecf20Sopenharmony_ci			value = rtlphy->txpwr_by_rate_base_24g[path][txnum][2];
3448c2ecf20Sopenharmony_ci			break;
3458c2ecf20Sopenharmony_ci		case HT_MCS8_MCS15:
3468c2ecf20Sopenharmony_ci			value = rtlphy->txpwr_by_rate_base_24g[path][txnum][3];
3478c2ecf20Sopenharmony_ci			break;
3488c2ecf20Sopenharmony_ci		default:
3498c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3508c2ecf20Sopenharmony_ci				"Invalid RateSection %d in Band 2.4G, Rf Path %d, %dTx in PHY_GetTxPowerByRateBase()\n",
3518c2ecf20Sopenharmony_ci				rate_section, path, txnum);
3528c2ecf20Sopenharmony_ci			break;
3538c2ecf20Sopenharmony_ci		}
3548c2ecf20Sopenharmony_ci	} else {
3558c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3568c2ecf20Sopenharmony_ci			"Invalid Band %d in PHY_GetTxPowerByRateBase()\n",
3578c2ecf20Sopenharmony_ci			band);
3588c2ecf20Sopenharmony_ci	}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return value;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_store_txpower_by_rate_base(struct ieee80211_hw *hw)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3668c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
3678c2ecf20Sopenharmony_ci	u16 rawvalue = 0;
3688c2ecf20Sopenharmony_ci	u8 base = 0, path = 0;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	for (path = RF90_PATH_A; path <= RF90_PATH_B; ++path) {
3718c2ecf20Sopenharmony_ci		if (path == RF90_PATH_A) {
3728c2ecf20Sopenharmony_ci			rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
3738c2ecf20Sopenharmony_ci				[BAND_ON_2_4G][path][RF_1TX][3] >> 24) & 0xFF;
3748c2ecf20Sopenharmony_ci			base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
3758c2ecf20Sopenharmony_ci			_rtl8723be_phy_set_txpower_by_rate_base(hw,
3768c2ecf20Sopenharmony_ci				BAND_ON_2_4G, path, CCK, RF_1TX, base);
3778c2ecf20Sopenharmony_ci		} else if (path == RF90_PATH_B) {
3788c2ecf20Sopenharmony_ci			rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
3798c2ecf20Sopenharmony_ci				[BAND_ON_2_4G][path][RF_1TX][3] >> 0) & 0xFF;
3808c2ecf20Sopenharmony_ci			base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
3818c2ecf20Sopenharmony_ci			_rtl8723be_phy_set_txpower_by_rate_base(hw,
3828c2ecf20Sopenharmony_ci								BAND_ON_2_4G,
3838c2ecf20Sopenharmony_ci								path, CCK,
3848c2ecf20Sopenharmony_ci								RF_1TX, base);
3858c2ecf20Sopenharmony_ci		}
3868c2ecf20Sopenharmony_ci		rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
3878c2ecf20Sopenharmony_ci				[BAND_ON_2_4G][path][RF_1TX][1] >> 24) & 0xFF;
3888c2ecf20Sopenharmony_ci		base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
3898c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
3908c2ecf20Sopenharmony_ci							path, OFDM, RF_1TX,
3918c2ecf20Sopenharmony_ci							base);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci		rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
3948c2ecf20Sopenharmony_ci				[BAND_ON_2_4G][path][RF_1TX][5] >> 24) & 0xFF;
3958c2ecf20Sopenharmony_ci		base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
3968c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
3978c2ecf20Sopenharmony_ci							path, HT_MCS0_MCS7,
3988c2ecf20Sopenharmony_ci							RF_1TX, base);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		rawvalue = (u16)(rtlphy->tx_power_by_rate_offset
4018c2ecf20Sopenharmony_ci				[BAND_ON_2_4G][path][RF_2TX][7] >> 24) & 0xFF;
4028c2ecf20Sopenharmony_ci		base = (rawvalue >> 4) * 10 + (rawvalue & 0xF);
4038c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_by_rate_base(hw, BAND_ON_2_4G,
4048c2ecf20Sopenharmony_ci							path, HT_MCS8_MCS15,
4058c2ecf20Sopenharmony_ci							RF_2TX, base);
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic void _phy_convert_txpower_dbm_to_relative_value(u32 *data, u8 start,
4108c2ecf20Sopenharmony_ci						u8 end, u8 base_val)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	s8 i = 0;
4138c2ecf20Sopenharmony_ci	u8 temp_value = 0;
4148c2ecf20Sopenharmony_ci	u32 temp_data = 0;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	for (i = 3; i >= 0; --i) {
4178c2ecf20Sopenharmony_ci		if (i >= start && i <= end) {
4188c2ecf20Sopenharmony_ci			/* Get the exact value */
4198c2ecf20Sopenharmony_ci			temp_value = (u8)(*data >> (i * 8)) & 0xF;
4208c2ecf20Sopenharmony_ci			temp_value += ((u8)((*data >> (i*8 + 4)) & 0xF)) * 10;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci			/* Change the value to a relative value */
4238c2ecf20Sopenharmony_ci			temp_value = (temp_value > base_val) ?
4248c2ecf20Sopenharmony_ci				     temp_value - base_val :
4258c2ecf20Sopenharmony_ci				     base_val - temp_value;
4268c2ecf20Sopenharmony_ci		} else {
4278c2ecf20Sopenharmony_ci			temp_value = (u8)(*data >> (i * 8)) & 0xFF;
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		temp_data <<= 8;
4308c2ecf20Sopenharmony_ci		temp_data |= temp_value;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci	*data = temp_data;
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_convert_txpower_dbm_to_relative_value(
4368c2ecf20Sopenharmony_ci							struct ieee80211_hw *hw)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4398c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
4408c2ecf20Sopenharmony_ci	u8 base = 0, rfpath = RF90_PATH_A;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	base = _rtl8723be_phy_get_txpower_by_rate_base(hw,
4438c2ecf20Sopenharmony_ci			BAND_ON_2_4G, rfpath, RF_1TX, CCK);
4448c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4458c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][2],
4468c2ecf20Sopenharmony_ci	    1, 1, base);
4478c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4488c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][3],
4498c2ecf20Sopenharmony_ci	    1, 3, base);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G, rfpath,
4528c2ecf20Sopenharmony_ci						       RF_1TX, OFDM);
4538c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4548c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][0],
4558c2ecf20Sopenharmony_ci	    0, 3, base);
4568c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4578c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][1],
4588c2ecf20Sopenharmony_ci	    0, 3, base);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
4618c2ecf20Sopenharmony_ci						rfpath, RF_1TX, HT_MCS0_MCS7);
4628c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4638c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][4],
4648c2ecf20Sopenharmony_ci	    0, 3, base);
4658c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4668c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_1TX][5],
4678c2ecf20Sopenharmony_ci	    0, 3, base);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	base = _rtl8723be_phy_get_txpower_by_rate_base(hw, BAND_ON_2_4G,
4708c2ecf20Sopenharmony_ci						       rfpath, RF_2TX,
4718c2ecf20Sopenharmony_ci						       HT_MCS8_MCS15);
4728c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4738c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][6],
4748c2ecf20Sopenharmony_ci	    0, 3, base);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	_phy_convert_txpower_dbm_to_relative_value(
4778c2ecf20Sopenharmony_ci	    &rtlphy->tx_power_by_rate_offset[BAND_ON_2_4G][rfpath][RF_2TX][7],
4788c2ecf20Sopenharmony_ci	    0, 3, base);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
4818c2ecf20Sopenharmony_ci		"<===%s\n", __func__);
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic void phy_txpower_by_rate_config(struct ieee80211_hw *hw)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	_rtl8723be_phy_store_txpower_by_rate_base(hw);
4878c2ecf20Sopenharmony_ci	_rtl8723be_phy_convert_txpower_dbm_to_relative_value(hw);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_bb8723b_config_parafile(struct ieee80211_hw *hw)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
4938c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
4948c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
4958c2ecf20Sopenharmony_ci	bool rtstatus;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	/* switch ant to BT */
4988c2ecf20Sopenharmony_ci	if (rtlpriv->rtlhal.interface == INTF_USB) {
4998c2ecf20Sopenharmony_ci		rtl_write_dword(rtlpriv, 0x948, 0x0);
5008c2ecf20Sopenharmony_ci	} else {
5018c2ecf20Sopenharmony_ci		if (rtlpriv->btcoexist.btc_info.single_ant_path == 0)
5028c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x948, 0x280);
5038c2ecf20Sopenharmony_ci		else
5048c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x948, 0x0);
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
5088c2ecf20Sopenharmony_ci						BASEBAND_CONFIG_PHY_REG);
5098c2ecf20Sopenharmony_ci	if (!rtstatus) {
5108c2ecf20Sopenharmony_ci		pr_err("Write BB Reg Fail!!\n");
5118c2ecf20Sopenharmony_ci		return false;
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci	_rtl8723be_phy_init_tx_power_by_rate(hw);
5148c2ecf20Sopenharmony_ci	if (!rtlefuse->autoload_failflag) {
5158c2ecf20Sopenharmony_ci		rtlphy->pwrgroup_cnt = 0;
5168c2ecf20Sopenharmony_ci		rtstatus = _rtl8723be_phy_config_bb_with_pgheaderfile(hw,
5178c2ecf20Sopenharmony_ci						BASEBAND_CONFIG_PHY_REG);
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci	phy_txpower_by_rate_config(hw);
5208c2ecf20Sopenharmony_ci	if (!rtstatus) {
5218c2ecf20Sopenharmony_ci		pr_err("BB_PG Reg Fail!!\n");
5228c2ecf20Sopenharmony_ci		return false;
5238c2ecf20Sopenharmony_ci	}
5248c2ecf20Sopenharmony_ci	rtstatus = _rtl8723be_phy_config_bb_with_headerfile(hw,
5258c2ecf20Sopenharmony_ci						BASEBAND_CONFIG_AGC_TAB);
5268c2ecf20Sopenharmony_ci	if (!rtstatus) {
5278c2ecf20Sopenharmony_ci		pr_err("AGC Table Fail\n");
5288c2ecf20Sopenharmony_ci		return false;
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci	rtlphy->cck_high_power = (bool)(rtl_get_bbreg(hw,
5318c2ecf20Sopenharmony_ci						      RFPGA0_XA_HSSIPARAMETER2,
5328c2ecf20Sopenharmony_ci						      0x200));
5338c2ecf20Sopenharmony_ci	return true;
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic bool rtl8723be_phy_config_with_headerfile(struct ieee80211_hw *hw,
5378c2ecf20Sopenharmony_ci						 u32 *array_table,
5388c2ecf20Sopenharmony_ci						 u16 arraylen,
5398c2ecf20Sopenharmony_ci		void (*set_reg)(struct ieee80211_hw *hw, u32 regaddr, u32 data))
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	#define COND_ELSE  2
5428c2ecf20Sopenharmony_ci	#define COND_ENDIF 3
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	int i = 0;
5458c2ecf20Sopenharmony_ci	u8 cond;
5468c2ecf20Sopenharmony_ci	bool matched = true, skipped = false;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	while ((i + 1) < arraylen) {
5498c2ecf20Sopenharmony_ci		u32 v1 = array_table[i];
5508c2ecf20Sopenharmony_ci		u32 v2 = array_table[i + 1];
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		if (v1 & (BIT(31) | BIT(30))) {/*positive & negative condition*/
5538c2ecf20Sopenharmony_ci			if (v1 & BIT(31)) {/* positive condition*/
5548c2ecf20Sopenharmony_ci				cond  = (u8)((v1 & (BIT(29) | BIT(28))) >> 28);
5558c2ecf20Sopenharmony_ci				if (cond == COND_ENDIF) { /*end*/
5568c2ecf20Sopenharmony_ci					matched = true;
5578c2ecf20Sopenharmony_ci					skipped = false;
5588c2ecf20Sopenharmony_ci				} else if (cond == COND_ELSE) { /*else*/
5598c2ecf20Sopenharmony_ci					matched = skipped ? false : true;
5608c2ecf20Sopenharmony_ci				} else {/*if , else if*/
5618c2ecf20Sopenharmony_ci					if (skipped) {
5628c2ecf20Sopenharmony_ci						matched = false;
5638c2ecf20Sopenharmony_ci					} else {
5648c2ecf20Sopenharmony_ci						if (_rtl8723be_check_positive(
5658c2ecf20Sopenharmony_ci								hw, v1, v2)) {
5668c2ecf20Sopenharmony_ci							matched = true;
5678c2ecf20Sopenharmony_ci							skipped = true;
5688c2ecf20Sopenharmony_ci						} else {
5698c2ecf20Sopenharmony_ci							matched = false;
5708c2ecf20Sopenharmony_ci							skipped = false;
5718c2ecf20Sopenharmony_ci						}
5728c2ecf20Sopenharmony_ci					}
5738c2ecf20Sopenharmony_ci				}
5748c2ecf20Sopenharmony_ci			} else if (v1 & BIT(30)) { /*negative condition*/
5758c2ecf20Sopenharmony_ci			/*do nothing*/
5768c2ecf20Sopenharmony_ci			}
5778c2ecf20Sopenharmony_ci		} else {
5788c2ecf20Sopenharmony_ci			if (matched)
5798c2ecf20Sopenharmony_ci				set_reg(hw, v1, v2);
5808c2ecf20Sopenharmony_ci		}
5818c2ecf20Sopenharmony_ci		i = i + 2;
5828c2ecf20Sopenharmony_ci	}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	return true;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_mac_with_headerfile(struct ieee80211_hw *hw)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE, "Read rtl8723beMACPHY_Array\n");
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	return rtl8723be_phy_config_with_headerfile(hw,
5948c2ecf20Sopenharmony_ci			RTL8723BEMAC_1T_ARRAY, RTL8723BEMAC_1T_ARRAYLEN,
5958c2ecf20Sopenharmony_ci			rtl_write_byte_with_val32);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_bb_with_headerfile(struct ieee80211_hw *hw,
5998c2ecf20Sopenharmony_ci						     u8 configtype)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (configtype == BASEBAND_CONFIG_PHY_REG)
6038c2ecf20Sopenharmony_ci		return rtl8723be_phy_config_with_headerfile(hw,
6048c2ecf20Sopenharmony_ci				RTL8723BEPHY_REG_1TARRAY,
6058c2ecf20Sopenharmony_ci				RTL8723BEPHY_REG_1TARRAYLEN,
6068c2ecf20Sopenharmony_ci				_rtl8723be_config_bb_reg);
6078c2ecf20Sopenharmony_ci	else if (configtype == BASEBAND_CONFIG_AGC_TAB)
6088c2ecf20Sopenharmony_ci		return rtl8723be_phy_config_with_headerfile(hw,
6098c2ecf20Sopenharmony_ci				RTL8723BEAGCTAB_1TARRAY,
6108c2ecf20Sopenharmony_ci				RTL8723BEAGCTAB_1TARRAYLEN,
6118c2ecf20Sopenharmony_ci				rtl_set_bbreg_with_dwmask);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	return false;
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic u8 _rtl8723be_get_rate_section_index(u32 regaddr)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	u8 index = 0;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	switch (regaddr) {
6218c2ecf20Sopenharmony_ci	case RTXAGC_A_RATE18_06:
6228c2ecf20Sopenharmony_ci		index = 0;
6238c2ecf20Sopenharmony_ci	break;
6248c2ecf20Sopenharmony_ci	case RTXAGC_A_RATE54_24:
6258c2ecf20Sopenharmony_ci		index = 1;
6268c2ecf20Sopenharmony_ci	break;
6278c2ecf20Sopenharmony_ci	case RTXAGC_A_CCK1_MCS32:
6288c2ecf20Sopenharmony_ci		index = 2;
6298c2ecf20Sopenharmony_ci	break;
6308c2ecf20Sopenharmony_ci	case RTXAGC_B_CCK11_A_CCK2_11:
6318c2ecf20Sopenharmony_ci		index = 3;
6328c2ecf20Sopenharmony_ci	break;
6338c2ecf20Sopenharmony_ci	case RTXAGC_A_MCS03_MCS00:
6348c2ecf20Sopenharmony_ci		index = 4;
6358c2ecf20Sopenharmony_ci	break;
6368c2ecf20Sopenharmony_ci	case RTXAGC_A_MCS07_MCS04:
6378c2ecf20Sopenharmony_ci		index = 5;
6388c2ecf20Sopenharmony_ci	break;
6398c2ecf20Sopenharmony_ci	case RTXAGC_A_MCS11_MCS08:
6408c2ecf20Sopenharmony_ci		index = 6;
6418c2ecf20Sopenharmony_ci	break;
6428c2ecf20Sopenharmony_ci	case RTXAGC_A_MCS15_MCS12:
6438c2ecf20Sopenharmony_ci		index = 7;
6448c2ecf20Sopenharmony_ci	break;
6458c2ecf20Sopenharmony_ci	case RTXAGC_B_RATE18_06:
6468c2ecf20Sopenharmony_ci		index = 0;
6478c2ecf20Sopenharmony_ci	break;
6488c2ecf20Sopenharmony_ci	case RTXAGC_B_RATE54_24:
6498c2ecf20Sopenharmony_ci		index = 1;
6508c2ecf20Sopenharmony_ci	break;
6518c2ecf20Sopenharmony_ci	case RTXAGC_B_CCK1_55_MCS32:
6528c2ecf20Sopenharmony_ci		index = 2;
6538c2ecf20Sopenharmony_ci	break;
6548c2ecf20Sopenharmony_ci	case RTXAGC_B_MCS03_MCS00:
6558c2ecf20Sopenharmony_ci		index = 4;
6568c2ecf20Sopenharmony_ci	break;
6578c2ecf20Sopenharmony_ci	case RTXAGC_B_MCS07_MCS04:
6588c2ecf20Sopenharmony_ci		index = 5;
6598c2ecf20Sopenharmony_ci	break;
6608c2ecf20Sopenharmony_ci	case RTXAGC_B_MCS11_MCS08:
6618c2ecf20Sopenharmony_ci		index = 6;
6628c2ecf20Sopenharmony_ci	break;
6638c2ecf20Sopenharmony_ci	case RTXAGC_B_MCS15_MCS12:
6648c2ecf20Sopenharmony_ci		index = 7;
6658c2ecf20Sopenharmony_ci	break;
6668c2ecf20Sopenharmony_ci	default:
6678c2ecf20Sopenharmony_ci		regaddr &= 0xFFF;
6688c2ecf20Sopenharmony_ci		if (regaddr >= 0xC20 && regaddr <= 0xC4C)
6698c2ecf20Sopenharmony_ci			index = (u8)((regaddr - 0xC20) / 4);
6708c2ecf20Sopenharmony_ci		else if (regaddr >= 0xE20 && regaddr <= 0xE4C)
6718c2ecf20Sopenharmony_ci			index = (u8)((regaddr - 0xE20) / 4);
6728c2ecf20Sopenharmony_ci		break;
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci	return index;
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cistatic void _rtl8723be_store_tx_power_by_rate(struct ieee80211_hw *hw,
6788c2ecf20Sopenharmony_ci					      u32 band, u32 rfpath,
6798c2ecf20Sopenharmony_ci					      u32 txnum, u32 regaddr,
6808c2ecf20Sopenharmony_ci					      u32 bitmask, u32 data)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6838c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
6848c2ecf20Sopenharmony_ci	u8 rate_section = _rtl8723be_get_rate_section_index(regaddr);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	if (band != BAND_ON_2_4G && band != BAND_ON_5G) {
6878c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, FPHY, PHY_TXPWR, "Invalid Band %d\n", band);
6888c2ecf20Sopenharmony_ci		return;
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci	if (rfpath > MAX_RF_PATH - 1) {
6918c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, FPHY, PHY_TXPWR,
6928c2ecf20Sopenharmony_ci			"Invalid RfPath %d\n", rfpath);
6938c2ecf20Sopenharmony_ci		return;
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci	if (txnum > MAX_RF_PATH - 1) {
6968c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, FPHY, PHY_TXPWR, "Invalid TxNum %d\n", txnum);
6978c2ecf20Sopenharmony_ci		return;
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	rtlphy->tx_power_by_rate_offset[band][rfpath][txnum][rate_section] =
7018c2ecf20Sopenharmony_ci									data;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_config_bb_with_pgheaderfile(struct ieee80211_hw *hw,
7068c2ecf20Sopenharmony_ci						       u8 configtype)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7098c2ecf20Sopenharmony_ci	int i;
7108c2ecf20Sopenharmony_ci	u32 *phy_regarray_table_pg;
7118c2ecf20Sopenharmony_ci	u16 phy_regarray_pg_len;
7128c2ecf20Sopenharmony_ci	u32 v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	phy_regarray_pg_len = RTL8723BEPHY_REG_ARRAY_PGLEN;
7158c2ecf20Sopenharmony_ci	phy_regarray_table_pg = RTL8723BEPHY_REG_ARRAY_PG;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (configtype == BASEBAND_CONFIG_PHY_REG) {
7188c2ecf20Sopenharmony_ci		for (i = 0; i < phy_regarray_pg_len; i = i + 6) {
7198c2ecf20Sopenharmony_ci			v1 = phy_regarray_table_pg[i];
7208c2ecf20Sopenharmony_ci			v2 = phy_regarray_table_pg[i+1];
7218c2ecf20Sopenharmony_ci			v3 = phy_regarray_table_pg[i+2];
7228c2ecf20Sopenharmony_ci			v4 = phy_regarray_table_pg[i+3];
7238c2ecf20Sopenharmony_ci			v5 = phy_regarray_table_pg[i+4];
7248c2ecf20Sopenharmony_ci			v6 = phy_regarray_table_pg[i+5];
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci			if (v1 < 0xcdcdcdcd) {
7278c2ecf20Sopenharmony_ci				if (phy_regarray_table_pg[i] == 0xfe ||
7288c2ecf20Sopenharmony_ci				    phy_regarray_table_pg[i] == 0xffe)
7298c2ecf20Sopenharmony_ci					mdelay(50);
7308c2ecf20Sopenharmony_ci				else
7318c2ecf20Sopenharmony_ci					_rtl8723be_store_tx_power_by_rate(hw,
7328c2ecf20Sopenharmony_ci							v1, v2, v3, v4, v5, v6);
7338c2ecf20Sopenharmony_ci				continue;
7348c2ecf20Sopenharmony_ci			}
7358c2ecf20Sopenharmony_ci		}
7368c2ecf20Sopenharmony_ci	} else {
7378c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_SEND, DBG_TRACE,
7388c2ecf20Sopenharmony_ci			"configtype != BaseBand_Config_PHY_REG\n");
7398c2ecf20Sopenharmony_ci	}
7408c2ecf20Sopenharmony_ci	return true;
7418c2ecf20Sopenharmony_ci}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cibool rtl8723be_phy_config_rf_with_headerfile(struct ieee80211_hw *hw,
7448c2ecf20Sopenharmony_ci					     enum radio_path rfpath)
7458c2ecf20Sopenharmony_ci{
7468c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7478c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
7488c2ecf20Sopenharmony_ci	bool ret = true;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Radio No %x\n", rfpath);
7518c2ecf20Sopenharmony_ci	switch (rfpath) {
7528c2ecf20Sopenharmony_ci	case RF90_PATH_A:
7538c2ecf20Sopenharmony_ci		ret =  rtl8723be_phy_config_with_headerfile(hw,
7548c2ecf20Sopenharmony_ci				RTL8723BE_RADIOA_1TARRAY,
7558c2ecf20Sopenharmony_ci				RTL8723BE_RADIOA_1TARRAYLEN,
7568c2ecf20Sopenharmony_ci				_rtl8723be_config_rf_radio_a);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		if (rtlhal->oem_id == RT_CID_819X_HP)
7598c2ecf20Sopenharmony_ci			_rtl8723be_config_rf_radio_a(hw, 0x52, 0x7E4BD);
7608c2ecf20Sopenharmony_ci		break;
7618c2ecf20Sopenharmony_ci	case RF90_PATH_B:
7628c2ecf20Sopenharmony_ci	case RF90_PATH_C:
7638c2ecf20Sopenharmony_ci		break;
7648c2ecf20Sopenharmony_ci	case RF90_PATH_D:
7658c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
7668c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", rfpath);
7678c2ecf20Sopenharmony_ci		break;
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci	return ret;
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_civoid rtl8723be_phy_get_hw_reg_originalvalue(struct ieee80211_hw *hw)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
7758c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[0] =
7788c2ecf20Sopenharmony_ci	    (u8)rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
7798c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[1] =
7808c2ecf20Sopenharmony_ci	    (u8)rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
7818c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[2] =
7828c2ecf20Sopenharmony_ci	    (u8)rtl_get_bbreg(hw, ROFDM0_XCAGCCORE1, MASKBYTE0);
7838c2ecf20Sopenharmony_ci	rtlphy->default_initialgain[3] =
7848c2ecf20Sopenharmony_ci	    (u8)rtl_get_bbreg(hw, ROFDM0_XDAGCCORE1, MASKBYTE0);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
7878c2ecf20Sopenharmony_ci		"Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x\n",
7888c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[0],
7898c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[1],
7908c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[2],
7918c2ecf20Sopenharmony_ci		rtlphy->default_initialgain[3]);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	rtlphy->framesync = (u8)rtl_get_bbreg(hw, ROFDM0_RXDETECTOR3,
7948c2ecf20Sopenharmony_ci					       MASKBYTE0);
7958c2ecf20Sopenharmony_ci	rtlphy->framesync_c34 = rtl_get_bbreg(hw, ROFDM0_RXDETECTOR2,
7968c2ecf20Sopenharmony_ci					      MASKDWORD);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
7998c2ecf20Sopenharmony_ci		"Default framesync (0x%x) = 0x%x\n",
8008c2ecf20Sopenharmony_ci		ROFDM0_RXDETECTOR3, rtlphy->framesync);
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_get_ratesection_intxpower_byrate(enum radio_path path,
8048c2ecf20Sopenharmony_ci							  u8 rate)
8058c2ecf20Sopenharmony_ci{
8068c2ecf20Sopenharmony_ci	u8 rate_section = 0;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	switch (rate) {
8098c2ecf20Sopenharmony_ci	case DESC92C_RATE1M:
8108c2ecf20Sopenharmony_ci		rate_section = 2;
8118c2ecf20Sopenharmony_ci		break;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	case DESC92C_RATE2M:
8148c2ecf20Sopenharmony_ci	case DESC92C_RATE5_5M:
8158c2ecf20Sopenharmony_ci		if (path == RF90_PATH_A)
8168c2ecf20Sopenharmony_ci			rate_section = 3;
8178c2ecf20Sopenharmony_ci		else if (path == RF90_PATH_B)
8188c2ecf20Sopenharmony_ci			rate_section = 2;
8198c2ecf20Sopenharmony_ci		break;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	case DESC92C_RATE11M:
8228c2ecf20Sopenharmony_ci		rate_section = 3;
8238c2ecf20Sopenharmony_ci		break;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	case DESC92C_RATE6M:
8268c2ecf20Sopenharmony_ci	case DESC92C_RATE9M:
8278c2ecf20Sopenharmony_ci	case DESC92C_RATE12M:
8288c2ecf20Sopenharmony_ci	case DESC92C_RATE18M:
8298c2ecf20Sopenharmony_ci		rate_section = 0;
8308c2ecf20Sopenharmony_ci		break;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	case DESC92C_RATE24M:
8338c2ecf20Sopenharmony_ci	case DESC92C_RATE36M:
8348c2ecf20Sopenharmony_ci	case DESC92C_RATE48M:
8358c2ecf20Sopenharmony_ci	case DESC92C_RATE54M:
8368c2ecf20Sopenharmony_ci		rate_section = 1;
8378c2ecf20Sopenharmony_ci		break;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS0:
8408c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS1:
8418c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS2:
8428c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS3:
8438c2ecf20Sopenharmony_ci		rate_section = 4;
8448c2ecf20Sopenharmony_ci		break;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS4:
8478c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS5:
8488c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS6:
8498c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS7:
8508c2ecf20Sopenharmony_ci		rate_section = 5;
8518c2ecf20Sopenharmony_ci		break;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS8:
8548c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS9:
8558c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS10:
8568c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS11:
8578c2ecf20Sopenharmony_ci		rate_section = 6;
8588c2ecf20Sopenharmony_ci		break;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS12:
8618c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS13:
8628c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS14:
8638c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS15:
8648c2ecf20Sopenharmony_ci		rate_section = 7;
8658c2ecf20Sopenharmony_ci		break;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	default:
8688c2ecf20Sopenharmony_ci		WARN_ONCE(true, "rtl8723be: Rate_Section is Illegal\n");
8698c2ecf20Sopenharmony_ci		break;
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	return rate_section;
8738c2ecf20Sopenharmony_ci}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_cistatic u8 _rtl8723be_get_txpower_by_rate(struct ieee80211_hw *hw,
8768c2ecf20Sopenharmony_ci					 enum band_type band,
8778c2ecf20Sopenharmony_ci					 enum radio_path rfpath, u8 rate)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
8808c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
8818c2ecf20Sopenharmony_ci	u8 shift = 0, rate_section, tx_num;
8828c2ecf20Sopenharmony_ci	s8 tx_pwr_diff = 0;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	rate_section = _rtl8723be_phy_get_ratesection_intxpower_byrate(rfpath,
8858c2ecf20Sopenharmony_ci								       rate);
8868c2ecf20Sopenharmony_ci	tx_num = RF_TX_NUM_NONIMPLEMENT;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	if (tx_num == RF_TX_NUM_NONIMPLEMENT) {
8898c2ecf20Sopenharmony_ci		if (rate >= DESC92C_RATEMCS8 && rate <= DESC92C_RATEMCS15)
8908c2ecf20Sopenharmony_ci			tx_num = RF_2TX;
8918c2ecf20Sopenharmony_ci		else
8928c2ecf20Sopenharmony_ci			tx_num = RF_1TX;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	switch (rate) {
8968c2ecf20Sopenharmony_ci	case DESC92C_RATE6M:
8978c2ecf20Sopenharmony_ci	case DESC92C_RATE24M:
8988c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS0:
8998c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS4:
9008c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS8:
9018c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS12:
9028c2ecf20Sopenharmony_ci		shift = 0;
9038c2ecf20Sopenharmony_ci		break;
9048c2ecf20Sopenharmony_ci	case DESC92C_RATE1M:
9058c2ecf20Sopenharmony_ci	case DESC92C_RATE2M:
9068c2ecf20Sopenharmony_ci	case DESC92C_RATE9M:
9078c2ecf20Sopenharmony_ci	case DESC92C_RATE36M:
9088c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS1:
9098c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS5:
9108c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS9:
9118c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS13:
9128c2ecf20Sopenharmony_ci		shift = 8;
9138c2ecf20Sopenharmony_ci		break;
9148c2ecf20Sopenharmony_ci	case DESC92C_RATE5_5M:
9158c2ecf20Sopenharmony_ci	case DESC92C_RATE12M:
9168c2ecf20Sopenharmony_ci	case DESC92C_RATE48M:
9178c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS2:
9188c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS6:
9198c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS10:
9208c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS14:
9218c2ecf20Sopenharmony_ci		shift = 16;
9228c2ecf20Sopenharmony_ci		break;
9238c2ecf20Sopenharmony_ci	case DESC92C_RATE11M:
9248c2ecf20Sopenharmony_ci	case DESC92C_RATE18M:
9258c2ecf20Sopenharmony_ci	case DESC92C_RATE54M:
9268c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS3:
9278c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS7:
9288c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS11:
9298c2ecf20Sopenharmony_ci	case DESC92C_RATEMCS15:
9308c2ecf20Sopenharmony_ci		shift = 24;
9318c2ecf20Sopenharmony_ci		break;
9328c2ecf20Sopenharmony_ci	default:
9338c2ecf20Sopenharmony_ci		WARN_ONCE(true, "rtl8723be: Rate_Section is Illegal\n");
9348c2ecf20Sopenharmony_ci		break;
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci	tx_pwr_diff = (u8)(rtlphy->tx_power_by_rate_offset[band][rfpath][tx_num]
9378c2ecf20Sopenharmony_ci					  [rate_section] >> shift) & 0xff;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	return	tx_pwr_diff;
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_cistatic u8 _rtl8723be_get_txpower_index(struct ieee80211_hw *hw, u8 path,
9438c2ecf20Sopenharmony_ci				       u8 rate, u8 bandwidth, u8 channel)
9448c2ecf20Sopenharmony_ci{
9458c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
9468c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
9478c2ecf20Sopenharmony_ci	u8 index = (channel - 1);
9488c2ecf20Sopenharmony_ci	u8 txpower = 0;
9498c2ecf20Sopenharmony_ci	u8 power_diff_byrate = 0;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	if (channel > 14 || channel < 1) {
9528c2ecf20Sopenharmony_ci		index = 0;
9538c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
9548c2ecf20Sopenharmony_ci			"Illegal channel!\n");
9558c2ecf20Sopenharmony_ci	}
9568c2ecf20Sopenharmony_ci	if (RX_HAL_IS_CCK_RATE(rate))
9578c2ecf20Sopenharmony_ci		txpower = rtlefuse->txpwrlevel_cck[path][index];
9588c2ecf20Sopenharmony_ci	else if (DESC92C_RATE6M <= rate)
9598c2ecf20Sopenharmony_ci		txpower = rtlefuse->txpwrlevel_ht40_1s[path][index];
9608c2ecf20Sopenharmony_ci	else
9618c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
9628c2ecf20Sopenharmony_ci			"invalid rate\n");
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	if (DESC92C_RATE6M <= rate && rate <= DESC92C_RATE54M &&
9658c2ecf20Sopenharmony_ci	    !RX_HAL_IS_CCK_RATE(rate))
9668c2ecf20Sopenharmony_ci		txpower += rtlefuse->txpwr_legacyhtdiff[0][TX_1S];
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	if (bandwidth == HT_CHANNEL_WIDTH_20) {
9698c2ecf20Sopenharmony_ci		if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
9708c2ecf20Sopenharmony_ci			txpower += rtlefuse->txpwr_ht20diff[0][TX_1S];
9718c2ecf20Sopenharmony_ci		if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
9728c2ecf20Sopenharmony_ci			txpower += rtlefuse->txpwr_ht20diff[0][TX_2S];
9738c2ecf20Sopenharmony_ci	} else if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
9748c2ecf20Sopenharmony_ci		if (DESC92C_RATEMCS0 <= rate && rate <= DESC92C_RATEMCS15)
9758c2ecf20Sopenharmony_ci			txpower += rtlefuse->txpwr_ht40diff[0][TX_1S];
9768c2ecf20Sopenharmony_ci		if (DESC92C_RATEMCS8 <= rate && rate <= DESC92C_RATEMCS15)
9778c2ecf20Sopenharmony_ci			txpower += rtlefuse->txpwr_ht40diff[0][TX_2S];
9788c2ecf20Sopenharmony_ci	}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	if (rtlefuse->eeprom_regulatory != 2)
9818c2ecf20Sopenharmony_ci		power_diff_byrate = _rtl8723be_get_txpower_by_rate(hw,
9828c2ecf20Sopenharmony_ci								   BAND_ON_2_4G,
9838c2ecf20Sopenharmony_ci								   path, rate);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	txpower += power_diff_byrate;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	if (txpower > MAX_POWER_INDEX)
9888c2ecf20Sopenharmony_ci		txpower = MAX_POWER_INDEX;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	return txpower;
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_set_txpower_index(struct ieee80211_hw *hw,
9948c2ecf20Sopenharmony_ci					     u8 power_index, u8 path, u8 rate)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
9978c2ecf20Sopenharmony_ci	if (path == RF90_PATH_A) {
9988c2ecf20Sopenharmony_ci		switch (rate) {
9998c2ecf20Sopenharmony_ci		case DESC92C_RATE1M:
10008c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_CCK1_MCS32,
10018c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10028c2ecf20Sopenharmony_ci			break;
10038c2ecf20Sopenharmony_ci		case DESC92C_RATE2M:
10048c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
10058c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10068c2ecf20Sopenharmony_ci			break;
10078c2ecf20Sopenharmony_ci		case DESC92C_RATE5_5M:
10088c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
10098c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10108c2ecf20Sopenharmony_ci			break;
10118c2ecf20Sopenharmony_ci		case DESC92C_RATE11M:
10128c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_B_CCK11_A_CCK2_11,
10138c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10148c2ecf20Sopenharmony_ci			break;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci		case DESC92C_RATE6M:
10178c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
10188c2ecf20Sopenharmony_ci					       MASKBYTE0, power_index);
10198c2ecf20Sopenharmony_ci			break;
10208c2ecf20Sopenharmony_ci		case DESC92C_RATE9M:
10218c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
10228c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10238c2ecf20Sopenharmony_ci			break;
10248c2ecf20Sopenharmony_ci		case DESC92C_RATE12M:
10258c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
10268c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10278c2ecf20Sopenharmony_ci			break;
10288c2ecf20Sopenharmony_ci		case DESC92C_RATE18M:
10298c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE18_06,
10308c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10318c2ecf20Sopenharmony_ci			break;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci		case DESC92C_RATE24M:
10348c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
10358c2ecf20Sopenharmony_ci					       MASKBYTE0, power_index);
10368c2ecf20Sopenharmony_ci			break;
10378c2ecf20Sopenharmony_ci		case DESC92C_RATE36M:
10388c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
10398c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10408c2ecf20Sopenharmony_ci			break;
10418c2ecf20Sopenharmony_ci		case DESC92C_RATE48M:
10428c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
10438c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10448c2ecf20Sopenharmony_ci			break;
10458c2ecf20Sopenharmony_ci		case DESC92C_RATE54M:
10468c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_RATE54_24,
10478c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10488c2ecf20Sopenharmony_ci			break;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS0:
10518c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
10528c2ecf20Sopenharmony_ci					       MASKBYTE0, power_index);
10538c2ecf20Sopenharmony_ci			break;
10548c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS1:
10558c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
10568c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10578c2ecf20Sopenharmony_ci			break;
10588c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS2:
10598c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
10608c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10618c2ecf20Sopenharmony_ci			break;
10628c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS3:
10638c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS03_MCS00,
10648c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10658c2ecf20Sopenharmony_ci			break;
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS4:
10688c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
10698c2ecf20Sopenharmony_ci					       MASKBYTE0, power_index);
10708c2ecf20Sopenharmony_ci			break;
10718c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS5:
10728c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
10738c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10748c2ecf20Sopenharmony_ci			break;
10758c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS6:
10768c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
10778c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10788c2ecf20Sopenharmony_ci			break;
10798c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS7:
10808c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS07_MCS04,
10818c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10828c2ecf20Sopenharmony_ci			break;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS8:
10858c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
10868c2ecf20Sopenharmony_ci					       MASKBYTE0, power_index);
10878c2ecf20Sopenharmony_ci			break;
10888c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS9:
10898c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
10908c2ecf20Sopenharmony_ci					       MASKBYTE1, power_index);
10918c2ecf20Sopenharmony_ci			break;
10928c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS10:
10938c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
10948c2ecf20Sopenharmony_ci					       MASKBYTE2, power_index);
10958c2ecf20Sopenharmony_ci			break;
10968c2ecf20Sopenharmony_ci		case DESC92C_RATEMCS11:
10978c2ecf20Sopenharmony_ci			rtl8723_phy_set_bb_reg(hw, RTXAGC_A_MCS11_MCS08,
10988c2ecf20Sopenharmony_ci					       MASKBYTE3, power_index);
10998c2ecf20Sopenharmony_ci			break;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci		default:
11028c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid Rate!!\n");
11038c2ecf20Sopenharmony_ci			break;
11048c2ecf20Sopenharmony_ci		}
11058c2ecf20Sopenharmony_ci	} else {
11068c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "Invalid RFPath!!\n");
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_civoid rtl8723be_phy_set_txpower_level(struct ieee80211_hw *hw, u8 channel)
11118c2ecf20Sopenharmony_ci{
11128c2ecf20Sopenharmony_ci	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
11138c2ecf20Sopenharmony_ci	u8 cck_rates[]  = {DESC92C_RATE1M, DESC92C_RATE2M,
11148c2ecf20Sopenharmony_ci			   DESC92C_RATE5_5M, DESC92C_RATE11M};
11158c2ecf20Sopenharmony_ci	u8 ofdm_rates[]  = {DESC92C_RATE6M, DESC92C_RATE9M,
11168c2ecf20Sopenharmony_ci			    DESC92C_RATE12M, DESC92C_RATE18M,
11178c2ecf20Sopenharmony_ci			    DESC92C_RATE24M, DESC92C_RATE36M,
11188c2ecf20Sopenharmony_ci			    DESC92C_RATE48M, DESC92C_RATE54M};
11198c2ecf20Sopenharmony_ci	u8 ht_rates_1t[]  = {DESC92C_RATEMCS0, DESC92C_RATEMCS1,
11208c2ecf20Sopenharmony_ci			     DESC92C_RATEMCS2, DESC92C_RATEMCS3,
11218c2ecf20Sopenharmony_ci			     DESC92C_RATEMCS4, DESC92C_RATEMCS5,
11228c2ecf20Sopenharmony_ci			     DESC92C_RATEMCS6, DESC92C_RATEMCS7};
11238c2ecf20Sopenharmony_ci	u8 i;
11248c2ecf20Sopenharmony_ci	u8 power_index;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	if (!rtlefuse->txpwr_fromeprom)
11278c2ecf20Sopenharmony_ci		return;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cck_rates); i++) {
11308c2ecf20Sopenharmony_ci		power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
11318c2ecf20Sopenharmony_ci					cck_rates[i],
11328c2ecf20Sopenharmony_ci					rtl_priv(hw)->phy.current_chan_bw,
11338c2ecf20Sopenharmony_ci					channel);
11348c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
11358c2ecf20Sopenharmony_ci						 cck_rates[i]);
11368c2ecf20Sopenharmony_ci	}
11378c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ofdm_rates); i++) {
11388c2ecf20Sopenharmony_ci		power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
11398c2ecf20Sopenharmony_ci					ofdm_rates[i],
11408c2ecf20Sopenharmony_ci					rtl_priv(hw)->phy.current_chan_bw,
11418c2ecf20Sopenharmony_ci					channel);
11428c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
11438c2ecf20Sopenharmony_ci						 ofdm_rates[i]);
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ht_rates_1t); i++) {
11468c2ecf20Sopenharmony_ci		power_index = _rtl8723be_get_txpower_index(hw, RF90_PATH_A,
11478c2ecf20Sopenharmony_ci					ht_rates_1t[i],
11488c2ecf20Sopenharmony_ci					rtl_priv(hw)->phy.current_chan_bw,
11498c2ecf20Sopenharmony_ci					channel);
11508c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_txpower_index(hw, power_index, RF90_PATH_A,
11518c2ecf20Sopenharmony_ci						 ht_rates_1t[i]);
11528c2ecf20Sopenharmony_ci	}
11538c2ecf20Sopenharmony_ci}
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_civoid rtl8723be_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
11568c2ecf20Sopenharmony_ci{
11578c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11588c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
11598c2ecf20Sopenharmony_ci	enum io_type iotype;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	if (!is_hal_stop(rtlhal)) {
11628c2ecf20Sopenharmony_ci		switch (operation) {
11638c2ecf20Sopenharmony_ci		case SCAN_OPT_BACKUP_BAND0:
11648c2ecf20Sopenharmony_ci			iotype = IO_CMD_PAUSE_BAND0_DM_BY_SCAN;
11658c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
11668c2ecf20Sopenharmony_ci						      (u8 *)&iotype);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci			break;
11698c2ecf20Sopenharmony_ci		case SCAN_OPT_RESTORE:
11708c2ecf20Sopenharmony_ci			iotype = IO_CMD_RESUME_DM_BY_SCAN;
11718c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_IO_CMD,
11728c2ecf20Sopenharmony_ci						      (u8 *)&iotype);
11738c2ecf20Sopenharmony_ci			break;
11748c2ecf20Sopenharmony_ci		default:
11758c2ecf20Sopenharmony_ci			pr_err("Unknown Scan Backup operation.\n");
11768c2ecf20Sopenharmony_ci			break;
11778c2ecf20Sopenharmony_ci		}
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_civoid rtl8723be_phy_set_bw_mode_callback(struct ieee80211_hw *hw)
11828c2ecf20Sopenharmony_ci{
11838c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
11848c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
11858c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
11868c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
11878c2ecf20Sopenharmony_ci	u8 reg_bw_opmode;
11888c2ecf20Sopenharmony_ci	u8 reg_prsr_rsc;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
11918c2ecf20Sopenharmony_ci		"Switch to %s bandwidth\n",
11928c2ecf20Sopenharmony_ci		rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20 ?
11938c2ecf20Sopenharmony_ci		"20MHz" : "40MHz");
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	if (is_hal_stop(rtlhal)) {
11968c2ecf20Sopenharmony_ci		rtlphy->set_bwmode_inprogress = false;
11978c2ecf20Sopenharmony_ci		return;
11988c2ecf20Sopenharmony_ci	}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	reg_bw_opmode = rtl_read_byte(rtlpriv, REG_BWOPMODE);
12018c2ecf20Sopenharmony_ci	reg_prsr_rsc = rtl_read_byte(rtlpriv, REG_RRSR + 2);
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	switch (rtlphy->current_chan_bw) {
12048c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
12058c2ecf20Sopenharmony_ci		reg_bw_opmode |= BW_OPMODE_20MHZ;
12068c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
12078c2ecf20Sopenharmony_ci		break;
12088c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
12098c2ecf20Sopenharmony_ci		reg_bw_opmode &= ~BW_OPMODE_20MHZ;
12108c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
12118c2ecf20Sopenharmony_ci		reg_prsr_rsc = (reg_prsr_rsc & 0x90) |
12128c2ecf20Sopenharmony_ci			       (mac->cur_40_prime_sc << 5);
12138c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_prsr_rsc);
12148c2ecf20Sopenharmony_ci		break;
12158c2ecf20Sopenharmony_ci	default:
12168c2ecf20Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n",
12178c2ecf20Sopenharmony_ci		       rtlphy->current_chan_bw);
12188c2ecf20Sopenharmony_ci		break;
12198c2ecf20Sopenharmony_ci	}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	switch (rtlphy->current_chan_bw) {
12228c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20:
12238c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x0);
12248c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x0);
12258c2ecf20Sopenharmony_ci	/*	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1);*/
12268c2ecf20Sopenharmony_ci		break;
12278c2ecf20Sopenharmony_ci	case HT_CHANNEL_WIDTH_20_40:
12288c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA0_RFMOD, BRFMOD, 0x1);
12298c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RFPGA1_RFMOD, BRFMOD, 0x1);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RCCK0_SYSTEM, BCCK_SIDEBAND,
12328c2ecf20Sopenharmony_ci			      (mac->cur_40_prime_sc >> 1));
12338c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM1_LSTF, 0xC00, mac->cur_40_prime_sc);
12348c2ecf20Sopenharmony_ci		/*rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 0);*/
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0x818, (BIT(26) | BIT(27)),
12378c2ecf20Sopenharmony_ci			      (mac->cur_40_prime_sc ==
12388c2ecf20Sopenharmony_ci			       HAL_PRIME_CHNL_OFFSET_LOWER) ? 2 : 1);
12398c2ecf20Sopenharmony_ci		break;
12408c2ecf20Sopenharmony_ci	default:
12418c2ecf20Sopenharmony_ci		pr_err("unknown bandwidth: %#X\n",
12428c2ecf20Sopenharmony_ci		       rtlphy->current_chan_bw);
12438c2ecf20Sopenharmony_ci		break;
12448c2ecf20Sopenharmony_ci	}
12458c2ecf20Sopenharmony_ci	rtl8723be_phy_rf6052_set_bandwidth(hw, rtlphy->current_chan_bw);
12468c2ecf20Sopenharmony_ci	rtlphy->set_bwmode_inprogress = false;
12478c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD, "\n");
12488c2ecf20Sopenharmony_ci}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_civoid rtl8723be_phy_set_bw_mode(struct ieee80211_hw *hw,
12518c2ecf20Sopenharmony_ci			    enum nl80211_channel_type ch_type)
12528c2ecf20Sopenharmony_ci{
12538c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12548c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
12558c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
12568c2ecf20Sopenharmony_ci	u8 tmp_bw = rtlphy->current_chan_bw;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	if (rtlphy->set_bwmode_inprogress)
12598c2ecf20Sopenharmony_ci		return;
12608c2ecf20Sopenharmony_ci	rtlphy->set_bwmode_inprogress = true;
12618c2ecf20Sopenharmony_ci	if ((!is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
12628c2ecf20Sopenharmony_ci		rtl8723be_phy_set_bw_mode_callback(hw);
12638c2ecf20Sopenharmony_ci	} else {
12648c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
12658c2ecf20Sopenharmony_ci			"false driver sleep or unload\n");
12668c2ecf20Sopenharmony_ci		rtlphy->set_bwmode_inprogress = false;
12678c2ecf20Sopenharmony_ci		rtlphy->current_chan_bw = tmp_bw;
12688c2ecf20Sopenharmony_ci	}
12698c2ecf20Sopenharmony_ci}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_civoid rtl8723be_phy_sw_chnl_callback(struct ieee80211_hw *hw)
12728c2ecf20Sopenharmony_ci{
12738c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
12748c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
12758c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
12768c2ecf20Sopenharmony_ci	u32 delay = 0;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE,
12798c2ecf20Sopenharmony_ci		"switch to channel%d\n", rtlphy->current_channel);
12808c2ecf20Sopenharmony_ci	if (is_hal_stop(rtlhal))
12818c2ecf20Sopenharmony_ci		return;
12828c2ecf20Sopenharmony_ci	do {
12838c2ecf20Sopenharmony_ci		if (!rtlphy->sw_chnl_inprogress)
12848c2ecf20Sopenharmony_ci			break;
12858c2ecf20Sopenharmony_ci		if (!_rtl8723be_phy_sw_chnl_step_by_step(hw,
12868c2ecf20Sopenharmony_ci							 rtlphy->current_channel,
12878c2ecf20Sopenharmony_ci							 &rtlphy->sw_chnl_stage,
12888c2ecf20Sopenharmony_ci							 &rtlphy->sw_chnl_step,
12898c2ecf20Sopenharmony_ci							 &delay)) {
12908c2ecf20Sopenharmony_ci			if (delay > 0)
12918c2ecf20Sopenharmony_ci				mdelay(delay);
12928c2ecf20Sopenharmony_ci			else
12938c2ecf20Sopenharmony_ci				continue;
12948c2ecf20Sopenharmony_ci		} else {
12958c2ecf20Sopenharmony_ci			rtlphy->sw_chnl_inprogress = false;
12968c2ecf20Sopenharmony_ci		}
12978c2ecf20Sopenharmony_ci		break;
12988c2ecf20Sopenharmony_ci	} while (true);
12998c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_SCAN, DBG_TRACE, "\n");
13008c2ecf20Sopenharmony_ci}
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ciu8 rtl8723be_phy_sw_chnl(struct ieee80211_hw *hw)
13038c2ecf20Sopenharmony_ci{
13048c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
13058c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
13068c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	if (rtlphy->sw_chnl_inprogress)
13098c2ecf20Sopenharmony_ci		return 0;
13108c2ecf20Sopenharmony_ci	if (rtlphy->set_bwmode_inprogress)
13118c2ecf20Sopenharmony_ci		return 0;
13128c2ecf20Sopenharmony_ci	WARN_ONCE((rtlphy->current_channel > 14),
13138c2ecf20Sopenharmony_ci		  "rtl8723be: WIRELESS_MODE_G but channel>14");
13148c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_inprogress = true;
13158c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_stage = 0;
13168c2ecf20Sopenharmony_ci	rtlphy->sw_chnl_step = 0;
13178c2ecf20Sopenharmony_ci	if (!(is_hal_stop(rtlhal)) && !(RT_CANNOT_IO(hw))) {
13188c2ecf20Sopenharmony_ci		rtl8723be_phy_sw_chnl_callback(hw);
13198c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
13208c2ecf20Sopenharmony_ci			"sw_chnl_inprogress false schedule workitem current channel %d\n",
13218c2ecf20Sopenharmony_ci			rtlphy->current_channel);
13228c2ecf20Sopenharmony_ci		rtlphy->sw_chnl_inprogress = false;
13238c2ecf20Sopenharmony_ci	} else {
13248c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CHAN, DBG_LOUD,
13258c2ecf20Sopenharmony_ci			"sw_chnl_inprogress false driver sleep or unload\n");
13268c2ecf20Sopenharmony_ci		rtlphy->sw_chnl_inprogress = false;
13278c2ecf20Sopenharmony_ci	}
13288c2ecf20Sopenharmony_ci	return 1;
13298c2ecf20Sopenharmony_ci}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_sw_chnl_step_by_step(struct ieee80211_hw *hw,
13328c2ecf20Sopenharmony_ci						u8 channel, u8 *stage,
13338c2ecf20Sopenharmony_ci						u8 *step, u32 *delay)
13348c2ecf20Sopenharmony_ci{
13358c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
13368c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
13378c2ecf20Sopenharmony_ci	struct swchnlcmd precommoncmd[MAX_PRECMD_CNT];
13388c2ecf20Sopenharmony_ci	u32 precommoncmdcnt;
13398c2ecf20Sopenharmony_ci	struct swchnlcmd postcommoncmd[MAX_POSTCMD_CNT];
13408c2ecf20Sopenharmony_ci	u32 postcommoncmdcnt;
13418c2ecf20Sopenharmony_ci	struct swchnlcmd rfdependcmd[MAX_RFDEPENDCMD_CNT];
13428c2ecf20Sopenharmony_ci	u32 rfdependcmdcnt;
13438c2ecf20Sopenharmony_ci	struct swchnlcmd *currentcmd = NULL;
13448c2ecf20Sopenharmony_ci	u8 rfpath;
13458c2ecf20Sopenharmony_ci	u8 num_total_rfpath = rtlphy->num_total_rfpath;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	precommoncmdcnt = 0;
13488c2ecf20Sopenharmony_ci	rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
13498c2ecf20Sopenharmony_ci					 MAX_PRECMD_CNT,
13508c2ecf20Sopenharmony_ci					 CMDID_SET_TXPOWEROWER_LEVEL,
13518c2ecf20Sopenharmony_ci					 0, 0, 0);
13528c2ecf20Sopenharmony_ci	rtl8723_phy_set_sw_chnl_cmdarray(precommoncmd, precommoncmdcnt++,
13538c2ecf20Sopenharmony_ci					 MAX_PRECMD_CNT, CMDID_END, 0, 0, 0);
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	postcommoncmdcnt = 0;
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	rtl8723_phy_set_sw_chnl_cmdarray(postcommoncmd, postcommoncmdcnt++,
13588c2ecf20Sopenharmony_ci					 MAX_POSTCMD_CNT, CMDID_END,
13598c2ecf20Sopenharmony_ci					    0, 0, 0);
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	rfdependcmdcnt = 0;
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	WARN_ONCE((channel < 1 || channel > 14),
13648c2ecf20Sopenharmony_ci		  "rtl8723be: illegal channel for Zebra: %d\n", channel);
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
13678c2ecf20Sopenharmony_ci					 MAX_RFDEPENDCMD_CNT,
13688c2ecf20Sopenharmony_ci					 CMDID_RF_WRITEREG,
13698c2ecf20Sopenharmony_ci					 RF_CHNLBW, channel, 10);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	rtl8723_phy_set_sw_chnl_cmdarray(rfdependcmd, rfdependcmdcnt++,
13728c2ecf20Sopenharmony_ci					 MAX_RFDEPENDCMD_CNT,
13738c2ecf20Sopenharmony_ci					    CMDID_END, 0, 0, 0);
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	do {
13768c2ecf20Sopenharmony_ci		switch (*stage) {
13778c2ecf20Sopenharmony_ci		case 0:
13788c2ecf20Sopenharmony_ci			currentcmd = &precommoncmd[*step];
13798c2ecf20Sopenharmony_ci			break;
13808c2ecf20Sopenharmony_ci		case 1:
13818c2ecf20Sopenharmony_ci			currentcmd = &rfdependcmd[*step];
13828c2ecf20Sopenharmony_ci			break;
13838c2ecf20Sopenharmony_ci		case 2:
13848c2ecf20Sopenharmony_ci			currentcmd = &postcommoncmd[*step];
13858c2ecf20Sopenharmony_ci			break;
13868c2ecf20Sopenharmony_ci		default:
13878c2ecf20Sopenharmony_ci			pr_err("Invalid 'stage' = %d, Check it!\n",
13888c2ecf20Sopenharmony_ci			       *stage);
13898c2ecf20Sopenharmony_ci			return true;
13908c2ecf20Sopenharmony_ci		}
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci		if (currentcmd->cmdid == CMDID_END) {
13938c2ecf20Sopenharmony_ci			if ((*stage) == 2) {
13948c2ecf20Sopenharmony_ci				return true;
13958c2ecf20Sopenharmony_ci			} else {
13968c2ecf20Sopenharmony_ci				(*stage)++;
13978c2ecf20Sopenharmony_ci				(*step) = 0;
13988c2ecf20Sopenharmony_ci				continue;
13998c2ecf20Sopenharmony_ci			}
14008c2ecf20Sopenharmony_ci		}
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci		switch (currentcmd->cmdid) {
14038c2ecf20Sopenharmony_ci		case CMDID_SET_TXPOWEROWER_LEVEL:
14048c2ecf20Sopenharmony_ci			rtl8723be_phy_set_txpower_level(hw, channel);
14058c2ecf20Sopenharmony_ci			break;
14068c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_ULONG:
14078c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, currentcmd->para1,
14088c2ecf20Sopenharmony_ci					currentcmd->para2);
14098c2ecf20Sopenharmony_ci			break;
14108c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_USHORT:
14118c2ecf20Sopenharmony_ci			rtl_write_word(rtlpriv, currentcmd->para1,
14128c2ecf20Sopenharmony_ci				       (u16)currentcmd->para2);
14138c2ecf20Sopenharmony_ci			break;
14148c2ecf20Sopenharmony_ci		case CMDID_WRITEPORT_UCHAR:
14158c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, currentcmd->para1,
14168c2ecf20Sopenharmony_ci				       (u8)currentcmd->para2);
14178c2ecf20Sopenharmony_ci			break;
14188c2ecf20Sopenharmony_ci		case CMDID_RF_WRITEREG:
14198c2ecf20Sopenharmony_ci			for (rfpath = 0; rfpath < num_total_rfpath; rfpath++) {
14208c2ecf20Sopenharmony_ci				rtlphy->rfreg_chnlval[rfpath] =
14218c2ecf20Sopenharmony_ci				    ((rtlphy->rfreg_chnlval[rfpath] &
14228c2ecf20Sopenharmony_ci				      0xfffffc00) | currentcmd->para2);
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci				rtl_set_rfreg(hw, (enum radio_path)rfpath,
14258c2ecf20Sopenharmony_ci					      currentcmd->para1,
14268c2ecf20Sopenharmony_ci					      RFREG_OFFSET_MASK,
14278c2ecf20Sopenharmony_ci					      rtlphy->rfreg_chnlval[rfpath]);
14288c2ecf20Sopenharmony_ci			}
14298c2ecf20Sopenharmony_ci			break;
14308c2ecf20Sopenharmony_ci		default:
14318c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
14328c2ecf20Sopenharmony_ci				"switch case %#x not processed\n",
14338c2ecf20Sopenharmony_ci				currentcmd->cmdid);
14348c2ecf20Sopenharmony_ci			break;
14358c2ecf20Sopenharmony_ci		}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci		break;
14388c2ecf20Sopenharmony_ci	} while (true);
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	(*delay) = currentcmd->msdelay;
14418c2ecf20Sopenharmony_ci	(*step)++;
14428c2ecf20Sopenharmony_ci	return false;
14438c2ecf20Sopenharmony_ci}
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_path_a_iqk(struct ieee80211_hw *hw)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	u32 reg_eac, reg_e94, reg_e9c, tmp;
14488c2ecf20Sopenharmony_ci	u8 result = 0x00;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	/* leave IQK mode */
14518c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
14528c2ecf20Sopenharmony_ci	/* switch to path A */
14538c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
14548c2ecf20Sopenharmony_ci	/* enable path A PA in TXIQK mode */
14558c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
14568c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x20000);
14578c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0003f);
14588c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xc7f87);
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	/* 1. TX IQK */
14618c2ecf20Sopenharmony_ci	/* path-A IQK setting */
14628c2ecf20Sopenharmony_ci	/* IQK setting */
14638c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
14648c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
14658c2ecf20Sopenharmony_ci	/* path-A IQK setting */
14668c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
14678c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
14688c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
14698c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
14728c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28160000);
14738c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
14748c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
14758c2ecf20Sopenharmony_ci	/* LO calibration setting */
14768c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
14778c2ecf20Sopenharmony_ci	/* enter IQK mode */
14788c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
14798c2ecf20Sopenharmony_ci
14808c2ecf20Sopenharmony_ci	/* One shot, path A LOK & IQK */
14818c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
14828c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	/* leave IQK mode */
14878c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	/* Check failed */
14908c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, 0xeac, MASKDWORD);
14918c2ecf20Sopenharmony_ci	reg_e94 = rtl_get_bbreg(hw, 0xe94, MASKDWORD);
14928c2ecf20Sopenharmony_ci	reg_e9c = rtl_get_bbreg(hw, 0xe9c, MASKDWORD);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
14958c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
14968c2ecf20Sopenharmony_ci	    (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
14978c2ecf20Sopenharmony_ci		result |= 0x01;
14988c2ecf20Sopenharmony_ci	else /* if Tx not OK, ignore Rx */
14998c2ecf20Sopenharmony_ci		return result;
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	/* Allen 20131125 */
15028c2ecf20Sopenharmony_ci	tmp = (reg_e9c & 0x03FF0000) >> 16;
15038c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
15048c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
15078c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
15088c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
15098c2ecf20Sopenharmony_ci	    (tmp < 0xf))
15108c2ecf20Sopenharmony_ci		result |= 0x01;
15118c2ecf20Sopenharmony_ci	else /* if Tx not OK, ignore Rx */
15128c2ecf20Sopenharmony_ci		return result;
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	return result;
15158c2ecf20Sopenharmony_ci}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
15188c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_path_a_rx_iqk(struct ieee80211_hw *hw)
15198c2ecf20Sopenharmony_ci{
15208c2ecf20Sopenharmony_ci	u32 reg_eac, reg_e94, reg_e9c, reg_ea4, u32tmp, tmp;
15218c2ecf20Sopenharmony_ci	u8 result = 0x00;
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	/* leave IQK mode */
15248c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci	/* switch to path A */
15278c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000000);
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	/* 1 Get TXIMR setting */
15308c2ecf20Sopenharmony_ci	/* modify RXIQK mode table */
15318c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
15328c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
15338c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
15348c2ecf20Sopenharmony_ci	/* LNA2 off, PA on for Dcut */
15358c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7fb7);
15368c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci	/* IQK setting */
15398c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
15408c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	/* path-A IQK setting */
15438c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
15448c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
15458c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
15468c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
15498c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
15508c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
15518c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	/* LO calibration setting */
15548c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/* enter IQK mode */
15578c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	/* One shot, path A LOK & IQK */
15608c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
15618c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	/* leave IQK mode */
15668c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	/* Check failed */
15698c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
15708c2ecf20Sopenharmony_ci	reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
15718c2ecf20Sopenharmony_ci	reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
15748c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
15758c2ecf20Sopenharmony_ci	    (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
15768c2ecf20Sopenharmony_ci		result |= 0x01;
15778c2ecf20Sopenharmony_ci	else /* if Tx not OK, ignore Rx */
15788c2ecf20Sopenharmony_ci		return result;
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	/* Allen 20131125 */
15818c2ecf20Sopenharmony_ci	tmp = (reg_e9c & 0x03FF0000) >> 16;
15828c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
15838c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
15868c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
15878c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
15888c2ecf20Sopenharmony_ci	    (tmp < 0xf))
15898c2ecf20Sopenharmony_ci		result |= 0x01;
15908c2ecf20Sopenharmony_ci	else /* if Tx not OK, ignore Rx */
15918c2ecf20Sopenharmony_ci		return result;
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000) |
15948c2ecf20Sopenharmony_ci		 ((reg_e9c & 0x3FF0000) >> 16);
15958c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	/* 1 RX IQK */
15988c2ecf20Sopenharmony_ci	/* modify RXIQK mode table */
15998c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
16008c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
16018c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
16028c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
16038c2ecf20Sopenharmony_ci	/* LAN2 on, PA off for Dcut */
16048c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	/* PA, PAD setting */
16078c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0xf80);
16088c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x55, RFREG_OFFSET_MASK, 0x4021f);
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	/* IQK setting */
16118c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	/* path-A IQK setting */
16148c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
16158c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
16168c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
16178c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
16208c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
16218c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
16228c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci	/* LO calibration setting */
16258c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	/* enter IQK mode */
16288c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
16298c2ecf20Sopenharmony_ci
16308c2ecf20Sopenharmony_ci	/* One shot, path A LOK & IQK */
16318c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
16328c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	/* leave IQK mode */
16378c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	/* Check failed */
16408c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
16418c2ecf20Sopenharmony_ci	reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	/* leave IQK mode */
16448c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
16458c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xdf, RFREG_OFFSET_MASK, 0x780);
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	/* Allen 20131125 */
16488c2ecf20Sopenharmony_ci	tmp = (reg_eac & 0x03FF0000) >> 16;
16498c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
16508c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
16518c2ecf20Sopenharmony_ci	/* if Tx is OK, check whether Rx is OK */
16528c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(27)) &&
16538c2ecf20Sopenharmony_ci	    (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
16548c2ecf20Sopenharmony_ci	    (((reg_eac & 0x03FF0000) >> 16) != 0x36))
16558c2ecf20Sopenharmony_ci		result |= 0x02;
16568c2ecf20Sopenharmony_ci	else if (!(reg_eac & BIT(27)) &&
16578c2ecf20Sopenharmony_ci		 (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
16588c2ecf20Sopenharmony_ci		 (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
16598c2ecf20Sopenharmony_ci		 (tmp < 0xf))
16608c2ecf20Sopenharmony_ci		result |= 0x02;
16618c2ecf20Sopenharmony_ci
16628c2ecf20Sopenharmony_ci	return result;
16638c2ecf20Sopenharmony_ci}
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_path_b_iqk(struct ieee80211_hw *hw)
16668c2ecf20Sopenharmony_ci{
16678c2ecf20Sopenharmony_ci	u32 reg_eac, reg_e94, reg_e9c, tmp;
16688c2ecf20Sopenharmony_ci	u8 result = 0x00;
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci	/* leave IQK mode */
16718c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
16728c2ecf20Sopenharmony_ci	/* switch to path B */
16738c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	/* enable path B PA in TXIQK mode */
16768c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
16778c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x40fc1);
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	/* 1 Tx IQK */
16808c2ecf20Sopenharmony_ci	/* IQK setting */
16818c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
16828c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
16838c2ecf20Sopenharmony_ci	/* path-A IQK setting */
16848c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
16858c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
16868c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
16878c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x821403ea);
16908c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
16918c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
16928c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	/* LO calibration setting */
16958c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x00462911);
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci	/* enter IQK mode */
16988c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_ci	/* One shot, path B LOK & IQK */
17018c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
17028c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	/* leave IQK mode */
17078c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	/* Check failed */
17108c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
17118c2ecf20Sopenharmony_ci	reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
17128c2ecf20Sopenharmony_ci	reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
17158c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
17168c2ecf20Sopenharmony_ci	    (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
17178c2ecf20Sopenharmony_ci		result |= 0x01;
17188c2ecf20Sopenharmony_ci	else
17198c2ecf20Sopenharmony_ci		return result;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	/* Allen 20131125 */
17228c2ecf20Sopenharmony_ci	tmp = (reg_e9c & 0x03FF0000) >> 16;
17238c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
17248c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
17278c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
17288c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
17298c2ecf20Sopenharmony_ci	    (tmp < 0xf))
17308c2ecf20Sopenharmony_ci		result |= 0x01;
17318c2ecf20Sopenharmony_ci	else
17328c2ecf20Sopenharmony_ci		return result;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	return result;
17358c2ecf20Sopenharmony_ci}
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci/* bit0 = 1 => Tx OK, bit1 = 1 => Rx OK */
17388c2ecf20Sopenharmony_cistatic u8 _rtl8723be_phy_path_b_rx_iqk(struct ieee80211_hw *hw)
17398c2ecf20Sopenharmony_ci{
17408c2ecf20Sopenharmony_ci	u32 reg_e94, reg_e9c, reg_ea4, reg_eac, u32tmp, tmp;
17418c2ecf20Sopenharmony_ci	u8 result = 0x00;
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci	/* leave IQK mode */
17448c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
17458c2ecf20Sopenharmony_ci	/* switch to path B */
17468c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x948, MASKDWORD, 0x00000280);
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	/* 1 Get TXIMR setting */
17498c2ecf20Sopenharmony_ci	/* modify RXIQK mode table */
17508c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, RFREG_OFFSET_MASK, 0x800a0);
17518c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
17528c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
17538c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7ff7);
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	/* open PA S1 & SMIXER */
17568c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
17578c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fed);
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	/* IQK setting */
17608c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, 0x01007c00);
17618c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	/* path-B IQK setting */
17648c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
17658c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
17668c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
17678c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82160ff0);
17708c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x28110000);
17718c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
17728c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci	/* LO calibration setting */
17758c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a911);
17768c2ecf20Sopenharmony_ci	/* enter IQK mode */
17778c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	/* One shot, path B TXIQK @ RXIQK */
17808c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
17818c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	/* leave IQK mode */
17868c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
17878c2ecf20Sopenharmony_ci	/* Check failed */
17888c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
17898c2ecf20Sopenharmony_ci	reg_e94 = rtl_get_bbreg(hw, RTX_POWER_BEFORE_IQK_A, MASKDWORD);
17908c2ecf20Sopenharmony_ci	reg_e9c = rtl_get_bbreg(hw, RTX_POWER_AFTER_IQK_A, MASKDWORD);
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
17938c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) != 0x142) &&
17948c2ecf20Sopenharmony_ci	    (((reg_e9c & 0x03FF0000) >> 16) != 0x42))
17958c2ecf20Sopenharmony_ci		result |= 0x01;
17968c2ecf20Sopenharmony_ci	else	/* if Tx not OK, ignore Rx */
17978c2ecf20Sopenharmony_ci		return result;
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci	/* Allen 20131125 */
18008c2ecf20Sopenharmony_ci	tmp = (reg_e9c & 0x03FF0000) >> 16;
18018c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
18028c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(28)) &&
18058c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) < 0x110) &&
18068c2ecf20Sopenharmony_ci	    (((reg_e94 & 0x03FF0000) >> 16) > 0xf0) &&
18078c2ecf20Sopenharmony_ci	    (tmp < 0xf))
18088c2ecf20Sopenharmony_ci		result |= 0x01;
18098c2ecf20Sopenharmony_ci	else
18108c2ecf20Sopenharmony_ci		return result;
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	u32tmp = 0x80007C00 | (reg_e94 & 0x3FF0000)  |
18138c2ecf20Sopenharmony_ci		 ((reg_e9c & 0x3FF0000) >> 16);
18148c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK, MASKDWORD, u32tmp);
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	/* 1 RX IQK */
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci	/* <20121009, Kordan> RF Mode = 3 */
18198c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
18208c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x1);
18218c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK_OS, RFREG_OFFSET_MASK, 0x30000);
18228c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G1, RFREG_OFFSET_MASK, 0x0001f);
18238c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_TXPA_G2, RFREG_OFFSET_MASK, 0xf7d77);
18248c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, RF_WE_LUT, 0x80000, 0x0);
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	/* open PA S1 & close SMIXER */
18278c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xed, RFREG_OFFSET_MASK, 0x00020);
18288c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x43, RFREG_OFFSET_MASK, 0x60fbd);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	/* IQK setting */
18318c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK, MASKDWORD, 0x01004800);
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	/* path-B IQK setting */
18348c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_A, MASKDWORD, 0x38008c1c);
18358c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_A, MASKDWORD, 0x18008c1c);
18368c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
18378c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_TONE_B, MASKDWORD, 0x38008c1c);
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_A, MASKDWORD, 0x82110000);
18408c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_A, MASKDWORD, 0x2816001f);
18418c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RTX_IQK_PI_B, MASKDWORD, 0x82110000);
18428c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RRX_IQK_PI_B, MASKDWORD, 0x28110000);
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	/* LO calibration setting */
18458c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_RSP, MASKDWORD, 0x0046a8d1);
18468c2ecf20Sopenharmony_ci	/* enter IQK mode */
18478c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x80800000);
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	/* One shot, path B LOK & IQK */
18508c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf9000000);
18518c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RIQK_AGC_PTS, MASKDWORD, 0xf8000000);
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	mdelay(IQK_DELAY_TIME);
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	/* leave IQK mode */
18568c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0x00000000);
18578c2ecf20Sopenharmony_ci	/* Check failed */
18588c2ecf20Sopenharmony_ci	reg_eac = rtl_get_bbreg(hw, RRX_POWER_AFTER_IQK_A_2, MASKDWORD);
18598c2ecf20Sopenharmony_ci	reg_ea4 = rtl_get_bbreg(hw, RRX_POWER_BEFORE_IQK_A_2, MASKDWORD);
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	/* Allen 20131125 */
18628c2ecf20Sopenharmony_ci	tmp = (reg_eac & 0x03FF0000) >> 16;
18638c2ecf20Sopenharmony_ci	if ((tmp & 0x200) > 0)
18648c2ecf20Sopenharmony_ci		tmp = 0x400 - tmp;
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_ci	/* if Tx is OK, check whether Rx is OK */
18678c2ecf20Sopenharmony_ci	if (!(reg_eac & BIT(27)) &&
18688c2ecf20Sopenharmony_ci	    (((reg_ea4 & 0x03FF0000) >> 16) != 0x132) &&
18698c2ecf20Sopenharmony_ci	    (((reg_eac & 0x03FF0000) >> 16) != 0x36))
18708c2ecf20Sopenharmony_ci		result |= 0x02;
18718c2ecf20Sopenharmony_ci	else if (!(reg_eac & BIT(27)) &&
18728c2ecf20Sopenharmony_ci		 (((reg_ea4 & 0x03FF0000) >> 16) < 0x110) &&
18738c2ecf20Sopenharmony_ci		 (((reg_ea4 & 0x03FF0000) >> 16) > 0xf0) &&
18748c2ecf20Sopenharmony_ci		 (tmp < 0xf))
18758c2ecf20Sopenharmony_ci		result |= 0x02;
18768c2ecf20Sopenharmony_ci	else
18778c2ecf20Sopenharmony_ci		return result;
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci	return result;
18808c2ecf20Sopenharmony_ci}
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_path_b_fill_iqk_matrix(struct ieee80211_hw *hw,
18838c2ecf20Sopenharmony_ci						  bool b_iqk_ok,
18848c2ecf20Sopenharmony_ci						  long result[][8],
18858c2ecf20Sopenharmony_ci						  u8 final_candidate,
18868c2ecf20Sopenharmony_ci						  bool btxonly)
18878c2ecf20Sopenharmony_ci{
18888c2ecf20Sopenharmony_ci	u32 oldval_1, x, tx1_a, reg;
18898c2ecf20Sopenharmony_ci	long y, tx1_c;
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	if (final_candidate == 0xFF) {
18928c2ecf20Sopenharmony_ci		return;
18938c2ecf20Sopenharmony_ci	} else if (b_iqk_ok) {
18948c2ecf20Sopenharmony_ci		oldval_1 = (rtl_get_bbreg(hw, ROFDM0_XBTXIQIMBALANCE,
18958c2ecf20Sopenharmony_ci					  MASKDWORD) >> 22) & 0x3FF;
18968c2ecf20Sopenharmony_ci		x = result[final_candidate][4];
18978c2ecf20Sopenharmony_ci		if ((x & 0x00000200) != 0)
18988c2ecf20Sopenharmony_ci			x = x | 0xFFFFFC00;
18998c2ecf20Sopenharmony_ci		tx1_a = (x * oldval_1) >> 8;
19008c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x3FF, tx1_a);
19018c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(27),
19028c2ecf20Sopenharmony_ci			      ((x * oldval_1 >> 7) & 0x1));
19038c2ecf20Sopenharmony_ci		y = result[final_candidate][5];
19048c2ecf20Sopenharmony_ci		if ((y & 0x00000200) != 0)
19058c2ecf20Sopenharmony_ci			y = y | 0xFFFFFC00;
19068c2ecf20Sopenharmony_ci		tx1_c = (y * oldval_1) >> 8;
19078c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XDTXAFE, 0xF0000000,
19088c2ecf20Sopenharmony_ci			      ((tx1_c & 0x3C0) >> 6));
19098c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBTXIQIMBALANCE, 0x003F0000,
19108c2ecf20Sopenharmony_ci			      (tx1_c & 0x3F));
19118c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_ECCATHRESHOLD, BIT(25),
19128c2ecf20Sopenharmony_ci			      ((y * oldval_1 >> 7) & 0x1));
19138c2ecf20Sopenharmony_ci		if (btxonly)
19148c2ecf20Sopenharmony_ci			return;
19158c2ecf20Sopenharmony_ci		reg = result[final_candidate][6];
19168c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0x3FF, reg);
19178c2ecf20Sopenharmony_ci		reg = result[final_candidate][7] & 0x3F;
19188c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, ROFDM0_XBRXIQIMBALANCE, 0xFC00, reg);
19198c2ecf20Sopenharmony_ci		reg = (result[final_candidate][7] >> 6) & 0xF;
19208c2ecf20Sopenharmony_ci		/* rtl_set_bbreg(hw, 0xca0, 0xF0000000, reg); */
19218c2ecf20Sopenharmony_ci	}
19228c2ecf20Sopenharmony_ci}
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_simularity_compare(struct ieee80211_hw *hw,
19258c2ecf20Sopenharmony_ci					      long result[][8], u8 c1, u8 c2)
19268c2ecf20Sopenharmony_ci{
19278c2ecf20Sopenharmony_ci	u32 i, j, diff, simularity_bitmap, bound = 0;
19288c2ecf20Sopenharmony_ci
19298c2ecf20Sopenharmony_ci	u8 final_candidate[2] = {0xFF, 0xFF}; /* for path A and path B */
19308c2ecf20Sopenharmony_ci	bool bresult = true; /* is2t = true*/
19318c2ecf20Sopenharmony_ci	s32 tmp1 = 0, tmp2 = 0;
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci	bound = 8;
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci	simularity_bitmap = 0;
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	for (i = 0; i < bound; i++) {
19388c2ecf20Sopenharmony_ci		if ((i == 1) || (i == 3) || (i == 5) || (i == 7)) {
19398c2ecf20Sopenharmony_ci			if ((result[c1][i] & 0x00000200) != 0)
19408c2ecf20Sopenharmony_ci				tmp1 = result[c1][i] | 0xFFFFFC00;
19418c2ecf20Sopenharmony_ci			else
19428c2ecf20Sopenharmony_ci				tmp1 = result[c1][i];
19438c2ecf20Sopenharmony_ci
19448c2ecf20Sopenharmony_ci			if ((result[c2][i] & 0x00000200) != 0)
19458c2ecf20Sopenharmony_ci				tmp2 = result[c2][i] | 0xFFFFFC00;
19468c2ecf20Sopenharmony_ci			else
19478c2ecf20Sopenharmony_ci				tmp2 = result[c2][i];
19488c2ecf20Sopenharmony_ci		} else {
19498c2ecf20Sopenharmony_ci			tmp1 = result[c1][i];
19508c2ecf20Sopenharmony_ci			tmp2 = result[c2][i];
19518c2ecf20Sopenharmony_ci		}
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci		diff = (tmp1 > tmp2) ? (tmp1 - tmp2) : (tmp2 - tmp1);
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci		if (diff > MAX_TOLERANCE) {
19568c2ecf20Sopenharmony_ci			if ((i == 2 || i == 6) && !simularity_bitmap) {
19578c2ecf20Sopenharmony_ci				if (result[c1][i] + result[c1][i + 1] == 0)
19588c2ecf20Sopenharmony_ci					final_candidate[(i / 4)] = c2;
19598c2ecf20Sopenharmony_ci				else if (result[c2][i] + result[c2][i + 1] == 0)
19608c2ecf20Sopenharmony_ci					final_candidate[(i / 4)] = c1;
19618c2ecf20Sopenharmony_ci				else
19628c2ecf20Sopenharmony_ci					simularity_bitmap |= (1 << i);
19638c2ecf20Sopenharmony_ci			} else
19648c2ecf20Sopenharmony_ci				simularity_bitmap |= (1 << i);
19658c2ecf20Sopenharmony_ci		}
19668c2ecf20Sopenharmony_ci	}
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci	if (simularity_bitmap == 0) {
19698c2ecf20Sopenharmony_ci		for (i = 0; i < (bound / 4); i++) {
19708c2ecf20Sopenharmony_ci			if (final_candidate[i] != 0xFF) {
19718c2ecf20Sopenharmony_ci				for (j = i * 4; j < (i + 1) * 4 - 2; j++)
19728c2ecf20Sopenharmony_ci					result[3][j] =
19738c2ecf20Sopenharmony_ci						result[final_candidate[i]][j];
19748c2ecf20Sopenharmony_ci				bresult = false;
19758c2ecf20Sopenharmony_ci			}
19768c2ecf20Sopenharmony_ci		}
19778c2ecf20Sopenharmony_ci		return bresult;
19788c2ecf20Sopenharmony_ci	} else {
19798c2ecf20Sopenharmony_ci		if (!(simularity_bitmap & 0x03)) { /* path A TX OK */
19808c2ecf20Sopenharmony_ci			for (i = 0; i < 2; i++)
19818c2ecf20Sopenharmony_ci				result[3][i] = result[c1][i];
19828c2ecf20Sopenharmony_ci		}
19838c2ecf20Sopenharmony_ci		if (!(simularity_bitmap & 0x0c)) { /* path A RX OK */
19848c2ecf20Sopenharmony_ci			for (i = 2; i < 4; i++)
19858c2ecf20Sopenharmony_ci				result[3][i] = result[c1][i];
19868c2ecf20Sopenharmony_ci		}
19878c2ecf20Sopenharmony_ci		if (!(simularity_bitmap & 0x30)) { /* path B TX OK */
19888c2ecf20Sopenharmony_ci			for (i = 4; i < 6; i++)
19898c2ecf20Sopenharmony_ci				result[3][i] = result[c1][i];
19908c2ecf20Sopenharmony_ci		}
19918c2ecf20Sopenharmony_ci		if (!(simularity_bitmap & 0xc0)) { /* path B RX OK */
19928c2ecf20Sopenharmony_ci			for (i = 6; i < 8; i++)
19938c2ecf20Sopenharmony_ci				result[3][i] = result[c1][i];
19948c2ecf20Sopenharmony_ci		}
19958c2ecf20Sopenharmony_ci		return false;
19968c2ecf20Sopenharmony_ci	}
19978c2ecf20Sopenharmony_ci}
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw,
20008c2ecf20Sopenharmony_ci					long result[][8], u8 t, bool is2t)
20018c2ecf20Sopenharmony_ci{
20028c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
20038c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
20048c2ecf20Sopenharmony_ci	u32 i;
20058c2ecf20Sopenharmony_ci	u8 patha_ok, pathb_ok;
20068c2ecf20Sopenharmony_ci	u32 adda_reg[IQK_ADDA_REG_NUM] = {
20078c2ecf20Sopenharmony_ci		0x85c, 0xe6c, 0xe70, 0xe74,
20088c2ecf20Sopenharmony_ci		0xe78, 0xe7c, 0xe80, 0xe84,
20098c2ecf20Sopenharmony_ci		0xe88, 0xe8c, 0xed0, 0xed4,
20108c2ecf20Sopenharmony_ci		0xed8, 0xedc, 0xee0, 0xeec
20118c2ecf20Sopenharmony_ci	};
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci	u32 iqk_mac_reg[IQK_MAC_REG_NUM] = {
20148c2ecf20Sopenharmony_ci		0x522, 0x550, 0x551, 0x040
20158c2ecf20Sopenharmony_ci	};
20168c2ecf20Sopenharmony_ci	u32 iqk_bb_reg[IQK_BB_REG_NUM] = {
20178c2ecf20Sopenharmony_ci		ROFDM0_TRXPATHENABLE, ROFDM0_TRMUXPAR,
20188c2ecf20Sopenharmony_ci		RFPGA0_XCD_RFINTERFACESW, 0xb68, 0xb6c,
20198c2ecf20Sopenharmony_ci		0x870, 0x860,
20208c2ecf20Sopenharmony_ci		0x864, 0xa04
20218c2ecf20Sopenharmony_ci	};
20228c2ecf20Sopenharmony_ci	const u32 retrycount = 2;
20238c2ecf20Sopenharmony_ci
20248c2ecf20Sopenharmony_ci	u32 path_sel_bb;/* path_sel_rf */
20258c2ecf20Sopenharmony_ci
20268c2ecf20Sopenharmony_ci	u8 tmp_reg_c50, tmp_reg_c58;
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ci	tmp_reg_c50 = rtl_get_bbreg(hw, 0xc50, MASKBYTE0);
20298c2ecf20Sopenharmony_ci	tmp_reg_c58 = rtl_get_bbreg(hw, 0xc58, MASKBYTE0);
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	if (t == 0) {
20328c2ecf20Sopenharmony_ci		rtl8723_save_adda_registers(hw, adda_reg,
20338c2ecf20Sopenharmony_ci					    rtlphy->adda_backup, 16);
20348c2ecf20Sopenharmony_ci		rtl8723_phy_save_mac_registers(hw, iqk_mac_reg,
20358c2ecf20Sopenharmony_ci					       rtlphy->iqk_mac_backup);
20368c2ecf20Sopenharmony_ci		rtl8723_save_adda_registers(hw, iqk_bb_reg,
20378c2ecf20Sopenharmony_ci					    rtlphy->iqk_bb_backup,
20388c2ecf20Sopenharmony_ci					    IQK_BB_REG_NUM);
20398c2ecf20Sopenharmony_ci	}
20408c2ecf20Sopenharmony_ci	rtl8723_phy_path_adda_on(hw, adda_reg, true, is2t);
20418c2ecf20Sopenharmony_ci	if (t == 0) {
20428c2ecf20Sopenharmony_ci		rtlphy->rfpi_enable = (u8)rtl_get_bbreg(hw,
20438c2ecf20Sopenharmony_ci						RFPGA0_XA_HSSIPARAMETER1,
20448c2ecf20Sopenharmony_ci						BIT(8));
20458c2ecf20Sopenharmony_ci	}
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci	path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
20488c2ecf20Sopenharmony_ci
20498c2ecf20Sopenharmony_ci	rtl8723_phy_mac_setting_calibration(hw, iqk_mac_reg,
20508c2ecf20Sopenharmony_ci					    rtlphy->iqk_mac_backup);
20518c2ecf20Sopenharmony_ci	/*BB Setting*/
20528c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xa04, 0x0f000000, 0xf);
20538c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xc04, MASKDWORD, 0x03a05600);
20548c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0xc08, MASKDWORD, 0x000800e4);
20558c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x874, MASKDWORD, 0x22204000);
20568c2ecf20Sopenharmony_ci
20578c2ecf20Sopenharmony_ci	/* path A TX IQK */
20588c2ecf20Sopenharmony_ci	for (i = 0; i < retrycount; i++) {
20598c2ecf20Sopenharmony_ci		patha_ok = _rtl8723be_phy_path_a_iqk(hw);
20608c2ecf20Sopenharmony_ci		if (patha_ok == 0x01) {
20618c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
20628c2ecf20Sopenharmony_ci				"Path A Tx IQK Success!!\n");
20638c2ecf20Sopenharmony_ci			result[t][0] = (rtl_get_bbreg(hw, 0xe94, MASKDWORD) &
20648c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
20658c2ecf20Sopenharmony_ci			result[t][1] = (rtl_get_bbreg(hw, 0xe9c, MASKDWORD) &
20668c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
20678c2ecf20Sopenharmony_ci			break;
20688c2ecf20Sopenharmony_ci		} else {
20698c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
20708c2ecf20Sopenharmony_ci				"Path A Tx IQK Fail!!\n");
20718c2ecf20Sopenharmony_ci		}
20728c2ecf20Sopenharmony_ci	}
20738c2ecf20Sopenharmony_ci	/* path A RX IQK */
20748c2ecf20Sopenharmony_ci	for (i = 0; i < retrycount; i++) {
20758c2ecf20Sopenharmony_ci		patha_ok = _rtl8723be_phy_path_a_rx_iqk(hw);
20768c2ecf20Sopenharmony_ci		if (patha_ok == 0x03) {
20778c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
20788c2ecf20Sopenharmony_ci				"Path A Rx IQK Success!!\n");
20798c2ecf20Sopenharmony_ci			result[t][2] = (rtl_get_bbreg(hw, 0xea4, MASKDWORD) &
20808c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
20818c2ecf20Sopenharmony_ci			result[t][3] = (rtl_get_bbreg(hw, 0xeac, MASKDWORD) &
20828c2ecf20Sopenharmony_ci					0x3FF0000) >> 16;
20838c2ecf20Sopenharmony_ci			break;
20848c2ecf20Sopenharmony_ci		}
20858c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
20868c2ecf20Sopenharmony_ci			"Path A Rx IQK Fail!!\n");
20878c2ecf20Sopenharmony_ci	}
20888c2ecf20Sopenharmony_ci
20898c2ecf20Sopenharmony_ci	if (0x00 == patha_ok)
20908c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "Path A IQK Fail!!\n");
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci	if (is2t) {
20938c2ecf20Sopenharmony_ci		/* path B TX IQK */
20948c2ecf20Sopenharmony_ci		for (i = 0; i < retrycount; i++) {
20958c2ecf20Sopenharmony_ci			pathb_ok = _rtl8723be_phy_path_b_iqk(hw);
20968c2ecf20Sopenharmony_ci			if (pathb_ok == 0x01) {
20978c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
20988c2ecf20Sopenharmony_ci					"Path B Tx IQK Success!!\n");
20998c2ecf20Sopenharmony_ci				result[t][4] = (rtl_get_bbreg(hw, 0xe94,
21008c2ecf20Sopenharmony_ci							      MASKDWORD) &
21018c2ecf20Sopenharmony_ci							      0x3FF0000) >> 16;
21028c2ecf20Sopenharmony_ci				result[t][5] = (rtl_get_bbreg(hw, 0xe9c,
21038c2ecf20Sopenharmony_ci							      MASKDWORD) &
21048c2ecf20Sopenharmony_ci							      0x3FF0000) >> 16;
21058c2ecf20Sopenharmony_ci				break;
21068c2ecf20Sopenharmony_ci			}
21078c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
21088c2ecf20Sopenharmony_ci				"Path B Tx IQK Fail!!\n");
21098c2ecf20Sopenharmony_ci		}
21108c2ecf20Sopenharmony_ci		/* path B RX IQK */
21118c2ecf20Sopenharmony_ci		for (i = 0; i < retrycount; i++) {
21128c2ecf20Sopenharmony_ci			pathb_ok = _rtl8723be_phy_path_b_rx_iqk(hw);
21138c2ecf20Sopenharmony_ci			if (pathb_ok == 0x03) {
21148c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
21158c2ecf20Sopenharmony_ci					"Path B Rx IQK Success!!\n");
21168c2ecf20Sopenharmony_ci				result[t][6] = (rtl_get_bbreg(hw, 0xea4,
21178c2ecf20Sopenharmony_ci							      MASKDWORD) &
21188c2ecf20Sopenharmony_ci							      0x3FF0000) >> 16;
21198c2ecf20Sopenharmony_ci				result[t][7] = (rtl_get_bbreg(hw, 0xeac,
21208c2ecf20Sopenharmony_ci							      MASKDWORD) &
21218c2ecf20Sopenharmony_ci							      0x3FF0000) >> 16;
21228c2ecf20Sopenharmony_ci				break;
21238c2ecf20Sopenharmony_ci			}
21248c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
21258c2ecf20Sopenharmony_ci				"Path B Rx IQK Fail!!\n");
21268c2ecf20Sopenharmony_ci		}
21278c2ecf20Sopenharmony_ci	}
21288c2ecf20Sopenharmony_ci
21298c2ecf20Sopenharmony_ci	/* Back to BB mode, load original value */
21308c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, RFPGA0_IQK, MASKDWORD, 0);
21318c2ecf20Sopenharmony_ci
21328c2ecf20Sopenharmony_ci	if (t != 0) {
21338c2ecf20Sopenharmony_ci		rtl8723_phy_reload_adda_registers(hw, adda_reg,
21348c2ecf20Sopenharmony_ci						  rtlphy->adda_backup, 16);
21358c2ecf20Sopenharmony_ci		rtl8723_phy_reload_mac_registers(hw, iqk_mac_reg,
21368c2ecf20Sopenharmony_ci						 rtlphy->iqk_mac_backup);
21378c2ecf20Sopenharmony_ci		rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
21388c2ecf20Sopenharmony_ci						  rtlphy->iqk_bb_backup,
21398c2ecf20Sopenharmony_ci						  IQK_BB_REG_NUM);
21408c2ecf20Sopenharmony_ci
21418c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
21428c2ecf20Sopenharmony_ci		/*rtl_set_rfreg(hw, RF90_PATH_B, 0xb0, 0xfffff, path_sel_rf);*/
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xc50, MASKBYTE0, 0x50);
21458c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xc50, MASKBYTE0, tmp_reg_c50);
21468c2ecf20Sopenharmony_ci		if (is2t) {
21478c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, 0xc58, MASKBYTE0, 0x50);
21488c2ecf20Sopenharmony_ci			rtl_set_bbreg(hw, 0xc58, MASKBYTE0, tmp_reg_c58);
21498c2ecf20Sopenharmony_ci		}
21508c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe30, MASKDWORD, 0x01008c00);
21518c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0xe34, MASKDWORD, 0x01008c00);
21528c2ecf20Sopenharmony_ci	}
21538c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "8723be IQK Finish!!\n");
21548c2ecf20Sopenharmony_ci}
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_cistatic u8 _get_right_chnl_place_for_iqk(u8 chnl)
21578c2ecf20Sopenharmony_ci{
21588c2ecf20Sopenharmony_ci	u8 channel_all[TARGET_CHNL_NUM_2G_5G] = {
21598c2ecf20Sopenharmony_ci			1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
21608c2ecf20Sopenharmony_ci			13, 14, 36, 38, 40, 42, 44, 46,
21618c2ecf20Sopenharmony_ci			48, 50, 52, 54, 56, 58, 60, 62, 64,
21628c2ecf20Sopenharmony_ci			100, 102, 104, 106, 108, 110,
21638c2ecf20Sopenharmony_ci			112, 114, 116, 118, 120, 122,
21648c2ecf20Sopenharmony_ci			124, 126, 128, 130, 132, 134, 136,
21658c2ecf20Sopenharmony_ci			138, 140, 149, 151, 153, 155, 157,
21668c2ecf20Sopenharmony_ci			159, 161, 163, 165};
21678c2ecf20Sopenharmony_ci	u8 place = chnl;
21688c2ecf20Sopenharmony_ci
21698c2ecf20Sopenharmony_ci	if (chnl > 14) {
21708c2ecf20Sopenharmony_ci		for (place = 14; place < sizeof(channel_all); place++) {
21718c2ecf20Sopenharmony_ci			if (channel_all[place] == chnl)
21728c2ecf20Sopenharmony_ci				return place - 13;
21738c2ecf20Sopenharmony_ci		}
21748c2ecf20Sopenharmony_ci	}
21758c2ecf20Sopenharmony_ci	return 0;
21768c2ecf20Sopenharmony_ci}
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw, bool is2t)
21798c2ecf20Sopenharmony_ci{
21808c2ecf20Sopenharmony_ci	u8 tmpreg;
21818c2ecf20Sopenharmony_ci	u32 rf_a_mode = 0, rf_b_mode = 0, lc_cal;
21828c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	tmpreg = rtl_read_byte(rtlpriv, 0xd03);
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ci	if ((tmpreg & 0x70) != 0)
21878c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, 0xd03, tmpreg & 0x8F);
21888c2ecf20Sopenharmony_ci	else
21898c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	if ((tmpreg & 0x70) != 0) {
21928c2ecf20Sopenharmony_ci		rf_a_mode = rtl_get_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS);
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci		if (is2t)
21958c2ecf20Sopenharmony_ci			rf_b_mode = rtl_get_rfreg(hw, RF90_PATH_B, 0x00,
21968c2ecf20Sopenharmony_ci						  MASK12BITS);
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS,
21998c2ecf20Sopenharmony_ci			      (rf_a_mode & 0x8FFFF) | 0x10000);
22008c2ecf20Sopenharmony_ci
22018c2ecf20Sopenharmony_ci		if (is2t)
22028c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, RF90_PATH_B, 0x00, MASK12BITS,
22038c2ecf20Sopenharmony_ci				      (rf_b_mode & 0x8FFFF) | 0x10000);
22048c2ecf20Sopenharmony_ci	}
22058c2ecf20Sopenharmony_ci	lc_cal = rtl_get_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS);
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdfbe0);
22088c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x18, MASK12BITS, 0x8c0a);
22098c2ecf20Sopenharmony_ci
22108c2ecf20Sopenharmony_ci	/* In order not to disturb BT music when wifi init.(1ant NIC only) */
22118c2ecf20Sopenharmony_ci	/*mdelay(100);*/
22128c2ecf20Sopenharmony_ci	/* In order not to disturb BT music when wifi init.(1ant NIC only) */
22138c2ecf20Sopenharmony_ci	mdelay(50);
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, RFREG_OFFSET_MASK, 0xdffe0);
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	if ((tmpreg & 0x70) != 0) {
22188c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, 0xd03, tmpreg);
22198c2ecf20Sopenharmony_ci		rtl_set_rfreg(hw, RF90_PATH_A, 0x00, MASK12BITS, rf_a_mode);
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ci		if (is2t)
22228c2ecf20Sopenharmony_ci			rtl_set_rfreg(hw, RF90_PATH_B, 0x00,
22238c2ecf20Sopenharmony_ci				      MASK12BITS, rf_b_mode);
22248c2ecf20Sopenharmony_ci	} else {
22258c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
22268c2ecf20Sopenharmony_ci	}
22278c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
22288c2ecf20Sopenharmony_ci}
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw,
22318c2ecf20Sopenharmony_ci					     bool bmain, bool is2t)
22328c2ecf20Sopenharmony_ci{
22338c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
22348c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci	if (bmain) /* left antenna */
22378c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x1);
22388c2ecf20Sopenharmony_ci	else
22398c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, 0x92C, MASKDWORD, 0x2);
22408c2ecf20Sopenharmony_ci}
22418c2ecf20Sopenharmony_ci
22428c2ecf20Sopenharmony_ci#undef IQK_ADDA_REG_NUM
22438c2ecf20Sopenharmony_ci#undef IQK_DELAY_TIME
22448c2ecf20Sopenharmony_ci/* IQK is merge from Merge Temp */
22458c2ecf20Sopenharmony_civoid rtl8723be_phy_iq_calibrate(struct ieee80211_hw *hw, bool b_recovery)
22468c2ecf20Sopenharmony_ci{
22478c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
22488c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
22498c2ecf20Sopenharmony_ci	long result[4][8];
22508c2ecf20Sopenharmony_ci	u8 i, final_candidate, idx;
22518c2ecf20Sopenharmony_ci	bool b_patha_ok, b_pathb_ok;
22528c2ecf20Sopenharmony_ci	long reg_e94, reg_e9c, reg_ea4, reg_eb4, reg_ebc, reg_ec4;
22538c2ecf20Sopenharmony_ci	long reg_tmp = 0;
22548c2ecf20Sopenharmony_ci	bool is12simular, is13simular, is23simular;
22558c2ecf20Sopenharmony_ci	u32 iqk_bb_reg[9] = {
22568c2ecf20Sopenharmony_ci		ROFDM0_XARXIQIMBALANCE,
22578c2ecf20Sopenharmony_ci		ROFDM0_XBRXIQIMBALANCE,
22588c2ecf20Sopenharmony_ci		ROFDM0_ECCATHRESHOLD,
22598c2ecf20Sopenharmony_ci		ROFDM0_AGCRSSITABLE,
22608c2ecf20Sopenharmony_ci		ROFDM0_XATXIQIMBALANCE,
22618c2ecf20Sopenharmony_ci		ROFDM0_XBTXIQIMBALANCE,
22628c2ecf20Sopenharmony_ci		ROFDM0_XCTXAFE,
22638c2ecf20Sopenharmony_ci		ROFDM0_XDTXAFE,
22648c2ecf20Sopenharmony_ci		ROFDM0_RXIQEXTANTA
22658c2ecf20Sopenharmony_ci	};
22668c2ecf20Sopenharmony_ci	u32 path_sel_bb = 0; /* path_sel_rf = 0 */
22678c2ecf20Sopenharmony_ci
22688c2ecf20Sopenharmony_ci	if (rtlphy->lck_inprogress)
22698c2ecf20Sopenharmony_ci		return;
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.iqk_lock);
22728c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = true;
22738c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.iqk_lock);
22748c2ecf20Sopenharmony_ci
22758c2ecf20Sopenharmony_ci	if (b_recovery) {
22768c2ecf20Sopenharmony_ci		rtl8723_phy_reload_adda_registers(hw, iqk_bb_reg,
22778c2ecf20Sopenharmony_ci						  rtlphy->iqk_bb_backup, 9);
22788c2ecf20Sopenharmony_ci		goto label_done;
22798c2ecf20Sopenharmony_ci	}
22808c2ecf20Sopenharmony_ci	/* Save RF Path */
22818c2ecf20Sopenharmony_ci	path_sel_bb = rtl_get_bbreg(hw, 0x948, MASKDWORD);
22828c2ecf20Sopenharmony_ci	/* path_sel_rf = rtl_get_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff); */
22838c2ecf20Sopenharmony_ci
22848c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
22858c2ecf20Sopenharmony_ci		result[0][i] = 0;
22868c2ecf20Sopenharmony_ci		result[1][i] = 0;
22878c2ecf20Sopenharmony_ci		result[2][i] = 0;
22888c2ecf20Sopenharmony_ci		result[3][i] = 0;
22898c2ecf20Sopenharmony_ci	}
22908c2ecf20Sopenharmony_ci	final_candidate = 0xff;
22918c2ecf20Sopenharmony_ci	b_patha_ok = false;
22928c2ecf20Sopenharmony_ci	b_pathb_ok = false;
22938c2ecf20Sopenharmony_ci	is12simular = false;
22948c2ecf20Sopenharmony_ci	is23simular = false;
22958c2ecf20Sopenharmony_ci	is13simular = false;
22968c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
22978c2ecf20Sopenharmony_ci		_rtl8723be_phy_iq_calibrate(hw, result, i, true);
22988c2ecf20Sopenharmony_ci		if (i == 1) {
22998c2ecf20Sopenharmony_ci			is12simular = _rtl8723be_phy_simularity_compare(hw,
23008c2ecf20Sopenharmony_ci									result,
23018c2ecf20Sopenharmony_ci									0, 1);
23028c2ecf20Sopenharmony_ci			if (is12simular) {
23038c2ecf20Sopenharmony_ci				final_candidate = 0;
23048c2ecf20Sopenharmony_ci				break;
23058c2ecf20Sopenharmony_ci			}
23068c2ecf20Sopenharmony_ci		}
23078c2ecf20Sopenharmony_ci		if (i == 2) {
23088c2ecf20Sopenharmony_ci			is13simular = _rtl8723be_phy_simularity_compare(hw,
23098c2ecf20Sopenharmony_ci									result,
23108c2ecf20Sopenharmony_ci									0, 2);
23118c2ecf20Sopenharmony_ci			if (is13simular) {
23128c2ecf20Sopenharmony_ci				final_candidate = 0;
23138c2ecf20Sopenharmony_ci				break;
23148c2ecf20Sopenharmony_ci			}
23158c2ecf20Sopenharmony_ci			is23simular = _rtl8723be_phy_simularity_compare(hw,
23168c2ecf20Sopenharmony_ci									result,
23178c2ecf20Sopenharmony_ci									1, 2);
23188c2ecf20Sopenharmony_ci			if (is23simular) {
23198c2ecf20Sopenharmony_ci				final_candidate = 1;
23208c2ecf20Sopenharmony_ci			} else {
23218c2ecf20Sopenharmony_ci				for (i = 0; i < 8; i++)
23228c2ecf20Sopenharmony_ci					reg_tmp += result[3][i];
23238c2ecf20Sopenharmony_ci
23248c2ecf20Sopenharmony_ci				if (reg_tmp != 0)
23258c2ecf20Sopenharmony_ci					final_candidate = 3;
23268c2ecf20Sopenharmony_ci				else
23278c2ecf20Sopenharmony_ci					final_candidate = 0xFF;
23288c2ecf20Sopenharmony_ci			}
23298c2ecf20Sopenharmony_ci		}
23308c2ecf20Sopenharmony_ci	}
23318c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
23328c2ecf20Sopenharmony_ci		reg_e94 = result[i][0];
23338c2ecf20Sopenharmony_ci		reg_e9c = result[i][1];
23348c2ecf20Sopenharmony_ci		reg_ea4 = result[i][2];
23358c2ecf20Sopenharmony_ci		reg_eb4 = result[i][4];
23368c2ecf20Sopenharmony_ci		reg_ebc = result[i][5];
23378c2ecf20Sopenharmony_ci		reg_ec4 = result[i][6];
23388c2ecf20Sopenharmony_ci	}
23398c2ecf20Sopenharmony_ci	if (final_candidate != 0xff) {
23408c2ecf20Sopenharmony_ci		reg_e94 = result[final_candidate][0];
23418c2ecf20Sopenharmony_ci		rtlphy->reg_e94 = reg_e94;
23428c2ecf20Sopenharmony_ci		reg_e9c = result[final_candidate][1];
23438c2ecf20Sopenharmony_ci		rtlphy->reg_e9c = reg_e9c;
23448c2ecf20Sopenharmony_ci		reg_ea4 = result[final_candidate][2];
23458c2ecf20Sopenharmony_ci		reg_eb4 = result[final_candidate][4];
23468c2ecf20Sopenharmony_ci		rtlphy->reg_eb4 = reg_eb4;
23478c2ecf20Sopenharmony_ci		reg_ebc = result[final_candidate][5];
23488c2ecf20Sopenharmony_ci		rtlphy->reg_ebc = reg_ebc;
23498c2ecf20Sopenharmony_ci		reg_ec4 = result[final_candidate][6];
23508c2ecf20Sopenharmony_ci		b_patha_ok = true;
23518c2ecf20Sopenharmony_ci		b_pathb_ok = true;
23528c2ecf20Sopenharmony_ci	} else {
23538c2ecf20Sopenharmony_ci		rtlphy->reg_e94 = 0x100;
23548c2ecf20Sopenharmony_ci		rtlphy->reg_eb4 = 0x100;
23558c2ecf20Sopenharmony_ci		rtlphy->reg_e9c = 0x0;
23568c2ecf20Sopenharmony_ci		rtlphy->reg_ebc = 0x0;
23578c2ecf20Sopenharmony_ci	}
23588c2ecf20Sopenharmony_ci	if (reg_e94 != 0)
23598c2ecf20Sopenharmony_ci		rtl8723_phy_path_a_fill_iqk_matrix(hw, b_patha_ok, result,
23608c2ecf20Sopenharmony_ci						   final_candidate,
23618c2ecf20Sopenharmony_ci						   (reg_ea4 == 0));
23628c2ecf20Sopenharmony_ci	if (reg_eb4 != 0)
23638c2ecf20Sopenharmony_ci		_rtl8723be_phy_path_b_fill_iqk_matrix(hw, b_pathb_ok, result,
23648c2ecf20Sopenharmony_ci						      final_candidate,
23658c2ecf20Sopenharmony_ci						      (reg_ec4 == 0));
23668c2ecf20Sopenharmony_ci
23678c2ecf20Sopenharmony_ci	idx = _get_right_chnl_place_for_iqk(rtlphy->current_channel);
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci	if (final_candidate < 4) {
23708c2ecf20Sopenharmony_ci		for (i = 0; i < IQK_MATRIX_REG_NUM; i++)
23718c2ecf20Sopenharmony_ci			rtlphy->iqk_matrix[idx].value[0][i] =
23728c2ecf20Sopenharmony_ci						result[final_candidate][i];
23738c2ecf20Sopenharmony_ci		rtlphy->iqk_matrix[idx].iqk_done = true;
23748c2ecf20Sopenharmony_ci
23758c2ecf20Sopenharmony_ci	}
23768c2ecf20Sopenharmony_ci	rtl8723_save_adda_registers(hw, iqk_bb_reg,
23778c2ecf20Sopenharmony_ci				    rtlphy->iqk_bb_backup, 9);
23788c2ecf20Sopenharmony_ci
23798c2ecf20Sopenharmony_ci	rtl_set_bbreg(hw, 0x948, MASKDWORD, path_sel_bb);
23808c2ecf20Sopenharmony_ci	/* rtl_set_rfreg(hw, RF90_PATH_A, 0xb0, 0xfffff, path_sel_rf); */
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_cilabel_done:
23838c2ecf20Sopenharmony_ci	spin_lock(&rtlpriv->locks.iqk_lock);
23848c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = false;
23858c2ecf20Sopenharmony_ci	spin_unlock(&rtlpriv->locks.iqk_lock);
23868c2ecf20Sopenharmony_ci}
23878c2ecf20Sopenharmony_ci
23888c2ecf20Sopenharmony_civoid rtl8723be_phy_lc_calibrate(struct ieee80211_hw *hw)
23898c2ecf20Sopenharmony_ci{
23908c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
23918c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
23928c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = &rtlpriv->rtlhal;
23938c2ecf20Sopenharmony_ci	u32 timeout = 2000, timecount = 0;
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	while (rtlpriv->mac80211.act_scanning && timecount < timeout) {
23968c2ecf20Sopenharmony_ci		udelay(50);
23978c2ecf20Sopenharmony_ci		timecount += 50;
23988c2ecf20Sopenharmony_ci	}
23998c2ecf20Sopenharmony_ci
24008c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = true;
24018c2ecf20Sopenharmony_ci	RTPRINT(rtlpriv, FINIT, INIT_IQK,
24028c2ecf20Sopenharmony_ci		"LCK:Start!!! currentband %x delay %d ms\n",
24038c2ecf20Sopenharmony_ci		 rtlhal->current_bandtype, timecount);
24048c2ecf20Sopenharmony_ci
24058c2ecf20Sopenharmony_ci	_rtl8723be_phy_lc_calibrate(hw, false);
24068c2ecf20Sopenharmony_ci
24078c2ecf20Sopenharmony_ci	rtlphy->lck_inprogress = false;
24088c2ecf20Sopenharmony_ci}
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_civoid rtl8723be_phy_set_rfpath_switch(struct ieee80211_hw *hw, bool bmain)
24118c2ecf20Sopenharmony_ci{
24128c2ecf20Sopenharmony_ci	_rtl8723be_phy_set_rfpath_switch(hw, bmain, true);
24138c2ecf20Sopenharmony_ci}
24148c2ecf20Sopenharmony_ci
24158c2ecf20Sopenharmony_cibool rtl8723be_phy_set_io_cmd(struct ieee80211_hw *hw, enum io_type iotype)
24168c2ecf20Sopenharmony_ci{
24178c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24188c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
24198c2ecf20Sopenharmony_ci	bool b_postprocessing = false;
24208c2ecf20Sopenharmony_ci
24218c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
24228c2ecf20Sopenharmony_ci		"-->IO Cmd(%#x), set_io_inprogress(%d)\n",
24238c2ecf20Sopenharmony_ci		iotype, rtlphy->set_io_inprogress);
24248c2ecf20Sopenharmony_ci	do {
24258c2ecf20Sopenharmony_ci		switch (iotype) {
24268c2ecf20Sopenharmony_ci		case IO_CMD_RESUME_DM_BY_SCAN:
24278c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
24288c2ecf20Sopenharmony_ci				"[IO CMD] Resume DM after scan.\n");
24298c2ecf20Sopenharmony_ci			b_postprocessing = true;
24308c2ecf20Sopenharmony_ci			break;
24318c2ecf20Sopenharmony_ci		case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
24328c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
24338c2ecf20Sopenharmony_ci				"[IO CMD] Pause DM before scan.\n");
24348c2ecf20Sopenharmony_ci			b_postprocessing = true;
24358c2ecf20Sopenharmony_ci			break;
24368c2ecf20Sopenharmony_ci		default:
24378c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
24388c2ecf20Sopenharmony_ci				"switch case %#x not processed\n", iotype);
24398c2ecf20Sopenharmony_ci			break;
24408c2ecf20Sopenharmony_ci		}
24418c2ecf20Sopenharmony_ci	} while (false);
24428c2ecf20Sopenharmony_ci	if (b_postprocessing && !rtlphy->set_io_inprogress) {
24438c2ecf20Sopenharmony_ci		rtlphy->set_io_inprogress = true;
24448c2ecf20Sopenharmony_ci		rtlphy->current_io_type = iotype;
24458c2ecf20Sopenharmony_ci	} else {
24468c2ecf20Sopenharmony_ci		return false;
24478c2ecf20Sopenharmony_ci	}
24488c2ecf20Sopenharmony_ci	rtl8723be_phy_set_io(hw);
24498c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE, "IO Type(%#x)\n", iotype);
24508c2ecf20Sopenharmony_ci	return true;
24518c2ecf20Sopenharmony_ci}
24528c2ecf20Sopenharmony_ci
24538c2ecf20Sopenharmony_cistatic void rtl8723be_phy_set_io(struct ieee80211_hw *hw)
24548c2ecf20Sopenharmony_ci{
24558c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24568c2ecf20Sopenharmony_ci	struct dig_t *dm_digtable = &rtlpriv->dm_digtable;
24578c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &rtlpriv->phy;
24588c2ecf20Sopenharmony_ci
24598c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
24608c2ecf20Sopenharmony_ci		"--->Cmd(%#x), set_io_inprogress(%d)\n",
24618c2ecf20Sopenharmony_ci		rtlphy->current_io_type, rtlphy->set_io_inprogress);
24628c2ecf20Sopenharmony_ci	switch (rtlphy->current_io_type) {
24638c2ecf20Sopenharmony_ci	case IO_CMD_RESUME_DM_BY_SCAN:
24648c2ecf20Sopenharmony_ci		dm_digtable->cur_igvalue = rtlphy->initgain_backup.xaagccore1;
24658c2ecf20Sopenharmony_ci		/*rtl92c_dm_write_dig(hw);*/
24668c2ecf20Sopenharmony_ci		rtl8723be_phy_set_txpower_level(hw, rtlphy->current_channel);
24678c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x83);
24688c2ecf20Sopenharmony_ci		break;
24698c2ecf20Sopenharmony_ci	case IO_CMD_PAUSE_BAND0_DM_BY_SCAN:
24708c2ecf20Sopenharmony_ci		rtlphy->initgain_backup.xaagccore1 = dm_digtable->cur_igvalue;
24718c2ecf20Sopenharmony_ci		dm_digtable->cur_igvalue = 0x17;
24728c2ecf20Sopenharmony_ci		rtl_set_bbreg(hw, RCCK0_CCA, 0xff0000, 0x40);
24738c2ecf20Sopenharmony_ci		break;
24748c2ecf20Sopenharmony_ci	default:
24758c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
24768c2ecf20Sopenharmony_ci			"switch case %#x not processed\n",
24778c2ecf20Sopenharmony_ci			rtlphy->current_io_type);
24788c2ecf20Sopenharmony_ci		break;
24798c2ecf20Sopenharmony_ci	}
24808c2ecf20Sopenharmony_ci	rtlphy->set_io_inprogress = false;
24818c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_TRACE,
24828c2ecf20Sopenharmony_ci		"(%#x)\n", rtlphy->current_io_type);
24838c2ecf20Sopenharmony_ci}
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_cistatic void rtl8723be_phy_set_rf_on(struct ieee80211_hw *hw)
24868c2ecf20Sopenharmony_ci{
24878c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24888c2ecf20Sopenharmony_ci
24898c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x2b);
24908c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
24918c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
24928c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE3);
24938c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
24948c2ecf20Sopenharmony_ci}
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_cistatic void _rtl8723be_phy_set_rf_sleep(struct ieee80211_hw *hw)
24978c2ecf20Sopenharmony_ci{
24988c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
24998c2ecf20Sopenharmony_ci
25008c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
25018c2ecf20Sopenharmony_ci	rtl_set_rfreg(hw, RF90_PATH_A, 0x00, RFREG_OFFSET_MASK, 0x00);
25028c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, 0xE2);
25038c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SPS0_CTRL, 0x22);
25048c2ecf20Sopenharmony_ci}
25058c2ecf20Sopenharmony_ci
25068c2ecf20Sopenharmony_cistatic bool _rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
25078c2ecf20Sopenharmony_ci					      enum rf_pwrstate rfpwr_state)
25088c2ecf20Sopenharmony_ci{
25098c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
25108c2ecf20Sopenharmony_ci	struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
25118c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
25128c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
25138c2ecf20Sopenharmony_ci	bool bresult = true;
25148c2ecf20Sopenharmony_ci	u8 i, queue_id;
25158c2ecf20Sopenharmony_ci	struct rtl8192_tx_ring *ring = NULL;
25168c2ecf20Sopenharmony_ci
25178c2ecf20Sopenharmony_ci	switch (rfpwr_state) {
25188c2ecf20Sopenharmony_ci	case ERFON:
25198c2ecf20Sopenharmony_ci		if ((ppsc->rfpwr_state == ERFOFF) &&
25208c2ecf20Sopenharmony_ci		     RT_IN_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC)) {
25218c2ecf20Sopenharmony_ci			bool rtstatus;
25228c2ecf20Sopenharmony_ci			u32 initializecount = 0;
25238c2ecf20Sopenharmony_ci			do {
25248c2ecf20Sopenharmony_ci				initializecount++;
25258c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
25268c2ecf20Sopenharmony_ci					"IPS Set eRf nic enable\n");
25278c2ecf20Sopenharmony_ci				rtstatus = rtl_ps_enable_nic(hw);
25288c2ecf20Sopenharmony_ci			} while (!rtstatus && (initializecount < 10));
25298c2ecf20Sopenharmony_ci			RT_CLEAR_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
25308c2ecf20Sopenharmony_ci		} else {
25318c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
25328c2ecf20Sopenharmony_ci				"Set ERFON slept:%d ms\n",
25338c2ecf20Sopenharmony_ci				jiffies_to_msecs(jiffies -
25348c2ecf20Sopenharmony_ci						 ppsc->last_sleep_jiffies));
25358c2ecf20Sopenharmony_ci			ppsc->last_awake_jiffies = jiffies;
25368c2ecf20Sopenharmony_ci			rtl8723be_phy_set_rf_on(hw);
25378c2ecf20Sopenharmony_ci		}
25388c2ecf20Sopenharmony_ci		if (mac->link_state == MAC80211_LINKED)
25398c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->led_control(hw, LED_CTL_LINK);
25408c2ecf20Sopenharmony_ci		else
25418c2ecf20Sopenharmony_ci			rtlpriv->cfg->ops->led_control(hw, LED_CTL_NO_LINK);
25428c2ecf20Sopenharmony_ci
25438c2ecf20Sopenharmony_ci		break;
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_ci	case ERFOFF:
25468c2ecf20Sopenharmony_ci		for (queue_id = 0, i = 0;
25478c2ecf20Sopenharmony_ci		     queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
25488c2ecf20Sopenharmony_ci			ring = &pcipriv->dev.tx_ring[queue_id];
25498c2ecf20Sopenharmony_ci			/* Don't check BEACON Q.
25508c2ecf20Sopenharmony_ci			 * BEACON Q is always not empty,
25518c2ecf20Sopenharmony_ci			 * because '_rtl8723be_cmd_send_packet'
25528c2ecf20Sopenharmony_ci			 */
25538c2ecf20Sopenharmony_ci			if (queue_id == BEACON_QUEUE ||
25548c2ecf20Sopenharmony_ci			    skb_queue_len(&ring->queue) == 0) {
25558c2ecf20Sopenharmony_ci				queue_id++;
25568c2ecf20Sopenharmony_ci				continue;
25578c2ecf20Sopenharmony_ci			} else {
25588c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
25598c2ecf20Sopenharmony_ci					"eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
25608c2ecf20Sopenharmony_ci					(i + 1), queue_id,
25618c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci				udelay(10);
25648c2ecf20Sopenharmony_ci				i++;
25658c2ecf20Sopenharmony_ci			}
25668c2ecf20Sopenharmony_ci			if (i >= MAX_DOZE_WAITING_TIMES_9x) {
25678c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
25688c2ecf20Sopenharmony_ci					"ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
25698c2ecf20Sopenharmony_ci					MAX_DOZE_WAITING_TIMES_9x,
25708c2ecf20Sopenharmony_ci					queue_id,
25718c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
25728c2ecf20Sopenharmony_ci				break;
25738c2ecf20Sopenharmony_ci			}
25748c2ecf20Sopenharmony_ci		}
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci		if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC) {
25778c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
25788c2ecf20Sopenharmony_ci				"IPS Set eRf nic disable\n");
25798c2ecf20Sopenharmony_ci			rtl_ps_disable_nic(hw);
25808c2ecf20Sopenharmony_ci			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
25818c2ecf20Sopenharmony_ci		} else {
25828c2ecf20Sopenharmony_ci			if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS) {
25838c2ecf20Sopenharmony_ci				rtlpriv->cfg->ops->led_control(hw,
25848c2ecf20Sopenharmony_ci							       LED_CTL_NO_LINK);
25858c2ecf20Sopenharmony_ci			} else {
25868c2ecf20Sopenharmony_ci				rtlpriv->cfg->ops->led_control(hw,
25878c2ecf20Sopenharmony_ci							     LED_CTL_POWER_OFF);
25888c2ecf20Sopenharmony_ci			}
25898c2ecf20Sopenharmony_ci		}
25908c2ecf20Sopenharmony_ci		break;
25918c2ecf20Sopenharmony_ci
25928c2ecf20Sopenharmony_ci	case ERFSLEEP:
25938c2ecf20Sopenharmony_ci		if (ppsc->rfpwr_state == ERFOFF)
25948c2ecf20Sopenharmony_ci			break;
25958c2ecf20Sopenharmony_ci		for (queue_id = 0, i = 0;
25968c2ecf20Sopenharmony_ci		     queue_id < RTL_PCI_MAX_TX_QUEUE_COUNT;) {
25978c2ecf20Sopenharmony_ci			ring = &pcipriv->dev.tx_ring[queue_id];
25988c2ecf20Sopenharmony_ci			if (skb_queue_len(&ring->queue) == 0) {
25998c2ecf20Sopenharmony_ci				queue_id++;
26008c2ecf20Sopenharmony_ci				continue;
26018c2ecf20Sopenharmony_ci			} else {
26028c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
26038c2ecf20Sopenharmony_ci					"eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
26048c2ecf20Sopenharmony_ci					(i + 1), queue_id,
26058c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
26068c2ecf20Sopenharmony_ci
26078c2ecf20Sopenharmony_ci				udelay(10);
26088c2ecf20Sopenharmony_ci				i++;
26098c2ecf20Sopenharmony_ci			}
26108c2ecf20Sopenharmony_ci			if (i >= MAX_DOZE_WAITING_TIMES_9x) {
26118c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
26128c2ecf20Sopenharmony_ci					"ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
26138c2ecf20Sopenharmony_ci					MAX_DOZE_WAITING_TIMES_9x,
26148c2ecf20Sopenharmony_ci					queue_id,
26158c2ecf20Sopenharmony_ci					skb_queue_len(&ring->queue));
26168c2ecf20Sopenharmony_ci				break;
26178c2ecf20Sopenharmony_ci			}
26188c2ecf20Sopenharmony_ci		}
26198c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_RF, DBG_DMESG,
26208c2ecf20Sopenharmony_ci			"Set ERFSLEEP awaked:%d ms\n",
26218c2ecf20Sopenharmony_ci			jiffies_to_msecs(jiffies -
26228c2ecf20Sopenharmony_ci					 ppsc->last_awake_jiffies));
26238c2ecf20Sopenharmony_ci		ppsc->last_sleep_jiffies = jiffies;
26248c2ecf20Sopenharmony_ci		_rtl8723be_phy_set_rf_sleep(hw);
26258c2ecf20Sopenharmony_ci		break;
26268c2ecf20Sopenharmony_ci
26278c2ecf20Sopenharmony_ci	default:
26288c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
26298c2ecf20Sopenharmony_ci			"switch case %#x not processed\n", rfpwr_state);
26308c2ecf20Sopenharmony_ci		bresult = false;
26318c2ecf20Sopenharmony_ci		break;
26328c2ecf20Sopenharmony_ci	}
26338c2ecf20Sopenharmony_ci	if (bresult)
26348c2ecf20Sopenharmony_ci		ppsc->rfpwr_state = rfpwr_state;
26358c2ecf20Sopenharmony_ci	return bresult;
26368c2ecf20Sopenharmony_ci}
26378c2ecf20Sopenharmony_ci
26388c2ecf20Sopenharmony_cibool rtl8723be_phy_set_rf_power_state(struct ieee80211_hw *hw,
26398c2ecf20Sopenharmony_ci				      enum rf_pwrstate rfpwr_state)
26408c2ecf20Sopenharmony_ci{
26418c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_ci	bool bresult = false;
26448c2ecf20Sopenharmony_ci
26458c2ecf20Sopenharmony_ci	if (rfpwr_state == ppsc->rfpwr_state)
26468c2ecf20Sopenharmony_ci		return bresult;
26478c2ecf20Sopenharmony_ci	bresult = _rtl8723be_phy_set_rf_power_state(hw, rfpwr_state);
26488c2ecf20Sopenharmony_ci	return bresult;
26498c2ecf20Sopenharmony_ci}
2650