1 /******************************************************************************
2  * rtl871x_pwrctrl.c
3  *
4  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5  * Linux device driver for RTL8192SU
6  *
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.
10  *
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
14  * more details.
15  *
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
19  *
20  * Modifications for inclusion into the Linux staging tree are
21  * Copyright(c) 2010 Larry Finger. All rights reserved.
22  *
23  * Contact information:
24  * WLAN FAE <wlanfae@realtek.com>
25  * Larry Finger <Larry.Finger@lwfinger.net>
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 
38 void 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:/* only for USB normal powersave mode use,
55 			  * temp mark some code. */
56 	case PS_STATE_S3:
57 	case PS_STATE_S4:
58 		pwrpriv->cpwm = val8;
59 		break;
60 	default:
61 		break;
62 	}
63 	pwrpriv->rpwm_retry = 0;
64 	pwrpriv->rpwm = val8;
65 	r8712_write8(padapter, 0x1025FE58, rpwm);
66 	pwrpriv->tog += 0x80;
67 }
68 
69 void r8712_set_ps_mode(struct _adapter *padapter, uint ps_mode, uint smart_ps)
70 {
71 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
72 
73 	if (ps_mode > PM_Card_Disable)
74 		return;
75 	/* if driver is in active state, we dont need set smart_ps.*/
76 	if (ps_mode == PS_MODE_ACTIVE)
77 		smart_ps = 0;
78 	if ((pwrpriv->pwr_mode != ps_mode) || (pwrpriv->smart_ps != smart_ps)) {
79 		if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
80 			pwrpriv->bSleep = true;
81 		else
82 			pwrpriv->bSleep = false;
83 		pwrpriv->pwr_mode = ps_mode;
84 		pwrpriv->smart_ps = smart_ps;
85 		schedule_work(&pwrpriv->SetPSModeWorkItem);
86 	}
87 }
88 
89 /*
90  * Caller:ISR handler...
91  *
92  * This will be called when CPWM interrupt is up.
93  *
94  * using to update cpwn of drv; and drv will make a decision to up or
95  * down pwr level
96  */
97 void r8712_cpwm_int_hdl(struct _adapter *padapter,
98 			struct reportpwrstate_parm *preportpwrstate)
99 {
100 	struct pwrctrl_priv *pwrpriv = &(padapter->pwrctrlpriv);
101 	struct cmd_priv	*pcmdpriv = &(padapter->cmdpriv);
102 
103 	if (pwrpriv->cpwm_tog == ((preportpwrstate->state) & 0x80))
104 		return;
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));
111 	}
112 	pwrpriv->cpwm_tog = (preportpwrstate->state) & 0x80;
113 	up(&pwrpriv->lock);
114 }
115 
116 static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
117 {
118 		pwrctrl->alives |= tag;
119 }
120 
121 static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, uint tag)
122 {
123 	if (pwrctrl->alives & tag)
124 		pwrctrl->alives ^= tag;
125 }
126 
127 static void _rpwm_check_handler (struct _adapter *padapter)
128 {
129 	struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
130 
131 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
132 		return;
133 	if (pwrpriv->cpwm != pwrpriv->rpwm)
134 		schedule_work(&pwrpriv->rpwm_workitem);
135 }
136 
137 static void SetPSModeWorkItemCallback(struct work_struct *work)
138 {
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);
147 		up(&pwrpriv->lock);
148 	}
149 }
150 
151 static void rpwm_workitem_callback(struct work_struct *work)
152 {
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);
162 		up(&pwrpriv->lock);
163 	}
164 }
165 
166 static void rpwm_check_handler (unsigned long data)
167 {
168 	struct _adapter *adapter = (struct _adapter *)data;
169 
170 	_rpwm_check_handler(adapter);
171 }
172 
173 void r8712_init_pwrctrl_priv(struct _adapter *padapter)
174 {
175 	struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
176 
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);
189 }
190 
191 /*
192 Caller: r8712_cmd_thread
193 
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.
197 
198 Calling Context: Passive
199 
200 Return Value:
201 
202 _SUCCESS: r8712_cmd_thread can issue cmds to firmware afterwards.
203 _FAIL: r8712_cmd_thread can not do anything.
204 */
205 sint r8712_register_cmd_alive(struct _adapter *padapter)
206 {
207 	uint res = _SUCCESS;
208 	struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
209 
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);
214 		res = _FAIL;
215 	}
216 	up(&pwrctrl->lock);
217 	return res;
218 }
219 
220 /*
221 Caller: ISR
222 
223 If ISR's txdone,
224 No more pkts for TX,
225 Then driver shall call this fun. to power down firmware again.
226 */
227 
228 void r8712_unregister_cmd_alive(struct _adapter *padapter)
229 {
230 	struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv;
231 
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);
240 		}
241 	}
242 	up(&pwrctrl->lock);
243 }
244