[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wireless / wl12xx / wl1271_ps.c
blob1dc74b0c77364430d6d3df29878cee69cbffadfe
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@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 "wl1271_reg.h"
25 #include "wl1271_ps.h"
26 #include "wl1271_spi.h"
28 #define WL1271_WAKEUP_TIMEOUT 500
30 /* Routines to toggle sleep mode while in ELP */
31 void wl1271_ps_elp_sleep(struct wl1271 *wl)
34 * FIXME: due to a problem in the firmware (causing a firmware
35 * crash), ELP entry is prevented below. Remove the "true" to
36 * re-enable ELP entry.
38 if (true || wl->elp || !wl->psm)
39 return;
42 * Go to ELP unless there is work already pending - pending work
43 * will immediately wakeup the chipset anyway.
45 if (!work_pending(&wl->irq_work) && !work_pending(&wl->tx_work)) {
46 wl1271_debug(DEBUG_PSM, "chip to elp");
47 wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
48 wl->elp = true;
52 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
54 DECLARE_COMPLETION_ONSTACK(compl);
55 unsigned long flags;
56 int ret;
57 u32 start_time = jiffies;
58 bool pending = false;
60 if (!wl->elp)
61 return 0;
63 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
66 * The spinlock is required here to synchronize both the work and
67 * the completion variable in one entity.
69 spin_lock_irqsave(&wl->wl_lock, flags);
70 if (work_pending(&wl->irq_work) || chip_awake)
71 pending = true;
72 else
73 wl->elp_compl = &compl;
74 spin_unlock_irqrestore(&wl->wl_lock, flags);
76 wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
78 if (!pending) {
79 ret = wait_for_completion_timeout(
80 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
81 if (ret == 0) {
82 wl1271_error("ELP wakeup timeout!");
83 ret = -ETIMEDOUT;
84 goto err;
85 } else if (ret < 0) {
86 wl1271_error("ELP wakeup completion error.");
87 goto err;
91 wl->elp = false;
93 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
94 jiffies_to_msecs(jiffies - start_time));
95 goto out;
97 err:
98 spin_lock_irqsave(&wl->wl_lock, flags);
99 wl->elp_compl = NULL;
100 spin_unlock_irqrestore(&wl->wl_lock, flags);
101 return ret;
103 out:
104 return 0;
107 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
109 int ret;
111 switch (mode) {
112 case STATION_POWER_SAVE_MODE:
113 wl1271_debug(DEBUG_PSM, "entering psm");
114 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
115 if (ret < 0)
116 return ret;
118 wl1271_ps_elp_sleep(wl);
119 if (ret < 0)
120 return ret;
122 wl->psm = 1;
123 break;
124 case STATION_ACTIVE_MODE:
125 default:
126 wl1271_debug(DEBUG_PSM, "leaving psm");
127 ret = wl1271_ps_elp_wakeup(wl, false);
128 if (ret < 0)
129 return ret;
131 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
132 if (ret < 0)
133 return ret;
135 wl->psm = 0;
136 break;
139 return ret;