1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
12 static u8 rtw_coex_next_rssi_state(struct rtw_dev *rtwdev, u8 pre_state,
13 u8 rssi, u8 rssi_thresh)
15 struct rtw_chip_info *chip = rtwdev->chip;
16 u8 tol = chip->rssi_tolerance;
19 if (pre_state == COEX_RSSI_STATE_LOW ||
20 pre_state == COEX_RSSI_STATE_STAY_LOW) {
21 if (rssi >= (rssi_thresh + tol))
22 next_state = COEX_RSSI_STATE_HIGH;
24 next_state = COEX_RSSI_STATE_STAY_LOW;
26 if (rssi < rssi_thresh)
27 next_state = COEX_RSSI_STATE_LOW;
29 next_state = COEX_RSSI_STATE_STAY_HIGH;
35 static void rtw_coex_limited_tx(struct rtw_dev *rtwdev,
36 bool tx_limit_en, bool ampdu_limit_en)
38 struct rtw_chip_info *chip = rtwdev->chip;
39 struct rtw_coex *coex = &rtwdev->coex;
40 struct rtw_coex_stat *coex_stat = &coex->stat;
41 bool wifi_under_b_mode = false;
43 if (!chip->scbd_support)
46 /* force max tx retry limit = 8 */
47 if (coex_stat->wl_tx_limit_en == tx_limit_en &&
48 coex_stat->wl_ampdu_limit_en == ampdu_limit_en)
51 if (!coex_stat->wl_tx_limit_en) {
52 coex_stat->darfrc = rtw_read32(rtwdev, REG_DARFRC);
53 coex_stat->darfrch = rtw_read32(rtwdev, REG_DARFRCH);
54 coex_stat->retry_limit = rtw_read16(rtwdev, REG_RETRY_LIMIT);
57 if (!coex_stat->wl_ampdu_limit_en)
58 coex_stat->ampdu_max_time =
59 rtw_read8(rtwdev, REG_AMPDU_MAX_TIME_V1);
61 coex_stat->wl_tx_limit_en = tx_limit_en;
62 coex_stat->wl_ampdu_limit_en = ampdu_limit_en;
65 /* set BT polluted packet on for tx rate adaptive,
66 * not including tx retry broken by PTA
68 rtw_write8_set(rtwdev, REG_TX_HANG_CTRL, BIT_EN_GNT_BT_AWAKE);
70 /* set queue life time to avoid can't reach tx retry limit
71 * if tx is always broken by GNT_BT
73 rtw_write8_set(rtwdev, REG_LIFETIME_EN, 0xf);
74 rtw_write16(rtwdev, REG_RETRY_LIMIT, 0x0808);
76 /* auto rate fallback step within 8 retries */
77 if (wifi_under_b_mode) {
78 rtw_write32(rtwdev, REG_DARFRC, 0x1000000);
79 rtw_write32(rtwdev, REG_DARFRCH, 0x1010101);
81 rtw_write32(rtwdev, REG_DARFRC, 0x1000000);
82 rtw_write32(rtwdev, REG_DARFRCH, 0x4030201);
85 rtw_write8_clr(rtwdev, REG_TX_HANG_CTRL, BIT_EN_GNT_BT_AWAKE);
86 rtw_write8_clr(rtwdev, REG_LIFETIME_EN, 0xf);
88 rtw_write16(rtwdev, REG_RETRY_LIMIT, coex_stat->retry_limit);
89 rtw_write32(rtwdev, REG_DARFRC, coex_stat->darfrc);
90 rtw_write32(rtwdev, REG_DARFRCH, coex_stat->darfrch);
94 rtw_write8(rtwdev, REG_AMPDU_MAX_TIME_V1, 0x20);
96 rtw_write8(rtwdev, REG_AMPDU_MAX_TIME_V1,
97 coex_stat->ampdu_max_time);
100 static void rtw_coex_limited_wl(struct rtw_dev *rtwdev)
102 struct rtw_coex *coex = &rtwdev->coex;
103 struct rtw_coex_dm *coex_dm = &coex->dm;
104 struct rtw_coex_stat *coex_stat = &coex->stat;
105 bool tx_limit = false;
106 bool tx_agg_ctrl = false;
108 if (coex->under_5g ||
109 coex_dm->bt_status == COEX_BTSTATUS_NCON_IDLE) {
110 /* no need to limit tx */
113 if (coex_stat->bt_hid_exist || coex_stat->bt_hfp_exist ||
114 coex_stat->bt_hid_pair_num > 0)
118 rtw_coex_limited_tx(rtwdev, tx_limit, tx_agg_ctrl);
121 static void rtw_coex_wl_ccklock_action(struct rtw_dev *rtwdev)
123 struct rtw_coex *coex = &rtwdev->coex;
124 struct rtw_coex_stat *coex_stat = &coex->stat;
130 para[0] = COEX_H2C69_WL_LEAKAP;
132 if (coex_stat->tdma_timer_base == 3 && coex_stat->wl_slot_extend) {
133 para[1] = PARA1_H2C69_DIS_5MS; /* disable 5ms extend */
134 rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
135 coex_stat->wl_slot_extend = false;
136 coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] = 0;
140 if (coex_stat->wl_slot_extend && coex_stat->wl_force_lps_ctrl &&
141 !coex_stat->wl_cck_lock_ever) {
142 if (coex_stat->wl_fw_dbg_info[7] <= 5)
143 coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND]++;
145 coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] = 0;
147 if (coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] == 7) {
148 para[1] = 0x1; /* disable 5ms extend */
149 rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
150 coex_stat->wl_slot_extend = false;
151 coex_stat->cnt_wl[COEX_CNT_WL_5MS_NOEXTEND] = 0;
153 } else if (!coex_stat->wl_slot_extend && coex_stat->wl_cck_lock) {
154 para[1] = 0x0; /* enable 5ms extend */
155 rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
156 coex_stat->wl_slot_extend = true;
160 static void rtw_coex_wl_ccklock_detect(struct rtw_dev *rtwdev)
162 struct rtw_coex *coex = &rtwdev->coex;
163 struct rtw_coex_stat *coex_stat = &coex->stat;
165 /* TODO: wait for rx_rate_change_notify implement */
166 coex_stat->wl_cck_lock = false;
167 coex_stat->wl_cck_lock_pre = false;
168 coex_stat->wl_cck_lock_ever = false;
171 static void rtw_coex_wl_noisy_detect(struct rtw_dev *rtwdev)
173 struct rtw_coex *coex = &rtwdev->coex;
174 struct rtw_coex_stat *coex_stat = &coex->stat;
175 struct rtw_dm_info *dm_info = &rtwdev->dm_info;
178 /* wifi noisy environment identification */
179 cnt_cck = dm_info->cck_ok_cnt + dm_info->cck_err_cnt;
181 if (!coex_stat->wl_gl_busy) {
183 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] < 5)
184 coex_stat->cnt_wl[COEX_CNT_WL_NOISY2]++;
186 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] == 5) {
187 coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] = 0;
188 coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] = 0;
190 } else if (cnt_cck < 100) {
191 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] < 5)
192 coex_stat->cnt_wl[COEX_CNT_WL_NOISY0]++;
194 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] == 5) {
195 coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] = 0;
196 coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] = 0;
199 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] < 5)
200 coex_stat->cnt_wl[COEX_CNT_WL_NOISY1]++;
202 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] == 5) {
203 coex_stat->cnt_wl[COEX_CNT_WL_NOISY0] = 0;
204 coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] = 0;
208 if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY2] == 5)
209 coex_stat->wl_noisy_level = 2;
210 else if (coex_stat->cnt_wl[COEX_CNT_WL_NOISY1] == 5)
211 coex_stat->wl_noisy_level = 1;
213 coex_stat->wl_noisy_level = 0;
217 static void rtw_coex_tdma_timer_base(struct rtw_dev *rtwdev, u8 type)
219 struct rtw_coex *coex = &rtwdev->coex;
220 struct rtw_coex_stat *coex_stat = &coex->stat;
223 if (coex_stat->tdma_timer_base == type)
226 coex_stat->tdma_timer_base = type;
228 para[0] = COEX_H2C69_TDMA_SLOT;
230 if (type == 3) /* 4-slot */
231 para[1] = PARA1_H2C69_TDMA_4SLOT; /* 4-slot */
233 para[1] = PARA1_H2C69_TDMA_2SLOT;
235 rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
237 /* no 5ms_wl_slot_extend for 4-slot mode */
238 if (coex_stat->tdma_timer_base == 3)
239 rtw_coex_wl_ccklock_action(rtwdev);
242 static void rtw_coex_set_wl_pri_mask(struct rtw_dev *rtwdev, u8 bitmap,
247 addr = REG_BT_COEX_TABLE_H + (bitmap / 8);
250 rtw_write8_mask(rtwdev, addr, BIT(bitmap), data);
253 void rtw_coex_write_scbd(struct rtw_dev *rtwdev, u16 bitpos, bool set)
255 struct rtw_chip_info *chip = rtwdev->chip;
256 struct rtw_coex *coex = &rtwdev->coex;
257 struct rtw_coex_stat *coex_stat = &coex->stat;
260 if (!chip->scbd_support)
263 val |= coex_stat->score_board;
265 /* for 8822b, scbd[10] is CQDDR on
266 * for 8822c, scbd[10] is no fix 2M
268 if (!chip->new_scbd10_def && (bitpos & COEX_SCBD_FIX2M)) {
270 val &= ~COEX_SCBD_FIX2M;
272 val |= COEX_SCBD_FIX2M;
280 if (val != coex_stat->score_board) {
281 coex_stat->score_board = val;
282 val |= BIT_BT_INT_EN;
283 rtw_write16(rtwdev, REG_WIFI_BT_INFO, val);
287 static u16 rtw_coex_read_scbd(struct rtw_dev *rtwdev)
289 struct rtw_chip_info *chip = rtwdev->chip;
291 if (!chip->scbd_support)
294 return (rtw_read16(rtwdev, REG_WIFI_BT_INFO)) & ~(BIT_BT_INT_EN);
297 static void rtw_coex_check_rfk(struct rtw_dev *rtwdev)
299 struct rtw_chip_info *chip = rtwdev->chip;
300 struct rtw_coex *coex = &rtwdev->coex;
301 struct rtw_coex_stat *coex_stat = &coex->stat;
302 struct rtw_coex_rfe *coex_rfe = &coex->rfe;
307 if (coex_rfe->wlg_at_btg && chip->scbd_support &&
308 coex_stat->bt_iqk_state != 0xff) {
309 wait_cnt = COEX_RFK_TIMEOUT / COEX_MIN_DELAY;
312 btk = !!(rtw_coex_read_scbd(rtwdev) & COEX_SCBD_BT_RFK);
315 wlk = !!(rtw_read8(rtwdev, REG_ARFR4) & BIT_WL_RFK);
320 mdelay(COEX_MIN_DELAY);
321 } while (++cnt < wait_cnt);
324 coex_stat->bt_iqk_state = 0xff;
328 static void rtw_coex_query_bt_info(struct rtw_dev *rtwdev)
330 struct rtw_coex *coex = &rtwdev->coex;
331 struct rtw_coex_stat *coex_stat = &coex->stat;
333 if (coex_stat->bt_disabled)
336 rtw_fw_query_bt_info(rtwdev);
339 static void rtw_coex_monitor_bt_enable(struct rtw_dev *rtwdev)
341 struct rtw_chip_info *chip = rtwdev->chip;
342 struct rtw_coex *coex = &rtwdev->coex;
343 struct rtw_coex_stat *coex_stat = &coex->stat;
344 struct rtw_coex_dm *coex_dm = &coex->dm;
345 bool bt_disabled = false;
348 if (chip->scbd_support) {
349 score_board = rtw_coex_read_scbd(rtwdev);
350 bt_disabled = !(score_board & COEX_SCBD_ONOFF);
353 if (coex_stat->bt_disabled != bt_disabled) {
354 rtw_dbg(rtwdev, RTW_DBG_COEX, "coex: BT state changed (%d) -> (%d)\n",
355 coex_stat->bt_disabled, bt_disabled);
357 coex_stat->bt_disabled = bt_disabled;
358 coex_stat->bt_ble_scan_type = 0;
359 coex_dm->cur_bt_lna_lvl = 0;
362 if (!coex_stat->bt_disabled) {
363 coex_stat->bt_reenable = true;
364 ieee80211_queue_delayed_work(rtwdev->hw,
365 &coex->bt_reenable_work, 15 * HZ);
367 coex_stat->bt_mailbox_reply = false;
368 coex_stat->bt_reenable = false;
372 static void rtw_coex_update_wl_link_info(struct rtw_dev *rtwdev, u8 reason)
374 struct rtw_coex *coex = &rtwdev->coex;
375 struct rtw_coex_stat *coex_stat = &coex->stat;
376 struct rtw_coex_dm *coex_dm = &coex->dm;
377 struct rtw_chip_info *chip = rtwdev->chip;
378 struct rtw_traffic_stats *stats = &rtwdev->stats;
380 bool scan = false, link = false;
386 scan = rtw_flag_check(rtwdev, RTW_FLAG_SCANNING);
387 coex_stat->wl_connected = !!rtwdev->sta_cnt;
388 coex_stat->wl_gl_busy = rtw_flag_check(rtwdev, RTW_FLAG_BUSY_TRAFFIC);
390 if (stats->tx_throughput > stats->rx_throughput)
391 coex_stat->wl_tput_dir = COEX_WL_TPUT_TX;
393 coex_stat->wl_tput_dir = COEX_WL_TPUT_RX;
395 if (scan || link || reason == COEX_RSN_2GCONSTART ||
396 reason == COEX_RSN_2GSCANSTART || reason == COEX_RSN_2GSWITCHBAND)
397 coex_stat->wl_linkscan_proc = true;
399 coex_stat->wl_linkscan_proc = false;
401 rtw_coex_wl_noisy_detect(rtwdev);
403 for (i = 0; i < 4; i++) {
404 rssi_state = coex_dm->wl_rssi_state[i];
405 rssi_step = chip->wl_rssi_step[i];
406 rssi = rtwdev->dm_info.min_rssi;
407 rssi_state = rtw_coex_next_rssi_state(rtwdev, rssi_state,
409 coex_dm->wl_rssi_state[i] = rssi_state;
413 case COEX_RSN_5GSCANSTART:
414 case COEX_RSN_5GSWITCHBAND:
415 case COEX_RSN_5GCONSTART:
419 case COEX_RSN_2GSCANSTART:
420 case COEX_RSN_2GSWITCHBAND:
421 case COEX_RSN_2GCONSTART:
426 if (rtwdev->hal.current_band_type == RTW_BAND_5G)
433 coex->under_5g = is_5G;
436 static inline u8 *get_payload_from_coex_resp(struct sk_buff *resp)
438 struct rtw_c2h_cmd *c2h;
441 pkt_offset = *((u32 *)resp->cb);
442 c2h = (struct rtw_c2h_cmd *)(resp->data + pkt_offset);
447 void rtw_coex_info_response(struct rtw_dev *rtwdev, struct sk_buff *skb)
449 struct rtw_coex *coex = &rtwdev->coex;
450 u8 *payload = get_payload_from_coex_resp(skb);
452 if (payload[0] != COEX_RESP_ACK_BY_WL_FW)
455 skb_queue_tail(&coex->queue, skb);
456 wake_up(&coex->wait);
459 static struct sk_buff *rtw_coex_info_request(struct rtw_dev *rtwdev,
460 struct rtw_coex_info_req *req)
462 struct rtw_coex *coex = &rtwdev->coex;
463 struct sk_buff *skb_resp = NULL;
465 mutex_lock(&coex->mutex);
467 rtw_fw_query_bt_mp_info(rtwdev, req);
469 if (!wait_event_timeout(coex->wait, !skb_queue_empty(&coex->queue),
470 COEX_REQUEST_TIMEOUT)) {
471 rtw_err(rtwdev, "coex request time out\n");
475 skb_resp = skb_dequeue(&coex->queue);
477 rtw_err(rtwdev, "failed to get coex info response\n");
482 mutex_unlock(&coex->mutex);
486 static bool rtw_coex_get_bt_scan_type(struct rtw_dev *rtwdev, u8 *scan_type)
488 struct rtw_coex_info_req req = {0};
493 req.op_code = BT_MP_INFO_OP_SCAN_TYPE;
494 skb = rtw_coex_info_request(rtwdev, &req);
498 payload = get_payload_from_coex_resp(skb);
499 *scan_type = GET_COEX_RESP_BT_SCAN_TYPE(payload);
500 dev_kfree_skb_any(skb);
507 static bool rtw_coex_set_lna_constrain_level(struct rtw_dev *rtwdev,
508 u8 lna_constrain_level)
510 struct rtw_coex_info_req req = {0};
514 req.op_code = BT_MP_INFO_OP_LNA_CONSTRAINT;
515 req.para1 = lna_constrain_level;
516 skb = rtw_coex_info_request(rtwdev, &req);
520 dev_kfree_skb_any(skb);
527 static void rtw_coex_update_bt_link_info(struct rtw_dev *rtwdev)
529 struct rtw_coex *coex = &rtwdev->coex;
530 struct rtw_coex_stat *coex_stat = &coex->stat;
531 struct rtw_coex_dm *coex_dm = &coex->dm;
532 struct rtw_chip_info *chip = rtwdev->chip;
538 /* update wl/bt rssi by btinfo */
539 for (i = 0; i < COEX_RSSI_STEP; i++) {
540 rssi_state = coex_dm->bt_rssi_state[i];
541 rssi_step = chip->bt_rssi_step[i];
542 rssi = coex_stat->bt_rssi;
543 rssi_state = rtw_coex_next_rssi_state(rtwdev, rssi_state,
545 coex_dm->bt_rssi_state[i] = rssi_state;
548 for (i = 0; i < COEX_RSSI_STEP; i++) {
549 rssi_state = coex_dm->wl_rssi_state[i];
550 rssi_step = chip->wl_rssi_step[i];
551 rssi = rtwdev->dm_info.min_rssi;
552 rssi_state = rtw_coex_next_rssi_state(rtwdev, rssi_state,
554 coex_dm->wl_rssi_state[i] = rssi_state;
557 if (coex_stat->bt_ble_scan_en &&
558 coex_stat->cnt_bt[COEX_CNT_BT_INFOUPDATE] % 3 == 0) {
561 if (rtw_coex_get_bt_scan_type(rtwdev, &scan_type)) {
562 coex_stat->bt_ble_scan_type = scan_type;
563 if ((coex_stat->bt_ble_scan_type & 0x1) == 0x1)
564 coex_stat->bt_init_scan = true;
566 coex_stat->bt_init_scan = false;
570 coex_stat->bt_profile_num = 0;
572 /* set link exist status */
573 if (!(coex_stat->bt_info_lb2 & COEX_INFO_CONNECTION)) {
574 coex_stat->bt_link_exist = false;
575 coex_stat->bt_pan_exist = false;
576 coex_stat->bt_a2dp_exist = false;
577 coex_stat->bt_hid_exist = false;
578 coex_stat->bt_hfp_exist = false;
580 /* connection exists */
581 coex_stat->bt_link_exist = true;
582 if (coex_stat->bt_info_lb2 & COEX_INFO_FTP) {
583 coex_stat->bt_pan_exist = true;
584 coex_stat->bt_profile_num++;
586 coex_stat->bt_pan_exist = false;
589 if (coex_stat->bt_info_lb2 & COEX_INFO_A2DP) {
590 coex_stat->bt_a2dp_exist = true;
591 coex_stat->bt_profile_num++;
593 coex_stat->bt_a2dp_exist = false;
596 if (coex_stat->bt_info_lb2 & COEX_INFO_HID) {
597 coex_stat->bt_hid_exist = true;
598 coex_stat->bt_profile_num++;
600 coex_stat->bt_hid_exist = false;
603 if (coex_stat->bt_info_lb2 & COEX_INFO_SCO_ESCO) {
604 coex_stat->bt_hfp_exist = true;
605 coex_stat->bt_profile_num++;
607 coex_stat->bt_hfp_exist = false;
611 if (coex_stat->bt_info_lb2 & COEX_INFO_INQ_PAGE) {
612 coex_dm->bt_status = COEX_BTSTATUS_INQ_PAGE;
613 } else if (!(coex_stat->bt_info_lb2 & COEX_INFO_CONNECTION)) {
614 coex_dm->bt_status = COEX_BTSTATUS_NCON_IDLE;
615 } else if (coex_stat->bt_info_lb2 == COEX_INFO_CONNECTION) {
616 coex_dm->bt_status = COEX_BTSTATUS_CON_IDLE;
617 } else if ((coex_stat->bt_info_lb2 & COEX_INFO_SCO_ESCO) ||
618 (coex_stat->bt_info_lb2 & COEX_INFO_SCO_BUSY)) {
619 if (coex_stat->bt_info_lb2 & COEX_INFO_ACL_BUSY)
620 coex_dm->bt_status = COEX_BTSTATUS_ACL_SCO_BUSY;
622 coex_dm->bt_status = COEX_BTSTATUS_SCO_BUSY;
623 } else if (coex_stat->bt_info_lb2 & COEX_INFO_ACL_BUSY) {
624 coex_dm->bt_status = COEX_BTSTATUS_ACL_BUSY;
626 coex_dm->bt_status = COEX_BTSTATUS_MAX;
629 coex_stat->cnt_bt[COEX_CNT_BT_INFOUPDATE]++;
631 rtw_dbg(rtwdev, RTW_DBG_COEX, "coex: bt status(%d)\n", coex_dm->bt_status);
634 static void rtw_coex_update_wl_ch_info(struct rtw_dev *rtwdev, u8 type)
636 struct rtw_chip_info *chip = rtwdev->chip;
637 struct rtw_coex_dm *coex_dm = &rtwdev->coex.dm;
638 struct rtw_efuse *efuse = &rtwdev->efuse;
644 bw = rtwdev->hal.current_band_width;
646 if (type != COEX_MEDIA_DISCONNECT)
647 center_chan = rtwdev->hal.current_channel;
649 if (center_chan == 0 || (efuse->share_ant && center_chan <= 14)) {
651 } else if (center_chan <= 14) {
654 if (bw == RTW_CHANNEL_WIDTH_40)
655 bw = chip->bt_afh_span_bw40;
657 bw = chip->bt_afh_span_bw20;
658 } else if (chip->afh_5g_num > 1) {
659 for (i = 0; i < chip->afh_5g_num; i++) {
660 if (center_chan == chip->afh_5g[i].wl_5g_ch) {
662 center_chan = chip->afh_5g[i].bt_skip_ch;
663 bw = chip->afh_5g[i].bt_skip_span;
669 coex_dm->wl_ch_info[0] = link;
670 coex_dm->wl_ch_info[1] = center_chan;
671 coex_dm->wl_ch_info[2] = bw;
673 rtw_fw_wl_ch_info(rtwdev, link, center_chan, bw);
676 static void rtw_coex_set_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
678 struct rtw_coex *coex = &rtwdev->coex;
679 struct rtw_coex_dm *coex_dm = &coex->dm;
681 if (bt_pwr_dec_lvl == coex_dm->cur_bt_pwr_lvl)
684 coex_dm->cur_bt_pwr_lvl = bt_pwr_dec_lvl;
686 rtw_fw_force_bt_tx_power(rtwdev, bt_pwr_dec_lvl);
689 static void rtw_coex_set_bt_rx_gain(struct rtw_dev *rtwdev, u8 bt_lna_lvl)
691 struct rtw_coex *coex = &rtwdev->coex;
692 struct rtw_coex_dm *coex_dm = &coex->dm;
694 if (bt_lna_lvl == coex_dm->cur_bt_lna_lvl)
697 coex_dm->cur_bt_lna_lvl = bt_lna_lvl;
699 /* notify BT rx gain table changed */
700 if (bt_lna_lvl < 7) {
701 rtw_coex_set_lna_constrain_level(rtwdev, bt_lna_lvl);
702 rtw_coex_write_scbd(rtwdev, COEX_SCBD_RXGAIN, true);
704 rtw_coex_write_scbd(rtwdev, COEX_SCBD_RXGAIN, false);
708 static void rtw_coex_set_rf_para(struct rtw_dev *rtwdev,
709 struct coex_rf_para para)
711 struct rtw_coex *coex = &rtwdev->coex;
712 struct rtw_coex_stat *coex_stat = &coex->stat;
715 if (coex->freerun && coex_stat->wl_noisy_level <= 1)
718 rtw_coex_set_wl_tx_power(rtwdev, para.wl_pwr_dec_lvl);
719 rtw_coex_set_bt_tx_power(rtwdev, para.bt_pwr_dec_lvl + offset);
720 rtw_coex_set_wl_rx_gain(rtwdev, para.wl_low_gain_en);
721 rtw_coex_set_bt_rx_gain(rtwdev, para.bt_lna_lvl);
724 u32 rtw_coex_read_indirect_reg(struct rtw_dev *rtwdev, u16 addr)
728 if (!ltecoex_read_reg(rtwdev, addr, &val)) {
729 rtw_err(rtwdev, "failed to read indirect register\n");
736 void rtw_coex_write_indirect_reg(struct rtw_dev *rtwdev, u16 addr,
739 u32 shift = __ffs(mask);
742 tmp = rtw_coex_read_indirect_reg(rtwdev, addr);
743 tmp = (tmp & (~mask)) | ((val << shift) & mask);
745 if (!ltecoex_reg_write(rtwdev, addr, tmp))
746 rtw_err(rtwdev, "failed to write indirect register\n");
749 static void rtw_coex_coex_ctrl_owner(struct rtw_dev *rtwdev, bool wifi_control)
752 rtw_write32_set(rtwdev, REG_SYS_SDIO_CTRL, BIT_LTE_MUX_CTRL_PATH);
754 rtw_write32_clr(rtwdev, REG_SYS_SDIO_CTRL, BIT_LTE_MUX_CTRL_PATH);
757 static void rtw_coex_set_gnt_bt(struct rtw_dev *rtwdev, u8 state)
759 rtw_coex_write_indirect_reg(rtwdev, 0x38, 0xc000, state);
760 rtw_coex_write_indirect_reg(rtwdev, 0x38, 0x0c00, state);
763 static void rtw_coex_set_gnt_wl(struct rtw_dev *rtwdev, u8 state)
765 rtw_coex_write_indirect_reg(rtwdev, 0x38, 0x3000, state);
766 rtw_coex_write_indirect_reg(rtwdev, 0x38, 0x0300, state);
769 static void rtw_coex_set_table(struct rtw_dev *rtwdev, u32 table0, u32 table1)
771 #define DEF_BRK_TABLE_VAL 0xf0ffffff
772 rtw_write32(rtwdev, REG_BT_COEX_TABLE0, table0);
773 rtw_write32(rtwdev, REG_BT_COEX_TABLE1, table1);
774 rtw_write32(rtwdev, REG_BT_COEX_BRK_TABLE, DEF_BRK_TABLE_VAL);
777 static void rtw_coex_table(struct rtw_dev *rtwdev, u8 type)
779 struct rtw_coex *coex = &rtwdev->coex;
780 struct rtw_coex_dm *coex_dm = &coex->dm;
781 struct rtw_chip_info *chip = rtwdev->chip;
782 struct rtw_efuse *efuse = &rtwdev->efuse;
784 coex_dm->cur_table = type;
786 if (efuse->share_ant) {
787 if (type < chip->table_sant_num)
788 rtw_coex_set_table(rtwdev,
789 chip->table_sant[type].bt,
790 chip->table_sant[type].wl);
793 if (type < chip->table_nsant_num)
794 rtw_coex_set_table(rtwdev,
795 chip->table_nsant[type].bt,
796 chip->table_nsant[type].wl);
800 static void rtw_coex_ignore_wlan_act(struct rtw_dev *rtwdev, bool enable)
802 struct rtw_coex *coex = &rtwdev->coex;
807 rtw_fw_bt_ignore_wlan_action(rtwdev, enable);
810 static void rtw_coex_power_save_state(struct rtw_dev *rtwdev, u8 ps_type,
811 u8 lps_val, u8 rpwm_val)
813 struct rtw_lps_conf *lps_conf = &rtwdev->lps_conf;
814 struct rtw_vif *rtwvif;
815 struct rtw_coex *coex = &rtwdev->coex;
816 struct rtw_coex_stat *coex_stat = &coex->stat;
819 lps_mode = rtwdev->lps_conf.mode;
822 case COEX_PS_WIFI_NATIVE:
823 /* recover to original 32k low power setting */
824 coex_stat->wl_force_lps_ctrl = false;
826 rtwvif = lps_conf->rtwvif;
827 if (rtwvif && rtw_in_lps(rtwdev))
828 rtw_leave_lps(rtwdev, rtwvif);
830 case COEX_PS_LPS_OFF:
831 coex_stat->wl_force_lps_ctrl = true;
833 rtw_fw_coex_tdma_type(rtwdev, 0x8, 0, 0, 0, 0);
835 rtwvif = lps_conf->rtwvif;
836 if (rtwvif && rtw_in_lps(rtwdev))
837 rtw_leave_lps(rtwdev, rtwvif);
844 static void rtw_coex_set_tdma(struct rtw_dev *rtwdev, u8 byte1, u8 byte2,
845 u8 byte3, u8 byte4, u8 byte5)
847 struct rtw_coex *coex = &rtwdev->coex;
848 struct rtw_coex_dm *coex_dm = &coex->dm;
849 struct rtw_chip_info *chip = rtwdev->chip;
850 u8 ps_type = COEX_PS_WIFI_NATIVE;
851 bool ap_enable = false;
853 if (ap_enable && (byte1 & BIT(4) && !(byte1 & BIT(5)))) {
860 ps_type = COEX_PS_WIFI_NATIVE;
861 rtw_coex_power_save_state(rtwdev, ps_type, 0x0, 0x0);
862 } else if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
863 if (chip->pstdma_type == COEX_PSTDMA_FORCE_LPSOFF)
864 ps_type = COEX_PS_LPS_OFF;
866 ps_type = COEX_PS_LPS_ON;
867 rtw_coex_power_save_state(rtwdev, ps_type, 0x50, 0x4);
869 ps_type = COEX_PS_WIFI_NATIVE;
870 rtw_coex_power_save_state(rtwdev, ps_type, 0x0, 0x0);
873 coex_dm->ps_tdma_para[0] = byte1;
874 coex_dm->ps_tdma_para[1] = byte2;
875 coex_dm->ps_tdma_para[2] = byte3;
876 coex_dm->ps_tdma_para[3] = byte4;
877 coex_dm->ps_tdma_para[4] = byte5;
879 rtw_fw_coex_tdma_type(rtwdev, byte1, byte2, byte3, byte4, byte5);
882 static void rtw_coex_tdma(struct rtw_dev *rtwdev, bool force, u32 tcase)
884 struct rtw_coex *coex = &rtwdev->coex;
885 struct rtw_coex_dm *coex_dm = &coex->dm;
886 struct rtw_chip_info *chip = rtwdev->chip;
887 struct rtw_efuse *efuse = &rtwdev->efuse;
891 if (tcase & TDMA_4SLOT)/* 4-slot (50ms) mode */
892 rtw_coex_tdma_timer_base(rtwdev, 3);
894 rtw_coex_tdma_timer_base(rtwdev, 0);
896 type = (u8)(tcase & 0xff);
898 turn_on = (type == 0 || type == 100) ? false : true;
901 if (turn_on == coex_dm->cur_ps_tdma_on &&
902 type == coex_dm->cur_ps_tdma) {
908 /* enable TBTT interrupt */
909 rtw_write8_set(rtwdev, REG_BCN_CTRL, BIT_EN_BCN_FUNCTION);
910 rtw_coex_write_scbd(rtwdev, COEX_SCBD_TDMA, true);
912 rtw_coex_write_scbd(rtwdev, COEX_SCBD_TDMA, false);
915 if (efuse->share_ant) {
916 if (type < chip->tdma_sant_num)
917 rtw_coex_set_tdma(rtwdev,
918 chip->tdma_sant[type].para[0],
919 chip->tdma_sant[type].para[1],
920 chip->tdma_sant[type].para[2],
921 chip->tdma_sant[type].para[3],
922 chip->tdma_sant[type].para[4]);
925 if (n < chip->tdma_nsant_num)
926 rtw_coex_set_tdma(rtwdev,
927 chip->tdma_nsant[n].para[0],
928 chip->tdma_nsant[n].para[1],
929 chip->tdma_nsant[n].para[2],
930 chip->tdma_nsant[n].para[3],
931 chip->tdma_nsant[n].para[4]);
934 /* update pre state */
935 coex_dm->cur_ps_tdma_on = turn_on;
936 coex_dm->cur_ps_tdma = type;
938 rtw_dbg(rtwdev, RTW_DBG_COEX, "coex: coex tdma type (%d)\n", type);
941 static void rtw_coex_set_ant_path(struct rtw_dev *rtwdev, bool force, u8 phase)
943 struct rtw_coex *coex = &rtwdev->coex;
944 struct rtw_coex_stat *coex_stat = &coex->stat;
945 struct rtw_coex_dm *coex_dm = &coex->dm;
946 u8 ctrl_type = COEX_SWITCH_CTRL_MAX;
947 u8 pos_type = COEX_SWITCH_TO_MAX;
949 if (!force && coex_dm->cur_ant_pos_type == phase)
952 coex_dm->cur_ant_pos_type = phase;
954 /* avoid switch coex_ctrl_owner during BT IQK */
955 rtw_coex_check_rfk(rtwdev);
958 case COEX_SET_ANT_POWERON:
959 /* set path control owner to BT at power-on */
960 if (coex_stat->bt_disabled)
961 rtw_coex_coex_ctrl_owner(rtwdev, true);
963 rtw_coex_coex_ctrl_owner(rtwdev, false);
965 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
966 pos_type = COEX_SWITCH_TO_BT;
968 case COEX_SET_ANT_INIT:
969 if (coex_stat->bt_disabled) {
970 /* set GNT_BT to SW low */
971 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_SW_LOW);
973 /* set GNT_WL to SW high */
974 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_SW_HIGH);
976 /* set GNT_BT to SW high */
977 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_SW_HIGH);
979 /* set GNT_WL to SW low */
980 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_SW_LOW);
983 /* set path control owner to wl at initial step */
984 rtw_coex_coex_ctrl_owner(rtwdev, true);
986 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
987 pos_type = COEX_SWITCH_TO_BT;
989 case COEX_SET_ANT_WONLY:
990 /* set GNT_BT to SW Low */
991 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_SW_LOW);
993 /* Set GNT_WL to SW high */
994 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_SW_HIGH);
996 /* set path control owner to wl at initial step */
997 rtw_coex_coex_ctrl_owner(rtwdev, true);
999 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
1000 pos_type = COEX_SWITCH_TO_WLG;
1002 case COEX_SET_ANT_WOFF:
1003 /* set path control owner to BT */
1004 rtw_coex_coex_ctrl_owner(rtwdev, false);
1006 ctrl_type = COEX_SWITCH_CTRL_BY_BT;
1007 pos_type = COEX_SWITCH_TO_NOCARE;
1009 case COEX_SET_ANT_2G:
1010 /* set GNT_BT to PTA */
1011 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_HW_PTA);
1013 /* set GNT_WL to PTA */
1014 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_HW_PTA);
1016 /* set path control owner to wl at runtime step */
1017 rtw_coex_coex_ctrl_owner(rtwdev, true);
1019 ctrl_type = COEX_SWITCH_CTRL_BY_PTA;
1020 pos_type = COEX_SWITCH_TO_NOCARE;
1022 case COEX_SET_ANT_5G:
1023 /* set GNT_BT to PTA */
1024 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_SW_HIGH);
1026 /* set GNT_WL to SW high */
1027 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_SW_HIGH);
1029 /* set path control owner to wl at runtime step */
1030 rtw_coex_coex_ctrl_owner(rtwdev, true);
1032 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
1033 pos_type = COEX_SWITCH_TO_WLA;
1035 case COEX_SET_ANT_2G_FREERUN:
1036 /* set GNT_BT to SW high */
1037 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_SW_HIGH);
1039 /* Set GNT_WL to SW high */
1040 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_SW_HIGH);
1042 /* set path control owner to wl at runtime step */
1043 rtw_coex_coex_ctrl_owner(rtwdev, true);
1045 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
1046 pos_type = COEX_SWITCH_TO_WLG_BT;
1048 case COEX_SET_ANT_2G_WLBT:
1049 /* set GNT_BT to SW high */
1050 rtw_coex_set_gnt_bt(rtwdev, COEX_GNT_SET_HW_PTA);
1052 /* Set GNT_WL to SW high */
1053 rtw_coex_set_gnt_wl(rtwdev, COEX_GNT_SET_HW_PTA);
1055 /* set path control owner to wl at runtime step */
1056 rtw_coex_coex_ctrl_owner(rtwdev, true);
1058 ctrl_type = COEX_SWITCH_CTRL_BY_BBSW;
1059 pos_type = COEX_SWITCH_TO_WLG_BT;
1062 WARN(1, "unknown phase when setting antenna path\n");
1066 if (ctrl_type < COEX_SWITCH_CTRL_MAX && pos_type < COEX_SWITCH_TO_MAX)
1067 rtw_coex_set_ant_switch(rtwdev, ctrl_type, pos_type);
1070 static u8 rtw_coex_algorithm(struct rtw_dev *rtwdev)
1072 struct rtw_coex *coex = &rtwdev->coex;
1073 struct rtw_coex_stat *coex_stat = &coex->stat;
1074 u8 algorithm = COEX_ALGO_NOPROFILE;
1077 if (coex_stat->bt_hfp_exist)
1078 profile_map |= BPM_HFP;
1079 if (coex_stat->bt_hid_exist)
1080 profile_map |= BPM_HID;
1081 if (coex_stat->bt_a2dp_exist)
1082 profile_map |= BPM_A2DP;
1083 if (coex_stat->bt_pan_exist)
1084 profile_map |= BPM_PAN;
1086 switch (profile_map) {
1088 algorithm = COEX_ALGO_HFP;
1091 case BPM_HFP + BPM_HID:
1092 algorithm = COEX_ALGO_HID;
1094 case BPM_HFP + BPM_A2DP:
1095 case BPM_HID + BPM_A2DP:
1096 case BPM_HFP + BPM_HID + BPM_A2DP:
1097 algorithm = COEX_ALGO_A2DP_HID;
1099 case BPM_HFP + BPM_PAN:
1100 case BPM_HID + BPM_PAN:
1101 case BPM_HFP + BPM_HID + BPM_PAN:
1102 algorithm = COEX_ALGO_PAN_HID;
1104 case BPM_HFP + BPM_A2DP + BPM_PAN:
1105 case BPM_HID + BPM_A2DP + BPM_PAN:
1106 case BPM_HFP + BPM_HID + BPM_A2DP + BPM_PAN:
1107 algorithm = COEX_ALGO_A2DP_PAN_HID;
1110 algorithm = COEX_ALGO_PAN;
1112 case BPM_A2DP + BPM_PAN:
1113 algorithm = COEX_ALGO_A2DP_PAN;
1116 if (coex_stat->bt_multi_link) {
1117 if (coex_stat->bt_hid_pair_num > 0)
1118 algorithm = COEX_ALGO_A2DP_HID;
1120 algorithm = COEX_ALGO_A2DP_PAN;
1122 algorithm = COEX_ALGO_A2DP;
1126 algorithm = COEX_ALGO_NOPROFILE;
1133 static void rtw_coex_action_coex_all_off(struct rtw_dev *rtwdev)
1135 struct rtw_efuse *efuse = &rtwdev->efuse;
1136 struct rtw_chip_info *chip = rtwdev->chip;
1137 u8 table_case, tdma_case;
1139 if (efuse->share_ant) {
1144 /* Non-Shared-Ant */
1149 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1150 rtw_coex_table(rtwdev, table_case);
1151 rtw_coex_tdma(rtwdev, false, tdma_case);
1154 static void rtw_coex_action_freerun(struct rtw_dev *rtwdev)
1156 struct rtw_coex *coex = &rtwdev->coex;
1157 struct rtw_coex_stat *coex_stat = &coex->stat;
1158 struct rtw_coex_dm *coex_dm = &coex->dm;
1159 struct rtw_efuse *efuse = &rtwdev->efuse;
1160 struct rtw_chip_info *chip = rtwdev->chip;
1163 if (efuse->share_ant)
1166 coex->freerun = true;
1168 if (coex_stat->wl_connected)
1169 rtw_coex_update_wl_ch_info(rtwdev, COEX_MEDIA_CONNECT);
1171 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G_FREERUN);
1173 rtw_coex_write_scbd(rtwdev, COEX_SCBD_FIX2M, false);
1175 if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[0]))
1177 else if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[1]))
1179 else if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[2]))
1184 if (level > chip->wl_rf_para_num - 1)
1185 level = chip->wl_rf_para_num - 1;
1187 if (coex_stat->wl_tput_dir == COEX_WL_TPUT_TX)
1188 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_tx[level]);
1190 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[level]);
1192 rtw_coex_table(rtwdev, 100);
1193 rtw_coex_tdma(rtwdev, false, 100);
1196 static void rtw_coex_action_bt_whql_test(struct rtw_dev *rtwdev)
1198 struct rtw_efuse *efuse = &rtwdev->efuse;
1199 struct rtw_chip_info *chip = rtwdev->chip;
1200 u8 table_case, tdma_case;
1202 if (efuse->share_ant) {
1207 /* Non-Shared-Ant */
1212 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1213 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1214 rtw_coex_table(rtwdev, table_case);
1215 rtw_coex_tdma(rtwdev, false, tdma_case);
1218 static void rtw_coex_action_bt_relink(struct rtw_dev *rtwdev)
1220 struct rtw_efuse *efuse = &rtwdev->efuse;
1221 struct rtw_chip_info *chip = rtwdev->chip;
1222 u8 table_case, tdma_case;
1224 if (efuse->share_ant) {
1229 /* Non-Shared-Ant */
1234 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1235 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1236 rtw_coex_table(rtwdev, table_case);
1237 rtw_coex_tdma(rtwdev, false, tdma_case);
1240 static void rtw_coex_action_bt_idle(struct rtw_dev *rtwdev)
1242 struct rtw_coex *coex = &rtwdev->coex;
1243 struct rtw_coex_stat *coex_stat = &coex->stat;
1244 struct rtw_coex_dm *coex_dm = &coex->dm;
1245 struct rtw_efuse *efuse = &rtwdev->efuse;
1246 struct rtw_chip_info *chip = rtwdev->chip;
1247 struct rtw_coex_rfe *coex_rfe = &coex->rfe;
1248 u8 table_case = 0xff, tdma_case = 0xff;
1250 if (coex_rfe->ant_switch_with_bt &&
1251 coex_dm->bt_status == COEX_BTSTATUS_NCON_IDLE) {
1252 if (efuse->share_ant &&
1253 COEX_RSSI_HIGH(coex_dm->wl_rssi_state[1])) {
1256 } else if (!efuse->share_ant) {
1262 if (table_case != 0xff && tdma_case != 0xff) {
1263 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G_FREERUN);
1264 rtw_coex_table(rtwdev, table_case);
1265 rtw_coex_tdma(rtwdev, false, tdma_case);
1269 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1271 if (efuse->share_ant) {
1273 if (!coex_stat->wl_gl_busy) {
1276 } else if (coex_dm->bt_status == COEX_BTSTATUS_NCON_IDLE) {
1284 /* Non-Shared-Ant */
1285 if (!coex_stat->wl_gl_busy) {
1288 } else if ((coex_stat->bt_ble_scan_type & 0x2) &&
1289 coex_dm->bt_status == COEX_BTSTATUS_NCON_IDLE) {
1298 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1299 rtw_coex_table(rtwdev, table_case);
1300 rtw_coex_tdma(rtwdev, false, tdma_case);
1303 static void rtw_coex_action_bt_inquiry(struct rtw_dev *rtwdev)
1305 struct rtw_coex *coex = &rtwdev->coex;
1306 struct rtw_coex_stat *coex_stat = &coex->stat;
1307 struct rtw_efuse *efuse = &rtwdev->efuse;
1308 struct rtw_chip_info *chip = rtwdev->chip;
1309 bool wl_hi_pri = false;
1310 u8 table_case, tdma_case;
1312 if (coex_stat->wl_linkscan_proc || coex_stat->wl_hi_pri_task1 ||
1313 coex_stat->wl_hi_pri_task2)
1316 if (efuse->share_ant) {
1320 if (coex_stat->bt_a2dp_exist &&
1321 !coex_stat->bt_pan_exist)
1323 else if (coex_stat->wl_hi_pri_task1)
1325 else if (!coex_stat->bt_page)
1329 } else if (coex_stat->wl_connected) {
1337 /* Non_Shared-Ant */
1340 if (coex_stat->bt_a2dp_exist &&
1341 !coex_stat->bt_pan_exist)
1343 else if (coex_stat->wl_hi_pri_task1)
1345 else if (!coex_stat->bt_page)
1349 } else if (coex_stat->wl_connected) {
1358 rtw_dbg(rtwdev, RTW_DBG_COEX, "coex: wifi hi(%d), bt page(%d)\n",
1359 wl_hi_pri, coex_stat->bt_page);
1361 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1362 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1363 rtw_coex_table(rtwdev, table_case);
1364 rtw_coex_tdma(rtwdev, false, tdma_case);
1367 static void rtw_coex_action_bt_hfp(struct rtw_dev *rtwdev)
1369 struct rtw_coex *coex = &rtwdev->coex;
1370 struct rtw_coex_stat *coex_stat = &coex->stat;
1371 struct rtw_efuse *efuse = &rtwdev->efuse;
1372 struct rtw_chip_info *chip = rtwdev->chip;
1373 u8 table_case, tdma_case;
1375 if (efuse->share_ant) {
1377 if (coex_stat->bt_multi_link) {
1385 /* Non-Shared-Ant */
1386 if (coex_stat->bt_multi_link) {
1395 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1396 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1397 rtw_coex_table(rtwdev, table_case);
1398 rtw_coex_tdma(rtwdev, false, tdma_case);
1401 static void rtw_coex_action_bt_hid(struct rtw_dev *rtwdev)
1403 struct rtw_coex *coex = &rtwdev->coex;
1404 struct rtw_coex_stat *coex_stat = &coex->stat;
1405 struct rtw_efuse *efuse = &rtwdev->efuse;
1406 struct rtw_chip_info *chip = rtwdev->chip;
1407 u8 table_case, tdma_case;
1410 wl_bw = rtwdev->hal.current_band_width;
1412 if (efuse->share_ant) {
1414 if (coex_stat->bt_ble_exist) {
1416 if (!coex_stat->wl_gl_busy)
1421 if (coex_stat->bt_a2dp_active || wl_bw == 0)
1423 else if (coex_stat->wl_gl_busy)
1428 if (coex_stat->bt_a2dp_active || wl_bw == 0) {
1433 if (coex_stat->bt_418_hid_exist &&
1434 coex_stat->wl_gl_busy)
1442 /* Non-Shared-Ant */
1443 if (coex_stat->bt_a2dp_active) {
1446 } else if (coex_stat->bt_ble_exist) {
1450 if (coex_stat->wl_gl_busy)
1460 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1461 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1462 rtw_coex_table(rtwdev, table_case);
1463 rtw_coex_tdma(rtwdev, false, tdma_case);
1466 static void rtw_coex_action_bt_a2dp(struct rtw_dev *rtwdev)
1468 struct rtw_coex *coex = &rtwdev->coex;
1469 struct rtw_coex_stat *coex_stat = &coex->stat;
1470 struct rtw_coex_dm *coex_dm = &coex->dm;
1471 struct rtw_efuse *efuse = &rtwdev->efuse;
1472 struct rtw_chip_info *chip = rtwdev->chip;
1473 u8 table_case, tdma_case;
1476 if (efuse->share_ant) {
1478 if (coex_stat->wl_gl_busy && coex_stat->wl_noisy_level == 0)
1483 slot_type = TDMA_4SLOT;
1485 if (coex_stat->wl_gl_busy)
1490 /* Non-Shared-Ant */
1493 if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[1]))
1499 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1500 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1501 rtw_coex_table(rtwdev, table_case);
1502 rtw_coex_tdma(rtwdev, false, tdma_case | slot_type);
1505 static void rtw_coex_action_bt_a2dpsink(struct rtw_dev *rtwdev)
1507 struct rtw_coex *coex = &rtwdev->coex;
1508 struct rtw_coex_stat *coex_stat = &coex->stat;
1509 struct rtw_efuse *efuse = &rtwdev->efuse;
1510 struct rtw_chip_info *chip = rtwdev->chip;
1511 u8 table_case, tdma_case;
1512 bool ap_enable = false;
1514 if (efuse->share_ant) { /* Shared-Ant */
1518 } else if (coex_stat->wl_gl_busy) {
1525 } else { /* Non-Shared-Ant */
1535 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1536 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1537 rtw_coex_table(rtwdev, table_case);
1538 rtw_coex_tdma(rtwdev, false, tdma_case);
1541 static void rtw_coex_action_bt_pan(struct rtw_dev *rtwdev)
1543 struct rtw_coex *coex = &rtwdev->coex;
1544 struct rtw_coex_stat *coex_stat = &coex->stat;
1545 struct rtw_efuse *efuse = &rtwdev->efuse;
1546 struct rtw_chip_info *chip = rtwdev->chip;
1547 u8 table_case, tdma_case;
1549 if (efuse->share_ant) {
1551 if (coex_stat->wl_gl_busy && coex_stat->wl_noisy_level == 0)
1556 if (coex_stat->wl_gl_busy)
1561 /* Non-Shared-Ant */
1564 if (coex_stat->wl_gl_busy)
1570 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1571 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1572 rtw_coex_table(rtwdev, table_case);
1573 rtw_coex_tdma(rtwdev, false, tdma_case);
1576 static void rtw_coex_action_bt_a2dp_hid(struct rtw_dev *rtwdev)
1578 struct rtw_coex *coex = &rtwdev->coex;
1579 struct rtw_coex_stat *coex_stat = &coex->stat;
1580 struct rtw_coex_dm *coex_dm = &coex->dm;
1581 struct rtw_efuse *efuse = &rtwdev->efuse;
1582 struct rtw_chip_info *chip = rtwdev->chip;
1583 u8 table_case, tdma_case;
1586 if (efuse->share_ant) {
1588 if (coex_stat->bt_ble_exist)
1593 if (coex_stat->wl_gl_busy) {
1594 slot_type = TDMA_4SLOT;
1600 /* Non-Shared-Ant */
1601 if (coex_stat->bt_ble_exist)
1606 if (COEX_RSSI_HIGH(coex_dm->wl_rssi_state[1]))
1612 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1613 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1614 rtw_coex_table(rtwdev, table_case);
1615 rtw_coex_tdma(rtwdev, false, tdma_case | slot_type);
1618 static void rtw_coex_action_bt_a2dp_pan(struct rtw_dev *rtwdev)
1620 struct rtw_coex *coex = &rtwdev->coex;
1621 struct rtw_coex_stat *coex_stat = &coex->stat;
1622 struct rtw_efuse *efuse = &rtwdev->efuse;
1623 struct rtw_chip_info *chip = rtwdev->chip;
1624 u8 table_case, tdma_case;
1626 if (efuse->share_ant) {
1628 if (coex_stat->wl_gl_busy &&
1629 coex_stat->wl_noisy_level == 0)
1634 if (coex_stat->wl_gl_busy)
1639 /* Non-Shared-Ant */
1642 if (coex_stat->wl_gl_busy)
1648 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1649 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1650 rtw_coex_table(rtwdev, table_case);
1651 rtw_coex_tdma(rtwdev, false, tdma_case);
1654 static void rtw_coex_action_bt_pan_hid(struct rtw_dev *rtwdev)
1656 struct rtw_coex *coex = &rtwdev->coex;
1657 struct rtw_coex_stat *coex_stat = &coex->stat;
1658 struct rtw_efuse *efuse = &rtwdev->efuse;
1659 struct rtw_chip_info *chip = rtwdev->chip;
1660 u8 table_case, tdma_case;
1662 if (efuse->share_ant) {
1666 if (coex_stat->wl_gl_busy)
1671 /* Non-Shared-Ant */
1674 if (coex_stat->wl_gl_busy)
1680 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1681 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1682 rtw_coex_table(rtwdev, table_case);
1683 rtw_coex_tdma(rtwdev, false, tdma_case);
1686 static void rtw_coex_action_bt_a2dp_pan_hid(struct rtw_dev *rtwdev)
1688 struct rtw_coex *coex = &rtwdev->coex;
1689 struct rtw_coex_stat *coex_stat = &coex->stat;
1690 struct rtw_efuse *efuse = &rtwdev->efuse;
1691 struct rtw_chip_info *chip = rtwdev->chip;
1692 u8 table_case, tdma_case;
1694 if (efuse->share_ant) {
1698 if (coex_stat->wl_gl_busy)
1703 /* Non-Shared-Ant */
1706 if (coex_stat->wl_gl_busy)
1712 rtw_coex_set_ant_path(rtwdev, false, COEX_SET_ANT_2G);
1713 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1714 rtw_coex_table(rtwdev, table_case);
1715 rtw_coex_tdma(rtwdev, false, tdma_case);
1718 static void rtw_coex_action_wl_under5g(struct rtw_dev *rtwdev)
1720 struct rtw_efuse *efuse = &rtwdev->efuse;
1721 struct rtw_chip_info *chip = rtwdev->chip;
1722 u8 table_case, tdma_case;
1724 rtw_coex_write_scbd(rtwdev, COEX_SCBD_FIX2M, false);
1726 if (efuse->share_ant) {
1731 /* Non-Shared-Ant */
1736 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_5G);
1737 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1738 rtw_coex_table(rtwdev, table_case);
1739 rtw_coex_tdma(rtwdev, false, tdma_case);
1742 static void rtw_coex_action_wl_only(struct rtw_dev *rtwdev)
1744 struct rtw_efuse *efuse = &rtwdev->efuse;
1745 struct rtw_chip_info *chip = rtwdev->chip;
1746 u8 table_case, tdma_case;
1748 if (efuse->share_ant) {
1753 /* Non-Shared-Ant */
1758 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
1759 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1760 rtw_coex_table(rtwdev, table_case);
1761 rtw_coex_tdma(rtwdev, false, tdma_case);
1764 static void rtw_coex_action_wl_native_lps(struct rtw_dev *rtwdev)
1766 struct rtw_coex *coex = &rtwdev->coex;
1767 struct rtw_efuse *efuse = &rtwdev->efuse;
1768 struct rtw_chip_info *chip = rtwdev->chip;
1769 u8 table_case, tdma_case;
1774 if (efuse->share_ant) {
1779 /* Non-Shared-Ant */
1784 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
1785 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1786 rtw_coex_table(rtwdev, table_case);
1787 rtw_coex_tdma(rtwdev, false, tdma_case);
1790 static void rtw_coex_action_wl_linkscan(struct rtw_dev *rtwdev)
1792 struct rtw_coex *coex = &rtwdev->coex;
1793 struct rtw_coex_stat *coex_stat = &coex->stat;
1794 struct rtw_efuse *efuse = &rtwdev->efuse;
1795 struct rtw_chip_info *chip = rtwdev->chip;
1796 u8 table_case, tdma_case;
1798 if (efuse->share_ant) {
1800 if (coex_stat->bt_a2dp_exist) {
1808 /* Non-Shared-Ant */
1809 if (coex_stat->bt_a2dp_exist) {
1818 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
1819 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1820 rtw_coex_table(rtwdev, table_case);
1821 rtw_coex_tdma(rtwdev, false, tdma_case);
1824 static void rtw_coex_action_wl_not_connected(struct rtw_dev *rtwdev)
1826 struct rtw_efuse *efuse = &rtwdev->efuse;
1827 struct rtw_chip_info *chip = rtwdev->chip;
1828 u8 table_case, tdma_case;
1830 if (efuse->share_ant) {
1835 /* Non-Shared-Ant */
1840 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
1841 rtw_coex_set_rf_para(rtwdev, chip->wl_rf_para_rx[0]);
1842 rtw_coex_table(rtwdev, table_case);
1843 rtw_coex_tdma(rtwdev, false, tdma_case);
1846 static void rtw_coex_action_wl_connected(struct rtw_dev *rtwdev)
1848 struct rtw_coex *coex = &rtwdev->coex;
1849 struct rtw_coex_stat *coex_stat = &coex->stat;
1850 struct rtw_coex_dm *coex_dm = &coex->dm;
1851 struct rtw_efuse *efuse = &rtwdev->efuse;
1854 /* Non-Shared-Ant */
1855 if (!efuse->share_ant && coex_stat->wl_gl_busy &&
1856 COEX_RSSI_HIGH(coex_dm->wl_rssi_state[3]) &&
1857 COEX_RSSI_HIGH(coex_dm->bt_rssi_state[0])) {
1858 rtw_coex_action_freerun(rtwdev);
1862 algorithm = rtw_coex_algorithm(rtwdev);
1864 switch (algorithm) {
1866 rtw_coex_action_bt_hfp(rtwdev);
1869 rtw_coex_action_bt_hid(rtwdev);
1871 case COEX_ALGO_A2DP:
1872 if (coex_stat->bt_a2dp_sink)
1873 rtw_coex_action_bt_a2dpsink(rtwdev);
1875 rtw_coex_action_bt_a2dp(rtwdev);
1878 rtw_coex_action_bt_pan(rtwdev);
1880 case COEX_ALGO_A2DP_HID:
1881 rtw_coex_action_bt_a2dp_hid(rtwdev);
1883 case COEX_ALGO_A2DP_PAN:
1884 rtw_coex_action_bt_a2dp_pan(rtwdev);
1886 case COEX_ALGO_PAN_HID:
1887 rtw_coex_action_bt_pan_hid(rtwdev);
1889 case COEX_ALGO_A2DP_PAN_HID:
1890 rtw_coex_action_bt_a2dp_pan_hid(rtwdev);
1893 case COEX_ALGO_NOPROFILE:
1894 rtw_coex_action_bt_idle(rtwdev);
1899 static void rtw_coex_run_coex(struct rtw_dev *rtwdev, u8 reason)
1901 struct rtw_coex *coex = &rtwdev->coex;
1902 struct rtw_coex_dm *coex_dm = &coex->dm;
1903 struct rtw_coex_stat *coex_stat = &coex->stat;
1905 lockdep_assert_held(&rtwdev->mutex);
1907 coex_dm->reason = reason;
1909 /* update wifi_link_info_ext variable */
1910 rtw_coex_update_wl_link_info(rtwdev, reason);
1912 rtw_coex_monitor_bt_enable(rtwdev);
1917 if (coex_stat->wl_under_ips)
1920 if (coex->freeze && !coex_stat->bt_setup_link)
1923 coex_stat->cnt_wl[COEX_CNT_WL_COEXRUN]++;
1924 coex->freerun = false;
1926 /* Pure-5G Coex Process */
1927 if (coex->under_5g) {
1928 coex_stat->wl_coex_mode = COEX_WLINK_5G;
1929 rtw_coex_action_wl_under5g(rtwdev);
1933 coex_stat->wl_coex_mode = COEX_WLINK_2G1PORT;
1934 rtw_coex_write_scbd(rtwdev, COEX_SCBD_FIX2M, false);
1935 if (coex_stat->bt_disabled) {
1936 rtw_coex_action_wl_only(rtwdev);
1940 if (coex_stat->wl_under_lps && !coex_stat->wl_force_lps_ctrl) {
1941 rtw_coex_action_wl_native_lps(rtwdev);
1945 if (coex_stat->bt_whck_test) {
1946 rtw_coex_action_bt_whql_test(rtwdev);
1950 if (coex_stat->bt_setup_link) {
1951 rtw_coex_action_bt_relink(rtwdev);
1955 if (coex_stat->bt_inq_page) {
1956 rtw_coex_action_bt_inquiry(rtwdev);
1960 if ((coex_dm->bt_status == COEX_BTSTATUS_NCON_IDLE ||
1961 coex_dm->bt_status == COEX_BTSTATUS_CON_IDLE) &&
1962 coex_stat->wl_connected) {
1963 rtw_coex_action_bt_idle(rtwdev);
1967 if (coex_stat->wl_linkscan_proc) {
1968 rtw_coex_action_wl_linkscan(rtwdev);
1972 if (coex_stat->wl_connected)
1973 rtw_coex_action_wl_connected(rtwdev);
1975 rtw_coex_action_wl_not_connected(rtwdev);
1978 rtw_coex_set_gnt_fix(rtwdev);
1979 rtw_coex_limited_wl(rtwdev);
1982 static void rtw_coex_init_coex_var(struct rtw_dev *rtwdev)
1984 struct rtw_coex *coex = &rtwdev->coex;
1985 struct rtw_coex_stat *coex_stat = &coex->stat;
1986 struct rtw_coex_dm *coex_dm = &coex->dm;
1989 memset(coex_dm, 0, sizeof(*coex_dm));
1990 memset(coex_stat, 0, sizeof(*coex_stat));
1992 for (i = 0; i < COEX_CNT_WL_MAX; i++)
1993 coex_stat->cnt_wl[i] = 0;
1995 for (i = 0; i < COEX_CNT_BT_MAX; i++)
1996 coex_stat->cnt_bt[i] = 0;
1998 for (i = 0; i < ARRAY_SIZE(coex_dm->bt_rssi_state); i++)
1999 coex_dm->bt_rssi_state[i] = COEX_RSSI_STATE_LOW;
2001 for (i = 0; i < ARRAY_SIZE(coex_dm->wl_rssi_state); i++)
2002 coex_dm->wl_rssi_state[i] = COEX_RSSI_STATE_LOW;
2004 coex_stat->wl_coex_mode = COEX_WLINK_MAX;
2007 static void __rtw_coex_init_hw_config(struct rtw_dev *rtwdev, bool wifi_only)
2009 struct rtw_coex *coex = &rtwdev->coex;
2011 rtw_coex_init_coex_var(rtwdev);
2012 rtw_coex_monitor_bt_enable(rtwdev);
2013 rtw_coex_set_rfe_type(rtwdev);
2014 rtw_coex_set_init(rtwdev);
2016 /* set Tx response = Hi-Pri (ex: Transmitting ACK,BA,CTS) */
2017 rtw_coex_set_wl_pri_mask(rtwdev, COEX_WLPRI_TX_RSP, 1);
2019 /* set Tx beacon = Hi-Pri */
2020 rtw_coex_set_wl_pri_mask(rtwdev, COEX_WLPRI_TX_BEACON, 1);
2022 /* set Tx beacon queue = Hi-Pri */
2023 rtw_coex_set_wl_pri_mask(rtwdev, COEX_WLPRI_TX_BEACONQ, 1);
2025 /* antenna config */
2026 if (coex->wl_rf_off) {
2027 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_WOFF);
2028 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ALL, false);
2029 coex->stop_dm = true;
2030 } else if (wifi_only) {
2031 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_WONLY);
2032 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE | COEX_SCBD_SCAN,
2034 coex->stop_dm = true;
2036 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_INIT);
2037 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE | COEX_SCBD_SCAN,
2039 coex->stop_dm = false;
2040 coex->freeze = true;
2044 rtw_coex_table(rtwdev, 0);
2045 rtw_coex_tdma(rtwdev, true, 0);
2046 rtw_coex_query_bt_info(rtwdev);
2049 void rtw_coex_power_on_setting(struct rtw_dev *rtwdev)
2051 struct rtw_coex *coex = &rtwdev->coex;
2053 coex->stop_dm = true;
2054 coex->wl_rf_off = false;
2056 /* enable BB, we can write 0x948 */
2057 rtw_write8_set(rtwdev, REG_SYS_FUNC_EN, BIT(0) | BIT(1));
2059 rtw_coex_monitor_bt_enable(rtwdev);
2060 rtw_coex_set_rfe_type(rtwdev);
2062 /* set antenna path to BT */
2063 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_POWERON);
2066 rtw_write8(rtwdev, 0xff1a, 0x0);
2069 void rtw_coex_init_hw_config(struct rtw_dev *rtwdev, bool wifi_only)
2071 __rtw_coex_init_hw_config(rtwdev, wifi_only);
2074 void rtw_coex_ips_notify(struct rtw_dev *rtwdev, u8 type)
2076 struct rtw_coex *coex = &rtwdev->coex;
2077 struct rtw_coex_stat *coex_stat = &coex->stat;
2082 if (type == COEX_IPS_ENTER) {
2083 coex_stat->wl_under_ips = true;
2086 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ALL, false);
2088 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_WOFF);
2089 rtw_coex_action_coex_all_off(rtwdev);
2090 } else if (type == COEX_IPS_LEAVE) {
2091 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE | COEX_SCBD_ONOFF, true);
2093 /* run init hw config (exclude wifi only) */
2094 __rtw_coex_init_hw_config(rtwdev, false);
2097 coex_stat->wl_under_ips = false;
2101 void rtw_coex_lps_notify(struct rtw_dev *rtwdev, u8 type)
2103 struct rtw_coex *coex = &rtwdev->coex;
2104 struct rtw_coex_stat *coex_stat = &coex->stat;
2109 if (type == COEX_LPS_ENABLE) {
2110 coex_stat->wl_under_lps = true;
2112 if (coex_stat->wl_force_lps_ctrl) {
2114 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, true);
2117 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, false);
2119 rtw_coex_run_coex(rtwdev, COEX_RSN_LPS);
2121 } else if (type == COEX_LPS_DISABLE) {
2122 coex_stat->wl_under_lps = false;
2125 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, true);
2127 if (!coex_stat->wl_force_lps_ctrl)
2128 rtw_coex_query_bt_info(rtwdev);
2132 void rtw_coex_scan_notify(struct rtw_dev *rtwdev, u8 type)
2134 struct rtw_coex *coex = &rtwdev->coex;
2135 struct rtw_coex_stat *coex_stat = &coex->stat;
2140 coex->freeze = false;
2142 if (type != COEX_SCAN_FINISH)
2143 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE | COEX_SCBD_SCAN |
2144 COEX_SCBD_ONOFF, true);
2146 if (type == COEX_SCAN_START_5G) {
2147 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_5G);
2148 rtw_coex_run_coex(rtwdev, COEX_RSN_5GSCANSTART);
2149 } else if ((type == COEX_SCAN_START_2G) || (type == COEX_SCAN_START)) {
2150 coex_stat->wl_hi_pri_task2 = true;
2152 /* Force antenna setup for no scan result issue */
2153 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
2154 rtw_coex_run_coex(rtwdev, COEX_RSN_2GSCANSTART);
2156 coex_stat->wl_hi_pri_task2 = false;
2157 rtw_coex_run_coex(rtwdev, COEX_RSN_SCANFINISH);
2161 void rtw_coex_switchband_notify(struct rtw_dev *rtwdev, u8 type)
2163 struct rtw_coex *coex = &rtwdev->coex;
2168 if (type == COEX_SWITCH_TO_5G)
2169 rtw_coex_run_coex(rtwdev, COEX_RSN_5GSWITCHBAND);
2170 else if (type == COEX_SWITCH_TO_24G_NOFORSCAN)
2171 rtw_coex_run_coex(rtwdev, COEX_RSN_2GSWITCHBAND);
2173 rtw_coex_scan_notify(rtwdev, COEX_SCAN_START_2G);
2176 void rtw_coex_connect_notify(struct rtw_dev *rtwdev, u8 type)
2178 struct rtw_coex *coex = &rtwdev->coex;
2179 struct rtw_coex_stat *coex_stat = &coex->stat;
2184 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE | COEX_SCBD_SCAN |
2185 COEX_SCBD_ONOFF, true);
2187 if (type == COEX_ASSOCIATE_5G_START) {
2188 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_5G);
2189 rtw_coex_run_coex(rtwdev, COEX_RSN_5GCONSTART);
2190 } else if (type == COEX_ASSOCIATE_5G_FINISH) {
2191 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_5G);
2192 rtw_coex_run_coex(rtwdev, COEX_RSN_5GCONFINISH);
2193 } else if (type == COEX_ASSOCIATE_START) {
2194 coex_stat->wl_hi_pri_task1 = true;
2195 coex_stat->cnt_wl[COEX_CNT_WL_CONNPKT] = 2;
2197 /* Force antenna setup for no scan result issue */
2198 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
2200 rtw_coex_run_coex(rtwdev, COEX_RSN_2GCONSTART);
2202 /* To keep TDMA case during connect process,
2203 * to avoid changed by Btinfo and runcoexmechanism
2205 coex->freeze = true;
2206 ieee80211_queue_delayed_work(rtwdev->hw, &coex->defreeze_work,
2209 coex_stat->wl_hi_pri_task1 = false;
2210 coex->freeze = false;
2212 rtw_coex_run_coex(rtwdev, COEX_RSN_2GCONFINISH);
2216 void rtw_coex_media_status_notify(struct rtw_dev *rtwdev, u8 type)
2218 struct rtw_coex *coex = &rtwdev->coex;
2219 struct rtw_coex_stat *coex_stat = &coex->stat;
2225 if (type == COEX_MEDIA_CONNECT_5G) {
2226 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, true);
2228 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_5G);
2229 rtw_coex_run_coex(rtwdev, COEX_RSN_5GMEDIA);
2230 } else if (type == COEX_MEDIA_CONNECT) {
2231 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, true);
2233 /* Force antenna setup for no scan result issue */
2234 rtw_coex_set_ant_path(rtwdev, true, COEX_SET_ANT_2G);
2236 /* Set CCK Rx high Pri */
2237 rtw_coex_set_wl_pri_mask(rtwdev, COEX_WLPRI_RX_CCK, 1);
2239 /* always enable 5ms extend if connect */
2240 para[0] = COEX_H2C69_WL_LEAKAP;
2241 para[1] = PARA1_H2C69_EN_5MS; /* enable 5ms extend */
2242 rtw_fw_bt_wifi_control(rtwdev, para[0], ¶[1]);
2243 coex_stat->wl_slot_extend = true;
2244 rtw_coex_run_coex(rtwdev, COEX_RSN_2GMEDIA);
2246 rtw_coex_write_scbd(rtwdev, COEX_SCBD_ACTIVE, false);
2248 rtw_coex_set_wl_pri_mask(rtwdev, COEX_WLPRI_RX_CCK, 0);
2250 rtw_coex_run_coex(rtwdev, COEX_RSN_MEDIADISCON);
2253 rtw_coex_update_wl_ch_info(rtwdev, type);
2256 void rtw_coex_bt_info_notify(struct rtw_dev *rtwdev, u8 *buf, u8 length)
2258 struct rtw_coex *coex = &rtwdev->coex;
2259 struct rtw_coex_stat *coex_stat = &coex->stat;
2260 struct rtw_chip_info *chip = rtwdev->chip;
2261 unsigned long bt_relink_time;
2262 u8 i, rsp_source = 0, type;
2264 rsp_source = buf[0] & 0xf;
2265 if (rsp_source >= COEX_BTINFO_SRC_MAX)
2266 rsp_source = COEX_BTINFO_SRC_WL_FW;
2268 if (rsp_source == COEX_BTINFO_SRC_BT_IQK) {
2269 coex_stat->bt_iqk_state = buf[1];
2270 if (coex_stat->bt_iqk_state == 1)
2271 coex_stat->cnt_bt[COEX_CNT_BT_IQK]++;
2272 else if (coex_stat->bt_iqk_state == 2)
2273 coex_stat->cnt_bt[COEX_CNT_BT_IQKFAIL]++;
2278 if (rsp_source == COEX_BTINFO_SRC_BT_SCBD) {
2279 rtw_coex_monitor_bt_enable(rtwdev);
2280 if (coex_stat->bt_disabled != coex_stat->bt_disabled_pre) {
2281 coex_stat->bt_disabled_pre = coex_stat->bt_disabled;
2282 rtw_coex_run_coex(rtwdev, COEX_RSN_BTINFO);
2287 if (rsp_source == COEX_BTINFO_SRC_BT_RSP ||
2288 rsp_source == COEX_BTINFO_SRC_BT_ACT) {
2289 if (coex_stat->bt_disabled) {
2290 coex_stat->bt_disabled = false;
2291 coex_stat->bt_reenable = true;
2292 ieee80211_queue_delayed_work(rtwdev->hw,
2293 &coex->bt_reenable_work,
2298 for (i = 0; i < length; i++) {
2299 if (i < COEX_BTINFO_LENGTH_MAX)
2300 coex_stat->bt_info_c2h[rsp_source][i] = buf[i];
2305 if (rsp_source == COEX_BTINFO_SRC_WL_FW) {
2306 rtw_coex_update_bt_link_info(rtwdev);
2307 rtw_coex_run_coex(rtwdev, COEX_RSN_BTINFO);
2311 /* get the same info from bt, skip it */
2312 if (coex_stat->bt_info_c2h[rsp_source][1] == coex_stat->bt_info_lb2 &&
2313 coex_stat->bt_info_c2h[rsp_source][2] == coex_stat->bt_info_lb3 &&
2314 coex_stat->bt_info_c2h[rsp_source][3] == coex_stat->bt_info_hb0 &&
2315 coex_stat->bt_info_c2h[rsp_source][4] == coex_stat->bt_info_hb1 &&
2316 coex_stat->bt_info_c2h[rsp_source][5] == coex_stat->bt_info_hb2 &&
2317 coex_stat->bt_info_c2h[rsp_source][6] == coex_stat->bt_info_hb3)
2320 coex_stat->bt_info_lb2 = coex_stat->bt_info_c2h[rsp_source][1];
2321 coex_stat->bt_info_lb3 = coex_stat->bt_info_c2h[rsp_source][2];
2322 coex_stat->bt_info_hb0 = coex_stat->bt_info_c2h[rsp_source][3];
2323 coex_stat->bt_info_hb1 = coex_stat->bt_info_c2h[rsp_source][4];
2324 coex_stat->bt_info_hb2 = coex_stat->bt_info_c2h[rsp_source][5];
2325 coex_stat->bt_info_hb3 = coex_stat->bt_info_c2h[rsp_source][6];
2327 /* 0xff means BT is under WHCK test */
2328 coex_stat->bt_whck_test = (coex_stat->bt_info_lb2 == 0xff);
2329 coex_stat->bt_inq_page = ((coex_stat->bt_info_lb2 & BIT(2)) == BIT(2));
2330 coex_stat->bt_acl_busy = ((coex_stat->bt_info_lb2 & BIT(3)) == BIT(3));
2331 coex_stat->cnt_bt[COEX_CNT_BT_RETRY] = coex_stat->bt_info_lb3 & 0xf;
2332 if (coex_stat->cnt_bt[COEX_CNT_BT_RETRY] >= 1)
2333 coex_stat->cnt_bt[COEX_CNT_BT_POPEVENT]++;
2335 coex_stat->bt_fix_2M = ((coex_stat->bt_info_lb3 & BIT(4)) == BIT(4));
2336 coex_stat->bt_inq = ((coex_stat->bt_info_lb3 & BIT(5)) == BIT(5));
2337 if (coex_stat->bt_inq)
2338 coex_stat->cnt_bt[COEX_CNT_BT_INQ]++;
2340 coex_stat->bt_page = ((coex_stat->bt_info_lb3 & BIT(7)) == BIT(7));
2341 if (coex_stat->bt_page) {
2342 coex_stat->cnt_bt[COEX_CNT_BT_PAGE]++;
2343 if (coex_stat->wl_linkscan_proc ||
2344 coex_stat->wl_hi_pri_task1 ||
2345 coex_stat->wl_hi_pri_task2 || coex_stat->wl_gl_busy)
2346 rtw_coex_write_scbd(rtwdev, COEX_SCBD_SCAN, true);
2348 rtw_coex_write_scbd(rtwdev, COEX_SCBD_SCAN, false);
2350 rtw_coex_write_scbd(rtwdev, COEX_SCBD_SCAN, false);
2353 /* unit: % (value-100 to translate to unit: dBm in coex info) */
2354 if (chip->bt_rssi_type == COEX_BTRSSI_RATIO) {
2355 coex_stat->bt_rssi = coex_stat->bt_info_hb0 * 2 + 10;
2356 } else { /* original unit: dbm -> unit: % -> value-100 in coex info */
2357 if (coex_stat->bt_info_hb0 <= 127)
2358 coex_stat->bt_rssi = 100;
2359 else if (256 - coex_stat->bt_info_hb0 <= 100)
2360 coex_stat->bt_rssi = 100 - (256 - coex_stat->bt_info_hb0);
2362 coex_stat->bt_rssi = 0;
2365 coex_stat->bt_ble_exist = ((coex_stat->bt_info_hb1 & BIT(0)) == BIT(0));
2366 if (coex_stat->bt_info_hb1 & BIT(1))
2367 coex_stat->cnt_bt[COEX_CNT_BT_REINIT]++;
2369 if (coex_stat->bt_info_hb1 & BIT(2)) {
2370 coex_stat->cnt_bt[COEX_CNT_BT_SETUPLINK]++;
2371 coex_stat->bt_setup_link = true;
2372 if (coex_stat->bt_reenable)
2373 bt_relink_time = 6 * HZ;
2375 bt_relink_time = 2 * HZ;
2377 ieee80211_queue_delayed_work(rtwdev->hw,
2378 &coex->bt_relink_work,
2382 if (coex_stat->bt_info_hb1 & BIT(3))
2383 coex_stat->cnt_bt[COEX_CNT_BT_IGNWLANACT]++;
2385 coex_stat->bt_ble_voice = ((coex_stat->bt_info_hb1 & BIT(4)) == BIT(4));
2386 coex_stat->bt_ble_scan_en = ((coex_stat->bt_info_hb1 & BIT(5)) == BIT(5));
2387 if (coex_stat->bt_info_hb1 & BIT(6))
2388 coex_stat->cnt_bt[COEX_CNT_BT_ROLESWITCH]++;
2390 coex_stat->bt_multi_link = ((coex_stat->bt_info_hb1 & BIT(7)) == BIT(7));
2391 /* resend wifi info to bt, it is reset and lost the info */
2392 if ((coex_stat->bt_info_hb1 & BIT(1))) {
2393 if (coex_stat->wl_connected)
2394 type = COEX_MEDIA_CONNECT;
2396 type = COEX_MEDIA_DISCONNECT;
2397 rtw_coex_update_wl_ch_info(rtwdev, type);
2400 /* if ignore_wlan_act && not set_up_link */
2401 if ((coex_stat->bt_info_hb1 & BIT(3)) &&
2402 (!(coex_stat->bt_info_hb1 & BIT(2))))
2403 rtw_coex_ignore_wlan_act(rtwdev, false);
2405 coex_stat->bt_opp_exist = ((coex_stat->bt_info_hb2 & BIT(0)) == BIT(0));
2406 if (coex_stat->bt_info_hb2 & BIT(1))
2407 coex_stat->cnt_bt[COEX_CNT_BT_AFHUPDATE]++;
2409 coex_stat->bt_a2dp_active = (coex_stat->bt_info_hb2 & BIT(2)) == BIT(2);
2410 coex_stat->bt_slave = ((coex_stat->bt_info_hb2 & BIT(3)) == BIT(3));
2411 coex_stat->bt_hid_slot = (coex_stat->bt_info_hb2 & 0x30) >> 4;
2412 coex_stat->bt_hid_pair_num = (coex_stat->bt_info_hb2 & 0xc0) >> 6;
2413 if (coex_stat->bt_hid_pair_num > 0 && coex_stat->bt_hid_slot >= 2)
2414 coex_stat->bt_418_hid_exist = true;
2415 else if (coex_stat->bt_hid_pair_num == 0)
2416 coex_stat->bt_418_hid_exist = false;
2418 if ((coex_stat->bt_info_lb2 & 0x49) == 0x49)
2419 coex_stat->bt_a2dp_bitpool = (coex_stat->bt_info_hb3 & 0x7f);
2421 coex_stat->bt_a2dp_bitpool = 0;
2423 coex_stat->bt_a2dp_sink = ((coex_stat->bt_info_hb3 & BIT(7)) == BIT(7));
2425 rtw_coex_update_bt_link_info(rtwdev);
2426 rtw_coex_run_coex(rtwdev, COEX_RSN_BTINFO);
2429 void rtw_coex_wl_fwdbginfo_notify(struct rtw_dev *rtwdev, u8 *buf, u8 length)
2431 struct rtw_coex *coex = &rtwdev->coex;
2432 struct rtw_coex_stat *coex_stat = &coex->stat;
2436 if (WARN(length < 8, "invalid wl info c2h length\n"))
2442 for (i = 1; i < 8; i++) {
2443 val = coex_stat->wl_fw_dbg_info_pre[i];
2445 coex_stat->wl_fw_dbg_info[i] = buf[i] - val;
2447 coex_stat->wl_fw_dbg_info[i] = val - buf[i];
2449 coex_stat->wl_fw_dbg_info_pre[i] = buf[i];
2452 coex_stat->cnt_wl[COEX_CNT_WL_FW_NOTIFY]++;
2453 rtw_coex_wl_ccklock_action(rtwdev);
2454 rtw_coex_wl_ccklock_detect(rtwdev);
2457 void rtw_coex_wl_status_change_notify(struct rtw_dev *rtwdev)
2459 struct rtw_coex *coex = &rtwdev->coex;
2464 rtw_coex_run_coex(rtwdev, COEX_RSN_WLSTATUS);
2467 void rtw_coex_bt_relink_work(struct work_struct *work)
2469 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
2470 coex.bt_relink_work.work);
2471 struct rtw_coex_stat *coex_stat = &rtwdev->coex.stat;
2473 mutex_lock(&rtwdev->mutex);
2474 coex_stat->bt_setup_link = false;
2475 rtw_coex_run_coex(rtwdev, COEX_RSN_WLSTATUS);
2476 mutex_unlock(&rtwdev->mutex);
2479 void rtw_coex_bt_reenable_work(struct work_struct *work)
2481 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
2482 coex.bt_reenable_work.work);
2483 struct rtw_coex_stat *coex_stat = &rtwdev->coex.stat;
2485 mutex_lock(&rtwdev->mutex);
2486 coex_stat->bt_reenable = false;
2487 mutex_unlock(&rtwdev->mutex);
2490 void rtw_coex_defreeze_work(struct work_struct *work)
2492 struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
2493 coex.defreeze_work.work);
2494 struct rtw_coex *coex = &rtwdev->coex;
2495 struct rtw_coex_stat *coex_stat = &rtwdev->coex.stat;
2497 mutex_lock(&rtwdev->mutex);
2498 coex->freeze = false;
2499 coex_stat->wl_hi_pri_task1 = false;
2500 rtw_coex_run_coex(rtwdev, COEX_RSN_WLSTATUS);
2501 mutex_unlock(&rtwdev->mutex);