Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 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 | } |
Luis de Bethencourt | 1ca9688 | 2015-10-19 18:14:29 +0100 | [diff] [blame] | 47 | if (padapter->bDriverStopped || padapter->bSurpriseRemoved) |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 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, |
Raphaƫl Beamonte | bef611a | 2016-09-09 11:31:45 -0400 | [diff] [blame] | 55 | * temp mark some code. |
| 56 | */ |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 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 | |
| 70 | void 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 | /* if driver is in active state, we dont need set smart_ps.*/ |
| 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; |
James A Shackleford | f2355b6 | 2014-07-09 15:10:46 -0400 | [diff] [blame] | 86 | schedule_work(&pwrpriv->SetPSModeWorkItem); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Caller:ISR handler... |
| 92 | * |
| 93 | * This will be called when CPWM interrupt is up. |
| 94 | * |
Justin P. Mattock | 3cbeb42 | 2011-02-24 22:14:23 -0800 | [diff] [blame] | 95 | * using to update cpwn of drv; and drv will make a decision to up or |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 96 | * down pwr level |
| 97 | */ |
| 98 | void 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); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 103 | |
| 104 | if (pwrpriv->cpwm_tog == ((preportpwrstate->state) & 0x80)) |
| 105 | return; |
Sudip Mukherjee | 39a6e73 | 2015-05-15 14:49:39 +0530 | [diff] [blame] | 106 | del_timer(&padapter->pwrctrlpriv.rpwm_check_timer); |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 107 | mutex_lock(&pwrpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 108 | pwrpriv->cpwm = (preportpwrstate->state) & 0xf; |
| 109 | if (pwrpriv->cpwm >= PS_STATE_S2) { |
| 110 | if (pwrpriv->alives & CMD_ALIVE) |
Binoy Jayan | 0f89054 | 2016-06-02 09:54:07 +0530 | [diff] [blame] | 111 | complete(&(pcmdpriv->cmd_queue_comp)); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 112 | } |
| 113 | pwrpriv->cpwm_tog = (preportpwrstate->state) & 0x80; |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 114 | mutex_unlock(&pwrpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline void register_task_alive(struct pwrctrl_priv *pwrctrl, uint tag) |
| 118 | { |
| 119 | pwrctrl->alives |= tag; |
| 120 | } |
| 121 | |
| 122 | static inline void unregister_task_alive(struct pwrctrl_priv *pwrctrl, uint tag) |
| 123 | { |
| 124 | if (pwrctrl->alives & tag) |
| 125 | pwrctrl->alives ^= tag; |
| 126 | } |
| 127 | |
| 128 | static void _rpwm_check_handler (struct _adapter *padapter) |
| 129 | { |
| 130 | struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv; |
| 131 | |
Luis de Bethencourt | 1ca9688 | 2015-10-19 18:14:29 +0100 | [diff] [blame] | 132 | if (padapter->bDriverStopped || padapter->bSurpriseRemoved) |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 133 | return; |
| 134 | if (pwrpriv->cpwm != pwrpriv->rpwm) |
James A Shackleford | f2355b6 | 2014-07-09 15:10:46 -0400 | [diff] [blame] | 135 | schedule_work(&pwrpriv->rpwm_workitem); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static 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); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 144 | if (!pwrpriv->bSleep) { |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 145 | mutex_lock(&pwrpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 146 | if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) |
| 147 | r8712_set_rpwm(padapter, PS_STATE_S4); |
Wei Yongjun | 4db7c0b | 2016-09-15 10:12:05 +0000 | [diff] [blame] | 148 | mutex_unlock(&pwrpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 149 | } |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static 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); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 158 | if (pwrpriv->cpwm != pwrpriv->rpwm) { |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 159 | mutex_lock(&pwrpriv->mutex_lock); |
Sudip Mukherjee | e29d3eb | 2014-10-27 17:42:25 +0530 | [diff] [blame] | 160 | r8712_read8(padapter, SDIO_HCPWM); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 161 | pwrpriv->rpwm_retry = 1; |
| 162 | r8712_set_rpwm(padapter, pwrpriv->rpwm); |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 163 | mutex_unlock(&pwrpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 164 | } |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Vaishali Thakkar | e922df7 | 2015-03-03 22:34:22 +0530 | [diff] [blame] | 167 | static void rpwm_check_handler (unsigned long data) |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 168 | { |
Vaishali Thakkar | e922df7 | 2015-03-03 22:34:22 +0530 | [diff] [blame] | 169 | struct _adapter *adapter = (struct _adapter *)data; |
Tapasweni Pathak | 02a29d2 | 2014-09-24 16:34:56 +0530 | [diff] [blame] | 170 | |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 171 | _rpwm_check_handler(adapter); |
| 172 | } |
| 173 | |
| 174 | void 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)); |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 179 | mutex_init(&pwrctrlpriv->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 180 | pwrctrlpriv->cpwm = PS_STATE_S4; |
| 181 | pwrctrlpriv->pwr_mode = PS_MODE_ACTIVE; |
| 182 | pwrctrlpriv->smart_ps = 0; |
| 183 | pwrctrlpriv->tog = 0x80; |
| 184 | /* clear RPWM to ensure driver and fw back to initial state. */ |
| 185 | r8712_write8(padapter, 0x1025FE58, 0); |
James A Shackleford | 9f95248 | 2014-07-09 15:10:45 -0400 | [diff] [blame] | 186 | INIT_WORK(&pwrctrlpriv->SetPSModeWorkItem, SetPSModeWorkItemCallback); |
| 187 | INIT_WORK(&pwrctrlpriv->rpwm_workitem, rpwm_workitem_callback); |
Vaishali Thakkar | e922df7 | 2015-03-03 22:34:22 +0530 | [diff] [blame] | 188 | setup_timer(&pwrctrlpriv->rpwm_check_timer, rpwm_check_handler, |
| 189 | (unsigned long)padapter); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | Caller: r8712_cmd_thread |
| 194 | |
| 195 | Check if the fw_pwrstate is okay for issuing cmd. |
| 196 | If not (cpwm should be is less than P2 state), then the sub-routine |
| 197 | will raise the cpwm to be greater than or equal to P2. |
| 198 | |
| 199 | Calling Context: Passive |
| 200 | |
| 201 | Return Value: |
| 202 | |
| 203 | _SUCCESS: r8712_cmd_thread can issue cmds to firmware afterwards. |
| 204 | _FAIL: r8712_cmd_thread can not do anything. |
| 205 | */ |
| 206 | sint r8712_register_cmd_alive(struct _adapter *padapter) |
| 207 | { |
| 208 | uint res = _SUCCESS; |
| 209 | struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv; |
| 210 | |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 211 | mutex_lock(&pwrctrl->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 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 | } |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 217 | mutex_unlock(&pwrctrl->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 218 | return res; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | Caller: ISR |
| 223 | |
| 224 | If ISR's txdone, |
| 225 | No more pkts for TX, |
| 226 | Then driver shall call this fun. to power down firmware again. |
| 227 | */ |
| 228 | |
| 229 | void r8712_unregister_cmd_alive(struct _adapter *padapter) |
| 230 | { |
| 231 | struct pwrctrl_priv *pwrctrl = &padapter->pwrctrlpriv; |
| 232 | |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 233 | mutex_lock(&pwrctrl->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 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 | } |
Binoy Jayan | 5c2ba8b | 2016-08-22 10:09:42 +0530 | [diff] [blame] | 243 | mutex_unlock(&pwrctrl->mutex_lock); |
Larry Finger | 2865d42 | 2010-08-20 10:15:30 -0500 | [diff] [blame] | 244 | } |