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