1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define _RTL871X_PWRCTRL_C_
30
31#include "osdep_service.h"
32#include "drv_types.h"
33#include "osdep_intf.h"
34
35#define RTL8712_SDIO_LOCAL_BASE 0X10100000
36#define SDIO_HCPWM (RTL8712_SDIO_LOCAL_BASE + 0x0081)
37
38void r8712_set_rpwm(struct _adapter *padapter, u8 val8)
39{
40 u8 rpwm;
41 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
42
43 if (pwrpriv->rpwm == val8) {
44 if (pwrpriv->rpwm_retry == 0)
45 return;
46 }
47 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
48 return;
49 rpwm = val8 | pwrpriv->tog;
50 switch (val8) {
51 case PS_STATE_S1:
52 pwrpriv->cpwm = val8;
53 break;
54 case PS_STATE_S2:
55
56
57 case PS_STATE_S3:
58 case PS_STATE_S4:
59 pwrpriv->cpwm = val8;
60 break;
61 default:
62 break;
63 }
64 pwrpriv->rpwm_retry = 0;
65 pwrpriv->rpwm = val8;
66 r8712_write8(padapter, 0x1025FE58, rpwm);
67 pwrpriv->tog += 0x80;
68}
69
70void r8712_set_ps_mode(struct _adapter *padapter, uint ps_mode, uint smart_ps)
71{
72 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
73
74 if (ps_mode > PM_Card_Disable)
75 return;
76
77 if (ps_mode == PS_MODE_ACTIVE)
78 smart_ps = 0;
79 if ((pwrpriv->pwr_mode != ps_mode) || (pwrpriv->smart_ps != smart_ps)) {
80 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
81 pwrpriv->bSleep = true;
82 else
83 pwrpriv->bSleep = false;
84 pwrpriv->pwr_mode = ps_mode;
85 pwrpriv->smart_ps = smart_ps;
86 schedule_work(&pwrpriv->SetPSModeWorkItem);
87 }
88}
89
90
91
92
93
94
95
96
97
98void r8712_cpwm_int_hdl(struct _adapter *padapter,
99 struct reportpwrstate_parm *preportpwrstate)
100{
101 struct pwrctrl_priv *pwrpriv = &(padapter->pwrctrlpriv);
102 struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
103
104 if (pwrpriv->cpwm_tog == ((preportpwrstate->state) & 0x80))
105 return;
106 del_timer(&padapter->pwrctrlpriv.rpwm_check_timer);
107 mutex_lock(&pwrpriv->mutex_lock);
108 pwrpriv->cpwm = (preportpwrstate->state) & 0xf;
109 if (pwrpriv->cpwm >= PS_STATE_S2) {
110 if (pwrpriv->alives & CMD_ALIVE)
111 complete(&(pcmdpriv->cmd_queue_comp));
112 }
113 pwrpriv->cpwm_tog = (preportpwrstate->state) & 0x80;
114 mutex_unlock(&pwrpriv->mutex_lock);
115}
116
117static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
118{
119 pwrctrl->alives |= tag;
120}
121
122static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
123{
124 if (pwrctrl->alives & tag)
125 pwrctrl->alives ^= tag;
126}
127
128static void _rpwm_check_handler (struct _adapter *padapter)
129{
130 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
131
132 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
133 return;
134 if (pwrpriv->cpwm != pwrpriv->rpwm)
135 schedule_work(&pwrpriv->rpwm_workitem);
136}
137
138static void SetPSModeWorkItemCallback(struct work_struct *work)
139{
140 struct pwrctrl_priv *pwrpriv = container_of(work,
141 struct pwrctrl_priv, SetPSModeWorkItem);
142 struct _adapter *padapter = container_of(pwrpriv,
143 struct _adapter, pwrctrlpriv);
144 if (!pwrpriv->bSleep) {
145 mutex_lock(&pwrpriv->mutex_lock);
146 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
147 r8712_set_rpwm(padapter, PS_STATE_S4);
148 mutex_unlock(&pwrpriv->mutex_lock);
149 }
150}
151
152static void rpwm_workitem_callback(struct work_struct *work)
153{
154 struct pwrctrl_priv *pwrpriv = container_of(work,
155 struct pwrctrl_priv, rpwm_workitem);
156 struct _adapter *padapter = container_of(pwrpriv,
157 struct _adapter, pwrctrlpriv);
158 if (pwrpriv->cpwm != pwrpriv->rpwm) {
159 mutex_lock(&pwrpriv->mutex_lock);
160 r8712_read8(padapter, SDIO_HCPWM);
161 pwrpriv->rpwm_retry = 1;
162 r8712_set_rpwm(padapter, pwrpriv->rpwm);
163 mutex_unlock(&pwrpriv->mutex_lock);
164 }
165}
166
167static void rpwm_check_handler (unsigned long data)
168{
169 struct _adapter *adapter = (struct _adapter *)data;
170
171 _rpwm_check_handler(adapter);
172}
173
174void r8712_init_pwrctrl_priv(struct _adapter *padapter)
175{
176 struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
177
178 memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv));
179 mutex_init(&pwrctrlpriv->mutex_lock);
180 pwrctrlpriv->cpwm = PS_STATE_S4;
181 pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
182 pwrctrlpriv->smart_ps = 0;
183 pwrctrlpriv->tog = 0x80;
184
185 r8712_write8(padapter, 0x1025FE58, 0);
186 INIT_WORK(&pwrctrlpriv->SetPSModeWorkItem, SetPSModeWorkItemCallback);
187 INIT_WORK(&pwrctrlpriv->rpwm_workitem, rpwm_workitem_callback);
188 setup_timer(&pwrctrlpriv->rpwm_check_timer, rpwm_check_handler,
189 (unsigned long)padapter);
190}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206sint r8712_register_cmd_alive(struct _adapter *padapter)
207{
208 uint res = _SUCCESS;
209 struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
210
211 mutex_lock(&pwrctrl->mutex_lock);
212 register_task_alive(pwrctrl, CMD_ALIVE);
213 if (pwrctrl->cpwm < PS_STATE_S2) {
214 r8712_set_rpwm(padapter, PS_STATE_S3);
215 res = _FAIL;
216 }
217 mutex_unlock(&pwrctrl->mutex_lock);
218 return res;
219}
220
221
222
223
224
225
226
227
228
229void r8712_unregister_cmd_alive(struct _adapter *padapter)
230{
231 struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
232
233 mutex_lock(&pwrctrl->mutex_lock);
234 unregister_task_alive(pwrctrl, CMD_ALIVE);
235 if ((pwrctrl->cpwm > PS_STATE_S2) &&
236 (pwrctrl->pwr_mode > PS_MODE_ACTIVE)) {
237 if ((pwrctrl->alives == 0) &&
238 (check_fwstate(&padapter->mlmepriv,
239 _FW_UNDER_LINKING) != true)) {
240 r8712_set_rpwm(padapter, PS_STATE_S0);
241 }
242 }
243 mutex_unlock(&pwrctrl->mutex_lock);
244}
245