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