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