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