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->pan_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->pan_exist) {
1968 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1969 						true, 22);
1970 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1971 							     NORMAL_EXEC, 4);
1972 		} else {
1973 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1974 						true, 20);
1975 			halbtc8723b1ant_coex_table_with_type(btcoexist,
1976 							     NORMAL_EXEC, 4);
1977 		}
1978 	} else if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
1979 		   coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
1980 		btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
1981 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1982 	} else {
1983 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1984 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1985 					     NORMAL_EXEC, false, false);
1986 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1987 	}
1988 }
1989 
halbtc8723b1ant_action_wifi_connected_special_packet(struct btc_coexist * btcoexist)1990 static void halbtc8723b1ant_action_wifi_connected_special_packet(
1991 						struct btc_coexist *btcoexist)
1992 {
1993 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1994 	bool wifi_busy = false;
1995 
1996 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1997 
1998 	/* no special packet process for both WiFi and BT very busy */
1999 	if ((wifi_busy) &&
2000 	    ((bt_link_info->pan_exist) || (coex_sta->num_of_profile >= 2)))
2001 		return;
2002 
2003 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2004 					 0x0, 0x0);
2005 
2006 	/* tdma and coex table */
2007 	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist)) {
2008 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
2009 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
2010 	} else if (bt_link_info->a2dp_exist) {
2011 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
2012 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2013 	} else if (bt_link_info->pan_exist) {
2014 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
2015 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2016 	} else {
2017 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2018 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2019 					     NORMAL_EXEC, false, false);
2020 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2021 	}
2022 }
2023 
halbtc8723b1ant_action_wifi_connected(struct btc_coexist * btcoexist)2024 static void halbtc8723b1ant_action_wifi_connected(struct btc_coexist *btcoexist)
2025 {
2026 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2027 	bool wifi_busy = false;
2028 	bool scan = false, link = false, roam = false;
2029 	bool under_4way = false, ap_enable = false;
2030 
2031 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2032 		"[BTCoex], CoexForWifiConnect()===>\n");
2033 
2034 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2035 			   &under_4way);
2036 	if (under_4way) {
2037 		halbtc8723b1ant_action_wifi_connected_special_packet(btcoexist);
2038 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2039 			"[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
2040 		return;
2041 	}
2042 
2043 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2044 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2045 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2046 
2047 	if (scan || link || roam) {
2048 		if (scan)
2049 			btc8723b1ant_action_wifi_conn_scan(btcoexist);
2050 		else
2051 			halbtc8723b1ant_action_wifi_connected_special_packet(
2052 								     btcoexist);
2053 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2054 			"[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
2055 		return;
2056 	}
2057 
2058 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
2059 			   &ap_enable);
2060 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2061 	/* power save state */
2062 	if (!ap_enable &&
2063 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY &&
2064 	    !btcoexist->bt_link_info.hid_only) {
2065 		if (btcoexist->bt_link_info.a2dp_only) {
2066 			if (!wifi_busy) {
2067 				halbtc8723b1ant_power_save_state(btcoexist,
2068 							 BTC_PS_WIFI_NATIVE,
2069 							 0x0, 0x0);
2070 			} else { /* busy */
2071 				if (coex_sta->scan_ap_num >=
2072 				    BT_8723B_1ANT_WIFI_NOISY_THRESH)
2073 					/* no force LPS, no PS-TDMA,
2074 					 * use pure TDMA
2075 					 */
2076 					halbtc8723b1ant_power_save_state(
2077 						btcoexist, BTC_PS_WIFI_NATIVE,
2078 						0x0, 0x0);
2079 				else
2080 					halbtc8723b1ant_power_save_state(
2081 						btcoexist, BTC_PS_LPS_ON, 0x50,
2082 						0x4);
2083 			}
2084 		} else if ((!coex_sta->pan_exist) && (!coex_sta->a2dp_exist) &&
2085 			   (!coex_sta->hid_exist))
2086 			halbtc8723b1ant_power_save_state(
2087 				btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2088 		else
2089 			halbtc8723b1ant_power_save_state(btcoexist,
2090 							 BTC_PS_LPS_ON,
2091 							 0x50, 0x4);
2092 	} else {
2093 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2094 						 0x0, 0x0);
2095 	}
2096 	/* tdma and coex table */
2097 	if (!wifi_busy) {
2098 		if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
2099 			halbtc8723b1ant_action_wifi_connected_bt_acl_busy(
2100 				btcoexist,
2101 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2102 		} else if (coex_dm->bt_status ==
2103 				BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2104 			   coex_dm->bt_status ==
2105 				BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
2106 			btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
2107 				     BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2108 		} else {
2109 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2110 						false, 8);
2111 			halbtc8723b1ant_set_ant_path(btcoexist,
2112 						     BTC_ANT_PATH_PTA,
2113 						     NORMAL_EXEC, false, false);
2114 			halbtc8723b1ant_coex_table_with_type(btcoexist,
2115 							     NORMAL_EXEC, 2);
2116 		}
2117 	} else {
2118 		if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) {
2119 			halbtc8723b1ant_action_wifi_connected_bt_acl_busy(
2120 				btcoexist,
2121 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2122 		} else if (coex_dm->bt_status ==
2123 				BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2124 			   coex_dm->bt_status ==
2125 				BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) {
2126 			btc8723b1ant_act_bt_sco_hid_only_busy(btcoexist,
2127 				    BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2128 		} else {
2129 			halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2130 						true, 32);
2131 			halbtc8723b1ant_set_ant_path(btcoexist,
2132 						     BTC_ANT_PATH_PTA,
2133 						     NORMAL_EXEC, false, false);
2134 			halbtc8723b1ant_coex_table_with_type(btcoexist,
2135 							     NORMAL_EXEC, 4);
2136 		}
2137 	}
2138 }
2139 
halbtc8723b1ant_run_coexist_mechanism(struct btc_coexist * btcoexist)2140 static void halbtc8723b1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
2141 {
2142 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2143 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2144 	bool wifi_connected = false, bt_hs_on = false, wifi_busy = false;
2145 	bool increase_scan_dev_num = false;
2146 	bool bt_ctrl_agg_buf_size = false;
2147 	bool miracast_plus_bt = false;
2148 	u8 agg_buf_size = 5;
2149 	u8 iot_peer = BTC_IOT_PEER_UNKNOWN;
2150 	u32 wifi_link_status = 0;
2151 	u32 num_of_wifi_link = 0;
2152 	u32 wifi_bw;
2153 
2154 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2155 		"[BTCoex], RunCoexistMechanism()===>\n");
2156 
2157 	if (btcoexist->manual_control) {
2158 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2159 			"[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2160 		return;
2161 	}
2162 
2163 	if (btcoexist->stop_coex_dm) {
2164 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2165 			"[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2166 		return;
2167 	}
2168 
2169 	if (coex_sta->under_ips) {
2170 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2171 			"[BTCoex], wifi is under IPS !!!\n");
2172 		return;
2173 	}
2174 
2175 	if (coex_sta->bt_whck_test) {
2176 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2177 			"[BTCoex], wifi is under IPS !!!\n");
2178 		halbtc8723b1ant_action_bt_whck_test(btcoexist);
2179 		return;
2180 	}
2181 
2182 	if (coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_BUSY ||
2183 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_SCO_BUSY ||
2184 	    coex_dm->bt_status == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY)
2185 		increase_scan_dev_num = true;
2186 
2187 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2188 			   &increase_scan_dev_num);
2189 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2190 			   &wifi_connected);
2191 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2192 
2193 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2194 			   &wifi_link_status);
2195 	num_of_wifi_link = wifi_link_status >> 16;
2196 
2197 	if (num_of_wifi_link >= 2 ||
2198 	    wifi_link_status & WIFI_P2P_GO_CONNECTED) {
2199 		if (bt_link_info->bt_link_exist) {
2200 			halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1,
2201 						   0, 1);
2202 			miracast_plus_bt = true;
2203 		} else {
2204 			halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0,
2205 						   0, 0);
2206 			miracast_plus_bt = false;
2207 		}
2208 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2209 				   &miracast_plus_bt);
2210 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2211 					   bt_ctrl_agg_buf_size, agg_buf_size);
2212 
2213 		if ((bt_link_info->a2dp_exist || wifi_busy) &&
2214 		    (coex_sta->c2h_bt_inquiry_page))
2215 			halbtc8723b1ant_action_bt_inquiry(btcoexist);
2216 		else
2217 			halbtc8723b1ant_action_wifi_multiport(btcoexist);
2218 
2219 		return;
2220 	}
2221 
2222 	miracast_plus_bt = false;
2223 	btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2224 			   &miracast_plus_bt);
2225 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2226 
2227 	if (bt_link_info->bt_link_exist && wifi_connected) {
2228 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1, 0, 1);
2229 
2230 		btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
2231 
2232 		if (iot_peer != BTC_IOT_PEER_CISCO &&
2233 		    iot_peer != BTC_IOT_PEER_BROADCOM) {
2234 			bool sco_exist = bt_link_info->sco_exist;
2235 
2236 			halbtc8723b1ant_limited_rx(btcoexist,
2237 						   NORMAL_EXEC, sco_exist,
2238 						   false, 0x5);
2239 		} else {
2240 			if (bt_link_info->sco_exist) {
2241 				halbtc8723b1ant_limited_rx(btcoexist,
2242 							   NORMAL_EXEC, true,
2243 							   false, 0x5);
2244 			} else {
2245 				if (wifi_bw == BTC_WIFI_BW_HT40)
2246 					halbtc8723b1ant_limited_rx(
2247 						btcoexist, NORMAL_EXEC, false,
2248 						true, 0x10);
2249 				else
2250 					halbtc8723b1ant_limited_rx(
2251 						btcoexist, NORMAL_EXEC, false,
2252 						true, 0x8);
2253 			}
2254 		}
2255 
2256 		halbtc8723b1ant_sw_mechanism(btcoexist, true);
2257 	} else {
2258 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2259 
2260 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false,
2261 					   0x5);
2262 
2263 		halbtc8723b1ant_sw_mechanism(btcoexist, false);
2264 	}
2265 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2266 
2267 	if (coex_sta->c2h_bt_inquiry_page) {
2268 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2269 		return;
2270 	} else if (bt_hs_on) {
2271 		halbtc8723b1ant_action_hs(btcoexist);
2272 		return;
2273 	}
2274 
2275 	if (!wifi_connected) {
2276 		bool scan = false, link = false, roam = false;
2277 
2278 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2279 			"[BTCoex], wifi is non connected-idle !!!\n");
2280 
2281 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2282 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2283 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2284 
2285 		if (scan || link || roam) {
2286 			if (scan)
2287 				btc8723b1ant_action_wifi_not_conn_scan(
2288 								     btcoexist);
2289 			else
2290 				btc8723b1ant_act_wifi_not_conn_asso_auth(
2291 								     btcoexist);
2292 		} else {
2293 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2294 		}
2295 	} else { /* wifi LPS/Busy */
2296 		halbtc8723b1ant_action_wifi_connected(btcoexist);
2297 	}
2298 }
2299 
2300 /* force coex mechanism to reset */
halbtc8723b1ant_init_coex_dm(struct btc_coexist * btcoexist)2301 static void halbtc8723b1ant_init_coex_dm(struct btc_coexist *btcoexist)
2302 {
2303 	/* sw all off */
2304 	halbtc8723b1ant_sw_mechanism(btcoexist, false);
2305 
2306 	coex_sta->pop_event_cnt = 0;
2307 }
2308 
halbtc8723b1ant_init_hw_config(struct btc_coexist * btcoexist,bool backup,bool wifi_only)2309 static void halbtc8723b1ant_init_hw_config(struct btc_coexist *btcoexist,
2310 					   bool backup, bool wifi_only)
2311 {
2312 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2313 	u32 u32tmp = 0;
2314 	u8 u8tmpa = 0, u8tmpb = 0;
2315 
2316 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2317 		"[BTCoex], 1Ant Init HW Config!!\n");
2318 
2319 	/* 0xf0[15:12] --> Chip Cut information */
2320 	coex_sta->cut_version =
2321 		(btcoexist->btc_read_1byte(btcoexist, 0xf1) & 0xf0) >> 4;
2322 	/* enable TBTT interrupt */
2323 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8, 0x1);
2324 
2325 	/* 0x790[5:0] = 0x5 */
2326 	btcoexist->btc_write_1byte(btcoexist, 0x790, 0x5);
2327 
2328 	/* Enable counter statistics */
2329 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x1);
2330 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2331 
2332 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2333 
2334 	/* Antenna config */
2335 	if (wifi_only)
2336 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
2337 					     FORCE_EXEC, true, false);
2338 	else
2339 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2340 					     FORCE_EXEC, true, false);
2341 
2342 	/* PTA parameter */
2343 	halbtc8723b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2344 
2345 	u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
2346 	u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
2347 	u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
2348 
2349 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2350 		"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
2351 		u32tmp, u8tmpa, u8tmpb);
2352 }
2353 
2354 /**************************************************************
2355  * extern function start with ex_btc8723b1ant_
2356  **************************************************************/
ex_btc8723b1ant_power_on_setting(struct btc_coexist * btcoexist)2357 void ex_btc8723b1ant_power_on_setting(struct btc_coexist *btcoexist)
2358 {
2359 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2360 	struct btc_board_info *board_info = &btcoexist->board_info;
2361 	u8 u8tmp = 0x0;
2362 	u16 u16tmp = 0x0;
2363 	u32 value;
2364 
2365 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2366 		"xxxxxxxxxxxxxxxx Execute 8723b 1-Ant PowerOn Setting xxxxxxxxxxxxxxxx!!\n");
2367 
2368 	btcoexist->stop_coex_dm = true;
2369 
2370 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
2371 
2372 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly. */
2373 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
2374 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT0 | BIT1);
2375 
2376 	/* set GRAN_BT = 1 */
2377 	btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
2378 	/* set WLAN_ACT = 0 */
2379 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
2380 
2381 	/* S0 or S1 setting and Local register setting(By the setting fw can get
2382 	 * ant number, S0/S1, ... info)
2383 	 *
2384 	 * Local setting bit define
2385 	 *	BIT0: "0" for no antenna inverse; "1" for antenna inverse
2386 	 *	BIT1: "0" for internal switch; "1" for external switch
2387 	 *	BIT2: "0" for one antenna; "1" for two antenna
2388 	 * NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and
2389 	 * BIT2 = 0
2390 	 */
2391 	if (btcoexist->chip_interface == BTC_INTF_USB) {
2392 		/* fixed at S0 for USB interface */
2393 		btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2394 
2395 		u8tmp |= 0x1; /* antenna inverse */
2396 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
2397 
2398 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2399 	} else {
2400 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
2401 		if (board_info->single_ant_path == 0) {
2402 			/* set to S1 */
2403 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x280);
2404 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
2405 			value = 1;
2406 		} else if (board_info->single_ant_path == 1) {
2407 			/* set to S0 */
2408 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2409 			u8tmp |= 0x1; /* antenna inverse */
2410 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2411 			value = 0;
2412 		}
2413 
2414 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
2415 				   &value);
2416 
2417 		if (btcoexist->chip_interface == BTC_INTF_PCI)
2418 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
2419 							     u8tmp);
2420 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
2421 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
2422 							     u8tmp);
2423 	}
2424 }
2425 
2426 
ex_btc8723b1ant_init_hwconfig(struct btc_coexist * btcoexist,bool wifi_only)2427 void ex_btc8723b1ant_init_hwconfig(struct btc_coexist *btcoexist,
2428 				   bool wifi_only)
2429 {
2430 	halbtc8723b1ant_init_hw_config(btcoexist, true, wifi_only);
2431 	btcoexist->stop_coex_dm = false;
2432 }
2433 
ex_btc8723b1ant_init_coex_dm(struct btc_coexist * btcoexist)2434 void ex_btc8723b1ant_init_coex_dm(struct btc_coexist *btcoexist)
2435 {
2436 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2437 
2438 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2439 		"[BTCoex], Coex Mechanism Init!!\n");
2440 
2441 	btcoexist->stop_coex_dm = false;
2442 
2443 	halbtc8723b1ant_init_coex_dm(btcoexist);
2444 
2445 	halbtc8723b1ant_query_bt_info(btcoexist);
2446 }
2447 
ex_btc8723b1ant_display_coex_info(struct btc_coexist * btcoexist,struct seq_file * m)2448 void ex_btc8723b1ant_display_coex_info(struct btc_coexist *btcoexist,
2449 				       struct seq_file *m)
2450 {
2451 	struct btc_board_info *board_info = &btcoexist->board_info;
2452 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2453 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2454 	u8 u8tmp[4], i, bt_info_ext, pstdmacase = 0;
2455 	u16 u16tmp[4];
2456 	u32 u32tmp[4];
2457 	bool roam = false, scan = false;
2458 	bool link = false, wifi_under_5g = false;
2459 	bool bt_hs_on = false, wifi_busy = false;
2460 	s32 wifi_rssi = 0, bt_hs_rssi = 0;
2461 	u32 wifi_bw, wifi_traffic_dir, fa_ofdm, fa_cck, wifi_link_status;
2462 	u8 wifi_dot11_chnl, wifi_hs_chnl;
2463 	u32 fw_ver = 0, bt_patch_ver = 0;
2464 
2465 	seq_puts(m, "\n ============[BT Coexist info]============");
2466 
2467 	if (btcoexist->manual_control) {
2468 		seq_puts(m, "\n ============[Under Manual Control]==========");
2469 		seq_puts(m, "\n ==========================================");
2470 	}
2471 	if (btcoexist->stop_coex_dm) {
2472 		seq_puts(m, "\n ============[Coex is STOPPED]============");
2473 		seq_puts(m, "\n ==========================================");
2474 	}
2475 
2476 	seq_printf(m, "\n %-35s = %d/ %d/ %d",
2477 		   "Ant PG Num/ Ant Mech/ Ant Pos:",
2478 		   board_info->pg_ant_num, board_info->btdm_ant_num,
2479 		   board_info->btdm_ant_pos);
2480 
2481 	seq_printf(m, "\n %-35s = %s / %d",
2482 		   "BT stack/ hci ext ver",
2483 		   ((stack_info->profile_notified) ? "Yes" : "No"),
2484 		   stack_info->hci_version);
2485 
2486 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
2487 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2488 	seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2489 		   "CoexVer/ FwVer/ PatchVer",
2490 		   glcoex_ver_date_8723b_1ant, glcoex_ver_8723b_1ant,
2491 		   fw_ver, bt_patch_ver, bt_patch_ver);
2492 
2493 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2494 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2495 			   &wifi_dot11_chnl);
2496 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
2497 	seq_printf(m, "\n %-35s = %d / %d(%d)",
2498 		   "Dot11 channel / HsChnl(HsMode)",
2499 		   wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2500 
2501 	seq_printf(m, "\n %-35s = %3ph ",
2502 		   "H2C Wifi inform bt chnl Info",
2503 		   coex_dm->wifi_chnl_info);
2504 
2505 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2506 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2507 	seq_printf(m, "\n %-35s = %d/ %d",
2508 		   "Wifi rssi/ HS rssi", wifi_rssi, bt_hs_rssi);
2509 
2510 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2511 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2512 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2513 	seq_printf(m, "\n %-35s = %d/ %d/ %d ",
2514 		   "Wifi link/ roam/ scan", link, roam, scan);
2515 
2516 	btcoexist->btc_get(btcoexist , BTC_GET_BL_WIFI_UNDER_5G,
2517 			   &wifi_under_5g);
2518 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2519 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2520 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2521 			   &wifi_traffic_dir);
2522 
2523 	seq_printf(m, "\n %-35s = %s / %s/ %s ",
2524 		   "Wifi status", (wifi_under_5g ? "5G" : "2.4G"),
2525 		   ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2526 		    ((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20")),
2527 		    ((!wifi_busy) ? "idle" :
2528 		     ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2529 		     "uplink" : "downlink")));
2530 
2531 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2532 			   &wifi_link_status);
2533 	seq_printf(m, "\n %-35s = %d/ %d/ %d/ %d/ %d",
2534 		   "sta/vwifi/hs/p2pGo/p2pGc",
2535 		   ((wifi_link_status & WIFI_STA_CONNECTED) ? 1 : 0),
2536 		   ((wifi_link_status & WIFI_AP_CONNECTED) ? 1 : 0),
2537 		   ((wifi_link_status & WIFI_HS_CONNECTED) ? 1 : 0),
2538 		   ((wifi_link_status & WIFI_P2P_GO_CONNECTED) ? 1 : 0),
2539 		   ((wifi_link_status & WIFI_P2P_GC_CONNECTED) ? 1 : 0));
2540 
2541 	seq_printf(m, "\n %-35s = [%s/ %d/ %d] ",
2542 		   "BT [status/ rssi/ retryCnt]",
2543 		   ((coex_sta->bt_disabled) ? ("disabled") :
2544 		    ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2545 		     ((BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2546 		       coex_dm->bt_status) ?
2547 		      "non-connected idle" :
2548 		      ((BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2549 			coex_dm->bt_status) ?
2550 		       "connected-idle" : "busy")))),
2551 		       coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2552 
2553 	seq_printf(m, "\n %-35s = %d / %d / %d / %d",
2554 		   "SCO/HID/PAN/A2DP", bt_link_info->sco_exist,
2555 		   bt_link_info->hid_exist, bt_link_info->pan_exist,
2556 		   bt_link_info->a2dp_exist);
2557 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
2558 
2559 	bt_info_ext = coex_sta->bt_info_ext;
2560 	seq_printf(m, "\n %-35s = %s",
2561 		   "BT Info A2DP rate",
2562 		   (bt_info_ext & BIT0) ? "Basic rate" : "EDR rate");
2563 
2564 	for (i = 0; i < BT_INFO_SRC_8723B_1ANT_MAX; i++) {
2565 		if (coex_sta->bt_info_c2h_cnt[i]) {
2566 			seq_printf(m, "\n %-35s = %7ph(%d)",
2567 				   glbt_info_src_8723b_1ant[i],
2568 				   coex_sta->bt_info_c2h[i],
2569 				   coex_sta->bt_info_c2h_cnt[i]);
2570 		}
2571 	}
2572 	seq_printf(m, "\n %-35s = %s/%s, (0x%x/0x%x)",
2573 		   "PS state, IPS/LPS, (lps/rpwm)",
2574 		   ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2575 		   ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")),
2576 		   btcoexist->bt_info.lps_val,
2577 		   btcoexist->bt_info.rpwm_val);
2578 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
2579 
2580 	if (!btcoexist->manual_control) {
2581 		/* Sw mechanism	*/
2582 		seq_printf(m, "\n %-35s",
2583 			   "============[Sw mechanism]============");
2584 
2585 		seq_printf(m, "\n %-35s = %d/",
2586 			   "SM[LowPenaltyRA]", coex_dm->cur_low_penalty_ra);
2587 
2588 		seq_printf(m, "\n %-35s = %s/ %s/ %d ",
2589 			   "DelBA/ BtCtrlAgg/ AggSize",
2590 			   (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2591 			   (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2592 			   btcoexist->bt_info.agg_buf_size);
2593 
2594 		seq_printf(m, "\n %-35s = 0x%x ",
2595 			   "Rate Mask", btcoexist->bt_info.ra_mask);
2596 
2597 		/* Fw mechanism	*/
2598 		seq_printf(m, "\n %-35s",
2599 			   "============[Fw mechanism]============");
2600 
2601 		pstdmacase = coex_dm->cur_ps_tdma;
2602 		seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
2603 			   "PS TDMA", coex_dm->ps_tdma_para,
2604 			   pstdmacase, coex_dm->auto_tdma_adjust);
2605 
2606 		seq_printf(m, "\n %-35s = %d ",
2607 			   "IgnWlanAct", coex_dm->cur_ignore_wlan_act);
2608 
2609 		seq_printf(m, "\n %-35s = 0x%x ",
2610 			   "Latest error condition(should be 0)",
2611 			   coex_dm->error_condition);
2612 	}
2613 
2614 	seq_printf(m, "\n %-35s = %d",
2615 		   "Coex Table Type", coex_sta->coex_table_type);
2616 
2617 	/* Hw setting */
2618 	seq_printf(m, "\n %-35s",
2619 		   "============[Hw setting]============");
2620 
2621 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2622 		   "backup ARFR1/ARFR2/RL/AMaxTime", coex_dm->backup_arfr_cnt1,
2623 		   coex_dm->backup_arfr_cnt2, coex_dm->backup_retry_limit,
2624 		   coex_dm->backup_ampdu_max_time);
2625 
2626 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2627 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2628 	u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2629 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2630 	seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2631 		   "0x430/0x434/0x42a/0x456",
2632 		   u32tmp[0], u32tmp[1], u16tmp[0], u8tmp[0]);
2633 
2634 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2635 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6cc);
2636 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x880);
2637 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2638 		   "0x778/0x6cc/0x880[29:25]", u8tmp[0], u32tmp[0],
2639 		   (u32tmp[1] & 0x3e000000) >> 25);
2640 
2641 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x948);
2642 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
2643 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x765);
2644 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2645 		   "0x948/ 0x67[5] / 0x765",
2646 		   u32tmp[0], ((u8tmp[0] & 0x20) >> 5), u8tmp[1]);
2647 
2648 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x92c);
2649 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x930);
2650 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x944);
2651 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2652 		   "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]",
2653 		   u32tmp[0] & 0x3, u32tmp[1] & 0xff, u32tmp[2] & 0x3);
2654 
2655 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x39);
2656 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2657 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2658 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2659 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2660 		   "0x38[11]/0x40/0x4c[24:23]/0x64[0]",
2661 		   ((u8tmp[0] & 0x8) >> 3), u8tmp[1],
2662 		    ((u32tmp[0] & 0x01800000) >> 23), u8tmp[2] & 0x1);
2663 
2664 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2665 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2666 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
2667 		   "0x550(bcn ctrl)/0x522", u32tmp[0], u8tmp[0]);
2668 
2669 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2670 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x49c);
2671 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x",
2672 		   "0xc50(dig)/0x49c(null-drop)", u32tmp[0] & 0xff, u8tmp[0]);
2673 
2674 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xda0);
2675 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xda4);
2676 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0xda8);
2677 	u32tmp[3] = btcoexist->btc_read_4byte(btcoexist, 0xcf0);
2678 
2679 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5b);
2680 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2681 
2682 	fa_ofdm = ((u32tmp[0] & 0xffff0000) >> 16) +
2683 		  ((u32tmp[1] & 0xffff0000) >> 16) +
2684 		   (u32tmp[1] & 0xffff) +
2685 		   (u32tmp[2] & 0xffff) +
2686 		  ((u32tmp[3] & 0xffff0000) >> 16) +
2687 		   (u32tmp[3] & 0xffff);
2688 	fa_cck = (u8tmp[0] << 8) + u8tmp[1];
2689 
2690 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2691 		   "OFDM-CCA/OFDM-FA/CCK-FA",
2692 		 u32tmp[0] & 0xffff, fa_ofdm, fa_cck);
2693 
2694 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2695 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2696 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2697 	seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2698 		   "0x6c0/0x6c4/0x6c8(coexTable)",
2699 		   u32tmp[0], u32tmp[1], u32tmp[2]);
2700 
2701 	seq_printf(m, "\n %-35s = %d/ %d",
2702 		   "0x770(high-pri rx/tx)", coex_sta->high_priority_rx,
2703 		   coex_sta->high_priority_tx);
2704 	seq_printf(m, "\n %-35s = %d/ %d",
2705 		   "0x774(low-pri rx/tx)", coex_sta->low_priority_rx,
2706 		   coex_sta->low_priority_tx);
2707 	if (btcoexist->auto_report_1ant)
2708 		halbtc8723b1ant_monitor_bt_ctr(btcoexist);
2709 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
2710 }
2711 
ex_btc8723b1ant_ips_notify(struct btc_coexist * btcoexist,u8 type)2712 void ex_btc8723b1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2713 {
2714 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2715 
2716 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2717 		return;
2718 
2719 	if (BTC_IPS_ENTER == type) {
2720 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2721 			"[BTCoex], IPS ENTER notify\n");
2722 		coex_sta->under_ips = true;
2723 
2724 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2725 					     FORCE_EXEC, false, true);
2726 		/* set PTA control */
2727 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2728 		halbtc8723b1ant_coex_table_with_type(btcoexist,
2729 						     NORMAL_EXEC, 0);
2730 	} else if (BTC_IPS_LEAVE == type) {
2731 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2732 			"[BTCoex], IPS LEAVE notify\n");
2733 		coex_sta->under_ips = false;
2734 
2735 		halbtc8723b1ant_init_hw_config(btcoexist, false, false);
2736 		halbtc8723b1ant_init_coex_dm(btcoexist);
2737 		halbtc8723b1ant_query_bt_info(btcoexist);
2738 	}
2739 }
2740 
ex_btc8723b1ant_lps_notify(struct btc_coexist * btcoexist,u8 type)2741 void ex_btc8723b1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2742 {
2743 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2744 
2745 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2746 		return;
2747 
2748 	if (BTC_LPS_ENABLE == type) {
2749 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2750 			"[BTCoex], LPS ENABLE notify\n");
2751 		coex_sta->under_lps = true;
2752 	} else if (BTC_LPS_DISABLE == type) {
2753 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2754 			"[BTCoex], LPS DISABLE notify\n");
2755 		coex_sta->under_lps = false;
2756 	}
2757 }
2758 
ex_btc8723b1ant_scan_notify(struct btc_coexist * btcoexist,u8 type)2759 void ex_btc8723b1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2760 {
2761 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2762 	bool wifi_connected = false, bt_hs_on = false;
2763 	u8 u8tmpa, u8tmpb;
2764 	u32 u32tmp;
2765 	u32 wifi_link_status = 0;
2766 	u32 num_of_wifi_link = 0;
2767 	bool bt_ctrl_agg_buf_size = false;
2768 	u8 agg_buf_size = 5;
2769 
2770 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2771 		return;
2772 
2773 	if (type == BTC_SCAN_START) {
2774 		coex_sta->wifi_is_high_pri_task = true;
2775 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2776 			"[BTCoex], SCAN START notify\n");
2777 		/* Force antenna setup for no scan result issue */
2778 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2779 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2780 					     FORCE_EXEC, false, false);
2781 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
2782 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
2783 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
2784 
2785 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2786 			"[BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
2787 			u32tmp, u8tmpa, u8tmpb);
2788 	} else {
2789 		coex_sta->wifi_is_high_pri_task = false;
2790 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2791 			"[BTCoex], SCAN FINISH notify\n");
2792 
2793 		btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
2794 				   &coex_sta->scan_ap_num);
2795 	}
2796 
2797 	if (coex_sta->bt_disabled)
2798 		return;
2799 
2800 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2801 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2802 			   &wifi_connected);
2803 
2804 	halbtc8723b1ant_query_bt_info(btcoexist);
2805 
2806 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2807 			   &wifi_link_status);
2808 	num_of_wifi_link = wifi_link_status >> 16;
2809 	if (num_of_wifi_link >= 2) {
2810 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2811 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2812 					   bt_ctrl_agg_buf_size, agg_buf_size);
2813 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
2814 		return;
2815 	}
2816 
2817 	if (coex_sta->c2h_bt_inquiry_page) {
2818 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2819 		return;
2820 	} else if (bt_hs_on) {
2821 		halbtc8723b1ant_action_hs(btcoexist);
2822 		return;
2823 	}
2824 
2825 	if (BTC_SCAN_START == type) {
2826 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2827 			"[BTCoex], SCAN START notify\n");
2828 		if (!wifi_connected)
2829 			/* non-connected scan */
2830 			btc8723b1ant_action_wifi_not_conn_scan(btcoexist);
2831 		else
2832 			/* wifi is connected */
2833 			btc8723b1ant_action_wifi_conn_scan(btcoexist);
2834 	} else if (BTC_SCAN_FINISH == type) {
2835 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2836 			"[BTCoex], SCAN FINISH notify\n");
2837 		if (!wifi_connected)
2838 			/* non-connected scan */
2839 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2840 		else
2841 			halbtc8723b1ant_action_wifi_connected(btcoexist);
2842 	}
2843 }
2844 
ex_btc8723b1ant_connect_notify(struct btc_coexist * btcoexist,u8 type)2845 void ex_btc8723b1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2846 {
2847 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2848 	bool wifi_connected = false, bt_hs_on = false;
2849 	u32 wifi_link_status = 0;
2850 	u32 num_of_wifi_link = 0;
2851 	bool bt_ctrl_agg_buf_size = false, under_4way = false;
2852 	u8 agg_buf_size = 5;
2853 
2854 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2855 			   &under_4way);
2856 
2857 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2858 	    coex_sta->bt_disabled)
2859 		return;
2860 
2861 	if (type == BTC_ASSOCIATE_START) {
2862 		coex_sta->wifi_is_high_pri_task = true;
2863 
2864 		/* Force antenna setup for no scan result issue */
2865 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2866 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2867 					     FORCE_EXEC, false, false);
2868 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2869 			"[BTCoex], CONNECT START notify\n");
2870 		coex_dm->arp_cnt = 0;
2871 	} else {
2872 		coex_sta->wifi_is_high_pri_task = false;
2873 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2874 			"[BTCoex], CONNECT FINISH notify\n");
2875 	}
2876 
2877 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2878 			   &wifi_link_status);
2879 	num_of_wifi_link = wifi_link_status>>16;
2880 	if (num_of_wifi_link >= 2) {
2881 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2882 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2883 					   bt_ctrl_agg_buf_size, agg_buf_size);
2884 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
2885 		return;
2886 	}
2887 
2888 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2889 	if (coex_sta->c2h_bt_inquiry_page) {
2890 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
2891 		return;
2892 	} else if (bt_hs_on) {
2893 		halbtc8723b1ant_action_hs(btcoexist);
2894 		return;
2895 	}
2896 
2897 	if (BTC_ASSOCIATE_START == type) {
2898 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2899 			"[BTCoex], CONNECT START notify\n");
2900 		btc8723b1ant_act_wifi_not_conn_asso_auth(btcoexist);
2901 	} else if (BTC_ASSOCIATE_FINISH == type) {
2902 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2903 			"[BTCoex], CONNECT FINISH notify\n");
2904 
2905 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2906 				   &wifi_connected);
2907 		if (!wifi_connected)
2908 			/* non-connected scan */
2909 			btc8723b1ant_action_wifi_not_conn(btcoexist);
2910 		else
2911 			halbtc8723b1ant_action_wifi_connected(btcoexist);
2912 	}
2913 }
2914 
ex_btc8723b1ant_media_status_notify(struct btc_coexist * btcoexist,u8 type)2915 void ex_btc8723b1ant_media_status_notify(struct btc_coexist *btcoexist,
2916 					 u8 type)
2917 {
2918 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2919 	u8 h2c_parameter[3] = {0};
2920 	u32 wifi_bw;
2921 	u8 wifi_central_chnl;
2922 	bool wifi_under_b_mode = false;
2923 
2924 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2925 	    coex_sta->bt_disabled)
2926 		return;
2927 
2928 	if (type == BTC_MEDIA_CONNECT) {
2929 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2930 			"[BTCoex], MEDIA connect notify\n");
2931 		/* Force antenna setup for no scan result issue */
2932 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2933 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2934 					     FORCE_EXEC, false, false);
2935 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
2936 				   &wifi_under_b_mode);
2937 
2938 		/* Set CCK Tx/Rx high Pri except 11b mode */
2939 		if (wifi_under_b_mode) {
2940 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2941 						   0x00); /* CCK Tx */
2942 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2943 						   0x00); /* CCK Rx */
2944 		} else {
2945 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2946 						   0x00); /* CCK Tx */
2947 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2948 						   0x10); /* CCK Rx */
2949 		}
2950 
2951 		coex_dm->backup_arfr_cnt1 =
2952 			btcoexist->btc_read_4byte(btcoexist, 0x430);
2953 		coex_dm->backup_arfr_cnt2 =
2954 			btcoexist->btc_read_4byte(btcoexist, 0x434);
2955 		coex_dm->backup_retry_limit =
2956 			btcoexist->btc_read_2byte(btcoexist, 0x42a);
2957 		coex_dm->backup_ampdu_max_time =
2958 			btcoexist->btc_read_1byte(btcoexist, 0x456);
2959 	} else {
2960 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2961 			"[BTCoex], MEDIA disconnect notify\n");
2962 		coex_dm->arp_cnt = 0;
2963 
2964 		btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x0); /* CCK Tx */
2965 		btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x0); /* CCK Rx */
2966 
2967 		coex_sta->cck_ever_lock = false;
2968 	}
2969 
2970 	/* only 2.4G we need to inform bt the chnl mask */
2971 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
2972 			   &wifi_central_chnl);
2973 
2974 	if (type == BTC_MEDIA_CONNECT && wifi_central_chnl <= 14) {
2975 		h2c_parameter[0] = 0x0;
2976 		h2c_parameter[1] = wifi_central_chnl;
2977 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2978 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2979 			h2c_parameter[2] = 0x30;
2980 		else
2981 			h2c_parameter[2] = 0x20;
2982 	}
2983 
2984 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2985 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2986 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2987 
2988 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2989 		"[BTCoex], FW write 0x66 = 0x%x\n",
2990 		h2c_parameter[0] << 16 | h2c_parameter[1] << 8 |
2991 		h2c_parameter[2]);
2992 
2993 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2994 }
2995 
ex_btc8723b1ant_special_packet_notify(struct btc_coexist * btcoexist,u8 type)2996 void ex_btc8723b1ant_special_packet_notify(struct btc_coexist *btcoexist,
2997 					   u8 type)
2998 {
2999 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3000 	bool bt_hs_on = false;
3001 	u32 wifi_link_status = 0;
3002 	u32 num_of_wifi_link = 0;
3003 	bool bt_ctrl_agg_buf_size = false, under_4way = false;
3004 	u8 agg_buf_size = 5;
3005 
3006 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
3007 			   &under_4way);
3008 
3009 	if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
3010 	    coex_sta->bt_disabled)
3011 		return;
3012 
3013 	if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
3014 	    type == BTC_PACKET_ARP) {
3015 		if (type == BTC_PACKET_ARP) {
3016 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3017 				"[BTCoex], special Packet ARP notify\n");
3018 
3019 			coex_dm->arp_cnt++;
3020 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3021 				"[BTCoex], ARP Packet Count = %d\n",
3022 				 coex_dm->arp_cnt);
3023 
3024 			if ((coex_dm->arp_cnt >= 10) && (!under_4way))
3025 				/* if APR PKT > 10 after connect, do not go to
3026 				 * ActionWifiConnectedSpecificPacket(btcoexist)
3027 				 */
3028 				coex_sta->wifi_is_high_pri_task = false;
3029 			else
3030 				coex_sta->wifi_is_high_pri_task = true;
3031 		} else {
3032 			coex_sta->wifi_is_high_pri_task = true;
3033 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3034 				"[BTCoex], special Packet DHCP or EAPOL notify\n");
3035 		}
3036 	} else {
3037 		coex_sta->wifi_is_high_pri_task = false;
3038 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3039 			"[BTCoex], special Packet [Type = %d] notify\n",
3040 			 type);
3041 	}
3042 
3043 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3044 		&wifi_link_status);
3045 	num_of_wifi_link = wifi_link_status >> 16;
3046 	if (num_of_wifi_link >= 2) {
3047 		halbtc8723b1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
3048 		halbtc8723b1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
3049 					   bt_ctrl_agg_buf_size, agg_buf_size);
3050 		halbtc8723b1ant_action_wifi_multiport(btcoexist);
3051 		return;
3052 	}
3053 
3054 	coex_sta->special_pkt_period_cnt = 0;
3055 
3056 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3057 	if (coex_sta->c2h_bt_inquiry_page) {
3058 		halbtc8723b1ant_action_bt_inquiry(btcoexist);
3059 		return;
3060 	} else if (bt_hs_on) {
3061 		halbtc8723b1ant_action_hs(btcoexist);
3062 		return;
3063 	}
3064 
3065 	if (BTC_PACKET_DHCP == type ||
3066 	    BTC_PACKET_EAPOL == type) {
3067 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3068 			"[BTCoex], special Packet(%d) notify\n", type);
3069 		halbtc8723b1ant_action_wifi_connected_special_packet(btcoexist);
3070 	}
3071 }
3072 
ex_btc8723b1ant_bt_info_notify(struct btc_coexist * btcoexist,u8 * tmp_buf,u8 length)3073 void ex_btc8723b1ant_bt_info_notify(struct btc_coexist *btcoexist,
3074 				    u8 *tmp_buf, u8 length)
3075 {
3076 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3077 	u8 bt_info = 0;
3078 	u8 i, rsp_source = 0;
3079 	bool wifi_connected = false;
3080 	bool bt_busy = false;
3081 
3082 	coex_sta->c2h_bt_info_req_sent = false;
3083 
3084 	rsp_source = tmp_buf[0] & 0xf;
3085 	if (rsp_source >= BT_INFO_SRC_8723B_1ANT_MAX)
3086 		rsp_source = BT_INFO_SRC_8723B_1ANT_WIFI_FW;
3087 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
3088 
3089 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3090 		"[BTCoex], Bt info[%d], length=%d, hex data = [",
3091 		 rsp_source, length);
3092 	for (i = 0; i < length; i++) {
3093 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
3094 		if (i == 1)
3095 			bt_info = tmp_buf[i];
3096 		if (i == length - 1)
3097 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3098 				"0x%02x]\n", tmp_buf[i]);
3099 		else
3100 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3101 				"0x%02x, ", tmp_buf[i]);
3102 	}
3103 
3104 	/* if 0xff, it means BT is under WHCK test */
3105 	if (bt_info == 0xff)
3106 		coex_sta->bt_whck_test = true;
3107 	else
3108 		coex_sta->bt_whck_test = false;
3109 
3110 	if (rsp_source != BT_INFO_SRC_8723B_1ANT_WIFI_FW) {
3111 		coex_sta->bt_retry_cnt = /* [3:0] */
3112 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
3113 
3114 		if (coex_sta->bt_retry_cnt >= 1)
3115 			coex_sta->pop_event_cnt++;
3116 
3117 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
3118 			coex_sta->c2h_bt_remote_name_req = true;
3119 		else
3120 			coex_sta->c2h_bt_remote_name_req = false;
3121 
3122 		coex_sta->bt_rssi =
3123 			coex_sta->bt_info_c2h[rsp_source][3] * 2 - 90;
3124 
3125 		coex_sta->bt_info_ext =
3126 			coex_sta->bt_info_c2h[rsp_source][4];
3127 
3128 		if (coex_sta->bt_info_c2h[rsp_source][1] == 0x49) {
3129 			coex_sta->a2dp_bit_pool =
3130 				coex_sta->bt_info_c2h[rsp_source][6];
3131 		} else {
3132 			coex_sta->a2dp_bit_pool = 0;
3133 		}
3134 
3135 		coex_sta->bt_tx_rx_mask =
3136 			(coex_sta->bt_info_c2h[rsp_source][2] & 0x40);
3137 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
3138 				   &coex_sta->bt_tx_rx_mask);
3139 
3140 		if (!coex_sta->bt_tx_rx_mask) {
3141 			/* BT into is responded by BT FW and BT RF REG
3142 			 * 0x3C != 0x15 => Need to switch BT TRx Mask
3143 			 */
3144 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3145 				"[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
3146 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3147 						  0x3c, 0x15);
3148 
3149 			/* BT TRx Mask lock 0x2c[0], 0x30[0] = 0 */
3150 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3151 						  0x2c, 0x7c44);
3152 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3153 						  0x30, 0x7c44);
3154 		}
3155 
3156 		/* Here we need to resend some wifi info to BT
3157 		 * because bt is reset and loss of the info.
3158 		 */
3159 		if (coex_sta->bt_info_ext & BIT1) {
3160 			rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3161 				"[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
3162 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3163 					   &wifi_connected);
3164 			if (wifi_connected)
3165 				ex_btc8723b1ant_media_status_notify(btcoexist,
3166 						BTC_MEDIA_CONNECT);
3167 			else
3168 				ex_btc8723b1ant_media_status_notify(btcoexist,
3169 						BTC_MEDIA_DISCONNECT);
3170 		}
3171 
3172 		if (coex_sta->bt_info_ext & BIT3) {
3173 			if (!btcoexist->manual_control &&
3174 			    !btcoexist->stop_coex_dm) {
3175 				rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3176 					"[BTCoex], BT ext info bit3 check, set BT NOT ignore Wlan active!!\n");
3177 				halbtc8723b1ant_ignore_wlan_act(btcoexist,
3178 								FORCE_EXEC,
3179 								false);
3180 			}
3181 		} else {
3182 			/* BT already NOT ignore Wlan active, do nothing here.*/
3183 		}
3184 		if (!btcoexist->auto_report_1ant) {
3185 			if (coex_sta->bt_info_ext & BIT4) {
3186 				/* BT auto report already enabled, do nothing */
3187 			} else {
3188 				halbtc8723b1ant_bt_auto_report(btcoexist,
3189 							       FORCE_EXEC,
3190 							       true);
3191 			}
3192 		}
3193 	}
3194 
3195 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
3196 	if (bt_info & BT_INFO_8723B_1ANT_B_INQ_PAGE)
3197 		coex_sta->c2h_bt_inquiry_page = true;
3198 	else
3199 		coex_sta->c2h_bt_inquiry_page = false;
3200 
3201 	coex_sta->num_of_profile = 0;
3202 
3203 	/* set link exist status */
3204 	if (!(bt_info & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3205 		coex_sta->bt_link_exist = false;
3206 		coex_sta->pan_exist = false;
3207 		coex_sta->a2dp_exist = false;
3208 		coex_sta->hid_exist = false;
3209 		coex_sta->sco_exist = false;
3210 
3211 		coex_sta->bt_hi_pri_link_exist = false;
3212 	} else {
3213 		/* connection exists */
3214 		coex_sta->bt_link_exist = true;
3215 		if (bt_info & BT_INFO_8723B_1ANT_B_FTP) {
3216 			coex_sta->pan_exist = true;
3217 			coex_sta->num_of_profile++;
3218 		} else {
3219 			coex_sta->pan_exist = false;
3220 		}
3221 		if (bt_info & BT_INFO_8723B_1ANT_B_A2DP) {
3222 			coex_sta->a2dp_exist = true;
3223 			coex_sta->num_of_profile++;
3224 		} else {
3225 			coex_sta->a2dp_exist = false;
3226 		}
3227 		if (bt_info & BT_INFO_8723B_1ANT_B_HID) {
3228 			coex_sta->hid_exist = true;
3229 			coex_sta->num_of_profile++;
3230 		} else {
3231 			coex_sta->hid_exist = false;
3232 		}
3233 		if (bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO) {
3234 			coex_sta->sco_exist = true;
3235 			coex_sta->num_of_profile++;
3236 		} else {
3237 			coex_sta->sco_exist = false;
3238 		}
3239 
3240 		if ((!coex_sta->hid_exist) &&
3241 		    (!coex_sta->c2h_bt_inquiry_page) &&
3242 		    (!coex_sta->sco_exist)) {
3243 			if (coex_sta->high_priority_tx +
3244 				    coex_sta->high_priority_rx >=
3245 			    160) {
3246 				coex_sta->hid_exist = true;
3247 				coex_sta->wrong_profile_notification++;
3248 				coex_sta->num_of_profile++;
3249 				bt_info = bt_info | 0x28;
3250 			}
3251 		}
3252 
3253 		/* Add Hi-Pri Tx/Rx counter to avoid false detection */
3254 		if (((coex_sta->hid_exist) || (coex_sta->sco_exist)) &&
3255 		    (coex_sta->high_priority_tx + coex_sta->high_priority_rx >=
3256 		     160) &&
3257 		    (!coex_sta->c2h_bt_inquiry_page))
3258 			coex_sta->bt_hi_pri_link_exist = true;
3259 
3260 		if ((bt_info & BT_INFO_8723B_1ANT_B_ACL_BUSY) &&
3261 		    (coex_sta->num_of_profile == 0)) {
3262 			if (coex_sta->low_priority_tx +
3263 				    coex_sta->low_priority_rx >=
3264 			    160) {
3265 				coex_sta->pan_exist = true;
3266 				coex_sta->num_of_profile++;
3267 				coex_sta->wrong_profile_notification++;
3268 				bt_info = bt_info | 0x88;
3269 			}
3270 		}
3271 	}
3272 
3273 	halbtc8723b1ant_update_bt_link_info(btcoexist);
3274 
3275 	/* mask profile bit for connect-ilde identification
3276 	 * ( for CSR case: A2DP idle --> 0x41)
3277 	 */
3278 	bt_info = bt_info & 0x1f;
3279 
3280 	if (!(bt_info & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3281 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3282 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3283 			"[BTCoex], BtInfoNotify(), BT Non-Connected idle!\n");
3284 	/* connection exists but no busy */
3285 	} else if (bt_info == BT_INFO_8723B_1ANT_B_CONNECTION) {
3286 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE;
3287 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3288 			"[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
3289 	} else if ((bt_info & BT_INFO_8723B_1ANT_B_SCO_ESCO) ||
3290 		(bt_info & BT_INFO_8723B_1ANT_B_SCO_BUSY)) {
3291 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_SCO_BUSY;
3292 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3293 			"[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
3294 	} else if (bt_info & BT_INFO_8723B_1ANT_B_ACL_BUSY) {
3295 		if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
3296 			coex_dm->auto_tdma_adjust = false;
3297 
3298 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_ACL_BUSY;
3299 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3300 			"[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
3301 	} else {
3302 		coex_dm->bt_status = BT_8723B_1ANT_BT_STATUS_MAX;
3303 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3304 			"[BTCoex], BtInfoNotify(), BT Non-Defined state!!\n");
3305 	}
3306 
3307 	if ((BT_8723B_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3308 	    (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3309 	    (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
3310 		bt_busy = true;
3311 	else
3312 		bt_busy = false;
3313 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
3314 
3315 	halbtc8723b1ant_run_coexist_mechanism(btcoexist);
3316 }
3317 
ex_btc8723b1ant_rf_status_notify(struct btc_coexist * btcoexist,u8 type)3318 void ex_btc8723b1ant_rf_status_notify(struct btc_coexist *btcoexist, u8 type)
3319 {
3320 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3321 	u32 u32tmp;
3322 	u8 u8tmpa, u8tmpb, u8tmpc;
3323 
3324 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3325 		"[BTCoex], RF Status notify\n");
3326 
3327 	if (type == BTC_RF_ON) {
3328 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3329 			"[BTCoex], RF is turned ON!!\n");
3330 		btcoexist->stop_coex_dm = false;
3331 	} else if (type == BTC_RF_OFF) {
3332 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3333 			"[BTCoex], RF is turned OFF!!\n");
3334 
3335 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3336 						 0x0, 0x0);
3337 		halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3338 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
3339 					     FORCE_EXEC, false, true);
3340 
3341 		halbtc8723b1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3342 		btcoexist->stop_coex_dm = true;
3343 
3344 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
3345 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
3346 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
3347 		u8tmpc = btcoexist->btc_read_1byte(btcoexist, 0x76e);
3348 
3349 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3350 			"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x, 0x76e=0x%x\n",
3351 			u32tmp, u8tmpa, u8tmpb, u8tmpc);
3352 	}
3353 }
3354 
ex_btc8723b1ant_halt_notify(struct btc_coexist * btcoexist)3355 void ex_btc8723b1ant_halt_notify(struct btc_coexist *btcoexist)
3356 {
3357 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3358 
3359 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Halt notify\n");
3360 
3361 	btcoexist->stop_coex_dm = true;
3362 
3363 	halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, FORCE_EXEC,
3364 				     false, true);
3365 
3366 	halbtc8723b1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3367 
3368 	halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3369 					 0x0, 0x0);
3370 	halbtc8723b1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3371 
3372 	ex_btc8723b1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
3373 
3374 	btcoexist->stop_coex_dm = true;
3375 }
3376 
ex_btc8723b1ant_pnp_notify(struct btc_coexist * btcoexist,u8 pnp_state)3377 void ex_btc8723b1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
3378 {
3379 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3380 
3381 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD, "[BTCoex], Pnp notify\n");
3382 
3383 	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
3384 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3385 			"[BTCoex], Pnp notify to SLEEP\n");
3386 		halbtc8723b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
3387 					     FORCE_EXEC, false, true);
3388 		halbtc8723b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3389 						 0x0, 0x0);
3390 		halbtc8723b1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
3391 		halbtc8723b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
3392 
3393 		/* Driver do not leave IPS/LPS when driver is going to sleep, so
3394 		 * BTCoexistence think wifi is still under IPS/LPS
3395 		 *
3396 		 * BT should clear UnderIPS/UnderLPS state to avoid mismatch
3397 		 * state after wakeup.
3398 		 */
3399 		coex_sta->under_ips = false;
3400 		coex_sta->under_lps = false;
3401 		btcoexist->stop_coex_dm = true;
3402 	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
3403 		rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3404 			"[BTCoex], Pnp notify to WAKE UP\n");
3405 		btcoexist->stop_coex_dm = false;
3406 		halbtc8723b1ant_init_hw_config(btcoexist, false, false);
3407 		halbtc8723b1ant_init_coex_dm(btcoexist);
3408 		halbtc8723b1ant_query_bt_info(btcoexist);
3409 	}
3410 }
3411 
ex_btc8723b1ant_coex_dm_reset(struct btc_coexist * btcoexist)3412 void ex_btc8723b1ant_coex_dm_reset(struct btc_coexist *btcoexist)
3413 {
3414 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3415 
3416 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3417 		"[BTCoex], *****************Coex DM Reset****************\n");
3418 
3419 	halbtc8723b1ant_init_hw_config(btcoexist, false, false);
3420 	halbtc8723b1ant_init_coex_dm(btcoexist);
3421 }
3422 
ex_btc8723b1ant_periodical(struct btc_coexist * btcoexist)3423 void ex_btc8723b1ant_periodical(struct btc_coexist *btcoexist)
3424 {
3425 	struct rtl_priv *rtlpriv = btcoexist->adapter;
3426 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3427 
3428 	rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
3429 		"[BTCoex], ==========================Periodical===========================\n");
3430 
3431 	if (!btcoexist->auto_report_1ant) {
3432 		halbtc8723b1ant_query_bt_info(btcoexist);
3433 		halbtc8723b1ant_monitor_bt_enable_disable(btcoexist);
3434 	} else {
3435 		halbtc8723b1ant_monitor_bt_ctr(btcoexist);
3436 		halbtc8723b1ant_monitor_wifi_ctr(btcoexist);
3437 
3438 		if ((coex_sta->high_priority_tx + coex_sta->high_priority_rx < 50) &&
3439 		    bt_link_info->hid_exist)
3440 			bt_link_info->hid_exist = false;
3441 
3442 		if (btc8723b1ant_is_wifi_status_changed(btcoexist) ||
3443 		    coex_dm->auto_tdma_adjust) {
3444 			halbtc8723b1ant_run_coexist_mechanism(btcoexist);
3445 		}
3446 		coex_sta->special_pkt_period_cnt++;
3447 	}
3448 }
3449