gpio.c 11.2 KB
Newer Older
S
Sujith 已提交
1
/*
2
 * Copyright (c) 2008-2011 Atheros Communications Inc.
S
Sujith 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "ath9k.h"

/********************************/
/*	 LED functions		*/
/********************************/

23
#ifdef CONFIG_MAC80211_LEDS
S
Sujith 已提交
24 25 26
static void ath_led_brightness(struct led_classdev *led_cdev,
			       enum led_brightness brightness)
{
27 28
	struct ath_softc *sc = container_of(led_cdev, struct ath_softc, led_cdev);
	ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, (brightness == LED_OFF));
S
Sujith 已提交
29 30 31 32
}

void ath_deinit_leds(struct ath_softc *sc)
{
33 34 35 36 37
	if (!sc->led_registered)
		return;

	ath_led_brightness(&sc->led_cdev, LED_OFF);
	led_classdev_unregister(&sc->led_cdev);
S
Sujith 已提交
38 39 40 41 42 43
}

void ath_init_leds(struct ath_softc *sc)
{
	int ret;

44 45 46 47 48
	if (sc->sc_ah->led_pin < 0) {
		if (AR_SREV_9287(sc->sc_ah))
			sc->sc_ah->led_pin = ATH_LED_PIN_9287;
		else if (AR_SREV_9485(sc->sc_ah))
			sc->sc_ah->led_pin = ATH_LED_PIN_9485;
49 50
		else if (AR_SREV_9300(sc->sc_ah))
			sc->sc_ah->led_pin = ATH_LED_PIN_9300;
51 52
		else if (AR_SREV_9462(sc->sc_ah))
			sc->sc_ah->led_pin = ATH_LED_PIN_9462;
53 54 55
		else
			sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
	}
S
Sujith 已提交
56 57 58 59 60 61 62

	/* Configure gpio 1 for output */
	ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
			    AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
	/* LED off, active low */
	ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);

63 64 65 66 67 68 69 70 71 72 73 74 75 76
	if (!led_blink)
		sc->led_cdev.default_trigger =
			ieee80211_get_radio_led_name(sc->hw);

	snprintf(sc->led_name, sizeof(sc->led_name),
		"ath9k-%s", wiphy_name(sc->hw->wiphy));
	sc->led_cdev.name = sc->led_name;
	sc->led_cdev.brightness_set = ath_led_brightness;

	ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
	if (ret < 0)
		return;

	sc->led_registered = true;
S
Sujith 已提交
77
}
78
#endif
S
Sujith 已提交
79 80 81 82 83 84 85 86

/*******************/
/*	Rfkill	   */
/*******************/

static bool ath_is_rfkill_set(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;
87
	bool is_blocked;
S
Sujith 已提交
88

89 90
	ath9k_ps_wakeup(sc);
	is_blocked = ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
S
Sujith 已提交
91
				  ah->rfkill_polarity;
92 93 94
	ath9k_ps_restore(sc);

	return is_blocked;
S
Sujith 已提交
95 96 97 98
}

void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
{
99
	struct ath_softc *sc = hw->priv;
S
Sujith 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
	bool blocked = !!ath_is_rfkill_set(sc);

	wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
}

void ath_start_rfkill_poll(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;

	if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
		wiphy_rfkill_start_polling(sc->hw->wiphy);
}

113 114
#ifdef CONFIG_ATH9K_BTCOEX_SUPPORT

S
Sujith 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/******************/
/*     BTCOEX     */
/******************/

/*
 * Detects if there is any priority bt traffic
 */
static void ath_detect_bt_priority(struct ath_softc *sc)
{
	struct ath_btcoex *btcoex = &sc->btcoex;
	struct ath_hw *ah = sc->sc_ah;

	if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
		btcoex->bt_priority_cnt++;

	if (time_after(jiffies, btcoex->bt_priority_time +
			msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
132 133 134
		sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
		/* Detect if colocated bt started scanning */
		if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
135
			ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
J
Joe Perches 已提交
136
				"BT scan detected\n");
137 138 139
			sc->sc_flags |= (SC_OP_BT_SCAN |
					 SC_OP_BT_PRIORITY_DETECTED);
		} else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
140
			ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
J
Joe Perches 已提交
141
				"BT priority traffic detected\n");
S
Sujith 已提交
142 143 144 145 146 147 148 149 150 151
			sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
		}

		btcoex->bt_priority_cnt = 0;
		btcoex->bt_priority_time = jiffies;
	}
}

