xref: /linux/drivers/net/wireless/ath/ath9k/gpio.c (revision cc4589ebfae6f8dbb5cf880a0a67eedab3416492)
1 /*
2  * Copyright (c) 2008-2009 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 "ath9k.h"
18 
19 /********************************/
20 /*	 LED functions		*/
21 /********************************/
22 
23 static void ath_led_blink_work(struct work_struct *work)
24 {
25 	struct ath_softc *sc = container_of(work, struct ath_softc,
26 					    ath_led_blink_work.work);
27 
28 	if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
29 		return;
30 
31 	if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
32 	    (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
33 		ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
34 	else
35 		ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
36 				  (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
37 
38 	ieee80211_queue_delayed_work(sc->hw,
39 				     &sc->ath_led_blink_work,
40 				     (sc->sc_flags & SC_OP_LED_ON) ?
41 					msecs_to_jiffies(sc->led_off_duration) :
42 					msecs_to_jiffies(sc->led_on_duration));
43 
44 	sc->led_on_duration = sc->led_on_cnt ?
45 			max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
46 			ATH_LED_ON_DURATION_IDLE;
47 	sc->led_off_duration = sc->led_off_cnt ?
48 			max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
49 			ATH_LED_OFF_DURATION_IDLE;
50 	sc->led_on_cnt = sc->led_off_cnt = 0;
51 	if (sc->sc_flags & SC_OP_LED_ON)
52 		sc->sc_flags &= ~SC_OP_LED_ON;
53 	else
54 		sc->sc_flags |= SC_OP_LED_ON;
55 }
56 
57 static void ath_led_brightness(struct led_classdev *led_cdev,
58 			       enum led_brightness brightness)
59 {
60 	struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
61 	struct ath_softc *sc = led->sc;
62 
63 	switch (brightness) {
64 	case LED_OFF:
65 		if (led->led_type == ATH_LED_ASSOC ||
66 		    led->led_type == ATH_LED_RADIO) {
67 			ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
68 				(led->led_type == ATH_LED_RADIO));
69 			sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
70 			if (led->led_type == ATH_LED_RADIO)
71 				sc->sc_flags &= ~SC_OP_LED_ON;
72 		} else {
73 			sc->led_off_cnt++;
74 		}
75 		break;
76 	case LED_FULL:
77 		if (led->led_type == ATH_LED_ASSOC) {
78 			sc->sc_flags |= SC_OP_LED_ASSOCIATED;
79 			if (led_blink)
80 				ieee80211_queue_delayed_work(sc->hw,
81 						     &sc->ath_led_blink_work, 0);
82 		} else if (led->led_type == ATH_LED_RADIO) {
83 			ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
84 			sc->sc_flags |= SC_OP_LED_ON;
85 		} else {
86 			sc->led_on_cnt++;
87 		}
88 		break;
89 	default:
90 		break;
91 	}
92 }
93 
94 static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
95 			    char *trigger)
96 {
97 	int ret;
98 
99 	led->sc = sc;
100 	led->led_cdev.name = led->name;
101 	led->led_cdev.default_trigger = trigger;
102 	led->led_cdev.brightness_set = ath_led_brightness;
103 
104 	ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
105 	if (ret)
106 		ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
107 			  "Failed to register led:%s", led->name);
108 	else
109 		led->registered = 1;
110 	return ret;
111 }
112 
113 static void ath_unregister_led(struct ath_led *led)
114 {
115 	if (led->registered) {
116 		led_classdev_unregister(&led->led_cdev);
117 		led->registered = 0;
118 	}
119 }
120 
121 void ath_deinit_leds(struct ath_softc *sc)
122 {
123 	ath_unregister_led(&sc->assoc_led);
124 	sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
125 	ath_unregister_led(&sc->tx_led);
126 	ath_unregister_led(&sc->rx_led);
127 	ath_unregister_led(&sc->radio_led);
128 	ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
129 }
130 
131 void ath_init_leds(struct ath_softc *sc)
132 {
133 	char *trigger;
134 	int ret;
135 
136 	if (AR_SREV_9287(sc->sc_ah))
137 		sc->sc_ah->led_pin = ATH_LED_PIN_9287;
138 	else
139 		sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
140 
141 	/* Configure gpio 1 for output */
142 	ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
143 			    AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
144 	/* LED off, active low */
145 	ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
146 
147 	if (led_blink)
148 		INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
149 
150 	trigger = ieee80211_get_radio_led_name(sc->hw);
151 	snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
152 		"ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
153 	ret = ath_register_led(sc, &sc->radio_led, trigger);
154 	sc->radio_led.led_type = ATH_LED_RADIO;
155 	if (ret)
156 		goto fail;
157 
158 	trigger = ieee80211_get_assoc_led_name(sc->hw);
159 	snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
160 		"ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
161 	ret = ath_register_led(sc, &sc->assoc_led, trigger);
162 	sc->assoc_led.led_type = ATH_LED_ASSOC;
163 	if (ret)
164 		goto fail;
165 
166 	trigger = ieee80211_get_tx_led_name(sc->hw);
167 	snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
168 		"ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
169 	ret = ath_register_led(sc, &sc->tx_led, trigger);
170 	sc->tx_led.led_type = ATH_LED_TX;
171 	if (ret)
172 		goto fail;
173 
174 	trigger = ieee80211_get_rx_led_name(sc->hw);
175 	snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
176 		"ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
177 	ret = ath_register_led(sc, &sc->rx_led, trigger);
178 	sc->rx_led.led_type = ATH_LED_RX;
179 	if (ret)
180 		goto fail;
181 
182 	return;
183 
184 fail:
185 	if (led_blink)
186 		cancel_delayed_work_sync(&sc->ath_led_blink_work);
187 	ath_deinit_leds(sc);
188 }
189 
190 /*******************/
191 /*	Rfkill	   */
192 /*******************/
193 
194 static bool ath_is_rfkill_set(struct ath_softc *sc)
195 {
196 	struct ath_hw *ah = sc->sc_ah;
197 
198 	return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
199 				  ah->rfkill_polarity;
200 }
201 
202 void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
203 {
204 	struct ath_wiphy *aphy = hw->priv;
205 	struct ath_softc *sc = aphy->sc;
206 	bool blocked = !!ath_is_rfkill_set(sc);
207 
208 	wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
209 }
210 
211 void ath_start_rfkill_poll(struct ath_softc *sc)
212 {
213 	struct ath_hw *ah = sc->sc_ah;
214 
215 	if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
216 		wiphy_rfkill_start_polling(sc->hw->wiphy);
217 }
218 
219 /******************/
220 /*     BTCOEX     */
221 /******************/
222 
223 /*
224  * Detects if there is any priority bt traffic
225  */
226 static void ath_detect_bt_priority(struct ath_softc *sc)
227 {
228 	struct ath_btcoex *btcoex = &sc->btcoex;
229 	struct ath_hw *ah = sc->sc_ah;
230 
231 	if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
232 		btcoex->bt_priority_cnt++;
233 
234 	if (time_after(jiffies, btcoex->bt_priority_time +
235 			msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
236 		sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
237 		/* Detect if colocated bt started scanning */
238 		if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
239 			ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
240 				  "BT scan detected");
241 			sc->sc_flags |= (SC_OP_BT_SCAN |
242 					 SC_OP_BT_PRIORITY_DETECTED);
243 		} else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
244 			ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
245 				  "BT priority traffic detected");
246 			sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
247 		}
248 
249 		btcoex->bt_priority_cnt = 0;
250 		btcoex->bt_priority_time = jiffies;
251 	}
252 }
253 
254 /*
255  * Configures appropriate weight based on stomp type.
256  */
257 static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
258 				  enum ath_stomp_type stomp_type)
259 {
260 	struct ath_hw *ah = sc->sc_ah;
261 
262 	switch (stomp_type) {
263 	case ATH_BTCOEX_STOMP_ALL:
264 		ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
265 					   AR_STOMP_ALL_WLAN_WGHT);
266 		break;
267 	case ATH_BTCOEX_STOMP_LOW:
268 		ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
269 					   AR_STOMP_LOW_WLAN_WGHT);
270 		break;
271 	case ATH_BTCOEX_STOMP_NONE:
272 		ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
273 					   AR_STOMP_NONE_WLAN_WGHT);
274 		break;
275 	default:
276 		ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
277 			  "Invalid Stomptype\n");
278 		break;
279 	}
280 
281 	ath9k_hw_btcoex_enable(ah);
282 }
283 
284 static void ath9k_gen_timer_start(struct ath_hw *ah,
285 				  struct ath_gen_timer *timer,
286 				  u32 timer_next,
287 				  u32 timer_period)
288 {
289 	ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
290 
291 	if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
292 		ath9k_hw_set_interrupts(ah, 0);
293 		ah->imask |= ATH9K_INT_GENTIMER;
294 		ath9k_hw_set_interrupts(ah, ah->imask);
295 	}
296 }
297 
298 static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
299 {
300 	struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
301 
302 	ath9k_hw_gen_timer_stop(ah, timer);
303 
304 	/* if no timer is enabled, turn off interrupt mask */
305 	if (timer_table->timer_mask.val == 0) {
306 		ath9k_hw_set_interrupts(ah, 0);
307 		ah->imask &= ~ATH9K_INT_GENTIMER;
308 		ath9k_hw_set_interrupts(ah, ah->imask);
309 	}
310 }
311 
312 /*
313  * This is the master bt coex timer which runs for every
314  * 45ms, bt traffic will be given priority during 55% of this
315  * period while wlan gets remaining 45%
316  */
317 static void ath_btcoex_period_timer(unsigned long data)
318 {
319 	struct ath_softc *sc = (struct ath_softc *) data;
320 	struct ath_hw *ah = sc->sc_ah;
321 	struct ath_btcoex *btcoex = &sc->btcoex;
322 	u32 timer_period;
323 	bool is_btscan;
324 
325 	ath_detect_bt_priority(sc);
326 
327 	is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
328 
329 	spin_lock_bh(&btcoex->btcoex_lock);
330 
331 	ath9k_btcoex_bt_stomp(sc, is_btscan ? ATH_BTCOEX_STOMP_ALL :
332 			      btcoex->bt_stomp_type);
333 
334 	spin_unlock_bh(&btcoex->btcoex_lock);
335 
336 	if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
337 		if (btcoex->hw_timer_enabled)
338 			ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
339 
340 		timer_period = is_btscan ? btcoex->btscan_no_stomp :
341 					   btcoex->btcoex_no_stomp;
342 		ath9k_gen_timer_start(ah,
343 				      btcoex->no_stomp_timer,
344 				      (ath9k_hw_gettsf32(ah) +
345 				       timer_period), timer_period * 10);
346 		btcoex->hw_timer_enabled = true;
347 	}
348 
349 	mod_timer(&btcoex->period_timer, jiffies +
350 				  msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
351 }
352 
353 /*
354  * Generic tsf based hw timer which configures weight
355  * registers to time slice between wlan and bt traffic
356  */
357 static void ath_btcoex_no_stomp_timer(void *arg)
358 {
359 	struct ath_softc *sc = (struct ath_softc *)arg;
360 	struct ath_hw *ah = sc->sc_ah;
361 	struct ath_btcoex *btcoex = &sc->btcoex;
362 	bool is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
363 
364 	ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
365 		  "no stomp timer running\n");
366 
367 	spin_lock_bh(&btcoex->btcoex_lock);
368 
369 	if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
370 		ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
371 	 else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
372 		ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
373 
374 	spin_unlock_bh(&btcoex->btcoex_lock);
375 }
376 
377 int ath_init_btcoex_timer(struct ath_softc *sc)
378 {
379 	struct ath_btcoex *btcoex = &sc->btcoex;
380 
381 	btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
382 	btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
383 		btcoex->btcoex_period / 100;
384 	btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
385 				   btcoex->btcoex_period / 100;
386 
387 	setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
388 			(unsigned long) sc);
389 
390 	spin_lock_init(&btcoex->btcoex_lock);
391 
392 	btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
393 			ath_btcoex_no_stomp_timer,
394 			ath_btcoex_no_stomp_timer,
395 			(void *) sc, AR_FIRST_NDP_TIMER);
396 
397 	if (!btcoex->no_stomp_timer)
398 		return -ENOMEM;
399 
400 	return 0;
401 }
402 
403 /*
404  * (Re)start btcoex timers
405  */
406 void ath9k_btcoex_timer_resume(struct ath_softc *sc)
407 {
408 	struct ath_btcoex *btcoex = &sc->btcoex;
409 	struct ath_hw *ah = sc->sc_ah;
410 
411 	ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
412 		  "Starting btcoex timers");
413 
414 	/* make sure duty cycle timer is also stopped when resuming */
415 	if (btcoex->hw_timer_enabled)
416 		ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
417 
418 	btcoex->bt_priority_cnt = 0;
419 	btcoex->bt_priority_time = jiffies;
420 	sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
421 
422 	mod_timer(&btcoex->period_timer, jiffies);
423 }
424 
425 
426 /*
427  * Pause btcoex timer and bt duty cycle timer
428  */
429 void ath9k_btcoex_timer_pause(struct ath_softc *sc)
430 {
431 	struct ath_btcoex *btcoex = &sc->btcoex;
432 	struct ath_hw *ah = sc->sc_ah;
433 
434 	del_timer_sync(&btcoex->period_timer);
435 
436 	if (btcoex->hw_timer_enabled)
437 		ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
438 
439 	btcoex->hw_timer_enabled = false;
440 }
441