1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 
26 #include "../wifi.h"
27 #include "../base.h"
28 #include "../pci.h"
29 #include "../core.h"
30 #include "reg.h"
31 #include "def.h"
32 #include "phy.h"
33 #include "dm.h"
34 #include "../rtl8192c/fw_common.h"
35 
36 void rtl92ce_dm_dynamic_txpower(struct ieee80211_hw *hw)
37 {
38 	struct rtl_priv *rtlpriv = rtl_priv(hw);
39 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
40 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
41 	long undec_sm_pwdb;
42 
43 	if (!rtlpriv->dm.dynamic_txpower_enable)
44 		return;
45 
46 	if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
47 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
48 		return;
49 	}
50 
51 	if ((mac->link_state < MAC80211_LINKED) &&
52 	    (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
53 		RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
54 			 "Not connected to any\n");
55 
56 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
57 
58 		rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
59 		return;
60 	}
61 
62 	if (mac->link_state >= MAC80211_LINKED) {
63 		if (mac->opmode == NL80211_IFTYPE_ADHOC) {
64 			undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
65 			RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
66 				 "AP Client PWDB = 0x%lx\n",
67 				 undec_sm_pwdb);
68 		} else {
69 			undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
70 			RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
71 				 "STA Default Port PWDB = 0x%lx\n",
72 				 undec_sm_pwdb);
73 		}
74 	} else {
75 		undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
76 
77 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
78 			 "AP Ext Port PWDB = 0x%lx\n",
79 			 undec_sm_pwdb);
80 	}
81 
82 	if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
83 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
84 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
85 			 "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x0)\n");
86 	} else if ((undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
87 		   (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
88 
89 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
90 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
91 			 "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x10)\n");
92 	} else if (undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
93 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
94 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
95 			 "TXHIGHPWRLEVEL_NORMAL\n");
96 	}
97 
98 	if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl)) {
99 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
100 			 "PHY_SetTxPowerLevel8192S() Channel = %d\n",
101 			 rtlphy->current_channel);
102 		rtl92c_phy_set_txpower_level(hw, rtlphy->current_channel);
103 	}
104 
105 	rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
106 }
107