gpio.c 11 KB

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