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  * Description:
27  *
28  * This file is for RTL8723B Co-exist mechanism
29  *
30  * History
31  * 2012/11/15 Cosa first check in.
32  *
33  **************************************************************/
34 /**************************************************************
35  * include files
36  **************************************************************/
37 #include "halbt_precomp.h"
38 /**************************************************************
39  * Global variables, these are static variables
40  **************************************************************/
41 static struct coex_dm_8723b_2ant glcoex_dm_8723b_2ant;
42 static struct coex_dm_8723b_2ant *coex_dm = &glcoex_dm_8723b_2ant;
43 static struct coex_sta_8723b_2ant glcoex_sta_8723b_2ant;
44 static struct coex_sta_8723b_2ant *coex_sta = &glcoex_sta_8723b_2ant;
45 
46 static const char *const glbt_info_src_8723b_2ant[] = {
47 	"BT Info[wifi fw]",
48 	"BT Info[bt rsp]",
49 	"BT Info[bt auto report]",
50 };
51 
52 static u32 glcoex_ver_date_8723b_2ant = 20131113;
53 static u32 glcoex_ver_8723b_2ant = 0x3f;
54 
55 /**************************************************************
56  * local function proto type if needed
57  **************************************************************/
58 /**************************************************************
59  * local function start with btc8723b2ant_
60  **************************************************************/
61 static u8 btc8723b2ant_bt_rssi_state(struct btc_coexist *btcoexist,
62 				     u8 level_num, u8 rssi_thresh,
63 				     u8 rssi_thresh1)
64 {
65 	struct rtl_priv *rtlpriv = btcoexist->adapter;
66 	s32 bt_rssi = 0;
67 	u8 bt_rssi_state = coex_sta->pre_bt_rssi_state;
68 
69 	bt_rssi = coex_sta->bt_rssi;
70 
71 	if (level_num == 2) {
72 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
73 		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
74 			if (bt_rssi >= rssi_thresh +
75 				       BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
76 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
77 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
78 					 "[BTCoex], BT Rssi state switch to High\n");
79 			} else {
80 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
81 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
82 					 "[BTCoex], BT Rssi state stay at Low\n");
83 			}
84 		} else {
85 			if (bt_rssi < rssi_thresh) {
86 				bt_rssi_state = BTC_RSSI_STATE_LOW;
87 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
88 					 "[BTCoex], BT Rssi state switch to Low\n");
89 			} else {
90 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
91 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
92 					 "[BTCoex], BT Rssi state stay at High\n");
93 			}
94 		}
95 	} else if (level_num == 3) {
96 		if (rssi_thresh > rssi_thresh1) {
97 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
98 				 "[BTCoex], BT Rssi thresh error!!\n");
99 			return coex_sta->pre_bt_rssi_state;
100 		}
101 
102 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
103 		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
104 			if (bt_rssi >= rssi_thresh +
105 				       BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
106 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
107 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
108 					 "[BTCoex], BT Rssi state switch to Medium\n");
109 			} else {
110 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
111 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
112 					 "[BTCoex], BT Rssi state stay at Low\n");
113 			}
114 		} else if ((coex_sta->pre_bt_rssi_state ==
115 						BTC_RSSI_STATE_MEDIUM) ||
116 			   (coex_sta->pre_bt_rssi_state ==
117 						BTC_RSSI_STATE_STAY_MEDIUM)) {
118 			if (bt_rssi >= rssi_thresh1 +
119 				       BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
120 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
121 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
122 					 "[BTCoex], BT Rssi state switch to High\n");
123 			} else if (bt_rssi < rssi_thresh) {
124 				bt_rssi_state = BTC_RSSI_STATE_LOW;
125 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
126 					 "[BTCoex], BT Rssi state switch to Low\n");
127 			} else {
128 				bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
129 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
130 					 "[BTCoex], BT Rssi state stay at Medium\n");
131 			}
132 		} else {
133 			if (bt_rssi < rssi_thresh1) {
134 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
135 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
136 					 "[BTCoex], BT Rssi state switch to Medium\n");
137 			} else {
138 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
139 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
140 					 "[BTCoex], BT Rssi state stay at High\n");
141 			}
142 		}
143 	}
144 
145 	coex_sta->pre_bt_rssi_state = bt_rssi_state;
146 
147 	return bt_rssi_state;
148 }
149 
150 static u8 btc8723b2ant_wifi_rssi_state(struct btc_coexist *btcoexist,
151 				       u8 index, u8 level_num,
152 				       u8 rssi_thresh, u8 rssi_thresh1)
153 {
154 	struct rtl_priv *rtlpriv = btcoexist->adapter;
155 	s32 wifi_rssi = 0;
156 	u8 wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
157 
158 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
159 
160 	if (level_num == 2) {
161 		if ((coex_sta->pre_wifi_rssi_state[index] ==
162 						BTC_RSSI_STATE_LOW) ||
163 		    (coex_sta->pre_wifi_rssi_state[index] ==
164 						BTC_RSSI_STATE_STAY_LOW)) {
165 			if (wifi_rssi >= rssi_thresh +
166 					 BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
167 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
168 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
169 					 "[BTCoex], wifi RSSI state switch to High\n");
170 			} else {
171 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
172 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
173 					 "[BTCoex], wifi RSSI state stay at Low\n");
174 			}
175 		} else {
176 			if (wifi_rssi < rssi_thresh) {
177 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
178 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
179 					 "[BTCoex], wifi RSSI state switch to Low\n");
180 			} else {
181 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
182 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
183 					 "[BTCoex], wifi RSSI state stay at High\n");
184 			}
185 		}
186 	} else if (level_num == 3) {
187 		if (rssi_thresh > rssi_thresh1) {
188 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
189 				 "[BTCoex], wifi RSSI thresh error!!\n");
190 			return coex_sta->pre_wifi_rssi_state[index];
191 		}
192 
193 		if ((coex_sta->pre_wifi_rssi_state[index] ==
194 						BTC_RSSI_STATE_LOW) ||
195 		    (coex_sta->pre_wifi_rssi_state[index] ==
196 						BTC_RSSI_STATE_STAY_LOW)) {
197 			if (wifi_rssi >= rssi_thresh +
198 					BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
199 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
200 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
201 					 "[BTCoex], wifi RSSI state switch to Medium\n");
202 			} else {
203 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
204 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
205 					 "[BTCoex], wifi RSSI state stay at Low\n");
206 			}
207 		} else if ((coex_sta->pre_wifi_rssi_state[index] ==
208 						BTC_RSSI_STATE_MEDIUM) ||
209 			   (coex_sta->pre_wifi_rssi_state[index] ==
210 						BTC_RSSI_STATE_STAY_MEDIUM)) {
211 			if (wifi_rssi >= rssi_thresh1 +
212 					 BTC_RSSI_COEX_THRESH_TOL_8723B_2ANT) {
213 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
214 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
215 					 "[BTCoex], wifi RSSI state switch to High\n");
216 			} else if (wifi_rssi < rssi_thresh) {
217 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
218 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
219 					 "[BTCoex], wifi RSSI state switch to Low\n");
220 			} else {
221 				wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
222 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
223 					 "[BTCoex], wifi RSSI state stay at Medium\n");
224 			}
225 		} else {
226 			if (wifi_rssi < rssi_thresh1) {
227 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
228 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
229 					 "[BTCoex], wifi RSSI state switch to Medium\n");
230 			} else {
231 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
232 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
233 					 "[BTCoex], wifi RSSI state stay at High\n");
234 			}
235 		}
236 	}
237 
238 	coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
239 
240 	return wifi_rssi_state;
241 }
242 
243 static
244 void btc8723b2ant_limited_rx(struct btc_coexist *btcoexist, bool force_exec,
245 			     bool rej_ap_agg_pkt, bool bt_ctrl_agg_buf_size,
246 			     u8 agg_buf_size)
247 {
248 	bool reject_rx_agg = rej_ap_agg_pkt;
249 	bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
250 	u8 rx_agg_size = agg_buf_size;
251 
252 	/* ============================================ */
253 	/*	Rx Aggregation related setting		*/
254 	/* ============================================ */
255 	btcoexist->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
256 			   &reject_rx_agg);
257 	/* decide BT control aggregation buf size or not */
258 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
259 			   &bt_ctrl_rx_agg_size);
260 	/* aggregate buf size, only work when BT control Rx aggregate size */
261 	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
262 	/* real update aggregation setting */
263 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
264 }
265 
266 static void btc8723b2ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
267 {
268 	struct rtl_priv *rtlpriv = btcoexist->adapter;
269 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
270 	u32 reg_hp_txrx, reg_lp_txrx, u32tmp;
271 	u32 reg_hp_tx = 0, reg_hp_rx = 0;
272 	u32 reg_lp_tx = 0, reg_lp_rx = 0;
273 
274 	reg_hp_txrx = 0x770;
275 	reg_lp_txrx = 0x774;
276 
277 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
278 	reg_hp_tx = u32tmp & MASKLWORD;
279 	reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
280 
281 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
282 	reg_lp_tx = u32tmp & MASKLWORD;
283 	reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
284 
285 	coex_sta->high_priority_tx = reg_hp_tx;
286 	coex_sta->high_priority_rx = reg_hp_rx;
287 	coex_sta->low_priority_tx = reg_lp_tx;
288 	coex_sta->low_priority_rx = reg_lp_rx;
289 
290 	if ((coex_sta->low_priority_tx > 1050) &&
291 	    (!coex_sta->c2h_bt_inquiry_page))
292 		coex_sta->pop_event_cnt++;
293 
294 	if ((coex_sta->low_priority_rx >= 950) &&
295 	    (coex_sta->low_priority_rx >= coex_sta->low_priority_tx) &&
296 	    (!coex_sta->under_ips))
297 		bt_link_info->slave_role = true;
298 	else
299 		bt_link_info->slave_role = false;
300 
301 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
302 		 "[BTCoex], High Priority Tx/Rx(reg 0x%x)=0x%x(%d)/0x%x(%d)\n",
303 		 reg_hp_txrx, reg_hp_tx, reg_hp_tx, reg_hp_rx, reg_hp_rx);
304 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
305 		 "[BTCoex], Low Priority Tx/Rx(reg 0x%x)=0x%x(%d)/0x%x(%d)\n",
306 		 reg_lp_txrx, reg_lp_tx, reg_lp_tx, reg_lp_rx, reg_lp_rx);
307 
308 	/* reset counter */
309 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
310 }
311 
312 static void btc8723b2ant_monitor_wifi_ctr(struct btc_coexist *btcoexist)
313 {
314 	if (coex_sta->under_ips) {
315 		coex_sta->crc_ok_cck = 0;
316 		coex_sta->crc_ok_11g = 0;
317 		coex_sta->crc_ok_11n = 0;
318 		coex_sta->crc_ok_11n_agg = 0;
319 
320 		coex_sta->crc_err_cck = 0;
321 		coex_sta->crc_err_11g = 0;
322 		coex_sta->crc_err_11n = 0;
323 		coex_sta->crc_err_11n_agg = 0;
324 	} else {
325 		coex_sta->crc_ok_cck =
326 			btcoexist->btc_read_4byte(btcoexist, 0xf88);
327 		coex_sta->crc_ok_11g =
328 			btcoexist->btc_read_2byte(btcoexist, 0xf94);
329 		coex_sta->crc_ok_11n =
330 			btcoexist->btc_read_2byte(btcoexist, 0xf90);
331 		coex_sta->crc_ok_11n_agg =
332 			btcoexist->btc_read_2byte(btcoexist, 0xfb8);
333 
334 		coex_sta->crc_err_cck =
335 			btcoexist->btc_read_4byte(btcoexist, 0xf84);
336 		coex_sta->crc_err_11g =
337 			btcoexist->btc_read_2byte(btcoexist, 0xf96);
338 		coex_sta->crc_err_11n =
339 			btcoexist->btc_read_2byte(btcoexist, 0xf92);
340 		coex_sta->crc_err_11n_agg =
341 			btcoexist->btc_read_2byte(btcoexist, 0xfba);
342 	}
343 
344 	/* reset counter */
345 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x1);
346 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x0);
347 }
348 
349 static void btc8723b2ant_query_bt_info(struct btc_coexist *btcoexist)
350 {
351 	struct rtl_priv *rtlpriv = btcoexist->adapter;
352 	u8 h2c_parameter[1] = {0};
353 
354 	coex_sta->c2h_bt_info_req_sent = true;
355 
356 	h2c_parameter[0] |= BIT0;	/* trigger */
357 
358 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
359 		 "[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
360 		 h2c_parameter[0]);
361 
362 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
363 }
364 
365 static bool btc8723b2ant_is_wifi_status_changed(struct btc_coexist *btcoexist)
366 {
367 	static bool pre_wifi_busy;
368 	static bool pre_under_4way;
369 	static bool pre_bt_hs_on;
370 	bool wifi_busy = false, under_4way = false, bt_hs_on = false;
371 	bool wifi_connected = false;
372 	u8 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
373 	u8 tmp;
374 
375 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
376 			   &wifi_connected);
377 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
378 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
379 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
380 			   &under_4way);
381 
382 	if (wifi_connected) {
383 		if (wifi_busy != pre_wifi_busy) {
384 			pre_wifi_busy = wifi_busy;
385 			return true;
386 		}
387 
388 		if (under_4way != pre_under_4way) {
389 			pre_under_4way = under_4way;
390 			return true;
391 		}
392 
393 		if (bt_hs_on != pre_bt_hs_on) {
394 			pre_bt_hs_on = bt_hs_on;
395 			return true;
396 		}
397 
398 		tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
399 				 coex_dm->switch_thres_offset;
400 		wifi_rssi_state =
401 		     btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, tmp, 0);
402 
403 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
404 		    (wifi_rssi_state == BTC_RSSI_STATE_LOW))
405 			return true;
406 	}
407 
408 	return false;
409 }
410 
411 static void btc8723b2ant_update_bt_link_info(struct btc_coexist *btcoexist)
412 {
413 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
414 	bool bt_hs_on = false;
415 
416 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
417 
418 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
419 	bt_link_info->sco_exist = coex_sta->sco_exist;
420 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
421 	bt_link_info->pan_exist = coex_sta->pan_exist;
422 	bt_link_info->hid_exist = coex_sta->hid_exist;
423 
424 	/* work around for HS mode. */
425 	if (bt_hs_on) {
426 		bt_link_info->pan_exist = true;
427 		bt_link_info->bt_link_exist = true;
428 	}
429 
430 	/* check if Sco only */
431 	if (bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
432 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
433 		bt_link_info->sco_only = true;
434 	else
435 		bt_link_info->sco_only = false;
436 
437 	/* check if A2dp only */
438 	if (!bt_link_info->sco_exist && bt_link_info->a2dp_exist &&
439 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
440 		bt_link_info->a2dp_only = true;
441 	else
442 		bt_link_info->a2dp_only = false;
443 
444 	/* check if Pan only */
445 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
446 	    bt_link_info->pan_exist && !bt_link_info->hid_exist)
447 		bt_link_info->pan_only = true;
448 	else
449 		bt_link_info->pan_only = false;
450 
451 	/* check if Hid only */
452 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
453 	    !bt_link_info->pan_exist && bt_link_info->hid_exist)
454 		bt_link_info->hid_only = true;
455 	else
456 		bt_link_info->hid_only = false;
457 }
458 
459 static u8 btc8723b2ant_action_algorithm(struct btc_coexist *btcoexist)
460 {
461 	struct rtl_priv *rtlpriv = btcoexist->adapter;
462 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
463 	bool bt_hs_on = false;
464 	u8 algorithm = BT_8723B_2ANT_COEX_ALGO_UNDEFINED;
465 	u8 num_of_diff_profile = 0;
466 
467 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
468 
469 	if (!bt_link_info->bt_link_exist) {
470 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
471 			 "[BTCoex], No BT link exists!!!\n");
472 		return algorithm;
473 	}
474 
475 	if (bt_link_info->sco_exist)
476 		num_of_diff_profile++;
477 	if (bt_link_info->hid_exist)
478 		num_of_diff_profile++;
479 	if (bt_link_info->pan_exist)
480 		num_of_diff_profile++;
481 	if (bt_link_info->a2dp_exist)
482 		num_of_diff_profile++;
483 
484 	if (num_of_diff_profile == 1) {
485 		if (bt_link_info->sco_exist) {
486 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
487 				 "[BTCoex], SCO only\n");
488 			algorithm = BT_8723B_2ANT_COEX_ALGO_SCO;
489 		} else {
490 			if (bt_link_info->hid_exist) {
491 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
492 					 "[BTCoex], HID only\n");
493 				algorithm = BT_8723B_2ANT_COEX_ALGO_HID;
494 			} else if (bt_link_info->a2dp_exist) {
495 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
496 					 "[BTCoex], A2DP only\n");
497 				algorithm = BT_8723B_2ANT_COEX_ALGO_A2DP;
498 			} else if (bt_link_info->pan_exist) {
499 				if (bt_hs_on) {
500 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
501 						 DBG_LOUD,
502 						 "[BTCoex], PAN(HS) only\n");
503 					algorithm =
504 						BT_8723B_2ANT_COEX_ALGO_PANHS;
505 				} else {
506 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
507 						 DBG_LOUD,
508 						 "[BTCoex], PAN(EDR) only\n");
509 					algorithm =
510 						BT_8723B_2ANT_COEX_ALGO_PANEDR;
511 				}
512 			}
513 		}
514 	} else if (num_of_diff_profile == 2) {
515 		if (bt_link_info->sco_exist) {
516 			if (bt_link_info->hid_exist) {
517 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
518 					 "[BTCoex], SCO + HID\n");
519 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
520 			} else if (bt_link_info->a2dp_exist) {
521 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
522 					 "[BTCoex], SCO + A2DP ==> SCO\n");
523 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
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], SCO + PAN(HS)\n");
529 					algorithm = BT_8723B_2ANT_COEX_ALGO_SCO;
530 				} else {
531 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
532 						 DBG_LOUD,
533 						 "[BTCoex], SCO + PAN(EDR)\n");
534 					algorithm =
535 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
536 				}
537 			}
538 		} else {
539 			if (bt_link_info->hid_exist &&
540 			    bt_link_info->a2dp_exist) {
541 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
542 					 "[BTCoex], HID + A2DP\n");
543 				algorithm = BT_8723B_2ANT_COEX_ALGO_HID_A2DP;
544 			} else if (bt_link_info->hid_exist &&
545 				   bt_link_info->pan_exist) {
546 				if (bt_hs_on) {
547 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
548 						 DBG_LOUD,
549 						 "[BTCoex], HID + PAN(HS)\n");
550 					algorithm = BT_8723B_2ANT_COEX_ALGO_HID;
551 				} else {
552 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
553 						 DBG_LOUD,
554 						 "[BTCoex], HID + PAN(EDR)\n");
555 					algorithm =
556 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
557 				}
558 			} else if (bt_link_info->pan_exist &&
559 				   bt_link_info->a2dp_exist) {
560 				if (bt_hs_on) {
561 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
562 						 DBG_LOUD,
563 						 "[BTCoex], A2DP + PAN(HS)\n");
564 					algorithm =
565 					    BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS;
566 				} else {
567 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
568 						 DBG_LOUD,
569 						 "[BTCoex],A2DP + PAN(EDR)\n");
570 					algorithm =
571 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP;
572 				}
573 			}
574 		}
575 	} else if (num_of_diff_profile == 3) {
576 		if (bt_link_info->sco_exist) {
577 			if (bt_link_info->hid_exist &&
578 			    bt_link_info->a2dp_exist) {
579 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
580 					 "[BTCoex], SCO + HID + A2DP ==> HID\n");
581 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
582 			} else if (bt_link_info->hid_exist &&
583 				   bt_link_info->pan_exist) {
584 				if (bt_hs_on) {
585 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
586 						 DBG_LOUD,
587 						 "[BTCoex], SCO + HID + PAN(HS)\n");
588 					algorithm =
589 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
590 				} else {
591 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
592 						 DBG_LOUD,
593 						 "[BTCoex], SCO + HID + PAN(EDR)\n");
594 					algorithm =
595 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
596 				}
597 			} else if (bt_link_info->pan_exist &&
598 				   bt_link_info->a2dp_exist) {
599 				if (bt_hs_on) {
600 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
601 						 DBG_LOUD,
602 						 "[BTCoex], SCO + A2DP + PAN(HS)\n");
603 					algorithm =
604 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
605 				} else {
606 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
607 						 DBG_LOUD,
608 						 "[BTCoex], SCO + A2DP + PAN(EDR) ==> HID\n");
609 					algorithm =
610 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
611 				}
612 			}
613 		} else {
614 			if (bt_link_info->hid_exist &&
615 			    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], HID + A2DP + PAN(HS)\n");
621 					algorithm =
622 					    BT_8723B_2ANT_COEX_ALGO_HID_A2DP;
623 				} else {
624 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
625 						 DBG_LOUD,
626 						 "[BTCoex], HID + A2DP + PAN(EDR)\n");
627 					algorithm =
628 					BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR;
629 				}
630 			}
631 		}
632 	} else if (num_of_diff_profile >= 3) {
633 		if (bt_link_info->sco_exist) {
634 			if (bt_link_info->hid_exist &&
635 			    bt_link_info->pan_exist &&
636 			    bt_link_info->a2dp_exist) {
637 				if (bt_hs_on) {
638 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
639 						 DBG_LOUD,
640 						 "[BTCoex], Error!!! SCO + HID + A2DP + PAN(HS)\n");
641 				} else {
642 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
643 						 DBG_LOUD,
644 						 "[BTCoex], SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
645 					algorithm =
646 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
647 				}
648 			}
649 		}
650 	}
651 	return algorithm;
652 }
653 
654 static void btc8723b2ant_set_fw_dac_swing_level(struct btc_coexist *btcoexist,
655 						u8 dac_swing_lvl)
656 {
657 	struct rtl_priv *rtlpriv = btcoexist->adapter;
658 	u8 h2c_parameter[1] = {0};
659 
660 	/* There are several type of dacswing
661 	 * 0x18/ 0x10/ 0xc/ 0x8/ 0x4/ 0x6
662 	 */
663 	h2c_parameter[0] = dac_swing_lvl;
664 
665 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
666 		 "[BTCoex], Set Dac Swing Level=0x%x\n", dac_swing_lvl);
667 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
668 		 "[BTCoex], FW write 0x64=0x%x\n", h2c_parameter[0]);
669 
670 	btcoexist->btc_fill_h2c(btcoexist, 0x64, 1, h2c_parameter);
671 }
672 
673 static void btc8723b2ant_set_fw_dec_bt_pwr(struct btc_coexist *btcoexist,
674 					   u8 dec_bt_pwr_lvl)
675 {
676 	struct rtl_priv *rtlpriv = btcoexist->adapter;
677 	u8 h2c_parameter[1] = {0};
678 
679 	h2c_parameter[0] = dec_bt_pwr_lvl;
680 
681 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
682 		 "[BTCoex], decrease Bt Power Level : %u\n", dec_bt_pwr_lvl);
683 
684 	btcoexist->btc_fill_h2c(btcoexist, 0x62, 1, h2c_parameter);
685 }
686 
687 static void btc8723b2ant_dec_bt_pwr(struct btc_coexist *btcoexist,
688 				    bool force_exec, u8 dec_bt_pwr_lvl)
689 {
690 	struct rtl_priv *rtlpriv = btcoexist->adapter;
691 
692 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
693 		 "[BTCoex], Dec BT power level = %u\n", dec_bt_pwr_lvl);
694 	coex_dm->cur_dec_bt_pwr_lvl = dec_bt_pwr_lvl;
695 
696 	if (!force_exec) {
697 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
698 			 "[BTCoex], PreDecBtPwrLvl=%d, CurDecBtPwrLvl=%d\n",
699 			    coex_dm->pre_dec_bt_pwr_lvl,
700 			    coex_dm->cur_dec_bt_pwr_lvl);
701 
702 		if (coex_dm->pre_dec_bt_pwr_lvl == coex_dm->cur_dec_bt_pwr_lvl)
703 			return;
704 	}
705 	btc8723b2ant_set_fw_dec_bt_pwr(btcoexist, coex_dm->cur_dec_bt_pwr_lvl);
706 
707 	coex_dm->pre_dec_bt_pwr_lvl = coex_dm->cur_dec_bt_pwr_lvl;
708 }
709 
710 static
711 void halbtc8723b2ant_set_bt_auto_report(struct btc_coexist *btcoexist,
712 					bool enable_auto_report)
713 {
714 	u8 h2c_parameter[1] = {0};
715 
716 	h2c_parameter[0] = 0;
717 
718 	if (enable_auto_report)
719 		h2c_parameter[0] |= BIT(0);
720 
721 	btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
722 }
723 
724 static
725 void btc8723b2ant_bt_auto_report(struct btc_coexist *btcoexist,
726 				 bool force_exec, bool enable_auto_report)
727 {
728 	coex_dm->cur_bt_auto_report = enable_auto_report;
729 
730 	if (!force_exec) {
731 		if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
732 			return;
733 	}
734 	halbtc8723b2ant_set_bt_auto_report(btcoexist,
735 					   coex_dm->cur_bt_auto_report);
736 
737 	coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
738 }
739 
740 static void btc8723b2ant_fw_dac_swing_lvl(struct btc_coexist *btcoexist,
741 					  bool force_exec, u8 fw_dac_swing_lvl)
742 {
743 	struct rtl_priv *rtlpriv = btcoexist->adapter;
744 
745 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
746 		 "[BTCoex], %s set FW Dac Swing level = %d\n",
747 		    (force_exec ? "force to" : ""), fw_dac_swing_lvl);
748 	coex_dm->cur_fw_dac_swing_lvl = fw_dac_swing_lvl;
749 
750 	if (!force_exec) {
751 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
752 			 "[BTCoex], preFwDacSwingLvl=%d, curFwDacSwingLvl=%d\n",
753 			    coex_dm->pre_fw_dac_swing_lvl,
754 			    coex_dm->cur_fw_dac_swing_lvl);
755 
756 		if (coex_dm->pre_fw_dac_swing_lvl ==
757 		   coex_dm->cur_fw_dac_swing_lvl)
758 			return;
759 	}
760 
761 	btc8723b2ant_set_fw_dac_swing_level(btcoexist,
762 					    coex_dm->cur_fw_dac_swing_lvl);
763 	coex_dm->pre_fw_dac_swing_lvl = coex_dm->cur_fw_dac_swing_lvl;
764 }
765 
766 static void btc8723b_set_penalty_txrate(struct btc_coexist *btcoexist,
767 					bool low_penalty_ra)
768 {
769 	struct rtl_priv *rtlpriv = btcoexist->adapter;
770 	u8 h2c_parameter[6] = {0};
771 
772 	h2c_parameter[0] = 0x6;	/* op_code, 0x6 = Retry_Penalty */
773 
774 	if (low_penalty_ra) {
775 		h2c_parameter[1] |= BIT0;
776 		/* normal rate except MCS7/6/5, OFDM54/48/36 */
777 		h2c_parameter[2] = 0x00;
778 		h2c_parameter[3] = 0xf4; /* MCS7 or OFDM54 */
779 		h2c_parameter[4] = 0xf5; /* MCS6 or OFDM48 */
780 		h2c_parameter[5] = 0xf6; /* MCS5 or OFDM36 */
781 	}
782 
783 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
784 		 "[BTCoex], set WiFi Low-Penalty Retry: %s",
785 		 (low_penalty_ra ? "ON!!" : "OFF!!"));
786 
787 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
788 }
789 
790 static void btc8723b2ant_low_penalty_ra(struct btc_coexist *btcoexist,
791 					bool force_exec, bool low_penalty_ra)
792 {
793 	struct rtl_priv *rtlpriv = btcoexist->adapter;
794 
795 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
796 		 "[BTCoex], %s turn LowPenaltyRA = %s\n",
797 		 (force_exec ? "force to" : ""), (low_penalty_ra ?
798 						  "ON" : "OFF"));
799 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
800 
801 	if (!force_exec) {
802 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
803 			 "[BTCoex], bPreLowPenaltyRa=%d, bCurLowPenaltyRa=%d\n",
804 			 coex_dm->pre_low_penalty_ra,
805 			 coex_dm->cur_low_penalty_ra);
806 
807 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
808 			return;
809 	}
810 	btc8723b_set_penalty_txrate(btcoexist, coex_dm->cur_low_penalty_ra);
811 
812 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
813 }
814 
815 static void btc8723b2ant_set_dac_swing_reg(struct btc_coexist *btcoexist,
816 					   u32 level)
817 {
818 	struct rtl_priv *rtlpriv = btcoexist->adapter;
819 	u8 val = (u8) level;
820 
821 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
822 		 "[BTCoex], Write SwDacSwing = 0x%x\n", level);
823 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x883, 0x3e, val);
824 }
825 
826 static void btc8723b2ant_set_sw_fulltime_dac_swing(struct btc_coexist *btcoex,
827 						   bool sw_dac_swing_on,
828 						   u32 sw_dac_swing_lvl)
829 {
830 	if (sw_dac_swing_on)
831 		btc8723b2ant_set_dac_swing_reg(btcoex, sw_dac_swing_lvl);
832 	else
833 		btc8723b2ant_set_dac_swing_reg(btcoex, 0x18);
834 }
835 
836 static void btc8723b2ant_dac_swing(struct btc_coexist *btcoexist,
837 				   bool force_exec, bool dac_swing_on,
838 				   u32 dac_swing_lvl)
839 {
840 	struct rtl_priv *rtlpriv = btcoexist->adapter;
841 
842 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
843 		 "[BTCoex], %s turn DacSwing=%s, dac_swing_lvl=0x%x\n",
844 		 (force_exec ? "force to" : ""),
845 		 (dac_swing_on ? "ON" : "OFF"), dac_swing_lvl);
846 	coex_dm->cur_dac_swing_on = dac_swing_on;
847 	coex_dm->cur_dac_swing_lvl = dac_swing_lvl;
848 
849 	if (!force_exec) {
850 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
851 			 "[BTCoex], bPreDacSwingOn=%d, preDacSwingLvl=0x%x, bCurDacSwingOn=%d, curDacSwingLvl=0x%x\n",
852 			 coex_dm->pre_dac_swing_on,
853 			 coex_dm->pre_dac_swing_lvl,
854 			 coex_dm->cur_dac_swing_on,
855 			 coex_dm->cur_dac_swing_lvl);
856 
857 		if ((coex_dm->pre_dac_swing_on == coex_dm->cur_dac_swing_on) &&
858 		    (coex_dm->pre_dac_swing_lvl == coex_dm->cur_dac_swing_lvl))
859 			return;
860 	}
861 	mdelay(30);
862 	btc8723b2ant_set_sw_fulltime_dac_swing(btcoexist, dac_swing_on,
863 					       dac_swing_lvl);
864 
865 	coex_dm->pre_dac_swing_on = coex_dm->cur_dac_swing_on;
866 	coex_dm->pre_dac_swing_lvl = coex_dm->cur_dac_swing_lvl;
867 }
868 
869 static void btc8723b2ant_set_coex_table(struct btc_coexist *btcoexist,
870 					u32 val0x6c0, u32 val0x6c4,
871 					u32 val0x6c8, u8 val0x6cc)
872 {
873 	struct rtl_priv *rtlpriv = btcoexist->adapter;
874 
875 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
876 		 "[BTCoex], set coex table, set 0x6c0=0x%x\n", val0x6c0);
877 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
878 
879 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
880 		 "[BTCoex], set coex table, set 0x6c4=0x%x\n", val0x6c4);
881 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
882 
883 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
884 		 "[BTCoex], set coex table, set 0x6c8=0x%x\n", val0x6c8);
885 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
886 
887 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
888 		 "[BTCoex], set coex table, set 0x6cc=0x%x\n", val0x6cc);
889 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
890 }
891 
892 static void btc8723b2ant_coex_table(struct btc_coexist *btcoexist,
893 				    bool force_exec, u32 val0x6c0,
894 				    u32 val0x6c4, u32 val0x6c8,
895 				    u8 val0x6cc)
896 {
897 	struct rtl_priv *rtlpriv = btcoexist->adapter;
898 
899 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
900 		 "[BTCoex], %s write Coex Table 0x6c0=0x%x, 0x6c4=0x%x, 0x6c8=0x%x, 0x6cc=0x%x\n",
901 		 force_exec ? "force to" : "",
902 		 val0x6c0, val0x6c4, val0x6c8, val0x6cc);
903 	coex_dm->cur_val0x6c0 = val0x6c0;
904 	coex_dm->cur_val0x6c4 = val0x6c4;
905 	coex_dm->cur_val0x6c8 = val0x6c8;
906 	coex_dm->cur_val0x6cc = val0x6cc;
907 
908 	if (!force_exec) {
909 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
910 			 "[BTCoex], preVal0x6c0=0x%x, preVal0x6c4=0x%x, preVal0x6c8=0x%x, preVal0x6cc=0x%x !!\n",
911 			 coex_dm->pre_val0x6c0, coex_dm->pre_val0x6c4,
912 			 coex_dm->pre_val0x6c8, coex_dm->pre_val0x6cc);
913 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
914 			 "[BTCoex], curVal0x6c0=0x%x, curVal0x6c4=0x%x, curVal0x6c8=0x%x, curVal0x6cc=0x%x !!\n",
915 			 coex_dm->cur_val0x6c0, coex_dm->cur_val0x6c4,
916 			 coex_dm->cur_val0x6c8, coex_dm->cur_val0x6cc);
917 
918 		if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
919 		    (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
920 		    (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
921 		    (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
922 			return;
923 	}
924 	btc8723b2ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
925 				    val0x6c8, val0x6cc);
926 
927 	coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
928 	coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
929 	coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
930 	coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
931 }
932 
933 static void btc8723b2ant_coex_table_with_type(struct btc_coexist *btcoexist,
934 					      bool force_exec, u8 type)
935 {
936 	switch (type) {
937 	case 0:
938 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55555555,
939 					0x55555555, 0xffffff, 0x3);
940 		break;
941 	case 1:
942 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55555555,
943 					0x5afa5afa, 0xffffff, 0x3);
944 		break;
945 	case 2:
946 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5ada5ada,
947 					0x5ada5ada, 0xffffff, 0x3);
948 		break;
949 	case 3:
950 		btc8723b2ant_coex_table(btcoexist, force_exec, 0xaaaaaaaa,
951 					0xaaaaaaaa, 0xffffff, 0x3);
952 		break;
953 	case 4:
954 		btc8723b2ant_coex_table(btcoexist, force_exec, 0xffffffff,
955 					0xffffffff, 0xffffff, 0x3);
956 		break;
957 	case 5:
958 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
959 					0x5fff5fff, 0xffffff, 0x3);
960 		break;
961 	case 6:
962 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
963 					0x5a5a5a5a, 0xffffff, 0x3);
964 		break;
965 	case 7:
966 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
967 					0x5ada5ada, 0xffffff, 0x3);
968 		break;
969 	case 8:
970 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
971 					0x5ada5ada, 0xffffff, 0x3);
972 		break;
973 	case 9:
974 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
975 					0x5ada5ada, 0xffffff, 0x3);
976 		break;
977 	case 10:
978 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
979 					0x5ada5ada, 0xffffff, 0x3);
980 		break;
981 	case 11:
982 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
983 					0x5ada5ada, 0xffffff, 0x3);
984 		break;
985 	case 12:
986 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
987 					0x5ada5ada, 0xffffff, 0x3);
988 		break;
989 	case 13:
990 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
991 					0xaaaaaaaa, 0xffffff, 0x3);
992 		break;
993 	case 14:
994 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
995 					0x5ada5ada, 0xffffff, 0x3);
996 		break;
997 	case 15:
998 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
999 					0xaaaaaaaa, 0xffffff, 0x3);
1000 		break;
1001 	default:
1002 		break;
1003 	}
1004 }
1005 
1006 static void btc8723b2ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
1007 						bool enable)
1008 {
1009 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1010 	u8 h2c_parameter[1] = {0};
1011 
1012 	if (enable)
1013 		h2c_parameter[0] |= BIT0; /* function enable */
1014 
1015 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1016 		 "[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63=0x%x\n",
1017 		 h2c_parameter[0]);
1018 
1019 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
1020 }
1021 
1022 static void btc8723b2ant_set_lps_rpwm(struct btc_coexist *btcoexist,
1023 				      u8 lps_val, u8 rpwm_val)
1024 {
1025 	u8 lps = lps_val;
1026 	u8 rpwm = rpwm_val;
1027 
1028 	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
1029 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
1030 }
1031 
1032 static void btc8723b2ant_lps_rpwm(struct btc_coexist *btcoexist,
1033 				  bool force_exec, u8 lps_val, u8 rpwm_val)
1034 {
1035 	coex_dm->cur_lps = lps_val;
1036 	coex_dm->cur_rpwm = rpwm_val;
1037 
1038 	if (!force_exec) {
1039 		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
1040 		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm))
1041 			return;
1042 	}
1043 	btc8723b2ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
1044 
1045 	coex_dm->pre_lps = coex_dm->cur_lps;
1046 	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
1047 }
1048 
1049 static void btc8723b2ant_ignore_wlan_act(struct btc_coexist *btcoexist,
1050 					 bool force_exec, bool enable)
1051 {
1052 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1053 
1054 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1055 		 "[BTCoex], %s turn Ignore WlanAct %s\n",
1056 		 (force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
1057 	coex_dm->cur_ignore_wlan_act = enable;
1058 
1059 	if (!force_exec) {
1060 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1061 			 "[BTCoex], bPreIgnoreWlanAct = %d, bCurIgnoreWlanAct = %d!!\n",
1062 			 coex_dm->pre_ignore_wlan_act,
1063 			 coex_dm->cur_ignore_wlan_act);
1064 
1065 		if (coex_dm->pre_ignore_wlan_act ==
1066 		    coex_dm->cur_ignore_wlan_act)
1067 			return;
1068 	}
1069 	btc8723b2ant_set_fw_ignore_wlan_act(btcoexist, enable);
1070 
1071 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
1072 }
1073 
1074 static void btc8723b2ant_set_fw_ps_tdma(struct btc_coexist *btcoexist, u8 byte1,
1075 					u8 byte2, u8 byte3, u8 byte4, u8 byte5)
1076 {
1077 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1078 	u8 h2c_parameter[5];
1079 	if ((coex_sta->a2dp_exist) && (coex_sta->hid_exist))
1080 		byte5 = byte5 | 0x1;
1081 
1082 	h2c_parameter[0] = byte1;
1083 	h2c_parameter[1] = byte2;
1084 	h2c_parameter[2] = byte3;
1085 	h2c_parameter[3] = byte4;
1086 	h2c_parameter[4] = byte5;
1087 
1088 	coex_dm->ps_tdma_para[0] = byte1;
1089 	coex_dm->ps_tdma_para[1] = byte2;
1090 	coex_dm->ps_tdma_para[2] = byte3;
1091 	coex_dm->ps_tdma_para[3] = byte4;
1092 	coex_dm->ps_tdma_para[4] = byte5;
1093 
1094 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1095 		 "[BTCoex], FW write 0x60(5bytes)=0x%x%08x\n",
1096 		 h2c_parameter[0],
1097 		 h2c_parameter[1] << 24 | h2c_parameter[2] << 16 |
1098 		 h2c_parameter[3] << 8 | h2c_parameter[4]);
1099 
1100 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1101 }
1102 
1103 static void btc8723b2ant_sw_mechanism(struct btc_coexist *btcoexist,
1104 				      bool shrink_rx_lpf, bool low_penalty_ra,
1105 				      bool limited_dig, bool bt_lna_constrain)
1106 {
1107 	btc8723b2ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
1108 }
1109 
1110 static void btc8723b2ant_set_ant_path(struct btc_coexist *btcoexist,
1111 				      u8 antpos_type, bool init_hwcfg,
1112 				      bool wifi_off)
1113 {
1114 	struct btc_board_info *board_info = &btcoexist->board_info;
1115 	u32 fw_ver = 0, u32tmp = 0;
1116 	bool pg_ext_switch = false;
1117 	bool use_ext_switch = false;
1118 	u8 h2c_parameter[2] = {0};
1119 
1120 	btcoexist->btc_get(btcoexist, BTC_GET_BL_EXT_SWITCH, &pg_ext_switch);
1121 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
1122 
1123 	if ((fw_ver < 0xc0000) || pg_ext_switch)
1124 		use_ext_switch = true;
1125 
1126 	if (init_hwcfg) {
1127 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x39, 0x8, 0x1);
1128 		btcoexist->btc_write_1byte(btcoexist, 0x974, 0xff);
1129 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x944, 0x3, 0x3);
1130 		btcoexist->btc_write_1byte(btcoexist, 0x930, 0x77);
1131 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x1);
1132 
1133 		if (fw_ver >= 0x180000) {
1134 			/* Use H2C to set GNT_BT to High to avoid A2DP click */
1135 			h2c_parameter[0] = 1;
1136 			btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1,
1137 						h2c_parameter);
1138 		} else {
1139 			btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
1140 		}
1141 
1142 		btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
1143 
1144 		/* WiFi TRx Mask off */
1145 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A,
1146 					  0x1, 0xfffff, 0x0);
1147 
1148 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
1149 			/* tell firmware "no antenna inverse" */
1150 			h2c_parameter[0] = 0;
1151 		} else {
1152 			/* tell firmware "antenna inverse" */
1153 			h2c_parameter[0] = 1;
1154 		}
1155 
1156 		if (use_ext_switch) {
1157 			/* ext switch type */
1158 			h2c_parameter[1] = 1;
1159 		} else {
1160 			/* int switch type */
1161 			h2c_parameter[1] = 0;
1162 		}
1163 		btcoexist->btc_fill_h2c(btcoexist, 0x65, 2, h2c_parameter);
1164 	} else {
1165 		if (fw_ver >= 0x180000) {
1166 			/* Use H2C to set GNT_BT to "Control by PTA"*/
1167 			h2c_parameter[0] = 0;
1168 			btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1,
1169 						h2c_parameter);
1170 		} else {
1171 			btcoexist->btc_write_1byte(btcoexist, 0x765, 0x0);
1172 		}
1173 	}
1174 
1175 	/* ext switch setting */
1176 	if (use_ext_switch) {
1177 		if (init_hwcfg) {
1178 			/* 0x4c[23] = 0, 0x4c[24] = 1 Ant controlled by WL/BT */
1179 			u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1180 			u32tmp &= ~BIT23;
1181 			u32tmp |= BIT24;
1182 			btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
1183 		}
1184 
1185 		/* fixed internal switch S1->WiFi, S0->BT */
1186 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1187 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1188 		else
1189 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1190 
1191 		switch (antpos_type) {
1192 		case BTC_ANT_WIFI_AT_MAIN:
1193 			/* ext switch main at wifi */
1194 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x92c,
1195 							   0x3, 0x1);
1196 			break;
1197 		case BTC_ANT_WIFI_AT_AUX:
1198 			/* ext switch aux at wifi */
1199 			btcoexist->btc_write_1byte_bitmask(btcoexist,
1200 							   0x92c, 0x3, 0x2);
1201 			break;
1202 		}
1203 	} else {
1204 		/* internal switch */
1205 		if (init_hwcfg) {
1206 			/* 0x4c[23] = 0, 0x4c[24] = 1 Ant controlled by WL/BT */
1207 			u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1208 			u32tmp |= BIT23;
1209 			u32tmp &= ~BIT24;
1210 			btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
1211 		}
1212 
1213 		/* fixed ext switch, S1->Main, S0->Aux */
1214 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64, 0x1, 0x0);
1215 		switch (antpos_type) {
1216 		case BTC_ANT_WIFI_AT_MAIN:
1217 			/* fixed internal switch S1->WiFi, S0->BT */
1218 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1219 			break;
1220 		case BTC_ANT_WIFI_AT_AUX:
1221 			/* fixed internal switch S0->WiFi, S1->BT */
1222 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1223 			break;
1224 		}
1225 	}
1226 }
1227 
1228 static void btc8723b2ant_ps_tdma(struct btc_coexist *btcoexist, bool force_exec,
1229 				 bool turn_on, u8 type)
1230 {
1231 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1232 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1233 	u8 wifi_rssi_state, bt_rssi_state;
1234 	s8 wifi_duration_adjust = 0x0;
1235 	u8 tdma_byte4_modify = 0x0;
1236 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
1237 			coex_dm->switch_thres_offset;
1238 
1239 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, tmp, 0);
1240 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
1241 			coex_dm->switch_thres_offset;
1242 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
1243 
1244 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1245 		 "[BTCoex], %s turn %s PS TDMA, type=%d\n",
1246 		 (force_exec ? "force to" : ""),
1247 		 (turn_on ? "ON" : "OFF"), type);
1248 	coex_dm->cur_ps_tdma_on = turn_on;
1249 	coex_dm->cur_ps_tdma = type;
1250 
1251 	if (!(BTC_RSSI_HIGH(wifi_rssi_state) &&
1252 	      BTC_RSSI_HIGH(bt_rssi_state)) && turn_on) {
1253 		 /* for WiFi RSSI low or BT RSSI low */
1254 		type = type + 100;
1255 		coex_dm->is_switch_to_1dot5_ant = true;
1256 	} else {
1257 		coex_dm->is_switch_to_1dot5_ant = false;
1258 	}
1259 
1260 	if (!force_exec) {
1261 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1262 			 "[BTCoex], bPrePsTdmaOn = %d, bCurPsTdmaOn = %d!!\n",
1263 			 coex_dm->pre_ps_tdma_on, coex_dm->cur_ps_tdma_on);
1264 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1265 			 "[BTCoex], prePsTdma = %d, curPsTdma = %d!!\n",
1266 			 coex_dm->pre_ps_tdma, coex_dm->cur_ps_tdma);
1267 
1268 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1269 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1270 			return;
1271 	}
1272 
1273 	if (coex_sta->scan_ap_num <= 5) {
1274 		if (coex_sta->a2dp_bit_pool >= 45)
1275 			wifi_duration_adjust = -15;
1276 		else if (coex_sta->a2dp_bit_pool >= 35)
1277 			wifi_duration_adjust = -10;
1278 		else
1279 			wifi_duration_adjust = 5;
1280 	} else if (coex_sta->scan_ap_num <= 20) {
1281 		if (coex_sta->a2dp_bit_pool >= 45)
1282 			wifi_duration_adjust = -15;
1283 		else if (coex_sta->a2dp_bit_pool >= 35)
1284 			wifi_duration_adjust = -10;
1285 		else
1286 			wifi_duration_adjust = 0;
1287 	} else if (coex_sta->scan_ap_num <= 40) {
1288 		if (coex_sta->a2dp_bit_pool >= 45)
1289 			wifi_duration_adjust = -15;
1290 		else if (coex_sta->a2dp_bit_pool >= 35)
1291 			wifi_duration_adjust = -10;
1292 		else
1293 			wifi_duration_adjust = -5;
1294 	} else {
1295 		if (coex_sta->a2dp_bit_pool >= 45)
1296 			wifi_duration_adjust = -15;
1297 		else if (coex_sta->a2dp_bit_pool >= 35)
1298 			wifi_duration_adjust = -10;
1299 		else
1300 			wifi_duration_adjust = -10;
1301 	}
1302 
1303 	if ((bt_link_info->slave_role) && (bt_link_info->a2dp_exist))
1304 		/* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1305 		tdma_byte4_modify = 0x1;
1306 
1307 	if (turn_on) {
1308 		switch (type) {
1309 		case 1:
1310 		default:
1311 			btc8723b2ant_set_fw_ps_tdma(
1312 				btcoexist, 0xe3, 0x3c,
1313 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1314 			break;
1315 		case 2:
1316 			btc8723b2ant_set_fw_ps_tdma(
1317 				btcoexist, 0xe3, 0x2d,
1318 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1319 			break;
1320 		case 3:
1321 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1322 						    0x3, 0xf1,
1323 						    0x90 | tdma_byte4_modify);
1324 			break;
1325 		case 4:
1326 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1327 						    0x03, 0xf1,
1328 						    0x90 | tdma_byte4_modify);
1329 			break;
1330 		case 5:
1331 			btc8723b2ant_set_fw_ps_tdma(
1332 				btcoexist, 0xe3, 0x3c,
1333 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1334 			break;
1335 		case 6:
1336 			btc8723b2ant_set_fw_ps_tdma(
1337 				btcoexist, 0xe3, 0x2d,
1338 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1339 			break;
1340 		case 7:
1341 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1342 						    0x3, 0x70,
1343 						    0x90 | tdma_byte4_modify);
1344 			break;
1345 		case 8:
1346 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x10,
1347 						    0x3, 0x70,
1348 						    0x90 | tdma_byte4_modify);
1349 			break;
1350 		case 9:
1351 			btc8723b2ant_set_fw_ps_tdma(
1352 				btcoexist, 0xe3, 0x3c + wifi_duration_adjust,
1353 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1354 			break;
1355 		case 10:
1356 			btc8723b2ant_set_fw_ps_tdma(
1357 				btcoexist, 0xe3, 0x2d,
1358 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1359 			break;
1360 		case 11:
1361 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1362 						    0x3, 0xf1,
1363 						    0x90 | tdma_byte4_modify);
1364 			break;
1365 		case 12:
1366 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1367 						    0x3, 0xf1,
1368 						    0x90 | tdma_byte4_modify);
1369 			break;
1370 		case 13:
1371 			btc8723b2ant_set_fw_ps_tdma(
1372 				btcoexist, 0xe3, 0x3c,
1373 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1374 			break;
1375 		case 14:
1376 			btc8723b2ant_set_fw_ps_tdma(
1377 				btcoexist, 0xe3, 0x2d,
1378 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1379 			break;
1380 		case 15:
1381 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1382 						    0x3, 0x70,
1383 						    0x90 | tdma_byte4_modify);
1384 			break;
1385 		case 16:
1386 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1387 						    0x3, 0x70,
1388 						    0x90 | tdma_byte4_modify);
1389 			break;
1390 		case 17:
1391 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x2f,
1392 						    0x2f, 0x60, 0x90);
1393 			break;
1394 		case 18:
1395 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x5, 0x5,
1396 						    0xe1, 0x90);
1397 			break;
1398 		case 19:
1399 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1400 						    0x25, 0xe1, 0x90);
1401 			break;
1402 		case 20:
1403 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1404 						    0x25, 0x60, 0x90);
1405 			break;
1406 		case 21:
1407 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1408 						    0x03, 0x70, 0x90);
1409 			break;
1410 
1411 		case 23:
1412 		case 123:
1413 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x35,
1414 						    0x03, 0x71, 0x10);
1415 			break;
1416 		case 71:
1417 			btc8723b2ant_set_fw_ps_tdma(
1418 				btcoexist, 0xe3, 0x3c + wifi_duration_adjust,
1419 				0x03, 0xf1, 0x90);
1420 			break;
1421 		case 101:
1422 		case 105:
1423 		case 113:
1424 		case 171:
1425 			btc8723b2ant_set_fw_ps_tdma(
1426 				btcoexist, 0xd3, 0x3a + wifi_duration_adjust,
1427 				0x03, 0x70, 0x50 | tdma_byte4_modify);
1428 			break;
1429 		case 102:
1430 		case 106:
1431 		case 110:
1432 		case 114:
1433 			btc8723b2ant_set_fw_ps_tdma(
1434 				btcoexist, 0xd3, 0x2d + wifi_duration_adjust,
1435 				0x03, 0x70, 0x50 | tdma_byte4_modify);
1436 			break;
1437 		case 103:
1438 		case 107:
1439 		case 111:
1440 		case 115:
1441 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x1c,
1442 						    0x03, 0x70,
1443 						    0x50 | tdma_byte4_modify);
1444 			break;
1445 		case 104:
1446 		case 108:
1447 		case 112:
1448 		case 116:
1449 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x10,
1450 						    0x03, 0x70,
1451 						    0x50 | tdma_byte4_modify);
1452 			break;
1453 		case 109:
1454 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x3c,
1455 						    0x03, 0xf1,
1456 						    0x90 | tdma_byte4_modify);
1457 			break;
1458 		case 121:
1459 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1460 						    0x03, 0x70,
1461 						    0x90 | tdma_byte4_modify);
1462 			break;
1463 		case 22:
1464 		case 122:
1465 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x35,
1466 						    0x03, 0x71, 0x11);
1467 			break;
1468 		}
1469 	} else {
1470 		/* disable PS tdma */
1471 		switch (type) {
1472 		case 0:
1473 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1474 						    0x40, 0x0);
1475 			break;
1476 		case 1:
1477 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1478 						    0x48, 0x0);
1479 			break;
1480 		default:
1481 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1482 						    0x40, 0x0);
1483 			break;
1484 		}
1485 	}
1486 
1487 	/* update pre state */
1488 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1489 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1490 }
1491 
1492 static void btc8723b2ant_ps_tdma_check_for_power_save_state(
1493 		struct btc_coexist *btcoexist, bool new_ps_state)
1494 {
1495 	u8 lps_mode = 0x0;
1496 
1497 	btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1498 
1499 	if (lps_mode) {
1500 		/* already under LPS state */
1501 		if (new_ps_state) {
1502 			/* keep state under LPS, do nothing. */
1503 		} else {
1504 			/* will leave LPS state, turn off psTdma first */
1505 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1506 		}
1507 	} else {
1508 		/* NO PS state */
1509 		if (new_ps_state) {
1510 			/* will enter LPS state, turn off psTdma first */
1511 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1512 		} else {
1513 			/* keep state under NO PS state, do nothing. */
1514 		}
1515 	}
1516 }
1517 
1518 static void btc8723b2ant_power_save_state(struct btc_coexist *btcoexist,
1519 					  u8 ps_type, u8 lps_val, u8 rpwm_val)
1520 {
1521 	bool low_pwr_disable = false;
1522 
1523 	switch (ps_type) {
1524 	case BTC_PS_WIFI_NATIVE:
1525 		/* recover to original 32k low power setting */
1526 		low_pwr_disable = false;
1527 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1528 				   &low_pwr_disable);
1529 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1530 		coex_sta->force_lps_on = false;
1531 		break;
1532 	case BTC_PS_LPS_ON:
1533 		btc8723b2ant_ps_tdma_check_for_power_save_state(btcoexist,
1534 								true);
1535 		btc8723b2ant_lps_rpwm(btcoexist, NORMAL_EXEC, lps_val,
1536 				      rpwm_val);
1537 		/* when coex force to enter LPS, do not enter 32k low power */
1538 		low_pwr_disable = true;
1539 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1540 				   &low_pwr_disable);
1541 		/* power save must executed before psTdma */
1542 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1543 		coex_sta->force_lps_on = true;
1544 		break;
1545 	case BTC_PS_LPS_OFF:
1546 		btc8723b2ant_ps_tdma_check_for_power_save_state(btcoexist,
1547 								false);
1548 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1549 		coex_sta->force_lps_on = false;
1550 		break;
1551 	default:
1552 		break;
1553 	}
1554 }
1555 
1556 static void btc8723b2ant_coex_alloff(struct btc_coexist *btcoexist)
1557 {
1558 	/* fw all off */
1559 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1560 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1561 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1562 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1563 
1564 	/* sw all off */
1565 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1566 
1567 	/* hw all off */
1568 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
1569 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1570 }
1571 
1572 static void btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
1573 {
1574 	/* force to reset coex mechanism*/
1575 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1576 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1577 
1578 	btc8723b2ant_ps_tdma(btcoexist, FORCE_EXEC, false, 1);
1579 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1580 	btc8723b2ant_dec_bt_pwr(btcoexist, FORCE_EXEC, 0);
1581 
1582 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1583 
1584 	coex_sta->pop_event_cnt = 0;
1585 }
1586 
1587 static void btc8723b2ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1588 {
1589 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1590 	bool wifi_connected = false;
1591 	bool low_pwr_disable = true;
1592 	bool scan = false, link = false, roam = false;
1593 
1594 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1595 			   &low_pwr_disable);
1596 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1597 			   &wifi_connected);
1598 
1599 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1600 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1601 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1602 
1603 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1604 
1605 	if (coex_sta->bt_abnormal_scan) {
1606 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 23);
1607 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
1608 	} else if (scan || link || roam) {
1609 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1610 			 "[BTCoex], Wifi link process + BT Inq/Page!!\n");
1611 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1612 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1613 	} else if (wifi_connected) {
1614 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1615 			 "[BTCoex], Wifi connected + BT Inq/Page!!\n");
1616 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1617 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1618 	} else {
1619 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1620 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1621 	}
1622 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1623 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1624 
1625 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1626 }
1627 
1628 static void btc8723b2ant_action_wifi_link_process(struct btc_coexist
1629 						     *btcoexist)
1630 {
1631 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1632 	u32 u32tmp;
1633 	u8 u8tmpa, u8tmpb;
1634 
1635 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1636 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1637 
1638 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1639 
1640 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
1641 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
1642 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x76e);
1643 
1644 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1645 		 "[BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x76e = 0x%x\n",
1646 		 u32tmp, u8tmpa, u8tmpb);
1647 }
1648 
1649 static bool btc8723b2ant_action_wifi_idle_process(struct btc_coexist *btcoexist)
1650 {
1651 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1652 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
1653 	u8 ap_num = 0;
1654 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
1655 		 coex_dm->switch_thres_offset - coex_dm->switch_thres_offset;
1656 
1657 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
1658 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
1659 							tmp, 0);
1660 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
1661 	      coex_dm->switch_thres_offset - coex_dm->switch_thres_offset;
1662 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
1663 
1664 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
1665 
1666 	/* office environment */
1667 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && (coex_sta->hid_exist) &&
1668 	    (coex_sta->a2dp_exist)) {
1669 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1670 			 "[BTCoex], Wifi  idle process for BT HID+A2DP exist!!\n");
1671 
1672 		btc8723b2ant_dac_swing(btcoexist, NORMAL_EXEC, true, 0x6);
1673 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1674 
1675 		/* sw all off */
1676 		btc8723b2ant_sw_mechanism(btcoexist, false, false, false,
1677 					  false);
1678 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1679 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1680 					      0x0, 0x0);
1681 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1682 
1683 		return true;
1684 	}
1685 
1686 	btc8723b2ant_dac_swing(btcoexist, NORMAL_EXEC, true, 0x18);
1687 	return false;
1688 }
1689 
1690 static bool btc8723b2ant_is_common_action(struct btc_coexist *btcoexist)
1691 {
1692 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1693 	bool common = false, wifi_connected = false;
1694 	bool wifi_busy = false;
1695 	bool bt_hs_on = false, low_pwr_disable = false;
1696 
1697 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1698 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1699 			   &wifi_connected);
1700 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1701 
1702 	if (!wifi_connected) {
1703 		low_pwr_disable = false;
1704 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1705 				   &low_pwr_disable);
1706 		btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1707 					false, false, 0x8);
1708 
1709 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1710 			 "[BTCoex], Wifi non-connected idle!!\n");
1711 
1712 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
1713 					  0x0);
1714 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1715 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1716 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1717 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1718 
1719 		btc8723b2ant_sw_mechanism(btcoexist, false, false, false,
1720 					  false);
1721 
1722 		common = true;
1723 	} else {
1724 		if (BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1725 		    coex_dm->bt_status) {
1726 			low_pwr_disable = false;
1727 			btcoexist->btc_set(btcoexist,
1728 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1729 					   &low_pwr_disable);
1730 			btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1731 						false, false, 0x8);
1732 
1733 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1734 				 "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1735 
1736 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1737 						  0xfffff, 0x0);
1738 			btc8723b2ant_coex_table_with_type(btcoexist,
1739 							  NORMAL_EXEC, 0);
1740 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1741 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1742 						      0xb);
1743 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1744 
1745 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
1746 						  false, false);
1747 
1748 			common = true;
1749 		} else if (BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE ==
1750 			   coex_dm->bt_status) {
1751 			low_pwr_disable = true;
1752 			btcoexist->btc_set(btcoexist,
1753 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1754 					   &low_pwr_disable);
1755 
1756 			if (bt_hs_on)
1757 				return false;
1758 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1759 				 "[BTCoex], Wifi connected + BT connected-idle!!\n");
1760 			btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1761 						false, false, 0x8);
1762 
1763 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1764 						  0xfffff, 0x0);
1765 			btc8723b2ant_coex_table_with_type(btcoexist,
1766 							  NORMAL_EXEC, 0);
1767 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1768 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1769 						      0xb);
1770 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1771 
1772 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
1773 						  false, false);
1774 
1775 			common = true;
1776 		} else {
1777 			low_pwr_disable = true;
1778 			btcoexist->btc_set(btcoexist,
1779 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1780 					   &low_pwr_disable);
1781 
1782 			if (wifi_busy) {
1783 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1784 					 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1785 				common = false;
1786 			} else {
1787 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1788 					 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1789 
1790 				common =
1791 				    btc8723b2ant_action_wifi_idle_process(
1792 						btcoexist);
1793 			}
1794 		}
1795 	}
1796 
1797 	return common;
1798 }
1799 
1800 static void btc8723b2ant_tdma_duration_adjust(struct btc_coexist *btcoexist,
1801 					  bool sco_hid, bool tx_pause,
1802 					  u8 max_interval)
1803 {
1804 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1805 	static s32 up, dn, m, n, wait_count;
1806 	/*0: no change, +1: increase WiFi duration, -1: decrease WiFi duration*/
1807 	s32 result;
1808 	u8 retry_count = 0;
1809 
1810 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1811 		 "[BTCoex], TdmaDurationAdjust()\n");
1812 
1813 	if (!coex_dm->auto_tdma_adjust) {
1814 		coex_dm->auto_tdma_adjust = true;
1815 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1816 			 "[BTCoex], first run TdmaDurationAdjust()!!\n");
1817 		if (sco_hid) {
1818 			if (tx_pause) {
1819 				if (max_interval == 1) {
1820 					btc8723b2ant_ps_tdma(btcoexist,
1821 							     NORMAL_EXEC,
1822 							     true, 13);
1823 					coex_dm->ps_tdma_du_adj_type = 13;
1824 				} else if (max_interval == 2) {
1825 					btc8723b2ant_ps_tdma(btcoexist,
1826 							     NORMAL_EXEC,
1827 							     true, 14);
1828 					coex_dm->ps_tdma_du_adj_type = 14;
1829 				} else if (max_interval == 3) {
1830 					btc8723b2ant_ps_tdma(btcoexist,
1831 							     NORMAL_EXEC,
1832 							     true, 15);
1833 					coex_dm->ps_tdma_du_adj_type = 15;
1834 				} else {
1835 					btc8723b2ant_ps_tdma(btcoexist,
1836 							     NORMAL_EXEC,
1837 							     true, 15);
1838 					coex_dm->ps_tdma_du_adj_type = 15;
1839 				}
1840 			} else {
1841 				if (max_interval == 1) {
1842 					btc8723b2ant_ps_tdma(btcoexist,
1843 							     NORMAL_EXEC,
1844 							     true, 9);
1845 					coex_dm->ps_tdma_du_adj_type = 9;
1846 				} else if (max_interval == 2) {
1847 					btc8723b2ant_ps_tdma(btcoexist,
1848 							     NORMAL_EXEC,
1849 							     true, 10);
1850 					coex_dm->ps_tdma_du_adj_type = 10;
1851 				} else if (max_interval == 3) {
1852 					btc8723b2ant_ps_tdma(btcoexist,
1853 							     NORMAL_EXEC,
1854 						     true, 11);
1855 					coex_dm->ps_tdma_du_adj_type = 11;
1856 				} else {
1857 					btc8723b2ant_ps_tdma(btcoexist,
1858 							     NORMAL_EXEC,
1859 							     true, 11);
1860 					coex_dm->ps_tdma_du_adj_type = 11;
1861 				}
1862 			}
1863 		} else {
1864 			if (tx_pause) {
1865 				if (max_interval == 1) {
1866 					btc8723b2ant_ps_tdma(btcoexist,
1867 							     NORMAL_EXEC,
1868 							     true, 5);
1869 					coex_dm->ps_tdma_du_adj_type = 5;
1870 				} else if (max_interval == 2) {
1871 					btc8723b2ant_ps_tdma(btcoexist,
1872 							     NORMAL_EXEC,
1873 							     true, 6);
1874 					coex_dm->ps_tdma_du_adj_type = 6;
1875 				} else if (max_interval == 3) {
1876 					btc8723b2ant_ps_tdma(btcoexist,
1877 							     NORMAL_EXEC,
1878 							     true, 7);
1879 					coex_dm->ps_tdma_du_adj_type = 7;
1880 				} else {
1881 					btc8723b2ant_ps_tdma(btcoexist,
1882 							     NORMAL_EXEC,
1883 							     true, 7);
1884 					coex_dm->ps_tdma_du_adj_type = 7;
1885 				}
1886 			} else {
1887 				if (max_interval == 1) {
1888 					btc8723b2ant_ps_tdma(btcoexist,
1889 							     NORMAL_EXEC,
1890 							     true, 1);
1891 					coex_dm->ps_tdma_du_adj_type = 1;
1892 				} else if (max_interval == 2) {
1893 					btc8723b2ant_ps_tdma(btcoexist,
1894 							     NORMAL_EXEC,
1895 							     true, 2);
1896 					coex_dm->ps_tdma_du_adj_type = 2;
1897 				} else if (max_interval == 3) {
1898 					btc8723b2ant_ps_tdma(btcoexist,
1899 							     NORMAL_EXEC,
1900 							     true, 3);
1901 					coex_dm->ps_tdma_du_adj_type = 3;
1902 				} else {
1903 					btc8723b2ant_ps_tdma(btcoexist,
1904 							     NORMAL_EXEC,
1905 							     true, 3);
1906 					coex_dm->ps_tdma_du_adj_type = 3;
1907 				}
1908 			}
1909 		}
1910 
1911 		up = 0;
1912 		dn = 0;
1913 		m = 1;
1914 		n = 3;
1915 		result = 0;
1916 		wait_count = 0;
1917 	} else {
1918 		/*accquire the BT TRx retry count from BT_Info byte2*/
1919 		retry_count = coex_sta->bt_retry_cnt;
1920 
1921 		if ((coex_sta->low_priority_tx) > 1050 ||
1922 		    (coex_sta->low_priority_rx) > 1250)
1923 			retry_count++;
1924 
1925 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1926 			 "[BTCoex], retry_count = %d\n", retry_count);
1927 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1928 			 "[BTCoex], up=%d, dn=%d, m=%d, n=%d, wait_count=%d\n",
1929 			 up, dn, m, n, wait_count);
1930 		result = 0;
1931 		wait_count++;
1932 		 /* no retry in the last 2-second duration*/
1933 		if (retry_count == 0) {
1934 			up++;
1935 			dn--;
1936 
1937 			if (dn <= 0)
1938 				dn = 0;
1939 
1940 			if (up >= n) {
1941 				/* if retry count during continuous n*2
1942 				 * seconds is 0, enlarge WiFi duration
1943 				 */
1944 				wait_count = 0;
1945 				n = 3;
1946 				up = 0;
1947 				dn = 0;
1948 				result = 1;
1949 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1950 					 "[BTCoex], Increase wifi duration!!\n");
1951 			} /* <=3 retry in the last 2-second duration*/
1952 		} else if (retry_count <= 3) {
1953 			up--;
1954 			dn++;
1955 
1956 			if (up <= 0)
1957 				up = 0;
1958 
1959 			if (dn == 2) {
1960 				/* if continuous 2 retry count(every 2
1961 				 * seconds) >0 and < 3, reduce WiFi duration
1962 				 */
1963 				if (wait_count <= 2)
1964 					/* avoid loop between the two levels */
1965 					m++;
1966 				else
1967 					m = 1;
1968 
1969 				if (m >= 20)
1970 					/* maximum of m = 20 ' will recheck if
1971 					 * need to adjust wifi duration in
1972 					 * maximum time interval 120 seconds
1973 					 */
1974 					m = 20;
1975 
1976 				n = 3 * m;
1977 				up = 0;
1978 				dn = 0;
1979 				wait_count = 0;
1980 				result = -1;
1981 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1982 					 "[BTCoex], Decrease wifi duration for retry_counter<3!!\n");
1983 			}
1984 		} else {
1985 			/* retry count > 3, once retry count > 3, to reduce
1986 			 *  WiFi duration
1987 			 */
1988 			if (wait_count == 1)
1989 				/* to avoid loop between the two levels */
1990 				m++;
1991 			else
1992 				m = 1;
1993 
1994 			if (m >= 20)
1995 				/* maximum of m = 20 ' will recheck if need to
1996 				 * adjust wifi duration in maximum time interval
1997 				 * 120 seconds
1998 				 */
1999 				m = 20;
2000 
2001 			n = 3 * m;
2002 			up = 0;
2003 			dn = 0;
2004 			wait_count = 0;
2005 			result = -1;
2006 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2007 				 "[BTCoex], Decrease wifi duration for retry_counter>3!!\n");
2008 		}
2009 
2010 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2011 			 "[BTCoex], max Interval = %d\n", max_interval);
2012 		if (max_interval == 1) {
2013 			if (tx_pause) {
2014 				if (coex_dm->cur_ps_tdma == 71) {
2015 					btc8723b2ant_ps_tdma(btcoexist,
2016 							     NORMAL_EXEC,
2017 							     true, 5);
2018 					coex_dm->ps_tdma_du_adj_type = 5;
2019 				} else if (coex_dm->cur_ps_tdma == 1) {
2020 					btc8723b2ant_ps_tdma(btcoexist,
2021 							     NORMAL_EXEC,
2022 							     true, 5);
2023 					coex_dm->ps_tdma_du_adj_type = 5;
2024 				} else if (coex_dm->cur_ps_tdma == 2) {
2025 					btc8723b2ant_ps_tdma(btcoexist,
2026 							     NORMAL_EXEC,
2027 							     true, 6);
2028 					coex_dm->ps_tdma_du_adj_type = 6;
2029 				} else if (coex_dm->cur_ps_tdma == 3) {
2030 					btc8723b2ant_ps_tdma(btcoexist,
2031 							     NORMAL_EXEC,
2032 							     true, 7);
2033 					coex_dm->ps_tdma_du_adj_type = 7;
2034 				} else if (coex_dm->cur_ps_tdma == 4) {
2035 					btc8723b2ant_ps_tdma(btcoexist,
2036 							     NORMAL_EXEC,
2037 							     true, 8);
2038 					coex_dm->ps_tdma_du_adj_type = 8;
2039 				}
2040 				if (coex_dm->cur_ps_tdma == 9) {
2041 					btc8723b2ant_ps_tdma(btcoexist,
2042 							     NORMAL_EXEC,
2043 							     true, 13);
2044 					coex_dm->ps_tdma_du_adj_type = 13;
2045 				} else if (coex_dm->cur_ps_tdma == 10) {
2046 					btc8723b2ant_ps_tdma(btcoexist,
2047 							     NORMAL_EXEC,
2048 							     true, 14);
2049 					coex_dm->ps_tdma_du_adj_type = 14;
2050 				} else if (coex_dm->cur_ps_tdma == 11) {
2051 					btc8723b2ant_ps_tdma(btcoexist,
2052 							     NORMAL_EXEC,
2053 							     true, 15);
2054 					coex_dm->ps_tdma_du_adj_type = 15;
2055 				} else if (coex_dm->cur_ps_tdma == 12) {
2056 					btc8723b2ant_ps_tdma(btcoexist,
2057 							     NORMAL_EXEC,
2058 							     true, 16);
2059 					coex_dm->ps_tdma_du_adj_type = 16;
2060 				}
2061 
2062 				if (result == -1) {
2063 					if (coex_dm->cur_ps_tdma == 5) {
2064 						btc8723b2ant_ps_tdma(
2065 							btcoexist, NORMAL_EXEC,
2066 							true, 6);
2067 						coex_dm->ps_tdma_du_adj_type =
2068 							6;
2069 					} else if (coex_dm->cur_ps_tdma == 6) {
2070 						btc8723b2ant_ps_tdma(
2071 							btcoexist, NORMAL_EXEC,
2072 							true, 7);
2073 						coex_dm->ps_tdma_du_adj_type =
2074 							7;
2075 					} else if (coex_dm->cur_ps_tdma == 7) {
2076 						btc8723b2ant_ps_tdma(
2077 							btcoexist, NORMAL_EXEC,
2078 							true, 8);
2079 						coex_dm->ps_tdma_du_adj_type =
2080 							8;
2081 					} else if (coex_dm->cur_ps_tdma == 13) {
2082 						btc8723b2ant_ps_tdma(
2083 							btcoexist, NORMAL_EXEC,
2084 							true, 14);
2085 						coex_dm->ps_tdma_du_adj_type =
2086 							14;
2087 					} else if (coex_dm->cur_ps_tdma == 14) {
2088 						btc8723b2ant_ps_tdma(
2089 							btcoexist, NORMAL_EXEC,
2090 							true, 15);
2091 						coex_dm->ps_tdma_du_adj_type =
2092 							15;
2093 					} else if (coex_dm->cur_ps_tdma == 15) {
2094 						btc8723b2ant_ps_tdma(
2095 							btcoexist, NORMAL_EXEC,
2096 							true, 16);
2097 						coex_dm->ps_tdma_du_adj_type =
2098 							16;
2099 					}
2100 				} else if (result == 1) {
2101 					if (coex_dm->cur_ps_tdma == 8) {
2102 						btc8723b2ant_ps_tdma(
2103 							btcoexist, NORMAL_EXEC,
2104 							true, 7);
2105 						coex_dm->ps_tdma_du_adj_type =
2106 							7;
2107 					} else if (coex_dm->cur_ps_tdma == 7) {
2108 						btc8723b2ant_ps_tdma(
2109 							btcoexist, NORMAL_EXEC,
2110 							true, 6);
2111 						coex_dm->ps_tdma_du_adj_type =
2112 							6;
2113 					} else if (coex_dm->cur_ps_tdma == 6) {
2114 						btc8723b2ant_ps_tdma(
2115 							btcoexist, NORMAL_EXEC,
2116 							true, 5);
2117 						coex_dm->ps_tdma_du_adj_type =
2118 							5;
2119 					} else if (coex_dm->cur_ps_tdma == 16) {
2120 						btc8723b2ant_ps_tdma(
2121 							btcoexist, NORMAL_EXEC,
2122 							true, 15);
2123 						coex_dm->ps_tdma_du_adj_type =
2124 							15;
2125 					} else if (coex_dm->cur_ps_tdma == 15) {
2126 						btc8723b2ant_ps_tdma(
2127 							btcoexist, NORMAL_EXEC,
2128 							true, 14);
2129 						coex_dm->ps_tdma_du_adj_type =
2130 							14;
2131 					} else if (coex_dm->cur_ps_tdma == 14) {
2132 						btc8723b2ant_ps_tdma(
2133 							btcoexist, NORMAL_EXEC,
2134 							true, 13);
2135 						coex_dm->ps_tdma_du_adj_type =
2136 							13;
2137 					}
2138 				}
2139 			} else {
2140 				if (coex_dm->cur_ps_tdma == 5) {
2141 					btc8723b2ant_ps_tdma(btcoexist,
2142 							     NORMAL_EXEC,
2143 							     true, 71);
2144 					coex_dm->ps_tdma_du_adj_type = 71;
2145 				} else if (coex_dm->cur_ps_tdma == 6) {
2146 					btc8723b2ant_ps_tdma(btcoexist,
2147 							     NORMAL_EXEC,
2148 							     true, 2);
2149 					coex_dm->ps_tdma_du_adj_type = 2;
2150 				} else if (coex_dm->cur_ps_tdma == 7) {
2151 					btc8723b2ant_ps_tdma(btcoexist,
2152 							     NORMAL_EXEC,
2153 							     true, 3);
2154 					coex_dm->ps_tdma_du_adj_type = 3;
2155 				} else if (coex_dm->cur_ps_tdma == 8) {
2156 					btc8723b2ant_ps_tdma(btcoexist,
2157 							     NORMAL_EXEC,
2158 							     true, 4);
2159 					coex_dm->ps_tdma_du_adj_type = 4;
2160 				}
2161 				if (coex_dm->cur_ps_tdma == 13) {
2162 					btc8723b2ant_ps_tdma(btcoexist,
2163 							     NORMAL_EXEC,
2164 							     true, 9);
2165 					coex_dm->ps_tdma_du_adj_type = 9;
2166 				} else if (coex_dm->cur_ps_tdma == 14) {
2167 					btc8723b2ant_ps_tdma(btcoexist,
2168 							     NORMAL_EXEC,
2169 							     true, 10);
2170 					coex_dm->ps_tdma_du_adj_type = 10;
2171 				} else if (coex_dm->cur_ps_tdma == 15) {
2172 					btc8723b2ant_ps_tdma(btcoexist,
2173 							     NORMAL_EXEC,
2174 							     true, 11);
2175 					coex_dm->ps_tdma_du_adj_type = 11;
2176 				} else if (coex_dm->cur_ps_tdma == 16) {
2177 					btc8723b2ant_ps_tdma(btcoexist,
2178 							     NORMAL_EXEC,
2179 							     true, 12);
2180 					coex_dm->ps_tdma_du_adj_type = 12;
2181 				}
2182 
2183 				if (result == -1) {
2184 					if (coex_dm->cur_ps_tdma == 71) {
2185 						btc8723b2ant_ps_tdma(
2186 							btcoexist, NORMAL_EXEC,
2187 							true, 1);
2188 						coex_dm->ps_tdma_du_adj_type =
2189 							1;
2190 					} else if (coex_dm->cur_ps_tdma == 1) {
2191 						btc8723b2ant_ps_tdma(
2192 							btcoexist, NORMAL_EXEC,
2193 							true, 2);
2194 						coex_dm->ps_tdma_du_adj_type =
2195 							2;
2196 					} else if (coex_dm->cur_ps_tdma == 2) {
2197 						btc8723b2ant_ps_tdma(
2198 							btcoexist, NORMAL_EXEC,
2199 							true, 3);
2200 						coex_dm->ps_tdma_du_adj_type =
2201 							3;
2202 					} else if (coex_dm->cur_ps_tdma == 3) {
2203 						btc8723b2ant_ps_tdma(
2204 							btcoexist, NORMAL_EXEC,
2205 							true, 4);
2206 						coex_dm->ps_tdma_du_adj_type =
2207 							4;
2208 					} else if (coex_dm->cur_ps_tdma == 9) {
2209 						btc8723b2ant_ps_tdma(
2210 							btcoexist, NORMAL_EXEC,
2211 							true, 10);
2212 						coex_dm->ps_tdma_du_adj_type =
2213 							10;
2214 					} else if (coex_dm->cur_ps_tdma == 10) {
2215 						btc8723b2ant_ps_tdma(
2216 							btcoexist, NORMAL_EXEC,
2217 							true, 11);
2218 						coex_dm->ps_tdma_du_adj_type =
2219 							11;
2220 					} else if (coex_dm->cur_ps_tdma == 11) {
2221 						btc8723b2ant_ps_tdma(
2222 							btcoexist, NORMAL_EXEC,
2223 							true, 12);
2224 						coex_dm->ps_tdma_du_adj_type =
2225 							12;
2226 					}
2227 				} else if (result == 1) {
2228 					if (coex_dm->cur_ps_tdma == 4) {
2229 						btc8723b2ant_ps_tdma(
2230 							btcoexist, NORMAL_EXEC,
2231 							true, 3);
2232 						coex_dm->ps_tdma_du_adj_type =
2233 							3;
2234 					} else if (coex_dm->cur_ps_tdma == 3) {
2235 						btc8723b2ant_ps_tdma(
2236 							btcoexist, NORMAL_EXEC,
2237 							true, 2);
2238 						coex_dm->ps_tdma_du_adj_type =
2239 							2;
2240 					} else if (coex_dm->cur_ps_tdma == 2) {
2241 						btc8723b2ant_ps_tdma(
2242 							btcoexist, NORMAL_EXEC,
2243 							true, 1);
2244 						coex_dm->ps_tdma_du_adj_type =
2245 							1;
2246 					} else if (coex_dm->cur_ps_tdma == 1) {
2247 						btc8723b2ant_ps_tdma(
2248 							btcoexist, NORMAL_EXEC,
2249 							true, 71);
2250 						coex_dm->ps_tdma_du_adj_type =
2251 							71;
2252 					} else if (coex_dm->cur_ps_tdma == 12) {
2253 						btc8723b2ant_ps_tdma(
2254 							btcoexist, NORMAL_EXEC,
2255 							true, 11);
2256 						coex_dm->ps_tdma_du_adj_type =
2257 							11;
2258 					} else if (coex_dm->cur_ps_tdma == 11) {
2259 						btc8723b2ant_ps_tdma(
2260 							btcoexist, NORMAL_EXEC,
2261 							true, 10);
2262 						coex_dm->ps_tdma_du_adj_type =
2263 							10;
2264 					} else if (coex_dm->cur_ps_tdma == 10) {
2265 						btc8723b2ant_ps_tdma(
2266 							btcoexist, NORMAL_EXEC,
2267 							true, 9);
2268 						coex_dm->ps_tdma_du_adj_type =
2269 							9;
2270 					}
2271 				}
2272 			}
2273 		} else if (max_interval == 2) {
2274 			if (tx_pause) {
2275 				if (coex_dm->cur_ps_tdma == 1) {
2276 					btc8723b2ant_ps_tdma(btcoexist,
2277 							     NORMAL_EXEC,
2278 							     true, 6);
2279 					coex_dm->ps_tdma_du_adj_type = 6;
2280 				} else if (coex_dm->cur_ps_tdma == 2) {
2281 					btc8723b2ant_ps_tdma(btcoexist,
2282 							     NORMAL_EXEC,
2283 							     true, 6);
2284 					coex_dm->ps_tdma_du_adj_type = 6;
2285 				} else if (coex_dm->cur_ps_tdma == 3) {
2286 					btc8723b2ant_ps_tdma(btcoexist,
2287 							     NORMAL_EXEC,
2288 							     true, 7);
2289 					coex_dm->ps_tdma_du_adj_type = 7;
2290 				} else if (coex_dm->cur_ps_tdma == 4) {
2291 					btc8723b2ant_ps_tdma(btcoexist,
2292 							     NORMAL_EXEC,
2293 							     true, 8);
2294 					coex_dm->ps_tdma_du_adj_type = 8;
2295 				}
2296 				if (coex_dm->cur_ps_tdma == 9) {
2297 					btc8723b2ant_ps_tdma(btcoexist,
2298 							     NORMAL_EXEC,
2299 							     true, 14);
2300 					coex_dm->ps_tdma_du_adj_type = 14;
2301 				} else if (coex_dm->cur_ps_tdma == 10) {
2302 					btc8723b2ant_ps_tdma(btcoexist,
2303 							     NORMAL_EXEC,
2304 							     true, 14);
2305 					coex_dm->ps_tdma_du_adj_type = 14;
2306 				} else if (coex_dm->cur_ps_tdma == 11) {
2307 					btc8723b2ant_ps_tdma(btcoexist,
2308 							     NORMAL_EXEC,
2309 							     true, 15);
2310 					coex_dm->ps_tdma_du_adj_type = 15;
2311 				} else if (coex_dm->cur_ps_tdma == 12) {
2312 					btc8723b2ant_ps_tdma(btcoexist,
2313 							     NORMAL_EXEC,
2314 							     true, 16);
2315 					coex_dm->ps_tdma_du_adj_type = 16;
2316 				}
2317 				if (result == -1) {
2318 					if (coex_dm->cur_ps_tdma == 5) {
2319 						btc8723b2ant_ps_tdma(
2320 							btcoexist, NORMAL_EXEC,
2321 							true, 6);
2322 						coex_dm->ps_tdma_du_adj_type =
2323 							6;
2324 					} else if (coex_dm->cur_ps_tdma == 6) {
2325 						btc8723b2ant_ps_tdma(
2326 							btcoexist, NORMAL_EXEC,
2327 							true, 7);
2328 						coex_dm->ps_tdma_du_adj_type =
2329 							7;
2330 					} else if (coex_dm->cur_ps_tdma == 7) {
2331 						btc8723b2ant_ps_tdma(
2332 							btcoexist, NORMAL_EXEC,
2333 							true, 8);
2334 						coex_dm->ps_tdma_du_adj_type =
2335 							8;
2336 					} else if (coex_dm->cur_ps_tdma == 13) {
2337 						btc8723b2ant_ps_tdma(
2338 							btcoexist, NORMAL_EXEC,
2339 							true, 14);
2340 						coex_dm->ps_tdma_du_adj_type =
2341 							14;
2342 					} else if (coex_dm->cur_ps_tdma == 14) {
2343 						btc8723b2ant_ps_tdma(
2344 							btcoexist, NORMAL_EXEC,
2345 							true, 15);
2346 						coex_dm->ps_tdma_du_adj_type =
2347 							15;
2348 					} else if (coex_dm->cur_ps_tdma == 15) {
2349 						btc8723b2ant_ps_tdma(
2350 							btcoexist, NORMAL_EXEC,
2351 							true, 16);
2352 						coex_dm->ps_tdma_du_adj_type =
2353 							16;
2354 					}
2355 				} else if (result == 1) {
2356 					if (coex_dm->cur_ps_tdma == 8) {
2357 						btc8723b2ant_ps_tdma(
2358 							btcoexist, NORMAL_EXEC,
2359 							true, 7);
2360 						coex_dm->ps_tdma_du_adj_type =
2361 							7;
2362 					} else if (coex_dm->cur_ps_tdma == 7) {
2363 						btc8723b2ant_ps_tdma(
2364 							btcoexist, NORMAL_EXEC,
2365 							true, 6);
2366 						coex_dm->ps_tdma_du_adj_type =
2367 							6;
2368 					} else if (coex_dm->cur_ps_tdma == 6) {
2369 						btc8723b2ant_ps_tdma(
2370 							btcoexist, NORMAL_EXEC,
2371 							true, 6);
2372 						coex_dm->ps_tdma_du_adj_type =
2373 							6;
2374 					} else if (coex_dm->cur_ps_tdma == 16) {
2375 						btc8723b2ant_ps_tdma(
2376 							btcoexist, NORMAL_EXEC,
2377 							true, 15);
2378 						coex_dm->ps_tdma_du_adj_type =
2379 							15;
2380 					} else if (coex_dm->cur_ps_tdma == 15) {
2381 						btc8723b2ant_ps_tdma(
2382 							btcoexist, NORMAL_EXEC,
2383 							true, 14);
2384 						coex_dm->ps_tdma_du_adj_type =
2385 							14;
2386 					} else if (coex_dm->cur_ps_tdma == 14) {
2387 						btc8723b2ant_ps_tdma(
2388 							btcoexist, NORMAL_EXEC,
2389 							true, 14);
2390 						coex_dm->ps_tdma_du_adj_type =
2391 							14;
2392 					}
2393 				}
2394 			} else {
2395 				if (coex_dm->cur_ps_tdma == 5) {
2396 					btc8723b2ant_ps_tdma(btcoexist,
2397 							     NORMAL_EXEC,
2398 							     true, 2);
2399 					coex_dm->ps_tdma_du_adj_type = 2;
2400 				} else if (coex_dm->cur_ps_tdma == 6) {
2401 					btc8723b2ant_ps_tdma(btcoexist,
2402 							     NORMAL_EXEC,
2403 							     true, 2);
2404 					coex_dm->ps_tdma_du_adj_type = 2;
2405 				} else if (coex_dm->cur_ps_tdma == 7) {
2406 					btc8723b2ant_ps_tdma(btcoexist,
2407 							     NORMAL_EXEC,
2408 							     true, 3);
2409 					coex_dm->ps_tdma_du_adj_type = 3;
2410 				} else if (coex_dm->cur_ps_tdma == 8) {
2411 					btc8723b2ant_ps_tdma(btcoexist,
2412 							     NORMAL_EXEC,
2413 							     true, 4);
2414 					coex_dm->ps_tdma_du_adj_type = 4;
2415 				}
2416 				if (coex_dm->cur_ps_tdma == 13) {
2417 					btc8723b2ant_ps_tdma(btcoexist,
2418 							     NORMAL_EXEC,
2419 							     true, 10);
2420 					coex_dm->ps_tdma_du_adj_type = 10;
2421 				} else if (coex_dm->cur_ps_tdma == 14) {
2422 					btc8723b2ant_ps_tdma(btcoexist,
2423 							     NORMAL_EXEC,
2424 							     true, 10);
2425 					coex_dm->ps_tdma_du_adj_type = 10;
2426 				} else if (coex_dm->cur_ps_tdma == 15) {
2427 					btc8723b2ant_ps_tdma(btcoexist,
2428 							     NORMAL_EXEC,
2429 							     true, 11);
2430 					coex_dm->ps_tdma_du_adj_type = 11;
2431 				} else if (coex_dm->cur_ps_tdma == 16) {
2432 					btc8723b2ant_ps_tdma(btcoexist,
2433 							     NORMAL_EXEC,
2434 							     true, 12);
2435 					coex_dm->ps_tdma_du_adj_type = 12;
2436 				}
2437 				if (result == -1) {
2438 					if (coex_dm->cur_ps_tdma == 1) {
2439 						btc8723b2ant_ps_tdma(
2440 							btcoexist, NORMAL_EXEC,
2441 							true, 2);
2442 						coex_dm->ps_tdma_du_adj_type =
2443 							2;
2444 					} else if (coex_dm->cur_ps_tdma == 2) {
2445 						btc8723b2ant_ps_tdma(
2446 							btcoexist, NORMAL_EXEC,
2447 							true, 3);
2448 						coex_dm->ps_tdma_du_adj_type =
2449 							3;
2450 					} else if (coex_dm->cur_ps_tdma == 3) {
2451 						btc8723b2ant_ps_tdma(
2452 							btcoexist, NORMAL_EXEC,
2453 							true, 4);
2454 						coex_dm->ps_tdma_du_adj_type =
2455 							4;
2456 					} else if (coex_dm->cur_ps_tdma == 9) {
2457 						btc8723b2ant_ps_tdma(
2458 							btcoexist, NORMAL_EXEC,
2459 							true, 10);
2460 						coex_dm->ps_tdma_du_adj_type =
2461 							10;
2462 					} else if (coex_dm->cur_ps_tdma == 10) {
2463 						btc8723b2ant_ps_tdma(
2464 							btcoexist, NORMAL_EXEC,
2465 							true, 11);
2466 						coex_dm->ps_tdma_du_adj_type =
2467 							11;
2468 					} else if (coex_dm->cur_ps_tdma == 11) {
2469 						btc8723b2ant_ps_tdma(
2470 							btcoexist, NORMAL_EXEC,
2471 							true, 12);
2472 						coex_dm->ps_tdma_du_adj_type =
2473 							12;
2474 					}
2475 				} else if (result == 1) {
2476 					if (coex_dm->cur_ps_tdma == 4) {
2477 						btc8723b2ant_ps_tdma(
2478 							btcoexist, NORMAL_EXEC,
2479 							true, 3);
2480 						coex_dm->ps_tdma_du_adj_type =
2481 							3;
2482 					} else if (coex_dm->cur_ps_tdma == 3) {
2483 						btc8723b2ant_ps_tdma(
2484 							btcoexist, NORMAL_EXEC,
2485 							true, 2);
2486 						coex_dm->ps_tdma_du_adj_type =
2487 							2;
2488 					} else if (coex_dm->cur_ps_tdma == 2) {
2489 						btc8723b2ant_ps_tdma(
2490 							btcoexist, NORMAL_EXEC,
2491 							true, 2);
2492 						coex_dm->ps_tdma_du_adj_type =
2493 							2;
2494 					} else if (coex_dm->cur_ps_tdma == 12) {
2495 						btc8723b2ant_ps_tdma(
2496 							btcoexist, NORMAL_EXEC,
2497 							true, 11);
2498 						coex_dm->ps_tdma_du_adj_type =
2499 							11;
2500 					} else if (coex_dm->cur_ps_tdma == 11) {
2501 						btc8723b2ant_ps_tdma(
2502 							btcoexist, NORMAL_EXEC,
2503 							true, 10);
2504 						coex_dm->ps_tdma_du_adj_type =
2505 							10;
2506 					} else if (coex_dm->cur_ps_tdma == 10) {
2507 						btc8723b2ant_ps_tdma(
2508 							btcoexist, NORMAL_EXEC,
2509 							true, 10);
2510 						coex_dm->ps_tdma_du_adj_type =
2511 							10;
2512 					}
2513 				}
2514 			}
2515 		} else if (max_interval == 3) {
2516 			if (tx_pause) {
2517 				if (coex_dm->cur_ps_tdma == 1) {
2518 					btc8723b2ant_ps_tdma(btcoexist,
2519 							     NORMAL_EXEC,
2520 							     true, 7);
2521 					coex_dm->ps_tdma_du_adj_type = 7;
2522 				} else if (coex_dm->cur_ps_tdma == 2) {
2523 					btc8723b2ant_ps_tdma(btcoexist,
2524 							     NORMAL_EXEC,
2525 							     true, 7);
2526 					coex_dm->ps_tdma_du_adj_type = 7;
2527 				} else if (coex_dm->cur_ps_tdma == 3) {
2528 					btc8723b2ant_ps_tdma(btcoexist,
2529 							     NORMAL_EXEC,
2530 							     true, 7);
2531 					coex_dm->ps_tdma_du_adj_type = 7;
2532 				} else if (coex_dm->cur_ps_tdma == 4) {
2533 					btc8723b2ant_ps_tdma(btcoexist,
2534 							     NORMAL_EXEC,
2535 							     true, 8);
2536 					coex_dm->ps_tdma_du_adj_type = 8;
2537 				}
2538 				if (coex_dm->cur_ps_tdma == 9) {
2539 					btc8723b2ant_ps_tdma(btcoexist,
2540 							     NORMAL_EXEC,
2541 							     true, 15);
2542 					coex_dm->ps_tdma_du_adj_type = 15;
2543 				} else if (coex_dm->cur_ps_tdma == 10) {
2544 					btc8723b2ant_ps_tdma(btcoexist,
2545 							     NORMAL_EXEC,
2546 							     true, 15);
2547 					coex_dm->ps_tdma_du_adj_type = 15;
2548 				} else if (coex_dm->cur_ps_tdma == 11) {
2549 					btc8723b2ant_ps_tdma(btcoexist,
2550 							     NORMAL_EXEC,
2551 							     true, 15);
2552 					coex_dm->ps_tdma_du_adj_type = 15;
2553 				} else if (coex_dm->cur_ps_tdma == 12) {
2554 					btc8723b2ant_ps_tdma(btcoexist,
2555 							     NORMAL_EXEC,
2556 							     true, 16);
2557 					coex_dm->ps_tdma_du_adj_type = 16;
2558 				}
2559 				if (result == -1) {
2560 					if (coex_dm->cur_ps_tdma == 5) {
2561 						btc8723b2ant_ps_tdma(
2562 							btcoexist, NORMAL_EXEC,
2563 							true, 7);
2564 						coex_dm->ps_tdma_du_adj_type =
2565 							7;
2566 					} else if (coex_dm->cur_ps_tdma == 6) {
2567 						btc8723b2ant_ps_tdma(
2568 							btcoexist, NORMAL_EXEC,
2569 							true, 7);
2570 						coex_dm->ps_tdma_du_adj_type =
2571 							7;
2572 					} else if (coex_dm->cur_ps_tdma == 7) {
2573 						btc8723b2ant_ps_tdma(
2574 							btcoexist, NORMAL_EXEC,
2575 							true, 8);
2576 						coex_dm->ps_tdma_du_adj_type =
2577 							8;
2578 					} else if (coex_dm->cur_ps_tdma == 13) {
2579 						btc8723b2ant_ps_tdma(
2580 							btcoexist, NORMAL_EXEC,
2581 							true, 15);
2582 						coex_dm->ps_tdma_du_adj_type =
2583 							15;
2584 					} else if (coex_dm->cur_ps_tdma == 14) {
2585 						btc8723b2ant_ps_tdma(
2586 							btcoexist, NORMAL_EXEC,
2587 							true, 15);
2588 						coex_dm->ps_tdma_du_adj_type =
2589 							15;
2590 					} else if (coex_dm->cur_ps_tdma == 15) {
2591 						btc8723b2ant_ps_tdma(
2592 							btcoexist, NORMAL_EXEC,
2593 							true, 16);
2594 						coex_dm->ps_tdma_du_adj_type =
2595 							16;
2596 					}
2597 				} else if (result == 1) {
2598 					if (coex_dm->cur_ps_tdma == 8) {
2599 						btc8723b2ant_ps_tdma(
2600 							btcoexist, NORMAL_EXEC,
2601 							true, 7);
2602 						coex_dm->ps_tdma_du_adj_type =
2603 							7;
2604 					} else if (coex_dm->cur_ps_tdma == 7) {
2605 						btc8723b2ant_ps_tdma(
2606 							btcoexist, NORMAL_EXEC,
2607 							true, 7);
2608 						coex_dm->ps_tdma_du_adj_type =
2609 							7;
2610 					} else if (coex_dm->cur_ps_tdma == 6) {
2611 						btc8723b2ant_ps_tdma(
2612 							btcoexist, NORMAL_EXEC,
2613 							true, 7);
2614 						coex_dm->ps_tdma_du_adj_type =
2615 							7;
2616 					} else if (coex_dm->cur_ps_tdma == 16) {
2617 						btc8723b2ant_ps_tdma(
2618 							btcoexist, NORMAL_EXEC,
2619 							true, 15);
2620 						coex_dm->ps_tdma_du_adj_type =
2621 							15;
2622 					} else if (coex_dm->cur_ps_tdma == 15) {
2623 						btc8723b2ant_ps_tdma(
2624 							btcoexist, NORMAL_EXEC,
2625 							true, 15);
2626 						coex_dm->ps_tdma_du_adj_type =
2627 							15;
2628 					} else if (coex_dm->cur_ps_tdma == 14) {
2629 						btc8723b2ant_ps_tdma(
2630 							btcoexist, NORMAL_EXEC,
2631 							true, 15);
2632 						coex_dm->ps_tdma_du_adj_type =
2633 							15;
2634 					}
2635 				}
2636 			} else {
2637 				if (coex_dm->cur_ps_tdma == 5) {
2638 					btc8723b2ant_ps_tdma(btcoexist,
2639 							     NORMAL_EXEC,
2640 							     true, 3);
2641 					coex_dm->ps_tdma_du_adj_type = 3;
2642 				} else if (coex_dm->cur_ps_tdma == 6) {
2643 					btc8723b2ant_ps_tdma(btcoexist,
2644 							     NORMAL_EXEC,
2645 							     true, 3);
2646 					coex_dm->ps_tdma_du_adj_type = 3;
2647 				} else if (coex_dm->cur_ps_tdma == 7) {
2648 					btc8723b2ant_ps_tdma(btcoexist,
2649 							     NORMAL_EXEC,
2650 							     true, 3);
2651 					coex_dm->ps_tdma_du_adj_type = 3;
2652 				} else if (coex_dm->cur_ps_tdma == 8) {
2653 					btc8723b2ant_ps_tdma(btcoexist,
2654 							     NORMAL_EXEC,
2655 							     true, 4);
2656 					coex_dm->ps_tdma_du_adj_type = 4;
2657 				}
2658 				if (coex_dm->cur_ps_tdma == 13) {
2659 					btc8723b2ant_ps_tdma(btcoexist,
2660 							     NORMAL_EXEC,
2661 							     true, 11);
2662 					coex_dm->ps_tdma_du_adj_type = 11;
2663 				} else if (coex_dm->cur_ps_tdma == 14) {
2664 					btc8723b2ant_ps_tdma(btcoexist,
2665 							     NORMAL_EXEC,
2666 							     true, 11);
2667 					coex_dm->ps_tdma_du_adj_type = 11;
2668 				} else if (coex_dm->cur_ps_tdma == 15) {
2669 					btc8723b2ant_ps_tdma(btcoexist,
2670 							     NORMAL_EXEC,
2671 							     true, 11);
2672 					coex_dm->ps_tdma_du_adj_type = 11;
2673 				} else if (coex_dm->cur_ps_tdma == 16) {
2674 					btc8723b2ant_ps_tdma(btcoexist,
2675 							     NORMAL_EXEC,
2676 							     true, 12);
2677 					coex_dm->ps_tdma_du_adj_type = 12;
2678 				}
2679 				if (result == -1) {
2680 					if (coex_dm->cur_ps_tdma == 1) {
2681 						btc8723b2ant_ps_tdma(
2682 							btcoexist, NORMAL_EXEC,
2683 							true, 3);
2684 						coex_dm->ps_tdma_du_adj_type =
2685 							3;
2686 					} else if (coex_dm->cur_ps_tdma == 2) {
2687 						btc8723b2ant_ps_tdma(
2688 							btcoexist, NORMAL_EXEC,
2689 							true, 3);
2690 						coex_dm->ps_tdma_du_adj_type =
2691 							3;
2692 					} else if (coex_dm->cur_ps_tdma == 3) {
2693 						btc8723b2ant_ps_tdma(
2694 							btcoexist, NORMAL_EXEC,
2695 							true, 4);
2696 						coex_dm->ps_tdma_du_adj_type =
2697 							4;
2698 					} else if (coex_dm->cur_ps_tdma == 9) {
2699 						btc8723b2ant_ps_tdma(
2700 							btcoexist, NORMAL_EXEC,
2701 							true, 11);
2702 						coex_dm->ps_tdma_du_adj_type =
2703 							11;
2704 					} else if (coex_dm->cur_ps_tdma == 10) {
2705 						btc8723b2ant_ps_tdma(
2706 							btcoexist, NORMAL_EXEC,
2707 							true, 11);
2708 						coex_dm->ps_tdma_du_adj_type =
2709 							11;
2710 					} else if (coex_dm->cur_ps_tdma == 11) {
2711 						btc8723b2ant_ps_tdma(
2712 							btcoexist, NORMAL_EXEC,
2713 							true, 12);
2714 						coex_dm->ps_tdma_du_adj_type =
2715 							12;
2716 					}
2717 				} else if (result == 1) {
2718 					if (coex_dm->cur_ps_tdma == 4) {
2719 						btc8723b2ant_ps_tdma(
2720 							btcoexist, NORMAL_EXEC,
2721 							true, 3);
2722 						coex_dm->ps_tdma_du_adj_type =
2723 							3;
2724 					} else if (coex_dm->cur_ps_tdma == 3) {
2725 						btc8723b2ant_ps_tdma(
2726 							btcoexist, NORMAL_EXEC,
2727 							true, 3);
2728 						coex_dm->ps_tdma_du_adj_type =
2729 							3;
2730 					} else if (coex_dm->cur_ps_tdma == 2) {
2731 						btc8723b2ant_ps_tdma(
2732 							btcoexist, NORMAL_EXEC,
2733 							true, 3);
2734 						coex_dm->ps_tdma_du_adj_type =
2735 							3;
2736 					} else if (coex_dm->cur_ps_tdma == 12) {
2737 						btc8723b2ant_ps_tdma(
2738 							btcoexist, NORMAL_EXEC,
2739 							true, 11);
2740 						coex_dm->ps_tdma_du_adj_type =
2741 							11;
2742 					} else if (coex_dm->cur_ps_tdma == 11) {
2743 						btc8723b2ant_ps_tdma(
2744 							btcoexist, NORMAL_EXEC,
2745 							true, 11);
2746 						coex_dm->ps_tdma_du_adj_type =
2747 							11;
2748 					} else if (coex_dm->cur_ps_tdma == 10) {
2749 						btc8723b2ant_ps_tdma(
2750 							btcoexist, NORMAL_EXEC,
2751 							true, 11);
2752 						coex_dm->ps_tdma_du_adj_type =
2753 							11;
2754 					}
2755 				}
2756 			}
2757 		}
2758 	}
2759 
2760 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2761 		 "[BTCoex], max Interval = %d\n", max_interval);
2762 
2763 	/* if current PsTdma not match with the recorded one (scan, dhcp, ...),
2764 	 * then we have to adjust it back to the previous recorded one.
2765 	 */
2766 	if (coex_dm->cur_ps_tdma != coex_dm->ps_tdma_du_adj_type) {
2767 		bool scan = false, link = false, roam = false;
2768 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2769 			 "[BTCoex], PsTdma type mismatch!!!, curPsTdma=%d, recordPsTdma=%d\n",
2770 			 coex_dm->cur_ps_tdma, coex_dm->ps_tdma_du_adj_type);
2771 
2772 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2773 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2774 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2775 
2776 		if (!scan && !link && !roam)
2777 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
2778 					     coex_dm->ps_tdma_du_adj_type);
2779 		else
2780 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2781 				 "[BTCoex], roaming/link/scan is under progress, will adjust next time!!!\n");
2782 	}
2783 }
2784 
2785 /* SCO only or SCO+PAN(HS) */
2786 static void btc8723b2ant_action_sco(struct btc_coexist *btcoexist)
2787 {
2788 	u8 wifi_rssi_state, bt_rssi_state;
2789 	u32 wifi_bw;
2790 
2791 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2792 	bt_rssi_state = btc8723b2ant_bt_rssi_state(
2793 		btcoexist, 2, BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2794 					       coex_dm->switch_thres_offset,
2795 		0);
2796 
2797 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2798 
2799 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2800 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 4);
2801 
2802 	if (BTC_RSSI_HIGH(bt_rssi_state))
2803 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2804 	else
2805 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2806 
2807 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2808 
2809 	if (BTC_WIFI_BW_LEGACY == wifi_bw)
2810 		/* for SCO quality at 11b/g mode */
2811 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2812 	else
2813 		/* for SCO quality & wifi performance balance at 11n mode */
2814 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 8);
2815 
2816 	/* for voice quality */
2817 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2818 
2819 	/* sw mechanism */
2820 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2821 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2822 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2823 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2824 						  false, false);
2825 		} else {
2826 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2827 						  false, false);
2828 		}
2829 	} else {
2830 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2831 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2832 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2833 						  false, false);
2834 		} else {
2835 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2836 						  false, false);
2837 		}
2838 	}
2839 }
2840 
2841 static void btc8723b2ant_action_hid(struct btc_coexist *btcoexist)
2842 {
2843 	u8 wifi_rssi_state, bt_rssi_state;
2844 	u32 wifi_bw;
2845 	u8 tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2846 			coex_dm->switch_thres_offset;
2847 
2848 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2849 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2850 
2851 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2852 
2853 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2854 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2855 
2856 	if (BTC_RSSI_HIGH(bt_rssi_state))
2857 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2858 	else
2859 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2860 
2861 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2862 
2863 	if (wifi_bw == BTC_WIFI_BW_LEGACY)
2864 		/* for HID at 11b/g mode */
2865 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2866 	else
2867 		/* for HID quality & wifi performance balance at 11n mode */
2868 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 9);
2869 
2870 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2871 
2872 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2873 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2874 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 9);
2875 	else
2876 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
2877 
2878 	/* sw mechanism */
2879 	if (wifi_bw == BTC_WIFI_BW_HT40)
2880 		btc8723b2ant_sw_mechanism(btcoexist, true, true, false, false);
2881 	else
2882 		btc8723b2ant_sw_mechanism(btcoexist, false, true, false, false);
2883 }
2884 
2885 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
2886 static void btc8723b2ant_action_a2dp(struct btc_coexist *btcoexist)
2887 {
2888 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2889 	u32 wifi_bw;
2890 	u8 ap_num = 0;
2891 	u8 tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2892 			coex_dm->switch_thres_offset;
2893 
2894 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2895 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2, 40, 0);
2896 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2897 
2898 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
2899 
2900 	/* define the office environment */
2901 	/* driver don't know AP num in Linux, so we will never enter this if */
2902 	if (ap_num >= 10 && BTC_RSSI_HIGH(wifi_rssi_state1)) {
2903 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
2904 					  0x0);
2905 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2906 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2907 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2908 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
2909 
2910 		/* sw mechanism */
2911 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2912 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
2913 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2914 						  false, false);
2915 		} else {
2916 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2917 						  false, false);
2918 		}
2919 		return;
2920 	}
2921 
2922 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2923 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2924 
2925 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2926 
2927 	if (BTC_RSSI_HIGH(bt_rssi_state))
2928 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2929 	else
2930 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2931 
2932 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
2933 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2934 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2935 					      0x0, 0x0);
2936 	} else {
2937 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
2938 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
2939 					      0x4);
2940 	}
2941 
2942 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2943 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2944 		btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2945 						  false, 1);
2946 	else
2947 		btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 1);
2948 
2949 	/* sw mechanism */
2950 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2951 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2952 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2953 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2954 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2955 						  false, false);
2956 		} else {
2957 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2958 						  false, false);
2959 		}
2960 	} else {
2961 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2962 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2963 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2964 						  false, false);
2965 		} else {
2966 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2967 						  false, false);
2968 		}
2969 	}
2970 }
2971 
2972 static void btc8723b2ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
2973 {
2974 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2975 	u32 wifi_bw;
2976 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
2977 			coex_dm->switch_thres_offset;
2978 
2979 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2980 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
2981 							tmp, 0);
2982 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2983 			coex_dm->switch_thres_offset;
2984 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2985 
2986 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2987 
2988 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2989 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2990 
2991 	if (BTC_RSSI_HIGH(bt_rssi_state))
2992 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2993 	else
2994 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2995 
2996 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
2997 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2998 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2999 					      0x0, 0x0);
3000 	} else {
3001 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3002 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3003 					      0x4);
3004 	}
3005 
3006 	btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 2);
3007 
3008 	/* sw mechanism */
3009 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3010 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3011 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3012 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3013 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3014 						  false, false);
3015 		} else {
3016 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3017 						  false, false);
3018 		}
3019 	} else {
3020 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3021 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3022 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3023 						  false, false);
3024 		} else {
3025 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3026 						  false, false);
3027 		}
3028 	}
3029 }
3030 
3031 static void btc8723b2ant_action_pan_edr(struct btc_coexist *btcoexist)
3032 {
3033 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3034 	u32 wifi_bw;
3035 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3036 			coex_dm->switch_thres_offset;
3037 
3038 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3039 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3040 							tmp, 0);
3041 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3042 			coex_dm->switch_thres_offset;
3043 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3044 
3045 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3046 
3047 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3048 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3049 
3050 	if (BTC_RSSI_HIGH(bt_rssi_state))
3051 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3052 	else
3053 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3054 
3055 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3056 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 10);
3057 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3058 					      0x0, 0x0);
3059 	} else {
3060 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3061 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3062 					      0x4);
3063 	}
3064 
3065 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3066 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
3067 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 1);
3068 	else
3069 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
3070 
3071 	/* sw mechanism */
3072 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3073 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3074 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3075 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3076 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3077 						  false, false);
3078 		} else {
3079 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3080 						  false, false);
3081 		}
3082 	} else {
3083 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3084 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3085 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3086 						  false, false);
3087 		} else {
3088 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3089 						  false, false);
3090 		}
3091 	}
3092 }
3093 
3094 /* PAN(HS) only */
3095 static void btc8723b2ant_action_pan_hs(struct btc_coexist *btcoexist)
3096 {
3097 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3098 	u32 wifi_bw;
3099 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3100 			coex_dm->switch_thres_offset;
3101 
3102 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3103 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3104 							tmp, 0);
3105 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3106 			coex_dm->switch_thres_offset;
3107 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3108 
3109 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3110 
3111 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3112 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3113 
3114 	if (BTC_RSSI_HIGH(bt_rssi_state))
3115 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3116 	else
3117 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3118 
3119 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3120 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3121 
3122 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3123 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3124 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3125 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3126 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3127 						  false, false);
3128 		} else {
3129 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3130 						  false, false);
3131 		}
3132 	} else {
3133 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3134 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3135 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3136 						  false, false);
3137 		} else {
3138 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3139 						  false, false);
3140 		}
3141 	}
3142 }
3143 
3144 /* PAN(EDR) + A2DP */
3145 static void btc8723b2ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
3146 {
3147 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3148 	u32 wifi_bw;
3149 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3150 			coex_dm->switch_thres_offset;
3151 
3152 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3153 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3154 							tmp, 0);
3155 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3156 			coex_dm->switch_thres_offset;
3157 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3158 
3159 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3160 
3161 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3162 
3163 	if (BTC_RSSI_HIGH(bt_rssi_state))
3164 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3165 	else
3166 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3167 
3168 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state))
3169 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3170 					      0x0, 0x0);
3171 	else
3172 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3173 					      0x4);
3174 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3175 
3176 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3177 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3178 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 12);
3179 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3180 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3181 							  true, 3);
3182 		else
3183 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3184 							  false, 3);
3185 	} else {
3186 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3187 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
3188 	}
3189 
3190 	/* sw mechanism	*/
3191 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3192 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3193 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3194 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3195 						  false, false);
3196 		} else {
3197 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3198 						  false, false);
3199 		}
3200 	} else {
3201 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3202 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3203 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3204 						  false, false);
3205 		} else {
3206 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3207 						  false, false);
3208 		}
3209 	}
3210 }
3211 
3212 static void btc8723b2ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
3213 {
3214 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3215 	u32 wifi_bw;
3216 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3217 			coex_dm->switch_thres_offset;
3218 
3219 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3220 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3221 							tmp, 0);
3222 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3223 			coex_dm->switch_thres_offset;
3224 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3225 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3226 
3227 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3228 
3229 	if (BTC_RSSI_HIGH(bt_rssi_state))
3230 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3231 	else
3232 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3233 
3234 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3235 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3236 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3237 					      0x0, 0x0);
3238 	} else {
3239 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3240 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3241 					      0x4);
3242 	}
3243 
3244 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3245 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3246 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
3247 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3248 						      3);
3249 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3250 						  0xfffff, 0x780);
3251 		} else {
3252 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3253 						      6);
3254 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3255 						  0xfffff, 0x0);
3256 		}
3257 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, false, 2);
3258 	} else {
3259 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3260 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
3261 					  0x0);
3262 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 2);
3263 	}
3264 
3265 	/* sw mechanism */
3266 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3267 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3268 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3269 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3270 						  false, false);
3271 		} else {
3272 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3273 						  false, false);
3274 		}
3275 	} else {
3276 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3277 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3278 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3279 						  false, false);
3280 		} else {
3281 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3282 						  false, false);
3283 		}
3284 	}
3285 }
3286 
3287 /* HID + A2DP + PAN(EDR) */
3288 static void btc8723b2ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
3289 {
3290 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3291 	u32 wifi_bw;
3292 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3293 			coex_dm->switch_thres_offset;
3294 
3295 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3296 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3297 							tmp, 0);
3298 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3299 			coex_dm->switch_thres_offset;
3300 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3301 
3302 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3303 
3304 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3305 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3306 
3307 	if (BTC_RSSI_HIGH(bt_rssi_state))
3308 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3309 	else
3310 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3311 
3312 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3313 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3314 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3315 					      0x0, 0x0);
3316 	} else {
3317 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3318 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3319 					      0x4);
3320 	}
3321 
3322 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3323 
3324 
3325 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3326 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3327 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3328 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3329 							  true, 2);
3330 		else
3331 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3332 							  false, 3);
3333 	} else {
3334 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 3);
3335 	}
3336 
3337 	/* sw mechanism */
3338 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3339 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3340 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3341 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3342 						  false, false);
3343 		} else {
3344 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3345 						  false, false);
3346 		}
3347 	} else {
3348 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3349 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3350 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3351 						  false, false);
3352 		} else {
3353 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3354 						  false, false);
3355 		}
3356 	}
3357 }
3358 
3359 static void btc8723b2ant_action_hid_a2dp(struct btc_coexist *btcoexist)
3360 {
3361 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3362 	u32 wifi_bw;
3363 	u8 ap_num = 0;
3364 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3365 			coex_dm->switch_thres_offset;
3366 
3367 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3368 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3369 							tmp, 0);
3370 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3371 			 coex_dm->switch_thres_offset;
3372 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 3, tmp, 37);
3373 
3374 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3375 
3376 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, true, 0x5);
3377 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3378 
3379 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3380 
3381 	if (wifi_bw == BTC_WIFI_BW_LEGACY) {
3382 		if (BTC_RSSI_HIGH(bt_rssi_state))
3383 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3384 		else if (BTC_RSSI_MEDIUM(bt_rssi_state))
3385 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3386 		else
3387 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3388 	} else {
3389 		/* only 802.11N mode we have to dec bt power to 4 degree */
3390 		if (BTC_RSSI_HIGH(bt_rssi_state)) {
3391 			/* need to check ap Number of Not */
3392 			if (ap_num < 10)
3393 				btc8723b2ant_dec_bt_pwr(btcoexist,
3394 							NORMAL_EXEC, 4);
3395 			else
3396 				btc8723b2ant_dec_bt_pwr(btcoexist,
3397 							NORMAL_EXEC, 2);
3398 		} else if (BTC_RSSI_MEDIUM(bt_rssi_state)) {
3399 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3400 		} else {
3401 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3402 		}
3403 	}
3404 
3405 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3406 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3407 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3408 					      0x0, 0x0);
3409 	} else {
3410 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3411 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3412 					      0x4);
3413 	}
3414 
3415 	if (BTC_RSSI_HIGH(bt_rssi_state)) {
3416 		if (ap_num < 10)
3417 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3418 							  false, 1);
3419 		else
3420 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3421 							  false, 3);
3422 	} else {
3423 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 18);
3424 		btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
3425 		btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
3426 		btcoexist->btc_write_4byte(btcoexist, 0x430, 0x0);
3427 		btcoexist->btc_write_4byte(btcoexist, 0x434, 0x01010000);
3428 
3429 		if (ap_num < 10)
3430 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3431 							  true, 1);
3432 		else
3433 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3434 							  true, 3);
3435 	}
3436 
3437 	/* sw mechanism */
3438 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3439 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3440 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3441 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3442 						  false, false);
3443 		} else {
3444 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3445 						  false, false);
3446 		}
3447 	} else {
3448 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3449 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3450 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3451 						  false, false);
3452 		} else {
3453 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3454 						  false, false);
3455 		}
3456 	}
3457 }
3458 
3459 static void btc8723b2ant_action_wifi_multi_port(struct btc_coexist *btcoexist)
3460 {
3461 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3462 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3463 
3464 	/* sw all off */
3465 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
3466 
3467 	/* hw all off */
3468 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
3469 
3470 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3471 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3472 }
3473 
3474 static void btc8723b2ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
3475 {
3476 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3477 	u8 algorithm = 0;
3478 	u32 num_of_wifi_link = 0;
3479 	u32 wifi_link_status = 0;
3480 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3481 	bool miracast_plus_bt = false;
3482 	bool scan = false, link = false, roam = false;
3483 
3484 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3485 		 "[BTCoex], RunCoexistMechanism()===>\n");
3486 
3487 	if (btcoexist->manual_control) {
3488 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3489 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
3490 		return;
3491 	}
3492 
3493 	if (coex_sta->under_ips) {
3494 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3495 			 "[BTCoex], wifi is under IPS !!!\n");
3496 		return;
3497 	}
3498 
3499 	algorithm = btc8723b2ant_action_algorithm(btcoexist);
3500 	if (coex_sta->c2h_bt_inquiry_page &&
3501 	    (BT_8723B_2ANT_COEX_ALGO_PANHS != algorithm)) {
3502 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3503 			 "[BTCoex], BT is under inquiry/page scan !!\n");
3504 		btc8723b2ant_action_bt_inquiry(btcoexist);
3505 		return;
3506 	}
3507 
3508 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3509 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3510 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3511 
3512 	if (scan || link || roam) {
3513 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3514 			 "[BTCoex], WiFi is under Link Process !!\n");
3515 		btc8723b2ant_action_wifi_link_process(btcoexist);
3516 		return;
3517 	}
3518 
3519 	/* for P2P */
3520 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3521 			   &wifi_link_status);
3522 	num_of_wifi_link = wifi_link_status >> 16;
3523 
3524 	if ((num_of_wifi_link >= 2) ||
3525 	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
3526 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3527 			 "############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
3528 			 num_of_wifi_link, wifi_link_status);
3529 
3530 		if (bt_link_info->bt_link_exist)
3531 			miracast_plus_bt = true;
3532 		else
3533 			miracast_plus_bt = false;
3534 
3535 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3536 				   &miracast_plus_bt);
3537 		btc8723b2ant_action_wifi_multi_port(btcoexist);
3538 
3539 		return;
3540 	}
3541 
3542 	miracast_plus_bt = false;
3543 	btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3544 			   &miracast_plus_bt);
3545 
3546 	coex_dm->cur_algorithm = algorithm;
3547 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3548 		 "[BTCoex], Algorithm = %d\n",
3549 		 coex_dm->cur_algorithm);
3550 
3551 	if (btc8723b2ant_is_common_action(btcoexist)) {
3552 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3553 			 "[BTCoex], Action 2-Ant common\n");
3554 		coex_dm->auto_tdma_adjust = false;
3555 	} else {
3556 		if (coex_dm->cur_algorithm != coex_dm->pre_algorithm) {
3557 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3558 				 "[BTCoex], preAlgorithm=%d, curAlgorithm=%d\n",
3559 				 coex_dm->pre_algorithm,
3560 				 coex_dm->cur_algorithm);
3561 			coex_dm->auto_tdma_adjust = false;
3562 		}
3563 		switch (coex_dm->cur_algorithm) {
3564 		case BT_8723B_2ANT_COEX_ALGO_SCO:
3565 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3566 				 "[BTCoex], Action 2-Ant, algorithm = SCO\n");
3567 			btc8723b2ant_action_sco(btcoexist);
3568 			break;
3569 		case BT_8723B_2ANT_COEX_ALGO_HID:
3570 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3571 				 "[BTCoex], Action 2-Ant, algorithm = HID\n");
3572 			btc8723b2ant_action_hid(btcoexist);
3573 			break;
3574 		case BT_8723B_2ANT_COEX_ALGO_A2DP:
3575 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3576 				 "[BTCoex], Action 2-Ant, algorithm = A2DP\n");
3577 			btc8723b2ant_action_a2dp(btcoexist);
3578 			break;
3579 		case BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS:
3580 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3581 				 "[BTCoex], Action 2-Ant, algorithm = A2DP+PAN(HS)\n");
3582 			btc8723b2ant_action_a2dp_pan_hs(btcoexist);
3583 			break;
3584 		case BT_8723B_2ANT_COEX_ALGO_PANEDR:
3585 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3586 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)\n");
3587 			btc8723b2ant_action_pan_edr(btcoexist);
3588 			break;
3589 		case BT_8723B_2ANT_COEX_ALGO_PANHS:
3590 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3591 				 "[BTCoex], Action 2-Ant, algorithm = HS mode\n");
3592 			btc8723b2ant_action_pan_hs(btcoexist);
3593 			break;
3594 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP:
3595 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3596 				 "[BTCoex], Action 2-Ant, algorithm = PAN+A2DP\n");
3597 			btc8723b2ant_action_pan_edr_a2dp(btcoexist);
3598 			break;
3599 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_HID:
3600 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3601 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)+HID\n");
3602 			btc8723b2ant_action_pan_edr_hid(btcoexist);
3603 			break;
3604 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
3605 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3606 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP+PAN\n");
3607 			btc8723b2ant_action_hid_a2dp_pan_edr(btcoexist);
3608 			break;
3609 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP:
3610 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3611 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP\n");
3612 			btc8723b2ant_action_hid_a2dp(btcoexist);
3613 			break;
3614 		default:
3615 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3616 				 "[BTCoex], Action 2-Ant, algorithm = coexist All Off!!\n");
3617 			btc8723b2ant_coex_alloff(btcoexist);
3618 			break;
3619 		}
3620 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
3621 	}
3622 }
3623 
3624 static void btc8723b2ant_wifioff_hwcfg(struct btc_coexist *btcoexist)
3625 {
3626 	bool is_in_mp_mode = false;
3627 	u8 h2c_parameter[2] = {0};
3628 	u32 fw_ver = 0;
3629 
3630 	/* set wlan_act to low */
3631 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3632 
3633 	/* WiFi standby while GNT_BT 0 -> 1 */
3634 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x780);
3635 
3636 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3637 	if (fw_ver >= 0x180000) {
3638 		/* Use H2C to set GNT_BT to HIGH */
3639 		h2c_parameter[0] = 1;
3640 		btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1, h2c_parameter);
3641 	} else {
3642 		btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
3643 	}
3644 
3645 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE,
3646 			   &is_in_mp_mode);
3647 	if (!is_in_mp_mode)
3648 		/* BT select s0/s1 is controlled by BT */
3649 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x0);
3650 	else
3651 		/* BT select s0/s1 is controlled by WiFi */
3652 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x1);
3653 }
3654 
3655 /*********************************************************************
3656  *  extern function start with ex_btc8723b2ant_
3657  *********************************************************************/
3658 void ex_btc8723b2ant_init_hwconfig(struct btc_coexist *btcoexist)
3659 {
3660 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3661 	u8 u8tmp = 0;
3662 
3663 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3664 		 "[BTCoex], 2Ant Init HW Config!!\n");
3665 	coex_dm->bt_rf0x1e_backup =
3666 		btcoexist->btc_get_rf_reg(btcoexist, BTC_RF_A, 0x1e, 0xfffff);
3667 
3668 	/* 0x790[5:0] = 0x5 */
3669 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
3670 	u8tmp &= 0xc0;
3671 	u8tmp |= 0x5;
3672 	btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
3673 
3674 	/* Antenna config */
3675 	btc8723b2ant_set_ant_path(btcoexist, BTC_ANT_WIFI_AT_MAIN,
3676 				  true, false);
3677 	coex_sta->dis_ver_info_cnt = 0;
3678 
3679 	/* PTA parameter */
3680 	btc8723b2ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
3681 
3682 	/* Enable counter statistics */
3683 	/* 0x76e[3] = 1, WLAN_ACT controlled by PTA */
3684 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3685 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
3686 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3687 	btcoexist->auto_report_2ant = true;
3688 }
3689 
3690 void ex_btc8723b2ant_power_on_setting(struct btc_coexist *btcoexist)
3691 {
3692 	struct btc_board_info *board_info = &btcoexist->board_info;
3693 	u16 u16tmp = 0x0;
3694 	u32 value = 0;
3695 
3696 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
3697 
3698 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly */
3699 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
3700 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT0 | BIT1);
3701 
3702 	btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
3703 
3704 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3705 		/* fixed at S0 for USB interface */
3706 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3707 	} else {
3708 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3709 		if (board_info->single_ant_path == 0) {
3710 			/* set to S1 */
3711 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
3712 		} else if (board_info->single_ant_path == 1) {
3713 			/* set to S0 */
3714 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3715 		}
3716 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
3717 				   &value);
3718 	}
3719 }
3720 
3721 void ex_btc8723b2ant_pre_load_firmware(struct btc_coexist *btcoexist)
3722 {
3723 	struct btc_board_info *board_info = &btcoexist->board_info;
3724 	u8 u8tmp = 0x4; /* Set BIT2 by default since it's 2ant case */
3725 
3726 	/**
3727 	 * S0 or S1 setting and Local register setting(By this fw can get
3728 	 * ant number, S0/S1, ... info)
3729 	 *
3730 	 * Local setting bit define
3731 	 *	BIT0: "0" : no antenna inverse; "1" : antenna inverse
3732 	 *	BIT1: "0" : internal switch; "1" : external switch
3733 	 *	BIT2: "0" : one antenna; "1" : two antennas
3734 	 *
3735 	 * NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and
3736 	 * BIT2 = 0
3737 	 */
3738 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3739 		/* fixed at S0 for USB interface */
3740 		u8tmp |= 0x1; /* antenna inverse */
3741 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
3742 	} else {
3743 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3744 		if (board_info->single_ant_path == 0) {
3745 		} else if (board_info->single_ant_path == 1) {
3746 			/* set to S0 */
3747 			u8tmp |= 0x1; /* antenna inverse */
3748 		}
3749 
3750 		if (btcoexist->chip_interface == BTC_INTF_PCI)
3751 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
3752 							     u8tmp);
3753 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
3754 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
3755 							     u8tmp);
3756 	}
3757 }
3758 
3759 void ex_btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
3760 {
3761 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3762 
3763 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3764 		 "[BTCoex], Coex Mechanism Init!!\n");
3765 	btc8723b2ant_init_coex_dm(btcoexist);
3766 }
3767 
3768 void ex_btc8723b2ant_display_coex_info(struct btc_coexist *btcoexist,
3769 				       struct seq_file *m)
3770 {
3771 	struct btc_board_info *board_info = &btcoexist->board_info;
3772 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
3773 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3774 	u8 u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
3775 	u32 u32tmp[4];
3776 	bool roam = false, scan = false;
3777 	bool link = false, wifi_under_5g = false;
3778 	bool bt_hs_on = false, wifi_busy = false;
3779 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
3780 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck;
3781 	u8 wifi_dot11_chnl, wifi_hs_chnl;
3782 	u32 fw_ver = 0, bt_patch_ver = 0;
3783 	u8 ap_num = 0;
3784 
3785 	seq_puts(m, "\n ============[BT Coexist info]============");
3786 
3787 	if (btcoexist->manual_control) {
3788 		seq_puts(m, "\n ==========[Under Manual Control]============");
3789 		seq_puts(m, "\n ==========================================");
3790 	}
3791 
3792 	seq_printf(m, "\n %-35s = %d/ %d ",
3793 		   "Ant PG number/ Ant mechanism:",
3794 		   board_info->pg_ant_num, board_info->btdm_ant_num);
3795 
3796 	seq_printf(m, "\n %-35s = %s / %d",
3797 		   "BT stack/ hci ext ver",
3798 		   ((stack_info->profile_notified) ? "Yes" : "No"),
3799 		   stack_info->hci_version);
3800 
3801 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3802 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3803 	seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
3804 		   "CoexVer/ FwVer/ PatchVer",
3805 		   glcoex_ver_date_8723b_2ant, glcoex_ver_8723b_2ant,
3806 		   fw_ver, bt_patch_ver, bt_patch_ver);
3807 
3808 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3809 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
3810 			   &wifi_dot11_chnl);
3811 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
3812 
3813 	seq_printf(m, "\n %-35s = %d / %d(%d)",
3814 		   "Dot11 channel / HsChnl(HsMode)",
3815 		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
3816 
3817 	seq_printf(m, "\n %-35s = %3ph ",
3818 		   "H2C Wifi inform bt chnl Info", coex_dm->wifi_chnl_info);
3819 
3820 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
3821 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
3822 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
3823 	seq_printf(m, "\n %-35s = %d/ %d/ %d",
3824 		   "Wifi rssi/ HS rssi/ AP#", wifi_rssi, bt_hs_rssi, ap_num);
3825 
3826 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3827 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3828 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3829 	seq_printf(m, "\n %-35s = %d/ %d/ %d ",
3830 		   "Wifi link/ roam/ scan", link, roam, scan);
3831 
3832 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3833 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3834 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3835 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
3836 			   &wifi_traffic_dir);
3837 	seq_printf(m, "\n %-35s = %s / %s/ %s ",
3838 		   "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
3839 		 ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
3840 		 (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
3841 		 ((!wifi_busy) ? "idle" :
3842 		 ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
3843 		  "uplink" : "downlink")));
3844 
3845 	seq_printf(m, "\n %-35s = %d / %d / %d / %d",
3846 		   "SCO/HID/PAN/A2DP",
3847 		   bt_link_info->sco_exist, bt_link_info->hid_exist,
3848 		   bt_link_info->pan_exist, bt_link_info->a2dp_exist);
3849 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
3850 
3851 	bt_info_ext = coex_sta->bt_info_ext;
3852 	seq_printf(m, "\n %-35s = %s",
3853 		   "BT Info A2DP rate",
3854 		   (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
3855 
3856 	for (i = 0; i < BT_INFO_SRC_8723B_2ANT_MAX; i++) {
3857 		if (coex_sta->bt_info_c2h_cnt[i]) {
3858 			seq_printf(m, "\n %-35s = %7ph(%d)",
3859 				   glbt_info_src_8723b_2ant[i],
3860 				   coex_sta->bt_info_c2h[i],
3861 				   coex_sta->bt_info_c2h_cnt[i]);
3862 		}
3863 	}
3864 
3865 	seq_printf(m, "\n %-35s = %s/%s",
3866 		   "PS state, IPS/LPS",
3867 		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
3868 		   ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")));
3869 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
3870 
3871 	/* Sw mechanism	*/
3872 	seq_printf(m,
3873 		   "\n %-35s", "============[Sw mechanism]============");
3874 	seq_printf(m, "\n %-35s = %d/ %d/ %d ",
3875 		   "SM1[ShRf/ LpRA/ LimDig]", coex_dm->cur_rf_rx_lpf_shrink,
3876 		   coex_dm->cur_low_penalty_ra, coex_dm->limited_dig);
3877 	seq_printf(m, "\n %-35s = %d/ %d/ %d(0x%x) ",
3878 		   "SM2[AgcT/ AdcB/ SwDacSwing(lvl)]",
3879 		   coex_dm->cur_agc_table_en, coex_dm->cur_adc_back_off,
3880 		   coex_dm->cur_dac_swing_on, coex_dm->cur_dac_swing_lvl);
3881 
3882 	/* Fw mechanism	*/
3883 	seq_printf(m, "\n %-35s",
3884 		   "============[Fw mechanism]============");
3885 
3886 	ps_tdma_case = coex_dm->cur_ps_tdma;
3887 	seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
3888 		   "PS TDMA", coex_dm->ps_tdma_para,
3889 		   ps_tdma_case, coex_dm->auto_tdma_adjust);
3890 
3891 	seq_printf(m, "\n %-35s = %d/ %d ",
3892 		   "DecBtPwr/ IgnWlanAct", coex_dm->cur_dec_bt_pwr_lvl,
3893 		   coex_dm->cur_ignore_wlan_act);
3894 
3895 	/* Hw setting */
3896 	seq_printf(m, "\n %-35s",
3897 		   "============[Hw setting]============");
3898 
3899 	seq_printf(m, "\n %-35s = 0x%x",
3900 		   "RF-A, 0x1e initVal", coex_dm->bt_rf0x1e_backup);
3901 
3902 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
3903 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x880);
3904 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
3905 		   "0x778/0x880[29:25]", u8tmp[0],
3906 		   (u32tmp[0] & 0x3e000000) >> 25);
3907 
3908 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
3909 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
3910 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
3911 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
3912 		   "0x948/ 0x67[5] / 0x765",
3913 		   u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
3914 
3915 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
3916 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
3917 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
3918 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
3919 		   "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
3920 		   u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
3921 
3922 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
3923 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
3924 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
3925 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
3926 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3927 		   "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
3928 		   ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
3929 		   ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
3930 
3931 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
3932 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
3933 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
3934 		   "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
3935 
3936 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
3937 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
3938 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
3939 		   "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
3940 
3941 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
3942 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
3943 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
3944 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
3945 
3946 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
3947 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
3948 
3949 	fa_ofdm = ((u32tmp[0]&0xffff0000) >> 16) +
3950 		  ((u32tmp[1]&0xffff0000) >> 16) +
3951 		   (u32tmp[1] & 0xffff) +
3952 		   (u32tmp[2] & 0xffff) +
3953 		  ((u32tmp[3]&0xffff0000) >> 16) +
3954 		   (u32tmp[3] & 0xffff);
3955 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
3956 
3957 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
3958 		   "OFDM-CCA/OFDM-FA/CCK-FA",
3959 		   u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
3960 
3961 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
3962 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
3963 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
3964 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
3965 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3966 		   "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
3967 		   u32tmp[0], u32tmp[1], u32tmp[2], u8tmp[0]);
3968 
3969 	seq_printf(m, "\n %-35s = %d/ %d",
3970 		   "0x770(high-pri rx/tx)",
3971 		   coex_sta->high_priority_rx, coex_sta->high_priority_tx);
3972 	seq_printf(m, "\n %-35s = %d/ %d",
3973 		   "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
3974 		   coex_sta->low_priority_tx);
3975 	if (btcoexist->auto_report_2ant)
3976 		btc8723b2ant_monitor_bt_ctr(btcoexist);
3977 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
3978 }
3979 
3980 void ex_btc8723b2ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
3981 {
3982 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3983 
3984 	if (BTC_IPS_ENTER == type) {
3985 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3986 			 "[BTCoex], IPS ENTER notify\n");
3987 		coex_sta->under_ips = true;
3988 		btc8723b2ant_wifioff_hwcfg(btcoexist);
3989 		btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3990 		btc8723b2ant_coex_alloff(btcoexist);
3991 	} else if (BTC_IPS_LEAVE == type) {
3992 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3993 			 "[BTCoex], IPS LEAVE notify\n");
3994 		coex_sta->under_ips = false;
3995 		ex_btc8723b2ant_init_hwconfig(btcoexist);
3996 		btc8723b2ant_init_coex_dm(btcoexist);
3997 		btc8723b2ant_query_bt_info(btcoexist);
3998 	}
3999 }
4000 
4001 void ex_btc8723b2ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
4002 {
4003 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4004 
4005 	if (BTC_LPS_ENABLE == type) {
4006 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4007 			 "[BTCoex], LPS ENABLE notify\n");
4008 		coex_sta->under_lps = true;
4009 	} else if (BTC_LPS_DISABLE == type) {
4010 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4011 			 "[BTCoex], LPS DISABLE notify\n");
4012 		coex_sta->under_lps = false;
4013 	}
4014 }
4015 
4016 void ex_btc8723b2ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
4017 {
4018 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4019 	u32 u32tmp;
4020 	u8 u8tmpa, u8tmpb;
4021 
4022 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
4023 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
4024 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x76e);
4025 
4026 	if (BTC_SCAN_START == type)
4027 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4028 			 "[BTCoex], SCAN START notify\n");
4029 	else if (BTC_SCAN_FINISH == type)
4030 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4031 			 "[BTCoex], SCAN FINISH notify\n");
4032 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4033 			   &coex_sta->scan_ap_num);
4034 
4035 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4036 		 "############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x76e=0x%x\n",
4037 		u32tmp, u8tmpa, u8tmpb);
4038 }
4039 
4040 void ex_btc8723b2ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
4041 {
4042 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4043 
4044 	if (BTC_ASSOCIATE_START == type)
4045 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4046 			 "[BTCoex], CONNECT START notify\n");
4047 	else if (BTC_ASSOCIATE_FINISH == type)
4048 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4049 			 "[BTCoex], CONNECT FINISH notify\n");
4050 }
4051 
4052 void ex_btc8723b2ant_media_status_notify(struct btc_coexist *btcoexist,
4053 					 u8 type)
4054 {
4055 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4056 	u8 h2c_parameter[3] = {0};
4057 	u32 wifi_bw;
4058 	u8 wifi_central_chnl;
4059 	u8 ap_num = 0;
4060 
4061 	if (BTC_MEDIA_CONNECT == type)
4062 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4063 			 "[BTCoex], MEDIA connect notify\n");
4064 	else
4065 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4066 			 "[BTCoex], MEDIA disconnect notify\n");
4067 
4068 	/* only 2.4G we need to inform bt the chnl mask */
4069 	btcoexist->btc_get(btcoexist,
4070 		BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifi_central_chnl);
4071 	if ((BTC_MEDIA_CONNECT == type) &&
4072 	    (wifi_central_chnl <= 14)) {
4073 		h2c_parameter[0] = 0x1;
4074 		h2c_parameter[1] = wifi_central_chnl;
4075 		btcoexist->btc_get(btcoexist,
4076 			BTC_GET_U4_WIFI_BW, &wifi_bw);
4077 		if (wifi_bw == BTC_WIFI_BW_HT40) {
4078 			h2c_parameter[2] = 0x30;
4079 		} else {
4080 			btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4081 					   &ap_num);
4082 			if (ap_num < 10)
4083 				h2c_parameter[2] = 0x30;
4084 			else
4085 				h2c_parameter[2] = 0x20;
4086 		}
4087 	}
4088 
4089 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
4090 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
4091 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
4092 
4093 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4094 		 "[BTCoex], FW write 0x66=0x%x\n",
4095 		 h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
4096 		 h2c_parameter[2]);
4097 
4098 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
4099 }
4100 
4101 void ex_btc8723b2ant_special_packet_notify(struct btc_coexist *btcoexist,
4102 					   u8 type)
4103 {
4104 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4105 
4106 	if (type == BTC_PACKET_DHCP)
4107 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4108 			 "[BTCoex], DHCP Packet notify\n");
4109 }
4110 
4111 void ex_btc8723b2ant_bt_info_notify(struct btc_coexist *btcoexist,
4112 				    u8 *tmpbuf, u8 length)
4113 {
4114 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4115 	u8 bt_info = 0;
4116 	u8 i, rsp_source = 0;
4117 	bool bt_busy = false, limited_dig = false;
4118 	bool wifi_connected = false;
4119 
4120 	coex_sta->c2h_bt_info_req_sent = false;
4121 
4122 	rsp_source = tmpbuf[0]&0xf;
4123 	if (rsp_source >= BT_INFO_SRC_8723B_2ANT_MAX)
4124 		rsp_source = BT_INFO_SRC_8723B_2ANT_WIFI_FW;
4125 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
4126 
4127 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4128 		 "[BTCoex], Bt info[%d], length=%d, hex data=[",
4129 		 rsp_source, length);
4130 	for (i = 0; i < length; i++) {
4131 		coex_sta->bt_info_c2h[rsp_source][i] = tmpbuf[i];
4132 		if (i == 1)
4133 			bt_info = tmpbuf[i];
4134 		if (i == length - 1)
4135 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4136 				 "0x%02x]\n", tmpbuf[i]);
4137 		else
4138 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4139 				 "0x%02x, ", tmpbuf[i]);
4140 	}
4141 
4142 	if (btcoexist->manual_control) {
4143 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4144 			 "[BTCoex], BtInfoNotify(), return for Manual CTRL<===\n");
4145 		return;
4146 	}
4147 
4148 	if (BT_INFO_SRC_8723B_2ANT_WIFI_FW != rsp_source) {
4149 		coex_sta->bt_retry_cnt =
4150 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
4151 
4152 		if (coex_sta->bt_retry_cnt >= 1)
4153 			coex_sta->pop_event_cnt++;
4154 
4155 		coex_sta->bt_rssi =
4156 			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
4157 
4158 		coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
4159 
4160 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
4161 			coex_sta->c2h_bt_remote_name_req = true;
4162 		else
4163 			coex_sta->c2h_bt_remote_name_req = false;
4164 
4165 		if (coex_sta->bt_info_c2h[rsp_source][1] == 0x49)
4166 			coex_sta->a2dp_bit_pool =
4167 				coex_sta->bt_info_c2h[rsp_source][6];
4168 		else
4169 			coex_sta->a2dp_bit_pool = 0;
4170 
4171 		/* Here we need to resend some wifi info to BT
4172 		 * because BT is reset and loss of the info.
4173 		 */
4174 		if ((coex_sta->bt_info_ext & BIT1)) {
4175 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4176 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
4177 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4178 					   &wifi_connected);
4179 			if (wifi_connected)
4180 				ex_btc8723b2ant_media_status_notify(
4181 							btcoexist,
4182 							BTC_MEDIA_CONNECT);
4183 			else
4184 				ex_btc8723b2ant_media_status_notify(
4185 							btcoexist,
4186 							BTC_MEDIA_DISCONNECT);
4187 		}
4188 
4189 		if ((coex_sta->bt_info_ext & BIT3)) {
4190 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4191 				 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
4192 			btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC,
4193 						     false);
4194 		} else {
4195 			/* BT already NOT ignore Wlan active, do nothing here.*/
4196 		}
4197 		if (!btcoexist->auto_report_2ant) {
4198 			if (!(coex_sta->bt_info_ext & BIT4))
4199 				btc8723b2ant_bt_auto_report(btcoexist,
4200 							    FORCE_EXEC, true);
4201 		}
4202 	}
4203 
4204 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
4205 	if (bt_info & BT_INFO_8723B_2ANT_B_INQ_PAGE)
4206 		coex_sta->c2h_bt_inquiry_page = true;
4207 	else
4208 		coex_sta->c2h_bt_inquiry_page = false;
4209 
4210 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4211 		/* set link exist status */
4212 		coex_sta->bt_link_exist = false;
4213 		coex_sta->pan_exist = false;
4214 		coex_sta->a2dp_exist = false;
4215 		coex_sta->hid_exist = false;
4216 		coex_sta->sco_exist = false;
4217 	} else {
4218 		/* connection exists */
4219 		coex_sta->bt_link_exist = true;
4220 		if (bt_info & BT_INFO_8723B_2ANT_B_FTP)
4221 			coex_sta->pan_exist = true;
4222 		else
4223 			coex_sta->pan_exist = false;
4224 		if (bt_info & BT_INFO_8723B_2ANT_B_A2DP)
4225 			coex_sta->a2dp_exist = true;
4226 		else
4227 			coex_sta->a2dp_exist = false;
4228 		if (bt_info & BT_INFO_8723B_2ANT_B_HID)
4229 			coex_sta->hid_exist = true;
4230 		else
4231 			coex_sta->hid_exist = false;
4232 		if (bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO)
4233 			coex_sta->sco_exist = true;
4234 		else
4235 			coex_sta->sco_exist = false;
4236 
4237 		if ((!coex_sta->hid_exist) &&
4238 		    (!coex_sta->c2h_bt_inquiry_page) &&
4239 		    (!coex_sta->sco_exist)) {
4240 			if (coex_sta->high_priority_tx +
4241 				    coex_sta->high_priority_rx >= 160) {
4242 				coex_sta->hid_exist = true;
4243 				bt_info = bt_info | 0x28;
4244 			}
4245 		}
4246 	}
4247 
4248 	btc8723b2ant_update_bt_link_info(btcoexist);
4249 
4250 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4251 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE;
4252 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4253 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
4254 	/* connection exists but no busy */
4255 	} else if (bt_info == BT_INFO_8723B_2ANT_B_CONNECTION) {
4256 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE;
4257 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4258 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
4259 	} else if ((bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO) ||
4260 		   (bt_info & BT_INFO_8723B_2ANT_B_SCO_BUSY)) {
4261 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_SCO_BUSY;
4262 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4263 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
4264 	} else if (bt_info&BT_INFO_8723B_2ANT_B_ACL_BUSY) {
4265 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_ACL_BUSY;
4266 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4267 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
4268 	} else {
4269 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_MAX;
4270 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4271 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
4272 	}
4273 
4274 	if ((BT_8723B_2ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
4275 	    (BT_8723B_2ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
4276 	    (BT_8723B_2ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status)) {
4277 		bt_busy = true;
4278 		limited_dig = true;
4279 	} else {
4280 		bt_busy = false;
4281 		limited_dig = false;
4282 	}
4283 
4284 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
4285 
4286 	coex_dm->limited_dig = limited_dig;
4287 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_LIMITED_DIG, &limited_dig);
4288 
4289 	btc8723b2ant_run_coexist_mechanism(btcoexist);
4290 }
4291 
4292 void ex_btc8723b2ant_halt_notify(struct btc_coexist *btcoexist)
4293 {
4294 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4295 
4296 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
4297 
4298 	btc8723b2ant_wifioff_hwcfg(btcoexist);
4299 	btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
4300 	ex_btc8723b2ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
4301 }
4302 
4303 void ex_btc8723b2ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
4304 {
4305 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4306 
4307 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Pnp notify\n");
4308 
4309 	if (pnp_state == BTC_WIFI_PNP_SLEEP) {
4310 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4311 			 "[BTCoex], Pnp notify to SLEEP\n");
4312 
4313 		/* Driver do not leave IPS/LPS when driver is going to sleep, so
4314 		 * BTCoexistence think wifi is still under IPS/LPS
4315 		 *
4316 		 * BT should clear UnderIPS/UnderLPS state to avoid mismatch
4317 		 * state after wakeup.
4318 		 */
4319 		coex_sta->under_ips = false;
4320 		coex_sta->under_lps = false;
4321 	} else if (pnp_state == BTC_WIFI_PNP_WAKE_UP) {
4322 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4323 			 "[BTCoex], Pnp notify to WAKE UP\n");
4324 		ex_btc8723b2ant_init_hwconfig(btcoexist);
4325 		btc8723b2ant_init_coex_dm(btcoexist);
4326 		btc8723b2ant_query_bt_info(btcoexist);
4327 	}
4328 }
4329 
4330 void ex_btc8723b2ant_periodical(struct btc_coexist *btcoexist)
4331 {
4332 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4333 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
4334 
4335 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4336 		 "[BTCoex], ==========================Periodical===========================\n");
4337 
4338 	if (coex_sta->dis_ver_info_cnt <= 5) {
4339 		coex_sta->dis_ver_info_cnt += 1;
4340 		if (coex_sta->dis_ver_info_cnt == 3) {
4341 			/* Antenna config to set 0x765 = 0x0 (GNT_BT control by
4342 			 * PTA) after initial
4343 			 */
4344 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4345 				 "[BTCoex], Set GNT_BT control by PTA\n");
4346 			btc8723b2ant_set_ant_path(
4347 				btcoexist, BTC_ANT_WIFI_AT_MAIN, false, false);
4348 		}
4349 	}
4350 
4351 	if (!btcoexist->auto_report_2ant) {
4352 		btc8723b2ant_query_bt_info(btcoexist);
4353 	} else {
4354 		btc8723b2ant_monitor_bt_ctr(btcoexist);
4355 		btc8723b2ant_monitor_wifi_ctr(btcoexist);
4356 
4357 		/* for some BT speakers that High-Priority pkts appear before
4358 		 * playing, this will cause HID exist
4359 		 */
4360 		if ((coex_sta->high_priority_tx +
4361 		    coex_sta->high_priority_rx < 50) &&
4362 		    (bt_link_info->hid_exist))
4363 			bt_link_info->hid_exist = false;
4364 
4365 		if (btc8723b2ant_is_wifi_status_changed(btcoexist) ||
4366 		    coex_dm->auto_tdma_adjust)
4367 			btc8723b2ant_run_coexist_mechanism(btcoexist);
4368 	}
4369 }
4370