gpio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include "ath9k.h"
  17. /********************************/
  18. /* LED functions */
  19. /********************************/
  20. #ifdef CONFIG_MAC80211_LEDS
  21. static void ath_led_brightness(struct led_classdev *led_cdev,
  22. enum led_brightness brightness)
  23. {
  24. struct ath_softc *sc = container_of(led_cdev, struct ath_softc, led_cdev);
  25. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, (brightness == LED_OFF));
  26. }
  27. void ath_deinit_leds(struct ath_softc *sc)
  28. {
  29. if (!sc->led_registered)
  30. return;
  31. ath_led_brightness(&sc->led_cdev, LED_OFF);
  32. led_classdev_unregister(&sc->led_cdev);
  33. }
  34. void ath_init_leds(struct ath_softc *sc)
  35. {
  36. int ret;
  37. if (AR_SREV_9287(sc->sc_ah))
  38. sc->sc_ah->led_pin = ATH_LED_PIN_9287;
  39. else if (AR_SREV_9485(sc->sc_ah))
  40. sc->sc_ah->led_pin = ATH_LED_PIN_9485;
  41. else
  42. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  43. /* Configure gpio 1 for output */
  44. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  45. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  46. /* LED off, active low */
  47. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  48. if (!led_blink)
  49. sc->led_cdev.default_trigger =
  50. ieee80211_get_radio_led_name(sc->hw);
  51. snprintf(sc->led_name, sizeof(sc->led_name),
  52. "ath9k-%s", wiphy_name(sc->hw->wiphy));
  53. sc->led_cdev.name = sc->led_name;
  54. sc->led_cdev.brightness_set = ath_led_brightness;
  55. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
  56. if (ret < 0)
  57. return;
  58. sc->led_registered = true;
  59. }
  60. #endif
  61. /*******************/
  62. /* Rfkill */
  63. /*******************/
  64. static bool ath_is_rfkill_set(struct ath_softc *sc)
  65. {
  66. struct ath_hw *ah = sc->sc_ah;
  67. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  68. ah->rfkill_polarity;
  69. }
  70. void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  71. {
  72. struct ath_softc *sc = hw->priv;
  73. bool blocked = !!ath_is_rfkill_set(sc);
  74. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  75. }
  76. void ath_start_rfkill_poll(struct ath_softc *sc)
  77. {
  78. struct ath_hw *ah = sc->sc_ah;
  79. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  80. wiphy_rfkill_start_polling(sc->hw->wiphy);
  81. }
  82. /******************/
  83. /* BTCOEX */
  84. /******************/
  85. /*
  86. * Detects if there is any priority bt traffic
  87. */
  88. static void ath_detect_bt_priority(struct ath_softc *sc)
  89. {
  90. struct ath_btcoex *btcoex = &sc->btcoex;
  91. struct ath_hw *ah = sc->sc_ah;
  92. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  93. btcoex->bt_priority_cnt++;
  94. if (time_after(jiffies, btcoex->bt_priority_time +
  95. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  96. sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
  97. /* Detect if colocated bt started scanning */
  98. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
  99. ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
  100. "BT scan detected\n");
  101. sc->sc_flags |= (SC_OP_BT_SCAN |
  102. SC_OP_BT_PRIORITY_DETECTED);
  103. } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  104. ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
  105. "BT priority traffic detected\n");
  106. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  107. }
  108. btcoex->bt_priority_cnt = 0;
  109. btcoex->bt_priority_time = jiffies;
  110. }
  111. }
  112. static void ath9k_gen_timer_start(struct ath_hw *ah,
  113. struct ath_gen_timer *timer,
  114. u32 timer_next,
  115. u32 timer_period)
  116. {
  117. ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
  118. if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
  119. ath9k_hw_disable_interrupts(ah);
  120. ah->imask |= ATH9K_INT_GENTIMER;
  121. ath9k_hw_set_interrupts(ah, ah->imask);
  122. }
  123. }
  124. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  125. {
  126. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  127. ath9k_hw_gen_timer_stop(ah, timer);
  128. /* if no timer is enabled, turn off interrupt mask */
  129. if (timer_table->timer_mask.val == 0) {
  130. ath9k_hw_disable_interrupts(ah);
  131. ah->imask &= ~ATH9K_INT_GENTIMER;
  132. ath9k_hw_set_interrupts(ah, ah->imask);
  133. }
  134. }
  135. /*
  136. * This is the master bt coex timer which runs for every
  137. * 45ms, bt traffic will be given priority during 55% of this
  138. * period while wlan gets remaining 45%
  139. */
  140. static void ath_btcoex_period_timer(unsigned long data)
  141. {
  142. struct ath_softc *sc = (struct ath_softc *) data;
  143. struct ath_hw *ah = sc->sc_ah;
  144. struct ath_btcoex *btcoex = &sc->btcoex;
  145. struct ath_common *common = ath9k_hw_common(ah);
  146. u32 timer_period;
  147. bool is_btscan;
  148. ath_detect_bt_priority(sc);
  149. is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  150. spin_lock_bh(&btcoex->btcoex_lock);
  151. ath9k_cmn_btcoex_bt_stomp(common, is_btscan ? ATH_BTCOEX_STOMP_ALL :
  152. btcoex->bt_stomp_type);
  153. spin_unlock_bh(&btcoex->btcoex_lock);
  154. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  155. if (btcoex->hw_timer_enabled)
  156. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  157. timer_period = is_btscan ? btcoex->btscan_no_stomp :
  158. btcoex->btcoex_no_stomp;
  159. ath9k_gen_timer_start(ah, btcoex->no_stomp_timer, 0,
  160. timer_period * 10);
  161. btcoex->hw_timer_enabled = true;
  162. }
  163. mod_timer(&btcoex->period_timer, jiffies +
  164. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  165. }
  166. /*
  167. * Generic tsf based hw timer which configures weight
  168. * registers to time slice between wlan and bt traffic
  169. */
  170. static void ath_btcoex_no_stomp_timer(void *arg)
  171. {
  172. struct ath_softc *sc = (struct ath_softc *)arg;
  173. struct ath_hw *ah = sc->sc_ah;
  174. struct ath_btcoex *btcoex = &sc->btcoex;
  175. struct ath_common *common = ath9k_hw_common(ah);
  176. bool is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  177. ath_dbg(common, ATH_DBG_BTCOEX,
  178. "no stomp timer running\n");
  179. spin_lock_bh(&btcoex->btcoex_lock);
  180. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
  181. ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_NONE);
  182. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  183. ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_LOW);
  184. spin_unlock_bh(&btcoex->btcoex_lock);
  185. }
  186. int ath_init_btcoex_timer(struct ath_softc *sc)
  187. {
  188. struct ath_btcoex *btcoex = &sc->btcoex;
  189. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  190. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  191. btcoex->btcoex_period / 100;
  192. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  193. btcoex->btcoex_period / 100;
  194. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  195. (unsigned long) sc);
  196. spin_lock_init(&btcoex->btcoex_lock);
  197. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  198. ath_btcoex_no_stomp_timer,
  199. ath_btcoex_no_stomp_timer,
  200. (void *) sc, AR_FIRST_NDP_TIMER);
  201. if (!btcoex->no_stomp_timer)
  202. return -ENOMEM;
  203. return 0;
  204. }
  205. /*
  206. * (Re)start btcoex timers
  207. */
  208. void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  209. {
  210. struct ath_btcoex *btcoex = &sc->btcoex;
  211. struct ath_hw *ah = sc->sc_ah;
  212. ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  213. "Starting btcoex timers\n");
  214. /* make sure duty cycle timer is also stopped when resuming */
  215. if (btcoex->hw_timer_enabled)
  216. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  217. btcoex->bt_priority_cnt = 0;
  218. btcoex->bt_priority_time = jiffies;
  219. sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
  220. mod_timer(&btcoex->period_timer, jiffies);
  221. }
  222. /*
  223. * Pause btcoex timer and bt duty cycle timer
  224. */
  225. void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  226. {
  227. struct ath_btcoex *btcoex = &sc->btcoex;
  228. struct ath_hw *ah = sc->sc_ah;
  229. del_timer_sync(&btcoex->period_timer);
  230. if (btcoex->hw_timer_enabled)
  231. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  232. btcoex->hw_timer_enabled = false;
  233. }