htc_drv_gpio.c 11 KB

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