15da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/******************************************************************************
25da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
34e3182626a914443a5e0fbe014813f03e51a75dfWey-Yi Guy * Copyright(c) 2007 - 2012 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"
4648f20d354e729afcfb29ff41aca7583ebb94613dEmmanuel Grumbach#include "iwl-shared.h"
475da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
485da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/*
49e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * Setting power level allows the card to go to sleep when not busy.
505da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas *
51e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * We calculate a sleep command based on the required latency, which
52e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * we get from mac80211. In order to handle thermal throttling, we can
53e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * also use pre-defined power levels.
545da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas */
555da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
56e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/*
57e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * This defines the old power levels. They are still used by default
58e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg * (level 1) and for thermal throttle (levels 3 through 5)
59e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg */
60e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
61e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstruct iwl_power_vec_entry {
62e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	struct iwl_powertable_cmd cmd;
634ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u8 no_dtim;	/* number of skip dtim */
64e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg};
65e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
66e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg#define IWL_DTIM_RANGE_0_MAX	2
67e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg#define IWL_DTIM_RANGE_1_MAX	10
685da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
697af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define NOSLP cpu_to_le16(0), 0, 0
707af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
7135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK |	\
7235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
7335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		IWL_POWER_ADVANCE_PM_ENA_MSK)
7435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy#define ASLP_TOUT(T) cpu_to_le32(T)
757af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define TU_TO_USEC 1024
767af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
777af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
787af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X1), \
797af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X2), \
807af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X3), \
817af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Berg				     cpu_to_le32(X4)}
825da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas/* default power management (not Tx power) table values */
83e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
844ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 0 - 2 */
857af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
864ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
875da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
885da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
895da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
905da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
915da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
925da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
935da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
94e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
954ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 3 - 10 */
967af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
975da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
985da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
995da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
1005da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
1014ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
1025da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
1035da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
104e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
1054ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy/* DTIM 11 - */
1067af2c460789a78d9c0d4dc7776fcb87acdc71052Johannes Bergstatic const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
1075da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1085da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1095da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1105da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1115da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1125da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas};
1135da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
11435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* advance power management */
11535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 0 - 2 */
11635162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
11735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
11835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
11935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
12535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
12635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
12735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
12835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
12935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
13035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
13135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 3 - 10 */
13235162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
13335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
13835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
13935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
14035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
14135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
14235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
14335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
14435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
14535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
14635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy/* DTIM 11 - */
14735162ba75900209755628ccf7357763797037ba6Wey-Yi Guystatic const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
14835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
14935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
15635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	{{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
15735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
15835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy};
15935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy
160e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_static_sleep_cmd(struct iwl_priv *priv,
161e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				 struct iwl_powertable_cmd *cmd,
162e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				 enum iwl_power_level lvl, int period)
163e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg{
164e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	const struct iwl_power_vec_entry *table;
1654ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
1664ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	int i;
1674ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u8 skip;
1684ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	u32 slp_itrvl;
169e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
1703862241945026a8fa165ab73c57739df77b8e1fbDon Fry	if (cfg(priv)->adv_pm) {
17135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		table = apm_range_2;
17235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_1_MAX)
17335162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = apm_range_1;
17435162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_0_MAX)
17535162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = apm_range_0;
17635162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	} else {
17735162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		table = range_2;
17835162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_1_MAX)
17935162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = range_1;
18035162ba75900209755628ccf7357763797037ba6Wey-Yi Guy		if (period <= IWL_DTIM_RANGE_0_MAX)
18135162ba75900209755628ccf7357763797037ba6Wey-Yi Guy			table = range_0;
18235162ba75900209755628ccf7357763797037ba6Wey-Yi Guy	}
183e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
1843e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg	if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
1853e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg		memset(cmd, 0, sizeof(*cmd));
1863e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg	else
1873e41ace5deef7af16dd277d9d17f9d36dca0a10eJohannes Berg		*cmd = table[lvl].cmd;
188e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
189e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	if (period == 0) {
1904ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		skip = 0;
191e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		period = 1;
1924ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
1934ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			max_sleep[i] =  1;
1944ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
195e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	} else {
1964ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		skip = table[lvl].no_dtim;
1974ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
1984ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
1994ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
200e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	}
201e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2024ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
2034ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	/* figure out the listen interval based on dtim period and skip */
2044ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (slp_itrvl == 0xFF)
2054ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
2064ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cpu_to_le32(period * (skip + 1));
2074ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2084ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
2094ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (slp_itrvl > period)
2104ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
2114ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cpu_to_le32((slp_itrvl / period) * period);
2124ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2134ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	if (skip)
214e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2154ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	else
216e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
217e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2180dde86b2f2999a25dcc62a27cb768c35a418890aJohannes Berg	if (cfg(priv)->base_params->shadow_reg_enable)
2191f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
2201f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	else
2211f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
2221f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy
22388e58fc5d940c3463c7070a2a7a8a0ce65af3fdcStanislaw Gruszka	if (iwl_advanced_bt_coexist(priv)) {
2243862241945026a8fa165ab73c57739df77b8e1fbDon Fry		if (!cfg(priv)->bt_params->bt_sco_disable)
225e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags |= IWL_POWER_BT_SCO_ENA;
226e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		else
227e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
228e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy	}
229e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
230e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
2314ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
232570af86e0ab7e7b42b2562ff3209c16ab1ebd5f8Wey-Yi Guy	if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
2334ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
234570af86e0ab7e7b42b2562ff3209c16ab1ebd5f8Wey-Yi Guy			cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
2354ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy
2364ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	/* enforce max sleep interval */
2374ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
2384ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		if (le32_to_cpu(cmd->sleep_interval[i]) >
2394ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		    (max_sleep[i] * period))
2404ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			cmd->sleep_interval[i] =
2414ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy				cpu_to_le32(max_sleep[i] * period);
2424ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		if (i != (IWL_POWER_VEC_SIZE - 1)) {
2434ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			if (le32_to_cpu(cmd->sleep_interval[i]) >
2444ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			    le32_to_cpu(cmd->sleep_interval[i+1]))
2454ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy				cmd->sleep_interval[i] =
2464ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy					cmd->sleep_interval[i+1];
2474ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy		}
2484ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	}
249e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
250d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
251e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
252e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	else
253e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
254e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
2554ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy	IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
2564ad177b5c860dc0b1083eccc55957daf4a116b90Wey-Yi Guy			skip, period);
257e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
258e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg}
259e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
260e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
261e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				    struct iwl_powertable_cmd *cmd)
2625da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
263e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(cmd, 0, sizeof(*cmd));
2645da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
265d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
266e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
2675da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
268e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
2695da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
2705da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
271e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
272e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     struct iwl_powertable_cmd *cmd,
273e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     int dynps_ms, int wakeup_period)
2745da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
2754c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	/*
2764c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * These are the original power level 3 sleep successions. The
2774c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * device may behave better with such succession and was also
2784c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * only tested with that. Just like the original sleep commands,
2794c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * also adjust the succession here to the wakeup_period below.
2804c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * The ranges are the same as for the sleep commands, 0-2, 3-9
2814c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * and >10, which is selected based on the DTIM interval for
2824c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * the sleep index but here we use the wakeup period since that
2834c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 * is what we need to do for the latency requirements.
2844c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	 */
2854c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
2864c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
2874c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
2884c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	const u8 *slp_succ = slp_succ_r0;
2895cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas	int i;
2905da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
2914c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
2924c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		slp_succ = slp_succ_r1;
2934c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg	if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
2944c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		slp_succ = slp_succ_r2;
2954c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg
296e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(cmd, 0, sizeof(*cmd));
2975cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas
298e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
299e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		     IWL_POWER_FAST_PD; /* no use seeing frames for others */
3005da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
301d57fa99d91bd6e8ca8217c115824b2732f4d3639Wey-Yi Guy	if (priv->power_data.bus_pm)
302e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		cmd->flags |= IWL_POWER_PCI_PM_MSK;
3035da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3040dde86b2f2999a25dcc62a27cb768c35a418890aJohannes Berg	if (cfg(priv)->base_params->shadow_reg_enable)
3051f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
3061f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy	else
3071f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy		cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
3081f37daf3233ccda5072f715d6c322d84833cdd92Wey-Yi Guy
30988e58fc5d940c3463c7070a2a7a8a0ce65af3fdcStanislaw Gruszka	if (iwl_advanced_bt_coexist(priv)) {
3103862241945026a8fa165ab73c57739df77b8e1fbDon Fry		if (!cfg(priv)->bt_params->bt_sco_disable)
311e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags |= IWL_POWER_BT_SCO_ENA;
312e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy		else
313e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
314e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy	}
315e366176e5c7f37d2d4cd0708e63b939e3fa3b5c6Wey-Yi Guy
316e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
317e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
3185da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3195cd19c5f15f4bd3354cc7f8f8b1125018a84a25cWinkler, Tomas	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
3204c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg		cmd->sleep_interval[i] =
3214c561a02291326c50f9b1f647eae891af9d1f3b2Johannes Berg			cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
322e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
323e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
324e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg}
3255da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
326e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Bergstatic int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
327e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg{
328e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
329e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
330e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
331e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
332e1623446bb1de1834ff1c57b3e8ed341d5d4a927Tomas Winkler	IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
3335da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[0]),
3345da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[1]),
3355da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[2]),
3365da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[3]),
3375da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas			le32_to_cpu(cmd->sleep_interval[4]));
3385da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
339e10a0533a9172471b52bd9512838d766420a3bafJohannes Berg	return iwl_dvm_send_cmd_pdu(priv, POWER_TABLE_CMD, CMD_SYNC,
340e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				sizeof(struct iwl_powertable_cmd), cmd);
3415da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
3425da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
343ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkastatic void iwl_power_build_cmd(struct iwl_priv *priv,
344ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka				struct iwl_powertable_cmd *cmd)
3455da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
3464d6959219bb71aa34383fc1a1a520820aee6292bJohannes Berg	bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
347e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	int dtimper;
3485da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
3493a065ab3b57bedefa1d59c88f731da6513ac482aJohannes Berg	dtimper = priv->hw->conf.ps_dtim_period ?: 1;
350e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
35115b86bff995525a38126eb44a951765a57ea2f4cJohannes Berg	if (priv->wowlan)
352c8ac61cf6e53fefb3b439fc58390fb65d2730e63Johannes Berg		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, dtimper);
3533862241945026a8fa165ab73c57739df77b8e1fbDon Fry	else if (!cfg(priv)->base_params->no_idle_support &&
354f35291082294ca6737953bbe4e9491ede04ab822Wey-Yi Guy		 priv->hw->conf.flags & IEEE80211_CONF_IDLE)
355ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
356f42e7662815647c1a6f73e160abcdf812d3057d2Wey-Yi Guy	else if (iwl_tt_is_low_power_state(priv)) {
3576ddbf8cd39dc1faee7ba60337b11eb02edfcbee6Wey-Yi Guy		/* in thermal throttling low power state */
358ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd,
359f42e7662815647c1a6f73e160abcdf812d3057d2Wey-Yi Guy		    iwl_tt_current_power_mode(priv), dtimper);
3600975cc8fbf5b61b188734f2edd9e588c7edff2a1Wey-Yi Guy	} else if (!enabled)
361ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_power_sleep_cam_cmd(priv, cmd);
362e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	else if (priv->power_data.debug_sleep_level_override >= 0)
363ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_static_sleep_cmd(priv, cmd,
364e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     priv->power_data.debug_sleep_level_override,
365e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg				     dtimper);
366f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy	else if (iwlagn_mod_params.no_sleep_autoadjust) {
367f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		if (iwlagn_mod_params.power_level > IWL_POWER_INDEX_1 &&
368f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		    iwlagn_mod_params.power_level <= IWL_POWER_INDEX_5)
369f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy			iwl_static_sleep_cmd(priv, cmd,
370f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy				iwlagn_mod_params.power_level, dtimper);
371f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy		else
372f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy			iwl_static_sleep_cmd(priv, cmd,
373f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy				IWL_POWER_INDEX_1, dtimper);
374f7538168915271083070a60ebb10def11fce0cb0Wey-Yi Guy	} else
375ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		iwl_power_fill_sleep_cmd(priv, cmd,
376e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg					 priv->hw->conf.dynamic_ps_timeout,
377e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg					 priv->hw->conf.max_sleep_period);
378ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka}
379ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
380ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkaint iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
381ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		       bool force)
382ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka{
383ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	int ret;
384ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	bool update_chains;
385ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
386b1eea297d6b522b801c95b60b1e64fb61228c6c7Johannes Berg	lockdep_assert_held(&priv->mutex);
387ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
388ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	/* Don't update the RX chain when chain noise calibration is running */
389ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
390ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka			priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
391ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
392ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
393ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return 0;
394ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
39583626404a70da74c67f32f119e53c0ba032ba2d8Don Fry	if (!iwl_is_ready_rf(priv))
396ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return -EIO;
397ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
398ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	/* scan complete use sleep_power_next, need to be updated */
399ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
40083626404a70da74c67f32f119e53c0ba032ba2d8Don Fry	if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
401ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
402ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		return 0;
403ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	}
404ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
405ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
40663013ae30159c90d2a873e20e680e7810fa533faEmmanuel Grumbach		set_bit(STATUS_POWER_PMI, &priv->shrd->status);
4075da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
408ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	ret = iwl_set_power(priv, cmd);
409ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	if (!ret) {
410ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
41163013ae30159c90d2a873e20e680e7810fa533faEmmanuel Grumbach			clear_bit(STATUS_POWER_PMI, &priv->shrd->status);
412ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
4136b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H		if (update_chains)
4146b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H			iwl_update_chain_flags(priv);
4156b6db91c8aa14fa9bd041321026d69b9f6ea42ffFry, Donald H		else
416ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka			IWL_DEBUG_POWER(priv,
4173a780d25428a0a391a8ba6c888cf4e89ac3fdbb1Wey-Yi Guy					"Cannot update the power, chain noise "
418a71c8f62d46e4496011182658dc058303960c068Winkler, Tomas					"calibration running: %d\n",
419a71c8f62d46e4496011182658dc058303960c068Winkler, Tomas					priv->chain_noise_data.state);
420ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
421ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
422ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	} else
423ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka		IWL_ERR(priv, "set power fail, ret = %d", ret);
4245da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
4255da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas	return ret;
4265da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
427ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
428ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszkaint iwl_power_update_mode(struct iwl_priv *priv, bool force)
429ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka{
430ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	struct iwl_powertable_cmd cmd;
431ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka
432ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	iwl_power_build_cmd(priv, &cmd);
433ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka	return iwl_power_set_mode(priv, &cmd, force);
434ac4f5457c7617999967e9740f8903b922714bab4Stanislaw Gruszka}
4355da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas
436a96a27f97f2bbfc1fca54bc3c0b0d41484152740Tomas Winkler/* initialize to default */
4375da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbasvoid iwl_power_initialize(struct iwl_priv *priv)
4385da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas{
439f6d0e9be6580ba607f730d7e056518c95a864601Emmanuel Grumbach	priv->power_data.bus_pm = trans(priv)->pm_support;
440e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
441e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	priv->power_data.debug_sleep_level_override = -1;
442e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg
443e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg	memset(&priv->power_data.sleep_cmd, 0,
444e312c24cf8229f9b6e76dbfd5d99eefe21f4ac0aJohannes Berg		sizeof(priv->power_data.sleep_cmd));
4455da4b55f78fb2ed40926b775d4f7c791594ecbd7Mohamed Abbas}
446