wl1251_ps.c 3.5 KB
Newer Older
K
Kalle Valo 已提交
1
/*
2
 * This file is part of wl1251
K
Kalle Valo 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (C) 2008 Nokia Corporation
 *
 * Contact: Kalle Valo <kalle.valo@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

24
#include "wl1251_reg.h"
25
#include "wl1251_ps.h"
26 27
#include "wl1251_cmd.h"
#include "wl1251_io.h"
K
Kalle Valo 已提交
28

29
#define WL1251_WAKEUP_TIMEOUT 2000
K
Kalle Valo 已提交
30 31

/* Routines to toggle sleep mode while in ELP */
32
void wl1251_ps_elp_sleep(struct wl1251 *wl)
K
Kalle Valo 已提交
33 34 35 36
{
	if (wl->elp || !wl->psm)
		return;

37
	wl1251_debug(DEBUG_PSM, "chip to elp");
K
Kalle Valo 已提交
38

39
	wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
K
Kalle Valo 已提交
40 41 42 43

	wl->elp = true;
}

44
int wl1251_ps_elp_wakeup(struct wl1251 *wl)
K
Kalle Valo 已提交
45 46 47 48 49 50 51
{
	unsigned long timeout;
	u32 elp_reg;

	if (!wl->elp)
		return 0;

52
	wl1251_debug(DEBUG_PSM, "waking up chip from elp");
K
Kalle Valo 已提交
53

54
	timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
K
Kalle Valo 已提交
55

56
	wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
K
Kalle Valo 已提交
57

58
	elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
K
Kalle Valo 已提交
59 60 61 62 63 64 65

	/*
	 * FIXME: we should wait for irq from chip but, as a temporary
	 * solution to simplify locking, let's poll instead
	 */
	while (!(elp_reg & ELPCTRL_WLAN_READY)) {
		if (time_after(jiffies, timeout)) {
66
			wl1251_error("elp wakeup timeout");
K
Kalle Valo 已提交
67 68 69
			return -ETIMEDOUT;
		}
		msleep(1);
70
		elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
K
Kalle Valo 已提交
71 72
	}

73
	wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
K
Kalle Valo 已提交
74
		     jiffies_to_msecs(jiffies) -
75
		     (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
K
Kalle Valo 已提交
76 77 78 79 80 81

	wl->elp = false;

	return 0;
}

82
static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
K
Kalle Valo 已提交
83 84 85 86
{
	int ret;

	if (enable) {
87
		wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
K
Kalle Valo 已提交
88

89
		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
K
Kalle Valo 已提交
90 91 92
		if (ret < 0)
			return ret;

93
		wl1251_ps_elp_sleep(wl);
K
Kalle Valo 已提交
94
	} else {
95
		wl1251_debug(DEBUG_PSM, "sleep auth cam");
K
Kalle Valo 已提交
96 97 98 99 100 101 102 103

		/*
		 * When the target is in ELP, we can only
		 * access the ELP control register. Thus,
		 * we have to wake the target up before
		 * changing the power authorization.
		 */

104
		wl1251_ps_elp_wakeup(wl);
K
Kalle Valo 已提交
105

106
		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
K
Kalle Valo 已提交
107 108 109 110 111 112 113
		if (ret < 0)
			return ret;
	}

	return 0;
}

114
int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
K
Kalle Valo 已提交
115 116 117 118 119
{
	int ret;

	switch (mode) {
	case STATION_POWER_SAVE_MODE:
120
		wl1251_debug(DEBUG_PSM, "entering psm");
121 122 123 124 125 126 127

		ret = wl1251_acx_wake_up_conditions(wl,
						    WAKE_UP_EVENT_DTIM_BITMAP,
						    wl->listen_int);
		if (ret < 0)
			return ret;

128
		ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
K
Kalle Valo 已提交
129 130 131
		if (ret < 0)
			return ret;

132
		ret = wl1251_ps_set_elp(wl, true);
K
Kalle Valo 已提交
133 134 135 136 137 138 139
		if (ret < 0)
			return ret;

		wl->psm = 1;
		break;
	case STATION_ACTIVE_MODE:
	default:
140 141
		wl1251_debug(DEBUG_PSM, "leaving psm");
		ret = wl1251_ps_set_elp(wl, false);
K
Kalle Valo 已提交
142 143 144
		if (ret < 0)
			return ret;

145 146 147 148 149 150
		ret = wl1251_acx_wake_up_conditions(wl,
						    WAKE_UP_EVENT_DTIM_BITMAP,
						    wl->listen_int);
		if (ret < 0)
			return ret;

151
		ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
K
Kalle Valo 已提交
152 153 154 155 156 157 158 159 160 161
		if (ret < 0)
			return ret;

		wl->psm = 0;
		break;
	}

	return ret;
}