Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2010-2011 Atheros Communications Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include "htc.h"
0018 
0019 /******************/
0020 /*     BTCOEX     */
0021 /******************/
0022 
0023 #define ATH_HTC_BTCOEX_PRODUCT_ID "wb193"
0024 
0025 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
0026 
0027 /*
0028  * Detects if there is any priority bt traffic
0029  */
0030 static void ath_detect_bt_priority(struct ath9k_htc_priv *priv)
0031 {
0032     struct ath_btcoex *btcoex = &priv->btcoex;
0033     struct ath_hw *ah = priv->ah;
0034 
0035     if (ath9k_hw_gpio_get(ah, ah->btcoex_hw.btpriority_gpio))
0036         btcoex->bt_priority_cnt++;
0037 
0038     if (time_after(jiffies, btcoex->bt_priority_time +
0039             msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
0040         clear_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
0041         clear_bit(OP_BT_SCAN, &priv->op_flags);
0042         /* Detect if colocated bt started scanning */
0043         if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
0044             ath_dbg(ath9k_hw_common(ah), BTCOEX,
0045                 "BT scan detected\n");
0046             set_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
0047             set_bit(OP_BT_SCAN, &priv->op_flags);
0048         } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
0049             ath_dbg(ath9k_hw_common(ah), BTCOEX,
0050                 "BT priority traffic detected\n");
0051             set_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
0052         }
0053 
0054         btcoex->bt_priority_cnt = 0;
0055         btcoex->bt_priority_time = jiffies;
0056     }
0057 }
0058 
0059 /*
0060  * This is the master bt coex work which runs for every
0061  * 45ms, bt traffic will be given priority during 55% of this
0062  * period while wlan gets remaining 45%
0063  */
0064 static void ath_btcoex_period_work(struct work_struct *work)
0065 {
0066     struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
0067                            coex_period_work.work);
0068     struct ath_btcoex *btcoex = &priv->btcoex;
0069     struct ath_common *common = ath9k_hw_common(priv->ah);
0070     u32 timer_period;
0071     int ret;
0072 
0073     ath_detect_bt_priority(priv);
0074 
0075     ret = ath9k_htc_update_cap_target(priv,
0076               test_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags));
0077     if (ret) {
0078         ath_err(common, "Unable to set BTCOEX parameters\n");
0079         return;
0080     }
0081 
0082     ath9k_hw_btcoex_bt_stomp(priv->ah, test_bit(OP_BT_SCAN, &priv->op_flags) ?
0083                  ATH_BTCOEX_STOMP_ALL : btcoex->bt_stomp_type);
0084 
0085     ath9k_hw_btcoex_enable(priv->ah);
0086     timer_period = test_bit(OP_BT_SCAN, &priv->op_flags) ?
0087         btcoex->btscan_no_stomp : btcoex->btcoex_no_stomp;
0088     ieee80211_queue_delayed_work(priv->hw, &priv->duty_cycle_work,
0089                      msecs_to_jiffies(timer_period));
0090     ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work,
0091                      msecs_to_jiffies(btcoex->btcoex_period));
0092 }
0093 
0094 /*
0095  * Work to time slice between wlan and bt traffic and
0096  * configure weight registers
0097  */
0098 static void ath_btcoex_duty_cycle_work(struct work_struct *work)
0099 {
0100     struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
0101                            duty_cycle_work.work);
0102     struct ath_hw *ah = priv->ah;
0103     struct ath_btcoex *btcoex = &priv->btcoex;
0104     struct ath_common *common = ath9k_hw_common(ah);
0105 
0106     ath_dbg(common, BTCOEX, "time slice work for bt and wlan\n");
0107 
0108     if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW ||
0109         test_bit(OP_BT_SCAN, &priv->op_flags))
0110         ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
0111     else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
0112         ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
0113 
0114     ath9k_hw_btcoex_enable(priv->ah);
0115 }
0116 
0117 static void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
0118 {
0119     struct ath_btcoex *btcoex = &priv->btcoex;
0120 
0121     btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
0122     btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
0123         btcoex->btcoex_period / 100;
0124     btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
0125                    btcoex->btcoex_period / 100;
0126     INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
0127     INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
0128 }
0129 
0130 /*
0131  * (Re)start btcoex work
0132  */
0133 
0134 static void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
0135 {
0136     struct ath_btcoex *btcoex = &priv->btcoex;
0137     struct ath_hw *ah = priv->ah;
0138 
0139     ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex work\n");
0140 
0141     btcoex->bt_priority_cnt = 0;
0142     btcoex->bt_priority_time = jiffies;
0143     clear_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
0144     clear_bit(OP_BT_SCAN, &priv->op_flags);
0145     ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
0146 }
0147 
0148 
0149 /*
0150  * Cancel btcoex and bt duty cycle work.
0151  */
0152 static void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
0153 {
0154     cancel_delayed_work_sync(&priv->coex_period_work);
0155     cancel_delayed_work_sync(&priv->duty_cycle_work);
0156 }
0157 
0158 void ath9k_htc_start_btcoex(struct ath9k_htc_priv *priv)
0159 {
0160     struct ath_hw *ah = priv->ah;
0161 
0162     if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE) {
0163         ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
0164                        AR_STOMP_LOW_WLAN_WGHT, 0);
0165         ath9k_hw_btcoex_enable(ah);
0166         ath_htc_resume_btcoex_work(priv);
0167     }
0168 }
0169 
0170 void ath9k_htc_stop_btcoex(struct ath9k_htc_priv *priv)
0171 {
0172     struct ath_hw *ah = priv->ah;
0173 
0174     if (ah->btcoex_hw.enabled &&
0175         ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) {
0176         if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
0177             ath_htc_cancel_btcoex_work(priv);
0178         ath9k_hw_btcoex_disable(ah);
0179     }
0180 }
0181 
0182 void ath9k_htc_init_btcoex(struct ath9k_htc_priv *priv, char *product)
0183 {
0184     struct ath_hw *ah = priv->ah;
0185     struct ath_common *common = ath9k_hw_common(ah);
0186     int qnum;
0187 
0188     /*
0189      * Check if BTCOEX is globally disabled.
0190      */
0191     if (!common->btcoex_enabled) {
0192         ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_NONE;
0193         return;
0194     }
0195 
0196     if (product && strncmp(product, ATH_HTC_BTCOEX_PRODUCT_ID, 5) == 0) {
0197         ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_3WIRE;
0198     }
0199 
0200     switch (ath9k_hw_get_btcoex_scheme(priv->ah)) {
0201     case ATH_BTCOEX_CFG_NONE:
0202         break;
0203     case ATH_BTCOEX_CFG_3WIRE:
0204         priv->ah->btcoex_hw.btactive_gpio = 7;
0205         priv->ah->btcoex_hw.btpriority_gpio = 6;
0206         priv->ah->btcoex_hw.wlanactive_gpio = 8;
0207         priv->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
0208         ath9k_hw_btcoex_init_3wire(priv->ah);
0209         ath_htc_init_btcoex_work(priv);
0210         qnum = priv->hwq_map[IEEE80211_AC_BE];
0211         ath9k_hw_init_btcoex_hw(priv->ah, qnum);
0212         break;
0213     default:
0214         WARN_ON(1);
0215         break;
0216     }
0217 }
0218 
0219 #endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */
0220 
0221 /*******/
0222 /* LED */
0223 /*******/
0224 
0225 #ifdef CONFIG_MAC80211_LEDS
0226 void ath9k_led_work(struct work_struct *work)
0227 {
0228     struct ath9k_htc_priv *priv = container_of(work,
0229                            struct ath9k_htc_priv,
0230                            led_work);
0231 
0232     ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
0233               (priv->brightness == LED_OFF));
0234 }
0235 
0236 static void ath9k_led_brightness(struct led_classdev *led_cdev,
0237                  enum led_brightness brightness)
0238 {
0239     struct ath9k_htc_priv *priv = container_of(led_cdev,
0240                            struct ath9k_htc_priv,
0241                            led_cdev);
0242 
0243     /* Not locked, but it's just a tiny green light..*/
0244     priv->brightness = brightness;
0245     ieee80211_queue_work(priv->hw, &priv->led_work);
0246 }
0247 
0248 void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
0249 {
0250     if (!priv->led_registered)
0251         return;
0252 
0253     ath9k_led_brightness(&priv->led_cdev, LED_OFF);
0254     led_classdev_unregister(&priv->led_cdev);
0255     cancel_work_sync(&priv->led_work);
0256 
0257     ath9k_hw_gpio_free(priv->ah, priv->ah->led_pin);
0258 }
0259 
0260 
0261 void ath9k_configure_leds(struct ath9k_htc_priv *priv)
0262 {
0263     /* Configure gpio 1 for output */
0264     ath9k_hw_gpio_request_out(priv->ah, priv->ah->led_pin,
0265                   "ath9k-led",
0266                   AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
0267     /* LED off, active low */
0268     ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
0269 }
0270 
0271 void ath9k_init_leds(struct ath9k_htc_priv *priv)
0272 {
0273     int ret;
0274 
0275     if (AR_SREV_9287(priv->ah))
0276         priv->ah->led_pin = ATH_LED_PIN_9287;
0277     else if (AR_SREV_9271(priv->ah))
0278         priv->ah->led_pin = ATH_LED_PIN_9271;
0279     else if (AR_DEVID_7010(priv->ah))
0280         priv->ah->led_pin = ATH_LED_PIN_7010;
0281     else
0282         priv->ah->led_pin = ATH_LED_PIN_DEF;
0283 
0284     if (!ath9k_htc_led_blink)
0285         priv->led_cdev.default_trigger =
0286             ieee80211_get_radio_led_name(priv->hw);
0287 
0288     ath9k_configure_leds(priv);
0289 
0290     snprintf(priv->led_name, sizeof(priv->led_name),
0291         "ath9k_htc-%s", wiphy_name(priv->hw->wiphy));
0292     priv->led_cdev.name = priv->led_name;
0293     priv->led_cdev.brightness_set = ath9k_led_brightness;
0294 
0295     ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &priv->led_cdev);
0296     if (ret < 0)
0297         return;
0298 
0299     INIT_WORK(&priv->led_work, ath9k_led_work);
0300     priv->led_registered = true;
0301 
0302     return;
0303 }
0304 #endif
0305 
0306 /*******************/
0307 /*  Rfkill     */
0308 /*******************/
0309 
0310 static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
0311 {
0312     bool is_blocked;
0313 
0314     ath9k_htc_ps_wakeup(priv);
0315     is_blocked = ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
0316                          priv->ah->rfkill_polarity;
0317     ath9k_htc_ps_restore(priv);
0318 
0319     return is_blocked;
0320 }
0321 
0322 void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
0323 {
0324     struct ath9k_htc_priv *priv = hw->priv;
0325     bool blocked = !!ath_is_rfkill_set(priv);
0326 
0327     wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
0328 }
0329 
0330 void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
0331 {
0332     if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
0333         wiphy_rfkill_start_polling(priv->hw->wiphy);
0334 }