1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #ifndef __RTW_PWRCTRL_H_
8 #define __RTW_PWRCTRL_H_
9 
10 #include <linux/mutex.h>
11 
12 #define XMIT_ALIVE	BIT(0)
13 #define CMD_ALIVE	BIT(2)
14 #define BTCOEX_ALIVE	BIT(4)
15 
16 
17 enum {
18 	PS_MODE_ACTIVE	= 0,
19 	PS_MODE_MIN,
20 	PS_MODE_MAX,
21 	PS_MODE_DTIM,	/* PS_MODE_SELF_DEFINED */
22 	PS_MODE_VOIP,
23 	PS_MODE_UAPSD_WMM,
24 	PS_MODE_UAPSD,
25 	PS_MODE_IBSS,
26 	PS_MODE_WWLAN,
27 	PM_Radio_Off,
28 	PM_Card_Disable,
29 	PS_MODE_NUM,
30 };
31 
32 /*
33 	BIT[2:0] = HW state
34 	BIT[3] = Protocol PS state,   0: register active state , 1: register sleep state
35 	BIT[4] = sub-state
36 */
37 
38 #define PS_DPS				BIT(0)
39 #define PS_LCLK				(PS_DPS)
40 #define PS_RF_OFF			BIT(1)
41 #define PS_ALL_ON			BIT(2)
42 #define PS_ST_ACTIVE		BIT(3)
43 
44 #define PS_ACK				BIT(6)
45 #define PS_TOGGLE			BIT(7)
46 
47 #define PS_STATE_MASK		(0x0F)
48 
49 #define PS_STATE(x)		(PS_STATE_MASK & (x))
50 
51 #define PS_STATE_S0		(PS_DPS)
52 #define PS_STATE_S2		(PS_RF_OFF)
53 #define PS_STATE_S4		((PS_ST_ACTIVE) | (PS_ALL_ON))
54 
55 struct reportpwrstate_parm {
56 	unsigned char mode;
57 	unsigned char state; /* the CPWM value */
58 	unsigned short rsvd;
59 };
60 
61 #define LPS_DELAY_TIME	(1 * HZ) /*  1 sec */
62 
63 /*  RF state. */
64 enum rt_rf_power_state {
65 	rf_on,		/*  RF is on after RFSleep or RFOff */
66 	rf_sleep,	/*  802.11 Power Save mode */
67 	rf_off,		/*  HW/SW Radio OFF or Inactive Power Save */
68 	/* Add the new RF state above this line ===== */
69 	rf_max
70 };
71 
72 /*  RF Off Level for IPS or HW/SW radio off */
73 #define	RT_RF_OFF_LEVL_ASPM			BIT(0)	/*  PCI ASPM */
74 #define	RT_RF_OFF_LEVL_CLK_REQ		BIT(1)	/*  PCI clock request */
75 #define	RT_RF_OFF_LEVL_PCI_D3			BIT(2)	/*  PCI D3 mode */
76 #define	RT_RF_OFF_LEVL_HALT_NIC		BIT(3)	/*  NIC halt, re-initialize hw parameters */
77 #define	RT_RF_OFF_LEVL_FREE_FW		BIT(4)	/*  FW free, re-download the FW */
78 #define	RT_RF_OFF_LEVL_FW_32K		BIT(5)	/*  FW in 32k */
79 #define	RT_RF_PS_LEVEL_ALWAYS_ASPM	BIT(6)	/*  Always enable ASPM and Clock Req in initialization. */
80 #define	RT_RF_LPS_DISALBE_2R			BIT(30)	/*  When LPS is on, disable 2R if no packet is received or transmitted. */
81 #define	RT_RF_LPS_LEVEL_ASPM			BIT(31)	/*  LPS with ASPM */
82 
83 #define	RT_IN_PS_LEVEL(ppsc, _PS_FLAG)		((ppsc->cur_ps_level & _PS_FLAG) ? true : false)
84 #define	RT_CLEAR_PS_LEVEL(ppsc, _PS_FLAG)	(ppsc->cur_ps_level &= (~(_PS_FLAG)))
85 #define	RT_SET_PS_LEVEL(ppsc, _PS_FLAG)		(ppsc->cur_ps_level |= _PS_FLAG)
86 
87 /*  ASPM OSC Control bit, added by Roger, 2013.03.29. */
88 #define	RT_PCI_ASPM_OSC_IGNORE		0	 /*  PCI ASPM ignore OSC control in default */
89 #define	RT_PCI_ASPM_OSC_ENABLE		BIT0 /*  PCI ASPM controlled by OS according to ACPI Spec 5.0 */
90 #define	RT_PCI_ASPM_OSC_DISABLE		BIT1 /*  PCI ASPM controlled by driver or BIOS, i.e., force enable ASPM */
91 
92 enum {
93 	PSBBREG_RF0 = 0,
94 	PSBBREG_RF1,
95 	PSBBREG_RF2,
96 	PSBBREG_AFE0,
97 	PSBBREG_TOTALCNT
98 };
99 
100 enum { /*  for ips_mode */
101 	IPS_NONE = 0,
102 	IPS_NORMAL,
103 	IPS_LEVEL_2,
104 	IPS_NUM
105 };
106 
107 /*  Design for pwrctrl_priv.ips_deny, 32 bits for 32 reasons at most */
108 enum ps_deny_reason {
109 	PS_DENY_DRV_INITIAL = 0,
110 	PS_DENY_SCAN,
111 	PS_DENY_JOIN,
112 	PS_DENY_DISCONNECT,
113 	PS_DENY_SUSPEND,
114 	PS_DENY_IOCTL,
115 	PS_DENY_MGNT_TX,
116 	PS_DENY_DRV_REMOVE = 30,
117 	PS_DENY_OTHERS = 31
118 };
119 
120 struct pwrctrl_priv {
121 	struct mutex lock;
122 	volatile u8 rpwm; /*  requested power state for fw */
123 	volatile u8 cpwm; /*  fw current power state. updated when 1. read from HCPWM 2. driver lowers power level */
124 	volatile u8 tog; /*  toggling */
125 	volatile u8 cpwm_tog; /*  toggling */
126 
127 	u8 pwr_mode;
128 	u8 smart_ps;
129 	u8 bcn_ant_mode;
130 	u8 dtim;
131 
132 	u32 alives;
133 	struct work_struct cpwm_event;
134 	u8 brpwmtimeout;
135 	struct work_struct rpwmtimeoutwi;
136 	struct timer_list pwr_rpwm_timer;
137 	u8 bpower_saving; /* for LPS/IPS */
138 
139 	u8 b_hw_radio_off;
140 	u8 reg_rfoff;
141 	u8 reg_pdnmode; /* powerdown mode */
142 	u32 rfoff_reason;
143 
144 	/* RF OFF Level */
145 	u32 cur_ps_level;
146 	u32 reg_rfps_level;
147 
148 	uint	ips_enter_cnts;
149 	uint	ips_leave_cnts;
150 
151 	u8 ips_mode;
152 	u8 ips_org_mode;
153 	u8 ips_mode_req; /*  used to accept the mode setting request, will update to ipsmode later */
154 	bool bips_processing;
155 	unsigned long ips_deny_time; /* will deny IPS when system time is smaller than this */
156 	u8 pre_ips_type;/*  0: default flow, 1: carddisbale flow */
157 
158 	/*  ps_deny: if 0, power save is free to go; otherwise deny all kinds of power save. */
159 	/*  Use enum ps_deny_reason to decide reason. */
160 	/*  Don't access this variable directly without control function, */
161 	/*  and this variable should be protected by lock. */
162 	u32 ps_deny;
163 
164 	u8 ps_processing; /* temporarily used to mark whether in rtw_ps_processor */
165 
166 	u8 fw_psmode_iface_id;
167 	u8 bLeisurePs;
168 	u8 LpsIdleCount;
169 	u8 power_mgnt;
170 	u8 org_power_mgnt;
171 	bool fw_current_in_ps_mode;
172 	unsigned long	DelayLPSLastTimeStamp;
173 	s32		pnp_current_pwr_state;
174 	u8 pnp_bstop_trx;
175 
176 
177 	u8 bInternalAutoSuspend;
178 	u8 bInSuspend;
179 
180 	u8 bAutoResume;
181 	u8 autopm_cnt;
182 
183 	u8 bSupportRemoteWakeup;
184 	u8 wowlan_wake_reason;
185 	u8 wowlan_ap_mode;
186 	u8 wowlan_mode;
187 	struct timer_list	pwr_state_check_timer;
188 	struct adapter *adapter;
189 	int		pwr_state_check_interval;
190 	u8 pwr_state_check_cnts;
191 
192 	int		ps_flag; /* used by autosuspend */
193 
194 	enum rt_rf_power_state	rf_pwrstate;/* cur power state, only for IPS */
195 	/* rt_rf_power_state	current_rfpwrstate; */
196 	enum rt_rf_power_state	change_rfpwrstate;
197 
198 	u8 bHWPowerdown; /* power down mode selection. 0:radio off, 1:power down */
199 	u8 bHWPwrPindetect; /* come from registrypriv.hwpwrp_detect. enable power down function. 0:disable, 1:enable */
200 	u8 bkeepfwalive;
201 	u8 brfoffbyhw;
202 	unsigned long PS_BBRegBackup[PSBBREG_TOTALCNT];
203 };
204 
205 #define rtw_ips_mode_req(pwrctl, ips_mode) \
206 	((pwrctl)->ips_mode_req = (ips_mode))
207 
208 #define RTW_PWR_STATE_CHK_INTERVAL 2000
209 
210 #define _rtw_set_pwr_state_check_timer(pwrctl, ms) \
211 	do { \
212 		_set_timer(&(pwrctl)->pwr_state_check_timer, (ms)); \
213 	} while (0)
214 
215 extern void rtw_init_pwrctrl_priv(struct adapter *adapter);
216 extern void rtw_free_pwrctrl_priv(struct adapter *adapter);
217 
218 s32 rtw_register_task_alive(struct adapter *, u32 task);
219 void rtw_unregister_task_alive(struct adapter *, u32 task);
220 extern s32 rtw_register_tx_alive(struct adapter *padapter);
221 extern void rtw_unregister_tx_alive(struct adapter *padapter);
222 extern s32 rtw_register_cmd_alive(struct adapter *padapter);
223 extern void rtw_unregister_cmd_alive(struct adapter *padapter);
224 extern void cpwm_int_hdl(struct adapter *padapter, struct reportpwrstate_parm *preportpwrstate);
225 extern void LPS_Leave_check(struct adapter *padapter);
226 
227 extern void LeaveAllPowerSaveMode(struct adapter *Adapter);
228 extern void LeaveAllPowerSaveModeDirect(struct adapter *Adapter);
229 void _ips_enter(struct adapter *padapter);
230 void ips_enter(struct adapter *padapter);
231 int _ips_leave(struct adapter *padapter);
232 int ips_leave(struct adapter *padapter);
233 
234 void rtw_ps_processor(struct adapter *padapter);
235 
236 s32 LPS_RF_ON_check(struct adapter *padapter, u32 delay_ms);
237 void LPS_Enter(struct adapter *padapter, const char *msg);
238 void LPS_Leave(struct adapter *padapter, const char *msg);
239 void traffic_check_for_leave_lps(struct adapter *padapter, u8 tx, u32 tx_packets);
240 void rtw_set_ps_mode(struct adapter *padapter, u8 ps_mode, u8 smart_ps, u8 bcn_ant_mode, const char *msg);
241 void rtw_set_rpwm(struct adapter *padapter, u8 val8);
242 
243 void rtw_set_ips_deny(struct adapter *padapter, u32 ms);
244 int _rtw_pwr_wakeup(struct adapter *padapter, u32 ips_deffer_ms, const char *caller);
245 #define rtw_pwr_wakeup(adapter) _rtw_pwr_wakeup(adapter, RTW_PWR_STATE_CHK_INTERVAL, __func__)
246 #define rtw_pwr_wakeup_ex(adapter, ips_deffer_ms) _rtw_pwr_wakeup(adapter, ips_deffer_ms, __func__)
247 int rtw_pm_set_ips(struct adapter *padapter, u8 mode);
248 int rtw_pm_set_lps(struct adapter *padapter, u8 mode);
249 
250 void rtw_ps_deny(struct adapter *padapter, enum ps_deny_reason reason);
251 void rtw_ps_deny_cancel(struct adapter *padapter, enum ps_deny_reason reason);
252 u32 rtw_ps_deny_get(struct adapter *padapter);
253 
254 #endif  /* __RTL871X_PWRCTRL_H_ */
255