]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/wl12xx/wl1251_ps.c
05816778f3dbed1b3e87589ebdcc38e0984fac8d
[karo-tx-linux.git] / drivers / net / wireless / wl12xx / wl1251_ps.c
1 /*
2  * This file is part of wl1251
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 "wl1251_ps.h"
26 #include "wl1251_cmd.h"
27 #include "wl1251_io.h"
28
29 #define WL1251_WAKEUP_TIMEOUT 2000
30
31 /* Routines to toggle sleep mode while in ELP */
32 void wl1251_ps_elp_sleep(struct wl1251 *wl)
33 {
34         if (wl->elp || !wl->psm)
35                 return;
36
37         wl1251_debug(DEBUG_PSM, "chip to elp");
38
39         wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
40
41         wl->elp = true;
42 }
43
44 int wl1251_ps_elp_wakeup(struct wl1251 *wl)
45 {
46         unsigned long timeout;
47         u32 elp_reg;
48
49         if (!wl->elp)
50                 return 0;
51
52         wl1251_debug(DEBUG_PSM, "waking up chip from elp");
53
54         timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
55
56         wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
57
58         elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
59
60         /*
61          * FIXME: we should wait for irq from chip but, as a temporary
62          * solution to simplify locking, let's poll instead
63          */
64         while (!(elp_reg & ELPCTRL_WLAN_READY)) {
65                 if (time_after(jiffies, timeout)) {
66                         wl1251_error("elp wakeup timeout");
67                         return -ETIMEDOUT;
68                 }
69                 msleep(1);
70                 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
71         }
72
73         wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
74                      jiffies_to_msecs(jiffies) -
75                      (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
76
77         wl->elp = false;
78
79         return 0;
80 }
81
82 static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
83 {
84         int ret;
85
86         if (enable) {
87                 wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
88
89                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
90                 if (ret < 0)
91                         return ret;
92
93                 wl1251_ps_elp_sleep(wl);
94         } else {
95                 wl1251_debug(DEBUG_PSM, "sleep auth cam");
96
97                 /*
98                  * When the target is in ELP, we can only
99                  * access the ELP control register. Thus,
100                  * we have to wake the target up before
101                  * changing the power authorization.
102                  */
103
104                 wl1251_ps_elp_wakeup(wl);
105
106                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
107                 if (ret < 0)
108                         return ret;
109         }
110
111         return 0;
112 }
113
114 int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
115 {
116         int ret;
117
118         switch (mode) {
119         case STATION_POWER_SAVE_MODE:
120                 wl1251_debug(DEBUG_PSM, "entering psm");
121                 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
122                 if (ret < 0)
123                         return ret;
124
125                 ret = wl1251_ps_set_elp(wl, true);
126                 if (ret < 0)
127                         return ret;
128
129                 wl->psm = 1;
130                 break;
131         case STATION_ACTIVE_MODE:
132         default:
133                 wl1251_debug(DEBUG_PSM, "leaving psm");
134                 ret = wl1251_ps_set_elp(wl, false);
135                 if (ret < 0)
136                         return ret;
137
138                 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
139                 if (ret < 0)
140                         return ret;
141
142                 wl->psm = 0;
143                 break;
144         }
145
146         return ret;
147 }
148