htc_drv_gpio.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. clear_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
  34. clear_bit(OP_BT_SCAN, &priv->op_flags);
  35. /* Detect if colocated bt started scanning */
  36. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_SCAN_THRESHOLD) {
  37. ath_dbg(ath9k_hw_common(ah), BTCOEX,
  38. "BT scan detected\n");
  39. set_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
  40. set_bit(OP_BT_SCAN, &priv->op_flags);
  41. } else if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  42. ath_dbg(ath9k_hw_common(ah), BTCOEX,
  43. "BT priority traffic detected\n");
  44. set_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
  45. }
  46. btcoex->bt_priority_cnt = 0;
  47. btcoex->bt_priority_time = jiffies;
  48. }
  49. }
  50. /*
  51. * This is the master bt coex work which runs for every
  52. * 45ms, bt traffic will be given priority during 55% of this
  53. * period while wlan gets remaining 45%
  54. */
  55. static void ath_btcoex_period_work(struct work_struct *work)
  56. {
  57. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  58. coex_period_work.work);
  59. struct ath_btcoex *btcoex = &priv->btcoex;
  60. struct ath_common *common = ath9k_hw_common(priv->ah);
  61. u32 timer_period;
  62. int ret;
  63. ath_detect_bt_priority(priv);
  64. ret = ath9k_htc_update_cap_target(priv,
  65. test_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags));
  66. if (ret) {
  67. ath_err(common, "Unable to set BTCOEX parameters\n");
  68. return;
  69. }
  70. ath9k_hw_btcoex_bt_stomp(priv->ah, test_bit(OP_BT_SCAN, &priv->op_flags) ?
  71. ATH_BTCOEX_STOMP_ALL : btcoex->bt_stomp_type);
  72. ath9k_hw_btcoex_enable(priv->ah);
  73. timer_period = test_bit(OP_BT_SCAN, &priv->op_flags) ?
  74. btcoex->btscan_no_stomp : 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. ath_dbg(common, BTCOEX, "time slice work for bt and wlan\n");
  92. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW ||
  93. test_bit(OP_BT_SCAN, &priv->op_flags))
  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. static void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
  100. {
  101. struct ath_btcoex *btcoex = &priv->btcoex;
  102. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
  103. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  104. btcoex->btcoex_period / 100;
  105. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  106. btcoex->btcoex_period / 100;
  107. INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
  108. INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
  109. }
  110. /*
  111. * (Re)start btcoex work
  112. */
  113. static void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
  114. {
  115. struct ath_btcoex *btcoex = &priv->btcoex;
  116. struct ath_hw *ah = priv->ah;
  117. ath_dbg(ath9k_hw_common(ah), BTCOEX, "Starting btcoex work\n");
  118. btcoex->bt_priority_cnt = 0;
  119. btcoex->bt_priority_time = jiffies;
  120. clear_bit(OP_BT_PRIORITY_DETECTED, &priv->op_flags);
  121. clear_bit(OP_BT_SCAN, &priv->op_flags);
  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, 0);
  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. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  148. ath_htc_cancel_btcoex_work(priv);
  149. ath9k_hw_btcoex_disable(ah);
  150. }
  151. }
  152. void ath9k_htc_init_btcoex(struct ath9k_htc_priv *priv, char *product)
  153. {
  154. struct ath_hw *ah = priv->ah;
  155. struct ath_common *common = ath9k_hw_common(ah);
  156. int qnum;
  157. /*
  158. * Check if BTCOEX is globally disabled.
  159. */
  160. if (!common->btcoex_enabled) {
  161. ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_NONE;
  162. return;
  163. }
  164. if (product && strncmp(product, ATH_HTC_BTCOEX_PRODUCT_ID, 5) == 0) {
  165. ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_3WIRE;
  166. }
  167. switch (ath9k_hw_get_btcoex_scheme(priv->ah)) {
  168. case ATH_BTCOEX_CFG_NONE:
  169. break;
  170. case ATH_BTCOEX_CFG_3WIRE:
  171. priv->ah->btcoex_hw.btactive_gpio = 7;
  172. priv->ah->btcoex_hw.btpriority_gpio = 6;
  173. priv->ah->btcoex_hw.wlanactive_gpio = 8;
  174. priv->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  175. ath9k_hw_btcoex_init_3wire(priv->ah);
  176. ath_htc_init_btcoex_work(priv);
  177. qnum = priv->hwq_map[IEEE80211_AC_BE];
  178. ath9k_hw_init_btcoex_hw(priv->ah, qnum);
  179. break;
  180. default:
  181. WARN_ON(1);
  182. break;
  183. }
  184. }
  185. #endif /* CONFIG_ATH9K_BTCOEX_SUPPORT */
  186. /*******/
  187. /* LED */
  188. /*******/
  189. #ifdef CONFIG_MAC80211_LEDS
  190. void ath9k_led_work(struct work_struct *work)
  191. {
  192. struct ath9k_htc_priv *priv = container_of(work,
  193. struct ath9k_htc_priv,
  194. led_work);
  195. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  196. (priv->brightness == LED_OFF));
  197. }
  198. static void ath9k_led_brightness(struct led_classdev *led_cdev,
  199. enum led_brightness brightness)
  200. {
  201. struct ath9k_htc_priv *priv = container_of(led_cdev,
  202. struct ath9k_htc_priv,
  203. led_cdev);
  204. /* Not locked, but it's just a tiny green light..*/
  205. priv->brightness = brightness;
  206. ieee80211_queue_work(priv->hw, &priv->led_work);
  207. }
  208. void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
  209. {
  210. if (!priv->led_registered)
  211. return;
  212. ath9k_led_brightness(&priv->led_cdev, LED_OFF);
  213. led_classdev_unregister(&priv->led_cdev);
  214. cancel_work_sync(&priv->led_work);
  215. }
  216. void ath9k_configure_leds(struct ath9k_htc_priv *priv)
  217. {
  218. /* Configure gpio 1 for output */
  219. ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
  220. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  221. /* LED off, active low */
  222. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  223. }
  224. void ath9k_init_leds(struct ath9k_htc_priv *priv)
  225. {
  226. int ret;
  227. if (AR_SREV_9287(priv->ah))
  228. priv->ah->led_pin = ATH_LED_PIN_9287;
  229. else if (AR_SREV_9271(priv->ah))
  230. priv->ah->led_pin = ATH_LED_PIN_9271;
  231. else if (AR_DEVID_7010(priv->ah))
  232. priv->ah->led_pin = ATH_LED_PIN_7010;
  233. else
  234. priv->ah->led_pin = ATH_LED_PIN_DEF;
  235. if (!ath9k_htc_led_blink)
  236. priv->led_cdev.default_trigger =
  237. ieee80211_get_radio_led_name(priv->hw);
  238. ath9k_configure_leds(priv);
  239. snprintf(priv->led_name, sizeof(priv->led_name),
  240. "ath9k_htc-%s", wiphy_name(priv->hw->wiphy));
  241. priv->led_cdev.name = priv->led_name;
  242. priv->led_cdev.brightness_set = ath9k_led_brightness;
  243. ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &priv->led_cdev);
  244. if (ret < 0)
  245. return;
  246. INIT_WORK(&priv->led_work, ath9k_led_work);
  247. priv->led_registered = true;
  248. return;
  249. }
  250. #endif
  251. /*******************/
  252. /* Rfkill */
  253. /*******************/
  254. static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
  255. {
  256. bool is_blocked;
  257. ath9k_htc_ps_wakeup(priv);
  258. is_blocked = ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
  259. priv->ah->rfkill_polarity;
  260. ath9k_htc_ps_restore(priv);
  261. return is_blocked;
  262. }
  263. void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
  264. {
  265. struct ath9k_htc_priv *priv = hw->priv;
  266. bool blocked = !!ath_is_rfkill_set(priv);
  267. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  268. }
  269. void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
  270. {
  271. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  272. wiphy_rfkill_start_polling(priv->hw->wiphy);
  273. }