gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /*
  218. * Configures appropriate weight based on stomp type.
  219. */
  220. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  221. enum ath_stomp_type stomp_type)
  222. {
  223. struct ath_hw *ah = sc->sc_ah;
  224. switch (stomp_type) {
  225. case ATH_BTCOEX_STOMP_ALL:
  226. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  227. AR_STOMP_ALL_WLAN_WGHT);
  228. break;
  229. case ATH_BTCOEX_STOMP_LOW:
  230. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  231. AR_STOMP_LOW_WLAN_WGHT);
  232. break;
  233. case ATH_BTCOEX_STOMP_NONE:
  234. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  235. AR_STOMP_NONE_WLAN_WGHT);
  236. break;
  237. default:
  238. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  239. "Invalid Stomptype\n");
  240. break;
  241. }
  242. ath9k_hw_btcoex_enable(ah);
  243. }
  244. static void ath9k_gen_timer_start(struct ath_hw *ah,
  245. struct ath_gen_timer *timer,
  246. u32 timer_next,
  247. u32 timer_period)
  248. {
  249. ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
  250. if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
  251. ath9k_hw_set_interrupts(ah, 0);
  252. ah->imask |= ATH9K_INT_GENTIMER;
  253. ath9k_hw_set_interrupts(ah, ah->imask);
  254. }
  255. }
  256. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  257. {
  258. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  259. ath9k_hw_gen_timer_stop(ah, timer);
  260. /* if no timer is enabled, turn off interrupt mask */
  261. if (timer_table->timer_mask.val == 0) {
  262. ath9k_hw_set_interrupts(ah, 0);
  263. ah->imask &= ~ATH9K_INT_GENTIMER;
  264. ath9k_hw_set_interrupts(ah, ah->imask);
  265. }
  266. }
  267. /*
  268. * This is the master bt coex timer which runs for every
  269. * 45ms, bt traffic will be given priority during 55% of this
  270. * period while wlan gets remaining 45%
  271. */
  272. static void ath_btcoex_period_timer(unsigned long data)
  273. {
  274. struct ath_softc *sc = (struct ath_softc *) data;
  275. struct ath_hw *ah = sc->sc_ah;
  276. struct ath_btcoex *btcoex = &sc->btcoex;
  277. u32 timer_period;
  278. bool is_btscan;
  279. ath_detect_bt_priority(sc);
  280. is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  281. spin_lock_bh(&btcoex->btcoex_lock);
  282. ath9k_btcoex_bt_stomp(sc, is_btscan ? ATH_BTCOEX_STOMP_ALL :
  283. btcoex->bt_stomp_type);
  284. spin_unlock_bh(&btcoex->btcoex_lock);
  285. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  286. if (btcoex->hw_timer_enabled)
  287. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  288. timer_period = is_btscan ? btcoex->btscan_no_stomp :
  289. btcoex->btcoex_no_stomp;
  290. ath9k_gen_timer_start(ah,
  291. btcoex->no_stomp_timer,
  292. (ath9k_hw_gettsf32(ah) +
  293. timer_period), timer_period * 10);
  294. btcoex->hw_timer_enabled = true;
  295. }
  296. mod_timer(&btcoex->period_timer, jiffies +
  297. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  298. }
  299. /*
  300. * Generic tsf based hw timer which configures weight
  301. * registers to time slice between wlan and bt traffic
  302. */
  303. static void ath_btcoex_no_stomp_timer(void *arg)
  304. {
  305. struct ath_softc *sc = (struct ath_softc *)arg;
  306. struct ath_hw *ah = sc->sc_ah;
  307. struct ath_btcoex *btcoex = &sc->btcoex;
  308. bool is_btscan = sc->sc_flags & SC_OP_BT_SCAN;
  309. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  310. "no stomp timer running\n");
  311. spin_lock_bh(&btcoex->btcoex_lock);
  312. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
  313. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  314. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  315. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  316. spin_unlock_bh(&btcoex->btcoex_lock);
  317. }
  318. int ath_init_btcoex_timer(struct ath_softc *sc)
  319. {
  320. struct ath_btcoex *btcoex = &sc->btcoex;
  321. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  322. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  323. btcoex->btcoex_period / 100;
  324. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  325. btcoex->btcoex_period / 100;
  326. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  327. (unsigned long) sc);
  328. spin_lock_init(&btcoex->btcoex_lock);
  329. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  330. ath_btcoex_no_stomp_timer,
  331. ath_btcoex_no_stomp_timer,
  332. (void *) sc, AR_FIRST_NDP_TIMER);
  333. if (!btcoex->no_stomp_timer)
  334. return -ENOMEM;
  335. return 0;
  336. }
  337. /*
  338. * (Re)start btcoex timers
  339. */
  340. void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  341. {
  342. struct ath_btcoex *btcoex = &sc->btcoex;
  343. struct ath_hw *ah = sc->sc_ah;
  344. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  345. "Starting btcoex timers");
  346. /* make sure duty cycle timer is also stopped when resuming */
  347. if (btcoex->hw_timer_enabled)
  348. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  349. btcoex->bt_priority_cnt = 0;
  350. btcoex->bt_priority_time = jiffies;
  351. sc->sc_flags &= ~(SC_OP_BT_PRIORITY_DETECTED | SC_OP_BT_SCAN);
  352. mod_timer(&btcoex->period_timer, jiffies);
  353. }
  354. /*
  355. * Pause btcoex timer and bt duty cycle timer
  356. */
  357. void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  358. {
  359. struct ath_btcoex *btcoex = &sc->btcoex;
  360. struct ath_hw *ah = sc->sc_ah;
  361. del_timer_sync(&btcoex->period_timer);
  362. if (btcoex->hw_timer_enabled)
  363. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  364. btcoex->hw_timer_enabled = false;
  365. }