Merge commit 'linux-pnfs/nfs41-for-2.6.31' into nfsv41-for-2.6.31
[linux-2.6] / drivers / net / wireless / wl12xx / ps.c
1 /*
2  * This file is part of wl12xx
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Kalle Valo <kalle.valo@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include "reg.h"
25 #include "ps.h"
26 #include "spi.h"
27
28 #define WL12XX_WAKEUP_TIMEOUT 2000
29
30 /* Routines to toggle sleep mode while in ELP */
31 void wl12xx_ps_elp_sleep(struct wl12xx *wl)
32 {
33         if (wl->elp || !wl->psm)
34                 return;
35
36         wl12xx_debug(DEBUG_PSM, "chip to elp");
37
38         wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
39
40         wl->elp = true;
41 }
42
43 int wl12xx_ps_elp_wakeup(struct wl12xx *wl)
44 {
45         unsigned long timeout;
46         u32 elp_reg;
47
48         if (!wl->elp)
49                 return 0;
50
51         wl12xx_debug(DEBUG_PSM, "waking up chip from elp");
52
53         timeout = jiffies + msecs_to_jiffies(WL12XX_WAKEUP_TIMEOUT);
54
55         wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
56
57         elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
58
59         /*
60          * FIXME: we should wait for irq from chip but, as a temporary
61          * solution to simplify locking, let's poll instead
62          */
63         while (!(elp_reg & ELPCTRL_WLAN_READY)) {
64                 if (time_after(jiffies, timeout)) {
65                         wl12xx_error("elp wakeup timeout");
66                         return -ETIMEDOUT;
67                 }
68                 msleep(1);
69                 elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
70         }
71
72         wl12xx_debug(DEBUG_PSM, "wakeup time: %u ms",
73                      jiffies_to_msecs(jiffies) -
74                      (jiffies_to_msecs(timeout) - WL12XX_WAKEUP_TIMEOUT));
75
76         wl->elp = false;
77
78         return 0;
79 }
80
81 static int wl12xx_ps_set_elp(struct wl12xx *wl, bool enable)
82 {
83         int ret;
84
85         if (enable) {
86                 wl12xx_debug(DEBUG_PSM, "sleep auth psm/elp");
87
88                 /*
89                  * FIXME: we should PSM_ELP, but because of firmware wakeup
90                  * problems let's use only PSM_PS
91                  */
92                 ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_PS);
93                 if (ret < 0)
94                         return ret;
95
96                 wl12xx_ps_elp_sleep(wl);
97         } else {
98                 wl12xx_debug(DEBUG_PSM, "sleep auth cam");
99
100                 /*
101                  * When the target is in ELP, we can only
102                  * access the ELP control register. Thus,
103                  * we have to wake the target up before
104                  * changing the power authorization.
105                  */
106
107                 wl12xx_ps_elp_wakeup(wl);
108
109                 ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_CAM);
110                 if (ret < 0)
111                         return ret;
112         }
113
114         return 0;
115 }
116
117 int wl12xx_ps_set_mode(struct wl12xx *wl, enum acx_ps_mode mode)
118 {
119         int ret;
120
121         switch (mode) {
122         case STATION_POWER_SAVE_MODE:
123                 wl12xx_debug(DEBUG_PSM, "entering psm");
124                 ret = wl12xx_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
125                 if (ret < 0)
126                         return ret;
127
128                 ret = wl12xx_ps_set_elp(wl, true);
129                 if (ret < 0)
130                         return ret;
131
132                 wl->psm = 1;
133                 break;
134         case STATION_ACTIVE_MODE:
135         default:
136                 wl12xx_debug(DEBUG_PSM, "leaving psm");
137                 ret = wl12xx_ps_set_elp(wl, false);
138                 if (ret < 0)
139                         return ret;
140
141                 ret = wl12xx_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
142                 if (ret < 0)
143                         return ret;
144
145                 wl->psm = 0;
146                 break;
147         }
148
149         return ret;
150 }
151