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