iwl-power.c revision c8ac61cf6e53fefb3b439fc58390fb65d2730e63
15da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/******************************************************************************
25da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
3901069c71415a76d731857ccda814e18ded062f7Wey-Yi Guy * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
45da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
55da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * Portions of this file are derived from the ipw3945 project, as well
65da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * as portions of the ieee80211 subsystem header files.
75da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
85da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * This program is free software; you can redistribute it and/or modify it
95da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * under the terms of version 2 of the GNU General Public License as
105da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * published by the Free Software Foundation.
115da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
125da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * This program is distributed in the hope that it will be useful, but WITHOUT
135da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
145da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
155da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * more details.
165da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
175da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * You should have received a copy of the GNU General Public License along with
185da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * this program; if not, write to the Free Software Foundation, Inc.,
195da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
205da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
215da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * The full GNU General Public License is included in this distribution in the
225da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * file called LICENSE.
235da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
245da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * Contact Information:
25759ef89fb096c4a6ef078d3cfd5682ac037bd789Winkler, Tomas *  Intel Linux Wireless <ilw@linux.intel.com>
265da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
275da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *****************************************************************************/
285da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
295da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
305da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include <linux/kernel.h>
315da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include <linux/module.h>
325a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
335da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include <linux/init.h>
345da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
355da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include <net/mac80211.h>
365da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
375da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include "iwl-eeprom.h"
383e0d4cb12f6fd97193a455b49125398b2231c87cTomas Winkler#include "iwl-dev.h"
393f1e5f4a2b0993b6e81b5665b28568624f581b0fWey-Yi Guy#include "iwl-agn.h"
405da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include "iwl-core.h"
4139b73fb15e4704fd4d1e33688135810637f5f3fbWey-Yi Guy#include "iwl-io.h"
425a36ba0e412a0e12a8bf2648a075226c1dd7870dTomas Winkler#include "iwl-commands.h"
435da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include "iwl-debug.h"
445da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas#include "iwl-power.h"
45bdfbf0924ab05e02d28e50bd2d5024097642a78dEmmanuel Grumbach#include "iwl-trans.h"
465da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
475da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/*
48e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * Setting power level allows the card to go to sleep when not busy.
495da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
50e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * We calculate a sleep command based on the required latency, which
51e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * we get from mac80211. In order to handle thermal throttling, we can
52e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * also use pre-defined power levels.
535da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas */
545da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
55e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/*
56e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * This defines the old power levels. They are still used by default
57e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * (level 1) and for thermal throttle (levels 3 through 5)
58e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg */
59e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
60e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstruct iwl_power_vec_entry {
61e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	struct iwl_powertable_cmd cmd;
624ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u8 no_dtim;	/* number of skip dtim */
63e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg};
64e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
65e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg#define IWL_DTIM_RANGE_0_MAX	2
66e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg#define IWL_DTIM_RANGE_1_MAX	10
675da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
687af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define NOSLP cpu_to_le16(0), 0, 0
697af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
7035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK |	\
7135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
7235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		IWL_POWER_ADVANCE_PM_ENA_MSK)
7335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy#define ASLP_TOUT(T) cpu_to_le32(T)
747af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define TU_TO_USEC 1024
757af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
767af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
777af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X1), \
787af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X2), \
797af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X3), \
807af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X4)}
815da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/* default power management (not Tx power) table values */
82e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
834ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 0 - 2 */
847af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
854ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
865da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
875da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
885da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
895da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
905da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
915da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
925da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
93e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
944ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 3 - 10 */
957af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
965da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
975da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
985da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
995da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
1004ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
1015da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
1025da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
103e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
1044ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 11 - */
1057af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
1065da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1075da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1085da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1095da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1105da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1115da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
1125da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
11335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* advance power management */
11435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 0 - 2 */
11535162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
11635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
11735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
11835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
11935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
12635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
12735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
12835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
12935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
13035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 3 - 10 */
13135162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
13235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
14035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
14135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
14235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
14335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
14435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
14535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 11 - */
14635162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
14735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
14835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
14935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
15735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
15835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
159e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_static_sleep_cmd(struct iwl_priv *priv,
160e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				 struct iwl_powertable_cmd *cmd,
161e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				 enum iwl_power_level lvl, int period)
162e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg{
163e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	const struct iwl_power_vec_entry *table;
1644ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
1654ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	int i;
1664ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u8 skip;
1674ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u32 slp_itrvl;
168e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
16935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	if (priv->cfg->adv_pm) {
17035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		table = apm_range_2;
17135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_1_MAX)
17235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = apm_range_1;
17335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_0_MAX)
17435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = apm_range_0;
17535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	} else {
17635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		table = range_2;
17735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_1_MAX)
17835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = range_1;
17935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_0_MAX)
18035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = range_0;
18135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	}
182e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
1833e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg	if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
1843e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg		memset(cmd, 0, sizeof(*cmd));
1853e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg	else
1863e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg		*cmd = table[lvl].cmd;
187e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
188e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	if (period == 0) {
1894ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		skip = 0;
190e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		period = 1;
1914ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
1924ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			max_sleep[i] =  1;
1934ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
194e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	} else {
1954ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		skip = table[lvl].no_dtim;
1964ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
1974ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
1984ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
199e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	}
200e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2014ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
2024ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	/* figure out the listen interval based on dtim period and skip */
2034ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (slp_itrvl == 0xFF)
2044ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
2054ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cpu_to_le32(period * (skip + 1));
2064ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2074ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
2084ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (slp_itrvl > period)
2094ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
2104ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cpu_to_le32((slp_itrvl / period) * period);
2114ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2124ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (skip)
213e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2144ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	else
215e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
216e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2171f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	if (priv->cfg->base_params->shadow_reg_enable)
2181f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
2191f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	else
2201f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
2211f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy
22288e58fc5d940c3463c7070a2a7a8a0ce65af3fdcStanislaw Gruszka	if (iwl_advanced_bt_coexist(priv)) {
223e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		if (!priv->cfg->bt_params->bt_sco_disable)
224e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags |= IWL_POWER_BT_SCO_ENA;
225e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		else
226e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
227e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy	}
228e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
229e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
2304ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
231570af86e0ab7e7b42b2562ff3209c16ab1ebd5f8Wey-Yi Guy	if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
2324ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
233570af86e0ab7e7b42b2562ff3209c16ab1ebd5f8Wey-Yi Guy			cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
2344ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2354ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	/* enforce max sleep interval */
2364ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
2374ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		if (le32_to_cpu(cmd->sleep_interval[i]) >
2384ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		    (max_sleep[i] * period))
2394ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cmd->sleep_interval[i] =
2404ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy				cpu_to_le32(max_sleep[i] * period);
2414ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		if (i != (IWL_POWER_VEC_SIZE - 1)) {
2424ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			if (le32_to_cpu(cmd->sleep_interval[i]) >
2434ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			    le32_to_cpu(cmd->sleep_interval[i+1]))
2444ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy				cmd->sleep_interval[i] =
2454ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy					cmd->sleep_interval[i+1];
2464ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		}
2474ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	}
248e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
249d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
250e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
251e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	else
252e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
253e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2544ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
2554ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			skip, period);
256e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
257e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg}
258e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
259e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
260e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				    struct iwl_powertable_cmd *cmd)
2615da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
262e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(cmd, 0, sizeof(*cmd));
2635da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
264d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
265e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
2665da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
267e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
2685da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
2695da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
270e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
271e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     struct iwl_powertable_cmd *cmd,
272e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     int dynps_ms, int wakeup_period)
2735da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
2744c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	/*
2754c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * These are the original power level 3 sleep successions. The
2764c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * device may behave better with such succession and was also
2774c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * only tested with that. Just like the original sleep commands,
2784c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * also adjust the succession here to the wakeup_period below.
2794c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * The ranges are the same as for the sleep commands, 0-2, 3-9
2804c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * and >10, which is selected based on the DTIM interval for
2814c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * the sleep index but here we use the wakeup period since that
2824c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * is what we need to do for the latency requirements.
2834c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 */
2844c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
2854c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
2864c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
2874c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	const u8 *slp_succ = slp_succ_r0;
2885cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas	int i;
2895da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
2904c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
2914c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		slp_succ = slp_succ_r1;
2924c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
2934c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		slp_succ = slp_succ_r2;
2944c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg
295e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(cmd, 0, sizeof(*cmd));
2965cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas
297e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
298e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		     IWL_POWER_FAST_PD; /* no use seeing frames for others */
2995da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
300d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
301e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
3025da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3031f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	if (priv->cfg->base_params->shadow_reg_enable)
3041f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
3051f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	else
3061f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
3071f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy
30888e58fc5d940c3463c7070a2a7a8a0ce65af3fdcStanislaw Gruszka	if (iwl_advanced_bt_coexist(priv)) {
309e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		if (!priv->cfg->bt_params->bt_sco_disable)
310e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags |= IWL_POWER_BT_SCO_ENA;
311e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		else
312e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
313e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy	}
314e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
315e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
316e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
3175da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3185cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
3194c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		cmd->sleep_interval[i] =
3204c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg			cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
321e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
322e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
323e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg}
3245da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
325e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
326e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg{
327e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
328e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
329e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
330e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
331e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
3325da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[0]),
3335da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[1]),
3345da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[2]),
3355da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[3]),
3365da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[4]));
3375da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
33841c50542669cd7aec45ad708f5120ff8fdaa1194Emmanuel Grumbach	return trans_send_cmd_pdu(&priv->trans, POWER_TABLE_CMD, CMD_SYNC,
339e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				sizeof(struct iwl_powertable_cmd), cmd);
3405da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
3415da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
342ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkastatic void iwl_power_build_cmd(struct iwl_priv *priv,
343ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka				struct iwl_powertable_cmd *cmd)
3445da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
3454d6959219bb71aa34383fc1a1a520820aee6292bJohannes Berg	bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
346e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	int dtimper;
3475da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3483a065ab3b57bedefa1d59c88f731da6513ac482aJohannes Berg	dtimper = priv->hw->conf.ps_dtim_period ?: 1;
349e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
350c8ac61cf6e53fefb3b439fc58390fb65d2730e63Johannes Berg	if (priv->wowlan)
351c8ac61cf6e53fefb3b439fc58390fb65d2730e63Johannes Berg		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, dtimper);
352c8ac61cf6e53fefb3b439fc58390fb65d2730e63Johannes Berg	else if (priv->hw->conf.flags & IEEE80211_CONF_IDLE)
353ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
354f42e7662815647c1a6f73e160abcdf812d3057d2Wey-Yi Guy	else if (iwl_tt_is_low_power_state(priv)) {
3556ddbf8cd39dc1faee7ba60337b11eb02edfcbee6Wey-Yi Guy		/* in thermal throttling low power state */
356ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd,
357f42e7662815647c1a6f73e160abcdf812d3057d2Wey-Yi Guy		    iwl_tt_current_power_mode(priv), dtimper);
3580975cc8fbf5b61b188734f2edd9e588c7edff2a1Wey-Yi Guy	} else if (!enabled)
359ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_power_sleep_cam_cmd(priv, cmd);
360e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	else if (priv->power_data.debug_sleep_level_override >= 0)
361ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd,
362e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     priv->power_data.debug_sleep_level_override,
363e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     dtimper);
364f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy	else if (iwlagn_mod_params.no_sleep_autoadjust) {
365f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		if (iwlagn_mod_params.power_level > IWL_POWER_INDEX_1 &&
366f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		    iwlagn_mod_params.power_level <= IWL_POWER_INDEX_5)
367f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy			iwl_static_sleep_cmd(priv, cmd,
368f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy				iwlagn_mod_params.power_level, dtimper);
369f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		else
370f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy			iwl_static_sleep_cmd(priv, cmd,
371f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy				IWL_POWER_INDEX_1, dtimper);
372f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy	} else
373ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_power_fill_sleep_cmd(priv, cmd,
374e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg					 priv->hw->conf.dynamic_ps_timeout,
375e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg					 priv->hw->conf.max_sleep_period);
376ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka}
377ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
378ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkaint iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
379ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		       bool force)
380ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka{
381ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	int ret;
382ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	bool update_chains;
383ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
384ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	lockdep_assert_held(&priv->mutex);
385ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
386ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	/* Don't update the RX chain when chain noise calibration is running */
387ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
388ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka			priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
389ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
390ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
391ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return 0;
392ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
393ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (!iwl_is_ready_rf(priv))
394ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return -EIO;
395ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
396ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	/* scan complete use sleep_power_next, need to be updated */
397ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
398ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
399ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
400ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return 0;
401ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	}
402ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
403ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
404ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		set_bit(STATUS_POWER_PMI, &priv->status);
4055da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
406ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	ret = iwl_set_power(priv, cmd);
407ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (!ret) {
408ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
409ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka			clear_bit(STATUS_POWER_PMI, &priv->status);
410ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
4116b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H		if (update_chains)
4126b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H			iwl_update_chain_flags(priv);
4136b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H		else
414ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka			IWL_DEBUG_POWER(priv,
4153a780d25428a0a391a8ba6c888cf4e89ac3fdbb1Wey-Yi Guy					"Cannot update the power, chain noise "
416a71c8f62d46e4496011182658dc058303960c068Winkler, Tomas					"calibration running: %d\n",
417a71c8f62d46e4496011182658dc058303960c068Winkler, Tomas					priv->chain_noise_data.state);
418ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
419ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
420ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	} else
421ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		IWL_ERR(priv, "set power fail, ret = %d", ret);
4225da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
4235da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	return ret;
4245da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
425ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
426ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkaint iwl_power_update_mode(struct iwl_priv *priv, bool force)
427ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka{
428ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	struct iwl_powertable_cmd cmd;
429ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
430ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	iwl_power_build_cmd(priv, &cmd);
431ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	return iwl_power_set_mode(priv, &cmd, force);
432ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka}
4335da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
434a96a27f97f2bbfc1fca54bc3c0b0d41484152740Tomas Winkler/* initialize to default */
4355da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbasvoid iwl_power_initialize(struct iwl_priv *priv)
4365da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
437d593411084a56124aa9d80aafa15db8463b2d8f7Emmanuel Grumbach	priv->power_data.bus_pm = bus_get_pm_support(priv->bus);
438e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
439e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	priv->power_data.debug_sleep_level_override = -1;
440e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
441e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(&priv->power_data.sleep_cmd, 0,
442e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		sizeof(priv->power_data.sleep_cmd));
4435da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
444