blob: 77cb59712235035c689034575bfd58794b933245 [file] [log] [blame]
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070033#include <linux/init.h>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070034#include <net/mac80211.h>
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -070035#include "iwl-io.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070036#include "iwl-debug.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070037#include "iwl-trans.h"
Johannes Berg65de7e82012-04-17 07:36:30 -070038#include "iwl-modparams.h"
Johannes Berg1023fdc2012-05-15 12:16:34 +020039#include "dev.h"
40#include "agn.h"
41#include "commands.h"
42#include "power.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070043
44/*
Johannes Berge312c242009-08-07 15:41:51 -070045 * Setting power level allows the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070046 *
Johannes Berge312c242009-08-07 15:41:51 -070047 * We calculate a sleep command based on the required latency, which
48 * we get from mac80211. In order to handle thermal throttling, we can
49 * also use pre-defined power levels.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070050 */
51
Johannes Berge312c242009-08-07 15:41:51 -070052/*
Johannes Berge312c242009-08-07 15:41:51 -070053 * This defines the old power levels. They are still used by default
54 * (level 1) and for thermal throttle (levels 3 through 5)
55 */
56
57struct iwl_power_vec_entry {
58 struct iwl_powertable_cmd cmd;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070059 u8 no_dtim; /* number of skip dtim */
Johannes Berge312c242009-08-07 15:41:51 -070060};
61
62#define IWL_DTIM_RANGE_0_MAX 2
63#define IWL_DTIM_RANGE_1_MAX 10
Mohamed Abbas5da4b552008-04-21 15:41:51 -070064
Johannes Berg7af2c462009-05-08 13:44:38 -070065#define NOSLP cpu_to_le16(0), 0, 0
66#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Wey-Yi Guy35162ba2010-11-23 10:58:56 -080067#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
68 IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
69 IWL_POWER_ADVANCE_PM_ENA_MSK)
70#define ASLP_TOUT(T) cpu_to_le32(T)
Johannes Berg7af2c462009-05-08 13:44:38 -070071#define TU_TO_USEC 1024
72#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
73#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
74 cpu_to_le32(X1), \
75 cpu_to_le32(X2), \
76 cpu_to_le32(X3), \
77 cpu_to_le32(X4)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070078/* default power management (not Tx power) table values */
Johannes Berge312c242009-08-07 15:41:51 -070079/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070080/* DTIM 0 - 2 */
Johannes Berg7af2c462009-05-08 13:44:38 -070081static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070082 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
Mohamed Abbas5da4b552008-04-21 15:41:51 -070083 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
84 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
85 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
86 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
87};
88
89
Johannes Berge312c242009-08-07 15:41:51 -070090/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070091/* DTIM 3 - 10 */
Johannes Berg7af2c462009-05-08 13:44:38 -070092static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070093 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
94 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
95 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
96 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070097 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070098};
99
Johannes Berge312c242009-08-07 15:41:51 -0700100/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700101/* DTIM 11 - */
Johannes Berg7af2c462009-05-08 13:44:38 -0700102static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700103 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
104 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
105 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
106 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
107 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
108};
109
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800110/* advance power management */
111/* DTIM 0 - 2 */
112static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
113 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
114 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
115 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
116 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
117 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
118 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
119 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
120 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
121 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
122 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
123};
124
125
126/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
127/* DTIM 3 - 10 */
128static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
129 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
130 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
131 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
132 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
133 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
134 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
135 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
136 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
137 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
138 SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
139};
140
141/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
142/* DTIM 11 - */
143static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
144 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
145 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
146 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
147 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
148 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
149 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
150 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
151 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
152 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
153 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
154};
155
Johannes Berge312c242009-08-07 15:41:51 -0700156static void iwl_static_sleep_cmd(struct iwl_priv *priv,
157 struct iwl_powertable_cmd *cmd,
158 enum iwl_power_level lvl, int period)
159{
160 const struct iwl_power_vec_entry *table;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700161 int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
162 int i;
163 u8 skip;
164 u32 slp_itrvl;
Johannes Berge312c242009-08-07 15:41:51 -0700165
Johannes Berg0d8877a2013-05-17 10:36:29 +0200166 if (priv->lib->adv_pm) {
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800167 table = apm_range_2;
168 if (period <= IWL_DTIM_RANGE_1_MAX)
169 table = apm_range_1;
170 if (period <= IWL_DTIM_RANGE_0_MAX)
171 table = apm_range_0;
172 } else {
173 table = range_2;
174 if (period <= IWL_DTIM_RANGE_1_MAX)
175 table = range_1;
176 if (period <= IWL_DTIM_RANGE_0_MAX)
177 table = range_0;
178 }
Johannes Berge312c242009-08-07 15:41:51 -0700179
Johannes Berg3e41ace2011-04-18 09:12:37 -0700180 if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
181 memset(cmd, 0, sizeof(*cmd));
182 else
183 *cmd = table[lvl].cmd;
Johannes Berge312c242009-08-07 15:41:51 -0700184
185 if (period == 0) {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700186 skip = 0;
Johannes Berge312c242009-08-07 15:41:51 -0700187 period = 1;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700188 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
189 max_sleep[i] = 1;
190
Johannes Berge312c242009-08-07 15:41:51 -0700191 } else {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700192 skip = table[lvl].no_dtim;
193 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
194 max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
195 max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
Johannes Berge312c242009-08-07 15:41:51 -0700196 }
197
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700198 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
199 /* figure out the listen interval based on dtim period and skip */
200 if (slp_itrvl == 0xFF)
201 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
202 cpu_to_le32(period * (skip + 1));
203
204 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
205 if (slp_itrvl > period)
206 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
207 cpu_to_le32((slp_itrvl / period) * period);
208
209 if (skip)
Johannes Berge312c242009-08-07 15:41:51 -0700210 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700211 else
Johannes Berge312c242009-08-07 15:41:51 -0700212 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Johannes Berge312c242009-08-07 15:41:51 -0700213
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200214 if (priv->cfg->base_params->shadow_reg_enable)
Wey-Yi Guy1f37daf2010-11-23 10:58:55 -0800215 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
216 else
217 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
218
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100219 if (iwl_advanced_bt_coexist(priv)) {
Johannes Berg0d8877a2013-05-17 10:36:29 +0200220 if (!priv->lib->bt_params->bt_sco_disable)
Wey-Yi Guye3661762010-11-23 10:58:54 -0800221 cmd->flags |= IWL_POWER_BT_SCO_ENA;
222 else
223 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
224 }
225
226
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700227 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
Wey-Yi Guy570af862009-10-23 13:42:33 -0700228 if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700229 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
Wey-Yi Guy570af862009-10-23 13:42:33 -0700230 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700231
232 /* enforce max sleep interval */
233 for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
234 if (le32_to_cpu(cmd->sleep_interval[i]) >
235 (max_sleep[i] * period))
236 cmd->sleep_interval[i] =
237 cpu_to_le32(max_sleep[i] * period);
238 if (i != (IWL_POWER_VEC_SIZE - 1)) {
239 if (le32_to_cpu(cmd->sleep_interval[i]) >
240 le32_to_cpu(cmd->sleep_interval[i+1]))
241 cmd->sleep_interval[i] =
242 cmd->sleep_interval[i+1];
243 }
244 }
Johannes Berge312c242009-08-07 15:41:51 -0700245
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700246 if (priv->power_data.bus_pm)
Johannes Berge312c242009-08-07 15:41:51 -0700247 cmd->flags |= IWL_POWER_PCI_PM_MSK;
248 else
249 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
250
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700251 IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
252 skip, period);
Amit Beka11e11652012-05-03 14:22:04 -0700253 /* The power level here is 0-4 (used as array index), but user expects
254 to see 1-5 (according to spec). */
Johannes Berge312c242009-08-07 15:41:51 -0700255 IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
256}
257
Johannes Berge312c242009-08-07 15:41:51 -0700258static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
259 struct iwl_powertable_cmd *cmd)
Mohamed Abbasca579612008-07-18 13:52:57 +0800260{
Johannes Berge312c242009-08-07 15:41:51 -0700261 memset(cmd, 0, sizeof(*cmd));
262
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700263 if (priv->power_data.bus_pm)
Johannes Berge312c242009-08-07 15:41:51 -0700264 cmd->flags |= IWL_POWER_PCI_PM_MSK;
265
266 IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
Mohamed Abbasca579612008-07-18 13:52:57 +0800267}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700268
Johannes Berge312c242009-08-07 15:41:51 -0700269static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
270{
271 IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800272 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
273 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
274 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
275 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700276 le32_to_cpu(cmd->sleep_interval[0]),
277 le32_to_cpu(cmd->sleep_interval[1]),
278 le32_to_cpu(cmd->sleep_interval[2]),
279 le32_to_cpu(cmd->sleep_interval[3]),
280 le32_to_cpu(cmd->sleep_interval[4]));
281
Johannes Berge10a0532012-03-06 13:30:39 -0800282 return iwl_dvm_send_cmd_pdu(priv, POWER_TABLE_CMD, CMD_SYNC,
Johannes Berge312c242009-08-07 15:41:51 -0700283 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700284}
285
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200286static void iwl_power_build_cmd(struct iwl_priv *priv,
287 struct iwl_powertable_cmd *cmd)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700288{
Johannes Berg4d695922010-01-21 05:24:04 -0800289 bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
Johannes Berge312c242009-08-07 15:41:51 -0700290 int dtimper;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700291
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200292 dtimper = priv->hw->conf.ps_dtim_period ?: 1;
293
Johannes Berg15b86bf2012-03-05 11:24:36 -0800294 if (priv->wowlan)
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700295 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, dtimper);
Johannes Berg0d8877a2013-05-17 10:36:29 +0200296 else if (!priv->lib->no_idle_support &&
Wey-Yi Guyf3529102011-08-02 09:08:37 -0700297 priv->hw->conf.flags & IEEE80211_CONF_IDLE)
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200298 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
Wey-Yi Guyf42e7662011-04-18 09:30:09 -0700299 else if (iwl_tt_is_low_power_state(priv)) {
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200300 /* in thermal throttling low power state */
301 iwl_static_sleep_cmd(priv, cmd,
Wey-Yi Guyf42e7662011-04-18 09:30:09 -0700302 iwl_tt_current_power_mode(priv), dtimper);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200303 } else if (!enabled)
304 iwl_power_sleep_cam_cmd(priv, cmd);
305 else if (priv->power_data.debug_sleep_level_override >= 0)
306 iwl_static_sleep_cmd(priv, cmd,
307 priv->power_data.debug_sleep_level_override,
308 dtimper);
Johannes Berg84965792012-03-09 08:58:46 +0100309 else {
Amit Beka11e11652012-05-03 14:22:04 -0700310 /* Note that the user parameter is 1-5 (according to spec),
311 but we pass 0-4 because it acts as an array index. */
Johannes Berg65de7e82012-04-17 07:36:30 -0700312 if (iwlwifi_mod_params.power_level > IWL_POWER_INDEX_1 &&
Amit Beka11e11652012-05-03 14:22:04 -0700313 iwlwifi_mod_params.power_level <= IWL_POWER_NUM)
Wey-Yi Guyf7538162011-06-11 10:00:06 -0700314 iwl_static_sleep_cmd(priv, cmd,
Amit Beka11e11652012-05-03 14:22:04 -0700315 iwlwifi_mod_params.power_level - 1, dtimper);
Wey-Yi Guyf7538162011-06-11 10:00:06 -0700316 else
317 iwl_static_sleep_cmd(priv, cmd,
318 IWL_POWER_INDEX_1, dtimper);
Johannes Berg84965792012-03-09 08:58:46 +0100319 }
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200320}
321
322int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
323 bool force)
324{
325 int ret;
326 bool update_chains;
327
Johannes Bergb1eea292012-03-06 13:30:42 -0800328 lockdep_assert_held(&priv->mutex);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200329
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800330 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800331 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
332 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800333
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200334 if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
335 return 0;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700336
Don Fry83626402012-03-07 09:52:37 -0800337 if (!iwl_is_ready_rf(priv))
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200338 return -EIO;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700339
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200340 /* scan complete use sleep_power_next, need to be updated */
341 memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
Don Fry83626402012-03-07 09:52:37 -0800342 if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200343 IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
344 return 0;
345 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700346
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200347 if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
Don Fry47107e82012-03-15 13:27:06 -0700348 iwl_dvm_set_pmi(priv, true);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700349
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200350 ret = iwl_set_power(priv, cmd);
351 if (!ret) {
352 if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
Don Fry47107e82012-03-15 13:27:06 -0700353 iwl_dvm_set_pmi(priv, false);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200354
Fry, Donald H6b6db912011-07-08 08:46:17 -0700355 if (update_chains)
356 iwl_update_chain_flags(priv);
357 else
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200358 IWL_DEBUG_POWER(priv,
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700359 "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800360 "calibration running: %d\n",
361 priv->chain_noise_data.state);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200362
363 memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
364 } else
365 IWL_ERR(priv, "set power fail, ret = %d", ret);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700366
367 return ret;
368}
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200369
370int iwl_power_update_mode(struct iwl_priv *priv, bool force)
371{
372 struct iwl_powertable_cmd cmd;
373
374 iwl_power_build_cmd(priv, &cmd);
375 return iwl_power_set_mode(priv, &cmd, force);
376}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700377
Tomas Winklera96a27f2008-10-23 23:48:56 -0700378/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700379void iwl_power_initialize(struct iwl_priv *priv)
380{
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700381 priv->power_data.bus_pm = priv->trans->pm_support;
Johannes Berge312c242009-08-07 15:41:51 -0700382
383 priv->power_data.debug_sleep_level_override = -1;
384
385 memset(&priv->power_data.sleep_cmd, 0,
386 sizeof(priv->power_data.sleep_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700387}