Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / wireless / ath / ath9k / htc_drv_gpio.c
blob6506e1fd503649ad42246b84c49c618b5b818ad9
1 /*
2 * Copyright (c) 2010-2011 Atheros Communications Inc.
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.
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.
17 #include "htc.h"
19 /******************/
20 /* BTCOEX */
21 /******************/
24 * Detects if there is any priority bt traffic
26 static void ath_detect_bt_priority(struct ath9k_htc_priv *priv)
28 struct ath_btcoex *btcoex = &priv->btcoex;
29 struct ath_hw *ah = priv->ah;
31 if (ath9k_hw_gpio_get(ah, ah->btcoex_hw.btpriority_gpio))
32 btcoex->bt_priority_cnt++;
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), 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), BTCOEX,
45 "BT priority traffic detected\n");
46 priv->op_flags |= OP_BT_PRIORITY_DETECTED;
49 btcoex->bt_priority_cnt = 0;
50 btcoex->bt_priority_time = jiffies;
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%
59 static void ath_btcoex_period_work(struct work_struct *work)
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;
69 ath_detect_bt_priority(priv);
71 is_btscan = !!(priv->op_flags & OP_BT_SCAN);
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;
80 ath9k_hw_btcoex_bt_stomp(priv->ah, is_btscan ? ATH_BTCOEX_STOMP_ALL :
81 btcoex->bt_stomp_type);
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));
93 * Work to time slice between wlan and bt traffic and
94 * configure weight registers
96 static void ath_btcoex_duty_cycle_work(struct work_struct *work)
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;
105 ath_dbg(common, BTCOEX, "time slice work for bt and wlan\n");
107 if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
108 ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
109 else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
110 ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
111 ath9k_hw_btcoex_enable(priv->ah);
114 void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
116 struct ath_btcoex *btcoex = &priv->btcoex;
118 if (ath9k_hw_get_btcoex_scheme(priv->ah) == ATH_BTCOEX_CFG_NONE)
119 return;
121 btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
122 btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
123 btcoex->btcoex_period / 100;
124 btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
125 btcoex->btcoex_period / 100;
126 INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
127 INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
131 * (Re)start btcoex work
134 void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
136 struct ath_btcoex *btcoex = &priv->btcoex;
137 struct ath_hw *ah = priv->ah;
139 if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
140 return;
142 ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex work\n");
144 btcoex->bt_priority_cnt = 0;
145 btcoex->bt_priority_time = jiffies;
146 priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
147 ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
152 * Cancel btcoex and bt duty cycle work.
154 void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
156 if (ath9k_hw_get_btcoex_scheme(priv->ah) == ATH_BTCOEX_CFG_NONE)
157 return;
159 cancel_delayed_work_sync(&priv->coex_period_work);
160 cancel_delayed_work_sync(&priv->duty_cycle_work);
163 /*******/
164 /* LED */
165 /*******/
167 #ifdef CONFIG_MAC80211_LEDS
168 void ath9k_led_work(struct work_struct *work)
170 struct ath9k_htc_priv *priv = container_of(work,
171 struct ath9k_htc_priv,
172 led_work);
174 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
175 (priv->brightness == LED_OFF));
178 static void ath9k_led_brightness(struct led_classdev *led_cdev,
179 enum led_brightness brightness)
181 struct ath9k_htc_priv *priv = container_of(led_cdev,
182 struct ath9k_htc_priv,
183 led_cdev);
185 /* Not locked, but it's just a tiny green light..*/
186 priv->brightness = brightness;
187 ieee80211_queue_work(priv->hw, &priv->led_work);
190 void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
192 if (!priv->led_registered)
193 return;
195 ath9k_led_brightness(&priv->led_cdev, LED_OFF);
196 led_classdev_unregister(&priv->led_cdev);
197 cancel_work_sync(&priv->led_work);
200 void ath9k_init_leds(struct ath9k_htc_priv *priv)
202 int ret;
204 if (AR_SREV_9287(priv->ah))
205 priv->ah->led_pin = ATH_LED_PIN_9287;
206 else if (AR_SREV_9271(priv->ah))
207 priv->ah->led_pin = ATH_LED_PIN_9271;
208 else if (AR_DEVID_7010(priv->ah))
209 priv->ah->led_pin = ATH_LED_PIN_7010;
210 else
211 priv->ah->led_pin = ATH_LED_PIN_DEF;
213 /* Configure gpio 1 for output */
214 ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
215 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
216 /* LED off, active low */
217 ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
219 snprintf(priv->led_name, sizeof(priv->led_name),
220 "ath9k_htc-%s", wiphy_name(priv->hw->wiphy));
221 priv->led_cdev.name = priv->led_name;
222 priv->led_cdev.brightness_set = ath9k_led_brightness;
224 ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &priv->led_cdev);
225 if (ret < 0)
226 return;
228 INIT_WORK(&priv->led_work, ath9k_led_work);
229 priv->led_registered = true;
231 return;
233 #endif
235 /*******************/
236 /* Rfkill */
237 /*******************/
239 static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
241 bool is_blocked;
243 ath9k_htc_ps_wakeup(priv);
244 is_blocked = ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
245 priv->ah->rfkill_polarity;
246 ath9k_htc_ps_restore(priv);
248 return is_blocked;
251 void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
253 struct ath9k_htc_priv *priv = hw->priv;
254 bool blocked = !!ath_is_rfkill_set(priv);
256 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
259 void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
261 if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
262 wiphy_rfkill_start_polling(priv->hw->wiphy);
265 void ath9k_htc_radio_enable(struct ieee80211_hw *hw)
267 struct ath9k_htc_priv *priv = hw->priv;
268 struct ath_hw *ah = priv->ah;
269 struct ath_common *common = ath9k_hw_common(ah);
270 int ret;
271 u8 cmd_rsp;
273 if (!ah->curchan)
274 ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
276 /* Reset the HW */
277 ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
278 if (ret) {
279 ath_err(common,
280 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
281 ret, ah->curchan->channel);
284 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
285 &priv->curtxpow);
287 /* Start RX */
288 WMI_CMD(WMI_START_RECV_CMDID);
289 ath9k_host_rx_init(priv);
291 /* Start TX */
292 htc_start(priv->htc);
293 spin_lock_bh(&priv->tx.tx_lock);
294 priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
295 spin_unlock_bh(&priv->tx.tx_lock);
296 ieee80211_wake_queues(hw);
298 WMI_CMD(WMI_ENABLE_INTR_CMDID);
300 /* Enable LED */
301 ath9k_hw_cfg_output(ah, ah->led_pin,
302 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
303 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
306 void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
308 struct ath9k_htc_priv *priv = hw->priv;
309 struct ath_hw *ah = priv->ah;
310 struct ath_common *common = ath9k_hw_common(ah);
311 int ret;
312 u8 cmd_rsp;
314 ath9k_htc_ps_wakeup(priv);
316 /* Disable LED */
317 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
318 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
320 WMI_CMD(WMI_DISABLE_INTR_CMDID);
322 /* Stop TX */
323 ieee80211_stop_queues(hw);
324 ath9k_htc_tx_drain(priv);
325 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
327 /* Stop RX */
328 WMI_CMD(WMI_STOP_RECV_CMDID);
330 /* Clear the WMI event queue */
331 ath9k_wmi_event_drain(priv);
334 * The MIB counters have to be disabled here,
335 * since the target doesn't do it.
337 ath9k_hw_disable_mib_counters(ah);
339 if (!ah->curchan)
340 ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
342 /* Reset the HW */
343 ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
344 if (ret) {
345 ath_err(common,
346 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
347 ret, ah->curchan->channel);
350 /* Disable the PHY */
351 ath9k_hw_phy_disable(ah);
353 ath9k_htc_ps_restore(priv);
354 ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);