Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2012  Realtek Corporation.*/
   3
   4/**************************************************************
   5 * Description:
   6 *
   7 * This file is for RTL8821A Co-exist mechanism
   8 *
   9 * History
  10 * 2012/11/15 Cosa first check in.
  11 *
  12 **************************************************************/
  13
  14/**************************************************************
  15 * include files
  16 **************************************************************/
  17#include "halbt_precomp.h"
  18/**************************************************************
  19 * Global variables, these are static variables
  20 **************************************************************/
  21static struct coex_dm_8821a_1ant glcoex_dm_8821a_1ant;
  22static struct coex_dm_8821a_1ant *coex_dm = &glcoex_dm_8821a_1ant;
  23static struct coex_sta_8821a_1ant glcoex_sta_8821a_1ant;
  24static struct coex_sta_8821a_1ant *coex_sta = &glcoex_sta_8821a_1ant;
  25static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
  26						  u8 wifi_status);
  27
  28static const char *const glbt_info_src_8821a_1ant[] = {
  29	  "BT Info[wifi fw]",
  30	  "BT Info[bt rsp]",
  31	  "BT Info[bt auto report]",
  32};
  33
  34static u32 glcoex_ver_date_8821a_1ant = 20130816;
  35static u32 glcoex_ver_8821a_1ant = 0x41;
  36
  37/**************************************************************
  38 * local function proto type if needed
  39 *
  40 * local function start with btc8821a1ant_
  41 **************************************************************/
  42static u8 btc8821a1ant_bt_rssi_state(struct btc_coexist *btcoexist,
  43				     u8 level_num, u8 rssi_thresh,
  44				     u8 rssi_thresh1)
  45{
  46	struct rtl_priv *rtlpriv = btcoexist->adapter;
  47	long bt_rssi = 0;
  48	u8 bt_rssi_state = coex_sta->pre_bt_rssi_state;
  49
  50	bt_rssi = coex_sta->bt_rssi;
  51
  52	if (level_num == 2) {
  53		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
  54		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
  55			if (bt_rssi >= (rssi_thresh +
  56					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
  57				bt_rssi_state = BTC_RSSI_STATE_HIGH;
  58				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  59					"[BTCoex], BT Rssi state switch to High\n");
  60			} else {
  61				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
  62				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  63					"[BTCoex], BT Rssi state stay at Low\n");
  64			}
  65		} else {
  66			if (bt_rssi < rssi_thresh) {
  67				bt_rssi_state = BTC_RSSI_STATE_LOW;
  68				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  69					"[BTCoex], BT Rssi state switch to Low\n");
  70			} else {
  71				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
  72				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  73					"[BTCoex], BT Rssi state stay at High\n");
  74			}
  75		}
  76	} else if (level_num == 3) {
  77		if (rssi_thresh > rssi_thresh1) {
  78			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  79				"[BTCoex], BT Rssi thresh error!!\n");
  80			return coex_sta->pre_bt_rssi_state;
  81		}
  82
  83		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
  84		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
  85			if (bt_rssi >= (rssi_thresh +
  86					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
  87				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
  88				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  89					"[BTCoex], BT Rssi state switch to Medium\n");
  90			} else {
  91				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
  92				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  93					"[BTCoex], BT Rssi state stay at Low\n");
  94			}
  95		} else if ((coex_sta->pre_bt_rssi_state ==
  96			   BTC_RSSI_STATE_MEDIUM) ||
  97			   (coex_sta->pre_bt_rssi_state ==
  98			    BTC_RSSI_STATE_STAY_MEDIUM)) {
  99			if (bt_rssi >= (rssi_thresh1 +
 100					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 101				bt_rssi_state = BTC_RSSI_STATE_HIGH;
 102				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 103					"[BTCoex], BT Rssi state switch to High\n");
 104			} else if (bt_rssi < rssi_thresh) {
 105				bt_rssi_state = BTC_RSSI_STATE_LOW;
 106				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 107					"[BTCoex], BT Rssi state switch to Low\n");
 108			} else {
 109				bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
 110				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 111					"[BTCoex], BT Rssi state stay at Medium\n");
 112			}
 113		} else {
 114			if (bt_rssi < rssi_thresh1) {
 115				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
 116				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 117					"[BTCoex], BT Rssi state switch to Medium\n");
 118			} else {
 119				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 120				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 121					"[BTCoex], BT Rssi state stay at High\n");
 122			}
 123		}
 124	}
 125	coex_sta->pre_bt_rssi_state = bt_rssi_state;
 126
 127	return bt_rssi_state;
 128}
 129
 130static u8 btc8821a1ant_wifi_rssi_state(struct btc_coexist *btcoexist,
 131				       u8 index, u8 level_num, u8 rssi_thresh,
 132				       u8 rssi_thresh1)
 133{
 134	struct rtl_priv *rtlpriv = btcoexist->adapter;
 135	long	wifi_rssi = 0;
 136	u8	wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
 137
 138	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
 139
 140	if (level_num == 2) {
 141		if ((coex_sta->pre_wifi_rssi_state[index] ==
 142		     BTC_RSSI_STATE_LOW) ||
 143		    (coex_sta->pre_wifi_rssi_state[index] ==
 144		     BTC_RSSI_STATE_STAY_LOW)) {
 145			if (wifi_rssi >= (rssi_thresh +
 146					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 147				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
 148				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 149					"[BTCoex], wifi RSSI state switch to High\n");
 150			} else {
 151				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
 152				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 153					"[BTCoex], wifi RSSI state stay at Low\n");
 154			}
 155		} else {
 156			if (wifi_rssi < rssi_thresh) {
 157				wifi_rssi_state = BTC_RSSI_STATE_LOW;
 158				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 159					"[BTCoex], wifi RSSI state switch to Low\n");
 160			} else {
 161				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 162				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 163					"[BTCoex], wifi RSSI state stay at High\n");
 164			}
 165		}
 166	} else if (level_num == 3) {
 167		if (rssi_thresh > rssi_thresh1) {
 168			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 169				"[BTCoex], wifi RSSI thresh error!!\n");
 170			return coex_sta->pre_wifi_rssi_state[index];
 171		}
 172
 173		if ((coex_sta->pre_wifi_rssi_state[index] ==
 174		     BTC_RSSI_STATE_LOW) ||
 175		    (coex_sta->pre_wifi_rssi_state[index] ==
 176		     BTC_RSSI_STATE_STAY_LOW)) {
 177			if (wifi_rssi >= (rssi_thresh +
 178					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 179				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
 180				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 181					"[BTCoex], wifi RSSI state switch to Medium\n");
 182			} else {
 183				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
 184				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 185					"[BTCoex], wifi RSSI state stay at Low\n");
 186			}
 187		} else if ((coex_sta->pre_wifi_rssi_state[index] ==
 188			BTC_RSSI_STATE_MEDIUM) ||
 189			(coex_sta->pre_wifi_rssi_state[index] ==
 190			BTC_RSSI_STATE_STAY_MEDIUM)) {
 191			if (wifi_rssi >= (rssi_thresh1 +
 192					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 193				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
 194				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 195					"[BTCoex], wifi RSSI state switch to High\n");
 196			} else if (wifi_rssi < rssi_thresh) {
 197				wifi_rssi_state = BTC_RSSI_STATE_LOW;
 198				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 199					"[BTCoex], wifi RSSI state switch to Low\n");
 200			} else {
 201				wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
 202				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 203					"[BTCoex], wifi RSSI state stay at Medium\n");
 204			}
 205		} else {
 206			if (wifi_rssi < rssi_thresh1) {
 207				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
 208				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 209					"[BTCoex], wifi RSSI state switch to Medium\n");
 210			} else {
 211				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 212				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 213					"[BTCoex], wifi RSSI state stay at High\n");
 214			}
 215		}
 216	}
 217	coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
 218
 219	return wifi_rssi_state;
 220}
 221
 222static void btc8821a1ant_update_ra_mask(struct btc_coexist *btcoexist,
 223					bool force_exec, u32 dis_rate_mask)
 224{
 225	coex_dm->cur_ra_mask = dis_rate_mask;
 226
 227	if (force_exec ||
 228	    (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask)) {
 229		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
 230				   &coex_dm->cur_ra_mask);
 231	}
 232	coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
 233}
 234
 235static void btc8821a1ant_auto_rate_fb_retry(struct btc_coexist *btcoexist,
 236					    bool force_exec, u8 type)
 237{
 238	bool wifi_under_b_mode = false;
 239
 240	coex_dm->cur_arfr_type = type;
 241
 242	if (force_exec ||
 243	    (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
 244		switch (coex_dm->cur_arfr_type) {
 245		case 0:	/* normal mode */
 246			btcoexist->btc_write_4byte(btcoexist, 0x430,
 247						   coex_dm->backup_arfr_cnt1);
 248			btcoexist->btc_write_4byte(btcoexist, 0x434,
 249						   coex_dm->backup_arfr_cnt2);
 250			break;
 251		case 1:
 252			btcoexist->btc_get(btcoexist,
 253					   BTC_GET_BL_WIFI_UNDER_B_MODE,
 254					   &wifi_under_b_mode);
 255			if (wifi_under_b_mode) {
 256				btcoexist->btc_write_4byte(btcoexist, 0x430,
 257							   0x0);
 258				btcoexist->btc_write_4byte(btcoexist, 0x434,
 259							   0x01010101);
 260			} else {
 261				btcoexist->btc_write_4byte(btcoexist, 0x430,
 262							   0x0);
 263				btcoexist->btc_write_4byte(btcoexist, 0x434,
 264							   0x04030201);
 265			}
 266			break;
 267		default:
 268			break;
 269		}
 270	}
 271
 272	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
 273}
 274
 275static void btc8821a1ant_retry_limit(struct btc_coexist *btcoexist,
 276				     bool force_exec, u8 type)
 277{
 278	coex_dm->cur_retry_limit_type = type;
 279
 280	if (force_exec ||
 281	    (coex_dm->pre_retry_limit_type != coex_dm->cur_retry_limit_type)) {
 282		switch (coex_dm->cur_retry_limit_type) {
 283		case 0:	/* normal mode */
 284			btcoexist->btc_write_2byte(btcoexist, 0x42a,
 285						   coex_dm->backup_retry_limit);
 286			break;
 287		case 1:	/* retry limit = 8 */
 288			btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
 289			break;
 290		default:
 291			break;
 292		}
 293	}
 294	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
 295}
 296
 297static void btc8821a1ant_ampdu_max_time(struct btc_coexist *btcoexist,
 298					bool force_exec, u8 type)
 299{
 300	coex_dm->cur_ampdu_time_type = type;
 301
 302	if (force_exec ||
 303	    (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
 304		switch (coex_dm->cur_ampdu_time_type) {
 305		case 0:	/* normal mode */
 306			btcoexist->btc_write_1byte(btcoexist, 0x456,
 307						   coex_dm->backup_ampdu_max_time);
 308			break;
 309		case 1:	/* AMPDU time = 0x38 * 32us */
 310			btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
 311			break;
 312		default:
 313			break;
 314		}
 315	}
 316
 317	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
 318}
 319
 320static void btc8821a1ant_limited_tx(struct btc_coexist *btcoexist,
 321				    bool force_exec, u8 ra_mask_type,
 322				    u8 arfr_type, u8 retry_limit_type,
 323				    u8 ampdu_time_type)
 324{
 325	switch (ra_mask_type) {
 326	case 0:	/* normal mode */
 327		btc8821a1ant_update_ra_mask(btcoexist, force_exec, 0x0);
 328		break;
 329	case 1:	/* disable cck 1/2 */
 330		btc8821a1ant_update_ra_mask(btcoexist, force_exec,
 331					    0x00000003);
 332		break;
 333	case 2:	/* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
 334		btc8821a1ant_update_ra_mask(btcoexist, force_exec,
 335					    0x0001f1f7);
 336		break;
 337	default:
 338		break;
 339	}
 340
 341	btc8821a1ant_auto_rate_fb_retry(btcoexist, force_exec, arfr_type);
 342	btc8821a1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
 343	btc8821a1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
 344}
 345
 346static void btc8821a1ant_limited_rx(struct btc_coexist *btcoexist,
 347				    bool force_exec, bool rej_ap_agg_pkt,
 348				    bool bt_ctrl_agg_buf_size, u8 agg_buf_size)
 349{
 350	bool reject_rx_agg = rej_ap_agg_pkt;
 351	bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
 352	u8 rx_agg_size = agg_buf_size;
 353
 354	/* Rx Aggregation related setting */
 355	btcoexist->btc_set(btcoexist,
 356		 BTC_SET_BL_TO_REJ_AP_AGG_PKT, &reject_rx_agg);
 357	/* decide BT control aggregation buf size or not */
 358	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
 359			   &bt_ctrl_rx_agg_size);
 360	/* aggregation buf size, only work when BT control Rx agg size */
 361	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
 362	/* real update aggregation setting */
 363	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
 364}
 365
 366static void btc8821a1ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
 367{
 368	u32 reg_hp_tx_rx, reg_lp_tx_rx, u4_tmp;
 369	u32 reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
 370
 371	reg_hp_tx_rx = 0x770;
 372	reg_lp_tx_rx = 0x774;
 373
 374	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_tx_rx);
 375	reg_hp_tx = u4_tmp & MASKLWORD;
 376	reg_hp_rx = (u4_tmp & MASKHWORD) >> 16;
 377
 378	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_tx_rx);
 379	reg_lp_tx = u4_tmp & MASKLWORD;
 380	reg_lp_rx = (u4_tmp & MASKHWORD) >> 16;
 381
 382	coex_sta->high_priority_tx = reg_hp_tx;
 383	coex_sta->high_priority_rx = reg_hp_rx;
 384	coex_sta->low_priority_tx = reg_lp_tx;
 385	coex_sta->low_priority_rx = reg_lp_rx;
 386
 387	/* reset counter */
 388	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
 389}
 390
 391static void btc8821a1ant_query_bt_info(struct btc_coexist *btcoexist)
 392{
 393	struct rtl_priv *rtlpriv = btcoexist->adapter;
 394	u8 h2c_parameter[1] = {0};
 395
 396	coex_sta->c2h_bt_info_req_sent = true;
 397
 398	h2c_parameter[0] |= BIT0; /* trigger */
 399
 400	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 401		"[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
 402		h2c_parameter[0]);
 403
 404	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
 405}
 406
 407static void btc8821a1ant_update_bt_link_info(struct btc_coexist *btcoexist)
 408{
 409	struct btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
 410	bool bt_hs_on = false;
 411
 412	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
 413
 414	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
 415	bt_link_info->sco_exist = coex_sta->sco_exist;
 416	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
 417	bt_link_info->pan_exist = coex_sta->pan_exist;
 418	bt_link_info->hid_exist = coex_sta->hid_exist;
 419
 420	/* work around for HS mode */
 421	if (bt_hs_on) {
 422		bt_link_info->pan_exist = true;
 423		bt_link_info->bt_link_exist = true;
 424	}
 425
 426	/* check if Sco only */
 427	if (bt_link_info->sco_exist &&
 428	    !bt_link_info->a2dp_exist &&
 429	    !bt_link_info->pan_exist &&
 430	    !bt_link_info->hid_exist)
 431		bt_link_info->sco_only = true;
 432	else
 433		bt_link_info->sco_only = false;
 434
 435	/* check if A2dp only */
 436	if (!bt_link_info->sco_exist &&
 437	    bt_link_info->a2dp_exist &&
 438	    !bt_link_info->pan_exist &&
 439	    !bt_link_info->hid_exist)
 440		bt_link_info->a2dp_only = true;
 441	else
 442		bt_link_info->a2dp_only = false;
 443
 444	/* check if Pan only */
 445	if (!bt_link_info->sco_exist &&
 446	    !bt_link_info->a2dp_exist &&
 447	    bt_link_info->pan_exist &&
 448	    !bt_link_info->hid_exist)
 449		bt_link_info->pan_only = true;
 450	else
 451		bt_link_info->pan_only = false;
 452
 453	/* check if Hid only */
 454	if (!bt_link_info->sco_exist &&
 455	    !bt_link_info->a2dp_exist &&
 456	    !bt_link_info->pan_exist &&
 457	    bt_link_info->hid_exist)
 458		bt_link_info->hid_only = true;
 459	else
 460		bt_link_info->hid_only = false;
 461}
 462
 463static u8 btc8821a1ant_action_algorithm(struct btc_coexist *btcoexist)
 464{
 465	struct rtl_priv *rtlpriv = btcoexist->adapter;
 466	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
 467	bool bt_hs_on = false;
 468	u8 algorithm = BT_8821A_1ANT_COEX_ALGO_UNDEFINED;
 469	u8 num_of_diff_profile = 0;
 470
 471	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
 472
 473	if (!bt_link_info->bt_link_exist) {
 474		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 475			"[BTCoex], No BT link exists!!!\n");
 476		return algorithm;
 477	}
 478
 479	if (bt_link_info->sco_exist)
 480		num_of_diff_profile++;
 481	if (bt_link_info->hid_exist)
 482		num_of_diff_profile++;
 483	if (bt_link_info->pan_exist)
 484		num_of_diff_profile++;
 485	if (bt_link_info->a2dp_exist)
 486		num_of_diff_profile++;
 487
 488	if (num_of_diff_profile == 1) {
 489		if (bt_link_info->sco_exist) {
 490			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 491				"[BTCoex], BT Profile = SCO only\n");
 492			algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 493		} else {
 494			if (bt_link_info->hid_exist) {
 495				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 496					"[BTCoex], BT Profile = HID only\n");
 497				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 498			} else if (bt_link_info->a2dp_exist) {
 499				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 500					"[BTCoex], BT Profile = A2DP only\n");
 501				algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP;
 502			} else if (bt_link_info->pan_exist) {
 503				if (bt_hs_on) {
 504					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 505						DBG_LOUD,
 506						"[BTCoex], BT Profile = PAN(HS) only\n");
 507					algorithm = BT_8821A_1ANT_COEX_ALGO_PANHS;
 508				} else {
 509					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 510						DBG_LOUD,
 511						"[BTCoex], BT Profile = PAN(EDR) only\n");
 512					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR;
 513				}
 514			}
 515		}
 516	} else if (num_of_diff_profile == 2) {
 517		if (bt_link_info->sco_exist) {
 518			if (bt_link_info->hid_exist) {
 519				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 520					"[BTCoex], BT Profile = SCO + HID\n");
 521				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 522			} else if (bt_link_info->a2dp_exist) {
 523				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 524					"[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
 525				algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 526			} else if (bt_link_info->pan_exist) {
 527				if (bt_hs_on) {
 528					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 529						DBG_LOUD,
 530						"[BTCoex], BT Profile = SCO + PAN(HS)\n");
 531					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 532				} else {
 533					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 534						DBG_LOUD,
 535						"[BTCoex], BT Profile = SCO + PAN(EDR)\n");
 536					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 537				}
 538			}
 539		} else {
 540			if (bt_link_info->hid_exist &&
 541			    bt_link_info->a2dp_exist) {
 542				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 543					"[BTCoex], BT Profile = HID + A2DP\n");
 544				algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 545			} else if (bt_link_info->hid_exist &&
 546				   bt_link_info->pan_exist) {
 547				if (bt_hs_on) {
 548					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 549						DBG_LOUD,
 550						"[BTCoex], BT Profile = HID + PAN(HS)\n");
 551					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 552				} else {
 553					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 554						DBG_LOUD,
 555						"[BTCoex], BT Profile = HID + PAN(EDR)\n");
 556					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 557				}
 558			} else if (bt_link_info->pan_exist &&
 559				   bt_link_info->a2dp_exist) {
 560				if (bt_hs_on) {
 561					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 562						DBG_LOUD,
 563						"[BTCoex], BT Profile = A2DP + PAN(HS)\n");
 564					algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS;
 565				} else {
 566					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 567						DBG_LOUD,
 568						"[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
 569					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP;
 570				}
 571			}
 572		}
 573	} else if (num_of_diff_profile == 3) {
 574		if (bt_link_info->sco_exist) {
 575			if (bt_link_info->hid_exist &&
 576			    bt_link_info->a2dp_exist) {
 577				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 578					"[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
 579				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 580			} else if (bt_link_info->hid_exist &&
 581				   bt_link_info->pan_exist) {
 582				if (bt_hs_on) {
 583					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 584						DBG_LOUD,
 585						"[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
 586					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 587				} else {
 588					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 589						DBG_LOUD,
 590						"[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
 591					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 592				}
 593			} else if (bt_link_info->pan_exist &&
 594				   bt_link_info->a2dp_exist) {
 595				if (bt_hs_on) {
 596					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 597						DBG_LOUD,
 598						"[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
 599					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 600				} else {
 601					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 602						DBG_LOUD,
 603						"[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
 604					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 605				}
 606			}
 607		} else {
 608			if (bt_link_info->hid_exist &&
 609			    bt_link_info->pan_exist &&
 610			    bt_link_info->a2dp_exist) {
 611				if (bt_hs_on) {
 612					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 613						DBG_LOUD,
 614						"[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
 615					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 616				} else {
 617					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 618						DBG_LOUD,
 619						"[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
 620					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
 621				}
 622			}
 623		}
 624	} else if (num_of_diff_profile >= 3) {
 625		if (bt_link_info->sco_exist) {
 626			if (bt_link_info->hid_exist &&
 627			    bt_link_info->pan_exist &&
 628			    bt_link_info->a2dp_exist) {
 629				if (bt_hs_on) {
 630					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 631						DBG_LOUD,
 632						"[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
 633
 634				} else {
 635					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
 636						DBG_LOUD,
 637						"[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
 638					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 639				}
 640			}
 641		}
 642	}
 643	return algorithm;
 644}
 645
 646static void btc8821a1ant_set_sw_penalty_tx_rate(struct btc_coexist *btcoexist,
 647						bool low_penalty_ra)
 648{
 649	struct rtl_priv *rtlpriv = btcoexist->adapter;
 650	u8 h2c_parameter[6] = {0};
 651
 652	h2c_parameter[0] = 0x6;	/* opCode, 0x6= Retry_Penalty*/
 653
 654	if (low_penalty_ra) {
 655		h2c_parameter[1] |= BIT0;
 656		/* normal rate except MCS7/6/5, OFDM54/48/36 */
 657		h2c_parameter[2] = 0x00;
 658		h2c_parameter[3] = 0xf7; /* MCS7 or OFDM54 */
 659		h2c_parameter[4] = 0xf8; /* MCS6 or OFDM48 */
 660		h2c_parameter[5] = 0xf9; /* MCS5 or OFDM36 */
 661	}
 662
 663	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 664		"[BTCoex], set WiFi Low-Penalty Retry: %s",
 665		(low_penalty_ra ? "ON!!" : "OFF!!"));
 666
 667	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
 668}
 669
 670static void btc8821a1ant_low_penalty_ra(struct btc_coexist *btcoexist,
 671					bool force_exec, bool low_penalty_ra)
 672{
 673	coex_dm->cur_low_penalty_ra = low_penalty_ra;
 674
 675	if (!force_exec) {
 676		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
 677			return;
 678	}
 679	btc8821a1ant_set_sw_penalty_tx_rate(btcoexist,
 680					    coex_dm->cur_low_penalty_ra);
 681
 682	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
 683}
 684
 685static void btc8821a1ant_set_coex_table(struct btc_coexist *btcoexist,
 686					u32 val0x6c0, u32 val0x6c4,
 687					u32 val0x6c8, u8 val0x6cc)
 688{
 689	struct rtl_priv *rtlpriv = btcoexist->adapter;
 690
 691	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 692		"[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0);
 693	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
 694
 695	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 696		"[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4);
 697	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
 698
 699	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 700		"[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8);
 701	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
 702
 703	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 704		"[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc);
 705	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
 706}
 707
 708static void btc8821a1ant_coex_table(struct btc_coexist *btcoexist,
 709				    bool force_exec, u32 val0x6c0, u32 val0x6c4,
 710				    u32 val0x6c8, u8 val0x6cc)
 711{
 712	struct rtl_priv *rtlpriv = btcoexist->adapter;
 713
 714	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 715		"[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x\n",
 716		(force_exec ? "force to" : ""), val0x6c0, val0x6c4,
 717		val0x6c8, val0x6cc);
 718	coex_dm->cur_val_0x6c0 = val0x6c0;
 719	coex_dm->cur_val_0x6c4 = val0x6c4;
 720	coex_dm->cur_val_0x6c8 = val0x6c8;
 721	coex_dm->cur_val_0x6cc = val0x6cc;
 722
 723	if (!force_exec) {
 724		if ((coex_dm->pre_val_0x6c0 == coex_dm->cur_val_0x6c0) &&
 725		    (coex_dm->pre_val_0x6c4 == coex_dm->cur_val_0x6c4) &&
 726		    (coex_dm->pre_val_0x6c8 == coex_dm->cur_val_0x6c8) &&
 727		    (coex_dm->pre_val_0x6cc == coex_dm->cur_val_0x6cc))
 728			return;
 729	}
 730	btc8821a1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
 731				    val0x6c8, val0x6cc);
 732
 733	coex_dm->pre_val_0x6c0 = coex_dm->cur_val_0x6c0;
 734	coex_dm->pre_val_0x6c4 = coex_dm->cur_val_0x6c4;
 735	coex_dm->pre_val_0x6c8 = coex_dm->cur_val_0x6c8;
 736	coex_dm->pre_val_0x6cc = coex_dm->cur_val_0x6cc;
 737}
 738
 739static void btc8821a1ant_coex_table_with_type(struct btc_coexist *btcoexist,
 740					      bool force_exec, u8 type)
 741{
 742	switch (type) {
 743	case 0:
 744		btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 745					0x55555555, 0xffffff, 0x3);
 746		break;
 747	case 1:
 748		btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 749					0x5a5a5a5a, 0xffffff, 0x3);
 750		break;
 751	case 2:
 752		btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
 753					0x5a5a5a5a, 0xffffff, 0x3);
 754		break;
 755	case 3:
 756		btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
 757					0xaaaaaaaa, 0xffffff, 0x3);
 758		break;
 759	case 4:
 760		btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 761					0x5a5a5a5a, 0xffffff, 0x3);
 762		break;
 763	case 5:
 764		btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
 765					0xaaaa5a5a, 0xffffff, 0x3);
 766		break;
 767	case 6:
 768		btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 769					0xaaaa5a5a, 0xffffff, 0x3);
 770		break;
 771	case 7:
 772		btc8821a1ant_coex_table(btcoexist, force_exec, 0xaaaaaaaa,
 773					0xaaaaaaaa, 0xffffff, 0x3);
 774		break;
 775	default:
 776		break;
 777	}
 778}
 779
 780static void btc8821a1ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
 781						bool enable)
 782{
 783	struct rtl_priv *rtlpriv = btcoexist->adapter;
 784	u8 h2c_parameter[1] = {0};
 785
 786	if (enable)
 787		h2c_parameter[0] |= BIT0; /* function enable */
 788
 789	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 790		"[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
 791		h2c_parameter[0]);
 792
 793	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
 794}
 795
 796static void btc8821a1ant_ignore_wlan_act(struct btc_coexist *btcoexist,
 797					 bool force_exec, bool enable)
 798{
 799	struct rtl_priv *rtlpriv = btcoexist->adapter;
 800
 801	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 802		"[BTCoex], %s turn Ignore WlanAct %s\n",
 803		(force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
 804	coex_dm->cur_ignore_wlan_act = enable;
 805
 806	if (!force_exec) {
 807		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 808			"[BTCoex], pre_ignore_wlan_act = %d, cur_ignore_wlan_act = %d!!\n",
 809			coex_dm->pre_ignore_wlan_act,
 810			 coex_dm->cur_ignore_wlan_act);
 811
 812		if (coex_dm->pre_ignore_wlan_act ==
 813		    coex_dm->cur_ignore_wlan_act)
 814			return;
 815	}
 816	btc8821a1ant_set_fw_ignore_wlan_act(btcoexist, enable);
 817
 818	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
 819}
 820
 821static void btc8821a1ant_set_fw_ps_tdma(struct btc_coexist *btcoexist, u8 byte1,
 822					u8 byte2, u8 byte3, u8 byte4, u8 byte5)
 823{
 824	struct rtl_priv *rtlpriv = btcoexist->adapter;
 825	u8 h2c_parameter[5] = {0};
 826	u8 real_byte1 = byte1, real_byte5 = byte5;
 827	bool ap_enable = false;
 828
 829	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
 830			   &ap_enable);
 831
 832	if (ap_enable) {
 833		if (byte1 & BIT4 && !(byte1 & BIT5)) {
 834			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 835				"[BTCoex], FW for 1Ant AP mode\n");
 836			real_byte1 &= ~BIT4;
 837			real_byte1 |= BIT5;
 838
 839			real_byte5 |= BIT5;
 840			real_byte5 &= ~BIT6;
 841		}
 842	}
 843
 844	h2c_parameter[0] = real_byte1;
 845	h2c_parameter[1] = byte2;
 846	h2c_parameter[2] = byte3;
 847	h2c_parameter[3] = byte4;
 848	h2c_parameter[4] = real_byte5;
 849
 850	coex_dm->ps_tdma_para[0] = real_byte1;
 851	coex_dm->ps_tdma_para[1] = byte2;
 852	coex_dm->ps_tdma_para[2] = byte3;
 853	coex_dm->ps_tdma_para[3] = byte4;
 854	coex_dm->ps_tdma_para[4] = real_byte5;
 855
 856	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 857		"[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
 858		h2c_parameter[0],
 859		h2c_parameter[1] << 24 |
 860		h2c_parameter[2] << 16 |
 861		h2c_parameter[3] << 8 |
 862		h2c_parameter[4]);
 863	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
 864}
 865
 866static void btc8821a1ant_set_lps_rpwm(struct btc_coexist *btcoexist,
 867				      u8 lps_val, u8 rpwm_val)
 868{
 869	u8 lps = lps_val;
 870	u8 rpwm = rpwm_val;
 871
 872	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
 873	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
 874}
 875
 876static void btc8821a1ant_lps_rpwm(struct btc_coexist *btcoexist,
 877				  bool force_exec, u8 lps_val, u8 rpwm_val)
 878{
 879	struct rtl_priv *rtlpriv = btcoexist->adapter;
 880
 881	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 882		"[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
 883		(force_exec ? "force to" : ""), lps_val, rpwm_val);
 884	coex_dm->cur_lps = lps_val;
 885	coex_dm->cur_rpwm = rpwm_val;
 886
 887	if (!force_exec) {
 888		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 889			"[BTCoex], LPS-RxBeaconMode = 0x%x, LPS-RPWM = 0x%x!!\n",
 890			coex_dm->cur_lps, coex_dm->cur_rpwm);
 891
 892		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
 893		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm)) {
 894			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 895				"[BTCoex], LPS-RPWM_Last = 0x%x, LPS-RPWM_Now = 0x%x!!\n",
 896				coex_dm->pre_rpwm, coex_dm->cur_rpwm);
 897
 898			return;
 899		}
 900	}
 901	btc8821a1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
 902
 903	coex_dm->pre_lps = coex_dm->cur_lps;
 904	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
 905}
 906
 907static void btc8821a1ant_sw_mechanism(struct btc_coexist *btcoexist,
 908				      bool low_penalty_ra)
 909{
 910	struct rtl_priv *rtlpriv = btcoexist->adapter;
 911
 912	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
 913		"[BTCoex], SM[LpRA] = %d\n", low_penalty_ra);
 914
 915	btc8821a1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
 916}
 917
 918static void btc8821a1ant_set_ant_path(struct btc_coexist *btcoexist,
 919				      u8 ant_pos_type, bool init_hw_cfg,
 920				      bool wifi_off)
 921{
 922	struct btc_board_info *board_info = &btcoexist->board_info;
 923	u32 u4_tmp = 0;
 924	u8 h2c_parameter[2] = {0};
 925
 926	if (init_hw_cfg) {
 927		/* 0x4c[23] = 0, 0x4c[24] = 1  Antenna control by WL/BT */
 928		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
 929		u4_tmp &= ~BIT23;
 930		u4_tmp |= BIT24;
 931		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
 932
 933		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x975, 0x3, 0x3);
 934		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
 935
 936		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
 937			/* tell firmware "antenna inverse"
 938			 * WRONG firmware antenna control code, need fw to fix
 939			 */
 940			h2c_parameter[0] = 1;
 941			h2c_parameter[1] = 1;
 942			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
 943						h2c_parameter);
 944		} else {
 945			/* tell firmware "no antenna inverse"
 946			 * WRONG firmware antenna control code, need fw to fix
 947			 */
 948			h2c_parameter[0] = 0;
 949			h2c_parameter[1] = 1;
 950			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
 951						h2c_parameter);
 952		}
 953	} else if (wifi_off) {
 954		/* 0x4c[24:23] = 00, Set Antenna control
 955		 * by BT_RFE_CTRL BT Vendor 0xac = 0xf002
 956		 */
 957		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
 958		u4_tmp &= ~BIT23;
 959		u4_tmp &= ~BIT24;
 960		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
 961
 962		/* 0x765 = 0x18 */
 963		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x3);
 964	} else {
 965		/* 0x765 = 0x0 */
 966		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x0);
 967	}
 968
 969	/* ext switch setting */
 970	switch (ant_pos_type) {
 971	case BTC_ANT_PATH_WIFI:
 972		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
 973		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
 974			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 975							   0x30, 0x1);
 976		else
 977			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 978							   0x30, 0x2);
 979		break;
 980	case BTC_ANT_PATH_BT:
 981		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
 982		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
 983			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 984							   0x30, 0x2);
 985		else
 986			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 987							   0x30, 0x1);
 988		break;
 989	default:
 990	case BTC_ANT_PATH_PTA:
 991		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x66);
 992		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
 993			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 994							   0x30, 0x1);
 995		else
 996			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
 997							   0x30, 0x2);
 998		break;
 999	}
