wl1251: rename reg.h to wl1251_reg.h
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / wl12xx / wl1251_ps.c
blobc53e28727ed4e97708030d5cbe21074717a4d68a
1 /*
2 * This file is part of wl1251
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Kalle Valo <kalle.valo@nokia.com>
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.
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.
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
24 #include "wl1251_reg.h"
25 #include "wl1251_ps.h"
26 #include "wl1251_cmd.h"
27 #include "wl1251_io.h"
29 #define WL1251_WAKEUP_TIMEOUT 2000
31 /* Routines to toggle sleep mode while in ELP */
32 void wl1251_ps_elp_sleep(struct wl1251 *wl)
34 if (wl->elp || !wl->psm)
35 return;
37 wl1251_debug(DEBUG_PSM, "chip to elp");
39 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
41 wl->elp = true;
44 int wl1251_ps_elp_wakeup(struct wl1251 *wl)
46 unsigned long timeout;
47 u32 elp_reg;
49 if (!wl->elp)
50 return 0;
52 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
54 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
56 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
58 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
61 * FIXME: we should wait for irq from chip but, as a temporary
62 * solution to simplify locking, let's poll instead
64 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
65 if (time_after(jiffies, timeout)) {
66 wl1251_error("elp wakeup timeout");
67 return -ETIMEDOUT;
69 msleep(1);
70 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
73 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
74 jiffies_to_msecs(jiffies) -
75 (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
77 wl->elp = false;
79 return 0;
82 static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
84 int ret;
86 if (enable) {
87 wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
89 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
90 if (ret < 0)
91 return ret;
93 wl1251_ps_elp_sleep(wl);
94 } else {
95 wl1251_debug(DEBUG_PSM, "sleep auth cam");
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.
104 wl1251_ps_elp_wakeup(wl);
106 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
107 if (ret < 0)
108 return ret;
111 return 0;
114 int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
116 int ret;
118 switch (mode) {
119 case STATION_POWER_SAVE_MODE:
120 wl1251_debug(DEBUG_PSM, "entering psm");
122 ret = wl1251_acx_wake_up_conditions(wl,
123 WAKE_UP_EVENT_DTIM_BITMAP,
124 wl->listen_int);
125 if (ret < 0)
126 return ret;
128 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
129 if (ret < 0)
130 return ret;
132 ret = wl1251_ps_set_elp(wl, true);
133 if (ret < 0)
134 return ret;
136 wl->psm = 1;
137 break;
138 case STATION_ACTIVE_MODE:
139 default:
140 wl1251_debug(DEBUG_PSM, "leaving psm");
141 ret = wl1251_ps_set_elp(wl, false);
142 if (ret < 0)
143 return ret;
145 ret = wl1251_acx_wake_up_conditions(wl,
146 WAKE_UP_EVENT_DTIM_BITMAP,
147 wl->listen_int);
148 if (ret < 0)
149 return ret;
151 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
152 if (ret < 0)
153 return ret;
155 wl->psm = 0;
156 break;
159 return ret;