blob: 507cd91d7eed3418a2c69e57fe98410b23031242 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@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 "wl1271_reg.h"
25#include "wl1271_ps.h"
26#include "wl1271_spi.h"
27
28#define WL1271_WAKEUP_TIMEOUT 500
29
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030030void wl1271_elp_work(struct work_struct *work)
31{
32 struct delayed_work *dwork;
33 struct wl1271 *wl;
34
35 dwork = container_of(work, struct delayed_work, work);
36 wl = container_of(dwork, struct wl1271, elp_work);
37
38 wl1271_debug(DEBUG_PSM, "elp work");
39
40 mutex_lock(&wl->mutex);
41
Juuso Oikarinen5d0af492009-10-12 15:08:51 +030042 if (wl->elp || !wl->psm)
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030043 goto out;
44
45 wl1271_debug(DEBUG_PSM, "chip to elp");
Juuso Oikarinen74621412009-10-12 15:08:54 +030046 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030047 wl->elp = true;
48
49out:
50 mutex_unlock(&wl->mutex);
51}
52
53#define ELP_ENTRY_DELAY 5
54
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030055/* Routines to toggle sleep mode while in ELP */
56void wl1271_ps_elp_sleep(struct wl1271 *wl)
57{
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030058 if (wl->psm) {
59 cancel_delayed_work(&wl->elp_work);
60 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
61 msecs_to_jiffies(ELP_ENTRY_DELAY));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030062 }
63}
64
65int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
66{
67 DECLARE_COMPLETION_ONSTACK(compl);
68 unsigned long flags;
69 int ret;
70 u32 start_time = jiffies;
71 bool pending = false;
72
73 if (!wl->elp)
74 return 0;
75
76 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
77
78 /*
79 * The spinlock is required here to synchronize both the work and
80 * the completion variable in one entity.
81 */
82 spin_lock_irqsave(&wl->wl_lock, flags);
83 if (work_pending(&wl->irq_work) || chip_awake)
84 pending = true;
85 else
86 wl->elp_compl = &compl;
87 spin_unlock_irqrestore(&wl->wl_lock, flags);
88
Juuso Oikarinen74621412009-10-12 15:08:54 +030089 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030090
91 if (!pending) {
92 ret = wait_for_completion_timeout(
93 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
94 if (ret == 0) {
95 wl1271_error("ELP wakeup timeout!");
96 ret = -ETIMEDOUT;
97 goto err;
98 } else if (ret < 0) {
99 wl1271_error("ELP wakeup completion error.");
100 goto err;
101 }
102 }
103
104 wl->elp = false;
105
106 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
107 jiffies_to_msecs(jiffies - start_time));
108 goto out;
109
110err:
111 spin_lock_irqsave(&wl->wl_lock, flags);
112 wl->elp_compl = NULL;
113 spin_unlock_irqrestore(&wl->wl_lock, flags);
114 return ret;
115
116out:
117 return 0;
118}
119
120int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
121{
122 int ret;
123
124 switch (mode) {
125 case STATION_POWER_SAVE_MODE:
126 wl1271_debug(DEBUG_PSM, "entering psm");
Juuso Oikarinen19221672009-10-08 21:56:35 +0300127
128 /* enable beacon filtering */
129 ret = wl1271_acx_beacon_filter_opt(wl, true);
130 if (ret < 0)
131 return ret;
132
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300133 /* enable beacon early termination */
134 ret = wl1271_acx_bet_enable(wl, true);
135 if (ret < 0)
136 return ret;
137
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300138 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
139 if (ret < 0)
140 return ret;
141
142 wl1271_ps_elp_sleep(wl);
143 if (ret < 0)
144 return ret;
145
146 wl->psm = 1;
147 break;
148 case STATION_ACTIVE_MODE:
149 default:
150 wl1271_debug(DEBUG_PSM, "leaving psm");
151 ret = wl1271_ps_elp_wakeup(wl, false);
152 if (ret < 0)
153 return ret;
154
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300155 /* disable beacon early termination */
156 ret = wl1271_acx_bet_enable(wl, false);
157 if (ret < 0)
158 return ret;
159
Juuso Oikarinen19221672009-10-08 21:56:35 +0300160 /* disable beacon filtering */
161 ret = wl1271_acx_beacon_filter_opt(wl, false);
162 if (ret < 0)
163 return ret;
164
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300165 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
166 if (ret < 0)
167 return ret;
168
169 wl->psm = 0;
170 break;
171 }
172
173 return ret;
174}
175
176