htc_drv_gpio.c 13 KB

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