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 		btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
1187 
1188 		switch (antpos_type) {
1189 		case BTC_ANT_WIFI_AT_MAIN:
1190 			/* ext switch main at wifi */
1191 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x92c,
1192 							   0x3, 0x1);
1193 			break;
1194 		case BTC_ANT_WIFI_AT_AUX:
1195 			/* ext switch aux at wifi */
1196 			btcoexist->btc_write_1byte_bitmask(btcoexist,
1197 							   0x92c, 0x3, 0x2);
1198 			break;
1199 		}
1200 	} else {
1201 		/* internal switch */
1202 		if (init_hwcfg) {
1203 			/* 0x4c[23] = 0, 0x4c[24] = 1 Ant controlled by WL/BT */
1204 			u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1205 			u32tmp |= BIT23;
1206 			u32tmp &= ~BIT24;
1207 			btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
1208 		}
1209 
1210 		/* fixed ext switch, S1->Main, S0->Aux */
1211 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64, 0x1, 0x0);
1212 		switch (antpos_type) {
1213 		case BTC_ANT_WIFI_AT_MAIN:
1214 			/* fixed internal switch S1->WiFi, S0->BT */
1215 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1216 			break;
1217 		case BTC_ANT_WIFI_AT_AUX:
1218 			/* fixed internal switch S0->WiFi, S1->BT */
1219 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1220 			break;
1221 		}
1222 	}
1223 }
1224 
1225 static void btc8723b2ant_ps_tdma(struct btc_coexist *btcoexist, bool force_exec,
1226 				 bool turn_on, u8 type)
1227 {
1228 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1229 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1230 	u8 wifi_rssi_state, bt_rssi_state;
1231 	s8 wifi_duration_adjust = 0x0;
1232 	u8 tdma_byte4_modify = 0x0;
1233 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
1234 			coex_dm->switch_thres_offset;
1235 
1236 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, tmp, 0);
1237 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
1238 			coex_dm->switch_thres_offset;
1239 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
1240 
1241 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1242 		 "[BTCoex], %s turn %s PS TDMA, type=%d\n",
1243 		 (force_exec ? "force to" : ""),
1244 		 (turn_on ? "ON" : "OFF"), type);
1245 	coex_dm->cur_ps_tdma_on = turn_on;
1246 	coex_dm->cur_ps_tdma = type;
1247 
1248 	if (!(BTC_RSSI_HIGH(wifi_rssi_state) &&
1249 	      BTC_RSSI_HIGH(bt_rssi_state)) && turn_on) {
1250 		 /* for WiFi RSSI low or BT RSSI low */
1251 		type = type + 100;
1252 		coex_dm->is_switch_to_1dot5_ant = true;
1253 	} else {
1254 		coex_dm->is_switch_to_1dot5_ant = false;
1255 	}
1256 
1257 	if (!force_exec) {
1258 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1259 			 "[BTCoex], bPrePsTdmaOn = %d, bCurPsTdmaOn = %d!!\n",
1260 			 coex_dm->pre_ps_tdma_on, coex_dm->cur_ps_tdma_on);
1261 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1262 			 "[BTCoex], prePsTdma = %d, curPsTdma = %d!!\n",
1263 			 coex_dm->pre_ps_tdma, coex_dm->cur_ps_tdma);
1264 
1265 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1266 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1267 			return;
1268 	}
1269 
1270 	if (coex_sta->scan_ap_num <= 5) {
1271 		if (coex_sta->a2dp_bit_pool >= 45)
1272 			wifi_duration_adjust = -15;
1273 		else if (coex_sta->a2dp_bit_pool >= 35)
1274 			wifi_duration_adjust = -10;
1275 		else
1276 			wifi_duration_adjust = 5;
1277 	} else if (coex_sta->scan_ap_num <= 20) {
1278 		if (coex_sta->a2dp_bit_pool >= 45)
1279 			wifi_duration_adjust = -15;
1280 		else if (coex_sta->a2dp_bit_pool >= 35)
1281 			wifi_duration_adjust = -10;
1282 		else
1283 			wifi_duration_adjust = 0;
1284 	} else if (coex_sta->scan_ap_num <= 40) {
1285 		if (coex_sta->a2dp_bit_pool >= 45)
1286 			wifi_duration_adjust = -15;
1287 		else if (coex_sta->a2dp_bit_pool >= 35)
1288 			wifi_duration_adjust = -10;
1289 		else
1290 			wifi_duration_adjust = -5;
1291 	} else {
1292 		if (coex_sta->a2dp_bit_pool >= 45)
1293 			wifi_duration_adjust = -15;
1294 		else if (coex_sta->a2dp_bit_pool >= 35)
1295 			wifi_duration_adjust = -10;
1296 		else
1297 			wifi_duration_adjust = -10;
1298 	}
1299 
1300 	if ((bt_link_info->slave_role) && (bt_link_info->a2dp_exist))
1301 		/* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1302 		tdma_byte4_modify = 0x1;
1303 
1304 	if (turn_on) {
1305 		switch (type) {
1306 		case 1:
1307 		default:
1308 			btc8723b2ant_set_fw_ps_tdma(
1309 				btcoexist, 0xe3, 0x3c,
1310 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1311 			break;
1312 		case 2:
1313 			btc8723b2ant_set_fw_ps_tdma(
1314 				btcoexist, 0xe3, 0x2d,
1315 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1316 			break;
1317 		case 3:
1318 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1319 						    0x3, 0xf1,
1320 						    0x90 | tdma_byte4_modify);
1321 			break;
1322 		case 4:
1323 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1324 						    0x03, 0xf1,
1325 						    0x90 | tdma_byte4_modify);
1326 			break;
1327 		case 5:
1328 			btc8723b2ant_set_fw_ps_tdma(
1329 				btcoexist, 0xe3, 0x3c,
1330 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1331 			break;
1332 		case 6:
1333 			btc8723b2ant_set_fw_ps_tdma(
1334 				btcoexist, 0xe3, 0x2d,
1335 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1336 			break;
1337 		case 7:
1338 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1339 						    0x3, 0x70,
1340 						    0x90 | tdma_byte4_modify);
1341 			break;
1342 		case 8:
1343 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x10,
1344 						    0x3, 0x70,
1345 						    0x90 | tdma_byte4_modify);
1346 			break;
1347 		case 9:
1348 			btc8723b2ant_set_fw_ps_tdma(
1349 				btcoexist, 0xe3, 0x3c + wifi_duration_adjust,
1350 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1351 			break;
1352 		case 10:
1353 			btc8723b2ant_set_fw_ps_tdma(
1354 				btcoexist, 0xe3, 0x2d,
1355 				0x03, 0xf1, 0x90 | tdma_byte4_modify);
1356 			break;
1357 		case 11:
1358 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1359 						    0x3, 0xf1,
1360 						    0x90 | tdma_byte4_modify);
1361 			break;
1362 		case 12:
1363 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1364 						    0x3, 0xf1,
1365 						    0x90 | tdma_byte4_modify);
1366 			break;
1367 		case 13:
1368 			btc8723b2ant_set_fw_ps_tdma(
1369 				btcoexist, 0xe3, 0x3c,
1370 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1371 			break;
1372 		case 14:
1373 			btc8723b2ant_set_fw_ps_tdma(
1374 				btcoexist, 0xe3, 0x2d,
1375 				0x3, 0x70, 0x90 | tdma_byte4_modify);
1376 			break;
1377 		case 15:
1378 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1379 						    0x3, 0x70,
1380 						    0x90 | tdma_byte4_modify);
1381 			break;
1382 		case 16:
1383 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1384 						    0x3, 0x70,
1385 						    0x90 | tdma_byte4_modify);
1386 			break;
1387 		case 17:
1388 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x2f,
1389 						    0x2f, 0x60, 0x90);
1390 			break;
1391 		case 18:
1392 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x5, 0x5,
1393 						    0xe1, 0x90);
1394 			break;
1395 		case 19:
1396 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1397 						    0x25, 0xe1, 0x90);
1398 			break;
1399 		case 20:
1400 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1401 						    0x25, 0x60, 0x90);
1402 			break;
1403 		case 21:
1404 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1405 						    0x03, 0x70, 0x90);
1406 			break;
1407 
1408 		case 23:
1409 		case 123:
1410 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x35,
1411 						    0x03, 0x71, 0x10);
1412 			break;
1413 		case 71:
1414 			btc8723b2ant_set_fw_ps_tdma(
1415 				btcoexist, 0xe3, 0x3c + wifi_duration_adjust,
1416 				0x03, 0xf1, 0x90);
1417 			break;
1418 		case 101:
1419 		case 105:
1420 		case 113:
1421 		case 171:
1422 			btc8723b2ant_set_fw_ps_tdma(
1423 				btcoexist, 0xd3, 0x3a + wifi_duration_adjust,
1424 				0x03, 0x70, 0x50 | tdma_byte4_modify);
1425 			break;
1426 		case 102:
1427 		case 106:
1428 		case 110:
1429 		case 114:
1430 			btc8723b2ant_set_fw_ps_tdma(
1431 				btcoexist, 0xd3, 0x2d + wifi_duration_adjust,
1432 				0x03, 0x70, 0x50 | tdma_byte4_modify);
1433 			break;
1434 		case 103:
1435 		case 107:
1436 		case 111:
1437 		case 115:
1438 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x1c,
1439 						    0x03, 0x70,
1440 						    0x50 | tdma_byte4_modify);
1441 			break;
1442 		case 104:
1443 		case 108:
1444 		case 112:
1445 		case 116:
1446 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x10,
1447 						    0x03, 0x70,
1448 						    0x50 | tdma_byte4_modify);
1449 			break;
1450 		case 109:
1451 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x3c,
1452 						    0x03, 0xf1,
1453 						    0x90 | tdma_byte4_modify);
1454 			break;
1455 		case 121:
1456 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1457 						    0x03, 0x70,
1458 						    0x90 | tdma_byte4_modify);
1459 			break;
1460 		case 22:
1461 		case 122:
1462 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x35,
1463 						    0x03, 0x71, 0x11);
1464 			break;
1465 		}
1466 	} else {
1467 		/* disable PS tdma */
1468 		switch (type) {
1469 		case 0:
1470 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1471 						    0x40, 0x0);
1472 			break;
1473 		case 1:
1474 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1475 						    0x48, 0x0);
1476 			break;
1477 		default:
1478 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1479 						    0x40, 0x0);
1480 			break;
1481 		}
1482 	}
1483 
1484 	/* update pre state */
1485 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1486 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1487 }
1488 
1489 static void btc8723b2ant_ps_tdma_check_for_power_save_state(
1490 		struct btc_coexist *btcoexist, bool new_ps_state)
1491 {
1492 	u8 lps_mode = 0x0;
1493 
1494 	btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1495 
1496 	if (lps_mode) {
1497 		/* already under LPS state */
1498 		if (new_ps_state) {
1499 			/* keep state under LPS, do nothing. */
1500 		} else {
1501 			/* will leave LPS state, turn off psTdma first */
1502 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1503 		}
1504 	} else {
1505 		/* NO PS state */
1506 		if (new_ps_state) {
1507 			/* will enter LPS state, turn off psTdma first */
1508 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1509 		} else {
1510 			/* keep state under NO PS state, do nothing. */
1511 		}
1512 	}
1513 }
1514 
1515 static void btc8723b2ant_power_save_state(struct btc_coexist *btcoexist,
1516 					  u8 ps_type, u8 lps_val, u8 rpwm_val)
1517 {
1518 	bool low_pwr_disable = false;
1519 
1520 	switch (ps_type) {
1521 	case BTC_PS_WIFI_NATIVE:
1522 		/* recover to original 32k low power setting */
1523 		low_pwr_disable = false;
1524 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1525 				   &low_pwr_disable);
1526 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1527 		coex_sta->force_lps_on = false;
1528 		break;
1529 	case BTC_PS_LPS_ON:
1530 		btc8723b2ant_ps_tdma_check_for_power_save_state(btcoexist,
1531 								true);
1532 		btc8723b2ant_lps_rpwm(btcoexist, NORMAL_EXEC, lps_val,
1533 				      rpwm_val);
1534 		/* when coex force to enter LPS, do not enter 32k low power */
1535 		low_pwr_disable = true;
1536 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1537 				   &low_pwr_disable);
1538 		/* power save must executed before psTdma */
1539 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1540 		coex_sta->force_lps_on = true;
1541 		break;
1542 	case BTC_PS_LPS_OFF:
1543 		btc8723b2ant_ps_tdma_check_for_power_save_state(btcoexist,
1544 								false);
1545 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1546 		coex_sta->force_lps_on = false;
1547 		break;
1548 	default:
1549 		break;
1550 	}
1551 }
1552 
1553 static void btc8723b2ant_coex_alloff(struct btc_coexist *btcoexist)
1554 {
1555 	/* fw all off */
1556 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1557 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1558 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1559 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1560 
1561 	/* sw all off */
1562 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1563 
1564 	/* hw all off */
1565 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
1566 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1567 }
1568 
1569 static void btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
1570 {
1571 	/* force to reset coex mechanism*/
1572 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1573 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1574 
1575 	btc8723b2ant_ps_tdma(btcoexist, FORCE_EXEC, false, 1);
1576 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1577 	btc8723b2ant_dec_bt_pwr(btcoexist, FORCE_EXEC, 0);
1578 
1579 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1580 
1581 	coex_sta->pop_event_cnt = 0;
1582 }
1583 
1584 static void btc8723b2ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1585 {
1586 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1587 	bool wifi_connected = false;
1588 	bool low_pwr_disable = true;
1589 	bool scan = false, link = false, roam = false;
1590 
1591 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1592 			   &low_pwr_disable);
1593 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1594 			   &wifi_connected);
1595 
1596 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1597 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1598 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1599 
1600 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1601 
1602 	if (coex_sta->bt_abnormal_scan) {
1603 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 23);
1604 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
1605 	} else if (scan || link || roam) {
1606 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1607 			 "[BTCoex], Wifi link process + BT Inq/Page!!\n");
1608 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1609 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1610 	} else if (wifi_connected) {
1611 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1612 			 "[BTCoex], Wifi connected + BT Inq/Page!!\n");
1613 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1614 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1615 	} else {
1616 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1617 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1618 	}
1619 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1620 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1621 
1622 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1623 }
1624 
1625 static void btc8723b2ant_action_wifi_link_process(struct btc_coexist
1626 						     *btcoexist)
1627 {
1628 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1629 	u32 u32tmp;
1630 	u8 u8tmpa, u8tmpb;
1631 
1632 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 15);
1633 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1634 
1635 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
1636 
1637 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
1638 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
1639 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x76e);
1640 
1641 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1642 		 "[BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x76e = 0x%x\n",
1643 		 u32tmp, u8tmpa, u8tmpb);
1644 }
1645 
1646 static bool btc8723b2ant_action_wifi_idle_process(struct btc_coexist *btcoexist)
1647 {
1648 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1649 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
1650 	u8 ap_num = 0;
1651 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
1652 		 coex_dm->switch_thres_offset - coex_dm->switch_thres_offset;
1653 
1654 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
1655 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
1656 							tmp, 0);
1657 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
1658 	      coex_dm->switch_thres_offset - coex_dm->switch_thres_offset;
1659 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
1660 
1661 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
1662 
1663 	/* office environment */
1664 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && (coex_sta->hid_exist) &&
1665 	    (coex_sta->a2dp_exist)) {
1666 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1667 			 "[BTCoex], Wifi  idle process for BT HID+A2DP exist!!\n");
1668 
1669 		btc8723b2ant_dac_swing(btcoexist, NORMAL_EXEC, true, 0x6);
1670 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1671 
1672 		/* sw all off */
1673 		btc8723b2ant_sw_mechanism(btcoexist, false, false, false,
1674 					  false);
1675 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1676 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1677 					      0x0, 0x0);
1678 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1679 
1680 		return true;
1681 	}
1682 
1683 	btc8723b2ant_dac_swing(btcoexist, NORMAL_EXEC, true, 0x18);
1684 	return false;
1685 }
1686 
1687 static bool btc8723b2ant_is_common_action(struct btc_coexist *btcoexist)
1688 {
1689 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1690 	bool common = false, wifi_connected = false;
1691 	bool wifi_busy = false;
1692 	bool bt_hs_on = false, low_pwr_disable = false;
1693 
1694 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1695 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1696 			   &wifi_connected);
1697 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1698 
1699 	if (!wifi_connected) {
1700 		low_pwr_disable = false;
1701 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1702 				   &low_pwr_disable);
1703 		btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1704 					false, false, 0x8);
1705 
1706 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1707 			 "[BTCoex], Wifi non-connected idle!!\n");
1708 
1709 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
1710 					  0x0);
1711 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1712 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1713 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1714 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1715 
1716 		btc8723b2ant_sw_mechanism(btcoexist, false, false, false,
1717 					  false);
1718 
1719 		common = true;
1720 	} else {
1721 		if (BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1722 		    coex_dm->bt_status) {
1723 			low_pwr_disable = false;
1724 			btcoexist->btc_set(btcoexist,
1725 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1726 					   &low_pwr_disable);
1727 			btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1728 						false, false, 0x8);
1729 
1730 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1731 				 "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1732 
1733 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1734 						  0xfffff, 0x0);
1735 			btc8723b2ant_coex_table_with_type(btcoexist,
1736 							  NORMAL_EXEC, 0);
1737 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1738 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1739 						      0xb);
1740 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1741 
1742 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
1743 						  false, false);
1744 
1745 			common = true;
1746 		} else if (BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE ==
1747 			   coex_dm->bt_status) {
1748 			low_pwr_disable = true;
1749 			btcoexist->btc_set(btcoexist,
1750 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1751 					   &low_pwr_disable);
1752 
1753 			if (bt_hs_on)
1754 				return false;
1755 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1756 				 "[BTCoex], Wifi connected + BT connected-idle!!\n");
1757 			btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC,
1758 						false, false, 0x8);
1759 
1760 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1761 						  0xfffff, 0x0);
1762 			btc8723b2ant_coex_table_with_type(btcoexist,
1763 							  NORMAL_EXEC, 0);
1764 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1765 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1766 						      0xb);
1767 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
1768 
1769 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
1770 						  false, false);
1771 
1772 			common = true;
1773 		} else {
1774 			low_pwr_disable = true;
1775 			btcoexist->btc_set(btcoexist,
1776 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1777 					   &low_pwr_disable);
1778 
1779 			if (wifi_busy) {
1780 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1781 					 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1782 				common = false;
1783 			} else {
1784 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1785 					 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1786 
1787 				common =
1788 				    btc8723b2ant_action_wifi_idle_process(
1789 						btcoexist);
1790 			}
1791 		}
1792 	}
1793 
1794 	return common;
1795 }
1796 
1797 static void btc8723b2ant_tdma_duration_adjust(struct btc_coexist *btcoexist,
1798 					  bool sco_hid, bool tx_pause,
1799 					  u8 max_interval)
1800 {
1801 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1802 	static s32 up, dn, m, n, wait_count;
1803 	/*0: no change, +1: increase WiFi duration, -1: decrease WiFi duration*/
1804 	s32 result;
1805 	u8 retry_count = 0;
1806 
1807 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1808 		 "[BTCoex], TdmaDurationAdjust()\n");
1809 
1810 	if (!coex_dm->auto_tdma_adjust) {
1811 		coex_dm->auto_tdma_adjust = true;
1812 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1813 			 "[BTCoex], first run TdmaDurationAdjust()!!\n");
1814 		if (sco_hid) {
1815 			if (tx_pause) {
1816 				if (max_interval == 1) {
1817 					btc8723b2ant_ps_tdma(btcoexist,
1818 							     NORMAL_EXEC,
1819 							     true, 13);
1820 					coex_dm->ps_tdma_du_adj_type = 13;
1821 				} else if (max_interval == 2) {
1822 					btc8723b2ant_ps_tdma(btcoexist,
1823 							     NORMAL_EXEC,
1824 							     true, 14);
1825 					coex_dm->ps_tdma_du_adj_type = 14;
1826 				} else if (max_interval == 3) {
1827 					btc8723b2ant_ps_tdma(btcoexist,
1828 							     NORMAL_EXEC,
1829 							     true, 15);
1830 					coex_dm->ps_tdma_du_adj_type = 15;
1831 				} else {
1832 					btc8723b2ant_ps_tdma(btcoexist,
1833 							     NORMAL_EXEC,
1834 							     true, 15);
1835 					coex_dm->ps_tdma_du_adj_type = 15;
1836 				}
1837 			} else {
1838 				if (max_interval == 1) {
1839 					btc8723b2ant_ps_tdma(btcoexist,
1840 							     NORMAL_EXEC,
1841 							     true, 9);
1842 					coex_dm->ps_tdma_du_adj_type = 9;
1843 				} else if (max_interval == 2) {
1844 					btc8723b2ant_ps_tdma(btcoexist,
1845 							     NORMAL_EXEC,
1846 							     true, 10);
1847 					coex_dm->ps_tdma_du_adj_type = 10;
1848 				} else if (max_interval == 3) {
1849 					btc8723b2ant_ps_tdma(btcoexist,
1850 							     NORMAL_EXEC,
1851 						     true, 11);
1852 					coex_dm->ps_tdma_du_adj_type = 11;
1853 				} else {
1854 					btc8723b2ant_ps_tdma(btcoexist,
1855 							     NORMAL_EXEC,
1856 							     true, 11);
1857 					coex_dm->ps_tdma_du_adj_type = 11;
1858 				}
1859 			}
1860 		} else {
1861 			if (tx_pause) {
1862 				if (max_interval == 1) {
1863 					btc8723b2ant_ps_tdma(btcoexist,
1864 							     NORMAL_EXEC,
1865 							     true, 5);
1866 					coex_dm->ps_tdma_du_adj_type = 5;
1867 				} else if (max_interval == 2) {
1868 					btc8723b2ant_ps_tdma(btcoexist,
1869 							     NORMAL_EXEC,
1870 							     true, 6);
1871 					coex_dm->ps_tdma_du_adj_type = 6;
1872 				} else if (max_interval == 3) {
1873 					btc8723b2ant_ps_tdma(btcoexist,
1874 							     NORMAL_EXEC,
1875 							     true, 7);
1876 					coex_dm->ps_tdma_du_adj_type = 7;
1877 				} else {
1878 					btc8723b2ant_ps_tdma(btcoexist,
1879 							     NORMAL_EXEC,
1880 							     true, 7);
1881 					coex_dm->ps_tdma_du_adj_type = 7;
1882 				}
1883 			} else {
1884 				if (max_interval == 1) {
1885 					btc8723b2ant_ps_tdma(btcoexist,
1886 							     NORMAL_EXEC,
1887 							     true, 1);
1888 					coex_dm->ps_tdma_du_adj_type = 1;
1889 				} else if (max_interval == 2) {
1890 					btc8723b2ant_ps_tdma(btcoexist,
1891 							     NORMAL_EXEC,
1892 							     true, 2);
1893 					coex_dm->ps_tdma_du_adj_type = 2;
1894 				} else if (max_interval == 3) {
1895 					btc8723b2ant_ps_tdma(btcoexist,
1896 							     NORMAL_EXEC,
1897 							     true, 3);
1898 					coex_dm->ps_tdma_du_adj_type = 3;
1899 				} else {
1900 					btc8723b2ant_ps_tdma(btcoexist,
1901 							     NORMAL_EXEC,
1902 							     true, 3);
1903 					coex_dm->ps_tdma_du_adj_type = 3;
1904 				}
1905 			}
1906 		}
1907 
1908 		up = 0;
1909 		dn = 0;
1910 		m = 1;
1911 		n = 3;
1912 		result = 0;
1913 		wait_count = 0;
1914 	} else {
1915 		/*accquire the BT TRx retry count from BT_Info byte2*/
1916 		retry_count = coex_sta->bt_retry_cnt;
1917 
1918 		if ((coex_sta->low_priority_tx) > 1050 ||
1919 		    (coex_sta->low_priority_rx) > 1250)
1920 			retry_count++;
1921 
1922 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1923 			 "[BTCoex], retry_count = %d\n", retry_count);
1924 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1925 			 "[BTCoex], up=%d, dn=%d, m=%d, n=%d, wait_count=%d\n",
1926 			 up, dn, m, n, wait_count);
1927 		result = 0;
1928 		wait_count++;
1929 		 /* no retry in the last 2-second duration*/
1930 		if (retry_count == 0) {
1931 			up++;
1932 			dn--;
1933 
1934 			if (dn <= 0)
1935 				dn = 0;
1936 
1937 			if (up >= n) {
1938 				/* if retry count during continuous n*2
1939 				 * seconds is 0, enlarge WiFi duration
1940 				 */
1941 				wait_count = 0;
1942 				n = 3;
1943 				up = 0;
1944 				dn = 0;
1945 				result = 1;
1946 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1947 					 "[BTCoex], Increase wifi duration!!\n");
1948 			} /* <=3 retry in the last 2-second duration*/
1949 		} else if (retry_count <= 3) {
1950 			up--;
1951 			dn++;
1952 
1953 			if (up <= 0)
1954 				up = 0;
1955 
1956 			if (dn == 2) {
1957 				/* if continuous 2 retry count(every 2
1958 				 * seconds) >0 and < 3, reduce WiFi duration
1959 				 */
1960 				if (wait_count <= 2)
1961 					/* avoid loop between the two levels */
1962 					m++;
1963 				else
1964 					m = 1;
1965 
1966 				if (m >= 20)
1967 					/* maximum of m = 20 ' will recheck if
1968 					 * need to adjust wifi duration in
1969 					 * maximum time interval 120 seconds
1970 					 */
1971 					m = 20;
1972 
1973 				n = 3 * m;
1974 				up = 0;
1975 				dn = 0;
1976 				wait_count = 0;
1977 				result = -1;
1978 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1979 					 "[BTCoex], Decrease wifi duration for retry_counter<3!!\n");
1980 			}
1981 		} else {
1982 			/* retry count > 3, once retry count > 3, to reduce
1983 			 *  WiFi duration
1984 			 */
1985 			if (wait_count == 1)
1986 				/* to avoid loop between the two levels */
1987 				m++;
1988 			else
1989 				m = 1;
1990 
1991 			if (m >= 20)
1992 				/* maximum of m = 20 ' will recheck if need to
1993 				 * adjust wifi duration in maximum time interval
1994 				 * 120 seconds
1995 				 */
1996 				m = 20;
1997 
1998 			n = 3 * m;
1999 			up = 0;
2000 			dn = 0;
2001 			wait_count = 0;
2002 			result = -1;
2003 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2004 				 "[BTCoex], Decrease wifi duration for retry_counter>3!!\n");
2005 		}
2006 
2007 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2008 			 "[BTCoex], max Interval = %d\n", max_interval);
2009 		if (max_interval == 1) {
2010 			if (tx_pause) {
2011 				if (coex_dm->cur_ps_tdma == 71) {
2012 					btc8723b2ant_ps_tdma(btcoexist,
2013 							     NORMAL_EXEC,
2014 							     true, 5);
2015 					coex_dm->ps_tdma_du_adj_type = 5;
2016 				} else if (coex_dm->cur_ps_tdma == 1) {
2017 					btc8723b2ant_ps_tdma(btcoexist,
2018 							     NORMAL_EXEC,
2019 							     true, 5);
2020 					coex_dm->ps_tdma_du_adj_type = 5;
2021 				} else if (coex_dm->cur_ps_tdma == 2) {
2022 					btc8723b2ant_ps_tdma(btcoexist,
2023 							     NORMAL_EXEC,
2024 							     true, 6);
2025 					coex_dm->ps_tdma_du_adj_type = 6;
2026 				} else if (coex_dm->cur_ps_tdma == 3) {
2027 					btc8723b2ant_ps_tdma(btcoexist,
2028 							     NORMAL_EXEC,
2029 							     true, 7);
2030 					coex_dm->ps_tdma_du_adj_type = 7;
2031 				} else if (coex_dm->cur_ps_tdma == 4) {
2032 					btc8723b2ant_ps_tdma(btcoexist,
2033 							     NORMAL_EXEC,
2034 							     true, 8);
2035 					coex_dm->ps_tdma_du_adj_type = 8;
2036 				}
2037 				if (coex_dm->cur_ps_tdma == 9) {
2038 					btc8723b2ant_ps_tdma(btcoexist,
2039 							     NORMAL_EXEC,
2040 							     true, 13);
2041 					coex_dm->ps_tdma_du_adj_type = 13;
2042 				} else if (coex_dm->cur_ps_tdma == 10) {
2043 					btc8723b2ant_ps_tdma(btcoexist,
2044 							     NORMAL_EXEC,
2045 							     true, 14);
2046 					coex_dm->ps_tdma_du_adj_type = 14;
2047 				} else if (coex_dm->cur_ps_tdma == 11) {
2048 					btc8723b2ant_ps_tdma(btcoexist,
2049 							     NORMAL_EXEC,
2050 							     true, 15);
2051 					coex_dm->ps_tdma_du_adj_type = 15;
2052 				} else if (coex_dm->cur_ps_tdma == 12) {
2053 					btc8723b2ant_ps_tdma(btcoexist,
2054 							     NORMAL_EXEC,
2055 							     true, 16);
2056 					coex_dm->ps_tdma_du_adj_type = 16;
2057 				}
2058 
2059 				if (result == -1) {
2060 					if (coex_dm->cur_ps_tdma == 5) {
2061 						btc8723b2ant_ps_tdma(
2062 							btcoexist, NORMAL_EXEC,
2063 							true, 6);
2064 						coex_dm->ps_tdma_du_adj_type =
2065 							6;
2066 					} else if (coex_dm->cur_ps_tdma == 6) {
2067 						btc8723b2ant_ps_tdma(
2068 							btcoexist, NORMAL_EXEC,
2069 							true, 7);
2070 						coex_dm->ps_tdma_du_adj_type =
2071 							7;
2072 					} else if (coex_dm->cur_ps_tdma == 7) {
2073 						btc8723b2ant_ps_tdma(
2074 							btcoexist, NORMAL_EXEC,
2075 							true, 8);
2076 						coex_dm->ps_tdma_du_adj_type =
2077 							8;
2078 					} else if (coex_dm->cur_ps_tdma == 13) {
2079 						btc8723b2ant_ps_tdma(
2080 							btcoexist, NORMAL_EXEC,
2081 							true, 14);
2082 						coex_dm->ps_tdma_du_adj_type =
2083 							14;
2084 					} else if (coex_dm->cur_ps_tdma == 14) {
2085 						btc8723b2ant_ps_tdma(
2086 							btcoexist, NORMAL_EXEC,
2087 							true, 15);
2088 						coex_dm->ps_tdma_du_adj_type =
2089 							15;
2090 					} else if (coex_dm->cur_ps_tdma == 15) {
2091 						btc8723b2ant_ps_tdma(
2092 							btcoexist, NORMAL_EXEC,
2093 							true, 16);
2094 						coex_dm->ps_tdma_du_adj_type =
2095 							16;
2096 					}
2097 				} else if (result == 1) {
2098 					if (coex_dm->cur_ps_tdma == 8) {
2099 						btc8723b2ant_ps_tdma(
2100 							btcoexist, NORMAL_EXEC,
2101 							true, 7);
2102 						coex_dm->ps_tdma_du_adj_type =
2103 							7;
2104 					} else if (coex_dm->cur_ps_tdma == 7) {
2105 						btc8723b2ant_ps_tdma(
2106 							btcoexist, NORMAL_EXEC,
2107 							true, 6);
2108 						coex_dm->ps_tdma_du_adj_type =
2109 							6;
2110 					} else if (coex_dm->cur_ps_tdma == 6) {
2111 						btc8723b2ant_ps_tdma(
2112 							btcoexist, NORMAL_EXEC,
2113 							true, 5);
2114 						coex_dm->ps_tdma_du_adj_type =
2115 							5;
2116 					} else if (coex_dm->cur_ps_tdma == 16) {
2117 						btc8723b2ant_ps_tdma(
2118 							btcoexist, NORMAL_EXEC,
2119 							true, 15);
2120 						coex_dm->ps_tdma_du_adj_type =
2121 							15;
2122 					} else if (coex_dm->cur_ps_tdma == 15) {
2123 						btc8723b2ant_ps_tdma(
2124 							btcoexist, NORMAL_EXEC,
2125 							true, 14);
2126 						coex_dm->ps_tdma_du_adj_type =
2127 							14;
2128 					} else if (coex_dm->cur_ps_tdma == 14) {
2129 						btc8723b2ant_ps_tdma(
2130 							btcoexist, NORMAL_EXEC,
2131 							true, 13);
2132 						coex_dm->ps_tdma_du_adj_type =
2133 							13;
2134 					}
2135 				}
2136 			} else {
2137 				if (coex_dm->cur_ps_tdma == 5) {
2138 					btc8723b2ant_ps_tdma(btcoexist,
2139 							     NORMAL_EXEC,
2140 							     true, 71);
2141 					coex_dm->ps_tdma_du_adj_type = 71;
2142 				} else if (coex_dm->cur_ps_tdma == 6) {
2143 					btc8723b2ant_ps_tdma(btcoexist,
2144 							     NORMAL_EXEC,
2145 							     true, 2);
2146 					coex_dm->ps_tdma_du_adj_type = 2;
2147 				} else if (coex_dm->cur_ps_tdma == 7) {
2148 					btc8723b2ant_ps_tdma(btcoexist,
2149 							     NORMAL_EXEC,
2150 							     true, 3);
2151 					coex_dm->ps_tdma_du_adj_type = 3;
2152 				} else if (coex_dm->cur_ps_tdma == 8) {
2153 					btc8723b2ant_ps_tdma(btcoexist,
2154 							     NORMAL_EXEC,
2155 							     true, 4);
2156 					coex_dm->ps_tdma_du_adj_type = 4;
2157 				}
2158 				if (coex_dm->cur_ps_tdma == 13) {
2159 					btc8723b2ant_ps_tdma(btcoexist,
2160 							     NORMAL_EXEC,
2161 							     true, 9);
2162 					coex_dm->ps_tdma_du_adj_type = 9;
2163 				} else if (coex_dm->cur_ps_tdma == 14) {
2164 					btc8723b2ant_ps_tdma(btcoexist,
2165 							     NORMAL_EXEC,
2166 							     true, 10);
2167 					coex_dm->ps_tdma_du_adj_type = 10;
2168 				} else if (coex_dm->cur_ps_tdma == 15) {
2169 					btc8723b2ant_ps_tdma(btcoexist,
2170 							     NORMAL_EXEC,
2171 							     true, 11);
2172 					coex_dm->ps_tdma_du_adj_type = 11;
2173 				} else if (coex_dm->cur_ps_tdma == 16) {
2174 					btc8723b2ant_ps_tdma(btcoexist,
2175 							     NORMAL_EXEC,
2176 							     true, 12);
2177 					coex_dm->ps_tdma_du_adj_type = 12;
2178 				}
2179 
2180 				if (result == -1) {
2181 					if (coex_dm->cur_ps_tdma == 71) {
2182 						btc8723b2ant_ps_tdma(
2183 							btcoexist, NORMAL_EXEC,
2184 							true, 1);
2185 						coex_dm->ps_tdma_du_adj_type =
2186 							1;
2187 					} else if (coex_dm->cur_ps_tdma == 1) {
2188 						btc8723b2ant_ps_tdma(
2189 							btcoexist, NORMAL_EXEC,
2190 							true, 2);
2191 						coex_dm->ps_tdma_du_adj_type =
2192 							2;
2193 					} else if (coex_dm->cur_ps_tdma == 2) {
2194 						btc8723b2ant_ps_tdma(
2195 							btcoexist, NORMAL_EXEC,
2196 							true, 3);
2197 						coex_dm->ps_tdma_du_adj_type =
2198 							3;
2199 					} else if (coex_dm->cur_ps_tdma == 3) {
2200 						btc8723b2ant_ps_tdma(
2201 							btcoexist, NORMAL_EXEC,
2202 							true, 4);
2203 						coex_dm->ps_tdma_du_adj_type =
2204 							4;
2205 					} else if (coex_dm->cur_ps_tdma == 9) {
2206 						btc8723b2ant_ps_tdma(
2207 							btcoexist, NORMAL_EXEC,
2208 							true, 10);
2209 						coex_dm->ps_tdma_du_adj_type =
2210 							10;
2211 					} else if (coex_dm->cur_ps_tdma == 10) {
2212 						btc8723b2ant_ps_tdma(
2213 							btcoexist, NORMAL_EXEC,
2214 							true, 11);
2215 						coex_dm->ps_tdma_du_adj_type =
2216 							11;
2217 					} else if (coex_dm->cur_ps_tdma == 11) {
2218 						btc8723b2ant_ps_tdma(
2219 							btcoexist, NORMAL_EXEC,
2220 							true, 12);
2221 						coex_dm->ps_tdma_du_adj_type =
2222 							12;
2223 					}
2224 				} else if (result == 1) {
2225 					if (coex_dm->cur_ps_tdma == 4) {
2226 						btc8723b2ant_ps_tdma(
2227 							btcoexist, NORMAL_EXEC,
2228 							true, 3);
2229 						coex_dm->ps_tdma_du_adj_type =
2230 							3;
2231 					} else if (coex_dm->cur_ps_tdma == 3) {
2232 						btc8723b2ant_ps_tdma(
2233 							btcoexist, NORMAL_EXEC,
2234 							true, 2);
2235 						coex_dm->ps_tdma_du_adj_type =
2236 							2;
2237 					} else if (coex_dm->cur_ps_tdma == 2) {
2238 						btc8723b2ant_ps_tdma(
2239 							btcoexist, NORMAL_EXEC,
2240 							true, 1);
2241 						coex_dm->ps_tdma_du_adj_type =
2242 							1;
2243 					} else if (coex_dm->cur_ps_tdma == 1) {
2244 						btc8723b2ant_ps_tdma(
2245 							btcoexist, NORMAL_EXEC,
2246 							true, 71);
2247 						coex_dm->ps_tdma_du_adj_type =
2248 							71;
2249 					} else if (coex_dm->cur_ps_tdma == 12) {
2250 						btc8723b2ant_ps_tdma(
2251 							btcoexist, NORMAL_EXEC,
2252 							true, 11);
2253 						coex_dm->ps_tdma_du_adj_type =
2254 							11;
2255 					} else if (coex_dm->cur_ps_tdma == 11) {
2256 						btc8723b2ant_ps_tdma(
2257 							btcoexist, NORMAL_EXEC,
2258 							true, 10);
2259 						coex_dm->ps_tdma_du_adj_type =
2260 							10;
2261 					} else if (coex_dm->cur_ps_tdma == 10) {
2262 						btc8723b2ant_ps_tdma(
2263 							btcoexist, NORMAL_EXEC,
2264 							true, 9);
2265 						coex_dm->ps_tdma_du_adj_type =
2266 							9;
2267 					}
2268 				}
2269 			}
2270 		} else if (max_interval == 2) {
2271 			if (tx_pause) {
2272 				if (coex_dm->cur_ps_tdma == 1) {
2273 					btc8723b2ant_ps_tdma(btcoexist,
2274 							     NORMAL_EXEC,
2275 							     true, 6);
2276 					coex_dm->ps_tdma_du_adj_type = 6;
2277 				} else if (coex_dm->cur_ps_tdma == 2) {
2278 					btc8723b2ant_ps_tdma(btcoexist,
2279 							     NORMAL_EXEC,
2280 							     true, 6);
2281 					coex_dm->ps_tdma_du_adj_type = 6;
2282 				} else if (coex_dm->cur_ps_tdma == 3) {
2283 					btc8723b2ant_ps_tdma(btcoexist,
2284 							     NORMAL_EXEC,
2285 							     true, 7);
2286 					coex_dm->ps_tdma_du_adj_type = 7;
2287 				} else if (coex_dm->cur_ps_tdma == 4) {
2288 					btc8723b2ant_ps_tdma(btcoexist,
2289 							     NORMAL_EXEC,
2290 							     true, 8);
2291 					coex_dm->ps_tdma_du_adj_type = 8;
2292 				}
2293 				if (coex_dm->cur_ps_tdma == 9) {
2294 					btc8723b2ant_ps_tdma(btcoexist,
2295 							     NORMAL_EXEC,
2296 							     true, 14);
2297 					coex_dm->ps_tdma_du_adj_type = 14;
2298 				} else if (coex_dm->cur_ps_tdma == 10) {
2299 					btc8723b2ant_ps_tdma(btcoexist,
2300 							     NORMAL_EXEC,
2301 							     true, 14);
2302 					coex_dm->ps_tdma_du_adj_type = 14;
2303 				} else if (coex_dm->cur_ps_tdma == 11) {
2304 					btc8723b2ant_ps_tdma(btcoexist,
2305 							     NORMAL_EXEC,
2306 							     true, 15);
2307 					coex_dm->ps_tdma_du_adj_type = 15;
2308 				} else if (coex_dm->cur_ps_tdma == 12) {
2309 					btc8723b2ant_ps_tdma(btcoexist,
2310 							     NORMAL_EXEC,
2311 							     true, 16);
2312 					coex_dm->ps_tdma_du_adj_type = 16;
2313 				}
2314 				if (result == -1) {
2315 					if (coex_dm->cur_ps_tdma == 5) {
2316 						btc8723b2ant_ps_tdma(
2317 							btcoexist, NORMAL_EXEC,
2318 							true, 6);
2319 						coex_dm->ps_tdma_du_adj_type =
2320 							6;
2321 					} else if (coex_dm->cur_ps_tdma == 6) {
2322 						btc8723b2ant_ps_tdma(
2323 							btcoexist, NORMAL_EXEC,
2324 							true, 7);
2325 						coex_dm->ps_tdma_du_adj_type =
2326 							7;
2327 					} else if (coex_dm->cur_ps_tdma == 7) {
2328 						btc8723b2ant_ps_tdma(
2329 							btcoexist, NORMAL_EXEC,
2330 							true, 8);
2331 						coex_dm->ps_tdma_du_adj_type =
2332 							8;
2333 					} else if (coex_dm->cur_ps_tdma == 13) {
2334 						btc8723b2ant_ps_tdma(
2335 							btcoexist, NORMAL_EXEC,
2336 							true, 14);
2337 						coex_dm->ps_tdma_du_adj_type =
2338 							14;
2339 					} else if (coex_dm->cur_ps_tdma == 14) {
2340 						btc8723b2ant_ps_tdma(
2341 							btcoexist, NORMAL_EXEC,
2342 							true, 15);
2343 						coex_dm->ps_tdma_du_adj_type =
2344 							15;
2345 					} else if (coex_dm->cur_ps_tdma == 15) {
2346 						btc8723b2ant_ps_tdma(
2347 							btcoexist, NORMAL_EXEC,
2348 							true, 16);
2349 						coex_dm->ps_tdma_du_adj_type =
2350 							16;
2351 					}
2352 				} else if (result == 1) {
2353 					if (coex_dm->cur_ps_tdma == 8) {
2354 						btc8723b2ant_ps_tdma(
2355 							btcoexist, NORMAL_EXEC,
2356 							true, 7);
2357 						coex_dm->ps_tdma_du_adj_type =
2358 							7;
2359 					} else if (coex_dm->cur_ps_tdma == 7) {
2360 						btc8723b2ant_ps_tdma(
2361 							btcoexist, NORMAL_EXEC,
2362 							true, 6);
2363 						coex_dm->ps_tdma_du_adj_type =
2364 							6;
2365 					} else if (coex_dm->cur_ps_tdma == 6) {
2366 						btc8723b2ant_ps_tdma(
2367 							btcoexist, NORMAL_EXEC,
2368 							true, 6);
2369 						coex_dm->ps_tdma_du_adj_type =
2370 							6;
2371 					} else if (coex_dm->cur_ps_tdma == 16) {
2372 						btc8723b2ant_ps_tdma(
2373 							btcoexist, NORMAL_EXEC,
2374 							true, 15);
2375 						coex_dm->ps_tdma_du_adj_type =
2376 							15;
2377 					} else if (coex_dm->cur_ps_tdma == 15) {
2378 						btc8723b2ant_ps_tdma(
2379 							btcoexist, NORMAL_EXEC,
2380 							true, 14);
2381 						coex_dm->ps_tdma_du_adj_type =
2382 							14;
2383 					} else if (coex_dm->cur_ps_tdma == 14) {
2384 						btc8723b2ant_ps_tdma(
2385 							btcoexist, NORMAL_EXEC,
2386 							true, 14);
2387 						coex_dm->ps_tdma_du_adj_type =
2388 							14;
2389 					}
2390 				}
2391 			} else {
2392 				if (coex_dm->cur_ps_tdma == 5) {
2393 					btc8723b2ant_ps_tdma(btcoexist,
2394 							     NORMAL_EXEC,
2395 							     true, 2);
2396 					coex_dm->ps_tdma_du_adj_type = 2;
2397 				} else if (coex_dm->cur_ps_tdma == 6) {
2398 					btc8723b2ant_ps_tdma(btcoexist,
2399 							     NORMAL_EXEC,
2400 							     true, 2);
2401 					coex_dm->ps_tdma_du_adj_type = 2;
2402 				} else if (coex_dm->cur_ps_tdma == 7) {
2403 					btc8723b2ant_ps_tdma(btcoexist,
2404 							     NORMAL_EXEC,
2405 							     true, 3);
2406 					coex_dm->ps_tdma_du_adj_type = 3;
2407 				} else if (coex_dm->cur_ps_tdma == 8) {
2408 					btc8723b2ant_ps_tdma(btcoexist,
2409 							     NORMAL_EXEC,
2410 							     true, 4);
2411 					coex_dm->ps_tdma_du_adj_type = 4;
2412 				}
2413 				if (coex_dm->cur_ps_tdma == 13) {
2414 					btc8723b2ant_ps_tdma(btcoexist,
2415 							     NORMAL_EXEC,
2416 							     true, 10);
2417 					coex_dm->ps_tdma_du_adj_type = 10;
2418 				} else if (coex_dm->cur_ps_tdma == 14) {
2419 					btc8723b2ant_ps_tdma(btcoexist,
2420 							     NORMAL_EXEC,
2421 							     true, 10);
2422 					coex_dm->ps_tdma_du_adj_type = 10;
2423 				} else if (coex_dm->cur_ps_tdma == 15) {
2424 					btc8723b2ant_ps_tdma(btcoexist,
2425 							     NORMAL_EXEC,
2426 							     true, 11);
2427 					coex_dm->ps_tdma_du_adj_type = 11;
2428 				} else if (coex_dm->cur_ps_tdma == 16) {
2429 					btc8723b2ant_ps_tdma(btcoexist,
2430 							     NORMAL_EXEC,
2431 							     true, 12);
2432 					coex_dm->ps_tdma_du_adj_type = 12;
2433 				}
2434 				if (result == -1) {
2435 					if (coex_dm->cur_ps_tdma == 1) {
2436 						btc8723b2ant_ps_tdma(
2437 							btcoexist, NORMAL_EXEC,
2438 							true, 2);
2439 						coex_dm->ps_tdma_du_adj_type =
2440 							2;
2441 					} else if (coex_dm->cur_ps_tdma == 2) {
2442 						btc8723b2ant_ps_tdma(
2443 							btcoexist, NORMAL_EXEC,
2444 							true, 3);
2445 						coex_dm->ps_tdma_du_adj_type =
2446 							3;
2447 					} else if (coex_dm->cur_ps_tdma == 3) {
2448 						btc8723b2ant_ps_tdma(
2449 							btcoexist, NORMAL_EXEC,
2450 							true, 4);
2451 						coex_dm->ps_tdma_du_adj_type =
2452 							4;
2453 					} else if (coex_dm->cur_ps_tdma == 9) {
2454 						btc8723b2ant_ps_tdma(
2455 							btcoexist, NORMAL_EXEC,
2456 							true, 10);
2457 						coex_dm->ps_tdma_du_adj_type =
2458 							10;
2459 					} else if (coex_dm->cur_ps_tdma == 10) {
2460 						btc8723b2ant_ps_tdma(
2461 							btcoexist, NORMAL_EXEC,
2462 							true, 11);
2463 						coex_dm->ps_tdma_du_adj_type =
2464 							11;
2465 					} else if (coex_dm->cur_ps_tdma == 11) {
2466 						btc8723b2ant_ps_tdma(
2467 							btcoexist, NORMAL_EXEC,
2468 							true, 12);
2469 						coex_dm->ps_tdma_du_adj_type =
2470 							12;
2471 					}
2472 				} else if (result == 1) {
2473 					if (coex_dm->cur_ps_tdma == 4) {
2474 						btc8723b2ant_ps_tdma(
2475 							btcoexist, NORMAL_EXEC,
2476 							true, 3);
2477 						coex_dm->ps_tdma_du_adj_type =
2478 							3;
2479 					} else if (coex_dm->cur_ps_tdma == 3) {
2480 						btc8723b2ant_ps_tdma(
2481 							btcoexist, NORMAL_EXEC,
2482 							true, 2);
2483 						coex_dm->ps_tdma_du_adj_type =
2484 							2;
2485 					} else if (coex_dm->cur_ps_tdma == 2) {
2486 						btc8723b2ant_ps_tdma(
2487 							btcoexist, NORMAL_EXEC,
2488 							true, 2);
2489 						coex_dm->ps_tdma_du_adj_type =
2490 							2;
2491 					} else if (coex_dm->cur_ps_tdma == 12) {
2492 						btc8723b2ant_ps_tdma(
2493 							btcoexist, NORMAL_EXEC,
2494 							true, 11);
2495 						coex_dm->ps_tdma_du_adj_type =
2496 							11;
2497 					} else if (coex_dm->cur_ps_tdma == 11) {
2498 						btc8723b2ant_ps_tdma(
2499 							btcoexist, NORMAL_EXEC,
2500 							true, 10);
2501 						coex_dm->ps_tdma_du_adj_type =
2502 							10;
2503 					} else if (coex_dm->cur_ps_tdma == 10) {
2504 						btc8723b2ant_ps_tdma(
2505 							btcoexist, NORMAL_EXEC,
2506 							true, 10);
2507 						coex_dm->ps_tdma_du_adj_type =
2508 							10;
2509 					}
2510 				}
2511 			}
2512 		} else if (max_interval == 3) {
2513 			if (tx_pause) {
2514 				if (coex_dm->cur_ps_tdma == 1) {
2515 					btc8723b2ant_ps_tdma(btcoexist,
2516 							     NORMAL_EXEC,
2517 							     true, 7);
2518 					coex_dm->ps_tdma_du_adj_type = 7;
2519 				} else if (coex_dm->cur_ps_tdma == 2) {
2520 					btc8723b2ant_ps_tdma(btcoexist,
2521 							     NORMAL_EXEC,
2522 							     true, 7);
2523 					coex_dm->ps_tdma_du_adj_type = 7;
2524 				} else if (coex_dm->cur_ps_tdma == 3) {
2525 					btc8723b2ant_ps_tdma(btcoexist,
2526 							     NORMAL_EXEC,
2527 							     true, 7);
2528 					coex_dm->ps_tdma_du_adj_type = 7;
2529 				} else if (coex_dm->cur_ps_tdma == 4) {
2530 					btc8723b2ant_ps_tdma(btcoexist,
2531 							     NORMAL_EXEC,
2532 							     true, 8);
2533 					coex_dm->ps_tdma_du_adj_type = 8;
2534 				}
2535 				if (coex_dm->cur_ps_tdma == 9) {
2536 					btc8723b2ant_ps_tdma(btcoexist,
2537 							     NORMAL_EXEC,
2538 							     true, 15);
2539 					coex_dm->ps_tdma_du_adj_type = 15;
2540 				} else if (coex_dm->cur_ps_tdma == 10) {
2541 					btc8723b2ant_ps_tdma(btcoexist,
2542 							     NORMAL_EXEC,
2543 							     true, 15);
2544 					coex_dm->ps_tdma_du_adj_type = 15;
2545 				} else if (coex_dm->cur_ps_tdma == 11) {
2546 					btc8723b2ant_ps_tdma(btcoexist,
2547 							     NORMAL_EXEC,
2548 							     true, 15);
2549 					coex_dm->ps_tdma_du_adj_type = 15;
2550 				} else if (coex_dm->cur_ps_tdma == 12) {
2551 					btc8723b2ant_ps_tdma(btcoexist,
2552 							     NORMAL_EXEC,
2553 							     true, 16);
2554 					coex_dm->ps_tdma_du_adj_type = 16;
2555 				}
2556 				if (result == -1) {
2557 					if (coex_dm->cur_ps_tdma == 5) {
2558 						btc8723b2ant_ps_tdma(
2559 							btcoexist, NORMAL_EXEC,
2560 							true, 7);
2561 						coex_dm->ps_tdma_du_adj_type =
2562 							7;
2563 					} else if (coex_dm->cur_ps_tdma == 6) {
2564 						btc8723b2ant_ps_tdma(
2565 							btcoexist, NORMAL_EXEC,
2566 							true, 7);
2567 						coex_dm->ps_tdma_du_adj_type =
2568 							7;
2569 					} else if (coex_dm->cur_ps_tdma == 7) {
2570 						btc8723b2ant_ps_tdma(
2571 							btcoexist, NORMAL_EXEC,
2572 							true, 8);
2573 						coex_dm->ps_tdma_du_adj_type =
2574 							8;
2575 					} else if (coex_dm->cur_ps_tdma == 13) {
2576 						btc8723b2ant_ps_tdma(
2577 							btcoexist, NORMAL_EXEC,
2578 							true, 15);
2579 						coex_dm->ps_tdma_du_adj_type =
2580 							15;
2581 					} else if (coex_dm->cur_ps_tdma == 14) {
2582 						btc8723b2ant_ps_tdma(
2583 							btcoexist, NORMAL_EXEC,
2584 							true, 15);
2585 						coex_dm->ps_tdma_du_adj_type =
2586 							15;
2587 					} else if (coex_dm->cur_ps_tdma == 15) {
2588 						btc8723b2ant_ps_tdma(
2589 							btcoexist, NORMAL_EXEC,
2590 							true, 16);
2591 						coex_dm->ps_tdma_du_adj_type =
2592 							16;
2593 					}
2594 				} else if (result == 1) {
2595 					if (coex_dm->cur_ps_tdma == 8) {
2596 						btc8723b2ant_ps_tdma(
2597 							btcoexist, NORMAL_EXEC,
2598 							true, 7);
2599 						coex_dm->ps_tdma_du_adj_type =
2600 							7;
2601 					} else if (coex_dm->cur_ps_tdma == 7) {
2602 						btc8723b2ant_ps_tdma(
2603 							btcoexist, NORMAL_EXEC,
2604 							true, 7);
2605 						coex_dm->ps_tdma_du_adj_type =
2606 							7;
2607 					} else if (coex_dm->cur_ps_tdma == 6) {
2608 						btc8723b2ant_ps_tdma(
2609 							btcoexist, NORMAL_EXEC,
2610 							true, 7);
2611 						coex_dm->ps_tdma_du_adj_type =
2612 							7;
2613 					} else if (coex_dm->cur_ps_tdma == 16) {
2614 						btc8723b2ant_ps_tdma(
2615 							btcoexist, NORMAL_EXEC,
2616 							true, 15);
2617 						coex_dm->ps_tdma_du_adj_type =
2618 							15;
2619 					} else if (coex_dm->cur_ps_tdma == 15) {
2620 						btc8723b2ant_ps_tdma(
2621 							btcoexist, NORMAL_EXEC,
2622 							true, 15);
2623 						coex_dm->ps_tdma_du_adj_type =
2624 							15;
2625 					} else if (coex_dm->cur_ps_tdma == 14) {
2626 						btc8723b2ant_ps_tdma(
2627 							btcoexist, NORMAL_EXEC,
2628 							true, 15);
2629 						coex_dm->ps_tdma_du_adj_type =
2630 							15;
2631 					}
2632 				}
2633 			} else {
2634 				if (coex_dm->cur_ps_tdma == 5) {
2635 					btc8723b2ant_ps_tdma(btcoexist,
2636 							     NORMAL_EXEC,
2637 							     true, 3);
2638 					coex_dm->ps_tdma_du_adj_type = 3;
2639 				} else if (coex_dm->cur_ps_tdma == 6) {
2640 					btc8723b2ant_ps_tdma(btcoexist,
2641 							     NORMAL_EXEC,
2642 							     true, 3);
2643 					coex_dm->ps_tdma_du_adj_type = 3;
2644 				} else if (coex_dm->cur_ps_tdma == 7) {
2645 					btc8723b2ant_ps_tdma(btcoexist,
2646 							     NORMAL_EXEC,
2647 							     true, 3);
2648 					coex_dm->ps_tdma_du_adj_type = 3;
2649 				} else if (coex_dm->cur_ps_tdma == 8) {
2650 					btc8723b2ant_ps_tdma(btcoexist,
2651 							     NORMAL_EXEC,
2652 							     true, 4);
2653 					coex_dm->ps_tdma_du_adj_type = 4;
2654 				}
2655 				if (coex_dm->cur_ps_tdma == 13) {
2656 					btc8723b2ant_ps_tdma(btcoexist,
2657 							     NORMAL_EXEC,
2658 							     true, 11);
2659 					coex_dm->ps_tdma_du_adj_type = 11;
2660 				} else if (coex_dm->cur_ps_tdma == 14) {
2661 					btc8723b2ant_ps_tdma(btcoexist,
2662 							     NORMAL_EXEC,
2663 							     true, 11);
2664 					coex_dm->ps_tdma_du_adj_type = 11;
2665 				} else if (coex_dm->cur_ps_tdma == 15) {
2666 					btc8723b2ant_ps_tdma(btcoexist,
2667 							     NORMAL_EXEC,
2668 							     true, 11);
2669 					coex_dm->ps_tdma_du_adj_type = 11;
2670 				} else if (coex_dm->cur_ps_tdma == 16) {
2671 					btc8723b2ant_ps_tdma(btcoexist,
2672 							     NORMAL_EXEC,
2673 							     true, 12);
2674 					coex_dm->ps_tdma_du_adj_type = 12;
2675 				}
2676 				if (result == -1) {
2677 					if (coex_dm->cur_ps_tdma == 1) {
2678 						btc8723b2ant_ps_tdma(
2679 							btcoexist, NORMAL_EXEC,
2680 							true, 3);
2681 						coex_dm->ps_tdma_du_adj_type =
2682 							3;
2683 					} else if (coex_dm->cur_ps_tdma == 2) {
2684 						btc8723b2ant_ps_tdma(
2685 							btcoexist, NORMAL_EXEC,
2686 							true, 3);
2687 						coex_dm->ps_tdma_du_adj_type =
2688 							3;
2689 					} else if (coex_dm->cur_ps_tdma == 3) {
2690 						btc8723b2ant_ps_tdma(
2691 							btcoexist, NORMAL_EXEC,
2692 							true, 4);
2693 						coex_dm->ps_tdma_du_adj_type =
2694 							4;
2695 					} else if (coex_dm->cur_ps_tdma == 9) {
2696 						btc8723b2ant_ps_tdma(
2697 							btcoexist, NORMAL_EXEC,
2698 							true, 11);
2699 						coex_dm->ps_tdma_du_adj_type =
2700 							11;
2701 					} else if (coex_dm->cur_ps_tdma == 10) {
2702 						btc8723b2ant_ps_tdma(
2703 							btcoexist, NORMAL_EXEC,
2704 							true, 11);
2705 						coex_dm->ps_tdma_du_adj_type =
2706 							11;
2707 					} else if (coex_dm->cur_ps_tdma == 11) {
2708 						btc8723b2ant_ps_tdma(
2709 							btcoexist, NORMAL_EXEC,
2710 							true, 12);
2711 						coex_dm->ps_tdma_du_adj_type =
2712 							12;
2713 					}
2714 				} else if (result == 1) {
2715 					if (coex_dm->cur_ps_tdma == 4) {
2716 						btc8723b2ant_ps_tdma(
2717 							btcoexist, NORMAL_EXEC,
2718 							true, 3);
2719 						coex_dm->ps_tdma_du_adj_type =
2720 							3;
2721 					} else if (coex_dm->cur_ps_tdma == 3) {
2722 						btc8723b2ant_ps_tdma(
2723 							btcoexist, NORMAL_EXEC,
2724 							true, 3);
2725 						coex_dm->ps_tdma_du_adj_type =
2726 							3;
2727 					} else if (coex_dm->cur_ps_tdma == 2) {
2728 						btc8723b2ant_ps_tdma(
2729 							btcoexist, NORMAL_EXEC,
2730 							true, 3);
2731 						coex_dm->ps_tdma_du_adj_type =
2732 							3;
2733 					} else if (coex_dm->cur_ps_tdma == 12) {
2734 						btc8723b2ant_ps_tdma(
2735 							btcoexist, NORMAL_EXEC,
2736 							true, 11);
2737 						coex_dm->ps_tdma_du_adj_type =
2738 							11;
2739 					} else if (coex_dm->cur_ps_tdma == 11) {
2740 						btc8723b2ant_ps_tdma(
2741 							btcoexist, NORMAL_EXEC,
2742 							true, 11);
2743 						coex_dm->ps_tdma_du_adj_type =
2744 							11;
2745 					} else if (coex_dm->cur_ps_tdma == 10) {
2746 						btc8723b2ant_ps_tdma(
2747 							btcoexist, NORMAL_EXEC,
2748 							true, 11);
2749 						coex_dm->ps_tdma_du_adj_type =
2750 							11;
2751 					}
2752 				}
2753 			}
2754 		}
2755 	}
2756 
2757 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2758 		 "[BTCoex], max Interval = %d\n", max_interval);
2759 
2760 	/* if current PsTdma not match with the recorded one (scan, dhcp, ...),
2761 	 * then we have to adjust it back to the previous recorded one.
2762 	 */
2763 	if (coex_dm->cur_ps_tdma != coex_dm->ps_tdma_du_adj_type) {
2764 		bool scan = false, link = false, roam = false;
2765 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2766 			 "[BTCoex], PsTdma type dismatch!!!, curPsTdma=%d, recordPsTdma=%d\n",
2767 			 coex_dm->cur_ps_tdma, coex_dm->ps_tdma_du_adj_type);
2768 
2769 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2770 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2771 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2772 
2773 		if (!scan && !link && !roam)
2774 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
2775 					     coex_dm->ps_tdma_du_adj_type);
2776 		else
2777 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2778 				 "[BTCoex], roaming/link/scan is under progress, will adjust next time!!!\n");
2779 	}
2780 }
2781 
2782 /* SCO only or SCO+PAN(HS) */
2783 static void btc8723b2ant_action_sco(struct btc_coexist *btcoexist)
2784 {
2785 	u8 wifi_rssi_state, bt_rssi_state;
2786 	u32 wifi_bw;
2787 
2788 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2789 	bt_rssi_state = btc8723b2ant_bt_rssi_state(
2790 		btcoexist, 2, BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2791 					       coex_dm->switch_thres_offset,
2792 		0);
2793 
2794 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2795 
2796 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2797 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 4);
2798 
2799 	if (BTC_RSSI_HIGH(bt_rssi_state))
2800 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2801 	else
2802 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2803 
2804 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2805 
2806 	if (BTC_WIFI_BW_LEGACY == wifi_bw)
2807 		/* for SCO quality at 11b/g mode */
2808 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2809 	else
2810 		/* for SCO quality & wifi performance balance at 11n mode */
2811 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 8);
2812 
2813 	/* for voice quality */
2814 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2815 
2816 	/* sw mechanism */
2817 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2818 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2819 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2820 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2821 						  false, false);
2822 		} else {
2823 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2824 						  false, false);
2825 		}
2826 	} else {
2827 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2828 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2829 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2830 						  false, false);
2831 		} else {
2832 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2833 						  false, false);
2834 		}
2835 	}
2836 }
2837 
2838 static void btc8723b2ant_action_hid(struct btc_coexist *btcoexist)
2839 {
2840 	u8 wifi_rssi_state, bt_rssi_state;
2841 	u32 wifi_bw;
2842 	u8 tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2843 			coex_dm->switch_thres_offset;
2844 
2845 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2846 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2847 
2848 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2849 
2850 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2851 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2852 
2853 	if (BTC_RSSI_HIGH(bt_rssi_state))
2854 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2855 	else
2856 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2857 
2858 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2859 
2860 	if (wifi_bw == BTC_WIFI_BW_LEGACY)
2861 		/* for HID at 11b/g mode */
2862 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2863 	else
2864 		/* for HID quality & wifi performance balance at 11n mode */
2865 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 9);
2866 
2867 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2868 
2869 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2870 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2871 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 9);
2872 	else
2873 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
2874 
2875 	/* sw mechanism */
2876 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2877 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2878 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2879 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2880 						  false, false);
2881 		} else {
2882 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
2883 						  false, false);
2884 		}
2885 	} else {
2886 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2887 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2888 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2889 						  false, false);
2890 		} else {
2891 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
2892 						  false, false);
2893 		}
2894 	}
2895 }
2896 
2897 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
2898 static void btc8723b2ant_action_a2dp(struct btc_coexist *btcoexist)
2899 {
2900 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2901 	u32 wifi_bw;
2902 	u8 ap_num = 0;
2903 	u8 tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2904 			coex_dm->switch_thres_offset;
2905 
2906 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2907 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2, 40, 0);
2908 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2909 
2910 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
2911 
2912 	/* define the office environment */
2913 	/* driver don't know AP num in Linux, so we will never enter this if */
2914 	if (ap_num >= 10 && BTC_RSSI_HIGH(wifi_rssi_state1)) {
2915 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
2916 					  0x0);
2917 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2918 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2919 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2920 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
2921 
2922 		/* sw mechanism */
2923 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2924 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
2925 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2926 						  false, false);
2927 		} else {
2928 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2929 						  false, false);
2930 		}
2931 		return;
2932 	}
2933 
2934 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2935 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
2936 
2937 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2938 
2939 	if (BTC_RSSI_HIGH(bt_rssi_state))
2940 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
2941 	else
2942 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
2943 
2944 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
2945 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2946 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2947 					      0x0, 0x0);
2948 	} else {
2949 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
2950 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
2951 					      0x4);
2952 	}
2953 
2954 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2955 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2956 		btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2957 						  false, 1);
2958 	else
2959 		btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 1);
2960 
2961 	/* sw mechanism */
2962 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2963 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2964 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2965 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2966 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2967 						  false, false);
2968 		} else {
2969 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
2970 						  false, false);
2971 		}
2972 	} else {
2973 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2974 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2975 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2976 						  false, false);
2977 		} else {
2978 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
2979 						  false, false);
2980 		}
2981 	}
2982 }
2983 
2984 static void btc8723b2ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
2985 {
2986 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2987 	u32 wifi_bw;
2988 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
2989 			coex_dm->switch_thres_offset;
2990 
2991 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
2992 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
2993 							tmp, 0);
2994 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
2995 			coex_dm->switch_thres_offset;
2996 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
2997 
2998 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2999 
3000 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3001 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3002 
3003 	if (BTC_RSSI_HIGH(bt_rssi_state))
3004 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3005 	else
3006 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3007 
3008 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3009 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3010 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3011 					      0x0, 0x0);
3012 	} else {
3013 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3014 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3015 					      0x4);
3016 	}
3017 
3018 	btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 2);
3019 
3020 	/* sw mechanism */
3021 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3022 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3023 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3024 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3025 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3026 						  false, false);
3027 		} else {
3028 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3029 						  false, false);
3030 		}
3031 	} else {
3032 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3033 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3034 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3035 						  false, false);
3036 		} else {
3037 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3038 						  false, false);
3039 		}
3040 	}
3041 }
3042 
3043 static void btc8723b2ant_action_pan_edr(struct btc_coexist *btcoexist)
3044 {
3045 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3046 	u32 wifi_bw;
3047 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3048 			coex_dm->switch_thres_offset;
3049 
3050 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3051 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3052 							tmp, 0);
3053 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3054 			coex_dm->switch_thres_offset;
3055 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3056 
3057 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3058 
3059 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3060 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3061 
3062 	if (BTC_RSSI_HIGH(bt_rssi_state))
3063 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3064 	else
3065 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3066 
3067 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3068 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 10);
3069 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3070 					      0x0, 0x0);
3071 	} else {
3072 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 13);
3073 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3074 					      0x4);
3075 	}
3076 
3077 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3078 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
3079 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 1);
3080 	else
3081 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
3082 
3083 	/* sw mechanism */
3084 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3085 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3086 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3087 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3088 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3089 						  false, false);
3090 		} else {
3091 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3092 						  false, false);
3093 		}
3094 	} else {
3095 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3096 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3097 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3098 						  false, false);
3099 		} else {
3100 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3101 						  false, false);
3102 		}
3103 	}
3104 }
3105 
3106 /* PAN(HS) only */
3107 static void btc8723b2ant_action_pan_hs(struct btc_coexist *btcoexist)
3108 {
3109 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3110 	u32 wifi_bw;
3111 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3112 			coex_dm->switch_thres_offset;
3113 
3114 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3115 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3116 							tmp, 0);
3117 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3118 			coex_dm->switch_thres_offset;
3119 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3120 
3121 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3122 
3123 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3124 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3125 
3126 	if (BTC_RSSI_HIGH(bt_rssi_state))
3127 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3128 	else
3129 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3130 
3131 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3132 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3133 
3134 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3135 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3136 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3137 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3138 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3139 						  false, false);
3140 		} else {
3141 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3142 						  false, false);
3143 		}
3144 	} else {
3145 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3146 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3147 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3148 						  false, false);
3149 		} else {
3150 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3151 						  false, false);
3152 		}
3153 	}
3154 }
3155 
3156 /* PAN(EDR) + A2DP */
3157 static void btc8723b2ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
3158 {
3159 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3160 	u32 wifi_bw;
3161 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3162 			coex_dm->switch_thres_offset;
3163 
3164 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3165 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3166 							tmp, 0);
3167 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3168 			coex_dm->switch_thres_offset;
3169 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3170 
3171 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3172 
3173 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3174 
3175 	if (BTC_RSSI_HIGH(bt_rssi_state))
3176 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3177 	else
3178 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3179 
3180 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state))
3181 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3182 					      0x0, 0x0);
3183 	else
3184 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3185 					      0x4);
3186 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3187 
3188 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3189 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3190 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 12);
3191 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3192 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3193 							  true, 3);
3194 		else
3195 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
3196 							  false, 3);
3197 	} else {
3198 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3199 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
3200 	}
3201 
3202 	/* sw mechanism	*/
3203 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3204 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3205 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3206 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3207 						  false, false);
3208 		} else {
3209 			btc8723b2ant_sw_mechanism(btcoexist, true, false,
3210 						  false, false);
3211 		}
3212 	} else {
3213 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3214 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3215 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3216 						  false, false);
3217 		} else {
3218 			btc8723b2ant_sw_mechanism(btcoexist, false, false,
3219 						  false, false);
3220 		}
3221 	}
3222 }
3223 
3224 static void btc8723b2ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
3225 {
3226 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3227 	u32 wifi_bw;
3228 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3229 			coex_dm->switch_thres_offset;
3230 
3231 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3232 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3233 							tmp, 0);
3234 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3235 			coex_dm->switch_thres_offset;
3236 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3237 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3238 
3239 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3240 
3241 	if (BTC_RSSI_HIGH(bt_rssi_state))
3242 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3243 	else
3244 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3245 
3246 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3247 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3248 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3249 					      0x0, 0x0);
3250 	} else {
3251 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3252 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3253 					      0x4);
3254 	}
3255 
3256 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3257 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3258 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
3259 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3260 						      3);
3261 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3262 						  0xfffff, 0x780);
3263 		} else {
3264 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
3265 						      6);
3266 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
3267 						  0xfffff, 0x0);
3268 		}
3269 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, false, 2);
3270 	} else {
3271 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3272 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
3273 					  0x0);
3274 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 2);
3275 	}
3276 
3277 	/* sw mechanism */
3278 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3279 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3280 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3281 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3282 						  false, false);
3283 		} else {
3284 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3285 						  false, false);
3286 		}
3287 	} else {
3288 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3289 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3290 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3291 						  false, false);
3292 		} else {
3293 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3294 						  false, false);
3295 		}
3296 	}
3297 }
3298 
3299 /* HID + A2DP + PAN(EDR) */
3300 static void btc8723b2ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
3301 {
3302 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3303 	u32 wifi_bw;
3304 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3305 			coex_dm->switch_thres_offset;
3306 
3307 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3308 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3309 							tmp, 0);
3310 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3311 			coex_dm->switch_thres_offset;
3312 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, tmp, 0);
3313 
3314 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3315 
3316 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 0x8);
3317 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3318 
3319 	if (BTC_RSSI_HIGH(bt_rssi_state))
3320 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3321 	else
3322 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3323 
3324 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3325 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3326 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3327 					      0x0, 0x0);
3328 	} else {
3329 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3330 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3331 					      0x4);
3332 	}
3333 
3334 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3335 
3336 
3337 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
3338 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3339 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3340 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3341 							  true, 2);
3342 		else
3343 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3344 							  false, 3);
3345 	} else {
3346 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 3);
3347 	}
3348 
3349 	/* sw mechanism */
3350 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3351 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3352 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3353 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3354 						  false, false);
3355 		} else {
3356 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3357 						  false, false);
3358 		}
3359 	} else {
3360 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3361 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3362 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3363 						  false, false);
3364 		} else {
3365 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3366 						  false, false);
3367 		}
3368 	}
3369 }
3370 
3371 static void btc8723b2ant_action_hid_a2dp(struct btc_coexist *btcoexist)
3372 {
3373 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
3374 	u32 wifi_bw;
3375 	u8 ap_num = 0;
3376 	u8 tmp = BT_8723B_2ANT_WIFI_RSSI_COEXSWITCH_THRES -
3377 			coex_dm->switch_thres_offset;
3378 
3379 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist, 0, 2, 15, 0);
3380 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist, 1, 2,
3381 							tmp, 0);
3382 	tmp = BT_8723B_2ANT_BT_RSSI_COEXSWITCH_THRES -
3383 			 coex_dm->switch_thres_offset;
3384 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 3, tmp, 37);
3385 
3386 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3387 
3388 	btc8723b2ant_limited_rx(btcoexist, NORMAL_EXEC, false, true, 0x5);
3389 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3390 
3391 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3392 
3393 	if (wifi_bw == BTC_WIFI_BW_LEGACY) {
3394 		if (BTC_RSSI_HIGH(bt_rssi_state))
3395 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3396 		else if (BTC_RSSI_MEDIUM(bt_rssi_state))
3397 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3398 		else
3399 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3400 	} else {
3401 		/* only 802.11N mode we have to dec bt power to 4 degree */
3402 		if (BTC_RSSI_HIGH(bt_rssi_state)) {
3403 			/* need to check ap Number of Not */
3404 			if (ap_num < 10)
3405 				btc8723b2ant_dec_bt_pwr(btcoexist,
3406 							NORMAL_EXEC, 4);
3407 			else
3408 				btc8723b2ant_dec_bt_pwr(btcoexist,
3409 							NORMAL_EXEC, 2);
3410 		} else if (BTC_RSSI_MEDIUM(bt_rssi_state)) {
3411 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 2);
3412 		} else {
3413 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3414 		}
3415 	}
3416 
3417 	if (BTC_RSSI_HIGH(wifi_rssi_state1) && BTC_RSSI_HIGH(bt_rssi_state)) {
3418 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
3419 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3420 					      0x0, 0x0);
3421 	} else {
3422 		btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 14);
3423 		btc8723b2ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50,
3424 					      0x4);
3425 	}
3426 
3427 	if (BTC_RSSI_HIGH(bt_rssi_state)) {
3428 		if (ap_num < 10)
3429 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3430 							  false, 1);
3431 		else
3432 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3433 							  false, 3);
3434 	} else {
3435 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 18);
3436 		btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
3437 		btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
3438 		btcoexist->btc_write_4byte(btcoexist, 0x430, 0x0);
3439 		btcoexist->btc_write_4byte(btcoexist, 0x434, 0x01010000);
3440 
3441 		if (ap_num < 10)
3442 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3443 							  true, 1);
3444 		else
3445 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
3446 							  true, 3);
3447 	}
3448 
3449 	/* sw mechanism */
3450 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
3451 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3452 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3453 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3454 						  false, false);
3455 		} else {
3456 			btc8723b2ant_sw_mechanism(btcoexist, true, true,
3457 						  false, false);
3458 		}
3459 	} else {
3460 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
3461 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
3462 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3463 						  false, false);
3464 		} else {
3465 			btc8723b2ant_sw_mechanism(btcoexist, false, true,
3466 						  false, false);
3467 		}
3468 	}
3469 }
3470 
3471 static void btc8723b2ant_action_wifi_multi_port(struct btc_coexist *btcoexist)
3472 {
3473 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
3474 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, 0);
3475 
3476 	/* sw all off */
3477 	btc8723b2ant_sw_mechanism(btcoexist, false, false, false, false);
3478 
3479 	/* hw all off */
3480 	btc8723b2ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
3481 
3482 	btc8723b2ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3483 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
3484 }
3485 
3486 static void btc8723b2ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
3487 {
3488 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3489 	u8 algorithm = 0;
3490 	u32 num_of_wifi_link = 0;
3491 	u32 wifi_link_status = 0;
3492 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3493 	bool miracast_plus_bt = false;
3494 	bool scan = false, link = false, roam = false;
3495 
3496 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3497 		 "[BTCoex], RunCoexistMechanism()===>\n");
3498 
3499 	if (btcoexist->manual_control) {
3500 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3501 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
3502 		return;
3503 	}
3504 
3505 	if (coex_sta->under_ips) {
3506 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3507 			 "[BTCoex], wifi is under IPS !!!\n");
3508 		return;
3509 	}
3510 
3511 	algorithm = btc8723b2ant_action_algorithm(btcoexist);
3512 	if (coex_sta->c2h_bt_inquiry_page &&
3513 	    (BT_8723B_2ANT_COEX_ALGO_PANHS != algorithm)) {
3514 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3515 			 "[BTCoex], BT is under inquiry/page scan !!\n");
3516 		btc8723b2ant_action_bt_inquiry(btcoexist);
3517 		return;
3518 	}
3519 
3520 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3521 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3522 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3523 
3524 	if (scan || link || roam) {
3525 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3526 			 "[BTCoex], WiFi is under Link Process !!\n");
3527 		btc8723b2ant_action_wifi_link_process(btcoexist);
3528 		return;
3529 	}
3530 
3531 	/* for P2P */
3532 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3533 			   &wifi_link_status);
3534 	num_of_wifi_link = wifi_link_status >> 16;
3535 
3536 	if ((num_of_wifi_link >= 2) ||
3537 	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
3538 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3539 			 "############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
3540 			 num_of_wifi_link, wifi_link_status);
3541 
3542 		if (bt_link_info->bt_link_exist)
3543 			miracast_plus_bt = true;
3544 		else
3545 			miracast_plus_bt = false;
3546 
3547 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3548 				   &miracast_plus_bt);
3549 		btc8723b2ant_action_wifi_multi_port(btcoexist);
3550 
3551 		return;
3552 	}
3553 
3554 	miracast_plus_bt = false;
3555 	btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3556 			   &miracast_plus_bt);
3557 
3558 	coex_dm->cur_algorithm = algorithm;
3559 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3560 		 "[BTCoex], Algorithm = %d\n",
3561 		 coex_dm->cur_algorithm);
3562 
3563 	if (btc8723b2ant_is_common_action(btcoexist)) {
3564 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3565 			 "[BTCoex], Action 2-Ant common\n");
3566 		coex_dm->auto_tdma_adjust = false;
3567 	} else {
3568 		if (coex_dm->cur_algorithm != coex_dm->pre_algorithm) {
3569 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3570 				 "[BTCoex], preAlgorithm=%d, curAlgorithm=%d\n",
3571 				 coex_dm->pre_algorithm,
3572 				 coex_dm->cur_algorithm);
3573 			coex_dm->auto_tdma_adjust = false;
3574 		}
3575 		switch (coex_dm->cur_algorithm) {
3576 		case BT_8723B_2ANT_COEX_ALGO_SCO:
3577 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3578 				 "[BTCoex], Action 2-Ant, algorithm = SCO\n");
3579 			btc8723b2ant_action_sco(btcoexist);
3580 			break;
3581 		case BT_8723B_2ANT_COEX_ALGO_HID:
3582 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3583 				 "[BTCoex], Action 2-Ant, algorithm = HID\n");
3584 			btc8723b2ant_action_hid(btcoexist);
3585 			break;
3586 		case BT_8723B_2ANT_COEX_ALGO_A2DP:
3587 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3588 				 "[BTCoex], Action 2-Ant, algorithm = A2DP\n");
3589 			btc8723b2ant_action_a2dp(btcoexist);
3590 			break;
3591 		case BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS:
3592 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3593 				 "[BTCoex], Action 2-Ant, algorithm = A2DP+PAN(HS)\n");
3594 			btc8723b2ant_action_a2dp_pan_hs(btcoexist);
3595 			break;
3596 		case BT_8723B_2ANT_COEX_ALGO_PANEDR:
3597 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3598 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)\n");
3599 			btc8723b2ant_action_pan_edr(btcoexist);
3600 			break;
3601 		case BT_8723B_2ANT_COEX_ALGO_PANHS:
3602 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3603 				 "[BTCoex], Action 2-Ant, algorithm = HS mode\n");
3604 			btc8723b2ant_action_pan_hs(btcoexist);
3605 			break;
3606 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP:
3607 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3608 				 "[BTCoex], Action 2-Ant, algorithm = PAN+A2DP\n");
3609 			btc8723b2ant_action_pan_edr_a2dp(btcoexist);
3610 			break;
3611 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_HID:
3612 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3613 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)+HID\n");
3614 			btc8723b2ant_action_pan_edr_hid(btcoexist);
3615 			break;
3616 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
3617 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3618 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP+PAN\n");
3619 			btc8723b2ant_action_hid_a2dp_pan_edr(btcoexist);
3620 			break;
3621 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP:
3622 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3623 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP\n");
3624 			btc8723b2ant_action_hid_a2dp(btcoexist);
3625 			break;
3626 		default:
3627 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3628 				 "[BTCoex], Action 2-Ant, algorithm = coexist All Off!!\n");
3629 			btc8723b2ant_coex_alloff(btcoexist);
3630 			break;
3631 		}
3632 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
3633 	}
3634 }
3635 
3636 static void btc8723b2ant_wifioff_hwcfg(struct btc_coexist *btcoexist)
3637 {
3638 	bool is_in_mp_mode = false;
3639 	u8 h2c_parameter[2] = {0};
3640 	u32 fw_ver = 0;
3641 
3642 	/* set wlan_act to low */
3643 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3644 
3645 	/* WiFi standby while GNT_BT 0 -> 1 */
3646 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x780);
3647 
3648 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3649 	if (fw_ver >= 0x180000) {
3650 		/* Use H2C to set GNT_BT to HIGH */
3651 		h2c_parameter[0] = 1;
3652 		btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1, h2c_parameter);
3653 	} else {
3654 		btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
3655 	}
3656 
3657 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE,
3658 			   &is_in_mp_mode);
3659 	if (!is_in_mp_mode)
3660 		/* BT select s0/s1 is controlled by BT */
3661 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x0);
3662 	else
3663 		/* BT select s0/s1 is controlled by WiFi */
3664 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x1);
3665 }
3666 
3667 /*********************************************************************
3668  *  extern function start with ex_btc8723b2ant_
3669  *********************************************************************/
3670 void ex_btc8723b2ant_init_hwconfig(struct btc_coexist *btcoexist)
3671 {
3672 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3673 	u8 u8tmp = 0;
3674 
3675 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3676 		 "[BTCoex], 2Ant Init HW Config!!\n");
3677 	coex_dm->bt_rf0x1e_backup =
3678 		btcoexist->btc_get_rf_reg(btcoexist, BTC_RF_A, 0x1e, 0xfffff);
3679 
3680 	/* 0x790[5:0] = 0x5 */
3681 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
3682 	u8tmp &= 0xc0;
3683 	u8tmp |= 0x5;
3684 	btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
3685 
3686 	/* Antenna config */
3687 	btc8723b2ant_set_ant_path(btcoexist, BTC_ANT_WIFI_AT_MAIN,
3688 				  true, false);
3689 	coex_sta->dis_ver_info_cnt = 0;
3690 
3691 	/* PTA parameter */
3692 	btc8723b2ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
3693 
3694 	/* Enable counter statistics */
3695 	/* 0x76e[3] = 1, WLAN_ACT controlled by PTA */
3696 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3697 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
3698 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3699 	btcoexist->auto_report_2ant = true;
3700 }
3701 
3702 void ex_btc8723b2ant_power_on_setting(struct btc_coexist *btcoexist)
3703 {
3704 	struct btc_board_info *board_info = &btcoexist->board_info;
3705 	u16 u16tmp = 0x0;
3706 	u32 value = 0;
3707 
3708 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
3709 
3710 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly */
3711 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
3712 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT0 | BIT1);
3713 
3714 	btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
3715 
3716 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3717 		/* fixed at S0 for USB interface */
3718 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3719 	} else {
3720 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3721 		if (board_info->single_ant_path == 0) {
3722 			/* set to S1 */
3723 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
3724 		} else if (board_info->single_ant_path == 1) {
3725 			/* set to S0 */
3726 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3727 		}
3728 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
3729 				   &value);
3730 	}
3731 }
3732 
3733 void ex_btc8723b2ant_pre_load_firmware(struct btc_coexist *btcoexist)
3734 {
3735 	struct btc_board_info *board_info = &btcoexist->board_info;
3736 	u8 u8tmp = 0x4; /* Set BIT2 by default since it's 2ant case */
3737 
3738 	/**
3739 	 * S0 or S1 setting and Local register setting(By this fw can get
3740 	 * ant number, S0/S1, ... info)
3741 	 *
3742 	 * Local setting bit define
3743 	 *	BIT0: "0" : no antenna inverse; "1" : antenna inverse
3744 	 *	BIT1: "0" : internal switch; "1" : external switch
3745 	 *	BIT2: "0" : one antenna; "1" : two antennas
3746 	 *
3747 	 * NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and
3748 	 * BIT2 = 0
3749 	 */
3750 	if (btcoexist->chip_interface == BTC_INTF_USB) {
3751 		/* fixed at S0 for USB interface */
3752 		u8tmp |= 0x1; /* antenna inverse */
3753 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
3754 	} else {
3755 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
3756 		if (board_info->single_ant_path == 0) {
3757 		} else if (board_info->single_ant_path == 1) {
3758 			/* set to S0 */
3759 			u8tmp |= 0x1; /* antenna inverse */
3760 		}
3761 
3762 		if (btcoexist->chip_interface == BTC_INTF_PCI)
3763 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
3764 							     u8tmp);
3765 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
3766 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
3767 							     u8tmp);
3768 	}
3769 }
3770 
3771 void ex_btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
3772 {
3773 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3774 
3775 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3776 		 "[BTCoex], Coex Mechanism Init!!\n");
3777 	btc8723b2ant_init_coex_dm(btcoexist);
3778 }
3779 
3780 void ex_btc8723b2ant_display_coex_info(struct btc_coexist *btcoexist)
3781 {
3782 	struct btc_board_info *board_info = &btcoexist->board_info;
3783 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
3784 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3785 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3786 	u8 u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
3787 	u32 u32tmp[4];
3788 	bool roam = false, scan = false;
3789 	bool link = false, wifi_under_5g = false;
3790 	bool bt_hs_on = false, wifi_busy = false;
3791 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
3792 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck;
3793 	u8 wifi_dot11_chnl, wifi_hs_chnl;
3794 	u32 fw_ver = 0, bt_patch_ver = 0;
3795 	u8 ap_num = 0;
3796 
3797 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3798 		 "\r\n ============[BT Coexist info]============");
3799 
3800 	if (btcoexist->manual_control) {
3801 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3802 			 "\r\n ==========[Under Manual Control]============");
3803 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3804 			 "\r\n ==========================================");
3805 	}
3806 
3807 	if (!board_info->bt_exist) {
3808 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n BT not exists !!!");
3809 		return;
3810 	}
3811 
3812 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3813 		 "Ant PG number/ Ant mechanism:",
3814 		 board_info->pg_ant_num, board_info->btdm_ant_num);
3815 
3816 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %d",
3817 		 "BT stack/ hci ext ver",
3818 		 ((stack_info->profile_notified) ? "Yes" : "No"),
3819 		 stack_info->hci_version);
3820 
3821 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3822 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3823 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3824 		 "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
3825 		 "CoexVer/ FwVer/ PatchVer",
3826 		 glcoex_ver_date_8723b_2ant, glcoex_ver_8723b_2ant,
3827 		 fw_ver, bt_patch_ver, bt_patch_ver);
3828 
3829 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3830 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
3831 			   &wifi_dot11_chnl);
3832 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
3833 
3834 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d(%d)",
3835 		 "Dot11 channel / HsChnl(HsMode)",
3836 		 wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
3837 
3838 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %3ph ",
3839 		 "H2C Wifi inform bt chnl Info", coex_dm->wifi_chnl_info);
3840 
3841 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
3842 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
3843 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
3844 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d",
3845 		 "Wifi rssi/ HS rssi/ AP#", wifi_rssi, bt_hs_rssi, ap_num);
3846 
3847 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3848 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3849 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3850 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3851 		 "Wifi link/ roam/ scan", link, roam, scan);
3852 
3853 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3854 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3855 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3856 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
3857 			   &wifi_traffic_dir);
3858 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %s/ %s ",
3859 		 "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
3860 		 ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
3861 		 (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
3862 		 ((!wifi_busy) ? "idle" :
3863 		 ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
3864 		  "uplink" : "downlink")));
3865 
3866 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d / %d / %d",
3867 		 "SCO/HID/PAN/A2DP",
3868 		 bt_link_info->sco_exist, bt_link_info->hid_exist,
3869 		 bt_link_info->pan_exist, bt_link_info->a2dp_exist);
3870 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
3871 
3872 	bt_info_ext = coex_sta->bt_info_ext;
3873 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s",
3874 		 "BT Info A2DP rate",
3875 		 (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
3876 
3877 	for (i = 0; i < BT_INFO_SRC_8723B_2ANT_MAX; i++) {
3878 		if (coex_sta->bt_info_c2h_cnt[i]) {
3879 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3880 				 "\r\n %-35s = %7ph(%d)",
3881 				 glbt_info_src_8723b_2ant[i],
3882 				 coex_sta->bt_info_c2h[i],
3883 				 coex_sta->bt_info_c2h_cnt[i]);
3884 		}
3885 	}
3886 
3887 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s/%s",
3888 		 "PS state, IPS/LPS",
3889 		 ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
3890 		 ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")));
3891 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
3892 
3893 	/* Sw mechanism	*/
3894 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3895 		 "\r\n %-35s", "============[Sw mechanism]============");
3896 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3897 		 "SM1[ShRf/ LpRA/ LimDig]", coex_dm->cur_rf_rx_lpf_shrink,
3898 		 coex_dm->cur_low_penalty_ra, coex_dm->limited_dig);
3899 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d(0x%x) ",
3900 		 "SM2[AgcT/ AdcB/ SwDacSwing(lvl)]",
3901 		 coex_dm->cur_agc_table_en, coex_dm->cur_adc_back_off,
3902 		 coex_dm->cur_dac_swing_on, coex_dm->cur_dac_swing_lvl);
3903 
3904 	/* Fw mechanism	*/
3905 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3906 		 "============[Fw mechanism]============");
3907 
3908 	ps_tdma_case = coex_dm->cur_ps_tdma;
3909 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3910 		 "\r\n %-35s = %5ph case-%d (auto:%d)",
3911 		 "PS TDMA", coex_dm->ps_tdma_para,
3912 		 ps_tdma_case, coex_dm->auto_tdma_adjust);
3913 
3914 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3915 		 "DecBtPwr/ IgnWlanAct", coex_dm->cur_dec_bt_pwr_lvl,
3916 		 coex_dm->cur_ignore_wlan_act);
3917 
3918 	/* Hw setting */
3919 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3920 		 "============[Hw setting]============");
3921 
3922 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x",
3923 		 "RF-A, 0x1e initVal", coex_dm->bt_rf0x1e_backup);
3924 
3925 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
3926 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x880);
3927 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3928 		 "0x778/0x880[29:25]", u8tmp[0],
3929 		 (u32tmp[0] & 0x3e000000) >> 25);
3930 
3931 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
3932 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
3933 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
3934 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3935 		 "0x948/ 0x67[5] / 0x765",
3936 		 u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
3937 
3938 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
3939 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
3940 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
3941 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3942 		 "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
3943 		 u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
3944 
3945 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
3946 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
3947 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
3948 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
3949 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3950 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3951 		 "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
3952 		 ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
3953 		 ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
3954 
3955 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
3956 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
3957 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3958 		 "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
3959 
3960 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
3961 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
3962 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3963 		 "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
3964 
3965 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
3966 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
3967 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
3968 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
3969 
3970 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
3971 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
3972 
3973 	fa_ofdm = ((u32tmp[0]&0xffff0000) >> 16) +
3974 		  ((u32tmp[1]&0xffff0000) >> 16) +
3975 		   (u32tmp[1] & 0xffff) +
3976 		   (u32tmp[2] & 0xffff) +
3977 		  ((u32tmp[3]&0xffff0000) >> 16) +
3978 		   (u32tmp[3] & 0xffff);
3979 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
3980 
3981 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3982 		 "OFDM-CCA/OFDM-FA/CCK-FA",
3983 		 u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
3984 
3985 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
3986 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
3987 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
3988 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
3989 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3990 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3991 		 "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
3992 		 u32tmp[0], u32tmp[1], u32tmp[2], u8tmp[0]);
3993 
3994 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
3995 		 "0x770(high-pri rx/tx)",
3996 		 coex_sta->high_priority_rx, coex_sta->high_priority_tx);
3997 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
3998 		 "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
3999 		 coex_sta->low_priority_tx);
4000 	if (btcoexist->auto_report_2ant)
4001 		btc8723b2ant_monitor_bt_ctr(btcoexist);
4002 	btcoexist->btc_disp_dbg_msg(btcoexist,
4003 	BTC_DBG_DISP_COEX_STATISTICS);
4004 }
4005 
4006 void ex_btc8723b2ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
4007 {
4008 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4009 
4010 	if (BTC_IPS_ENTER == type) {
4011 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4012 			 "[BTCoex], IPS ENTER notify\n");
4013 		coex_sta->under_ips = true;
4014 		btc8723b2ant_wifioff_hwcfg(btcoexist);
4015 		btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
4016 		btc8723b2ant_coex_alloff(btcoexist);
4017 	} else if (BTC_IPS_LEAVE == type) {
4018 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4019 			 "[BTCoex], IPS LEAVE notify\n");
4020 		coex_sta->under_ips = false;
4021 		ex_btc8723b2ant_init_hwconfig(btcoexist);
4022 		btc8723b2ant_init_coex_dm(btcoexist);
4023 		btc8723b2ant_query_bt_info(btcoexist);
4024 	}
4025 }
4026 
4027 void ex_btc8723b2ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
4028 {
4029 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4030 
4031 	if (BTC_LPS_ENABLE == type) {
4032 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4033 			 "[BTCoex], LPS ENABLE notify\n");
4034 		coex_sta->under_lps = true;
4035 	} else if (BTC_LPS_DISABLE == type) {
4036 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4037 			 "[BTCoex], LPS DISABLE notify\n");
4038 		coex_sta->under_lps = false;
4039 	}
4040 }
4041 
4042 void ex_btc8723b2ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
4043 {
4044 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4045 	u32 u32tmp;
4046 	u8 u8tmpa, u8tmpb;
4047 
4048 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
4049 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
4050 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x76e);
4051 
4052 	if (BTC_SCAN_START == type)
4053 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4054 			 "[BTCoex], SCAN START notify\n");
4055 	else if (BTC_SCAN_FINISH == type)
4056 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4057 			 "[BTCoex], SCAN FINISH notify\n");
4058 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4059 			   &coex_sta->scan_ap_num);
4060 
4061 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4062 		 "############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x76e=0x%x\n",
4063 		u32tmp, u8tmpa, u8tmpb);
4064 }
4065 
4066 void ex_btc8723b2ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
4067 {
4068 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4069 
4070 	if (BTC_ASSOCIATE_START == type)
4071 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4072 			 "[BTCoex], CONNECT START notify\n");
4073 	else if (BTC_ASSOCIATE_FINISH == type)
4074 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4075 			 "[BTCoex], CONNECT FINISH notify\n");
4076 }
4077 
4078 void ex_btc8723b2ant_media_status_notify(struct btc_coexist *btcoexist,
4079 					 u8 type)
4080 {
4081 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4082 	u8 h2c_parameter[3] = {0};
4083 	u32 wifi_bw;
4084 	u8 wifi_central_chnl;
4085 	u8 ap_num = 0;
4086 
4087 	if (BTC_MEDIA_CONNECT == type)
4088 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4089 			 "[BTCoex], MEDIA connect notify\n");
4090 	else
4091 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4092 			 "[BTCoex], MEDIA disconnect notify\n");
4093 
4094 	/* only 2.4G we need to inform bt the chnl mask */
4095 	btcoexist->btc_get(btcoexist,
4096 		BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifi_central_chnl);
4097 	if ((BTC_MEDIA_CONNECT == type) &&
4098 	    (wifi_central_chnl <= 14)) {
4099 		h2c_parameter[0] = 0x1;
4100 		h2c_parameter[1] = wifi_central_chnl;
4101 		btcoexist->btc_get(btcoexist,
4102 			BTC_GET_U4_WIFI_BW, &wifi_bw);
4103 		if (wifi_bw == BTC_WIFI_BW_HT40) {
4104 			h2c_parameter[2] = 0x30;
4105 		} else {
4106 			btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4107 					   &ap_num);
4108 			if (ap_num < 10)
4109 				h2c_parameter[2] = 0x30;
4110 			else
4111 				h2c_parameter[2] = 0x20;
4112 		}
4113 	}
4114 
4115 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
4116 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
4117 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
4118 
4119 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4120 		 "[BTCoex], FW write 0x66=0x%x\n",
4121 		 h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
4122 		 h2c_parameter[2]);
4123 
4124 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
4125 }
4126 
4127 void ex_btc8723b2ant_special_packet_notify(struct btc_coexist *btcoexist,
4128 					   u8 type)
4129 {
4130 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4131 
4132 	if (type == BTC_PACKET_DHCP)
4133 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4134 			 "[BTCoex], DHCP Packet notify\n");
4135 }
4136 
4137 void ex_btc8723b2ant_bt_info_notify(struct btc_coexist *btcoexist,
4138 				    u8 *tmpbuf, u8 length)
4139 {
4140 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4141 	u8 bt_info = 0;
4142 	u8 i, rsp_source = 0;
4143 	bool bt_busy = false, limited_dig = false;
4144 	bool wifi_connected = false;
4145 
4146 	coex_sta->c2h_bt_info_req_sent = false;
4147 
4148 	rsp_source = tmpbuf[0]&0xf;
4149 	if (rsp_source >= BT_INFO_SRC_8723B_2ANT_MAX)
4150 		rsp_source = BT_INFO_SRC_8723B_2ANT_WIFI_FW;
4151 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
4152 
4153 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4154 		 "[BTCoex], Bt info[%d], length=%d, hex data=[",
4155 		 rsp_source, length);
4156 	for (i = 0; i < length; i++) {
4157 		coex_sta->bt_info_c2h[rsp_source][i] = tmpbuf[i];
4158 		if (i == 1)
4159 			bt_info = tmpbuf[i];
4160 		if (i == length - 1)
4161 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4162 				 "0x%02x]\n", tmpbuf[i]);
4163 		else
4164 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4165 				 "0x%02x, ", tmpbuf[i]);
4166 	}
4167 
4168 	if (btcoexist->manual_control) {
4169 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4170 			 "[BTCoex], BtInfoNotify(), return for Manual CTRL<===\n");
4171 		return;
4172 	}
4173 
4174 	if (BT_INFO_SRC_8723B_2ANT_WIFI_FW != rsp_source) {
4175 		coex_sta->bt_retry_cnt =
4176 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
4177 
4178 		if (coex_sta->bt_retry_cnt >= 1)
4179 			coex_sta->pop_event_cnt++;
4180 
4181 		coex_sta->bt_rssi =
4182 			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
4183 
4184 		coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
4185 
4186 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
4187 			coex_sta->c2h_bt_remote_name_req = true;
4188 		else
4189 			coex_sta->c2h_bt_remote_name_req = false;
4190 
4191 		if (coex_sta->bt_info_c2h[rsp_source][1] == 0x49)
4192 			coex_sta->a2dp_bit_pool =
4193 				coex_sta->bt_info_c2h[rsp_source][6];
4194 		else
4195 			coex_sta->a2dp_bit_pool = 0;
4196 
4197 		/* Here we need to resend some wifi info to BT
4198 		 * because BT is reset and loss of the info.
4199 		 */
4200 		if ((coex_sta->bt_info_ext & BIT1)) {
4201 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4202 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
4203 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4204 					   &wifi_connected);
4205 			if (wifi_connected)
4206 				ex_btc8723b2ant_media_status_notify(
4207 							btcoexist,
4208 							BTC_MEDIA_CONNECT);
4209 			else
4210 				ex_btc8723b2ant_media_status_notify(
4211 							btcoexist,
4212 							BTC_MEDIA_DISCONNECT);
4213 		}
4214 
4215 		if ((coex_sta->bt_info_ext & BIT3)) {
4216 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4217 				 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
4218 			btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC,
4219 						     false);
4220 		} else {
4221 			/* BT already NOT ignore Wlan active, do nothing here.*/
4222 		}
4223 		if (!btcoexist->auto_report_2ant) {
4224 			if (!(coex_sta->bt_info_ext & BIT4))
4225 				btc8723b2ant_bt_auto_report(btcoexist,
4226 							    FORCE_EXEC, true);
4227 		}
4228 	}
4229 
4230 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
4231 	if (bt_info & BT_INFO_8723B_2ANT_B_INQ_PAGE)
4232 		coex_sta->c2h_bt_inquiry_page = true;
4233 	else
4234 		coex_sta->c2h_bt_inquiry_page = false;
4235 
4236 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4237 		/* set link exist status */
4238 		coex_sta->bt_link_exist = false;
4239 		coex_sta->pan_exist = false;
4240 		coex_sta->a2dp_exist = false;
4241 		coex_sta->hid_exist = false;
4242 		coex_sta->sco_exist = false;
4243 	} else {
4244 		/* connection exists */
4245 		coex_sta->bt_link_exist = true;
4246 		if (bt_info & BT_INFO_8723B_2ANT_B_FTP)
4247 			coex_sta->pan_exist = true;
4248 		else
4249 			coex_sta->pan_exist = false;
4250 		if (bt_info & BT_INFO_8723B_2ANT_B_A2DP)
4251 			coex_sta->a2dp_exist = true;
4252 		else
4253 			coex_sta->a2dp_exist = false;
4254 		if (bt_info & BT_INFO_8723B_2ANT_B_HID)
4255 			coex_sta->hid_exist = true;
4256 		else
4257 			coex_sta->hid_exist = false;
4258 		if (bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO)
4259 			coex_sta->sco_exist = true;
4260 		else
4261 			coex_sta->sco_exist = false;
4262 
4263 		if ((!coex_sta->hid_exist) &&
4264 		    (!coex_sta->c2h_bt_inquiry_page) &&
4265 		    (!coex_sta->sco_exist)) {
4266 			if (coex_sta->high_priority_tx +
4267 				    coex_sta->high_priority_rx >= 160) {
4268 				coex_sta->hid_exist = true;
4269 				bt_info = bt_info | 0x28;
4270 			}
4271 		}
4272 	}
4273 
4274 	btc8723b2ant_update_bt_link_info(btcoexist);
4275 
4276 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
4277 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE;
4278 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4279 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
4280 	/* connection exists but no busy */
4281 	} else if (bt_info == BT_INFO_8723B_2ANT_B_CONNECTION) {
4282 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE;
4283 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4284 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
4285 	} else if ((bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO) ||
4286 		   (bt_info & BT_INFO_8723B_2ANT_B_SCO_BUSY)) {
4287 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_SCO_BUSY;
4288 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4289 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
4290 	} else if (bt_info&BT_INFO_8723B_2ANT_B_ACL_BUSY) {
4291 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_ACL_BUSY;
4292 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4293 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
4294 	} else {
4295 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_MAX;
4296 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4297 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
4298 	}
4299 
4300 	if ((BT_8723B_2ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
4301 	    (BT_8723B_2ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
4302 	    (BT_8723B_2ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status)) {
4303 		bt_busy = true;
4304 		limited_dig = true;
4305 	} else {
4306 		bt_busy = false;
4307 		limited_dig = false;
4308 	}
4309 
4310 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
4311 
4312 	coex_dm->limited_dig = limited_dig;
4313 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_LIMITED_DIG, &limited_dig);
4314 
4315 	btc8723b2ant_run_coexist_mechanism(btcoexist);
4316 }
4317 
4318 void ex_btc8723b2ant_halt_notify(struct btc_coexist *btcoexist)
4319 {
4320 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4321 
4322 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
4323 
4324 	btc8723b2ant_wifioff_hwcfg(btcoexist);
4325 	btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
4326 	ex_btc8723b2ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
4327 }
4328 
4329 void ex_btc8723b2ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
4330 {
4331 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4332 
4333 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Pnp notify\n");
4334 
4335 	if (pnp_state == BTC_WIFI_PNP_SLEEP) {
4336 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4337 			 "[BTCoex], Pnp notify to SLEEP\n");
4338 
4339 		/* Driver do not leave IPS/LPS when driver is going to sleep, so
4340 		 * BTCoexistence think wifi is still under IPS/LPS
4341 		 *
4342 		 * BT should clear UnderIPS/UnderLPS state to avoid mismatch
4343 		 * state after wakeup.
4344 		 */
4345 		coex_sta->under_ips = false;
4346 		coex_sta->under_lps = false;
4347 	} else if (pnp_state == BTC_WIFI_PNP_WAKE_UP) {
4348 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4349 			 "[BTCoex], Pnp notify to WAKE UP\n");
4350 		ex_btc8723b2ant_init_hwconfig(btcoexist);
4351 		btc8723b2ant_init_coex_dm(btcoexist);
4352 		btc8723b2ant_query_bt_info(btcoexist);
4353 	}
4354 }
4355 
4356 void ex_btc8723b2ant_periodical(struct btc_coexist *btcoexist)
4357 {
4358 	struct rtl_priv *rtlpriv = btcoexist->adapter;
4359 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
4360 
4361 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4362 		 "[BTCoex], ==========================Periodical===========================\n");
4363 
4364 	if (coex_sta->dis_ver_info_cnt <= 5) {
4365 		coex_sta->dis_ver_info_cnt += 1;
4366 		if (coex_sta->dis_ver_info_cnt == 3) {
4367 			/* Antenna config to set 0x765 = 0x0 (GNT_BT control by
4368 			 * PTA) after initial
4369 			 */
4370 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
4371 				 "[BTCoex], Set GNT_BT control by PTA\n");
4372 			btc8723b2ant_set_ant_path(
4373 				btcoexist, BTC_ANT_WIFI_AT_MAIN, false, false);
4374 		}
4375 	}
4376 
4377 	if (!btcoexist->auto_report_2ant) {
4378 		btc8723b2ant_query_bt_info(btcoexist);
4379 	} else {
4380 		btc8723b2ant_monitor_bt_ctr(btcoexist);
4381 		btc8723b2ant_monitor_wifi_ctr(btcoexist);
4382 
4383 		/* for some BT speakers that High-Priority pkts appear before
4384 		 * playing, this will cause HID exist
4385 		 */
4386 		if ((coex_sta->high_priority_tx +
4387 		    coex_sta->high_priority_rx < 50) &&
4388 		    (bt_link_info->hid_exist))
4389 			bt_link_info->hid_exist = false;
4390 
4391 		if (btc8723b2ant_is_wifi_status_changed(btcoexist) ||
4392 		    coex_dm->auto_tdma_adjust)
4393 			btc8723b2ant_run_coexist_mechanism(btcoexist);
4394 	}
4395 }
4396