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