gpio.c 14 KB

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