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