gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 (AR_SREV_9100(sc->sc_ah))
  38. return;
  39. if (!led_blink)
  40. sc->led_cdev.default_trigger =
  41. ieee80211_get_radio_led_name(sc->hw);
  42. snprintf(sc->led_name, sizeof(sc->led_name),
  43. "ath9k-%s", wiphy_name(sc->hw->wiphy));
  44. sc->led_cdev.name = sc->led_name;
  45. sc->led_cdev.brightness_set = ath_led_brightness;
  46. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &sc->led_cdev);
  47. if (ret < 0)
  48. return;
  49. sc->led_registered = true;
  50. }
  51. void ath_fill_led_pin(struct ath_softc *sc)
  52. {
  53. struct ath_hw *ah = sc->sc_ah;
  54. if (AR_SREV_9100(ah) || (ah->led_pin >= 0))
  55. return;
  56. if (AR_SREV_9287(ah))
  57. ah->led_pin = ATH_LED_PIN_9287;
  58. else if (AR_SREV_9485(sc->sc_ah))
  59. ah->led_pin = ATH_LED_PIN_9485;
  60. else if (AR_SREV_9300(sc->sc_ah))
  61. ah->led_pin = ATH_LED_PIN_9300;
  62. else if (AR_SREV_9462(sc->sc_ah) || AR_SREV_9565(sc->sc_ah))
  63. ah->led_pin = ATH_LED_PIN_9462;
  64. else
  65. ah->led_pin = ATH_LED_PIN_DEF;
  66. /* Configure gpio 1 for output */
  67. ath9k_hw_cfg_output(ah, ah->led_pin, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  68. /* LED off, active low */
  69. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  70. }
  71. #endif
  72. /*******************/
  73. /* Rfkill */
  74. /*******************/
  75. static bool ath_is_rfkill_set(struct ath_softc *sc)
  76. {
  77. struct ath_hw *ah = sc->sc_ah;
  78. bool is_blocked;
  79. ath9k_ps_wakeup(sc);
  80. is_blocked = ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  81. ah->rfkill_polarity;
  82. ath9k_ps_restore(sc);
  83. return is_blocked;
  84. }
  85. void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  86. {
  87. struct ath_softc *sc = hw->priv;
  88. bool blocked = !!ath_is_rfkill_set(sc);
  89. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  90. }
  91. void ath_start_rfkill_poll(struct ath_softc *sc)
  92. {
  93. struct ath_hw *ah = sc->sc_ah;
  94. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  95. wiphy_rfkill_start_polling(sc->hw->wiphy);
  96. }
  97. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  98. /******************/
  99. /* BTCOEX */
  100. /******************/
  101. /*
  102. * Detects if there is any priority bt traffic
  103. */
  104. static void ath_detect_bt_priority(struct ath_softc *sc)
  105. {
  106. struct ath_btcoex *btcoex = &sc->btcoex;
  107. struct ath_hw *ah = sc->sc_ah;
  108. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  109. btcoex->bt_priority_cnt++;
  110. if (time_after(jiffies, btcoex->bt_priority_time +
  111. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  112. clear_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
  113. clear_bit(BT_OP_SCAN, &btcoex->op_flags);
  114. /* Detect if colocated bt started scanning */
  115. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
  116. ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
  117. "BT scan detected\n");
  118. set_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
  119. set_bit(BT_OP_SCAN, &btcoex->op_flags);
  120. } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  121. ath_dbg(ath9k_hw_common(sc->sc_ah), BTCOEX,
  122. "BT priority traffic detected\n");
  123. set_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
  124. }
  125. btcoex->bt_priority_cnt = 0;
  126. btcoex->bt_priority_time = jiffies;
  127. }
  128. }
  129. static void ath_mci_ftp_adjust(struct ath_softc *sc)
  130. {
  131. struct ath_btcoex *btcoex = &sc->btcoex;
  132. struct ath_mci_profile *mci = &btcoex->mci;
  133. struct ath_hw *ah = sc->sc_ah;
  134. if (btcoex->bt_wait_time > ATH_BTCOEX_RX_WAIT_TIME) {
  135. if (ar9003_mci_state(ah, MCI_STATE_NEED_FTP_STOMP) &&
  136. (mci->num_pan || mci->num_other_acl))
  137. ah->btcoex_hw.mci.stomp_ftp =
  138. (sc->rx.num_pkts < ATH_BTCOEX_STOMP_FTP_THRESH);
  139. else
  140. ah->btcoex_hw.mci.stomp_ftp = false;
  141. btcoex->bt_wait_time = 0;
  142. sc->rx.num_pkts = 0;
  143. }
  144. }
  145. /*
  146. * This is the master bt coex timer which runs for every
  147. * 45ms, bt traffic will be given priority during 55% of this
  148. * period while wlan gets remaining 45%
  149. */
  150. static void ath_btcoex_period_timer(unsigned long data)
  151. {
  152. struct ath_softc *sc = (struct ath_softc *) data;
  153. struct ath_hw *ah = sc->sc_ah;
  154. struct ath_btcoex *btcoex = &sc->btcoex;
  155. enum ath_stomp_type stomp_type;
  156. u32 timer_period;
  157. unsigned long flags;
  158. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  159. if (sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP) {
  160. btcoex->bt_wait_time += btcoex->btcoex_period;
  161. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  162. goto skip_hw_wakeup;
  163. }
  164. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  165. ath9k_mci_update_rssi(sc);
  166. ath9k_ps_wakeup(sc);
  167. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
  168. ath_detect_bt_priority(sc);
  169. if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
  170. ath_mci_ftp_adjust(sc);
  171. spin_lock_bh(&btcoex->btcoex_lock);
  172. stomp_type = btcoex->bt_stomp_type;
  173. timer_period = btcoex->btcoex_no_stomp;
  174. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI)) {
  175. if (test_bit(BT_OP_SCAN, &btcoex->op_flags)) {
  176. stomp_type = ATH_BTCOEX_STOMP_ALL;
  177. timer_period = btcoex->btscan_no_stomp;
  178. }
  179. } else if (btcoex->stomp_audio >= 5) {
  180. stomp_type = ATH_BTCOEX_STOMP_AUDIO;
  181. btcoex->stomp_audio = 0;
  182. }
  183. ath9k_hw_btcoex_bt_stomp(ah, stomp_type);
  184. ath9k_hw_btcoex_enable(ah);
  185. spin_unlock_bh(&btcoex->btcoex_lock);
  186. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp)
  187. mod_timer(&btcoex->no_stomp_timer,
  188. jiffies + msecs_to_jiffies(timer_period));
  189. ath9k_ps_restore(sc);
  190. skip_hw_wakeup:
  191. mod_timer(&btcoex->period_timer,
  192. jiffies + msecs_to_jiffies(btcoex->btcoex_period));
  193. }
  194. /*
  195. * Generic tsf based hw timer which configures weight
  196. * registers to time slice between wlan and bt traffic
  197. */
  198. static void ath_btcoex_no_stomp_timer(unsigned long arg)
  199. {
  200. struct ath_softc *sc = (struct ath_softc *)arg;
  201. struct ath_hw *ah = sc->sc_ah;
  202. struct ath_btcoex *btcoex = &sc->btcoex;
  203. struct ath_common *common = ath9k_hw_common(ah);
  204. ath_dbg(common, BTCOEX, "no stomp timer running\n");
  205. ath9k_ps_wakeup(sc);
  206. spin_lock_bh(&btcoex->btcoex_lock);
  207. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW ||
  208. (!(ah->caps.hw_caps & ATH9K_HW_CAP_MCI) &&
  209. test_bit(BT_OP_SCAN, &btcoex->op_flags)))
  210. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
  211. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  212. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
  213. ath9k_hw_btcoex_enable(ah);
  214. spin_unlock_bh(&btcoex->btcoex_lock);
  215. ath9k_ps_restore(sc);
  216. }
  217. static int ath_init_btcoex_timer(struct ath_softc *sc)
  218. {
  219. struct ath_btcoex *btcoex = &sc->btcoex;
  220. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
  221. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  222. btcoex->btcoex_period / 100;
  223. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  224. btcoex->btcoex_period / 100;
  225. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  226. (unsigned long) sc);
  227. setup_timer(&btcoex->no_stomp_timer, ath_btcoex_no_stomp_timer,
  228. (unsigned long) sc);
  229. spin_lock_init(&btcoex->btcoex_lock);
  230. return 0;
  231. }
  232. /*
  233. * (Re)start btcoex timers
  234. */
  235. void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  236. {
  237. struct ath_btcoex *btcoex = &sc->btcoex;
  238. struct ath_hw *ah = sc->sc_ah;
  239. ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex timers\n");
  240. /* make sure duty cycle timer is also stopped when resuming */
  241. del_timer_sync(&btcoex->no_stomp_timer);
  242. btcoex->bt_priority_cnt = 0;
  243. btcoex->bt_priority_time = jiffies;
  244. clear_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags);
  245. clear_bit(BT_OP_SCAN, &btcoex->op_flags);
  246. mod_timer(&btcoex->period_timer, jiffies);
  247. }
  248. /*
  249. * Pause btcoex timer and bt duty cycle timer
  250. */
  251. void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  252. {
  253. struct ath_btcoex *btcoex = &sc->btcoex;
  254. del_timer_sync(&btcoex->period_timer);
  255. del_timer_sync(&btcoex->no_stomp_timer);
  256. }
  257. void ath9k_btcoex_stop_gen_timer(struct ath_softc *sc)
  258. {
  259. struct ath_btcoex *btcoex = &sc->btcoex;
  260. del_timer_sync(&btcoex->no_stomp_timer);
  261. }
  262. u16 ath9k_btcoex_aggr_limit(struct ath_softc *sc, u32 max_4ms_framelen)
  263. {
  264. struct ath_btcoex *btcoex = &sc->btcoex;
  265. struct ath_mci_profile *mci = &sc->btcoex.mci;
  266. u16 aggr_limit = 0;
  267. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI) && mci->aggr_limit)
  268. aggr_limit = (max_4ms_framelen * mci->aggr_limit) >> 4;
  269. else if (test_bit(BT_OP_PRIORITY_DETECTED, &btcoex->op_flags))
  270. aggr_limit = min((max_4ms_framelen * 3) / 8,
  271. (u32)ATH_AMPDU_LIMIT_MAX);
  272. return aggr_limit;
  273. }
  274. void ath9k_btcoex_handle_interrupt(struct ath_softc *sc, u32 status)
  275. {
  276. if (status & ATH9K_INT_MCI)
  277. ath_mci_intr(sc);
  278. }
  279. void ath9k_start_btcoex(struct ath_softc *sc)
  280. {
  281. struct ath_hw *ah = sc->sc_ah;
  282. if ((ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) &&
  283. !ah->btcoex_hw.enabled) {
  284. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI))
  285. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  286. AR_STOMP_LOW_WLAN_WGHT, 0);
  287. else
  288. ath9k_hw_btcoex_set_weight(ah, 0, 0,
  289. ATH_BTCOEX_STOMP_NONE);
  290. ath9k_hw_btcoex_enable(ah);
  291. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
  292. ath9k_btcoex_timer_resume(sc);
  293. }
  294. }
  295. void ath9k_stop_btcoex(struct ath_softc *sc)
  296. {
  297. struct ath_hw *ah = sc->sc_ah;
  298. if (ah->btcoex_hw.enabled &&
  299. ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE) {
  300. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_3WIRE)
  301. ath9k_btcoex_timer_pause(sc);
  302. ath9k_hw_btcoex_disable(ah);
  303. if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
  304. ath_mci_flush_profile(&sc->btcoex.mci);
  305. }
  306. }
  307. void ath9k_deinit_btcoex(struct ath_softc *sc)
  308. {
  309. struct ath_hw *ah = sc->sc_ah;
  310. if (ath9k_hw_mci_is_enabled(ah))
  311. ath_mci_cleanup(sc);
  312. }
  313. int ath9k_init_btcoex(struct ath_softc *sc)
  314. {
  315. struct ath_txq *txq;
  316. struct ath_hw *ah = sc->sc_ah;
  317. int r;
  318. ath9k_hw_btcoex_init_scheme(ah);
  319. switch (ath9k_hw_get_btcoex_scheme(sc->sc_ah)) {
  320. case ATH_BTCOEX_CFG_NONE:
  321. break;
  322. case ATH_BTCOEX_CFG_2WIRE:
  323. ath9k_hw_btcoex_init_2wire(sc->sc_ah);
  324. break;
  325. case ATH_BTCOEX_CFG_3WIRE:
  326. ath9k_hw_btcoex_init_3wire(sc->sc_ah);
  327. r = ath_init_btcoex_timer(sc);
  328. if (r)
  329. return -1;
  330. txq = sc->tx.txq_map[IEEE80211_AC_BE];
  331. ath9k_hw_init_btcoex_hw(sc->sc_ah, txq->axq_qnum);
  332. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  333. if (ath9k_hw_mci_is_enabled(ah)) {
  334. sc->btcoex.duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
  335. INIT_LIST_HEAD(&sc->btcoex.mci.info);
  336. r = ath_mci_setup(sc);
  337. if (r)
  338. return r;
  339. ath9k_hw_btcoex_init_mci(ah);
  340. }
  341. break;
  342. default:
  343. WARN_ON(1);
  344. break;
  345. }
  346. return 0;
  347. }
  348. static int ath9k_dump_mci_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
  349. {
  350. struct ath_btcoex *btcoex = &sc->btcoex;
  351. struct ath_mci_profile *mci = &btcoex->mci;
  352. struct ath_hw *ah = sc->sc_ah;
  353. struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
  354. u32 len = 0;
  355. int i;
  356. ATH_DUMP_BTCOEX("Total BT profiles", NUM_PROF(mci));
  357. ATH_DUMP_BTCOEX("MGMT", mci->num_mgmt);
  358. ATH_DUMP_BTCOEX("SCO", mci->num_sco);
  359. ATH_DUMP_BTCOEX("A2DP", mci->num_a2dp);
  360. ATH_DUMP_BTCOEX("HID", mci->num_hid);
  361. ATH_DUMP_BTCOEX("PAN", mci->num_pan);
  362. ATH_DUMP_BTCOEX("ACL", mci->num_other_acl);
  363. ATH_DUMP_BTCOEX("BDR", mci->num_bdr);
  364. ATH_DUMP_BTCOEX("Aggr. Limit", mci->aggr_limit);
  365. ATH_DUMP_BTCOEX("Stomp Type", btcoex->bt_stomp_type);
  366. ATH_DUMP_BTCOEX("BTCoex Period (msec)", btcoex->btcoex_period);
  367. ATH_DUMP_BTCOEX("Duty Cycle", btcoex->duty_cycle);
  368. ATH_DUMP_BTCOEX("BT Wait time", btcoex->bt_wait_time);
  369. ATH_DUMP_BTCOEX("Concurrent Tx", btcoex_hw->mci.concur_tx);
  370. ATH_DUMP_BTCOEX("Concurrent RSSI cnt", btcoex->rssi_count);
  371. len += scnprintf(buf + len, size - len, "BT Weights: ");
  372. for (i = 0; i < AR9300_NUM_BT_WEIGHTS; i++)
  373. len += scnprintf(buf + len, size - len, "%08x ",
  374. btcoex_hw->bt_weight[i]);
  375. len += scnprintf(buf + len, size - len, "\n");
  376. len += scnprintf(buf + len, size - len, "WLAN Weights: ");
  377. for (i = 0; i < AR9300_NUM_BT_WEIGHTS; i++)
  378. len += scnprintf(buf + len, size - len, "%08x ",
  379. btcoex_hw->wlan_weight[i]);
  380. len += scnprintf(buf + len, size - len, "\n");
  381. len += scnprintf(buf + len, size - len, "Tx Priorities: ");
  382. for (i = 0; i < ATH_BTCOEX_STOMP_MAX; i++)
  383. len += scnprintf(buf + len, size - len, "%08x ",
  384. btcoex_hw->tx_prio[i]);
  385. len += scnprintf(buf + len, size - len, "\n");
  386. return len;
  387. }
  388. static int ath9k_dump_legacy_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
  389. {
  390. struct ath_btcoex *btcoex = &sc->btcoex;
  391. u32 len = 0;
  392. ATH_DUMP_BTCOEX("Stomp Type", btcoex->bt_stomp_type);
  393. ATH_DUMP_BTCOEX("BTCoex Period (msec)", btcoex->btcoex_period);
  394. ATH_DUMP_BTCOEX("Duty Cycle", btcoex->duty_cycle);
  395. ATH_DUMP_BTCOEX("BT Wait time", btcoex->bt_wait_time);
  396. return len;
  397. }
  398. int ath9k_dump_btcoex(struct ath_softc *sc, u8 *buf, u32 size)
  399. {
  400. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  401. return ath9k_dump_mci_btcoex(sc, buf, size);
  402. else
  403. return ath9k_dump_legacy_btcoex(sc, buf, size);
  404. }
  405. #endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */