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 "reg.h"
29 #include "def.h"
30 #include "phy.h"
31 #include "dm.h"
32 
33 void rtl92cu_dm_dynamic_txpower(struct ieee80211_hw *hw)
34 {
35 	struct rtl_priv *rtlpriv = rtl_priv(hw);
36 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
37 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
38 	long undec_sm_pwdb;
39 
40 	if (!rtlpriv->dm.dynamic_txpower_enable)
41 		return;
42 
43 	if (rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
44 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
45 		return;
46 	}
47 
48 	if ((mac->link_state < MAC80211_LINKED) &&
49 	    (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
50 		RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
51 			 "Not connected to any\n");
52 
53 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
54 
55 		rtlpriv->dm.last_dtp_lvl = TXHIGHPWRLEVEL_NORMAL;
56 		return;
57 	}
58 
59 	if (mac->link_state >= MAC80211_LINKED) {
60 		if (mac->opmode == NL80211_IFTYPE_ADHOC) {
61 			undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
62 			RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
63 				 "AP Client PWDB = 0x%lx\n",
64 				 undec_sm_pwdb);
65 		} else {
66 			undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
67 			RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
68 				 "STA Default Port PWDB = 0x%lx\n",
69 				 undec_sm_pwdb);
70 		}
71 	} else {
72 		undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
73 
74 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
75 			 "AP Ext Port PWDB = 0x%lx\n",
76 			 undec_sm_pwdb);
77 	}
78 
79 	if (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL2) {
80 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
81 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
82 			 "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x0)\n");
83 	} else if ((undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL2 - 3)) &&
84 		   (undec_sm_pwdb >= TX_POWER_NEAR_FIELD_THRESH_LVL1)) {
85 
86 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_LEVEL1;
87 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
88 			 "TXHIGHPWRLEVEL_LEVEL1 (TxPwr=0x10)\n");
89 	} else if (undec_sm_pwdb < (TX_POWER_NEAR_FIELD_THRESH_LVL1 - 5)) {
90 		rtlpriv->dm.dynamic_txhighpower_lvl = TXHIGHPWRLEVEL_NORMAL;
91 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
92 			 "TXHIGHPWRLEVEL_NORMAL\n");
93 	}
94 
95 	if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl)) {
96 		RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
97 			 "PHY_SetTxPowerLevel8192S() Channel = %d\n",
98 			 rtlphy->current_channel);
99 		rtl92c_phy_set_txpower_level(hw, rtlphy->current_channel);
100 		if (rtlpriv->dm.dynamic_txhighpower_lvl ==
101 		    TXHIGHPWRLEVEL_NORMAL)
102 			dm_restorepowerindex(hw);
103 		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
104 			 TXHIGHPWRLEVEL_LEVEL1)
105 			dm_writepowerindex(hw, 0x14);
106 		else if (rtlpriv->dm.dynamic_txhighpower_lvl ==
107 			 TXHIGHPWRLEVEL_LEVEL2)
108 			dm_writepowerindex(hw, 0x10);
109 	}
110 
111 	rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
112 }
113