2 * Copyright (c) 2012 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * EEPROM has 2 4-bit fields containing the card configuration.
27 * bb_enable_ant_div_lnadiv : 1
28 * bb_ant_div_alt_gaintb : 1
29 * bb_ant_div_main_gaintb : 1
30 * bb_enable_ant_fast_div : 1
34 * bb_ant_div_alt_lnaconf : 2
35 * bb_ant_div_main_lnaconf : 2
37 * The EEPROM bits are used as follows:
38 * ------------------------------------
40 * bb_enable_ant_div_lnadiv - Enable LNA path rx antenna diversity/combining.
41 * Set in AR_PHY_MULTICHAIN_GAIN_CTL.
43 * bb_ant_div_[alt/main]_gaintb - 0 -> Antenna config Alt/Main uses gaintable 0
44 * 1 -> Antenna config Alt/Main uses gaintable 1
45 * Set in AR_PHY_MULTICHAIN_GAIN_CTL.
47 * bb_enable_ant_fast_div - Enable fast antenna diversity.
48 * Set in AR_PHY_CCK_DETECT.
50 * bb_ant_div_[alt/main]_lnaconf - Alt/Main LNA diversity/combining input config.
51 * Set in AR_PHY_MULTICHAIN_GAIN_CTL.
57 * AR9485 / AR9565 / AR9331
58 * ========================
60 * The same bits are present in the EEPROM, but the location in the
61 * EEPROM is different (ant_div_control in ar9300_BaseExtension_1).
63 * ant_div_alt_lnaconf ==> bit 0~1
64 * ant_div_main_lnaconf ==> bit 2~3
65 * ant_div_alt_gaintb ==> bit 4
66 * ant_div_main_gaintb ==> bit 5
67 * enable_ant_div_lnadiv ==> bit 6
68 * enable_ant_fast_div ==> bit 7
71 static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
72 int mindelta, int main_rssi_avg,
73 int alt_rssi_avg, int pkt_count)
75 return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
76 (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
77 (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
80 static inline bool ath_ant_div_comb_alt_check(struct ath_hw_antcomb_conf *conf,
81 int alt_ratio, int alt_rssi_avg,
84 bool result, set1, set2;
86 result = set1 = set2 = false;
88 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2 &&
89 conf->alt_lna_conf == ATH_ANT_DIV_COMB_LNA1)
92 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA1 &&
93 conf->alt_lna_conf == ATH_ANT_DIV_COMB_LNA2)
96 switch (conf->div_group) {
98 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
103 if (alt_rssi_avg < 4)
106 if ((set1 && (alt_rssi_avg >= (main_rssi_avg - 5))) ||
107 (set2 && (alt_rssi_avg >= (main_rssi_avg - 2))))
112 if (alt_rssi_avg < 4)
115 if ((set1 && (alt_rssi_avg >= (main_rssi_avg - 3))) ||
116 (set2 && (alt_rssi_avg >= (main_rssi_avg + 3))))
125 static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
126 struct ath_hw_antcomb_conf ant_conf,
129 antcomb->quick_scan_cnt = 0;
131 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
132 antcomb->rssi_lna2 = main_rssi_avg;
133 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
134 antcomb->rssi_lna1 = main_rssi_avg;
136 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
137 case 0x10: /* LNA2 A-B */
138 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
139 antcomb->first_quick_scan_conf =
140 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
141 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
143 case 0x20: /* LNA1 A-B */
144 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
145 antcomb->first_quick_scan_conf =
146 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
147 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
149 case 0x21: /* LNA1 LNA2 */
150 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
151 antcomb->first_quick_scan_conf =
152 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
153 antcomb->second_quick_scan_conf =
154 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
156 case 0x12: /* LNA2 LNA1 */
157 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
158 antcomb->first_quick_scan_conf =
159 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
160 antcomb->second_quick_scan_conf =
161 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
163 case 0x13: /* LNA2 A+B */
164 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
165 antcomb->first_quick_scan_conf =
166 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
167 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
169 case 0x23: /* LNA1 A+B */
170 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
171 antcomb->first_quick_scan_conf =
172 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
173 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
180 static void ath_ant_set_alt_ratio(struct ath_ant_comb *antcomb,
181 struct ath_hw_antcomb_conf *conf)
183 /* set alt to the conf with maximun ratio */
184 if (antcomb->first_ratio && antcomb->second_ratio) {
185 if (antcomb->rssi_second > antcomb->rssi_third) {
187 if ((antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
188 (antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
189 /* Set alt LNA1 or LNA2*/
190 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
191 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
193 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
195 /* Set alt to A+B or A-B */
197 antcomb->first_quick_scan_conf;
198 } else if ((antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
199 (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2)) {
200 /* Set alt LNA1 or LNA2 */
201 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
202 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
204 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
206 /* Set alt to A+B or A-B */
207 conf->alt_lna_conf = antcomb->second_quick_scan_conf;
209 } else if (antcomb->first_ratio) {
211 if ((antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
212 (antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
213 /* Set alt LNA1 or LNA2 */
214 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
215 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
217 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
219 /* Set alt to A+B or A-B */
220 conf->alt_lna_conf = antcomb->first_quick_scan_conf;
221 } else if (antcomb->second_ratio) {
223 if ((antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
224 (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
225 /* Set alt LNA1 or LNA2 */
226 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
227 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
229 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
231 /* Set alt to A+B or A-B */
232 conf->alt_lna_conf = antcomb->second_quick_scan_conf;
234 /* main is largest */
235 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
236 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
237 /* Set alt LNA1 or LNA2 */
238 if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
239 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
241 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
243 /* Set alt to A+B or A-B */
244 conf->alt_lna_conf = antcomb->main_conf;
248 static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
249 struct ath_hw_antcomb_conf *div_ant_conf,
250 int main_rssi_avg, int alt_rssi_avg,
254 switch (antcomb->quick_scan_cnt) {
256 /* set alt to main, and alt to first conf */
257 div_ant_conf->main_lna_conf = antcomb->main_conf;
258 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
261 /* set alt to main, and alt to first conf */
262 div_ant_conf->main_lna_conf = antcomb->main_conf;
263 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
264 antcomb->rssi_first = main_rssi_avg;
265 antcomb->rssi_second = alt_rssi_avg;
267 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
269 if (ath_is_alt_ant_ratio_better(alt_ratio,
270 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
271 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
272 main_rssi_avg, alt_rssi_avg,
273 antcomb->total_pkt_count))
274 antcomb->first_ratio = true;
276 antcomb->first_ratio = false;
277 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
278 if (ath_is_alt_ant_ratio_better(alt_ratio,
279 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
280 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
281 main_rssi_avg, alt_rssi_avg,
282 antcomb->total_pkt_count))
283 antcomb->first_ratio = true;
285 antcomb->first_ratio = false;
287 if (ath_is_alt_ant_ratio_better(alt_ratio,
288 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
290 main_rssi_avg, alt_rssi_avg,
291 antcomb->total_pkt_count))
292 antcomb->first_ratio = true;
294 antcomb->first_ratio = false;
298 antcomb->alt_good = false;
299 antcomb->scan_not_start = false;
300 antcomb->scan = false;
301 antcomb->rssi_first = main_rssi_avg;
302 antcomb->rssi_third = alt_rssi_avg;
304 switch(antcomb->second_quick_scan_conf) {
305 case ATH_ANT_DIV_COMB_LNA1:
306 antcomb->rssi_lna1 = alt_rssi_avg;
308 case ATH_ANT_DIV_COMB_LNA2:
309 antcomb->rssi_lna2 = alt_rssi_avg;
311 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
312 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
313 antcomb->rssi_lna2 = main_rssi_avg;
314 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
315 antcomb->rssi_lna1 = main_rssi_avg;
321 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
322 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
323 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
325 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
327 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
328 if (ath_is_alt_ant_ratio_better(alt_ratio,
329 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
330 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
331 main_rssi_avg, alt_rssi_avg,
332 antcomb->total_pkt_count))
333 antcomb->second_ratio = true;
335 antcomb->second_ratio = false;
336 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
337 if (ath_is_alt_ant_ratio_better(alt_ratio,
338 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
339 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
340 main_rssi_avg, alt_rssi_avg,
341 antcomb->total_pkt_count))
342 antcomb->second_ratio = true;
344 antcomb->second_ratio = false;
346 if (ath_is_alt_ant_ratio_better(alt_ratio,
347 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
349 main_rssi_avg, alt_rssi_avg,
350 antcomb->total_pkt_count))
351 antcomb->second_ratio = true;
353 antcomb->second_ratio = false;
356 ath_ant_set_alt_ratio(antcomb, div_ant_conf);
364 static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
365 struct ath_ant_comb *antcomb,
368 ant_conf->main_gaintb = 0;
369 ant_conf->alt_gaintb = 0;
371 if (ant_conf->div_group == 0) {
372 /* Adjust the fast_div_bias based on main and alt lna conf */
373 switch ((ant_conf->main_lna_conf << 4) |
374 ant_conf->alt_lna_conf) {
375 case 0x01: /* A-B LNA2 */
376 ant_conf->fast_div_bias = 0x3b;
378 case 0x02: /* A-B LNA1 */
379 ant_conf->fast_div_bias = 0x3d;
381 case 0x03: /* A-B A+B */
382 ant_conf->fast_div_bias = 0x1;
384 case 0x10: /* LNA2 A-B */
385 ant_conf->fast_div_bias = 0x7;
387 case 0x12: /* LNA2 LNA1 */
388 ant_conf->fast_div_bias = 0x2;
390 case 0x13: /* LNA2 A+B */
391 ant_conf->fast_div_bias = 0x7;
393 case 0x20: /* LNA1 A-B */
394 ant_conf->fast_div_bias = 0x6;
396 case 0x21: /* LNA1 LNA2 */
397 ant_conf->fast_div_bias = 0x0;
399 case 0x23: /* LNA1 A+B */
400 ant_conf->fast_div_bias = 0x6;
402 case 0x30: /* A+B A-B */
403 ant_conf->fast_div_bias = 0x1;
405 case 0x31: /* A+B LNA2 */
406 ant_conf->fast_div_bias = 0x3b;
408 case 0x32: /* A+B LNA1 */
409 ant_conf->fast_div_bias = 0x3d;
414 } else if (ant_conf->div_group == 1) {
415 /* Adjust the fast_div_bias based on main and alt_lna_conf */
416 switch ((ant_conf->main_lna_conf << 4) |
417 ant_conf->alt_lna_conf) {
418 case 0x01: /* A-B LNA2 */
419 ant_conf->fast_div_bias = 0x1;
421 case 0x02: /* A-B LNA1 */
422 ant_conf->fast_div_bias = 0x1;
424 case 0x03: /* A-B A+B */
425 ant_conf->fast_div_bias = 0x1;
427 case 0x10: /* LNA2 A-B */
428 if (!(antcomb->scan) &&
429 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
430 ant_conf->fast_div_bias = 0x3f;
432 ant_conf->fast_div_bias = 0x1;
434 case 0x12: /* LNA2 LNA1 */
435 ant_conf->fast_div_bias = 0x1;
437 case 0x13: /* LNA2 A+B */
438 if (!(antcomb->scan) &&
439 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
440 ant_conf->fast_div_bias = 0x3f;
442 ant_conf->fast_div_bias = 0x1;
444 case 0x20: /* LNA1 A-B */
445 if (!(antcomb->scan) &&
446 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
447 ant_conf->fast_div_bias = 0x3f;
449 ant_conf->fast_div_bias = 0x1;
451 case 0x21: /* LNA1 LNA2 */
452 ant_conf->fast_div_bias = 0x1;
454 case 0x23: /* LNA1 A+B */
455 if (!(antcomb->scan) &&
456 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
457 ant_conf->fast_div_bias = 0x3f;
459 ant_conf->fast_div_bias = 0x1;
461 case 0x30: /* A+B A-B */
462 ant_conf->fast_div_bias = 0x1;
464 case 0x31: /* A+B LNA2 */
465 ant_conf->fast_div_bias = 0x1;
467 case 0x32: /* A+B LNA1 */
468 ant_conf->fast_div_bias = 0x1;
473 } else if (ant_conf->div_group == 2) {
474 /* Adjust the fast_div_bias based on main and alt_lna_conf */
475 switch ((ant_conf->main_lna_conf << 4) |
476 ant_conf->alt_lna_conf) {
477 case 0x01: /* A-B LNA2 */
478 ant_conf->fast_div_bias = 0x1;
480 case 0x02: /* A-B LNA1 */
481 ant_conf->fast_div_bias = 0x1;
483 case 0x03: /* A-B A+B */
484 ant_conf->fast_div_bias = 0x1;
486 case 0x10: /* LNA2 A-B */
487 if (!(antcomb->scan) &&
488 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
489 ant_conf->fast_div_bias = 0x1;
491 ant_conf->fast_div_bias = 0x2;
493 case 0x12: /* LNA2 LNA1 */
494 ant_conf->fast_div_bias = 0x1;
496 case 0x13: /* LNA2 A+B */
497 if (!(antcomb->scan) &&
498 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
499 ant_conf->fast_div_bias = 0x1;
501 ant_conf->fast_div_bias = 0x2;
503 case 0x20: /* LNA1 A-B */
504 if (!(antcomb->scan) &&
505 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
506 ant_conf->fast_div_bias = 0x1;
508 ant_conf->fast_div_bias = 0x2;
510 case 0x21: /* LNA1 LNA2 */
511 ant_conf->fast_div_bias = 0x1;
513 case 0x23: /* LNA1 A+B */
514 if (!(antcomb->scan) &&
515 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
516 ant_conf->fast_div_bias = 0x1;
518 ant_conf->fast_div_bias = 0x2;
520 case 0x30: /* A+B A-B */
521 ant_conf->fast_div_bias = 0x1;
523 case 0x31: /* A+B LNA2 */
524 ant_conf->fast_div_bias = 0x1;
526 case 0x32: /* A+B LNA1 */
527 ant_conf->fast_div_bias = 0x1;
532 } else if (ant_conf->div_group == 3) {
533 switch ((ant_conf->main_lna_conf << 4) |
534 ant_conf->alt_lna_conf) {
535 case 0x01: /* A-B LNA2 */
536 ant_conf->fast_div_bias = 0x1;
538 case 0x02: /* A-B LNA1 */
539 ant_conf->fast_div_bias = 0x39;
541 case 0x03: /* A-B A+B */
542 ant_conf->fast_div_bias = 0x1;
544 case 0x10: /* LNA2 A-B */
545 if ((antcomb->scan == 0) &&
546 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)) {
547 ant_conf->fast_div_bias = 0x3f;
549 ant_conf->fast_div_bias = 0x1;
552 case 0x12: /* LNA2 LNA1 */
553 ant_conf->fast_div_bias = 0x39;
555 case 0x13: /* LNA2 A+B */
556 if ((antcomb->scan == 0) &&
557 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)) {
558 ant_conf->fast_div_bias = 0x3f;
560 ant_conf->fast_div_bias = 0x1;
563 case 0x20: /* LNA1 A-B */
564 if ((antcomb->scan == 0) &&
565 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)) {
566 ant_conf->fast_div_bias = 0x3f;
568 ant_conf->fast_div_bias = 0x4;
571 case 0x21: /* LNA1 LNA2 */
572 ant_conf->fast_div_bias = 0x6;
574 case 0x23: /* LNA1 A+B */
575 if ((antcomb->scan == 0) &&
576 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)) {
577 ant_conf->fast_div_bias = 0x3f;
579 ant_conf->fast_div_bias = 0x6;
582 case 0x30: /* A+B A-B */
583 ant_conf->fast_div_bias = 0x1;
585 case 0x31: /* A+B LNA2 */
586 ant_conf->fast_div_bias = 0x6;
588 case 0x32: /* A+B LNA1 */
589 ant_conf->fast_div_bias = 0x1;
597 static void ath_ant_try_scan(struct ath_ant_comb *antcomb,
598 struct ath_hw_antcomb_conf *conf,
599 int curr_alt_set, int alt_rssi_avg,
602 switch (curr_alt_set) {
603 case ATH_ANT_DIV_COMB_LNA2:
604 antcomb->rssi_lna2 = alt_rssi_avg;
605 antcomb->rssi_lna1 = main_rssi_avg;
606 antcomb->scan = true;
608 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
609 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
611 case ATH_ANT_DIV_COMB_LNA1:
612 antcomb->rssi_lna1 = alt_rssi_avg;
613 antcomb->rssi_lna2 = main_rssi_avg;
614 antcomb->scan = true;
616 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
617 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
619 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
620 antcomb->rssi_add = alt_rssi_avg;
621 antcomb->scan = true;
623 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
625 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
626 antcomb->rssi_sub = alt_rssi_avg;
627 antcomb->scan = false;
628 if (antcomb->rssi_lna2 >
629 (antcomb->rssi_lna1 + ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
630 /* use LNA2 as main LNA */
631 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
632 (antcomb->rssi_add > antcomb->rssi_sub)) {
634 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
635 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
636 } else if (antcomb->rssi_sub >
637 antcomb->rssi_lna1) {
639 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
640 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
643 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
644 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
647 /* use LNA1 as main LNA */
648 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
649 (antcomb->rssi_add > antcomb->rssi_sub)) {
651 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
652 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
653 } else if (antcomb->rssi_sub >
654 antcomb->rssi_lna1) {
656 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
657 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
660 conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
661 conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
670 static bool ath_ant_try_switch(struct ath_hw_antcomb_conf *div_ant_conf,
671 int alt_ratio, int alt_rssi_avg,
672 int main_rssi_avg, int curr_main_set,
677 if (ath_ant_div_comb_alt_check(div_ant_conf, alt_ratio,
678 alt_rssi_avg, main_rssi_avg)) {
679 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
681 * Switch main and alt LNA.
683 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
684 div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
685 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
686 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
687 div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
691 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
692 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
694 Set alt to another LNA.
696 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
697 div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
698 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
699 div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
707 static bool ath_ant_short_scan_check(struct ath_ant_comb *antcomb)
711 if (!antcomb->scan || !antcomb->alt_good)
714 if (time_after(jiffies, antcomb->scan_start_time +
715 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
718 if (antcomb->total_pkt_count == ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
719 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
720 antcomb->total_pkt_count);
721 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
728 void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
730 struct ath_hw_antcomb_conf div_ant_conf;
731 struct ath_ant_comb *antcomb = &sc->ant_comb;
732 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
734 int main_rssi = rs->rs_rssi_ctl0;
735 int alt_rssi = rs->rs_rssi_ctl1;
736 int rx_ant_conf, main_ant_conf;
737 bool short_scan = false, ret;
739 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
741 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
744 /* Record packet only when both main_rssi and alt_rssi is positive */
745 if (main_rssi > 0 && alt_rssi > 0) {
746 antcomb->total_pkt_count++;
747 antcomb->main_total_rssi += main_rssi;
748 antcomb->alt_total_rssi += alt_rssi;
750 if (main_ant_conf == rx_ant_conf)
751 antcomb->main_recv_cnt++;
753 antcomb->alt_recv_cnt++;
756 if (main_ant_conf == rx_ant_conf) {
757 ANT_STAT_INC(ANT_MAIN, recv_cnt);
758 ANT_LNA_INC(ANT_MAIN, rx_ant_conf);
760 ANT_STAT_INC(ANT_ALT, recv_cnt);
761 ANT_LNA_INC(ANT_ALT, rx_ant_conf);
764 /* Short scan check */
765 short_scan = ath_ant_short_scan_check(antcomb);
767 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
768 rs->rs_moreaggr) && !short_scan)
771 if (antcomb->total_pkt_count) {
772 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
773 antcomb->total_pkt_count);
774 main_rssi_avg = (antcomb->main_total_rssi /
775 antcomb->total_pkt_count);
776 alt_rssi_avg = (antcomb->alt_total_rssi /
777 antcomb->total_pkt_count);
780 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
781 curr_alt_set = div_ant_conf.alt_lna_conf;
782 curr_main_set = div_ant_conf.main_lna_conf;
785 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
786 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
787 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
789 antcomb->alt_good = true;
791 antcomb->alt_good = false;
795 antcomb->scan = true;
796 antcomb->scan_not_start = true;
799 if (!antcomb->scan) {
800 ret = ath_ant_try_switch(&div_ant_conf, alt_ratio,
801 alt_rssi_avg, main_rssi_avg,
802 curr_main_set, curr_alt_set);
807 if (!antcomb->scan &&
808 (alt_rssi_avg < (main_rssi_avg + div_ant_conf.lna1_lna2_delta)))
811 if (!antcomb->scan_not_start) {
812 ath_ant_try_scan(antcomb, &div_ant_conf, curr_alt_set,
813 alt_rssi_avg, main_rssi_avg);
815 if (!antcomb->alt_good) {
816 antcomb->scan_not_start = false;
817 /* Set alt to another LNA */
818 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
819 div_ant_conf.main_lna_conf =
820 ATH_ANT_DIV_COMB_LNA2;
821 div_ant_conf.alt_lna_conf =
822 ATH_ANT_DIV_COMB_LNA1;
823 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
824 div_ant_conf.main_lna_conf =
825 ATH_ANT_DIV_COMB_LNA1;
826 div_ant_conf.alt_lna_conf =
827 ATH_ANT_DIV_COMB_LNA2;
831 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
832 main_rssi_avg, alt_rssi_avg,
834 antcomb->quick_scan_cnt++;
838 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
839 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
840 ath9k_debug_stat_ant(sc, &div_ant_conf, main_rssi_avg, alt_rssi_avg);
842 antcomb->scan_start_time = jiffies;
843 antcomb->total_pkt_count = 0;
844 antcomb->main_total_rssi = 0;
845 antcomb->alt_total_rssi = 0;
846 antcomb->main_recv_cnt = 0;
847 antcomb->alt_recv_cnt = 0;
850 void ath_ant_comb_update(struct ath_softc *sc)
852 struct ath_hw *ah = sc->sc_ah;
853 struct ath_common *common = ath9k_hw_common(ah);
854 struct ath_hw_antcomb_conf div_ant_conf;
857 ath9k_hw_antdiv_comb_conf_get(ah, &div_ant_conf);
860 lna_conf = ATH_ANT_DIV_COMB_LNA1;
862 lna_conf = ATH_ANT_DIV_COMB_LNA2;
864 div_ant_conf.main_lna_conf = lna_conf;
865 div_ant_conf.alt_lna_conf = lna_conf;
867 ath9k_hw_antdiv_comb_conf_set(ah, &div_ant_conf);
869 if (common->antenna_diversity)
870 ath9k_hw_antctrl_shared_chain_lnadiv(ah, true);