htc_drv_gpio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2010-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 "htc.h"
  17. /******************/
  18. /* BTCOEX */
  19. /******************/
  20. /*
  21. * Detects if there is any priority bt traffic
  22. */
  23. static void ath_detect_bt_priority(struct ath9k_htc_priv *priv)
  24. {
  25. struct ath_btcoex *btcoex = &priv->btcoex;
  26. struct ath_hw *ah = priv->ah;
  27. if (ath9k_hw_gpio_get(ah, ah->btcoex_hw.btpriority_gpio))
  28. btcoex->bt_priority_cnt++;
  29. if (time_after(jiffies, btcoex->bt_priority_time +
  30. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  31. priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
  32. /* Detect if colocated bt started scanning */
  33. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
  34. ath_dbg(ath9k_hw_common(ah), BTCOEX,
  35. "BT scan detected\n");
  36. priv->op_flags |= (OP_BT_SCAN |
  37. OP_BT_PRIORITY_DETECTED);
  38. } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  39. ath_dbg(ath9k_hw_common(ah), BTCOEX,
  40. "BT priority traffic detected\n");
  41. priv->op_flags |= OP_BT_PRIORITY_DETECTED;
  42. }
  43. btcoex->bt_priority_cnt = 0;
  44. btcoex->bt_priority_time = jiffies;
  45. }
  46. }
  47. /*
  48. * This is the master bt coex work which runs for every
  49. * 45ms, bt traffic will be given priority during 55% of this
  50. * period while wlan gets remaining 45%
  51. */
  52. static void ath_btcoex_period_work(struct work_struct *work)
  53. {
  54. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  55. coex_period_work.work);
  56. struct ath_btcoex *btcoex = &priv->btcoex;
  57. struct ath_common *common = ath9k_hw_common(priv->ah);
  58. u32 timer_period;
  59. bool is_btscan;
  60. int ret;
  61. ath_detect_bt_priority(priv);
  62. is_btscan = !!(priv->op_flags & OP_BT_SCAN);
  63. ret = ath9k_htc_update_cap_target(priv,
  64. !!(priv->op_flags & OP_BT_PRIORITY_DETECTED));
  65. if (ret) {
  66. ath_err(common, "Unable to set BTCOEX parameters\n");
  67. return;
  68. }
  69. ath9k_hw_btcoex_bt_stomp(priv->ah, is_btscan ? ATH_BTCOEX_STOMP_ALL :
  70. btcoex->bt_stomp_type);
  71. ath9k_hw_btcoex_enable(priv->ah);
  72. timer_period = is_btscan ? btcoex->btscan_no_stomp :
  73. btcoex->btcoex_no_stomp;
  74. ieee80211_queue_delayed_work(priv->hw, &priv->duty_cycle_work,
  75. msecs_to_jiffies(timer_period));
  76. ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work,
  77. msecs_to_jiffies(btcoex->btcoex_period));
  78. }
  79. /*
  80. * Work to time slice between wlan and bt traffic and
  81. * configure weight registers
  82. */
  83. static void ath_btcoex_duty_cycle_work(struct work_struct *work)
  84. {
  85. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  86. duty_cycle_work.work);
  87. struct ath_hw *ah = priv->ah;
  88. struct ath_btcoex *btcoex = &priv->btcoex;
  89. struct ath_common *common = ath9k_hw_common(ah);
  90. bool is_btscan = priv->op_flags & OP_BT_SCAN;
  91. ath_dbg(common, BTCOEX, "time slice work for bt and wlan\n");
  92. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
  93. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_NONE);
  94. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  95. ath9k_hw_btcoex_bt_stomp(ah, ATH_BTCOEX_STOMP_LOW);
  96. ath9k_hw_btcoex_enable(priv->ah);
  97. }
  98. void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
  99. {
  100. struct ath_btcoex *btcoex = &priv->btcoex;
  101. if (ath9k_hw_get_btcoex_scheme(priv->ah) == ATH_BTCOEX_CFG_NONE)
  102. return;
  103. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
  104. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  105. btcoex->btcoex_period / 100;
  106. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  107. btcoex->btcoex_period / 100;
  108. INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
  109. INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
  110. }
  111. /*
  112. * (Re)start btcoex work
  113. */
  114. void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
  115. {
  116. struct ath_btcoex *btcoex = &priv->btcoex;
  117. struct ath_hw *ah = priv->ah;
  118. if (ath9k_hw_get_btcoex_scheme(ah) == ATH_BTCOEX_CFG_NONE)
  119. return;
  120. ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex work\n");
  121. btcoex->bt_priority_cnt = 0;
  122. btcoex->bt_priority_time = jiffies;
  123. priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
  124. ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
  125. }
  126. /*
  127. * Cancel btcoex and bt duty cycle work.
  128. */
  129. void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
  130. {
  131. if (ath9k_hw_get_btcoex_scheme(priv->ah) == ATH_BTCOEX_CFG_NONE)
  132. return;
  133. cancel_delayed_work_sync(&priv->coex_period_work);
  134. cancel_delayed_work_sync(&priv->duty_cycle_work);
  135. }
  136. /*******/
  137. /* LED */
  138. /*******/
  139. #ifdef CONFIG_MAC80211_LEDS
  140. void ath9k_led_work(struct work_struct *work)
  141. {
  142. struct ath9k_htc_priv *priv = container_of(work,
  143. struct ath9k_htc_priv,
  144. led_work);
  145. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  146. (priv->brightness == LED_OFF));
  147. }
  148. static void ath9k_led_brightness(struct led_classdev *led_cdev,
  149. enum led_brightness brightness)
  150. {
  151. struct ath9k_htc_priv *priv = container_of(led_cdev,
  152. struct ath9k_htc_priv,
  153. led_cdev);
  154. /* Not locked, but it's just a tiny green light..*/
  155. priv->brightness = brightness;
  156. ieee80211_queue_work(priv->hw, &priv->led_work);
  157. }
  158. void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
  159. {
  160. if (!priv->led_registered)
  161. return;
  162. ath9k_led_brightness(&priv->led_cdev, LED_OFF);
  163. led_classdev_unregister(&priv->led_cdev);
  164. cancel_work_sync(&priv->led_work);
  165. }
  166. void ath9k_init_leds(struct ath9k_htc_priv *priv)
  167. {
  168. int ret;
  169. if (AR_SREV_9287(priv->ah))
  170. priv->ah->led_pin = ATH_LED_PIN_9287;
  171. else if (AR_SREV_9271(priv->ah))
  172. priv->ah->led_pin = ATH_LED_PIN_9271;
  173. else if (AR_DEVID_7010(priv->ah))
  174. priv->ah->led_pin = ATH_LED_PIN_7010;
  175. else
  176. priv->ah->led_pin = ATH_LED_PIN_DEF;
  177. /* Configure gpio 1 for output */
  178. ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
  179. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  180. /* LED off, active low */
  181. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  182. snprintf(priv->led_name, sizeof(priv->led_name),
  183. "ath9k_htc-%s", wiphy_name(priv->hw->wiphy));
  184. priv->led_cdev.name = priv->led_name;
  185. priv->led_cdev.brightness_set = ath9k_led_brightness;
  186. ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &priv->led_cdev);
  187. if (ret < 0)
  188. return;
  189. INIT_WORK(&priv->led_work, ath9k_led_work);
  190. priv->led_registered = true;
  191. return;
  192. }
  193. #endif
  194. /*******************/
  195. /* Rfkill */
  196. /*******************/
  197. static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
  198. {
  199. bool is_blocked;
  200. ath9k_htc_ps_wakeup(priv);
  201. is_blocked = ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
  202. priv->ah->rfkill_polarity;
  203. ath9k_htc_ps_restore(priv);
  204. return is_blocked;
  205. }
  206. void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
  207. {
  208. struct ath9k_htc_priv *priv = hw->priv;
  209. bool blocked = !!ath_is_rfkill_set(priv);
  210. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  211. }
  212. void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
  213. {
  214. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  215. wiphy_rfkill_start_polling(priv->hw->wiphy);
  216. }
  217. void ath9k_htc_radio_enable(struct ieee80211_hw *hw)
  218. {
  219. struct ath9k_htc_priv *priv = hw->priv;
  220. struct ath_hw *ah = priv->ah;
  221. struct ath_common *common = ath9k_hw_common(ah);
  222. int ret;
  223. u8 cmd_rsp;
  224. if (!ah->curchan)
  225. ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
  226. /* Reset the HW */
  227. ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  228. if (ret) {
  229. ath_err(common,
  230. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  231. ret, ah->curchan->channel);
  232. }
  233. ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
  234. &priv->curtxpow);
  235. /* Start RX */
  236. WMI_CMD(WMI_START_RECV_CMDID);
  237. ath9k_host_rx_init(priv);
  238. /* Start TX */
  239. htc_start(priv->htc);
  240. spin_lock_bh(&priv->tx.tx_lock);
  241. priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
  242. spin_unlock_bh(&priv->tx.tx_lock);
  243. ieee80211_wake_queues(hw);
  244. WMI_CMD(WMI_ENABLE_INTR_CMDID);
  245. /* Enable LED */
  246. ath9k_hw_cfg_output(ah, ah->led_pin,
  247. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  248. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  249. }
  250. void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
  251. {
  252. struct ath9k_htc_priv *priv = hw->priv;
  253. struct ath_hw *ah = priv->ah;
  254. struct ath_common *common = ath9k_hw_common(ah);
  255. int ret;
  256. u8 cmd_rsp;
  257. ath9k_htc_ps_wakeup(priv);
  258. /* Disable LED */
  259. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  260. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  261. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  262. /* Stop TX */
  263. ieee80211_stop_queues(hw);
  264. ath9k_htc_tx_drain(priv);
  265. WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
  266. /* Stop RX */
  267. WMI_CMD(WMI_STOP_RECV_CMDID);
  268. /* Clear the WMI event queue */
  269. ath9k_wmi_event_drain(priv);
  270. /*
  271. * The MIB counters have to be disabled here,
  272. * since the target doesn't do it.
  273. */
  274. ath9k_hw_disable_mib_counters(ah);
  275. if (!ah->curchan)
  276. ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
  277. /* Reset the HW */
  278. ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  279. if (ret) {
  280. ath_err(common,
  281. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  282. ret, ah->curchan->channel);
  283. }
  284. /* Disable the PHY */
  285. ath9k_hw_phy_disable(ah);
  286. ath9k_htc_ps_restore(priv);
  287. ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
  288. }