2 * Copyright (c) 2010-2011 Atheros Communications 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.
17 #include <linux/export.h>
19 #include "ar9003_phy.h"
21 void ar9003_paprd_enable(struct ath_hw *ah, bool val)
23 struct ath9k_channel *chan = ah->curchan;
24 struct ar9300_eeprom *eep = &ah->eeprom.ar9300_eep;
27 * 3 bits for modalHeader5G.papdRateMaskHt20
28 * is used for sub-band disabling of PAPRD.
29 * 5G band is divided into 3 sub-bands -- upper,
31 * if bit 30 of modalHeader5G.papdRateMaskHt20 is set
32 * -- disable PAPRD for upper band 5GHz
33 * if bit 29 of modalHeader5G.papdRateMaskHt20 is set
34 * -- disable PAPRD for middle band 5GHz
35 * if bit 28 of modalHeader5G.papdRateMaskHt20 is set
36 * -- disable PAPRD for lower band 5GHz
39 if (IS_CHAN_5GHZ(chan)) {
40 if (chan->channel >= UPPER_5G_SUB_BAND_START) {
41 if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
44 } else if (chan->channel >= MID_5G_SUB_BAND_START) {
45 if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
49 if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
56 ah->paprd_table_write_done = true;
57 ath9k_hw_apply_txpower(ah, chan, false);
60 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B0,
61 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
62 if (ah->caps.tx_chainmask & BIT(1))
63 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B1,
64 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
65 if (ah->caps.tx_chainmask & BIT(2))
66 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B2,
67 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
69 EXPORT_SYMBOL(ar9003_paprd_enable);
71 static int ar9003_get_training_power_2g(struct ath_hw *ah)
73 struct ath9k_channel *chan = ah->curchan;
74 unsigned int power, scale, delta;
76 scale = ar9003_get_paprd_scale_factor(ah, chan);
78 if (AR_SREV_9330(ah) || AR_SREV_9340(ah) ||
79 AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
80 power = ah->paprd_target_power + 2;
81 } else if (AR_SREV_9485(ah)) {
84 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE5,
85 AR_PHY_POWERTX_RATE5_POWERTXHT20_0);
87 delta = abs((int) ah->paprd_target_power - (int) power);
98 static int ar9003_get_training_power_5g(struct ath_hw *ah)
100 struct ath_common *common = ath9k_hw_common(ah);
101 struct ath9k_channel *chan = ah->curchan;
102 unsigned int power, scale, delta;
104 scale = ar9003_get_paprd_scale_factor(ah, chan);
106 if (IS_CHAN_HT40(chan))
107 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE8,
108 AR_PHY_POWERTX_RATE8_POWERTXHT40_5);
110 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE6,
111 AR_PHY_POWERTX_RATE6_POWERTXHT20_5);
114 delta = abs((int) ah->paprd_target_power - (int) power);
118 switch (get_streams(ah->txchainmask)) {
130 ath_dbg(common, CALIBRATE, "Invalid tx-chainmask: %u\n",
138 static int ar9003_paprd_setup_single_table(struct ath_hw *ah)
140 struct ath_common *common = ath9k_hw_common(ah);
141 static const u32 ctrl0[3] = {
142 AR_PHY_PAPRD_CTRL0_B0,
143 AR_PHY_PAPRD_CTRL0_B1,
144 AR_PHY_PAPRD_CTRL0_B2
146 static const u32 ctrl1[3] = {
147 AR_PHY_PAPRD_CTRL1_B0,
148 AR_PHY_PAPRD_CTRL1_B1,
149 AR_PHY_PAPRD_CTRL1_B2
153 u32 am2pm_mask = ah->paprd_ratemask;
155 if (IS_CHAN_2GHZ(ah->curchan))
156 training_power = ar9003_get_training_power_2g(ah);
158 training_power = ar9003_get_training_power_5g(ah);
160 ath_dbg(common, CALIBRATE, "Training power: %d, Target power: %d\n",
161 training_power, ah->paprd_target_power);
163 if (training_power < 0) {
164 ath_dbg(common, CALIBRATE,
165 "PAPRD target power delta out of range\n");
168 ah->paprd_training_power = training_power;
170 if (AR_SREV_9330(ah))
173 REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2AM, AR_PHY_PAPRD_AM2AM_MASK,
175 REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2PM, AR_PHY_PAPRD_AM2PM_MASK,
177 REG_RMW_FIELD(ah, AR_PHY_PAPRD_HT40, AR_PHY_PAPRD_HT40_MASK,
178 ah->paprd_ratemask_ht40);
180 ath_dbg(common, CALIBRATE, "PAPRD HT20 mask: 0x%x, HT40 mask: 0x%x\n",
181 ah->paprd_ratemask, ah->paprd_ratemask_ht40);
183 for (i = 0; i < ah->caps.max_txchains; i++) {
184 REG_RMW_FIELD(ah, ctrl0[i],
185 AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE_MASK, 1);
186 REG_RMW_FIELD(ah, ctrl1[i],
187 AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENABLE, 1);
188 REG_RMW_FIELD(ah, ctrl1[i],
189 AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENABLE, 1);
190 REG_RMW_FIELD(ah, ctrl1[i],
191 AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
192 REG_RMW_FIELD(ah, ctrl1[i],
193 AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT_MASK, 181);
194 REG_RMW_FIELD(ah, ctrl1[i],
195 AR_PHY_PAPRD_CTRL1_PAPRD_MAG_SCALE_FACT, 361);
196 REG_RMW_FIELD(ah, ctrl1[i],
197 AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
198 REG_RMW_FIELD(ah, ctrl0[i],
199 AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
202 ar9003_paprd_enable(ah, false);
204 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
205 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_SKIP, 0x30);
206 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
207 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_ENABLE, 1);
208 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
209 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_TX_GAIN_FORCE, 1);
210 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
211 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_RX_BB_GAIN_FORCE, 0);
212 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
213 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_IQCORR_ENABLE, 0);
214 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
215 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_AGC2_SETTLING, 28);
216 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
217 AR_PHY_PAPRD_TRAINER_CNTL1_CF_CF_PAPRD_TRAIN_ENABLE, 1);
219 if (AR_SREV_9485(ah)) {
222 if (IS_CHAN_2GHZ(ah->curchan)) {
223 if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
232 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL2,
233 AR_PHY_PAPRD_TRAINER_CNTL2_CF_PAPRD_INIT_RX_BB_GAIN, val);
234 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
235 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_FINE_CORR_LEN, 4);
236 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
237 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_COARSE_CORR_LEN, 4);
238 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
239 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_NUM_CORR_STAGES, 7);
240 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
241 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_MIN_LOOPBACK_DEL, 1);
243 if (AR_SREV_9485(ah) ||
249 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
250 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP, -3);
252 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
253 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP, -6);
257 if (IS_CHAN_2GHZ(ah->curchan) && !AR_SREV_9462(ah) && !AR_SREV_9565(ah))
260 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
261 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_ADC_DESIRED_SIZE,
263 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
264 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_BBTXMIX_DISABLE, 1);
265 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
266 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_SAFETY_DELTA, 0);
267 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
268 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_MIN_CORR, 400);
269 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
270 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_NUM_TRAIN_SAMPLES,
272 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_0_B0,
273 AR_PHY_PAPRD_PRE_POST_SCALING, 261376);
274 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_1_B0,
275 AR_PHY_PAPRD_PRE_POST_SCALING, 248079);
276 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_2_B0,
277 AR_PHY_PAPRD_PRE_POST_SCALING, 233759);
278 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_3_B0,
279 AR_PHY_PAPRD_PRE_POST_SCALING, 220464);
280 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_4_B0,
281 AR_PHY_PAPRD_PRE_POST_SCALING, 208194);
282 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_5_B0,
283 AR_PHY_PAPRD_PRE_POST_SCALING, 196949);
284 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_6_B0,
285 AR_PHY_PAPRD_PRE_POST_SCALING, 185706);
286 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_7_B0,
287 AR_PHY_PAPRD_PRE_POST_SCALING, 175487);
291 static void ar9003_paprd_get_gain_table(struct ath_hw *ah)
293 u32 *entry = ah->paprd_gain_table_entries;
294 u8 *index = ah->paprd_gain_table_index;
295 u32 reg = AR_PHY_TXGAIN_TABLE;
298 for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
299 entry[i] = REG_READ(ah, reg);
300 index[i] = (entry[i] >> 24) & 0xff;
305 static unsigned int ar9003_get_desired_gain(struct ath_hw *ah, int chain,
308 int olpc_gain_delta = 0, cl_gain_mod;
309 int alpha_therm, alpha_volt;
310 int therm_cal_value, volt_cal_value;
311 int therm_value, volt_value;
312 int thermal_gain_corr, voltage_gain_corr;
313 int desired_scale, desired_gain = 0;
314 u32 reg_olpc = 0, reg_cl_gain = 0;
316 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
317 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
318 desired_scale = REG_READ_FIELD(ah, AR_PHY_TPC_12,
319 AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
320 alpha_therm = REG_READ_FIELD(ah, AR_PHY_TPC_19,
321 AR_PHY_TPC_19_ALPHA_THERM);
322 alpha_volt = REG_READ_FIELD(ah, AR_PHY_TPC_19,
323 AR_PHY_TPC_19_ALPHA_VOLT);
324 therm_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
325 AR_PHY_TPC_18_THERM_CAL_VALUE);
326 volt_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
327 AR_PHY_TPC_18_VOLT_CAL_VALUE);
328 therm_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
329 AR_PHY_BB_THERM_ADC_4_LATEST_THERM_VALUE);
330 volt_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
331 AR_PHY_BB_THERM_ADC_4_LATEST_VOLT_VALUE);
335 reg_olpc = AR_PHY_TPC_11_B0;
336 reg_cl_gain = AR_PHY_CL_TAB_0;
339 reg_olpc = AR_PHY_TPC_11_B1;
340 reg_cl_gain = AR_PHY_CL_TAB_1;
343 reg_olpc = AR_PHY_TPC_11_B2;
344 reg_cl_gain = AR_PHY_CL_TAB_2;
347 ath_dbg(ath9k_hw_common(ah), CALIBRATE,
348 "Invalid chainmask: %d\n", chain);
352 olpc_gain_delta = REG_READ_FIELD(ah, reg_olpc,
353 AR_PHY_TPC_11_OLPC_GAIN_DELTA);
354 cl_gain_mod = REG_READ_FIELD(ah, reg_cl_gain,
355 AR_PHY_CL_TAB_CL_GAIN_MOD);
357 if (olpc_gain_delta >= 128)
358 olpc_gain_delta = olpc_gain_delta - 256;
360 thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
362 voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
364 desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
365 voltage_gain_corr + desired_scale + cl_gain_mod;
370 static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
372 int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
373 int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
374 u32 *gain_table_entries = ah->paprd_gain_table_entries;
376 selected_gain_entry = gain_table_entries[gain_index];
377 txbb1dbgain = selected_gain_entry & 0x7;
378 txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
379 txmxrgain = (selected_gain_entry >> 5) & 0xf;
380 padrvgnA = (selected_gain_entry >> 9) & 0xf;
381 padrvgnB = (selected_gain_entry >> 13) & 0xf;
382 padrvgnC = (selected_gain_entry >> 17) & 0xf;
383 padrvgnD = (selected_gain_entry >> 21) & 0x3;
385 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
386 AR_PHY_TX_FORCED_GAIN_FORCED_TXBB1DBGAIN, txbb1dbgain);
387 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
388 AR_PHY_TX_FORCED_GAIN_FORCED_TXBB6DBGAIN, txbb6dbgain);
389 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
390 AR_PHY_TX_FORCED_GAIN_FORCED_TXMXRGAIN, txmxrgain);
391 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
392 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNA, padrvgnA);
393 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
394 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNB, padrvgnB);
395 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
396 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNC, padrvgnC);
397 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
398 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGND, padrvgnD);
399 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
400 AR_PHY_TX_FORCED_GAIN_FORCED_ENABLE_PAL, 0);
401 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
402 AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN, 0);
403 REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
404 REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCE_DAC_GAIN, 0);
407 static inline int find_expn(int num)
412 static inline int find_proper_scale(int expn, int N)
414 return (expn > N) ? expn - 10 : 0;
419 static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
421 unsigned int thresh_accum_cnt;
422 int x_est[NUM_BIN + 1], Y[NUM_BIN + 1], theta[NUM_BIN + 1];
423 int PA_in[NUM_BIN + 1];
424 int B1_tmp[NUM_BIN + 1], B2_tmp[NUM_BIN + 1];
425 unsigned int B1_abs_max, B2_abs_max;
426 int max_index, scale_factor;
427 int y_est[NUM_BIN + 1];
428 int x_est_fxp1_nonlin, x_tilde[NUM_BIN + 1];
429 unsigned int x_tilde_abs;
430 int G_fxp, Y_intercept, order_x_by_y, M, I, L, sum_y_sqr, sum_y_quad;
431 int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
432 int Q_scale_B, Q_beta, Q_alpha, alpha, beta, order_1, order_2;
433 int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
435 int theta_low_bin = 0;
438 /* disregard any bin that contains <= 16 samples */
439 thresh_accum_cnt = 16;
442 memset(theta, 0, sizeof(theta));
443 memset(x_est, 0, sizeof(x_est));
444 memset(Y, 0, sizeof(Y));
445 memset(y_est, 0, sizeof(y_est));
446 memset(x_tilde, 0, sizeof(x_tilde));
448 for (i = 0; i < NUM_BIN; i++) {
449 s32 accum_cnt, accum_tx, accum_rx, accum_ang;
451 /* number of samples */
452 accum_cnt = data_L[i] & 0xffff;
454 if (accum_cnt <= thresh_accum_cnt)
457 /* sum(tx amplitude) */
458 accum_tx = ((data_L[i] >> 16) & 0xffff) |
459 ((data_U[i] & 0x7ff) << 16);
461 /* sum(rx amplitude distance to lower bin edge) */
462 accum_rx = ((data_U[i] >> 11) & 0x1f) |
463 ((data_L[i + 23] & 0xffff) << 5);
466 accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
467 ((data_U[i + 23] & 0x7ff) << 16);
469 accum_tx <<= scale_factor;
470 accum_rx <<= scale_factor;
471 x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
474 Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
476 (1 << scale_factor) * max_index + 16;
478 if (accum_ang >= (1 << 26))
479 accum_ang -= 1 << 27;
481 theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
488 * Find average theta of first 5 bin and all of those to same value.
489 * Curve is linear at that range.
491 for (i = 1; i < 6; i++)
492 theta_low_bin += theta[i];
494 theta_low_bin = theta_low_bin / 5;
495 for (i = 1; i < 6; i++)
496 theta[i] = theta_low_bin;
498 /* Set values at origin */
499 theta[0] = theta_low_bin;
500 for (i = 0; i <= max_index; i++)
501 theta[i] -= theta_low_bin;
507 /* low signal gain */
508 if (x_est[6] == x_est[3])
512 (((Y[6] - Y[3]) * 1 << scale_factor) +
513 (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
515 /* prevent division by zero */
520 (G_fxp * (x_est[0] - x_est[3]) +
521 (1 << scale_factor)) / (1 << scale_factor) + Y[3];
523 for (i = 0; i <= max_index; i++)
524 y_est[i] = Y[i] - Y_intercept;
526 for (i = 0; i <= 3; i++) {
528 x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
531 if (y_est[max_index] == 0)
535 x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
539 (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
541 if (order_x_by_y == 0)
543 else if (order_x_by_y == 1)
548 I = (max_index > 15) ? 7 : max_index >> 1;
555 for (i = 0; i <= L; i++) {
558 unsigned int tmp_abs;
560 /* prevent division by zero */
561 if (y_est[i + I] == 0)
565 x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
569 (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
572 (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
574 (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
576 (y_est[i + I] * y_est[i + I] +
577 (scale_factor * scale_factor)) / (scale_factor *
579 tmp_abs = abs(x_tilde[i]);
580 if (tmp_abs > x_tilde_abs)
581 x_tilde_abs = tmp_abs;
583 y_quad = y_sqr * y_sqr;
584 sum_y_sqr = sum_y_sqr + y_sqr;
585 sum_y_quad = sum_y_quad + y_quad;
586 B1_tmp[i] = y_sqr * (L + 1);
592 for (i = 0; i <= L; i++) {
595 B1_tmp[i] -= sum_y_sqr;
596 B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
598 abs_val = abs(B1_tmp[i]);
599 if (abs_val > B1_abs_max)
600 B1_abs_max = abs_val;
602 abs_val = abs(B2_tmp[i]);
603 if (abs_val > B2_abs_max)
604 B2_abs_max = abs_val;
607 Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
608 Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
609 Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
613 for (i = 0; i <= L; i++) {
614 x_tilde[i] = x_tilde[i] / (1 << Q_x);
615 B1_tmp[i] = B1_tmp[i] / (1 << Q_B1);
616 B2_tmp[i] = B2_tmp[i] / (1 << Q_B2);
617 beta_raw = beta_raw + B1_tmp[i] * x_tilde[i];
618 alpha_raw = alpha_raw + B2_tmp[i] * x_tilde[i];
622 ((sum_y_quad / scale_factor) * (L + 1) -
623 (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
625 Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
626 scale_B = scale_B / (1 << Q_scale_B);
629 Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
630 Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
631 beta_raw = beta_raw / (1 << Q_beta);
632 alpha_raw = alpha_raw / (1 << Q_alpha);
633 alpha = (alpha_raw << 10) / scale_B;
634 beta = (beta_raw << 10) / scale_B;
635 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
636 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
637 order1_5x = order_1 / 5;
638 order2_3x = order_2 / 3;
639 order1_5x_rem = order_1 - 5 * order1_5x;
640 order2_3x_rem = order_2 - 3 * order2_3x;
642 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
644 y5 = ((beta * tmp) >> 6) >> order1_5x;
645 y5 = (y5 * tmp) >> order1_5x;
646 y5 = (y5 * tmp) >> order1_5x;
647 y5 = (y5 * tmp) >> order1_5x;
648 y5 = (y5 * tmp) >> order1_5x;
649 y5 = y5 >> order1_5x_rem;
650 y3 = (alpha * tmp) >> order2_3x;
651 y3 = (y3 * tmp) >> order2_3x;
652 y3 = (y3 * tmp) >> order2_3x;
653 y3 = y3 >> order2_3x_rem;
654 PA_in[i] = y5 + y3 + (256 * tmp) / G_fxp;
657 tmp = PA_in[i] - PA_in[i - 1];
660 PA_in[i - 1] + (PA_in[i - 1] -
664 PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
670 for (i = 0; i <= L; i++) {
672 ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
674 ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
676 ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
677 beta_raw = beta_raw + B1_tmp[i] * theta_tilde;
678 alpha_raw = alpha_raw + B2_tmp[i] * theta_tilde;
681 Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
682 Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
683 beta_raw = beta_raw / (1 << Q_beta);
684 alpha_raw = alpha_raw / (1 << Q_alpha);
686 alpha = (alpha_raw << 10) / scale_B;
687 beta = (beta_raw << 10) / scale_B;
688 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
689 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
690 order1_5x = order_1 / 5;
691 order2_3x = order_2 / 3;
692 order1_5x_rem = order_1 - 5 * order1_5x;
693 order2_3x_rem = order_2 - 3 * order2_3x;
695 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
698 /* pa_table[4] is calculated from PA_angle for i=5 */
704 y5 = (((beta * tmp - 64) >> 6) -
705 (1 << order1_5x)) / (1 << order1_5x);
707 y5 = ((((beta * tmp - 64) >> 6) +
708 (1 << order1_5x)) / (1 << order1_5x));
710 y5 = (y5 * tmp) / (1 << order1_5x);
711 y5 = (y5 * tmp) / (1 << order1_5x);
712 y5 = (y5 * tmp) / (1 << order1_5x);
713 y5 = (y5 * tmp) / (1 << order1_5x);
714 y5 = y5 / (1 << order1_5x_rem);
718 (1 << order2_3x)) / (1 << order2_3x);
721 (1 << order2_3x)) / (1 << order2_3x);
722 y3 = (y3 * tmp) / (1 << order2_3x);
723 y3 = (y3 * tmp) / (1 << order2_3x);
724 y3 = y3 / (1 << order2_3x_rem);
732 else if (PA_angle > 150)
736 pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
738 PA_angle = (PA_angle + 2) >> 1;
739 pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
748 void ar9003_paprd_populate_single_table(struct ath_hw *ah,
749 struct ath9k_hw_cal_data *caldata,
752 u32 *paprd_table_val = caldata->pa_table[chain];
753 u32 small_signal_gain = caldata->small_signal_gain[chain];
754 u32 training_power = ah->paprd_training_power;
759 reg = AR_PHY_PAPRD_MEM_TAB_B0;
761 reg = AR_PHY_PAPRD_MEM_TAB_B1;
763 reg = AR_PHY_PAPRD_MEM_TAB_B2;
765 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
766 REG_WRITE(ah, reg, paprd_table_val[i]);
771 reg = AR_PHY_PA_GAIN123_B0;
773 reg = AR_PHY_PA_GAIN123_B1;
775 reg = AR_PHY_PA_GAIN123_B2;
777 REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
779 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
780 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
783 if (ah->caps.tx_chainmask & BIT(1))
784 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B1,
785 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
788 if (ah->caps.tx_chainmask & BIT(2))
789 /* val AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL correct? */
790 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B2,
791 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
794 EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
796 void ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
798 unsigned int i, desired_gain, gain_index;
799 unsigned int train_power = ah->paprd_training_power;
801 desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
804 for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
805 if (ah->paprd_gain_table_index[i] >= desired_gain)
810 ar9003_tx_force_gain(ah, gain_index);
812 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
813 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
815 EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
817 static bool ar9003_paprd_retrain_pa_in(struct ath_hw *ah,
818 struct ath9k_hw_cal_data *caldata,
821 u32 *pa_in = caldata->pa_table[chain];
822 int capdiv_offset, quick_drop_offset;
823 int capdiv2g, quick_drop;
827 if (!AR_SREV_9485(ah) && !AR_SREV_9330(ah))
830 capdiv2g = REG_READ_FIELD(ah, AR_PHY_65NM_CH0_TXRF3,
831 AR_PHY_65NM_CH0_TXRF3_CAPDIV2G);
833 quick_drop = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
834 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP);
839 for (i = 0; i < NUM_BIN + 1; i++) {
840 if (pa_in[i] == 1400)
844 if (AR_SREV_9485(ah)) {
845 if (pa_in[23] < 800) {
846 capdiv_offset = (int)((1000 - pa_in[23] + 75) / 150);
847 capdiv2g += capdiv_offset;
850 if (pa_in[23] < 600) {
856 } else if (pa_in[23] == 1400) {
857 quick_drop_offset = min_t(int, count / 3, 2);
858 quick_drop += quick_drop_offset;
859 capdiv2g += quick_drop_offset / 2;
864 if (quick_drop > 0) {
866 capdiv2g -= quick_drop_offset;
873 } else if (AR_SREV_9330(ah)) {
874 if (pa_in[23] < 1000) {
875 capdiv_offset = (1000 - pa_in[23]) / 100;
876 capdiv2g += capdiv_offset;
877 if (capdiv_offset > 3) {
882 capdiv2g += capdiv_offset;
887 } else if (pa_in[23] == 1400) {
890 capdiv2g -= count / 4;
904 REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_TXRF3,
905 AR_PHY_65NM_CH0_TXRF3_CAPDIV2G, capdiv2g);
906 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
907 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
913 int ar9003_paprd_create_curve(struct ath_hw *ah,
914 struct ath9k_hw_cal_data *caldata, int chain)
916 u16 *small_signal_gain = &caldata->small_signal_gain[chain];
917 u32 *pa_table = caldata->pa_table[chain];
918 u32 *data_L, *data_U;
923 memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
925 buf = kmalloc(2 * 48 * sizeof(u32), GFP_KERNEL);
932 REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
933 AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
935 reg = AR_PHY_CHAN_INFO_TAB_0;
936 for (i = 0; i < 48; i++)
937 data_L[i] = REG_READ(ah, reg + (i << 2));
939 REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
940 AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
942 for (i = 0; i < 48; i++)
943 data_U[i] = REG_READ(ah, reg + (i << 2));
945 if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
948 if (ar9003_paprd_retrain_pa_in(ah, caldata, chain))
949 status = -EINPROGRESS;
951 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
952 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
958 EXPORT_SYMBOL(ar9003_paprd_create_curve);
960 int ar9003_paprd_init_table(struct ath_hw *ah)
964 ret = ar9003_paprd_setup_single_table(ah);
968 ar9003_paprd_get_gain_table(ah);
971 EXPORT_SYMBOL(ar9003_paprd_init_table);
973 bool ar9003_paprd_is_done(struct ath_hw *ah)
975 int paprd_done, agc2_pwr;
977 paprd_done = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
978 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
980 if (AR_SREV_9485(ah))
983 if (paprd_done == 0x1) {
984 agc2_pwr = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
985 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_AGC2_PWR);
987 ath_dbg(ath9k_hw_common(ah), CALIBRATE,
988 "AGC2_PWR = 0x%x training done = 0x%x\n",
989 agc2_pwr, paprd_done);
991 * agc2_pwr range should not be less than 'IDEAL_AGC2_PWR_CHANGE'
992 * when the training is completely done, otherwise retraining is
993 * done to make sure the value is in ideal range
995 if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
1001 EXPORT_SYMBOL(ar9003_paprd_is_done);
1003 bool ar9003_is_paprd_enabled(struct ath_hw *ah)
1005 if ((ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->config.enable_paprd)
1010 EXPORT_SYMBOL(ar9003_is_paprd_enabled);