htc_drv_gpio.c 11 KB

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