1000}
1001
1002static void btc8821a1ant_ps_tdma(struct btc_coexist *btcoexist,
1003				 bool force_exec, bool turn_on, u8 type)
1004{
1005	struct rtl_priv *rtlpriv = btcoexist->adapter;
1006	u8 rssi_adjust_val = 0;
1007
1008	coex_dm->cur_ps_tdma_on = turn_on;
1009	coex_dm->cur_ps_tdma = type;
1010
1011	if (!force_exec) {
1012		if (coex_dm->cur_ps_tdma_on) {
1013			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1014				"[BTCoex], ********** TDMA(on, %d) **********\n",
1015				coex_dm->cur_ps_tdma);
1016		} else {
1017			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1018				"[BTCoex], ********** TDMA(off, %d) **********\n",
1019				coex_dm->cur_ps_tdma);
1020		}
1021		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1022		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1023			return;
1024	}
1025	if (turn_on) {
1026		switch (type) {
1027		default:
1028			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1a,
1029						    0x1a, 0x0, 0x50);
1030			break;
1031		case 1:
1032			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x3a,
1033						    0x03, 0x10, 0x50);
1034			rssi_adjust_val = 11;
1035			break;
1036		case 2:
1037			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x2b,
1038						    0x03, 0x10, 0x50);
1039			rssi_adjust_val = 14;
1040			break;
1041		case 3:
1042			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1d,
1043						    0x1d, 0x0, 0x10);
1044			break;
1045		case 4:
1046			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1047						    0x3, 0x14, 0x0);
1048			rssi_adjust_val = 17;
1049			break;
1050		case 5:
1051			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x15,
1052						    0x3, 0x11, 0x10);
1053			break;
1054		case 6:
1055			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x20,
1056						    0x3, 0x11, 0x13);
1057			break;
1058		case 7:
1059			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xc,
1060						    0x5, 0x0, 0x0);
1061			break;
1062		case 8:
1063			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1064						    0x3, 0x10, 0x0);
1065			break;
1066		case 9:
1067			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x21,
1068						    0x3, 0x10, 0x50);
1069			rssi_adjust_val = 18;
1070			break;
1071		case 10:
1072			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1073						    0xa, 0x0, 0x40);
1074			break;
1075		case 11:
1076			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x15,
1077						    0x03, 0x10, 0x50);
1078			rssi_adjust_val = 20;
1079			break;
1080		case 12:
1081			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x0a,
1082						    0x0a, 0x0, 0x50);
1083			break;
1084		case 13:
1085			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x12,
1086						    0x12, 0x0, 0x50);
1087			break;
1088		case 14:
1089			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1e,
1090						    0x3, 0x10, 0x14);
1091			break;
1092		case 15:
1093			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1094						    0x3, 0x8, 0x0);
1095			break;
1096		case 16:
1097			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1098						    0x3, 0x10, 0x0);
1099			rssi_adjust_val = 18;
1100			break;
1101		case 18:
1102			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1103						    0x3, 0x10, 0x0);
1104			rssi_adjust_val = 14;
1105			break;
1106		case 20:
1107			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x35,
1108						    0x03, 0x11, 0x10);
1109			break;
1110		case 21:
1111			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x25,
1112						    0x03, 0x11, 0x11);
1113			break;
1114		case 22:
1115			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x25,
1116						    0x03, 0x11, 0x10);
1117			break;
1118		case 23:
1119			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1120						    0x3, 0x31, 0x18);
1121			rssi_adjust_val = 22;
1122			break;
1123		case 24:
1124			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1125						    0x3, 0x31, 0x18);
1126			rssi_adjust_val = 22;
1127			break;
1128		case 25:
1129			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1130						    0x3, 0x31, 0x18);
1131			rssi_adjust_val = 22;
1132			break;
1133		case 26:
1134			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1135						    0x3, 0x31, 0x18);
1136			rssi_adjust_val = 22;
1137			break;
1138		case 27:
1139			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1140						    0x3, 0x31, 0x98);
1141			rssi_adjust_val = 22;
1142			break;
1143		case 28:
1144			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x69, 0x25,
1145						    0x3, 0x31, 0x0);
1146			break;
1147		case 29:
1148			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xab, 0x1a,
1149						    0x1a, 0x1, 0x10);
1150			break;
1151		case 30:
1152			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x30,
1153						    0x3, 0x10, 0x10);
1154			break;
1155		case 31:
1156			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x1a,
1157						    0x1a, 0, 0x58);
1158			break;
1159		case 32:
1160			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x35,
1161						    0x3, 0x11, 0x11);
1162			break;
1163		case 33:
1164			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x25,
1165						    0x3, 0x30, 0x90);
1166			break;
1167		case 34:
1168			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x53, 0x1a,
1169						    0x1a, 0x0, 0x10);
1170			break;
1171		case 35:
1172			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x63, 0x1a,
1173						    0x1a, 0x0, 0x10);
1174			break;
1175		case 36:
1176			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x12,
1177						    0x3, 0x14, 0x50);
1178			break;
1179		case 40:
1180			/* SoftAP only with no sta associated, BT disable, TDMA
1181			 * mode for power saving
1182			 *
1183			 * here softap mode screen off will cost 70-80mA for
1184			 * phone
1185			 */
1186			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x23, 0x18,
1187						    0x00, 0x10, 0x24);
1188			break;
1189		case 41:
1190			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x15,
1191						    0x3, 0x11, 0x11);
1192			break;
1193		case 42:
1194			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x20,
1195						    0x3, 0x11, 0x11);
1196			break;
1197		case 43:
1198			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x30,
1199						    0x3, 0x10, 0x11);
1200			break;
1201		}
1202	} else {
1203		/* disable PS tdma */
1204		switch (type) {
1205		case 8:
1206			/* PTA Control */
1207			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x8, 0x0, 0x0,
1208						    0x0, 0x0);
1209			btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1210						  false, false);
1211			break;
1212		case 0:
1213		default:
1214			/* Software control, Antenna at BT side */
1215			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1216						    0x0, 0x0);
1217			btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1218						  false, false);
1219			break;
1220		case 9:
1221			/* Software control, Antenna at WiFi side */
1222			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1223						    0x0, 0x0);
1224			btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
1225						  false, false);
1226			break;
1227		case 10:
1228			/* under 5G */
1229			btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1230						    0x8, 0x0);
1231			btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1232						  false, false);
1233			break;
1234		}
1235	}
1236	rssi_adjust_val = 0;
1237	btcoexist->btc_set(btcoexist,
1238		 BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1239
1240	/* update pre state */
1241	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1242	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1243}
1244
1245static bool btc8821a1ant_is_common_action(struct btc_coexist *btcoexist)
1246{
1247	struct rtl_priv *rtlpriv = btcoexist->adapter;
1248	bool common = false, wifi_connected = false, wifi_busy = false;
1249
1250	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1251			   &wifi_connected);
1252	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1253
1254	if (!wifi_connected &&
1255	    BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1256	    coex_dm->bt_status) {
1257		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1258			"[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1259		btc8821a1ant_sw_mechanism(btcoexist, false);
1260
1261		common = true;
1262	} else if (wifi_connected &&
1263		   (BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1264		    coex_dm->bt_status)) {
1265		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1266			"[BTCoex], Wifi connected + BT non connected-idle!!\n");
1267		btc8821a1ant_sw_mechanism(btcoexist, false);
1268
1269		common = true;
1270	} else if (!wifi_connected &&
1271		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1272		    coex_dm->bt_status)) {
1273		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1274			"[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1275		btc8821a1ant_sw_mechanism(btcoexist, false);
1276
1277		common = true;
1278	} else if (wifi_connected &&
1279		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1280		    coex_dm->bt_status)) {
1281		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1282			"[BTCoex], Wifi connected + BT connected-idle!!\n");
1283		btc8821a1ant_sw_mechanism(btcoexist, false);
1284
1285		common = true;
1286	} else if (!wifi_connected &&
1287		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE !=
1288		    coex_dm->bt_status)) {
1289		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1290			"[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1291		btc8821a1ant_sw_mechanism(btcoexist, false);
1292
1293		common = true;
1294	} else {
1295		if (wifi_busy) {
1296			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1297				"[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1298		} else {
1299			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1300				"[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1301		}
1302
1303		common = false;
1304	}
1305
1306	return common;
1307}
1308
1309static void btc8821a1ant_ps_tdma_check_for_pwr_save(struct btc_coexist *btcoex,
1310						    bool new_ps_state)
1311{
1312	u8 lps_mode = 0x0;
1313
1314	btcoex->btc_get(btcoex, BTC_GET_U1_LPS_MODE, &lps_mode);
1315
1316	if (lps_mode) {
1317		/* already under LPS state */
1318		if (new_ps_state) {
1319			/* keep state under LPS, do nothing */
1320		} else {
1321			/* will leave LPS state, turn off psTdma first */
1322			btc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1323		}
1324	} else {
1325		/* NO PS state*/
1326		if (new_ps_state) {
1327			/* will enter LPS state, turn off psTdma first */
1328			btc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1329		} else {
1330			/* keep state under NO PS state, do nothing */
1331		}
1332	}
1333}
1334
1335static void btc8821a1ant_power_save_state(struct btc_coexist *btcoexist,
1336					  u8 ps_type, u8 lps_val, u8 rpwm_val)
1337{
1338	bool low_pwr_disable = false;
1339
1340	switch (ps_type) {
1341	case BTC_PS_WIFI_NATIVE:
1342		/* recover to original 32k low power setting */
1343		low_pwr_disable = false;
1344		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1345				   &low_pwr_disable);
1346		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1347		break;
1348	case BTC_PS_LPS_ON:
1349		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist,
1350							true);
1351		btc8821a1ant_lps_rpwm(btcoexist, NORMAL_EXEC, lps_val,
1352				      rpwm_val);
1353		/* when coex force to enter LPS, do not enter 32k low power */
1354		low_pwr_disable = true;
1355		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1356				   &low_pwr_disable);
1357		/* power save must executed before psTdma */
1358		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1359		break;
1360	case BTC_PS_LPS_OFF:
1361		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist, false);
1362		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1363		break;
1364	default:
1365		break;
1366	}
1367}
1368
1369static void btc8821a1ant_coex_under_5g(struct btc_coexist *btcoexist)
1370{
1371	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1372				      0x0, 0x0);
1373	btc8821a1ant_ignore_wlan_act(btcoexist, NORMAL_EXEC, true);
1374
1375	btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 10);
1376
1377	btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1378
1379	btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1380
1381	btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 5);
1382}
1383
1384/***********************************************
1385 *
1386 *	Software Coex Mechanism start
1387 *
1388 ***********************************************/
1389
1390/* SCO only or SCO+PAN(HS) */
1391static void btc8821a1ant_action_sco(struct btc_coexist *btcoexist)
1392{
1393	btc8821a1ant_sw_mechanism(btcoexist, true);
1394}
1395
1396static void btc8821a1ant_action_hid(struct btc_coexist *btcoexist)
1397{
1398	btc8821a1ant_sw_mechanism(btcoexist, true);
1399}
1400
1401/* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
1402static void btc8821a1ant_action_a2dp(struct btc_coexist *btcoexist)
1403{
1404	btc8821a1ant_sw_mechanism(btcoexist, false);
1405}
1406
1407static void btc8821a1ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
1408{
1409	btc8821a1ant_sw_mechanism(btcoexist, false);
1410}
1411
1412static void btc8821a1ant_action_pan_edr(struct btc_coexist *btcoexist)
1413{
1414	btc8821a1ant_sw_mechanism(btcoexist, false);
1415}
1416
1417/* PAN(HS) only */
1418static void btc8821a1ant_action_pan_hs(struct btc_coexist *btcoexist)
1419{
1420	btc8821a1ant_sw_mechanism(btcoexist, false);
1421}
1422
1423/* PAN(EDR)+A2DP */
1424static void btc8821a1ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
1425{
1426	btc8821a1ant_sw_mechanism(btcoexist, false);
1427}
1428
1429static void btc8821a1ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
1430{
1431	btc8821a1ant_sw_mechanism(btcoexist, true);
1432}
1433
1434/* HID+A2DP+PAN(EDR) */
1435static void btc8821a1ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
1436{
1437	btc8821a1ant_sw_mechanism(btcoexist, true);
1438}
1439
1440static void btc8821a1ant_action_hid_a2dp(struct btc_coexist *btcoexist)
1441{
1442	btc8821a1ant_sw_mechanism(btcoexist, true);
1443}
1444
1445/***********************************************
1446 *
1447 *	Non-Software Coex Mechanism start
1448 *
1449 ***********************************************/
1450static
1451void btc8821a1ant_action_wifi_multi_port(struct btc_coexist *btcoexist)
1452{
1453	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1454
1455	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1456	/* tdma and coex table */
1457	if (coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_ACL_BUSY) {
1458		if (bt_link_info->a2dp_exist) {
1459			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1460			btc8821a1ant_coex_table_with_type(btcoexist,
1461							  NORMAL_EXEC, 1);
1462		} else if (bt_link_info->a2dp_exist &&
1463			   bt_link_info->pan_exist) {
1464			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1465			btc8821a1ant_coex_table_with_type(btcoexist,
1466							  NORMAL_EXEC, 4);
1467		} else {
1468			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1469			btc8821a1ant_coex_table_with_type(btcoexist,
1470							  NORMAL_EXEC, 4);
1471		}
1472	} else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1473		   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1474		    coex_dm->bt_status)) {
1475		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1476				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1477	} else {
1478		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1479		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1480	}
1481}
1482
1483static
1484void btc8821a1ant_action_wifi_not_connected_asso_auth(
1485					struct btc_coexist *btcoexist)
1486{
1487	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1488
1489	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1490				      0x0);
1491
1492	/* tdma and coex table */
1493	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist)) {
1494		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1495		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1496	} else if ((bt_link_info->a2dp_exist) || (bt_link_info->pan_exist)) {
1497		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1498		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1499	} else {
1500		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1501		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1502	}
1503}
1504
1505
1506static void btc8821a1ant_action_hs(struct btc_coexist *btcoexist)
1507{
1508	btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1509	btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 2);
1510}
1511
1512static void btc8821a1ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1513{
1514	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1515	bool wifi_connected = false;
1516	bool ap_enable = false;
1517	bool wifi_busy = false, bt_busy = false;
1518
1519	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1520			   &wifi_connected);
1521	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1522			   &ap_enable);
1523	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1524	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1525
1526	if (!wifi_connected && !coex_sta->wifi_is_high_pri_task) {
1527		btc8821a1ant_power_save_state(btcoexist,
1528					      BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1529		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1530		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1531	} else if ((bt_link_info->sco_exist) || (bt_link_info->a2dp_exist) ||
1532		   (bt_link_info->hid_only)) {
1533		/* SCO/HID-only busy */
1534		btc8821a1ant_power_save_state(btcoexist,
1535					      BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1536		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1537		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1538	} else if ((bt_link_info->a2dp_exist) && (bt_link_info->hid_exist)) {
1539		/* A2DP+HID busy */
1540		btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1541					      0x0, 0x0);
1542		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1543
1544		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1545	} else if ((bt_link_info->pan_exist) || (wifi_busy)) {
1546		btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1547					      0x0, 0x0);
1548		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1549
1550		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1551	} else {
1552		btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1553					      0x0, 0x0);
1554		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1555		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1556	}
1557}
1558
1559static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
1560						  u8 wifi_status)
1561{
1562	/* tdma and coex table */
1563	btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1564
1565	btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1566}
1567
1568static void btc8821a1ant_act_wifi_con_bt_acl_busy(struct btc_coexist *btcoexist,
1569						  u8 wifi_status)
1570{
1571	u8 bt_rssi_state;
1572
1573	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1574
1575	bt_rssi_state = btc8821a1ant_bt_rssi_state(btcoexist, 2, 28, 0);
1576
1577	if (bt_link_info->hid_only) {
1578		/* HID */
1579		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1580						      wifi_status);
1581		coex_dm->auto_tdma_adjust = false;
1582		return;
1583	} else if (bt_link_info->a2dp_only) {
1584		/* A2DP */
1585		if (wifi_status == BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE) {
1586			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1587			btc8821a1ant_coex_table_with_type(btcoexist,
1588							  NORMAL_EXEC, 1);
1589			coex_dm->auto_tdma_adjust = false;
1590		} else if ((bt_rssi_state != BTC_RSSI_STATE_HIGH) &&
1591			   (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1592			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1593			btc8821a1ant_coex_table_with_type(btcoexist,
1594							  NORMAL_EXEC, 1);
1595		} else {
1596			/* for low BT RSSI */
1597			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1598			btc8821a1ant_coex_table_with_type(btcoexist,
1599							  NORMAL_EXEC, 1);
1600			coex_dm->auto_tdma_adjust = false;
1601		}
1602	} else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1603		/* HID+A2DP (no need to consider BT RSSI) */
1604		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1605				     true, 14);
1606		coex_dm->auto_tdma_adjust = false;
1607
1608		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1609	} else if ((bt_link_info->pan_only) ||
1610		(bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1611		/* PAN(OPP, FTP), HID+PAN(OPP, FTP) */
1612		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1613		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 6);
1614		coex_dm->auto_tdma_adjust = false;
1615	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1616		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1617		    bt_link_info->pan_exist)) {
1618		/* A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP) */
1619		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 43);
1620		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1621		coex_dm->auto_tdma_adjust = false;
1622	} else {
1623		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1624		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1625		coex_dm->auto_tdma_adjust = false;
1626	}
1627}
1628
1629static
1630void btc8821a1ant_action_wifi_not_connected(struct btc_coexist *btcoexist)
1631{
1632	/* power save state */
1633	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1634
1635	/* tdma and coex table */
1636	btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1637	btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1638}
1639
1640static void btc8821a1ant_act_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1641{
1642	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1643
1644	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1645
1646	/* tdma and coex table */
1647	if (coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_ACL_BUSY) {
1648		if (bt_link_info->a2dp_exist) {
1649			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1650			btc8821a1ant_coex_table_with_type(btcoexist,
1651							  NORMAL_EXEC, 1);
1652		} else if (bt_link_info->a2dp_exist &&
1653			   bt_link_info->pan_exist) {
1654			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1655			btc8821a1ant_coex_table_with_type(btcoexist,
1656							  NORMAL_EXEC, 4);
1657		} else {
1658			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1659			btc8821a1ant_coex_table_with_type(btcoexist,
1660							  NORMAL_EXEC, 4);
1661		}
1662	} else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1663		   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1664		    coex_dm->bt_status)) {
1665		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1666				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1667	} else {
1668		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1669		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1670	}
1671}
1672
1673static
1674void btc8821a1ant_action_wifi_connected_scan(struct btc_coexist *btcoexist)
1675{
1676	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1677
1678	/* power save state */
1679	btc8821a1ant_power_save_state(btcoexist,
1680				      BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1681
1682	/* tdma and coex table */
1683	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1684		if (bt_link_info->a2dp_exist) {
1685			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1686			btc8821a1ant_coex_table_with_type(btcoexist,
1687							  NORMAL_EXEC, 1);
1688		} else {
1689			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1690			btc8821a1ant_coex_table_with_type(btcoexist,
1691							  NORMAL_EXEC, 4);
1692		}
1693	} else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1694		   (coex_dm->bt_status ==
1695		    BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY)) {
1696		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1697			BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1698	} else {
1699		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1700		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1701	}
1702}
1703
1704static void btc8821a1ant_act_wifi_conn_sp_pkt(struct btc_coexist *btcoexist)
1705{
1706	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1707
1708	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1709				      0x0, 0x0);
1710
1711	/* tdma and coex table */
1712	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1713	    (bt_link_info->a2dp_exist)) {
1714		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1715		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1716	}
1717
1718	if ((bt_link_info->hid_exist) && (bt_link_info->a2dp_exist)) {
1719		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1720		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1721	} else if (bt_link_info->pan_exist) {
1722		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1723		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1724	} else {
1725		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1726		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1727	}
1728}
1729
1730static void btc8821a1ant_action_wifi_connected(struct btc_coexist *btcoexist)
1731{
1732	struct rtl_priv *rtlpriv = btcoexist->adapter;
1733	bool wifi_busy = false;
1734	bool scan = false, link = false, roam = false;
1735	bool under_4way = false;
1736	bool ap_enable = false;
1737
1738	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1739		"[BTCoex], CoexForWifiConnect()===>\n");
1740
1741	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
1742			   &under_4way);
1743	if (under_4way) {
1744		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1745		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1746			"[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1747		return;
1748	}
1749
1750	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1751	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1752	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1753	if (scan || link || roam) {
1754		if (scan)
1755			btc8821a1ant_action_wifi_connected_scan(btcoexist);
1756		else
1757			btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1758
1759		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1760			"[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1761		return;
1762	}
1763
1764	/* power save state*/
1765	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1766			   &ap_enable);
1767	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1768	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY ==
1769	    coex_dm->bt_status && !ap_enable &&
1770	    !btcoexist->bt_link_info.hid_only) {
1771		if (!wifi_busy && btcoexist->bt_link_info.a2dp_only)
1772			/* A2DP */
1773			btc8821a1ant_power_save_state(btcoexist,
1774						BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1775		else
1776			btc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON,
1777						      0x50, 0x4);
1778	} else {
1779		btc8821a1ant_power_save_state(btcoexist,
1780					      BTC_PS_WIFI_NATIVE,
1781					      0x0, 0x0);
1782	}
1783
1784	/* tdma and coex table */
1785	if (!wifi_busy) {
1786		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1787			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1788				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1789		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1790			    coex_dm->bt_status) ||
1791			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1792			    coex_dm->bt_status)) {
1793			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1794				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1795		} else {
1796			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1797			btc8821a1ant_coex_table_with_type(btcoexist,
1798							  NORMAL_EXEC, 2);
1799		}
1800	} else {
1801		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1802			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1803				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1804		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1805			    coex_dm->bt_status) ||
1806			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1807			    coex_dm->bt_status)) {
1808			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1809				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1810		} else {
1811			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1812			btc8821a1ant_coex_table_with_type(btcoexist,
1813							  NORMAL_EXEC, 2);
1814		}
1815	}
1816}
1817
1818static void btc8821a1ant_run_sw_coex_mech(struct btc_coexist *btcoexist)
1819{
1820	struct rtl_priv *rtlpriv = btcoexist->adapter;
1821	u8 algorithm = 0;
1822
1823	algorithm = btc8821a1ant_action_algorithm(btcoexist);
1824	coex_dm->cur_algorithm = algorithm;
1825
1826	if (!btc8821a1ant_is_common_action(btcoexist)) {
1827		switch (coex_dm->cur_algorithm) {
1828		case BT_8821A_1ANT_COEX_ALGO_SCO:
1829			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1830				"[BTCoex], Action algorithm = SCO\n");
1831			btc8821a1ant_action_sco(btcoexist);
1832			break;
1833		case BT_8821A_1ANT_COEX_ALGO_HID:
1834			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1835				"[BTCoex], Action algorithm = HID\n");
1836			btc8821a1ant_action_hid(btcoexist);
1837			break;
1838		case BT_8821A_1ANT_COEX_ALGO_A2DP:
1839			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1840				"[BTCoex], Action algorithm = A2DP\n");
1841			btc8821a1ant_action_a2dp(btcoexist);
1842			break;
1843		case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
1844			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1845				"[BTCoex], Action algorithm = A2DP+PAN(HS)\n");
1846			btc8821a1ant_action_a2dp_pan_hs(btcoexist);
1847			break;
1848		case BT_8821A_1ANT_COEX_ALGO_PANEDR:
1849			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1850				"[BTCoex], Action algorithm = PAN(EDR)\n");
1851			btc8821a1ant_action_pan_edr(btcoexist);
1852			break;
1853		case BT_8821A_1ANT_COEX_ALGO_PANHS:
1854			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1855				"[BTCoex], Action algorithm = HS mode\n");
1856			btc8821a1ant_action_pan_hs(btcoexist);
1857			break;
1858		case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
1859			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1860				"[BTCoex], Action algorithm = PAN+A2DP\n");
1861			btc8821a1ant_action_pan_edr_a2dp(btcoexist);
1862			break;
1863		case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
1864			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1865				"[BTCoex], Action algorithm = PAN(EDR)+HID\n");
1866			btc8821a1ant_action_pan_edr_hid(btcoexist);
1867			break;
1868		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
1869			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1870				"[BTCoex], Action algorithm = HID+A2DP+PAN\n");
1871			btc8821a1ant_action_hid_a2dp_pan_edr(btcoexist);
1872			break;
1873		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
1874			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1875				"[BTCoex], Action algorithm = HID+A2DP\n");
1876			btc8821a1ant_action_hid_a2dp(btcoexist);
1877			break;
1878		default:
1879			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1880				"[BTCoex], Action algorithm = coexist All Off!!\n");
1881			/*btc8821a1ant_coex_all_off(btcoexist);*/
1882			break;
1883		}
1884		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
1885	}
1886}
1887
1888static void btc8821a1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
1889{
1890	struct rtl_priv *rtlpriv = btcoexist->adapter;
1891	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1892	bool wifi_connected = false, bt_hs_on = false;
1893	bool increase_scan_dev_num = false;
1894	bool bt_ctrl_agg_buf_size = false;
1895	u8 agg_buf_size = 5;
1896	u32 wifi_link_status = 0;
1897	u32 num_of_wifi_link = 0;
1898	bool wifi_under_5g = false;
1899
1900	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1901		"[BTCoex], RunCoexistMechanism()===>\n");
1902
1903	if (btcoexist->manual_control) {
1904		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1905			"[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
1906		return;
1907	}
1908
1909	if (btcoexist->stop_coex_dm) {
1910		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1911			"[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
1912		return;
1913	}
1914
1915	if (coex_sta->under_ips) {
1916		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1917			"[BTCoex], wifi is under IPS !!!\n");
1918		return;
1919	}
1920
1921	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
1922	if (wifi_under_5g) {
1923		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1924			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
1925		btc8821a1ant_coex_under_5g(btcoexist);
1926		return;
1927	}
1928
1929	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
1930	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1931	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
1932		increase_scan_dev_num = true;
1933
1934	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
1935			   &increase_scan_dev_num);
1936
1937	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1938			   &wifi_connected);
1939
1940	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
1941			   &wifi_link_status);
1942	num_of_wifi_link = wifi_link_status >> 16;
1943	if ((num_of_wifi_link >= 2) ||
1944	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
1945		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1946		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
1947					bt_ctrl_agg_buf_size, agg_buf_size);
1948		btc8821a1ant_action_wifi_multi_port(btcoexist);
1949		return;
1950	}
1951
1952	if (!bt_link_info->sco_exist && !bt_link_info->hid_exist) {
1953		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1954	} else {
1955		if (wifi_connected) {
1956			btc8821a1ant_wifi_rssi_state(btcoexist, 1, 2,
1957							     30, 0);
1958			btc8821a1ant_limited_tx(btcoexist,
1959						NORMAL_EXEC, 1, 1,
1960						0, 1);
1961		} else {
1962			btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC,
1963						0, 0, 0, 0);
1964		}
1965	}
1966
1967	if (bt_link_info->sco_exist) {
1968		bt_ctrl_agg_buf_size = true;
1969		agg_buf_size = 0x3;
1970	} else if (bt_link_info->hid_exist) {
1971		bt_ctrl_agg_buf_size = true;
1972		agg_buf_size = 0x5;
1973	} else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
1974		bt_ctrl_agg_buf_size = true;
1975		agg_buf_size = 0x8;
1976	}
1977	btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
1978				bt_ctrl_agg_buf_size, agg_buf_size);
1979
1980	btc8821a1ant_run_sw_coex_mech(btcoexist);
1981
1982	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1983	if (coex_sta->c2h_bt_inquiry_page) {
1984		btc8821a1ant_action_bt_inquiry(btcoexist);
1985		return;
1986	} else if (bt_hs_on) {
1987		btc8821a1ant_action_hs(btcoexist);
1988		return;
1989	}
1990
1991	if (!wifi_connected) {
1992		bool scan = false, link = false, roam = false;
1993
1994		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1995			"[BTCoex], wifi is non connected-idle !!!\n");
1996
1997		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1998		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1999		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2000
2001		if (scan || link || roam) {
2002			if (scan)
2003				btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2004			else
2005				btc8821a1ant_action_wifi_not_connected_asso_auth(
2006					btcoexist);
2007		} else {
2008			btc8821a1ant_action_wifi_not_connected(btcoexist);
2009		}
2010	} else {
2011		/* wifi LPS/Busy */
2012		btc8821a1ant_action_wifi_connected(btcoexist);
2013	}
2014}
2015
2016static void btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2017{
2018	/* force to reset coex mechanism
2019	 * sw all off
2020	 */
2021	btc8821a1ant_sw_mechanism(btcoexist, false);
2022
2023	btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2024}
2025
2026static void btc8821a1ant_init_hw_config(struct btc_coexist *btcoexist,
2027					bool back_up, bool wifi_only)
2028{
2029	struct rtl_priv *rtlpriv = btcoexist->adapter;
2030	u8 u1_tmp = 0;
2031	bool wifi_under_5g = false;
2032
2033	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2034		"[BTCoex], 1Ant Init HW Config!!\n");
2035
2036	if (wifi_only)
2037		return;
2038
2039	if (back_up) {
2040		coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2041								      0x430);
2042		coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2043								      0x434);
2044		coex_dm->backup_retry_limit =
2045			btcoexist->btc_read_2byte(btcoexist, 0x42a);
2046		coex_dm->backup_ampdu_max_time =
2047			btcoexist->btc_read_1byte(btcoexist, 0x456);
2048	}
2049
2050	/* 0x790[5:0] = 0x5 */
2051	u1_tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2052	u1_tmp &= 0xc0;
2053	u1_tmp |= 0x5;
2054	btcoexist->btc_write_1byte(btcoexist, 0x790, u1_tmp);
2055
2056	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2057
2058	/* Antenna config */
2059	if (wifi_under_5g)
2060		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2061					  true, false);
2062	else
2063		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2064					  true, false);
2065	/* PTA parameter */
2066	btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2067
2068	/* Enable counter statistics
2069	 * 0x76e[3] =1, WLAN_Act control by PTA
2070	 */
2071	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
2072	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2073	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2074}
2075
2076/**************************************************************
2077 * extern function start with ex_btc8821a1ant_
2078 **************************************************************/
2079void ex_btc8821a1ant_init_hwconfig(struct btc_coexist *btcoexist, bool wifionly)
2080{
2081	btc8821a1ant_init_hw_config(btcoexist, true, wifionly);
2082	btcoexist->auto_report_1ant = true;
2083}
2084
2085void ex_btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2086{
2087	struct rtl_priv *rtlpriv = btcoexist->adapter;
2088
2089	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2090		"[BTCoex], Coex Mechanism Init!!\n");
2091
2092	btcoexist->stop_coex_dm = false;
2093
2094	btc8821a1ant_init_coex_dm(btcoexist);
2095
2096	btc8821a1ant_query_bt_info(btcoexist);
2097}
2098
2099void ex_btc8821a1ant_display_coex_info(struct btc_coexist *btcoexist,
2100				       struct seq_file *m)
2101{
2102	struct btc_board_info *board_info = &btcoexist->board_info;
2103	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2104	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2105	u8 u1_tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2106	u16 u2_tmp[4];
2107	u32 u4_tmp[4];
2108	bool roam = false, scan = false, link = false, wifi_under_5g = false;
2109	bool bt_hs_on = false, wifi_busy = false;
2110	long wifi_rssi = 0, bt_hs_rssi = 0;
2111	u32 wifi_bw, wifi_traffic_dir;
2112	u8 wifi_dot11_chnl, wifi_hs_chnl;
2113	u32 fw_ver = 0, bt_patch_ver = 0;
2114
2115	seq_puts(m, "\n ============[BT Coexist info]============");
2116
2117	if (btcoexist->manual_control) {
2118		seq_puts(m, "\n ============[Under Manual Control]============");
2119		seq_puts(m, "\n ==========================================");
2120	}
2121	if (btcoexist->stop_coex_dm) {
2122		seq_puts(m, "\n ============[Coex is STOPPED]============");
2123		seq_puts(m, "\n ==========================================");
2124	}
2125
2126	seq_printf(m, "\n %-35s = %d/ %d/ %d",
2127		   "Ant PG Num/ Ant Mech/ Ant Pos:",
2128		   board_info->pg_ant_num,
2129		   board_info->btdm_ant_num,
2130		   board_info->btdm_ant_pos);
2131
2132	seq_printf(m, "\n %-35s = %s / %d", "BT stack/ hci ext ver",
2133		   ((stack_info->profile_notified) ? "Yes" : "No"),
2134		   stack_info->hci_version);
2135
2136	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2137			   &bt_patch_ver);
2138	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2139	seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2140		   "CoexVer/ FwVer/ PatchVer",
2141		   glcoex_ver_date_8821a_1ant,
2142		   glcoex_ver_8821a_1ant,
2143		   fw_ver, bt_patch_ver,
2144		   bt_patch_ver);
2145
2146	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION,
2147			   &bt_hs_on);
2148	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2149			   &wifi_dot11_chnl);
2150	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL,
2151			   &wifi_hs_chnl);
2152	seq_printf(m, "\n %-35s = %d / %d(%d)",
2153		   "Dot11 channel / HsChnl(HsMode)",
2154		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2155
2156	seq_printf(m, "\n %-35s = %3ph ",
2157		   "H2C Wifi inform bt chnl Info",
2158		   coex_dm->wifi_chnl_info);
2159
2160	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2161	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2162	seq_printf(m, "\n %-35s = %d/ %d", "Wifi rssi/ HS rssi",
2163		   (int)wifi_rssi, (int)bt_hs_rssi);
2164
2165	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2166	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2167	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2168	seq_printf(m, "\n %-35s = %d/ %d/ %d ", "Wifi link/ roam/ scan",
2169		   link, roam, scan);
2170
2171	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2172			   &wifi_under_5g);
2173	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2174			   &wifi_bw);
2175	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
2176			   &wifi_busy);
2177	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2178			   &wifi_traffic_dir);
2179	seq_printf(m, "\n %-35s = %s / %s/ %s ", "Wifi status",
2180		   (wifi_under_5g ? "5G" : "2.4G"),
2181		   ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2182		   (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
2183		   ((!wifi_busy) ? "idle" :
2184		   ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2185		   "uplink" : "downlink")));
2186	seq_printf(m, "\n %-35s = [%s/ %d/ %d] ",
2187		   "BT [status/ rssi/ retryCnt]",
2188		   ((coex_sta->bt_disabled) ? ("disabled") :
2189		   ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2190		   ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2191		     coex_dm->bt_status) ?
2192		   "non-connected idle" :
2193		   ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
2194		     coex_dm->bt_status) ?
2195		   "connected-idle" : "busy")))),
2196		   coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2197
2198	seq_printf(m, "\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
2199		   bt_link_info->sco_exist,
2200		   bt_link_info->hid_exist,
2201		   bt_link_info->pan_exist,
2202		   bt_link_info->a2dp_exist);
2203	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
2204
2205	bt_info_ext = coex_sta->bt_info_ext;
2206	seq_printf(m, "\n %-35s = %s",
2207		   "BT Info A2DP rate",
2208		   (bt_info_ext & BIT0) ?
2209		   "Basic rate" : "EDR rate");
2210
2211	for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2212		if (coex_sta->bt_info_c2h_cnt[i]) {
2213			seq_printf(m, "\n %-35s = %7ph(%d)",
2214				   glbt_info_src_8821a_1ant[i],
2215				   coex_sta->bt_info_c2h[i],
2216				   coex_sta->bt_info_c2h_cnt[i]);
2217		}
2218	}
2219	seq_printf(m, "\n %-35s = %s/%s, (0x%x/0x%x)",
2220		   "PS state, IPS/LPS, (lps/rpwm)",
2221		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2222		   ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")),
2223		   btcoexist->bt_info.lps_val,
2224		   btcoexist->bt_info.rpwm_val);
2225	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
2226
2227	if (!btcoexist->manual_control) {
2228		/* Sw mechanism*/
2229		seq_printf(m, "\n %-35s",
2230			   "============[Sw mechanism]============");
2231
2232		seq_printf(m, "\n %-35s = %d", "SM[LowPenaltyRA]",
2233			   coex_dm->cur_low_penalty_ra);
2234
2235		seq_printf(m, "\n %-35s = %s/ %s/ %d ",
2236			   "DelBA/ BtCtrlAgg/ AggSize",
2237			   (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2238			   (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2239			   btcoexist->bt_info.agg_buf_size);
2240		seq_printf(m, "\n %-35s = 0x%x ", "Rate Mask",
2241			   btcoexist->bt_info.ra_mask);
2242
2243		/* Fw mechanism */
2244		seq_printf(m, "\n %-35s",
2245			   "============[Fw mechanism]============");
2246
2247		ps_tdma_case = coex_dm->cur_ps_tdma;
2248		seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
2249			   "PS TDMA",
2250			   coex_dm->ps_tdma_para,
2251			   ps_tdma_case,
2252			   coex_dm->auto_tdma_adjust);
2253
2254		seq_printf(m, "\n %-35s = 0x%x ",
2255			   "Latest error condition(should be 0)",
2256			   coex_dm->error_condition);
2257
2258		seq_printf(m, "\n %-35s = %d ", "IgnWlanAct",
2259			   coex_dm->cur_ignore_wlan_act);
2260	}
2261
2262	/* Hw setting */
2263	seq_printf(m, "\n %-35s", "============[Hw setting]============");
2264
2265	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2266		   "backup ARFR1/ARFR2/RL/AMaxTime",
2267		   coex_dm->backup_arfr_cnt1,
2268		   coex_dm->backup_arfr_cnt2,
2269		   coex_dm->backup_retry_limit,
2270		   coex_dm->backup_ampdu_max_time);
2271
2272	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2273	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2274	u2_tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2275	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2276	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2277		   "0x430/0x434/0x42a/0x456",
2278		   u4_tmp[0], u4_tmp[1], u2_tmp[0], u1_tmp[0]);
2279
2280	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2281	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2282	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x778/ 0xc58[29:25]",
2283		   u1_tmp[0], (u4_tmp[0] & 0x3e000000) >> 25);
2284
2285	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2286	seq_printf(m, "\n %-35s = 0x%x", "0x8db[6:5]",
2287		   ((u1_tmp[0] & 0x60) >> 5));
2288
2289	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2290	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2291	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2292		   "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2293		   (u4_tmp[0] & 0x30000000) >> 28,
2294		    u4_tmp[0] & 0xff,
2295		    u1_tmp[0] & 0x3);
2296
2297	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2298	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2299	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2300	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2301		   "0x40/0x4c[24:23]/0x64[0]",
2302		   u1_tmp[0], ((u4_tmp[0] & 0x01800000) >> 23),
2303		   u1_tmp[1] & 0x1);
2304
2305	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2306	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2307	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522",
2308		   u4_tmp[0], u1_tmp[0]);
2309
2310	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2311	seq_printf(m, "\n %-35s = 0x%x", "0xc50(dig)",
2312		   u4_tmp[0] & 0xff);
2313
2314	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xf48);
2315	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5d);
2316	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2317	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "OFDM-FA/ CCK-FA",
2318		   u4_tmp[0], (u1_tmp[0] << 8) + u1_tmp[1]);
2319
2320	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2321	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2322	u4_tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2323	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2324	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2325		   "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2326		   u4_tmp[0], u4_tmp[1], u4_tmp[2], u1_tmp[0]);
2327
2328	seq_printf(m, "\n %-35s = %d/ %d", "0x770(high-pri rx/tx)",
2329		   coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2330	seq_printf(m, "\n %-35s = %d/ %d", "0x774(low-pri rx/tx)",
2331		   coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2332	if (btcoexist->auto_report_1ant)
2333		btc8821a1ant_monitor_bt_ctr(btcoexist);
2334	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
2335}
2336
2337void ex_btc8821a1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2338{
2339	struct rtl_priv *rtlpriv = btcoexist->adapter;
2340	bool wifi_under_5g = false;
2341
2342	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2343		return;
2344	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2345	if (wifi_under_5g) {
2346		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2347			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2348		btc8821a1ant_coex_under_5g(btcoexist);
2349		return;
2350	}
2351
2352	if (BTC_IPS_ENTER == type) {
2353		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2354			"[BTCoex], IPS ENTER notify\n");
2355		coex_sta->under_ips = true;
2356		btc8821a1ant_set_ant_path(btcoexist,
2357					  BTC_ANT_PATH_BT, false, true);
2358		/* set PTA control */
2359		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2360		btc8821a1ant_coex_table_with_type(btcoexist,
2361						  NORMAL_EXEC, 0);
2362	} else if (BTC_IPS_LEAVE == type) {
2363		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2364			"[BTCoex], IPS LEAVE notify\n");
2365		coex_sta->under_ips = false;
2366
2367		btc8821a1ant_init_hw_config(btcoexist, false, false);
2368		btc8821a1ant_init_coex_dm(btcoexist);
2369		btc8821a1ant_query_bt_info(btcoexist);
2370	}
2371}
2372
2373void ex_btc8821a1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2374{
2375	struct rtl_priv *rtlpriv = btcoexist->adapter;
2376
2377	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2378		return;
2379
2380	if (BTC_LPS_ENABLE == type) {
2381		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2382			"[BTCoex], LPS ENABLE notify\n");
2383		coex_sta->under_lps = true;
2384	} else if (BTC_LPS_DISABLE == type) {
2385		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2386			"[BTCoex], LPS DISABLE notify\n");
2387		coex_sta->under_lps = false;
2388	}
2389}
2390
2391void ex_btc8821a1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2392{
2393	struct rtl_priv *rtlpriv = btcoexist->adapter;
2394	bool wifi_connected = false, bt_hs_on = false;
2395	bool bt_ctrl_agg_buf_size = false;
2396	bool wifi_under_5g = false;
2397	u32 wifi_link_status = 0;
2398	u32 num_of_wifi_link = 0;
2399	u8 agg_buf_size = 5;
2400
2401	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2402		return;
2403	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2404	if (wifi_under_5g) {
2405		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2406			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2407		btc8821a1ant_coex_under_5g(btcoexist);
2408		return;
2409	}
2410
2411	if (type == BTC_SCAN_START) {
2412		coex_sta->wifi_is_high_pri_task = true;
2413		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2414			"[BTCoex], SCAN START notify\n");
2415
2416		/* Force antenna setup for no scan result issue */
2417		btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2418	} else {
2419		coex_sta->wifi_is_high_pri_task = false;
2420		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2421			"[BTCoex], SCAN FINISH notify\n");
2422	}
2423
2424	if (coex_sta->bt_disabled)
2425		return;
2426
2427	btcoexist->btc_get(btcoexist,
2428		 BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2429	btcoexist->btc_get(btcoexist,
2430		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2431
2432	btc8821a1ant_query_bt_info(btcoexist);
2433
2434	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2435			   &wifi_link_status);
2436	num_of_wifi_link = wifi_link_status >> 16;
2437	if (num_of_wifi_link >= 2) {
2438		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2439		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2440					bt_ctrl_agg_buf_size, agg_buf_size);
2441		btc8821a1ant_action_wifi_multi_port(btcoexist);
2442		return;
2443	}
2444
2445	if (coex_sta->c2h_bt_inquiry_page) {
2446		btc8821a1ant_action_bt_inquiry(btcoexist);
2447		return;
2448	} else if (bt_hs_on) {
2449		btc8821a1ant_action_hs(btcoexist);
2450		return;
2451	}
2452
2453	if (BTC_SCAN_START == type) {
2454		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2455			"[BTCoex], SCAN START notify\n");
2456		if (!wifi_connected) {
2457			/* non-connected scan */
2458			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2459		} else {
2460			/* wifi is connected */
2461			btc8821a1ant_action_wifi_connected_scan(btcoexist);
2462		}
2463	} else if (BTC_SCAN_FINISH == type) {
2464		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2465			"[BTCoex], SCAN FINISH notify\n");
2466		if (!wifi_connected) {
2467			/* non-connected scan */
2468			btc8821a1ant_action_wifi_not_connected(btcoexist);
2469		} else {
2470			btc8821a1ant_action_wifi_connected(btcoexist);
2471		}
2472	}
2473}
2474
2475void ex_btc8821a1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2476{
2477	struct rtl_priv *rtlpriv = btcoexist->adapter;
2478	bool wifi_connected = false, bt_hs_on = false;
2479	u32 wifi_link_status = 0;
2480	u32 num_of_wifi_link = 0;
2481	bool bt_ctrl_agg_buf_size = false;
2482	bool wifi_under_5g = false;
2483	u8 agg_buf_size = 5;
2484
2485	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2486	    coex_sta->bt_disabled)
2487		return;
2488	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2489	if (wifi_under_5g) {
2490		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2491			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2492		btc8821a1ant_coex_under_5g(btcoexist);
2493		return;
2494	}
2495
2496	if (type == BTC_ASSOCIATE_START) {
2497		coex_sta->wifi_is_high_pri_task = true;
2498		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2499			"[BTCoex], CONNECT START notify\n");
2500		coex_dm->arp_cnt = 0;
2501	} else {
2502		coex_sta->wifi_is_high_pri_task = false;
2503		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2504			"[BTCoex], CONNECT FINISH notify\n");
2505		coex_dm->arp_cnt = 0;
2506	}
2507
2508	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2509			   &wifi_link_status);
2510	num_of_wifi_link = wifi_link_status >> 16;
2511	if (num_of_wifi_link >= 2) {
2512		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2513		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2514					bt_ctrl_agg_buf_size, agg_buf_size);
2515		btc8821a1ant_action_wifi_multi_port(btcoexist);
2516		return;
2517	}
2518
2519	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2520	if (coex_sta->c2h_bt_inquiry_page) {
2521		btc8821a1ant_action_bt_inquiry(btcoexist);
2522		return;
2523	} else if (bt_hs_on) {
2524		btc8821a1ant_action_hs(btcoexist);
2525		return;
2526	}
2527
2528	if (BTC_ASSOCIATE_START == type) {
2529		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2530			"[BTCoex], CONNECT START notify\n");
2531		btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2532	} else if (BTC_ASSOCIATE_FINISH == type) {
2533		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2534			"[BTCoex], CONNECT FINISH notify\n");
2535
2536		btcoexist->btc_get(btcoexist,
2537			 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2538		if (!wifi_connected) {
2539			/* non-connected scan */
2540			btc8821a1ant_action_wifi_not_connected(btcoexist);
2541		} else {
2542			btc8821a1ant_action_wifi_connected(btcoexist);
2543		}
2544	}
2545}
2546
2547void ex_btc8821a1ant_media_status_notify(struct btc_coexist *btcoexist,
2548					 u8 type)
2549{
2550	struct rtl_priv *rtlpriv = btcoexist->adapter;
2551	u8 h2c_parameter[3] = {0};
2552	u32 wifi_bw;
2553	u8 wifi_central_chnl;
2554	bool wifi_under_5g = false;
2555
2556	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2557	    coex_sta->bt_disabled)
2558		return;
2559	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2560	if (wifi_under_5g) {
2561		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2562			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2563		btc8821a1ant_coex_under_5g(btcoexist);
2564		return;
2565	}
2566
2567	if (BTC_MEDIA_CONNECT == type) {
2568		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2569			"[BTCoex], MEDIA connect notify\n");
2570	} else {
2571		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2572			"[BTCoex], MEDIA disconnect notify\n");
2573		coex_dm->arp_cnt = 0;
2574	}
2575
2576	/* only 2.4G we need to inform bt the chnl mask */
2577	btcoexist->btc_get(btcoexist,
2578			   BTC_GET_U1_WIFI_CENTRAL_CHNL,
2579			   &wifi_central_chnl);
2580	if ((type == BTC_MEDIA_CONNECT) &&
2581	    (wifi_central_chnl <= 14)) {
2582		h2c_parameter[0] = 0x0;
2583		h2c_parameter[1] = wifi_central_chnl;
2584		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2585		if (wifi_bw == BTC_WIFI_BW_HT40)
2586			h2c_parameter[2] = 0x30;
2587		else
2588			h2c_parameter[2] = 0x20;
2589	}
2590
2591	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2592	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2593	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2594
2595	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2596		"[BTCoex], FW write 0x66 = 0x%x\n",
2597		h2c_parameter[0] << 16 |
2598		h2c_parameter[1] << 8 |
2599		h2c_parameter[2]);
2600
2601	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2602}
2603
2604void ex_btc8821a1ant_special_packet_notify(struct btc_coexist *btcoexist,
2605					   u8 type)
2606{
2607	struct rtl_priv *rtlpriv = btcoexist->adapter;
2608	bool bt_hs_on = false;
2609	bool bt_ctrl_agg_buf_size = false;
2610	bool wifi_under_5g = false;
2611	u32 wifi_link_status = 0;
2612	u32 num_of_wifi_link = 0;
2613	u8 agg_buf_size = 5;
2614
2615	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2616	    coex_sta->bt_disabled)
2617		return;
2618
2619	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2620	if (wifi_under_5g) {
2621		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2622			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2623		btc8821a1ant_coex_under_5g(btcoexist);
2624		return;
2625	}
2626
2627	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2628	    type == BTC_PACKET_ARP) {
2629		coex_sta->wifi_is_high_pri_task = true;
2630
2631		if (type == BTC_PACKET_ARP) {
2632			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2633				"[BTCoex], specific Packet ARP notify\n");
2634		} else {
2635			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2636				"[BTCoex], specific Packet DHCP or EAPOL notify\n");
2637		}
2638	} else {
2639		coex_sta->wifi_is_high_pri_task = false;
2640		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2641			"[BTCoex], specific Packet [Type = %d] notify\n",
2642			type);
2643	}
2644
2645	coex_sta->special_pkt_period_cnt = 0;
2646
2647	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2648			   &wifi_link_status);
2649	num_of_wifi_link = wifi_link_status >> 16;
2650	if (num_of_wifi_link >= 2) {
2651		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2652		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2653					bt_ctrl_agg_buf_size, agg_buf_size);
2654		btc8821a1ant_action_wifi_multi_port(btcoexist);
2655		return;
2656	}
2657
2658	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2659	if (coex_sta->c2h_bt_inquiry_page) {
2660		btc8821a1ant_action_bt_inquiry(btcoexist);
2661		return;
2662	} else if (bt_hs_on) {
2663		btc8821a1ant_action_hs(btcoexist);
2664		return;
2665	}
2666
2667	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2668	    type == BTC_PACKET_ARP) {
2669		if (type == BTC_PACKET_ARP) {
2670			coex_dm->arp_cnt++;
2671			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2672				"[BTCoex], ARP Packet Count = %d\n",
2673				coex_dm->arp_cnt);
2674			if (coex_dm->arp_cnt >= 10)
2675				/* if APR PKT > 10 after connect, do not go to
2676				 * btc8821a1ant_act_wifi_conn_sp_pkt
2677				 */
2678				return;
2679		}
2680
2681		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2682			"[BTCoex], special Packet(%d) notify\n", type);
2683		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
2684	}
2685}
2686
2687void ex_btc8821a1ant_bt_info_notify(struct btc_coexist *btcoexist,
2688				    u8 *tmp_buf, u8 length)
2689{
2690	struct rtl_priv *rtlpriv = btcoexist->adapter;
2691	u8 i;
2692	u8 bt_info = 0;
2693	u8 rsp_source = 0;
2694	bool wifi_connected = false;
2695	bool bt_busy = false;
2696	bool wifi_under_5g = false;
2697
2698	coex_sta->c2h_bt_info_req_sent = false;
2699
2700	btcoexist->btc_get(btcoexist,
2701		 BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2702
2703	rsp_source = tmp_buf[0] & 0xf;
2704	if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2705		rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2706	coex_sta->bt_info_c2h_cnt[rsp_source]++;
2707
2708	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2709		"[BTCoex], Bt info[%d], length = %d, hex data = [",
2710		rsp_source, length);
2711	for (i = 0; i < length; i++) {
2712		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2713		if (i == 1)
2714			bt_info = tmp_buf[i];
2715		if (i == length - 1) {
2716			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2717				"0x%02x]\n", tmp_buf[i]);
2718		} else {
2719			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2720				"0x%02x, ", tmp_buf[i]);
2721		}
2722	}
2723
2724	if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
2725		/* [3:0] */
2726		coex_sta->bt_retry_cnt =
2727			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
2728
2729		coex_sta->bt_rssi =
2730			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
2731
2732		coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
2733
2734		coex_sta->bt_tx_rx_mask =
2735			(coex_sta->bt_info_c2h[rsp_source][2] & 0x40);
2736		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
2737				   &coex_sta->bt_tx_rx_mask);
2738		if (!coex_sta->bt_tx_rx_mask) {
2739			/* BT into is responded by BT FW and BT RF REG 0x3C !=
2740			 * 0x15 => Need to switch BT TRx Mask
2741			 */
2742			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2743				"[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
2744			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
2745						  0x3c, 0x15);
2746		}
2747
2748		/* Here we need to resend some wifi info to BT
2749		 * because bt is reset and lost the info
2750		 */
2751		if (coex_sta->bt_info_ext & BIT1) {
2752			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2753				"[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
2754			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2755					   &wifi_connected);
2756			if (wifi_connected) {
2757				ex_btc8821a1ant_media_status_notify(btcoexist,
2758							       BTC_MEDIA_CONNECT);
2759			} else {
2760				ex_btc8821a1ant_media_status_notify(btcoexist,
2761							       BTC_MEDIA_DISCONNECT);
2762			}
2763		}
2764
2765		if ((coex_sta->bt_info_ext & BIT3) && !wifi_under_5g) {
2766			if (!btcoexist->manual_control &&
2767			    !btcoexist->stop_coex_dm) {
2768				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2769					"[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
2770				btc8821a1ant_ignore_wlan_act(btcoexist,
2771							     FORCE_EXEC,
2772							     false);
2773			}
2774		}
2775	}
2776
2777	/* check BIT2 first ==> check if bt is under inquiry or page scan */
2778	if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
2779		coex_sta->c2h_bt_inquiry_page = true;
2780	else
2781		coex_sta->c2h_bt_inquiry_page = false;
2782
2783	/* set link exist status */
2784	if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2785		coex_sta->bt_link_exist = false;
2786		coex_sta->pan_exist = false;
2787		coex_sta->a2dp_exist = false;
2788		coex_sta->hid_exist = false;
2789		coex_sta->sco_exist = false;
2790	} else {
2791		/* connection exists */
2792		coex_sta->bt_link_exist = true;
2793		if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
2794			coex_sta->pan_exist = true;
2795		else
2796			coex_sta->pan_exist = false;
2797		if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
2798			coex_sta->a2dp_exist = true;
2799		else
2800			coex_sta->a2dp_exist = false;
2801		if (bt_info & BT_INFO_8821A_1ANT_B_HID)
2802			coex_sta->hid_exist = true;
2803		else
2804			coex_sta->hid_exist = false;
2805		if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
2806			coex_sta->sco_exist = true;
2807		else
2808			coex_sta->sco_exist = false;
2809	}
2810
2811	btc8821a1ant_update_bt_link_info(btcoexist);
2812
2813	/* mask profile bit for connect-ilde identification
2814	 * (for CSR case: A2DP idle --> 0x41)
2815	 */
2816	bt_info = bt_info & 0x1f;
2817
2818	if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2819		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
2820		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2821			"[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
2822	} else if (bt_info == BT_INFO_8821A_1ANT_B_CONNECTION) {
2823		/* connection exists but no busy */
2824		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
2825		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2826			"[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
2827	} else if ((bt_info&BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
2828		(bt_info & BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
2829		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
2830		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2831			"[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
2832	} else if (bt_info & BT_INFO_8821A_1ANT_B_ACL_BUSY) {
2833		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
2834			coex_dm->auto_tdma_adjust = false;
2835		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
2836		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2837			"[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
2838	} else {
2839		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
2840		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2841			"[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
2842	}
2843
2844	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2845	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2846	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2847		bt_busy = true;
2848	else
2849		bt_busy = false;
2850	btcoexist->btc_set(btcoexist,
2851			   BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2852
2853	btc8821a1ant_run_coexist_mechanism(btcoexist);
2854}
2855
2856void ex_btc8821a1ant_halt_notify(struct btc_coexist *btcoexist)
2857{
2858	struct rtl_priv *rtlpriv = btcoexist->adapter;
2859	bool wifi_under_5g = false;
2860
2861	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2862		"[BTCoex], Halt notify\n");
2863	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2864	if (wifi_under_5g) {
2865		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2866			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2867		btc8821a1ant_coex_under_5g(btcoexist);
2868		return;
2869	}
2870
2871
2872	btcoexist->stop_coex_dm = true;
2873
2874	btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false, true);
2875	btc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2876
2877	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2878	btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
2879
2880	ex_btc8821a1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
2881}
2882
2883void ex_btc8821a1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
2884{
2885	struct rtl_priv *rtlpriv = btcoexist->adapter;
2886	bool wifi_under_5g = false;
2887
2888	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2889	if (wifi_under_5g) {
2890		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2891			"[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2892		btc8821a1ant_coex_under_5g(btcoexist);
2893		return;
2894	}
2895
2896	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2897		"[BTCoex], Pnp notify\n");
2898
2899	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
2900		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2901			"[BTCoex], Pnp notify to SLEEP\n");
2902		/* BT should clear UnderIPS/UnderLPS state to avoid mismatch
2903		 * state after wakeup.
2904		 */
2905		coex_sta->under_ips = false;
2906		coex_sta->under_lps = false;
2907		btcoexist->stop_coex_dm = true;
2908		btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2909					      0x0, 0x0);
2910		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2911		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2912		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
2913					  true);
2914	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
2915		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2916			"[BTCoex], Pnp notify to WAKE UP\n");
2917		btcoexist->stop_coex_dm = false;
2918		btc8821a1ant_init_hw_config(btcoexist, false, false);
2919		btc8821a1ant_init_coex_dm(btcoexist);
2920		btc8821a1ant_query_bt_info(btcoexist);
2921	}
2922}
2923
2924void ex_btc8821a1ant_periodical(struct btc_coexist *btcoexist)
2925{
2926	struct rtl_priv *rtlpriv = btcoexist->adapter;
2927	static u8 dis_ver_info_cnt;
2928	u32 fw_ver = 0, bt_patch_ver = 0;
2929	struct btc_board_info *board_info = &btcoexist->board_info;
2930	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2931
2932	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2933		"[BTCoex], ==========================Periodical===========================\n");
2934
2935	if (dis_ver_info_cnt <= 5) {
2936		dis_ver_info_cnt += 1;
2937		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2938			"[BTCoex], ****************************************************************\n");
2939		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2940			"[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
2941			board_info->pg_ant_num,
2942			board_info->btdm_ant_num,
2943			board_info->btdm_ant_pos);
2944		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2945			"[BTCoex], BT stack/ hci ext ver = %s / %d\n",
2946			stack_info->profile_notified ? "Yes" : "No",
2947			stack_info->hci_version);
2948		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2949				   &bt_patch_ver);
2950		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2951		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2952			"[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
2953			glcoex_ver_date_8821a_1ant,
2954			glcoex_ver_8821a_1ant,
2955			fw_ver, bt_patch_ver,
2956			bt_patch_ver);
2957		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2958			"[BTCoex], ****************************************************************\n");
2959	}
2960
2961	if (!btcoexist->auto_report_1ant) {
2962		btc8821a1ant_query_bt_info(btcoexist);
2963		btc8821a1ant_monitor_bt_ctr(btcoexist);
2964	} else {
2965		coex_sta->special_pkt_period_cnt++;
2966	}
2967}
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2012  Realtek Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of version 2 of the GNU General Public License as
   7 * published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * The full GNU General Public License is included in this distribution in the
  15 * file called LICENSE.
  16 *
  17 * Contact Information:
  18 * wlanfae <wlanfae@realtek.com>
  19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20 * Hsinchu 300, Taiwan.
  21 *
  22 * Larry Finger <Larry.Finger@lwfinger.net>
  23 *
  24 *****************************************************************************/
  25
  26/*============================================================
  27 * Description:
  28 *
  29 * This file is for RTL8821A Co-exist mechanism
  30 *
  31 * History
  32 * 2012/11/15 Cosa first check in.
  33 *
  34 *============================================================
  35*/
  36/*============================================================
  37 * include files
  38 *============================================================
  39 */
  40#include "halbt_precomp.h"
  41/*============================================================
  42 * Global variables, these are static variables
  43 *============================================================
  44 */
  45static struct coex_dm_8821a_1ant glcoex_dm_8821a_1ant;
  46static struct coex_dm_8821a_1ant *coex_dm = &glcoex_dm_8821a_1ant;
  47static struct coex_sta_8821a_1ant glcoex_sta_8821a_1ant;
  48static struct coex_sta_8821a_1ant *coex_sta = &glcoex_sta_8821a_1ant;
  49
  50static const char *const glbt_info_src_8821a_1ant[] = {
  51	  "BT Info[wifi fw]",
  52	  "BT Info[bt rsp]",
  53	  "BT Info[bt auto report]",
  54};
  55
  56static u32	glcoex_ver_date_8821a_1ant = 20130816;
  57static u32	glcoex_ver_8821a_1ant = 0x41;
  58
  59/*============================================================
  60 * local function proto type if needed
  61 *
  62 * local function start with halbtc8821a1ant_
  63 *============================================================
  64 */
  65static u8 halbtc8821a1ant_bt_rssi_state(u8 level_num, u8 rssi_thresh,
  66					u8 rssi_thresh1)
  67{
  68	long	bt_rssi = 0;
  69	u8	bt_rssi_state = coex_sta->pre_bt_rssi_state;
  70
  71	bt_rssi = coex_sta->bt_rssi;
  72
  73	if (level_num == 2) {
  74		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
  75		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
  76			if (bt_rssi >= (rssi_thresh +
  77					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
  78				bt_rssi_state = BTC_RSSI_STATE_HIGH;
  79				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
  80					  "[BTCoex], BT Rssi state switch to High\n");
  81			} else {
  82				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
  83				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
  84					  "[BTCoex], BT Rssi state stay at Low\n");
  85			}
  86		} else {
  87			if (bt_rssi < rssi_thresh) {
  88				bt_rssi_state = BTC_RSSI_STATE_LOW;
  89				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
  90					  "[BTCoex], BT Rssi state switch to Low\n");
  91			} else {
  92				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
  93				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
  94					  "[BTCoex], BT Rssi state stay at High\n");
  95			}
  96		}
  97	} else if (level_num == 3) {
  98		if (rssi_thresh > rssi_thresh1) {
  99			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 100				  "[BTCoex], BT Rssi thresh error!!\n");
 101			return coex_sta->pre_bt_rssi_state;
 102		}
 103
 104		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
 105		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
 106			if (bt_rssi >= (rssi_thresh +
 107					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 108				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
 109				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 110					  "[BTCoex], BT Rssi state switch to Medium\n");
 111			} else {
 112				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
 113				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 114					  "[BTCoex], BT Rssi state stay at Low\n");
 115			}
 116		} else if ((coex_sta->pre_bt_rssi_state ==
 117			   BTC_RSSI_STATE_MEDIUM) ||
 118			   (coex_sta->pre_bt_rssi_state ==
 119			    BTC_RSSI_STATE_STAY_MEDIUM)) {
 120			if (bt_rssi >= (rssi_thresh1 +
 121					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 122				bt_rssi_state = BTC_RSSI_STATE_HIGH;
 123				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 124					  "[BTCoex], BT Rssi state switch to High\n");
 125			} else if (bt_rssi < rssi_thresh) {
 126				bt_rssi_state = BTC_RSSI_STATE_LOW;
 127				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 128					  "[BTCoex], BT Rssi state switch to Low\n");
 129			} else {
 130				bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
 131				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 132					  "[BTCoex], BT Rssi state stay at Medium\n");
 133			}
 134		} else {
 135			if (bt_rssi < rssi_thresh1) {
 136				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
 137				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 138					  "[BTCoex], BT Rssi state switch to Medium\n");
 139			} else {
 140				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 141				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_RSSI_STATE,
 142					  "[BTCoex], BT Rssi state stay at High\n");
 143			}
 144		}
 145	}
 146	coex_sta->pre_bt_rssi_state = bt_rssi_state;
 147
 148	return bt_rssi_state;
 149}
 150
 151static u8 halbtc8821a1ant_WifiRssiState(struct btc_coexist *btcoexist,
 152					u8 index, u8 level_num, u8 rssi_thresh,
 153					u8 rssi_thresh1)
 154{
 155	long	wifi_rssi = 0;
 156	u8	wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
 157
 158	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
 159
 160	if (level_num == 2) {
 161		if ((coex_sta->pre_wifi_rssi_state[index] ==
 162		     BTC_RSSI_STATE_LOW) ||
 163		    (coex_sta->pre_wifi_rssi_state[index] ==
 164		     BTC_RSSI_STATE_STAY_LOW)) {
 165			if (wifi_rssi >=
 166			    (rssi_thresh+BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 167				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
 168				BTC_PRINT(BTC_MSG_ALGORITHM,
 169					  ALGO_WIFI_RSSI_STATE,
 170					  "[BTCoex], wifi RSSI state switch to High\n");
 171			} else {
 172				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
 173				BTC_PRINT(BTC_MSG_ALGORITHM,
 174					  ALGO_WIFI_RSSI_STATE,
 175					  "[BTCoex], wifi RSSI state stay at Low\n");
 176			}
 177		} else {
 178			if (wifi_rssi < rssi_thresh) {
 179				wifi_rssi_state = BTC_RSSI_STATE_LOW;
 180				BTC_PRINT(BTC_MSG_ALGORITHM,
 181					  ALGO_WIFI_RSSI_STATE,
 182					  "[BTCoex], wifi RSSI state switch to Low\n");
 183			} else {
 184				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 185				BTC_PRINT(BTC_MSG_ALGORITHM,
 186					  ALGO_WIFI_RSSI_STATE,
 187					  "[BTCoex], wifi RSSI state stay at High\n");
 188			}
 189		}
 190	} else if (level_num == 3) {
 191		if (rssi_thresh > rssi_thresh1) {
 192			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_WIFI_RSSI_STATE,
 193				  "[BTCoex], wifi RSSI thresh error!!\n");
 194			return coex_sta->pre_wifi_rssi_state[index];
 195		}
 196
 197		if ((coex_sta->pre_wifi_rssi_state[index] ==
 198		     BTC_RSSI_STATE_LOW) ||
 199		    (coex_sta->pre_wifi_rssi_state[index] ==
 200		     BTC_RSSI_STATE_STAY_LOW)) {
 201			if (wifi_rssi >=
 202			    (rssi_thresh+BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 203				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
 204				BTC_PRINT(BTC_MSG_ALGORITHM,
 205					  ALGO_WIFI_RSSI_STATE,
 206					  "[BTCoex], wifi RSSI state switch to Medium\n");
 207			} else {
 208				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
 209				BTC_PRINT(BTC_MSG_ALGORITHM,
 210					  ALGO_WIFI_RSSI_STATE,
 211					  "[BTCoex], wifi RSSI state stay at Low\n");
 212			}
 213		} else if ((coex_sta->pre_wifi_rssi_state[index] ==
 214			BTC_RSSI_STATE_MEDIUM) ||
 215			(coex_sta->pre_wifi_rssi_state[index] ==
 216			BTC_RSSI_STATE_STAY_MEDIUM)) {
 217			if (wifi_rssi >=
 218			    (rssi_thresh1 +
 219			     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
 220				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
 221				BTC_PRINT(BTC_MSG_ALGORITHM,
 222					  ALGO_WIFI_RSSI_STATE,
 223					  "[BTCoex], wifi RSSI state switch to High\n");
 224			} else if (wifi_rssi < rssi_thresh) {
 225				wifi_rssi_state = BTC_RSSI_STATE_LOW;
 226				BTC_PRINT(BTC_MSG_ALGORITHM,
 227					  ALGO_WIFI_RSSI_STATE,
 228					  "[BTCoex], wifi RSSI state switch to Low\n");
 229			} else {
 230				wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
 231				BTC_PRINT(BTC_MSG_ALGORITHM,
 232					  ALGO_WIFI_RSSI_STATE,
 233					  "[BTCoex], wifi RSSI state stay at Medium\n");
 234			}
 235		} else {
 236			if (wifi_rssi < rssi_thresh1) {
 237				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
 238				BTC_PRINT(BTC_MSG_ALGORITHM,
 239					  ALGO_WIFI_RSSI_STATE,
 240					  "[BTCoex], wifi RSSI state switch to Medium\n");
 241			} else {
 242				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
 243				BTC_PRINT(BTC_MSG_ALGORITHM,
 244					  ALGO_WIFI_RSSI_STATE,
 245					  "[BTCoex], wifi RSSI state stay at High\n");
 246			}
 247		}
 248	}
 249	coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
 250
 251	return wifi_rssi_state;
 252}
 253
 254static void halbtc8821a1ant_update_ra_mask(struct btc_coexist *btcoexist,
 255					   bool force_exec, u32 dis_rate_mask)
 256{
 257	coex_dm->cur_ra_mask = dis_rate_mask;
 258
 259	if (force_exec ||
 260	    (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask)) {
 261		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_ra_mask,
 262				   &coex_dm->cur_ra_mask);
 263	}
 264	coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
 265}
 266
 267static void btc8821a1ant_auto_rate_fb_retry(struct btc_coexist *btcoexist,
 268					    bool force_exec, u8 type)
 269{
 270	bool	wifi_under_b_mode = false;
 271
 272	coex_dm->cur_arfr_type = type;
 273
 274	if (force_exec ||
 275	    (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
 276		switch (coex_dm->cur_arfr_type) {
 277		case 0:	/* normal mode*/
 278			btcoexist->btc_write_4byte(btcoexist, 0x430,
 279						   coex_dm->backup_arfr_cnt1);
 280			btcoexist->btc_write_4byte(btcoexist, 0x434,
 281						   coex_dm->backup_arfr_cnt2);
 282			break;
 283		case 1:
 284			btcoexist->btc_get(btcoexist,
 285					   BTC_GET_BL_WIFI_UNDER_B_MODE,
 286					   &wifi_under_b_mode);
 287			if (wifi_under_b_mode) {
 288				btcoexist->btc_write_4byte(btcoexist, 0x430,
 289							   0x0);
 290				btcoexist->btc_write_4byte(btcoexist, 0x434,
 291							   0x01010101);
 292			} else {
 293				btcoexist->btc_write_4byte(btcoexist, 0x430,
 294							   0x0);
 295				btcoexist->btc_write_4byte(btcoexist, 0x434,
 296							   0x04030201);
 297			}
 298			break;
 299		default:
 300			break;
 301		}
 302	}
 303
 304	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
 305}
 306
 307static void halbtc8821a1ant_retry_limit(struct btc_coexist *btcoexist,
 308					bool force_exec, u8 type)
 309{
 310	coex_dm->cur_retry_limit_type = type;
 311
 312	if (force_exec ||
 313	    (coex_dm->pre_retry_limit_type != coex_dm->cur_retry_limit_type)) {
 314		switch (coex_dm->cur_retry_limit_type) {
 315		case 0:	/* normal mode*/
 316			btcoexist->btc_write_2byte(btcoexist, 0x42a,
 317						   coex_dm->backup_retry_limit);
 318			break;
 319		case 1:	/* retry limit = 8*/
 320			btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
 321			break;
 322		default:
 323			break;
 324		}
 325	}
 326	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
 327}
 328
 329static void halbtc8821a1ant_ampdu_max_time(struct btc_coexist *btcoexist,
 330					   bool force_exec, u8 type)
 331{
 332	coex_dm->cur_ampdu_time_type = type;
 333
 334	if (force_exec ||
 335	    (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
 336		switch (coex_dm->cur_ampdu_time_type) {
 337		case 0:	/* normal mode*/
 338			btcoexist->btc_write_1byte(btcoexist, 0x456,
 339						   coex_dm->backup_ampdu_max_time);
 340			break;
 341		case 1:	/* AMPDU timw = 0x38 * 32us*/
 342			btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
 343			break;
 344		default:
 345			break;
 346		}
 347	}
 348
 349	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
 350}
 351
 352static void halbtc8821a1ant_limited_tx(struct btc_coexist *btcoexist,
 353				       bool force_exec, u8 ra_mask_type,
 354				       u8 arfr_type, u8 retry_limit_type,
 355				       u8 ampdu_time_type)
 356{
 357	switch (ra_mask_type) {
 358	case 0:	/* normal mode*/
 359		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec, 0x0);
 360		break;
 361	case 1:	/* disable cck 1/2*/
 362		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
 363					       0x00000003);
 364		break;
 365	case 2:	/* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4*/
 366		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
 367					       0x0001f1f7);
 368		break;
 369	default:
 370		break;
 371	}
 372
 373	btc8821a1ant_auto_rate_fb_retry(btcoexist, force_exec, arfr_type);
 374	halbtc8821a1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
 375	halbtc8821a1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
 376}
 377
 378static void halbtc8821a1ant_limited_rx(struct btc_coexist *btcoexist,
 379				       bool force_exec, bool rej_ap_agg_pkt,
 380				       bool bt_ctrl_agg_buf_size,
 381				       u8 agg_buf_size)
 382{
 383	bool reject_rx_agg = rej_ap_agg_pkt;
 384	bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
 385	u8 rx_agg_size = agg_buf_size;
 386
 387	/*============================================*/
 388	/*	Rx Aggregation related setting*/
 389	/*============================================*/
 390	btcoexist->btc_set(btcoexist,
 391		 BTC_SET_BL_TO_REJ_AP_AGG_PKT, &reject_rx_agg);
 392	/* decide BT control aggregation buf size or not*/
 393	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
 394			   &bt_ctrl_rx_agg_size);
 395	/* aggregation buf size, only work when BT control Rx agg size.*/
 396	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
 397	/* real update aggregation setting*/
 398	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
 399}
 400
 401static void halbtc8821a1ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
 402{
 403	u32	reg_hp_tx_rx, reg_lp_tx_rx, u4_tmp;
 404	u32	reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
 405
 406	reg_hp_tx_rx = 0x770;
 407	reg_lp_tx_rx = 0x774;
 408
 409	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_tx_rx);
 410	reg_hp_tx = u4_tmp & MASKLWORD;
 411	reg_hp_rx = (u4_tmp & MASKHWORD)>>16;
 412
 413	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_tx_rx);
 414	reg_lp_tx = u4_tmp & MASKLWORD;
 415	reg_lp_rx = (u4_tmp & MASKHWORD)>>16;
 416
 417	coex_sta->high_priority_tx = reg_hp_tx;
 418	coex_sta->high_priority_rx = reg_hp_rx;
 419	coex_sta->low_priority_tx = reg_lp_tx;
 420	coex_sta->low_priority_rx = reg_lp_rx;
 421
 422	/* reset counter*/
 423	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
 424}
 425
 426static void halbtc8821a1ant_query_bt_info(struct btc_coexist *btcoexist)
 427{
 428	u8 h2c_parameter[1] = {0};
 429
 430	coex_sta->c2h_bt_info_req_sent = true;
 431
 432	h2c_parameter[0] |= BIT0;	/* trigger*/
 433
 434	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
 435		  "[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
 436		  h2c_parameter[0]);
 437
 438	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
 439}
 440
 441static void halbtc8821a1ant_update_bt_link_info(struct btc_coexist *btcoexist)
 442{
 443	struct btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
 444	bool	bt_hs_on = false;
 445
 446	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
 447
 448	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
 449	bt_link_info->sco_exist = coex_sta->sco_exist;
 450	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
 451	bt_link_info->pan_exist = coex_sta->pan_exist;
 452	bt_link_info->hid_exist = coex_sta->hid_exist;
 453
 454	/* work around for HS mode.*/
 455	if (bt_hs_on) {
 456		bt_link_info->pan_exist = true;
 457		bt_link_info->bt_link_exist = true;
 458	}
 459
 460	/* check if Sco only*/
 461	if (bt_link_info->sco_exist &&
 462	    !bt_link_info->a2dp_exist &&
 463	    !bt_link_info->pan_exist &&
 464	    !bt_link_info->hid_exist)
 465		bt_link_info->sco_only = true;
 466	else
 467		bt_link_info->sco_only = false;
 468
 469	/* check if A2dp only*/
 470	if (!bt_link_info->sco_exist &&
 471	    bt_link_info->a2dp_exist &&
 472	    !bt_link_info->pan_exist &&
 473	    !bt_link_info->hid_exist)
 474		bt_link_info->a2dp_only = true;
 475	else
 476		bt_link_info->a2dp_only = false;
 477
 478	/* check if Pan only*/
 479	if (!bt_link_info->sco_exist &&
 480	    !bt_link_info->a2dp_exist &&
 481	    bt_link_info->pan_exist &&
 482	    !bt_link_info->hid_exist)
 483		bt_link_info->pan_only = true;
 484	else
 485		bt_link_info->pan_only = false;
 486
 487	/* check if Hid only*/
 488	if (!bt_link_info->sco_exist &&
 489	    !bt_link_info->a2dp_exist &&
 490	    !bt_link_info->pan_exist &&
 491	    bt_link_info->hid_exist)
 492		bt_link_info->hid_only = true;
 493	else
 494		bt_link_info->hid_only = false;
 495}
 496
 497static u8 halbtc8821a1ant_action_algorithm(struct btc_coexist *btcoexist)
 498{
 499	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
 500	bool	bt_hs_on = false;
 501	u8	algorithm = BT_8821A_1ANT_COEX_ALGO_UNDEFINED;
 502	u8	num_of_diff_profile = 0;
 503
 504	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
 505
 506	if (!bt_link_info->bt_link_exist) {
 507		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 508			  "[BTCoex], No BT link exists!!!\n");
 509		return algorithm;
 510	}
 511
 512	if (bt_link_info->sco_exist)
 513		num_of_diff_profile++;
 514	if (bt_link_info->hid_exist)
 515		num_of_diff_profile++;
 516	if (bt_link_info->pan_exist)
 517		num_of_diff_profile++;
 518	if (bt_link_info->a2dp_exist)
 519		num_of_diff_profile++;
 520
 521	if (num_of_diff_profile == 1) {
 522		if (bt_link_info->sco_exist) {
 523			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 524				  "[BTCoex], BT Profile = SCO only\n");
 525			algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 526		} else {
 527			if (bt_link_info->hid_exist) {
 528				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 529					  "[BTCoex], BT Profile = HID only\n");
 530				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 531			} else if (bt_link_info->a2dp_exist) {
 532				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 533					  "[BTCoex], BT Profile = A2DP only\n");
 534				algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP;
 535			} else if (bt_link_info->pan_exist) {
 536				if (bt_hs_on) {
 537					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 538						  "[BTCoex], BT Profile = PAN(HS) only\n");
 539					algorithm = BT_8821A_1ANT_COEX_ALGO_PANHS;
 540				} else {
 541					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 542						  "[BTCoex], BT Profile = PAN(EDR) only\n");
 543					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR;
 544				}
 545			}
 546		}
 547	} else if (num_of_diff_profile == 2) {
 548		if (bt_link_info->sco_exist) {
 549			if (bt_link_info->hid_exist) {
 550				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 551					  "[BTCoex], BT Profile = SCO + HID\n");
 552				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 553			} else if (bt_link_info->a2dp_exist) {
 554				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 555					  "[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
 556				algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 557			} else if (bt_link_info->pan_exist) {
 558				if (bt_hs_on) {
 559					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 560						  "[BTCoex], BT Profile = SCO + PAN(HS)\n");
 561					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 562				} else {
 563					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 564						  "[BTCoex], BT Profile = SCO + PAN(EDR)\n");
 565					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 566				}
 567			}
 568		} else {
 569			if (bt_link_info->hid_exist &&
 570			    bt_link_info->a2dp_exist) {
 571				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 572					  "[BTCoex], BT Profile = HID + A2DP\n");
 573				algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 574			} else if (bt_link_info->hid_exist &&
 575				   bt_link_info->pan_exist) {
 576				if (bt_hs_on) {
 577					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 578						  "[BTCoex], BT Profile = HID + PAN(HS)\n");
 579					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 580				} else {
 581					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 582						  "[BTCoex], BT Profile = HID + PAN(EDR)\n");
 583					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 584				}
 585			} else if (bt_link_info->pan_exist &&
 586				   bt_link_info->a2dp_exist) {
 587				if (bt_hs_on) {
 588					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 589						  "[BTCoex], BT Profile = A2DP + PAN(HS)\n");
 590					algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS;
 591				} else {
 592					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 593						  "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
 594					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP;
 595				}
 596			}
 597		}
 598	} else if (num_of_diff_profile == 3) {
 599		if (bt_link_info->sco_exist) {
 600			if (bt_link_info->hid_exist &&
 601			    bt_link_info->a2dp_exist) {
 602				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 603					  "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
 604				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
 605			} else if (bt_link_info->hid_exist &&
 606				bt_link_info->pan_exist) {
 607				if (bt_hs_on) {
 608					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 609						  "[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
 610					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 611				} else {
 612					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 613						  "[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
 614					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 615				}
 616			} else if (bt_link_info->pan_exist &&
 617				bt_link_info->a2dp_exist) {
 618				if (bt_hs_on) {
 619					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 620						  "[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
 621					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
 622				} else {
 623					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 624						  "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
 625					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 626				}
 627			}
 628		} else {
 629			if (bt_link_info->hid_exist &&
 630			    bt_link_info->pan_exist &&
 631			    bt_link_info->a2dp_exist) {
 632				if (bt_hs_on) {
 633					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 634						  "[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
 635					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
 636				} else {
 637					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 638						  "[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
 639					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
 640				}
 641			}
 642		}
 643	} else if (num_of_diff_profile >= 3) {
 644		if (bt_link_info->sco_exist) {
 645			if (bt_link_info->hid_exist &&
 646			    bt_link_info->pan_exist &&
 647			    bt_link_info->a2dp_exist) {
 648				if (bt_hs_on) {
 649					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 650						  "[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
 651
 652				} else {
 653					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
 654						  "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
 655					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
 656				}
 657			}
 658		}
 659	}
 660	return algorithm;
 661}
 662
 663static void halbtc8821a1ant_set_bt_auto_report(struct btc_coexist *btcoexist,
 664					       bool enable_auto_report)
 665{
 666	u8 h2c_parameter[1] = {0};
 667
 668	h2c_parameter[0] = 0;
 669
 670	if (enable_auto_report)
 671		h2c_parameter[0] |= BIT0;
 672
 673	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
 674		  "[BTCoex], BT FW auto report : %s, FW write 0x68 = 0x%x\n",
 675		  (enable_auto_report ? "Enabled!!" : "Disabled!!"),
 676		   h2c_parameter[0]);
 677
 678	btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
 679}
 680
 681static void halbtc8821a1ant_bt_auto_report(struct btc_coexist *btcoexist,
 682					   bool force_exec,
 683					   bool enable_auto_report)
 684{
 685	BTC_PRINT(BTC_MSG_ALGORITHM,
 686		  ALGO_TRACE_FW, "[BTCoex], %s BT Auto report = %s\n",
 687		  (force_exec ? "force to" : ""), ((enable_auto_report) ?
 688		  "Enabled" : "Disabled"));
 689	coex_dm->cur_bt_auto_report = enable_auto_report;
 690
 691	if (!force_exec) {
 692		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
 693			  "[BTCoex], pre_bt_auto_report = %d, cur_bt_auto_report = %d\n",
 694			  coex_dm->pre_bt_auto_report,
 695			  coex_dm->cur_bt_auto_report);
 696
 697		if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
 698			return;
 699	}
 700	halbtc8821a1ant_set_bt_auto_report(btcoexist, coex_dm->cur_bt_auto_report);
 701
 702	coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
 703}
 704
 705static void btc8821a1ant_set_sw_pen_tx_rate(struct btc_coexist *btcoexist,
 706					    bool low_penalty_ra)
 707{
 708	u8 h2c_parameter[6] = {0};
 709
 710	h2c_parameter[0] = 0x6;	/* opCode, 0x6= Retry_Penalty*/
 711
 712	if (low_penalty_ra) {
 713		h2c_parameter[1] |= BIT0;
 714		/*normal rate except MCS7/6/5, OFDM54/48/36*/
 715		h2c_parameter[2] = 0x00;
 716		h2c_parameter[3] = 0xf7;  /*MCS7 or OFDM54*/
 717		h2c_parameter[4] = 0xf8;  /*MCS6 or OFDM48*/
 718		h2c_parameter[5] = 0xf9;	/*MCS5 or OFDM36*/
 719	}
 720
 721	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
 722		  "[BTCoex], set WiFi Low-Penalty Retry: %s",
 723		  (low_penalty_ra ? "ON!!" : "OFF!!"));
 724
 725	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
 726}
 727
 728static void halbtc8821a1ant_low_penalty_ra(struct btc_coexist *btcoexist,
 729					   bool force_exec, bool low_penalty_ra)
 730{
 731	coex_dm->cur_low_penalty_ra = low_penalty_ra;
 732
 733	if (!force_exec) {
 734		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
 735			return;
 736	}
 737	btc8821a1ant_set_sw_pen_tx_rate(btcoexist, coex_dm->cur_low_penalty_ra);
 738
 739	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
 740}
 741
 742static void halbtc8821a1ant_set_coex_table(struct btc_coexist *btcoexist,
 743					   u32 val0x6c0, u32 val0x6c4,
 744					   u32 val0x6c8, u8 val0x6cc)
 745{
 746	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC,
 747		  "[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0);
 748	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
 749
 750	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC,
 751		  "[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4);
 752	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
 753
 754	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC,
 755		  "[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8);
 756	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
 757
 758	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC,
 759		  "[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc);
 760	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
 761}
 762
 763static void halbtc8821a1ant_coex_table(struct btc_coexist *btcoexist,
 764				       bool force_exec, u32 val0x6c0,
 765				       u32 val0x6c4, u32 val0x6c8, u8 val0x6cc)
 766{
 767	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW,
 768		  "[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x\n",
 769		  (force_exec ? "force to" : ""), val0x6c0, val0x6c4,
 770		  val0x6c8, val0x6cc);
 771	coex_dm->cur_val_0x6c0 = val0x6c0;
 772	coex_dm->cur_val_0x6c4 = val0x6c4;
 773	coex_dm->cur_val_0x6c8 = val0x6c8;
 774	coex_dm->cur_val_0x6cc = val0x6cc;
 775
 776	if (!force_exec) {
 777		if ((coex_dm->pre_val_0x6c0 == coex_dm->cur_val_0x6c0) &&
 778		    (coex_dm->pre_val_0x6c4 == coex_dm->cur_val_0x6c4) &&
 779		    (coex_dm->pre_val_0x6c8 == coex_dm->cur_val_0x6c8) &&
 780		    (coex_dm->pre_val_0x6cc == coex_dm->cur_val_0x6cc))
 781			return;
 782	}
 783	halbtc8821a1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
 784				       val0x6c8, val0x6cc);
 785
 786	coex_dm->pre_val_0x6c0 = coex_dm->cur_val_0x6c0;
 787	coex_dm->pre_val_0x6c4 = coex_dm->cur_val_0x6c4;
 788	coex_dm->pre_val_0x6c8 = coex_dm->cur_val_0x6c8;
 789	coex_dm->pre_val_0x6cc = coex_dm->cur_val_0x6cc;
 790}
 791
 792static void halbtc8821a1ant_coex_table_with_type(struct btc_coexist *btcoexist,
 793						 bool force_exec, u8 type)
 794{
 795	switch (type) {
 796	case 0:
 797		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 798					   0x55555555, 0xffffff, 0x3);
 799		break;
 800	case 1:
 801			halbtc8821a1ant_coex_table(btcoexist, force_exec,
 802						   0x55555555, 0x5a5a5a5a,
 803						   0xffffff, 0x3);
 804			break;
 805	case 2:
 806		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
 807					   0x5a5a5a5a, 0xffffff, 0x3);
 808		break;
 809	case 3:
 810		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
 811					   0xaaaaaaaa, 0xffffff, 0x3);
 812		break;
 813	case 4:
 814		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0xffffffff,
 815					   0xffffffff, 0xffffff, 0x3);
 816		break;
 817	case 5:
 818		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
 819					   0x5fff5fff, 0xffffff, 0x3);
 820		break;
 821	case 6:
 822		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
 823					   0x5a5a5a5a, 0xffffff, 0x3);
 824		break;
 825	case 7:
 826		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5afa5afa,
 827					   0x5afa5afa, 0xffffff, 0x3);
 828		break;
 829	default:
 830		break;
 831	}
 832}
 833
 834static void btc8821a1ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
 835						bool enable)
 836{
 837	u8	h2c_parameter[1] = {0};
 838
 839	if (enable)
 840		h2c_parameter[0] |= BIT0;	/* function enable*/
 841
 842	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
 843		  "[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
 844		  h2c_parameter[0]);
 845
 846	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
 847}
 848
 849static void halbtc8821a1ant_ignore_wlan_act(struct btc_coexist *btcoexist,
 850					    bool force_exec, bool enable)
 851{
 852	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW,
 853		  "[BTCoex], %s turn Ignore WlanAct %s\n",
 854		  (force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
 855	coex_dm->cur_ignore_wlan_act = enable;
 856
 857	if (!force_exec) {
 858		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
 859			  "[BTCoex], pre_ignore_wlan_act = %d, cur_ignore_wlan_act = %d!!\n",
 860			  coex_dm->pre_ignore_wlan_act,
 861			  coex_dm->cur_ignore_wlan_act);
 862
 863		if (coex_dm->pre_ignore_wlan_act ==
 864		    coex_dm->cur_ignore_wlan_act)
 865			return;
 866	}
 867	btc8821a1ant_set_fw_ignore_wlan_act(btcoexist, enable);
 868
 869	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
 870}
 871
 872static void halbtc8821a1ant_set_fw_pstdma(struct btc_coexist *btcoexist,
 873					  u8 byte1, u8 byte2, u8 byte3,
 874					  u8 byte4, u8 byte5)
 875{
 876	u8 h2c_parameter[5] = {0};
 877
 878	h2c_parameter[0] = byte1;
 879	h2c_parameter[1] = byte2;
 880	h2c_parameter[2] = byte3;
 881	h2c_parameter[3] = byte4;
 882	h2c_parameter[4] = byte5;
 883
 884	coex_dm->ps_tdma_para[0] = byte1;
 885	coex_dm->ps_tdma_para[1] = byte2;
 886	coex_dm->ps_tdma_para[2] = byte3;
 887	coex_dm->ps_tdma_para[3] = byte4;
 888	coex_dm->ps_tdma_para[4] = byte5;
 889
 890	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
 891		  "[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
 892		  h2c_parameter[0],
 893		  h2c_parameter[1]<<24 |
 894		  h2c_parameter[2]<<16 |
 895		  h2c_parameter[3]<<8 |
 896		  h2c_parameter[4]);
 897	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
 898}
 899
 900static void halbtc8821a1ant_set_lps_rpwm(struct btc_coexist *btcoexist,
 901					 u8 lps_val, u8 rpwm_val)
 902{
 903	u8	lps = lps_val;
 904	u8	rpwm = rpwm_val;
 905
 906	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
 907	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
 908}
 909
 910static void halbtc8821a1ant_lps_rpwm(struct btc_coexist *btcoexist,
 911				     bool force_exec, u8 lps_val, u8 rpwm_val)
 912{
 913	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW,
 914		  "[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
 915		  (force_exec ? "force to" : ""), lps_val, rpwm_val);
 916	coex_dm->cur_lps = lps_val;
 917	coex_dm->cur_rpwm = rpwm_val;
 918
 919	if (!force_exec) {
 920		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
 921			  "[BTCoex], LPS-RxBeaconMode = 0x%x, LPS-RPWM = 0x%x!!\n",
 922			  coex_dm->cur_lps, coex_dm->cur_rpwm);
 923
 924		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
 925		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm)) {
 926			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
 927				  "[BTCoex], LPS-RPWM_Last = 0x%x, LPS-RPWM_Now = 0x%x!!\n",
 928				  coex_dm->pre_rpwm, coex_dm->cur_rpwm);
 929
 930			return;
 931		}
 932	}
 933	halbtc8821a1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
 934
 935	coex_dm->pre_lps = coex_dm->cur_lps;
 936	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
 937}
 938
 939static void halbtc8821a1ant_sw_mechanism(struct btc_coexist *btcoexist,
 940					 bool low_penalty_ra)
 941{
 942	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR,
 943		  "[BTCoex], SM[LpRA] = %d\n", low_penalty_ra);
 944
 945	halbtc8821a1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
 946}
 947
 948static void halbtc8821a1ant_set_ant_path(struct btc_coexist *btcoexist,
 949					 u8 ant_pos_type, bool init_hw_cfg,
 950					 bool wifi_off)
 951{
 952	struct btc_board_info *board_info = &btcoexist->board_info;
 953	u32 u4_tmp = 0;
 954	u8 h2c_parameter[2] = {0};
 955
 956	if (init_hw_cfg) {
 957		/* 0x4c[23] = 0, 0x4c[24] = 1  Antenna control by WL/BT*/
 958		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
 959		u4_tmp &= ~BIT23;
 960		u4_tmp |= BIT24;
 961		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
 962
 963		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x975, 0x3, 0x3);
 964		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
 965
 966		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
 967			/*tell firmware "antenna inverse"  ==>
 968			 * WRONG firmware antenna control code.==>need fw to fix
 969			 */
 970			h2c_parameter[0] = 1;
 971			h2c_parameter[1] = 1;
 972			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
 973						h2c_parameter);
 974			/*Main Ant to  BT for IPS case 0x4c[23] = 1*/
 975			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64,
 976							   0x1, 0x1);
 977		} else {
 978			/*tell firmware "no antenna inverse" ==>
 979			 * WRONG firmware antenna control code.==>need fw to fix
 980			 */
 981			h2c_parameter[0] = 0;
 982			h2c_parameter[1] = 1;
 983			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
 984						h2c_parameter);
 985			/*Aux Ant to  BT for IPS case 0x4c[23] = 1*/
 986			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64,
 987							   0x1, 0x0);
 988		}
 989	} else if (wifi_off) {
 990		/* 0x4c[24:23] = 00, Set Antenna control
 991		 *	by BT_RFE_CTRL	BT Vendor 0xac = 0xf002
 992		 */
 993		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
 994		u4_tmp &= ~BIT23;
 995		u4_tmp &= ~BIT24;
 996		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
 997	}
 998
 999	/* ext switch setting*/
