1 /*
2  * Copyright (c) 2010-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "htc.h"
18 
19 /******************/
20 /*     BTCOEX     */
21 /******************/
22 
23 /*
24  * Detects if there is any priority bt traffic
25  */
26 static void ath_detect_bt_priority(struct ath9k_htc_priv *priv)
27 {
28 	struct ath_btcoex *btcoex = &priv->btcoex;
29 	struct ath_hw *ah = priv->ah;
30 
31 	if (ath9k_hw_gpio_get(ah, ah->btcoex_hw.btpriority_gpio))
32 		btcoex->bt_priority_cnt++;
33 
34 	if (time_after(jiffies, btcoex->bt_priority_time +
35 			msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
36 		priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
37 		/* Detect if colocated bt started scanning */
38 		if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
39 			ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
40 				"BT scan detected\n");
41 			priv->op_flags |= (OP_BT_SCAN |
42 					 OP_BT_PRIORITY_DETECTED);
43 		} else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
44 			ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
45 				"BT priority traffic detected\n");
46 			priv->op_flags |= OP_BT_PRIORITY_DETECTED;
47 		}
48 
49 		btcoex->bt_priority_cnt = 0;
50 		btcoex->bt_priority_time = jiffies;
51 	}
52 }
53 
54 /*
55  * This is the master bt coex work which runs for every
56  * 45ms, bt traffic will be given priority during 55% of this
57  * period while wlan gets remaining 45%
58  */
59 static void ath_btcoex_period_work(struct work_struct *work)
60 {
61 	struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
62 						   coex_period_work.work);
63 	struct ath_btcoex *btcoex = &priv->btcoex;
64 	struct ath_common *common = ath9k_hw_common(priv->ah);
65 	u32 timer_period;
66 	bool is_btscan;
67 	int ret;
68 
69 	ath_detect_bt_priority(priv);
70 
71 	is_btscan = !!(priv->op_flags & OP_BT_SCAN);
72 
73 	ret = ath9k_htc_update_cap_target(priv,
74 				  !!(priv->op_flags & OP_BT_PRIORITY_DETECTED));
75 	if (ret) {
76 		ath_err(common, "Unable to set BTCOEX parameters\n");
77 		return;
78 	}
79 
80 	ath9k_hw_btcoex_bt_stomp(priv->ah, is_btscan ? ATH_BTCOEX_STOMP_ALL :
81 			btcoex->bt_stomp_type);
82 
83 	ath9k_hw_btcoex_enable(priv->ah);
84 	timer_period = is_btscan ? btcoex->btscan_no_stomp :
85 		btcoex->btcoex_no_stomp;
86 	ieee80211_queue_delayed_work(priv->hw, &priv->duty_cycle_work,
87 				     msecs_to_jiffies(timer_period));
88 	ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work,
89 				     msecs_to_jiffies(btcoex->btcoex_period));
90 }
91 
92 /*
93  * Work to time slice between wlan and bt traffic and
94  * configure weight registers
95  */
96 static void ath_btcoex_duty_cycle_work(struct work_struct *work)
97 {
98 	struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
99 						   duty_cycle_work.work);
100 	struct ath_hw *ah = priv->ah;
101 	struct ath_btcoex *btcoex = &priv->btcoex;
102 	struct ath_common *common = ath9k_hw_common(ah);
103 	bool is_btscan = priv->op_flags & OP_BT_SCAN;
104 
105 	ath_dbg(common, ATH_DBG_BTCOEX,
106 		"time slice work for bt and wlan\n");
107 
108 	if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
109 		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
110 	else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
111 		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
112 	ath9k_hw_btcoex_enable(priv->ah);
113 }
114 
115 void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
116 {
117 	struct ath_btcoex *btcoex = &priv->btcoex;
118 
119 	btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
120 	btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
121 		btcoex->btcoex_period / 100;
122 	btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
123 				   btcoex->btcoex_period / 100;
124 	INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
125 	INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
126 }
127 
128 /*
129  * (Re)start btcoex work
130  */
131 
132 void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
133 {
134 	struct ath_btcoex *btcoex = &priv->btcoex;
135 	struct ath_hw *ah = priv->ah;
136 
137 	ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX, "Starting btcoex work\n");
138 
139 	btcoex->bt_priority_cnt = 0;
140 	btcoex->bt_priority_time = jiffies;
141 	priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
142 	ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
143 }
144 
145 
146 /*
147  * Cancel btcoex and bt duty cycle work.
148  */
149 void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
150 {
151 	cancel_delayed_work_sync(&priv->coex_period_work);
152 	cancel_delayed_work_sync(&priv->duty_cycle_work);
153 }
154 
155 /*******/
156 /* LED */
157 /*******/
158 
159 #ifdef CONFIG_MAC80211_LEDS
160 void ath9k_led_work(struct work_struct *work)
161 {
162 	struct ath9k_htc_priv *priv = container_of(work,
163 						   struct ath9k_htc_priv,
164 						   led_work);
165 
166 	ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
167 			  (priv->brightness == LED_OFF));
168 }
169 
170 static void ath9k_led_brightness(struct led_classdev *led_cdev,
171 				 enum led_brightness brightness)
172 {
173 	struct ath9k_htc_priv *priv = container_of(led_cdev,
174 						   struct ath9k_htc_priv,
175 						   led_cdev);
176 
177 	/* Not locked, but it's just a tiny green light..*/
178 	priv->brightness = brightness;
179 	ieee80211_queue_work(priv->hw, &priv->led_work);
180 }
181 
182 void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
183 {
184 	if (!priv->led_registered)
185 		return;
186 
187 	ath9k_led_brightness(&priv->led_cdev, LED_OFF);
188 	led_classdev_unregister(&priv->led_cdev);
189 	cancel_work_sync(&priv->led_work);
190 }
191 
192 void ath9k_init_leds(struct ath9k_htc_priv *priv)
193 {
194 	int ret;
195 
196 	if (AR_SREV_9287(priv->ah))
197 		priv->ah->led_pin = ATH_LED_PIN_9287;
198 	else if (AR_SREV_9271(priv->ah))
199 		priv->ah->led_pin = ATH_LED_PIN_9271;
200 	else if (AR_DEVID_7010(priv->ah))
201 		priv->ah->led_pin = ATH_LED_PIN_7010;
202 	else
203 		priv->ah->led_pin = ATH_LED_PIN_DEF;
204 
205 	/* Configure gpio 1 for output */
206 	ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
207 			    AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
208 	/* LED off, active low */
209 	ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
210 
211 	snprintf(priv->led_name, sizeof(priv->led_name),
212 		"ath9k_htc-%s", wiphy_name(priv->hw->wiphy));
213 	priv->led_cdev.name = priv->led_name;
214 	priv->led_cdev.brightness_set = ath9k_led_brightness;
215 
216 	ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &priv->led_cdev);
217 	if (ret < 0)
218 		return;
219 
220 	INIT_WORK(&priv->led_work, ath9k_led_work);
221 	priv->led_registered = true;
222 
223 	return;
224 }
225 #endif
226 
227 /*******************/
228 /*	Rfkill	   */
229 /*******************/
230 
231 static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
232 {
233 	bool is_blocked;
234 
235 	ath9k_htc_ps_wakeup(priv);
236 	is_blocked = ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
237 						 priv->ah->rfkill_polarity;
238 	ath9k_htc_ps_restore(priv);
239 
240 	return is_blocked;
241 }
242 
243 void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
244 {
245 	struct ath9k_htc_priv *priv = hw->priv;
246 	bool blocked = !!ath_is_rfkill_set(priv);
247 
248 	wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
249 }
250 
251 void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
252 {
253 	if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
254 		wiphy_rfkill_start_polling(priv->hw->wiphy);
255 }
256 
257 void ath9k_htc_radio_enable(struct ieee80211_hw *hw)
258 {
259 	struct ath9k_htc_priv *priv = hw->priv;
260 	struct ath_hw *ah = priv->ah;
261 	struct ath_common *common = ath9k_hw_common(ah);
262 	int ret;
263 	u8 cmd_rsp;
264 
265 	if (!ah->curchan)
266 		ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
267 
268 	/* Reset the HW */
269 	ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
270 	if (ret) {
271 		ath_err(common,
272 			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
273 			ret, ah->curchan->channel);
274 	}
275 
276 	ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
277 			       &priv->curtxpow);
278 
279 	/* Start RX */
280 	WMI_CMD(WMI_START_RECV_CMDID);
281 	ath9k_host_rx_init(priv);
282 
283 	/* Start TX */
284 	htc_start(priv->htc);
285 	spin_lock_bh(&priv->tx.tx_lock);
286 	priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
287 	spin_unlock_bh(&priv->tx.tx_lock);
288 	ieee80211_wake_queues(hw);
289 
290 	WMI_CMD(WMI_ENABLE_INTR_CMDID);
291 
292 	/* Enable LED */
293 	ath9k_hw_cfg_output(ah, ah->led_pin,
294 			    AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
295 	ath9k_hw_set_gpio(ah, ah->led_pin, 0);
296 }
297 
298 void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
299 {
300 	struct ath9k_htc_priv *priv = hw->priv;
301 	struct ath_hw *ah = priv->ah;
302 	struct ath_common *common = ath9k_hw_common(ah);
303 	int ret;
304 	u8 cmd_rsp;
305 
306 	ath9k_htc_ps_wakeup(priv);
307 
308 	/* Disable LED */
309 	ath9k_hw_set_gpio(ah, ah->led_pin, 1);
310 	ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
311 
312 	WMI_CMD(WMI_DISABLE_INTR_CMDID);
313 
314 	/* Stop TX */
315 	ieee80211_stop_queues(hw);
316 	ath9k_htc_tx_drain(priv);
317 	WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
318 
319 	/* Stop RX */
320 	WMI_CMD(WMI_STOP_RECV_CMDID);
321 
322 	/* Clear the WMI event queue */
323 	ath9k_wmi_event_drain(priv);
324 
325 	/*
326 	 * The MIB counters have to be disabled here,
327 	 * since the target doesn't do it.
328 	 */
329 	ath9k_hw_disable_mib_counters(ah);
330 
331 	if (!ah->curchan)
332 		ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
333 
334 	/* Reset the HW */
335 	ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
336 	if (ret) {
337 		ath_err(common,
338 			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
339 			ret, ah->curchan->channel);
340 	}
341 
342 	/* Disable the PHY */
343 	ath9k_hw_phy_disable(ah);
344 
345 	ath9k_htc_ps_restore(priv);
346 	ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
347 }
348