OSDN Git Service

Add rtl8821ce driver version 5.5.2
[android-x86/external-kernel-drivers.git] / rtl8821ce / hal / btc / halbtc8821c2ant.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2016 - 2017 Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  *****************************************************************************/
15
16 #include "mp_precomp.h"
17
18 #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1)
19
20 #if (RTL8821C_SUPPORT == 1)
21 static u8       *trace_buf = &gl_btc_trace_buf[0];
22
23 const char *const glbt_info_src_8821c_2ant[] = {
24         "BT Info[wifi fw]",
25         "BT Info[bt rsp]",
26         "BT Info[bt auto report]",
27 };
28
29 u32 glcoex_ver_date_8821c_2ant = 20180712;
30 u32 glcoex_ver_8821c_2ant = 0x32;
31 u32 glcoex_ver_btdesired_8821c_2ant = 0x28;
32
33 static
34 u8 halbtc8821c2ant_bt_rssi_state(struct btc_coexist *btc,
35                                  u8 *ppre_bt_rssi_state, u8 level_num,
36                                  u8 rssi_thresh, u8 rssi_thresh1)
37 {
38         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
39         s32 bt_rssi = 0;
40         u8 bt_rssi_state = *ppre_bt_rssi_state;
41
42         bt_rssi = coex_sta->bt_rssi;
43
44         if (level_num == 2) {
45                 if ((*ppre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
46                     (*ppre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
47                         if (bt_rssi >= (rssi_thresh +
48                                         BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
49                                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
50                         else
51                                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
52                 } else {
53                         if (bt_rssi < rssi_thresh)
54                                 bt_rssi_state = BTC_RSSI_STATE_LOW;
55                         else
56                                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
57                 }
58         } else if (level_num == 3) {
59                 if (rssi_thresh > rssi_thresh1) {
60                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
61                                     "[BTCoex], BT Rssi thresh error!!\n");
62                         BTC_TRACE(trace_buf);
63                         return *ppre_bt_rssi_state;
64                 }
65
66                 if ((*ppre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
67                     (*ppre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
68                         if (bt_rssi >= (rssi_thresh +
69                                         BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
70                                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
71                         else
72                                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
73                 } else if ((*ppre_bt_rssi_state == BTC_RSSI_STATE_MEDIUM) ||
74                         (*ppre_bt_rssi_state == BTC_RSSI_STATE_STAY_MEDIUM)) {
75                         if (bt_rssi >= (rssi_thresh1 +
76                                         BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
77                                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
78                         else if (bt_rssi < rssi_thresh)
79                                 bt_rssi_state = BTC_RSSI_STATE_LOW;
80                         else
81                                 bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
82                 } else {
83                         if (bt_rssi < rssi_thresh1)
84                                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
85                         else
86                                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
87                 }
88         }
89
90         *ppre_bt_rssi_state = bt_rssi_state;
91
92         return bt_rssi_state;
93 }
94
95 static
96 u8 halbtc8821c2ant_wifi_rssi_state(struct btc_coexist *btc,
97                                    u8 *pprewifi_rssi_state,
98                                    u8 level_num,
99                                    u8 rssi_thresh, u8 rssi_thresh1)
100 {
101         s32 wifi_rssi = 0;
102         u8 wifi_rssi_state = *pprewifi_rssi_state;
103
104         btc->btc_get(btc, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
105
106         if (level_num == 2) {
107                 if ((*pprewifi_rssi_state == BTC_RSSI_STATE_LOW) ||
108                     (*pprewifi_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
109                         if (wifi_rssi >= (rssi_thresh +
110                                           BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
111                                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
112                         else
113                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
114                 } else {
115                         if (wifi_rssi < rssi_thresh)
116                                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
117                         else
118                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
119                 }
120         } else if (level_num == 3) {
121                 if (rssi_thresh > rssi_thresh1) {
122                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
123                                     "[BTCoex], wifi RSSI thresh error!!\n");
124                         BTC_TRACE(trace_buf);
125                         return *pprewifi_rssi_state;
126                 }
127
128                 if ((*pprewifi_rssi_state == BTC_RSSI_STATE_LOW) ||
129                     (*pprewifi_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
130                         if (wifi_rssi >= (rssi_thresh +
131                                           BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
132                                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
133                         else
134                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
135                 } else if ((*pprewifi_rssi_state == BTC_RSSI_STATE_MEDIUM) ||
136                         (*pprewifi_rssi_state == BTC_RSSI_STATE_STAY_MEDIUM)) {
137                         if (wifi_rssi >= (rssi_thresh1 +
138                                           BTC_RSSI_COEX_THRESH_TOL_8821C_2ANT))
139                                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
140                         else if (wifi_rssi < rssi_thresh)
141                                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
142                         else
143                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
144                 } else {
145                         if (wifi_rssi < rssi_thresh1)
146                                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
147                         else
148                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
149                 }
150         }
151
152         *pprewifi_rssi_state = wifi_rssi_state;
153
154         return wifi_rssi_state;
155 }
156
157 static
158 void halbtc8821c2ant_coex_switch_thres(struct btc_coexist *btc,
159                                        u8 isolation_measuared)
160 {
161         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
162         s8      interference_wl_tx = 0, interference_bt_tx = 0;
163
164         interference_wl_tx = BT_8821C_2ANT_WIFI_MAX_TX_POWER -
165                                                 isolation_measuared;
166         interference_bt_tx = BT_8821C_2ANT_BT_MAX_TX_POWER -
167                                                 isolation_measuared;
168
169         coex_sta->wifi_coex_thres = BT_8821C_2ANT_WIFI_RSSI_COEXSWITCH_THRES1;
170         coex_sta->wifi_coex_thres2 =  BT_8821C_2ANT_WIFI_RSSI_COEXSWITCH_THRES2;
171
172         coex_sta->bt_coex_thres = BT_8821C_2ANT_BT_RSSI_COEXSWITCH_THRES1;
173         coex_sta->bt_coex_thres2 = BT_8821C_2ANT_BT_RSSI_COEXSWITCH_THRES2;
174
175 #if 0
176         coex_sta->wifi_coex_thres =
177                 interference_wl_tx + BT_8821C_2ANT_WIFI_SIR_THRES1;
178         coex_sta->wifi_coex_thres2 =
179                 interference_wl_tx + BT_8821C_2ANT_WIFI_SIR_THRES2;
180
181         coex_sta->bt_coex_thres =
182                 interference_bt_tx + BT_8821C_2ANT_BT_SIR_THRES1;
183         coex_sta->bt_coex_thres2 =
184                 interference_bt_tx + BT_8821C_2ANT_BT_SIR_THRES2;
185
186         if  (BT_8821C_2ANT_WIFI_RSSI_COEXSWITCH_THRES1 <
187                 (isolation_measuared - BT_8821C_2ANT_DEFAULT_ISOLATION))
188                 coex_sta->wifi_coex_thres =
189                                 BT_8821C_2ANT_WIFI_RSSI_COEXSWITCH_THRES1;
190         else
191                 coex_sta->wifi_coex_thres =
192                 BT_8821C_2ANT_WIFI_RSSI_COEXSWITCH_THRES1 -
193                   (isolation_measuared - BT_8821C_2ANT_DEFAULT_ISOLATION);
194
195         if  (BT_8821C_2ANT_BT_RSSI_COEXSWITCH_THRES1 <
196                 (isolation_measuared - BT_8821C_2ANT_DEFAULT_ISOLATION))
197                 coex_sta->bt_coex_thres =
198                                  BT_8821C_2ANT_BT_RSSI_COEXSWITCH_THRES1;
199         else
200                 coex_sta->bt_coex_thres =
201                 BT_8821C_2ANT_BT_RSSI_COEXSWITCH_THRES1 -
202                  (isolation_measuared - BT_8821C_2ANT_DEFAULT_ISOLATION);
203
204 #endif
205 }
206
207 static
208 void halbtc8821c2ant_low_penalty_ra(struct btc_coexist *btc,
209                                     boolean force_exec, boolean low_penalty_ra,
210                                     u8 thres)
211 {
212         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
213         static u8 cur_thres;
214
215         if (!force_exec) {
216                 if (low_penalty_ra == coex_dm->cur_low_penalty_ra &&
217                     thres == cur_thres)
218                         return;
219         }
220
221         if (low_penalty_ra)
222                 btc->btc_phydm_modify_RA_PCR_threshold(btc, 0, thres);
223         else
224                 btc->btc_phydm_modify_RA_PCR_threshold(btc, 0, 0);
225
226         coex_dm->cur_low_penalty_ra = low_penalty_ra;
227         cur_thres = thres;
228 }
229
230 static
231 void halbtc8821c2ant_set_antdiv_hwsw(struct btc_coexist *btc,
232                                      boolean force_exec, boolean is_hw_div)
233 {
234         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
235         static u8 pre_antdiv_type;
236
237         coex_dm->cur_antdiv_type =  ((is_hw_div) ? 1 : 0);
238
239         if (!force_exec) {
240                 if (coex_dm->cur_antdiv_type == pre_antdiv_type)
241                         return;
242         }
243
244         btc->btc_phydm_modify_antdiv_hwsw(btc, coex_dm->cur_antdiv_type);
245
246         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
247                     "[BTCoex], %s = %d!!\n", __func__,
248                     coex_dm->cur_antdiv_type);
249         BTC_TRACE(trace_buf);
250
251         pre_antdiv_type = coex_dm->cur_antdiv_type;
252 }
253
254 static
255 void halbtc8821c2ant_write_scbd(struct btc_coexist *btc, u16 bitpos,
256                                 boolean state)
257 {
258         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
259         static u16 originalval = 0x8002, preval;
260
261         if (state)
262                 originalval = originalval | bitpos;
263         else
264                 originalval = originalval & (~bitpos);
265
266         coex_sta->score_board_WB = originalval;
267
268         if (originalval != preval) {
269                 preval = originalval;
270                 btc->btc_write_2byte(btc, 0xaa, originalval);
271         } else {
272                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
273                             "[BTCoex], %s: return for nochange\n", __func__);
274                 BTC_TRACE(trace_buf);
275         }
276 }
277
278 static
279 void halbtc8821c2ant_read_scbd(struct btc_coexist *btc, u16 *score_board_val)
280 {
281         *score_board_val = (btc->btc_read_2byte(btc, 0xaa)) & 0x7fff;
282 }
283
284 static
285 void halbtc8821c2ant_query_bt_info(struct btc_coexist *btc)
286 {
287         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
288         u8 h2c_parameter[1] = {0};
289
290         if (coex_sta->bt_disabled) {
291                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
292                         "[BTCoex], No query BT info because BT is disabled!\n");
293                 BTC_TRACE(trace_buf);
294                 return;
295         }
296
297         h2c_parameter[0] |= BIT(0);     /* trigger */
298
299         btc->btc_fill_h2c(btc, 0x61, 1, h2c_parameter);
300 }
301
302 static
303 void halbtc8821c2ant_enable_gnt_to_gpio(struct btc_coexist *btc,
304                                         boolean isenable)
305 {
306         static u8       bit_val[5] = {0, 0, 0, 0, 0};
307         static boolean state;
308
309         if (!btc->dbg_mode)
310                 return;
311
312         if (state == isenable)
313                 return;
314
315         state = isenable;
316
317         if (isenable) {
318                 /* enable GNT_WL, GNT_BT to GPIO for debug */
319                 btc->btc_write_1byte_bitmask(btc, 0x73, 0x8, 0x1);
320
321                 /* store original value */
322                 /*0x66[4] */
323                 bit_val[0] = (btc->btc_read_1byte(btc, 0x66) & BIT(4)) >> 4;
324                 /*0x66[8] */
325                 bit_val[1] = (btc->btc_read_1byte(btc, 0x67) & BIT(0));
326                 /*0x40[19] */
327                 bit_val[2] = (btc->btc_read_1byte(btc, 0x42) & BIT(3)) >> 3;
328                 /*0x64[15] */
329                 bit_val[3] = (btc->btc_read_1byte(btc, 0x65) & BIT(7)) >> 7;
330                 /*0x70[18] */
331                 bit_val[4] = (btc->btc_read_1byte(btc, 0x72) & BIT(2)) >> 2;
332
333                 /*  switch GPIO Mux */
334                 /*0x66[4] = 0 */
335                 btc->btc_write_1byte_bitmask(btc, 0x66, BIT(4), 0x0);
336                 /*0x66[8] = 0 */
337                 btc->btc_write_1byte_bitmask(btc, 0x67, BIT(0), 0x0);
338                 /*0x40[19] = 0 */
339                 btc->btc_write_1byte_bitmask(btc, 0x42, BIT(3), 0x0);
340                 /*0x64[15] = 0 */
341                 btc->btc_write_1byte_bitmask(btc, 0x65, BIT(7), 0x0);
342                  /*0x70[18] = 0 */
343                 btc->btc_write_1byte_bitmask(btc, 0x72, BIT(2), 0x0);
344         } else {
345                 btc->btc_write_1byte_bitmask(btc, 0x73, 0x8, 0x0);
346
347                 /*  Restore original value */
348                 /*  switch GPIO Mux */
349                 /*0x66[4] = 0 */
350                 btc->btc_write_1byte_bitmask(btc, 0x66, BIT(4), bit_val[0]);
351                 /*0x66[8] = 0 */
352                 btc->btc_write_1byte_bitmask(btc, 0x67, BIT(0), bit_val[1]);
353                 /*0x40[19] = 0 */
354                 btc->btc_write_1byte_bitmask(btc, 0x42, BIT(3), bit_val[2]);
355                 /*0x64[15] = 0 */
356                 btc->btc_write_1byte_bitmask(btc, 0x65, BIT(7), bit_val[3]);
357                 /*0x70[18] = 0 */
358                 btc->btc_write_1byte_bitmask(btc, 0x72, BIT(2), bit_val[4]);
359         }
360 }
361
362 static
363 void halbtc8821c2ant_monitor_bt_ctr(struct btc_coexist *btc)
364 {
365         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
366         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
367         u32 reg_hp_txrx, reg_lp_txrx, u32tmp;
368         u32 reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
369         static u8       num_of_bt_counter_chk, cnt_slave, cnt_overhead,
370                 cnt_autoslot_hang;
371         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
372
373         reg_hp_txrx = 0x770;
374         reg_lp_txrx = 0x774;
375
376         u32tmp = btc->btc_read_4byte(btc, reg_hp_txrx);
377         reg_hp_tx = u32tmp & MASKLWORD;
378         reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
379
380         u32tmp = btc->btc_read_4byte(btc, reg_lp_txrx);
381         reg_lp_tx = u32tmp & MASKLWORD;
382         reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
383
384         coex_sta->high_priority_tx = reg_hp_tx;
385         coex_sta->high_priority_rx = reg_hp_rx;
386         coex_sta->low_priority_tx = reg_lp_tx;
387         coex_sta->low_priority_rx = reg_lp_rx;
388
389         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
390                     "[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
391                     reg_hp_rx, reg_hp_tx, reg_lp_rx, reg_lp_tx);
392
393         BTC_TRACE(trace_buf);
394
395         if (coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_NCON_IDLE) {
396
397                 if (coex_sta->high_priority_rx >= 15) {
398                         if (cnt_overhead < 3)
399                                 cnt_overhead++;
400
401                         if (cnt_overhead == 3)
402                                 coex_sta->is_hi_pri_rx_overhead = TRUE;
403
404                 } else {
405                         if (cnt_overhead > 0)
406                                 cnt_overhead--;
407
408                         if (cnt_overhead == 0)
409                                 coex_sta->is_hi_pri_rx_overhead = FALSE;
410                 }
411         } else
412                 coex_sta->is_hi_pri_rx_overhead = FALSE;
413
414         /* reset counter */
415         btc->btc_write_1byte(btc, 0x76e, 0xc);
416
417         if (coex_sta->low_priority_tx > 1150 &&
418             !coex_sta->c2h_bt_inquiry_page)
419                 coex_sta->pop_event_cnt++;
420
421         if (coex_sta->low_priority_rx >= 1150 &&
422             coex_sta->low_priority_rx >= coex_sta->low_priority_tx &&
423             !coex_sta->under_ips && !coex_sta->c2h_bt_inquiry_page &&
424             (bt_link_info->a2dp_exist || bt_link_info->pan_exist)) {
425                 if (cnt_slave >= 2) {
426                         bt_link_info->slave_role = TRUE;
427                         cnt_slave = 2;
428                 } else
429                         cnt_slave++;
430         } else {
431                 if (cnt_slave == 0)     {
432                         bt_link_info->slave_role = FALSE;
433                         cnt_slave = 0;
434                 } else
435                         cnt_slave--;
436         }
437
438         if (coex_sta->is_tdma_btautoslot) {
439                 if (coex_sta->low_priority_tx >= 1300 &&
440                     coex_sta->low_priority_rx <= 150) {
441                         if (cnt_autoslot_hang >= 2) {
442                                 coex_sta->is_tdma_btautoslot_hang = TRUE;
443                                 cnt_autoslot_hang = 2;
444                         } else
445                                 cnt_autoslot_hang++;
446                 } else {
447                         if (cnt_autoslot_hang == 0)     {
448                                 coex_sta->is_tdma_btautoslot_hang = FALSE;
449                                 cnt_autoslot_hang = 0;
450                         } else
451                                 cnt_autoslot_hang--;
452                 }
453         }
454
455         if (coex_sta->sco_exist) {
456                 if (coex_sta->high_priority_tx >= 400 &&
457                     coex_sta->high_priority_rx >= 400)
458                         coex_sta->is_esco_mode = FALSE;
459                 else
460                         coex_sta->is_esco_mode = TRUE;
461         }
462
463         if (bt_link_info->hid_only) {
464                 if (coex_sta->low_priority_tx > 100)
465                         coex_sta->is_hid_low_pri_tx_overhead = true;
466                 else
467                         coex_sta->is_hid_low_pri_tx_overhead = false;
468         }
469
470         if (!coex_sta->bt_disabled) {
471                 if (coex_sta->high_priority_tx == 0 &&
472                     coex_sta->high_priority_rx == 0 &&
473                     coex_sta->low_priority_tx == 0 &&
474                     coex_sta->low_priority_rx == 0) {
475                         num_of_bt_counter_chk++;
476                         if (num_of_bt_counter_chk >= 3) {
477                                 halbtc8821c2ant_query_bt_info(btc);
478                                 num_of_bt_counter_chk = 0;
479                         }
480                 }
481         }
482 }
483
484 static
485 void halbtc8821c2ant_monitor_wifi_ctr(struct btc_coexist *btc)
486 {
487         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
488         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
489         boolean wifi_busy = FALSE, wifi_scan = FALSE;
490         static u8 wl_noisy_count0, wl_noisy_count1 = 3, wl_noisy_count2;
491         u32 cnt_cck;
492         static u8 cnt_ccklocking;
493         u8 h2c_parameter[1] = {0};
494         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
495
496         /* Only enable for windows becaus 8821cu
497          * H2C 0x69 unknown fail @ linux
498          */
499         if (btc->chip_interface != BTC_INTF_USB) {
500                 /*send h2c to query WL FW dbg info  */
501                 if ((coex_dm->cur_ps_tdma_on && coex_sta->force_lps_ctrl) ||
502                     (coex_sta->acl_busy && bt_link_info->a2dp_exist)) {
503                         h2c_parameter[0] = 0x8;
504                         btc->btc_fill_h2c(btc, 0x69, 1, h2c_parameter);
505                 }
506         }
507
508         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
509         btc->btc_get(btc, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
510
511         coex_sta->crc_ok_cck =
512                 btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_OK_CCK);
513         coex_sta->crc_ok_11g =
514             btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_OK_LEGACY);
515         coex_sta->crc_ok_11n =
516                 btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_OK_HT);
517         coex_sta->crc_ok_11n_vht =
518                 btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_OK_VHT);
519
520         coex_sta->crc_err_cck =
521             btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_ERROR_CCK);
522         coex_sta->crc_err_11g =
523           btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_ERROR_LEGACY);
524         coex_sta->crc_err_11n =
525             btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_ERROR_HT);
526         coex_sta->crc_err_11n_vht =
527             btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_ERROR_VHT);
528
529         /* CCK lock identification */
530         if (coex_sta->cck_lock)
531                 cnt_ccklocking++;
532         else if (cnt_ccklocking != 0)
533                 cnt_ccklocking--;
534
535         if (cnt_ccklocking >= 3) {
536                 cnt_ccklocking = 3;
537                 coex_sta->cck_lock_ever = TRUE;
538         }
539
540         /* WiFi environment noisy identification */
541         cnt_cck = coex_sta->crc_ok_cck + coex_sta->crc_err_cck;
542
543         if (!wifi_busy && !coex_sta->cck_lock) {
544                 if (cnt_cck > 250) {
545                         if (wl_noisy_count2 < 3)
546                                 wl_noisy_count2++;
547
548                         if (wl_noisy_count2 == 3) {
549                                 wl_noisy_count0 = 0;
550                                 wl_noisy_count1 = 0;
551                         }
552
553                 } else if (cnt_cck < 100) {
554                         if (wl_noisy_count0 < 3)
555                                 wl_noisy_count0++;
556
557                         if (wl_noisy_count0 == 3) {
558                                 wl_noisy_count1 = 0;
559                                 wl_noisy_count2 = 0;
560                         }
561
562                 } else {
563                         if (wl_noisy_count1 < 3)
564                                 wl_noisy_count1++;
565
566                         if (wl_noisy_count1 == 3) {
567                                 wl_noisy_count0 = 0;
568                                 wl_noisy_count2 = 0;
569                         }
570                 }
571
572                 if (wl_noisy_count2 == 3)
573                         coex_sta->wl_noisy_level = 2;
574                 else if (wl_noisy_count1 == 3)
575                         coex_sta->wl_noisy_level = 1;
576                 else
577                         coex_sta->wl_noisy_level = 0;
578         }
579 }
580
581 static
582 void halbtc8821c2ant_monitor_bt_enable(struct btc_coexist *btc)
583 {
584         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
585         static u32 bt_disable_cnt;
586         boolean bt_active = TRUE, bt_disabled = FALSE;
587         u16 u16tmp;
588
589         /* Read BT on/off status from scoreboard[1],
590          * enable this only if BT patch support this feature
591          */
592         halbtc8821c2ant_read_scbd(btc, &u16tmp);
593
594         bt_active = u16tmp & BIT(1);
595
596         if (bt_active) {
597                 bt_disable_cnt = 0;
598                 bt_disabled = FALSE;
599                 btc->btc_set(btc, BTC_SET_BL_BT_DISABLE, &bt_disabled);
600         } else {
601                 bt_disable_cnt++;
602                 if (bt_disable_cnt >= 2) {
603                         bt_disabled = TRUE;
604                         bt_disable_cnt = 2;
605                 }
606
607                 btc->btc_set(btc, BTC_SET_BL_BT_DISABLE, &bt_disabled);
608         }
609
610         if (coex_sta->bt_disabled != bt_disabled) {
611                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
612                             "[BTCoex], BT is from %s to %s!!\n",
613                             (coex_sta->bt_disabled ? "disabled" : "enabled"),
614                             (bt_disabled ? "disabled" : "enabled"));
615                 BTC_TRACE(trace_buf);
616                 coex_sta->bt_disabled = bt_disabled;
617
618                 /*for win10 BT disable->enable trigger wifi scan issue   */
619                 if (!coex_sta->bt_disabled) {
620                         coex_sta->is_bt_reenable = TRUE;
621                         coex_sta->cnt_bt_reenable = 15;
622                 } else {
623                         coex_sta->is_bt_reenable = FALSE;
624                         coex_sta->cnt_bt_reenable = 0;
625                 }
626         }
627 }
628
629 static
630 boolean halbtc8821c2ant_moniter_wifibt_status(struct btc_coexist
631                 *btc)
632 {
633         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
634         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
635                                          &btc->wifi_link_info_8821c_2ant;
636         static boolean pre_wifi_busy, pre_under_4way,
637                      pre_bt_off, pre_bt_slave, pre_hid_low_pri_tx_overhead,
638                      pre_wifi_under_lps, pre_bt_setup_link,
639                      pre_cck_lock, pre_cck_lock_warn;
640         static u8 pre_hid_busy_num, pre_wl_noisy_level;
641         boolean wifi_busy = FALSE, under_4way = FALSE;
642         boolean wifi_connected = FALSE;
643         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
644         static u8 cnt_wifi_busytoidle;
645         u32 num_of_wifi_link = 0, wifi_link_mode = 0;
646         static u32 pre_num_of_wifi_link, pre_wifi_link_mode;
647         boolean miracast_plus_bt = FALSE;
648
649         btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
650         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
651         btc->btc_get(btc, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &under_4way);
652
653         if (wifi_busy) {
654                 coex_sta->gl_wifi_busy = TRUE;
655                 cnt_wifi_busytoidle = 6;
656         } else {
657                 if (coex_sta->gl_wifi_busy && cnt_wifi_busytoidle > 0)
658                         cnt_wifi_busytoidle--;
659                 else if (cnt_wifi_busytoidle == 0)
660                         coex_sta->gl_wifi_busy = FALSE;
661         }
662
663         if (coex_sta->bt_disabled != pre_bt_off) {
664                 pre_bt_off = coex_sta->bt_disabled;
665
666                 if (coex_sta->bt_disabled)
667                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
668                                     "[BTCoex], BT is disabled !!\n");
669                 else
670                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
671                                     "[BTCoex], BT is enabled !!\n");
672
673                 BTC_TRACE(trace_buf);
674
675                 coex_sta->bt_coex_supported_feature = 0;
676                 coex_sta->bt_coex_supported_version = 0;
677                 coex_sta->bt_ble_scan_type = 0;
678                 coex_sta->bt_ble_scan_para[0] = 0;
679                 coex_sta->bt_ble_scan_para[1] = 0;
680                 coex_sta->bt_ble_scan_para[2] = 0;
681                 coex_sta->bt_reg_vendor_ac = 0xffff;
682                 coex_sta->bt_reg_vendor_ae = 0xffff;
683                 coex_sta->legacy_forbidden_slot = 0;
684                 coex_sta->le_forbidden_slot = 0;
685                 coex_sta->bt_a2dp_vendor_id = 0;
686                 coex_sta->bt_a2dp_device_name = 0;
687                 return TRUE;
688         }
689
690         num_of_wifi_link = wifi_link_info_ext->num_of_active_port;
691
692         if (num_of_wifi_link != pre_num_of_wifi_link) {
693                 pre_num_of_wifi_link = num_of_wifi_link;
694
695                 if (wifi_link_info_ext->is_p2p_connected) {
696                         if (bt_link_info->bt_link_exist)
697                                 miracast_plus_bt = TRUE;
698                         else
699                                 miracast_plus_bt = FALSE;
700
701                         btc->btc_set(btc, BTC_SET_BL_MIRACAST_PLUS_BT,
702                                      &miracast_plus_bt);
703                 }
704                 return TRUE;
705         }
706
707         wifi_link_mode = btc->wifi_link_info.link_mode;
708         if (wifi_link_mode != pre_wifi_link_mode) {
709                 pre_wifi_link_mode = wifi_link_mode;
710                 return TRUE;
711         }
712
713         if (wifi_connected) {
714                 if (wifi_busy != pre_wifi_busy) {
715                         pre_wifi_busy = wifi_busy;
716                         return TRUE;
717                 }
718                 if (under_4way != pre_under_4way) {
719                         pre_under_4way = under_4way;
720                         return TRUE;
721                 }
722
723                 if (coex_sta->wl_noisy_level != pre_wl_noisy_level) {
724                         pre_wl_noisy_level = coex_sta->wl_noisy_level;
725                         return TRUE;
726                 }
727                 if (coex_sta->under_lps != pre_wifi_under_lps) {
728                         pre_wifi_under_lps = coex_sta->under_lps;
729                         if (coex_sta->under_lps)
730                                 return TRUE;
731                 }
732                 if (coex_sta->cck_lock != pre_cck_lock) {
733                         pre_cck_lock = coex_sta->cck_lock;
734                         return TRUE;
735                 }
736                 if (coex_sta->cck_lock_warn != pre_cck_lock_warn) {
737                         pre_cck_lock_warn = coex_sta->cck_lock_warn;
738                         return TRUE;
739                 }
740         }
741
742         if (!coex_sta->bt_disabled) {
743 #if 0
744                 if (coex_sta->acl_busy != pre_bt_acl_busy) {
745                         pre_bt_acl_busy = coex_sta->acl_busy;
746                         btc->btc_set(btc, BTC_SET_BL_BT_LNA_CONSTRAIN_LEVEL,
747                                      &lna_lvl);
748                         return TRUE;
749                 }
750 #endif
751                 if (coex_sta->hid_busy_num != pre_hid_busy_num) {
752                         pre_hid_busy_num = coex_sta->hid_busy_num;
753                         return TRUE;
754                 }
755
756                 if (bt_link_info->slave_role != pre_bt_slave) {
757                         pre_bt_slave = bt_link_info->slave_role;
758                         return TRUE;
759                 }
760
761                 if (pre_hid_low_pri_tx_overhead !=
762                                 coex_sta->is_hid_low_pri_tx_overhead) {
763                         pre_hid_low_pri_tx_overhead =
764                                 coex_sta->is_hid_low_pri_tx_overhead;
765                         return TRUE;
766                 }
767
768                 if (pre_bt_setup_link != coex_sta->is_setup_link) {
769                         pre_bt_setup_link = coex_sta->is_setup_link;
770                         return TRUE;
771                 }
772         }
773
774         return FALSE;
775 }
776
777 static
778 void halbtc8821c2ant_update_wifi_link_info(struct btc_coexist *btc, u8 reason)
779 {
780         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
781         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
782                                          &btc->wifi_link_info_8821c_2ant;
783         struct btc_wifi_link_info wifi_link_info;
784         u8 wifi_central_chnl = 0, num_of_wifi_link = 0;
785         boolean isunder5G = FALSE, ismcc25g = FALSE, isp2pconnected = FALSE;
786         u32 wifi_link_status = 0;
787
788         btc->btc_get(btc, BTC_GET_U4_WIFI_LINK_STATUS, &wifi_link_status);
789         wifi_link_info_ext->port_connect_status = wifi_link_status & 0xffff;
790
791         btc->btc_get(btc, BTC_GET_BL_WIFI_LINK_INFO, &wifi_link_info);
792         btc->wifi_link_info = wifi_link_info;
793
794         btc->btc_get(btc, BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifi_central_chnl);
795         coex_sta->wl_center_channel = wifi_central_chnl;
796
797         /* Check scan/connect/special-pkt action first  */
798         switch (reason) {
799         case BT_8821C_2ANT_RSN_5GSCANSTART:
800         case BT_8821C_2ANT_RSN_5GSWITCHBAND:
801         case BT_8821C_2ANT_RSN_5GCONSTART:
802                 isunder5G = TRUE;
803                 break;
804         case BT_8821C_2ANT_RSN_2GSCANSTART:
805         case BT_8821C_2ANT_RSN_2GSWITCHBAND:
806         case BT_8821C_2ANT_RSN_2GCONSTART:
807                 isunder5G = FALSE;
808                 break;
809         case BT_8821C_2ANT_RSN_2GCONFINISH:
810         case BT_8821C_2ANT_RSN_5GCONFINISH:
811         case BT_8821C_2ANT_RSN_2GMEDIA:
812         case BT_8821C_2ANT_RSN_5GMEDIA:
813         case BT_8821C_2ANT_RSN_BTINFO:
814         case BT_8821C_2ANT_RSN_PERIODICAL:
815         case BT_8821C_2ANT_RSN_2GSPECIALPKT:
816         case BT_8821C_2ANT_RSN_5GSPECIALPKT:
817         default:
818                 switch (wifi_link_info.link_mode) {
819                 case BTC_LINK_5G_MCC_GO_STA:
820                 case BTC_LINK_5G_MCC_GC_STA:
821                 case BTC_LINK_5G_SCC_GO_STA:
822                 case BTC_LINK_5G_SCC_GC_STA:
823                         isunder5G = TRUE;
824                         break;
825                 case BTC_LINK_2G_MCC_GO_STA:
826                 case BTC_LINK_2G_MCC_GC_STA:
827                 case BTC_LINK_2G_SCC_GO_STA:
828                 case BTC_LINK_2G_SCC_GC_STA:
829                         isunder5G = FALSE;
830                         break;
831                 case BTC_LINK_25G_MCC_GO_STA:
832                 case BTC_LINK_25G_MCC_GC_STA:
833                         isunder5G = FALSE;
834                         ismcc25g = TRUE;
835                         break;
836                 case BTC_LINK_ONLY_STA:
837                         if (wifi_link_info.sta_center_channel > 14)
838                                 isunder5G = TRUE;
839                         else
840                                 isunder5G = FALSE;
841                         break;
842                 case BTC_LINK_ONLY_GO:
843                 case BTC_LINK_ONLY_GC:
844                 case BTC_LINK_ONLY_AP:
845                 default:
846                         if (wifi_link_info.p2p_center_channel > 14)
847                                 isunder5G = TRUE;
848                         else
849                                 isunder5G = FALSE;
850
851                         break;
852                 }
853                 break;
854         }
855
856         wifi_link_info_ext->is_all_under_5g = isunder5G;
857         wifi_link_info_ext->is_mcc_25g = ismcc25g;
858
859         if (wifi_link_status & WIFI_STA_CONNECTED)
860                 num_of_wifi_link++;
861
862         if (wifi_link_status & WIFI_AP_CONNECTED)
863                 num_of_wifi_link++;
864
865         if (wifi_link_status & WIFI_P2P_GO_CONNECTED) {
866                 num_of_wifi_link++;
867                 isp2pconnected = TRUE;
868         }
869
870         if (wifi_link_status & WIFI_P2P_GC_CONNECTED) {
871                 num_of_wifi_link++;
872                 isp2pconnected = TRUE;
873         }
874
875         wifi_link_info_ext->num_of_active_port = num_of_wifi_link;
876         wifi_link_info_ext->is_p2p_connected = isp2pconnected;
877
878         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
879                     "[BTCoex], wifi_link_info: link_mode=%d, STA_Ch=%d, P2P_Ch=%d, AnyClient_Join_Go=%d !\n",
880                     btc->wifi_link_info.link_mode,
881                     btc->wifi_link_info.sta_center_channel,
882                     btc->wifi_link_info.p2p_center_channel,
883                     btc->wifi_link_info.bany_client_join_go);
884         BTC_TRACE(trace_buf);
885
886         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
887                     "[BTCoex], wifi_link_info: center_ch=%d, is_all_under_5g=%d, is_mcc_25g=%d!\n",
888                     coex_sta->wl_center_channel,
889                     wifi_link_info_ext->is_all_under_5g,
890                     wifi_link_info_ext->is_mcc_25g);
891         BTC_TRACE(trace_buf);
892
893         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
894                     "[BTCoex], wifi_link_info: port_connect_status=0x%x, active_port_cnt=%d, P2P_Connect=%d!\n",
895                     wifi_link_info_ext->port_connect_status,
896                     wifi_link_info_ext->num_of_active_port,
897                     wifi_link_info_ext->is_p2p_connected);
898         BTC_TRACE(trace_buf);
899
900         switch (reason) {
901         case BT_8821C_2ANT_RSN_2GSCANSTART:
902                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
903                             "[BTCoex], Update reason = %s\n", "2GSCANSTART");
904                 break;
905         case BT_8821C_2ANT_RSN_5GSCANSTART:
906                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
907                             "[BTCoex], Update reason = %s\n", "5GSCANSTART");
908                 break;
909         case BT_8821C_2ANT_RSN_SCANFINISH:
910                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
911                             "[BTCoex], Update reason = %s\n", "SCANFINISH");
912                 break;
913         case BT_8821C_2ANT_RSN_2GSWITCHBAND:
914                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
915                             "[BTCoex], Update reason = %s\n", "2GSWITCHBAND");
916                 break;
917         case BT_8821C_2ANT_RSN_5GSWITCHBAND:
918                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
919                             "[BTCoex], Update reason = %s\n", "5GSWITCHBAND");
920                 break;
921         case BT_8821C_2ANT_RSN_2GCONSTART:
922                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
923                             "[BTCoex], Update reason = %s\n", "2GCONNECTSTART");
924                 break;
925         case BT_8821C_2ANT_RSN_5GCONSTART:
926                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
927                             "[BTCoex], Update reason = %s\n", "5GCONNECTSTART");
928                 break;
929         case BT_8821C_2ANT_RSN_2GCONFINISH:
930                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
931                             "[BTCoex], Update reason = %s\n",
932                             "2GCONNECTFINISH");
933                 break;
934         case BT_8821C_2ANT_RSN_5GCONFINISH:
935                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
936                             "[BTCoex], Update reason = %s\n",
937                             "5GCONNECTFINISH");
938                 break;
939         case BT_8821C_2ANT_RSN_2GMEDIA:
940                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
941                             "[BTCoex], Update reason = %s\n", "2GMEDIASTATUS");
942                 break;
943         case BT_8821C_2ANT_RSN_5GMEDIA:
944                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
945                             "[BTCoex], Update reason = %s\n", "5GMEDIASTATUS");
946                 break;
947         case BT_8821C_2ANT_RSN_MEDIADISCON:
948                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
949                             "[BTCoex], Update reason = %s\n",
950                             "MEDIADISCONNECT");
951                 break;
952         case BT_8821C_2ANT_RSN_2GSPECIALPKT:
953                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
954                             "[BTCoex], Update reason = %s\n", "2GSPECIALPKT");
955                 break;
956         case BT_8821C_2ANT_RSN_5GSPECIALPKT:
957                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
958                             "[BTCoex], Update reason = %s\n", "5GSPECIALPKT");
959                 break;
960         case BT_8821C_2ANT_RSN_BTINFO:
961                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
962                             "[BTCoex], Update reason = %s\n", "BTINFO");
963                 break;
964         case BT_8821C_2ANT_RSN_PERIODICAL:
965                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
966                             "[BTCoex], Update reason = %s\n", "PERIODICAL");
967                 break;
968         case BT_8821C_2ANT_RSN_PNP:
969                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
970                             "[BTCoex], Update reason = %s\n", "PNPNotify");
971                 break;
972         default:
973                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
974                             "[BTCoex], Update reason = %s\n", "UNKNOWN");
975                 break;
976         }
977
978         BTC_TRACE(trace_buf);
979
980         if (wifi_link_info_ext->is_all_under_5g ||
981             coex_sta->num_of_profile == 0)
982                 halbtc8821c2ant_low_penalty_ra(btc, NM_EXCU, FALSE, 0);
983         else if (wifi_link_info_ext->is_p2p_connected)
984                 halbtc8821c2ant_low_penalty_ra(btc, NM_EXCU, TRUE, 30);
985         else
986                 halbtc8821c2ant_low_penalty_ra(btc, NM_EXCU, TRUE, 15);
987 }
988
989 static
990 void halbtc8821c2ant_update_bt_link_info(struct btc_coexist *btc)
991 {
992         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
993         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
994         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
995         boolean bt_busy = FALSE, increase_scan_dev_num = FALSE;
996         u32 val = 0;
997         static u8 pre_num_of_profile, cur_num_of_profile, cnt, ble_cnt;
998
999         if (++ble_cnt >= 3)
1000                 ble_cnt = 0;
1001
1002         if (coex_sta->is_ble_scan_en && ble_cnt == 0) {
1003                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1004                             "[BTCoex], BT ext info bit4 check, query BLE Scan type!!\n");
1005                 BTC_TRACE(trace_buf);
1006                 coex_sta->bt_ble_scan_type =
1007                         btc->btc_get_ble_scan_type_from_bt(btc);
1008
1009                 if ((coex_sta->bt_ble_scan_type & 0x1) == 0x1)
1010                         coex_sta->bt_ble_scan_para[0]  =
1011                                 btc->btc_get_ble_scan_para_from_bt(btc, 0x1);
1012                 if ((coex_sta->bt_ble_scan_type & 0x2) == 0x2)
1013                         coex_sta->bt_ble_scan_para[1]  =
1014                                 btc->btc_get_ble_scan_para_from_bt(btc, 0x2);
1015                 if ((coex_sta->bt_ble_scan_type & 0x4) == 0x4)
1016                         coex_sta->bt_ble_scan_para[2]  =
1017                                 btc->btc_get_ble_scan_para_from_bt(btc, 0x4);
1018         }
1019
1020         coex_sta->num_of_profile = 0;
1021
1022         /* set link exist status */
1023         if (!(coex_sta->bt_info & BT_INFO_8821C_2ANT_B_CONNECTION)) {
1024                 coex_sta->bt_link_exist = FALSE;
1025                 coex_sta->pan_exist = FALSE;
1026                 coex_sta->a2dp_exist = FALSE;
1027                 coex_sta->hid_exist = FALSE;
1028                 coex_sta->sco_exist = FALSE;
1029                 coex_sta->msft_mr_exist = FALSE;
1030         } else {        /* connection exists */
1031                 coex_sta->bt_link_exist = TRUE;
1032                 if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_FTP) {
1033                         coex_sta->pan_exist = TRUE;
1034                         coex_sta->num_of_profile++;
1035                 } else {
1036                         coex_sta->pan_exist = FALSE;
1037                 }
1038
1039                 if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_A2DP) {
1040                         coex_sta->a2dp_exist = TRUE;
1041                         coex_sta->num_of_profile++;
1042                 } else {
1043                         coex_sta->a2dp_exist = FALSE;
1044                 }
1045
1046                 if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_HID) {
1047                         coex_sta->hid_exist = TRUE;
1048                         coex_sta->num_of_profile++;
1049                 } else {
1050                         coex_sta->hid_exist = FALSE;
1051                 }
1052
1053                 if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_SCO_ESCO) {
1054                         coex_sta->sco_exist = TRUE;
1055                         coex_sta->num_of_profile++;
1056                 } else {
1057                         coex_sta->sco_exist = FALSE;
1058                 }
1059
1060                 if (coex_sta->hid_busy_num == 0 &&
1061                     coex_sta->hid_pair_cnt > 0 &&
1062                     coex_sta->low_priority_tx > 1000 &&
1063                     coex_sta->low_priority_rx > 1000 &&
1064                     !coex_sta->c2h_bt_inquiry_page)
1065                         coex_sta->msft_mr_exist = true;
1066                 else
1067                         coex_sta->msft_mr_exist = false;
1068         }
1069
1070         bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
1071         bt_link_info->sco_exist = coex_sta->sco_exist;
1072         bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
1073         bt_link_info->pan_exist = coex_sta->pan_exist;
1074         bt_link_info->hid_exist = coex_sta->hid_exist;
1075         bt_link_info->acl_busy = coex_sta->acl_busy;
1076
1077         /* check if Sco only */
1078         if (bt_link_info->sco_exist &&
1079             !bt_link_info->a2dp_exist &&
1080             !bt_link_info->pan_exist &&
1081             !bt_link_info->hid_exist)
1082                 bt_link_info->sco_only = TRUE;
1083         else
1084                 bt_link_info->sco_only = FALSE;
1085
1086         /* check if A2dp only */
1087         if (!bt_link_info->sco_exist &&
1088             bt_link_info->a2dp_exist &&
1089             !bt_link_info->pan_exist &&
1090             !bt_link_info->hid_exist)
1091                 bt_link_info->a2dp_only = TRUE;
1092         else
1093                 bt_link_info->a2dp_only = FALSE;
1094
1095         /* check if Pan only */
1096         if (!bt_link_info->sco_exist &&
1097             !bt_link_info->a2dp_exist &&
1098             bt_link_info->pan_exist &&
1099             !bt_link_info->hid_exist)
1100                 bt_link_info->pan_only = TRUE;
1101         else
1102                 bt_link_info->pan_only = FALSE;
1103
1104         /* check if Hid only */
1105         if (!bt_link_info->sco_exist &&
1106             !bt_link_info->a2dp_exist &&
1107             !bt_link_info->pan_exist &&
1108             bt_link_info->hid_exist)
1109                 bt_link_info->hid_only = TRUE;
1110         else
1111                 bt_link_info->hid_only = FALSE;
1112
1113         if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_INQ_PAGE) {
1114                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_INQ_PAGE;
1115                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1116                             "[BTCoex], BtInfoNotify(), BT Inq/page!!!\n");
1117         } else if (!(coex_sta->bt_info & BT_INFO_8821C_2ANT_B_CONNECTION)) {
1118                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_NCON_IDLE;
1119                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1120                             "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
1121         } else if (coex_sta->bt_info == BT_INFO_8821C_2ANT_B_CONNECTION) {
1122                 /* connection exists but no busy */
1123                 if (coex_sta->msft_mr_exist) {
1124                         coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_ACL_BUSY;
1125                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1126                                     "[BTCoex], BtInfoNotify(),  BT ACL busy!!\n");
1127                 } else {
1128                         coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_CON_IDLE;
1129                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1130                                     "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
1131                 }
1132         } else if (((coex_sta->bt_info & BT_INFO_8821C_2ANT_B_SCO_ESCO) ||
1133                     (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_SCO_BUSY)) &&
1134                    (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_ACL_BUSY)) {
1135                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_ACL_SCO_BUSY;
1136                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1137                             "[BTCoex], BtInfoNotify(), BT ACL SCO busy!!!\n");
1138         } else if ((coex_sta->bt_info & BT_INFO_8821C_2ANT_B_SCO_ESCO) ||
1139                    (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_SCO_BUSY)) {
1140                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_SCO_BUSY;
1141                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1142                             "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
1143         } else if (coex_sta->bt_info & BT_INFO_8821C_2ANT_B_ACL_BUSY) {
1144                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_ACL_BUSY;
1145                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1146                             "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
1147         } else {
1148                 coex_dm->bt_status = BT_8821C_2ANT_BSTATUS_MAX;
1149                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1150                             "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
1151         }
1152
1153         BTC_TRACE(trace_buf);
1154
1155         if (coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_ACL_BUSY ||
1156             coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_SCO_BUSY ||
1157             coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_ACL_SCO_BUSY) {
1158                 bt_busy = TRUE;
1159                 increase_scan_dev_num = TRUE;
1160         } else {
1161                 bt_busy = FALSE;
1162                 increase_scan_dev_num = FALSE;
1163         }
1164
1165         btc->btc_set(btc, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1166         btc->btc_set(btc, BTC_SET_BL_INC_SCAN_DEV_NUM, &increase_scan_dev_num);
1167
1168         cur_num_of_profile = coex_sta->num_of_profile;
1169
1170         if (cur_num_of_profile != pre_num_of_profile)
1171                 cnt = 2;
1172
1173         if (btc->board_info.customer_id == RT_CID_LENOVO_CHINA) {
1174                 if (cur_num_of_profile > 0)
1175                         halbtc8821c2ant_set_antdiv_hwsw(btc, NM_EXCU, TRUE);
1176                 else
1177                         halbtc8821c2ant_set_antdiv_hwsw(btc, NM_EXCU, FALSE);
1178         }
1179
1180         if (bt_link_info->a2dp_exist && (cnt > 0)) {
1181                 cnt--;
1182                 if (coex_sta->bt_a2dp_vendor_id == 0 &&
1183                     coex_sta->bt_a2dp_device_name == 0) {
1184                         btc->btc_get(btc, BTC_GET_U4_BT_DEVICE_INFO, &val);
1185
1186                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1187                                     "[BTCoex], BtInfoNotify(), get BT DEVICE_INFO = %x\n",
1188                                     val);
1189                         BTC_TRACE(trace_buf);
1190
1191                         coex_sta->bt_a2dp_vendor_id = (u8)(val & 0xff);
1192                         coex_sta->bt_a2dp_device_name = (val & 0xffffff00) >> 8;
1193                 }
1194
1195                 if (coex_sta->legacy_forbidden_slot == 0 &&
1196                     coex_sta->le_forbidden_slot == 0) {
1197                         btc->btc_get(btc, BTC_GET_U4_BT_FORBIDDEN_SLOT_VAL,
1198                                      &val);
1199
1200                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1201                                     "[BTCoex], BtInfoNotify(), get BT FORBIDDEN_SLOT_VAL = %x\n",
1202                                     val);
1203                         BTC_TRACE(trace_buf);
1204
1205                         coex_sta->legacy_forbidden_slot = (u16)(val & 0xffff);
1206                         coex_sta->le_forbidden_slot =
1207                                                 (u16)((val & 0xffff0000) >> 16);
1208                 }
1209         }
1210
1211         pre_num_of_profile = coex_sta->num_of_profile;
1212 }
1213
1214 static
1215 void halbtc8821c2ant_update_wifi_ch_info(struct btc_coexist *btc, u8 type)
1216 {
1217         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
1218         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1219         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
1220                                          &btc->wifi_link_info_8821c_2ant;
1221         u8 h2c_parameter[3] = {0}, i;
1222         u32 wifi_bw;
1223         u8 wifi_central_chnl = 0;
1224         u8 wifi_5g_chnl[19] = {120, 124, 128, 132, 136, 140, 144, 149, 153, 157,
1225                                118, 126, 134, 142, 151, 159, 122, 138, 155};
1226         u8 bt_skip_cneter_chanl[19] = {2, 8, 17, 26, 34, 42, 51, 62, 71, 77, 2,
1227                                        12, 29, 46, 66, 76, 10, 37, 68};
1228         u8 bt_skip_span[19] = {4, 8, 8, 10, 8, 10, 8, 8, 10, 4, 4, 16, 16, 16,
1229                                16, 4, 20, 34, 20};
1230         boolean is_any_connected = FALSE;
1231
1232         if (btc->manual_control)
1233                 return;
1234
1235         btc->btc_get(btc, BTC_GET_U4_WIFI_BW, &wifi_bw);
1236
1237         if (btc->stop_coex_dm || coex_sta->is_rf_state_off) {
1238                 is_any_connected = FALSE;
1239                 wifi_central_chnl = 0;
1240         } else if (type != BTC_MEDIA_DISCONNECT ||
1241                   (type == BTC_MEDIA_DISCONNECT &&
1242                    wifi_link_info_ext->num_of_active_port > 0)) {
1243                 if (wifi_link_info_ext->num_of_active_port == 1) {
1244                         if (wifi_link_info_ext->is_p2p_connected)
1245                                 wifi_central_chnl =
1246                                         btc->wifi_link_info
1247                                                 .p2p_center_channel;
1248                         else
1249                                 wifi_central_chnl =
1250                                         btc->wifi_link_info
1251                                                 .sta_center_channel;
1252                 } else { /* port > 2 */
1253                         if ((btc->wifi_link_info
1254                                      .p2p_center_channel > 14) &&
1255                             (btc->wifi_link_info
1256                                      .sta_center_channel > 14))
1257                                 wifi_central_chnl =
1258                                         btc->wifi_link_info
1259                                                 .p2p_center_channel;
1260                         else if (btc->wifi_link_info
1261                                          .p2p_center_channel <= 14)
1262                                 wifi_central_chnl =
1263                                         btc->wifi_link_info
1264                                                 .p2p_center_channel;
1265                         else if (btc->wifi_link_info
1266                                          .sta_center_channel <= 14)
1267                                 wifi_central_chnl =
1268                                         btc->wifi_link_info
1269                                                 .sta_center_channel;
1270                 }
1271         }
1272
1273         if (wifi_central_chnl > 0)
1274                 is_any_connected = TRUE;
1275
1276         if (is_any_connected) {
1277                 if (wifi_central_chnl <= 14) {
1278                         h2c_parameter[0] = 0x1;
1279                         h2c_parameter[1] = wifi_central_chnl;
1280
1281                         if (wifi_bw == BTC_WIFI_BW_HT40)
1282                                 h2c_parameter[2] = 0x36;
1283                         else
1284                                 h2c_parameter[2] = 0x24;
1285                 } else {  /* for 5G  */
1286                         for (i = 0; i <= 18; i++) {
1287                                 if (wifi_central_chnl == wifi_5g_chnl[i])
1288                                         break;
1289                         }
1290
1291                         if (i <= 18) {
1292                                 h2c_parameter[0] = 0x3;
1293                                 h2c_parameter[1] = bt_skip_cneter_chanl[i];
1294                                 h2c_parameter[2] = bt_skip_span[i];
1295                         }
1296                 }
1297         }
1298
1299         coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
1300         coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
1301         coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
1302
1303         btc->btc_fill_h2c(btc, 0x66, 3, h2c_parameter);
1304
1305         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1306                     "[BTCoex], para[0:2] = 0x%x 0x%x 0x%x\n",
1307                     h2c_parameter[0], h2c_parameter[1], h2c_parameter[2]);
1308         BTC_TRACE(trace_buf);
1309 }
1310
1311 static
1312 void halbtc8821c2ant_set_wl_tx_power(struct btc_coexist *btc,
1313                                      boolean force_exec,  u8 wl_pwr_lvl)
1314 {
1315         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1316
1317         if (!force_exec) {
1318                 if (wl_pwr_lvl == coex_dm->cur_wl_pwr_lvl)
1319                         return;
1320         }
1321
1322         /* btc->btc_write_1byte_bitmask(btc, 0xc5b, 0xff, wl_pwr_lvl); */
1323         coex_dm->cur_wl_pwr_lvl = wl_pwr_lvl;
1324 }
1325
1326 static
1327 void halbtc8821c2ant_set_bt_tx_power(struct btc_coexist *btc,
1328                                      boolean force_exec,  u8 bt_pwr_lvl)
1329 {
1330         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1331         u8 h2c_parameter[1] = {0};
1332
1333         if (!force_exec) {
1334                 if (bt_pwr_lvl == coex_dm->cur_bt_pwr_lvl)
1335                         return;
1336         }
1337
1338         h2c_parameter[0] = 0 - bt_pwr_lvl;
1339         btc->btc_fill_h2c(btc, 0x62, 1, h2c_parameter);
1340
1341         coex_dm->cur_bt_pwr_lvl = bt_pwr_lvl;
1342 }
1343
1344 #if 0
1345 static
1346 void halbtc8821c2ant_set_wl_rx_gain(struct btc_coexist *btc,
1347                                     boolean force_exec,  boolean agc_table_en)
1348 {
1349         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1350
1351         u32 rx_gain_value_enable[] = {0xff000003,
1352         0xbd120003, 0xbe100003, 0xbf080003, 0xbf060003, 0xbf050003,
1353         0xbc140003, 0xbb160003, 0xba180003, 0xb91a0003, 0xb81c0003,
1354         0xb71e0003, 0xb4200003, 0xb5220003, 0xb4240003, 0xb3260003,
1355         0xb2280003, 0xb12a0003, 0xb02c0003, 0xaf2e0003, 0xae300003,
1356         0xad320003, 0xac340003, 0xab360003, 0x8d380003, 0x8c3a0003,
1357         0x8b3c0003, 0x8a3e0003, 0x6e400003, 0x6d420003, 0x6c440003,
1358         0x6b460003, 0x6a480003, 0x694a0003, 0x684c0003, 0x674e0003,
1359         0x66500003, 0x65520003, 0x64540003, 0x64560003, 0x007e0403};
1360
1361         u32 rx_gain_value_disable[] = {0xff000003,
1362         0xf4120003, 0xf5100003, 0xf60e0003, 0xf70c0003, 0xf80a0003,
1363         0xf3140003, 0xf2160003, 0xf1180003, 0xf01a0003, 0xef1c0003,
1364         0xee1e0003, 0xed200003, 0xec220003, 0xeb240003, 0xea260003,
1365         0xe9280003, 0xe82a0003, 0xe72c0003, 0xe62e0003, 0xe5300003,
1366         0xc8320003, 0xc7340003, 0xc6360003, 0xc5380003, 0xc43a0003,
1367         0xc33c0003, 0xc23e0003, 0xc1400003, 0xc0420003, 0xa5440003,
1368         0xa4460003, 0xa3480003, 0xa24a0003, 0xa14c0003, 0x834e0003,
1369         0x82500003, 0x81520003, 0x80540003, 0x65560003, 0x007e0403};
1370
1371         u8 i;
1372
1373         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1374                     "[BTCoex], *************wl rx gain*************\n");
1375         BTC_TRACE(trace_buf);
1376
1377         if (!force_exec) {
1378                 if (agc_table_en == coex_dm->cur_agc_table_en)
1379                         return;
1380         }
1381
1382                 if (agc_table_en) {
1383                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1384                                     "[BTCoex], BB Agc Table On!\n");
1385                         BTC_TRACE(trace_buf);
1386
1387                         for (i = 0; i <= 100; i++) {
1388                                 btc->btc_write_4byte(btc,
1389                                         0x81c, rx_gain_value_enable[i]);
1390
1391                                 if (rx_gain_value_enable[i] == 0x007e0403)
1392                                         break;
1393                         }
1394
1395                         /* set Rx filter corner RCK offset */
1396                         btc->btc_set_rf_reg(btc, BTC_RF_A, 0xde, 0x2, 0x1);
1397                         btc->btc_set_rf_reg(btc, BTC_RF_A, 0x1d, 0x3f, 0x3f);
1398
1399                         /* ADC clock 80M
1400                          * btc->btc_write_1byte_bitmask(btc, 0x8ad, 0x3, 0x3);
1401                          */
1402
1403                 } else {
1404                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1405                                     "[BTCoex], BB Agc Table Off!\n");
1406                         BTC_TRACE(trace_buf);
1407
1408                         for (i = 0; i <= 100; i++) {
1409                                 btc->btc_write_4byte(btc,
1410                                         0x81c, rx_gain_value_disable[i]);
1411
1412                                 if (rx_gain_value_disable[i] == 0x007e0403)
1413                                         break;
1414                         }
1415
1416                         /* set Rx filter corner RCK offset */
1417                         btc->btc_set_rf_reg(btc, BTC_RF_A, 0x1d, 0x3f, 0x4);
1418                         btc->btc_set_rf_reg(btc, BTC_RF_A, 0xde, 0x2, 0x0);
1419
1420                         /* ADC clock 160M
1421                          * btc->btc_write_1byte_bitmask(btc, 0x8ad, 0x3, 0x0);
1422                          */
1423                 }
1424
1425         coex_dm->cur_agc_table_en = agc_table_en;
1426 }
1427
1428 static
1429 void halbtc8821c2ant_set_bt_rx_gain(struct btc_coexist *btc,
1430                                     boolean force_exec,  boolean rx_gain_en)
1431 {
1432         u8 lna_constrain_level = 0;
1433
1434         /* use scoreboard[4] to notify BT Rx gain table change   */
1435         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_RXGAIN,
1436                                    rx_gain_en);
1437
1438         if (rx_gain_en)
1439                 lna_constrain_level = 1;
1440         else
1441                 lna_constrain_level = 7;
1442
1443         btc->btc_set(btc, BTC_SET_BL_BT_LNA_CONSTRAIN_LEVEL,
1444                      &lna_constrain_level);
1445 }
1446 #endif
1447
1448 static
1449 void halbtc8821c2ant_bt_auto_report(struct btc_coexist *btc,
1450                                     boolean force_exec,
1451                                     boolean enable_auto_report)
1452 {
1453         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1454         u8                      h2c_parameter[1] = {0};
1455
1456         if (!force_exec) {
1457                 if (enable_auto_report == coex_dm->cur_bt_auto_report)
1458                         return;
1459         }
1460
1461         if (enable_auto_report)
1462                 h2c_parameter[0] |= BIT(0);
1463
1464         btc->btc_fill_h2c(btc, 0x68, 1, h2c_parameter);
1465
1466         coex_dm->cur_bt_auto_report = enable_auto_report;
1467 }
1468
1469 static
1470 u32 halbtc8821c2ant_read_indirect_reg(struct btc_coexist *btc, u16 reg_addr)
1471 {
1472         u32 j = 0, delay_count = 0;
1473
1474         while (1) {
1475                 if ((btc->btc_read_1byte(btc, 0x1703) & BIT(5)) == 0) {
1476                         delay_ms(10);
1477                         delay_count++;
1478                         if (delay_count >= 10) {
1479                                 delay_count = 0;
1480                                 break;
1481                         }
1482                 } else
1483                         break;
1484         }
1485
1486         /* wait for ready bit before access 0x1700 */
1487         btc->btc_write_4byte(btc, 0x1700, 0x800F0000 | reg_addr);
1488
1489         return btc->btc_read_4byte(btc, 0x1708);  /* get read data */
1490 }
1491
1492 static
1493 void halbtc8821c2ant_write_indirect_reg(struct btc_coexist *btc, u16 reg_addr,
1494                                         u32 bit_mask, u32 reg_value)
1495 {
1496         u32 val, i = 0, j = 0, bitpos = 0, delay_count = 0;
1497
1498         if (bit_mask == 0x0)
1499                 return;
1500         if (bit_mask == 0xffffffff) {
1501                 /* wait for ready bit before access 0x1700/0x1704 */
1502                 while (1) {
1503                         if ((btc->btc_read_1byte(btc, 0x1703) & BIT(5)) == 0) {
1504                                 delay_ms(10);
1505                                 delay_count++;
1506                                 if (delay_count >= 10) {
1507                                         delay_count = 0;
1508                                         break;
1509                                 }
1510                         } else
1511                                 break;
1512                 }
1513
1514                 /* put write data */
1515                 btc->btc_write_4byte(btc, 0x1704, reg_value);
1516                 btc->btc_write_4byte(btc, 0x1700, 0xc00F0000 | reg_addr);
1517         } else {
1518                 for (i = 0; i <= 31; i++) {
1519                         if (((bit_mask >> i) & 0x1) == 0x1) {
1520                                 bitpos = i;
1521                                 break;
1522                         }
1523                 }
1524
1525                 /* read back register value before write */
1526                 val = halbtc8821c2ant_read_indirect_reg(btc, reg_addr);
1527                 val = (val & (~bit_mask)) | (reg_value << bitpos);
1528
1529                 /* wait for ready bit before access 0x1700/0x1704 */
1530                 while (1) {
1531                         if ((btc->btc_read_1byte(btc, 0x1703) & BIT(5)) == 0) {
1532                                 delay_ms(10);
1533                                 delay_count++;
1534                                 if (delay_count >= 10) {
1535                                         delay_count = 0;
1536                                         break;
1537                                 }
1538                         } else
1539                                 break;
1540                 }
1541
1542                 /* put write data */
1543                 btc->btc_write_4byte(btc, 0x1704, val);
1544                 btc->btc_write_4byte(btc, 0x1700, 0xc00F0000 | reg_addr);
1545         }
1546 }
1547
1548 static
1549 void halbtc8821c2ant_ltecoex_enable(struct btc_coexist *btc, boolean enable)
1550 {
1551         u8 val;
1552
1553         /* 0x38[7] */
1554         val = (enable) ? 1 : 0;
1555         halbtc8821c2ant_write_indirect_reg(btc, 0x38, 0x80, val);
1556 }
1557
1558 static
1559 void halbtc8821c2ant_ltecoex_table(struct btc_coexist *btc, u8 table_type,
1560                                    u16 table_content)
1561 {
1562         u16 reg_addr = 0x0000;
1563
1564         switch (table_type) {
1565         case BT_8821C_2ANT_CTT_WL_VS_LTE:
1566                 reg_addr = 0xa0;
1567                 break;
1568         case BT_8821C_2ANT_CTT_BT_VS_LTE:
1569                 reg_addr = 0xa4;
1570                 break;
1571         }
1572
1573          /* 0xa0[15:0] or 0xa4[15:0] */
1574         if (reg_addr != 0x0000)
1575                 halbtc8821c2ant_write_indirect_reg(btc, reg_addr, 0xffff,
1576                                                    table_content);
1577 }
1578
1579 static
1580 void halbtc8821c2ant_coex_ctrl_owner(struct btc_coexist *btc,
1581                                      boolean wifi_control)
1582 {
1583         u8 val;
1584
1585         /* 0x70[26] */
1586         val = (wifi_control) ? 1 : 0;
1587         btc->btc_write_1byte_bitmask(btc, 0x73, 0x4, val);
1588 }
1589
1590 static
1591 void halbtc8821c2ant_set_gnt_bt(struct btc_coexist *btc, u8 control_block,
1592                                 boolean sw_control, u8 state)
1593 {
1594         u32 val = 0, val_orig = 0;
1595
1596         if (!sw_control)
1597                 val = 0x0;
1598         else if (state & 0x1)
1599                 val = 0x3;
1600         else
1601                 val = 0x1;
1602
1603         val_orig = halbtc8821c2ant_read_indirect_reg(btc, 0x38);
1604
1605         switch (control_block) {
1606         case BT_8821C_2ANT_GNT_BLOCK_RFC_BB:
1607         default:
1608                 val = ((val << 14) | (val << 10)) | (val_orig & 0xffff33ff);
1609                 break;
1610         case BT_8821C_2ANT_GNT_BLOCK_RFC:
1611                 val = (val << 14) | (val_orig & 0xffff3fff);
1612                 break;
1613         case BT_8821C_2ANT_GNT_BLOCK_BB:
1614                 val = (val << 10) | (val_orig & 0xfffff3ff);
1615                 break;
1616         }
1617
1618         halbtc8821c2ant_write_indirect_reg(btc, 0x38, 0xffffffff, val);
1619 }
1620
1621 static
1622 void halbtc8821c2ant_set_gnt_wl(struct btc_coexist *btc, u8 control_block,
1623                                 boolean sw_control, u8 state)
1624 {
1625         u32 val = 0, val_orig = 0;
1626
1627         if (!sw_control)
1628                 val = 0x0;
1629         else if (state & 0x1)
1630                 val = 0x3;
1631         else
1632                 val = 0x1;
1633
1634         val_orig = halbtc8821c2ant_read_indirect_reg(btc, 0x38);
1635
1636         switch (control_block) {
1637         case BT_8821C_2ANT_GNT_BLOCK_RFC_BB:
1638         default:
1639                 val = ((val << 12) | (val << 8)) | (val_orig & 0xffffccff);
1640                 break;
1641         case BT_8821C_2ANT_GNT_BLOCK_RFC:
1642                 val = (val << 12) | (val_orig & 0xffffcfff);
1643                 break;
1644         case BT_8821C_2ANT_GNT_BLOCK_BB:
1645                 val = (val << 8) | (val_orig & 0xfffffcff);
1646                 break;
1647         }
1648
1649         halbtc8821c2ant_write_indirect_reg(btc, 0x38, 0xffffffff, val);
1650 }
1651
1652 #if 0
1653 static
1654 void halbtc8821c2ant_ltecoex_set_break_table(struct btc_coexist *btc,
1655                                              u8 table_type, u8 table_content)
1656 {
1657         u16 reg_addr = 0x0000;
1658
1659         switch (table_type) {
1660         case BT_8821C_2ANT_LBTT_WL_BREAK_LTE:
1661                 reg_addr = 0xa8;
1662                 break;
1663         case BT_8821C_2ANT_LBTT_BT_BREAK_LTE:
1664                 reg_addr = 0xac;
1665                 break;
1666         case BT_8821C_2ANT_LBTT_LTE_BREAK_WL:
1667                 reg_addr = 0xb0;
1668                 break;
1669         case BT_8821C_2ANT_LBTT_LTE_BREAK_BT:
1670                 reg_addr = 0xb4;
1671                 break;
1672         }
1673
1674         /* 0xa8[15:0] or 0xb4[15:0] */
1675         if (reg_addr != 0x0000)
1676                 halbtc8821c2ant_write_indirect_reg(btc, reg_addr, 0xff,
1677                                                    table_content);
1678 }
1679 #endif
1680
1681 #if 0
1682 static
1683 void halbtc8821c2ant_set_wltoggle_coex_table(struct btc_coexist *btc,
1684                                              boolean force_exec, u8 interval,
1685                                              u8 val0x6c4_b0, u8 val0x6c4_b1,
1686                                              u8 val0x6c4_b2, u8 val0x6c4_b3)
1687 {
1688         static u8 pre_h2c_parameter[6] = {0};
1689         u8      cur_h2c_parameter[6] = {0};
1690         u8 i, match_cnt = 0;
1691
1692         cur_h2c_parameter[0] = 0x7;     /* op_code, 0x7= wlan toggle slot*/
1693
1694         cur_h2c_parameter[1] = interval;
1695         cur_h2c_parameter[2] = val0x6c4_b0;
1696         cur_h2c_parameter[3] = val0x6c4_b1;
1697         cur_h2c_parameter[4] = val0x6c4_b2;
1698         cur_h2c_parameter[5] = val0x6c4_b3;
1699
1700         if (!force_exec) {
1701                 for (i = 1; i <= 5; i++) {
1702                         if (cur_h2c_parameter[i] != pre_h2c_parameter[i])
1703                                 break;
1704
1705                         match_cnt++;
1706                 }
1707
1708                 if (match_cnt == 5)
1709                         return;
1710         }
1711
1712         for (i = 1; i <= 5; i++)
1713                 pre_h2c_parameter[i] = cur_h2c_parameter[i];
1714
1715         btc->btc_fill_h2c(btc, 0x69, 6, cur_h2c_parameter);
1716 }
1717 #endif
1718
1719 static
1720 void halbtc8821c2ant_coex_table(struct btc_coexist *btc,
1721                                 boolean force_exec, u32 val0x6c0,
1722                                 u32 val0x6c4, u32 val0x6c8,
1723                                 u8 val0x6cc)
1724 {
1725         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1726
1727         if (!force_exec) {
1728                 if (val0x6c0 == coex_dm->cur_val0x6c0 &&
1729                     val0x6c4 == coex_dm->cur_val0x6c4 &&
1730                     val0x6c8 == coex_dm->cur_val0x6c8 &&
1731                     val0x6cc == coex_dm->cur_val0x6cc)
1732                         return;
1733         }
1734
1735         btc->btc_write_4byte(btc, 0x6c0, val0x6c0);
1736         btc->btc_write_4byte(btc, 0x6c4, val0x6c4);
1737         btc->btc_write_4byte(btc, 0x6c8, val0x6c8);
1738         btc->btc_write_1byte(btc, 0x6cc, val0x6cc);
1739
1740         coex_dm->cur_val0x6c0 = val0x6c0;
1741         coex_dm->cur_val0x6c4 = val0x6c4;
1742         coex_dm->cur_val0x6c8 = val0x6c8;
1743         coex_dm->cur_val0x6cc = val0x6cc;
1744 }
1745
1746 void halbtc8821c2ant_table(struct btc_coexist *btc, boolean force_exec, u8 type)
1747 {
1748         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
1749         u32     break_table;
1750         u8      select_table;
1751
1752         coex_sta->coex_table_type = type;
1753
1754         if (coex_sta->concurrent_rx_mode_on == TRUE) {
1755                 /* set WL hi-pri can break BT */
1756                 break_table = 0xf0ffffff;
1757                 /* set Tx response = Hi-Pri (ex: Transmitting ACK,BA,CTS) */
1758                 select_table = 0x1b;
1759         } else {
1760                 break_table = 0xffffff;
1761                 select_table = 0x13;
1762         }
1763
1764         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1765                     "[BTCoex], ********** Table-%d **********\n",
1766                     coex_sta->coex_table_type);
1767         BTC_TRACE(trace_buf);
1768
1769         switch (type) {
1770         case 0:
1771                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffffffff,
1772                                            0xffffffff, break_table,
1773                                            select_table);
1774                 break;
1775         case 1:
1776                 halbtc8821c2ant_coex_table(btc, force_exec, 0x55555555,
1777                                            0xfafafafa, break_table,
1778                                            select_table);
1779                 break;
1780         case 2:
1781                 halbtc8821c2ant_coex_table(btc, force_exec, 0x5a5a5a5a,
1782                                            0x5a5a5a5a, break_table,
1783                                            select_table);
1784                 break;
1785         case 3:
1786                 halbtc8821c2ant_coex_table(btc, force_exec, 0x55555555,
1787                                            0x5a5a5a5a, break_table,
1788                                            select_table);
1789                 break;
1790         case 4:
1791                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffff55ff,
1792                                            0xfafafafa, break_table,
1793                                            select_table);
1794                 break;
1795         case 5:
1796                 halbtc8821c2ant_coex_table(btc, force_exec, 0x55555555,
1797                                            0x55555555, break_table,
1798                                            select_table);
1799                 break;
1800         case 6:
1801                 halbtc8821c2ant_coex_table(btc, force_exec, 0xaaffffaa,
1802                                            0x5afa5afa, break_table,
1803                                            select_table);
1804                 break;
1805         case 7:
1806                 halbtc8821c2ant_coex_table(btc, force_exec, 0xaaffffaa,
1807                                            0xfafafafa, break_table,
1808                                            select_table);
1809                 break;
1810         case 8:
1811                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffff55ff,
1812                                            0xfafafafa, break_table,
1813                                            select_table);
1814                 break;
1815         case 9:
1816                 halbtc8821c2ant_coex_table(btc, force_exec, 0x5a5a5a5a,
1817                                            0xaaaa5aaa, break_table,
1818                                            select_table);
1819                 break;
1820         case 10:
1821                 halbtc8821c2ant_coex_table(btc, force_exec, 0xaaaaaaaa,
1822                                            0xaaaaaaaa, break_table,
1823                                            select_table);
1824                 break;
1825         case 11:
1826                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffffffff,
1827                                            0xfafafafa, break_table,
1828                                            select_table);
1829                 break;
1830         case 12:
1831                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffff55ff,
1832                                            0x5afa5afa, break_table,
1833                                            select_table);
1834                 break;
1835         case 14:
1836                 halbtc8821c2ant_coex_table(btc, force_exec, 0xffff55ff,
1837                                            0xaaaaaaaa, break_table,
1838                                            select_table);
1839                 break;
1840         case 15:
1841                 halbtc8821c2ant_coex_table(btc, force_exec, 0x65555555,
1842                                            0xaaaaaaaa, break_table,
1843                                            select_table);
1844                 break;
1845         default:
1846                 break;
1847         }
1848 }
1849
1850 static
1851 void halbtc8821c2ant_ignore_wlan_act(struct btc_coexist *btc,
1852                                      boolean force_exec, boolean enable)
1853 {
1854         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1855         u8      h2c_parameter[1] = {0};
1856
1857         if (btc->manual_control || btc->stop_coex_dm)
1858                 return;
1859
1860         if (!force_exec) {
1861                 if (enable == coex_dm->cur_ignore_wlan_act)
1862                         return;
1863         }
1864
1865         if (enable)
1866                 h2c_parameter[0] |= BIT(0); /* function enable */
1867
1868         btc->btc_fill_h2c(btc, 0x63, 1, h2c_parameter);
1869
1870         coex_dm->cur_ignore_wlan_act = enable;
1871 }
1872
1873 static
1874 void halbtc8821c2ant_lps_rpwm(struct btc_coexist *btc, boolean force_exec,
1875                               u8 lps_val, u8 rpwm_val)
1876 {
1877         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1878
1879         if (!force_exec) {
1880                 if (lps_val == coex_dm->cur_lps &&
1881                     rpwm_val == coex_dm->cur_rpwm)
1882                         return;
1883         }
1884
1885         btc->btc_set(btc, BTC_SET_U1_LPS_VAL, &lps_val);
1886         btc->btc_set(btc, BTC_SET_U1_RPWM_VAL, &rpwm_val);
1887
1888         coex_dm->cur_lps = lps_val;
1889         coex_dm->cur_rpwm = rpwm_val;
1890 }
1891
1892 static
1893 void halbtc8821c2ant_tdma_check(struct btc_coexist *btc, boolean new_ps_state)
1894 {
1895         u8 lps_mode = 0x0;
1896         u8 h2c_parameter[5] = {0, 0, 0, 0x40, 0};
1897
1898         btc->btc_get(btc, BTC_GET_U1_LPS_MODE, &lps_mode);
1899
1900         if (lps_mode) { /* already under LPS state */
1901                 if (new_ps_state) {
1902                         /* keep state under LPS, do nothing. */
1903                 } else {
1904                         /* will leave LPS state, turn off psTdma first */
1905                         btc->btc_fill_h2c(btc, 0x60, 5, h2c_parameter);
1906                 }
1907         } else {                                        /* NO PS state */
1908                 if (new_ps_state) {
1909                         /* will enter LPS state, turn off psTdma first */
1910                         btc->btc_fill_h2c(btc, 0x60, 5, h2c_parameter);
1911                 } else {
1912                         /* keep state under NO PS state, do nothing. */
1913                 }
1914         }
1915 }
1916
1917 static
1918 boolean halbtc8821c2ant_power_save_state(struct btc_coexist *btc, u8 ps_type,
1919                                          u8 lps_val, u8 rpwm_val)
1920 {
1921         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
1922         boolean low_pwr_disable = FALSE, result = TRUE;
1923
1924         switch (ps_type) {
1925         case BTC_PS_WIFI_NATIVE:
1926                 coex_sta->force_lps_ctrl = FALSE;
1927                 /* recover to original 32k low power setting */
1928                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1929                             "[BTCoex], %s == BTC_PS_WIFI_NATIVE\n", __func__);
1930                 BTC_TRACE(trace_buf);
1931
1932                 low_pwr_disable = FALSE;
1933                 /* btc->btc_set(btc, BTC_SET_ACT_DISABLE_LOW_POWER,
1934                  * &low_pwr_disable);
1935                  */
1936                 btc->btc_set(btc, BTC_SET_ACT_PRE_NORMAL_LPS, NULL);
1937                 break;
1938         case BTC_PS_LPS_ON:
1939                 coex_sta->force_lps_ctrl = TRUE;
1940                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1941                             "[BTCoex], %s == BTC_PS_LPS_ON\n", __func__);
1942                 BTC_TRACE(trace_buf);
1943
1944                 halbtc8821c2ant_tdma_check(btc, TRUE);
1945                 halbtc8821c2ant_lps_rpwm(btc, NM_EXCU, lps_val, rpwm_val);
1946                 /* when coex force to enter LPS, do not enter 32k low power. */
1947                 low_pwr_disable = TRUE;
1948                 btc->btc_set(btc, BTC_SET_ACT_DISABLE_LOW_POWER,
1949                              &low_pwr_disable);
1950                 /* power save must executed before psTdma.*/
1951                 btc->btc_set(btc, BTC_SET_ACT_ENTER_LPS, NULL);
1952                 break;
1953         case BTC_PS_LPS_OFF:
1954                 coex_sta->force_lps_ctrl = TRUE;
1955                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1956                             "[BTCoex], %s == BTC_PS_LPS_OFF\n", __func__);
1957                 BTC_TRACE(trace_buf);
1958
1959                 halbtc8821c2ant_tdma_check(btc, FALSE);
1960                 result = btc->btc_set(btc, BTC_SET_ACT_LEAVE_LPS, NULL);
1961                 break;
1962         default:
1963                 break;
1964         }
1965
1966         return result;
1967 }
1968
1969 static
1970 void halbtc8821c2ant_set_tdma(struct btc_coexist *btc, u8 byte1, u8 byte2,
1971                               u8 byte3, u8 byte4, u8 byte5)
1972 {
1973         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
1974         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
1975         u8 h2c_parameter[5] = {0};
1976         u8 real_byte1 = byte1, real_byte5 = byte5;
1977         boolean ap_enable = FALSE, result = FALSE;
1978         struct btc_bt_link_info *bt_link_info = &btc->bt_link_info;
1979         u8 ps_type = BTC_PS_WIFI_NATIVE;
1980
1981         if (byte5 & BIT(2))
1982                 coex_sta->is_tdma_btautoslot = TRUE;
1983         else
1984                 coex_sta->is_tdma_btautoslot = FALSE;
1985
1986         /* release bt-auto slot for auto-slot hang is detected!! */
1987         if (coex_sta->is_tdma_btautoslot)
1988                 if (coex_sta->is_tdma_btautoslot_hang ||
1989                     bt_link_info->slave_role)
1990                         byte5 = byte5 & 0xfb;
1991
1992 #if 1
1993         btc->btc_get(btc, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &ap_enable);
1994 #else
1995         if (btc->wifi_link_info.link_mode == BTC_LINK_ONLY_GO &&
1996             btc->wifi_link_info.bhotspot &&
1997             btc->wifi_link_info.bany_client_join_go)
1998                 ap_enable = TRUE;
1999 #endif
2000
2001         if ((ap_enable) && (byte1 & BIT(4) && !(byte1 & BIT(5)))) {
2002                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2003                             "[BTCoex], %s == FW for AP mode\n", __func__);
2004                 BTC_TRACE(trace_buf);
2005
2006                 real_byte1 &= ~BIT(4);
2007                 real_byte1 |= BIT(5);
2008
2009                 real_byte5 |= BIT(5);
2010                 real_byte5 &= ~BIT(6);
2011
2012                 ps_type = BTC_PS_WIFI_NATIVE;
2013                 halbtc8821c2ant_power_save_state(btc, ps_type, 0x0, 0x0);
2014         } else if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
2015                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2016                             "[BTCoex], %s == Force LPS (byte1 = 0x%x)\n",
2017                             __func__, byte1);
2018                 BTC_TRACE(trace_buf);
2019
2020                 ps_type = BTC_PS_LPS_OFF;
2021                 if (!halbtc8821c2ant_power_save_state(btc, ps_type, 0x50, 0x4))
2022                         result = TRUE;
2023         } else {
2024                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2025                             "[BTCoex], %s == Native LPS (byte1 = 0x%x)\n",
2026                             __func__, byte1);
2027                 BTC_TRACE(trace_buf);
2028
2029                 ps_type = BTC_PS_WIFI_NATIVE;
2030                 halbtc8821c2ant_power_save_state(btc, ps_type, 0x0, 0x0);
2031         }
2032
2033         coex_sta->is_set_ps_state_fail = result;
2034
2035         if (!coex_sta->is_set_ps_state_fail) {
2036                 h2c_parameter[0] = real_byte1;
2037                 h2c_parameter[1] = byte2;
2038                 h2c_parameter[2] = byte3;
2039                 h2c_parameter[3] = byte4;
2040                 h2c_parameter[4] = real_byte5;
2041
2042                 coex_dm->ps_tdma_para[0] = real_byte1;
2043                 coex_dm->ps_tdma_para[1] = byte2;
2044                 coex_dm->ps_tdma_para[2] = byte3;
2045                 coex_dm->ps_tdma_para[3] = byte4;
2046                 coex_dm->ps_tdma_para[4] = real_byte5;
2047
2048                 btc->btc_fill_h2c(btc, 0x60, 5, h2c_parameter);
2049         } else {
2050                 coex_sta->cnt_set_ps_state_fail++;
2051                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2052                             "[BTCoex], %s == Force Leave LPS Fail (cnt = %d)\n",
2053                             __func__, coex_sta->cnt_set_ps_state_fail);
2054                 BTC_TRACE(trace_buf);
2055         }
2056
2057         if (ps_type == BTC_PS_WIFI_NATIVE)
2058                 btc->btc_set(btc, BTC_SET_ACT_POST_NORMAL_LPS, NULL);
2059 }
2060
2061 static
2062 void halbtc8821c2ant_tdma(struct btc_coexist *btc,
2063                           boolean force_exec, boolean turn_on, u8 type)
2064 {
2065         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
2066         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
2067         static u8 tdma_byte4_modify, pre_tdma_byte4_modify;
2068         struct btc_bt_link_info *bt_link_info = &btc->bt_link_info;
2069
2070         btc->btc_set_atomic(btc, &coex_dm->setting_tdma, TRUE);
2071
2072         /* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
2073         if (bt_link_info->slave_role && bt_link_info->a2dp_exist)
2074                 tdma_byte4_modify = 0x1;
2075         else
2076                 tdma_byte4_modify = 0x0;
2077
2078         if (pre_tdma_byte4_modify != tdma_byte4_modify) {
2079                 force_exec = TRUE;
2080                 pre_tdma_byte4_modify = tdma_byte4_modify;
2081         }
2082
2083         if (!force_exec) {
2084                 if (turn_on == coex_dm->cur_ps_tdma_on &&
2085                     type == coex_dm->cur_ps_tdma) {
2086                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2087                                     "[BTCoex], Skip TDMA because no change TDMA(%s, %d)\n",
2088                                     (coex_dm->cur_ps_tdma_on ? "on" : "off"),
2089                                     coex_dm->cur_ps_tdma);
2090                         BTC_TRACE(trace_buf);
2091
2092                         btc->btc_set_atomic(btc, &coex_dm->setting_tdma, FALSE);
2093                         return;
2094                 }
2095         }
2096
2097         if (turn_on) {
2098                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2099                             "[BTCoex], ********** TDMA(on, %d) **********\n",
2100                             type);
2101                 BTC_TRACE(trace_buf);
2102
2103                 /* enable TBTT nterrupt */
2104                 btc->btc_write_1byte_bitmask(btc, 0x550, 0x8, 0x1);
2105
2106                 switch (type) {
2107                 case 1:
2108                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x03, 0x91,
2109                                                  0x50 | tdma_byte4_modify);
2110                         break;
2111                 case 2:
2112                 default:
2113                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x35, 0x03, 0x11,
2114                                                  0x11 | tdma_byte4_modify);
2115                         break;
2116                 case 3:
2117                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x30, 0x3, 0x91,
2118                                                  0x10 | tdma_byte4_modify);
2119                         break;
2120                 case 4:
2121                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x21, 0x3, 0x91,
2122                                                  0x10 | tdma_byte4_modify);
2123                         break;
2124                 case 5:
2125                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x25, 0x3, 0x91,
2126                                                  0x10 | tdma_byte4_modify);
2127                         break;
2128                 case 6:
2129                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x3, 0x91,
2130                                                  0x10 | tdma_byte4_modify);
2131                         break;
2132                 case 7:
2133                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x20, 0x3, 0x91,
2134                                                  0x10 | tdma_byte4_modify);
2135                         break;
2136                 case 8:
2137                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x15, 0x03, 0x11,
2138                                                  0x11);
2139                         break;
2140                 case 10:
2141                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x30, 0x03, 0x11,
2142                                                  0x10);
2143                         break;
2144                 case 11:
2145                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x35, 0x03, 0x11,
2146                                                  0x10 | tdma_byte4_modify);
2147                         break;
2148                 case 12:
2149                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x35, 0x03, 0x11,
2150                                                  0x11);
2151                         break;
2152                 case 13:
2153                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x1c, 0x03, 0x11,
2154                                                  0x10 | tdma_byte4_modify);
2155                         break;
2156                 case 14:
2157                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x20, 0x03, 0x11,
2158                                                  0x11);
2159                         break;
2160                 case 15:
2161                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x03, 0x11,
2162                                                  0x10);
2163                         break;
2164                 case 16:
2165                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x03, 0x11,
2166                                                  0x11);
2167                         break;
2168                 case 17:
2169                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x08, 0x03, 0x11,
2170                                                  0x14 | tdma_byte4_modify);
2171                         break;
2172                 case 21:
2173                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x30, 0x03, 0x11,
2174                                                  0x10);
2175                         break;
2176                 case 22:
2177                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x25, 0x03, 0x11,
2178                                                  0x10);
2179                         break;
2180                 case 23:
2181                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x03, 0x11,
2182                                                  0x10);
2183                         break;
2184                 case 25:
2185                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x3a, 0x3, 0x11,
2186                                                  0x50);
2187                         break;
2188                 case 51:
2189                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x10, 0x03, 0x91,
2190                                                  0x10 | tdma_byte4_modify);
2191                         break;
2192                 case 101:
2193                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x08, 0x03, 0x10,
2194                                                  0x54 | tdma_byte4_modify);
2195                         break;
2196                 case 102:
2197                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x35, 0x03, 0x11,
2198                                                  0x11 | tdma_byte4_modify);
2199                         break;
2200                 case 103:
2201                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x30, 0x3, 0x10,
2202                                                  0x50 | tdma_byte4_modify);
2203                         break;
2204                 case 104:
2205                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x21, 0x3, 0x10,
2206                                                  0x50 | tdma_byte4_modify);
2207                         break;
2208                 case 105:
2209                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x45, 0x3, 0x10,
2210                                                  0x50);
2211                         break;
2212                 case 106:
2213                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x1a, 0x3, 0x10,
2214                                                  0x50 | tdma_byte4_modify);
2215                         break;
2216                 case 107:
2217                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x08, 0x7, 0x10,
2218                                                  0x54);
2219                         break;
2220                 case 108:
2221                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x30, 0x3, 0x10,
2222                                                  0x50 | tdma_byte4_modify);
2223                         break;
2224                 case 109:
2225                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x08, 0x03, 0x10,
2226                                                  0x54 | tdma_byte4_modify);
2227                         break;
2228                 case 110:
2229                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x30, 0x03, 0x10,
2230                                                  0x50);
2231                         break;
2232                 case 111:
2233                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x25, 0x03, 0x11,
2234                                                  0x11);
2235                         break;
2236                 case 112:
2237                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x4a, 0x3, 0x10,
2238                                                  0x50);
2239                         break;
2240                 case 113:
2241                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x48, 0x03, 0x11,
2242                                                  0x10);
2243                         break;
2244                 case 115:
2245                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x30, 0x03, 0x10,
2246                                                  0x50);
2247                         break;
2248                 case 116: /* Not use  */
2249                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x08, 0x03, 0x10,
2250                                                  0x54 | tdma_byte4_modify);
2251                         break;
2252                 case 117: /* Not use  */
2253                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x08, 0x03, 0x10,
2254                                                  0x10 | tdma_byte4_modify);
2255                         break;
2256                 case 119:
2257                         halbtc8821c2ant_set_tdma(btc, 0x61, 0x08, 0x03, 0x10,
2258                                                  0x14 | tdma_byte4_modify);
2259                         break;
2260                 case 151:
2261                         halbtc8821c2ant_set_tdma(btc, 0x51, 0x10, 0x03, 0x10,
2262                                                  0x50 | tdma_byte4_modify);
2263                         break;
2264                 }
2265         } else {
2266                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2267                             "[BTCoex], ********** TDMA(off, %d) **********\n",
2268                             type);
2269                 BTC_TRACE(trace_buf);
2270
2271                 /* disable PS tdma */
2272                 switch (type) {
2273                 case 0:
2274                         halbtc8821c2ant_set_tdma(btc, 0x0, 0x0, 0x0, 0x40, 0x0);
2275                         break;
2276                 case 1:
2277                         halbtc8821c2ant_set_tdma(btc, 0x0, 0x0, 0x0, 0x48, 0x0);
2278                         break;
2279                 default:
2280                         halbtc8821c2ant_set_tdma(btc, 0x0, 0x0, 0x0, 0x40, 0x0);
2281                         break;
2282                 }
2283         }
2284
2285         if (!coex_sta->is_set_ps_state_fail) {
2286                 /* update pre state */
2287                 coex_dm->cur_ps_tdma_on = turn_on;
2288                 coex_dm->cur_ps_tdma = type;
2289         }
2290
2291         btc->btc_set_atomic(btc, &coex_dm->setting_tdma, FALSE);
2292 }
2293
2294 static
2295 void halbtc8821c2ant_set_ant_switch(struct btc_coexist *btc,
2296                                     boolean force_exec, u8 ctrl_type,
2297                                     u8 pos_type)
2298 {
2299         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
2300         struct rfe_type_8821c_2ant *rfe_type = &btc->rfe_type_8821c_2ant;
2301         struct  btc_board_info  *board_info = &btc->board_info;
2302         boolean polarity_inverse = FALSE;
2303         u8 regval = 0;
2304         u32 u32tmp1 = 0, u32tmp2 = 0, u32tmp3 = 0;
2305
2306         if (!rfe_type->ext_ant_switch_exist)
2307                 return;
2308
2309         if (!force_exec) {
2310                 if (((ctrl_type << 8) + pos_type) ==
2311                                         coex_dm->cur_ext_ant_switch_status)
2312                         return;
2313         }
2314
2315         coex_dm->cur_ext_ant_switch_status = (ctrl_type << 8)  + pos_type;
2316
2317         /* swap control polarity if use different switch control polarity
2318          * Normal switch polarity for DPDT,
2319          * 0xcb4[29:28] = 2b'01 => BTG to Main, WLG to Aux,
2320          * 0xcb4[29:28] = 2b'10 => BTG to Aux, WLG to Main
2321          * Normal switch polarity for SPDT,
2322          * 0xcb4[29:28] = 2b'01 => Ant to BTG,
2323          * 0xcb4[29:28] = 2b'10 => Ant to WLG
2324          */
2325         if (rfe_type->ext_ant_switch_ctrl_polarity)
2326                 polarity_inverse =  !polarity_inverse;
2327
2328         /* swap control polarity if 1-Ant at Aux */
2329         if (rfe_type->ant_at_main_port == FALSE)
2330                 polarity_inverse =  !polarity_inverse;
2331
2332         switch (pos_type) {
2333         default:
2334         case BT_8821C_2ANT_TO_BT:
2335         case BT_8821C_2ANT_TO_NOCARE:
2336         case BT_8821C_2ANT_TO_WLA:
2337
2338                 break;
2339         case BT_8821C_2ANT_TO_WLG:
2340                 if (!rfe_type->wlg_locate_at_btg)
2341                         polarity_inverse =  !polarity_inverse;
2342
2343                 break;
2344         }
2345
2346         if (board_info->ant_div_cfg)
2347                 ctrl_type = BT_8821C_2ANT_CTRL_BY_ANTDIV;
2348
2349
2350         switch (ctrl_type) {
2351         default:
2352         case BT_8821C_2ANT_CTRL_BY_BBSW:
2353                 /*  0x4c[23] = 0 */
2354                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x0);
2355                 /* 0x4c[24] = 1 */
2356                 btc->btc_write_1byte_bitmask(btc, 0x4f, 0x01, 0x1);
2357                 /* BB SW, DPDT use RFE_ctrl8 and RFE_ctrl9 as control pin */
2358                 btc->btc_write_1byte_bitmask(btc, 0xcb4, 0xff, 0x77);
2359                 /* 0xcb4[29:28] = 2b'01 for no switch_polarity_inverse,
2360                  * DPDT_SEL_N =1, DPDT_SEL_P =0
2361                  */
2362                 regval = (!polarity_inverse ? 0x1 : 0x2);
2363                 btc->btc_write_1byte_bitmask(btc, 0xcb7, 0x30, regval);
2364                 break;
2365         case BT_8821C_2ANT_CTRL_BY_PTA:
2366                 /* 0x4c[23] = 0 */
2367                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x0);
2368                 /* 0x4c[24] = 1 */
2369                 btc->btc_write_1byte_bitmask(btc, 0x4f, 0x01, 0x1);
2370                 /* PTA,  DPDT use RFE_ctrl8 and RFE_ctrl9 as control pin */
2371                 btc->btc_write_1byte_bitmask(btc, 0xcb4, 0xff, 0x66);
2372                 /* 0xcb4[29:28] = 2b'10 for no switch_polatiry_inverse,
2373                  * DPDT_SEL_N =1, DPDT_SEL_P =0  @ GNT_BT=1
2374                  */
2375                 regval = (!polarity_inverse ? 0x2 : 0x1);
2376                 btc->btc_write_1byte_bitmask(btc, 0xcb7, 0x30, regval);
2377                 break;
2378         case BT_8821C_2ANT_CTRL_BY_ANTDIV:
2379                 /* 0x4c[23] = 0 */
2380                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x0);
2381                 /* 0x4c[24] = 1 */
2382                 btc->btc_write_1byte_bitmask(btc, 0x4f, 0x01, 0x1);
2383                 btc->btc_write_1byte_bitmask(btc, 0xcb4, 0xff, 0x88);
2384
2385                 /* no regval_0xcb7 setup required, because antenna switch
2386                  * control value by antenna diversity
2387                  */
2388                 break;
2389         case BT_8821C_2ANT_CTRL_BY_MAC:
2390                 /*  0x4c[23] = 1 */
2391                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x1);
2392                 /* 0x64[0] = 1b'0 for no switch_polarity_inverse,
2393                  * DPDT_SEL_N =1, DPDT_SEL_P =0
2394                  */
2395                 regval = (!polarity_inverse ?  0x0 : 0x1);
2396                 btc->btc_write_1byte_bitmask(btc, 0x64, 0x1, regval);
2397                 break;
2398         case BT_8821C_2ANT_CTRL_BY_FW:
2399                 /* 0x4c[23] = 0 */
2400                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x0);
2401                 /* 0x4c[24] = 1 */
2402                 btc->btc_write_1byte_bitmask(btc, 0x4f, 0x01, 0x1);
2403                 break;
2404         case BT_8821C_2ANT_CTRL_BY_BT:
2405                 /* 0x4c[23] = 0 */
2406                 btc->btc_write_1byte_bitmask(btc, 0x4e, 0x80, 0x0);
2407                 /* 0x4c[24] = 0 */
2408                 btc->btc_write_1byte_bitmask(btc, 0x4f, 0x01, 0x0);
2409                 /* no  setup required, because  antenna switch control value
2410                  * by BT vendor 0xac[1:0]
2411                  */
2412                 break;
2413         }
2414
2415         /* PAPE, LNA_ON control by BT  while WLAN off
2416          * for current leakage issue
2417          */
2418         if (ctrl_type == BT_8821C_2ANT_CTRL_BY_BT) {
2419                 /* PAPE   0x64[29] = 0 */
2420                 btc->btc_write_1byte_bitmask(btc, 0x67, 0x20, 0x0);
2421                 /* LNA_ON 0x64[28] = 0 */
2422                 btc->btc_write_1byte_bitmask(btc, 0x67, 0x10, 0x0);
2423         } else {
2424                 /* PAPE   0x64[29] = 1 */
2425                 btc->btc_write_1byte_bitmask(btc, 0x67, 0x20, 0x1);
2426                 /* LNA_ON 0x64[28] = 1 */
2427                 btc->btc_write_1byte_bitmask(btc, 0x67, 0x10, 0x1);
2428         }
2429 }
2430
2431 static
2432 void halbtc8821c2ant_set_rfe_type(struct btc_coexist *btc)
2433 {
2434         struct rfe_type_8821c_2ant *rfe_type = &btc->rfe_type_8821c_2ant;
2435         struct  btc_board_info *board_info = &btc->board_info;
2436
2437         /* the following setup should be got from Efuse in the future */
2438         rfe_type->rfe_module_type = board_info->rfe_type & 0x1f;
2439
2440         rfe_type->ext_ant_switch_ctrl_polarity = 0;
2441
2442         switch (rfe_type->rfe_module_type) {
2443         case 0:
2444         case 8:
2445         default: /*2-Ant, DPDT, WLG*/
2446                 rfe_type->ext_ant_switch_exist = TRUE;
2447                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_DPDT;
2448                 rfe_type->wlg_locate_at_btg = FALSE;
2449                 rfe_type->ant_at_main_port = TRUE;
2450                 break;
2451         case 1:
2452         case 9:  /*1-Ant, Main, WLG */
2453                 rfe_type->ext_ant_switch_exist = TRUE;
2454                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_SPDT;
2455                 rfe_type->wlg_locate_at_btg = FALSE;
2456                 rfe_type->ant_at_main_port = TRUE;
2457                 break;
2458         case 2:
2459         case 10:  /*1-Ant, Main, BTG */
2460                 rfe_type->ext_ant_switch_exist = TRUE;
2461                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_SPDT;
2462                 rfe_type->wlg_locate_at_btg = TRUE;
2463                 rfe_type->ant_at_main_port = TRUE;
2464                 break;
2465         case 3:
2466         case 11: /*1-Ant, Aux, WLG */
2467                 rfe_type->ext_ant_switch_exist = TRUE;
2468                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_DPDT;
2469                 rfe_type->wlg_locate_at_btg = FALSE;
2470                 rfe_type->ant_at_main_port = FALSE;
2471                 break;
2472         case 4:
2473         case 12: /*1-Ant, Aux, BTG */
2474                 rfe_type->ext_ant_switch_exist = TRUE;
2475                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_DPDT;
2476                 rfe_type->wlg_locate_at_btg = TRUE;
2477                 rfe_type->ant_at_main_port = FALSE;
2478                 break;
2479         case 5:
2480         case 13: /*2-Ant, no switch, WLG*/
2481                 rfe_type->ext_ant_switch_exist = FALSE;
2482                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_SWITCH_NONE;
2483                 rfe_type->wlg_locate_at_btg = FALSE;
2484                 rfe_type->ant_at_main_port = TRUE;
2485                 break;
2486         case 6:
2487         case 14: /*2-Ant, no antenna switch, WLG*/
2488                 rfe_type->ext_ant_switch_exist = FALSE;
2489                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_SWITCH_NONE;
2490                 rfe_type->wlg_locate_at_btg = FALSE;
2491                 rfe_type->ant_at_main_port = TRUE;
2492                 break;
2493         case 7:
2494         case 15: /*2-Ant, DPDT, BTG*/
2495                 rfe_type->ext_ant_switch_exist = TRUE;
2496                 rfe_type->ext_ant_switch_type = BT_8821C_2ANT_USE_DPDT;
2497                 rfe_type->wlg_locate_at_btg = TRUE;
2498                 rfe_type->ant_at_main_port = TRUE;
2499                 break;
2500         }
2501 }
2502
2503 static
2504 void halbtc8821c2ant_set_ant_path(struct btc_coexist *btc,
2505                                   u8 ant_pos_type,  boolean force_exec,
2506                                   u8 phase)
2507 {
2508         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
2509         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
2510         struct btc_board_info *board_info = &btc->board_info;
2511         u32 cnt_bt_cal_chk = 0, u32tmp1 = 0, u32tmp2 = 0;
2512         u8 u8tmp = 0, ctrl_type, pos_type;
2513
2514         if (!force_exec) {
2515                 if (coex_dm->cur_ant_pos_type == ((ant_pos_type << 8)  + phase))
2516                         return;
2517         }
2518
2519         coex_dm->cur_ant_pos_type = (ant_pos_type << 8)  + phase;
2520
2521         if (btc->dbg_mode) {
2522                 u32tmp1 = btc->btc_read_4byte(btc, 0xcbc);
2523                 u32tmp2 = btc->btc_read_4byte(btc, 0xcb4);
2524                 u8tmp  = btc->btc_read_1byte(btc, 0x73);
2525
2526                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2527                             "[BTCoex], (Before Ant Setup) 0xcb4 = 0x%x, 0xcbc = 0x%x, 0x73 = 0x%x\n",
2528                             u32tmp1, u32tmp2, u8tmp);
2529                 BTC_TRACE(trace_buf);
2530         }
2531
2532         switch (phase) {
2533         case BT_8821C_2ANT_PHASE_POWERON:
2534                 /* set Path control owner to WL at initial step */
2535                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_BTSIDE);
2536
2537                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2538                         if (board_info->btdm_ant_pos ==
2539                             BTC_ANTENNA_AT_MAIN_PORT)
2540                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2541                         else
2542                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2543                 }
2544
2545                 coex_sta->run_time_state = FALSE;
2546                 break;
2547         case BT_8821C_2ANT_PHASE_INIT:
2548                 /* Disable LTE Coex Function in WiFi side
2549                  * (this should be on if LTE coex is required)
2550                  */
2551                 halbtc8821c2ant_ltecoex_enable(btc, 0x0);
2552
2553                 /* GNT_WL_LTE always = 1
2554                  * (this should be config if LTE coex is required)
2555                  */
2556                 halbtc8821c2ant_ltecoex_table(btc, BT_8821C_2ANT_CTT_WL_VS_LTE,
2557                                               0xffff);
2558
2559                 /* GNT_BT_LTE always = 1
2560                  * (this should be config if LTE coex is required)
2561                  */
2562                 halbtc8821c2ant_ltecoex_table(btc, BT_8821C_2ANT_CTT_BT_VS_LTE,
2563                                               0xffff);
2564
2565                 /* Wait If BT IQK running, because Path control owner
2566                  * is at BT during BT IQK (setup by WiFi firmware)
2567                  */
2568                 while (cnt_bt_cal_chk <= 20) {
2569                         u8tmp = btc->btc_read_1byte(btc, 0x49c);
2570                         cnt_bt_cal_chk++;
2571
2572                         if (u8tmp & BIT(1)) {
2573                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2574                                             "[BTCoex], ########### BT is calibrating (wait cnt=%d)\n",
2575                                             cnt_bt_cal_chk);
2576                                 BTC_TRACE(trace_buf);
2577                                 delay_ms(10);
2578                         } else {
2579                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2580                                             "[BTCoex], ********** BT is NOT calibrating (wait cnt=%d)\n",
2581                                             cnt_bt_cal_chk);
2582                                 BTC_TRACE(trace_buf);
2583                                 break;
2584                         }
2585                 }
2586
2587                 /* set Path control owner to WL at initial step */
2588                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2589
2590                 /* set GNT_BT to SW high */
2591                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2592                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2593                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2594                 /* Set GNT_WL to SW high */
2595                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2596                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2597                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2598
2599                 coex_sta->run_time_state = FALSE;
2600
2601                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2602                         if (board_info->btdm_ant_pos ==
2603                             BTC_ANTENNA_AT_MAIN_PORT)
2604                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2605                         else
2606                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2607                 }
2608                 break;
2609         case BT_8821C_2ANT_PHASE_WONLY:
2610                 /* Disable LTE Coex Function in WiFi side
2611                  * (this should be on if LTE coex is required)
2612                  */
2613                 halbtc8821c2ant_ltecoex_enable(btc, 0x0);
2614
2615                 /* GNT_WL_LTE always = 1
2616                  * (this should be config if LTE coex is required)
2617                  */
2618                 halbtc8821c2ant_ltecoex_table(btc, BT_8821C_2ANT_CTT_WL_VS_LTE,
2619                                               0xffff);
2620
2621                 /* GNT_BT_LTE always = 1
2622                  * (this should be config if LTE coex is required)
2623                  */
2624                 halbtc8821c2ant_ltecoex_table(btc, BT_8821C_2ANT_CTT_BT_VS_LTE,
2625                                               0xffff);
2626
2627                 /* set Path control owner to WL at initial step */
2628                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2629
2630                 /* set GNT_BT to SW Low */
2631                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2632                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2633                                            BT_8821C_2ANT_GNT_SET_TO_LOW);
2634                 /* Set GNT_WL to SW high */
2635                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2636                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2637                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2638
2639                 coex_sta->run_time_state = FALSE;
2640
2641                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2642                         if (board_info->btdm_ant_pos ==
2643                             BTC_ANTENNA_AT_MAIN_PORT)
2644                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2645                         else
2646                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2647                 }
2648
2649                 break;
2650         case BT_8821C_2ANT_PHASE_WOFF:
2651                 /* Disable LTE Coex Function in WiFi side */
2652                 halbtc8821c2ant_ltecoex_enable(btc, 0x0);
2653
2654                 /* set Path control owner to BT */
2655                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_BTSIDE);
2656
2657                 coex_sta->run_time_state = FALSE;
2658                 break;
2659         case BT_8821C_2ANT_PHASE_2G:
2660                 while (cnt_bt_cal_chk <= 20) {
2661                         /* 0x49c[0]=1 WL IQK, 0x49c[1]=1 BT IQK*/
2662                         u8tmp = btc->btc_read_1byte(btc, 0x49c);
2663
2664                         cnt_bt_cal_chk++;
2665                         if (u8tmp & BIT(0)) {
2666                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2667                                             "[BTCoex], ########### WL is IQK (wait cnt=%d)\n",
2668                                             cnt_bt_cal_chk);
2669                                 BTC_TRACE(trace_buf);
2670                                 delay_ms(10);
2671                         } else if (u8tmp & BIT(1)) {
2672                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2673                                             "[BTCoex], ########### BT is IQK (wait cnt=%d)\n",
2674                                             cnt_bt_cal_chk);
2675                                 BTC_TRACE(trace_buf);
2676                                 delay_ms(10);
2677                         } else {
2678                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2679                                             "[BTCoex], ********** WL and BT is NOT IQK (wait cnt=%d)\n",
2680                                             cnt_bt_cal_chk);
2681                                 BTC_TRACE(trace_buf);
2682                                 break;
2683                         }
2684                 }
2685
2686                 /* set Path control owner to WL at runtime step */
2687                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2688
2689                 /* set GNT_BT to PTA */
2690                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2691                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_PTA,
2692                                            BT_8821C_2ANT_GNT_SET_BY_HW);
2693
2694                 /* Set GNT_WL to PTA */
2695                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2696                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_PTA,
2697                                            BT_8821C_2ANT_GNT_SET_BY_HW);
2698
2699                 coex_sta->run_time_state = TRUE;
2700
2701                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2702                         if (board_info->btdm_ant_pos ==
2703                             BTC_ANTENNA_AT_MAIN_PORT)
2704                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2705                         else
2706                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2707                 }
2708
2709                 break;
2710         case BT_8821C_2ANT_PHASE_5G:
2711                 /* set Path control owner to WL at runtime step */
2712                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2713
2714                 /* set GNT_BT to SW Hi */
2715                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2716                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_PTA,
2717                                            BT_8821C_2ANT_GNT_SET_BY_HW);
2718
2719                 /* Set GNT_WL to SW Hi */
2720                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2721                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2722                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2723
2724                 coex_sta->run_time_state = TRUE;
2725
2726                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2727                         if (board_info->btdm_ant_pos ==
2728                             BTC_ANTENNA_AT_MAIN_PORT)
2729                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2730                         else
2731                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2732                 }
2733
2734                 break;
2735         case BT_8821C_2ANT_PHASE_BTMP:
2736                 /* Disable LTE Coex Function in WiFi side */
2737                 halbtc8821c2ant_ltecoex_enable(btc, 0x0);
2738
2739                 /* set Path control owner to WL */
2740                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2741
2742                 /* set GNT_BT to SW Hi */
2743                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2744                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2745                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2746
2747                 /* Set GNT_WL to SW Lo */
2748                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2749                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2750                                            BT_8821C_2ANT_GNT_SET_TO_LOW);
2751
2752                 coex_sta->run_time_state = FALSE;
2753
2754                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2755                         if (board_info->btdm_ant_pos ==
2756                             BTC_ANTENNA_AT_MAIN_PORT)
2757                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2758                         else
2759                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2760                 }
2761
2762                 break;
2763         case BT_8821C_2ANT_PHASE_ANTDET:
2764                 halbtc8821c2ant_coex_ctrl_owner(btc, BT_8821C_2ANT_PCO_WLSIDE);
2765
2766                 /* set GNT_BT to high */
2767                 halbtc8821c2ant_set_gnt_bt(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2768                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2769                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2770                 /* Set GNT_WL to high */
2771                 halbtc8821c2ant_set_gnt_wl(btc, BT_8821C_2ANT_GNT_BLOCK_RFC_BB,
2772                                            BT_8821C_2ANT_GNT_TYPE_CTRL_BY_SW,
2773                                            BT_8821C_2ANT_GNT_SET_TO_HIGH);
2774
2775                 if (ant_pos_type == BTC_ANT_PATH_AUTO) {
2776                         if (board_info->btdm_ant_pos ==
2777                             BTC_ANTENNA_AT_MAIN_PORT)
2778                                 ant_pos_type = BTC_ANT_WIFI_AT_MAIN;
2779                         else
2780                                 ant_pos_type = BTC_ANT_WIFI_AT_AUX;
2781                 }
2782
2783                 coex_sta->run_time_state = FALSE;
2784                 break;
2785         }
2786
2787         if (phase != BT_8821C_2ANT_PHASE_WOFF) {
2788                 /* Set Ext Ant Switch to BT control at wifi off step */
2789                 ctrl_type = BT_8821C_2ANT_CTRL_BY_BT;
2790                 pos_type = BT_8821C_2ANT_TO_NOCARE;
2791         } else {
2792                 switch (ant_pos_type) {
2793                 default:
2794                 case BTC_ANT_WIFI_AT_MAIN:
2795                         ctrl_type = BT_8821C_2ANT_CTRL_BY_BBSW;
2796                         pos_type = BT_8821C_2ANT_TO_WLG;
2797                         break;
2798                 case BTC_ANT_WIFI_AT_AUX:
2799                         ctrl_type = BT_8821C_2ANT_CTRL_BY_BBSW;
2800                         pos_type = BT_8821C_2ANT_TO_BT;
2801                         break;
2802                 case BTC_ANT_WIFI_AT_DIVERSITY:
2803                         ctrl_type = BT_8821C_2ANT_CTRL_BY_ANTDIV;
2804                         pos_type = BT_8821C_2ANT_TO_NOCARE;
2805                         break;
2806                 }
2807         }
2808
2809         halbtc8821c2ant_set_ant_switch(btc, force_exec, ctrl_type, pos_type);
2810
2811         if (btc->dbg_mode) {
2812                 u32tmp1 = btc->btc_read_4byte(btc, 0xcbc);
2813                 u32tmp2 = btc->btc_read_4byte(btc, 0xcb4);
2814                 u8tmp  = btc->btc_read_1byte(btc, 0x73);
2815
2816                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2817                             "[BTCoex], (After Ant Setup) 0xcb4 = 0x%x, 0xcbc = 0x%x, 0x73 = 0x%x\n",
2818                             u32tmp1, u32tmp2, u8tmp);
2819                 BTC_TRACE(trace_buf);
2820         }
2821 }
2822
2823 static
2824 u8 halbtc8821c2ant_action_algorithm(struct btc_coexist *btc)
2825 {
2826         struct btc_bt_link_info *bt_link_info = &btc->bt_link_info;
2827         u8 algorithm = BT_8821C_2ANT_COEX_UNDEFINED;
2828         u8 profile_map = 0;
2829
2830         if (bt_link_info->sco_exist)
2831                 profile_map = profile_map | BIT(0);
2832
2833         if (bt_link_info->hid_exist)
2834                 profile_map = profile_map | BIT(1);
2835
2836         if (bt_link_info->a2dp_exist)
2837                 profile_map = profile_map | BIT(2);
2838
2839         if (bt_link_info->pan_exist)
2840                 profile_map = profile_map | BIT(3);
2841
2842         switch (profile_map) {
2843         default:
2844         case 0:
2845                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2846                             "[BTCoex], No BT link exists!!!\n");
2847                 BTC_TRACE(trace_buf);
2848                 algorithm = BT_8821C_2ANT_COEX_UNDEFINED;
2849                 break;
2850         case 1:
2851                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2852                             "[BTCoex], BT Profile = SCO only\n");
2853                 BTC_TRACE(trace_buf);
2854                 algorithm = BT_8821C_2ANT_COEX_SCO;
2855                 break;
2856         case 2:
2857                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2858                             "[BTCoex], BT Profile = HID only\n");
2859                 BTC_TRACE(trace_buf);
2860                 algorithm = BT_8821C_2ANT_COEX_HID;
2861                 break;
2862         case 3:
2863                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2864                             "[BTCoex], BT Profile = SCO + HID ==> HID\n");
2865                 BTC_TRACE(trace_buf);
2866                 algorithm = BT_8821C_2ANT_COEX_HID;
2867                 break;
2868         case 4:
2869                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2870                                     "[BTCoex], BT Profile = A2DP only\n");
2871                 BTC_TRACE(trace_buf);
2872                 algorithm = BT_8821C_2ANT_COEX_A2DP;
2873                 break;
2874         case 5:
2875                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2876                             "[BTCoex], BT Profile = SCO + A2DP ==> HID + A2DP\n");
2877                 BTC_TRACE(trace_buf);
2878                 algorithm = BT_8821C_2ANT_COEX_HID_A2DP;
2879                 break;
2880         case 6:
2881                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2882                             "[BTCoex], BT Profile = HID + A2DP\n");
2883                 BTC_TRACE(trace_buf);
2884                 algorithm = BT_8821C_2ANT_COEX_HID_A2DP;
2885                 break;
2886         case 7:
2887                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2888                             "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID + A2DP\n");
2889                 BTC_TRACE(trace_buf);
2890                 algorithm = BT_8821C_2ANT_COEX_HID_A2DP;
2891                 break;
2892         case 8:
2893                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2894                             "[BTCoex], BT Profile = PAN(EDR) only\n");
2895                 BTC_TRACE(trace_buf);
2896                 algorithm = BT_8821C_2ANT_COEX_PAN;
2897                 break;
2898         case 9:
2899                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2900                             "[BTCoex], BT Profile = SCO + PAN(EDR) ==> HID + PAN(EDR)\n");
2901                 BTC_TRACE(trace_buf);
2902                 algorithm = BT_8821C_2ANT_COEX_PAN_HID;
2903                 break;
2904         case 10:
2905                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2906                             "[BTCoex], BT Profile = HID + PAN(EDR)\n");
2907                 BTC_TRACE(trace_buf);
2908                 algorithm = BT_8821C_2ANT_COEX_PAN_HID;
2909                 break;
2910         case 11:
2911                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2912                             "[BTCoex], BT Profile = SCO + HID + PAN(EDR) ==> HID + PAN(EDR)\n");
2913                 BTC_TRACE(trace_buf);
2914                 algorithm = BT_8821C_2ANT_COEX_PAN_HID;
2915                 break;
2916         case 12:
2917                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2918                             "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
2919                 BTC_TRACE(trace_buf);
2920                 algorithm = BT_8821C_2ANT_COEX_PAN_A2DP;
2921                 break;
2922         case 13:
2923                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2924                             "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> A2DP + PAN(EDR)\n");
2925                 BTC_TRACE(trace_buf);
2926                 algorithm = BT_8821C_2ANT_COEX_PAN_A2DP;
2927                 break;
2928         case 14:
2929                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2930                             "[BTCoex], BT Profile = HID + A2DP + PAN(EDR) ==> A2DP + PAN(EDR)\n");
2931                 BTC_TRACE(trace_buf);
2932                 algorithm = BT_8821C_2ANT_COEX_PAN_A2DP;
2933                 break;
2934         case 15:
2935                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2936                             "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR) ==> A2DP + PAN(EDR)\n");
2937                 BTC_TRACE(trace_buf);
2938                 algorithm = BT_8821C_2ANT_COEX_PAN_A2DP;
2939                 break;
2940         }
2941
2942         return algorithm;
2943 }
2944
2945
2946 static
2947 void halbtc8821c2ant_action_coex_all_off(struct btc_coexist *btc)
2948 {
2949         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
2950         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
2951
2952         halbtc8821c2ant_table(btc, NM_EXCU, 0);
2953         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
2954 }
2955
2956 static
2957 void halbtc8821c2ant_action_bt_whql_test(struct btc_coexist *btc)
2958 {
2959         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
2960         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
2961
2962         halbtc8821c2ant_table(btc, NM_EXCU, 0);
2963         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
2964 }
2965
2966 static
2967 void halbtc8821c2ant_action_bt_inquiry(struct btc_coexist *btc)
2968 {
2969         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
2970         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
2971         boolean wifi_connected = FALSE, wifi_busy = FALSE;
2972
2973         halbtc8821c2ant_set_wl_tx_power(btc, FC_EXCU, 0xd8);
2974         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
2975
2976         btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2977         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2978
2979         if (coex_sta->is_wifi_linkscan_process ||
2980             coex_sta->wifi_high_pri_task1 ||
2981             coex_sta->wifi_high_pri_task2) {
2982
2983                 if (coex_sta->bt_create_connection) {
2984                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2985                                     "[BTCoex], bt page +  wifi hi-pri task\n");
2986                         BTC_TRACE(trace_buf);
2987
2988                         halbtc8821c2ant_table(btc, NM_EXCU, 8);
2989
2990                         if (bt_link_info->a2dp_exist &&
2991                             !bt_link_info->pan_exist)
2992                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 17);
2993                         else if (coex_sta->wifi_high_pri_task1)
2994                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 113);
2995                         else
2996                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 21);
2997                 } else {
2998                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2999                                     "[BTCoex], bt inquiry +  wifi hi-pri task\n");
3000                         BTC_TRACE(trace_buf);
3001
3002                         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3003                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 21);
3004                 }
3005         } else if (wifi_busy) {
3006                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3007                             "[BTCoex], bt inq/page +  wifi busy\n");
3008                 BTC_TRACE(trace_buf);
3009
3010                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3011                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 23);
3012         } else if (wifi_connected) {
3013                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3014                             "[BTCoex], bt inq/page +  wifi connected\n");
3015                 BTC_TRACE(trace_buf);
3016
3017                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3018                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 23);
3019         } else {
3020                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3021                             "[BTCoex], bt inq/page +  wifi not-connected\n");
3022                 BTC_TRACE(trace_buf);
3023                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3024                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3025         }
3026 }
3027
3028 static
3029 void halbtc8821c2ant_action_bt_relink(struct btc_coexist *btc)
3030 {
3031         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3032         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3033
3034         if ((!coex_sta->is_bt_multi_link && !bt_link_info->pan_exist) ||
3035             (bt_link_info->a2dp_exist && bt_link_info->hid_exist)) {
3036                 halbtc8821c2ant_table(btc, NM_EXCU, 5);
3037                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3038         }
3039 }
3040
3041 static
3042 void halbtc8821c2ant_action_bt_idle(struct btc_coexist *btc)
3043 {
3044         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3045         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3046         boolean wifi_busy = FALSE;
3047
3048         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3049
3050         if (!wifi_busy) {
3051                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3052                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 14);
3053         } else {  /* if wl busy */
3054                 if ((coex_sta->bt_ble_scan_type & 0x2) &&
3055                     BT_8821C_2ANT_BSTATUS_NCON_IDLE ==
3056                     coex_dm->bt_status) {
3057                         halbtc8821c2ant_table(btc, NM_EXCU, 14);
3058                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 12);
3059                 } else {
3060                         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3061                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 12);
3062                 }
3063         }
3064
3065         halbtc8821c2ant_set_wl_tx_power(btc, FC_EXCU, 0xd8);
3066         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3067 }
3068
3069 static
3070 void halbtc8821c2ant_action_bt_mr(struct btc_coexist *btc)
3071 {
3072         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
3073                                          &btc->wifi_link_info_8821c_2ant;
3074
3075         if (!wifi_link_info_ext->is_all_under_5g) {
3076                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, NM_EXCU,
3077                                              BT_8821C_2ANT_PHASE_2G);
3078
3079                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3080                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3081         } else {
3082                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, NM_EXCU,
3083                                              BT_8821C_2ANT_PHASE_5G);
3084
3085                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3086                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3087         }
3088 }
3089
3090 /* SCO only or SCO+PAN(HS) */
3091 static
3092 void halbtc8821c2ant_action_sco(struct btc_coexist *btc)
3093 {
3094         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3095         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3096         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3097         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3098         u8 wifi_rssi_state, bt_rssi_state;
3099
3100         wifi_rssi_state =
3101                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3102                                                 coex_sta->wifi_coex_thres, 0);
3103
3104         bt_rssi_state =
3105                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3106                                               coex_sta->bt_coex_thres, 0);
3107
3108         if (BTC_RSSI_HIGH(wifi_rssi_state) && BTC_RSSI_HIGH(bt_rssi_state)) {
3109                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3110                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3111
3112                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3113
3114                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3115
3116                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3117         }  else {
3118                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3119                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3120
3121                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3122
3123                 if (coex_sta->is_bt_multi_link) {
3124                         halbtc8821c2ant_table(btc, NM_EXCU, 7);
3125                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 25);
3126                 } else {
3127                         if (coex_sta->is_esco_mode)
3128                                 halbtc8821c2ant_table(btc, NM_EXCU, 1);
3129                         else  /* 2-Ant free run if SCO mode */
3130                                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3131
3132                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 8);
3133                 }
3134         }
3135 }
3136
3137 static
3138 void halbtc8821c2ant_action_hid(struct btc_coexist *btc)
3139 {
3140         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3141         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3142         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3143         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3144         u8 wifi_rssi_state, bt_rssi_state;
3145         boolean wifi_busy = FALSE;
3146         u32  wifi_bw = 1;
3147
3148         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3149         btc->btc_get(btc, BTC_GET_U4_WIFI_BW,  &wifi_bw);
3150
3151         wifi_rssi_state =
3152                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3153                                                 coex_sta->wifi_coex_thres, 0);
3154
3155         bt_rssi_state =
3156                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3157                                               coex_sta->bt_coex_thres, 0);
3158
3159         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3160             BTC_RSSI_HIGH(bt_rssi_state)) {
3161                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3162                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3163
3164                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3165
3166                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3167
3168                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3169         }  else {
3170                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3171                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3172
3173                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3174
3175                 if (coex_sta->is_hid_low_pri_tx_overhead) {
3176                         halbtc8821c2ant_table(btc, NM_EXCU, 12);
3177                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 108);
3178                 }  else if (coex_sta->is_hid_rcu) {
3179                         halbtc8821c2ant_table(btc, NM_EXCU, 12);
3180
3181                         if (wifi_busy)
3182                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 113);
3183                         else
3184                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 111);
3185                 } else {
3186                         halbtc8821c2ant_table(btc, NM_EXCU, 12);
3187                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 111);
3188                 }
3189         }
3190 }
3191
3192 static
3193 void halbtc8821c2ant_action_a2dpsink(struct btc_coexist *btc)
3194 {
3195         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3196         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3197         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3198         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3199         u8 wifi_rssi_state, bt_rssi_state;
3200
3201         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3202         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3203         u8 wifi_rssi_state2, bt_rssi_state2;
3204         boolean wifi_busy = FALSE;
3205         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3206         boolean ap_enable = FALSE;
3207
3208         btc->btc_get(btc, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &ap_enable);
3209         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3210
3211         wifi_rssi_state =
3212                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3213                                                 coex_sta->wifi_coex_thres, 0);
3214
3215         wifi_rssi_state2 =
3216                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3217                                                 coex_sta->wifi_coex_thres2, 0);
3218
3219         bt_rssi_state =
3220                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3221                                               coex_sta->bt_coex_thres, 0);
3222
3223         bt_rssi_state2 =
3224                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3225                                               coex_sta->bt_coex_thres2, 0);
3226
3227         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3228                 BTC_RSSI_HIGH(bt_rssi_state)) {
3229                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3230                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3231
3232                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3233
3234                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3235
3236                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3237         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3238                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3239                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3240                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3241
3242                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3243
3244                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3245
3246                 if (wifi_busy)
3247                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 1);
3248                 else
3249                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 16);
3250         } else {
3251                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3252                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3253
3254                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3255
3256                 if (ap_enable) {
3257                         halbtc8821c2ant_table(btc, NM_EXCU, 0);
3258                         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3259                 } else {
3260                         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3261                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 115);
3262                 }
3263         }
3264 }
3265
3266 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
3267 static
3268 void halbtc8821c2ant_action_a2dp(struct btc_coexist *btc)
3269 {
3270         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3271         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3272         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3273         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3274         u8 wifi_rssi_state, bt_rssi_state;
3275
3276         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3277         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3278         u8 wifi_rssi_state2, bt_rssi_state2;
3279
3280         static u8       prewifi_rssi_state3 = BTC_RSSI_STATE_LOW;
3281         static u8       pre_bt_rssi_state3 = BTC_RSSI_STATE_LOW;
3282         u8 wifi_rssi_state3, bt_rssi_state3;
3283
3284         boolean wifi_busy = FALSE;
3285         u8 iot_peer = BTC_IOT_PEER_UNKNOWN;
3286
3287         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3288
3289         wifi_rssi_state =
3290                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3291                                                 coex_sta->wifi_coex_thres, 0);
3292
3293         wifi_rssi_state2 =
3294                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3295                                                 coex_sta->wifi_coex_thres2, 0);
3296
3297         wifi_rssi_state3 =
3298                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state3, 2,
3299                                                 45, 0);
3300
3301         bt_rssi_state =
3302                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3303                                               coex_sta->bt_coex_thres, 0);
3304
3305         bt_rssi_state2 =
3306                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3307                                               coex_sta->bt_coex_thres2, 0);
3308
3309         bt_rssi_state3 =
3310                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state3, 2,
3311                                               50, 0);
3312
3313         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3314             BTC_RSSI_HIGH(bt_rssi_state)) {
3315                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3316                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3317
3318                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3319
3320                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3321
3322                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3323         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3324                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3325                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3326                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3327
3328                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3329
3330                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3331
3332                 if (wifi_busy)
3333                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 1);
3334                 else
3335                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 16);
3336         } else {
3337                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3338                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3339                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3340
3341                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3342
3343                 if (BTC_RSSI_HIGH(wifi_rssi_state3))
3344                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 119);
3345                 else
3346                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 101);
3347         }
3348 }
3349
3350 static
3351 void halbtc8821c2ant_action_pan(struct btc_coexist *btc)
3352 {
3353         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3354         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3355         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3356         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3357         u8 wifi_rssi_state, bt_rssi_state;
3358
3359         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3360         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3361         u8 wifi_rssi_state2, bt_rssi_state2;
3362         boolean wifi_busy = FALSE;
3363
3364         static u8       prewifi_rssi_state3 = BTC_RSSI_STATE_LOW;
3365         static u8       pre_bt_rssi_state3 = BTC_RSSI_STATE_LOW;
3366         u8 wifi_rssi_state3, bt_rssi_state3;
3367
3368         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3369
3370         wifi_rssi_state =
3371                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3372                                                 coex_sta->wifi_coex_thres, 0);
3373
3374         wifi_rssi_state2 =
3375                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3376                                                 coex_sta->wifi_coex_thres2, 0);
3377
3378         wifi_rssi_state3 =
3379                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state3, 2,
3380                                                 58, 0);
3381
3382         bt_rssi_state =
3383                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3384                                               coex_sta->bt_coex_thres, 0);
3385
3386         bt_rssi_state2 =
3387                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3388                                               coex_sta->bt_coex_thres2, 0);
3389
3390         bt_rssi_state3 =
3391                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state3, 2,
3392                                               47, 0);
3393 #if 0
3394         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3395         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3396
3397         coex_dm->is_switch_to_1dot5_ant = FALSE;
3398
3399         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3400
3401         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 21);
3402 #endif
3403
3404 #if 1
3405         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3406             BTC_RSSI_HIGH(bt_rssi_state)) {
3407                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3408                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3409
3410                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3411
3412                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3413
3414                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3415         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3416                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3417                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3418                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3419
3420                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3421
3422                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3423
3424                 if (wifi_busy)
3425                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 3);
3426                 else
3427                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 4);
3428         } else {
3429                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3430                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3431
3432                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3433
3434                 /* for Lenovo CPT_For_WiFi OPP test  */
3435                 if (btc->board_info.customer_id == RT_CID_LENOVO_CHINA &&
3436                     BTC_RSSI_HIGH(wifi_rssi_state3) && wifi_busy) {
3437                         halbtc8821c2ant_table(btc, NM_EXCU, 7);
3438
3439                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 103);
3440                 } else {
3441                         halbtc8821c2ant_table(btc, NM_EXCU, 7);
3442
3443                         if (wifi_busy)
3444                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 103);
3445                         else
3446                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 104);
3447                 }
3448         }
3449 #endif
3450 }
3451
3452 static
3453 void halbtc8821c2ant_action_hid_a2dp(struct btc_coexist *btc)
3454 {
3455         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3456         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3457         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3458         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3459         u8 wifi_rssi_state, bt_rssi_state;
3460
3461         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3462         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3463         u8 wifi_rssi_state2, bt_rssi_state2;
3464         static u8       prewifi_rssi_state3 = BTC_RSSI_STATE_LOW;
3465         u8 wifi_rssi_state3;
3466         boolean wifi_busy = FALSE;
3467
3468         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3469
3470         wifi_rssi_state =
3471                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3472                                                 coex_sta->wifi_coex_thres, 0);
3473
3474         wifi_rssi_state2 =
3475                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3476                                                 coex_sta->wifi_coex_thres2, 0);
3477
3478         wifi_rssi_state3 =
3479                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state3, 2,
3480                                                 45, 0);
3481
3482         bt_rssi_state =
3483                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3484                                               coex_sta->bt_coex_thres, 0);
3485
3486         bt_rssi_state2 =
3487                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3488                                               coex_sta->bt_coex_thres2, 0);
3489
3490         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3491             BTC_RSSI_HIGH(bt_rssi_state)) {
3492                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3493                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3494
3495                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3496
3497                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3498                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3499         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3500                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3501                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3502                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3503
3504                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3505
3506                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3507
3508                 if (wifi_busy)
3509                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 1);
3510                 else
3511                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 16);
3512         } else {
3513                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3514                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3515
3516                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3517
3518                 halbtc8821c2ant_table(btc, NM_EXCU, 12);
3519
3520                 if (BTC_RSSI_HIGH(wifi_rssi_state3))
3521                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 119);
3522                 else
3523                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 109);
3524         }
3525 }
3526
3527 static
3528 void halbtc8821c2ant_action_a2dp_pan_hs(struct btc_coexist *btc)
3529 {
3530         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3531         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3532         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3533         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3534         u8 wifi_rssi_state, bt_rssi_state;
3535
3536         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3537         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3538         u8 wifi_rssi_state2, bt_rssi_state2;
3539         boolean wifi_busy = FALSE;
3540
3541         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3542
3543         wifi_rssi_state =
3544                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3545                                                 coex_sta->wifi_coex_thres, 0);
3546
3547         wifi_rssi_state2 =
3548                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3549                                                 coex_sta->wifi_coex_thres2, 0);
3550
3551         bt_rssi_state =
3552                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3553                                               coex_sta->bt_coex_thres, 0);
3554
3555         bt_rssi_state2 =
3556                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3557                                               coex_sta->bt_coex_thres2, 0);
3558
3559         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3560             BTC_RSSI_HIGH(bt_rssi_state)) {
3561                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3562                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3563
3564                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3565
3566                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3567
3568                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3569         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3570                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3571                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3572                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3573
3574                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3575
3576                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3577
3578                 if (wifi_busy) {
3579
3580                         if ((coex_sta->a2dp_bit_pool > 40) &&
3581                             (coex_sta->a2dp_bit_pool < 255))
3582                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 7);
3583                         else
3584                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 5);
3585                 } else
3586                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 6);
3587
3588         } else {
3589                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3590                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3591
3592                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3593
3594                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3595                 if (wifi_busy) {
3596                         if ((coex_sta->a2dp_bit_pool > 40) &&
3597                             (coex_sta->a2dp_bit_pool < 255))
3598                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 107);
3599                         else
3600                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 105);
3601                 } else
3602                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 106);
3603         }
3604 }
3605
3606 /* PAN(EDR)+A2DP */
3607 static
3608 void halbtc8821c2ant_action_pan_a2dp(struct btc_coexist *btc)
3609 {
3610         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3611         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3612         static u8       prewifi_rssi_state3 = BTC_RSSI_STATE_LOW;
3613         static u8       pre_bt_rssi_state3 = BTC_RSSI_STATE_LOW;
3614         u8 wifi_rssi_state3, bt_rssi_state3;
3615
3616         boolean wifi_busy = FALSE;
3617         u8 iot_peer = BTC_IOT_PEER_UNKNOWN;
3618
3619         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3620
3621         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3622         btc->btc_get(btc, BTC_GET_U1_IOT_PEER, &iot_peer);
3623
3624         if (!wifi_busy)
3625                 wifi_busy = coex_sta->gl_wifi_busy;
3626
3627         wifi_rssi_state3 =
3628                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state3, 2,
3629                                                 42, 0);
3630         bt_rssi_state3 =
3631                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state3, 2,
3632                                               45, 0);
3633
3634         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3635
3636         coex_dm->is_switch_to_1dot5_ant = TRUE;
3637
3638         /* for Lenovo coex test case    */
3639         if (btc->board_info.customer_id == RT_CID_LENOVO_CHINA &&
3640             coex_sta->scan_ap_num <= 10 &&
3641             iot_peer == BTC_IOT_PEER_ATHEROS) {
3642                 /* for CPT_for_WiFi   */
3643                 if (BTC_RSSI_LOW(wifi_rssi_state3)) {
3644                         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 20);
3645                         if (wifi_busy) {
3646                                 halbtc8821c2ant_table(btc, NM_EXCU, 7);
3647                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 105);
3648                         } else {
3649                                 halbtc8821c2ant_table(btc, NM_EXCU, 7);
3650                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 107);
3651                         }
3652                 } else {  /* for CPT_for_BT   */
3653                         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3654                         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3655                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 107);
3656                 }
3657         } else {
3658                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3659                 halbtc8821c2ant_table(btc, NM_EXCU, 8);
3660
3661                 if (wifi_busy)
3662                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 107);
3663                 else
3664                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 106);
3665         }
3666 }
3667
3668 static
3669 void halbtc8821c2ant_action_pan_hid(struct btc_coexist *btc)
3670 {
3671         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3672         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3673         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3674         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3675         u8 wifi_rssi_state, bt_rssi_state;
3676
3677         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3678         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3679         u8 wifi_rssi_state2, bt_rssi_state2;
3680         boolean wifi_busy = FALSE;
3681
3682         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3683
3684         wifi_rssi_state =
3685                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3686                                                 coex_sta->wifi_coex_thres, 0);
3687
3688         wifi_rssi_state2 =
3689                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3690                                                 coex_sta->wifi_coex_thres2, 0);
3691
3692         bt_rssi_state =
3693                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3694                                               coex_sta->bt_coex_thres, 0);
3695
3696         bt_rssi_state2 =
3697                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3698                                               coex_sta->bt_coex_thres2, 0);
3699
3700         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3701             BTC_RSSI_HIGH(bt_rssi_state)) {
3702                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3703                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3704
3705                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3706
3707                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3708                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3709         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3710                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3711                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3712                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3713
3714                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3715
3716                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3717
3718                 if (wifi_busy)
3719                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 3);
3720                 else
3721                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 4);
3722         } else {
3723                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3724                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3725
3726                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3727
3728                 halbtc8821c2ant_table(btc, NM_EXCU, 6);
3729
3730                 if (wifi_busy)
3731                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 103);
3732                 else
3733                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 104);
3734         }
3735 }
3736
3737 /* HID+A2DP+PAN(EDR) */
3738 static
3739 void halbtc8821c2ant_action_hid_a2dp_pan(struct btc_coexist *btc)
3740 {
3741         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3742         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3743         static u8       prewifi_rssi_state = BTC_RSSI_STATE_LOW;
3744         static u8       pre_bt_rssi_state = BTC_RSSI_STATE_LOW;
3745         u8 wifi_rssi_state, bt_rssi_state;
3746
3747         static u8       prewifi_rssi_state2 = BTC_RSSI_STATE_LOW;
3748         static u8       pre_bt_rssi_state2 = BTC_RSSI_STATE_LOW;
3749         u8 wifi_rssi_state2, bt_rssi_state2;
3750         boolean wifi_busy = FALSE;
3751
3752         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3753
3754         wifi_rssi_state =
3755                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state, 2,
3756                                                 coex_sta->wifi_coex_thres, 0);
3757
3758         wifi_rssi_state2 =
3759                 halbtc8821c2ant_wifi_rssi_state(btc, &prewifi_rssi_state2, 2,
3760                                                 coex_sta->wifi_coex_thres2, 0);
3761
3762         bt_rssi_state =
3763                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state, 2,
3764                                               coex_sta->bt_coex_thres, 0);
3765
3766         bt_rssi_state2 =
3767                 halbtc8821c2ant_bt_rssi_state(btc, &pre_bt_rssi_state2, 2,
3768                                               coex_sta->bt_coex_thres2, 0);
3769
3770         if (BTC_RSSI_HIGH(wifi_rssi_state) &&
3771             BTC_RSSI_HIGH(bt_rssi_state)) {
3772                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3773                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3774
3775                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3776
3777                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3778                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3779         } else if (BTC_RSSI_HIGH(wifi_rssi_state2) &&
3780                    BTC_RSSI_HIGH(bt_rssi_state2)) {
3781                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xc8);
3782                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 2);
3783
3784                 coex_dm->is_switch_to_1dot5_ant = FALSE;
3785
3786                 halbtc8821c2ant_table(btc, NM_EXCU, 4);
3787
3788                 if (wifi_busy) {
3789                         if ((coex_sta->a2dp_bit_pool > 40 &&
3790                              coex_sta->a2dp_bit_pool < 255) ||
3791                             !coex_sta->is_A2DP_3M)
3792                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 7);
3793                         else
3794                                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 5);
3795                 } else
3796                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 6);
3797         } else {
3798                 halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3799                 halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3800
3801                 coex_dm->is_switch_to_1dot5_ant = TRUE;
3802
3803                 halbtc8821c2ant_table(btc, NM_EXCU, 12);
3804
3805                 if (wifi_busy)
3806                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 107);
3807                 else
3808                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 106);
3809         }
3810 }
3811
3812 static
3813 void halbtc8821c2ant_action_wifi_under5g(struct btc_coexist *btc)
3814 {
3815         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3816         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3817
3818         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, NM_EXCU,
3819                                      BT_8821C_2ANT_PHASE_5G);
3820         /* fw all off */
3821         halbtc8821c2ant_table(btc, NM_EXCU, 0);
3822         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3823 }
3824
3825 static
3826 void halbtc8821c2ant_action_wifi_native_lps(struct btc_coexist *btc)
3827 {
3828         halbtc8821c2ant_table(btc, NM_EXCU, 2);
3829         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3830 }
3831
3832 static
3833 void halbtc8821c2ant_action_wifi_linkscan(struct btc_coexist *btc)
3834 {
3835         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3836
3837         halbtc8821c2ant_set_wl_tx_power(btc, FC_EXCU, 0xd8);
3838         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3839
3840         halbtc8821c2ant_table(btc, NM_EXCU, 8);
3841
3842         if (bt_link_info->pan_exist)
3843                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 22);
3844         else if (bt_link_info->a2dp_exist)
3845                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 16);
3846         else
3847                 halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 21);
3848 }
3849
3850 static
3851 void halbtc8821c2ant_action_wifi_not_connected(struct btc_coexist *btc)
3852 {
3853         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3854         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3855
3856         halbtc8821c2ant_table(btc, NM_EXCU, 0);
3857         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3858 }
3859
3860 static
3861 void halbtc8821c2ant_action_wifi_connected(struct btc_coexist *btc)
3862 {
3863         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3864         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
3865         u8 algorithm = 0;
3866         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3867
3868         algorithm = halbtc8821c2ant_action_algorithm(btc);
3869
3870         switch (algorithm) {
3871
3872         case BT_8821C_2ANT_COEX_SCO:
3873                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3874                             "[BTCoex], Action 2-Ant, algorithm = SCO.\n");
3875                 BTC_TRACE(trace_buf);
3876                 halbtc8821c2ant_action_sco(btc);
3877                 break;
3878         case BT_8821C_2ANT_COEX_HID:
3879                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3880                             "[BTCoex], Action 2-Ant, algorithm = HID.\n");
3881                 BTC_TRACE(trace_buf);
3882                 halbtc8821c2ant_action_hid(btc);
3883                 break;
3884         case BT_8821C_2ANT_COEX_A2DP:
3885                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3886                             "[BTCoex], Action 2-Ant, algorithm = A2DP.\n");
3887                 BTC_TRACE(trace_buf);
3888
3889                 /* for A2DP + OPP test but BTinfo is
3890                  * A2DP only in Lenovo test case
3891                  */
3892                 if (coex_sta->is_bt_multi_link && coex_sta->hid_pair_cnt == 0)
3893                         halbtc8821c2ant_action_pan_a2dp(btc);
3894                 else
3895                         halbtc8821c2ant_action_a2dp(btc);
3896                 break;
3897         case BT_8821C_2ANT_COEX_A2DPSINK:
3898                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3899                             "[BTCoex], Action 2-Ant, algorithm = A2DP Sink.\n");
3900                 BTC_TRACE(trace_buf);
3901                 halbtc8821c2ant_action_a2dpsink(btc);
3902                 break;
3903         case BT_8821C_2ANT_COEX_A2DP_PANHS:
3904                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3905                             "[BTCoex], Action 2-Ant, algorithm = A2DP+PAN(HS).\n");
3906                 BTC_TRACE(trace_buf);
3907                 halbtc8821c2ant_action_a2dp_pan_hs(btc);
3908                 break;
3909         case BT_8821C_2ANT_COEX_PAN:
3910                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3911                             "[BTCoex], Action 2-Ant, algorithm = PAN(EDR).\n");
3912                 BTC_TRACE(trace_buf);
3913                 halbtc8821c2ant_action_pan(btc);
3914                 break;
3915         case BT_8821C_2ANT_COEX_PAN_A2DP:
3916                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3917                             "[BTCoex], Action 2-Ant, algorithm = PAN+A2DP.\n");
3918                 BTC_TRACE(trace_buf);
3919                 halbtc8821c2ant_action_pan_a2dp(btc);
3920                 break;
3921         case BT_8821C_2ANT_COEX_PAN_HID:
3922                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3923                             "[BTCoex], Action 2-Ant, algorithm = PAN(EDR)+HID.\n");
3924                 BTC_TRACE(trace_buf);
3925                 halbtc8821c2ant_action_pan_hid(btc);
3926                 break;
3927         case BT_8821C_2ANT_COEX_HID_A2DP_PAN:
3928                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3929                             "[BTCoex], Action 2-Ant, algorithm = HID+A2DP+PAN.\n");
3930                 BTC_TRACE(trace_buf);
3931                 halbtc8821c2ant_action_hid_a2dp_pan(btc);
3932                 break;
3933         case BT_8821C_2ANT_COEX_HID_A2DP:
3934                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3935                             "[BTCoex], Action 2-Ant, algorithm = HID+A2DP.\n");
3936                 BTC_TRACE(trace_buf);
3937                 halbtc8821c2ant_action_hid_a2dp(btc);
3938                 break;
3939         case BT_8821C_2ANT_COEX_NOPROFILEBUSY:
3940                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3941                             "[BTCoex], Action 2-Ant, algorithm = No-Profile busy.\n");
3942                 BTC_TRACE(trace_buf);
3943                 halbtc8821c2ant_action_bt_idle(btc);
3944                 break;
3945         default:
3946                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3947                             "[BTCoex], Action 2-Ant, algorithm = coexist All Off!!\n");
3948                 BTC_TRACE(trace_buf);
3949                 halbtc8821c2ant_action_coex_all_off(btc);
3950                 break;
3951         }
3952
3953         coex_dm->cur_algorithm = algorithm;
3954 }
3955
3956 static
3957 void halbtc8821c2ant_action_wifi_multiport25g(struct btc_coexist *btc)
3958 {
3959         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3960         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3961
3962         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
3963         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
3964
3965         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_BTCQDDR, TRUE);
3966
3967         if (coex_sta->is_setup_link || coex_sta->bt_relink_downcount != 0) {
3968                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3969                             "[BTCoex], wifi_multiport25g(), BT Relink!!\n");
3970                 BTC_TRACE(trace_buf);
3971
3972                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
3973                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3974         } else if (coex_sta->c2h_bt_inquiry_page) {
3975                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3976                             "[BTCoex], wifi_multiport25g(), BT Inq-Page!!\n");
3977                 BTC_TRACE(trace_buf);
3978
3979                 halbtc8821c2ant_table(btc, NM_EXCU, 11);
3980                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3981         } else {
3982                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3983                             "[BTCoex], wifi_multiport25g(), BT idle or busy!!\n");
3984                 BTC_TRACE(trace_buf);
3985
3986                 halbtc8821c2ant_table(btc, NM_EXCU, 11);
3987                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
3988         }
3989 }
3990
3991 static
3992 void halbtc8821c2ant_action_wifi_multiport2g(struct btc_coexist *btc)
3993 {
3994         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
3995         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
3996         struct btc_multi_port_tdma_info multiport_tdma_para;
3997         u32 traffic_dir;
3998
3999         btc->btc_get(btc, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION, &traffic_dir);
4000
4001         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
4002         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
4003
4004         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_BTCQDDR, TRUE);
4005
4006         if (coex_sta->is_setup_link || coex_sta->bt_relink_downcount != 0) {
4007                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4008                             "[BTCoex], wifi_multiport2g, BT Relink!!\n");
4009                 BTC_TRACE(trace_buf);
4010
4011                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
4012                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4013         } else if (coex_sta->c2h_bt_inquiry_page) {
4014                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4015                             "[BTCoex], wifi_multiport2g, BT Inq-Page!!\n");
4016                 BTC_TRACE(trace_buf);
4017
4018                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
4019                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4020         } else if (coex_sta->num_of_profile == 0) {
4021                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4022                             "[BTCoex], wifi_multiport2g, BT idle!!\n");
4023                 BTC_TRACE(trace_buf);
4024
4025                 /* for P2P-GO only A2DP sink */
4026                 if (btc->wifi_link_info.link_mode == BTC_LINK_ONLY_GO &&
4027                     traffic_dir == BTC_WIFI_TRAFFIC_RX) {
4028                         halbtc8821c2ant_table(btc, NM_EXCU, 15);
4029                         halbtc8821c2ant_tdma(btc, NM_EXCU, TRUE, 11);
4030                 } else {
4031                         halbtc8821c2ant_table(btc, NM_EXCU, 5);
4032                         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4033                 }
4034         } else if (coex_sta->is_wifi_linkscan_process) {
4035                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4036                             "[BTCoex], wifi_multiport2g, WL scan!!\n");
4037                 BTC_TRACE(trace_buf);
4038
4039                 halbtc8821c2ant_action_wifi_linkscan(btc);
4040         } else {
4041                 if (!coex_sta->is_bt_multi_link &&
4042                     (bt_link_info->sco_exist || bt_link_info->hid_exist ||
4043                      coex_sta->is_hid_rcu)) {
4044                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4045                                     "[BTCoex], wifi_multiport2g, 2G multi-port + BT HID/HFP/RCU!!\n");
4046                         BTC_TRACE(trace_buf);
4047
4048                         halbtc8821c2ant_table(btc, NM_EXCU, 5);
4049                         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4050                 } else {
4051                         switch (btc->wifi_link_info.link_mode) {
4052                         #if 0
4053                         case BTC_LINK_2G_SCC_GO_STA:
4054                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4055                                             "[BTCoex], wifi_multiport2g, 2G_SCC_GO_STA + BT busy!!\n");
4056                                 BTC_TRACE(trace_buf);
4057
4058                                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
4059                                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4060                                 break;
4061                         case BTC_LINK_ONLY_GO:
4062                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4063                                             "[BTCoex], wifi_singleport2g, Only_P2PGO with client-join + BT busy!!\n");
4064                                 BTC_TRACE(trace_buf);
4065
4066                                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
4067                                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4068                                 break;
4069                         #endif
4070                         default:
4071                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4072                                             "[BTCoex], wifi_multiport2g, Other multi-port + BT busy!!\n");
4073                                 BTC_TRACE(trace_buf);
4074
4075                                 halbtc8821c2ant_table(btc, NM_EXCU, 0);
4076                                 halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4077                                 break;
4078                         }
4079                 }
4080         }
4081 }
4082
4083 static
4084 void halbtc8821c2ant_run_coex(struct btc_coexist *btc,  u8 reason)
4085 {
4086         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4087         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
4088         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
4089                                          &btc->wifi_link_info_8821c_2ant;
4090         boolean wifi_connected = FALSE, wifi_32k = FALSE;
4091         boolean scan = FALSE, link = FALSE, roam = FALSE, under_4way = FALSE;
4092
4093         btc->btc_get(btc, BTC_GET_BL_WIFI_SCAN, &scan);
4094         btc->btc_get(btc, BTC_GET_BL_WIFI_LINK, &link);
4095         btc->btc_get(btc, BTC_GET_BL_WIFI_ROAM, &roam);
4096         btc->btc_get(btc, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &under_4way);
4097         btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
4098         btc->btc_get(btc, BTC_GET_BL_WIFI_LW_PWR_STATE, &wifi_32k);
4099
4100         if (scan || link || roam || under_4way ||
4101             reason == BT_8821C_2ANT_RSN_2GSCANSTART ||
4102             reason == BT_8821C_2ANT_RSN_2GSWITCHBAND ||
4103             reason == BT_8821C_2ANT_RSN_2GCONSTART ||
4104             reason == BT_8821C_2ANT_RSN_2GSPECIALPKT) {
4105                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4106                             "[BTCoex], scan = %d, link = %d, roam = %d 4way = %d!!!\n",
4107                             scan, link, roam, under_4way);
4108                 BTC_TRACE(trace_buf);
4109                 coex_sta->is_wifi_linkscan_process = TRUE;
4110         } else {
4111                 coex_sta->is_wifi_linkscan_process = FALSE;
4112         }
4113
4114         /* update wifi_link_info_ext variable */
4115         halbtc8821c2ant_update_wifi_link_info(btc, reason);
4116
4117         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4118                     "[BTCoex], RunCoexistMechanism()===> reason = %d\n",
4119                     reason);
4120         BTC_TRACE(trace_buf);
4121
4122         if (btc->manual_control) {
4123                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4124                             "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
4125                 BTC_TRACE(trace_buf);
4126                 return;
4127         }
4128
4129         if (btc->stop_coex_dm) {
4130                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4131                            "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
4132                 BTC_TRACE(trace_buf);
4133                 return;
4134         }
4135
4136         if (coex_sta->under_ips) {
4137                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4138                             "[BTCoex], RunCoexistMechanism(), return for wifi is under IPS !!!\n");
4139                 BTC_TRACE(trace_buf);
4140                 return;
4141         }
4142
4143         if (coex_sta->under_lps && wifi_32k) {
4144                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4145                             "[BTCoex], RunCoexistMechanism(), return for wifi is under LPS-32K !!!\n");
4146                 BTC_TRACE(trace_buf);
4147                 return;
4148         }
4149
4150         if (!coex_sta->run_time_state) {
4151                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4152                             "[BTCoex], return for run_time_state = FALSE !!!\n");
4153                 BTC_TRACE(trace_buf);
4154                 return;
4155         }
4156
4157         if (coex_sta->freeze_coexrun_by_btinfo && !coex_sta->is_setup_link) {
4158                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4159                             "[BTCoex], return for freeze_coexrun_by_btinfo\n");
4160                 BTC_TRACE(trace_buf);
4161                 return;
4162         }
4163
4164         coex_sta->coex_run_cnt++;
4165
4166         if (coex_sta->msft_mr_exist && wifi_connected) {
4167                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4168                             "[BTCoex], RunCoexistMechanism(), microsoft MR!!\n");
4169                 BTC_TRACE(trace_buf);
4170
4171                 coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_BTMR;
4172                 halbtc8821c2ant_action_bt_mr(btc);
4173                 return;
4174         }
4175
4176         if (wifi_link_info_ext->is_all_under_5g) {
4177                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4178                             "[BTCoex], WiFi is under 5G!!!\n");
4179                 BTC_TRACE(trace_buf);
4180
4181                 coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_5G;
4182                 halbtc8821c2ant_action_wifi_under5g(btc);
4183                 return;
4184         }
4185
4186         if (wifi_link_info_ext->is_mcc_25g) { /* not iclude scan action */
4187                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4188                             "[BTCoex], WiFi is under mcc dual-band!!!\n");
4189                 BTC_TRACE(trace_buf);
4190
4191                 coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_25GMPORT;
4192                 halbtc8821c2ant_action_wifi_multiport25g(btc);
4193                 return;
4194         }
4195
4196         if (wifi_link_info_ext->num_of_active_port > 1 ||
4197             (btc->wifi_link_info.link_mode == BTC_LINK_ONLY_GO &&
4198             !btc->wifi_link_info.bhotspot &&
4199             btc->wifi_link_info.bany_client_join_go)) {
4200                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4201                             "[BTCoex], WiFi is under scc-2g/mcc-2g/p2pGO-only!!!\n");
4202                 BTC_TRACE(trace_buf);
4203
4204                 if (btc->wifi_link_info.link_mode ==
4205                                                         BTC_LINK_ONLY_GO)
4206                         coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_2GGO;
4207                 else
4208                         coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_2GMPORT;
4209                 halbtc8821c2ant_action_wifi_multiport2g(btc);
4210                 return;
4211         }
4212
4213         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4214                     "[BTCoex], WiFi is single-port 2G!!!\n");
4215         BTC_TRACE(trace_buf);
4216
4217         coex_sta->wl_coex_mode = BT_8821C_2ANT_WLINK_2G1PORT;
4218
4219         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, NM_EXCU,
4220                                      BT_8821C_2ANT_PHASE_2G);
4221
4222         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_BTCQDDR, TRUE);
4223
4224         if (coex_sta->bt_disabled) {
4225                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4226                             "[BTCoex], BT is disabled !!!\n");
4227                 BTC_TRACE(trace_buf);
4228                 halbtc8821c2ant_action_coex_all_off(btc);
4229                 return;
4230         }
4231
4232         if (coex_sta->under_lps && !coex_sta->force_lps_ctrl &&
4233             !coex_sta->acl_busy) {
4234                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4235                             "[BTCoex], RunCoexistMechanism(), wifi is under LPS !!!\n");
4236                 BTC_TRACE(trace_buf);
4237                 halbtc8821c2ant_action_wifi_native_lps(btc);
4238                 return;
4239         }
4240
4241         if (coex_sta->bt_whck_test) {
4242                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4243                             "[BTCoex], BT is under WHCK TEST!!!\n");
4244                 BTC_TRACE(trace_buf);
4245                 halbtc8821c2ant_action_bt_whql_test(btc);
4246                 return;
4247         }
4248
4249         if (coex_sta->is_setup_link || coex_sta->bt_relink_downcount != 0) {
4250                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4251                             "[BTCoex], BT is re-link !!!\n");
4252                 BTC_TRACE(trace_buf);
4253                 halbtc8821c2ant_action_bt_relink(btc);
4254                 return;
4255         }
4256
4257         if (coex_sta->c2h_bt_inquiry_page) {
4258                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4259                             "[BTCoex], BT is under inquiry/page scan !!\n");
4260                 BTC_TRACE(trace_buf);
4261                 halbtc8821c2ant_action_bt_inquiry(btc);
4262                 return;
4263         }
4264
4265         if (coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_NCON_IDLE ||
4266             coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_CON_IDLE) {
4267                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4268                             "############# [BTCoex],  BT Is idle\n");
4269                 BTC_TRACE(trace_buf);
4270                 halbtc8821c2ant_action_bt_idle(btc);
4271                 return;
4272         }
4273
4274         if (coex_sta->is_wifi_linkscan_process) {
4275                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4276                             "[BTCoex], wifi is under linkscan process!!\n");
4277                 BTC_TRACE(trace_buf);
4278                 halbtc8821c2ant_action_wifi_linkscan(btc);
4279                 return;
4280         }
4281
4282         if (wifi_connected) {
4283                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4284                             "[BTCoex], wifi is under connected!!\n");
4285                 BTC_TRACE(trace_buf);
4286
4287                 halbtc8821c2ant_action_wifi_connected(btc);
4288         } else {
4289                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4290                             "[BTCoex], wifi is under not-connected!!\n");
4291                 BTC_TRACE(trace_buf);
4292
4293                 halbtc8821c2ant_action_wifi_not_connected(btc);
4294         }
4295 }
4296
4297 static
4298 void halbtc8821c2ant_init_coex_dm(struct btc_coexist *btc)
4299 {
4300         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4301         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
4302
4303         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4304                     "[BTCoex], Coex Mechanism Init!!\n");
4305         BTC_TRACE(trace_buf);
4306
4307         halbtc8821c2ant_low_penalty_ra(btc, NM_EXCU, FALSE, 0);
4308
4309         halbtc8821c2ant_set_wl_tx_power(btc, NM_EXCU, 0xd8);
4310         halbtc8821c2ant_set_bt_tx_power(btc, NM_EXCU, 0);
4311
4312         coex_sta->pop_event_cnt = 0;
4313         coex_sta->cnt_remote_name_req = 0;
4314         coex_sta->cnt_reinit = 0;
4315         coex_sta->cnt_setup_link = 0;
4316         coex_sta->cnt_ign_wlan_act = 0;
4317         coex_sta->cnt_page = 0;
4318         coex_sta->cnt_role_switch = 0;
4319         coex_sta->switch_band_notify_to = BTC_NOT_SWITCH;
4320         coex_dm->setting_tdma = FALSE;
4321
4322         halbtc8821c2ant_table(btc, NM_EXCU, 0);
4323
4324         /* fw all off */
4325         halbtc8821c2ant_tdma(btc, NM_EXCU, FALSE, 0);
4326
4327         halbtc8821c2ant_query_bt_info(btc);
4328 }
4329
4330 static
4331 void halbtc8821c2ant_init_hw_config(struct btc_coexist *btc, boolean wifi_only)
4332 {
4333         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4334         u8 u8tmp = 0;
4335         u32 vendor;
4336         u32 u32tmp0 = 0, u32tmp1 = 0, u32tmp2 = 0, u32tmp3 = 0;
4337         u8 i;
4338
4339         u32tmp3 = btc->btc_read_4byte(btc, 0xcb4);
4340         u32tmp1 = halbtc8821c2ant_read_indirect_reg(btc, 0x38);
4341         u32tmp2 = halbtc8821c2ant_read_indirect_reg(btc, 0x54);
4342
4343         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4344                     "[BTCoex], (Before Init HW config) 0xcb4 = 0x%x, 0x38= 0x%x, 0x54= 0x%x\n",
4345                     u32tmp3, u32tmp1, u32tmp2);
4346         BTC_TRACE(trace_buf);;
4347
4348         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4349                     "[BTCoex], 2Ant Init HW Config!!\n");
4350         BTC_TRACE(trace_buf);
4351
4352 #if 0
4353         coex_sta->bt_coex_supported_feature = 0;
4354         coex_sta->bt_coex_supported_version = 0;
4355         coex_sta->bt_ble_scan_type = 0;
4356         coex_sta->bt_ble_scan_para[0] = 0;
4357         coex_sta->bt_ble_scan_para[1] = 0;
4358         coex_sta->bt_ble_scan_para[2] = 0;
4359 #endif
4360         coex_sta->bt_reg_vendor_ac = 0xffff;
4361         coex_sta->bt_reg_vendor_ae = 0xffff;
4362         coex_sta->isolation_btween_wb = BT_8821C_2ANT_DEFAULT_ISOLATION;
4363         coex_sta->gnt_error_cnt = 0;
4364         coex_sta->bt_relink_downcount = 0;
4365         coex_sta->is_set_ps_state_fail = FALSE;
4366         coex_sta->cnt_set_ps_state_fail = 0;
4367         coex_sta->wl_rx_rate = BTC_UNKNOWN;
4368         coex_sta->coex_run_cnt = 0;
4369
4370         for (i = 0; i <= 9; i++)
4371                 coex_sta->bt_afh_map[i] = 0;
4372
4373         /* 0xf0[15:12] --> Chip Cut information */
4374         coex_sta->cut_version = (btc->btc_read_1byte(btc, 0xf1) & 0xf0) >> 4;
4375
4376         coex_sta->dis_ver_info_cnt = 0;
4377
4378 #if 0  /* HW antenna diversity for test */
4379         halbtc8821c2ant_set_antdiv_hwsw(btc, NM_EXCU, TRUE);
4380 #endif
4381
4382         halbtc8821c2ant_coex_switch_thres(btc, coex_sta->isolation_btween_wb);
4383          /* enable TBTT nterrupt */
4384         btc->btc_write_1byte_bitmask(btc, 0x550, 0x8, 0x1);
4385
4386         /* BT report packet sample rate  */
4387         btc->btc_write_1byte(btc, 0x790, 0x5);
4388
4389         /* Init 0x778 = 0x1 for 2-Ant */
4390         btc->btc_write_1byte(btc, 0x778, 0x1);
4391
4392         /* Enable PTA (3-wire function form BT side) */
4393         btc->btc_write_1byte_bitmask(btc, 0x40, 0x20, 0x1);
4394         btc->btc_write_1byte_bitmask(btc, 0x41, 0x02, 0x1);
4395
4396         /* Enable PTA (tx/rx signal form WiFi side) */
4397         btc->btc_write_1byte_bitmask(btc, 0x4c6, 0x10, 0x1);
4398
4399         /* set GNT_BT=1 for coex table select both */
4400         btc->btc_write_1byte_bitmask(btc, 0x763, 0x10, 0x1);
4401
4402         halbtc8821c2ant_enable_gnt_to_gpio(btc, TRUE);
4403
4404 #if 0
4405         /* check if WL firmware download ok */
4406         /*if (btc->btc_read_1byte(btc, 0x80) == 0xc6)*/
4407         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ONOFF, TRUE);
4408 #endif
4409
4410         /* Enable counter statistics */
4411         /* 0x76e[3] =1, WLAN_Act control by PTA */
4412         btc->btc_write_1byte(btc, 0x76e, 0x4);
4413
4414         /* WLAN_Tx by GNT_WL  0x950[29] = 0 */
4415         /* btc->btc_write_1byte_bitmask(btc, 0x953, 0x20, 0x0); */
4416
4417         if (coex_sta->is_rf_state_off) {
4418                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
4419                                              BT_8821C_2ANT_PHASE_WOFF);
4420
4421                 btc->stop_coex_dm = TRUE;
4422                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4423                             "[BTCoex], **********  %s (RF Off)\n", __func__);
4424                 BTC_TRACE(trace_buf);
4425         } else if (wifi_only) {
4426                 coex_sta->concurrent_rx_mode_on = FALSE;
4427                 /* Path config   */
4428                 /* Set Antenna Path */
4429                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
4430                                              BT_8821C_2ANT_PHASE_WONLY);
4431                 btc->stop_coex_dm = TRUE;
4432         } else {
4433                 /* Set BT polluted packet on for Tx rate adaptive not
4434                  * including Tx retry break by PTA, 0x45c[19] =1
4435                  */
4436                 btc->btc_write_1byte_bitmask(btc, 0x45e, 0x8, 0x1);
4437
4438                 coex_sta->concurrent_rx_mode_on = TRUE;
4439                 /* btc->btc_write_1byte_bitmask(btc, 0x953, 0x2, 0x1); */
4440
4441                 /* RF 0x1[1] = 0->Set GNT_WL_RF_Rx always = 1
4442                  * for con-current Rx, mask Tx only
4443                  */
4444                 btc->btc_set_rf_reg(btc, BTC_RF_A, 0x1, 0x2, 0x0);
4445
4446                 /* Set Antenna Path */
4447                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
4448                                              BT_8821C_2ANT_PHASE_INIT);
4449
4450                 btc->stop_coex_dm = FALSE;
4451         }
4452         halbtc8821c2ant_table(btc, FC_EXCU, 0);
4453         halbtc8821c2ant_tdma(btc, FC_EXCU, FALSE, 0);
4454 }
4455
4456 /* ************************************************************
4457  * work around function start with wa_halbtc8821c2ant_
4458  * ************************************************************
4459  * ************************************************************
4460  * extern function start with ex_halbtc8821c2ant_
4461  * ************************************************************ */
4462 void ex_halbtc8821c2ant_power_on_setting(struct btc_coexist *btc)
4463 {
4464         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4465         struct  btc_board_info *board_info = &btc->board_info;
4466         u8 u8tmp = 0x0;
4467         u16 u16tmp = 0x0;
4468
4469         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4470                     "[BTCoex], Execute %s !!\n", __func__);
4471         BTC_TRACE(trace_buf);
4472
4473         btc->stop_coex_dm = TRUE;
4474         coex_sta->is_rf_state_off = FALSE;
4475
4476         /* enable BB, REG_SYS_FUNC_EN such that
4477          * we can write BB Register correctly.
4478          */
4479         u16tmp = btc->btc_read_2byte(btc, 0x2);
4480         btc->btc_write_2byte(btc, 0x2, u16tmp | BIT(0) | BIT(1));
4481
4482         /* Local setting bit define
4483          *      BIT0: "0" for no antenna inverse; "1" for antenna inverse
4484          *      BIT1: "0" for internal switch; "1" for external switch
4485          *      BIT2: "0" for one antenna; "1" for two antenna
4486          * NOTE: here default all internal switch and 1-antenna
4487          * ==> BIT1=0 and BIT2=0
4488          */
4489
4490         /* Check efuse 0xc3[6] for Single Antenna Path */
4491         if (board_info->single_ant_path == 0) {
4492                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4493                             "[BTCoex], Single Antenna, Antenna at Aux Port\n");
4494                 BTC_TRACE(trace_buf);
4495
4496                 board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
4497                 u8tmp = 7;
4498         } else if (board_info->single_ant_path == 1) {
4499                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4500                             "[BTCoex], Single Antenna, Antenna at Main Port\n");
4501                 BTC_TRACE(trace_buf);
4502
4503                 board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
4504                 u8tmp = 6;
4505         }
4506
4507         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4508                     "[BTCoex], (Power On) single_ant_path  = %d, btdm_ant_pos = %d\n",
4509                     board_info->single_ant_path , board_info->btdm_ant_pos);
4510         BTC_TRACE(trace_buf);
4511
4512         /* Setup RF front end type */
4513         halbtc8821c2ant_set_rfe_type(btc);
4514
4515         /* Set Antenna Path to BT side */
4516         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
4517                                      BT_8821C_2ANT_PHASE_POWERON);
4518
4519         /* Save"single antenna position" info in Local register setting
4520          * for FW reading, because FW may not ready at  power on
4521          */
4522         if (btc->chip_interface == BTC_INTF_PCI)
4523                 btc->btc_write_local_reg_1byte(btc, 0x3e0, u8tmp);
4524         else if (btc->chip_interface == BTC_INTF_USB)
4525                 btc->btc_write_local_reg_1byte(btc, 0xfe08, u8tmp);
4526         else if (btc->chip_interface == BTC_INTF_SDIO)
4527                 btc->btc_write_local_reg_1byte(btc, 0x60, u8tmp);
4528
4529         /* enable GNT_WL/GNT_BT debug signal to GPIO14/15 */
4530         halbtc8821c2ant_enable_gnt_to_gpio(btc, TRUE);
4531
4532         if (btc->dbg_mode) {
4533                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4534                             "[BTCoex], LTE coex Reg 0x38 (Power-On) = 0x%x\n",
4535                             halbtc8821c2ant_read_indirect_reg(btc, 0x38));
4536                 BTC_TRACE(trace_buf);
4537
4538                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4539                             "[BTCoex], MACReg 0x70/ BBReg 0xcb4 (Power-On) = 0x%x/ 0x%x\n",
4540                             btc->btc_read_4byte(btc, 0x70),
4541                             btc->btc_read_4byte(btc, 0xcb4));
4542                 BTC_TRACE(trace_buf);
4543         }
4544 }
4545
4546 void ex_halbtc8821c2ant_pre_load_firmware(struct btc_coexist *btc)
4547 {
4548         struct  btc_board_info  *board_info = &btc->board_info;
4549         u8 u8tmp = 0x4; /* Set BIT2 by default since it's 2ant case */
4550
4551         /* S0 or S1 setting and Local register setting
4552          *(By the setting fw can get ant number, S0/S1, ... info)
4553          * Local setting bit define
4554          *      BIT0: "0" for no antenna inverse; "1" for antenna inverse
4555          *      BIT1: "0" for internal switch; "1" for external switch
4556          *      BIT2: "0" for one antenna; "1" for two antenna
4557          * NOTE: here default all internal switch
4558          * and 1-antenna ==> BIT1=0 and BIT2=0
4559          */
4560         if (btc->chip_interface == BTC_INTF_USB) {
4561                 /* fixed at S0 for USB interface */
4562                 u8tmp |= 0x1;   /* antenna inverse */
4563                 btc->btc_write_local_reg_1byte(btc, 0xfe08, u8tmp);
4564         } else {
4565                 /* for PCIE and SDIO interface, we check efuse 0xc3[6] */
4566                 if (board_info->single_ant_path == 0) {
4567                 } else if (board_info->single_ant_path == 1) {
4568                         /* set to S0 */
4569                         u8tmp |= 0x1;   /* antenna inverse */
4570                 }
4571
4572                 if (btc->chip_interface == BTC_INTF_PCI)
4573                         btc->btc_write_local_reg_1byte(btc, 0x3e0, u8tmp);
4574                 else if (btc->chip_interface == BTC_INTF_SDIO)
4575                         btc->btc_write_local_reg_1byte(btc, 0x60, u8tmp);
4576         }
4577 }
4578
4579
4580 void ex_halbtc8821c2ant_init_hw_config(struct btc_coexist *btc,
4581                                        boolean wifi_only)
4582 {
4583         halbtc8821c2ant_init_hw_config(btc, wifi_only);
4584 }
4585
4586 void ex_halbtc8821c2ant_init_coex_dm(struct btc_coexist *btc)
4587 {
4588         btc->stop_coex_dm = FALSE;
4589         btc->auto_report = TRUE;
4590         btc->dbg_mode = FALSE;
4591         halbtc8821c2ant_init_coex_dm(btc);
4592 }
4593
4594 void ex_halbtc8821c2ant_display_simple_coex_info(struct btc_coexist *btc)
4595 {
4596         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4597         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
4598         struct btc_board_info *board_info   = &btc->board_info;
4599         struct btc_bt_link_info *bt_link_info = &btc->bt_link_info;
4600
4601         u8 *cli_buf = btc->cli_buf;
4602         u32 bt_patch_ver = 0, bt_coex_ver = 0;
4603         static u8       cnt;
4604         u8 * const p = &coex_sta->bt_afh_map[0];
4605
4606         if (!coex_sta->bt_disabled &&
4607             (coex_sta->bt_coex_supported_version == 0 ||
4608             coex_sta->bt_coex_supported_version == 0xffff) &&
4609             cnt == 0) {
4610                 btc->btc_get(btc, BTC_GET_U4_SUPPORTED_FEATURE,
4611                              &coex_sta->bt_coex_supported_feature);
4612
4613                 btc->btc_get(btc, BTC_GET_U4_SUPPORTED_VERSION,
4614                              &coex_sta->bt_coex_supported_version);
4615
4616                 coex_sta->bt_reg_vendor_ac = (u16)(btc->btc_get_bt_reg(btc, 3,
4617                                                                        0xac) &
4618                                                    0xffff);
4619
4620                 coex_sta->bt_reg_vendor_ae = (u16)(btc->btc_get_bt_reg(btc, 3,
4621                                                                        0xae) &
4622                                                    0xffff);
4623
4624                 btc->btc_get(btc, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
4625                 btc->bt_info.bt_get_fw_ver = bt_patch_ver;
4626
4627                 if (coex_sta->num_of_profile > 0)
4628                         btc->btc_get_bt_afh_map_from_bt(btc, 0, p);
4629         }
4630
4631         if (++cnt >= 3)
4632                 cnt = 0;
4633
4634         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4635                    "\r\n _____[BT Coexist info]____");
4636         CL_PRINTF(cli_buf);
4637
4638         if (btc->manual_control) {
4639                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4640                            "\r\n __[Under Manual Control]_");
4641         CL_PRINTF(cli_buf);
4642                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4643                            "\r\n _________________________");
4644                 CL_PRINTF(cli_buf);
4645         }
4646
4647         if (btc->stop_coex_dm) {
4648                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4649                            "\r\n ____[Coex is STOPPED]____");
4650                 CL_PRINTF(cli_buf);
4651                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4652                            "\r\n _________________________");
4653                 CL_PRINTF(cli_buf);
4654         }
4655
4656         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4657                    "\r\n %-35s = %d/ %d/ %s / 0x%x",
4658                    "Ant PG Num/ Mech/ Pos/ RFE",
4659                    board_info->pg_ant_num, board_info->btdm_ant_num,
4660                    (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT
4661                     ? "Main" : "Aux"),
4662                    board_info->rfe_type);
4663         CL_PRINTF(cli_buf);
4664
4665         bt_coex_ver = (coex_sta->bt_coex_supported_version & 0xff);
4666         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4667                    "\r\n %-35s = %d_%02x/ 0x%02x/ 0x%02x (%s)",
4668                    "CoexVer WL/  BT_Desired/ BT_Report",
4669                    glcoex_ver_date_8821c_2ant, glcoex_ver_8821c_2ant,
4670                    glcoex_ver_btdesired_8821c_2ant,
4671                    bt_coex_ver,
4672                    (bt_coex_ver == 0xff ? "Unknown" :
4673                    (coex_sta->bt_disabled ? "BT-disable" :
4674                    (bt_coex_ver >= glcoex_ver_btdesired_8821c_2ant ?
4675                    "Match" : "Mis-Match"))));
4676         CL_PRINTF(cli_buf);
4677
4678         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s ", "BT status",
4679                    ((coex_sta->bt_disabled) ? ("disabled") :
4680                    ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page")
4681                     : ((BT_8821C_2ANT_BSTATUS_NCON_IDLE ==
4682                     coex_dm->bt_status) ? "non-connected idle" :
4683                     ((BT_8821C_2ANT_BSTATUS_CON_IDLE ==
4684                     coex_dm->bt_status) ? "connected-idle" : "busy")))));
4685         CL_PRINTF(cli_buf);
4686
4687         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
4688                    "0x770(Hi-pri rx/tx)",
4689                    coex_sta->high_priority_rx, coex_sta->high_priority_tx);
4690         CL_PRINTF(cli_buf);
4691
4692         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
4693                    "0x774(Lo-pri rx/tx)",
4694                    coex_sta->low_priority_rx, coex_sta->low_priority_tx,
4695                    (bt_link_info->slave_role ? "(Slave!!)" :
4696                    (coex_sta->is_tdma_btautoslot_hang ?
4697                    "(auto-slot hang!!)" : "")));
4698         CL_PRINTF(cli_buf);
4699 }
4700
4701 void ex_halbtc8821c2ant_display_coex_info(struct btc_coexist *btc)
4702 {
4703         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
4704         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
4705         struct  btc_board_info *board_info = &btc->board_info;
4706         struct  btc_bt_link_info *bt_link_info = &btc->bt_link_info;
4707
4708         u8 *cli_buf = btc->cli_buf;
4709         u8 u8tmp[4], i, ps_tdma_case = 0;
4710         u32 u32tmp[4];
4711         u16 u16tmp[4];
4712         u32 fa_ofdm, fa_cck, cca_ofdm, cca_cck;
4713         u32 fw_ver = 0, bt_patch_ver = 0, bt_coex_ver = 0;
4714         static u8 pop_report_in_10s;
4715         u32 phyver = 0;
4716         boolean lte_coex_on = FALSE;
4717         static u8 cnt;
4718         u32 ratio_crc, cnt_ok, cnt_err;
4719         u8 * const p = &coex_sta->bt_afh_map[0];
4720
4721         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4722                    "\r\n ============[BT Coexist info 8821C]============");
4723         CL_PRINTF(cli_buf);
4724
4725         if (btc->manual_control) {
4726                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4727                            "\r\n ============[Under Manual Control]============");
4728                 CL_PRINTF(cli_buf);
4729                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4730                            "\r\n ==========================================");
4731                 CL_PRINTF(cli_buf);
4732         } else if (btc->stop_coex_dm) {
4733                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4734                            "\r\n ============[Coex is STOPPED]============");
4735                 CL_PRINTF(cli_buf);
4736                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4737                            "\r\n ==========================================");
4738                 CL_PRINTF(cli_buf);
4739         } else if (!coex_sta->run_time_state) {
4740                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4741                            "\r\n ============[Run Time State = False]============");
4742                 CL_PRINTF(cli_buf);
4743                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4744                            "\r\n ==========================================");
4745                 CL_PRINTF(cli_buf);
4746         } else if (coex_sta->freeze_coexrun_by_btinfo) {
4747                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4748                            "\r\n ============[freeze_coexrun_by_btinfo]============");
4749                 CL_PRINTF(cli_buf);
4750                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4751                            "\r\n ==========================================");
4752                 CL_PRINTF(cli_buf);
4753         }
4754
4755         if (!coex_sta->bt_disabled &&
4756             (coex_sta->bt_coex_supported_version == 0 ||
4757             coex_sta->bt_coex_supported_version == 0xffff) &&
4758             cnt == 0) {
4759                 btc->btc_get(btc, BTC_GET_U4_SUPPORTED_FEATURE,
4760                              &coex_sta->bt_coex_supported_feature);
4761
4762                 btc->btc_get(btc, BTC_GET_U4_SUPPORTED_VERSION,
4763                              &coex_sta->bt_coex_supported_version);
4764
4765                 coex_sta->bt_reg_vendor_ac = (u16)(btc->btc_get_bt_reg(btc, 3,
4766                                                                        0xac) &
4767                                                    0xffff);
4768
4769                 coex_sta->bt_reg_vendor_ae = (u16)(btc->btc_get_bt_reg(btc, 3,
4770                                                                        0xae) &
4771                                                    0xffff);
4772
4773                 btc->btc_get(btc, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
4774                 btc->bt_info.bt_get_fw_ver = bt_patch_ver;
4775
4776                 if (coex_sta->num_of_profile > 0)
4777                         btc->btc_get_bt_afh_map_from_bt(btc, 0, p);
4778         }
4779
4780         if (++cnt >= 3)
4781                 cnt = 0;
4782
4783         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4784                    "\r\n %-35s = %d/ %s/ %s / 0x%x",
4785                    "Ant PG Num/ Mech/ Pos/ RFE",
4786                    board_info->pg_ant_num,
4787                    (board_info->btdm_ant_num == 1 ? "Shared" : "Non-Shared"),
4788                    (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT
4789                     ? "Main" : "Aux"),
4790                    board_info->rfe_type);
4791         CL_PRINTF(cli_buf);
4792
4793         bt_patch_ver = btc->bt_info.bt_get_fw_ver;
4794         btc->btc_get(btc, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
4795         phyver = btc->btc_get_bt_phydm_version(btc);
4796         bt_coex_ver = (coex_sta->bt_coex_supported_version & 0xff);
4797
4798         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4799                    "\r\n %-35s = %d_%02x/ 0x%02x/ 0x%02x (%s)",
4800                    "CoexVer WL/  BT_Desired/ BT_Report",
4801                    glcoex_ver_date_8821c_2ant, glcoex_ver_8821c_2ant,
4802                    glcoex_ver_btdesired_8821c_2ant,
4803                    bt_coex_ver,
4804                    (bt_coex_ver == 0xff ? "Unknown" :
4805                    (coex_sta->bt_disabled ? "BT-disable" :
4806                    (bt_coex_ver >= glcoex_ver_btdesired_8821c_2ant ?
4807                     "Match" : "Mis-Match"))));
4808         CL_PRINTF(cli_buf);
4809
4810         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4811                    "\r\n %-35s = 0x%x/ 0x%x/ v%d/ %c",
4812                    "W_FW/ B_FW/ Phy/ Kt",
4813                    fw_ver, bt_patch_ver, phyver,
4814                    coex_sta->cut_version + 65);
4815         CL_PRINTF(cli_buf);
4816
4817         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4818                    "\r\n %-35s = %02x %02x %02x (RF-Ch = %d)",
4819                    "AFH Map to BT",
4820                    coex_dm->wifi_chnl_info[0], coex_dm->wifi_chnl_info[1],
4821                    coex_dm->wifi_chnl_info[2], coex_sta->wl_center_channel);
4822         CL_PRINTF(cli_buf);
4823
4824         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d ",
4825                    "Isolation/WL_Thres/BT_Thres",
4826                    coex_sta->isolation_btween_wb,
4827                    coex_sta->wifi_coex_thres,
4828                    coex_sta->bt_coex_thres);
4829         CL_PRINTF(cli_buf);
4830
4831         /* wifi status */
4832         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4833                    "============[Wifi Status]============");
4834         CL_PRINTF(cli_buf);
4835         btc->btc_disp_dbg_msg(btc, BTC_DBG_DISP_WIFI_STATUS);
4836
4837         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4838                    "============[BT Status]============");
4839         CL_PRINTF(cli_buf);
4840
4841         pop_report_in_10s++;
4842         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4843                    "\r\n %-35s = %s/ %ddBm/ %d/ %d",
4844                    "BT status/ rssi/ retryCnt/ popCnt",
4845                    ((coex_sta->bt_disabled) ? ("disabled") :    ((
4846                     coex_sta->c2h_bt_inquiry_page) ? ("inquiry-page")
4847                     : ((BT_8821C_2ANT_BSTATUS_NCON_IDLE ==
4848                                coex_dm->bt_status) ? "non-connected-idle" :
4849                    ((coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_CON_IDLE)
4850                    ? "connected-idle" : "busy")))),
4851                    coex_sta->bt_rssi - 100, coex_sta->bt_retry_cnt,
4852                    coex_sta->pop_event_cnt);
4853         CL_PRINTF(cli_buf);
4854
4855         if (pop_report_in_10s >= 5) {
4856                 coex_sta->pop_event_cnt = 0;
4857                 pop_report_in_10s = 0;
4858         }
4859
4860         if (coex_sta->num_of_profile != 0)
4861                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4862                             "\r\n %-35s = %s%s%s%s%s%s (multilink = %d)",
4863                             "Profiles",
4864                             ((bt_link_info->a2dp_exist) ?
4865                             ((coex_sta->is_bt_a2dp_sink) ? "A2DP sink," :
4866                             "A2DP,") : ""),
4867                             ((bt_link_info->sco_exist) ?  "HFP," : ""),
4868                             ((bt_link_info->hid_exist) ?
4869                             ((coex_sta->is_hid_rcu) ? "HID(RCU)" :
4870                             ((coex_sta->hid_busy_num >= 2) ? "HID(4/18)," :
4871                             "HID(2/18),")) : ""),
4872                             ((bt_link_info->pan_exist) ?
4873                             ((coex_sta->is_bt_opp_exist) ? "OPP," : "PAN,") :
4874                             ""),
4875                             ((coex_sta->voice_over_HOGP) ? "Voice," : ""),
4876                             ((coex_sta->msft_mr_exist) ? "MR" : ""),
4877                             coex_sta->is_bt_multi_link);
4878         else
4879                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4880                            "\r\n %-35s = %s", "Profiles",
4881                            (coex_sta->msft_mr_exist) ? "MR" : "None");
4882
4883         CL_PRINTF(cli_buf);
4884
4885         if (bt_link_info->a2dp_exist) {
4886                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %s",
4887                            "CQDDR/Bitpool/Auto_Slot",
4888                            ((coex_sta->is_A2DP_3M) ? "On" : "Off"),
4889                            coex_sta->a2dp_bit_pool,
4890                            ((coex_sta->is_autoslot) ? "On" : "Off")
4891                           );
4892                 CL_PRINTF(cli_buf);
4893
4894                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4895                            "\r\n %-35s = 0x%x/ 0x%x/ %d/ %d",
4896                            "V_ID/D_name/FBSlot_Legacy/FBSlot_Le",
4897                            coex_sta->bt_a2dp_vendor_id,
4898                            coex_sta->bt_a2dp_device_name,
4899                            coex_sta->legacy_forbidden_slot,
4900                            coex_sta->le_forbidden_slot
4901                           );
4902                 CL_PRINTF(cli_buf);
4903         }
4904
4905         if (bt_link_info->hid_exist) {
4906                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d",
4907                            "HID PairNum",
4908                            coex_sta->hid_pair_cnt);
4909                 CL_PRINTF(cli_buf);
4910         }
4911
4912         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %s/ 0x%x",
4913                     "Role/RoleSwCnt/IgnWlact/Feature",
4914                     ((bt_link_info->slave_role) ? "Slave" : "Master"),
4915                     coex_sta->cnt_role_switch,
4916                     ((coex_dm->cur_ignore_wlan_act) ? "Yes" : "No"),
4917                     coex_sta->bt_coex_supported_feature);
4918         CL_PRINTF(cli_buf);
4919
4920         if (coex_sta->is_ble_scan_en) {
4921                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4922                            "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
4923                            "BLEScan Type/TV/Init/Ble",
4924                            coex_sta->bt_ble_scan_type,
4925                            (coex_sta->bt_ble_scan_type & 0x1 ?
4926                            coex_sta->bt_ble_scan_para[0] : 0x0),
4927                            (coex_sta->bt_ble_scan_type & 0x2 ?
4928                            coex_sta->bt_ble_scan_para[1] : 0x0),
4929                            (coex_sta->bt_ble_scan_type & 0x4 ?
4930                            coex_sta->bt_ble_scan_para[2] : 0x0));
4931                 CL_PRINTF(cli_buf);
4932         }
4933
4934         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4935                    "\r\n %-35s = %d/ %d/ %d/ %d/ %d %s",
4936                    "ReInit/ReLink/IgnWlact/Page/NameReq", coex_sta->cnt_reinit,
4937                    coex_sta->cnt_setup_link, coex_sta->cnt_ign_wlan_act,
4938                    coex_sta->cnt_page, coex_sta->cnt_remote_name_req,
4939                    (coex_sta->is_setup_link ? "(Relink!!)" : ""));
4940         CL_PRINTF(cli_buf);
4941
4942         halbtc8821c2ant_read_scbd(btc,  &u16tmp[0]);
4943
4944         if (coex_sta->bt_reg_vendor_ae == 0xffff ||
4945             coex_sta->bt_reg_vendor_ac == 0xffff)
4946                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4947                            "\r\n %-35s = x/ x/ 0x%04x",
4948                            "0xae[4]/0xac[1:0]/ScBd(B->W)", u16tmp[0]);
4949         else
4950                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4951                            "\r\n %-35s = 0x%x/ 0x%x/ 0x%04x",
4952                            "0xae[4]/0xac[1:0]/ScBd(B->W)",
4953                            (int)((coex_sta->bt_reg_vendor_ae & BIT(4)) >> 4),
4954                            coex_sta->bt_reg_vendor_ac & 0x3, u16tmp[0]);
4955         CL_PRINTF(cli_buf);
4956
4957         if (coex_sta->num_of_profile > 0) {
4958                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4959                            "\r\n %-35s = %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x",
4960                            "AFH MAP",
4961                            coex_sta->bt_afh_map[0],
4962                            coex_sta->bt_afh_map[1],
4963                            coex_sta->bt_afh_map[2],
4964                            coex_sta->bt_afh_map[3],
4965                            coex_sta->bt_afh_map[4],
4966                            coex_sta->bt_afh_map[5],
4967                            coex_sta->bt_afh_map[6],
4968                            coex_sta->bt_afh_map[7],
4969                            coex_sta->bt_afh_map[8],
4970                            coex_sta->bt_afh_map[9]);
4971                 CL_PRINTF(cli_buf);
4972         }
4973
4974         for (i = 0; i < BT_8821C_2ANT_INFO_SRC_MAX; i++) {
4975                 if (coex_sta->bt_info_c2h_cnt[i]) {
4976                         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4977                                    "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x (%d)",
4978                                    glbt_info_src_8821c_2ant[i],
4979                                    coex_sta->bt_info_c2h[i][0],
4980                                    coex_sta->bt_info_c2h[i][1],
4981                                    coex_sta->bt_info_c2h[i][2],
4982                                    coex_sta->bt_info_c2h[i][3],
4983                                    coex_sta->bt_info_c2h[i][4],
4984                                    coex_sta->bt_info_c2h[i][5],
4985                                    coex_sta->bt_info_c2h[i][6],
4986                                    coex_sta->bt_info_c2h_cnt[i]);
4987                         CL_PRINTF(cli_buf);
4988                 }
4989         }
4990
4991         /* Sw mechanism  */
4992         if (btc->manual_control)
4993                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4994                         "============[mechanism] (before Manual)============");
4995         else
4996                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4997                            "============[Mechanism]============");
4998
4999         CL_PRINTF(cli_buf);
5000
5001         ps_tdma_case = coex_dm->cur_ps_tdma;
5002         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5003                    "\r\n %-35s = %02x %02x %02x %02x %02x (case-%d, %s)",
5004                    "TDMA",
5005                    coex_dm->ps_tdma_para[0], coex_dm->ps_tdma_para[1],
5006                    coex_dm->ps_tdma_para[2], coex_dm->ps_tdma_para[3],
5007                    coex_dm->ps_tdma_para[4], ps_tdma_case,
5008                    (coex_dm->cur_ps_tdma_on ? "TDMA-On" : "TDMA-Off"));
5009         CL_PRINTF(cli_buf);
5010
5011         switch (coex_sta->wl_coex_mode) {
5012         case BT_8821C_2ANT_WLINK_2G1PORT:
5013         default:
5014                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5015                            "\r\n %-35s = %s", "Coex_Mode", "2G-SP");
5016                 break;
5017         case BT_8821C_2ANT_WLINK_2GMPORT:
5018                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5019                            "\r\n %-35s = %s", "Coex_Mode", "2G-MP");
5020                 break;
5021         case BT_8821C_2ANT_WLINK_25GMPORT:
5022                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5023                            "\r\n %-35s = %s", "Coex_Mode", "25G-MP");
5024                 break;
5025         case BT_8821C_2ANT_WLINK_5G:
5026                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5027                            "\r\n %-35s = %s", "Coex_Mode", "5G");
5028                 break;
5029         case BT_8821C_2ANT_WLINK_2GGO:
5030                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5031                            "\r\n %-35s = %s", "Coex_Mode", "2G-P2P");
5032                 break;
5033         case BT_8821C_2ANT_WLINK_BTMR:
5034                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5035                            "\r\n %-35s = %s", "Coex_Mode", "BT-MR");
5036                 break;
5037         }
5038         CL_PRINTF(cli_buf);
5039
5040         u32tmp[0] = btc->btc_read_4byte(btc, 0x6c0);
5041         u32tmp[1] = btc->btc_read_4byte(btc, 0x6c4);
5042         u32tmp[2] = btc->btc_read_4byte(btc, 0x6c8);
5043         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5044                    "\r\n %-35s = %d/ 0x%x/ 0x%x/ 0x%x",
5045                    "Table/0x6c0/0x6c4/0x6c8",
5046                    coex_sta->coex_table_type, u32tmp[0], u32tmp[1], u32tmp[2]);
5047         CL_PRINTF(cli_buf);
5048
5049         u8tmp[0] = btc->btc_read_1byte(btc, 0x778);
5050         u32tmp[0] = btc->btc_read_4byte(btc, 0x6cc);
5051         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5052                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%04x/ %d",
5053                    "0x778/0x6cc/ScBd(W->B)/RunCnt", u8tmp[0], u32tmp[0],
5054                    coex_sta->score_board_WB, coex_sta->coex_run_cnt);
5055         CL_PRINTF(cli_buf);
5056
5057         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s/ %d/ %d",
5058                    "AntDiv/BtCtrlLPS/LPRA/PsFail/g_busy",
5059                    ((board_info->ant_div_cfg) ?
5060                    ((coex_dm->cur_antdiv_type) ? "On(Hw)" : "On(Sw)") : "Off"),
5061                    ((coex_sta->force_lps_ctrl) ? "On" : "Off"),
5062                    ((coex_dm->cur_low_penalty_ra) ? "On" : "Off"),
5063                    coex_sta->cnt_set_ps_state_fail,
5064                    coex_sta->gl_wifi_busy);
5065         CL_PRINTF(cli_buf);
5066
5067         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
5068                    "WL_Pwr/ BT_Pwr", coex_dm->cur_wl_pwr_lvl,
5069                    coex_dm->cur_bt_pwr_lvl);
5070         CL_PRINTF(cli_buf);
5071
5072         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d",
5073                    "Null All/Retry/Ack/BT_Empty/BT_Late",
5074                    coex_sta->wl_fw_dbg_info[1],
5075                    coex_sta->wl_fw_dbg_info[2],
5076                    coex_sta->wl_fw_dbg_info[3],
5077                    coex_sta->wl_fw_dbg_info[4],
5078                    coex_sta->wl_fw_dbg_info[5]);
5079         CL_PRINTF(cli_buf);
5080
5081         u32tmp[0] = halbtc8821c2ant_read_indirect_reg(btc, 0x38);
5082         lte_coex_on = ((u32tmp[0] & BIT(7)) >> 7) ?  TRUE : FALSE;
5083
5084         if (lte_coex_on) {
5085
5086                 u32tmp[0] = halbtc8821c2ant_read_indirect_reg(btc, 0xa0);
5087                 u32tmp[1] = halbtc8821c2ant_read_indirect_reg(btc, 0xa4);
5088                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
5089                            "LTE Coex Table W_L/B_L", u32tmp[0] & 0xffff,
5090                            u32tmp[1] & 0xffff);
5091                 CL_PRINTF(cli_buf);
5092
5093                 u32tmp[0] = halbtc8821c2ant_read_indirect_reg(btc, 0xa8);
5094                 u32tmp[1] = halbtc8821c2ant_read_indirect_reg(btc, 0xac);
5095                 u32tmp[2] = halbtc8821c2ant_read_indirect_reg(btc, 0xb0);
5096                 u32tmp[3] = halbtc8821c2ant_read_indirect_reg(btc, 0xb4);
5097                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5098                            "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
5099                            "LTE Break Table W_L/B_L/L_W/L_B",
5100                            u32tmp[0] & 0xffff, u32tmp[1] & 0xffff,
5101                            u32tmp[2] & 0xffff, u32tmp[3] & 0xffff);
5102                 CL_PRINTF(cli_buf);
5103         }
5104
5105         /* Hw setting            */
5106         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
5107                    "============[Hw setting]============");
5108         CL_PRINTF(cli_buf);
5109
5110         u32tmp[0] = halbtc8821c2ant_read_indirect_reg(btc, 0x38);
5111         u32tmp[1] = halbtc8821c2ant_read_indirect_reg(btc, 0x54);
5112         u8tmp[0] = btc->btc_read_1byte(btc, 0x73);
5113
5114         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s",
5115                    "LTE Coex/Path Owner",
5116                    ((lte_coex_on) ? "On" : "Off") ,
5117                    ((u8tmp[0] & BIT(2)) ? "WL" : "BT"));
5118         CL_PRINTF(cli_buf);
5119
5120         if (lte_coex_on) {
5121                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5122                            "\r\n %-35s = %d/ %d/ %d/ %d",
5123                            "LTE 3Wire/OPMode/UART/UARTMode",
5124                            (int)((u32tmp[0] & BIT(6)) >> 6),
5125                            (int)((u32tmp[0] & (BIT(5) | BIT(4))) >> 4),
5126                            (int)((u32tmp[0] & BIT(3)) >> 3),
5127                            (int)(u32tmp[0] & (BIT(2) | BIT(1) | BIT(0))));
5128                 CL_PRINTF(cli_buf);
5129
5130                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
5131                            "LTE_Busy/UART_Busy",
5132                            (int)((u32tmp[1] & BIT(1)) >> 1),
5133                            (int)(u32tmp[1] & BIT(0)));
5134                 CL_PRINTF(cli_buf);
5135         }
5136         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5137                    "\r\n %-35s = %s (BB:%s)/ %s (BB:%s)/ %s (gnt_err = %d)",
5138                    "GNT_WL_Ctrl/GNT_BT_Ctrl/Dbg",
5139                    ((u32tmp[0] & BIT(12)) ? "SW" : "HW"),
5140                    ((u32tmp[0] & BIT(8)) ?  "SW" : "HW"),
5141                    ((u32tmp[0] & BIT(14)) ? "SW" : "HW"),
5142                    ((u32tmp[0] & BIT(10)) ?  "SW" : "HW"),
5143                    ((u8tmp[0] & BIT(3)) ? "On" : "Off"),
5144                    coex_sta->gnt_error_cnt);
5145         CL_PRINTF(cli_buf);
5146
5147         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ 0x%x",
5148                    "GNT_WL/GNT_BT/ RF_0x1",
5149                    (int)((u32tmp[1] & BIT(2)) >> 2),
5150                    (int)((u32tmp[1] & BIT(3)) >> 3),
5151                    btc->btc_get_rf_reg(btc, BTC_RF_A, 0x1, 0xfffff));
5152         CL_PRINTF(cli_buf);
5153
5154         u32tmp[0] = btc->btc_read_4byte(btc, 0xcb0);
5155         u32tmp[1] = btc->btc_read_4byte(btc, 0xcb4);
5156         u8tmp[0] = btc->btc_read_1byte(btc, 0xcba);
5157
5158         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5159                    "\r\n %-35s = 0x%04x/ 0x%04x/ 0x%02x %s",
5160                    "0xcb0/0xcb4/0xcb8[23:16]",
5161                    u32tmp[0], u32tmp[1], u8tmp[0],
5162                    ((u8tmp[0] & 0x1) == 0x1 ?  "(BTG)" : "(WL_A+G)"));
5163         CL_PRINTF(cli_buf);
5164
5165         u32tmp[0] = btc->btc_read_4byte(btc, 0x4c);
5166         u8tmp[2] = btc->btc_read_1byte(btc, 0x64);
5167         u8tmp[0] = btc->btc_read_1byte(btc, 0x4c6);
5168         u8tmp[1] = btc->btc_read_1byte(btc, 0x40);
5169
5170         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5171                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
5172                    "4c[24:23]/64[0]/4c6[4]/40[5]",
5173                    (int)(u32tmp[0] & (BIT(24) | BIT(23))) >> 23,
5174                     u8tmp[2] & 0x1, (int)((u8tmp[0] & BIT(4)) >> 4),
5175                    (int)((u8tmp[1] & BIT(5)) >> 5));
5176         CL_PRINTF(cli_buf);
5177
5178         u32tmp[0] = btc->btc_read_4byte(btc, 0x550);
5179         u8tmp[0] = btc->btc_read_1byte(btc, 0x522);
5180         u8tmp[1] = btc->btc_read_1byte(btc, 0x953);
5181         u8tmp[2] = btc->btc_read_1byte(btc, 0xc50);
5182
5183         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5184                    "\r\n %-35s = 0x%x/ 0x%x/ %s/ 0x%x",
5185                    "0x550/0x522/4-RxAGC/0xc50", u32tmp[0], u8tmp[0],
5186                    (u8tmp[1] & 0x2) ? "On" : "Off", u8tmp[2]);
5187         CL_PRINTF(cli_buf);
5188
5189         fa_ofdm = btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_FA_OFDM);
5190         fa_cck = btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_FA_CCK);
5191         cca_ofdm = btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CCA_OFDM);
5192         cca_cck = btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CCA_CCK);
5193
5194         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5195                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
5196                    "CCK-CCA/CCK-FA/OFDM-CCA/OFDM-FA",
5197                    cca_cck, fa_cck, cca_ofdm, fa_ofdm);
5198         CL_PRINTF(cli_buf);
5199
5200         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
5201                    "CRC_OK CCK/11g/11n/11ac",
5202                    coex_sta->crc_ok_cck, coex_sta->crc_ok_11g,
5203                    coex_sta->crc_ok_11n, coex_sta->crc_ok_11n_vht);
5204         CL_PRINTF(cli_buf);
5205
5206         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
5207                    "CRC_Err CCK/11g/11n/11ac",
5208                    coex_sta->crc_err_cck, coex_sta->crc_err_11g,
5209                    coex_sta->crc_err_11n, coex_sta->crc_err_11n_vht);
5210         CL_PRINTF(cli_buf);
5211
5212         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
5213                    "\r\n %-35s = %d/ %d/ %s-%d/ %d (Tx macid: %d)",
5214                    "Rate RxD/RxRTS/TxD/TxRetry_ratio",
5215                    coex_sta->wl_rx_rate, coex_sta->wl_rts_rx_rate,
5216                    (coex_sta->wl_tx_rate & 0x80 ? "SGI" : "LGI"),
5217                    coex_sta->wl_tx_rate & 0x7f,
5218                    coex_sta->wl_tx_retry_ratio,
5219                    coex_sta->wl_tx_macid);
5220         CL_PRINTF(cli_buf);
5221
5222         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s/ %s/ %d",
5223                    "HiPr/ Locking/ warn/ Locked/ Noisy",
5224                    (coex_sta->wifi_high_pri_task1 ? "Yes" : "No"),
5225                    (coex_sta->cck_lock ? "Yes" : "No"),
5226                    (coex_sta->cck_lock_warn ? "Yes" : "No"),
5227                    (coex_sta->cck_lock_ever ? "Yes" : "No"),
5228                    coex_sta->wl_noisy_level);
5229         CL_PRINTF(cli_buf);
5230
5231         u8tmp[0] = btc->btc_read_1byte(btc, 0xf8e);
5232         u8tmp[1] = btc->btc_read_1byte(btc, 0xf8f);
5233         u8tmp[2] = btc->btc_read_1byte(btc, 0xd14);
5234         u8tmp[3] = btc->btc_read_1byte(btc, 0xd54);
5235
5236         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
5237                    "EVM_A/ EVM_B/ SNR_A/ SNR_B",
5238                    (u8tmp[0] > 127 ? u8tmp[0] - 256 : u8tmp[0]),
5239                    (u8tmp[1] > 127 ? u8tmp[1] - 256 : u8tmp[1]),
5240                    (u8tmp[2] > 127 ? u8tmp[2] - 256 : u8tmp[2]),
5241                    (u8tmp[3] > 127 ? u8tmp[3] - 256 : u8tmp[3]));
5242         CL_PRINTF(cli_buf);
5243
5244         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
5245                    "0x770(Hi-pri rx/tx)",
5246                    coex_sta->high_priority_rx, coex_sta->high_priority_tx);
5247         CL_PRINTF(cli_buf);
5248
5249         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
5250                    "0x774(Lo-pri rx/tx)",
5251                    coex_sta->low_priority_rx, coex_sta->low_priority_tx,
5252                    (bt_link_info->slave_role ? "(Slave!!)" : (
5253                    coex_sta->is_tdma_btautoslot_hang ?
5254                    "(auto-slot hang!!)" : "")));
5255         CL_PRINTF(cli_buf);
5256
5257         btc->btc_disp_dbg_msg(btc, BTC_DBG_DISP_COEX_STATISTICS);
5258 }
5259
5260 void ex_halbtc8821c2ant_ips_notify(struct btc_coexist *btc, u8 type)
5261 {
5262         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5263
5264         if (btc->manual_control || btc->stop_coex_dm)
5265                 return;
5266
5267         if (type == BTC_IPS_ENTER) {
5268                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5269                             "[BTCoex], IPS ENTER notify\n");
5270                 BTC_TRACE(trace_buf);
5271                 coex_sta->under_ips = TRUE;
5272                 coex_sta->under_lps = FALSE;
5273
5274                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
5275                                            BT_8821C_2ANT_SCBD_ONOFF |
5276                                            BT_8821C_2ANT_SCBD_SCAN |
5277                                            BT_8821C_2ANT_SCBD_UNDERTEST,
5278                                            FALSE);
5279
5280                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
5281                                              BT_8821C_2ANT_PHASE_WOFF);
5282
5283                 halbtc8821c2ant_action_coex_all_off(btc);
5284         } else if (type == BTC_IPS_LEAVE) {
5285                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5286                             "[BTCoex], IPS LEAVE notify\n");
5287                 BTC_TRACE(trace_buf);
5288                 coex_sta->under_ips = FALSE;
5289
5290                 halbtc8821c2ant_init_hw_config(btc, FALSE);
5291                 halbtc8821c2ant_init_coex_dm(btc);
5292                 halbtc8821c2ant_query_bt_info(btc);
5293         }
5294 }
5295
5296 void ex_halbtc8821c2ant_lps_notify(struct btc_coexist *btc, u8 type)
5297 {
5298         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5299         static boolean  pre_force_lps_on = FALSE;
5300
5301         if (btc->manual_control || btc->stop_coex_dm)
5302                 return;
5303
5304         if (type == BTC_LPS_ENABLE) {
5305                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5306                             "[BTCoex], LPS ENABLE notify\n");
5307                 BTC_TRACE(trace_buf);
5308                 coex_sta->under_lps = TRUE;
5309                 coex_sta->under_ips = FALSE;
5310
5311                 if (coex_sta->force_lps_ctrl == TRUE) { /* LPS No-32K */
5312                         /* Write WL "Active" in Score-board for PS-TDMA */
5313                         pre_force_lps_on = TRUE;
5314                         halbtc8821c2ant_write_scbd(btc,
5315                                                    BT_8821C_2ANT_SCBD_ACTIVE,
5316                                                    TRUE);
5317
5318                 } else { /* LPS-32K, need check if this h2c 0x71 can work?? (2015/08/28) */
5319                         /* Write WL "Non-Active" in Score-board for Native-PS */
5320                         pre_force_lps_on = FALSE;
5321                         halbtc8821c2ant_write_scbd(btc,
5322                                                    BT_8821C_2ANT_SCBD_ACTIVE,
5323                                                    FALSE);
5324
5325                         halbtc8821c2ant_action_wifi_native_lps(btc);
5326                 }
5327
5328         } else if (type == BTC_LPS_DISABLE) {
5329                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5330                             "[BTCoex], LPS DISABLE notify\n");
5331                 BTC_TRACE(trace_buf);
5332                 coex_sta->under_lps = FALSE;
5333
5334                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE,
5335                                            TRUE);
5336
5337                 if (!pre_force_lps_on && !coex_sta->force_lps_ctrl)
5338                         halbtc8821c2ant_query_bt_info(btc);
5339         }
5340 }
5341
5342 void ex_halbtc8821c2ant_scan_notify(struct btc_coexist *btc, u8 type)
5343 {
5344         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5345         boolean wifi_connected = FALSE;
5346
5347         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5348                     "[BTCoex], SCAN notify()\n");
5349         BTC_TRACE(trace_buf);
5350
5351         if (btc->manual_control || btc->stop_coex_dm)
5352                 return;
5353
5354         btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
5355
5356         if (wifi_connected)
5357                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5358                             "[BTCoex], ********** WL connected before SCAN\n");
5359         else
5360                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5361                             "[BTCoex], **********  WL is not connected before SCAN\n");
5362
5363         BTC_TRACE(trace_buf);
5364
5365         /*  this can't be removed for RF off_on event,
5366          * r BT would dis-connect
5367          */
5368         if (type != BTC_SCAN_FINISH) {
5369                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
5370                                            BT_8821C_2ANT_SCBD_SCAN |
5371                                            BT_8821C_2ANT_SCBD_ONOFF, TRUE);
5372         }
5373
5374         if (type == BTC_SCAN_START_5G) {
5375                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5376                             "[BTCoex], SCAN START notify (5G)\n");
5377                 BTC_TRACE(trace_buf);
5378
5379                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO,
5380                                              FC_EXCU, BT_8821C_2ANT_PHASE_5G);
5381
5382                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_5GSCANSTART);
5383         } else if (type == BTC_SCAN_START_2G || type == BTC_SCAN_START) {
5384                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5385                             "[BTCoex], SCAN START notify (2G)\n");
5386                 BTC_TRACE(trace_buf);
5387
5388                 if (!wifi_connected)
5389                         coex_sta->wifi_high_pri_task2 = TRUE;
5390
5391                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
5392                                              BT_8821C_2ANT_PHASE_2G);
5393
5394                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_2GSCANSTART);
5395         } else if (type == BTC_SCAN_FINISH) {
5396                 btc->btc_get(btc, BTC_GET_U1_AP_NUM, &coex_sta->scan_ap_num);
5397
5398                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5399                             "[BTCoex], SCAN FINISH notify  (Scan-AP = %d)\n",
5400                             coex_sta->scan_ap_num);
5401                 BTC_TRACE(trace_buf);
5402
5403                 coex_sta->wifi_high_pri_task2 = FALSE;
5404
5405                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_SCANFINISH);
5406         }
5407 }
5408
5409 void ex_halbtc8821c2ant_switchband_notify(struct btc_coexist *btc, u8 type)
5410 {
5411         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5412
5413         if (btc->manual_control || btc->stop_coex_dm)
5414                 return;
5415
5416         coex_sta->switch_band_notify_to = type;
5417
5418         if (type == BTC_SWITCH_TO_5G) {
5419                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5420                             "[BTCoex], switchband_notify ---  switch to 5G\n");
5421                 BTC_TRACE(trace_buf);
5422
5423                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_5GSWITCHBAND);
5424         } else if (type == BTC_SWITCH_TO_24G_NOFORSCAN) {
5425                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5426                         "[BTCoex], switchband_notify --- BTC_SWITCH_TO_2G (no for scan)\n");
5427                 BTC_TRACE(trace_buf);
5428
5429                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_2GSWITCHBAND);
5430         } else {
5431                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5432                             "[BTCoex], switchband_notify ---  switch to 2G\n");
5433                 BTC_TRACE(trace_buf);
5434
5435                 ex_halbtc8821c2ant_scan_notify(btc, BTC_SCAN_START_2G);
5436         }
5437
5438         coex_sta->switch_band_notify_to = BTC_NOT_SWITCH;
5439 }
5440
5441 void ex_halbtc8821c2ant_connect_notify(struct btc_coexist *btc,
5442                                        u8 type)
5443 {
5444         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5445         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
5446
5447         if (btc->manual_control || btc->stop_coex_dm)
5448                 return;
5449
5450         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
5451                                    BT_8821C_2ANT_SCBD_SCAN |
5452                                    BT_8821C_2ANT_SCBD_ONOFF, TRUE);
5453
5454         if (type == BTC_ASSOCIATE_5G_START ||
5455             type == BTC_ASSOCIATE_5G_FINISH) {
5456                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
5457                                              BT_8821C_2ANT_PHASE_5G);
5458
5459                 if (type == BTC_ASSOCIATE_5G_START) {
5460                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5461                                         "[BTCoex], CONNECT START notify (5G)\n");
5462                         BTC_TRACE(trace_buf);
5463
5464                         halbtc8821c2ant_run_coex(btc,
5465                                                  BT_8821C_2ANT_RSN_5GCONSTART);
5466                 } else {
5467                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5468                                 "[BTCoex], CONNECT FINISH notify (5G)\n");
5469                         BTC_TRACE(trace_buf);
5470
5471                         halbtc8821c2ant_run_coex(btc,
5472                                                  BT_8821C_2ANT_RSN_5GCONFINISH);
5473                 }
5474         } else if (type == BTC_ASSOCIATE_START) {
5475                 coex_sta->wifi_high_pri_task1 = TRUE;
5476
5477                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5478                             "[BTCoex], CONNECT START notify (2G)\n");
5479                 BTC_TRACE(trace_buf);
5480
5481                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
5482                                              BT_8821C_2ANT_PHASE_2G);
5483
5484                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_2GCONSTART);
5485
5486                 /* To keep TDMA case during connect process,
5487                 to avoid changed by Btinfo and runcoexmechanism */
5488                 coex_sta->freeze_coexrun_by_btinfo = TRUE;
5489                 coex_dm->arp_cnt = 0;
5490         } else if (type == BTC_ASSOCIATE_FINISH) {
5491                 coex_sta->wifi_high_pri_task1 = FALSE;
5492                 coex_sta->freeze_coexrun_by_btinfo = FALSE;
5493
5494                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5495                             "[BTCoex], CONNECT FINISH notify (2G)\n");
5496                 BTC_TRACE(trace_buf);
5497
5498                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_2GCONFINISH);
5499         }
5500 }
5501
5502 void ex_halbtc8821c2ant_media_status_notify(struct btc_coexist *btc, u8 type)
5503 {
5504         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5505         u8 h2c_parameter[3] = {0};
5506         boolean wifi_under_b_mode = FALSE;
5507         u16 ap_beacon_interval = 100;
5508
5509         if (btc->manual_control || btc->stop_coex_dm)
5510                 return;
5511
5512         if (type == BTC_MEDIA_CONNECT || type == BTC_MEDIA_CONNECT_5G) {
5513                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5514                             "[BTCoex], MEDIA connect notify\n");
5515                 BTC_TRACE(trace_buf);
5516
5517                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
5518                                            BT_8821C_2ANT_SCBD_ONOFF, TRUE);
5519
5520                 if (type == BTC_MEDIA_CONNECT_5G) {
5521                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5522                                     "[BTCoex], WiFi is under 5G!!!\n");
5523                         BTC_TRACE(trace_buf);
5524
5525                         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO,
5526                                                      FC_EXCU,
5527                                                      BT_8821C_2ANT_PHASE_5G);
5528
5529                         halbtc8821c2ant_run_coex(btc,
5530                                                  BT_8821C_2ANT_RSN_5GMEDIA);
5531                 } else {
5532                         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO,
5533                                                      FC_EXCU,
5534                                                      BT_8821C_2ANT_PHASE_2G);
5535
5536                         btc->btc_get(btc, BTC_GET_BL_WIFI_UNDER_B_MODE,
5537                                      &wifi_under_b_mode);
5538
5539                         /* Set CCK Tx/Rx high Pri except 11b mode */
5540                         if (wifi_under_b_mode) {
5541                                 /* CCK Tx */
5542                                 btc->btc_write_1byte(btc, 0x6cd, 0x00);
5543                                 /* CCK Rx */
5544                                 btc->btc_write_1byte(btc, 0x6cf, 0x00);
5545                         } else {
5546                                 /* CCK Tx */
5547                                 btc->btc_write_1byte(btc, 0x6cd, 0x00);
5548                                 /* CCK Rx */
5549                                 btc->btc_write_1byte(btc, 0x6cf, 0x10);
5550                         }
5551
5552                         btc->btc_get(btc, BTC_GET_U2_BEACON_PERIOD,
5553                                      &ap_beacon_interval);
5554
5555                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5556                                     "[BTCoex], AP beacon interval = %d\n",
5557                                     ap_beacon_interval);
5558                         BTC_TRACE(trace_buf);
5559
5560                         /* set TDMA waiting BI if BI is not equal to 100 */
5561                         if (ap_beacon_interval < 80 && ap_beacon_interval > 0) {
5562                                 h2c_parameter[0] = 0xb;
5563                                 h2c_parameter[1] = (100 / ap_beacon_interval);
5564
5565                                 if (100 % ap_beacon_interval != 0)
5566                                         h2c_parameter[1] = h2c_parameter[1] + 1;
5567                         } else if (ap_beacon_interval >= 180) {
5568                                 h2c_parameter[0] = 0xb;
5569                                 h2c_parameter[1] = (ap_beacon_interval / 100);
5570
5571                                 if (ap_beacon_interval % 100 <= 80)
5572                                         h2c_parameter[1] = h2c_parameter[1] - 1;
5573
5574                                 h2c_parameter[1] = h2c_parameter[1] | 0x80;
5575                         } else {
5576                                 h2c_parameter[0] = 0xb;
5577                                 h2c_parameter[1] = 0x1;
5578                         }
5579
5580                         btc->btc_fill_h2c(btc, 0x69, 2, h2c_parameter);
5581
5582                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5583                                     "[BTCoex], TDMA waiting BI = 0x%x\n",
5584                                     h2c_parameter[1]);
5585                         BTC_TRACE(trace_buf);
5586
5587                         halbtc8821c2ant_run_coex(btc,
5588                                                  BT_8821C_2ANT_RSN_2GMEDIA);
5589                 }
5590         } else {
5591                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5592                             "[BTCoex], MEDIA disconnect notify\n");
5593                 BTC_TRACE(trace_buf);
5594
5595                 btc->btc_write_1byte(btc, 0x6cd, 0x0); /* CCK Tx */
5596                 btc->btc_write_1byte(btc, 0x6cf, 0x0); /* CCK Rx */
5597
5598                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE,
5599                                            FALSE);
5600
5601                 coex_sta->cck_lock_ever = FALSE;
5602                 coex_sta->cck_lock_warn = FALSE;
5603                 coex_sta->cck_lock = FALSE;
5604
5605                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_MEDIADISCON);
5606         }
5607
5608         halbtc8821c2ant_update_wifi_ch_info(btc, type);
5609 }
5610
5611 void ex_halbtc8821c2ant_specific_packet_notify(struct btc_coexist *btc, u8 type)
5612 {
5613         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5614         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
5615         boolean under_4way = FALSE;
5616
5617         if (btc->manual_control || btc->stop_coex_dm)
5618                 return;
5619
5620         if (type & BTC_5G_BAND) {
5621                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5622                             "[BTCoex], 5g special packet notify\n");
5623                 BTC_TRACE(trace_buf);
5624
5625                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_5GSPECIALPKT);
5626                 return;
5627         }
5628
5629         btc->btc_get(btc, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &under_4way);
5630
5631         if (under_4way) {
5632                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5633                             "[BTCoex], specific Packet ---- under_4way!!\n");
5634                 BTC_TRACE(trace_buf);
5635
5636                 coex_sta->wifi_high_pri_task1 = TRUE;
5637                 coex_sta->specific_pkt_period_cnt = 2;
5638         } else if (type == BTC_PACKET_ARP) {
5639                 coex_dm->arp_cnt++;
5640                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5641                             "[BTCoex], specific Packet ARP notify -cnt = %d\n",
5642                             coex_dm->arp_cnt);
5643                 BTC_TRACE(trace_buf);
5644         } else {
5645                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5646                             "[BTCoex], specific Packet DHCP or EAPOL notify [Type = %d]\n",
5647                             type);
5648                 BTC_TRACE(trace_buf);
5649
5650                 coex_sta->wifi_high_pri_task1 = TRUE;
5651                 coex_sta->specific_pkt_period_cnt = 2;
5652         }
5653
5654         if (coex_sta->wifi_high_pri_task1) {
5655                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_SCAN, TRUE);
5656                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_2GSPECIALPKT);
5657         }
5658 }
5659
5660 void ex_halbtc8821c2ant_bt_info_notify(struct btc_coexist *btc, u8 *tmp_buf,
5661                                        u8 length)
5662 {
5663         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5664         u8 i, rsp_source = 0;
5665         boolean wifi_connected = FALSE;
5666         boolean wifi_busy = FALSE;
5667         static boolean is_scoreboard_scan;
5668         const u16 type_is_scan = BT_8821C_2ANT_SCBD_SCAN;
5669         u8 type;
5670
5671         rsp_source = tmp_buf[0] & 0xf;
5672         if (rsp_source >= BT_8821C_2ANT_INFO_SRC_MAX)
5673                 rsp_source = BT_8821C_2ANT_INFO_SRC_WIFI_FW;
5674         coex_sta->bt_info_c2h_cnt[rsp_source]++;
5675
5676         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5677                     "[BTCoex], Bt_info[%d], len=%d, data=[", rsp_source,
5678                     length);
5679         BTC_TRACE(trace_buf);
5680
5681         if (rsp_source == BT_8821C_2ANT_INFO_SRC_BT_RSP ||
5682             rsp_source == BT_8821C_2ANT_INFO_SRC_BT_ACT) {
5683                 if (coex_sta->bt_disabled) {
5684                         coex_sta->bt_disabled = FALSE;
5685                         coex_sta->is_bt_reenable = TRUE;
5686                         coex_sta->cnt_bt_reenable = 15;
5687
5688                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5689                                     "[BTCoex], BT enable detected by bt_info\n");
5690                         BTC_TRACE(trace_buf);
5691                 }
5692         }
5693
5694         for (i = 0; i < length; i++) {
5695                 coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
5696
5697                 if (i == length - 1) {
5698                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x]\n",
5699                                     tmp_buf[i]);
5700                         BTC_TRACE(trace_buf);
5701                 } else {
5702                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x, ",
5703                                     tmp_buf[i]);
5704                         BTC_TRACE(trace_buf);
5705                 }
5706         }
5707
5708         coex_sta->bt_info = coex_sta->bt_info_c2h[rsp_source][1];
5709         coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
5710         coex_sta->bt_info_ext2 = coex_sta->bt_info_c2h[rsp_source][5];
5711
5712         if (rsp_source != BT_8821C_2ANT_INFO_SRC_WIFI_FW) {
5713                 /* if 0xff, it means BT is under WHCK test */
5714                 coex_sta->bt_whck_test = ((coex_sta->bt_info == 0xff) ? TRUE :
5715                                           FALSE);
5716
5717                 coex_sta->bt_create_connection = ((
5718                         coex_sta->bt_info_c2h[rsp_source][2] & 0x80) ? TRUE :
5719                                                   FALSE);
5720
5721                 /* unit: %, value-100 to translate to unit: dBm */
5722                 coex_sta->bt_rssi = coex_sta->bt_info_c2h[rsp_source][3] * 2 +
5723                                     10;
5724
5725                 coex_sta->c2h_bt_remote_name_req = ((
5726                         coex_sta->bt_info_c2h[rsp_source][2] & 0x20) ? TRUE :
5727                                                     FALSE);
5728
5729                 coex_sta->is_A2DP_3M = ((coex_sta->bt_info_c2h[rsp_source][2] &
5730                                          0x10) ? TRUE : FALSE);
5731
5732                 coex_sta->acl_busy = ((coex_sta->bt_info_c2h[rsp_source][1] &
5733                                        0x8) ? TRUE : FALSE);
5734
5735                 coex_sta->voice_over_HOGP = ((coex_sta->bt_info_ext & 0x10) ?
5736                                              TRUE : FALSE);
5737
5738                 coex_sta->c2h_bt_inquiry_page = ((coex_sta->bt_info &
5739                           BT_INFO_8821C_2ANT_B_INQ_PAGE) ? TRUE : FALSE);
5740
5741                 coex_sta->a2dp_bit_pool = (((
5742                         coex_sta->bt_info_c2h[rsp_source][1] & 0x49) == 0x49) ?
5743                         (coex_sta->bt_info_c2h[rsp_source][6] & 0x7f) : 0);
5744
5745                 coex_sta->is_bt_a2dp_sink =
5746                         (coex_sta->bt_info_c2h[rsp_source][6] & 0x80) ?
5747                         TRUE : FALSE;
5748
5749                 coex_sta->bt_retry_cnt = coex_sta->bt_info_c2h[rsp_source][2] &
5750                                          0xf;
5751
5752                 coex_sta->is_autoslot = coex_sta->bt_info_ext2 & 0x8;
5753
5754                 coex_sta->forbidden_slot = coex_sta->bt_info_ext2 & 0x7;
5755
5756                 coex_sta->hid_busy_num = (coex_sta->bt_info_ext2 & 0x30) >> 4;
5757
5758                 coex_sta->hid_pair_cnt = (coex_sta->bt_info_ext2 & 0xc0) >> 6;
5759
5760                 coex_sta->is_bt_opp_exist =
5761                                 (coex_sta->bt_info_ext2 & 0x1) ? TRUE : FALSE;
5762
5763                 if (coex_sta->bt_retry_cnt >= 1)
5764                         coex_sta->pop_event_cnt++;
5765
5766                 if (coex_sta->c2h_bt_remote_name_req)
5767                         coex_sta->cnt_remote_name_req++;
5768
5769                 if (coex_sta->bt_info_ext & BIT(1))
5770                         coex_sta->cnt_reinit++;
5771
5772                 if (coex_sta->bt_info_ext & BIT(2) ||
5773                     (coex_sta->bt_create_connection &&
5774                     coex_sta->wl_pnp_wakeup_downcnt > 0)) {
5775                         coex_sta->cnt_setup_link++;
5776                         coex_sta->is_setup_link = TRUE;
5777                         if (coex_sta->is_bt_reenable)
5778                                 coex_sta->bt_relink_downcount = 6;
5779                         else
5780                                 coex_sta->bt_relink_downcount = 2;
5781                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5782                                 "[BTCoex], Re-Link start in BT info!!\n");
5783                         BTC_TRACE(trace_buf);
5784                 } else {
5785                         coex_sta->is_setup_link = FALSE;
5786                         if (!coex_sta->is_bt_reenable)
5787                                 coex_sta->bt_relink_downcount = 0;
5788                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5789                                     "[BTCoex], Re-Link stop in BT info!!\n");
5790                         BTC_TRACE(trace_buf);
5791                 }
5792
5793                 if (coex_sta->bt_info_ext & BIT(3))
5794                         coex_sta->cnt_ign_wlan_act++;
5795
5796                 if (coex_sta->bt_info_ext & BIT(6))
5797                         coex_sta->cnt_role_switch++;
5798
5799                 if (coex_sta->bt_info_ext & BIT(7))
5800                         coex_sta->is_bt_multi_link = TRUE;
5801                 else
5802                         coex_sta->is_bt_multi_link = FALSE;
5803
5804                 if (coex_sta->bt_info_ext & BIT(0))
5805                         coex_sta->is_hid_rcu = TRUE;
5806                 else
5807                         coex_sta->is_hid_rcu = FALSE;
5808
5809                 if (coex_sta->bt_info_ext & BIT(5))
5810                         coex_sta->is_ble_scan_en = TRUE;
5811                 else
5812                         coex_sta->is_ble_scan_en = FALSE;
5813
5814                 if (coex_sta->bt_create_connection) {
5815                         coex_sta->cnt_page++;
5816
5817                         btc->btc_get(btc, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
5818
5819                         if (coex_sta->is_wifi_linkscan_process ||
5820                             coex_sta->wifi_high_pri_task1 ||
5821                             coex_sta->wifi_high_pri_task2 ||
5822                             wifi_busy) {
5823                                 is_scoreboard_scan = TRUE;
5824                                 halbtc8821c2ant_write_scbd(btc, type_is_scan,
5825                                                            TRUE);
5826
5827                         } else
5828                                 halbtc8821c2ant_write_scbd(btc, type_is_scan,
5829                                                            FALSE);
5830
5831                 } else {
5832                         if (is_scoreboard_scan) {
5833                                 halbtc8821c2ant_write_scbd(btc, type_is_scan,
5834                                                            FALSE);
5835                                 is_scoreboard_scan = FALSE;
5836                         }
5837                 }
5838
5839                 /* Here we need to resend some wifi info to BT */
5840                 /* because bt is reset and loss of the info. */
5841                 btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
5842
5843                 /*  Re-Init */
5844                 if ((coex_sta->bt_info_ext & BIT(1))) {
5845                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5846                                     "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
5847                         BTC_TRACE(trace_buf);
5848                         if (wifi_connected)
5849                                 type = BTC_MEDIA_CONNECT;
5850                         else
5851                                 type = BTC_MEDIA_DISCONNECT;
5852                         halbtc8821c2ant_update_wifi_ch_info(btc, type);
5853                 }
5854
5855                 /*  If Ignore_WLanAct && not SetUp_Link */
5856                 if ((coex_sta->bt_info_ext & BIT(3)) &&
5857                     (!(coex_sta->bt_info_ext & BIT(2))) &&
5858                     (!(coex_sta->bt_info_ext & BIT(6)))) {
5859
5860                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5861                                     "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
5862                         BTC_TRACE(trace_buf);
5863                         halbtc8821c2ant_ignore_wlan_act(btc, FC_EXCU, FALSE);
5864                 } else {
5865                         if (coex_sta->bt_info_ext & BIT(2)) {
5866                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5867                                             "[BTCoex], BT ignore Wlan active because Re-link!!\n");
5868                                 BTC_TRACE(trace_buf);
5869                         } else if (coex_sta->bt_info_ext & BIT(6)) {
5870                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5871                                             "[BTCoex], BT ignore Wlan active because Role-Switch!!\n");
5872                                 BTC_TRACE(trace_buf);
5873                         }
5874                 }
5875         }
5876
5877         halbtc8821c2ant_update_bt_link_info(btc);
5878         halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_BTINFO);
5879 }
5880
5881 void ex_halbtc8821c2ant_wl_fwdbginfo_notify(struct btc_coexist *btc,
5882                                             u8 *tmp_buf, u8 length)
5883 {
5884         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5885         u8 i = 0;
5886         static u8 tmp_buf_pre[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
5887
5888         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5889                     "[BTCoex], WiFi Fw Dbg info = %d %d %d %d %d %d (len = %d)\n",
5890                     tmp_buf[0], tmp_buf[1], tmp_buf[2], tmp_buf[3], tmp_buf[4],
5891                     tmp_buf[5], length);
5892         BTC_TRACE(trace_buf);
5893
5894         if (tmp_buf[0] == 0x8) {
5895                 for (i = 1; i <= 5; i++) {
5896                         coex_sta->wl_fw_dbg_info[i] =
5897                                 (tmp_buf[i] >= tmp_buf_pre[i]) ?
5898                                 (tmp_buf[i] - tmp_buf_pre[i]) :
5899                                 (255 - tmp_buf_pre[i] + tmp_buf[i]);
5900
5901                         tmp_buf_pre[i] = tmp_buf[i];
5902                 }
5903         }
5904 }
5905
5906 void ex_halbtc8821c2ant_rx_rate_change_notify(struct btc_coexist *btc,
5907                                               BOOLEAN is_data_frame,
5908                                               u8 btc_rate_id)
5909 {
5910         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5911         struct coex_dm_8821c_2ant *coex_dm = &btc->coex_dm_8821c_2ant;
5912         BOOLEAN wifi_connected = FALSE;
5913
5914         btc->btc_get(btc, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
5915
5916         if (is_data_frame) {
5917                 coex_sta->wl_rx_rate = btc_rate_id;
5918
5919                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5920                             "[BTCoex], rx_rate_change_notify data rate id = %d, RTS_Rate = %d\n",
5921                             coex_sta->wl_rx_rate, coex_sta->wl_rts_rx_rate);
5922                 BTC_TRACE(trace_buf);
5923         } else {
5924                 coex_sta->wl_rts_rx_rate = btc_rate_id;
5925
5926                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5927                             "[BTCoex], rts_rate_change_notify RTS rate id = %d, RTS_Rate = %d\n",
5928                             coex_sta->wl_rts_rx_rate, coex_sta->wl_rts_rx_rate);
5929                 BTC_TRACE(trace_buf);
5930         }
5931
5932         if (wifi_connected &&
5933             (coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_ACL_BUSY ||
5934              coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_ACL_SCO_BUSY ||
5935              coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_SCO_BUSY)) {
5936                 if (coex_sta->wl_rx_rate == BTC_CCK_5_5 ||
5937                     coex_sta->wl_rx_rate == BTC_OFDM_6 ||
5938                     coex_sta->wl_rx_rate == BTC_MCS_0) {
5939                         coex_sta->cck_lock_warn = TRUE;
5940
5941                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5942                                     "[BTCoex], cck lock warning...\n");
5943                         BTC_TRACE(trace_buf);
5944                 } else if (coex_sta->wl_rx_rate == BTC_CCK_1 ||
5945                            coex_sta->wl_rx_rate == BTC_CCK_2 ||
5946                            coex_sta->wl_rts_rx_rate == BTC_CCK_1 ||
5947                            coex_sta->wl_rts_rx_rate == BTC_CCK_2) {
5948                         coex_sta->cck_lock = TRUE;
5949
5950                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5951                                     "[BTCoex], cck locking...\n");
5952                         BTC_TRACE(trace_buf);
5953                 } else {
5954                         coex_sta->cck_lock_warn = FALSE;
5955                         coex_sta->cck_lock = FALSE;
5956
5957                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5958                                     "[BTCoex], cck unlock...\n");
5959                         BTC_TRACE(trace_buf);
5960                 }
5961         } else {
5962                 if (coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_CON_IDLE ||
5963                     coex_dm->bt_status == BT_8821C_2ANT_BSTATUS_NCON_IDLE) {
5964                         coex_sta->cck_lock_warn = FALSE;
5965                         coex_sta->cck_lock = FALSE;
5966                 }
5967         }
5968 }
5969
5970 void ex_halbtc8821c2ant_tx_rate_change_notify(struct btc_coexist *btc,
5971                                               u8 tx_rate, u8 tx_retry_ratio,
5972                                               u8 macid)
5973 {
5974         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5975
5976         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5977                     "[BTCoex], tx_rate_change_notify Tx_Rate = %d, Tx_Retry_Ratio = %d, macid =%d\n",
5978                     tx_rate, tx_retry_ratio, macid);
5979         BTC_TRACE(trace_buf);
5980
5981         coex_sta->wl_tx_rate = tx_rate;
5982         coex_sta->wl_tx_retry_ratio = tx_retry_ratio;
5983         coex_sta->wl_tx_macid = macid;
5984 }
5985
5986 void ex_halbtc8821c2ant_rf_status_notify(struct btc_coexist *btc, u8 type)
5987 {
5988         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
5989
5990         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], RF Status notify\n");
5991         BTC_TRACE(trace_buf);
5992
5993         if (type == BTC_RF_ON) {
5994                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5995                             "[BTCoex], RF is turned ON!!\n");
5996                 BTC_TRACE(trace_buf);
5997
5998                 btc->stop_coex_dm = FALSE;
5999                 coex_sta->is_rf_state_off = FALSE;
6000         } else if (type == BTC_RF_OFF) {
6001                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6002                             "[BTCoex], RF is turned OFF!!\n");
6003                 BTC_TRACE(trace_buf);
6004
6005                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
6006                                            BT_8821C_2ANT_SCBD_ONOFF |
6007                                            BT_8821C_2ANT_SCBD_SCAN |
6008                                            BT_8821C_2ANT_SCBD_UNDERTEST,
6009                                            FALSE);
6010
6011                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
6012                                              BT_8821C_2ANT_PHASE_WOFF);
6013
6014                 halbtc8821c2ant_action_coex_all_off(btc);
6015
6016                 btc->stop_coex_dm = TRUE;
6017                 coex_sta->is_rf_state_off = TRUE;
6018                 /* must place in the last step */
6019                 halbtc8821c2ant_update_wifi_ch_info(btc, BTC_MEDIA_DISCONNECT);
6020         }
6021 }
6022
6023 void ex_halbtc8821c2ant_halt_notify(struct btc_coexist *btc)
6024 {
6025         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Halt notify\n");
6026         BTC_TRACE(trace_buf);
6027
6028         halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
6029                                    BT_8821C_2ANT_SCBD_ONOFF |
6030                                    BT_8821C_2ANT_SCBD_SCAN |
6031                                    BT_8821C_2ANT_SCBD_UNDERTEST,
6032                                    FALSE);
6033
6034         halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
6035                                      BT_8821C_2ANT_PHASE_WOFF);
6036
6037         halbtc8821c2ant_action_coex_all_off(btc);
6038
6039         btc->stop_coex_dm = TRUE;
6040
6041         /* must place in the last step */
6042         halbtc8821c2ant_update_wifi_ch_info(btc, BTC_MEDIA_DISCONNECT);
6043 }
6044
6045 void ex_halbtc8821c2ant_pnp_notify(struct btc_coexist *btc, u8 pnp_state)
6046 {
6047         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
6048         struct wifi_link_info_8821c_2ant *wifi_link_info_ext =
6049                                          &btc->wifi_link_info_8821c_2ant;
6050         static u8 pre_pnp_state;
6051         u8 phase;
6052
6053         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Pnp notify\n");
6054         BTC_TRACE(trace_buf);
6055
6056         if (pnp_state == BTC_WIFI_PNP_SLEEP ||
6057             pnp_state == BTC_WIFI_PNP_SLEEP_KEEP_ANT) {
6058                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6059                             "[BTCoex], Pnp notify to SLEEP\n");
6060                 BTC_TRACE(trace_buf);
6061
6062                 /* Sinda 20150819, workaround for driver skip
6063                  * leave IPS/LPS to speed up sleep time.
6064                  * Driver do not leave IPS/LPS when driver is going to sleep,
6065                  * so BTCoexistence think wifi is still under IPS/LPS
6066                  * BT should clear UnderIPS/UnderLPS state to avoid mismatch
6067                  * state after wakeup.
6068                  */
6069                 coex_sta->under_ips = FALSE;
6070                 coex_sta->under_lps = FALSE;
6071
6072                 halbtc8821c2ant_write_scbd(btc, BT_8821C_2ANT_SCBD_ACTIVE |
6073                                            BT_8821C_2ANT_SCBD_ONOFF |
6074                                            BT_8821C_2ANT_SCBD_SCAN |
6075                                            BT_8821C_2ANT_SCBD_UNDERTEST,
6076                                            FALSE);
6077
6078                 if (pnp_state == BTC_WIFI_PNP_SLEEP_KEEP_ANT) {
6079                         if (wifi_link_info_ext->is_all_under_5g)
6080                                 phase = BT_8821C_2ANT_PHASE_5G;
6081                         else
6082                                 phase = BT_8821C_2ANT_PHASE_2G;
6083                 } else {
6084                         phase = BT_8821C_2ANT_PHASE_WOFF;
6085                 }
6086
6087                 halbtc8821c2ant_set_ant_path(btc, BTC_ANT_PATH_AUTO, FC_EXCU,
6088                                              phase);
6089                 btc->stop_coex_dm = TRUE;
6090         } else {
6091                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6092                             "[BTCoex], Pnp notify to WAKE UP\n");
6093                 BTC_TRACE(trace_buf);
6094                 coex_sta->wl_pnp_wakeup_downcnt = 3;
6095
6096                 /*WoWLAN*/
6097                 if (pre_pnp_state == BTC_WIFI_PNP_SLEEP_KEEP_ANT ||
6098                     pnp_state == BTC_WIFI_PNP_WOWLAN) {
6099                         coex_sta->run_time_state = TRUE;
6100                         btc->stop_coex_dm = FALSE;
6101                         halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_PNP);
6102                 }
6103         }
6104
6105         pre_pnp_state = pnp_state;
6106 }
6107
6108 void ex_halbtc8821c2ant_periodical(struct btc_coexist *btc)
6109 {
6110         struct coex_sta_8821c_2ant *coex_sta = &btc->coex_sta_8821c_2ant;
6111         struct  btc_board_info  *board_info = &btc->board_info;
6112         boolean bt_relink_finish = FALSE, is_defreeze = FALSE;
6113         static u8 freeze_cnt;
6114
6115         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6116                     "[BTCoex], ************* Periodical *************\n");
6117         BTC_TRACE(trace_buf);
6118
6119         if (!btc->auto_report)
6120                 halbtc8821c2ant_query_bt_info(btc);
6121
6122         halbtc8821c2ant_monitor_bt_ctr(btc);
6123         halbtc8821c2ant_monitor_wifi_ctr(btc);
6124         halbtc8821c2ant_update_wifi_link_info(btc,
6125                                               BT_8821C_2ANT_RSN_PERIODICAL);
6126         halbtc8821c2ant_monitor_bt_enable(btc);
6127
6128         if (coex_sta->bt_relink_downcount != 0) {
6129                 coex_sta->bt_relink_downcount--;
6130                 if (coex_sta->bt_relink_downcount == 0) {
6131                         coex_sta->is_setup_link = FALSE;
6132                         bt_relink_finish = TRUE;
6133                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6134                                     "[BTCoex], Re-Link stop by periodical count-down!!\n");
6135                         BTC_TRACE(trace_buf);
6136                 }
6137         }
6138
6139         if (coex_sta->freeze_coexrun_by_btinfo) {
6140                 freeze_cnt++;
6141
6142                 if (freeze_cnt >= 5) {
6143                         freeze_cnt = 0;
6144                         coex_sta->freeze_coexrun_by_btinfo = FALSE;
6145                         is_defreeze = TRUE;
6146                 }
6147         } else {
6148                 freeze_cnt = 0;
6149         }
6150
6151         /* for 4-way, DHCP, EAPOL packet */
6152         if (coex_sta->specific_pkt_period_cnt > 0) {
6153                 coex_sta->specific_pkt_period_cnt--;
6154
6155                 if (!coex_sta->freeze_coexrun_by_btinfo &&
6156                     coex_sta->specific_pkt_period_cnt == 0)
6157                         coex_sta->wifi_high_pri_task1 = FALSE;
6158
6159                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6160                             "[BTCoex], Hi-Pri Task = %s\n",
6161                             (coex_sta->wifi_high_pri_task1 ? "Yes" : "No"));
6162                 BTC_TRACE(trace_buf);
6163         }
6164
6165         if (coex_sta->wl_pnp_wakeup_downcnt > 0) {
6166                 coex_sta->wl_pnp_wakeup_downcnt--;
6167                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6168                             "[BTCoex], wl_pnp_wakeup_downcnt = %d!!\n",
6169                             coex_sta->wl_pnp_wakeup_downcnt);
6170                 BTC_TRACE(trace_buf);
6171         }
6172
6173         if (coex_sta->cnt_bt_reenable > 0) {
6174                 coex_sta->cnt_bt_reenable--;
6175                 if (coex_sta->cnt_bt_reenable == 0) {
6176                         coex_sta->is_bt_reenable = FALSE;
6177                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
6178                                     "[BTCoex], BT renable 30s finish!!\n");
6179                         BTC_TRACE(trace_buf);
6180                 }
6181         }
6182
6183         if (halbtc8821c2ant_moniter_wifibt_status(btc) || bt_relink_finish ||
6184             coex_sta->is_set_ps_state_fail || is_defreeze)
6185                 halbtc8821c2ant_run_coex(btc, BT_8821C_2ANT_RSN_PERIODICAL);
6186 }
6187 /*#pragma optimize( "", off )*/
6188
6189 #endif
6190
6191 #endif  /*  #if (RTL8821C_SUPPORT == 1) */