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