wl1251_ps.c 3.2 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 24
 *
 * 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
 *
 */

#include "reg.h"
25 26
#include "wl1251_ps.h"
#include "wl1251_spi.h"
K
Kalle Valo 已提交
27

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

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

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

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

	wl->elp = true;
}

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

	if (!wl->elp)
		return 0;

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

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

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

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

	/*
	 * 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)) {
65
			wl1251_error("elp wakeup timeout");
K
Kalle Valo 已提交
66 67 68
			return -ETIMEDOUT;
		}
		msleep(1);
69
		elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
K
Kalle Valo 已提交
70 71
	}

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

	wl->elp = false;

	return 0;
}

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

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

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

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

		/*
		 * 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.
		 */

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

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

	return 0;
}

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

	switch (mode) {
	case STATION_POWER_SAVE_MODE:
119 120
		wl1251_debug(DEBUG_PSM, "entering psm");
		ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
K
Kalle Valo 已提交
121 122 123
		if (ret < 0)
			return ret;

124
		ret = wl1251_ps_set_elp(wl, true);
K
Kalle Valo 已提交
125 126 127 128 129 130 131
		if (ret < 0)
			return ret;

		wl->psm = 1;
		break;
	case STATION_ACTIVE_MODE:
	default:
132 133
		wl1251_debug(DEBUG_PSM, "leaving psm");
		ret = wl1251_ps_set_elp(wl, false);
K
Kalle Valo 已提交
134 135 136
		if (ret < 0)
			return ret;

137
		ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
K
Kalle Valo 已提交
138 139 140 141 142 143 144 145 146 147
		if (ret < 0)
			return ret;

		wl->psm = 0;
		break;
	}

	return ret;
}