gpio.c 12 KB

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