blob: 5a04482b93531d201ca994f58f2994b5a883554e [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"
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020026#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030027
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 Oikarinen71449f82009-12-11 15:41:07 +020042 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
43 !test_bit(WL1271_FLAG_PSM, &wl->flags))
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030044 goto out;
45
46 wl1271_debug(DEBUG_PSM, "chip to elp");
Juuso Oikarinen74621412009-10-12 15:08:54 +030047 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinen71449f82009-12-11 15:41:07 +020048 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030049
50out:
51 mutex_unlock(&wl->mutex);
52}
53
54#define ELP_ENTRY_DELAY 5
55
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030056/* Routines to toggle sleep mode while in ELP */
57void wl1271_ps_elp_sleep(struct wl1271 *wl)
58{
Juuso Oikarinen71449f82009-12-11 15:41:07 +020059 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030060 cancel_delayed_work(&wl->elp_work);
61 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
62 msecs_to_jiffies(ELP_ENTRY_DELAY));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030063 }
64}
65
66int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
67{
68 DECLARE_COMPLETION_ONSTACK(compl);
69 unsigned long flags;
70 int ret;
71 u32 start_time = jiffies;
72 bool pending = false;
73
Juuso Oikarinen71449f82009-12-11 15:41:07 +020074 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030075 return 0;
76
77 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
78
79 /*
80 * The spinlock is required here to synchronize both the work and
81 * the completion variable in one entity.
82 */
83 spin_lock_irqsave(&wl->wl_lock, flags);
84 if (work_pending(&wl->irq_work) || chip_awake)
85 pending = true;
86 else
87 wl->elp_compl = &compl;
88 spin_unlock_irqrestore(&wl->wl_lock, flags);
89
Juuso Oikarinen74621412009-10-12 15:08:54 +030090 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030091
92 if (!pending) {
93 ret = wait_for_completion_timeout(
94 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
95 if (ret == 0) {
96 wl1271_error("ELP wakeup timeout!");
97 ret = -ETIMEDOUT;
98 goto err;
99 } else if (ret < 0) {
100 wl1271_error("ELP wakeup completion error.");
101 goto err;
102 }
103 }
104
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200105 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300106
107 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
108 jiffies_to_msecs(jiffies - start_time));
109 goto out;
110
111err:
112 spin_lock_irqsave(&wl->wl_lock, flags);
113 wl->elp_compl = NULL;
114 spin_unlock_irqrestore(&wl->wl_lock, flags);
115 return ret;
116
117out:
118 return 0;
119}
120
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200121int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
122 bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300123{
124 int ret;
125
126 switch (mode) {
127 case STATION_POWER_SAVE_MODE:
128 wl1271_debug(DEBUG_PSM, "entering psm");
Juuso Oikarinen19221672009-10-08 21:56:35 +0300129
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200130 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE, send);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300131 if (ret < 0)
132 return ret;
133
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200134 set_bit(WL1271_FLAG_PSM, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300135 break;
136 case STATION_ACTIVE_MODE:
137 default:
138 wl1271_debug(DEBUG_PSM, "leaving psm");
139 ret = wl1271_ps_elp_wakeup(wl, false);
140 if (ret < 0)
141 return ret;
142
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300143 /* disable beacon early termination */
144 ret = wl1271_acx_bet_enable(wl, false);
145 if (ret < 0)
146 return ret;
147
Juuso Oikarinen19221672009-10-08 21:56:35 +0300148 /* disable beacon filtering */
149 ret = wl1271_acx_beacon_filter_opt(wl, false);
150 if (ret < 0)
151 return ret;
152
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200153 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE, send);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300154 if (ret < 0)
155 return ret;
156
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200157 clear_bit(WL1271_FLAG_PSM, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300158 break;
159 }
160
161 return ret;
162}
163
164