htc_drv_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (c) 2010 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), ATH_DBG_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), ATH_DBG_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. u8 cmd_rsp, aggr;
  62. ath_detect_bt_priority(priv);
  63. is_btscan = !!(priv->op_flags & OP_BT_SCAN);
  64. aggr = priv->op_flags & OP_BT_PRIORITY_DETECTED;
  65. WMI_CMD_BUF(WMI_AGGR_LIMIT_CMD, &aggr);
  66. ath9k_cmn_btcoex_bt_stomp(common, is_btscan ? ATH_BTCOEX_STOMP_ALL :
  67. btcoex->bt_stomp_type);
  68. timer_period = is_btscan ? btcoex->btscan_no_stomp :
  69. btcoex->btcoex_no_stomp;
  70. ieee80211_queue_delayed_work(priv->hw, &priv->duty_cycle_work,
  71. msecs_to_jiffies(timer_period));
  72. ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work,
  73. msecs_to_jiffies(btcoex->btcoex_period));
  74. }
  75. /*
  76. * Work to time slice between wlan and bt traffic and
  77. * configure weight registers
  78. */
  79. static void ath_btcoex_duty_cycle_work(struct work_struct *work)
  80. {
  81. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  82. duty_cycle_work.work);
  83. struct ath_hw *ah = priv->ah;
  84. struct ath_btcoex *btcoex = &priv->btcoex;
  85. struct ath_common *common = ath9k_hw_common(ah);
  86. bool is_btscan = priv->op_flags & OP_BT_SCAN;
  87. ath_dbg(common, ATH_DBG_BTCOEX,
  88. "time slice work for bt and wlan\n");
  89. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW || is_btscan)
  90. ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_NONE);
  91. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  92. ath9k_cmn_btcoex_bt_stomp(common, ATH_BTCOEX_STOMP_LOW);
  93. }
  94. void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv)
  95. {
  96. struct ath_btcoex *btcoex = &priv->btcoex;
  97. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD;
  98. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  99. btcoex->btcoex_period / 100;
  100. btcoex->btscan_no_stomp = (100 - ATH_BTCOEX_BTSCAN_DUTY_CYCLE) *
  101. btcoex->btcoex_period / 100;
  102. INIT_DELAYED_WORK(&priv->coex_period_work, ath_btcoex_period_work);
  103. INIT_DELAYED_WORK(&priv->duty_cycle_work, ath_btcoex_duty_cycle_work);
  104. }
  105. /*
  106. * (Re)start btcoex work
  107. */
  108. void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv)
  109. {
  110. struct ath_btcoex *btcoex = &priv->btcoex;
  111. struct ath_hw *ah = priv->ah;
  112. ath_dbg(ath9k_hw_common(ah), ATH_DBG_BTCOEX, "Starting btcoex work\n");
  113. btcoex->bt_priority_cnt = 0;
  114. btcoex->bt_priority_time = jiffies;
  115. priv->op_flags &= ~(OP_BT_PRIORITY_DETECTED | OP_BT_SCAN);
  116. ieee80211_queue_delayed_work(priv->hw, &priv->coex_period_work, 0);
  117. }
  118. /*
  119. * Cancel btcoex and bt duty cycle work.
  120. */
  121. void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv)
  122. {
  123. cancel_delayed_work_sync(&priv->coex_period_work);
  124. cancel_delayed_work_sync(&priv->duty_cycle_work);
  125. }
  126. /*******/
  127. /* LED */
  128. /*******/
  129. static void ath9k_led_blink_work(struct work_struct *work)
  130. {
  131. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  132. ath9k_led_blink_work.work);
  133. if (!(priv->op_flags & OP_LED_ASSOCIATED))
  134. return;
  135. if ((priv->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  136. (priv->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  137. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  138. else
  139. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  140. (priv->op_flags & OP_LED_ON) ? 1 : 0);
  141. ieee80211_queue_delayed_work(priv->hw,
  142. &priv->ath9k_led_blink_work,
  143. (priv->op_flags & OP_LED_ON) ?
  144. msecs_to_jiffies(priv->led_off_duration) :
  145. msecs_to_jiffies(priv->led_on_duration));
  146. priv->led_on_duration = priv->led_on_cnt ?
  147. max((ATH_LED_ON_DURATION_IDLE - priv->led_on_cnt), 25) :
  148. ATH_LED_ON_DURATION_IDLE;
  149. priv->led_off_duration = priv->led_off_cnt ?
  150. max((ATH_LED_OFF_DURATION_IDLE - priv->led_off_cnt), 10) :
  151. ATH_LED_OFF_DURATION_IDLE;
  152. priv->led_on_cnt = priv->led_off_cnt = 0;
  153. if (priv->op_flags & OP_LED_ON)
  154. priv->op_flags &= ~OP_LED_ON;
  155. else
  156. priv->op_flags |= OP_LED_ON;
  157. }
  158. static void ath9k_led_brightness_work(struct work_struct *work)
  159. {
  160. struct ath_led *led = container_of(work, struct ath_led,
  161. brightness_work.work);
  162. struct ath9k_htc_priv *priv = led->priv;
  163. switch (led->brightness) {
  164. case LED_OFF:
  165. if (led->led_type == ATH_LED_ASSOC ||
  166. led->led_type == ATH_LED_RADIO) {
  167. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  168. (led->led_type == ATH_LED_RADIO));
  169. priv->op_flags &= ~OP_LED_ASSOCIATED;
  170. if (led->led_type == ATH_LED_RADIO)
  171. priv->op_flags &= ~OP_LED_ON;
  172. } else {
  173. priv->led_off_cnt++;
  174. }
  175. break;
  176. case LED_FULL:
  177. if (led->led_type == ATH_LED_ASSOC) {
  178. priv->op_flags |= OP_LED_ASSOCIATED;
  179. ieee80211_queue_delayed_work(priv->hw,
  180. &priv->ath9k_led_blink_work, 0);
  181. } else if (led->led_type == ATH_LED_RADIO) {
  182. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  183. priv->op_flags |= OP_LED_ON;
  184. } else {
  185. priv->led_on_cnt++;
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. static void ath9k_led_brightness(struct led_classdev *led_cdev,
  193. enum led_brightness brightness)
  194. {
  195. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  196. struct ath9k_htc_priv *priv = led->priv;
  197. led->brightness = brightness;
  198. if (!(priv->op_flags & OP_LED_DEINIT))
  199. ieee80211_queue_delayed_work(priv->hw,
  200. &led->brightness_work, 0);
  201. }
  202. void ath9k_led_stop_brightness(struct ath9k_htc_priv *priv)
  203. {
  204. cancel_delayed_work_sync(&priv->radio_led.brightness_work);
  205. cancel_delayed_work_sync(&priv->assoc_led.brightness_work);
  206. cancel_delayed_work_sync(&priv->tx_led.brightness_work);
  207. cancel_delayed_work_sync(&priv->rx_led.brightness_work);
  208. }
  209. static int ath9k_register_led(struct ath9k_htc_priv *priv, struct ath_led *led,
  210. char *trigger)
  211. {
  212. int ret;
  213. led->priv = priv;
  214. led->led_cdev.name = led->name;
  215. led->led_cdev.default_trigger = trigger;
  216. led->led_cdev.brightness_set = ath9k_led_brightness;
  217. ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_cdev);
  218. if (ret)
  219. ath_err(ath9k_hw_common(priv->ah),
  220. "Failed to register led:%s", led->name);
  221. else
  222. led->registered = 1;
  223. INIT_DELAYED_WORK(&led->brightness_work, ath9k_led_brightness_work);
  224. return ret;
  225. }
  226. static void ath9k_unregister_led(struct ath_led *led)
  227. {
  228. if (led->registered) {
  229. led_classdev_unregister(&led->led_cdev);
  230. led->registered = 0;
  231. }
  232. }
  233. void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
  234. {
  235. priv->op_flags |= OP_LED_DEINIT;
  236. ath9k_unregister_led(&priv->assoc_led);
  237. priv->op_flags &= ~OP_LED_ASSOCIATED;
  238. ath9k_unregister_led(&priv->tx_led);
  239. ath9k_unregister_led(&priv->rx_led);
  240. ath9k_unregister_led(&priv->radio_led);
  241. }
  242. void ath9k_init_leds(struct ath9k_htc_priv *priv)
  243. {
  244. char *trigger;
  245. int ret;
  246. if (AR_SREV_9287(priv->ah))
  247. priv->ah->led_pin = ATH_LED_PIN_9287;
  248. else if (AR_SREV_9271(priv->ah))
  249. priv->ah->led_pin = ATH_LED_PIN_9271;
  250. else if (AR_DEVID_7010(priv->ah))
  251. priv->ah->led_pin = ATH_LED_PIN_7010;
  252. else
  253. priv->ah->led_pin = ATH_LED_PIN_DEF;
  254. /* Configure gpio 1 for output */
  255. ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
  256. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  257. /* LED off, active low */
  258. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  259. INIT_DELAYED_WORK(&priv->ath9k_led_blink_work, ath9k_led_blink_work);
  260. trigger = ieee80211_get_radio_led_name(priv->hw);
  261. snprintf(priv->radio_led.name, sizeof(priv->radio_led.name),
  262. "ath9k-%s::radio", wiphy_name(priv->hw->wiphy));
  263. ret = ath9k_register_led(priv, &priv->radio_led, trigger);
  264. priv->radio_led.led_type = ATH_LED_RADIO;
  265. if (ret)
  266. goto fail;
  267. trigger = ieee80211_get_assoc_led_name(priv->hw);
  268. snprintf(priv->assoc_led.name, sizeof(priv->assoc_led.name),
  269. "ath9k-%s::assoc", wiphy_name(priv->hw->wiphy));
  270. ret = ath9k_register_led(priv, &priv->assoc_led, trigger);
  271. priv->assoc_led.led_type = ATH_LED_ASSOC;
  272. if (ret)
  273. goto fail;
  274. trigger = ieee80211_get_tx_led_name(priv->hw);
  275. snprintf(priv->tx_led.name, sizeof(priv->tx_led.name),
  276. "ath9k-%s::tx", wiphy_name(priv->hw->wiphy));
  277. ret = ath9k_register_led(priv, &priv->tx_led, trigger);
  278. priv->tx_led.led_type = ATH_LED_TX;
  279. if (ret)
  280. goto fail;
  281. trigger = ieee80211_get_rx_led_name(priv->hw);
  282. snprintf(priv->rx_led.name, sizeof(priv->rx_led.name),
  283. "ath9k-%s::rx", wiphy_name(priv->hw->wiphy));
  284. ret = ath9k_register_led(priv, &priv->rx_led, trigger);
  285. priv->rx_led.led_type = ATH_LED_RX;
  286. if (ret)
  287. goto fail;
  288. priv->op_flags &= ~OP_LED_DEINIT;
  289. return;
  290. fail:
  291. cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
  292. ath9k_deinit_leds(priv);
  293. }
  294. /*******************/
  295. /* Rfkill */
  296. /*******************/
  297. static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
  298. {
  299. return ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
  300. priv->ah->rfkill_polarity;
  301. }
  302. void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
  303. {
  304. struct ath9k_htc_priv *priv = hw->priv;
  305. bool blocked = !!ath_is_rfkill_set(priv);
  306. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  307. }
  308. void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
  309. {
  310. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  311. wiphy_rfkill_start_polling(priv->hw->wiphy);
  312. }
  313. void ath9k_htc_radio_enable(struct ieee80211_hw *hw)
  314. {
  315. struct ath9k_htc_priv *priv = hw->priv;
  316. struct ath_hw *ah = priv->ah;
  317. struct ath_common *common = ath9k_hw_common(ah);
  318. int ret;
  319. u8 cmd_rsp;
  320. if (!ah->curchan)
  321. ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
  322. /* Reset the HW */
  323. ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  324. if (ret) {
  325. ath_err(common,
  326. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  327. ret, ah->curchan->channel);
  328. }
  329. ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
  330. &priv->curtxpow);
  331. /* Start RX */
  332. WMI_CMD(WMI_START_RECV_CMDID);
  333. ath9k_host_rx_init(priv);
  334. /* Start TX */
  335. htc_start(priv->htc);
  336. spin_lock_bh(&priv->tx_lock);
  337. priv->tx_queues_stop = false;
  338. spin_unlock_bh(&priv->tx_lock);
  339. ieee80211_wake_queues(hw);
  340. WMI_CMD(WMI_ENABLE_INTR_CMDID);
  341. /* Enable LED */
  342. ath9k_hw_cfg_output(ah, ah->led_pin,
  343. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  344. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  345. }
  346. void ath9k_htc_radio_disable(struct ieee80211_hw *hw)
  347. {
  348. struct ath9k_htc_priv *priv = hw->priv;
  349. struct ath_hw *ah = priv->ah;
  350. struct ath_common *common = ath9k_hw_common(ah);
  351. int ret;
  352. u8 cmd_rsp;
  353. ath9k_htc_ps_wakeup(priv);
  354. /* Disable LED */
  355. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  356. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  357. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  358. /* Stop TX */
  359. ieee80211_stop_queues(hw);
  360. htc_stop(priv->htc);
  361. WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
  362. skb_queue_purge(&priv->tx_queue);
  363. /* Stop RX */
  364. WMI_CMD(WMI_STOP_RECV_CMDID);
  365. /*
  366. * The MIB counters have to be disabled here,
  367. * since the target doesn't do it.
  368. */
  369. ath9k_hw_disable_mib_counters(ah);
  370. if (!ah->curchan)
  371. ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
  372. /* Reset the HW */
  373. ret = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  374. if (ret) {
  375. ath_err(common,
  376. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  377. ret, ah->curchan->channel);
  378. }
  379. /* Disable the PHY */
  380. ath9k_hw_phy_disable(ah);
  381. ath9k_htc_ps_restore(priv);
  382. ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
  383. }