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