static void ath9k_gen_timer_start(struct ath_hw *ah,
				  struct ath_gen_timer *timer,
152
				  u32 trig_timeout,
S
Sujith 已提交
153 154
				  u32 timer_period)
{
155
	ath9k_hw_gen_timer_start(ah, timer, trig_timeout, timer_period);
S
Sujith 已提交
156

P
Pavel Roskin 已提交
157
	if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
158
		ath9k_hw_disable_interrupts(ah);
P
Pavel Roskin 已提交
159
		ah->imask |= ATH9K_INT_GENTIMER;
160
		ath9k_hw_set_interrupts(ah);
161
		ath9k_hw_enable_interrupts(ah);
S
Sujith 已提交
162 163 164 165 166 167 168 169 170 171 172
	}
}

static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
{
	struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;

	ath9k_hw_gen_timer_stop(ah, timer);

	/* if no timer is enabled, turn off interrupt mask */
	if (timer_table->timer_mask.val == 0) {
173
		ath9k_hw_disable_interrupts(ah);
P
Pavel Roskin 已提交
174
		ah->imask &= ~ATH9K_INT_GENTIMER;
175
		ath9k_hw_set_interrupts(ah);
176
		ath9k_hw_enable_interrupts(ah);
S
Sujith 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189
	}
}

/*
 * This is the master bt coex timer which runs for every
 * 45ms, bt traffic will be given priority during 55% of this
 * period while wlan gets remaining 45%
 */
static void ath_btcoex_period_timer(unsigned long data)
{
	struct ath_softc *sc = (struct ath_softc *) data;
	struct ath_hw *ah = sc->sc_ah;
	struct ath_btcoex *btcoex = &sc->btcoex;
190 191
	u32 timer_period;
	bool is_btscan;
S
Sujith 已提交
192

193
	ath9k_ps_wakeup(sc);
194 195
	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
		ath_detect_bt_priority(sc);
196 197
	is_btscan = sc->sc_flags & SC_OP_BT_SCAN;

S
Sujith 已提交
198 199
	spin_lock_bh(&btcoex->btcoex_lock);

200
	ath9k_hw_btcoex_bt_stomp(ah, is_btscan ? ATH_BTCOEX_STOMP_ALL :
201
			      btcoex->bt_stomp_type);
S
Sujith 已提交
202

203
	ath9k_hw_btcoex_enable(ah);
S
Sujith 已提交
204 205 206 207 208 209
	spin_unlock_bh(&btcoex->btcoex_lock);

	if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
		if (btcoex->hw_timer_enabled)
			ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);

210 211
		timer_period = is_btscan ? btcoex->btscan_no_stomp :
					   btcoex->btcoex_no_stomp;
212
		ath9k_gen_timer_start(ah, btcoex->no_stomp_timer, timer_period,
213
				      timer_period * 10);
S
Sujith 已提交
214 215 216
		btcoex->hw_timer_enabled = true;
	}

217
	ath9k_ps_restore(sc);
218
	timer_period = btcoex->btcoex_period / 1000;
S
Sujith 已提交
219
	mod_timer(&btcoex->period_timer, jiffies +
220
				  msecs_to_jiffies(timer_period));
S
Sujith 已提交
221 222 223 224 225 226 227 228 229 230 231
}

/*
 * Generic tsf based hw timer which configures weight
 * registers to time slice between wlan and bt traffic
 */
static void ath_btcoex_no_stomp_timer(void *arg)
{
	struct ath_softc *sc = (struct ath_softc *)arg;
	struct ath_hw *ah = sc->sc_ah;
	struct ath_btcoex *btcoex = &sc->btcoex;
232
	struct ath_common *common = ath9k_hw_common(ah);
233
	bool is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
S
Sujith 已提交
234

235
	ath_dbg(common, BTCOEX, "no stomp timer running\n");
S
Sujith 已提交
236

237
	ath9k_ps_wakeup(sc);
S
Sujith 已提交
238 239
	spin_lock_bh(&btcoex->btcoex_lock);

240
	if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
241
		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
S
Sujith 已提交
242
	 else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
243
		ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
S
Sujith 已提交
244

245
	ath9k_hw_btcoex_enable(ah);
S
Sujith 已提交
246
	spin_unlock_bh(&btcoex->btcoex_lock);
247
	ath9k_ps_restore(sc);
S
Sujith 已提交
248 249
}

250
static int ath_init_btcoex_timer(struct ath_softc *sc)
S
Sujith 已提交
251 252 253 254 255 256
{
	struct ath_btcoex *btcoex = &sc->btcoex;

	btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
	btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
		btcoex->btcoex_period / 100;
257 258
	btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
				   btcoex->btcoex_period / 100;
S
Sujith 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

	setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
			(unsigned long) sc);

	spin_lock_init(&btcoex->btcoex_lock);

	btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
			ath_btcoex_no_stomp_timer,
			ath_btcoex_no_stomp_timer,
			(void *) sc, AR_FIRST_NDP_TIMER);

	if (!btcoex->no_stomp_timer)
		return -ENOMEM;

	return 0;
}

