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 void btc8723b2ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
244 {
245 	struct rtl_priv *rtlpriv = btcoexist->adapter;
246 	u32 reg_hp_txrx, reg_lp_txrx, u32tmp;
247 	u32 reg_hp_tx = 0, reg_hp_rx = 0;
248 	u32 reg_lp_tx = 0, reg_lp_rx = 0;
249 
250 	reg_hp_txrx = 0x770;
251 	reg_lp_txrx = 0x774;
252 
253 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
254 	reg_hp_tx = u32tmp & MASKLWORD;
255 	reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
256 
257 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
258 	reg_lp_tx = u32tmp & MASKLWORD;
259 	reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
260 
261 	coex_sta->high_priority_tx = reg_hp_tx;
262 	coex_sta->high_priority_rx = reg_hp_rx;
263 	coex_sta->low_priority_tx = reg_lp_tx;
264 	coex_sta->low_priority_rx = reg_lp_rx;
265 
266 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
267 		 "[BTCoex], High Priority Tx/Rx(reg 0x%x)=0x%x(%d)/0x%x(%d)\n",
268 		 reg_hp_txrx, reg_hp_tx, reg_hp_tx, reg_hp_rx, reg_hp_rx);
269 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
270 		 "[BTCoex], Low Priority Tx/Rx(reg 0x%x)=0x%x(%d)/0x%x(%d)\n",
271 		 reg_lp_txrx, reg_lp_tx, reg_lp_tx, reg_lp_rx, reg_lp_rx);
272 
273 	/* reset counter */
274 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
275 }
276 
277 static void btc8723b2ant_query_bt_info(struct btc_coexist *btcoexist)
278 {
279 	struct rtl_priv *rtlpriv = btcoexist->adapter;
280 	u8 h2c_parameter[1] = {0};
281 
282 	coex_sta->c2h_bt_info_req_sent = true;
283 
284 	h2c_parameter[0] |= BIT0;	/* trigger */
285 
286 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
287 		 "[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
288 		 h2c_parameter[0]);
289 
290 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
291 }
292 
293 static bool btc8723b2ant_is_wifi_status_changed(struct btc_coexist *btcoexist)
294 {
295 	static bool pre_wifi_busy;
296 	static bool pre_under_4way;
297 	static bool pre_bt_hs_on;
298 	bool wifi_busy = false, under_4way = false, bt_hs_on = false;
299 	bool wifi_connected = false;
300 
301 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
302 			   &wifi_connected);
303 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
304 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
305 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
306 			   &under_4way);
307 
308 	if (wifi_connected) {
309 		if (wifi_busy != pre_wifi_busy) {
310 			pre_wifi_busy = wifi_busy;
311 			return true;
312 		}
313 
314 		if (under_4way != pre_under_4way) {
315 			pre_under_4way = under_4way;
316 			return true;
317 		}
318 
319 		if (bt_hs_on != pre_bt_hs_on) {
320 			pre_bt_hs_on = bt_hs_on;
321 			return true;
322 		}
323 	}
324 
325 	return false;
326 }
327 
328 static void btc8723b2ant_update_bt_link_info(struct btc_coexist *btcoexist)
329 {
330 	/*struct btc_stack_info *stack_info = &btcoexist->stack_info;*/
331 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
332 	bool bt_hs_on = false;
333 
334 #if (BT_AUTO_REPORT_ONLY_8723B_2ANT == 1) /* profile from bt patch */
335 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
336 
337 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
338 	bt_link_info->sco_exist = coex_sta->sco_exist;
339 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
340 	bt_link_info->pan_exist = coex_sta->pan_exist;
341 	bt_link_info->hid_exist = coex_sta->hid_exist;
342 
343 	/* work around for HS mode. */
344 	if (bt_hs_on) {
345 		bt_link_info->pan_exist = true;
346 		bt_link_info->bt_link_exist = true;
347 	}
348 #else	/* profile from bt stack */
349 	bt_link_info->bt_link_exist = stack_info->bt_link_exist;
350 	bt_link_info->sco_exist = stack_info->sco_exist;
351 	bt_link_info->a2dp_exist = stack_info->a2dp_exist;
352 	bt_link_info->pan_exist = stack_info->pan_exist;
353 	bt_link_info->hid_exist = stack_info->hid_exist;
354 
355 	/*for win-8 stack HID report error*/
356 	if (!stack_info->hid_exist)
357 		stack_info->hid_exist = coex_sta->hid_exist;
358 	/*sync  BTInfo with BT firmware and stack*/
359 	/* when stack HID report error, here we use the info from bt fw.*/
360 	if (!stack_info->bt_link_exist)
361 		stack_info->bt_link_exist = coex_sta->bt_link_exist;
362 #endif
363 	/* check if Sco only */
364 	if (bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
365 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
366 		bt_link_info->sco_only = true;
367 	else
368 		bt_link_info->sco_only = false;
369 
370 	/* check if A2dp only */
371 	if (!bt_link_info->sco_exist && bt_link_info->a2dp_exist &&
372 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
373 		bt_link_info->a2dp_only = true;
374 	else
375 		bt_link_info->a2dp_only = false;
376 
377 	/* check if Pan only */
378 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
379 	    bt_link_info->pan_exist && !bt_link_info->hid_exist)
380 		bt_link_info->pan_only = true;
381 	else
382 		bt_link_info->pan_only = false;
383 
384 	/* check if Hid only */
385 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
386 	    !bt_link_info->pan_exist && bt_link_info->hid_exist)
387 		bt_link_info->hid_only = true;
388 	else
389 		bt_link_info->hid_only = false;
390 }
391 
392 static u8 btc8723b2ant_action_algorithm(struct btc_coexist *btcoexist)
393 {
394 	struct rtl_priv *rtlpriv = btcoexist->adapter;
395 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
396 	bool bt_hs_on = false;
397 	u8 algorithm = BT_8723B_2ANT_COEX_ALGO_UNDEFINED;
398 	u8 num_of_diff_profile = 0;
399 
400 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
401 
402 	if (!bt_link_info->bt_link_exist) {
403 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
404 			 "[BTCoex], No BT link exists!!!\n");
405 		return algorithm;
406 	}
407 
408 	if (bt_link_info->sco_exist)
409 		num_of_diff_profile++;
410 	if (bt_link_info->hid_exist)
411 		num_of_diff_profile++;
412 	if (bt_link_info->pan_exist)
413 		num_of_diff_profile++;
414 	if (bt_link_info->a2dp_exist)
415 		num_of_diff_profile++;
416 
417 	if (num_of_diff_profile == 1) {
418 		if (bt_link_info->sco_exist) {
419 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
420 				 "[BTCoex], SCO only\n");
421 			algorithm = BT_8723B_2ANT_COEX_ALGO_SCO;
422 		} else {
423 			if (bt_link_info->hid_exist) {
424 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
425 					 "[BTCoex], HID only\n");
426 				algorithm = BT_8723B_2ANT_COEX_ALGO_HID;
427 			} else if (bt_link_info->a2dp_exist) {
428 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
429 					 "[BTCoex], A2DP only\n");
430 				algorithm = BT_8723B_2ANT_COEX_ALGO_A2DP;
431 			} else if (bt_link_info->pan_exist) {
432 				if (bt_hs_on) {
433 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
434 						 DBG_LOUD,
435 						 "[BTCoex], PAN(HS) only\n");
436 					algorithm =
437 						BT_8723B_2ANT_COEX_ALGO_PANHS;
438 				} else {
439 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
440 						 DBG_LOUD,
441 						 "[BTCoex], PAN(EDR) only\n");
442 					algorithm =
443 						BT_8723B_2ANT_COEX_ALGO_PANEDR;
444 				}
445 			}
446 		}
447 	} else if (num_of_diff_profile == 2) {
448 		if (bt_link_info->sco_exist) {
449 			if (bt_link_info->hid_exist) {
450 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
451 					 "[BTCoex], SCO + HID\n");
452 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
453 			} else if (bt_link_info->a2dp_exist) {
454 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
455 					 "[BTCoex], SCO + A2DP ==> SCO\n");
456 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
457 			} else if (bt_link_info->pan_exist) {
458 				if (bt_hs_on) {
459 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
460 						 DBG_LOUD,
461 						 "[BTCoex], SCO + PAN(HS)\n");
462 					algorithm = BT_8723B_2ANT_COEX_ALGO_SCO;
463 				} else {
464 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
465 						 DBG_LOUD,
466 						 "[BTCoex], SCO + PAN(EDR)\n");
467 					algorithm =
468 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
469 				}
470 			}
471 		} else {
472 			if (bt_link_info->hid_exist &&
473 			    bt_link_info->a2dp_exist) {
474 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
475 					 "[BTCoex], HID + A2DP\n");
476 				algorithm = BT_8723B_2ANT_COEX_ALGO_HID_A2DP;
477 			} else if (bt_link_info->hid_exist &&
478 				   bt_link_info->pan_exist) {
479 				if (bt_hs_on) {
480 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
481 						 DBG_LOUD,
482 						 "[BTCoex], HID + PAN(HS)\n");
483 					algorithm = BT_8723B_2ANT_COEX_ALGO_HID;
484 				} else {
485 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
486 						 DBG_LOUD,
487 						 "[BTCoex], HID + PAN(EDR)\n");
488 					algorithm =
489 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
490 				}
491 			} else if (bt_link_info->pan_exist &&
492 				   bt_link_info->a2dp_exist) {
493 				if (bt_hs_on) {
494 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
495 						 DBG_LOUD,
496 						 "[BTCoex], A2DP + PAN(HS)\n");
497 					algorithm =
498 					    BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS;
499 				} else {
500 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
501 						 DBG_LOUD,
502 						 "[BTCoex],A2DP + PAN(EDR)\n");
503 					algorithm =
504 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP;
505 				}
506 			}
507 		}
508 	} else if (num_of_diff_profile == 3) {
509 		if (bt_link_info->sco_exist) {
510 			if (bt_link_info->hid_exist &&
511 			    bt_link_info->a2dp_exist) {
512 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
513 					 "[BTCoex], SCO + HID + A2DP ==> HID\n");
514 				algorithm = BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
515 			} else if (bt_link_info->hid_exist &&
516 				   bt_link_info->pan_exist) {
517 				if (bt_hs_on) {
518 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
519 						 DBG_LOUD,
520 						 "[BTCoex], SCO + HID + PAN(HS)\n");
521 					algorithm =
522 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
523 				} else {
524 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
525 						 DBG_LOUD,
526 						 "[BTCoex], SCO + HID + PAN(EDR)\n");
527 					algorithm =
528 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
529 				}
530 			} else if (bt_link_info->pan_exist &&
531 				   bt_link_info->a2dp_exist) {
532 				if (bt_hs_on) {
533 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
534 						 DBG_LOUD,
535 						 "[BTCoex], SCO + A2DP + PAN(HS)\n");
536 					algorithm =
537 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
538 				} else {
539 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
540 						 DBG_LOUD,
541 						 "[BTCoex], SCO + A2DP + PAN(EDR) ==> HID\n");
542 					algorithm =
543 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
544 				}
545 			}
546 		} else {
547 			if (bt_link_info->hid_exist &&
548 			    bt_link_info->pan_exist &&
549 			    bt_link_info->a2dp_exist) {
550 				if (bt_hs_on) {
551 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
552 						 DBG_LOUD,
553 						 "[BTCoex], HID + A2DP + PAN(HS)\n");
554 					algorithm =
555 					    BT_8723B_2ANT_COEX_ALGO_HID_A2DP;
556 				} else {
557 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
558 						 DBG_LOUD,
559 						 "[BTCoex], HID + A2DP + PAN(EDR)\n");
560 					algorithm =
561 					BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR;
562 				}
563 			}
564 		}
565 	} else if (num_of_diff_profile >= 3) {
566 		if (bt_link_info->sco_exist) {
567 			if (bt_link_info->hid_exist &&
568 			    bt_link_info->pan_exist &&
569 			    bt_link_info->a2dp_exist) {
570 				if (bt_hs_on) {
571 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
572 						 DBG_LOUD,
573 						 "[BTCoex], Error!!! SCO + HID + A2DP + PAN(HS)\n");
574 				} else {
575 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
576 						 DBG_LOUD,
577 						 "[BTCoex], SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
578 					algorithm =
579 					    BT_8723B_2ANT_COEX_ALGO_PANEDR_HID;
580 				}
581 			}
582 		}
583 	}
584 	return algorithm;
585 }
586 
587 static bool btc8723b_need_dec_pwr(struct btc_coexist *btcoexist)
588 {
589 	struct rtl_priv *rtlpriv = btcoexist->adapter;
590 	bool ret = false;
591 	bool bt_hs_on = false, wifi_connected = false;
592 	s32 bt_hs_rssi = 0;
593 	u8 bt_rssi_state;
594 
595 	if (!btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on))
596 		return false;
597 	if (!btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
598 				&wifi_connected))
599 		return false;
600 	if (!btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi))
601 		return false;
602 
603 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
604 
605 	if (wifi_connected) {
606 		if (bt_hs_on) {
607 			if (bt_hs_rssi > 37) {
608 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
609 					 "[BTCoex], Need to decrease bt power for HS mode!!\n");
610 				ret = true;
611 			}
612 		} else {
613 			if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
614 			    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
615 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
616 					 "[BTCoex], Need to decrease bt power for Wifi is connected!!\n");
617 				ret = true;
618 			}
619 		}
620 	}
621 
622 	return ret;
623 }
624 
625 static void btc8723b2ant_set_fw_dac_swing_level(struct btc_coexist *btcoexist,
626 						u8 dac_swing_lvl)
627 {
628 	struct rtl_priv *rtlpriv = btcoexist->adapter;
629 	u8 h2c_parameter[1] = {0};
630 
631 	/* There are several type of dacswing
632 	 * 0x18/ 0x10/ 0xc/ 0x8/ 0x4/ 0x6
633 	 */
634 	h2c_parameter[0] = dac_swing_lvl;
635 
636 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
637 		 "[BTCoex], Set Dac Swing Level=0x%x\n", dac_swing_lvl);
638 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
639 		 "[BTCoex], FW write 0x64=0x%x\n", h2c_parameter[0]);
640 
641 	btcoexist->btc_fill_h2c(btcoexist, 0x64, 1, h2c_parameter);
642 }
643 
644 static void btc8723b2ant_set_fw_dec_bt_pwr(struct btc_coexist *btcoexist,
645 					   bool dec_bt_pwr)
646 {
647 	struct rtl_priv *rtlpriv = btcoexist->adapter;
648 	u8 h2c_parameter[1] = {0};
649 
650 	h2c_parameter[0] = 0;
651 
652 	if (dec_bt_pwr)
653 		h2c_parameter[0] |= BIT1;
654 
655 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
656 		 "[BTCoex], decrease Bt Power : %s, FW write 0x62=0x%x\n",
657 		    (dec_bt_pwr ? "Yes!!" : "No!!"), h2c_parameter[0]);
658 
659 	btcoexist->btc_fill_h2c(btcoexist, 0x62, 1, h2c_parameter);
660 }
661 
662 static void btc8723b2ant_dec_bt_pwr(struct btc_coexist *btcoexist,
663 				    bool force_exec, bool dec_bt_pwr)
664 {
665 	struct rtl_priv *rtlpriv = btcoexist->adapter;
666 
667 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
668 		 "[BTCoex], %s Dec BT power = %s\n",
669 		    force_exec ? "force to" : "", dec_bt_pwr ? "ON" : "OFF");
670 	coex_dm->cur_dec_bt_pwr = dec_bt_pwr;
671 
672 	if (!force_exec) {
673 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
674 			 "[BTCoex], bPreDecBtPwr=%d, bCurDecBtPwr=%d\n",
675 			    coex_dm->pre_dec_bt_pwr, coex_dm->cur_dec_bt_pwr);
676 
677 		if (coex_dm->pre_dec_bt_pwr == coex_dm->cur_dec_bt_pwr)
678 			return;
679 	}
680 	btc8723b2ant_set_fw_dec_bt_pwr(btcoexist, coex_dm->cur_dec_bt_pwr);
681 
682 	coex_dm->pre_dec_bt_pwr = coex_dm->cur_dec_bt_pwr;
683 }
684 
685 static void btc8723b2ant_fw_dac_swing_lvl(struct btc_coexist *btcoexist,
686 					  bool force_exec, u8 fw_dac_swing_lvl)
687 {
688 	struct rtl_priv *rtlpriv = btcoexist->adapter;
689 
690 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
691 		 "[BTCoex], %s set FW Dac Swing level = %d\n",
692 		    (force_exec ? "force to" : ""), fw_dac_swing_lvl);
693 	coex_dm->cur_fw_dac_swing_lvl = fw_dac_swing_lvl;
694 
695 	if (!force_exec) {
696 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
697 			 "[BTCoex], preFwDacSwingLvl=%d, curFwDacSwingLvl=%d\n",
698 			    coex_dm->pre_fw_dac_swing_lvl,
699 			    coex_dm->cur_fw_dac_swing_lvl);
700 
701 		if (coex_dm->pre_fw_dac_swing_lvl ==
702 		   coex_dm->cur_fw_dac_swing_lvl)
703 			return;
704 	}
705 
706 	btc8723b2ant_set_fw_dac_swing_level(btcoexist,
707 					    coex_dm->cur_fw_dac_swing_lvl);
708 	coex_dm->pre_fw_dac_swing_lvl = coex_dm->cur_fw_dac_swing_lvl;
709 }
710 
711 static void btc8723b2ant_set_sw_rf_rx_lpf_corner(struct btc_coexist *btcoexist,
712 						 bool rx_rf_shrink_on)
713 {
714 	struct rtl_priv *rtlpriv = btcoexist->adapter;
715 
716 	if (rx_rf_shrink_on) {
717 		/* Shrink RF Rx LPF corner */
718 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
719 			 "[BTCoex], Shrink RF Rx LPF corner!!\n");
720 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1e,
721 					  0xfffff, 0xffffc);
722 	} else {
723 		/* Resume RF Rx LPF corner */
724 		/* After initialized, we can use coex_dm->btRf0x1eBackup */
725 		if (btcoexist->initilized) {
726 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
727 				 "[BTCoex], Resume RF Rx LPF corner!!\n");
728 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1e,
729 						  0xfffff,
730 						  coex_dm->bt_rf0x1e_backup);
731 		}
732 	}
733 }
734 
735 static void btc8723b2ant_rf_shrink(struct btc_coexist *btcoexist,
736 				   bool force_exec, bool rx_rf_shrink_on)
737 {
738 	struct rtl_priv *rtlpriv = btcoexist->adapter;
739 
740 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
741 		 "[BTCoex], %s turn Rx RF Shrink = %s\n",
742 		    (force_exec ? "force to" : ""), (rx_rf_shrink_on ?
743 						     "ON" : "OFF"));
744 	coex_dm->cur_rf_rx_lpf_shrink = rx_rf_shrink_on;
745 
746 	if (!force_exec) {
747 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
748 			 "[BTCoex], bPreRfRxLpfShrink=%d, bCurRfRxLpfShrink=%d\n",
749 			    coex_dm->pre_rf_rx_lpf_shrink,
750 			    coex_dm->cur_rf_rx_lpf_shrink);
751 
752 		if (coex_dm->pre_rf_rx_lpf_shrink ==
753 		    coex_dm->cur_rf_rx_lpf_shrink)
754 			return;
755 	}
756 	btc8723b2ant_set_sw_rf_rx_lpf_corner(btcoexist,
757 					     coex_dm->cur_rf_rx_lpf_shrink);
758 
759 	coex_dm->pre_rf_rx_lpf_shrink = coex_dm->cur_rf_rx_lpf_shrink;
760 }
761 
762 static void btc8723b_set_penalty_txrate(struct btc_coexist *btcoexist,
763 					bool low_penalty_ra)
764 {
765 	struct rtl_priv *rtlpriv = btcoexist->adapter;
766 	u8 h2c_parameter[6] = {0};
767 
768 	h2c_parameter[0] = 0x6;	/* opCode, 0x6= Retry_Penalty*/
769 
770 	if (low_penalty_ra) {
771 		h2c_parameter[1] |= BIT0;
772 		/*normal rate except MCS7/6/5, OFDM54/48/36*/
773 		h2c_parameter[2] = 0x00;
774 		h2c_parameter[3] = 0xf7;  /*MCS7 or OFDM54*/
775 		h2c_parameter[4] = 0xf8;  /*MCS6 or OFDM48*/
776 		h2c_parameter[5] = 0xf9;  /*MCS5 or OFDM36*/
777 	}
778 
779 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
780 		 "[BTCoex], set WiFi Low-Penalty Retry: %s",
781 		 (low_penalty_ra ? "ON!!" : "OFF!!"));
782 
783 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
784 }
785 
786 static void btc8723b2ant_low_penalty_ra(struct btc_coexist *btcoexist,
787 					bool force_exec, bool low_penalty_ra)
788 {
789 	struct rtl_priv *rtlpriv = btcoexist->adapter;
790 
791 	/*return; */
792 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
793 		 "[BTCoex], %s turn LowPenaltyRA = %s\n",
794 		 (force_exec ? "force to" : ""), (low_penalty_ra ?
795 						  "ON" : "OFF"));
796 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
797 
798 	if (!force_exec) {
799 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
800 			 "[BTCoex], bPreLowPenaltyRa=%d, bCurLowPenaltyRa=%d\n",
801 			 coex_dm->pre_low_penalty_ra,
802 			 coex_dm->cur_low_penalty_ra);
803 
804 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
805 			return;
806 	}
807 	btc8723b_set_penalty_txrate(btcoexist, coex_dm->cur_low_penalty_ra);
808 
809 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
810 }
811 
812 static void btc8723b2ant_set_dac_swing_reg(struct btc_coexist *btcoexist,
813 					   u32 level)
814 {
815 	struct rtl_priv *rtlpriv = btcoexist->adapter;
816 	u8 val = (u8) level;
817 
818 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
819 		 "[BTCoex], Write SwDacSwing = 0x%x\n", level);
820 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x883, 0x3e, val);
821 }
822 
823 static void btc8723b2ant_set_sw_fulltime_dac_swing(struct btc_coexist *btcoex,
824 						   bool sw_dac_swing_on,
825 						   u32 sw_dac_swing_lvl)
826 {
827 	if (sw_dac_swing_on)
828 		btc8723b2ant_set_dac_swing_reg(btcoex, sw_dac_swing_lvl);
829 	else
830 		btc8723b2ant_set_dac_swing_reg(btcoex, 0x18);
831 }
832 
833 static void btc8723b2ant_dac_swing(struct btc_coexist *btcoexist,
834 				   bool force_exec, bool dac_swing_on,
835 				   u32 dac_swing_lvl)
836 {
837 	struct rtl_priv *rtlpriv = btcoexist->adapter;
838 
839 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
840 		 "[BTCoex], %s turn DacSwing=%s, dac_swing_lvl=0x%x\n",
841 		 (force_exec ? "force to" : ""),
842 		 (dac_swing_on ? "ON" : "OFF"), dac_swing_lvl);
843 	coex_dm->cur_dac_swing_on = dac_swing_on;
844 	coex_dm->cur_dac_swing_lvl = dac_swing_lvl;
845 
846 	if (!force_exec) {
847 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
848 			 "[BTCoex], bPreDacSwingOn=%d, preDacSwingLvl=0x%x, bCurDacSwingOn=%d, curDacSwingLvl=0x%x\n",
849 			 coex_dm->pre_dac_swing_on,
850 			 coex_dm->pre_dac_swing_lvl,
851 			 coex_dm->cur_dac_swing_on,
852 			 coex_dm->cur_dac_swing_lvl);
853 
854 		if ((coex_dm->pre_dac_swing_on == coex_dm->cur_dac_swing_on) &&
855 		    (coex_dm->pre_dac_swing_lvl == coex_dm->cur_dac_swing_lvl))
856 			return;
857 	}
858 	mdelay(30);
859 	btc8723b2ant_set_sw_fulltime_dac_swing(btcoexist, dac_swing_on,
860 					       dac_swing_lvl);
861 
862 	coex_dm->pre_dac_swing_on = coex_dm->cur_dac_swing_on;
863 	coex_dm->pre_dac_swing_lvl = coex_dm->cur_dac_swing_lvl;
864 }
865 
866 static void btc8723b2ant_set_agc_table(struct btc_coexist *btcoexist,
867 				       bool agc_table_en)
868 {
869 	struct rtl_priv *rtlpriv = btcoexist->adapter;
870 	u8 rssi_adjust_val = 0;
871 
872 	/*  BB AGC Gain Table */
873 	if (agc_table_en) {
874 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
875 			 "[BTCoex], BB Agc Table On!\n");
876 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x6e1A0001);
877 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x6d1B0001);
878 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x6c1C0001);
879 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x6b1D0001);
880 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x6a1E0001);
881 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x691F0001);
882 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0x68200001);
883 	} else {
884 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
885 			 "[BTCoex], BB Agc Table Off!\n");
886 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xaa1A0001);
887 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa91B0001);
888 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa81C0001);
889 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa71D0001);
890 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa61E0001);
891 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa51F0001);
892 		btcoexist->btc_write_4byte(btcoexist, 0xc78, 0xa4200001);
893 	}
894 
895 	/* RF Gain */
896 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0xef, 0xfffff, 0x02000);
897 	if (agc_table_en) {
898 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
899 			 "[BTCoex], Agc Table On!\n");
900 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x3b,
901 					  0xfffff, 0x38fff);
902 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x3b,
903 					  0xfffff, 0x38ffe);
904 	} else {
905 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
906 			 "[BTCoex], Agc Table Off!\n");
907 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x3b,
908 					  0xfffff, 0x380c3);
909 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x3b,
910 					  0xfffff, 0x28ce6);
911 	}
912 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0xef, 0xfffff, 0x0);
913 
914 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0xed, 0xfffff, 0x1);
915 
916 	if (agc_table_en) {
917 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
918 			 "[BTCoex], Agc Table On!\n");
919 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x40,
920 					  0xfffff, 0x38fff);
921 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x40,
922 					  0xfffff, 0x38ffe);
923 	} else {
924 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
925 			 "[BTCoex], Agc Table Off!\n");
926 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x40,
927 					  0xfffff, 0x380c3);
928 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x40,
929 					  0xfffff, 0x28ce6);
930 	}
931 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0xed, 0xfffff, 0x0);
932 
933 	/* set rssiAdjustVal for wifi module. */
934 	if (agc_table_en)
935 		rssi_adjust_val = 8;
936 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON,
937 			   &rssi_adjust_val);
938 }
939 
940 static void btc8723b2ant_agc_table(struct btc_coexist *btcoexist,
941 				   bool force_exec, bool agc_table_en)
942 {
943 	struct rtl_priv *rtlpriv = btcoexist->adapter;
944 
945 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
946 		 "[BTCoex], %s %s Agc Table\n",
947 		 (force_exec ? "force to" : ""),
948 		 (agc_table_en ? "Enable" : "Disable"));
949 	coex_dm->cur_agc_table_en = agc_table_en;
950 
951 	if (!force_exec) {
952 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
953 			 "[BTCoex], bPreAgcTableEn=%d, bCurAgcTableEn=%d\n",
954 			 coex_dm->pre_agc_table_en,
955 			 coex_dm->cur_agc_table_en);
956 
957 		if (coex_dm->pre_agc_table_en == coex_dm->cur_agc_table_en)
958 			return;
959 	}
960 	btc8723b2ant_set_agc_table(btcoexist, agc_table_en);
961 
962 	coex_dm->pre_agc_table_en = coex_dm->cur_agc_table_en;
963 }
964 
965 static void btc8723b2ant_set_coex_table(struct btc_coexist *btcoexist,
966 					u32 val0x6c0, u32 val0x6c4,
967 					u32 val0x6c8, u8 val0x6cc)
968 {
969 	struct rtl_priv *rtlpriv = btcoexist->adapter;
970 
971 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
972 		 "[BTCoex], set coex table, set 0x6c0=0x%x\n", val0x6c0);
973 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
974 
975 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
976 		 "[BTCoex], set coex table, set 0x6c4=0x%x\n", val0x6c4);
977 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
978 
979 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
980 		 "[BTCoex], set coex table, set 0x6c8=0x%x\n", val0x6c8);
981 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
982 
983 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
984 		 "[BTCoex], set coex table, set 0x6cc=0x%x\n", val0x6cc);
985 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
986 }
987 
988 static void btc8723b2ant_coex_table(struct btc_coexist *btcoexist,
989 				    bool force_exec, u32 val0x6c0,
990 				    u32 val0x6c4, u32 val0x6c8,
991 				    u8 val0x6cc)
992 {
993 	struct rtl_priv *rtlpriv = btcoexist->adapter;
994 
995 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
996 		 "[BTCoex], %s write Coex Table 0x6c0=0x%x, 0x6c4=0x%x, 0x6c8=0x%x, 0x6cc=0x%x\n",
997 		 force_exec ? "force to" : "",
998 		 val0x6c0, val0x6c4, val0x6c8, val0x6cc);
999 	coex_dm->cur_val0x6c0 = val0x6c0;
1000 	coex_dm->cur_val0x6c4 = val0x6c4;
1001 	coex_dm->cur_val0x6c8 = val0x6c8;
1002 	coex_dm->cur_val0x6cc = val0x6cc;
1003 
1004 	if (!force_exec) {
1005 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1006 			 "[BTCoex], preVal0x6c0=0x%x, preVal0x6c4=0x%x, preVal0x6c8=0x%x, preVal0x6cc=0x%x !!\n",
1007 			 coex_dm->pre_val0x6c0, coex_dm->pre_val0x6c4,
1008 			 coex_dm->pre_val0x6c8, coex_dm->pre_val0x6cc);
1009 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1010 			 "[BTCoex], curVal0x6c0=0x%x, curVal0x6c4=0x%x, curVal0x6c8=0x%x, curVal0x6cc=0x%x !!\n",
1011 			 coex_dm->cur_val0x6c0, coex_dm->cur_val0x6c4,
1012 			 coex_dm->cur_val0x6c8, coex_dm->cur_val0x6cc);
1013 
1014 		if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
1015 		    (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
1016 		    (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
1017 		    (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
1018 			return;
1019 	}
1020 	btc8723b2ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
1021 				    val0x6c8, val0x6cc);
1022 
1023 	coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
1024 	coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
1025 	coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
1026 	coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
1027 }
1028 
1029 static void btc8723b_coex_tbl_type(struct btc_coexist *btcoexist,
1030 				   bool force_exec, u8 type)
1031 {
1032 	switch (type) {
1033 	case 0:
1034 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55555555,
1035 					0x55555555, 0xffff, 0x3);
1036 		break;
1037 	case 1:
1038 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55555555,
1039 					0x5afa5afa, 0xffff, 0x3);
1040 		break;
1041 	case 2:
1042 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
1043 					0x5a5a5a5a, 0xffff, 0x3);
1044 		break;
1045 	case 3:
1046 		btc8723b2ant_coex_table(btcoexist, force_exec, 0xaaaaaaaa,
1047 					0xaaaaaaaa, 0xffff, 0x3);
1048 		break;
1049 	case 4:
1050 		btc8723b2ant_coex_table(btcoexist, force_exec, 0xffffffff,
1051 					0xffffffff, 0xffff, 0x3);
1052 		break;
1053 	case 5:
1054 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
1055 					0x5fff5fff, 0xffff, 0x3);
1056 		break;
1057 	case 6:
1058 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1059 					0x5a5a5a5a, 0xffff, 0x3);
1060 		break;
1061 	case 7:
1062 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1063 					0x5afa5afa, 0xffff, 0x3);
1064 		break;
1065 	case 8:
1066 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x5aea5aea,
1067 					0x5aea5aea, 0xffff, 0x3);
1068 		break;
1069 	case 9:
1070 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1071 					0x5aea5aea, 0xffff, 0x3);
1072 		break;
1073 	case 10:
1074 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1075 					0x5aff5aff, 0xffff, 0x3);
1076 		break;
1077 	case 11:
1078 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1079 					0x5a5f5a5f, 0xffff, 0x3);
1080 		break;
1081 	case 12:
1082 		btc8723b2ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
1083 					0x5f5f5f5f, 0xffff, 0x3);
1084 		break;
1085 	default:
1086 		break;
1087 	}
1088 }
1089 
1090 static void btc8723b2ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
1091 						bool enable)
1092 {
1093 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1094 	u8 h2c_parameter[1] = {0};
1095 
1096 	if (enable)
1097 		h2c_parameter[0] |= BIT0;/* function enable*/
1098 
1099 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1100 		 "[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63=0x%x\n",
1101 		 h2c_parameter[0]);
1102 
1103 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
1104 }
1105 
1106 static void btc8723b2ant_ignore_wlan_act(struct btc_coexist *btcoexist,
1107 					 bool force_exec, bool enable)
1108 {
1109 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1110 
1111 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1112 		 "[BTCoex], %s turn Ignore WlanAct %s\n",
1113 		 (force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
1114 	coex_dm->cur_ignore_wlan_act = enable;
1115 
1116 	if (!force_exec) {
1117 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1118 			 "[BTCoex], bPreIgnoreWlanAct = %d, bCurIgnoreWlanAct = %d!!\n",
1119 			 coex_dm->pre_ignore_wlan_act,
1120 			 coex_dm->cur_ignore_wlan_act);
1121 
1122 		if (coex_dm->pre_ignore_wlan_act ==
1123 		    coex_dm->cur_ignore_wlan_act)
1124 			return;
1125 	}
1126 	btc8723b2ant_set_fw_ignore_wlan_act(btcoexist, enable);
1127 
1128 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
1129 }
1130 
1131 static void btc8723b2ant_set_fw_ps_tdma(struct btc_coexist *btcoexist, u8 byte1,
1132 					u8 byte2, u8 byte3, u8 byte4, u8 byte5)
1133 {
1134 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1135 	u8 h2c_parameter[5];
1136 
1137 	h2c_parameter[0] = byte1;
1138 	h2c_parameter[1] = byte2;
1139 	h2c_parameter[2] = byte3;
1140 	h2c_parameter[3] = byte4;
1141 	h2c_parameter[4] = byte5;
1142 
1143 	coex_dm->ps_tdma_para[0] = byte1;
1144 	coex_dm->ps_tdma_para[1] = byte2;
1145 	coex_dm->ps_tdma_para[2] = byte3;
1146 	coex_dm->ps_tdma_para[3] = byte4;
1147 	coex_dm->ps_tdma_para[4] = byte5;
1148 
1149 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1150 		 "[BTCoex], FW write 0x60(5bytes)=0x%x%08x\n",
1151 		 h2c_parameter[0],
1152 		 h2c_parameter[1] << 24 | h2c_parameter[2] << 16 |
1153 		 h2c_parameter[3] << 8 | h2c_parameter[4]);
1154 
1155 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1156 }
1157 
1158 static void btc8723b2ant_sw_mechanism1(struct btc_coexist *btcoexist,
1159 				       bool shrink_rx_lpf, bool low_penalty_ra,
1160 				       bool limited_dig, bool bt_lna_constrain)
1161 {
1162 	btc8723b2ant_rf_shrink(btcoexist, NORMAL_EXEC, shrink_rx_lpf);
1163 	btc8723b2ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
1164 }
1165 
1166 static void btc8723b2ant_sw_mechanism2(struct btc_coexist *btcoexist,
1167 				       bool agc_table_shift, bool adc_backoff,
1168 				       bool sw_dac_swing, u32 dac_swing_lvl)
1169 {
1170 	btc8723b2ant_agc_table(btcoexist, NORMAL_EXEC, agc_table_shift);
1171 	btc8723b2ant_dac_swing(btcoexist, NORMAL_EXEC, sw_dac_swing,
1172 			       dac_swing_lvl);
1173 }
1174 
1175 static void btc8723b2ant_set_ant_path(struct btc_coexist *btcoexist,
1176 				      u8 antpos_type, bool init_hwcfg,
1177 				      bool wifi_off)
1178 {
1179 	struct btc_board_info *board_info = &btcoexist->board_info;
1180 	u32 fw_ver = 0, u32tmp = 0;
1181 	bool pg_ext_switch = false;
1182 	bool use_ext_switch = false;
1183 	u8 h2c_parameter[2] = {0};
1184 
1185 	btcoexist->btc_get(btcoexist, BTC_GET_BL_EXT_SWITCH, &pg_ext_switch);
1186 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
1187 
1188 	if ((fw_ver < 0xc0000) || pg_ext_switch)
1189 		use_ext_switch = true;
1190 
1191 	if (init_hwcfg) {
1192 		/* 0x4c[23] = 0, 0x4c[24] = 1  Antenna control by WL/BT */
1193 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1194 		u32tmp &= ~BIT23;
1195 		u32tmp |= BIT24;
1196 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
1197 
1198 		btcoexist->btc_write_1byte(btcoexist, 0x974, 0xff);
1199 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x944, 0x3, 0x3);
1200 		btcoexist->btc_write_1byte(btcoexist, 0x930, 0x77);
1201 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x1);
1202 
1203 		/* Force GNT_BT to low */
1204 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x0);
1205 
1206 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
1207 			/* tell firmware "no antenna inverse" */
1208 			h2c_parameter[0] = 0;
1209 			h2c_parameter[1] = 1;  /* ext switch type */
1210 			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
1211 						h2c_parameter);
1212 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1213 		} else {
1214 			/* tell firmware "antenna inverse" */
1215 			h2c_parameter[0] = 1;
1216 			h2c_parameter[1] = 1;  /* ext switch type */
1217 			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
1218 						h2c_parameter);
1219 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1220 		}
1221 	}
1222 
1223 	/* ext switch setting */
1224 	if (use_ext_switch) {
1225 		/* fixed internal switch S1->WiFi, S0->BT */
1226 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1227 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1228 		else
1229 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1230 
1231 		switch (antpos_type) {
1232 		case BTC_ANT_WIFI_AT_MAIN:
1233 			/* ext switch main at wifi */
1234 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x92c,
1235 							   0x3, 0x1);
1236 			break;
1237 		case BTC_ANT_WIFI_AT_AUX:
1238 			/* ext switch aux at wifi */
1239 			btcoexist->btc_write_1byte_bitmask(btcoexist,
1240 							   0x92c, 0x3, 0x2);
1241 			break;
1242 		}
1243 	} else {	/* internal switch */
1244 		/* fixed ext switch */
1245 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x92c, 0x3, 0x1);
1246 		switch (antpos_type) {
1247 		case BTC_ANT_WIFI_AT_MAIN:
1248 			/* fixed internal switch S1->WiFi, S0->BT */
1249 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x0);
1250 			break;
1251 		case BTC_ANT_WIFI_AT_AUX:
1252 			/* fixed internal switch S0->WiFi, S1->BT */
1253 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x280);
1254 			break;
1255 		}
1256 	}
1257 }
1258 
1259 static void btc8723b2ant_ps_tdma(struct btc_coexist *btcoexist, bool force_exec,
1260 				 bool turn_on, u8 type)
1261 {
1262 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1263 
1264 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1265 		 "[BTCoex], %s turn %s PS TDMA, type=%d\n",
1266 		 (force_exec ? "force to" : ""),
1267 		 (turn_on ? "ON" : "OFF"), type);
1268 	coex_dm->cur_ps_tdma_on = turn_on;
1269 	coex_dm->cur_ps_tdma = type;
1270 
1271 	if (!force_exec) {
1272 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1273 			 "[BTCoex], bPrePsTdmaOn = %d, bCurPsTdmaOn = %d!!\n",
1274 			 coex_dm->pre_ps_tdma_on, coex_dm->cur_ps_tdma_on);
1275 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1276 			 "[BTCoex], prePsTdma = %d, curPsTdma = %d!!\n",
1277 			 coex_dm->pre_ps_tdma, coex_dm->cur_ps_tdma);
1278 
1279 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1280 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1281 			return;
1282 	}
1283 	if (turn_on) {
1284 		switch (type) {
1285 		case 1:
1286 		default:
1287 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1a,
1288 						    0x1a, 0xe1, 0x90);
1289 			break;
1290 		case 2:
1291 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x12,
1292 						    0x12, 0xe1, 0x90);
1293 			break;
1294 		case 3:
1295 			/* This call breaks BT when wireless is active -
1296 			 * comment it out for now until a better fix is found:
1297 			 * btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1298 			 *			    0x3, 0xf1, 0x90);
1299 			 */
1300 			break;
1301 		case 4:
1302 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x10,
1303 						    0x03, 0xf1, 0x90);
1304 			break;
1305 		case 5:
1306 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1a,
1307 						    0x1a, 0x60, 0x90);
1308 			break;
1309 		case 6:
1310 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x12,
1311 						    0x12, 0x60, 0x90);
1312 			break;
1313 		case 7:
1314 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1c,
1315 						    0x3, 0x70, 0x90);
1316 			break;
1317 		case 8:
1318 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x10,
1319 						    0x3, 0x70, 0x90);
1320 			break;
1321 		case 9:
1322 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1a,
1323 						    0x1a, 0xe1, 0x90);
1324 			break;
1325 		case 10:
1326 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x12,
1327 						    0x12, 0xe1, 0x90);
1328 			break;
1329 		case 11:
1330 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1331 						    0xa, 0xe1, 0x90);
1332 			break;
1333 		case 12:
1334 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x5,
1335 						    0x5, 0xe1, 0x90);
1336 			break;
1337 		case 13:
1338 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1a,
1339 						    0x1a, 0x60, 0x90);
1340 			break;
1341 		case 14:
1342 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x12,
1343 						    0x12, 0x60, 0x90);
1344 			break;
1345 		case 15:
1346 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1347 						    0xa, 0x60, 0x90);
1348 			break;
1349 		case 16:
1350 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x5,
1351 						    0x5, 0x60, 0x90);
1352 			break;
1353 		case 17:
1354 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x2f,
1355 						    0x2f, 0x60, 0x90);
1356 			break;
1357 		case 18:
1358 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x5,
1359 						    0x5, 0xe1, 0x90);
1360 			break;
1361 		case 19:
1362 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1363 						    0x25, 0xe1, 0x90);
1364 			break;
1365 		case 20:
1366 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1367 						    0x25, 0x60, 0x90);
1368 			break;
1369 		case 21:
1370 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1371 						    0x03, 0x70, 0x90);
1372 			break;
1373 		case 71:
1374 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x1a,
1375 						    0x1a, 0xe1, 0x90);
1376 			break;
1377 		}
1378 	} else {
1379 		/* disable PS tdma */
1380 		switch (type) {
1381 		case 0:
1382 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1383 						    0x40, 0x0);
1384 			break;
1385 		case 1:
1386 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1387 						    0x48, 0x0);
1388 			break;
1389 		default:
1390 			btc8723b2ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1391 						    0x40, 0x0);
1392 			break;
1393 		}
1394 	}
1395 
1396 	/* update pre state */
1397 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1398 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1399 }
1400 
1401 static void btc8723b2ant_coex_alloff(struct btc_coexist *btcoexist)
1402 {
1403 	/* fw all off */
1404 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1405 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1406 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
1407 
1408 	/* sw all off */
1409 	btc8723b2ant_sw_mechanism1(btcoexist, false, false, false, false);
1410 	btc8723b2ant_sw_mechanism2(btcoexist, false, false, false, 0x18);
1411 
1412 	/* hw all off */
1413 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
1414 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
1415 }
1416 
1417 static void btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
1418 {
1419 	/* force to reset coex mechanism*/
1420 
1421 	btc8723b2ant_ps_tdma(btcoexist, FORCE_EXEC, false, 1);
1422 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1423 	btc8723b2ant_dec_bt_pwr(btcoexist, FORCE_EXEC, false);
1424 
1425 	btc8723b2ant_sw_mechanism1(btcoexist, false, false, false, false);
1426 	btc8723b2ant_sw_mechanism2(btcoexist, false, false, false, 0x18);
1427 }
1428 
1429 static void btc8723b2ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1430 {
1431 	bool wifi_connected = false;
1432 	bool low_pwr_disable = true;
1433 
1434 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1435 			   &low_pwr_disable);
1436 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1437 			   &wifi_connected);
1438 
1439 	if (wifi_connected) {
1440 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
1441 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1442 	} else {
1443 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
1444 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1445 	}
1446 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, FORCE_EXEC, 6);
1447 	btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
1448 
1449 	btc8723b2ant_sw_mechanism1(btcoexist, false, false, false, false);
1450 	btc8723b2ant_sw_mechanism2(btcoexist, false, false, false, 0x18);
1451 
1452 	coex_dm->need_recover_0x948 = true;
1453 	coex_dm->backup_0x948 = btcoexist->btc_read_2byte(btcoexist, 0x948);
1454 
1455 	btc8723b2ant_set_ant_path(btcoexist, BTC_ANT_WIFI_AT_AUX,
1456 				  false, false);
1457 }
1458 
1459 static bool btc8723b2ant_is_common_action(struct btc_coexist *btcoexist)
1460 {
1461 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1462 	bool common = false, wifi_connected = false;
1463 	bool wifi_busy = false;
1464 	bool bt_hs_on = false, low_pwr_disable = false;
1465 
1466 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1467 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1468 			   &wifi_connected);
1469 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1470 
1471 	if (!wifi_connected) {
1472 		low_pwr_disable = false;
1473 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1474 				   &low_pwr_disable);
1475 
1476 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1477 			 "[BTCoex], Wifi non-connected idle!!\n");
1478 
1479 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
1480 					  0x0);
1481 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
1482 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1483 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
1484 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
1485 
1486 		btc8723b2ant_sw_mechanism1(btcoexist, false, false, false,
1487 					   false);
1488 		btc8723b2ant_sw_mechanism2(btcoexist, false, false, false,
1489 					   0x18);
1490 
1491 		common = true;
1492 	} else {
1493 		if (BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1494 		    coex_dm->bt_status) {
1495 			low_pwr_disable = false;
1496 			btcoexist->btc_set(btcoexist,
1497 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1498 					   &low_pwr_disable);
1499 
1500 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1501 				 "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1502 
1503 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1504 						  0xfffff, 0x0);
1505 			btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
1506 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1507 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1508 						      0xb);
1509 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC,
1510 						false);
1511 
1512 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
1513 						   false, false);
1514 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
1515 						   false, 0x18);
1516 
1517 			common = true;
1518 		} else if (BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE ==
1519 			   coex_dm->bt_status) {
1520 			low_pwr_disable = true;
1521 			btcoexist->btc_set(btcoexist,
1522 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1523 					   &low_pwr_disable);
1524 
1525 			if (bt_hs_on)
1526 				return false;
1527 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1528 				 "[BTCoex], Wifi connected + BT connected-idle!!\n");
1529 
1530 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
1531 						  0xfffff, 0x0);
1532 			btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
1533 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
1534 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
1535 						      0xb);
1536 			btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC,
1537 						false);
1538 
1539 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
1540 						   false, false);
1541 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
1542 						   false, 0x18);
1543 
1544 			common = true;
1545 		} else {
1546 			low_pwr_disable = true;
1547 			btcoexist->btc_set(btcoexist,
1548 					   BTC_SET_ACT_DISABLE_LOW_POWER,
1549 					   &low_pwr_disable);
1550 
1551 			if (wifi_busy) {
1552 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1553 					 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1554 				common = false;
1555 			} else {
1556 				if (bt_hs_on)
1557 					return false;
1558 
1559 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1560 					 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1561 
1562 				btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A,
1563 							  0x1, 0xfffff, 0x0);
1564 				btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC,
1565 						       7);
1566 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1567 						     true, 21);
1568 				btc8723b2ant_fw_dac_swing_lvl(btcoexist,
1569 							      NORMAL_EXEC,
1570 							      0xb);
1571 				if (btc8723b_need_dec_pwr(btcoexist))
1572 					btc8723b2ant_dec_bt_pwr(btcoexist,
1573 								NORMAL_EXEC,
1574 								true);
1575 				else
1576 					btc8723b2ant_dec_bt_pwr(btcoexist,
1577 								NORMAL_EXEC,
1578 								false);
1579 				btc8723b2ant_sw_mechanism1(btcoexist, false,
1580 							   false, false,
1581 							   false);
1582 				btc8723b2ant_sw_mechanism2(btcoexist, false,
1583 							   false, false,
1584 							   0x18);
1585 				common = true;
1586 			}
1587 		}
1588 	}
1589 
1590 	return common;
1591 }
1592 
1593 static void set_tdma_int1(struct btc_coexist *btcoexist, bool tx_pause,
1594 			  s32 result)
1595 {
1596 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1597 
1598 	/* Set PS TDMA for max interval == 1 */
1599 	if (tx_pause) {
1600 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1601 			 "[BTCoex], TxPause = 1\n");
1602 
1603 		if (coex_dm->cur_ps_tdma == 71) {
1604 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1605 					     true, 5);
1606 			coex_dm->tdma_adj_type = 5;
1607 		} else if (coex_dm->cur_ps_tdma == 1) {
1608 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1609 					     true, 5);
1610 			coex_dm->tdma_adj_type = 5;
1611 		} else if (coex_dm->cur_ps_tdma == 2) {
1612 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1613 					     true, 6);
1614 			coex_dm->tdma_adj_type = 6;
1615 		} else if (coex_dm->cur_ps_tdma == 3) {
1616 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1617 					     true, 7);
1618 			coex_dm->tdma_adj_type = 7;
1619 		} else if (coex_dm->cur_ps_tdma == 4) {
1620 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1621 					     true, 8);
1622 			coex_dm->tdma_adj_type = 8;
1623 		}
1624 
1625 		if (coex_dm->cur_ps_tdma == 9) {
1626 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1627 					     true, 13);
1628 			coex_dm->tdma_adj_type = 13;
1629 		} else if (coex_dm->cur_ps_tdma == 10) {
1630 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1631 					     true, 14);
1632 			coex_dm->tdma_adj_type = 14;
1633 		} else if (coex_dm->cur_ps_tdma == 11) {
1634 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1635 					     true, 15);
1636 			coex_dm->tdma_adj_type = 15;
1637 		} else if (coex_dm->cur_ps_tdma == 12) {
1638 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1639 					     true, 16);
1640 			coex_dm->tdma_adj_type = 16;
1641 		}
1642 
1643 		if (result == -1) {
1644 			if (coex_dm->cur_ps_tdma == 5) {
1645 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1646 						     true, 6);
1647 				coex_dm->tdma_adj_type = 6;
1648 			} else if (coex_dm->cur_ps_tdma == 6) {
1649 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1650 						     true, 7);
1651 				coex_dm->tdma_adj_type = 7;
1652 			} else if (coex_dm->cur_ps_tdma == 7) {
1653 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1654 						     true, 8);
1655 				coex_dm->tdma_adj_type = 8;
1656 			} else if (coex_dm->cur_ps_tdma == 13) {
1657 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1658 						     true, 14);
1659 				coex_dm->tdma_adj_type = 14;
1660 			} else if (coex_dm->cur_ps_tdma == 14) {
1661 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1662 						     true, 15);
1663 				coex_dm->tdma_adj_type = 15;
1664 			} else if (coex_dm->cur_ps_tdma == 15) {
1665 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1666 						     true, 16);
1667 				coex_dm->tdma_adj_type = 16;
1668 			}
1669 		}  else if (result == 1) {
1670 			if (coex_dm->cur_ps_tdma == 8) {
1671 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1672 						     true, 7);
1673 				coex_dm->tdma_adj_type = 7;
1674 			} else if (coex_dm->cur_ps_tdma == 7) {
1675 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1676 						     true, 6);
1677 				coex_dm->tdma_adj_type = 6;
1678 			} else if (coex_dm->cur_ps_tdma == 6) {
1679 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1680 						     true, 5);
1681 				coex_dm->tdma_adj_type = 5;
1682 			} else if (coex_dm->cur_ps_tdma == 16) {
1683 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1684 						     true, 15);
1685 				coex_dm->tdma_adj_type = 15;
1686 			} else if (coex_dm->cur_ps_tdma == 15) {
1687 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1688 						     true, 14);
1689 				coex_dm->tdma_adj_type = 14;
1690 			} else if (coex_dm->cur_ps_tdma == 14) {
1691 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1692 						     true, 13);
1693 				coex_dm->tdma_adj_type = 13;
1694 			}
1695 		}
1696 	} else {
1697 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1698 			 "[BTCoex], TxPause = 0\n");
1699 		if (coex_dm->cur_ps_tdma == 5) {
1700 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 71);
1701 			coex_dm->tdma_adj_type = 71;
1702 		} else if (coex_dm->cur_ps_tdma == 6) {
1703 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1704 			coex_dm->tdma_adj_type = 2;
1705 		} else if (coex_dm->cur_ps_tdma == 7) {
1706 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1707 			coex_dm->tdma_adj_type = 3;
1708 		} else if (coex_dm->cur_ps_tdma == 8) {
1709 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 4);
1710 			coex_dm->tdma_adj_type = 4;
1711 		}
1712 
1713 		if (coex_dm->cur_ps_tdma == 13) {
1714 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 9);
1715 			coex_dm->tdma_adj_type = 9;
1716 		} else if (coex_dm->cur_ps_tdma == 14) {
1717 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 10);
1718 			coex_dm->tdma_adj_type = 10;
1719 		} else if (coex_dm->cur_ps_tdma == 15) {
1720 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1721 			coex_dm->tdma_adj_type = 11;
1722 		} else if (coex_dm->cur_ps_tdma == 16) {
1723 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 12);
1724 			coex_dm->tdma_adj_type = 12;
1725 		}
1726 
1727 		if (result == -1) {
1728 			if (coex_dm->cur_ps_tdma == 71) {
1729 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1730 						     true, 1);
1731 				coex_dm->tdma_adj_type = 1;
1732 			} else if (coex_dm->cur_ps_tdma == 1) {
1733 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1734 						     true, 2);
1735 				coex_dm->tdma_adj_type = 2;
1736 			} else if (coex_dm->cur_ps_tdma == 2) {
1737 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1738 						     true, 3);
1739 				coex_dm->tdma_adj_type = 3;
1740 			} else if (coex_dm->cur_ps_tdma == 3) {
1741 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1742 						     true, 4);
1743 				coex_dm->tdma_adj_type = 4;
1744 			} else if (coex_dm->cur_ps_tdma == 9) {
1745 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1746 						     true, 10);
1747 				coex_dm->tdma_adj_type = 10;
1748 			} else if (coex_dm->cur_ps_tdma == 10) {
1749 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1750 						     true, 11);
1751 				coex_dm->tdma_adj_type = 11;
1752 			} else if (coex_dm->cur_ps_tdma == 11) {
1753 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1754 						     true, 12);
1755 				coex_dm->tdma_adj_type = 12;
1756 			}
1757 		}  else if (result == 1) {
1758 			if (coex_dm->cur_ps_tdma == 4) {
1759 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1760 						     true, 3);
1761 				coex_dm->tdma_adj_type = 3;
1762 			} else if (coex_dm->cur_ps_tdma == 3) {
1763 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1764 						     true, 2);
1765 				coex_dm->tdma_adj_type = 2;
1766 			} else if (coex_dm->cur_ps_tdma == 2) {
1767 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1768 						     true, 1);
1769 				coex_dm->tdma_adj_type = 1;
1770 			} else if (coex_dm->cur_ps_tdma == 1) {
1771 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1772 						     true, 71);
1773 				coex_dm->tdma_adj_type = 71;
1774 			} else if (coex_dm->cur_ps_tdma == 12) {
1775 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1776 						     true, 11);
1777 				coex_dm->tdma_adj_type = 11;
1778 			} else if (coex_dm->cur_ps_tdma == 11) {
1779 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1780 						     true, 10);
1781 				coex_dm->tdma_adj_type = 10;
1782 			} else if (coex_dm->cur_ps_tdma == 10) {
1783 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1784 						     true, 9);
1785 				coex_dm->tdma_adj_type = 9;
1786 			}
1787 		}
1788 	}
1789 }
1790 
1791 static void set_tdma_int2(struct btc_coexist *btcoexist, bool tx_pause,
1792 			  s32 result)
1793 {
1794 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1795 
1796 	/* Set PS TDMA for max interval == 2 */
1797 	if (tx_pause) {
1798 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1799 			 "[BTCoex], TxPause = 1\n");
1800 		if (coex_dm->cur_ps_tdma == 1) {
1801 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 6);
1802 			coex_dm->tdma_adj_type = 6;
1803 		} else if (coex_dm->cur_ps_tdma == 2) {
1804 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 6);
1805 			coex_dm->tdma_adj_type = 6;
1806 		} else if (coex_dm->cur_ps_tdma == 3) {
1807 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 7);
1808 			coex_dm->tdma_adj_type = 7;
1809 		} else if (coex_dm->cur_ps_tdma == 4) {
1810 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 8);
1811 			coex_dm->tdma_adj_type = 8;
1812 		}
1813 		if (coex_dm->cur_ps_tdma == 9) {
1814 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1815 			coex_dm->tdma_adj_type = 14;
1816 		} else if (coex_dm->cur_ps_tdma == 10) {
1817 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1818 			coex_dm->tdma_adj_type = 14;
1819 		} else if (coex_dm->cur_ps_tdma == 11) {
1820 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 15);
1821 			coex_dm->tdma_adj_type = 15;
1822 		} else if (coex_dm->cur_ps_tdma == 12) {
1823 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 16);
1824 			coex_dm->tdma_adj_type = 16;
1825 		}
1826 		if (result == -1) {
1827 			if (coex_dm->cur_ps_tdma == 5) {
1828 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1829 						     true, 6);
1830 				coex_dm->tdma_adj_type = 6;
1831 			} else if (coex_dm->cur_ps_tdma == 6) {
1832 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1833 						     true, 7);
1834 				coex_dm->tdma_adj_type = 7;
1835 			} else if (coex_dm->cur_ps_tdma == 7) {
1836 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1837 						     true, 8);
1838 				coex_dm->tdma_adj_type = 8;
1839 			} else if (coex_dm->cur_ps_tdma == 13) {
1840 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1841 						     true, 14);
1842 				coex_dm->tdma_adj_type = 14;
1843 			} else if (coex_dm->cur_ps_tdma == 14) {
1844 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1845 						     true, 15);
1846 				coex_dm->tdma_adj_type = 15;
1847 			} else if (coex_dm->cur_ps_tdma == 15) {
1848 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1849 						     true, 16);
1850 				coex_dm->tdma_adj_type = 16;
1851 			}
1852 		}  else if (result == 1) {
1853 			if (coex_dm->cur_ps_tdma == 8) {
1854 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1855 						     true, 7);
1856 				coex_dm->tdma_adj_type = 7;
1857 			} else if (coex_dm->cur_ps_tdma == 7) {
1858 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1859 						     true, 6);
1860 				coex_dm->tdma_adj_type = 6;
1861 			} else if (coex_dm->cur_ps_tdma == 6) {
1862 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1863 						     true, 6);
1864 				coex_dm->tdma_adj_type = 6;
1865 			} else if (coex_dm->cur_ps_tdma == 16) {
1866 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1867 						     true, 15);
1868 				coex_dm->tdma_adj_type = 15;
1869 			} else if (coex_dm->cur_ps_tdma == 15) {
1870 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1871 						     true, 14);
1872 				coex_dm->tdma_adj_type = 14;
1873 			} else if (coex_dm->cur_ps_tdma == 14) {
1874 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1875 						     true, 14);
1876 				coex_dm->tdma_adj_type = 14;
1877 			}
1878 		}
1879 	} else {
1880 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1881 			 "[BTCoex], TxPause = 0\n");
1882 		if (coex_dm->cur_ps_tdma == 5) {
1883 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1884 			coex_dm->tdma_adj_type = 2;
1885 		} else if (coex_dm->cur_ps_tdma == 6) {
1886 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1887 			coex_dm->tdma_adj_type = 2;
1888 		} else if (coex_dm->cur_ps_tdma == 7) {
1889 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1890 			coex_dm->tdma_adj_type = 3;
1891 		} else if (coex_dm->cur_ps_tdma == 8) {
1892 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 4);
1893 			coex_dm->tdma_adj_type = 4;
1894 		}
1895 		if (coex_dm->cur_ps_tdma == 13) {
1896 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 10);
1897 			coex_dm->tdma_adj_type = 10;
1898 		} else if (coex_dm->cur_ps_tdma == 14) {
1899 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 10);
1900 			coex_dm->tdma_adj_type = 10;
1901 		} else if (coex_dm->cur_ps_tdma == 15) {
1902 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1903 			coex_dm->tdma_adj_type = 11;
1904 		} else if (coex_dm->cur_ps_tdma == 16) {
1905 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 12);
1906 			coex_dm->tdma_adj_type = 12;
1907 		}
1908 		if (result == -1) {
1909 			if (coex_dm->cur_ps_tdma == 1) {
1910 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1911 						     true, 2);
1912 				coex_dm->tdma_adj_type = 2;
1913 			} else if (coex_dm->cur_ps_tdma == 2) {
1914 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1915 						     true, 3);
1916 				coex_dm->tdma_adj_type = 3;
1917 			} else if (coex_dm->cur_ps_tdma == 3) {
1918 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1919 						     true, 4);
1920 				coex_dm->tdma_adj_type = 4;
1921 			} else if (coex_dm->cur_ps_tdma == 9) {
1922 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1923 						     true, 10);
1924 				coex_dm->tdma_adj_type = 10;
1925 			} else if (coex_dm->cur_ps_tdma == 10) {
1926 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1927 						     true, 11);
1928 				coex_dm->tdma_adj_type = 11;
1929 			} else if (coex_dm->cur_ps_tdma == 11) {
1930 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1931 						     true, 12);
1932 				coex_dm->tdma_adj_type = 12;
1933 			}
1934 		} else if (result == 1) {
1935 			if (coex_dm->cur_ps_tdma == 4) {
1936 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1937 						     true, 3);
1938 				coex_dm->tdma_adj_type = 3;
1939 			} else if (coex_dm->cur_ps_tdma == 3) {
1940 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1941 						     true, 2);
1942 				coex_dm->tdma_adj_type = 2;
1943 			} else if (coex_dm->cur_ps_tdma == 2) {
1944 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1945 						     true, 2);
1946 				coex_dm->tdma_adj_type = 2;
1947 			} else if (coex_dm->cur_ps_tdma == 12) {
1948 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1949 						     true, 11);
1950 				coex_dm->tdma_adj_type = 11;
1951 			} else if (coex_dm->cur_ps_tdma == 11) {
1952 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1953 						     true, 10);
1954 				coex_dm->tdma_adj_type = 10;
1955 			} else if (coex_dm->cur_ps_tdma == 10) {
1956 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
1957 						     true, 10);
1958 				coex_dm->tdma_adj_type = 10;
1959 			}
1960 		}
1961 	}
1962 }
1963 
1964 static void set_tdma_int3(struct btc_coexist *btcoexist, bool tx_pause,
1965 			  s32 result)
1966 {
1967 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1968 
1969 	/* Set PS TDMA for max interval == 3 */
1970 	if (tx_pause) {
1971 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1972 			 "[BTCoex], TxPause = 1\n");
1973 		if (coex_dm->cur_ps_tdma == 1) {
1974 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 7);
1975 			coex_dm->tdma_adj_type = 7;
1976 		} else if (coex_dm->cur_ps_tdma == 2) {
1977 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 7);
1978 			coex_dm->tdma_adj_type = 7;
1979 		} else if (coex_dm->cur_ps_tdma == 3) {
1980 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 7);
1981 			coex_dm->tdma_adj_type = 7;
1982 		} else if (coex_dm->cur_ps_tdma == 4) {
1983 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 8);
1984 			coex_dm->tdma_adj_type = 8;
1985 		}
1986 		if (coex_dm->cur_ps_tdma == 9) {
1987 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 15);
1988 			coex_dm->tdma_adj_type = 15;
1989 		} else if (coex_dm->cur_ps_tdma == 10) {
1990 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 15);
1991 			coex_dm->tdma_adj_type = 15;
1992 		} else if (coex_dm->cur_ps_tdma == 11) {
1993 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 15);
1994 			coex_dm->tdma_adj_type = 15;
1995 		} else if (coex_dm->cur_ps_tdma == 12) {
1996 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 16);
1997 			coex_dm->tdma_adj_type = 16;
1998 		}
1999 		if (result == -1) {
2000 			if (coex_dm->cur_ps_tdma == 5) {
2001 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2002 						     true, 7);
2003 				coex_dm->tdma_adj_type = 7;
2004 			} else if (coex_dm->cur_ps_tdma == 6) {
2005 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2006 						     true, 7);
2007 				coex_dm->tdma_adj_type = 7;
2008 			} else if (coex_dm->cur_ps_tdma == 7) {
2009 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2010 						     true, 8);
2011 				coex_dm->tdma_adj_type = 8;
2012 			} else if (coex_dm->cur_ps_tdma == 13) {
2013 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2014 						     true, 15);
2015 				coex_dm->tdma_adj_type = 15;
2016 			} else if (coex_dm->cur_ps_tdma == 14) {
2017 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2018 						     true, 15);
2019 				coex_dm->tdma_adj_type = 15;
2020 			} else if (coex_dm->cur_ps_tdma == 15) {
2021 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2022 						     true, 16);
2023 				coex_dm->tdma_adj_type = 16;
2024 			}
2025 		}  else if (result == 1) {
2026 			if (coex_dm->cur_ps_tdma == 8) {
2027 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2028 						     true, 7);
2029 				coex_dm->tdma_adj_type = 7;
2030 			} else if (coex_dm->cur_ps_tdma == 7) {
2031 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2032 						     true, 7);
2033 				coex_dm->tdma_adj_type = 7;
2034 			} else if (coex_dm->cur_ps_tdma == 6) {
2035 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2036 						     true, 7);
2037 				coex_dm->tdma_adj_type = 7;
2038 			} else if (coex_dm->cur_ps_tdma == 16) {
2039 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2040 						     true, 15);
2041 				coex_dm->tdma_adj_type = 15;
2042 			} else if (coex_dm->cur_ps_tdma == 15) {
2043 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2044 						     true, 15);
2045 				coex_dm->tdma_adj_type = 15;
2046 			} else if (coex_dm->cur_ps_tdma == 14) {
2047 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2048 						     true, 15);
2049 				coex_dm->tdma_adj_type = 15;
2050 			}
2051 		}
2052 	} else {
2053 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2054 			 "[BTCoex], TxPause = 0\n");
2055 		if (coex_dm->cur_ps_tdma == 5) {
2056 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
2057 			coex_dm->tdma_adj_type = 3;
2058 		} else if (coex_dm->cur_ps_tdma == 6) {
2059 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
2060 			coex_dm->tdma_adj_type = 3;
2061 		} else if (coex_dm->cur_ps_tdma == 7) {
2062 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
2063 			coex_dm->tdma_adj_type = 3;
2064 		} else if (coex_dm->cur_ps_tdma == 8) {
2065 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 4);
2066 			coex_dm->tdma_adj_type = 4;
2067 		}
2068 		if (coex_dm->cur_ps_tdma == 13) {
2069 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
2070 			coex_dm->tdma_adj_type = 11;
2071 		} else if (coex_dm->cur_ps_tdma == 14) {
2072 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
2073 			coex_dm->tdma_adj_type = 11;
2074 		} else if (coex_dm->cur_ps_tdma == 15) {
2075 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
2076 			coex_dm->tdma_adj_type = 11;
2077 		} else if (coex_dm->cur_ps_tdma == 16) {
2078 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 12);
2079 			coex_dm->tdma_adj_type = 12;
2080 		}
2081 		if (result == -1) {
2082 			if (coex_dm->cur_ps_tdma == 1) {
2083 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2084 						     true, 3);
2085 				coex_dm->tdma_adj_type = 3;
2086 			} else if (coex_dm->cur_ps_tdma == 2) {
2087 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2088 						     true, 3);
2089 				coex_dm->tdma_adj_type = 3;
2090 			} else if (coex_dm->cur_ps_tdma == 3) {
2091 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2092 						     true, 4);
2093 				coex_dm->tdma_adj_type = 4;
2094 			} else if (coex_dm->cur_ps_tdma == 9) {
2095 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2096 						     true, 11);
2097 				coex_dm->tdma_adj_type = 11;
2098 			} else if (coex_dm->cur_ps_tdma == 10) {
2099 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2100 						     true, 11);
2101 				coex_dm->tdma_adj_type = 11;
2102 			} else if (coex_dm->cur_ps_tdma == 11) {
2103 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2104 						     true, 12);
2105 				coex_dm->tdma_adj_type = 12;
2106 			}
2107 		} else if (result == 1) {
2108 			if (coex_dm->cur_ps_tdma == 4) {
2109 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2110 						     true, 3);
2111 				coex_dm->tdma_adj_type = 3;
2112 			} else if (coex_dm->cur_ps_tdma == 3) {
2113 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2114 						     true, 3);
2115 				coex_dm->tdma_adj_type = 3;
2116 			} else if (coex_dm->cur_ps_tdma == 2) {
2117 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2118 						     true, 3);
2119 				coex_dm->tdma_adj_type = 3;
2120 			} else if (coex_dm->cur_ps_tdma == 12) {
2121 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2122 						     true, 11);
2123 				coex_dm->tdma_adj_type = 11;
2124 			} else if (coex_dm->cur_ps_tdma == 11) {
2125 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2126 						     true, 11);
2127 				coex_dm->tdma_adj_type = 11;
2128 			} else if (coex_dm->cur_ps_tdma == 10) {
2129 				btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC,
2130 						     true, 11);
2131 				coex_dm->tdma_adj_type = 11;
2132 			}
2133 		}
2134 	}
2135 }
2136 
2137 static void btc8723b2ant_tdma_duration_adjust(struct btc_coexist *btcoexist,
2138 					  bool sco_hid, bool tx_pause,
2139 					  u8 max_interval)
2140 {
2141 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2142 	static s32 up, dn, m, n, wait_count;
2143 	/*0: no change, +1: increase WiFi duration, -1: decrease WiFi duration*/
2144 	s32 result;
2145 	u8 retry_count = 0;
2146 
2147 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2148 		 "[BTCoex], TdmaDurationAdjust()\n");
2149 
2150 	if (!coex_dm->auto_tdma_adjust) {
2151 		coex_dm->auto_tdma_adjust = true;
2152 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2153 			 "[BTCoex], first run TdmaDurationAdjust()!!\n");
2154 		if (sco_hid) {
2155 			if (tx_pause) {
2156 				if (max_interval == 1) {
2157 					btc8723b2ant_ps_tdma(btcoexist,
2158 							     NORMAL_EXEC,
2159 							     true, 13);
2160 					coex_dm->tdma_adj_type = 13;
2161 				} else if (max_interval == 2) {
2162 					btc8723b2ant_ps_tdma(btcoexist,
2163 							     NORMAL_EXEC,
2164 							     true, 14);
2165 					coex_dm->tdma_adj_type = 14;
2166 				} else {
2167 					btc8723b2ant_ps_tdma(btcoexist,
2168 							     NORMAL_EXEC,
2169 							     true, 15);
2170 					coex_dm->tdma_adj_type = 15;
2171 				}
2172 			} else {
2173 				if (max_interval == 1) {
2174 					btc8723b2ant_ps_tdma(btcoexist,
2175 							     NORMAL_EXEC,
2176 							     true, 9);
2177 					coex_dm->tdma_adj_type = 9;
2178 				} else if (max_interval == 2) {
2179 					btc8723b2ant_ps_tdma(btcoexist,
2180 							     NORMAL_EXEC,
2181 							     true, 10);
2182 					coex_dm->tdma_adj_type = 10;
2183 				} else {
2184 					btc8723b2ant_ps_tdma(btcoexist,
2185 							     NORMAL_EXEC,
2186 							     true, 11);
2187 					coex_dm->tdma_adj_type = 11;
2188 				}
2189 			}
2190 		} else {
2191 			if (tx_pause) {
2192 				if (max_interval == 1) {
2193 					btc8723b2ant_ps_tdma(btcoexist,
2194 							     NORMAL_EXEC,
2195 							     true, 5);
2196 					coex_dm->tdma_adj_type = 5;
2197 				} else if (max_interval == 2) {
2198 					btc8723b2ant_ps_tdma(btcoexist,
2199 							     NORMAL_EXEC,
2200 							     true, 6);
2201 					coex_dm->tdma_adj_type = 6;
2202 				} else {
2203 					btc8723b2ant_ps_tdma(btcoexist,
2204 							     NORMAL_EXEC,
2205 							     true, 7);
2206 					coex_dm->tdma_adj_type = 7;
2207 				}
2208 			} else {
2209 				if (max_interval == 1) {
2210 					btc8723b2ant_ps_tdma(btcoexist,
2211 							     NORMAL_EXEC,
2212 							     true, 1);
2213 					coex_dm->tdma_adj_type = 1;
2214 				} else if (max_interval == 2) {
2215 					btc8723b2ant_ps_tdma(btcoexist,
2216 							     NORMAL_EXEC,
2217 							     true, 2);
2218 					coex_dm->tdma_adj_type = 2;
2219 				} else {
2220 					btc8723b2ant_ps_tdma(btcoexist,
2221 							     NORMAL_EXEC,
2222 							     true, 3);
2223 					coex_dm->tdma_adj_type = 3;
2224 				}
2225 			}
2226 		}
2227 
2228 		up = 0;
2229 		dn = 0;
2230 		m = 1;
2231 		n = 3;
2232 		result = 0;
2233 		wait_count = 0;
2234 	} else {
2235 		/*accquire the BT TRx retry count from BT_Info byte2*/
2236 		retry_count = coex_sta->bt_retry_cnt;
2237 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2238 			 "[BTCoex], retry_count = %d\n", retry_count);
2239 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2240 			 "[BTCoex], up=%d, dn=%d, m=%d, n=%d, wait_count=%d\n",
2241 			 up, dn, m, n, wait_count);
2242 		result = 0;
2243 		wait_count++;
2244 		 /* no retry in the last 2-second duration*/
2245 		if (retry_count == 0) {
2246 			up++;
2247 			dn--;
2248 
2249 			if (dn <= 0)
2250 				dn = 0;
2251 
2252 			if (up >= n) {
2253 				wait_count = 0;
2254 				n = 3;
2255 				up = 0;
2256 				dn = 0;
2257 				result = 1;
2258 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2259 					 "[BTCoex], Increase wifi duration!!\n");
2260 			} /* <=3 retry in the last 2-second duration*/
2261 		} else if (retry_count <= 3) {
2262 			up--;
2263 			dn++;
2264 
2265 			if (up <= 0)
2266 				up = 0;
2267 
2268 			if (dn == 2) {
2269 				if (wait_count <= 2)
2270 					m++;
2271 				else
2272 					m = 1;
2273 
2274 				if (m >= 20)
2275 					m = 20;
2276 
2277 				n = 3 * m;
2278 				up = 0;
2279 				dn = 0;
2280 				wait_count = 0;
2281 				result = -1;
2282 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2283 					 "[BTCoex], Decrease wifi duration for retry_counter<3!!\n");
2284 			}
2285 		} else {
2286 			if (wait_count == 1)
2287 				m++;
2288 			else
2289 				m = 1;
2290 
2291 			if (m >= 20)
2292 				m = 20;
2293 
2294 			n = 3 * m;
2295 			up = 0;
2296 			dn = 0;
2297 			wait_count = 0;
2298 			result = -1;
2299 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2300 				 "[BTCoex], Decrease wifi duration for retry_counter>3!!\n");
2301 		}
2302 
2303 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2304 			 "[BTCoex], max Interval = %d\n", max_interval);
2305 		if (max_interval == 1)
2306 			set_tdma_int1(btcoexist, tx_pause, result);
2307 		else if (max_interval == 2)
2308 			set_tdma_int2(btcoexist, tx_pause, result);
2309 		else if (max_interval == 3)
2310 			set_tdma_int3(btcoexist, tx_pause, result);
2311 	}
2312 
2313 	/*if current PsTdma not match with the recorded one (when scan, dhcp..),
2314 	 *then we have to adjust it back to the previous recorded one.
2315 	 */
2316 	if (coex_dm->cur_ps_tdma != coex_dm->tdma_adj_type) {
2317 		bool scan = false, link = false, roam = false;
2318 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2319 			 "[BTCoex], PsTdma type dismatch!!!, curPsTdma=%d, recordPsTdma=%d\n",
2320 			 coex_dm->cur_ps_tdma, coex_dm->tdma_adj_type);
2321 
2322 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2323 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2324 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2325 
2326 		if (!scan && !link && !roam)
2327 			btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
2328 					     coex_dm->tdma_adj_type);
2329 		else
2330 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2331 				 "[BTCoex], roaming/link/scan is under progress, will adjust next time!!!\n");
2332 	}
2333 }
2334 
2335 /* SCO only or SCO+PAN(HS) */
2336 static void btc8723b2ant_action_sco(struct btc_coexist *btcoexist)
2337 {
2338 	u8 wifi_rssi_state;
2339 	u32 wifi_bw;
2340 
2341 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2342 						       0, 2, 15, 0);
2343 
2344 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2345 
2346 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 4);
2347 
2348 	if (btc8723b_need_dec_pwr(btcoexist))
2349 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2350 	else
2351 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2352 
2353 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2354 
2355 	/*for SCO quality at 11b/g mode*/
2356 	if (BTC_WIFI_BW_LEGACY == wifi_bw)
2357 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 2);
2358 	else  /*for SCO quality & wifi performance balance at 11n mode*/
2359 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 8);
2360 
2361 	/*for voice quality */
2362 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2363 
2364 	/* sw mechanism */
2365 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2366 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2367 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2368 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2369 						   false, false);
2370 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2371 						   true, 0x4);
2372 		} else {
2373 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2374 						   false, false);
2375 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2376 						   true, 0x4);
2377 		}
2378 	} else {
2379 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2380 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2381 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2382 						   false, false);
2383 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2384 						   true, 0x4);
2385 		} else {
2386 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2387 						   false, false);
2388 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2389 						   true, 0x4);
2390 		}
2391 	}
2392 }
2393 
2394 static void btc8723b2ant_action_hid(struct btc_coexist *btcoexist)
2395 {
2396 	u8 wifi_rssi_state, bt_rssi_state;
2397 	u32 wifi_bw;
2398 
2399 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2400 						       0, 2, 15, 0);
2401 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2402 
2403 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2404 
2405 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2406 
2407 	if (btc8723b_need_dec_pwr(btcoexist))
2408 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2409 	else
2410 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2411 
2412 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2413 
2414 	if (BTC_WIFI_BW_LEGACY == wifi_bw) /*/for HID at 11b/g mode*/
2415 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2416 	else  /*for HID quality & wifi performance balance at 11n mode*/
2417 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 9);
2418 
2419 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2420 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2421 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 9);
2422 	else
2423 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
2424 
2425 	/* sw mechanism */
2426 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2427 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2428 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2429 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2430 						   false, false);
2431 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2432 						   false, 0x18);
2433 		} else {
2434 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2435 						   false, false);
2436 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2437 						   false, 0x18);
2438 		}
2439 	} else {
2440 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2441 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2442 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2443 						   false, false);
2444 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2445 						   false, 0x18);
2446 		} else {
2447 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2448 						   false, false);
2449 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2450 						   false, 0x18);
2451 		}
2452 	}
2453 }
2454 
2455 /*A2DP only / PAN(EDR) only/ A2DP+PAN(HS)*/
2456 static void btc8723b2ant_action_a2dp(struct btc_coexist *btcoexist)
2457 {
2458 	u8 wifi_rssi_state, wifi_rssi_state1, bt_rssi_state;
2459 	u32 wifi_bw;
2460 	u8 ap_num = 0;
2461 
2462 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2463 						       0, 2, 15, 0);
2464 	wifi_rssi_state1 = btc8723b2ant_wifi_rssi_state(btcoexist,
2465 							1, 2, 40, 0);
2466 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2467 
2468 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
2469 
2470 	/* define the office environment */
2471 	/* driver don't know AP num in Linux, so we will never enter this if */
2472 	if (ap_num >= 10 && BTC_RSSI_HIGH(wifi_rssi_state1)) {
2473 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
2474 					  0x0);
2475 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2476 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2477 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 0);
2478 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
2479 
2480 		/* sw mechanism */
2481 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2482 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
2483 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2484 						   false, false);
2485 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2486 						   true, 0x18);
2487 		} else {
2488 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2489 						   false, false);
2490 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2491 						   true, 0x18);
2492 		}
2493 		return;
2494 	}
2495 
2496 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2497 
2498 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2499 
2500 	if (btc8723b_need_dec_pwr(btcoexist))
2501 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2502 	else
2503 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2504 
2505 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2506 
2507 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2508 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2509 		btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2510 						  false, 1);
2511 	else
2512 		btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 1);
2513 
2514 	/* sw mechanism */
2515 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2516 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2517 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2518 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2519 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2520 						   false, false);
2521 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2522 						   false, 0x18);
2523 		} else {
2524 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2525 						   false, false);
2526 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2527 						   false, 0x18);
2528 		}
2529 	} else {
2530 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2531 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2532 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2533 						   false, false);
2534 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2535 						   false, 0x18);
2536 		} else {
2537 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2538 						   false, false);
2539 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2540 						   false, 0x18);
2541 		}
2542 	}
2543 }
2544 
2545 static void btc8723b2ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
2546 {
2547 	u8 wifi_rssi_state;
2548 	u32 wifi_bw;
2549 
2550 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2551 						       0, 2, 15, 0);
2552 
2553 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2554 
2555 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2556 
2557 	if (btc8723b_need_dec_pwr(btcoexist))
2558 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2559 	else
2560 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2561 
2562 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2563 
2564 	btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 2);
2565 
2566 	/* sw mechanism */
2567 	btcoexist->btc_get(btcoexist,
2568 		BTC_GET_U4_WIFI_BW, &wifi_bw);
2569 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2570 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2571 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2572 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2573 						   false, false);
2574 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2575 						   false, 0x18);
2576 		} else {
2577 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2578 						   false, false);
2579 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2580 						   false, 0x18);
2581 		}
2582 	} else {
2583 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2584 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2585 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2586 						   false, false);
2587 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2588 						   false, 0x18);
2589 		} else {
2590 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2591 						   false, false);
2592 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2593 						   false, 0x18);
2594 		}
2595 	}
2596 }
2597 
2598 static void btc8723b2ant_action_pan_edr(struct btc_coexist *btcoexist)
2599 {
2600 	u8 wifi_rssi_state, bt_rssi_state;
2601 	u32 wifi_bw;
2602 
2603 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2604 						       0, 2, 15, 0);
2605 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2606 
2607 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2608 
2609 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2610 
2611 	if (btc8723b_need_dec_pwr(btcoexist))
2612 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2613 	else
2614 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2615 
2616 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 10);
2617 
2618 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2619 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2620 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 1);
2621 	else
2622 		btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
2623 
2624 	/* sw mechanism */
2625 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2626 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2627 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2628 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2629 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2630 						   false, false);
2631 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2632 						   false, 0x18);
2633 		} else {
2634 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2635 						   false, false);
2636 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2637 						   false, 0x18);
2638 		}
2639 	} else {
2640 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2641 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2642 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2643 						   false, false);
2644 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2645 						   false, 0x18);
2646 		} else {
2647 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2648 						   false, false);
2649 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2650 						   false, 0x18);
2651 		}
2652 	}
2653 }
2654 
2655 /*PAN(HS) only*/
2656 static void btc8723b2ant_action_pan_hs(struct btc_coexist *btcoexist)
2657 {
2658 	u8 wifi_rssi_state;
2659 	u32 wifi_bw;
2660 
2661 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2662 						       0, 2, 15, 0);
2663 
2664 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2665 
2666 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2667 
2668 	if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2669 	    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2670 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2671 	else
2672 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2673 
2674 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2675 
2676 	btc8723b2ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 1);
2677 
2678 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2679 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2680 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2681 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2682 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2683 						   false, false);
2684 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2685 						   false, 0x18);
2686 		} else {
2687 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2688 						   false, false);
2689 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2690 						   false, 0x18);
2691 		}
2692 	} else {
2693 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2694 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2695 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2696 						   false, false);
2697 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2698 						   false, 0x18);
2699 		} else {
2700 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2701 						   false, false);
2702 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2703 						   false, 0x18);
2704 		}
2705 	}
2706 }
2707 
2708 /*PAN(EDR)+A2DP*/
2709 static void btc8723b2ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
2710 {
2711 	u8 wifi_rssi_state, bt_rssi_state;
2712 	u32 wifi_bw;
2713 
2714 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2715 						       0, 2, 15, 0);
2716 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2717 
2718 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2719 
2720 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2721 
2722 	if (btc8723b_need_dec_pwr(btcoexist))
2723 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2724 	else
2725 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2726 
2727 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2728 
2729 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2730 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2731 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 12);
2732 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2733 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2734 							  true, 3);
2735 		else
2736 			btc8723b2ant_tdma_duration_adjust(btcoexist, false,
2737 							  false, 3);
2738 	} else {
2739 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2740 		btc8723b2ant_tdma_duration_adjust(btcoexist, false, true, 3);
2741 	}
2742 
2743 	/* sw mechanism	*/
2744 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2745 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2746 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2747 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2748 						   false, false);
2749 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2750 						   false, 0x18);
2751 		} else {
2752 			btc8723b2ant_sw_mechanism1(btcoexist, true, false,
2753 						   false, false);
2754 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2755 						   false, 0x18);
2756 		}
2757 	} else {
2758 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2759 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2760 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2761 						   false, false);
2762 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2763 						   false, 0x18);
2764 		} else {
2765 			btc8723b2ant_sw_mechanism1(btcoexist, false, false,
2766 						   false, false);
2767 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2768 						   false, 0x18);
2769 		}
2770 	}
2771 }
2772 
2773 static void btc8723b2ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
2774 {
2775 	u8 wifi_rssi_state, bt_rssi_state;
2776 	u32 wifi_bw;
2777 
2778 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2779 						       0, 2, 15, 0);
2780 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2781 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2782 
2783 	if (btc8723b_need_dec_pwr(btcoexist))
2784 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2785 	else
2786 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2787 
2788 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2789 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2790 		if (BTC_WIFI_BW_HT40 == wifi_bw) {
2791 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
2792 						      3);
2793 			btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 11);
2794 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
2795 						  0xfffff, 0x780);
2796 		} else {
2797 			btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC,
2798 						      6);
2799 			btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2800 			btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1,
2801 						  0xfffff, 0x0);
2802 		}
2803 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, false, 2);
2804 	} else {
2805 		btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2806 		btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 11);
2807 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
2808 					  0x0);
2809 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 2);
2810 	}
2811 
2812 	/* sw mechanism */
2813 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2814 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2815 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2816 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2817 						   false, false);
2818 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2819 						   false, 0x18);
2820 		} else {
2821 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2822 						   false, false);
2823 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2824 						   false, 0x18);
2825 		}
2826 	} else {
2827 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2828 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2829 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2830 						   false, false);
2831 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2832 						   false, 0x18);
2833 		} else {
2834 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2835 						   false, false);
2836 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2837 						   false, 0x18);
2838 		}
2839 	}
2840 }
2841 
2842 /* HID+A2DP+PAN(EDR) */
2843 static void btc8723b2ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
2844 {
2845 	u8 wifi_rssi_state, bt_rssi_state;
2846 	u32 wifi_bw;
2847 
2848 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2849 						       0, 2, 15, 0);
2850 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2851 
2852 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2853 
2854 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2855 
2856 	if (btc8723b_need_dec_pwr(btcoexist))
2857 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2858 	else
2859 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2860 
2861 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2862 
2863 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2864 
2865 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2866 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2867 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2868 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
2869 							  true, 2);
2870 		else
2871 			btc8723b2ant_tdma_duration_adjust(btcoexist, true,
2872 							  false, 3);
2873 	} else {
2874 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 3);
2875 	}
2876 
2877 	/* sw mechanism */
2878 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2879 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2880 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2881 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2882 						   false, false);
2883 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2884 						   false, 0x18);
2885 		} else {
2886 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2887 						   false, false);
2888 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2889 						   false, 0x18);
2890 		}
2891 	} else {
2892 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2893 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2894 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2895 						   false, false);
2896 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2897 						   false, 0x18);
2898 		} else {
2899 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2900 						   false, false);
2901 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2902 						   false, 0x18);
2903 		}
2904 	}
2905 }
2906 
2907 static void btc8723b2ant_action_hid_a2dp(struct btc_coexist *btcoexist)
2908 {
2909 	u8 wifi_rssi_state, bt_rssi_state;
2910 	u32 wifi_bw;
2911 
2912 	wifi_rssi_state = btc8723b2ant_wifi_rssi_state(btcoexist,
2913 						       0, 2, 15, 0);
2914 	bt_rssi_state = btc8723b2ant_bt_rssi_state(btcoexist, 2, 29, 0);
2915 
2916 	btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
2917 
2918 	btc8723b2ant_fw_dac_swing_lvl(btcoexist, NORMAL_EXEC, 6);
2919 
2920 	if (btc8723b_need_dec_pwr(btcoexist))
2921 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, true);
2922 	else
2923 		btc8723b2ant_dec_bt_pwr(btcoexist, NORMAL_EXEC, false);
2924 
2925 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2926 
2927 	btc8723b_coex_tbl_type(btcoexist, NORMAL_EXEC, 7);
2928 
2929 	if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
2930 	    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH))
2931 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, false, 2);
2932 	else
2933 		btc8723b2ant_tdma_duration_adjust(btcoexist, true, true, 2);
2934 
2935 	/* sw mechanism */
2936 	if (BTC_WIFI_BW_HT40 == wifi_bw) {
2937 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2938 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2939 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2940 						   false, false);
2941 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2942 						   false, 0x18);
2943 		} else {
2944 			btc8723b2ant_sw_mechanism1(btcoexist, true, true,
2945 						   false, false);
2946 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2947 						   false, 0x18);
2948 		}
2949 	} else {
2950 		if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2951 		    (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2952 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2953 						   false, false);
2954 			btc8723b2ant_sw_mechanism2(btcoexist, true, false,
2955 						   false, 0x18);
2956 		} else {
2957 			btc8723b2ant_sw_mechanism1(btcoexist, false, true,
2958 						   false, false);
2959 			btc8723b2ant_sw_mechanism2(btcoexist, false, false,
2960 						   false, 0x18);
2961 		}
2962 	}
2963 }
2964 
2965 static void btc8723b2ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
2966 {
2967 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2968 	u8 algorithm = 0;
2969 
2970 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2971 		 "[BTCoex], RunCoexistMechanism()===>\n");
2972 
2973 	if (btcoexist->manual_control) {
2974 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2975 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2976 		return;
2977 	}
2978 
2979 	if (coex_sta->under_ips) {
2980 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2981 			 "[BTCoex], wifi is under IPS !!!\n");
2982 		return;
2983 	}
2984 
2985 	algorithm = btc8723b2ant_action_algorithm(btcoexist);
2986 	if (coex_sta->c2h_bt_inquiry_page &&
2987 	    (BT_8723B_2ANT_COEX_ALGO_PANHS != algorithm)) {
2988 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2989 			 "[BTCoex], BT is under inquiry/page scan !!\n");
2990 		btc8723b2ant_action_bt_inquiry(btcoexist);
2991 		return;
2992 	} else {
2993 		if (coex_dm->need_recover_0x948) {
2994 			coex_dm->need_recover_0x948 = false;
2995 			btcoexist->btc_write_2byte(btcoexist, 0x948,
2996 						   coex_dm->backup_0x948);
2997 		}
2998 	}
2999 
3000 	coex_dm->cur_algorithm = algorithm;
3001 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3002 		 "[BTCoex], Algorithm = %d\n",
3003 		 coex_dm->cur_algorithm);
3004 
3005 	if (btc8723b2ant_is_common_action(btcoexist)) {
3006 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3007 			 "[BTCoex], Action 2-Ant common\n");
3008 		coex_dm->auto_tdma_adjust = false;
3009 	} else {
3010 		if (coex_dm->cur_algorithm != coex_dm->pre_algorithm) {
3011 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3012 				 "[BTCoex], preAlgorithm=%d, curAlgorithm=%d\n",
3013 				 coex_dm->pre_algorithm,
3014 				 coex_dm->cur_algorithm);
3015 			coex_dm->auto_tdma_adjust = false;
3016 		}
3017 		switch (coex_dm->cur_algorithm) {
3018 		case BT_8723B_2ANT_COEX_ALGO_SCO:
3019 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3020 				 "[BTCoex], Action 2-Ant, algorithm = SCO\n");
3021 			btc8723b2ant_action_sco(btcoexist);
3022 			break;
3023 		case BT_8723B_2ANT_COEX_ALGO_HID:
3024 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3025 				 "[BTCoex], Action 2-Ant, algorithm = HID\n");
3026 			btc8723b2ant_action_hid(btcoexist);
3027 			break;
3028 		case BT_8723B_2ANT_COEX_ALGO_A2DP:
3029 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3030 				 "[BTCoex], Action 2-Ant, algorithm = A2DP\n");
3031 			btc8723b2ant_action_a2dp(btcoexist);
3032 			break;
3033 		case BT_8723B_2ANT_COEX_ALGO_A2DP_PANHS:
3034 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3035 				 "[BTCoex], Action 2-Ant, algorithm = A2DP+PAN(HS)\n");
3036 			btc8723b2ant_action_a2dp_pan_hs(btcoexist);
3037 			break;
3038 		case BT_8723B_2ANT_COEX_ALGO_PANEDR:
3039 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3040 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)\n");
3041 			btc8723b2ant_action_pan_edr(btcoexist);
3042 			break;
3043 		case BT_8723B_2ANT_COEX_ALGO_PANHS:
3044 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3045 				 "[BTCoex], Action 2-Ant, algorithm = HS mode\n");
3046 			btc8723b2ant_action_pan_hs(btcoexist);
3047 			break;
3048 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_A2DP:
3049 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3050 				 "[BTCoex], Action 2-Ant, algorithm = PAN+A2DP\n");
3051 			btc8723b2ant_action_pan_edr_a2dp(btcoexist);
3052 			break;
3053 		case BT_8723B_2ANT_COEX_ALGO_PANEDR_HID:
3054 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3055 				 "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)+HID\n");
3056 			btc8723b2ant_action_pan_edr_hid(btcoexist);
3057 			break;
3058 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
3059 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3060 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP+PAN\n");
3061 			btc8723b2ant_action_hid_a2dp_pan_edr(btcoexist);
3062 			break;
3063 		case BT_8723B_2ANT_COEX_ALGO_HID_A2DP:
3064 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3065 				 "[BTCoex], Action 2-Ant, algorithm = HID+A2DP\n");
3066 			btc8723b2ant_action_hid_a2dp(btcoexist);
3067 			break;
3068 		default:
3069 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3070 				 "[BTCoex], Action 2-Ant, algorithm = coexist All Off!!\n");
3071 			btc8723b2ant_coex_alloff(btcoexist);
3072 			break;
3073 		}
3074 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
3075 	}
3076 }
3077 
3078 static void btc8723b2ant_wifioff_hwcfg(struct btc_coexist *btcoexist)
3079 {
3080 	/* set wlan_act to low */
3081 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3082 	/* Force GNT_BT to High */
3083 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x3);
3084 	/* BT select s0/s1 is controlled by BT */
3085 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x0);
3086 }
3087 
3088 /*********************************************************************
3089  *  work around function start with wa_btc8723b2ant_
3090  *********************************************************************/
3091 /*********************************************************************
3092  *  extern function start with EXbtc8723b2ant_
3093  *********************************************************************/
3094 void ex_btc8723b2ant_init_hwconfig(struct btc_coexist *btcoexist)
3095 {
3096 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3097 	u8 u8tmp = 0;
3098 
3099 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3100 		 "[BTCoex], 2Ant Init HW Config!!\n");
3101 	coex_dm->bt_rf0x1e_backup =
3102 		btcoexist->btc_get_rf_reg(btcoexist, BTC_RF_A, 0x1e, 0xfffff);
3103 
3104 	/* 0x790[5:0] = 0x5 */
3105 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
3106 	u8tmp &= 0xc0;
3107 	u8tmp |= 0x5;
3108 	btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
3109 
3110 	/*Antenna config */
3111 	btc8723b2ant_set_ant_path(btcoexist, BTC_ANT_WIFI_AT_MAIN,
3112 				  true, false);
3113 	/* PTA parameter */
3114 	btc8723b_coex_tbl_type(btcoexist, FORCE_EXEC, 0);
3115 
3116 	/* Enable counter statistics */
3117 	/*0x76e[3] =1, WLAN_Act control by PTA*/
3118 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
3119 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
3120 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3121 }
3122 
3123 void ex_btc8723b2ant_init_coex_dm(struct btc_coexist *btcoexist)
3124 {
3125 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3126 
3127 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3128 		 "[BTCoex], Coex Mechanism Init!!\n");
3129 	btc8723b2ant_init_coex_dm(btcoexist);
3130 }
3131 
3132 void ex_btc8723b2ant_display_coex_info(struct btc_coexist *btcoexist)
3133 {
3134 	struct btc_board_info *board_info = &btcoexist->board_info;
3135 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
3136 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3137 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3138 	u8 u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
3139 	u32 u32tmp[4];
3140 	bool roam = false, scan = false;
3141 	bool link = false, wifi_under_5g = false;
3142 	bool bt_hs_on = false, wifi_busy = false;
3143 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
3144 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck;
3145 	u8 wifi_dot11_chnl, wifi_hs_chnl;
3146 	u32 fw_ver = 0, bt_patch_ver = 0;
3147 	u8 ap_num = 0;
3148 
3149 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3150 		 "\r\n ============[BT Coexist info]============");
3151 
3152 	if (btcoexist->manual_control) {
3153 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3154 			 "\r\n ==========[Under Manual Control]============");
3155 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3156 			 "\r\n ==========================================");
3157 	}
3158 
3159 	if (!board_info->bt_exist) {
3160 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n BT not exists !!!");
3161 		return;
3162 	}
3163 
3164 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3165 		 "Ant PG number/ Ant mechanism:",
3166 		 board_info->pg_ant_num, board_info->btdm_ant_num);
3167 
3168 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %d",
3169 		 "BT stack/ hci ext ver",
3170 		 ((stack_info->profile_notified) ? "Yes" : "No"),
3171 		 stack_info->hci_version);
3172 
3173 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3174 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3175 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3176 		 "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
3177 		 "CoexVer/ FwVer/ PatchVer",
3178 		 glcoex_ver_date_8723b_2ant, glcoex_ver_8723b_2ant,
3179 		 fw_ver, bt_patch_ver, bt_patch_ver);
3180 
3181 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3182 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
3183 			   &wifi_dot11_chnl);
3184 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
3185 
3186 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d(%d)",
3187 		 "Dot11 channel / HsChnl(HsMode)",
3188 		 wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
3189 
3190 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %3ph ",
3191 		 "H2C Wifi inform bt chnl Info", coex_dm->wifi_chnl_info);
3192 
3193 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
3194 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
3195 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
3196 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d",
3197 		 "Wifi rssi/ HS rssi/ AP#", wifi_rssi, bt_hs_rssi, ap_num);
3198 
3199 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3200 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3201 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3202 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3203 		 "Wifi link/ roam/ scan", link, roam, scan);
3204 
3205 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3206 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3207 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3208 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
3209 			   &wifi_traffic_dir);
3210 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s / %s/ %s ",
3211 		 "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
3212 		 ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
3213 		 (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
3214 		 ((!wifi_busy) ? "idle" :
3215 		 ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
3216 		  "uplink" : "downlink")));
3217 
3218 
3219 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d / %d / %d / %d",
3220 		 "SCO/HID/PAN/A2DP",
3221 		 bt_link_info->sco_exist, bt_link_info->hid_exist,
3222 		 bt_link_info->pan_exist, bt_link_info->a2dp_exist);
3223 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
3224 
3225 	bt_info_ext = coex_sta->bt_info_ext;
3226 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s",
3227 		 "BT Info A2DP rate",
3228 		 (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
3229 
3230 	for (i = 0; i < BT_INFO_SRC_8723B_2ANT_MAX; i++) {
3231 		if (coex_sta->bt_info_c2h_cnt[i]) {
3232 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3233 				 "\r\n %-35s = %7ph(%d)",
3234 				 glbt_info_src_8723b_2ant[i],
3235 				 coex_sta->bt_info_c2h[i],
3236 				 coex_sta->bt_info_c2h_cnt[i]);
3237 		}
3238 	}
3239 
3240 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %s/%s",
3241 		 "PS state, IPS/LPS",
3242 		 ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
3243 		 ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")));
3244 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
3245 
3246 	/* Sw mechanism	*/
3247 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3248 		 "\r\n %-35s", "============[Sw mechanism]============");
3249 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d ",
3250 		 "SM1[ShRf/ LpRA/ LimDig]", coex_dm->cur_rf_rx_lpf_shrink,
3251 		 coex_dm->cur_low_penalty_ra, coex_dm->limited_dig);
3252 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d/ %d(0x%x) ",
3253 		 "SM2[AgcT/ AdcB/ SwDacSwing(lvl)]",
3254 		 coex_dm->cur_agc_table_en, coex_dm->cur_adc_back_off,
3255 		 coex_dm->cur_dac_swing_on, coex_dm->cur_dac_swing_lvl);
3256 
3257 	/* Fw mechanism	*/
3258 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3259 		 "============[Fw mechanism]============");
3260 
3261 	ps_tdma_case = coex_dm->cur_ps_tdma;
3262 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3263 		 "\r\n %-35s = %5ph case-%d (auto:%d)",
3264 		 "PS TDMA", coex_dm->ps_tdma_para,
3265 		 ps_tdma_case, coex_dm->auto_tdma_adjust);
3266 
3267 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d ",
3268 		 "DecBtPwr/ IgnWlanAct", coex_dm->cur_dec_bt_pwr,
3269 		 coex_dm->cur_ignore_wlan_act);
3270 
3271 	/* Hw setting */
3272 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
3273 		 "============[Hw setting]============");
3274 
3275 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x",
3276 		 "RF-A, 0x1e initVal", coex_dm->bt_rf0x1e_backup);
3277 
3278 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
3279 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x880);
3280 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3281 		 "0x778/0x880[29:25]", u8tmp[0],
3282 		 (u32tmp[0] & 0x3e000000) >> 25);
3283 
3284 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
3285 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
3286 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
3287 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3288 		 "0x948/ 0x67[5] / 0x765",
3289 		 u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
3290 
3291 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
3292 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
3293 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
3294 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3295 		 "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
3296 		 u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
3297 
3298 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
3299 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
3300 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
3301 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
3302 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3303 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3304 		 "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
3305 		 ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
3306 		 ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
3307 
3308 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
3309 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
3310 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3311 		 "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
3312 
3313 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
3314 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
3315 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x",
3316 		 "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
3317 
3318 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
3319 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
3320 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
3321 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
3322 
3323 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
3324 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
3325 
3326 	fa_ofdm = ((u32tmp[0]&0xffff0000) >> 16) +
3327 		  ((u32tmp[1]&0xffff0000) >> 16) +
3328 		   (u32tmp[1] & 0xffff) +
3329 		   (u32tmp[2] & 0xffff) +
3330 		  ((u32tmp[3]&0xffff0000) >> 16) +
3331 		   (u32tmp[3] & 0xffff);
3332 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
3333 
3334 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3335 		 "OFDM-CCA/OFDM-FA/CCK-FA",
3336 		 u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
3337 
3338 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
3339 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
3340 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
3341 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
3342 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3343 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3344 		 "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
3345 		 u32tmp[0], u32tmp[1], u32tmp[2], u8tmp[0]);
3346 
3347 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
3348 		 "0x770(high-pri rx/tx)",
3349 		 coex_sta->high_priority_rx, coex_sta->high_priority_tx);
3350 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s = %d/ %d",
3351 		 "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
3352 		 coex_sta->low_priority_tx);
3353 #if (BT_AUTO_REPORT_ONLY_8723B_2ANT == 1)
3354 	btc8723b2ant_monitor_bt_ctr(btcoexist);
3355 #endif
3356 	btcoexist->btc_disp_dbg_msg(btcoexist,
3357 	BTC_DBG_DISP_COEX_STATISTICS);
3358 }
3359 
3360 void ex_btc8723b2ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
3361 {
3362 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3363 
3364 	if (BTC_IPS_ENTER == type) {
3365 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3366 			 "[BTCoex], IPS ENTER notify\n");
3367 		coex_sta->under_ips = true;
3368 		btc8723b2ant_wifioff_hwcfg(btcoexist);
3369 		btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3370 		btc8723b2ant_coex_alloff(btcoexist);
3371 	} else if (BTC_IPS_LEAVE == type) {
3372 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3373 			 "[BTCoex], IPS LEAVE notify\n");
3374 		coex_sta->under_ips = false;
3375 		ex_btc8723b2ant_init_hwconfig(btcoexist);
3376 		btc8723b2ant_init_coex_dm(btcoexist);
3377 		btc8723b2ant_query_bt_info(btcoexist);
3378 	}
3379 }
3380 
3381 void ex_btc8723b2ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
3382 {
3383 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3384 
3385 	if (BTC_LPS_ENABLE == type) {
3386 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3387 			 "[BTCoex], LPS ENABLE notify\n");
3388 		coex_sta->under_lps = true;
3389 	} else if (BTC_LPS_DISABLE == type) {
3390 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3391 			 "[BTCoex], LPS DISABLE notify\n");
3392 		coex_sta->under_lps = false;
3393 	}
3394 }
3395 
3396 void ex_btc8723b2ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
3397 {
3398 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3399 
3400 	if (BTC_SCAN_START == type)
3401 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3402 			 "[BTCoex], SCAN START notify\n");
3403 	else if (BTC_SCAN_FINISH == type)
3404 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3405 			 "[BTCoex], SCAN FINISH notify\n");
3406 }
3407 
3408 void ex_btc8723b2ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
3409 {
3410 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3411 
3412 	if (BTC_ASSOCIATE_START == type)
3413 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3414 			 "[BTCoex], CONNECT START notify\n");
3415 	else if (BTC_ASSOCIATE_FINISH == type)
3416 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3417 			 "[BTCoex], CONNECT FINISH notify\n");
3418 }
3419 
3420 void ex_btc8723b2ant_media_status_notify(struct btc_coexist *btcoexist,
3421 					 u8 type)
3422 {
3423 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3424 	u8 h2c_parameter[3] = {0};
3425 	u32 wifi_bw;
3426 	u8 wifi_central_chnl;
3427 
3428 	if (BTC_MEDIA_CONNECT == type)
3429 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3430 			 "[BTCoex], MEDIA connect notify\n");
3431 	else
3432 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3433 			 "[BTCoex], MEDIA disconnect notify\n");
3434 
3435 	/* only 2.4G we need to inform bt the chnl mask */
3436 	btcoexist->btc_get(btcoexist,
3437 		BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifi_central_chnl);
3438 	if ((BTC_MEDIA_CONNECT == type) &&
3439 	    (wifi_central_chnl <= 14)) {
3440 		h2c_parameter[0] = 0x1;
3441 		h2c_parameter[1] = wifi_central_chnl;
3442 		btcoexist->btc_get(btcoexist,
3443 			BTC_GET_U4_WIFI_BW, &wifi_bw);
3444 		if (BTC_WIFI_BW_HT40 == wifi_bw)
3445 			h2c_parameter[2] = 0x30;
3446 		else
3447 			h2c_parameter[2] = 0x20;
3448 	}
3449 
3450 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
3451 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
3452 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
3453 
3454 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3455 		 "[BTCoex], FW write 0x66=0x%x\n",
3456 		 h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
3457 		 h2c_parameter[2]);
3458 
3459 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
3460 }
3461 
3462 void ex_btc8723b2ant_special_packet_notify(struct btc_coexist *btcoexist,
3463 					   u8 type)
3464 {
3465 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3466 
3467 	if (type == BTC_PACKET_DHCP)
3468 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3469 			 "[BTCoex], DHCP Packet notify\n");
3470 }
3471 
3472 void ex_btc8723b2ant_bt_info_notify(struct btc_coexist *btcoexist,
3473 				    u8 *tmpbuf, u8 length)
3474 {
3475 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3476 	u8 bt_info = 0;
3477 	u8 i, rsp_source = 0;
3478 	bool bt_busy = false, limited_dig = false;
3479 	bool wifi_connected = false;
3480 
3481 	coex_sta->c2h_bt_info_req_sent = false;
3482 
3483 	rsp_source = tmpbuf[0]&0xf;
3484 	if (rsp_source >= BT_INFO_SRC_8723B_2ANT_MAX)
3485 		rsp_source = BT_INFO_SRC_8723B_2ANT_WIFI_FW;
3486 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
3487 
3488 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3489 		 "[BTCoex], Bt info[%d], length=%d, hex data=[",
3490 		 rsp_source, length);
3491 	for (i = 0; i < length; i++) {
3492 		coex_sta->bt_info_c2h[rsp_source][i] = tmpbuf[i];
3493 		if (i == 1)
3494 			bt_info = tmpbuf[i];
3495 		if (i == length-1)
3496 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3497 				 "0x%02x]\n", tmpbuf[i]);
3498 		else
3499 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3500 				 "0x%02x, ", tmpbuf[i]);
3501 	}
3502 
3503 	if (btcoexist->manual_control) {
3504 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3505 			 "[BTCoex], BtInfoNotify(), return for Manual CTRL<===\n");
3506 		return;
3507 	}
3508 
3509 	if (BT_INFO_SRC_8723B_2ANT_WIFI_FW != rsp_source) {
3510 		coex_sta->bt_retry_cnt =	/* [3:0]*/
3511 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
3512 
3513 		coex_sta->bt_rssi =
3514 			coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
3515 
3516 		coex_sta->bt_info_ext =
3517 			coex_sta->bt_info_c2h[rsp_source][4];
3518 
3519 		/* Here we need to resend some wifi info to BT
3520 		     because bt is reset and loss of the info.
3521 		 */
3522 		if ((coex_sta->bt_info_ext & BIT1)) {
3523 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3524 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
3525 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3526 					   &wifi_connected);
3527 			if (wifi_connected)
3528 				ex_btc8723b2ant_media_status_notify(
3529 							btcoexist,
3530 							BTC_MEDIA_CONNECT);
3531 			else
3532 				ex_btc8723b2ant_media_status_notify(
3533 							btcoexist,
3534 							BTC_MEDIA_DISCONNECT);
3535 		}
3536 
3537 		if ((coex_sta->bt_info_ext & BIT3)) {
3538 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3539 				 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
3540 			btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC,
3541 						     false);
3542 		} else {
3543 			/* BT already NOT ignore Wlan active, do nothing here.*/
3544 		}
3545 #if (BT_AUTO_REPORT_ONLY_8723B_2ANT == 0)
3546 		if ((coex_sta->bt_info_ext & BIT4)) {
3547 			/* BT auto report already enabled, do nothing*/
3548 		} else {
3549 			btc8723b2ant_bt_auto_report(btcoexist, FORCE_EXEC,
3550 						    true);
3551 		}
3552 #endif
3553 	}
3554 
3555 	/* check BIT2 first ==> check if bt is under inquiry or page scan*/
3556 	if (bt_info & BT_INFO_8723B_2ANT_B_INQ_PAGE)
3557 		coex_sta->c2h_bt_inquiry_page = true;
3558 	else
3559 		coex_sta->c2h_bt_inquiry_page = false;
3560 
3561 	/* set link exist status*/
3562 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
3563 		coex_sta->bt_link_exist = false;
3564 		coex_sta->pan_exist = false;
3565 		coex_sta->a2dp_exist = false;
3566 		coex_sta->hid_exist = false;
3567 		coex_sta->sco_exist = false;
3568 	} else { /* connection exists */
3569 		coex_sta->bt_link_exist = true;
3570 		if (bt_info & BT_INFO_8723B_2ANT_B_FTP)
3571 			coex_sta->pan_exist = true;
3572 		else
3573 			coex_sta->pan_exist = false;
3574 		if (bt_info & BT_INFO_8723B_2ANT_B_A2DP)
3575 			coex_sta->a2dp_exist = true;
3576 		else
3577 			coex_sta->a2dp_exist = false;
3578 		if (bt_info & BT_INFO_8723B_2ANT_B_HID)
3579 			coex_sta->hid_exist = true;
3580 		else
3581 			coex_sta->hid_exist = false;
3582 		if (bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO)
3583 			coex_sta->sco_exist = true;
3584 		else
3585 			coex_sta->sco_exist = false;
3586 	}
3587 
3588 	btc8723b2ant_update_bt_link_info(btcoexist);
3589 
3590 	if (!(bt_info & BT_INFO_8723B_2ANT_B_CONNECTION)) {
3591 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_NON_CONNECTED_IDLE;
3592 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3593 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
3594 	/* connection exists but no busy */
3595 	} else if (bt_info == BT_INFO_8723B_2ANT_B_CONNECTION) {
3596 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_CONNECTED_IDLE;
3597 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3598 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
3599 	} else if ((bt_info & BT_INFO_8723B_2ANT_B_SCO_ESCO) ||
3600 		   (bt_info & BT_INFO_8723B_2ANT_B_SCO_BUSY)) {
3601 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_SCO_BUSY;
3602 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3603 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
3604 	} else if (bt_info&BT_INFO_8723B_2ANT_B_ACL_BUSY) {
3605 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_ACL_BUSY;
3606 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3607 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
3608 	} else {
3609 		coex_dm->bt_status = BT_8723B_2ANT_BT_STATUS_MAX;
3610 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3611 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
3612 	}
3613 
3614 	if ((BT_8723B_2ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3615 	    (BT_8723B_2ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3616 	    (BT_8723B_2ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status)) {
3617 		bt_busy = true;
3618 		limited_dig = true;
3619 	} else {
3620 		bt_busy = false;
3621 		limited_dig = false;
3622 	}
3623 
3624 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
3625 
3626 	coex_dm->limited_dig = limited_dig;
3627 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_LIMITED_DIG, &limited_dig);
3628 
3629 	btc8723b2ant_run_coexist_mechanism(btcoexist);
3630 }
3631 
3632 void ex_btc8723b2ant_halt_notify(struct btc_coexist *btcoexist)
3633 {
3634 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3635 
3636 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
3637 
3638 	btc8723b2ant_wifioff_hwcfg(btcoexist);
3639 	btc8723b2ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3640 	ex_btc8723b2ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
3641 }
3642 
3643 void ex_btc8723b2ant_periodical(struct btc_coexist *btcoexist)
3644 {
3645 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3646 	struct btc_board_info *board_info = &btcoexist->board_info;
3647 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
3648 	static u8 dis_ver_info_cnt;
3649 	u32 fw_ver = 0, bt_patch_ver = 0;
3650 
3651 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3652 		 "[BTCoex], ==========================Periodical===========================\n");
3653 
3654 	if (dis_ver_info_cnt <= 5) {
3655 		dis_ver_info_cnt += 1;
3656 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3657 			 "[BTCoex], ****************************************************************\n");
3658 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3659 			 "[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
3660 			      board_info->pg_ant_num,
3661 			      board_info->btdm_ant_num,
3662 			      board_info->btdm_ant_pos);
3663 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3664 			 "[BTCoex], BT stack/ hci ext ver = %s / %d\n",
3665 			      stack_info->profile_notified ? "Yes" : "No",
3666 			      stack_info->hci_version);
3667 		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
3668 				   &bt_patch_ver);
3669 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3670 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3671 			 "[BTCoex], CoexVer/ fw_ver/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
3672 			      glcoex_ver_date_8723b_2ant, glcoex_ver_8723b_2ant,
3673 			      fw_ver, bt_patch_ver, bt_patch_ver);
3674 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3675 			 "[BTCoex], ****************************************************************\n");
3676 	}
3677 
3678 #if (BT_AUTO_REPORT_ONLY_8723B_2ANT == 0)
3679 	btc8723b2ant_query_bt_info(btcoexist);
3680 	btc8723b2ant_monitor_bt_ctr(btcoexist);
3681 	btc8723b2ant_monitor_bt_enable_disable(btcoexist);
3682 #else
3683 	if (btc8723b2ant_is_wifi_status_changed(btcoexist) ||
3684 	    coex_dm->auto_tdma_adjust)
3685 		btc8723b2ant_run_coexist_mechanism(btcoexist);
3686 #endif
3687 }
3688