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