1000	switch (ant_pos_type) {
1001	case BTC_ANT_PATH_WIFI:
1002		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1003			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1004							   0x30, 0x1);
1005		else
1006			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1007							   0x30, 0x2);
1008		break;
1009	case BTC_ANT_PATH_BT:
1010		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1011			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1012							   0x30, 0x2);
1013		else
1014			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1015							   0x30, 0x1);
1016		break;
1017	default:
1018	case BTC_ANT_PATH_PTA:
1019		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1020			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1021							   0x30, 0x1);
1022		else
1023			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1024							   0x30, 0x2);
1025		break;
1026	}
1027}
1028
1029static void halbtc8821a1ant_ps_tdma(struct btc_coexist *btcoexist,
1030				    bool force_exec, bool turn_on, u8 type)
1031{
1032	u8 rssi_adjust_val = 0;
1033
1034	coex_dm->cur_ps_tdma_on = turn_on;
1035	coex_dm->cur_ps_tdma = type;
1036
1037	if (!force_exec) {
1038		if (coex_dm->cur_ps_tdma_on) {
1039			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
1040				  "[BTCoex], ********** TDMA(on, %d) **********\n",
1041				  coex_dm->cur_ps_tdma);
1042		} else {
1043			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
1044				  "[BTCoex], ********** TDMA(off, %d) **********\n",
1045				  coex_dm->cur_ps_tdma);
1046		}
1047		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1048		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1049			return;
1050	}
1051	if (turn_on) {
1052		switch (type) {
1053		default:
1054			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x1a,
1055						      0x1a, 0x0, 0x50);
1056			break;
1057		case 1:
1058			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x3a,
1059						      0x03, 0x10, 0x50);
1060			rssi_adjust_val = 11;
1061			break;
1062		case 2:
1063			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x2b,
1064						      0x03, 0x10, 0x50);
1065			rssi_adjust_val = 14;
1066			break;
1067		case 3:
1068			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x1d,
1069						      0x1d, 0x0, 0x10);
1070			break;
1071		case 4:
1072			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x15,
1073						      0x3, 0x14, 0x0);
1074			rssi_adjust_val = 17;
1075			break;
1076		case 5:
1077			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x15,
1078						      0x3, 0x11, 0x10);
1079			break;
1080		case 6:
1081			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1082						      0x3, 0x0, 0x0);
1083			break;
1084		case 7:
1085			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xc,
1086						      0x5, 0x0, 0x0);
1087			break;
1088		case 8:
1089			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x25,
1090						      0x3, 0x10, 0x0);
1091			break;
1092		case 9:
1093			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x21,
1094						      0x3, 0x10, 0x50);
1095			rssi_adjust_val = 18;
1096			break;
1097		case 10:
1098			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1099						      0xa, 0x0, 0x40);
1100			break;
1101		case 11:
1102			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x14,
1103						      0x03, 0x10, 0x10);
1104			rssi_adjust_val = 20;
1105			break;
1106		case 12:
1107			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x0a,
1108						      0x0a, 0x0, 0x50);
1109			break;
1110		case 13:
1111			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x18,
1112						      0x18, 0x0, 0x10);
1113			break;
1114		case 14:
1115			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x21,
1116						      0x3, 0x10, 0x10);
1117			break;
1118		case 15:
1119			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1120						      0x3, 0x8, 0x0);
1121			break;
1122		case 16:
1123			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x15,
1124						      0x3, 0x10, 0x0);
1125			rssi_adjust_val = 18;
1126			break;
1127		case 18:
1128			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x25,
1129						      0x3, 0x10, 0x0);
1130			rssi_adjust_val = 14;
1131			break;
1132		case 20:
1133			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x35,
1134						      0x03, 0x11, 0x10);
1135			break;
1136		case 21:
1137			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x15,
1138						      0x03, 0x11, 0x10);
1139			break;
1140		case 22:
1141			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x25,
1142						      0x03, 0x11, 0x10);
1143			break;
1144		case 23:
1145			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x25,
1146						      0x3, 0x31, 0x18);
1147			rssi_adjust_val = 22;
1148			break;
1149		case 24:
1150			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x15,
1151						      0x3, 0x31, 0x18);
1152			rssi_adjust_val = 22;
1153			break;
1154		case 25:
1155			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0xa,
1156						      0x3, 0x31, 0x18);
1157			rssi_adjust_val = 22;
1158			break;
1159		case 26:
1160			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0xa,
1161						      0x3, 0x31, 0x18);
1162			rssi_adjust_val = 22;
1163			break;
1164		case 27:
1165			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x25,
1166						      0x3, 0x31, 0x98);
1167			rssi_adjust_val = 22;
1168			break;
1169		case 28:
1170			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x69, 0x25,
1171						      0x3, 0x31, 0x0);
1172			break;
1173		case 29:
1174			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xab, 0x1a,
1175						      0x1a, 0x1, 0x10);
1176			break;
1177		case 30:
1178			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x14,
1179						      0x3, 0x10, 0x50);
1180			break;
1181		case 31:
1182			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3, 0x1a,
1183						      0x1a, 0, 0x58);
1184			break;
1185		case 32:
1186			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0xa,
1187						      0x3, 0x10, 0x0);
1188			break;
1189		case 33:
1190			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xa3, 0x25,
1191						      0x3, 0x30, 0x90);
1192			break;
1193		case 34:
1194			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x53, 0x1a,
1195						      0x1a, 0x0, 0x10);
1196			break;
1197		case 35:
1198			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x63, 0x1a,
1199						      0x1a, 0x0, 0x10);
1200			break;
1201		case 36:
1202			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3, 0x12,
1203						      0x3, 0x14, 0x50);
1204			break;
1205		}
1206	} else {
1207		/* disable PS tdma*/
1208		switch (type) {
1209		case 8: /*PTA Control*/
1210			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x8, 0x0, 0x0,
1211						      0x0, 0x0);
1212			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1213						     false, false);
1214			break;
1215		case 0:
1216		default:  /*Software control, Antenna at BT side*/
1217			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1218						      0x0, 0x0);
1219			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1220						     false, false);
1221			break;
1222		case 9:   /*Software control, Antenna at WiFi side*/
1223			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1224						      0x0, 0x0);
1225			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
1226						     false, false);
1227			break;
1228		case 10:	/* under 5G*/
1229			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1230						      0x8, 0x0);
1231			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1232						     false, false);
1233			break;
1234		}
1235	}
1236	rssi_adjust_val = 0;
1237	btcoexist->btc_set(btcoexist,
1238		 BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1239
1240	/* update pre state*/
1241	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1242	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1243}
1244
1245static bool halbtc8821a1ant_is_common_action(struct btc_coexist *btcoexist)
1246{
1247	bool	common = false, wifi_connected = false, wifi_busy = false;
1248
1249	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1250			   &wifi_connected);
1251	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1252
1253	if (!wifi_connected &&
1254	    BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1255	    coex_dm->bt_status) {
1256		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1257			  "[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1258		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1259
1260		common = true;
1261	} else if (wifi_connected &&
1262		   (BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1263		    coex_dm->bt_status)) {
1264		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1265			  "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1266		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1267
1268		common = true;
1269	} else if (!wifi_connected &&
1270		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1271		    coex_dm->bt_status)) {
1272		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1273			  "[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1274		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1275
1276		common = true;
1277	} else if (wifi_connected &&
1278		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1279		   coex_dm->bt_status)) {
1280		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1281			  "[BTCoex], Wifi connected + BT connected-idle!!\n");
1282		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1283
1284		common = true;
1285	} else if (!wifi_connected &&
1286		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE !=
1287		    coex_dm->bt_status)) {
1288		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1289			  "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1290		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1291
1292		common = true;
1293	} else {
1294		if (wifi_busy) {
1295			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1296				  "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1297		} else {
1298			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1299				  "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1300		}
1301
1302		common = false;
1303	}
1304
1305	return common;
1306}
1307
1308static void btc8821a1ant_tdma_dur_adj(struct btc_coexist *btcoexist,
1309				      u8 wifi_status)
1310{
1311	static long		up, dn, m, n, wait_count;
1312	/*0: no change, +1: increase WiFi duration, -1: decrease WiFi duration*/
1313	long			result;
1314	u8			retry_count = 0, bt_info_ext;
1315
1316	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW,
1317		  "[BTCoex], TdmaDurationAdjustForAcl()\n");
1318
1319	if ((BT_8821A_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN ==
1320	     wifi_status) ||
1321	    (BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN ==
1322	     wifi_status) ||
1323	    (BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT ==
1324	     wifi_status)) {
1325		if (coex_dm->cur_ps_tdma != 1 &&
1326		    coex_dm->cur_ps_tdma != 2 &&
1327		    coex_dm->cur_ps_tdma != 3 &&
1328		    coex_dm->cur_ps_tdma != 9) {
1329			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1330						true, 9);
1331			coex_dm->tdma_adj_type = 9;
1332
1333			up = 0;
1334			dn = 0;
1335			m = 1;
1336			n = 3;
1337			result = 0;
1338			wait_count = 0;
1339		}
1340		return;
1341	}
1342
1343	if (!coex_dm->auto_tdma_adjust) {
1344		coex_dm->auto_tdma_adjust = true;
1345		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
1346			  "[BTCoex], first run TdmaDurationAdjust()!!\n");
1347
1348		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1349		coex_dm->tdma_adj_type = 2;
1350		/*============*/
1351		up = 0;
1352		dn = 0;
1353		m = 1;
1354		n = 3;
1355		result = 0;
1356		wait_count = 0;
1357	} else {
1358		/*accquire the BT TRx retry count from BT_Info byte2*/
1359		retry_count = coex_sta->bt_retry_cnt;
1360		bt_info_ext = coex_sta->bt_info_ext;
1361		result = 0;
1362		wait_count++;
1363
1364		if (retry_count == 0) {
1365			/* no retry in the last 2-second duration*/
1366			up++;
1367			dn--;
1368
1369			if (dn <= 0)
1370				dn = 0;
1371
1372			if (up >= n) {
1373				/* if (retry count == 0) for 2*n seconds ,
1374				 * make WiFi duration wider
1375				 */
1376				wait_count = 0;
1377				n = 3;
1378				up = 0;
1379				dn = 0;
1380				result = 1;
1381				BTC_PRINT(BTC_MSG_ALGORITHM,
1382					  ALGO_TRACE_FW_DETAIL,
1383					  "[BTCoex], Increase wifi duration!!\n");
1384			}
1385		} else if (retry_count <= 3) {
1386			/* <=3 retry in the last 2-second duration*/
1387			up--;
1388			dn++;
1389
1390			if (up <= 0)
1391				up = 0;
1392
1393			if (dn == 2) {
1394				/* if retry count< 3 for 2*2 seconds,
1395				 * shrink wifi duration
1396				 */
1397				if (wait_count <= 2)
1398					m++; /* avoid bounce in two levels */
1399				else
1400					m = 1;
1401
1402				if (m >= 20) {
1403					/* m max value is 20, max time is 120 s,
1404					 *	recheck if adjust WiFi duration.
1405					 */
1406					m = 20;
1407				}
1408				n = 3*m;
1409				up = 0;
1410				dn = 0;
1411				wait_count = 0;
1412				result = -1;
1413				BTC_PRINT(BTC_MSG_ALGORITHM,
1414					  ALGO_TRACE_FW_DETAIL,
1415					  "[BTCoex], Decrease wifi duration for retryCounter<3!!\n");
1416			}
1417		} else {
1418			/* retry count > 3, if retry count > 3 happens once,
1419			 *	shrink WiFi duration
1420			 */
1421			if (wait_count == 1)
1422				m++; /* avoid bounce in two levels */
1423			else
1424				m = 1;
1425		/* m max value is 20, max time is 120 second,
1426		 *	recheck if adjust WiFi duration.
1427		*/
1428			if (m >= 20)
1429				m = 20;
1430
1431			n = 3*m;
1432			up = 0;
1433			dn = 0;
1434			wait_count = 0;
1435			result = -1;
1436			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
1437				  "[BTCoex], Decrease wifi duration for retryCounter>3!!\n");
1438		}
1439
1440		if (result == -1) {
1441			if ((BT_INFO_8821A_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1442			    ((coex_dm->cur_ps_tdma == 1) ||
1443			     (coex_dm->cur_ps_tdma == 2))) {
1444				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1445							true, 9);
1446				coex_dm->tdma_adj_type = 9;
1447			} else if (coex_dm->cur_ps_tdma == 1) {
1448				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1449							true, 2);
1450				coex_dm->tdma_adj_type = 2;
1451			} else if (coex_dm->cur_ps_tdma == 2) {
1452				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1453							true, 9);
1454				coex_dm->tdma_adj_type = 9;
1455			} else if (coex_dm->cur_ps_tdma == 9) {
1456				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1457							true, 11);
1458				coex_dm->tdma_adj_type = 11;
1459			}
1460		} else if (result == 1) {
1461			if ((BT_INFO_8821A_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1462			    ((coex_dm->cur_ps_tdma == 1) ||
1463			     (coex_dm->cur_ps_tdma == 2))) {
1464				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1465							true, 9);
1466				coex_dm->tdma_adj_type = 9;
1467			} else if (coex_dm->cur_ps_tdma == 11) {
1468				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1469							true, 9);
1470				coex_dm->tdma_adj_type = 9;
1471			} else if (coex_dm->cur_ps_tdma == 9) {
1472				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1473							true, 2);
1474				coex_dm->tdma_adj_type = 2;
1475			} else if (coex_dm->cur_ps_tdma == 2) {
1476				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1477							true, 1);
1478				coex_dm->tdma_adj_type = 1;
1479			}
1480		} else {
1481			/*no change*/
1482			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL,
1483				  "[BTCoex], ********** TDMA(on, %d) **********\n",
1484				coex_dm->cur_ps_tdma);
1485		}
1486
1487		if (coex_dm->cur_ps_tdma != 1 &&
1488		    coex_dm->cur_ps_tdma != 2 &&
1489		    coex_dm->cur_ps_tdma != 9 &&
1490		    coex_dm->cur_ps_tdma != 11) {
1491			/* recover to previous adjust type*/
1492			halbtc8821a1ant_ps_tdma(btcoexist,
1493						NORMAL_EXEC, true,
1494						coex_dm->tdma_adj_type);
1495		}
1496	}
1497}
1498
1499static void btc8821a1ant_ps_tdma_check_for_pwr_save(struct btc_coexist *btcoex,
1500						    bool new_ps_state)
1501{
1502	u8	lps_mode = 0x0;
1503
1504	btcoex->btc_get(btcoex, BTC_GET_U1_LPS_MODE, &lps_mode);
1505
1506	if (lps_mode) {
1507		/* already under LPS state*/
1508		if (new_ps_state) {
1509			/* keep state under LPS, do nothing.*/
1510		} else {
1511			/* will leave LPS state, turn off psTdma first*/
1512			halbtc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1513		}
1514	} else {
1515		/* NO PS state*/
1516		if (new_ps_state) {
1517			/* will enter LPS state, turn off psTdma first*/
1518			halbtc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1519		} else {
1520			/* keep state under NO PS state, do nothing.*/
1521		}
1522	}
1523}
1524
1525static void halbtc8821a1ant_power_save_state(struct btc_coexist *btcoexist,
1526					     u8 ps_type, u8 lps_val,
1527					     u8 rpwm_val)
1528{
1529	bool low_pwr_disable = false;
1530
1531	switch (ps_type) {
1532	case BTC_PS_WIFI_NATIVE:
1533		/* recover to original 32k low power setting*/
1534		low_pwr_disable = false;
1535		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1536				   &low_pwr_disable);
1537		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1538		break;
1539	case BTC_PS_LPS_ON:
1540		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist,
1541							true);
1542		halbtc8821a1ant_lps_rpwm(btcoexist,
1543					 NORMAL_EXEC, lps_val, rpwm_val);
1544		/* when coex force to enter LPS, do not enter 32k low power.*/
1545		low_pwr_disable = true;
1546		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1547				   &low_pwr_disable);
1548		/* power save must executed before psTdma.*/
1549		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1550		break;
1551	case BTC_PS_LPS_OFF:
1552		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist, false);
1553		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1554		break;
1555	default:
1556		break;
1557	}
1558}
1559
1560static void halbtc8821a1ant_coex_under_5g(struct btc_coexist *btcoexist)
1561{
1562	halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1563					 0x0, 0x0);
1564	halbtc8821a1ant_ignore_wlan_act(btcoexist, NORMAL_EXEC, true);
1565
1566	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 10);
1567
1568	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1569
1570	halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1571
1572	halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 5);
1573}
1574
1575static void halbtc8821a1ant_action_wifi_only(struct btc_coexist *btcoexist)
1576{
1577	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1578	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
1579}
1580
1581static void btc8821a1ant_mon_bt_en_dis(struct btc_coexist *btcoexist)
1582{
1583	static bool	pre_bt_disabled;
1584	static u32	bt_disable_cnt;
1585	bool		bt_active = true, bt_disabled = false;
1586
1587	/* This function check if bt is disabled*/
1588
1589	if (coex_sta->high_priority_tx == 0 &&
1590	    coex_sta->high_priority_rx == 0 &&
1591	    coex_sta->low_priority_tx == 0 &&
1592	    coex_sta->low_priority_rx == 0) {
1593		bt_active = false;
1594	}
1595	if (coex_sta->high_priority_tx == 0xffff &&
1596	    coex_sta->high_priority_rx == 0xffff &&
1597	    coex_sta->low_priority_tx == 0xffff &&
1598	    coex_sta->low_priority_rx == 0xffff) {
1599		bt_active = false;
1600	}
1601	if (bt_active) {
1602		bt_disable_cnt = 0;
1603		bt_disabled = false;
1604		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1605				   &bt_disabled);
1606		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR,
1607			  "[BTCoex], BT is enabled !!\n");
1608	} else {
1609		bt_disable_cnt++;
1610		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR,
1611			  "[BTCoex], bt all counters = 0, %d times!!\n",
1612			  bt_disable_cnt);
1613		if (bt_disable_cnt >= 2) {
1614			bt_disabled = true;
1615			btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1616					   &bt_disabled);
1617			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR,
1618				  "[BTCoex], BT is disabled !!\n");
1619			halbtc8821a1ant_action_wifi_only(btcoexist);
1620		}
1621	}
1622	if (pre_bt_disabled != bt_disabled) {
1623		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR,
1624			  "[BTCoex], BT is from %s to %s!!\n",
1625			(pre_bt_disabled ? "disabled" : "enabled"),
1626			(bt_disabled ? "disabled" : "enabled"));
1627		pre_bt_disabled = bt_disabled;
1628		if (bt_disabled) {
1629			btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1630					   NULL);
1631			btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1632					   NULL);
1633		}
1634	}
1635}
1636
1637/*=============================================*/
1638/**/
1639/*	Software Coex Mechanism start*/
1640/**/
1641/*=============================================*/
1642
1643/* SCO only or SCO+PAN(HS)*/
1644static void halbtc8821a1ant_action_sco(struct btc_coexist *btcoexist)
1645{
1646	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1647}
1648
1649static void halbtc8821a1ant_action_hid(struct btc_coexist *btcoexist)
1650{
1651	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1652}
1653
1654/*A2DP only / PAN(EDR) only/ A2DP+PAN(HS)*/
1655static void halbtc8821a1ant_action_a2dp(struct btc_coexist *btcoexist)
1656{
1657	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1658}
1659
1660static void halbtc8821a1ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
1661{
1662	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1663}
1664
1665static void halbtc8821a1ant_action_pan_edr(struct btc_coexist *btcoexist)
1666{
1667	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1668}
1669
1670/*PAN(HS) only*/
1671static void halbtc8821a1ant_action_pan_hs(struct btc_coexist *btcoexist)
1672{
1673	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1674}
1675
1676/*PAN(EDR)+A2DP*/
1677static void halbtc8821a1ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
1678{
1679	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1680}
1681
1682static void halbtc8821a1ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
1683{
1684	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1685}
1686
1687/* HID+A2DP+PAN(EDR)*/
1688static void btc8821a1ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
1689{
1690	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1691}
1692
1693static void halbtc8821a1ant_action_hid_a2dp(struct btc_coexist *btcoexist)
1694{
1695	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1696}
1697
1698/*=============================================*/
1699/**/
1700/*	Non-Software Coex Mechanism start*/
1701/**/
1702/*=============================================*/
1703
1704static void halbtc8821a1ant_action_hs(struct btc_coexist *btcoexist)
1705{
1706	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1707	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 2);
1708}
1709
1710static void halbtc8821a1ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1711{
1712	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1713	bool wifi_connected = false;
1714
1715	btcoexist->btc_get(btcoexist,
1716		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
1717
1718	if (!wifi_connected) {
1719		halbtc8821a1ant_power_save_state(btcoexist,
1720						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1721		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1722		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1723	} else if ((bt_link_info->sco_exist) ||
1724		   (bt_link_info->hid_only)) {
1725		/* SCO/HID-only busy*/
1726		halbtc8821a1ant_power_save_state(btcoexist,
1727						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1728		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1729		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1730	} else {
1731		halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON,
1732						 0x50, 0x4);
1733		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 30);
1734		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1735	}
1736}
1737
1738static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
1739						  u8 wifi_status) {
1740	/* tdma and coex table*/
1741	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1742
1743	if (BT_8821A_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN ==
1744	    wifi_status)
1745		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1746	else
1747		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1748}
1749
1750static void btc8821a1ant_act_wifi_con_bt_acl_busy(struct btc_coexist *btcoexist,
1751						  u8 wifi_status)
1752{
1753	u8		bt_rssi_state;
1754
1755	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1756
1757	bt_rssi_state = halbtc8821a1ant_bt_rssi_state(2, 28, 0);
1758
1759	if (bt_link_info->hid_only) {
1760		/*HID*/
1761		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1762						      wifi_status);
1763		coex_dm->auto_tdma_adjust = false;
1764		return;
1765	} else if (bt_link_info->a2dp_only) {
1766		/*A2DP*/
1767		if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1768		    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1769			btc8821a1ant_tdma_dur_adj(btcoexist, wifi_status);
1770		} else {
1771			/*for low BT RSSI*/
1772			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1773						true, 11);
1774			coex_dm->auto_tdma_adjust = false;
1775		}
1776
1777		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1778	} else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1779		/*HID+A2DP*/
1780		if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1781		    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1782			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1783						true, 14);
1784			coex_dm->auto_tdma_adjust = false;
1785		} else {
1786			/*for low BT RSSI*/
1787			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1788						true, 11);
1789			coex_dm->auto_tdma_adjust = false;
1790		}
1791
1792		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1793	} else if ((bt_link_info->pan_only) ||
1794		(bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1795		/*PAN(OPP, FTP), HID+PAN(OPP, FTP)*/
1796		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1797		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1798		coex_dm->auto_tdma_adjust = false;
1799	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1800		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1801		    bt_link_info->pan_exist)) {
1802		/*A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP)*/
1803		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
1804		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1805		coex_dm->auto_tdma_adjust = false;
1806	} else {
1807		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1808		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1809		coex_dm->auto_tdma_adjust = false;
1810	}
1811}
1812
1813static void halbtc8821a1ant_action_wifi_not_connected(
1814	struct btc_coexist *btcoexist)
1815{
1816	/* power save state*/
1817	halbtc8821a1ant_power_save_state(btcoexist,
1818					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1819
1820	/* tdma and coex table*/
1821	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1822	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1823}
1824
1825static void btc8821a1ant_act_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1826{
1827	halbtc8821a1ant_power_save_state(btcoexist,
1828					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1829
1830	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1831	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1832}
1833
1834static void halbtc8821a1ant_action_wifi_connected_scan(
1835	struct btc_coexist *btcoexist) {
1836	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1837
1838	/* power save state*/
1839	halbtc8821a1ant_power_save_state(btcoexist,
1840					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1841
1842	/* tdma and coex table*/
1843	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1844		if (bt_link_info->a2dp_exist && bt_link_info->pan_exist) {
1845			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1846						true, 22);
1847			halbtc8821a1ant_coex_table_with_type(btcoexist,
1848							     NORMAL_EXEC, 1);
1849		} else {
1850		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1851		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1852	}
1853	} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1854		    coex_dm->bt_status) ||
1855		   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1856		    coex_dm->bt_status)) {
1857		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1858			BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1859	} else {
1860		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1861		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1862	}
1863}
1864
1865static void btc8821a1ant_act_wifi_conn_sp_pkt(struct btc_coexist *btcoexist)
1866{
1867	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1868	bool	hs_connecting = false;
1869
1870	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_CONNECTING, &hs_connecting);
1871
1872	halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1873					 0x0, 0x0);
1874
1875	/* tdma and coex table*/
1876	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1877		if (bt_link_info->a2dp_exist && bt_link_info->pan_exist) {
1878			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1879						true, 22);
1880			halbtc8821a1ant_coex_table_with_type(btcoexist,
1881							     NORMAL_EXEC, 1);
1882		} else {
1883			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1884						true, 20);
1885			halbtc8821a1ant_coex_table_with_type(btcoexist,
1886							     NORMAL_EXEC, 1);
1887		}
1888	} else {
1889		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1890		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1891	}
1892}
1893
1894static void halbtc8821a1ant_action_wifi_connected(struct btc_coexist *btcoexist)
1895{
1896	bool	wifi_busy = false;
1897	bool	scan = false, link = false, roam = false;
1898	bool	under_4way = false;
1899
1900	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1901		  "[BTCoex], CoexForWifiConnect()===>\n");
1902
1903	btcoexist->btc_get(btcoexist,
1904		 BTC_GET_BL_WIFI_4_WAY_PROGRESS, &under_4way);
1905	if (under_4way) {
1906		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1907		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1908			  "[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1909		return;
1910	}
1911
1912	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1913	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1914	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1915	if (scan || link || roam) {
1916		halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
1917		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1918			  "[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1919		return;
1920	}
1921
1922	/* power save state*/
1923	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY ==
1924			coex_dm->bt_status && !btcoexist->bt_link_info.hid_only)
1925		halbtc8821a1ant_power_save_state(btcoexist,
1926						 BTC_PS_LPS_ON, 0x50, 0x4);
1927	else
1928		halbtc8821a1ant_power_save_state(btcoexist,
1929						 BTC_PS_WIFI_NATIVE,
1930						 0x0, 0x0);
1931
1932	/* tdma and coex table*/
1933	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1934	if (!wifi_busy) {
1935		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1936			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1937				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1938		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1939			    coex_dm->bt_status) ||
1940			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1941			    coex_dm->bt_status)) {
1942			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1943				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1944		} else {
1945			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1946						true, 5);
1947			halbtc8821a1ant_coex_table_with_type(btcoexist,
1948							     NORMAL_EXEC, 2);
1949		}
1950	} else {
1951		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1952			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1953				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1954		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1955			    coex_dm->bt_status) ||
1956			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1957			    coex_dm->bt_status)) {
1958			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1959				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1960		} else {
1961			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1962						true, 5);
1963			halbtc8821a1ant_coex_table_with_type(btcoexist,
1964							     NORMAL_EXEC, 2);
1965		}
1966	}
1967}
1968
1969static void btc8821a1ant_run_sw_coex_mech(struct btc_coexist *btcoexist)
1970{
1971	u8	algorithm = 0;
1972
1973	algorithm = halbtc8821a1ant_action_algorithm(btcoexist);
1974	coex_dm->cur_algorithm = algorithm;
1975
1976	if (!halbtc8821a1ant_is_common_action(btcoexist)) {
1977		switch (coex_dm->cur_algorithm) {
1978		case BT_8821A_1ANT_COEX_ALGO_SCO:
1979			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1980				  "[BTCoex], Action algorithm = SCO.\n");
1981			halbtc8821a1ant_action_sco(btcoexist);
1982			break;
1983		case BT_8821A_1ANT_COEX_ALGO_HID:
1984			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1985				  "[BTCoex], Action algorithm = HID.\n");
1986			halbtc8821a1ant_action_hid(btcoexist);
1987			break;
1988		case BT_8821A_1ANT_COEX_ALGO_A2DP:
1989			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1990				  "[BTCoex], Action algorithm = A2DP.\n");
1991			halbtc8821a1ant_action_a2dp(btcoexist);
1992			break;
1993		case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
1994			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
1995				  "[BTCoex], Action algorithm = A2DP+PAN(HS).\n");
1996			halbtc8821a1ant_action_a2dp_pan_hs(btcoexist);
1997			break;
1998		case BT_8821A_1ANT_COEX_ALGO_PANEDR:
1999			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2000				  "[BTCoex], Action algorithm = PAN(EDR).\n");
2001			halbtc8821a1ant_action_pan_edr(btcoexist);
2002			break;
2003		case BT_8821A_1ANT_COEX_ALGO_PANHS:
2004			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2005				  "[BTCoex], Action algorithm = HS mode.\n");
2006			halbtc8821a1ant_action_pan_hs(btcoexist);
2007			break;
2008		case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
2009			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2010				  "[BTCoex], Action algorithm = PAN+A2DP.\n");
2011			halbtc8821a1ant_action_pan_edr_a2dp(btcoexist);
2012			break;
2013		case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
2014			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2015				  "[BTCoex], Action algorithm = PAN(EDR)+HID.\n");
2016			halbtc8821a1ant_action_pan_edr_hid(btcoexist);
2017			break;
2018		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2019			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2020				  "[BTCoex], Action algorithm = HID+A2DP+PAN.\n");
2021			btc8821a1ant_action_hid_a2dp_pan_edr(btcoexist);
2022			break;
2023		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
2024			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2025				  "[BTCoex], Action algorithm = HID+A2DP.\n");
2026			halbtc8821a1ant_action_hid_a2dp(btcoexist);
2027			break;
2028		default:
2029			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2030				  "[BTCoex], Action algorithm = coexist All Off!!\n");
2031			/*halbtc8821a1ant_coex_all_off(btcoexist);*/
2032			break;
2033		}
2034		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
2035	}
2036}
2037
2038static void halbtc8821a1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
2039{
2040	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2041	bool	wifi_connected = false, bt_hs_on = false;
2042	bool	increase_scan_dev_num = false;
2043	bool	bt_ctrl_agg_buf_size = false;
2044	u8	agg_buf_size = 5;
2045	u8	wifi_rssi_state = BTC_RSSI_STATE_HIGH;
2046	bool	wifi_under_5g = false;
2047
2048	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2049		  "[BTCoex], RunCoexistMechanism()===>\n");
2050
2051	if (btcoexist->manual_control) {
2052		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2053			  "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2054		return;
2055	}
2056
2057	if (btcoexist->stop_coex_dm) {
2058		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2059			  "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2060		return;
2061	}
2062
2063	if (coex_sta->under_ips) {
2064		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2065			  "[BTCoex], wifi is under IPS !!!\n");
2066		return;
2067	}
2068
2069	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2070	if (wifi_under_5g) {
2071		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2072			  "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2073		halbtc8821a1ant_coex_under_5g(btcoexist);
2074		return;
2075	}
2076
2077	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2078	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2079	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2080		increase_scan_dev_num = true;
2081
2082	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2083			   &increase_scan_dev_num);
2084
2085	btcoexist->btc_get(btcoexist,
2086		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2087
2088	if (!bt_link_info->sco_exist && !bt_link_info->hid_exist) {
2089		halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2090	} else {
2091		if (wifi_connected) {
2092			wifi_rssi_state =
2093				 halbtc8821a1ant_WifiRssiState(btcoexist, 1, 2,
2094							       30, 0);
2095			if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2096			    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2097				halbtc8821a1ant_limited_tx(btcoexist,
2098							   NORMAL_EXEC, 1, 1,
2099							   1, 1);
2100			} else {
2101				halbtc8821a1ant_limited_tx(btcoexist,
2102							   NORMAL_EXEC, 1, 1,
2103							   1, 1);
2104			}
2105		} else {
2106			halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC,
2107						   0, 0, 0, 0);
2108		}
2109	}
2110
2111	if (bt_link_info->sco_exist) {
2112		bt_ctrl_agg_buf_size = true;
2113		agg_buf_size = 0x3;
2114	} else if (bt_link_info->hid_exist) {
2115		bt_ctrl_agg_buf_size = true;
2116		agg_buf_size = 0x5;
2117	} else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
2118		bt_ctrl_agg_buf_size = true;
2119		agg_buf_size = 0x8;
2120	}
2121	halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2122				   bt_ctrl_agg_buf_size, agg_buf_size);
2123
2124	btc8821a1ant_run_sw_coex_mech(btcoexist);
2125
2126	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2127	if (coex_sta->c2h_bt_inquiry_page) {
2128		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2129		return;
2130	} else if (bt_hs_on) {
2131		halbtc8821a1ant_action_hs(btcoexist);
2132		return;
2133	}
2134
2135	if (!wifi_connected) {
2136		bool	scan = false, link = false, roam = false;
2137
2138		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2139			  "[BTCoex], wifi is non connected-idle !!!\n");
2140
2141		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2142		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2143		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2144
2145		if (scan || link || roam)
2146			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2147		else
2148			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2149	} else {
2150		/* wifi LPS/Busy*/
2151		halbtc8821a1ant_action_wifi_connected(btcoexist);
2152	}
2153}
2154
2155static void halbtc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2156{
2157	/* force to reset coex mechanism*/
2158	/* sw all off*/
2159	halbtc8821a1ant_sw_mechanism(btcoexist, false);
2160
2161	halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2162	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2163}
2164
2165static void halbtc8821a1ant_init_hw_config(struct btc_coexist *btcoexist,
2166					   bool back_up)
2167{
2168	u8	u1_tmp = 0;
2169	bool	wifi_under_5g = false;
2170
2171	BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2172		  "[BTCoex], 1Ant Init HW Config!!\n");
2173
2174	if (back_up) {
2175		coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2176								      0x430);
2177		coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2178								      0x434);
2179		coex_dm->backup_retry_limit =
2180			 btcoexist->btc_read_2byte(btcoexist, 0x42a);
2181		coex_dm->backup_ampdu_max_time =
2182			 btcoexist->btc_read_1byte(btcoexist, 0x456);
2183	}
2184
2185	/* 0x790[5:0] = 0x5*/
2186	u1_tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2187	u1_tmp &= 0xc0;
2188	u1_tmp |= 0x5;
2189	btcoexist->btc_write_1byte(btcoexist, 0x790, u1_tmp);
2190
2191	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2192
2193	/*Antenna config*/
2194	if (wifi_under_5g)
2195		halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2196					     true, false);
2197	else
2198		halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2199					     true, false);
2200	/* PTA parameter*/
2201	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2202
2203	/* Enable counter statistics*/
2204	/*0x76e[3] =1, WLAN_Act control by PTA*/
2205	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
2206	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2207	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2208}
2209
2210/*============================================================*/
2211/* work around function start with wa_halbtc8821a1ant_*/
2212/*============================================================*/
2213/*============================================================*/
2214/* extern function start with EXhalbtc8821a1ant_*/
2215/*============================================================*/
2216void ex_halbtc8821a1ant_init_hwconfig(struct btc_coexist *btcoexist)
2217{
2218	halbtc8821a1ant_init_hw_config(btcoexist, true);
2219}
2220
2221void ex_halbtc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2222{
2223	BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2224		  "[BTCoex], Coex Mechanism Init!!\n");
2225
2226	btcoexist->stop_coex_dm = false;
2227
2228	halbtc8821a1ant_init_coex_dm(btcoexist);
2229
2230	halbtc8821a1ant_query_bt_info(btcoexist);
2231}
2232
2233void ex_halbtc8821a1ant_display_coex_info(struct btc_coexist *btcoexist)
2234{
2235	struct btc_board_info *board_info = &btcoexist->board_info;
2236	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2237	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2238	struct rtl_priv *rtlpriv = btcoexist->adapter;
2239	u8 u1_tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2240	u16 u2_tmp[4];
2241	u32 u4_tmp[4];
2242	bool roam = false, scan = false, link = false, wifi_under_5g = false;
2243	bool bt_hs_on = false, wifi_busy = false;
2244	long wifi_rssi = 0, bt_hs_rssi = 0;
2245	u32 wifi_bw, wifi_traffic_dir;
2246	u8 wifi_dot11_chnl, wifi_hs_chnl;
2247	u32 fw_ver = 0, bt_patch_ver = 0;
2248
2249	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2250		   "\r\n ============[BT Coexist info]============");
2251
2252	if (btcoexist->manual_control) {
2253		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2254			   "\r\n ============[Under Manual Control]============");
2255		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2256			   "\r\n ==========================================");
2257	}
2258	if (btcoexist->stop_coex_dm) {
2259		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2260			   "\r\n ============[Coex is STOPPED]============");
2261		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2262			   "\r\n ==========================================");
2263	}
2264
2265	if (!board_info->bt_exist) {
2266		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n BT not exists !!!");
2267		return;
2268	}
2269
2270	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2271		   "\r\n %-35s = %d/ %d/ %d",
2272		   "Ant PG Num/ Ant Mech/ Ant Pos:",
2273		   board_info->pg_ant_num,
2274		   board_info->btdm_ant_num,
2275		   board_info->btdm_ant_pos);
2276
2277	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2278		   "\r\n %-35s = %s / %d", "BT stack/ hci ext ver",
2279		   ((stack_info->profile_notified) ? "Yes" : "No"),
2280		stack_info->hci_version);
2281
2282	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2283			   &bt_patch_ver);
2284	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2285	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2286		   "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2287		   "CoexVer/ FwVer/ PatchVer",
2288		   glcoex_ver_date_8821a_1ant,
2289		   glcoex_ver_8821a_1ant,
2290		   fw_ver, bt_patch_ver,
2291		   bt_patch_ver);
2292
2293	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION,
2294			   &bt_hs_on);
2295	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2296			   &wifi_dot11_chnl);
2297	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL,
2298			   &wifi_hs_chnl);
2299	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2300		   "\r\n %-35s = %d / %d(%d)",
2301		   "Dot11 channel / HsChnl(HsMode)",
2302		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2303
2304	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2305		   "\r\n %-35s = %3ph ",
2306		   "H2C Wifi inform bt chnl Info",
2307		   coex_dm->wifi_chnl_info);
2308
2309	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2310	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2311	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2312		   "\r\n %-35s = %d/ %d", "Wifi rssi/ HS rssi",
2313		   (int)wifi_rssi, (int)bt_hs_rssi);
2314
2315	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2316	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2317	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2318	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2319		   "\r\n %-35s = %d/ %d/ %d ", "Wifi link/ roam/ scan",
2320		   link, roam, scan);
2321
2322	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2323			   &wifi_under_5g);
2324	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2325			   &wifi_bw);
2326	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
2327			   &wifi_busy);
2328	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2329			   &wifi_traffic_dir);
2330	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2331		   "\r\n %-35s = %s / %s/ %s ", "Wifi status",
2332		   (wifi_under_5g ? "5G" : "2.4G"),
2333		   ((BTC_WIFI_BW_LEGACY == wifi_bw) ? "Legacy" :
2334		   (((BTC_WIFI_BW_HT40 == wifi_bw) ? "HT40" : "HT20"))),
2335		   ((!wifi_busy) ? "idle" :
2336		   ((BTC_WIFI_TRAFFIC_TX == wifi_traffic_dir) ?
2337		   "uplink" : "downlink")));
2338	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2339		   "\r\n %-35s = [%s/ %d/ %d] ", "BT [status/ rssi/ retryCnt]",
2340		   ((btcoexist->bt_info.bt_disabled) ? ("disabled") :
2341		   ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2342		   ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2343		     coex_dm->bt_status) ?
2344		   "non-connected idle" :
2345		   ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
2346		     coex_dm->bt_status) ?
2347		   "connected-idle" : "busy")))),
2348		   coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2349
2350	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2351		   "\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
2352		   bt_link_info->sco_exist,
2353		   bt_link_info->hid_exist,
2354		   bt_link_info->pan_exist,
2355		   bt_link_info->a2dp_exist);
2356	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
2357
2358	bt_info_ext = coex_sta->bt_info_ext;
2359	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2360		   "\r\n %-35s = %s",
2361		   "BT Info A2DP rate",
2362		   (bt_info_ext&BIT0) ?
2363		   "Basic rate" : "EDR rate");
2364
2365	for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2366		if (coex_sta->bt_info_c2h_cnt[i]) {
2367			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2368				   "\r\n %-35s = %7ph(%d)",
2369				   glbt_info_src_8821a_1ant[i],
2370				   coex_sta->bt_info_c2h[i],
2371				   coex_sta->bt_info_c2h_cnt[i]);
2372		}
2373	}
2374	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2375		   "\r\n %-35s = %s/%s, (0x%x/0x%x)",
2376		   "PS state, IPS/LPS, (lps/rpwm)",
2377		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2378		   ((coex_sta->under_Lps ? "LPS ON" : "LPS OFF")),
2379		   btcoexist->bt_info.lps_val,
2380		   btcoexist->bt_info.rpwm_val);
2381	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
2382
2383	if (!btcoexist->manual_control) {
2384		/* Sw mechanism*/
2385		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2386			   "\r\n %-35s", "============[Sw mechanism]============");
2387
2388		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2389			   "\r\n %-35s = %d", "SM[LowPenaltyRA]",
2390			   coex_dm->cur_low_penalty_ra);
2391
2392		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2393			   "\r\n %-35s = %s/ %s/ %d ",
2394			   "DelBA/ BtCtrlAgg/ AggSize",
2395			   (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2396			   (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2397			   btcoexist->bt_info.agg_buf_size);
2398		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2399			   "\r\n %-35s = 0x%x ", "Rate Mask",
2400			   btcoexist->bt_info.ra_mask);
2401
2402		/* Fw mechanism*/
2403		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
2404			   "============[Fw mechanism]============");
2405
2406		ps_tdma_case = coex_dm->cur_ps_tdma;
2407		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2408			   "\r\n %-35s = %5ph case-%d (auto:%d)",
2409			   "PS TDMA",
2410			   coex_dm->ps_tdma_para,
2411			   ps_tdma_case,
2412			   coex_dm->auto_tdma_adjust);
2413
2414		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2415			   "\r\n %-35s = 0x%x ",
2416			   "Latest error condition(should be 0)",
2417			   coex_dm->error_condition);
2418
2419		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2420			   "\r\n %-35s = %d ", "IgnWlanAct",
2421			   coex_dm->cur_ignore_wlan_act);
2422	}
2423
2424	/* Hw setting*/
2425	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2426		   "\r\n %-35s", "============[Hw setting]============");
2427
2428	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2429		   "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2430		   "backup ARFR1/ARFR2/RL/AMaxTime",
2431		   coex_dm->backup_arfr_cnt1,
2432		   coex_dm->backup_arfr_cnt2,
2433		   coex_dm->backup_retry_limit,
2434		   coex_dm->backup_ampdu_max_time);
2435
2436	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2437	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2438	u2_tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2439	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2440	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2441		   "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2442		   "0x430/0x434/0x42a/0x456",
2443		   u4_tmp[0], u4_tmp[1], u2_tmp[0], u1_tmp[0]);
2444
2445	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2446	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2447	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2448		   "\r\n %-35s = 0x%x/ 0x%x", "0x778/ 0xc58[29:25]",
2449		   u1_tmp[0], (u4_tmp[0]&0x3e000000) >> 25);
2450
2451	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2452	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2453		   "\r\n %-35s = 0x%x", "0x8db[6:5]",
2454		   ((u1_tmp[0]&0x60)>>5));
2455
2456	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2457	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2458	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2459		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2460		   "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2461		   (u4_tmp[0] & 0x30000000)>>28,
2462		    u4_tmp[0] & 0xff,
2463		    u1_tmp[0] & 0x3);
2464
2465	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2466	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2467	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2468	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2469		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2470		   "0x40/0x4c[24:23]/0x64[0]",
2471		   u1_tmp[0], ((u4_tmp[0]&0x01800000)>>23), u1_tmp[1]&0x1);
2472
2473	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2474	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2475	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2476		   "\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522",
2477		   u4_tmp[0], u1_tmp[0]);
2478
2479	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2480	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2481		   "\r\n %-35s = 0x%x", "0xc50(dig)",
2482		   u4_tmp[0]&0xff);
2483
2484	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xf48);
2485	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5d);
2486	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2487	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2488		   "\r\n %-35s = 0x%x/ 0x%x", "OFDM-FA/ CCK-FA",
2489		   u4_tmp[0], (u1_tmp[0]<<8) + u1_tmp[1]);
2490
2491	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2492	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2493	u4_tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2494	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2495	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2496		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2497		   "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2498		   u4_tmp[0], u4_tmp[1], u4_tmp[2], u1_tmp[0]);
2499
2500	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2501		   "\r\n %-35s = %d/ %d", "0x770(high-pri rx/tx)",
2502		   coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2503	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2504		   "\r\n %-35s = %d/ %d", "0x774(low-pri rx/tx)",
2505		   coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2506#if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 1)
2507	halbtc8821a1ant_monitor_bt_ctr(btcoexist);
2508#endif
2509	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
2510}
2511
2512void ex_halbtc8821a1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2513{
2514	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2515		return;
2516
2517	if (BTC_IPS_ENTER == type) {
2518		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2519			  "[BTCoex], IPS ENTER notify\n");
2520		coex_sta->under_ips = true;
2521		halbtc8821a1ant_set_ant_path(btcoexist,
2522					     BTC_ANT_PATH_BT, false, true);
2523		/*set PTA control*/
2524		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2525		halbtc8821a1ant_coex_table_with_type(btcoexist,
2526						     NORMAL_EXEC, 0);
2527	} else if (BTC_IPS_LEAVE == type) {
2528		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2529			  "[BTCoex], IPS LEAVE notify\n");
2530		coex_sta->under_ips = false;
2531
2532		halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2533	}
2534}
2535
2536void ex_halbtc8821a1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2537{
2538	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2539		return;
2540
2541	if (BTC_LPS_ENABLE == type) {
2542		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2543			  "[BTCoex], LPS ENABLE notify\n");
2544		coex_sta->under_Lps = true;
2545	} else if (BTC_LPS_DISABLE == type) {
2546		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2547			  "[BTCoex], LPS DISABLE notify\n");
2548		coex_sta->under_Lps = false;
2549	}
2550}
2551
2552void ex_halbtc8821a1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2553{
2554	bool wifi_connected = false, bt_hs_on = false;
2555
2556	if (btcoexist->manual_control ||
2557	    btcoexist->stop_coex_dm ||
2558	    btcoexist->bt_info.bt_disabled)
2559		return;
2560
2561	btcoexist->btc_get(btcoexist,
2562		 BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2563	btcoexist->btc_get(btcoexist,
2564		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2565
2566	halbtc8821a1ant_query_bt_info(btcoexist);
2567
2568	if (coex_sta->c2h_bt_inquiry_page) {
2569		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2570		return;
2571	} else if (bt_hs_on) {
2572		halbtc8821a1ant_action_hs(btcoexist);
2573		return;
2574	}
2575
2576	if (BTC_SCAN_START == type) {
2577		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2578			  "[BTCoex], SCAN START notify\n");
2579		if (!wifi_connected) {
2580			/* non-connected scan*/
2581			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2582		} else {
2583			/* wifi is connected*/
2584			halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
2585		}
2586	} else if (BTC_SCAN_FINISH == type) {
2587		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2588			  "[BTCoex], SCAN FINISH notify\n");
2589		if (!wifi_connected) {
2590			/* non-connected scan*/
2591			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2592		} else {
2593			halbtc8821a1ant_action_wifi_connected(btcoexist);
2594		}
2595	}
2596}
2597
2598void ex_halbtc8821a1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2599{
2600	bool	wifi_connected = false, bt_hs_on = false;
2601
2602	if (btcoexist->manual_control ||
2603	    btcoexist->stop_coex_dm ||
2604	    btcoexist->bt_info.bt_disabled)
2605		return;
2606
2607	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2608	if (coex_sta->c2h_bt_inquiry_page) {
2609		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2610		return;
2611	} else if (bt_hs_on) {
2612		halbtc8821a1ant_action_hs(btcoexist);
2613		return;
2614	}
2615
2616	if (BTC_ASSOCIATE_START == type) {
2617		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2618			  "[BTCoex], CONNECT START notify\n");
2619		btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2620	} else if (BTC_ASSOCIATE_FINISH == type) {
2621		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2622			  "[BTCoex], CONNECT FINISH notify\n");
2623
2624		btcoexist->btc_get(btcoexist,
2625			 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2626		if (!wifi_connected) {
2627			/* non-connected scan*/
2628			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2629		} else {
2630			halbtc8821a1ant_action_wifi_connected(btcoexist);
2631		}
2632	}
2633}
2634
2635void ex_halbtc8821a1ant_media_status_notify(struct btc_coexist *btcoexist,
2636					    u8 type)
2637{
2638	u8 h2c_parameter[3] = {0};
2639	u32 wifi_bw;
2640	u8 wifi_central_chnl;
2641
2642	if (btcoexist->manual_control ||
2643	    btcoexist->stop_coex_dm ||
2644	    btcoexist->bt_info.bt_disabled)
2645		return;
2646
2647	if (BTC_MEDIA_CONNECT == type) {
2648		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2649			  "[BTCoex], MEDIA connect notify\n");
2650	} else {
2651		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2652			  "[BTCoex], MEDIA disconnect notify\n");
2653	}
2654
2655	/* only 2.4G we need to inform bt the chnl mask*/
2656	btcoexist->btc_get(btcoexist,
2657			   BTC_GET_U1_WIFI_CENTRAL_CHNL,
2658			   &wifi_central_chnl);
2659	if ((BTC_MEDIA_CONNECT == type) &&
2660	    (wifi_central_chnl <= 14)) {
2661		/*h2c_parameter[0] = 0x1;*/
2662		h2c_parameter[0] = 0x0;
2663		h2c_parameter[1] = wifi_central_chnl;
2664		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2665		if (BTC_WIFI_BW_HT40 == wifi_bw)
2666			h2c_parameter[2] = 0x30;
2667		else
2668			h2c_parameter[2] = 0x20;
2669	}
2670
2671	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2672	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2673	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2674
2675	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC,
2676		  "[BTCoex], FW write 0x66 = 0x%x\n",
2677		  h2c_parameter[0]<<16|h2c_parameter[1]<<8|h2c_parameter[2]);
2678
2679	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2680}
2681
2682void ex_halbtc8821a1ant_special_packet_notify(struct btc_coexist *btcoexist,
2683					      u8 type)
2684{
2685	bool bt_hs_on = false;
2686
2687	if (btcoexist->manual_control ||
2688	    btcoexist->stop_coex_dm ||
2689	    btcoexist->bt_info.bt_disabled)
2690		return;
2691
2692	coex_sta->special_pkt_period_cnt = 0;
2693
2694	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2695	if (coex_sta->c2h_bt_inquiry_page) {
2696		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2697		return;
2698	} else if (bt_hs_on) {
2699		halbtc8821a1ant_action_hs(btcoexist);
2700		return;
2701	}
2702
2703	if (BTC_PACKET_DHCP == type ||
2704	    BTC_PACKET_EAPOL == type) {
2705		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2706			  "[BTCoex], special Packet(%d) notify\n", type);
2707		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
2708	}
2709}
2710
2711void ex_halbtc8821a1ant_bt_info_notify(struct btc_coexist *btcoexist,
2712				       u8 *tmp_buf, u8 length)
2713{
2714	u8 bt_info = 0;
2715	u8 i, rsp_source = 0;
2716	bool wifi_connected = false;
2717	bool bt_busy = false;
2718	bool wifi_under_5g = false;
2719
2720	coex_sta->c2h_bt_info_req_sent = false;
2721
2722	btcoexist->btc_get(btcoexist,
2723		 BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2724
2725	rsp_source = tmp_buf[0]&0xf;
2726	if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2727		rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2728	coex_sta->bt_info_c2h_cnt[rsp_source]++;
2729
2730	BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2731		  "[BTCoex], Bt info[%d], length = %d, hex data = [",
2732		  rsp_source, length);
2733	for (i = 0; i < length; i++) {
2734		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2735		if (i == 1)
2736			bt_info = tmp_buf[i];
2737		if (i == length-1) {
2738			BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2739				  "0x%02x]\n", tmp_buf[i]);
2740		} else {
2741			BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2742				  "0x%02x, ", tmp_buf[i]);
2743		}
2744	}
2745
2746	if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
2747		coex_sta->bt_retry_cnt =	/* [3:0]*/
2748			coex_sta->bt_info_c2h[rsp_source][2]&0xf;
2749
2750		coex_sta->bt_rssi =
2751			coex_sta->bt_info_c2h[rsp_source][3]*2+10;
2752
2753		coex_sta->bt_info_ext =
2754			coex_sta->bt_info_c2h[rsp_source][4];
2755
2756		/* Here we need to resend some wifi info to BT*/
2757		/* because bt is reset and loss of the info.*/
2758		if (coex_sta->bt_info_ext & BIT1) {
2759			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2760				  "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
2761			btcoexist->btc_get(btcoexist,
2762					   BTC_GET_BL_WIFI_CONNECTED,
2763					   &wifi_connected);
2764			if (wifi_connected) {
2765				ex_halbtc8821a1ant_media_status_notify(btcoexist,
2766							       BTC_MEDIA_CONNECT);
2767			} else {
2768				ex_halbtc8821a1ant_media_status_notify(btcoexist,
2769							       BTC_MEDIA_DISCONNECT);
2770			}
2771		}
2772
2773		if ((coex_sta->bt_info_ext & BIT3) && !wifi_under_5g) {
2774			if (!btcoexist->manual_control &&
2775			    !btcoexist->stop_coex_dm) {
2776				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2777					  "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
2778				halbtc8821a1ant_ignore_wlan_act(btcoexist,
2779								FORCE_EXEC,
2780								false);
2781			}
2782		}
2783#if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
2784		if (!(coex_sta->bt_info_ext & BIT4)) {
2785			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2786				  "[BTCoex], BT ext info bit4 check, set BT to enable Auto Report!!\n");
2787			halbtc8821a1ant_bt_auto_report(btcoexist,
2788						       FORCE_EXEC, true);
2789		}
2790#endif
2791	}
2792
2793	/* check BIT2 first ==> check if bt is under inquiry or page scan*/
2794	if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
2795		coex_sta->c2h_bt_inquiry_page = true;
2796	else
2797		coex_sta->c2h_bt_inquiry_page = false;
2798
2799	/* set link exist status*/
2800	if (!(bt_info&BT_INFO_8821A_1ANT_B_CONNECTION)) {
2801		coex_sta->bt_link_exist = false;
2802		coex_sta->pan_exist = false;
2803		coex_sta->a2dp_exist = false;
2804		coex_sta->hid_exist = false;
2805		coex_sta->sco_exist = false;
2806	} else {
2807		/* connection exists*/
2808		coex_sta->bt_link_exist = true;
2809		if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
2810			coex_sta->pan_exist = true;
2811		else
2812			coex_sta->pan_exist = false;
2813		if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
2814			coex_sta->a2dp_exist = true;
2815		else
2816			coex_sta->a2dp_exist = false;
2817		if (bt_info & BT_INFO_8821A_1ANT_B_HID)
2818			coex_sta->hid_exist = true;
2819		else
2820			coex_sta->hid_exist = false;
2821		if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
2822			coex_sta->sco_exist = true;
2823		else
2824			coex_sta->sco_exist = false;
2825	}
2826
2827	halbtc8821a1ant_update_bt_link_info(btcoexist);
2828
2829	if (!(bt_info&BT_INFO_8821A_1ANT_B_CONNECTION)) {
2830		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
2831		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2832			  "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
2833	} else if (bt_info == BT_INFO_8821A_1ANT_B_CONNECTION) {
2834		/* connection exists but no busy*/
2835		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
2836		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2837			  "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
2838	} else if ((bt_info&BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
2839		(bt_info&BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
2840		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
2841		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2842			  "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
2843	} else if (bt_info&BT_INFO_8821A_1ANT_B_ACL_BUSY) {
2844		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
2845			coex_dm->auto_tdma_adjust = false;
2846		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
2847		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2848			  "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
2849	} else {
2850		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
2851		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2852			  "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
2853	}
2854
2855	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2856	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2857	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2858		bt_busy = true;
2859	else
2860		bt_busy = false;
2861	btcoexist->btc_set(btcoexist,
2862			   BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2863
2864	halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2865}
2866
2867void ex_halbtc8821a1ant_halt_notify(struct btc_coexist *btcoexist)
2868{
2869	BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2870		  "[BTCoex], Halt notify\n");
2871
2872	btcoexist->stop_coex_dm = true;
2873
2874	halbtc8821a1ant_set_ant_path(btcoexist,
2875				     BTC_ANT_PATH_BT, false, true);
2876	halbtc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2877
2878	halbtc8821a1ant_power_save_state(btcoexist,
2879					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2880	halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
2881
2882	ex_halbtc8821a1ant_media_status_notify(btcoexist,
2883					       BTC_MEDIA_DISCONNECT);
2884}
2885
2886void ex_halbtc8821a1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
2887{
2888	BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2889		  "[BTCoex], Pnp notify\n");
2890
2891	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
2892		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2893			  "[BTCoex], Pnp notify to SLEEP\n");
2894		btcoexist->stop_coex_dm = true;
2895		halbtc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2896		halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2897						 0x0, 0x0);
2898		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
2899	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
2900		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY,
2901			  "[BTCoex], Pnp notify to WAKE UP\n");
2902		btcoexist->stop_coex_dm = false;
2903		halbtc8821a1ant_init_hw_config(btcoexist, false);
2904		halbtc8821a1ant_init_coex_dm(btcoexist);
2905		halbtc8821a1ant_query_bt_info(btcoexist);
2906	}
2907}
2908
2909void
2910ex_halbtc8821a1ant_periodical(
2911	struct btc_coexist *btcoexist) {
2912	static u8	dis_ver_info_cnt;
2913	u32		fw_ver = 0, bt_patch_ver = 0;
2914	struct btc_board_info *board_info = &btcoexist->board_info;
2915	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2916
2917	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE,
2918		  "[BTCoex], ==========================Periodical===========================\n");
2919
2920	if (dis_ver_info_cnt <= 5) {
2921		dis_ver_info_cnt += 1;
2922		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2923			  "[BTCoex], ****************************************************************\n");
2924		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2925			  "[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
2926			  board_info->pg_ant_num,
2927			  board_info->btdm_ant_num,
2928			  board_info->btdm_ant_pos);
2929		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2930			  "[BTCoex], BT stack/ hci ext ver = %s / %d\n",
2931			  ((stack_info->profile_notified) ? "Yes" : "No"),
2932			  stack_info->hci_version);
2933		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2934				   &bt_patch_ver);
2935		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2936		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2937			  "[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
2938			glcoex_ver_date_8821a_1ant,
2939			glcoex_ver_8821a_1ant,
2940			fw_ver, bt_patch_ver,
2941			bt_patch_ver);
2942		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT,
2943			  "[BTCoex], ****************************************************************\n");
2944	}
2945
2946#if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
2947	halbtc8821a1ant_query_bt_info(btcoexist);
2948	halbtc8821a1ant_monitor_bt_ctr(btcoexist);
2949	btc8821a1ant_mon_bt_en_dis(btcoexist);
2950#else
2951	if (halbtc8821a1ant_Is_wifi_status_changed(btcoexist) ||
2952	    coex_dm->auto_tdma_adjust) {
2953		if (coex_sta->special_pkt_period_cnt > 2)
2954			halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2955	}
2956
2957	coex_sta->special_pkt_period_cnt++;
2958#endif
2959}