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 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 dismatch!!!, 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 (BTC_WIFI_BW_HT40 == wifi_bw) {
2880 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2881 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2882 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2883 						  false, false);
2884 		} else {
2885 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2886 						  false, false);
2887 		}
2888 	} else {
2889 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2890 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2891 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2892 						  false, false);
2893 		} else {
2894 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2895 						  false, false);
2896 		}
2897 	}
2898 }
2899 
2900 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
2901 static void btc8723b2ant_action_a2dp(struct btc_coexist *btcoexist)
2902 {
2903 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2904 	u32 wifi_bw;
2905 	u8 ap_num = 0;
2906 	u8 tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2907 			coex_dm->switch_thres_offset;
2908 
2909 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2910 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2, 40, 0);
2911 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2912 
2913 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
2914 
2915 	/* define the office environment */
2916 	/* driver don't know AP num in Linux, so we will never enter this if */
2917 	if (ap_num >= 10 && BTC_RSSI_HIGH(wifi_rssi_state1)) {
2918 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
2919 					  0x0);
2920 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2921 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2922 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2923 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
2924 
2925 		/* sw mechanism */
2926 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2927 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
2928 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2929 						  false, false);
2930 		} else {
2931 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2932 						  false, false);
2933 		}
2934 		return;
2935 	}
2936 
2937 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2938 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2939 
2940 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2941 
2942 	if (BTC_RSSI_HIGH(bt_rssi_state))
2943 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2944 	else
2945 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2946 
2947 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
2948 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2949 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2950 					      0x0, 0x0);
2951 	} else {
2952 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
2953 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
2954 					      0x4);
2955 	}
2956 
2957 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2958 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2959 		btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2960 						  false, 1);
2961 	else
2962 		btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 1);
2963 
2964 	/* sw mechanism */
2965 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2966 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2967 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2968 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2969 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2970 						  false, false);
2971 		} else {
2972 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2973 						  false, false);
2974 		}
2975 	} else {
2976 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2977 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2978 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2979 						  false, false);
2980 		} else {
2981 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2982 						  false, false);
2983 		}
2984 	}
2985 }
2986 
2987 static void btc8723b2ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
2988 {
2989 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2990 	u32 wifi_bw;
2991 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
2992 			coex_dm->switch_thres_offset;
2993 
2994 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2995 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
2996 							tmp, 0);
2997 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2998 			coex_dm->switch_thres_offset;
2999 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3000 
3001 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3002 
3003 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3004 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3005 
3006 	if (BTC_RSSI_HIGH(bt_rssi_state))
3007 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3008 	else
3009 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3010 
3011 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3012 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3013 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3014 					      0x0, 0x0);
3015 	} else {
3016 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3017 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3018 					      0x4);
3019 	}
3020 
3021 	btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 2);
3022 
3023 	/* sw mechanism */
3024 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3025 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3026 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3027 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3028 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3029 						  false, false);
3030 		} else {
3031 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3032 						  false, false);
3033 		}
3034 	} else {
3035 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3036 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3037 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3038 						  false, false);
3039 		} else {
3040 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3041 						  false, false);
3042 		}
3043 	}
3044 }
3045 
3046 static void btc8723b2ant_action_pan_edr(struct btc_coexist *btcoexist)
3047 {
3048 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3049 	u32 wifi_bw;
3050 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3051 			coex_dm->switch_thres_offset;
3052 
3053 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3054 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3055 							tmp, 0);
3056 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3057 			coex_dm->switch_thres_offset;
3058 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3059 
3060 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3061 
3062 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3063 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3064 
3065 	if (BTC_RSSI_HIGH(bt_rssi_state))
3066 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3067 	else
3068 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3069 
3070 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3071 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 10);
3072 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3073 					      0x0, 0x0);
3074 	} else {
3075 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3076 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3077 					      0x4);
3078 	}
3079 
3080 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3081 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
3082 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 1);
3083 	else
3084 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
3085 
3086 	/* sw mechanism */
3087 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3088 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3089 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3090 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3091 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3092 						  false, false);
3093 		} else {
3094 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3095 						  false, false);
3096 		}
3097 	} else {
3098 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3099 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3100 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3101 						  false, false);
3102 		} else {
3103 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3104 						  false, false);
3105 		}
3106 	}
3107 }
3108 
3109 /* PAN(HS) only */
3110 static void btc8723b2ant_action_pan_hs(struct btc_coexist *btcoexist)
3111 {
3112 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3113 	u32 wifi_bw;
3114 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3115 			coex_dm->switch_thres_offset;
3116 
3117 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3118 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3119 							tmp, 0);
3120 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3121 			coex_dm->switch_thres_offset;
3122 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3123 
3124 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3125 
3126 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3127 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3128 
3129 	if (BTC_RSSI_HIGH(bt_rssi_state))
3130 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3131 	else
3132 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3133 
3134 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3135 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3136 
3137 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3138 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3139 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3140 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3141 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3142 						  false, false);
3143 		} else {
3144 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3145 						  false, false);
3146 		}
3147 	} else {
3148 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3149 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3150 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3151 						  false, false);
3152 		} else {
3153 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3154 						  false, false);
3155 		}
3156 	}
3157 }
3158 
3159 /* PAN(EDR) + A2DP */
3160 static void btc8723b2ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
3161 {
3162 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3163 	u32 wifi_bw;
3164 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3165 			coex_dm->switch_thres_offset;
3166 
3167 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3168 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3169 							tmp, 0);
3170 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3171 			coex_dm->switch_thres_offset;
3172 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3173 
3174 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3175 
3176 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3177 
3178 	if (BTC_RSSI_HIGH(bt_rssi_state))
3179 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3180 	else
3181 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3182 
3183 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state))
3184 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3185 					      0x0, 0x0);
3186 	else
3187 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3188 					      0x4);
3189 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3190 
3191 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3192 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3193 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 12);
3194 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3195 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3196 							  true, 3);
3197 		else
3198 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3199 							  false, 3);
3200 	} else {
3201 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3202 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
3203 	}
3204 
3205 	/* sw mechanism	*/
3206 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3207 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3208 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3209 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3210 						  false, false);
3211 		} else {
3212 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3213 						  false, false);
3214 		}
3215 	} else {
3216 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3217 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3218 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3219 						  false, false);
3220 		} else {
3221 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3222 						  false, false);
3223 		}
3224 	}
3225 }
3226 
3227 static void btc8723b2ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
3228 {
3229 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3230 	u32 wifi_bw;
3231 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3232 			coex_dm->switch_thres_offset;
3233 
3234 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3235 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3236 							tmp, 0);
3237 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3238 			coex_dm->switch_thres_offset;
3239 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3240 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3241 
3242 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3243 
3244 	if (BTC_RSSI_HIGH(bt_rssi_state))
3245 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3246 	else
3247 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3248 
3249 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3250 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3251 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3252 					      0x0, 0x0);
3253 	} else {
3254 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3255 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3256 					      0x4);
3257 	}
3258 
3259 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3260 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3261 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
3262 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3263 						      3);
3264 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3265 						  0xfffff, 0x780);
3266 		} else {
3267 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3268 						      6);
3269 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3270 						  0xfffff, 0x0);
3271 		}
3272 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, false, 2);
3273 	} else {
3274 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3275 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
3276 					  0x0);
3277 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 2);
3278 	}
3279 
3280 	/* sw mechanism */
3281 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3282 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3283 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3284 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3285 						  false, false);
3286 		} else {
3287 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3288 						  false, false);
3289 		}
3290 	} else {
3291 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3292 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3293 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3294 						  false, false);
3295 		} else {
3296 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3297 						  false, false);
3298 		}
3299 	}
3300 }
3301 
3302 /* HID + A2DP + PAN(EDR) */
3303 static void btc8723b2ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
3304 {
3305 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3306 	u32 wifi_bw;
3307 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3308 			coex_dm->switch_thres_offset;
3309 
3310 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3311 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3312 							tmp, 0);
3313 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3314 			coex_dm->switch_thres_offset;
3315 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3316 
3317 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3318 
3319 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3320 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3321 
3322 	if (BTC_RSSI_HIGH(bt_rssi_state))
3323 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3324 	else
3325 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3326 
3327 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3328 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3329 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3330 					      0x0, 0x0);
3331 	} else {
3332 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3333 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3334 					      0x4);
3335 	}
3336 
3337 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3338 
3339 
3340 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3341 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3342 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3343 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3344 							  true, 2);
3345 		else
3346 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3347 							  false, 3);
3348 	} else {
3349 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 3);
3350 	}
3351 
3352 	/* sw mechanism */
3353 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3354 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3355 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3356 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3357 						  false, false);
3358 		} else {
3359 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3360 						  false, false);
3361 		}
3362 	} else {
3363 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3364 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3365 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3366 						  false, false);
3367 		} else {
3368 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3369 						  false, false);
3370 		}
3371 	}
3372 }
3373 
3374 static void btc8723b2ant_action_hid_a2dp(struct btc_coexist *btcoexist)
3375 {
3376 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3377 	u32 wifi_bw;
3378 	u8 ap_num = 0;
3379 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3380 			coex_dm->switch_thres_offset;
3381 
3382 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3383 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3384 							tmp, 0);
3385 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3386 			 coex_dm->switch_thres_offset;
3387 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 3, tmp, 37);
3388 
3389 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3390 
3391 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, true, 0x5);
3392 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3393 
3394 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3395 
3396 	if (wifi_bw == BTC_WIFI_BW_LEGACY) {
3397 		if (BTC_RSSI_HIGH(bt_rssi_state))
3398 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3399 		else if (BTC_RSSI_MEDIUM(bt_rssi_state))
3400 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3401 		else
3402 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3403 	} else {
3404 		/* only 802.11N mode we have to dec bt power to 4 degree */
3405 		if (BTC_RSSI_HIGH(bt_rssi_state)) {
3406 			/* need to check ap Number of Not */
3407 			if (ap_num < 10)
3408 				btc8723b2ant_dec_bt_pwr(btcoexist,
3409 							NORMAL_EXEC, 4);
3410 			else
3411 				btc8723b2ant_dec_bt_pwr(btcoexist,
3412 							NORMAL_EXEC, 2);
3413 		} else if (BTC_RSSI_MEDIUM(bt_rssi_state)) {
3414 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3415 		} else {
3416 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3417 		}
3418 	}
3419 
3420 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3421 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3422 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3423 					      0x0, 0x0);
3424 	} else {
3425 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3426 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3427 					      0x4);
3428 	}
3429 
3430 	if (BTC_RSSI_HIGH(bt_rssi_state)) {
3431 		if (ap_num < 10)
3432 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3433 							  false, 1);
3434 		else
3435 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3436 							  false, 3);
3437 	} else {
3438 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 18);
3439 		btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
3440 		btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
3441 		btcoexist->btc_write_4byte(btcoexist, 0x430, 0x0);
3442 		btcoexist->btc_write_4byte(btcoexist, 0x434, 0x01010000);
3443 
3444 		if (ap_num < 10)
3445 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3446 							  true, 1);
3447 		else
3448 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3449 							  true, 3);
3450 	}
3451 
3452 	/* sw mechanism */
3453 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3454 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3455 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3456 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3457 						  false, false);
3458 		} else {
3459 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3460 						  false, false);
3461 		}
3462 	} else {
3463 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3464 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3465 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3466 						  false, false);
3467 		} else {
3468 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3469 						  false, false);
3470 		}
3471 	}
3472 }
3473 
3474 static void btc8723b2ant_action_wifi_multi_port(struct btc_coexist *btcoexist)
3475 {
3476 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3477 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3478 
3479 	/* sw all off */
3480 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
3481 
3482 	/* hw all off */
3483 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
3484 
3485 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3486 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3487 }
3488 
3489 static void btc8723b2ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
3490 {
3491 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3492 	u8 algorithm = 0;
3493 	u32 num_of_wifi_link = 0;
3494 	u32 wifi_link_status = 0;
3495 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3496 	bool miracast_plus_bt = false;
3497 	bool scan = false, link = false, roam = false;
3498 
3499 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3500 		 "[BTCoex], RunCoexistMechanism()===>\n");
3501 
3502 	if (btcoexist->manual_control) {
3503 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3504 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
3505 		return;
3506 	}
3507 
3508 	if (coex_sta->under_ips) {
3509 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3510 			 "[BTCoex], wifi is under IPS !!!\n");
3511 		return;
3512 	}
3513 
3514 	algorithm = btc8723b2ant_action_algorithm(btcoexist);
3515 	if (coex_sta->c2h_bt_inquiry_page &&
3516 	    (BT_8723B_2ANT_COEX_ALGO_PANHS != algorithm)) {
3517 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3518 			 "[BTCoex], BT is under inquiry/page scan !!\n");
3519 		btc8723b2ant_action_bt_inquiry(btcoexist);
3520 		return;
3521 	}
3522 
3523 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3524 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3525 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3526 
3527 	if (scan || link || roam) {
3528 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3529 			 "[BTCoex], WiFi is under Link Process !!\n");
3530 		btc8723b2ant_action_wifi_link_process(btcoexist);
3531 		return;
3532 	}
3533 
3534 	/* for P2P */
3535 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3536 			   &wifi_link_status);
3537 	num_of_wifi_link = wifi_link_status >> 16;
3538 
3539 	if ((num_of_wifi_link >= 2) ||
3540 	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
3541 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3542 			 "############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
3543 			 num_of_wifi_link, wifi_link_status);
3544 
3545 		if (bt_link_info->bt_link_exist)
3546 			miracast_plus_bt = true;
3547 		else
3548 			miracast_plus_bt = false;
3549 
3550 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3551 				   &miracast_plus_bt);
3552 		btc8723b2ant_action_wifi_multi_port(btcoexist);
3553 
3554 		return;
3555 	}
3556 
3557 	miracast_plus_bt = false;
3558 	btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3559 			   &miracast_plus_bt);
3560 
3561 	coex_dm->cur_algorithm = algorithm;
3562 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3563 		 "[BTCoex], Algorithm = %d\n",
3564 		 coex_dm->cur_algorithm);
3565 
3566 	if (btc8723b2ant_is_common_action(btcoexist)) {
3567 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3568 			 "[BTCoex], Action 2-Ant common\n");
3569 		coex_dm->auto_tdma_adjust = false;
3570 	} else {
3571 		if (coex_dm->cur_algorithm != coex_dm->pre_algorithm) {
3572 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3573 				 "[BTCoex], preAlgorithm=%d, curAlgorithm=%d\n",
3574 				 coex_dm->pre_algorithm,
3575 				 coex_dm->cur_algorithm);
3576 			coex_dm->auto_tdma_adjust = false;
3577 		}
3578 		switch (coex_dm->cur_algorithm) {
3579 		case BT_8723B_2ANT_COEX_ALGO_SCO:
3580 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3581 				 "[BTCoex], Action 2-Ant, algorithm = SCO\n");
3582 			btc8723b2ant_action_sco(btcoexist);
3583 			break;
3584 		case BT_8723B_2ANT_COEX_ALGO_HID:
3585 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3586 				 "[BTCoex], Action 2-Ant, algorithm = HID\n");
3587 			btc8723b2ant_action_hid(btcoexist);
3588 			break;
3589 		case BT_8723B_2ANT_COEX_ALGO_A2DP:
3590 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3591 				 "[BTCoex], Action 2-Ant, algorithm = A2DP\n");
3592 			btc8723b2ant_action_a2dp(btcoexist);
3593 			break;
3594 		case BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS:
3595 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3596 				 "[BTCoex], Action 2-Ant, algorithm = A2DP+PAN(HS)\n");
3597 			btc8723b2ant_action_a2dp_pan_hs(btcoexist);
3598 			break;
3599 		case BT_8723B_2ANT_COEX_ALGO_PANEDR:
3600 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3601 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)\n");
3602 			btc8723b2ant_action_pan_edr(btcoexist);
3603 			break;
3604 		case BT_8723B_2ANT_COEX_ALGO_PANHS:
3605 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3606 				 "[BTCoex], Action 2-Ant, algorithm = HS mode\n");
3607 			btc8723b2ant_action_pan_hs(btcoexist);
3608 			break;
3609 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP:
3610 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3611 				 "[BTCoex], Action 2-Ant, algorithm = PAN+A2DP\n");
3612 			btc8723b2ant_action_pan_edr_a2dp(btcoexist);
3613 			break;
3614 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_HID:
3615 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3616 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)+HID\n");
3617 			btc8723b2ant_action_pan_edr_hid(btcoexist);
3618 			break;
3619 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
3620 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3621 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP+PAN\n");
3622 			btc8723b2ant_action_hid_a2dp_pan_edr(btcoexist);
3623 			break;
3624 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP:
3625 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3626 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP\n");
3627 			btc8723b2ant_action_hid_a2dp(btcoexist);
3628 			break;
3629 		default:
3630 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3631 				 "[BTCoex], Action 2-Ant, algorithm = coexist All Off!!\n");
3632 			btc8723b2ant_coex_alloff(btcoexist);
3633 			break;
3634 		}
3635 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
3636 	}
3637 }
3638 
3639 static void btc8723b2ant_wifioff_hwcfg(struct btc_coexist *btcoexist)
3640 {
3641 	bool is_in_mp_mode = false;
3642 	u8 h2c_parameter[2] = {0};
3643 	u32 fw_ver = 0;
3644 
3645 	/* set wlan_act to low */
3646 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3647 
3648 	/* WiFi standby while GNT_BT 0 -> 1 */
3649 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x780);
3650 
3651 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3652 	if (fw_ver >= 0x180000) {
3653 		/* Use H2C to set GNT_BT to HIGH */
3654 		h2c_parameter[0] = 1;
3655 		btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1, h2c_parameter);
3656 	} else {
3657 		btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
3658 	}
3659 
3660 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE,
3661 			   &is_in_mp_mode);
3662 	if (!is_in_mp_mode)
3663 		/* BT select s0/s1 is controlled by BT */
3664 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x0);
3665 	else
3666 		/* BT select s0/s1 is controlled by WiFi */
3667 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x1);
3668 }
3669 
3670 /*********************************************************************
3671  *  extern function start with ex_btc8723b2ant_
3672  *********************************************************************/
3673 void ex_btc8723b2ant_init_hwconfig(struct btc_coexist *btcoexist)
3674 {
3675 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3676 	u8 u8tmp = 0;
3677 
3678 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3679 		 "[BTCoex], 2Ant Init HW Config!!\n");
3680 	coex_dm->bt_rf0x1e_backup =
3681 		btcoexist->btc_get_rf_reg(btcoexist, BTC_RF_A, 0x1e, 0xfffff);
3682 
3683 	/* 0x790[5:0] = 0x5 */
3684 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
3685 	u8tmp &= 0xc0;
3686 	u8tmp |= 0x5;
3687 	btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
3688 
3689 	/* Antenna config */
3690 	btc8723b2ant_set_ant_path(btcoexist, BTC_ANT_WIFI_AT_MAIN,
3691 				  true, false);
3692 	coex_sta->dis_ver_info_cnt = 0;
3693 
3694 	/* PTA parameter */
3695 	btc8723b2ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
3696 
3697 	/* Enable counter statistics */
3698 	/* 0x76e[3] = 1, WLAN_ACT controlled by PTA */
3699 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3700 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
3701 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3702 	btcoexist->auto_report_2ant = true;
3703 }
3704 
3705 void ex_btc8723b2ant_power_on_setting(struct btc_coexist *btcoexist)
3706 {
3707 	struct btc_board_info *board_info = &btcoexist->board_info;
3708 	u16 u16tmp = 0x0;
3709 	u32 value = 0;
3710 
3711 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
3712 
3713 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly */
3714 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
3715 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT0 | BIT1);
3716 
3717 	btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
3718 
3719 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3720 		/* fixed at S0 for USB interface */
3721 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3722 	} else {
3723 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3724 		if (board_info->single_ant_path == 0) {
3725 			/* set to S1 */
3726 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
3727 		} else if (board_info->single_ant_path == 1) {
3728 			/* set to S0 */
3729 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3730 		}
3731 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
3732 				   &value);
3733 	}
3734 }
3735 
3736 void ex_btc8723b2ant_pre_load_firmware(struct btc_coexist *btcoexist)
3737 {
3738 	struct btc_board_info *board_info = &btcoexist->board_info;
3739 	u8 u8tmp = 0x4; /* Set BIT2 by default since it's 2ant case */
3740 
3741 	/**
3742 	 * S0 or S1 setting and Local register setting(By this fw can get
3743 	 * ant number, S0/S1, ... info)
3744 	 *
3745 	 * Local setting bit define
3746 	 *	BIT0: "0" : no antenna inverse; "1" : antenna inverse
3747 	 *	BIT1: "0" : internal switch; "1" : external switch
3748 	 *	BIT2: "0" : one antenna; "1" : two antennas
3749 	 *
3750 	 * NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and
3751 	 * BIT2 = 0
3752 	 */
3753 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3754 		/* fixed at S0 for USB interface */
3755 		u8tmp |= 0x1; /* antenna inverse */
3756 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
3757 	} else {
3758 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3759 		if (board_info->single_ant_path == 0) {
3760 		} else if (board_info->single_ant_path == 1) {
3761 			/* set to S0 */
3762 			u8tmp |= 0x1; /* antenna inverse */
3763 		}
3764 
3765 		if (btcoexist->chip_interface == BTC_INTF_PCI)
3766 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
3767 							     u8tmp);
3768 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
3769 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
3770 							     u8tmp);
3771 	}
3772 }
3773 
3774 void ex_btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
3775 {
3776 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3777 
3778 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3779 		 "[BTCoex], Coex Mechanism Init!!\n");
3780 	btc8723b2ant_init_coex_dm(btcoexist);
3781 }
3782 
3783 void ex_btc8723b2ant_display_coex_info(struct btc_coexist *btcoexist)
3784 {
3785 	struct btc_board_info *board_info = &btcoexist->board_info;
3786 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
3787 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3788 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3789 	u8 u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
3790 	u32 u32tmp[4];
3791 	bool roam = false, scan = false;
3792 	bool link = false, wifi_under_5g = false;
3793 	bool bt_hs_on = false, wifi_busy = false;
3794 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
3795 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck;
3796 	u8 wifi_dot11_chnl, wifi_hs_chnl;
3797 	u32 fw_ver = 0, bt_patch_ver = 0;
3798 	u8 ap_num = 0;
3799 
3800 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3801 		 "\r\n ============[BT Coexist info]============");
3802 
3803 	if (btcoexist->manual_control) {
3804 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3805 			 "\r\n ==========[Under Manual Control]============");
3806 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3807 			 "\r\n ==========================================");
3808 	}
3809 
3810 	if (!board_info->bt_exist) {
3811 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n BT not exists !!!");
3812 		return;
3813 	}
3814 
3815 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3816 		 "Ant PG number/ Ant mechanism:",
3817 		 board_info->pg_ant_num, board_info->btdm_ant_num);
3818 
3819 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %d",
3820 		 "BT stack/ hci ext ver",
3821 		 ((stack_info->profile_notified) ? "Yes" : "No"),
3822 		 stack_info->hci_version);
3823 
3824 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3825 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3826 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3827 		 "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
3828 		 "CoexVer/ FwVer/ PatchVer",
3829 		 glcoex_ver_date_8723b_2ant, glcoex_ver_8723b_2ant,
3830 		 fw_ver, bt_patch_ver, bt_patch_ver);
3831 
3832 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3833 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
3834 			   &wifi_dot11_chnl);
3835 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
3836 
3837 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d(%d)",
3838 		 "Dot11 channel / HsChnl(HsMode)",
3839 		 wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
3840 
3841 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %3ph ",
3842 		 "H2C Wifi inform bt chnl Info", coex_dm->wifi_chnl_info);
3843 
3844 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
3845 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
3846 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
3847 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d",
3848 		 "Wifi rssi/ HS rssi/ AP#", wifi_rssi, bt_hs_rssi, ap_num);
3849 
3850 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3851 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3852 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3853 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3854 		 "Wifi link/ roam/ scan", link, roam, scan);
3855 
3856 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3857 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3858 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3859 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
3860 			   &wifi_traffic_dir);
3861 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %s/ %s ",
3862 		 "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
3863 		 ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
3864 		 (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
3865 		 ((!wifi_busy) ? "idle" :
3866 		 ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
3867 		  "uplink" : "downlink")));
3868 
3869 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d / %d / %d",
3870 		 "SCO/HID/PAN/A2DP",
3871 		 bt_link_info->sco_exist, bt_link_info->hid_exist,
3872 		 bt_link_info->pan_exist, bt_link_info->a2dp_exist);
3873 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
3874 
3875 	bt_info_ext = coex_sta->bt_info_ext;
3876 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s",
3877 		 "BT Info A2DP rate",
3878 		 (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
3879 
3880 	for (i = 0; i < BT_INFO_SRC_8723B_2ANT_MAX; i++) {
3881 		if (coex_sta->bt_info_c2h_cnt[i]) {
3882 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3883 				 "\r\n %-35s = %7ph(%d)",
3884 				 glbt_info_src_8723b_2ant[i],
3885 				 coex_sta->bt_info_c2h[i],
3886 				 coex_sta->bt_info_c2h_cnt[i]);
3887 		}
3888 	}
3889 
3890 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s/%s",
3891 		 "PS state, IPS/LPS",
3892 		 ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
3893 		 ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")));
3894 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
3895 
3896 	/* Sw mechanism	*/
3897 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3898 		 "\r\n %-35s", "============[Sw mechanism]============");
3899 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3900 		 "SM1[ShRf/ LpRA/ LimDig]", coex_dm->cur_rf_rx_lpf_shrink,
3901 		 coex_dm->cur_low_penalty_ra, coex_dm->limited_dig);
3902 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d(0x%x) ",
3903 		 "SM2[AgcT/ AdcB/ SwDacSwing(lvl)]",
3904 		 coex_dm->cur_agc_table_en, coex_dm->cur_adc_back_off,
3905 		 coex_dm->cur_dac_swing_on, coex_dm->cur_dac_swing_lvl);
3906 
3907 	/* Fw mechanism	*/
3908 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3909 		 "============[Fw mechanism]============");
3910 
3911 	ps_tdma_case = coex_dm->cur_ps_tdma;
3912 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3913 		 "\r\n %-35s = %5ph case-%d (auto:%d)",
3914 		 "PS TDMA", coex_dm->ps_tdma_para,
3915 		 ps_tdma_case, coex_dm->auto_tdma_adjust);
3916 
3917 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3918 		 "DecBtPwr/ IgnWlanAct", coex_dm->cur_dec_bt_pwr_lvl,
3919 		 coex_dm->cur_ignore_wlan_act);
3920 
3921 	/* Hw setting */
3922 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3923 		 "============[Hw setting]============");
3924 
3925 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x",
3926 		 "RF-A, 0x1e initVal", coex_dm->bt_rf0x1e_backup);
3927 
3928 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
3929 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x880);
3930 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3931 		 "0x778/0x880[29:25]", u8tmp[0],
3932 		 (u32tmp[0] & 0x3e000000) >> 25);
3933 
3934 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
3935 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
3936 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
3937 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3938 		 "0x948/ 0x67[5] / 0x765",
3939 		 u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
3940 
3941 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
3942 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
3943 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
3944 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3945 		 "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
3946 		 u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
3947 
3948 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
3949 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
3950 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
3951 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
3952 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3953 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3954 		 "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
3955 		 ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
3956 		 ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
3957 
3958 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
3959 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
3960 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3961 		 "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
3962 
3963 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
3964 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
3965 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3966 		 "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
3967 
3968 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
3969 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
3970 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
3971 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
3972 
3973 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
3974 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
3975 
3976 	fa_ofdm = ((u32tmp[0]&0xffff0000) >> 16) +
3977 		  ((u32tmp[1]&0xffff0000) >> 16) +
3978 		   (u32tmp[1] & 0xffff) +
3979 		   (u32tmp[2] & 0xffff) +
3980 		  ((u32tmp[3]&0xffff0000) >> 16) +
3981 		   (u32tmp[3] & 0xffff);
3982 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
3983 
3984 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3985 		 "OFDM-CCA/OFDM-FA/CCK-FA",
3986 		 u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
3987 
3988 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
3989 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
3990 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
3991 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
3992 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3993 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3994 		 "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
3995 		 u32tmp[0], u32tmp[1], u32tmp[2], u8tmp[0]);
3996 
3997 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
3998 		 "0x770(high-pri rx/tx)",
3999 		 coex_sta->high_priority_rx, coex_sta->high_priority_tx);
4000 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
4001 		 "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
4002 		 coex_sta->low_priority_tx);
4003 	if (btcoexist->auto_report_2ant)
4004 		btc8723b2ant_monitor_bt_ctr(btcoexist);
4005 	btcoexist->btc_disp_dbg_msg(btcoexist,
4006 	BTC_DBG_DISP_COEX_STATISTICS);
4007 }
4008 
4009 void ex_btc8723b2ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
4010 {
4011 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4012 
4013 	if (BTC_IPS_ENTER == type) {
4014 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4015 			 "[BTCoex], IPS ENTER notify\n");
4016 		coex_sta->under_ips = true;
4017 		btc8723b2ant_wifioff_hwcfg(btcoexist);
4018 		btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
4019 		btc8723b2ant_coex_alloff(btcoexist);
4020 	} else if (BTC_IPS_LEAVE == type) {
4021 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4022 			 "[BTCoex], IPS LEAVE notify\n");
4023 		coex_sta->under_ips = false;
4024 		ex_btc8723b2ant_init_hwconfig(btcoexist);
4025 		btc8723b2ant_init_coex_dm(btcoexist);
4026 		btc8723b2ant_query_bt_info(btcoexist);
4027 	}
4028 }
4029 
4030 void ex_btc8723b2ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
4031 {
4032 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4033 
4034 	if (BTC_LPS_ENABLE == type) {
4035 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4036 			 "[BTCoex], LPS ENABLE notify\n");
4037 		coex_sta->under_lps = true;
4038 	} else if (BTC_LPS_DISABLE == type) {
4039 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4040 			 "[BTCoex], LPS DISABLE notify\n");
4041 		coex_sta->under_lps = false;
4042 	}
4043 }
4044 
4045 void ex_btc8723b2ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
4046 {
4047 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4048 	u32 u32tmp;
4049 	u8 u8tmpa, u8tmpb;
4050 
4051 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
4052 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
4053 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x76e);
4054 
4055 	if (BTC_SCAN_START == type)
4056 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4057 			 "[BTCoex], SCAN START notify\n");
4058 	else if (BTC_SCAN_FINISH == type)
4059 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4060 			 "[BTCoex], SCAN FINISH notify\n");
4061 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4062 			   &coex_sta->scan_ap_num);
4063 
4064 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4065 		 "############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x76e=0x%x\n",
4066 		u32tmp, u8tmpa, u8tmpb);
4067 }
4068 
4069 void ex_btc8723b2ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
4070 {
4071 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4072 
4073 	if (BTC_ASSOCIATE_START == type)
4074 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4075 			 "[BTCoex], CONNECT START notify\n");
4076 	else if (BTC_ASSOCIATE_FINISH == type)
4077 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4078 			 "[BTCoex], CONNECT FINISH notify\n");
4079 }
4080 
4081 void ex_btc8723b2ant_media_status_notify(struct btc_coexist *btcoexist,
4082 					 u8 type)
4083 {
4084 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4085 	u8 h2c_parameter[3] = {0};
4086 	u32 wifi_bw;
4087 	u8 wifi_central_chnl;
4088 	u8 ap_num = 0;
4089 
4090 	if (BTC_MEDIA_CONNECT == type)
4091 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4092 			 "[BTCoex], MEDIA connect notify\n");
4093 	else
4094 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4095 			 "[BTCoex], MEDIA disconnect notify\n");
4096 
4097 	/* only 2.4G we need to inform bt the chnl mask */
4098 	btcoexist->btc_get(btcoexist,
4099 		BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifi_central_chnl);
4100 	if ((BTC_MEDIA_CONNECT == type) &&
4101 	    (wifi_central_chnl <= 14)) {
4102 		h2c_parameter[0] = 0x1;
4103 		h2c_parameter[1] = wifi_central_chnl;
4104 		btcoexist->btc_get(btcoexist,
4105 			BTC_GET_U4_WIFI_BW, &wifi_bw);
4106 		if (wifi_bw == BTC_WIFI_BW_HT40) {
4107 			h2c_parameter[2] = 0x30;
4108 		} else {
4109 			btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4110 					   &ap_num);
4111 			if (ap_num < 10)
4112 				h2c_parameter[2] = 0x30;
4113 			else
4114 				h2c_parameter[2] = 0x20;
4115 		}
4116 	}
4117 
4118 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
4119 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
4120 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
4121 
4122 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4123 		 "[BTCoex], FW write 0x66=0x%x\n",
4124 		 h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
4125 		 h2c_parameter[2]);
4126 
4127 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
4128 }
4129 
4130 void ex_btc8723b2ant_special_packet_notify(struct btc_coexist *btcoexist,
4131 					   u8 type)
4132 {
4133 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4134 
4135 	if (type == BTC_PACKET_DHCP)
4136 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4137 			 "[BTCoex], DHCP Packet notify\n");
4138 }
4139 
4140 void ex_btc8723b2ant_bt_info_notify(struct btc_coexist *btcoexist,
4141 				    u8 *tmpbuf, u8 length)
4142 {
4143 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4144 	u8 bt_info = 0;
4145 	u8 i, rsp_source = 0;
4146 	bool bt_busy = false, limited_dig = false;
4147 	bool wifi_connected = false;
4148 
4149 	coex_sta->c2h_bt_info_req_sent = false;
4150 
4151 	rsp_source = tmpbuf[0]&0xf;
4152 	if (rsp_source >= BT_INFO_SRC_8723B_2ANT_MAX)
4153 		rsp_source = BT_INFO_SRC_8723B_2ANT_WIFI_FW;
4154 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
4155 
4156 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4157 		 "[BTCoex], Bt info[%d], length=%d, hex data=[",
4158 		 rsp_source, length);
4159 	for (i = 0; i < length; i++) {
4160 		coex_sta->bt_info_c2h[rsp_source][i] = tmpbuf[i];
4161 		if (i == 1)
4162 			bt_info = tmpbuf[i];
4163 		if (i == length - 1)
4164 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4165 				 "0x%02x]\n", tmpbuf[i]);
4166 		else
4167 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4168 				 "0x%02x, ", tmpbuf[i]);
4169 	}
4170 
4171 	if (btcoexist->manual_control) {
4172 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4173 			 "[BTCoex], BtInfoNotify(), return for Manual CTRL<===\n");
4174 		return;
4175 	}
4176 
4177 	if (BT_INFO_SRC_8723B_2ANT_WIFI_FW != rsp_source) {
4178 		coex_sta->bt_retry_cnt =
4179 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
4180 
4181 		if (coex_sta->bt_retry_cnt >= 1)
4182 			coex_sta->pop_event_cnt++;
4183 
4184 		coex_sta->bt_rssi =
4185 			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
4186 
4187 		coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
4188 
4189 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
4190 			coex_sta->c2h_bt_remote_name_req = true;
4191 		else
4192 			coex_sta->c2h_bt_remote_name_req = false;
4193 
4194 		if (coex_sta->bt_info_c2h[rsp_source][1] == 0x49)
4195 			coex_sta->a2dp_bit_pool =
4196 				coex_sta->bt_info_c2h[rsp_source][6];
4197 		else
4198 			coex_sta->a2dp_bit_pool = 0;
4199 
4200 		/* Here we need to resend some wifi info to BT
4201 		 * because BT is reset and loss of the info.
4202 		 */
4203 		if ((coex_sta->bt_info_ext & BIT1)) {
4204 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4205 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
4206 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4207 					   &wifi_connected);
4208 			if (wifi_connected)
4209 				ex_btc8723b2ant_media_status_notify(
4210 							btcoexist,
4211 							BTC_MEDIA_CONNECT);
4212 			else
4213 				ex_btc8723b2ant_media_status_notify(
4214 							btcoexist,
4215 							BTC_MEDIA_DISCONNECT);
4216 		}
4217 
4218 		if ((coex_sta->bt_info_ext & BIT3)) {
4219 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4220 				 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
4221 			btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC,
4222 						     false);
4223 		} else {
4224 			/* BT already NOT ignore Wlan active, do nothing here.*/
4225 		}
4226 		if (!btcoexist->auto_report_2ant) {
4227 			if (!(coex_sta->bt_info_ext & BIT4))
4228 				btc8723b2ant_bt_auto_report(btcoexist,
4229 							    FORCE_EXEC, true);
4230 		}
4231 	}
4232 
4233 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
4234 	if (bt_info & BT_INFO_8723B_2ANT_B_INQ_PAGE)
4235 		coex_sta->c2h_bt_inquiry_page = true;
4236 	else
4237 		coex_sta->c2h_bt_inquiry_page = false;
4238 
4239 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4240 		/* set link exist status */
4241 		coex_sta->bt_link_exist = false;
4242 		coex_sta->pan_exist = false;
4243 		coex_sta->a2dp_exist = false;
4244 		coex_sta->hid_exist = false;
4245 		coex_sta->sco_exist = false;
4246 	} else {
4247 		/* connection exists */
4248 		coex_sta->bt_link_exist = true;
4249 		if (bt_info & BT_INFO_8723B_2ANT_B_FTP)
4250 			coex_sta->pan_exist = true;
4251 		else
4252 			coex_sta->pan_exist = false;
4253 		if (bt_info & BT_INFO_8723B_2ANT_B_A2DP)
4254 			coex_sta->a2dp_exist = true;
4255 		else
4256 			coex_sta->a2dp_exist = false;
4257 		if (bt_info & BT_INFO_8723B_2ANT_B_HID)
4258 			coex_sta->hid_exist = true;
4259 		else
4260 			coex_sta->hid_exist = false;
4261 		if (bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO)
4262 			coex_sta->sco_exist = true;
4263 		else
4264 			coex_sta->sco_exist = false;
4265 
4266 		if ((!coex_sta->hid_exist) &&
4267 		    (!coex_sta->c2h_bt_inquiry_page) &&
4268 		    (!coex_sta->sco_exist)) {
4269 			if (coex_sta->high_priority_tx +
4270 				    coex_sta->high_priority_rx >= 160) {
4271 				coex_sta->hid_exist = true;
4272 				bt_info = bt_info | 0x28;
4273 			}
4274 		}
4275 	}
4276 
4277 	btc8723b2ant_update_bt_link_info(btcoexist);
4278 
4279 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4280 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE;
4281 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4282 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
4283 	/* connection exists but no busy */
4284 	} else if (bt_info == BT_INFO_8723B_2ANT_B_CONNECTION) {
4285 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE;
4286 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4287 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
4288 	} else if ((bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO) ||
4289 		   (bt_info & BT_INFO_8723B_2ANT_B_SCO_BUSY)) {
4290 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_SCO_BUSY;
4291 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4292 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
4293 	} else if (bt_info&BT_INFO_8723B_2ANT_B_ACL_BUSY) {
4294 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_ACL_BUSY;
4295 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4296 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
4297 	} else {
4298 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_MAX;
4299 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4300 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
4301 	}
4302 
4303 	if ((BT_8723B_2ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
4304 	    (BT_8723B_2ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
4305 	    (BT_8723B_2ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status)) {
4306 		bt_busy = true;
4307 		limited_dig = true;
4308 	} else {
4309 		bt_busy = false;
4310 		limited_dig = false;
4311 	}
4312 
4313 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
4314 
4315 	coex_dm->limited_dig = limited_dig;
4316 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_LIMITED_DIG, &limited_dig);
4317 
4318 	btc8723b2ant_run_coexist_mechanism(btcoexist);
4319 }
4320 
4321 void ex_btc8723b2ant_halt_notify(struct btc_coexist *btcoexist)
4322 {
4323 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4324 
4325 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
4326 
4327 	btc8723b2ant_wifioff_hwcfg(btcoexist);
4328 	btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
4329 	ex_btc8723b2ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
4330 }
4331 
4332 void ex_btc8723b2ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
4333 {
4334 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4335 
4336 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Pnp notify\n");
4337 
4338 	if (pnp_state == BTC_WIFI_PNP_SLEEP) {
4339 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4340 			 "[BTCoex], Pnp notify to SLEEP\n");
4341 
4342 		/* Driver do not leave IPS/LPS when driver is going to sleep, so
4343 		 * BTCoexistence think wifi is still under IPS/LPS
4344 		 *
4345 		 * BT should clear UnderIPS/UnderLPS state to avoid mismatch
4346 		 * state after wakeup.
4347 		 */
4348 		coex_sta->under_ips = false;
4349 		coex_sta->under_lps = false;
4350 	} else if (pnp_state == BTC_WIFI_PNP_WAKE_UP) {
4351 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4352 			 "[BTCoex], Pnp notify to WAKE UP\n");
4353 		ex_btc8723b2ant_init_hwconfig(btcoexist);
4354 		btc8723b2ant_init_coex_dm(btcoexist);
4355 		btc8723b2ant_query_bt_info(btcoexist);
4356 	}
4357 }
4358 
4359 void ex_btc8723b2ant_periodical(struct btc_coexist *btcoexist)
4360 {
4361 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4362 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
4363 
4364 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4365 		 "[BTCoex], ==========================Periodical===========================\n");
4366 
4367 	if (coex_sta->dis_ver_info_cnt <= 5) {
4368 		coex_sta->dis_ver_info_cnt += 1;
4369 		if (coex_sta->dis_ver_info_cnt == 3) {
4370 			/* Antenna config to set 0x765 = 0x0 (GNT_BT control by
4371 			 * PTA) after initial
4372 			 */
4373 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4374 				 "[BTCoex], Set GNT_BT control by PTA\n");
4375 			btc8723b2ant_set_ant_path(
4376 				btcoexist, BTC_ANT_WIFI_AT_MAIN, false, false);
4377 		}
4378 	}
4379 
4380 	if (!btcoexist->auto_report_2ant) {
4381 		btc8723b2ant_query_bt_info(btcoexist);
4382 	} else {
4383 		btc8723b2ant_monitor_bt_ctr(btcoexist);
4384 		btc8723b2ant_monitor_wifi_ctr(btcoexist);
4385 
4386 		/* for some BT speakers that High-Priority pkts appear before
4387 		 * playing, this will cause HID exist
4388 		 */
4389 		if ((coex_sta->high_priority_tx +
4390 		    coex_sta->high_priority_rx < 50) &&
4391 		    (bt_link_info->hid_exist))
4392 			bt_link_info->hid_exist = false;
4393 
4394 		if (btc8723b2ant_is_wifi_status_changed(btcoexist) ||
4395 		    coex_dm->auto_tdma_adjust)
4396 			btc8723b2ant_run_coexist_mechanism(btcoexist);
4397 	}
4398 }
4399