gpio.c 8.1 KB

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