1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2012  Realtek Corporation.*/
3 
4 /***************************************************************
5  * Description:
6  *
7  * This file is for RTL8723B Co-exist mechanism
8  *
9  * History
10  * 2012/11/15 Cosa first check in.
11  *
12  ***************************************************************/
13 
14 /***************************************************************
15  * include files
16  ***************************************************************/
17 #include "halbt_precomp.h"
18 /***************************************************************
19  * Global variables, these are static variables
20  ***************************************************************/
21 static struct coex_dm_8723b_1ant glcoex_dm_8723b_1ant;
22 static struct coex_dm_8723b_1ant *coex_dm = &glcoex_dm_8723b_1ant;
23 static struct coex_sta_8723b_1ant glcoex_sta_8723b_1ant;
24 static struct coex_sta_8723b_1ant *coex_sta = &glcoex_sta_8723b_1ant;
25 
26 static const char *const glbt_info_src_8723b_1ant[] = {
27 	"BT Info[wifi fw]",
28 	"BT Info[bt rsp]",
29 	"BT Info[bt auto report]",
30 };
31 
32 static u32 glcoex_ver_date_8723b_1ant = 20130918;
33 static u32 glcoex_ver_8723b_1ant = 0x47;
34 
35 /***************************************************************
36  * local function proto type if needed
37  ***************************************************************/
38 /***************************************************************
39  * local function start with halbtc8723b1ant_
40  ***************************************************************/
41 
halbtc8723b1ant_updatera_mask(struct btc_coexist * btcoexist,bool force_exec,u32 dis_rate_mask)42 static void halbtc8723b1ant_updatera_mask(struct btc_coexist *btcoexist,
43 					  bool force_exec, u32 dis_rate_mask)
44 {
45 	coex_dm->curra_mask = dis_rate_mask;
46 
47 	if (force_exec || (coex_dm->prera_mask != coex_dm->curra_mask))
48 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
49 				   &coex_dm->curra_mask);
50 
51 	coex_dm->prera_mask = coex_dm->curra_mask;
52 }
53 
btc8723b1ant_auto_rate_fb_retry(struct btc_coexist * btcoexist,bool force_exec,u8 type)54 static void btc8723b1ant_auto_rate_fb_retry(struct btc_coexist *btcoexist,
55 					    bool force_exec, u8 type)
56 {
57 	bool wifi_under_bmode = false;
58 
59 	coex_dm->cur_arfr_type = type;
60 
61 	if (force_exec || (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
62 		switch (coex_dm->cur_arfr_type) {
63 		case 0:	/* normal mode */
64 			btcoexist->btc_write_4byte(btcoexist, 0x430,
65 						   coex_dm->backup_arfr_cnt1);
66 			btcoexist->btc_write_4byte(btcoexist, 0x434,
67 						   coex_dm->backup_arfr_cnt2);
68 			break;
69 		case 1:
70 			btcoexist->btc_get(btcoexist,
71 					   BTC_GET_BL_WIFI_UNDER_B_MODE,
72 					   &wifi_under_bmode);
73 			if (wifi_under_bmode) {
74 				btcoexist->btc_write_4byte(btcoexist,
75 							   0x430, 0x0);
76 				btcoexist->btc_write_4byte(btcoexist,
77 							   0x434, 0x01010101);
78 			} else {
79 				btcoexist->btc_write_4byte(btcoexist,
80 							   0x430, 0x0);
81 				btcoexist->btc_write_4byte(btcoexist,
82 							   0x434, 0x04030201);
83 			}
84 			break;
85 		default:
86 			break;
87 		}
88 	}
89 
90 	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
91 }
92 
halbtc8723b1ant_retry_limit(struct btc_coexist * btcoexist,bool force_exec,u8 type)93 static void halbtc8723b1ant_retry_limit(struct btc_coexist *btcoexist,
94 					bool force_exec, u8 type)
95 {
96 	coex_dm->cur_retry_limit_type = type;
97 
98 	if (force_exec || (coex_dm->pre_retry_limit_type !=
99 			   coex_dm->cur_retry_limit_type)) {
100 		switch (coex_dm->cur_retry_limit_type) {
101 		case 0:	/* normal mode */
102 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
103 						   coex_dm->backup_retry_limit);
104 			break;
105 		case 1:	/* retry limit = 8 */
106 			btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
107 			break;
108 		default:
109 			break;
110 		}
111 	}
112 
113 	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
114 }
115 
halbtc8723b1ant_ampdu_maxtime(struct btc_coexist * btcoexist,bool force_exec,u8 type)116 static void halbtc8723b1ant_ampdu_maxtime(struct btc_coexist *btcoexist,
117 					  bool force_exec, u8 type)
118 {
119 	coex_dm->cur_ampdu_time_type = type;
120 
121 	if (force_exec || (coex_dm->pre_ampdu_time_type !=
122 		coex_dm->cur_ampdu_time_type)) {
123 		switch (coex_dm->cur_ampdu_time_type) {
124 		case 0:	/* normal mode */
125 			btcoexist->btc_write_1byte(btcoexist, 0x456,
126 					coex_dm->backup_ampdu_max_time);
127 			break;
128 		case 1:	/* AMPDU timw = 0x38 * 32us */
129 			btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
130 			break;
131 		default:
132 			break;
133 		}
134 	}
135 
136 	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
137 }
138 
halbtc8723b1ant_limited_tx(struct btc_coexist * btcoexist,bool force_exec,u8 ra_masktype,u8 arfr_type,u8 retry_limit_type,u8 ampdu_time_type)139 static void halbtc8723b1ant_limited_tx(struct btc_coexist *btcoexist,
140 				       bool force_exec, u8 ra_masktype,
141 				       u8 arfr_type, u8 retry_limit_type,
142 				       u8 ampdu_time_type)
143 {
144 	switch (ra_masktype) {
145 	case 0:	/* normal mode */
146 		halbtc8723b1ant_updatera_mask(btcoexist, force_exec, 0x0);
147 		break;
148 	case 1:	/* disable cck 1/2 */
149 		halbtc8723b1ant_updatera_mask(btcoexist, force_exec,
150 					      0x00000003);
151 		break;
152 	/* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
153 	case 2:
154 		halbtc8723b1ant_updatera_mask(btcoexist, force_exec,
155 					      0x0001f1f7);
156 		break;
157 	default:
158 		break;
159 	}
160 
161 	btc8723b1ant_auto_rate_fb_retry(btcoexist, force_exec, arfr_type);
162 	halbtc8723b1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
163 	halbtc8723b1ant_ampdu_maxtime(btcoexist, force_exec, ampdu_time_type);
164 }
165 
halbtc8723b1ant_limited_rx(struct btc_coexist * btcoexist,bool force_exec,bool rej_ap_agg_pkt,bool bt_ctrl_agg_buf_size,u8 agg_buf_size)166 static void halbtc8723b1ant_limited_rx(struct btc_coexist *btcoexist,
167 				       bool force_exec, bool rej_ap_agg_pkt,
168 				       bool bt_ctrl_agg_buf_size,
169 				       u8 agg_buf_size)
170 {
171 	bool reject_rx_agg = rej_ap_agg_pkt;
172 	bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
173 	u8 rxaggsize = agg_buf_size;
174 
175 	/**********************************************
176 	 *	Rx Aggregation related setting
177 	 **********************************************/
178 	btcoexist->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
179 			   &reject_rx_agg);
180 	/* decide BT control aggregation buf size or not  */
181 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
182 			   &bt_ctrl_rx_agg_size);
183 	/* aggregation buf size, only work
184 	 * when BT control Rx aggregation size.
185 	 */
186 	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rxaggsize);
187 	/* real update aggregation setting  */
188 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
189 }
190 
halbtc8723b1ant_query_bt_info(struct btc_coexist * btcoexist)191 static void halbtc8723b1ant_query_bt_info(struct btc_coexist *btcoexist)
192 {
193 	u8 h2c_parameter[1] = {0};
194 
195 	coex_sta->c2h_bt_info_req_sent = true;
196 
197 	/* trigger */
198 	h2c_parameter[0] |= BIT(0);
199 
200 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
201 }
202 
halbtc8723b1ant_monitor_bt_ctr(struct btc_coexist * btcoexist)203 static void halbtc8723b1ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
204 {
205 	u32 reg_hp_txrx, reg_lp_txrx, u32tmp;
206 	u32 reg_hp_tx = 0, reg_hp_rx = 0;
207 	u32 reg_lp_tx = 0, reg_lp_rx = 0;
208 	static u32 num_of_bt_counter_chk;
209 
210 	reg_hp_txrx = 0x770;
211 	reg_lp_txrx = 0x774;
212 
213 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
214 	reg_hp_tx = u32tmp & MASKLWORD;
215 	reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
216 
217 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
218 	reg_lp_tx = u32tmp & MASKLWORD;
219 	reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
220 
221 	coex_sta->high_priority_tx = reg_hp_tx;
222 	coex_sta->high_priority_rx = reg_hp_rx;
223 	coex_sta->low_priority_tx = reg_lp_tx;
224 	coex_sta->low_priority_rx = reg_lp_rx;
225 
226 	if ((coex_sta->low_priority_tx > 1050) &&
227 	    (!coex_sta->c2h_bt_inquiry_page))
228 		coex_sta->pop_event_cnt++;
229 
230 	/* reset counter */
231 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
232 
233 	/* This part is for wifi FW and driver to update BT's status as
234 	 * disabled.
235 	 *
236 	 * The flow is as the following
237 	 * 1. disable BT
238 	 * 2. if all BT Tx/Rx counter = 0, after 6 sec we query bt info
239 	 * 3. Because BT will not rsp from mailbox, so wifi fw will know BT is
240 	 * disabled
241 	 *
242 	 * 4. FW will rsp c2h for BT that driver will know BT is disabled.
243 	 */
244 	if ((reg_hp_tx == 0) && (reg_hp_rx == 0) && (reg_lp_tx == 0) &&
245 	    (reg_lp_rx == 0)) {
246 		num_of_bt_counter_chk++;
247 		if (num_of_bt_counter_chk == 3)
248 			halbtc8723b1ant_query_bt_info(btcoexist);
249 	} else {
250 		num_of_bt_counter_chk = 0;
251 	}
252 }
253 
halbtc8723b1ant_monitor_wifi_ctr(struct btc_coexist * btcoexist)254 static void halbtc8723b1ant_monitor_wifi_ctr(struct btc_coexist *btcoexist)
255 {
256 	s32 wifi_rssi = 0;
257 	bool wifi_busy = false, wifi_under_b_mode = false;
258 	static u8 cck_lock_counter;
259 	u32 total_cnt;
260 
261 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
262 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
263 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
264 			   &wifi_under_b_mode);
265 
266 	if (coex_sta->under_ips) {
267 		coex_sta->crc_ok_cck = 0;
268 		coex_sta->crc_ok_11g = 0;
269 		coex_sta->crc_ok_11n = 0;
270 		coex_sta->crc_ok_11n_agg = 0;
271 
272 		coex_sta->crc_err_cck = 0;
273 		coex_sta->crc_err_11g = 0;
274 		coex_sta->crc_err_11n = 0;
275 		coex_sta->crc_err_11n_agg = 0;
276 	} else {
277 		coex_sta->crc_ok_cck =
278 			btcoexist->btc_read_4byte(btcoexist, 0xf88);
279 		coex_sta->crc_ok_11g =
280 			btcoexist->btc_read_2byte(btcoexist, 0xf94);
281 		coex_sta->crc_ok_11n =
282 			btcoexist->btc_read_2byte(btcoexist, 0xf90);
283 		coex_sta->crc_ok_11n_agg =
284 			btcoexist->btc_read_2byte(btcoexist, 0xfb8);
285 
286 		coex_sta->crc_err_cck =
287 			btcoexist->btc_read_4byte(btcoexist, 0xf84);
288 		coex_sta->crc_err_11g =
289 			btcoexist->btc_read_2byte(btcoexist, 0xf96);
290 		coex_sta->crc_err_11n =
291 			btcoexist->btc_read_2byte(btcoexist, 0xf92);
292 		coex_sta->crc_err_11n_agg =
293 			btcoexist->btc_read_2byte(btcoexist, 0xfba);
294 	}
295 
296 	/* reset counter */
297 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x1);
298 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x0);
299 
300 	if ((wifi_busy) && (wifi_rssi >= 30) && (!wifi_under_b_mode)) {
301 		total_cnt = coex_sta->crc_ok_cck + coex_sta->crc_ok_11g +
302 			    coex_sta->crc_ok_11n + coex_sta->crc_ok_11n_agg;
303 
304 		if ((coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) ||
305 		    (coex_dm->bt_status ==
306 		     BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) ||
307 		    (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY)) {
308 			if (coex_sta->crc_ok_cck >
309 			    (total_cnt - coex_sta->crc_ok_cck)) {
310 				if (cck_lock_counter < 3)
311 					cck_lock_counter++;
312 			} else {
313 				if (cck_lock_counter > 0)
314 					cck_lock_counter--;
315 			}
316 
317 		} else {
318 			if (cck_lock_counter > 0)
319 				cck_lock_counter--;
320 		}
321 	} else {
322 		if (cck_lock_counter > 0)
323 			cck_lock_counter--;
324 	}
325 
326 	if (!coex_sta->pre_ccklock) {
327 		if (cck_lock_counter >= 3)
328 			coex_sta->cck_lock = true;
329 		else
330 			coex_sta->cck_lock = false;
331 	} else {
332 		if (cck_lock_counter == 0)
333 			coex_sta->cck_lock = false;
334 		else
335 			coex_sta->cck_lock = true;
336 	}
337 
338 	if (coex_sta->cck_lock)
339 		coex_sta->cck_ever_lock = true;
340 
341 	coex_sta->pre_ccklock = coex_sta->cck_lock;
342 }
343 
btc8723b1ant_is_wifi_status_changed(struct btc_coexist * btcoexist)344 static bool btc8723b1ant_is_wifi_status_changed(struct btc_coexist *btcoexist)
345 {
346 	static bool pre_wifi_busy;
347 	static bool pre_under_4way, pre_bt_hs_on;
348 	bool wifi_busy = false, under_4way = false, bt_hs_on = false;
349 	bool wifi_connected = false;
350 
351 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
352 			   &wifi_connected);
353 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
354 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
355 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
356 			   &under_4way);
357 
358 	if (wifi_connected) {
359 		if (wifi_busy != pre_wifi_busy) {
360 			pre_wifi_busy = wifi_busy;
361 			return true;
362 		}
363 		if (under_4way != pre_under_4way) {
364 			pre_under_4way = under_4way;
365 			return true;
366 		}
367 		if (bt_hs_on != pre_bt_hs_on) {
368 			pre_bt_hs_on = bt_hs_on;
369 			return true;
370 		}
371 	}
372 
373 	return false;
374 }
375 
halbtc8723b1ant_update_bt_link_info(struct btc_coexist * btcoexist)376 static void halbtc8723b1ant_update_bt_link_info(struct btc_coexist *btcoexist)
377 {
378 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
379 	bool bt_hs_on = false;
380 
381 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
382 
383 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
384 	bt_link_info->sco_exist = coex_sta->sco_exist;
385 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
386 	bt_link_info->pan_exist = coex_sta->pan_exist;
387 	bt_link_info->hid_exist = coex_sta->hid_exist;
388 	bt_link_info->bt_hi_pri_link_exist = coex_sta->bt_hi_pri_link_exist;
389 
390 	/* work around for HS mode. */
391 	if (bt_hs_on) {
392 		bt_link_info->pan_exist = true;
393 		bt_link_info->bt_link_exist = true;
394 	}
395 
396 	/* check if Sco only */
397 	if (bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
398 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
399 		bt_link_info->sco_only = true;
400 	else
401 		bt_link_info->sco_only = false;
402 
403 	/* check if A2dp only */
404 	if (!bt_link_info->sco_exist && bt_link_info->a2dp_exist &&
405 	    !bt_link_info->pan_exist && !bt_link_info->hid_exist)
406 		bt_link_info->a2dp_only = true;
407 	else
408 		bt_link_info->a2dp_only = false;
409 
410 	/* check if Pan only */
411 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
412 	    bt_link_info->pan_exist && !bt_link_info->hid_exist)
413 		bt_link_info->pan_only = true;
414 	else
415 		bt_link_info->pan_only = false;
416 
417 	/* check if Hid only */
418 	if (!bt_link_info->sco_exist && !bt_link_info->a2dp_exist &&
419 	    !bt_link_info->pan_exist && bt_link_info->hid_exist)
420 		bt_link_info->hid_only = true;
421 	else
422 		bt_link_info->hid_only = false;
423 }
424 
halbtc8723b1ant_set_bt_auto_report(struct btc_coexist * btcoexist,bool enable_auto_report)425 static void halbtc8723b1ant_set_bt_auto_report(struct btc_coexist *btcoexist,
426 					       bool enable_auto_report)
427 {
428 	u8 h2c_parameter[1] = {0};
429 
430 	h2c_parameter[0] = 0;
431 
432 	if (enable_auto_report)
433 		h2c_parameter[0] |= BIT(0);
434 
435 	btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
436 }
437 
halbtc8723b1ant_bt_auto_report(struct btc_coexist * btcoexist,bool force_exec,bool enable_auto_report)438 static void halbtc8723b1ant_bt_auto_report(struct btc_coexist *btcoexist,
439 					   bool force_exec,
440 					   bool enable_auto_report)
441 {
442 	coex_dm->cur_bt_auto_report = enable_auto_report;
443 
444 	if (!force_exec) {
445 		if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
446 			return;
447 	}
448 	halbtc8723b1ant_set_bt_auto_report(btcoexist,
449 					   coex_dm->cur_bt_auto_report);
450 
451 	coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
452 }
453 
btc8723b1ant_set_sw_pen_tx_rate_adapt(struct btc_coexist * btcoexist,bool low_penalty_ra)454 static void btc8723b1ant_set_sw_pen_tx_rate_adapt(struct btc_coexist *btcoexist,
455 						  bool low_penalty_ra)
456 {
457 	struct rtl_priv *rtlpriv = btcoexist->adapter;
458 	u8 h2c_parameter[6] = {0};
459 
460 	h2c_parameter[0] = 0x6;	/* opCode, 0x6= Retry_Penalty */
461 
462 	if (low_penalty_ra) {
463 		h2c_parameter[1] |= BIT0;
464 		/* normal rate except MCS7/6/5, OFDM54/48/36 */
465 		h2c_parameter[2] = 0x00;
466 		h2c_parameter[3] = 0xf7;  /* MCS7 or OFDM54 */
467 		h2c_parameter[4] = 0xf8;  /* MCS6 or OFDM48 */
468 		h2c_parameter[5] = 0xf9;  /* MCS5 or OFDM36 */
469 	}
470 
471 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
472 		"[BTCoex], set WiFi Low-Penalty Retry: %s",
473 		(low_penalty_ra ? "ON!!" : "OFF!!"));
474 
475 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
476 }
477 
halbtc8723b1ant_low_penalty_ra(struct btc_coexist * btcoexist,bool force_exec,bool low_penalty_ra)478 static void halbtc8723b1ant_low_penalty_ra(struct btc_coexist *btcoexist,
479 					   bool force_exec, bool low_penalty_ra)
480 {
481 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
482 
483 	if (!force_exec) {
484 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
485 			return;
486 	}
487 	btc8723b1ant_set_sw_pen_tx_rate_adapt(btcoexist,
488 					      coex_dm->cur_low_penalty_ra);
489 
490 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
491 }
492 
halbtc8723b1ant_set_coex_table(struct btc_coexist * btcoexist,u32 val0x6c0,u32 val0x6c4,u32 val0x6c8,u8 val0x6cc)493 static void halbtc8723b1ant_set_coex_table(struct btc_coexist *btcoexist,
494 					   u32 val0x6c0, u32 val0x6c4,
495 					   u32 val0x6c8, u8 val0x6cc)
496 {
497 	struct rtl_priv *rtlpriv = btcoexist->adapter;
498 
499 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
500 		"[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0);
501 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
502 
503 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
504 		"[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4);
505 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
506 
507 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
508 		"[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8);
509 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
510 
511 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
512 		"[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc);
513 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
514 }
515 
halbtc8723b1ant_coex_table(struct btc_coexist * btcoexist,bool force_exec,u32 val0x6c0,u32 val0x6c4,u32 val0x6c8,u8 val0x6cc)516 static void halbtc8723b1ant_coex_table(struct btc_coexist *btcoexist,
517 				       bool force_exec, u32 val0x6c0,
518 				       u32 val0x6c4, u32 val0x6c8,
519 				       u8 val0x6cc)
520 {
521 	struct rtl_priv *rtlpriv = btcoexist->adapter;
522 
523 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
524 		"[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6cc = 0x%x\n",
525 		 (force_exec ? "force to" : ""),
526 		 val0x6c0, val0x6c4, val0x6cc);
527 	coex_dm->cur_val0x6c0 = val0x6c0;
528 	coex_dm->cur_val0x6c4 = val0x6c4;
529 	coex_dm->cur_val0x6c8 = val0x6c8;
530 	coex_dm->cur_val0x6cc = val0x6cc;
531 
532 	if (!force_exec) {
533 		if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
534 		    (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
535 		    (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
536 		    (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
537 			return;
538 	}
539 	halbtc8723b1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
540 				       val0x6c8, val0x6cc);
541 
542 	coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
543 	coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
544 	coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
545 	coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
546 }
547 
halbtc8723b1ant_coex_table_with_type(struct btc_coexist * btcoexist,bool force_exec,u8 type)548 static void halbtc8723b1ant_coex_table_with_type(struct btc_coexist *btcoexist,
549 						 bool force_exec, u8 type)
550 {
551 	coex_sta->coex_table_type = type;
552 
553 	switch (type) {
554 	case 0:
555 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55555555,
556 					   0x55555555, 0xffffff, 0x3);
557 		break;
558 	case 1:
559 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55555555,
560 					   0x5a5a5a5a, 0xffffff, 0x3);
561 		break;
562 	case 2:
563 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
564 					   0x5a5a5a5a, 0xffffff, 0x3);
565 		break;
566 	case 3:
567 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55555555,
568 					   0x5a5a5a5a, 0xffffff, 0x3);
569 		break;
570 	case 4:
571 		if ((coex_sta->cck_ever_lock) && (coex_sta->scan_ap_num <= 5))
572 			halbtc8723b1ant_coex_table(btcoexist, force_exec,
573 						   0x55555555, 0xaaaa5a5a,
574 						   0xffffff, 0x3);
575 		else
576 			halbtc8723b1ant_coex_table(btcoexist, force_exec,
577 						   0x55555555, 0x5a5a5a5a,
578 						   0xffffff, 0x3);
579 		break;
580 	case 5:
581 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
582 					   0x5aaa5a5a, 0xffffff, 0x3);
583 		break;
584 	case 6:
585 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55555555,
586 					   0xaaaaaaaa, 0xffffff, 0x3);
587 		break;
588 	case 7:
589 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0xaaaaaaaa,
590 					   0xaaaaaaaa, 0xffffff, 0x3);
591 		break;
592 	case 8:
593 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
594 					   0x5ada5ada, 0xffffff, 0x3);
595 		break;
596 	case 9:
597 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
598 					   0x5ada5ada, 0xffffff, 0x3);
599 		break;
600 	case 10:
601 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
602 					   0x5ada5ada, 0xffffff, 0x3);
603 		break;
604 	case 11:
605 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
606 					   0x5ada5ada, 0xffffff, 0x3);
607 		break;
608 	case 12:
609 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
610 					   0x5ada5ada, 0xffffff, 0x3);
611 		break;
612 	case 13:
613 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
614 					   0xaaaaaaaa, 0xffffff, 0x3);
615 		break;
616 	case 14:
617 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
618 					   0x5ada5ada, 0xffffff, 0x3);
619 		break;
620 	case 15:
621 		halbtc8723b1ant_coex_table(btcoexist, force_exec, 0x55dd55dd,
622 					   0xaaaaaaaa, 0xffffff, 0x3);
623 		break;
624 	default:
625 		break;
626 	}
627 }
628 
629 static void
halbtc8723b1ant_set_fw_ignore_wlan_act(struct btc_coexist * btcoexist,bool enable)630 halbtc8723b1ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
631 				       bool enable)
632 {
633 	struct rtl_priv *rtlpriv = btcoexist->adapter;
634 	u8 h2c_parameter[1] = {0};
635 
636 	if (enable)
637 		h2c_parameter[0] |= BIT0;	/* function enable */
638 
639 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
640 		"[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
641 		h2c_parameter[0]);
642 
643 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
644 }
645 
halbtc8723b1ant_ignore_wlan_act(struct btc_coexist * btcoexist,bool force_exec,bool enable)646 static void halbtc8723b1ant_ignore_wlan_act(struct btc_coexist *btcoexist,
647 					    bool force_exec, bool enable)
648 {
649 	struct rtl_priv *rtlpriv = btcoexist->adapter;
650 
651 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
652 		"[BTCoex], %s turn Ignore WlanAct %s\n",
653 		(force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
654 	coex_dm->cur_ignore_wlan_act = enable;
655 
656 	if (!force_exec) {
657 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
658 			"[BTCoex], bPreIgnoreWlanAct = %d, bCurIgnoreWlanAct = %d!!\n",
659 			coex_dm->pre_ignore_wlan_act,
660 			 coex_dm->cur_ignore_wlan_act);
661 
662 		if (coex_dm->pre_ignore_wlan_act ==
663 		    coex_dm->cur_ignore_wlan_act)
664 			return;
665 	}
666 	halbtc8723b1ant_set_fw_ignore_wlan_act(btcoexist, enable);
667 
668 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
669 }
670 
halbtc8723b1ant_set_fw_ps_tdma(struct btc_coexist * btcoexist,u8 byte1,u8 byte2,u8 byte3,u8 byte4,u8 byte5)671 static void halbtc8723b1ant_set_fw_ps_tdma(struct btc_coexist *btcoexist,
672 					   u8 byte1, u8 byte2, u8 byte3,
673 					   u8 byte4, u8 byte5)
674 {
675 	struct rtl_priv *rtlpriv = btcoexist->adapter;
676 	u8 h2c_parameter[5] = {0};
677 	u8 real_byte1 = byte1, real_byte5 = byte5;
678 	bool ap_enable = false;
679 
680 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
681 			   &ap_enable);
682 
683 	if (ap_enable) {
684 		if ((byte1 & BIT4) && !(byte1 & BIT5)) {
685 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
686 				"[BTCoex], FW for 1Ant AP mode\n");
687 			real_byte1 &= ~BIT4;
688 			real_byte1 |= BIT5;
689 
690 			real_byte5 |= BIT5;
691 			real_byte5 &= ~BIT6;
692 		}
693 	}
694 
695 	h2c_parameter[0] = real_byte1;
696 	h2c_parameter[1] = byte2;
697 	h2c_parameter[2] = byte3;
698 	h2c_parameter[3] = byte4;
699 	h2c_parameter[4] = real_byte5;
700 
701 	coex_dm->ps_tdma_para[0] = real_byte1;
702 	coex_dm->ps_tdma_para[1] = byte2;
703 	coex_dm->ps_tdma_para[2] = byte3;
704 	coex_dm->ps_tdma_para[3] = byte4;
705 	coex_dm->ps_tdma_para[4] = real_byte5;
706 
707 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
708 		"[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
709 		h2c_parameter[0],
710 		h2c_parameter[1] << 24 |
711 		h2c_parameter[2] << 16 |
712 		h2c_parameter[3] << 8 |
713 		h2c_parameter[4]);
714 
715 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
716 }
717 
halbtc8723b1ant_set_lps_rpwm(struct btc_coexist * btcoexist,u8 lps_val,u8 rpwm_val)718 static void halbtc8723b1ant_set_lps_rpwm(struct btc_coexist *btcoexist,
719 					 u8 lps_val, u8 rpwm_val)
720 {
721 	u8 lps = lps_val;
722 	u8 rpwm = rpwm_val;
723 
724 	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
725 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
726 }
727 
halbtc8723b1ant_lps_rpwm(struct btc_coexist * btcoexist,bool force_exec,u8 lps_val,u8 rpwm_val)728 static void halbtc8723b1ant_lps_rpwm(struct btc_coexist *btcoexist,
729 				     bool force_exec,
730 				     u8 lps_val, u8 rpwm_val)
731 {
732 	struct rtl_priv *rtlpriv = btcoexist->adapter;
733 
734 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
735 		"[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
736 		(force_exec ? "force to" : ""), lps_val, rpwm_val);
737 	coex_dm->cur_lps = lps_val;
738 	coex_dm->cur_rpwm = rpwm_val;
739 
740 	if (!force_exec) {
741 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
742 			"[BTCoex], LPS-RxBeaconMode = 0x%x , LPS-RPWM = 0x%x!!\n",
743 			coex_dm->cur_lps, coex_dm->cur_rpwm);
744 
745 		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
746 		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm)) {
747 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
748 				"[BTCoex], LPS-RPWM_Last = 0x%x , LPS-RPWM_Now = 0x%x!!\n",
749 				coex_dm->pre_rpwm, coex_dm->cur_rpwm);
750 
751 			return;
752 		}
753 	}
754 	halbtc8723b1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
755 
756 	coex_dm->pre_lps = coex_dm->cur_lps;
757 	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
758 }
759 
halbtc8723b1ant_sw_mechanism(struct btc_coexist * btcoexist,bool low_penalty_ra)760 static void halbtc8723b1ant_sw_mechanism(struct btc_coexist *btcoexist,
761 					 bool low_penalty_ra)
762 {
763 	struct rtl_priv *rtlpriv = btcoexist->adapter;
764 
765 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
766 		"[BTCoex], SM[LpRA] = %d\n", low_penalty_ra);
767 
768 	halbtc8723b1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
769 }
770 
halbtc8723b1ant_set_ant_path(struct btc_coexist * btcoexist,u8 ant_pos_type,bool force_exec,bool init_hw_cfg,bool wifi_off)771 static void halbtc8723b1ant_set_ant_path(struct btc_coexist *btcoexist,
772 					 u8 ant_pos_type, bool force_exec,
773 					 bool init_hw_cfg, bool wifi_off)
774 {
775 	struct rtl_priv *rtlpriv = btcoexist->adapter;
776 	struct btc_board_info *board_info = &btcoexist->board_info;
777 	u32 fw_ver = 0, u32tmp = 0, cnt_bt_cal_chk = 0;
778 	bool pg_ext_switch = false;
779 	bool use_ext_switch = false;
780 	bool is_in_mp_mode = false;
781 	u8 h2c_parameter[2] = {0}, u8tmp = 0;
782 
783 	coex_dm->cur_ant_pos_type = ant_pos_type;
784 
785 	btcoexist->btc_get(btcoexist, BTC_GET_BL_EXT_SWITCH, &pg_ext_switch);
786 	/* [31:16] = fw ver, [15:0] = fw sub ver */
787 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
788 
789 	if ((fw_ver < 0xc0000) || pg_ext_switch)
790 		use_ext_switch = true;
791 
792 	if (init_hw_cfg) {
793 		/* WiFi TRx Mask on */
794 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff,
795 					  0x780);
796 		/* remove due to interrupt is disabled that polling c2h will
797 		 * fail and delay 100ms.
798 		 */
799 
800 		if (fw_ver >= 0x180000) {
801 			/* Use H2C to set GNT_BT to HIGH */
802 			h2c_parameter[0] = 1;
803 			btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1,
804 						h2c_parameter);
805 		} else {
806 			/* set grant_bt to high */
807 			btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
808 		}
809 		/* set wlan_act control by PTA */
810 		btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
811 
812 		/* BT select s0/s1 is controlled by BT */
813 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x20, 0x0);
814 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x39, 0x8, 0x1);
815 		btcoexist->btc_write_1byte(btcoexist, 0x974, 0xff);
816 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x944, 0x3, 0x3);
817 		btcoexist->btc_write_1byte(btcoexist, 0x930, 0x77);
818 	} else if (wifi_off) {
819 		if (fw_ver >= 0x180000) {
820 			/* Use H2C to set GNT_BT to HIGH */
821 			h2c_parameter[0] = 1;
822 			btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1,
823 						h2c_parameter);
824 		} else {
825 			/* set grant_bt to high */
826 			btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
827 		}
828 		/* set wlan_act to always low */
829 		btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
830 
831 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE,
832 				   &is_in_mp_mode);
833 		if (!is_in_mp_mode)
834 			/* BT select s0/s1 is controlled by BT */
835 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67,
836 							   0x20, 0x0);
837 		else
838 			/* BT select s0/s1 is controlled by WiFi */
839 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67,
840 							   0x20, 0x1);
841 
842 		/* 0x4c[24:23]=00, Set Antenna control by BT_RFE_CTRL
843 		 * BT Vendor 0xac=0xf002
844 		 */
845 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
846 		u32tmp &= ~BIT23;
847 		u32tmp &= ~BIT24;
848 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
849 	} else {
850 		/* Use H2C to set GNT_BT to LOW */
851 		if (fw_ver >= 0x180000) {
852 			if (btcoexist->btc_read_1byte(btcoexist, 0x765) != 0) {
853 				h2c_parameter[0] = 0;
854 				btcoexist->btc_fill_h2c(btcoexist, 0x6E, 1,
855 							h2c_parameter);
856 			}
857 		} else {
858 			/* BT calibration check */
859 			while (cnt_bt_cal_chk <= 20) {
860 				u8tmp = btcoexist->btc_read_1byte(btcoexist,
861 								  0x49d);
862 				cnt_bt_cal_chk++;
863 				if (u8tmp & BIT(0)) {
864 					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
865 						DBG_LOUD,
866 						"[BTCoex], ########### BT is calibrating (wait cnt=%d) ###########\n",
867 						cnt_bt_cal_chk);
868 					mdelay(50);
869 				} else {
870 					rtl_dbg(rtlpriv, COMP_BT_COEXIST,
871 						DBG_LOUD,
872 						"[BTCoex], ********** BT is NOT calibrating (wait cnt=%d)**********\n",
873 						cnt_bt_cal_chk);
874 					break;
875 				}
876 			}
877 
878 			/* set grant_bt to PTA */
879 			btcoexist->btc_write_1byte(btcoexist, 0x765, 0x0);
880 		}
881 
882 		if (btcoexist->btc_read_1byte(btcoexist, 0x76e) != 0xc) {
883 			/* set wlan_act control by PTA */
884 			btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
885 		}
886 
887 		btcoexist->btc_write_1byte_bitmask(
888 			btcoexist, 0x67, 0x20,
889 			0x1); /* BT select s0/s1 is controlled by WiFi */
890 	}
891 
892 	if (use_ext_switch) {
893 		if (init_hw_cfg) {
894 			/* 0x4c[23] = 0, 0x4c[24] = 1
895 			 * Antenna control by WL/BT
896 			 */
897 			u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
898 			u32tmp &= ~BIT23;
899 			u32tmp |= BIT24;
900 			btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
901 
902 			/* fixed internal switch S1->WiFi, S0->BT */
903 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
904 
905 			if (board_info->btdm_ant_pos ==
906 			    BTC_ANTENNA_AT_MAIN_PORT) {
907 				/* tell firmware "no antenna inverse" */
908 				h2c_parameter[0] = 0;
909 				/* ext switch type */
910 				h2c_parameter[1] = 1;
911 				btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
912 							h2c_parameter);
913 			} else {
914 				/* tell firmware "antenna inverse" */
915 				h2c_parameter[0] = 1;
916 				/* ext switch type */
917 				h2c_parameter[1] = 1;
918 				btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
919 							h2c_parameter);
920 			}
921 		}
922 
923 		if (force_exec ||
924 		    (coex_dm->cur_ant_pos_type != coex_dm->pre_ant_pos_type)) {
925 			/* ext switch setting */
926 			switch (ant_pos_type) {
927 			case BTC_ANT_PATH_WIFI:
928 				if (board_info->btdm_ant_pos ==
929 				    BTC_ANTENNA_AT_MAIN_PORT)
930 					btcoexist->btc_write_1byte_bitmask(
931 						btcoexist, 0x92c, 0x3, 0x1);
932 				else
933 					btcoexist->btc_write_1byte_bitmask(
934 						btcoexist, 0x92c, 0x3, 0x2);
935 				break;
936 			case BTC_ANT_PATH_BT:
937 				if (board_info->btdm_ant_pos ==
938 				    BTC_ANTENNA_AT_MAIN_PORT)
939 					btcoexist->btc_write_1byte_bitmask(
940 						btcoexist, 0x92c, 0x3, 0x2);
941 				else
942 					btcoexist->btc_write_1byte_bitmask(
943 						btcoexist, 0x92c, 0x3, 0x1);
944 				break;
945 			default:
946 			case BTC_ANT_PATH_PTA:
947 				if (board_info->btdm_ant_pos ==
948 				    BTC_ANTENNA_AT_MAIN_PORT)
949 					btcoexist->btc_write_1byte_bitmask(
950 						btcoexist, 0x92c, 0x3, 0x1);
951 				else
952 					btcoexist->btc_write_1byte_bitmask(
953 						btcoexist, 0x92c, 0x3, 0x2);
954 				break;
955 			}
956 		}
957 	} else {
958 		if (init_hw_cfg) {
959 			/* 0x4c[23] = 1, 0x4c[24] = 0,
960 			 * Antenna control by 0x64
961 			 */
962 			u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
963 			u32tmp |= BIT23;
964 			u32tmp &= ~BIT24;
965 			btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
966 
967 			/* Fix Ext switch Main->S1, Aux->S0 */
968 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64, 0x1,
969 							   0x0);
970 
971 			if (board_info->btdm_ant_pos ==
972 			    BTC_ANTENNA_AT_MAIN_PORT) {
973 				/* tell firmware "no antenna inverse" */
974 				h2c_parameter[0] = 0;
975 				/* internal switch type */
976 				h2c_parameter[1] = 0;
977 				btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
978 							h2c_parameter);
979 			} else {
980 				/* tell firmware "antenna inverse" */
981 				h2c_parameter[0] = 1;
982 				/* internal switch type */
983 				h2c_parameter[1] = 0;
984 				btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
985 							h2c_parameter);
986 			}
987 		}
988 
989 		if (force_exec ||
990 		    (coex_dm->cur_ant_pos_type != coex_dm->pre_ant_pos_type)) {
991 			/* internal switch setting */
992 			switch (ant_pos_type) {
993 			case BTC_ANT_PATH_WIFI:
994 				if (board_info->btdm_ant_pos ==
995 				    BTC_ANTENNA_AT_MAIN_PORT)
996 					btcoexist->btc_write_4byte(btcoexist,
997 							0x948, 0x0);
998 				else
999 					btcoexist->btc_write_4byte(btcoexist,
1000 							0x948, 0x280);
1001 				break;
1002 			case BTC_ANT_PATH_BT:
1003 				if (board_info->btdm_ant_pos ==
1004 				    BTC_ANTENNA_AT_MAIN_PORT)
1005 					btcoexist->btc_write_4byte(btcoexist,
1006 							0x948, 0x280);
1007 				else
1008 					btcoexist->btc_write_4byte(btcoexist,
1009 							0x948, 0x0);
1010 				break;
1011 			default:
1012 			case BTC_ANT_PATH_PTA:
1013 				if (board_info->btdm_ant_pos ==
1014 				    BTC_ANTENNA_AT_MAIN_PORT)
1015 					btcoexist->btc_write_4byte(btcoexist,
1016 							0x948, 0x200);
1017 				else
1018 					btcoexist->btc_write_4byte(btcoexist,
1019 							0x948, 0x80);
1020 				break;
1021 			}
1022 		}
1023 	}
1024 
1025 	coex_dm->pre_ant_pos_type = coex_dm->cur_ant_pos_type;
1026 }
1027 
halbtc8723b1ant_ps_tdma(struct btc_coexist * btcoexist,bool force_exec,bool turn_on,u8 type)1028 static void halbtc8723b1ant_ps_tdma(struct btc_coexist *btcoexist,
1029 				    bool force_exec, bool turn_on, u8 type)
1030 {
1031 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1032 	bool wifi_busy = false;
1033 	u8 rssi_adjust_val = 0;
1034 	u8 ps_tdma_byte0_val = 0x51;
1035 	u8 ps_tdma_byte3_val = 0x10;
1036 	u8 ps_tdma_byte4_val = 0x50;
1037 	s8 wifi_duration_adjust = 0x0;
1038 	static bool pre_wifi_busy;
1039 
1040 	coex_dm->cur_ps_tdma_on = turn_on;
1041 	coex_dm->cur_ps_tdma = type;
1042 
1043 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1044 
1045 	if (wifi_busy != pre_wifi_busy) {
1046 		force_exec = true;
1047 		pre_wifi_busy = wifi_busy;
1048 	}
1049 
1050 	if (!force_exec) {
1051 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1052 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1053 			return;
1054 	}
1055 
1056 	if (coex_sta->scan_ap_num <= 5) {
1057 		wifi_duration_adjust = 5;
1058 
1059 		if (coex_sta->a2dp_bit_pool >= 35)
1060 			wifi_duration_adjust = -10;
1061 		else if (coex_sta->a2dp_bit_pool >= 45)
1062 			wifi_duration_adjust = -15;
1063 	} else if (coex_sta->scan_ap_num >= 40) {
1064 		wifi_duration_adjust = -15;
1065 
1066 		if (coex_sta->a2dp_bit_pool < 35)
1067 			wifi_duration_adjust = -5;
1068 		else if (coex_sta->a2dp_bit_pool < 45)
1069 			wifi_duration_adjust = -10;
1070 	} else if (coex_sta->scan_ap_num >= 20) {
1071 		wifi_duration_adjust = -10;
1072 
1073 		if (coex_sta->a2dp_bit_pool >= 45)
1074 			wifi_duration_adjust = -15;
1075 	} else {
1076 		wifi_duration_adjust = 0;
1077 
1078 		if (coex_sta->a2dp_bit_pool >= 35)
1079 			wifi_duration_adjust = -10;
1080 		else if (coex_sta->a2dp_bit_pool >= 45)
1081 			wifi_duration_adjust = -15;
1082 	}
1083 
1084 	if ((type == 1) || (type == 2) || (type == 9) || (type == 11) ||
1085 	    (type == 101) || (type == 102) || (type == 109) || (type == 111)) {
1086 		if (!coex_sta->force_lps_on) {
1087 			/* Native power save TDMA, only for A2DP-only case
1088 			 * 1/2/9/11 while wifi noisy threshold > 30
1089 			 */
1090 
1091 			/* no null-pkt */
1092 			ps_tdma_byte0_val = 0x61;
1093 			/* no tx-pause at BT-slot */
1094 			ps_tdma_byte3_val = 0x11;
1095 			/* 0x778 = d/1 toggle, no dynamic slot */
1096 			ps_tdma_byte4_val = 0x10;
1097 		} else {
1098 			/* null-pkt */
1099 			ps_tdma_byte0_val = 0x51;
1100 			/* tx-pause at BT-slot */
1101 			ps_tdma_byte3_val = 0x10;
1102 			/* 0x778 = d/1 toggle, dynamic slot */
1103 			ps_tdma_byte4_val = 0x50;
1104 		}
1105 	} else if ((type == 3) || (type == 13) || (type == 14) ||
1106 		   (type == 103) || (type == 113) || (type == 114)) {
1107 		/* null-pkt */
1108 		ps_tdma_byte0_val = 0x51;
1109 		/* tx-pause at BT-slot */
1110 		ps_tdma_byte3_val = 0x10;
1111 		/* 0x778 = d/1 toggle, no dynamic slot */
1112 		ps_tdma_byte4_val = 0x10;
1113 	} else { /* native power save case */
1114 		/* no null-pkt */
1115 		ps_tdma_byte0_val = 0x61;
1116 		/* no tx-pause at BT-slot */
1117 		ps_tdma_byte3_val = 0x11;
1118 		/* 0x778 = d/1 toggle, no dynamic slot */
1119 		ps_tdma_byte4_val = 0x11;
1120 		/* psTdmaByte4Va is not define for 0x778 = d/1, 1/1 case */
1121 	}
1122 
1123 	/* if (bt_link_info->slave_role) */
1124 	if ((bt_link_info->slave_role) && (bt_link_info->a2dp_exist))
1125 		/* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1126 		ps_tdma_byte4_val = ps_tdma_byte4_val | 0x1;
1127 
1128 	if (type > 100) {
1129 		/* set antenna control by SW	 */
1130 		ps_tdma_byte0_val = ps_tdma_byte0_val | 0x82;
1131 		/* set antenna no toggle, control by antenna diversity */
1132 		ps_tdma_byte3_val = ps_tdma_byte3_val | 0x60;
1133 	}
1134 
1135 	if (turn_on) {
1136 		switch (type) {
1137 		default:
1138 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1a,
1139 						      0x1a, 0x0,
1140 						      ps_tdma_byte4_val);
1141 			break;
1142 		case 1:
1143 			halbtc8723b1ant_set_fw_ps_tdma(
1144 				btcoexist, ps_tdma_byte0_val,
1145 				0x3a + wifi_duration_adjust, 0x03,
1146 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1147 
1148 			rssi_adjust_val = 11;
1149 			break;
1150 		case 2:
1151 			halbtc8723b1ant_set_fw_ps_tdma(
1152 				btcoexist, ps_tdma_byte0_val,
1153 				0x2d + wifi_duration_adjust, 0x03,
1154 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1155 			break;
1156 		case 3:
1157 			halbtc8723b1ant_set_fw_ps_tdma(
1158 				btcoexist, ps_tdma_byte0_val, 0x30, 0x03,
1159 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1160 			break;
1161 		case 4:
1162 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1163 						      0x3, 0x14, 0x0);
1164 			break;
1165 		case 5:
1166 			halbtc8723b1ant_set_fw_ps_tdma(
1167 				btcoexist, ps_tdma_byte0_val, 0x1f, 0x3,
1168 				ps_tdma_byte3_val, 0x11);
1169 			break;
1170 		case 6:
1171 			halbtc8723b1ant_set_fw_ps_tdma(
1172 				btcoexist, ps_tdma_byte0_val, 0x20, 0x3,
1173 				ps_tdma_byte3_val, 0x11);
1174 			break;
1175 		case 7:
1176 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xc,
1177 						       0x5, 0x0, 0x0);
1178 			break;
1179 		case 8:
1180 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1181 						      0x3, 0x10, 0x0);
1182 			break;
1183 		case 9:
1184 			halbtc8723b1ant_set_fw_ps_tdma(
1185 				btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1186 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1187 			break;
1188 		case 10:
1189 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1190 						       0xa, 0x0, 0x40);
1191 			break;
1192 		case 11:
1193 			halbtc8723b1ant_set_fw_ps_tdma(
1194 				btcoexist, ps_tdma_byte0_val, 0x21, 0x03,
1195 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1196 			break;
1197 		case 12:
1198 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x0a,
1199 						      0x0a, 0x0, 0x50);
1200 			break;
1201 		case 13:
1202 			if (coex_sta->scan_ap_num <= 3)
1203 				halbtc8723b1ant_set_fw_ps_tdma(
1204 					btcoexist, ps_tdma_byte0_val, 0x40, 0x3,
1205 					ps_tdma_byte3_val, ps_tdma_byte4_val);
1206 			else
1207 				halbtc8723b1ant_set_fw_ps_tdma(
1208 					btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1209 					ps_tdma_byte3_val, ps_tdma_byte4_val);
1210 			break;
1211 		case 14:
1212 			if (coex_sta->scan_ap_num <= 3)
1213 				halbtc8723b1ant_set_fw_ps_tdma(
1214 					btcoexist, 0x51, 0x30, 0x3, 0x10, 0x50);
1215 			else
1216 				halbtc8723b1ant_set_fw_ps_tdma(
1217 					btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1218 					ps_tdma_byte3_val, ps_tdma_byte4_val);
1219 			break;
1220 		case 15:
1221 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1222 						       0x3, 0x8, 0x0);
1223 			break;
1224 		case 16:
1225 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1226 						      0x3, 0x10, 0x0);
1227 			break;
1228 		case 18:
1229 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1230 						      0x3, 0x10, 0x0);
1231 			break;
1232 		case 20:
1233 			halbtc8723b1ant_set_fw_ps_tdma(
1234 				btcoexist, ps_tdma_byte0_val, 0x3f, 0x03,
1235 				ps_tdma_byte3_val, 0x10);
1236 			break;
1237 		case 21:
1238 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x25,
1239 						      0x03, 0x11, 0x11);
1240 			break;
1241 		case 22:
1242 			halbtc8723b1ant_set_fw_ps_tdma(
1243 				btcoexist, ps_tdma_byte0_val, 0x25, 0x03,
1244 				ps_tdma_byte3_val, 0x10);
1245 			break;
1246 		case 23:
1247 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1248 						      0x3, 0x31, 0x18);
1249 			break;
1250 		case 24:
1251 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1252 						      0x3, 0x31, 0x18);
1253 			break;
1254 		case 25:
1255 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1256 						       0x3, 0x31, 0x18);
1257 			break;
1258 		case 26:
1259 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1260 						       0x3, 0x31, 0x18);
1261 			break;
1262 		case 27:
1263 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1264 						      0x3, 0x31, 0x98);
1265 			break;
1266 		case 28:
1267 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x69, 0x25,
1268 						      0x3, 0x31, 0x0);
1269 			break;
1270 		case 29:
1271 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xab, 0x1a,
1272 						      0x1a, 0x1, 0x10);
1273 			break;
1274 		case 30:
1275 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x30,
1276 						       0x3, 0x10, 0x10);
1277 			break;
1278 		case 31:
1279 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x1a,
1280 						      0x1a, 0, 0x58);
1281 			break;
1282 		case 32:
1283 			halbtc8723b1ant_set_fw_ps_tdma(
1284 				btcoexist, ps_tdma_byte0_val, 0x35, 0x3,
1285 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1286 			break;
1287 		case 33:
1288 			halbtc8723b1ant_set_fw_ps_tdma(
1289 				btcoexist, ps_tdma_byte0_val, 0x35, 0x3,
1290 				ps_tdma_byte3_val, 0x10);
1291 			break;
1292 		case 34:
1293 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x53, 0x1a,
1294 						      0x1a, 0x0, 0x10);
1295 			break;
1296 		case 35:
1297 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x63, 0x1a,
1298 						      0x1a, 0x0, 0x10);
1299 			break;
1300 		case 36:
1301 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x12,
1302 						      0x3, 0x14, 0x50);
1303 			break;
1304 		case 40:
1305 			/* SoftAP only with no sta associated,BT disable ,TDMA
1306 			 * mode for power saving
1307 			 *
1308 			 * here softap mode screen off will cost 70-80mA for
1309 			 * phone
1310 			 */
1311 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x23, 0x18,
1312 						      0x00, 0x10, 0x24);
1313 			break;
1314 
1315 		case 101:
1316 			/* for 1-Ant translate to 2-Ant	 */
1317 			halbtc8723b1ant_set_fw_ps_tdma(
1318 				btcoexist, ps_tdma_byte0_val,
1319 				0x3a + wifi_duration_adjust, 0x03,
1320 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1321 			break;
1322 		case 102:
1323 			halbtc8723b1ant_set_fw_ps_tdma(
1324 				btcoexist, ps_tdma_byte0_val,
1325 				0x2d + wifi_duration_adjust, 0x03,
1326 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1327 			break;
1328 		case 103:
1329 			halbtc8723b1ant_set_fw_ps_tdma(
1330 				btcoexist, ps_tdma_byte0_val, 0x3a, 0x03,
1331 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1332 			break;
1333 		case 105:
1334 			halbtc8723b1ant_set_fw_ps_tdma(
1335 				btcoexist, ps_tdma_byte0_val, 0x15, 0x3,
1336 				ps_tdma_byte3_val, 0x11);
1337 			break;
1338 		case 106:
1339 			halbtc8723b1ant_set_fw_ps_tdma(
1340 				btcoexist, ps_tdma_byte0_val, 0x20, 0x3,
1341 				ps_tdma_byte3_val, 0x11);
1342 			break;
1343 		case 109:
1344 			halbtc8723b1ant_set_fw_ps_tdma(
1345 				btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1346 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1347 			break;
1348 		case 111:
1349 			halbtc8723b1ant_set_fw_ps_tdma(
1350 				btcoexist, ps_tdma_byte0_val, 0x21, 0x03,
1351 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1352 			break;
1353 		case 113:
1354 			halbtc8723b1ant_set_fw_ps_tdma(
1355 				btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1356 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1357 			break;
1358 		case 114:
1359 			halbtc8723b1ant_set_fw_ps_tdma(
1360 				btcoexist, ps_tdma_byte0_val, 0x21, 0x3,
1361 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1362 			break;
1363 		case 120:
1364 			halbtc8723b1ant_set_fw_ps_tdma(
1365 				btcoexist, ps_tdma_byte0_val, 0x3f, 0x03,
1366 				ps_tdma_byte3_val, 0x10);
1367 			break;
1368 		case 122:
1369 			halbtc8723b1ant_set_fw_ps_tdma(
1370 				btcoexist, ps_tdma_byte0_val, 0x25, 0x03,
1371 				ps_tdma_byte3_val, 0x10);
1372 			break;
1373 		case 132:
1374 			halbtc8723b1ant_set_fw_ps_tdma(
1375 				btcoexist, ps_tdma_byte0_val, 0x25, 0x03,
1376 				ps_tdma_byte3_val, ps_tdma_byte4_val);
1377 			break;
1378 		case 133:
1379 			halbtc8723b1ant_set_fw_ps_tdma(
1380 				btcoexist, ps_tdma_byte0_val, 0x25, 0x03,
1381 				ps_tdma_byte3_val, 0x11);
1382 			break;
1383 		}
1384 	} else {
1385 		/* disable PS tdma */
1386 		switch (type) {
1387 		case 8: /* PTA Control */
1388 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x8, 0x0,
1389 						       0x0, 0x0, 0x0);
1390 			halbtc8723b1ant_set_ant_path(btcoexist,
1391 						     BTC_ANT_PATH_PTA,
1392 						     FORCE_EXEC,
1393 						     false, false);
1394 			break;
1395 		case 0:
1396 		default:
1397 			/* Software control, Antenna at BT side */
1398 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0,
1399 						       0x0, 0x0, 0x0);
1400 			break;
1401 		case 1: /* 2-Ant, 0x778=3, antenna control by ant diversity */
1402 			halbtc8723b1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1403 						       0x48, 0x0);
1404 			break;
1405 		}
1406 	}
1407 	rssi_adjust_val = 0;
1408 	btcoexist->btc_set(btcoexist,
1409 			   BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE,
1410 			   &rssi_adjust_val);
1411 
1412 	/* update pre state */
1413 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1414 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1415 }
1416 
1417 static
btc8723b1ant_tdma_dur_adj_for_acl(struct btc_coexist * btcoexist,u8 wifi_status)1418 void btc8723b1ant_tdma_dur_adj_for_acl(struct btc_coexist *btcoexist,
1419 				       u8 wifi_status)
1420 {
1421 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1422 	static s32 up, dn, m, n, wait_count;
1423 	/*  0: no change, +1: increase WiFi duration,
1424 	 * -1: decrease WiFi duration
1425 	 */
1426 	s32 result;
1427 	u8 retry_count = 0;
1428 
1429 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1430 		"[BTCoex], TdmaDurationAdjustForAcl()\n");
1431 
1432 	if ((wifi_status ==
1433 	     BT_8723B_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN) ||
1434 	    (wifi_status == BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN) ||
1435 	    (wifi_status == BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT)) {
1436 		if (coex_dm->cur_ps_tdma != 1 && coex_dm->cur_ps_tdma != 2 &&
1437 		    coex_dm->cur_ps_tdma != 3 && coex_dm->cur_ps_tdma != 9) {
1438 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1439 						true, 9);
1440 			coex_dm->ps_tdma_du_adj_type = 9;
1441 
1442 			up = 0;
1443 			dn = 0;
1444 			m = 1;
1445 			n = 3;
1446 			result = 0;
1447 			wait_count = 0;
1448 		}
1449 		return;
1450 	}
1451 
1452 	if (!coex_dm->auto_tdma_adjust) {
1453 		coex_dm->auto_tdma_adjust = true;
1454 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1455 			"[BTCoex], first run TdmaDurationAdjust()!!\n");
1456 
1457 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1458 		coex_dm->ps_tdma_du_adj_type = 2;
1459 
1460 		up = 0;
1461 		dn = 0;
1462 		m = 1;
1463 		n = 3;
1464 		result = 0;
1465 		wait_count = 0;
1466 	} else {
1467 		/* acquire the BT TRx retry count from BT_Info byte2 */
1468 		retry_count = coex_sta->bt_retry_cnt;
1469 
1470 		if ((coex_sta->low_priority_tx) > 1050 ||
1471 		    (coex_sta->low_priority_rx) > 1250)
1472 			retry_count++;
1473 
1474 		result = 0;
1475 		wait_count++;
1476 		/* no retry in the last 2-second duration */
1477 		if (retry_count == 0) {
1478 			up++;
1479 			dn--;
1480 
1481 			if (dn <= 0)
1482 				dn = 0;
1483 
1484 			if (up >= n) {
1485 				/* if retry count during continuous n*2 seconds
1486 				 * is 0, enlarge WiFi duration
1487 				 */
1488 				wait_count = 0;
1489 				n = 3;
1490 				up = 0;
1491 				dn = 0;
1492 				result = 1;
1493 				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1494 					"[BTCoex], Increase wifi duration!!\n");
1495 			}
1496 		} else if (retry_count <= 3) {
1497 			/* <=3 retry in the last 2-second duration */
1498 			up--;
1499 			dn++;
1500 
1501 			if (up <= 0)
1502 				up = 0;
1503 
1504 			if (dn == 2) {
1505 				/* if continuous 2 retry count(every 2 seconds)
1506 				 * >0 and < 3, reduce WiFi duration
1507 				 */
1508 				if (wait_count <= 2)
1509 					/* avoid loop between the two levels */
1510 					m++;
1511 				else
1512 					m = 1;
1513 
1514 				if (m >= 20)
1515 					/* maximum of m = 20 ' will recheck if
1516 					 * need to adjust wifi duration in
1517 					 * maximum time interval 120 seconds
1518 					 */
1519 					m = 20;
1520 
1521 				n = 3 * m;
1522 				up = 0;
1523 				dn = 0;
1524 				wait_count = 0;
1525 				result = -1;
1526 				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1527 					"[BTCoex], Decrease wifi duration for retryCounter<3!!\n");
1528 			}
1529 		} else {
1530 			/* retry count > 3, once retry count > 3, to reduce
1531 			 * WiFi duration
1532 			 */
1533 			if (wait_count == 1)
1534 				/* to avoid loop between the two levels */
1535 				m++;
1536 			else
1537 				m = 1;
1538 
1539 			if (m >= 20)
1540 				/* maximum of m = 20 ' will recheck if need to
1541 				 * adjust wifi duration in maximum time interval
1542 				 * 120 seconds
1543 				 */
1544 				m = 20;
1545 
1546 			n = 3 * m;
1547 			up = 0;
1548 			dn = 0;
1549 			wait_count = 0;
1550 			result = -1;
1551 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1552 				"[BTCoex], Decrease wifi duration for retryCounter>3!!\n");
1553 		}
1554 
1555 		if (result == -1) {
1556 			if (coex_dm->cur_ps_tdma == 1) {
1557 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1558 							true, 2);
1559 				coex_dm->ps_tdma_du_adj_type = 2;
1560 			} else if (coex_dm->cur_ps_tdma == 2) {
1561 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1562 							true, 9);
1563 				coex_dm->ps_tdma_du_adj_type = 9;
1564 			} else if (coex_dm->cur_ps_tdma == 9) {
1565 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1566 							true, 11);
1567 				coex_dm->ps_tdma_du_adj_type = 11;
1568 			}
1569 		} else if (result == 1) {
1570 			if (coex_dm->cur_ps_tdma == 11) {
1571 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1572 							true, 9);
1573 				coex_dm->ps_tdma_du_adj_type = 9;
1574 			} else if (coex_dm->cur_ps_tdma == 9) {
1575 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1576 							true, 2);
1577 				coex_dm->ps_tdma_du_adj_type = 2;
1578 			} else if (coex_dm->cur_ps_tdma == 2) {
1579 				halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1580 							true, 1);
1581 				coex_dm->ps_tdma_du_adj_type = 1;
1582 			}
1583 		}
1584 
1585 		if (coex_dm->cur_ps_tdma != 1 && coex_dm->cur_ps_tdma != 2 &&
1586 		    coex_dm->cur_ps_tdma != 9 && coex_dm->cur_ps_tdma != 11) {
1587 			/* recover to previous adjust type */
1588 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1589 						coex_dm->ps_tdma_du_adj_type);
1590 		}
1591 	}
1592 }
1593 
halbtc8723b1ant_ps_tdma_chk_pwr_save(struct btc_coexist * btcoexist,bool new_ps_state)1594 static void halbtc8723b1ant_ps_tdma_chk_pwr_save(struct btc_coexist *btcoexist,
1595 					  bool new_ps_state)
1596 {
1597 	u8 lps_mode = 0x0;
1598 
1599 	btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1600 
1601 	if (lps_mode) {
1602 		/* already under LPS state */
1603 		if (new_ps_state) {
1604 			/* keep state under LPS, do nothing. */
1605 		} else {
1606 			/* will leave LPS state, turn off psTdma first */
1607 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1608 						false, 0);
1609 		}
1610 	} else {
1611 		/* NO PS state */
1612 		if (new_ps_state) {
1613 			/* will enter LPS state, turn off psTdma first */
1614 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1615 						false, 0);
1616 		} else {
1617 			/* keep state under NO PS state, do nothing. */
1618 		}
1619 	}
1620 }
1621 
halbtc8723b1ant_power_save_state(struct btc_coexist * btcoexist,u8 ps_type,u8 lps_val,u8 rpwm_val)1622 static void halbtc8723b1ant_power_save_state(struct btc_coexist *btcoexist,
1623 					     u8 ps_type, u8 lps_val,
1624 					     u8 rpwm_val)
1625 {
1626 	bool low_pwr_disable = false;
1627 
1628 	switch (ps_type) {
1629 	case BTC_PS_WIFI_NATIVE:
1630 		/* recover to original 32k low power setting */
1631 		low_pwr_disable = false;
1632 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1633 				   &low_pwr_disable);
1634 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1635 		coex_sta->force_lps_on = false;
1636 		break;
1637 	case BTC_PS_LPS_ON:
1638 		halbtc8723b1ant_ps_tdma_chk_pwr_save(btcoexist, true);
1639 		halbtc8723b1ant_lps_rpwm(btcoexist, NORMAL_EXEC, lps_val,
1640 					 rpwm_val);
1641 		/* when coex force to enter LPS, do not enter 32k low power */
1642 		low_pwr_disable = true;
1643 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1644 				   &low_pwr_disable);
1645 		/* power save must executed before psTdma */
1646 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1647 		coex_sta->force_lps_on = true;
1648 		break;
1649 	case BTC_PS_LPS_OFF:
1650 		halbtc8723b1ant_ps_tdma_chk_pwr_save(btcoexist, false);
1651 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1652 		coex_sta->force_lps_on = false;
1653 		break;
1654 	default:
1655 		break;
1656 	}
1657 }
1658 
halbtc8723b1ant_action_wifi_only(struct btc_coexist * btcoexist)1659 static void halbtc8723b1ant_action_wifi_only(struct btc_coexist *btcoexist)
1660 {
1661 	halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
1662 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
1663 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1664 				     FORCE_EXEC, false, false);
1665 }
1666 
1667 /* check if BT is disabled */
halbtc8723b1ant_monitor_bt_enable_disable(struct btc_coexist * btcoexist)1668 static void halbtc8723b1ant_monitor_bt_enable_disable(struct btc_coexist
1669 						      *btcoexist)
1670 {
1671 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1672 	static u32 bt_disable_cnt;
1673 	bool bt_active = true, bt_disabled = false;
1674 
1675 	if (coex_sta->high_priority_tx == 0 &&
1676 	    coex_sta->high_priority_rx == 0 && coex_sta->low_priority_tx == 0 &&
1677 	    coex_sta->low_priority_rx == 0)
1678 		bt_active = false;
1679 	if (coex_sta->high_priority_tx == 0xffff &&
1680 	    coex_sta->high_priority_rx == 0xffff &&
1681 	    coex_sta->low_priority_tx == 0xffff &&
1682 	    coex_sta->low_priority_rx == 0xffff)
1683 		bt_active = false;
1684 	if (bt_active) {
1685 		bt_disable_cnt = 0;
1686 		bt_disabled = false;
1687 	} else {
1688 		bt_disable_cnt++;
1689 		if (bt_disable_cnt >= 2)
1690 			bt_disabled = true;
1691 	}
1692 	if (coex_sta->bt_disabled != bt_disabled) {
1693 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1694 			"[BTCoex], BT is from %s to %s!!\n",
1695 			(coex_sta->bt_disabled ? "disabled" : "enabled"),
1696 			(bt_disabled ? "disabled" : "enabled"));
1697 
1698 		coex_sta->bt_disabled = bt_disabled;
1699 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1700 				   &bt_disabled);
1701 		if (bt_disabled) {
1702 			halbtc8723b1ant_action_wifi_only(btcoexist);
1703 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1704 					   NULL);
1705 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1706 					   NULL);
1707 		}
1708 	}
1709 }
1710 
1711 /*****************************************************
1712  *
1713  *	Non-Software Coex Mechanism start
1714  *
1715  *****************************************************/
1716 
halbtc8723b1ant_action_bt_whck_test(struct btc_coexist * btcoexist)1717 static void halbtc8723b1ant_action_bt_whck_test(struct btc_coexist *btcoexist)
1718 {
1719 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1720 					 0x0);
1721 
1722 	halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1723 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC,
1724 				     false, false);
1725 	halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1726 }
1727 
halbtc8723b1ant_action_wifi_multiport(struct btc_coexist * btcoexist)1728 static void halbtc8723b1ant_action_wifi_multiport(struct btc_coexist *btcoexist)
1729 {
1730 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1731 					 0x0, 0x0);
1732 
1733 	halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1734 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC,
1735 				     false, false);
1736 	halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1737 }
1738 
halbtc8723b1ant_action_hs(struct btc_coexist * btcoexist)1739 static void halbtc8723b1ant_action_hs(struct btc_coexist *btcoexist)
1740 {
1741 	halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1742 	halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1743 }
1744 
halbtc8723b1ant_action_bt_inquiry(struct btc_coexist * btcoexist)1745 static void halbtc8723b1ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1746 {
1747 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1748 	bool wifi_connected = false, ap_enable = false;
1749 	bool wifi_busy = false, bt_busy = false;
1750 
1751 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1752 			   &ap_enable);
1753 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1754 			   &wifi_connected);
1755 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1756 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1757 
1758 	if (coex_sta->bt_abnormal_scan) {
1759 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 33);
1760 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1761 	} else if (!wifi_connected && !coex_sta->wifi_is_high_pri_task) {
1762 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1763 						 0x0, 0x0);
1764 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1765 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1766 					     NORMAL_EXEC, false, false);
1767 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1768 	} else if (bt_link_info->sco_exist || bt_link_info->hid_exist ||
1769 		   bt_link_info->a2dp_exist) {
1770 		/* SCO/HID/A2DP busy */
1771 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1772 						 0x0, 0x0);
1773 		if (coex_sta->c2h_bt_remote_name_req)
1774 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1775 						33);
1776 		else
1777 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1778 						32);
1779 
1780 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1781 	} else if (bt_link_info->pan_exist || wifi_busy) {
1782 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1783 						 0x0, 0x0);
1784 		if (coex_sta->c2h_bt_remote_name_req)
1785 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1786 						33);
1787 		else
1788 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1789 						32);
1790 
1791 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1792 	} else {
1793 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1794 						 0x0, 0x0);
1795 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1796 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1797 					     NORMAL_EXEC, false, false);
1798 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1799 	}
1800 }
1801 
btc8723b1ant_act_bt_sco_hid_only_busy(struct btc_coexist * btcoexist,u8 wifi_status)1802 static void btc8723b1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
1803 						  u8 wifi_status)
1804 {
1805 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1806 	bool wifi_connected = false;
1807 
1808 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1809 			   &wifi_connected);
1810 
1811 	/* tdma and coex table */
1812 	if (bt_link_info->sco_exist) {
1813 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1814 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
1815 	} else {
1816 		/* HID */
1817 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 6);
1818 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
1819 	}
1820 }
1821 
halbtc8723b1ant_action_wifi_connected_bt_acl_busy(struct btc_coexist * btcoexist,u8 wifi_status)1822 static void halbtc8723b1ant_action_wifi_connected_bt_acl_busy(
1823 					struct btc_coexist *btcoexist,
1824 					u8 wifi_status)
1825 {
1826 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1827 
1828 	if ((coex_sta->low_priority_rx >= 950) && (!coex_sta->under_ips))
1829 		bt_link_info->slave_role = true;
1830 	else
1831 		bt_link_info->slave_role = false;
1832 
1833 	if (bt_link_info->hid_only) { /* HID */
1834 		btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist, wifi_status);
1835 		coex_dm->auto_tdma_adjust = false;
1836 		return;
1837 	} else if (bt_link_info->a2dp_only) { /* A2DP */
1838 		if (wifi_status == BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE) {
1839 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1840 						true, 32);
1841 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1842 							     NORMAL_EXEC, 4);
1843 			coex_dm->auto_tdma_adjust = false;
1844 		} else {
1845 			btc8723b1ant_tdma_dur_adj_for_acl(btcoexist,
1846 							  wifi_status);
1847 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1848 							     NORMAL_EXEC, 1);
1849 			coex_dm->auto_tdma_adjust = true;
1850 		}
1851 	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1852 		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1853 		    bt_link_info->pan_exist)) {
1854 		/* A2DP + PAN(OPP,FTP), HID + A2DP + PAN(OPP,FTP) */
1855 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
1856 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1857 		coex_dm->auto_tdma_adjust = false;
1858 	} else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1859 		/* HID + A2DP */
1860 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,	true, 14);
1861 		coex_dm->auto_tdma_adjust = false;
1862 
1863 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1864 	} else if (bt_link_info->pan_only ||
1865 			(bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1866 		/* PAN(OPP,FTP), HID + PAN(OPP,FTP) */
1867 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1868 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1869 		coex_dm->auto_tdma_adjust = false;
1870 	} else {
1871 		/* BT no-profile busy (0x9) */
1872 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 33);
1873 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1874 		coex_dm->auto_tdma_adjust = false;
1875 	}
1876 }
1877 
btc8723b1ant_action_wifi_not_conn(struct btc_coexist * btcoexist)1878 static void btc8723b1ant_action_wifi_not_conn(struct btc_coexist *btcoexist)
1879 {
1880 	/* power save state */
1881 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1882 					 0x0, 0x0);
1883 
1884 	/* tdma and coex table */
1885 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
1886 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC,
1887 				     false, false);
1888 	halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1889 }
1890 
1891 static void
btc8723b1ant_action_wifi_not_conn_scan(struct btc_coexist * btcoexist)1892 btc8723b1ant_action_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1893 {
1894 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1895 
1896 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1897 					 0x0, 0x0);
1898 
1899 	/* tdma and coex table */
1900 	if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
1901 		if (bt_link_info->a2dp_exist) {
1902 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1903 						true, 32);
1904 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1905 							     NORMAL_EXEC, 4);
1906 		} else if (bt_link_info->a2dp_exist) {
1907 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1908 						true, 22);
1909 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1910 							     NORMAL_EXEC, 4);
1911 		} else {
1912 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1913 						true, 20);
1914 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1915 							     NORMAL_EXEC, 1);
1916 		}
1917 	} else if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
1918 		   coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY){
1919 		btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
1920 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1921 	} else {
1922 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1923 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1924 					     NORMAL_EXEC, false, false);
1925 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1926 	}
1927 }
1928 
1929 static void
btc8723b1ant_act_wifi_not_conn_asso_auth(struct btc_coexist * btcoexist)1930 btc8723b1ant_act_wifi_not_conn_asso_auth(struct btc_coexist *btcoexist)
1931 {
1932 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1933 
1934 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1935 					 0x0, 0x0);
1936 
1937 	/* tdma and coex table */
1938 	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1939 	    (bt_link_info->a2dp_exist)) {
1940 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1941 		halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 4);
1942 	} else if (bt_link_info->pan_exist) {
1943 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1944 		halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 4);
1945 	} else {
1946 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1947 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1948 					     NORMAL_EXEC, false, false);
1949 		halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 2);
1950 	}
1951 }
1952 
btc8723b1ant_action_wifi_conn_scan(struct btc_coexist * btcoexist)1953 static void btc8723b1ant_action_wifi_conn_scan(struct btc_coexist *btcoexist)
1954 {
1955 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1956 
1957 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1958 					 0x0, 0x0);
1959 
1960 	/* tdma and coex table */
1961 	if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
1962 		if (bt_link_info->a2dp_exist) {
1963 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1964 						true, 32);
1965 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1966 							     NORMAL_EXEC, 4);
1967 		} else if (bt_link_info->a2dp_exist &&
1968 			   bt_link_info->pan_exist) {
1969 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1970 						true, 22);
1971 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1972 							     NORMAL_EXEC, 4);
1973 		} else {
1974 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1975 						true, 20);
1976 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1977 							     NORMAL_EXEC, 4);
1978 		}
1979 	} else if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
1980 		   coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
1981 		btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
1982 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1983 	} else {
1984 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1985 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1986 					     NORMAL_EXEC, false, false);
1987 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1988 	}
1989 }
1990 
halbtc8723b1ant_action_wifi_connected_special_packet(struct btc_coexist * btcoexist)1991 static void halbtc8723b1ant_action_wifi_connected_special_packet(
1992 						struct btc_coexist *btcoexist)
1993 {
1994 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1995 	bool wifi_busy = false;
1996 
1997 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1998 
1999 	/* no special packet process for both WiFi and BT very busy */
2000 	if ((wifi_busy) &&
2001 	    ((bt_link_info->pan_exist) || (coex_sta->num_of_profile >= 2)))
2002 		return;
2003 
2004 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2005 					 0x0, 0x0);
2006 
2007 	/* tdma and coex table */
2008 	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist)) {
2009 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
2010 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
2011 	} else if (bt_link_info->a2dp_exist) {
2012 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
2013 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2014 	} else if (bt_link_info->pan_exist) {
2015 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
2016 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2017 	} else {
2018 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2019 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2020 					     NORMAL_EXEC, false, false);
2021 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2022 	}
2023 }
2024 
halbtc8723b1ant_action_wifi_connected(struct btc_coexist * btcoexist)2025 static void halbtc8723b1ant_action_wifi_connected(struct btc_coexist *btcoexist)
2026 {
2027 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2028 	bool wifi_busy = false;
2029 	bool scan = false, link = false, roam = false;
2030 	bool under_4way = false, ap_enable = false;
2031 
2032 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2033 		"[BTCoex], CoexForWifiConnect()===>\n");
2034 
2035 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2036 			   &under_4way);
2037 	if (under_4way) {
2038 		halbtc8723b1ant_action_wifi_connected_special_packet(btcoexist);
2039 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2040 			"[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
2041 		return;
2042 	}
2043 
2044 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2045 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2046 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2047 
2048 	if (scan || link || roam) {
2049 		if (scan)
2050 			btc8723b1ant_action_wifi_conn_scan(btcoexist);
2051 		else
2052 			halbtc8723b1ant_action_wifi_connected_special_packet(
2053 								     btcoexist);
2054 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2055 			"[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
2056 		return;
2057 	}
2058 
2059 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
2060 			   &ap_enable);
2061 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2062 	/* power save state */
2063 	if (!ap_enable &&
2064 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY &&
2065 	    !btcoexist->bt_link_info.hid_only) {
2066 		if (btcoexist->bt_link_info.a2dp_only) {
2067 			if (!wifi_busy) {
2068 				halbtc8723b1ant_power_save_state(btcoexist,
2069 							 BTC_PS_WIFI_NATIVE,
2070 							 0x0, 0x0);
2071 			} else { /* busy */
2072 				if (coex_sta->scan_ap_num >=
2073 				    BT_8723B_1ANT_WIFI_NOISY_THRESH)
2074 					/* no force LPS, no PS-TDMA,
2075 					 * use pure TDMA
2076 					 */
2077 					halbtc8723b1ant_power_save_state(
2078 						btcoexist, BTC_PS_WIFI_NATIVE,
2079 						0x0, 0x0);
2080 				else
2081 					halbtc8723b1ant_power_save_state(
2082 						btcoexist, BTC_PS_LPS_ON, 0x50,
2083 						0x4);
2084 			}
2085 		} else if ((!coex_sta->pan_exist) && (!coex_sta->a2dp_exist) &&
2086 			   (!coex_sta->hid_exist))
2087 			halbtc8723b1ant_power_save_state(
2088 				btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2089 		else
2090 			halbtc8723b1ant_power_save_state(btcoexist,
2091 							 BTC_PS_LPS_ON,
2092 							 0x50, 0x4);
2093 	} else {
2094 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2095 						 0x0, 0x0);
2096 	}
2097 	/* tdma and coex table */
2098 	if (!wifi_busy) {
2099 		if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
2100 			halbtc8723b1ant_action_wifi_connected_bt_acl_busy(
2101 				btcoexist,
2102 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2103 		} else if (coex_dm->bt_status ==
2104 				BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2105 			   coex_dm->bt_status ==
2106 				BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
2107 			btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
2108 				     BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2109 		} else {
2110 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2111 						false, 8);
2112 			halbtc8723b1ant_set_ant_path(btcoexist,
2113 						     BTC_ANT_PATH_PTA,
2114 						     NORMAL_EXEC, false, false);
2115 			halbtc8723b1ant_coex_table_with_type(btcoexist,
2116 							     NORMAL_EXEC, 2);
2117 		}
2118 	} else {
2119 		if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
2120 			halbtc8723b1ant_action_wifi_connected_bt_acl_busy(
2121 				btcoexist,
2122 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2123 		} else if (coex_dm->bt_status ==
2124 				BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2125 			   coex_dm->bt_status ==
2126 				BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
2127 			btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
2128 				    BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2129 		} else {
2130 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2131 						true, 32);
2132 			halbtc8723b1ant_set_ant_path(btcoexist,
2133 						     BTC_ANT_PATH_PTA,
2134 						     NORMAL_EXEC, false, false);
2135 			halbtc8723b1ant_coex_table_with_type(btcoexist,
2136 							     NORMAL_EXEC, 4);
2137 		}
2138 	}
2139 }
2140 
halbtc8723b1ant_run_coexist_mechanism(struct btc_coexist * btcoexist)2141 static void halbtc8723b1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
2142 {
2143 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2144 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2145 	bool wifi_connected = false, bt_hs_on = false, wifi_busy = false;
2146 	bool increase_scan_dev_num = false;
2147 	bool bt_ctrl_agg_buf_size = false;
2148 	bool miracast_plus_bt = false;
2149 	u8 agg_buf_size = 5;
2150 	u8 iot_peer = BTC_IOT_PEER_UNKNOWN;
2151 	u32 wifi_link_status = 0;
2152 	u32 num_of_wifi_link = 0;
2153 	u32 wifi_bw;
2154 
2155 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2156 		"[BTCoex], RunCoexistMechanism()===>\n");
2157 
2158 	if (btcoexist->manual_control) {
2159 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2160 			"[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2161 		return;
2162 	}
2163 
2164 	if (btcoexist->stop_coex_dm) {
2165 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2166 			"[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2167 		return;
2168 	}
2169 
2170 	if (coex_sta->under_ips) {
2171 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2172 			"[BTCoex], wifi is under IPS !!!\n");
2173 		return;
2174 	}
2175 
2176 	if (coex_sta->bt_whck_test) {
2177 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2178 			"[BTCoex], wifi is under IPS !!!\n");
2179 		halbtc8723b1ant_action_bt_whck_test(btcoexist);
2180 		return;
2181 	}
2182 
2183 	if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY ||
2184 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2185 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY)
2186 		increase_scan_dev_num = true;
2187 
2188 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2189 			   &increase_scan_dev_num);
2190 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2191 			   &wifi_connected);
2192 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2193 
2194 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2195 			   &wifi_link_status);
2196 	num_of_wifi_link = wifi_link_status >> 16;
2197 
2198 	if (num_of_wifi_link >= 2 ||
2199 	    wifi_link_status & WIFI_P2P_GO_CONNECTED) {
2200 		if (bt_link_info->bt_link_exist) {
2201 			halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1,
2202 						   0, 1);
2203 			miracast_plus_bt = true;
2204 		} else {
2205 			halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0,
2206 						   0, 0);
2207 			miracast_plus_bt = false;
2208 		}
2209 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2210 				   &miracast_plus_bt);
2211 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2212 					   bt_ctrl_agg_buf_size, agg_buf_size);
2213 
2214 		if ((bt_link_info->a2dp_exist || wifi_busy) &&
2215 		    (coex_sta->c2h_bt_inquiry_page))
2216 			halbtc8723b1ant_action_bt_inquiry(btcoexist);
2217 		else
2218 			halbtc8723b1ant_action_wifi_multiport(btcoexist);
2219 
2220 		return;
2221 	}
2222 
2223 	miracast_plus_bt = false;
2224 	btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2225 			   &miracast_plus_bt);
2226 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2227 
2228 	if (bt_link_info->bt_link_exist && wifi_connected) {
2229 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1, 0, 1);
2230 
2231 		btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
2232 
2233 		if (iot_peer != BTC_IOT_PEER_CISCO &&
2234 		    iot_peer != BTC_IOT_PEER_BROADCOM) {
2235 			bool sco_exist = bt_link_info->sco_exist;
2236 
2237 			halbtc8723b1ant_limited_rx(btcoexist,
2238 						   NORMAL_EXEC, sco_exist,
2239 						   false, 0x5);
2240 		} else {
2241 			if (bt_link_info->sco_exist) {
2242 				halbtc8723b1ant_limited_rx(btcoexist,
2243 							   NORMAL_EXEC, true,
2244 							   false, 0x5);
2245 			} else {
2246 				if (wifi_bw == BTC_WIFI_BW_HT40)
2247 					halbtc8723b1ant_limited_rx(
2248 						btcoexist, NORMAL_EXEC, false,
2249 						true, 0x10);
2250 				else
2251 					halbtc8723b1ant_limited_rx(
2252 						btcoexist, NORMAL_EXEC, false,
2253 						true, 0x8);
2254 			}
2255 		}
2256 
2257 		halbtc8723b1ant_sw_mechanism(btcoexist, true);
2258 	} else {
2259 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2260 
2261 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false,
2262 					   0x5);
2263 
2264 		halbtc8723b1ant_sw_mechanism(btcoexist, false);
2265 	}
2266 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2267 
2268 	if (coex_sta->c2h_bt_inquiry_page) {
2269 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2270 		return;
2271 	} else if (bt_hs_on) {
2272 		halbtc8723b1ant_action_hs(btcoexist);
2273 		return;
2274 	}
2275 
2276 	if (!wifi_connected) {
2277 		bool scan = false, link = false, roam = false;
2278 
2279 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2280 			"[BTCoex], wifi is non connected-idle !!!\n");
2281 
2282 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2283 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2284 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2285 
2286 		if (scan || link || roam) {
2287 			if (scan)
2288 				btc8723b1ant_action_wifi_not_conn_scan(
2289 								     btcoexist);
2290 			else
2291 				btc8723b1ant_act_wifi_not_conn_asso_auth(
2292 								     btcoexist);
2293 		} else {
2294 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2295 		}
2296 	} else { /* wifi LPS/Busy */
2297 		halbtc8723b1ant_action_wifi_connected(btcoexist);
2298 	}
2299 }
2300 
2301 /* force coex mechanism to reset */
halbtc8723b1ant_init_coex_dm(struct btc_coexist * btcoexist)2302 static void halbtc8723b1ant_init_coex_dm(struct btc_coexist *btcoexist)
2303 {
2304 	/* sw all off */
2305 	halbtc8723b1ant_sw_mechanism(btcoexist, false);
2306 
2307 	coex_sta->pop_event_cnt = 0;
2308 }
2309 
halbtc8723b1ant_init_hw_config(struct btc_coexist * btcoexist,bool backup,bool wifi_only)2310 static void halbtc8723b1ant_init_hw_config(struct btc_coexist *btcoexist,
2311 					   bool backup, bool wifi_only)
2312 {
2313 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2314 	u32 u32tmp = 0;
2315 	u8 u8tmpa = 0, u8tmpb = 0;
2316 
2317 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2318 		"[BTCoex], 1Ant Init HW Config!!\n");
2319 
2320 	/* 0xf0[15:12] --> Chip Cut information */
2321 	coex_sta->cut_version =
2322 		(btcoexist->btc_read_1byte(btcoexist, 0xf1) & 0xf0) >> 4;
2323 	/* enable TBTT interrupt */
2324 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8, 0x1);
2325 
2326 	/* 0x790[5:0] = 0x5 */
2327 	btcoexist->btc_write_1byte(btcoexist, 0x790, 0x5);
2328 
2329 	/* Enable counter statistics */
2330 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x1);
2331 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2332 
2333 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2334 
2335 	/* Antenna config */
2336 	if (wifi_only)
2337 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
2338 					     FORCE_EXEC, true, false);
2339 	else
2340 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2341 					     FORCE_EXEC, true, false);
2342 
2343 	/* PTA parameter */
2344 	halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2345 
2346 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
2347 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
2348 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
2349 
2350 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2351 		"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
2352 		u32tmp, u8tmpa, u8tmpb);
2353 }
2354 
2355 /**************************************************************
2356  * extern function start with ex_btc8723b1ant_
2357  **************************************************************/
ex_btc8723b1ant_power_on_setting(struct btc_coexist * btcoexist)2358 void ex_btc8723b1ant_power_on_setting(struct btc_coexist *btcoexist)
2359 {
2360 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2361 	struct btc_board_info *board_info = &btcoexist->board_info;
2362 	u8 u8tmp = 0x0;
2363 	u16 u16tmp = 0x0;
2364 	u32 value;
2365 
2366 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2367 		"xxxxxxxxxxxxxxxx Execute 8723b 1-Ant PowerOn Setting xxxxxxxxxxxxxxxx!!\n");
2368 
2369 	btcoexist->stop_coex_dm = true;
2370 
2371 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
2372 
2373 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly. */
2374 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
2375 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT0 | BIT1);
2376 
2377 	/* set GRAN_BT = 1 */
2378 	btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
2379 	/* set WLAN_ACT = 0 */
2380 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
2381 
2382 	/* S0 or S1 setting and Local register setting(By the setting fw can get
2383 	 * ant number, S0/S1, ... info)
2384 	 *
2385 	 * Local setting bit define
2386 	 *	BIT0: "0" for no antenna inverse; "1" for antenna inverse
2387 	 *	BIT1: "0" for internal switch; "1" for external switch
2388 	 *	BIT2: "0" for one antenna; "1" for two antenna
2389 	 * NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and
2390 	 * BIT2 = 0
2391 	 */
2392 	if (btcoexist->chip_interface == BTC_INTF_USB) {
2393 		/* fixed at S0 for USB interface */
2394 		btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2395 
2396 		u8tmp |= 0x1; /* antenna inverse */
2397 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
2398 
2399 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2400 	} else {
2401 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
2402 		if (board_info->single_ant_path == 0) {
2403 			/* set to S1 */
2404 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x280);
2405 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
2406 			value = 1;
2407 		} else if (board_info->single_ant_path == 1) {
2408 			/* set to S0 */
2409 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2410 			u8tmp |= 0x1; /* antenna inverse */
2411 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2412 			value = 0;
2413 		}
2414 
2415 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
2416 				   &value);
2417 
2418 		if (btcoexist->chip_interface == BTC_INTF_PCI)
2419 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
2420 							     u8tmp);
2421 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
2422 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
2423 							     u8tmp);
2424 	}
2425 }
2426 
2427 
ex_btc8723b1ant_init_hwconfig(struct btc_coexist * btcoexist,bool wifi_only)2428 void ex_btc8723b1ant_init_hwconfig(struct btc_coexist *btcoexist,
2429 				   bool wifi_only)
2430 {
2431 	halbtc8723b1ant_init_hw_config(btcoexist, true, wifi_only);
2432 	btcoexist->stop_coex_dm = false;
2433 }
2434 
ex_btc8723b1ant_init_coex_dm(struct btc_coexist * btcoexist)2435 void ex_btc8723b1ant_init_coex_dm(struct btc_coexist *btcoexist)
2436 {
2437 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2438 
2439 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2440 		"[BTCoex], Coex Mechanism Init!!\n");
2441 
2442 	btcoexist->stop_coex_dm = false;
2443 
2444 	halbtc8723b1ant_init_coex_dm(btcoexist);
2445 
2446 	halbtc8723b1ant_query_bt_info(btcoexist);
2447 }
2448 
ex_btc8723b1ant_display_coex_info(struct btc_coexist * btcoexist,struct seq_file * m)2449 void ex_btc8723b1ant_display_coex_info(struct btc_coexist *btcoexist,
2450 				       struct seq_file *m)
2451 {
2452 	struct btc_board_info *board_info = &btcoexist->board_info;
2453 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2454 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2455 	u8 u8tmp[4], i, bt_info_ext, pstdmacase = 0;
2456 	u16 u16tmp[4];
2457 	u32 u32tmp[4];
2458 	bool roam = false, scan = false;
2459 	bool link = false, wifi_under_5g = false;
2460 	bool bt_hs_on = false, wifi_busy = false;
2461 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
2462 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck, wifi_link_status;
2463 	u8 wifi_dot11_chnl, wifi_hs_chnl;
2464 	u32 fw_ver = 0, bt_patch_ver = 0;
2465 
2466 	seq_puts(m, "\n ============[BT Coexist info]============");
2467 
2468 	if (btcoexist->manual_control) {
2469 		seq_puts(m, "\n ============[Under Manual Control]==========");
2470 		seq_puts(m, "\n ==========================================");
2471 	}
2472 	if (btcoexist->stop_coex_dm) {
2473 		seq_puts(m, "\n ============[Coex is STOPPED]============");
2474 		seq_puts(m, "\n ==========================================");
2475 	}
2476 
2477 	seq_printf(m, "\n %-35s = %d/ %d/ %d",
2478 		   "Ant PG Num/ Ant Mech/ Ant Pos:",
2479 		   board_info->pg_ant_num, board_info->btdm_ant_num,
2480 		   board_info->btdm_ant_pos);
2481 
2482 	seq_printf(m, "\n %-35s = %s / %d",
2483 		   "BT stack/ hci ext ver",
2484 		   ((stack_info->profile_notified) ? "Yes" : "No"),
2485 		   stack_info->hci_version);
2486 
2487 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
2488 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2489 	seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2490 		   "CoexVer/ FwVer/ PatchVer",
2491 		   glcoex_ver_date_8723b_1ant, glcoex_ver_8723b_1ant,
2492 		   fw_ver, bt_patch_ver, bt_patch_ver);
2493 
2494 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2495 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2496 			   &wifi_dot11_chnl);
2497 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
2498 	seq_printf(m, "\n %-35s = %d / %d(%d)",
2499 		   "Dot11 channel / HsChnl(HsMode)",
2500 		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2501 
2502 	seq_printf(m, "\n %-35s = %3ph ",
2503 		   "H2C Wifi inform bt chnl Info",
2504 		   coex_dm->wifi_chnl_info);
2505 
2506 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2507 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2508 	seq_printf(m, "\n %-35s = %d/ %d",
2509 		   "Wifi rssi/ HS rssi", wifi_rssi, bt_hs_rssi);
2510 
2511 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2512 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2513 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2514 	seq_printf(m, "\n %-35s = %d/ %d/ %d ",
2515 		   "Wifi link/ roam/ scan", link, roam, scan);
2516 
2517 	btcoexist->btc_get(btcoexist , BTC_GET_BL_WIFI_UNDER_5G,
2518 			   &wifi_under_5g);
2519 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2520 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2521 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2522 			   &wifi_traffic_dir);
2523 
2524 	seq_printf(m, "\n %-35s = %s / %s/ %s ",
2525 		   "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
2526 		   ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2527 		    ((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20")),
2528 		    ((!wifi_busy) ? "idle" :
2529 		     ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2530 		     "uplink" : "downlink")));
2531 
2532 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2533 			   &wifi_link_status);
2534 	seq_printf(m, "\n %-35s = %d/ %d/ %d/ %d/ %d",
2535 		   "sta/vwifi/hs/p2pGo/p2pGc",
2536 		   ((wifi_link_status & WIFI_STA_CONNECTED) ? 1 : 0),
2537 		   ((wifi_link_status & WIFI_AP_CONNECTED) ? 1 : 0),
2538 		   ((wifi_link_status & WIFI_HS_CONNECTED) ? 1 : 0),
2539 		   ((wifi_link_status & WIFI_P2P_GO_CONNECTED) ? 1 : 0),
2540 		   ((wifi_link_status & WIFI_P2P_GC_CONNECTED) ? 1 : 0));
2541 
2542 	seq_printf(m, "\n %-35s = [%s/ %d/ %d] ",
2543 		   "BT [status/ rssi/ retryCnt]",
2544 		   ((coex_sta->bt_disabled) ? ("disabled") :
2545 		    ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2546 		     ((BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2547 		       coex_dm->bt_status) ?
2548 		      "non-connected idle" :
2549 		      ((BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2550 			coex_dm->bt_status) ?
2551 		       "connected-idle" : "busy")))),
2552 		       coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2553 
2554 	seq_printf(m, "\n %-35s = %d / %d / %d / %d",
2555 		   "SCO/HID/PAN/A2DP", bt_link_info->sco_exist,
2556 		   bt_link_info->hid_exist, bt_link_info->pan_exist,
2557 		   bt_link_info->a2dp_exist);
2558 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
2559 
2560 	bt_info_ext = coex_sta->bt_info_ext;
2561 	seq_printf(m, "\n %-35s = %s",
2562 		   "BT Info A2DP rate",
2563 		   (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
2564 
2565 	for (i = 0; i < BT_INFO_SRC_8723B_1ANT_MAX; i++) {
2566 		if (coex_sta->bt_info_c2h_cnt[i]) {
2567 			seq_printf(m, "\n %-35s = %7ph(%d)",
2568 				   glbt_info_src_8723b_1ant[i],
2569 				   coex_sta->bt_info_c2h[i],
2570 				   coex_sta->bt_info_c2h_cnt[i]);
2571 		}
2572 	}
2573 	seq_printf(m, "\n %-35s = %s/%s, (0x%x/0x%x)",
2574 		   "PS state, IPS/LPS, (lps/rpwm)",
2575 		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2576 		   ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")),
2577 		   btcoexist->bt_info.lps_val,
2578 		   btcoexist->bt_info.rpwm_val);
2579 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
2580 
2581 	if (!btcoexist->manual_control) {
2582 		/* Sw mechanism	*/
2583 		seq_printf(m, "\n %-35s",
2584 			   "============[Sw mechanism]============");
2585 
2586 		seq_printf(m, "\n %-35s = %d/",
2587 			   "SM[LowPenaltyRA]", coex_dm->cur_low_penalty_ra);
2588 
2589 		seq_printf(m, "\n %-35s = %s/ %s/ %d ",
2590 			   "DelBA/ BtCtrlAgg/ AggSize",
2591 			   (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2592 			   (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2593 			   btcoexist->bt_info.agg_buf_size);
2594 
2595 		seq_printf(m, "\n %-35s = 0x%x ",
2596 			   "Rate Mask", btcoexist->bt_info.ra_mask);
2597 
2598 		/* Fw mechanism	*/
2599 		seq_printf(m, "\n %-35s",
2600 			   "============[Fw mechanism]============");
2601 
2602 		pstdmacase = coex_dm->cur_ps_tdma;
2603 		seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
2604 			   "PS TDMA", coex_dm->ps_tdma_para,
2605 			   pstdmacase, coex_dm->auto_tdma_adjust);
2606 
2607 		seq_printf(m, "\n %-35s = %d ",
2608 			   "IgnWlanAct", coex_dm->cur_ignore_wlan_act);
2609 
2610 		seq_printf(m, "\n %-35s = 0x%x ",
2611 			   "Latest error condition(should be 0)",
2612 			   coex_dm->error_condition);
2613 	}
2614 
2615 	seq_printf(m, "\n %-35s = %d",
2616 		   "Coex Table Type", coex_sta->coex_table_type);
2617 
2618 	/* Hw setting */
2619 	seq_printf(m, "\n %-35s",
2620 		   "============[Hw setting]============");
2621 
2622 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2623 		   "backup ARFR1/ARFR2/RL/AMaxTime", coex_dm->backup_arfr_cnt1,
2624 		   coex_dm->backup_arfr_cnt2, coex_dm->backup_retry_limit,
2625 		   coex_dm->backup_ampdu_max_time);
2626 
2627 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2628 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2629 	u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2630 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2631 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2632 		   "0x430/0x434/0x42a/0x456",
2633 		   u32tmp[0], u32tmp[1], u16tmp[0], u8tmp[0]);
2634 
2635 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2636 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6cc);
2637 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x880);
2638 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2639 		   "0x778/0x6cc/0x880[29:25]", u8tmp[0], u32tmp[0],
2640 		   (u32tmp[1] & 0x3e000000) >> 25);
2641 
2642 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
2643 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
2644 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
2645 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2646 		   "0x948/ 0x67[5] / 0x765",
2647 		   u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
2648 
2649 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
2650 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
2651 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
2652 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2653 		   "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
2654 		   u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
2655 
2656 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
2657 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2658 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2659 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2660 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2661 		   "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
2662 		   ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
2663 		    ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
2664 
2665 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2666 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2667 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
2668 		   "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
2669 
2670 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2671 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
2672 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
2673 		   "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
2674 
2675 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
2676 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
2677 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
2678 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
2679 
2680 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
2681 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2682 
2683 	fa_ofdm = ((u32tmp[0] & 0xffff0000) >> 16) +
2684 		  ((u32tmp[1] & 0xffff0000) >> 16) +
2685 		   (u32tmp[1] & 0xffff) +
2686 		   (u32tmp[2] & 0xffff) +
2687 		  ((u32tmp[3] & 0xffff0000) >> 16) +
2688 		   (u32tmp[3] & 0xffff);
2689 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
2690 
2691 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2692 		   "OFDM-CCA/OFDM-FA/CCK-FA",
2693 		 u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
2694 
2695 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2696 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2697 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2698 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2699 		   "0x6c0/0x6c4/0x6c8(coexTable)",
2700 		   u32tmp[0], u32tmp[1], u32tmp[2]);
2701 
2702 	seq_printf(m, "\n %-35s = %d/ %d",
2703 		   "0x770(high-pri rx/tx)", coex_sta->high_priority_rx,
2704 		   coex_sta->high_priority_tx);
2705 	seq_printf(m, "\n %-35s = %d/ %d",
2706 		   "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
2707 		   coex_sta->low_priority_tx);
2708 	if (btcoexist->auto_report_1ant)
2709 		halbtc8723b1ant_monitor_bt_ctr(btcoexist);
2710 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
2711 }
2712 
ex_btc8723b1ant_ips_notify(struct btc_coexist * btcoexist,u8 type)2713 void ex_btc8723b1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2714 {
2715 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2716 
2717 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2718 		return;
2719 
2720 	if (BTC_IPS_ENTER == type) {
2721 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2722 			"[BTCoex], IPS ENTER notify\n");
2723 		coex_sta->under_ips = true;
2724 
2725 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2726 					     FORCE_EXEC, false, true);
2727 		/* set PTA control */
2728 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2729 		halbtc8723b1ant_coex_table_with_type(btcoexist,
2730 						     NORMAL_EXEC, 0);
2731 	} else if (BTC_IPS_LEAVE == type) {
2732 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2733 			"[BTCoex], IPS LEAVE notify\n");
2734 		coex_sta->under_ips = false;
2735 
2736 		halbtc8723b1ant_init_hw_config(btcoexist, false, false);
2737 		halbtc8723b1ant_init_coex_dm(btcoexist);
2738 		halbtc8723b1ant_query_bt_info(btcoexist);
2739 	}
2740 }
2741 
ex_btc8723b1ant_lps_notify(struct btc_coexist * btcoexist,u8 type)2742 void ex_btc8723b1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2743 {
2744 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2745 
2746 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2747 		return;
2748 
2749 	if (BTC_LPS_ENABLE == type) {
2750 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2751 			"[BTCoex], LPS ENABLE notify\n");
2752 		coex_sta->under_lps = true;
2753 	} else if (BTC_LPS_DISABLE == type) {
2754 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2755 			"[BTCoex], LPS DISABLE notify\n");
2756 		coex_sta->under_lps = false;
2757 	}
2758 }
2759 
ex_btc8723b1ant_scan_notify(struct btc_coexist * btcoexist,u8 type)2760 void ex_btc8723b1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2761 {
2762 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2763 	bool wifi_connected = false, bt_hs_on = false;
2764 	u8 u8tmpa, u8tmpb;
2765 	u32 u32tmp;
2766 	u32 wifi_link_status = 0;
2767 	u32 num_of_wifi_link = 0;
2768 	bool bt_ctrl_agg_buf_size = false;
2769 	u8 agg_buf_size = 5;
2770 
2771 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2772 		return;
2773 
2774 	if (type == BTC_SCAN_START) {
2775 		coex_sta->wifi_is_high_pri_task = true;
2776 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2777 			"[BTCoex], SCAN START notify\n");
2778 		/* Force antenna setup for no scan result issue */
2779 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2780 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2781 					     FORCE_EXEC, false, false);
2782 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
2783 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
2784 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
2785 
2786 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2787 			"[BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
2788 			u32tmp, u8tmpa, u8tmpb);
2789 	} else {
2790 		coex_sta->wifi_is_high_pri_task = false;
2791 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2792 			"[BTCoex], SCAN FINISH notify\n");
2793 
2794 		btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
2795 				   &coex_sta->scan_ap_num);
2796 	}
2797 
2798 	if (coex_sta->bt_disabled)
2799 		return;
2800 
2801 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2802 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2803 			   &wifi_connected);
2804 
2805 	halbtc8723b1ant_query_bt_info(btcoexist);
2806 
2807 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2808 			   &wifi_link_status);
2809 	num_of_wifi_link = wifi_link_status >> 16;
2810 	if (num_of_wifi_link >= 2) {
2811 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2812 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2813 					   bt_ctrl_agg_buf_size, agg_buf_size);
2814 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
2815 		return;
2816 	}
2817 
2818 	if (coex_sta->c2h_bt_inquiry_page) {
2819 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2820 		return;
2821 	} else if (bt_hs_on) {
2822 		halbtc8723b1ant_action_hs(btcoexist);
2823 		return;
2824 	}
2825 
2826 	if (BTC_SCAN_START == type) {
2827 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2828 			"[BTCoex], SCAN START notify\n");
2829 		if (!wifi_connected)
2830 			/* non-connected scan */
2831 			btc8723b1ant_action_wifi_not_conn_scan(btcoexist);
2832 		else
2833 			/* wifi is connected */
2834 			btc8723b1ant_action_wifi_conn_scan(btcoexist);
2835 	} else if (BTC_SCAN_FINISH == type) {
2836 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2837 			"[BTCoex], SCAN FINISH notify\n");
2838 		if (!wifi_connected)
2839 			/* non-connected scan */
2840 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2841 		else
2842 			halbtc8723b1ant_action_wifi_connected(btcoexist);
2843 	}
2844 }
2845 
ex_btc8723b1ant_connect_notify(struct btc_coexist * btcoexist,u8 type)2846 void ex_btc8723b1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2847 {
2848 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2849 	bool wifi_connected = false, bt_hs_on = false;
2850 	u32 wifi_link_status = 0;
2851 	u32 num_of_wifi_link = 0;
2852 	bool bt_ctrl_agg_buf_size = false, under_4way = false;
2853 	u8 agg_buf_size = 5;
2854 
2855 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2856 			   &under_4way);
2857 
2858 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2859 	    coex_sta->bt_disabled)
2860 		return;
2861 
2862 	if (type == BTC_ASSOCIATE_START) {
2863 		coex_sta->wifi_is_high_pri_task = true;
2864 
2865 		/* Force antenna setup for no scan result issue */
2866 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2867 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2868 					     FORCE_EXEC, false, false);
2869 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2870 			"[BTCoex], CONNECT START notify\n");
2871 		coex_dm->arp_cnt = 0;
2872 	} else {
2873 		coex_sta->wifi_is_high_pri_task = false;
2874 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2875 			"[BTCoex], CONNECT FINISH notify\n");
2876 	}
2877 
2878 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2879 			   &wifi_link_status);
2880 	num_of_wifi_link = wifi_link_status>>16;
2881 	if (num_of_wifi_link >= 2) {
2882 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2883 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2884 					   bt_ctrl_agg_buf_size, agg_buf_size);
2885 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
2886 		return;
2887 	}
2888 
2889 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2890 	if (coex_sta->c2h_bt_inquiry_page) {
2891 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2892 		return;
2893 	} else if (bt_hs_on) {
2894 		halbtc8723b1ant_action_hs(btcoexist);
2895 		return;
2896 	}
2897 
2898 	if (BTC_ASSOCIATE_START == type) {
2899 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2900 			"[BTCoex], CONNECT START notify\n");
2901 		btc8723b1ant_act_wifi_not_conn_asso_auth(btcoexist);
2902 	} else if (BTC_ASSOCIATE_FINISH == type) {
2903 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2904 			"[BTCoex], CONNECT FINISH notify\n");
2905 
2906 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2907 				   &wifi_connected);
2908 		if (!wifi_connected)
2909 			/* non-connected scan */
2910 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2911 		else
2912 			halbtc8723b1ant_action_wifi_connected(btcoexist);
2913 	}
2914 }
2915 
ex_btc8723b1ant_media_status_notify(struct btc_coexist * btcoexist,u8 type)2916 void ex_btc8723b1ant_media_status_notify(struct btc_coexist *btcoexist,
2917 					 u8 type)
2918 {
2919 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2920 	u8 h2c_parameter[3] = {0};
2921 	u32 wifi_bw;
2922 	u8 wifi_central_chnl;
2923 	bool wifi_under_b_mode = false;
2924 
2925 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2926 	    coex_sta->bt_disabled)
2927 		return;
2928 
2929 	if (type == BTC_MEDIA_CONNECT) {
2930 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2931 			"[BTCoex], MEDIA connect notify\n");
2932 		/* Force antenna setup for no scan result issue */
2933 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2934 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2935 					     FORCE_EXEC, false, false);
2936 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
2937 				   &wifi_under_b_mode);
2938 
2939 		/* Set CCK Tx/Rx high Pri except 11b mode */
2940 		if (wifi_under_b_mode) {
2941 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2942 						   0x00); /* CCK Tx */
2943 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2944 						   0x00); /* CCK Rx */
2945 		} else {
2946 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2947 						   0x00); /* CCK Tx */
2948 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2949 						   0x10); /* CCK Rx */
2950 		}
2951 
2952 		coex_dm->backup_arfr_cnt1 =
2953 			btcoexist->btc_read_4byte(btcoexist, 0x430);
2954 		coex_dm->backup_arfr_cnt2 =
2955 			btcoexist->btc_read_4byte(btcoexist, 0x434);
2956 		coex_dm->backup_retry_limit =
2957 			btcoexist->btc_read_2byte(btcoexist, 0x42a);
2958 		coex_dm->backup_ampdu_max_time =
2959 			btcoexist->btc_read_1byte(btcoexist, 0x456);
2960 	} else {
2961 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2962 			"[BTCoex], MEDIA disconnect notify\n");
2963 		coex_dm->arp_cnt = 0;
2964 
2965 		btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x0); /* CCK Tx */
2966 		btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x0); /* CCK Rx */
2967 
2968 		coex_sta->cck_ever_lock = false;
2969 	}
2970 
2971 	/* only 2.4G we need to inform bt the chnl mask */
2972 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
2973 			   &wifi_central_chnl);
2974 
2975 	if (type == BTC_MEDIA_CONNECT && wifi_central_chnl <= 14) {
2976 		h2c_parameter[0] = 0x0;
2977 		h2c_parameter[1] = wifi_central_chnl;
2978 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2979 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2980 			h2c_parameter[2] = 0x30;
2981 		else
2982 			h2c_parameter[2] = 0x20;
2983 	}
2984 
2985 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2986 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2987 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2988 
2989 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2990 		"[BTCoex], FW write 0x66 = 0x%x\n",
2991 		h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
2992 		h2c_parameter[2]);
2993 
2994 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2995 }
2996 
ex_btc8723b1ant_special_packet_notify(struct btc_coexist * btcoexist,u8 type)2997 void ex_btc8723b1ant_special_packet_notify(struct btc_coexist *btcoexist,
2998 					   u8 type)
2999 {
3000 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3001 	bool bt_hs_on = false;
3002 	u32 wifi_link_status = 0;
3003 	u32 num_of_wifi_link = 0;
3004 	bool bt_ctrl_agg_buf_size = false, under_4way = false;
3005 	u8 agg_buf_size = 5;
3006 
3007 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
3008 			   &under_4way);
3009 
3010 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
3011 	    coex_sta->bt_disabled)
3012 		return;
3013 
3014 	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
3015 	    type == BTC_PACKET_ARP) {
3016 		if (type == BTC_PACKET_ARP) {
3017 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3018 				"[BTCoex], special Packet ARP notify\n");
3019 
3020 			coex_dm->arp_cnt++;
3021 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3022 				"[BTCoex], ARP Packet Count = %d\n",
3023 				 coex_dm->arp_cnt);
3024 
3025 			if ((coex_dm->arp_cnt >= 10) && (!under_4way))
3026 				/* if APR PKT > 10 after connect, do not go to
3027 				 * ActionWifiConnectedSpecificPacket(btcoexist)
3028 				 */
3029 				coex_sta->wifi_is_high_pri_task = false;
3030 			else
3031 				coex_sta->wifi_is_high_pri_task = true;
3032 		} else {
3033 			coex_sta->wifi_is_high_pri_task = true;
3034 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3035 				"[BTCoex], special Packet DHCP or EAPOL notify\n");
3036 		}
3037 	} else {
3038 		coex_sta->wifi_is_high_pri_task = false;
3039 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3040 			"[BTCoex], special Packet [Type = %d] notify\n",
3041 			 type);
3042 	}
3043 
3044 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3045 		&wifi_link_status);
3046 	num_of_wifi_link = wifi_link_status >> 16;
3047 	if (num_of_wifi_link >= 2) {
3048 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
3049 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
3050 					   bt_ctrl_agg_buf_size, agg_buf_size);
3051 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
3052 		return;
3053 	}
3054 
3055 	coex_sta->special_pkt_period_cnt = 0;
3056 
3057 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3058 	if (coex_sta->c2h_bt_inquiry_page) {
3059 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
3060 		return;
3061 	} else if (bt_hs_on) {
3062 		halbtc8723b1ant_action_hs(btcoexist);
3063 		return;
3064 	}
3065 
3066 	if (BTC_PACKET_DHCP == type ||
3067 	    BTC_PACKET_EAPOL == type) {
3068 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3069 			"[BTCoex], special Packet(%d) notify\n", type);
3070 		halbtc8723b1ant_action_wifi_connected_special_packet(btcoexist);
3071 	}
3072 }
3073 
ex_btc8723b1ant_bt_info_notify(struct btc_coexist * btcoexist,u8 * tmp_buf,u8 length)3074 void ex_btc8723b1ant_bt_info_notify(struct btc_coexist *btcoexist,
3075 				    u8 *tmp_buf, u8 length)
3076 {
3077 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3078 	u8 bt_info = 0;
3079 	u8 i, rsp_source = 0;
3080 	bool wifi_connected = false;
3081 	bool bt_busy = false;
3082 
3083 	coex_sta->c2h_bt_info_req_sent = false;
3084 
3085 	rsp_source = tmp_buf[0] & 0xf;
3086 	if (rsp_source >= BT_INFO_SRC_8723B_1ANT_MAX)
3087 		rsp_source = BT_INFO_SRC_8723B_1ANT_WIFI_FW;
3088 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
3089 
3090 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3091 		"[BTCoex], Bt info[%d], length=%d, hex data = [",
3092 		 rsp_source, length);
3093 	for (i = 0; i < length; i++) {
3094 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
3095 		if (i == 1)
3096 			bt_info = tmp_buf[i];
3097 		if (i == length - 1)
3098 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3099 				"0x%02x]\n", tmp_buf[i]);
3100 		else
3101 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3102 				"0x%02x, ", tmp_buf[i]);
3103 	}
3104 
3105 	/* if 0xff, it means BT is under WHCK test */
3106 	if (bt_info == 0xff)
3107 		coex_sta->bt_whck_test = true;
3108 	else
3109 		coex_sta->bt_whck_test = false;
3110 
3111 	if (rsp_source != BT_INFO_SRC_8723B_1ANT_WIFI_FW) {
3112 		coex_sta->bt_retry_cnt = /* [3:0] */
3113 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
3114 
3115 		if (coex_sta->bt_retry_cnt >= 1)
3116 			coex_sta->pop_event_cnt++;
3117 
3118 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
3119 			coex_sta->c2h_bt_remote_name_req = true;
3120 		else
3121 			coex_sta->c2h_bt_remote_name_req = false;
3122 
3123 		coex_sta->bt_rssi =
3124 			coex_sta->bt_info_c2h[rsp_source][3] * 2 - 90;
3125 
3126 		coex_sta->bt_info_ext =
3127 			coex_sta->bt_info_c2h[rsp_source][4];
3128 
3129 		if (coex_sta->bt_info_c2h[rsp_source][1] == 0x49) {
3130 			coex_sta->a2dp_bit_pool =
3131 				coex_sta->bt_info_c2h[rsp_source][6];
3132 		} else {
3133 			coex_sta->a2dp_bit_pool = 0;
3134 		}
3135 
3136 		coex_sta->bt_tx_rx_mask =
3137 			(coex_sta->bt_info_c2h[rsp_source][2] & 0x40);
3138 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
3139 				   &coex_sta->bt_tx_rx_mask);
3140 
3141 		if (!coex_sta->bt_tx_rx_mask) {
3142 			/* BT into is responded by BT FW and BT RF REG
3143 			 * 0x3C != 0x15 => Need to switch BT TRx Mask
3144 			 */
3145 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3146 				"[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
3147 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3148 						  0x3c, 0x15);
3149 
3150 			/* BT TRx Mask lock 0x2c[0], 0x30[0] = 0 */
3151 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3152 						  0x2c, 0x7c44);
3153 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3154 						  0x30, 0x7c44);
3155 		}
3156 
3157 		/* Here we need to resend some wifi info to BT
3158 		 * because bt is reset and loss of the info.
3159 		 */
3160 		if (coex_sta->bt_info_ext & BIT1) {
3161 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3162 				"[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
3163 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3164 					   &wifi_connected);
3165 			if (wifi_connected)
3166 				ex_btc8723b1ant_media_status_notify(btcoexist,
3167 						BTC_MEDIA_CONNECT);
3168 			else
3169 				ex_btc8723b1ant_media_status_notify(btcoexist,
3170 						BTC_MEDIA_DISCONNECT);
3171 		}
3172 
3173 		if (coex_sta->bt_info_ext & BIT3) {
3174 			if (!btcoexist->manual_control &&
3175 			    !btcoexist->stop_coex_dm) {
3176 				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3177 					"[BTCoex], BT ext info bit3 check, set BT NOT ignore Wlan active!!\n");
3178 				halbtc8723b1ant_ignore_wlan_act(btcoexist,
3179 								FORCE_EXEC,
3180 								false);
3181 			}
3182 		} else {
3183 			/* BT already NOT ignore Wlan active, do nothing here.*/
3184 		}
3185 		if (!btcoexist->auto_report_1ant) {
3186 			if (coex_sta->bt_info_ext & BIT4) {
3187 				/* BT auto report already enabled, do nothing */
3188 			} else {
3189 				halbtc8723b1ant_bt_auto_report(btcoexist,
3190 							       FORCE_EXEC,
3191 							       true);
3192 			}
3193 		}
3194 	}
3195 
3196 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
3197 	if (bt_info & BT_INFO_8723B_1ANT_B_INQ_PAGE)
3198 		coex_sta->c2h_bt_inquiry_page = true;
3199 	else
3200 		coex_sta->c2h_bt_inquiry_page = false;
3201 
3202 	coex_sta->num_of_profile = 0;
3203 
3204 	/* set link exist status */
3205 	if (!(bt_info & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3206 		coex_sta->bt_link_exist = false;
3207 		coex_sta->pan_exist = false;
3208 		coex_sta->a2dp_exist = false;
3209 		coex_sta->hid_exist = false;
3210 		coex_sta->sco_exist = false;
3211 
3212 		coex_sta->bt_hi_pri_link_exist = false;
3213 	} else {
3214 		/* connection exists */
3215 		coex_sta->bt_link_exist = true;
3216 		if (bt_info & BT_INFO_8723B_1ANT_B_FTP) {
3217 			coex_sta->pan_exist = true;
3218 			coex_sta->num_of_profile++;
3219 		} else {
3220 			coex_sta->pan_exist = false;
3221 		}
3222 		if (bt_info & BT_INFO_8723B_1ANT_B_A2DP) {
3223 			coex_sta->a2dp_exist = true;
3224 			coex_sta->num_of_profile++;
3225 		} else {
3226 			coex_sta->a2dp_exist = false;
3227 		}
3228 		if (bt_info & BT_INFO_8723B_1ANT_B_HID) {
3229 			coex_sta->hid_exist = true;
3230 			coex_sta->num_of_profile++;
3231 		} else {
3232 			coex_sta->hid_exist = false;
3233 		}
3234 		if (bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO) {
3235 			coex_sta->sco_exist = true;
3236 			coex_sta->num_of_profile++;
3237 		} else {
3238 			coex_sta->sco_exist = false;
3239 		}
3240 
3241 		if ((!coex_sta->hid_exist) &&
3242 		    (!coex_sta->c2h_bt_inquiry_page) &&
3243 		    (!coex_sta->sco_exist)) {
3244 			if (coex_sta->high_priority_tx +
3245 				    coex_sta->high_priority_rx >=
3246 			    160) {
3247 				coex_sta->hid_exist = true;
3248 				coex_sta->wrong_profile_notification++;
3249 				coex_sta->num_of_profile++;
3250 				bt_info = bt_info | 0x28;
3251 			}
3252 		}
3253 
3254 		/* Add Hi-Pri Tx/Rx counter to avoid false detection */
3255 		if (((coex_sta->hid_exist) || (coex_sta->sco_exist)) &&
3256 		    (coex_sta->high_priority_tx + coex_sta->high_priority_rx >=
3257 		     160) &&
3258 		    (!coex_sta->c2h_bt_inquiry_page))
3259 			coex_sta->bt_hi_pri_link_exist = true;
3260 
3261 		if ((bt_info & BT_INFO_8723B_1ANT_B_ACL_BUSY) &&
3262 		    (coex_sta->num_of_profile == 0)) {
3263 			if (coex_sta->low_priority_tx +
3264 				    coex_sta->low_priority_rx >=
3265 			    160) {
3266 				coex_sta->pan_exist = true;
3267 				coex_sta->num_of_profile++;
3268 				coex_sta->wrong_profile_notification++;
3269 				bt_info = bt_info | 0x88;
3270 			}
3271 		}
3272 	}
3273 
3274 	halbtc8723b1ant_update_bt_link_info(btcoexist);
3275 
3276 	/* mask profile bit for connect-ilde identification
3277 	 * ( for CSR case: A2DP idle --> 0x41)
3278 	 */
3279 	bt_info = bt_info & 0x1f;
3280 
3281 	if (!(bt_info & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3282 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3283 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3284 			"[BTCoex], BtInfoNotify(), BT Non-Connected idle!\n");
3285 	/* connection exists but no busy */
3286 	} else if (bt_info == BT_INFO_8723B_1ANT_B_CONNECTION) {
3287 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE;
3288 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3289 			"[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
3290 	} else if ((bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO) ||
3291 		(bt_info & BT_INFO_8723B_1ANT_B_SCO_BUSY)) {
3292 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_SCO_BUSY;
3293 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3294 			"[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
3295 	} else if (bt_info & BT_INFO_8723B_1ANT_B_ACL_BUSY) {
3296 		if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
3297 			coex_dm->auto_tdma_adjust = false;
3298 
3299 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_ACL_BUSY;
3300 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3301 			"[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
3302 	} else {
3303 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_MAX;
3304 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3305 			"[BTCoex], BtInfoNotify(), BT Non-Defined state!!\n");
3306 	}
3307 
3308 	if ((BT_8723B_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3309 	    (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3310 	    (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
3311 		bt_busy = true;
3312 	else
3313 		bt_busy = false;
3314 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
3315 
3316 	halbtc8723b1ant_run_coexist_mechanism(btcoexist);
3317 }
3318 
ex_btc8723b1ant_rf_status_notify(struct btc_coexist * btcoexist,u8 type)3319 void ex_btc8723b1ant_rf_status_notify(struct btc_coexist *btcoexist, u8 type)
3320 {
3321 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3322 	u32 u32tmp;
3323 	u8 u8tmpa, u8tmpb, u8tmpc;
3324 
3325 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3326 		"[BTCoex], RF Status notify\n");
3327 
3328 	if (type == BTC_RF_ON) {
3329 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3330 			"[BTCoex], RF is turned ON!!\n");
3331 		btcoexist->stop_coex_dm = false;
3332 	} else if (type == BTC_RF_OFF) {
3333 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3334 			"[BTCoex], RF is turned OFF!!\n");
3335 
3336 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3337 						 0x0, 0x0);
3338 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3339 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
3340 					     FORCE_EXEC, false, true);
3341 
3342 		halbtc8723b1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3343 		btcoexist->stop_coex_dm = true;
3344 
3345 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
3346 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
3347 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
3348 		u8tmpc = btcoexist->btc_read_1byte(btcoexist, 0x76e);
3349 
3350 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3351 			"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x, 0x76e=0x%x\n",
3352 			u32tmp, u8tmpa, u8tmpb, u8tmpc);
3353 	}
3354 }
3355 
ex_btc8723b1ant_halt_notify(struct btc_coexist * btcoexist)3356 void ex_btc8723b1ant_halt_notify(struct btc_coexist *btcoexist)
3357 {
3358 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3359 
3360 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
3361 
3362 	btcoexist->stop_coex_dm = true;
3363 
3364 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, FORCE_EXEC,
3365 				     false, true);
3366 
3367 	halbtc8723b1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3368 
3369 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3370 					 0x0, 0x0);
3371 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3372 
3373 	ex_btc8723b1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
3374 
3375 	btcoexist->stop_coex_dm = true;
3376 }
3377 
ex_btc8723b1ant_pnp_notify(struct btc_coexist * btcoexist,u8 pnp_state)3378 void ex_btc8723b1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
3379 {
3380 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3381 
3382 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Pnp notify\n");
3383 
3384 	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
3385 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3386 			"[BTCoex], Pnp notify to SLEEP\n");
3387 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
3388 					     FORCE_EXEC, false, true);
3389 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3390 						 0x0, 0x0);
3391 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
3392 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
3393 
3394 		/* Driver do not leave IPS/LPS when driver is going to sleep, so
3395 		 * BTCoexistence think wifi is still under IPS/LPS
3396 		 *
3397 		 * BT should clear UnderIPS/UnderLPS state to avoid mismatch
3398 		 * state after wakeup.
3399 		 */
3400 		coex_sta->under_ips = false;
3401 		coex_sta->under_lps = false;
3402 		btcoexist->stop_coex_dm = true;
3403 	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
3404 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3405 			"[BTCoex], Pnp notify to WAKE UP\n");
3406 		btcoexist->stop_coex_dm = false;
3407 		halbtc8723b1ant_init_hw_config(btcoexist, false, false);
3408 		halbtc8723b1ant_init_coex_dm(btcoexist);
3409 		halbtc8723b1ant_query_bt_info(btcoexist);
3410 	}
3411 }
3412 
ex_btc8723b1ant_coex_dm_reset(struct btc_coexist * btcoexist)3413 void ex_btc8723b1ant_coex_dm_reset(struct btc_coexist *btcoexist)
3414 {
3415 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3416 
3417 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3418 		"[BTCoex], *****************Coex DM Reset****************\n");
3419 
3420 	halbtc8723b1ant_init_hw_config(btcoexist, false, false);
3421 	halbtc8723b1ant_init_coex_dm(btcoexist);
3422 }
3423 
ex_btc8723b1ant_periodical(struct btc_coexist * btcoexist)3424 void ex_btc8723b1ant_periodical(struct btc_coexist *btcoexist)
3425 {
3426 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3427 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3428 
3429 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3430 		"[BTCoex], ==========================Periodical===========================\n");
3431 
3432 	if (!btcoexist->auto_report_1ant) {
3433 		halbtc8723b1ant_query_bt_info(btcoexist);
3434 		halbtc8723b1ant_monitor_bt_enable_disable(btcoexist);
3435 	} else {
3436 		halbtc8723b1ant_monitor_bt_ctr(btcoexist);
3437 		halbtc8723b1ant_monitor_wifi_ctr(btcoexist);
3438 
3439 		if ((coex_sta->high_priority_tx + coex_sta->high_priority_rx < 50) &&
3440 		    bt_link_info->hid_exist)
3441 			bt_link_info->hid_exist = false;
3442 
3443 		if (btc8723b1ant_is_wifi_status_changed(btcoexist) ||
3444 		    coex_dm->auto_tdma_adjust) {
3445 			halbtc8723b1ant_run_coexist_mechanism(btcoexist);
3446 		}
3447 		coex_sta->special_pkt_period_cnt++;
3448 	}
3449 }
3450