gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 if (AR_SREV_9462(sc->sc_ah))
  45. sc->sc_ah->led_pin = ATH_LED_PIN_9462;
  46. else
  47. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  48. }
  49. /* Configure gpio 1 for output */
  50. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  51. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  52. /* LED off, active low */
  53. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  54. if (!led_blink)
  55. sc->led_cdev.default_trigger =
  56. ieee80211_get_radio_led_name(sc->hw);
  57. snprintf(sc->led_name, sizeof(sc->led_name),
  58. "ath9k-%s", wiphy_name(sc->hw->wiphy));
  59. sc->led_cdev.name = sc->led_name;
  60. sc->led_cdev.brightness_set = ath_led_brightness;
  61. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
  62. if (ret < 0)
  63. return;
  64. sc->led_registered = true;
  65. }
  66. #endif
  67. /*******************/
  68. /* Rfkill */
  69. /*******************/
  70. static bool ath_is_rfkill_set(struct ath_softc *sc)
  71. {
  72. struct ath_hw *ah = sc->sc_ah;
  73. bool is_blocked;
  74. ath9k_ps_wakeup(sc);
  75. is_blocked = ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  76. ah->rfkill_polarity;
  77. ath9k_ps_restore(sc);
  78. return is_blocked;
  79. }
  80. void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  81. {
  82. struct ath_softc *sc = hw->priv;
  83. bool blocked = !!ath_is_rfkill_set(sc);
  84. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  85. }
  86. void ath_start_rfkill_poll(struct ath_softc *sc)
  87. {
  88. struct ath_hw *ah = sc->sc_ah;
  89. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  90. wiphy_rfkill_start_polling(sc->hw->wiphy);
  91. }
  92. /******************/
  93. /* BTCOEX */
  94. /******************/
  95. /*
  96. * Detects if there is any priority bt traffic
  97. */
  98. static void ath_detect_bt_priority(struct ath_softc *sc)
  99. {
  100. struct ath_btcoex *btcoex = &sc->btcoex;
  101. struct ath_hw *ah = sc->sc_ah;
  102. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  103. btcoex->bt_priority_cnt++;
  104. if (time_after(jiffies, btcoex->bt_priority_time +
  105. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  106. sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
  107. /* Detect if colocated bt started scanning */
  108. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
  109. ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
  110. "BT scan detected\n");
  111. sc->sc_flags |= (SC_OP_BT_SCAN |
  112. SC_OP_BT_PRIORITY_DETECTED);
  113. } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  114. ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
  115. "BT priority traffic detected\n");
  116. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  117. }
  118. btcoex->bt_priority_cnt = 0;
  119. btcoex->bt_priority_time = jiffies;
  120. }
  121. }
  122. static void ath9k_gen_timer_start(struct ath_hw *ah,
  123. struct ath_gen_timer *timer,
  124. u32 trig_timeout,
  125. u32 timer_period)
  126. {
  127. ath9k_hw_gen_timer_start(ah, timer, trig_timeout, timer_period);
  128. if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
  129. ath9k_hw_disable_interrupts(ah);
  130. ah->imask |= ATH9K_INT_GENTIMER;
  131. ath9k_hw_set_interrupts(ah);
  132. ath9k_hw_enable_interrupts(ah);
  133. }
  134. }
  135. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  136. {
  137. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  138. ath9k_hw_gen_timer_stop(ah, timer);
  139. /* if no timer is enabled, turn off interrupt mask */
  140. if (timer_table->timer_mask.val == 0) {
  141. ath9k_hw_disable_interrupts(ah);
  142. ah->imask &= ~ATH9K_INT_GENTIMER;
  143. ath9k_hw_set_interrupts(ah);
  144. ath9k_hw_enable_interrupts(ah);
  145. }
  146. }
  147. /*
  148. * This is the master bt coex timer which runs for every
  149. * 45ms, bt traffic will be given priority during 55% of this
  150. * period while wlan gets remaining 45%
  151. */
  152. static void ath_btcoex_period_timer(unsigned long data)
  153. {
  154. struct ath_softc *sc = (struct ath_softc *) data;
  155. struct ath_hw *ah = sc->sc_ah;
  156. struct ath_btcoex *btcoex = &sc->btcoex;
  157. u32 timer_period;
  158. bool is_btscan;
  159. ath9k_ps_wakeup(sc);
  160. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
  161. ath_detect_bt_priority(sc);
  162. is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  163. spin_lock_bh(&btcoex->btcoex_lock);
  164. ath9k_hw_btcoex_bt_stomp(ah, is_btscan ? ATH_BTCOEX_STOMP_ALL :
  165. btcoex->bt_stomp_type);
  166. ath9k_hw_btcoex_enable(ah);
  167. spin_unlock_bh(&btcoex->btcoex_lock);
  168. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  169. if (btcoex->hw_timer_enabled)
  170. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  171. timer_period = is_btscan ? btcoex->btscan_no_stomp :
  172. btcoex->btcoex_no_stomp;
  173. ath9k_gen_timer_start(ah, btcoex->no_stomp_timer, timer_period,
  174. timer_period * 10);
  175. btcoex->hw_timer_enabled = true;
  176. }
  177. ath9k_ps_restore(sc);
  178. timer_period = btcoex->btcoex_period / 1000;
  179. mod_timer(&btcoex->period_timer, jiffies +
  180. msecs_to_jiffies(timer_period));
  181. }
  182. /*
  183. * Generic tsf based hw timer which configures weight
  184. * registers to time slice between wlan and bt traffic
  185. */
  186. static void ath_btcoex_no_stomp_timer(void *arg)
  187. {
  188. struct ath_softc *sc = (struct ath_softc *)arg;
  189. struct ath_hw *ah = sc->sc_ah;
  190. struct ath_btcoex *btcoex = &sc->btcoex;
  191. struct ath_common *common = ath9k_hw_common(ah);
  192. bool is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  193. ath_dbg(common, BTCOEX, "no stomp timer running\n");
  194. ath9k_ps_wakeup(sc);
  195. spin_lock_bh(&btcoex->btcoex_lock);
  196. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
  197. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
  198. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  199. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
  200. ath9k_hw_btcoex_enable(ah);
  201. spin_unlock_bh(&btcoex->btcoex_lock);
  202. ath9k_ps_restore(sc);
  203. }
  204. static int ath_init_btcoex_timer(struct ath_softc *sc)
  205. {
  206. struct ath_btcoex *btcoex = &sc->btcoex;
  207. if (ath9k_hw_get_btcoex_scheme(sc->sc_ah) == ATH_BTCOEX_CFG_NONE)
  208. return 0;
  209. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  210. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  211. btcoex->btcoex_period / 100;
  212. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  213. btcoex->btcoex_period / 100;
  214. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  215. (unsigned long) sc);
  216. spin_lock_init(&btcoex->btcoex_lock);
  217. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  218. ath_btcoex_no_stomp_timer,
  219. ath_btcoex_no_stomp_timer,
  220. (void *) sc, AR_FIRST_NDP_TIMER);
  221. if (!btcoex->no_stomp_timer)
  222. return -ENOMEM;
  223. return 0;
  224. }
  225. /*
  226. * (Re)start btcoex timers
  227. */
  228. void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  229. {
  230. struct ath_btcoex *btcoex = &sc->btcoex;
  231. struct ath_hw *ah = sc->sc_ah;
  232. ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex timers\n");
  233. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
  234. return;
  235. /* make sure duty cycle timer is also stopped when resuming */
  236. if (btcoex->hw_timer_enabled)
  237. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  238. btcoex->bt_priority_cnt = 0;
  239. btcoex->bt_priority_time = jiffies;
  240. sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
  241. mod_timer(&btcoex->period_timer, jiffies);
  242. }
  243. /*
  244. * Pause btcoex timer and bt duty cycle timer
  245. */
  246. void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  247. {
  248. struct ath_btcoex *btcoex = &sc->btcoex;
  249. struct ath_hw *ah = sc->sc_ah;
  250. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
  251. return;
  252. del_timer_sync(&btcoex->period_timer);
  253. if (btcoex->hw_timer_enabled)
  254. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  255. btcoex->hw_timer_enabled = false;
  256. }
  257. u16 ath9k_btcoex_aggr_limit(struct ath_softc *sc, u32 max_4ms_framelen)
  258. {
  259. struct ath_mci_profile *mci = &sc->btcoex.mci;
  260. u16 aggr_limit = 0;
  261. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI) && mci->aggr_limit)
  262. aggr_limit = (max_4ms_framelen * mci->aggr_limit) >> 4;
  263. else if (sc->sc_flags & SC_OP_BT_PRIORITY_DETECTED)
  264. aggr_limit = min((max_4ms_framelen * 3) / 8,
  265. (u32)ATH_AMPDU_LIMIT_MAX);
  266. return aggr_limit;
  267. }
  268. void ath9k_btcoex_handle_interrupt(struct ath_softc *sc, u32 status)
  269. {
  270. struct ath_hw *ah = sc->sc_ah;
  271. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
  272. if (status & ATH9K_INT_GENTIMER)
  273. ath_gen_timer_isr(sc->sc_ah);
  274. if ((status & ATH9K_INT_MCI) && ATH9K_HW_CAP_MCI)
  275. ath_mci_intr(sc);
  276. }
  277. void ath9k_start_btcoex(struct ath_softc *sc)
  278. {
  279. struct ath_hw *ah = sc->sc_ah;
  280. if ((ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) &&
  281. !ah->btcoex_hw.enabled) {
  282. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
  283. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  284. AR_STOMP_LOW_WLAN_WGHT);
  285. ath9k_hw_btcoex_enable(ah);
  286. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
  287. ath9k_btcoex_timer_resume(sc);
  288. }
  289. }
  290. void ath9k_stop_btcoex(struct ath_softc *sc)
  291. {
  292. struct ath_hw *ah = sc->sc_ah;
  293. if (ah->btcoex_hw.enabled &&
  294. ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) {
  295. ath9k_hw_btcoex_disable(ah);
  296. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
  297. ath9k_btcoex_timer_pause(sc);
  298. ath_mci_flush_profile(&sc->btcoex.mci);
  299. }
  300. }
  301. void ath9k_deinit_btcoex(struct ath_softc *sc)
  302. {
  303. if ((sc->btcoex.no_stomp_timer) &&
  304. ath9k_hw_get_btcoex_scheme(sc->sc_ah) == ATH_BTCOEX_CFG_3WIRE)
  305. ath_gen_timer_free(sc->sc_ah, sc->btcoex.no_stomp_timer);
  306. if (ath9k_hw_get_btcoex_scheme(sc->sc_ah) == ATH_BTCOEX_CFG_MCI)
  307. ath_mci_cleanup(sc);
  308. }
  309. int ath9k_init_btcoex(struct ath_softc *sc)
  310. {
  311. struct ath_txq *txq;
  312. struct ath_hw *ah = sc->sc_ah;
  313. int r;
  314. switch (ath9k_hw_get_btcoex_scheme(sc->sc_ah)) {
  315. case ATH_BTCOEX_CFG_NONE:
  316. break;
  317. case ATH_BTCOEX_CFG_2WIRE:
  318. ath9k_hw_btcoex_init_2wire(sc->sc_ah);
  319. break;
  320. case ATH_BTCOEX_CFG_3WIRE:
  321. ath9k_hw_btcoex_init_3wire(sc->sc_ah);
  322. r = ath_init_btcoex_timer(sc);
  323. if (r)
  324. return -1;
  325. txq = sc->tx.txq_map[WME_AC_BE];
  326. ath9k_hw_init_btcoex_hw(sc->sc_ah, txq->axq_qnum);
  327. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  328. break;
  329. case ATH_BTCOEX_CFG_MCI:
  330. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  331. sc->btcoex.duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
  332. INIT_LIST_HEAD(&sc->btcoex.mci.info);
  333. r = ath_mci_setup(sc);
  334. if (r)
  335. return r;
  336. ath9k_hw_btcoex_init_mci(ah);
  337. break;
  338. default:
  339. WARN_ON(1);
  340. break;
  341. }
  342. return 0;
  343. }