xref: /linux/drivers/net/wireless/realtek/rtlwifi/btcoexist/halbtc8821a1ant.c (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
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 	btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1588 }
1589 
1590 static void btc8821a1ant_act_wifi_con_bt_acl_busy(struct btc_coexist *btcoexist,
1591 						  u8 wifi_status)
1592 {
1593 	u8 bt_rssi_state;
1594 
1595 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1596 
1597 	bt_rssi_state = btc8821a1ant_bt_rssi_state(btcoexist, 2, 28, 0);
1598 
1599 	if (bt_link_info->hid_only) {
1600 		/* HID */
1601 		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1602 						      wifi_status);
1603 		coex_dm->auto_tdma_adjust = false;
1604 		return;
1605 	} else if (bt_link_info->a2dp_only) {
1606 		/* A2DP */
1607 		if (wifi_status == BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE) {
1608 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1609 			btc8821a1ant_coex_table_with_type(btcoexist,
1610 							  NORMAL_EXEC, 1);
1611 			coex_dm->auto_tdma_adjust = false;
1612 		} else if ((bt_rssi_state != BTC_RSSI_STATE_HIGH) &&
1613 			   (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1614 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1615 			btc8821a1ant_coex_table_with_type(btcoexist,
1616 							  NORMAL_EXEC, 1);
1617 		} else {
1618 			/* for low BT RSSI */
1619 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1620 			btc8821a1ant_coex_table_with_type(btcoexist,
1621 							  NORMAL_EXEC, 1);
1622 			coex_dm->auto_tdma_adjust = false;
1623 		}
1624 	} else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1625 		/* HID+A2DP */
1626 		if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1627 		    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1628 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1629 					     true, 14);
1630 			coex_dm->auto_tdma_adjust = false;
1631 		} else {
1632 			/*for low BT RSSI*/
1633 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1634 					     true, 14);
1635 			coex_dm->auto_tdma_adjust = false;
1636 		}
1637 
1638 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1639 	} else if ((bt_link_info->pan_only) ||
1640 		(bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1641 		/* PAN(OPP, FTP), HID+PAN(OPP, FTP) */
1642 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1643 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 6);
1644 		coex_dm->auto_tdma_adjust = false;
1645 	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1646 		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1647 		    bt_link_info->pan_exist)) {
1648 		/* A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP) */
1649 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 43);
1650 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1651 		coex_dm->auto_tdma_adjust = false;
1652 	} else {
1653 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1654 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1655 		coex_dm->auto_tdma_adjust = false;
1656 	}
1657 }
1658 
1659 static
1660 void btc8821a1ant_action_wifi_not_connected(struct btc_coexist *btcoexist)
1661 {
1662 	/* power save state */
1663 	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1664 
1665 	/* tdma and coex table */
1666 	btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1667 	btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1668 }
1669 
1670 static void btc8821a1ant_act_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1671 {
1672 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1673 
1674 	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1675 
1676 	/* tdma and coex table */
1677 	if (coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_ACL_BUSY) {
1678 		if (bt_link_info->a2dp_exist) {
1679 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1680 			btc8821a1ant_coex_table_with_type(btcoexist,
1681 							  NORMAL_EXEC, 1);
1682 		} else if (bt_link_info->a2dp_exist &&
1683 			   bt_link_info->pan_exist) {
1684 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1685 			btc8821a1ant_coex_table_with_type(btcoexist,
1686 							  NORMAL_EXEC, 4);
1687 		} else {
1688 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1689 			btc8821a1ant_coex_table_with_type(btcoexist,
1690 							  NORMAL_EXEC, 4);
1691 		}
1692 	} else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1693 		   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1694 		    coex_dm->bt_status)) {
1695 		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1696 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1697 	} else {
1698 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1699 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1700 	}
1701 }
1702 
1703 static
1704 void btc8821a1ant_action_wifi_connected_scan(struct btc_coexist *btcoexist)
1705 {
1706 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1707 
1708 	/* power save state */
1709 	btc8821a1ant_power_save_state(btcoexist,
1710 				      BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1711 
1712 	/* tdma and coex table */
1713 	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1714 		if (bt_link_info->a2dp_exist) {
1715 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1716 			btc8821a1ant_coex_table_with_type(btcoexist,
1717 							  NORMAL_EXEC, 1);
1718 		} else {
1719 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1720 			btc8821a1ant_coex_table_with_type(btcoexist,
1721 							  NORMAL_EXEC, 4);
1722 		}
1723 	} else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1724 		   (coex_dm->bt_status ==
1725 		    BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY)) {
1726 		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1727 			BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1728 	} else {
1729 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1730 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1731 	}
1732 }
1733 
1734 static void btc8821a1ant_act_wifi_conn_sp_pkt(struct btc_coexist *btcoexist)
1735 {
1736 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1737 
1738 	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1739 				      0x0, 0x0);
1740 
1741 	/* tdma and coex table */
1742 	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1743 	    (bt_link_info->a2dp_exist)) {
1744 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1745 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1746 	}
1747 
1748 	if ((bt_link_info->hid_exist) && (bt_link_info->a2dp_exist)) {
1749 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1750 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1751 	} else if (bt_link_info->pan_exist) {
1752 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1753 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1754 	} else {
1755 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1756 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1757 	}
1758 }
1759 
1760 static void btc8821a1ant_action_wifi_connected(struct btc_coexist *btcoexist)
1761 {
1762 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1763 	bool wifi_busy = false;
1764 	bool scan = false, link = false, roam = false;
1765 	bool under_4way = false;
1766 	bool ap_enable = false;
1767 
1768 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1769 		 "[BTCoex], CoexForWifiConnect()===>\n");
1770 
1771 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
1772 			   &under_4way);
1773 	if (under_4way) {
1774 		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1775 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1776 			 "[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1777 		return;
1778 	}
1779 
1780 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1781 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1782 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1783 	if (scan || link || roam) {
1784 		if (scan)
1785 			btc8821a1ant_action_wifi_connected_scan(btcoexist);
1786 		else
1787 			btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1788 
1789 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1790 			 "[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1791 		return;
1792 	}
1793 
1794 	/* power save state*/
1795 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1796 			   &ap_enable);
1797 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1798 	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY ==
1799 	    coex_dm->bt_status && !ap_enable &&
1800 	    !btcoexist->bt_link_info.hid_only) {
1801 		if (!wifi_busy && btcoexist->bt_link_info.a2dp_only)
1802 			/* A2DP */
1803 			btc8821a1ant_power_save_state(btcoexist,
1804 						BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1805 		else
1806 			btc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON,
1807 						      0x50, 0x4);
1808 	} else {
1809 		btc8821a1ant_power_save_state(btcoexist,
1810 					      BTC_PS_WIFI_NATIVE,
1811 					      0x0, 0x0);
1812 	}
1813 
1814 	/* tdma and coex table */
1815 	if (!wifi_busy) {
1816 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1817 			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1818 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1819 		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1820 			    coex_dm->bt_status) ||
1821 			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1822 			    coex_dm->bt_status)) {
1823 			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1824 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1825 		} else {
1826 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1827 			btc8821a1ant_coex_table_with_type(btcoexist,
1828 							  NORMAL_EXEC, 2);
1829 		}
1830 	} else {
1831 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1832 			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1833 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1834 		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1835 			    coex_dm->bt_status) ||
1836 			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1837 			    coex_dm->bt_status)) {
1838 			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1839 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1840 		} else {
1841 			btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1842 			btc8821a1ant_coex_table_with_type(btcoexist,
1843 							  NORMAL_EXEC, 2);
1844 		}
1845 	}
1846 }
1847 
1848 static void btc8821a1ant_run_sw_coex_mech(struct btc_coexist *btcoexist)
1849 {
1850 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1851 	u8 algorithm = 0;
1852 
1853 	algorithm = btc8821a1ant_action_algorithm(btcoexist);
1854 	coex_dm->cur_algorithm = algorithm;
1855 
1856 	if (!btc8821a1ant_is_common_action(btcoexist)) {
1857 		switch (coex_dm->cur_algorithm) {
1858 		case BT_8821A_1ANT_COEX_ALGO_SCO:
1859 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1860 				 "[BTCoex], Action algorithm = SCO\n");
1861 			btc8821a1ant_action_sco(btcoexist);
1862 			break;
1863 		case BT_8821A_1ANT_COEX_ALGO_HID:
1864 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1865 				 "[BTCoex], Action algorithm = HID\n");
1866 			btc8821a1ant_action_hid(btcoexist);
1867 			break;
1868 		case BT_8821A_1ANT_COEX_ALGO_A2DP:
1869 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1870 				 "[BTCoex], Action algorithm = A2DP\n");
1871 			btc8821a1ant_action_a2dp(btcoexist);
1872 			break;
1873 		case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
1874 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1875 				 "[BTCoex], Action algorithm = A2DP+PAN(HS)\n");
1876 			btc8821a1ant_action_a2dp_pan_hs(btcoexist);
1877 			break;
1878 		case BT_8821A_1ANT_COEX_ALGO_PANEDR:
1879 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1880 				 "[BTCoex], Action algorithm = PAN(EDR)\n");
1881 			btc8821a1ant_action_pan_edr(btcoexist);
1882 			break;
1883 		case BT_8821A_1ANT_COEX_ALGO_PANHS:
1884 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1885 				 "[BTCoex], Action algorithm = HS mode\n");
1886 			btc8821a1ant_action_pan_hs(btcoexist);
1887 			break;
1888 		case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
1889 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1890 				 "[BTCoex], Action algorithm = PAN+A2DP\n");
1891 			btc8821a1ant_action_pan_edr_a2dp(btcoexist);
1892 			break;
1893 		case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
1894 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1895 				 "[BTCoex], Action algorithm = PAN(EDR)+HID\n");
1896 			btc8821a1ant_action_pan_edr_hid(btcoexist);
1897 			break;
1898 		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
1899 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1900 				 "[BTCoex], Action algorithm = HID+A2DP+PAN\n");
1901 			btc8821a1ant_action_hid_a2dp_pan_edr(btcoexist);
1902 			break;
1903 		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
1904 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1905 				 "[BTCoex], Action algorithm = HID+A2DP\n");
1906 			btc8821a1ant_action_hid_a2dp(btcoexist);
1907 			break;
1908 		default:
1909 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1910 				 "[BTCoex], Action algorithm = coexist All Off!!\n");
1911 			/*btc8821a1ant_coex_all_off(btcoexist);*/
1912 			break;
1913 		}
1914 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
1915 	}
1916 }
1917 
1918 static void btc8821a1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
1919 {
1920 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1921 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1922 	bool wifi_connected = false, bt_hs_on = false;
1923 	bool increase_scan_dev_num = false;
1924 	bool bt_ctrl_agg_buf_size = false;
1925 	u8 agg_buf_size = 5;
1926 	u8 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
1927 	u32 wifi_link_status = 0;
1928 	u32 num_of_wifi_link = 0;
1929 	bool wifi_under_5g = false;
1930 
1931 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1932 		 "[BTCoex], RunCoexistMechanism()===>\n");
1933 
1934 	if (btcoexist->manual_control) {
1935 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1936 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
1937 		return;
1938 	}
1939 
1940 	if (btcoexist->stop_coex_dm) {
1941 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1942 			 "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
1943 		return;
1944 	}
1945 
1946 	if (coex_sta->under_ips) {
1947 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1948 			 "[BTCoex], wifi is under IPS !!!\n");
1949 		return;
1950 	}
1951 
1952 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
1953 	if (wifi_under_5g) {
1954 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1955 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
1956 		btc8821a1ant_coex_under_5g(btcoexist);
1957 		return;
1958 	}
1959 
1960 	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
1961 	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1962 	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
1963 		increase_scan_dev_num = true;
1964 
1965 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
1966 			   &increase_scan_dev_num);
1967 
1968 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1969 			   &wifi_connected);
1970 
1971 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
1972 			   &wifi_link_status);
1973 	num_of_wifi_link = wifi_link_status >> 16;
1974 	if ((num_of_wifi_link >= 2) ||
1975 	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
1976 		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1977 		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
1978 					bt_ctrl_agg_buf_size, agg_buf_size);
1979 		btc8821a1ant_action_wifi_multi_port(btcoexist);
1980 		return;
1981 	}
1982 
1983 	if (!bt_link_info->sco_exist && !bt_link_info->hid_exist) {
1984 		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1985 	} else {
1986 		if (wifi_connected) {
1987 			wifi_rssi_state =
1988 				btc8821a1ant_wifi_rssi_state(btcoexist, 1, 2,
1989 							     30, 0);
1990 			btc8821a1ant_limited_tx(btcoexist,
1991 						NORMAL_EXEC, 1, 1,
1992 						0, 1);
1993 		} else {
1994 			btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC,
1995 						0, 0, 0, 0);
1996 		}
1997 	}
1998 
1999 	if (bt_link_info->sco_exist) {
2000 		bt_ctrl_agg_buf_size = true;
2001 		agg_buf_size = 0x3;
2002 	} else if (bt_link_info->hid_exist) {
2003 		bt_ctrl_agg_buf_size = true;
2004 		agg_buf_size = 0x5;
2005 	} else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
2006 		bt_ctrl_agg_buf_size = true;
2007 		agg_buf_size = 0x8;
2008 	}
2009 	btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2010 				bt_ctrl_agg_buf_size, agg_buf_size);
2011 
2012 	btc8821a1ant_run_sw_coex_mech(btcoexist);
2013 
2014 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2015 	if (coex_sta->c2h_bt_inquiry_page) {
2016 		btc8821a1ant_action_bt_inquiry(btcoexist);
2017 		return;
2018 	} else if (bt_hs_on) {
2019 		btc8821a1ant_action_hs(btcoexist);
2020 		return;
2021 	}
2022 
2023 	if (!wifi_connected) {
2024 		bool scan = false, link = false, roam = false;
2025 
2026 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2027 			 "[BTCoex], wifi is non connected-idle !!!\n");
2028 
2029 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2030 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2031 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2032 
2033 		if (scan || link || roam) {
2034 			if (scan)
2035 				btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2036 			else
2037 				btc8821a1ant_action_wifi_not_connected_asso_auth(
2038 					btcoexist);
2039 		} else {
2040 			btc8821a1ant_action_wifi_not_connected(btcoexist);
2041 		}
2042 	} else {
2043 		/* wifi LPS/Busy */
2044 		btc8821a1ant_action_wifi_connected(btcoexist);
2045 	}
2046 }
2047 
2048 static void btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2049 {
2050 	/* force to reset coex mechanism
2051 	 * sw all off
2052 	 */
2053 	btc8821a1ant_sw_mechanism(btcoexist, false);
2054 
2055 	btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2056 }
2057 
2058 static void btc8821a1ant_init_hw_config(struct btc_coexist *btcoexist,
2059 					bool back_up, bool wifi_only)
2060 {
2061 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2062 	u8 u1_tmp = 0;
2063 	bool wifi_under_5g = false;
2064 
2065 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2066 		 "[BTCoex], 1Ant Init HW Config!!\n");
2067 
2068 	if (wifi_only)
2069 		return;
2070 
2071 	if (back_up) {
2072 		coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2073 								      0x430);
2074 		coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2075 								      0x434);
2076 		coex_dm->backup_retry_limit =
2077 			btcoexist->btc_read_2byte(btcoexist, 0x42a);
2078 		coex_dm->backup_ampdu_max_time =
2079 			btcoexist->btc_read_1byte(btcoexist, 0x456);
2080 	}
2081 
2082 	/* 0x790[5:0] = 0x5 */
2083 	u1_tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2084 	u1_tmp &= 0xc0;
2085 	u1_tmp |= 0x5;
2086 	btcoexist->btc_write_1byte(btcoexist, 0x790, u1_tmp);
2087 
2088 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2089 
2090 	/* Antenna config */
2091 	if (wifi_under_5g)
2092 		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2093 					  true, false);
2094 	else
2095 		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2096 					  true, false);
2097 	/* PTA parameter */
2098 	btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2099 
2100 	/* Enable counter statistics
2101 	 * 0x76e[3] =1, WLAN_Act control by PTA
2102 	 */
2103 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
2104 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2105 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2106 }
2107 
2108 /**************************************************************
2109  * extern function start with ex_btc8821a1ant_
2110  **************************************************************/
2111 void ex_btc8821a1ant_init_hwconfig(struct btc_coexist *btcoexist, bool wifionly)
2112 {
2113 	btc8821a1ant_init_hw_config(btcoexist, true, wifionly);
2114 	btcoexist->auto_report_1ant = true;
2115 }
2116 
2117 void ex_btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2118 {
2119 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2120 
2121 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2122 		 "[BTCoex], Coex Mechanism Init!!\n");
2123 
2124 	btcoexist->stop_coex_dm = false;
2125 
2126 	btc8821a1ant_init_coex_dm(btcoexist);
2127 
2128 	btc8821a1ant_query_bt_info(btcoexist);
2129 }
2130 
2131 void ex_btc8821a1ant_display_coex_info(struct btc_coexist *btcoexist,
2132 				       struct seq_file *m)
2133 {
2134 	struct btc_board_info *board_info = &btcoexist->board_info;
2135 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2136 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2137 	u8 u1_tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2138 	u16 u2_tmp[4];
2139 	u32 u4_tmp[4];
2140 	bool roam = false, scan = false, link = false, wifi_under_5g = false;
2141 	bool bt_hs_on = false, wifi_busy = false;
2142 	long wifi_rssi = 0, bt_hs_rssi = 0;
2143 	u32 wifi_bw, wifi_traffic_dir;
2144 	u8 wifi_dot11_chnl, wifi_hs_chnl;
2145 	u32 fw_ver = 0, bt_patch_ver = 0;
2146 
2147 	seq_puts(m, "\n ============[BT Coexist info]============");
2148 
2149 	if (btcoexist->manual_control) {
2150 		seq_puts(m, "\n ============[Under Manual Control]============");
2151 		seq_puts(m, "\n ==========================================");
2152 	}
2153 	if (btcoexist->stop_coex_dm) {
2154 		seq_puts(m, "\n ============[Coex is STOPPED]============");
2155 		seq_puts(m, "\n ==========================================");
2156 	}
2157 
2158 	seq_printf(m, "\n %-35s = %d/ %d/ %d",
2159 		   "Ant PG Num/ Ant Mech/ Ant Pos:",
2160 		   board_info->pg_ant_num,
2161 		   board_info->btdm_ant_num,
2162 		   board_info->btdm_ant_pos);
2163 
2164 	seq_printf(m, "\n %-35s = %s / %d", "BT stack/ hci ext ver",
2165 		   ((stack_info->profile_notified) ? "Yes" : "No"),
2166 		   stack_info->hci_version);
2167 
2168 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2169 			   &bt_patch_ver);
2170 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2171 	seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2172 		   "CoexVer/ FwVer/ PatchVer",
2173 		   glcoex_ver_date_8821a_1ant,
2174 		   glcoex_ver_8821a_1ant,
2175 		   fw_ver, bt_patch_ver,
2176 		   bt_patch_ver);
2177 
2178 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION,
2179 			   &bt_hs_on);
2180 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2181 			   &wifi_dot11_chnl);
2182 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL,
2183 			   &wifi_hs_chnl);
2184 	seq_printf(m, "\n %-35s = %d / %d(%d)",
2185 		   "Dot11 channel / HsChnl(HsMode)",
2186 		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2187 
2188 	seq_printf(m, "\n %-35s = %3ph ",
2189 		   "H2C Wifi inform bt chnl Info",
2190 		   coex_dm->wifi_chnl_info);
2191 
2192 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2193 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2194 	seq_printf(m, "\n %-35s = %d/ %d", "Wifi rssi/ HS rssi",
2195 		   (int)wifi_rssi, (int)bt_hs_rssi);
2196 
2197 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2198 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2199 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2200 	seq_printf(m, "\n %-35s = %d/ %d/ %d ", "Wifi link/ roam/ scan",
2201 		   link, roam, scan);
2202 
2203 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2204 			   &wifi_under_5g);
2205 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2206 			   &wifi_bw);
2207 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
2208 			   &wifi_busy);
2209 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2210 			   &wifi_traffic_dir);
2211 	seq_printf(m, "\n %-35s = %s / %s/ %s ", "Wifi status",
2212 		   (wifi_under_5g ? "5G" : "2.4G"),
2213 		   ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2214 		   (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
2215 		   ((!wifi_busy) ? "idle" :
2216 		   ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2217 		   "uplink" : "downlink")));
2218 	seq_printf(m, "\n %-35s = [%s/ %d/ %d] ",
2219 		   "BT [status/ rssi/ retryCnt]",
2220 		   ((coex_sta->bt_disabled) ? ("disabled") :
2221 		   ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2222 		   ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2223 		     coex_dm->bt_status) ?
2224 		   "non-connected idle" :
2225 		   ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
2226 		     coex_dm->bt_status) ?
2227 		   "connected-idle" : "busy")))),
2228 		   coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2229 
2230 	seq_printf(m, "\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
2231 		   bt_link_info->sco_exist,
2232 		   bt_link_info->hid_exist,
2233 		   bt_link_info->pan_exist,
2234 		   bt_link_info->a2dp_exist);
2235 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
2236 
2237 	bt_info_ext = coex_sta->bt_info_ext;
2238 	seq_printf(m, "\n %-35s = %s",
2239 		   "BT Info A2DP rate",
2240 		   (bt_info_ext & BIT0) ?
2241 		   "Basic rate" : "EDR rate");
2242 
2243 	for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2244 		if (coex_sta->bt_info_c2h_cnt[i]) {
2245 			seq_printf(m, "\n %-35s = %7ph(%d)",
2246 				   glbt_info_src_8821a_1ant[i],
2247 				   coex_sta->bt_info_c2h[i],
2248 				   coex_sta->bt_info_c2h_cnt[i]);
2249 		}
2250 	}
2251 	seq_printf(m, "\n %-35s = %s/%s, (0x%x/0x%x)",
2252 		   "PS state, IPS/LPS, (lps/rpwm)",
2253 		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2254 		   ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")),
2255 		   btcoexist->bt_info.lps_val,
2256 		   btcoexist->bt_info.rpwm_val);
2257 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
2258 
2259 	if (!btcoexist->manual_control) {
2260 		/* Sw mechanism*/
2261 		seq_printf(m, "\n %-35s",
2262 			   "============[Sw mechanism]============");
2263 
2264 		seq_printf(m, "\n %-35s = %d", "SM[LowPenaltyRA]",
2265 			   coex_dm->cur_low_penalty_ra);
2266 
2267 		seq_printf(m, "\n %-35s = %s/ %s/ %d ",
2268 			   "DelBA/ BtCtrlAgg/ AggSize",
2269 			   (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2270 			   (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2271 			   btcoexist->bt_info.agg_buf_size);
2272 		seq_printf(m, "\n %-35s = 0x%x ", "Rate Mask",
2273 			   btcoexist->bt_info.ra_mask);
2274 
2275 		/* Fw mechanism */
2276 		seq_printf(m, "\n %-35s",
2277 			   "============[Fw mechanism]============");
2278 
2279 		ps_tdma_case = coex_dm->cur_ps_tdma;
2280 		seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
2281 			   "PS TDMA",
2282 			   coex_dm->ps_tdma_para,
2283 			   ps_tdma_case,
2284 			   coex_dm->auto_tdma_adjust);
2285 
2286 		seq_printf(m, "\n %-35s = 0x%x ",
2287 			   "Latest error condition(should be 0)",
2288 			   coex_dm->error_condition);
2289 
2290 		seq_printf(m, "\n %-35s = %d ", "IgnWlanAct",
2291 			   coex_dm->cur_ignore_wlan_act);
2292 	}
2293 
2294 	/* Hw setting */
2295 	seq_printf(m, "\n %-35s", "============[Hw setting]============");
2296 
2297 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2298 		   "backup ARFR1/ARFR2/RL/AMaxTime",
2299 		   coex_dm->backup_arfr_cnt1,
2300 		   coex_dm->backup_arfr_cnt2,
2301 		   coex_dm->backup_retry_limit,
2302 		   coex_dm->backup_ampdu_max_time);
2303 
2304 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2305 	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2306 	u2_tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2307 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2308 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2309 		   "0x430/0x434/0x42a/0x456",
2310 		   u4_tmp[0], u4_tmp[1], u2_tmp[0], u1_tmp[0]);
2311 
2312 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2313 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2314 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x778/ 0xc58[29:25]",
2315 		   u1_tmp[0], (u4_tmp[0] & 0x3e000000) >> 25);
2316 
2317 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2318 	seq_printf(m, "\n %-35s = 0x%x", "0x8db[6:5]",
2319 		   ((u1_tmp[0] & 0x60) >> 5));
2320 
2321 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2322 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2323 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2324 		   "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2325 		   (u4_tmp[0] & 0x30000000) >> 28,
2326 		    u4_tmp[0] & 0xff,
2327 		    u1_tmp[0] & 0x3);
2328 
2329 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2330 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2331 	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2332 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2333 		   "0x40/0x4c[24:23]/0x64[0]",
2334 		   u1_tmp[0], ((u4_tmp[0] & 0x01800000) >> 23),
2335 		   u1_tmp[1] & 0x1);
2336 
2337 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2338 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2339 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522",
2340 		   u4_tmp[0], u1_tmp[0]);
2341 
2342 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2343 	seq_printf(m, "\n %-35s = 0x%x", "0xc50(dig)",
2344 		   u4_tmp[0] & 0xff);
2345 
2346 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xf48);
2347 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5d);
2348 	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2349 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "OFDM-FA/ CCK-FA",
2350 		   u4_tmp[0], (u1_tmp[0] << 8) + u1_tmp[1]);
2351 
2352 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2353 	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2354 	u4_tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2355 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2356 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2357 		   "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2358 		   u4_tmp[0], u4_tmp[1], u4_tmp[2], u1_tmp[0]);
2359 
2360 	seq_printf(m, "\n %-35s = %d/ %d", "0x770(high-pri rx/tx)",
2361 		   coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2362 	seq_printf(m, "\n %-35s = %d/ %d", "0x774(low-pri rx/tx)",
2363 		   coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2364 	if (btcoexist->auto_report_1ant)
2365 		btc8821a1ant_monitor_bt_ctr(btcoexist);
2366 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
2367 }
2368 
2369 void ex_btc8821a1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2370 {
2371 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2372 	bool wifi_under_5g = false;
2373 
2374 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2375 		return;
2376 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2377 	if (wifi_under_5g) {
2378 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2379 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2380 		btc8821a1ant_coex_under_5g(btcoexist);
2381 		return;
2382 	}
2383 
2384 	if (BTC_IPS_ENTER == type) {
2385 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2386 			 "[BTCoex], IPS ENTER notify\n");
2387 		coex_sta->under_ips = true;
2388 		btc8821a1ant_set_ant_path(btcoexist,
2389 					  BTC_ANT_PATH_BT, false, true);
2390 		/* set PTA control */
2391 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2392 		btc8821a1ant_coex_table_with_type(btcoexist,
2393 						  NORMAL_EXEC, 0);
2394 	} else if (BTC_IPS_LEAVE == type) {
2395 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2396 			 "[BTCoex], IPS LEAVE notify\n");
2397 		coex_sta->under_ips = false;
2398 
2399 		btc8821a1ant_init_hw_config(btcoexist, false, false);
2400 		btc8821a1ant_init_coex_dm(btcoexist);
2401 		btc8821a1ant_query_bt_info(btcoexist);
2402 	}
2403 }
2404 
2405 void ex_btc8821a1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2406 {
2407 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2408 
2409 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2410 		return;
2411 
2412 	if (BTC_LPS_ENABLE == type) {
2413 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2414 			 "[BTCoex], LPS ENABLE notify\n");
2415 		coex_sta->under_lps = true;
2416 	} else if (BTC_LPS_DISABLE == type) {
2417 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2418 			 "[BTCoex], LPS DISABLE notify\n");
2419 		coex_sta->under_lps = false;
2420 	}
2421 }
2422 
2423 void ex_btc8821a1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2424 {
2425 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2426 	bool wifi_connected = false, bt_hs_on = false;
2427 	bool bt_ctrl_agg_buf_size = false;
2428 	bool wifi_under_5g = false;
2429 	u32 wifi_link_status = 0;
2430 	u32 num_of_wifi_link = 0;
2431 	u8 agg_buf_size = 5;
2432 
2433 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2434 		return;
2435 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2436 	if (wifi_under_5g) {
2437 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2438 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2439 		btc8821a1ant_coex_under_5g(btcoexist);
2440 		return;
2441 	}
2442 
2443 	if (type == BTC_SCAN_START) {
2444 		coex_sta->wifi_is_high_pri_task = true;
2445 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2446 			 "[BTCoex], SCAN START notify\n");
2447 
2448 		/* Force antenna setup for no scan result issue */
2449 		btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2450 	} else {
2451 		coex_sta->wifi_is_high_pri_task = false;
2452 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2453 			 "[BTCoex], SCAN FINISH notify\n");
2454 	}
2455 
2456 	if (coex_sta->bt_disabled)
2457 		return;
2458 
2459 	btcoexist->btc_get(btcoexist,
2460 		 BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2461 	btcoexist->btc_get(btcoexist,
2462 		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2463 
2464 	btc8821a1ant_query_bt_info(btcoexist);
2465 
2466 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2467 			   &wifi_link_status);
2468 	num_of_wifi_link = wifi_link_status >> 16;
2469 	if (num_of_wifi_link >= 2) {
2470 		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2471 		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2472 					bt_ctrl_agg_buf_size, agg_buf_size);
2473 		btc8821a1ant_action_wifi_multi_port(btcoexist);
2474 		return;
2475 	}
2476 
2477 	if (coex_sta->c2h_bt_inquiry_page) {
2478 		btc8821a1ant_action_bt_inquiry(btcoexist);
2479 		return;
2480 	} else if (bt_hs_on) {
2481 		btc8821a1ant_action_hs(btcoexist);
2482 		return;
2483 	}
2484 
2485 	if (BTC_SCAN_START == type) {
2486 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2487 			 "[BTCoex], SCAN START notify\n");
2488 		if (!wifi_connected) {
2489 			/* non-connected scan */
2490 			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2491 		} else {
2492 			/* wifi is connected */
2493 			btc8821a1ant_action_wifi_connected_scan(btcoexist);
2494 		}
2495 	} else if (BTC_SCAN_FINISH == type) {
2496 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2497 			 "[BTCoex], SCAN FINISH notify\n");
2498 		if (!wifi_connected) {
2499 			/* non-connected scan */
2500 			btc8821a1ant_action_wifi_not_connected(btcoexist);
2501 		} else {
2502 			btc8821a1ant_action_wifi_connected(btcoexist);
2503 		}
2504 	}
2505 }
2506 
2507 void ex_btc8821a1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2508 {
2509 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2510 	bool wifi_connected = false, bt_hs_on = false;
2511 	u32 wifi_link_status = 0;
2512 	u32 num_of_wifi_link = 0;
2513 	bool bt_ctrl_agg_buf_size = false;
2514 	bool wifi_under_5g = false;
2515 	u8 agg_buf_size = 5;
2516 
2517 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2518 	    coex_sta->bt_disabled)
2519 		return;
2520 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2521 	if (wifi_under_5g) {
2522 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2523 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2524 		btc8821a1ant_coex_under_5g(btcoexist);
2525 		return;
2526 	}
2527 
2528 	if (type == BTC_ASSOCIATE_START) {
2529 		coex_sta->wifi_is_high_pri_task = true;
2530 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2531 			 "[BTCoex], CONNECT START notify\n");
2532 		coex_dm->arp_cnt = 0;
2533 	} else {
2534 		coex_sta->wifi_is_high_pri_task = false;
2535 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2536 			 "[BTCoex], CONNECT FINISH notify\n");
2537 		coex_dm->arp_cnt = 0;
2538 	}
2539 
2540 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2541 			   &wifi_link_status);
2542 	num_of_wifi_link = wifi_link_status >> 16;
2543 	if (num_of_wifi_link >= 2) {
2544 		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2545 		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2546 					bt_ctrl_agg_buf_size, agg_buf_size);
2547 		btc8821a1ant_action_wifi_multi_port(btcoexist);
2548 		return;
2549 	}
2550 
2551 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2552 	if (coex_sta->c2h_bt_inquiry_page) {
2553 		btc8821a1ant_action_bt_inquiry(btcoexist);
2554 		return;
2555 	} else if (bt_hs_on) {
2556 		btc8821a1ant_action_hs(btcoexist);
2557 		return;
2558 	}
2559 
2560 	if (BTC_ASSOCIATE_START == type) {
2561 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2562 			 "[BTCoex], CONNECT START notify\n");
2563 		btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2564 	} else if (BTC_ASSOCIATE_FINISH == type) {
2565 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2566 			 "[BTCoex], CONNECT FINISH notify\n");
2567 
2568 		btcoexist->btc_get(btcoexist,
2569 			 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2570 		if (!wifi_connected) {
2571 			/* non-connected scan */
2572 			btc8821a1ant_action_wifi_not_connected(btcoexist);
2573 		} else {
2574 			btc8821a1ant_action_wifi_connected(btcoexist);
2575 		}
2576 	}
2577 }
2578 
2579 void ex_btc8821a1ant_media_status_notify(struct btc_coexist *btcoexist,
2580 					 u8 type)
2581 {
2582 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2583 	u8 h2c_parameter[3] = {0};
2584 	u32 wifi_bw;
2585 	u8 wifi_central_chnl;
2586 	bool wifi_under_5g = false;
2587 
2588 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2589 	    coex_sta->bt_disabled)
2590 		return;
2591 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2592 	if (wifi_under_5g) {
2593 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2594 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2595 		btc8821a1ant_coex_under_5g(btcoexist);
2596 		return;
2597 	}
2598 
2599 	if (BTC_MEDIA_CONNECT == type) {
2600 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2601 			 "[BTCoex], MEDIA connect notify\n");
2602 	} else {
2603 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2604 			 "[BTCoex], MEDIA disconnect notify\n");
2605 		coex_dm->arp_cnt = 0;
2606 	}
2607 
2608 	/* only 2.4G we need to inform bt the chnl mask */
2609 	btcoexist->btc_get(btcoexist,
2610 			   BTC_GET_U1_WIFI_CENTRAL_CHNL,
2611 			   &wifi_central_chnl);
2612 	if ((type == BTC_MEDIA_CONNECT) &&
2613 	    (wifi_central_chnl <= 14)) {
2614 		h2c_parameter[0] = 0x0;
2615 		h2c_parameter[1] = wifi_central_chnl;
2616 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2617 		if (wifi_bw == BTC_WIFI_BW_HT40)
2618 			h2c_parameter[2] = 0x30;
2619 		else
2620 			h2c_parameter[2] = 0x20;
2621 	}
2622 
2623 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2624 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2625 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2626 
2627 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2628 		 "[BTCoex], FW write 0x66 = 0x%x\n",
2629 		 h2c_parameter[0] << 16 |
2630 		 h2c_parameter[1] << 8 |
2631 		 h2c_parameter[2]);
2632 
2633 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2634 }
2635 
2636 void ex_btc8821a1ant_special_packet_notify(struct btc_coexist *btcoexist,
2637 					   u8 type)
2638 {
2639 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2640 	bool bt_hs_on = false;
2641 	bool bt_ctrl_agg_buf_size = false;
2642 	bool wifi_under_5g = false;
2643 	u32 wifi_link_status = 0;
2644 	u32 num_of_wifi_link = 0;
2645 	u8 agg_buf_size = 5;
2646 
2647 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2648 	    coex_sta->bt_disabled)
2649 		return;
2650 
2651 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2652 	if (wifi_under_5g) {
2653 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2654 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2655 		btc8821a1ant_coex_under_5g(btcoexist);
2656 		return;
2657 	}
2658 
2659 	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2660 	    type == BTC_PACKET_ARP) {
2661 		coex_sta->wifi_is_high_pri_task = true;
2662 
2663 		if (type == BTC_PACKET_ARP) {
2664 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2665 				 "[BTCoex], specific Packet ARP notify\n");
2666 		} else {
2667 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2668 				 "[BTCoex], specific Packet DHCP or EAPOL notify\n");
2669 		}
2670 	} else {
2671 		coex_sta->wifi_is_high_pri_task = false;
2672 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2673 			 "[BTCoex], specific Packet [Type = %d] notify\n",
2674 			 type);
2675 	}
2676 
2677 	coex_sta->special_pkt_period_cnt = 0;
2678 
2679 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2680 			   &wifi_link_status);
2681 	num_of_wifi_link = wifi_link_status >> 16;
2682 	if (num_of_wifi_link >= 2) {
2683 		btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2684 		btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2685 					bt_ctrl_agg_buf_size, agg_buf_size);
2686 		btc8821a1ant_action_wifi_multi_port(btcoexist);
2687 		return;
2688 	}
2689 
2690 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2691 	if (coex_sta->c2h_bt_inquiry_page) {
2692 		btc8821a1ant_action_bt_inquiry(btcoexist);
2693 		return;
2694 	} else if (bt_hs_on) {
2695 		btc8821a1ant_action_hs(btcoexist);
2696 		return;
2697 	}
2698 
2699 	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2700 	    type == BTC_PACKET_ARP) {
2701 		if (type == BTC_PACKET_ARP) {
2702 			coex_dm->arp_cnt++;
2703 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2704 				 "[BTCoex], ARP Packet Count = %d\n",
2705 				 coex_dm->arp_cnt);
2706 			if (coex_dm->arp_cnt >= 10)
2707 				/* if APR PKT > 10 after connect, do not go to
2708 				 * btc8821a1ant_act_wifi_conn_sp_pkt
2709 				 */
2710 				return;
2711 		}
2712 
2713 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2714 			 "[BTCoex], special Packet(%d) notify\n", type);
2715 		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
2716 	}
2717 }
2718 
2719 void ex_btc8821a1ant_bt_info_notify(struct btc_coexist *btcoexist,
2720 				    u8 *tmp_buf, u8 length)
2721 {
2722 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2723 	u8 i;
2724 	u8 bt_info = 0;
2725 	u8 rsp_source = 0;
2726 	bool wifi_connected = false;
2727 	bool bt_busy = false;
2728 	bool wifi_under_5g = false;
2729 
2730 	coex_sta->c2h_bt_info_req_sent = false;
2731 
2732 	btcoexist->btc_get(btcoexist,
2733 		 BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2734 
2735 	rsp_source = tmp_buf[0] & 0xf;
2736 	if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2737 		rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2738 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
2739 
2740 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2741 		 "[BTCoex], Bt info[%d], length = %d, hex data = [",
2742 		 rsp_source, length);
2743 	for (i = 0; i < length; i++) {
2744 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2745 		if (i == 1)
2746 			bt_info = tmp_buf[i];
2747 		if (i == length - 1) {
2748 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2749 				 "0x%02x]\n", tmp_buf[i]);
2750 		} else {
2751 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2752 				 "0x%02x, ", tmp_buf[i]);
2753 		}
2754 	}
2755 
2756 	if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
2757 		/* [3:0] */
2758 		coex_sta->bt_retry_cnt =
2759 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
2760 
2761 		coex_sta->bt_rssi =
2762 			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
2763 
2764 		coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
2765 
2766 		coex_sta->bt_tx_rx_mask =
2767 			(coex_sta->bt_info_c2h[rsp_source][2] & 0x40);
2768 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
2769 				   &coex_sta->bt_tx_rx_mask);
2770 		if (!coex_sta->bt_tx_rx_mask) {
2771 			/* BT into is responded by BT FW and BT RF REG 0x3C !=
2772 			 * 0x15 => Need to switch BT TRx Mask
2773 			 */
2774 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2775 				 "[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
2776 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
2777 						  0x3c, 0x15);
2778 		}
2779 
2780 		/* Here we need to resend some wifi info to BT
2781 		 * because bt is reset and lost the info
2782 		 */
2783 		if (coex_sta->bt_info_ext & BIT1) {
2784 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2785 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
2786 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2787 					   &wifi_connected);
2788 			if (wifi_connected) {
2789 				ex_btc8821a1ant_media_status_notify(btcoexist,
2790 							       BTC_MEDIA_CONNECT);
2791 			} else {
2792 				ex_btc8821a1ant_media_status_notify(btcoexist,
2793 							       BTC_MEDIA_DISCONNECT);
2794 			}
2795 		}
2796 
2797 		if ((coex_sta->bt_info_ext & BIT3) && !wifi_under_5g) {
2798 			if (!btcoexist->manual_control &&
2799 			    !btcoexist->stop_coex_dm) {
2800 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2801 					 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
2802 				btc8821a1ant_ignore_wlan_act(btcoexist,
2803 							     FORCE_EXEC,
2804 							     false);
2805 			}
2806 		}
2807 	}
2808 
2809 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
2810 	if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
2811 		coex_sta->c2h_bt_inquiry_page = true;
2812 	else
2813 		coex_sta->c2h_bt_inquiry_page = false;
2814 
2815 	/* set link exist status */
2816 	if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2817 		coex_sta->bt_link_exist = false;
2818 		coex_sta->pan_exist = false;
2819 		coex_sta->a2dp_exist = false;
2820 		coex_sta->hid_exist = false;
2821 		coex_sta->sco_exist = false;
2822 	} else {
2823 		/* connection exists */
2824 		coex_sta->bt_link_exist = true;
2825 		if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
2826 			coex_sta->pan_exist = true;
2827 		else
2828 			coex_sta->pan_exist = false;
2829 		if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
2830 			coex_sta->a2dp_exist = true;
2831 		else
2832 			coex_sta->a2dp_exist = false;
2833 		if (bt_info & BT_INFO_8821A_1ANT_B_HID)
2834 			coex_sta->hid_exist = true;
2835 		else
2836 			coex_sta->hid_exist = false;
2837 		if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
2838 			coex_sta->sco_exist = true;
2839 		else
2840 			coex_sta->sco_exist = false;
2841 	}
2842 
2843 	btc8821a1ant_update_bt_link_info(btcoexist);
2844 
2845 	/* mask profile bit for connect-ilde identification
2846 	 * (for CSR case: A2DP idle --> 0x41)
2847 	 */
2848 	bt_info = bt_info & 0x1f;
2849 
2850 	if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2851 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
2852 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2853 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
2854 	} else if (bt_info == BT_INFO_8821A_1ANT_B_CONNECTION) {
2855 		/* connection exists but no busy */
2856 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
2857 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2858 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
2859 	} else if ((bt_info&BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
2860 		(bt_info & BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
2861 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
2862 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2863 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
2864 	} else if (bt_info & BT_INFO_8821A_1ANT_B_ACL_BUSY) {
2865 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
2866 			coex_dm->auto_tdma_adjust = false;
2867 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
2868 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2869 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
2870 	} else {
2871 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
2872 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2873 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
2874 	}
2875 
2876 	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2877 	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2878 	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2879 		bt_busy = true;
2880 	else
2881 		bt_busy = false;
2882 	btcoexist->btc_set(btcoexist,
2883 			   BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2884 
2885 	btc8821a1ant_run_coexist_mechanism(btcoexist);
2886 }
2887 
2888 void ex_btc8821a1ant_halt_notify(struct btc_coexist *btcoexist)
2889 {
2890 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2891 	bool wifi_under_5g = false;
2892 
2893 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2894 		 "[BTCoex], Halt notify\n");
2895 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2896 	if (wifi_under_5g) {
2897 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2898 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2899 		btc8821a1ant_coex_under_5g(btcoexist);
2900 		return;
2901 	}
2902 
2903 
2904 	btcoexist->stop_coex_dm = true;
2905 
2906 	btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false, true);
2907 	btc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2908 
2909 	btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2910 	btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
2911 
2912 	ex_btc8821a1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
2913 }
2914 
2915 void ex_btc8821a1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
2916 {
2917 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2918 	bool wifi_under_5g = false;
2919 
2920 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2921 	if (wifi_under_5g) {
2922 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2923 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2924 		btc8821a1ant_coex_under_5g(btcoexist);
2925 		return;
2926 	}
2927 
2928 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2929 		 "[BTCoex], Pnp notify\n");
2930 
2931 	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
2932 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2933 			 "[BTCoex], Pnp notify to SLEEP\n");
2934 		/* BT should clear UnderIPS/UnderLPS state to avoid mismatch
2935 		 * state after wakeup.
2936 		 */
2937 		coex_sta->under_ips = false;
2938 		coex_sta->under_lps = false;
2939 		btcoexist->stop_coex_dm = true;
2940 		btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2941 					      0x0, 0x0);
2942 		btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2943 		btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2944 		btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
2945 					  true);
2946 	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
2947 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2948 			 "[BTCoex], Pnp notify to WAKE UP\n");
2949 		btcoexist->stop_coex_dm = false;
2950 		btc8821a1ant_init_hw_config(btcoexist, false, false);
2951 		btc8821a1ant_init_coex_dm(btcoexist);
2952 		btc8821a1ant_query_bt_info(btcoexist);
2953 	}
2954 }
2955 
2956 void ex_btc8821a1ant_periodical(struct btc_coexist *btcoexist)
2957 {
2958 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2959 	static u8 dis_ver_info_cnt;
2960 	u32 fw_ver = 0, bt_patch_ver = 0;
2961 	struct btc_board_info *board_info = &btcoexist->board_info;
2962 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2963 
2964 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2965 		 "[BTCoex], ==========================Periodical===========================\n");
2966 
2967 	if (dis_ver_info_cnt <= 5) {
2968 		dis_ver_info_cnt += 1;
2969 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2970 			 "[BTCoex], ****************************************************************\n");
2971 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2972 			 "[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
2973 			      board_info->pg_ant_num,
2974 			      board_info->btdm_ant_num,
2975 			      board_info->btdm_ant_pos);
2976 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2977 			 "[BTCoex], BT stack/ hci ext ver = %s / %d\n",
2978 			      stack_info->profile_notified ? "Yes" : "No",
2979 			      stack_info->hci_version);
2980 		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2981 				   &bt_patch_ver);
2982 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2983 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2984 			 "[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
2985 			      glcoex_ver_date_8821a_1ant,
2986 			      glcoex_ver_8821a_1ant,
2987 			      fw_ver, bt_patch_ver,
2988 			      bt_patch_ver);
2989 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2990 			 "[BTCoex], ****************************************************************\n");
2991 	}
2992 
2993 	if (!btcoexist->auto_report_1ant) {
2994 		btc8821a1ant_query_bt_info(btcoexist);
2995 		btc8821a1ant_monitor_bt_ctr(btcoexist);
2996 	} else {
2997 		coex_sta->special_pkt_period_cnt++;
2998 	}
2999 }
3000