blob: 83baaa2eb19d726845f8667564e5aad1e77d1e60 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
2 * This file is part of wl12xx
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"
Kalle Valoef2f8d42009-06-12 14:17:19 +030025#include "wl1251_ps.h"
26#include "wl1251_spi.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030027
28#define WL12XX_WAKEUP_TIMEOUT 2000
29
30/* Routines to toggle sleep mode while in ELP */
31void wl12xx_ps_elp_sleep(struct wl12xx *wl)
32{
33 if (wl->elp || !wl->psm)
34 return;
35
36 wl12xx_debug(DEBUG_PSM, "chip to elp");
37
38 wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
39
40 wl->elp = true;
41}
42
43int wl12xx_ps_elp_wakeup(struct wl12xx *wl)
44{
45 unsigned long timeout;
46 u32 elp_reg;
47
48 if (!wl->elp)
49 return 0;
50
51 wl12xx_debug(DEBUG_PSM, "waking up chip from elp");
52
53 timeout = jiffies + msecs_to_jiffies(WL12XX_WAKEUP_TIMEOUT);
54
55 wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
56
57 elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
58
59 /*
60 * FIXME: we should wait for irq from chip but, as a temporary
61 * solution to simplify locking, let's poll instead
62 */
63 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
64 if (time_after(jiffies, timeout)) {
65 wl12xx_error("elp wakeup timeout");
66 return -ETIMEDOUT;
67 }
68 msleep(1);
69 elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
70 }
71
72 wl12xx_debug(DEBUG_PSM, "wakeup time: %u ms",
73 jiffies_to_msecs(jiffies) -
74 (jiffies_to_msecs(timeout) - WL12XX_WAKEUP_TIMEOUT));
75
76 wl->elp = false;
77
78 return 0;
79}
80
81static int wl12xx_ps_set_elp(struct wl12xx *wl, bool enable)
82{
83 int ret;
84
85 if (enable) {
86 wl12xx_debug(DEBUG_PSM, "sleep auth psm/elp");
87
Kalle Valo0182f8d2009-06-12 14:16:39 +030088 ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089 if (ret < 0)
90 return ret;
91
92 wl12xx_ps_elp_sleep(wl);
93 } else {
94 wl12xx_debug(DEBUG_PSM, "sleep auth cam");
95
96 /*
97 * When the target is in ELP, we can only
98 * access the ELP control register. Thus,
99 * we have to wake the target up before
100 * changing the power authorization.
101 */
102
103 wl12xx_ps_elp_wakeup(wl);
104
105 ret = wl12xx_acx_sleep_auth(wl, WL12XX_PSM_CAM);
106 if (ret < 0)
107 return ret;
108 }
109
110 return 0;
111}
112
Kalle Valoff258392009-06-12 14:14:19 +0300113int wl12xx_ps_set_mode(struct wl12xx *wl, enum wl12xx_cmd_ps_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114{
115 int ret;
116
117 switch (mode) {
118 case STATION_POWER_SAVE_MODE:
119 wl12xx_debug(DEBUG_PSM, "entering psm");
120 ret = wl12xx_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
121 if (ret < 0)
122 return ret;
123
124 ret = wl12xx_ps_set_elp(wl, true);
125 if (ret < 0)
126 return ret;
127
128 wl->psm = 1;
129 break;
130 case STATION_ACTIVE_MODE:
131 default:
132 wl12xx_debug(DEBUG_PSM, "leaving psm");
133 ret = wl12xx_ps_set_elp(wl, false);
134 if (ret < 0)
135 return ret;
136
137 ret = wl12xx_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
138 if (ret < 0)
139 return ret;
140
141 wl->psm = 0;
142 break;
143 }
144
145 return ret;
146}
147