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.
23 #define ATH_HTC_BTCOEX_PRODUCT_ID "wb193"
25 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
28 * Detects if there is any priority bt traffic
30 static void ath_detect_bt_priority(struct ath9k_htc_priv
*priv
)
32 struct ath_btcoex
*btcoex
= &priv
->btcoex
;
33 struct ath_hw
*ah
= priv
->ah
;
35 if (ath9k_hw_gpio_get(ah
, ah
->btcoex_hw
.btpriority_gpio
))
36 btcoex
->bt_priority_cnt
++;
38 if (time_after(jiffies
, btcoex
->bt_priority_time
+
39 msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD
))) {
40 clear_bit(OP_BT_PRIORITY_DETECTED
, &priv
->op_flags
);
41 clear_bit(OP_BT_SCAN
, &priv
->op_flags
);
42 /* Detect if colocated bt started scanning */
43 if (btcoex
->bt_priority_cnt
>= ATH_BT_CNT_SCAN_THRESHOLD
) {
44 ath_dbg(ath9k_hw_common(ah
), BTCOEX
,
45 "BT scan detected\n");
46 set_bit(OP_BT_PRIORITY_DETECTED
, &priv
->op_flags
);
47 set_bit(OP_BT_SCAN
, &priv
->op_flags
);
48 } else if (btcoex
->bt_priority_cnt
>= ATH_BT_CNT_THRESHOLD
) {
49 ath_dbg(ath9k_hw_common(ah
), BTCOEX
,
50 "BT priority traffic detected\n");
51 set_bit(OP_BT_PRIORITY_DETECTED
, &priv
->op_flags
);
54 btcoex
->bt_priority_cnt
= 0;
55 btcoex
->bt_priority_time
= jiffies
;
60 * This is the master bt coex work which runs for every
61 * 45ms, bt traffic will be given priority during 55% of this
62 * period while wlan gets remaining 45%
64 static void ath_btcoex_period_work(struct work_struct
*work
)
66 struct ath9k_htc_priv
*priv
= container_of(work
, struct ath9k_htc_priv
,
67 coex_period_work
.work
);
68 struct ath_btcoex
*btcoex
= &priv
->btcoex
;
69 struct ath_common
*common
= ath9k_hw_common(priv
->ah
);
73 ath_detect_bt_priority(priv
);
75 ret
= ath9k_htc_update_cap_target(priv
,
76 test_bit(OP_BT_PRIORITY_DETECTED
, &priv
->op_flags
));
78 ath_err(common
, "Unable to set BTCOEX parameters\n");
82 ath9k_hw_btcoex_bt_stomp(priv
->ah
, test_bit(OP_BT_SCAN
, &priv
->op_flags
) ?
83 ATH_BTCOEX_STOMP_ALL
: btcoex
->bt_stomp_type
);
85 ath9k_hw_btcoex_enable(priv
->ah
);
86 timer_period
= test_bit(OP_BT_SCAN
, &priv
->op_flags
) ?
87 btcoex
->btscan_no_stomp
: btcoex
->btcoex_no_stomp
;
88 ieee80211_queue_delayed_work(priv
->hw
, &priv
->duty_cycle_work
,
89 msecs_to_jiffies(timer_period
));
90 ieee80211_queue_delayed_work(priv
->hw
, &priv
->coex_period_work
,
91 msecs_to_jiffies(btcoex
->btcoex_period
));
95 * Work to time slice between wlan and bt traffic and
96 * configure weight registers
98 static void ath_btcoex_duty_cycle_work(struct work_struct
*work
)
100 struct ath9k_htc_priv
*priv
= container_of(work
, struct ath9k_htc_priv
,
101 duty_cycle_work
.work
);
102 struct ath_hw
*ah
= priv
->ah
;
103 struct ath_btcoex
*btcoex
= &priv
->btcoex
;
104 struct ath_common
*common
= ath9k_hw_common(ah
);
106 ath_dbg(common
, BTCOEX
, "time slice work for bt and wlan\n");
108 if (btcoex
->bt_stomp_type
== ATH_BTCOEX_STOMP_LOW
||
109 test_bit(OP_BT_SCAN
, &priv
->op_flags
))
110 ath9k_hw_btcoex_bt_stomp(ah
, ATH_BTCOEX_STOMP_NONE
);
111 else if (btcoex
->bt_stomp_type
== ATH_BTCOEX_STOMP_ALL
)
112 ath9k_hw_btcoex_bt_stomp(ah
, ATH_BTCOEX_STOMP_LOW
);
114 ath9k_hw_btcoex_enable(priv
->ah
);
117 static void ath_htc_init_btcoex_work(struct ath9k_htc_priv
*priv
)
119 struct ath_btcoex
*btcoex
= &priv
->btcoex
;
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 static 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 ath_dbg(ath9k_hw_common(ah
), BTCOEX
, "Starting btcoex work\n");
141 btcoex
->bt_priority_cnt
= 0;
142 btcoex
->bt_priority_time
= jiffies
;
143 clear_bit(OP_BT_PRIORITY_DETECTED
, &priv
->op_flags
);
144 clear_bit(OP_BT_SCAN
, &priv
->op_flags
);
145 ieee80211_queue_delayed_work(priv
->hw
, &priv
->coex_period_work
, 0);
150 * Cancel btcoex and bt duty cycle work.
152 static void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv
*priv
)
154 cancel_delayed_work_sync(&priv
->coex_period_work
);
155 cancel_delayed_work_sync(&priv
->duty_cycle_work
);
158 void ath9k_htc_start_btcoex(struct ath9k_htc_priv
*priv
)
160 struct ath_hw
*ah
= priv
->ah
;
162 if (ath9k_hw_get_btcoex_scheme(ah
) == ATH_BTCOEX_CFG_3WIRE
) {
163 ath9k_hw_btcoex_set_weight(ah
, AR_BT_COEX_WGHT
,
164 AR_STOMP_LOW_WLAN_WGHT
, 0);
165 ath9k_hw_btcoex_enable(ah
);
166 ath_htc_resume_btcoex_work(priv
);
170 void ath9k_htc_stop_btcoex(struct ath9k_htc_priv
*priv
)
172 struct ath_hw
*ah
= priv
->ah
;
174 if (ah
->btcoex_hw
.enabled
&&
175 ath9k_hw_get_btcoex_scheme(ah
) != ATH_BTCOEX_CFG_NONE
) {
176 if (ah
->btcoex_hw
.scheme
== ATH_BTCOEX_CFG_3WIRE
)
177 ath_htc_cancel_btcoex_work(priv
);
178 ath9k_hw_btcoex_disable(ah
);
182 void ath9k_htc_init_btcoex(struct ath9k_htc_priv
*priv
, char *product
)
184 struct ath_hw
*ah
= priv
->ah
;
185 struct ath_common
*common
= ath9k_hw_common(ah
);
189 * Check if BTCOEX is globally disabled.
191 if (!common
->btcoex_enabled
) {
192 ah
->btcoex_hw
.scheme
= ATH_BTCOEX_CFG_NONE
;
196 if (product
&& strncmp(product
, ATH_HTC_BTCOEX_PRODUCT_ID
, 5) == 0) {
197 ah
->btcoex_hw
.scheme
= ATH_BTCOEX_CFG_3WIRE
;
200 switch (ath9k_hw_get_btcoex_scheme(priv
->ah
)) {
201 case ATH_BTCOEX_CFG_NONE
:
203 case ATH_BTCOEX_CFG_3WIRE
:
204 priv
->ah
->btcoex_hw
.btactive_gpio
= 7;
205 priv
->ah
->btcoex_hw
.btpriority_gpio
= 6;
206 priv
->ah
->btcoex_hw
.wlanactive_gpio
= 8;
207 priv
->btcoex
.bt_stomp_type
= ATH_BTCOEX_STOMP_LOW
;
208 ath9k_hw_btcoex_init_3wire(priv
->ah
);
209 ath_htc_init_btcoex_work(priv
);
210 qnum
= priv
->hwq_map
[IEEE80211_AC_BE
];
211 ath9k_hw_init_btcoex_hw(priv
->ah
, qnum
);
219 #endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */
225 #ifdef CONFIG_MAC80211_LEDS
226 void ath9k_led_work(struct work_struct
*work
)
228 struct ath9k_htc_priv
*priv
= container_of(work
,
229 struct ath9k_htc_priv
,
232 ath9k_hw_set_gpio(priv
->ah
, priv
->ah
->led_pin
,
233 (priv
->brightness
== LED_OFF
));
236 static void ath9k_led_brightness(struct led_classdev
*led_cdev
,
237 enum led_brightness brightness
)
239 struct ath9k_htc_priv
*priv
= container_of(led_cdev
,
240 struct ath9k_htc_priv
,
243 /* Not locked, but it's just a tiny green light..*/
244 priv
->brightness
= brightness
;
245 ieee80211_queue_work(priv
->hw
, &priv
->led_work
);
248 void ath9k_deinit_leds(struct ath9k_htc_priv
*priv
)
250 if (!priv
->led_registered
)
253 ath9k_led_brightness(&priv
->led_cdev
, LED_OFF
);
254 led_classdev_unregister(&priv
->led_cdev
);
255 cancel_work_sync(&priv
->led_work
);
258 void ath9k_init_leds(struct ath9k_htc_priv
*priv
)
262 if (AR_SREV_9287(priv
->ah
))
263 priv
->ah
->led_pin
= ATH_LED_PIN_9287
;
264 else if (AR_SREV_9271(priv
->ah
))
265 priv
->ah
->led_pin
= ATH_LED_PIN_9271
;
266 else if (AR_DEVID_7010(priv
->ah
))
267 priv
->ah
->led_pin
= ATH_LED_PIN_7010
;
269 priv
->ah
->led_pin
= ATH_LED_PIN_DEF
;
271 /* Configure gpio 1 for output */
272 ath9k_hw_cfg_output(priv
->ah
, priv
->ah
->led_pin
,
273 AR_GPIO_OUTPUT_MUX_AS_OUTPUT
);
274 /* LED off, active low */
275 ath9k_hw_set_gpio(priv
->ah
, priv
->ah
->led_pin
, 1);
277 snprintf(priv
->led_name
, sizeof(priv
->led_name
),
278 "ath9k_htc-%s", wiphy_name(priv
->hw
->wiphy
));
279 priv
->led_cdev
.name
= priv
->led_name
;
280 priv
->led_cdev
.brightness_set
= ath9k_led_brightness
;
282 ret
= led_classdev_register(wiphy_dev(priv
->hw
->wiphy
), &priv
->led_cdev
);
286 INIT_WORK(&priv
->led_work
, ath9k_led_work
);
287 priv
->led_registered
= true;
293 /*******************/
295 /*******************/
297 static bool ath_is_rfkill_set(struct ath9k_htc_priv
*priv
)
301 ath9k_htc_ps_wakeup(priv
);
302 is_blocked
= ath9k_hw_gpio_get(priv
->ah
, priv
->ah
->rfkill_gpio
) ==
303 priv
->ah
->rfkill_polarity
;
304 ath9k_htc_ps_restore(priv
);
309 void ath9k_htc_rfkill_poll_state(struct ieee80211_hw
*hw
)
311 struct ath9k_htc_priv
*priv
= hw
->priv
;
312 bool blocked
= !!ath_is_rfkill_set(priv
);
314 wiphy_rfkill_set_hw_state(hw
->wiphy
, blocked
);
317 void ath9k_start_rfkill_poll(struct ath9k_htc_priv
*priv
)
319 if (priv
->ah
->caps
.hw_caps
& ATH9K_HW_CAP_RFSILENT
)
320 wiphy_rfkill_start_polling(priv
->hw
->wiphy
);