xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192cu/dm.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1b44febedSLarry Finger // SPDX-License-Identifier: GPL-2.0
2b44febedSLarry Finger /* Copyright(c) 2009-2012  Realtek Corporation.*/
3f1d2b4d3SLarry Finger 
4f1d2b4d3SLarry Finger #include "../wifi.h"
5f1d2b4d3SLarry Finger #include "../base.h"
6f1d2b4d3SLarry Finger #include "reg.h"
7f1d2b4d3SLarry Finger #include "def.h"
8f1d2b4d3SLarry Finger #include "phy.h"
9f1d2b4d3SLarry Finger #include "dm.h"
10f1d2b4d3SLarry Finger 
rtl92cu_dm_dynamic_txpower(struct ieee80211_hw * hw)11f1d2b4d3SLarry Finger void rtl92cu_dm_dynamic_txpower(struct ieee80211_hw *hw)
12f1d2b4d3SLarry Finger {
13f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
14f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
15f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
16f1d2b4d3SLarry Finger 	long undec_sm_pwdb;
17f1d2b4d3SLarry Finger 
18f1d2b4d3SLarry Finger 	if (!rtlpriv->dm.dynamic_txpower_enable)
19f1d2b4d3SLarry Finger 		return;
20f1d2b4d3SLarry Finger 
21f1d2b4d3SLarry Finger 	if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
22f1d2b4d3SLarry Finger 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
23f1d2b4d3SLarry Finger 		return;
24f1d2b4d3SLarry Finger 	}
25f1d2b4d3SLarry Finger 
26f1d2b4d3SLarry Finger 	if ((mac->link_state < MAC80211_LINKED) &&
27f1d2b4d3SLarry Finger 	    (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
28*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_TRACE,
29f1d2b4d3SLarry Finger 			"Not connected to any\n");
30f1d2b4d3SLarry Finger 
31f1d2b4d3SLarry Finger 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
32f1d2b4d3SLarry Finger 
33f1d2b4d3SLarry Finger 		rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
34f1d2b4d3SLarry Finger 		return;
35f1d2b4d3SLarry Finger 	}
36f1d2b4d3SLarry Finger 
37f1d2b4d3SLarry Finger 	if (mac->link_state >= MAC80211_LINKED) {
38f1d2b4d3SLarry Finger 		if (mac->opmode == NL80211_IFTYPE_ADHOC) {
39f1d2b4d3SLarry Finger 			undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
40*34d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
41f1d2b4d3SLarry Finger 				"AP Client PWDB = 0x%lx\n",
42f1d2b4d3SLarry Finger 				undec_sm_pwdb);
43f1d2b4d3SLarry Finger 		} else {
44f1d2b4d3SLarry Finger 			undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
45*34d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
46f1d2b4d3SLarry Finger 				"STA Default Port PWDB = 0x%lx\n",
47f1d2b4d3SLarry Finger 				undec_sm_pwdb);
48f1d2b4d3SLarry Finger 		}
49f1d2b4d3SLarry Finger 	} else {
50f1d2b4d3SLarry Finger 		undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
51f1d2b4d3SLarry Finger 
52*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
53f1d2b4d3SLarry Finger 			"AP Ext Port PWDB = 0x%lx\n",
54f1d2b4d3SLarry Finger 			undec_sm_pwdb);
55f1d2b4d3SLarry Finger 	}
56f1d2b4d3SLarry Finger 
57f1d2b4d3SLarry Finger 	if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
58f1d2b4d3SLarry Finger 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
59*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
60f1d2b4d3SLarry Finger 			"TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x0)\n");
61f1d2b4d3SLarry Finger 	} else if ((undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
62f1d2b4d3SLarry Finger 		   (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
63f1d2b4d3SLarry Finger 
64f1d2b4d3SLarry Finger 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
65*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
66f1d2b4d3SLarry Finger 			"TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x10)\n");
67f1d2b4d3SLarry Finger 	} else if (undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
68f1d2b4d3SLarry Finger 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
69*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
70f1d2b4d3SLarry Finger 			"TXHIGHPWRLEVEL_NORMAL\n");
71f1d2b4d3SLarry Finger 	}
72f1d2b4d3SLarry Finger 
73f1d2b4d3SLarry Finger 	if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl)) {
74*34d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
75f1d2b4d3SLarry Finger 			"PHY_SetTxPowerLevel8192S() Channel = %d\n",
76f1d2b4d3SLarry Finger 			 rtlphy->current_channel);
77f1d2b4d3SLarry Finger 		rtl92c_phy_set_txpower_level(hw, rtlphy->current_channel);
78f1d2b4d3SLarry Finger 		if (rtlpriv->dm.dynamic_txhighpower_lvl ==
79f1d2b4d3SLarry Finger 		    TXHIGHPWRLEVEL_NORMAL)
80f1d2b4d3SLarry Finger 			dm_restorepowerindex(hw);
81f1d2b4d3SLarry Finger 		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
82f1d2b4d3SLarry Finger 			 TXHIGHPWRLEVEL_LEVEL1)
83f1d2b4d3SLarry Finger 			dm_writepowerindex(hw, 0x14);
84f1d2b4d3SLarry Finger 		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
85f1d2b4d3SLarry Finger 			 TXHIGHPWRLEVEL_LEVEL2)
86f1d2b4d3SLarry Finger 			dm_writepowerindex(hw, 0x10);
87f1d2b4d3SLarry Finger 	}
88f1d2b4d3SLarry Finger 
89f1d2b4d3SLarry Finger 	rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
90f1d2b4d3SLarry Finger }
91