/*
 * (Re)start btcoex timers
 */
void ath9k_btcoex_timer_resume(struct ath_softc *sc)
{
	struct ath_btcoex *btcoex = &sc->btcoex;
	struct ath_hw *ah = sc->sc_ah;

284
	ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex timers\n");
S
Sujith 已提交
285 286 287 288 289 290 291

	/* make sure duty cycle timer is also stopped when resuming */
	if (btcoex->hw_timer_enabled)
		ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);

	btcoex->bt_priority_cnt = 0;
	btcoex->bt_priority_time = jiffies;
292
	sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
S
Sujith 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

	mod_timer(&btcoex->period_timer, jiffies);
}


/*
 * Pause btcoex timer and bt duty cycle timer
 */
void ath9k_btcoex_timer_pause(struct ath_softc *sc)
{
	struct ath_btcoex *btcoex = &sc->btcoex;
	struct ath_hw *ah = sc->sc_ah;

	del_timer_sync(&btcoex->period_timer);

	if (btcoex->hw_timer_enabled)
		ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);

	btcoex->hw_timer_enabled = false;
}
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327
u16 ath9k_btcoex_aggr_limit(struct ath_softc *sc, u32 max_4ms_framelen)
{
	struct ath_mci_profile *mci = &sc->btcoex.mci;
	u16 aggr_limit = 0;

	if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI) && mci->aggr_limit)
		aggr_limit = (max_4ms_framelen * mci->aggr_limit) >> 4;
	else if (sc->sc_flags & SC_OP_BT_PRIORITY_DETECTED)
		aggr_limit = min((max_4ms_framelen * 3) / 8,
				 (u32)ATH_AMPDU_LIMIT_MAX);

	return aggr_limit;
}

328 329 330 331 332 333 334 335
void ath9k_btcoex_handle_interrupt(struct ath_softc *sc, u32 status)
{
	struct ath_hw *ah = sc->sc_ah;

	if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
		if (status & ATH9K_INT_GENTIMER)
			ath_gen_timer_isr(sc->sc_ah);

336
	if (status & ATH9K_INT_MCI)
337 338 339
		ath_mci_intr(sc);
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
void ath9k_start_btcoex(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;

	if ((ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) &&
	    !ah->btcoex_hw.enabled) {
		if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
			ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
						   AR_STOMP_LOW_WLAN_WGHT);
		ath9k_hw_btcoex_enable(ah);

		if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
			ath9k_btcoex_timer_resume(sc);
	}
}

void ath9k_stop_btcoex(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;

	if (ah->btcoex_hw.enabled &&
	    ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) {
		ath9k_hw_btcoex_disable(ah);
		if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
			ath9k_btcoex_timer_pause(sc);
		ath_mci_flush_profile(&sc->btcoex.mci);
	}
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
void ath9k_deinit_btcoex(struct ath_softc *sc)
{
        if ((sc->btcoex.no_stomp_timer) &&
	    ath9k_hw_get_btcoex_scheme(sc->sc_ah) == ATH_BTCOEX_CFG_3WIRE)
		ath_gen_timer_free(sc->sc_ah, sc->btcoex.no_stomp_timer);

	if (ath9k_hw_get_btcoex_scheme(sc->sc_ah) == ATH_BTCOEX_CFG_MCI)
		ath_mci_cleanup(sc);
}

int ath9k_init_btcoex(struct ath_softc *sc)
{
	struct ath_txq *txq;
	struct ath_hw *ah = sc->sc_ah;
	int r;

385 386
	ath9k_hw_btcoex_init_scheme(ah);

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	switch (ath9k_hw_get_btcoex_scheme(sc->sc_ah)) {
	case ATH_BTCOEX_CFG_NONE:
		break;
	case ATH_BTCOEX_CFG_2WIRE:
		ath9k_hw_btcoex_init_2wire(sc->sc_ah);
		break;
	case ATH_BTCOEX_CFG_3WIRE:
		ath9k_hw_btcoex_init_3wire(sc->sc_ah);
		r = ath_init_btcoex_timer(sc);
		if (r)
			return -1;
		txq = sc->tx.txq_map[WME_AC_BE];
		ath9k_hw_init_btcoex_hw(sc->sc_ah, txq->axq_qnum);
		sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
		break;
	case ATH_BTCOEX_CFG_MCI:
		sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
		sc->btcoex.duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
		INIT_LIST_HEAD(&sc->btcoex.mci.info);

		r = ath_mci_setup(sc);
		if (r)
			return r;

		ath9k_hw_btcoex_init_mci(ah);

		break;
	default:
		WARN_ON(1);
		break;
	}

	return 0;
}
421 422

#endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */