1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
27 ******************************************************************************/
29 #define _RTL871X_PWRCTRL_C_
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "osdep_intf.h"
35 #define RTL8712_SDIO_LOCAL_BASE 0X10100000
36 #define SDIO_HCPWM (RTL8712_SDIO_LOCAL_BASE + 0x0081)
38 void r8712_set_rpwm(struct _adapter *padapter, u8 val8)
41 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
43 if (pwrpriv->rpwm == val8) {
44 if (pwrpriv->rpwm_retry == 0)
47 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
49 rpwm = val8 | pwrpriv->tog;
54 case PS_STATE_S2:/* only for USB normal powersave mode use,
55 * temp mark some code. */
63 pwrpriv->rpwm_retry = 0;
65 r8712_write8(padapter, 0x1025FE58, rpwm);
69 void r8712_set_ps_mode(struct _adapter *padapter, uint ps_mode, uint smart_ps)
71 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
73 if (ps_mode > PM_Card_Disable)
75 /* if driver is in active state, we dont need set smart_ps.*/
76 if (ps_mode == PS_MODE_ACTIVE)
78 if ((pwrpriv->pwr_mode != ps_mode) || (pwrpriv->smart_ps != smart_ps)) {
79 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
80 pwrpriv->bSleep = true;
82 pwrpriv->bSleep = false;
83 pwrpriv->pwr_mode = ps_mode;
84 pwrpriv->smart_ps = smart_ps;
85 schedule_work(&pwrpriv->SetPSModeWorkItem);
90 * Caller:ISR handler...
92 * This will be called when CPWM interrupt is up.
94 * using to update cpwn of drv; and drv will make a decision to up or
97 void r8712_cpwm_int_hdl(struct _adapter *padapter,
98 struct reportpwrstate_parm *preportpwrstate)
100 struct pwrctrl_priv *pwrpriv = &(padapter->pwrctrlpriv);
101 struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
103 if (pwrpriv->cpwm_tog == ((preportpwrstate->state) & 0x80))
105 del_timer(&padapter->pwrctrlpriv.rpwm_check_timer);
106 _enter_pwrlock(&pwrpriv->lock);
107 pwrpriv->cpwm = (preportpwrstate->state) & 0xf;
108 if (pwrpriv->cpwm >= PS_STATE_S2) {
109 if (pwrpriv->alives & CMD_ALIVE)
110 up(&(pcmdpriv->cmd_queue_sema));
112 pwrpriv->cpwm_tog = (preportpwrstate->state) & 0x80;
116 static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
118 pwrctrl->alives |= tag;
121 static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
123 if (pwrctrl->alives & tag)
124 pwrctrl->alives ^= tag;
127 static void _rpwm_check_handler (struct _adapter *padapter)
129 struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
131 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
133 if (pwrpriv->cpwm != pwrpriv->rpwm)
134 schedule_work(&pwrpriv->rpwm_workitem);
137 static void SetPSModeWorkItemCallback(struct work_struct *work)
139 struct pwrctrl_priv *pwrpriv = container_of(work,
140 struct pwrctrl_priv, SetPSModeWorkItem);
141 struct _adapter *padapter = container_of(pwrpriv,
142 struct _adapter, pwrctrlpriv);
143 if (!pwrpriv->bSleep) {
144 _enter_pwrlock(&pwrpriv->lock);
145 if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
146 r8712_set_rpwm(padapter, PS_STATE_S4);
151 static void rpwm_workitem_callback(struct work_struct *work)
153 struct pwrctrl_priv *pwrpriv = container_of(work,
154 struct pwrctrl_priv, rpwm_workitem);
155 struct _adapter *padapter = container_of(pwrpriv,
156 struct _adapter, pwrctrlpriv);
157 if (pwrpriv->cpwm != pwrpriv->rpwm) {
158 _enter_pwrlock(&pwrpriv->lock);
159 r8712_read8(padapter, SDIO_HCPWM);
160 pwrpriv->rpwm_retry = 1;
161 r8712_set_rpwm(padapter, pwrpriv->rpwm);
166 static void rpwm_check_handler (unsigned long data)
168 struct _adapter *adapter = (struct _adapter *)data;
170 _rpwm_check_handler(adapter);
173 void r8712_init_pwrctrl_priv(struct _adapter *padapter)
175 struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
177 memset((unsigned char *)pwrctrlpriv, 0, sizeof(struct pwrctrl_priv));
178 sema_init(&pwrctrlpriv->lock, 1);
179 pwrctrlpriv->cpwm = PS_STATE_S4;
180 pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE;
181 pwrctrlpriv->smart_ps = 0;
182 pwrctrlpriv->tog = 0x80;
183 /* clear RPWM to ensure driver and fw back to initial state. */
184 r8712_write8(padapter, 0x1025FE58, 0);
185 INIT_WORK(&pwrctrlpriv->SetPSModeWorkItem, SetPSModeWorkItemCallback);
186 INIT_WORK(&pwrctrlpriv->rpwm_workitem, rpwm_workitem_callback);
187 setup_timer(&pwrctrlpriv->rpwm_check_timer, rpwm_check_handler,
188 (unsigned long)padapter);
192 Caller: r8712_cmd_thread
194 Check if the fw_pwrstate is okay for issuing cmd.
195 If not (cpwm should be is less than P2 state), then the sub-routine
196 will raise the cpwm to be greater than or equal to P2.
198 Calling Context: Passive
202 _SUCCESS: r8712_cmd_thread can issue cmds to firmware afterwards.
203 _FAIL: r8712_cmd_thread can not do anything.
205 sint r8712_register_cmd_alive(struct _adapter *padapter)
208 struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
210 _enter_pwrlock(&pwrctrl->lock);
211 register_task_alive(pwrctrl, CMD_ALIVE);
212 if (pwrctrl->cpwm < PS_STATE_S2) {
213 r8712_set_rpwm(padapter, PS_STATE_S3);
225 Then driver shall call this fun. to power down firmware again.
228 void r8712_unregister_cmd_alive(struct _adapter *padapter)
230 struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
232 _enter_pwrlock(&pwrctrl->lock);
233 unregister_task_alive(pwrctrl, CMD_ALIVE);
234 if ((pwrctrl->cpwm > PS_STATE_S2) &&
235 (pwrctrl->pwr_mode > PS_MODE_ACTIVE)) {
236 if ((pwrctrl->alives == 0) &&
237 (check_fwstate(&padapter->mlmepriv,
238 _FW_UNDER_LINKING) != true)) {
239 r8712_set_rpwm(padapter, PS_STATE_S0);