17 #include <linux/export.h>
62 if (ah->
caps.tx_chainmask &
BIT(1))
65 if (ah->
caps.tx_chainmask &
BIT(2))
71 static int ar9003_get_training_power_2g(
struct ath_hw *
ah)
74 unsigned int power, scale,
delta;
90 static int ar9003_get_training_power_5g(
struct ath_hw *ah)
94 unsigned int power, scale,
delta;
122 ath_dbg(common, CALIBRATE,
"Invalid tx-chainmask: %u\n",
130 static int ar9003_paprd_setup_single_table(
struct ath_hw *ah)
132 struct ath_common *common = ath9k_hw_common(ah);
133 static const u32 ctrl0[3] = {
138 static const u32 ctrl1[3] = {
148 training_power = ar9003_get_training_power_2g(ah);
150 training_power = ar9003_get_training_power_5g(ah);
152 ath_dbg(common, CALIBRATE,
"Training power: %d, Target power: %d\n",
155 if (training_power < 0) {
157 "PAPRD target power delta out of range\n");
172 for (i = 0; i < ah->
caps.max_txchains; i++) {
258 static void ar9003_paprd_get_gain_table(
struct ath_hw *ah)
270 index[
i] = (entry[
i] >> 24) & 0xff;
275 static unsigned int ar9003_get_desired_gain(
struct ath_hw *ah,
int chain,
278 int olpc_gain_delta = 0, cl_gain_mod;
279 int alpha_therm, alpha_volt;
280 int therm_cal_value, volt_cal_value;
281 int therm_value, volt_value;
282 int thermal_gain_corr, voltage_gain_corr;
283 int desired_scale, desired_gain = 0;
284 u32 reg_olpc = 0, reg_cl_gain = 0;
317 ath_dbg(ath9k_hw_common(ah), CALIBRATE,
318 "Invalid chainmask: %d\n", chain);
327 if (olpc_gain_delta >= 128)
328 olpc_gain_delta = olpc_gain_delta - 256;
330 thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
332 voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
334 desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
335 voltage_gain_corr + desired_scale + cl_gain_mod;
340 static void ar9003_tx_force_gain(
struct ath_hw *ah,
unsigned int gain_index)
342 int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
343 int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
346 selected_gain_entry = gain_table_entries[gain_index];
347 txbb1dbgain = selected_gain_entry & 0x7;
348 txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
349 txmxrgain = (selected_gain_entry >> 5) & 0xf;
350 padrvgnA = (selected_gain_entry >> 9) & 0xf;
351 padrvgnB = (selected_gain_entry >> 13) & 0xf;
352 padrvgnC = (selected_gain_entry >> 17) & 0xf;
353 padrvgnD = (selected_gain_entry >> 21) & 0x3;
377 static inline int find_expn(
int num)
382 static inline int find_proper_scale(
int expn,
int N)
384 return (expn > N) ? expn - 10 : 0;
389 static bool create_pa_curve(
u32 *data_L,
u32 *data_U,
u32 *pa_table,
u16 *
gain)
391 unsigned int thresh_accum_cnt;
395 unsigned int B1_abs_max, B2_abs_max;
396 int max_index, scale_factor;
398 int x_est_fxp1_nonlin, x_tilde[
NUM_BIN + 1];
399 unsigned int x_tilde_abs;
400 int G_fxp, Y_intercept, order_x_by_y,
M,
I,
L, sum_y_sqr, sum_y_quad;
401 int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
402 int Q_scale_B, Q_beta, Q_alpha,
alpha, beta, order_1, order_2;
403 int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
405 int theta_low_bin = 0;
409 thresh_accum_cnt = 16;
412 memset(theta, 0,
sizeof(theta));
413 memset(x_est, 0,
sizeof(x_est));
415 memset(y_est, 0,
sizeof(y_est));
416 memset(x_tilde, 0,
sizeof(x_tilde));
418 for (i = 0; i <
NUM_BIN; i++) {
419 s32 accum_cnt, accum_tx, accum_rx, accum_ang;
422 accum_cnt = data_L[
i] & 0xffff;
424 if (accum_cnt <= thresh_accum_cnt)
428 accum_tx = ((data_L[
i] >> 16) & 0xffff) |
429 ((data_U[
i] & 0x7ff) << 16);
432 accum_rx = ((data_U[
i] >> 11) & 0x1f) |
433 ((data_L[i + 23] & 0xffff) << 5);
436 accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
437 ((data_U[i + 23] & 0x7ff) << 16);
439 accum_tx <<= scale_factor;
440 accum_rx <<= scale_factor;
441 x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
444 Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
446 (1 << scale_factor) * max_index + 16;
448 if (accum_ang >= (1 << 26))
449 accum_ang -= 1 << 27;
451 theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
461 for (i = 1; i < 6; i++)
462 theta_low_bin += theta[i];
464 theta_low_bin = theta_low_bin / 5;
465 for (i = 1; i < 6; i++)
466 theta[i] = theta_low_bin;
469 theta[0] = theta_low_bin;
470 for (i = 0; i <= max_index; i++)
471 theta[i] -= theta_low_bin;
478 if (x_est[6] == x_est[3])
482 (((Y[6] - Y[3]) * 1 << scale_factor) +
483 (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
490 (G_fxp * (x_est[0] - x_est[3]) +
491 (1 << scale_factor)) / (1 << scale_factor) + Y[3];
493 for (i = 0; i <= max_index; i++)
494 y_est[i] = Y[i] - Y_intercept;
496 for (i = 0; i <= 3; i++) {
498 x_est[
i] = ((y_est[
i] * 1 << scale_factor) + G_fxp) / G_fxp;
501 if (y_est[max_index] == 0)
505 x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
509 (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
511 if (order_x_by_y == 0)
513 else if (order_x_by_y == 1)
518 I = (max_index > 15) ? 7 : max_index >> 1;
525 for (i = 0; i <= L; i++) {
528 unsigned int tmp_abs;
531 if (y_est[i + I] == 0)
535 x_est[i +
I] - ((1 << scale_factor) * y_est[i + I] +
539 (x_est_fxp1_nonlin * (1 <<
M) + y_est[i + I]) / y_est[i +
542 (x_tilde[
i] * (1 <<
M) + y_est[i + I]) / y_est[i +
I];
544 (x_tilde[
i] * (1 <<
M) + y_est[i + I]) / y_est[i +
I];
546 (y_est[i +
I] * y_est[i +
I] +
547 (scale_factor * scale_factor)) / (scale_factor *
549 tmp_abs =
abs(x_tilde[i]);
550 if (tmp_abs > x_tilde_abs)
551 x_tilde_abs = tmp_abs;
553 y_quad = y_sqr * y_sqr;
554 sum_y_sqr = sum_y_sqr + y_sqr;
555 sum_y_quad = sum_y_quad + y_quad;
556 B1_tmp[
i] = y_sqr * (L + 1);
562 for (i = 0; i <= L; i++) {
565 B1_tmp[
i] -= sum_y_sqr;
566 B2_tmp[
i] = sum_y_quad - sum_y_sqr * B2_tmp[
i];
568 abs_val =
abs(B1_tmp[i]);
569 if (abs_val > B1_abs_max)
570 B1_abs_max = abs_val;
572 abs_val =
abs(B2_tmp[i]);
573 if (abs_val > B2_abs_max)
574 B2_abs_max = abs_val;
577 Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
578 Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
579 Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
583 for (i = 0; i <= L; i++) {
584 x_tilde[
i] = x_tilde[
i] / (1 << Q_x);
585 B1_tmp[
i] = B1_tmp[
i] / (1 << Q_B1);
586 B2_tmp[
i] = B2_tmp[
i] / (1 << Q_B2);
587 beta_raw = beta_raw + B1_tmp[
i] * x_tilde[
i];
588 alpha_raw = alpha_raw + B2_tmp[
i] * x_tilde[
i];
592 ((sum_y_quad / scale_factor) * (L + 1) -
593 (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
595 Q_scale_B = find_proper_scale(find_expn(
abs(scale_B)), 10);
596 scale_B = scale_B / (1 << Q_scale_B);
599 Q_beta = find_proper_scale(find_expn(
abs(beta_raw)), 10);
600 Q_alpha = find_proper_scale(find_expn(
abs(alpha_raw)), 10);
601 beta_raw = beta_raw / (1 << Q_beta);
602 alpha_raw = alpha_raw / (1 << Q_alpha);
603 alpha = (alpha_raw << 10) / scale_B;
604 beta = (beta_raw << 10) / scale_B;
605 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
606 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
607 order1_5x = order_1 / 5;
608 order2_3x = order_2 / 3;
609 order1_5x_rem = order_1 - 5 * order1_5x;
610 order2_3x_rem = order_2 - 3 * order2_3x;
614 y5 = ((beta *
tmp) >> 6) >> order1_5x;
615 y5 = (y5 *
tmp) >> order1_5x;
616 y5 = (y5 *
tmp) >> order1_5x;
617 y5 = (y5 *
tmp) >> order1_5x;
618 y5 = (y5 *
tmp) >> order1_5x;
619 y5 = y5 >> order1_5x_rem;
620 y3 = (alpha *
tmp) >> order2_3x;
621 y3 = (y3 *
tmp) >> order2_3x;
622 y3 = (y3 *
tmp) >> order2_3x;
623 y3 = y3 >> order2_3x_rem;
624 PA_in[
i] = y5 + y3 + (256 *
tmp) / G_fxp;
627 tmp = PA_in[
i] - PA_in[i - 1];
630 PA_in[i - 1] + (PA_in[i - 1] -
634 PA_in[
i] = (PA_in[
i] < 1400) ? PA_in[i] : 1400;
640 for (i = 0; i <= L; i++) {
642 ((theta[i +
I] <<
M) + y_est[i + I]) / y_est[i +
I];
644 ((theta_tilde <<
M) + y_est[i + I]) / y_est[i +
I];
646 ((theta_tilde <<
M) + y_est[i + I]) / y_est[i +
I];
647 beta_raw = beta_raw + B1_tmp[
i] * theta_tilde;
648 alpha_raw = alpha_raw + B2_tmp[
i] * theta_tilde;
651 Q_beta = find_proper_scale(find_expn(
abs(beta_raw)), 10);
652 Q_alpha = find_proper_scale(find_expn(
abs(alpha_raw)), 10);
653 beta_raw = beta_raw / (1 << Q_beta);
654 alpha_raw = alpha_raw / (1 << Q_alpha);
656 alpha = (alpha_raw << 10) / scale_B;
657 beta = (beta_raw << 10) / scale_B;
658 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
659 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
660 order1_5x = order_1 / 5;
661 order2_3x = order_2 / 3;
662 order1_5x_rem = order_1 - 5 * order1_5x;
663 order2_3x_rem = order_2 - 3 * order2_3x;
674 y5 = (((beta * tmp - 64) >> 6) -
675 (1 << order1_5x)) / (1 << order1_5x);
677 y5 = ((((beta * tmp - 64) >> 6) +
678 (1 << order1_5x)) / (1 << order1_5x));
680 y5 = (y5 *
tmp) / (1 << order1_5x);
681 y5 = (y5 *
tmp) / (1 << order1_5x);
682 y5 = (y5 *
tmp) / (1 << order1_5x);
683 y5 = (y5 *
tmp) / (1 << order1_5x);
684 y5 = y5 / (1 << order1_5x_rem);
688 (1 << order2_3x)) / (1 << order2_3x);
691 (1 << order2_3x)) / (1 << order2_3x);
692 y3 = (y3 *
tmp) / (1 << order2_3x);
693 y3 = (y3 *
tmp) / (1 << order2_3x);
694 y3 = y3 / (1 << order2_3x_rem);
702 else if (PA_angle > 150)
706 pa_table[
i] = ((PA_in[
i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
708 PA_angle = (PA_angle + 2) >> 1;
709 pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
753 if (ah->
caps.tx_chainmask &
BIT(1))
758 if (ah->
caps.tx_chainmask &
BIT(2))
768 unsigned int i, desired_gain, gain_index;
771 desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
780 ar9003_tx_force_gain(ah, gain_index);
789 static bool ar9003_paprd_retrain_pa_in(
struct ath_hw *ah,
794 int capdiv_offset, quick_drop_offset;
795 int capdiv2g, quick_drop;
811 for (i = 0; i <
NUM_BIN + 1; i++) {
812 if (pa_in[i] == 1400)
817 if (pa_in[23] < 800) {
818 capdiv_offset = (
int)((1000 - pa_in[23] + 75) / 150);
819 capdiv2g += capdiv_offset;
822 if (pa_in[23] < 600) {
828 }
else if (pa_in[23] == 1400) {
829 quick_drop_offset =
min_t(
int, count / 3, 2);
830 quick_drop += quick_drop_offset;
831 capdiv2g += quick_drop_offset / 2;
836 if (quick_drop > 0) {
838 capdiv2g -= quick_drop_offset;
846 if (pa_in[23] < 1000) {
847 capdiv_offset = (1000 - pa_in[23]) / 100;
848 capdiv2g += capdiv_offset;
849 if (capdiv_offset > 3) {
854 capdiv2g += capdiv_offset;
859 }
else if (pa_in[23] == 1400) {
862 capdiv2g -= count / 4;
890 u32 *data_L, *data_U;
908 for (i = 0; i < 48; i++)
909 data_L[i] =
REG_READ(ah, reg + (i << 2));
914 for (i = 0; i < 48; i++)
915 data_U[i] =
REG_READ(ah, reg + (i << 2));
917 if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
920 if (ar9003_paprd_retrain_pa_in(ah, caldata, chain))
936 ret = ar9003_paprd_setup_single_table(ah);
940 ar9003_paprd_get_gain_table(ah);
947 int paprd_done, agc2_pwr;
951 if (paprd_done == 0x1) {
955 ath_dbg(ath9k_hw_common(ah), CALIBRATE,
956 "AGC2_PWR = 0x%x training done = 0x%x\n",
957 agc2_pwr, paprd_done);