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.
18 #include "ar9003_phy.h"
20 void ar9003_paprd_enable(struct ath_hw *ah, bool val)
22 struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
23 struct ath9k_channel *chan = ah->curchan;
26 ah->paprd_table_write_done = true;
28 ah->eep_ops->set_txpower(ah, chan,
29 ath9k_regd_get_ctl(regulatory, chan),
30 chan->chan->max_antenna_gain * 2,
31 chan->chan->max_power * 2,
32 min((u32) MAX_RATE_POWER,
33 (u32) regulatory->power_limit), false);
36 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B0,
37 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
38 if (ah->caps.tx_chainmask & BIT(1))
39 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B1,
40 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
41 if (ah->caps.tx_chainmask & BIT(2))
42 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B2,
43 AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
45 EXPORT_SYMBOL(ar9003_paprd_enable);
47 static int ar9003_get_training_power_2g(struct ath_hw *ah)
49 struct ath9k_channel *chan = ah->curchan;
50 unsigned int power, scale, delta;
52 scale = ar9003_get_paprd_scale_factor(ah, chan);
53 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE5,
54 AR_PHY_POWERTX_RATE5_POWERTXHT20_0);
56 delta = abs((int) ah->paprd_target_power - (int) power);
66 static int ar9003_get_training_power_5g(struct ath_hw *ah)
68 struct ath_common *common = ath9k_hw_common(ah);
69 struct ath9k_channel *chan = ah->curchan;
70 unsigned int power, scale, delta;
72 scale = ar9003_get_paprd_scale_factor(ah, chan);
74 if (IS_CHAN_HT40(chan))
75 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE8,
76 AR_PHY_POWERTX_RATE8_POWERTXHT40_5);
78 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE6,
79 AR_PHY_POWERTX_RATE6_POWERTXHT20_5);
82 delta = abs((int) ah->paprd_target_power - (int) power);
86 switch (get_streams(common->tx_chainmask)) {
98 ath_dbg(common, ATH_DBG_CALIBRATE,
99 "Invalid tx-chainmask: %u\n", common->tx_chainmask);
106 static int ar9003_paprd_setup_single_table(struct ath_hw *ah)
108 struct ath_common *common = ath9k_hw_common(ah);
109 static const u32 ctrl0[3] = {
110 AR_PHY_PAPRD_CTRL0_B0,
111 AR_PHY_PAPRD_CTRL0_B1,
112 AR_PHY_PAPRD_CTRL0_B2
114 static const u32 ctrl1[3] = {
115 AR_PHY_PAPRD_CTRL1_B0,
116 AR_PHY_PAPRD_CTRL1_B1,
117 AR_PHY_PAPRD_CTRL1_B2
122 if (IS_CHAN_2GHZ(ah->curchan))
123 training_power = ar9003_get_training_power_2g(ah);
125 training_power = ar9003_get_training_power_5g(ah);
127 ath_dbg(common, ATH_DBG_CALIBRATE,
128 "Training power: %d, Target power: %d\n",
129 training_power, ah->paprd_target_power);
131 if (training_power < 0) {
132 ath_dbg(common, ATH_DBG_CALIBRATE,
133 "PAPRD target power delta out of range");
136 ah->paprd_training_power = training_power;
138 REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2AM, AR_PHY_PAPRD_AM2AM_MASK,
140 REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2PM, AR_PHY_PAPRD_AM2PM_MASK,
142 REG_RMW_FIELD(ah, AR_PHY_PAPRD_HT40, AR_PHY_PAPRD_HT40_MASK,
143 ah->paprd_ratemask_ht40);
145 for (i = 0; i < ah->caps.max_txchains; i++) {
146 REG_RMW_FIELD(ah, ctrl0[i],
147 AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE_MASK, 1);
148 REG_RMW_FIELD(ah, ctrl1[i],
149 AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENABLE, 1);
150 REG_RMW_FIELD(ah, ctrl1[i],
151 AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENABLE, 1);
152 REG_RMW_FIELD(ah, ctrl1[i],
153 AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
154 REG_RMW_FIELD(ah, ctrl1[i],
155 AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT_MASK, 181);
156 REG_RMW_FIELD(ah, ctrl1[i],
157 AR_PHY_PAPRD_CTRL1_PAPRD_MAG_SCALE_FACT, 361);
158 REG_RMW_FIELD(ah, ctrl1[i],
159 AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
160 REG_RMW_FIELD(ah, ctrl0[i],
161 AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
164 ar9003_paprd_enable(ah, false);
166 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
167 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_SKIP, 0x30);
168 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
169 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_ENABLE, 1);
170 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
171 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_TX_GAIN_FORCE, 1);
172 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
173 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_RX_BB_GAIN_FORCE, 0);
174 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
175 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_IQCORR_ENABLE, 0);
176 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
177 AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_AGC2_SETTLING, 28);
178 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
179 AR_PHY_PAPRD_TRAINER_CNTL1_CF_CF_PAPRD_TRAIN_ENABLE, 1);
180 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL2,
181 AR_PHY_PAPRD_TRAINER_CNTL2_CF_PAPRD_INIT_RX_BB_GAIN, 147);
182 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
183 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_FINE_CORR_LEN, 4);
184 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
185 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_COARSE_CORR_LEN, 4);
186 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
187 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_NUM_CORR_STAGES, 7);
188 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
189 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_MIN_LOOPBACK_DEL, 1);
190 if (AR_SREV_9485(ah))
191 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
192 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
195 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
196 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
198 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
199 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_ADC_DESIRED_SIZE,
201 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
202 AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_BBTXMIX_DISABLE, 1);
203 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
204 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_SAFETY_DELTA, 0);
205 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
206 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_MIN_CORR, 400);
207 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
208 AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_NUM_TRAIN_SAMPLES,
210 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_0_B0,
211 AR_PHY_PAPRD_PRE_POST_SCALING, 261376);
212 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_1_B0,
213 AR_PHY_PAPRD_PRE_POST_SCALING, 248079);
214 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_2_B0,
215 AR_PHY_PAPRD_PRE_POST_SCALING, 233759);
216 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_3_B0,
217 AR_PHY_PAPRD_PRE_POST_SCALING, 220464);
218 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_4_B0,
219 AR_PHY_PAPRD_PRE_POST_SCALING, 208194);
220 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_5_B0,
221 AR_PHY_PAPRD_PRE_POST_SCALING, 196949);
222 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_6_B0,
223 AR_PHY_PAPRD_PRE_POST_SCALING, 185706);
224 REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_7_B0,
225 AR_PHY_PAPRD_PRE_POST_SCALING, 175487);
229 static void ar9003_paprd_get_gain_table(struct ath_hw *ah)
231 u32 *entry = ah->paprd_gain_table_entries;
232 u8 *index = ah->paprd_gain_table_index;
233 u32 reg = AR_PHY_TXGAIN_TABLE;
236 memset(entry, 0, sizeof(ah->paprd_gain_table_entries));
237 memset(index, 0, sizeof(ah->paprd_gain_table_index));
239 for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
240 entry[i] = REG_READ(ah, reg);
241 index[i] = (entry[i] >> 24) & 0xff;
246 static unsigned int ar9003_get_desired_gain(struct ath_hw *ah, int chain,
249 int olpc_gain_delta = 0, cl_gain_mod;
250 int alpha_therm, alpha_volt;
251 int therm_cal_value, volt_cal_value;
252 int therm_value, volt_value;
253 int thermal_gain_corr, voltage_gain_corr;
254 int desired_scale, desired_gain = 0;
255 u32 reg_olpc = 0, reg_cl_gain = 0;
257 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
258 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
259 desired_scale = REG_READ_FIELD(ah, AR_PHY_TPC_12,
260 AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
261 alpha_therm = REG_READ_FIELD(ah, AR_PHY_TPC_19,
262 AR_PHY_TPC_19_ALPHA_THERM);
263 alpha_volt = REG_READ_FIELD(ah, AR_PHY_TPC_19,
264 AR_PHY_TPC_19_ALPHA_VOLT);
265 therm_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
266 AR_PHY_TPC_18_THERM_CAL_VALUE);
267 volt_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
268 AR_PHY_TPC_18_VOLT_CAL_VALUE);
269 therm_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
270 AR_PHY_BB_THERM_ADC_4_LATEST_THERM_VALUE);
271 volt_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
272 AR_PHY_BB_THERM_ADC_4_LATEST_VOLT_VALUE);
276 reg_olpc = AR_PHY_TPC_11_B0;
277 reg_cl_gain = AR_PHY_CL_TAB_0;
280 reg_olpc = AR_PHY_TPC_11_B1;
281 reg_cl_gain = AR_PHY_CL_TAB_1;
284 reg_olpc = AR_PHY_TPC_11_B2;
285 reg_cl_gain = AR_PHY_CL_TAB_2;
288 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
289 "Invalid chainmask: %d\n", chain);
293 olpc_gain_delta = REG_READ_FIELD(ah, reg_olpc,
294 AR_PHY_TPC_11_OLPC_GAIN_DELTA);
295 cl_gain_mod = REG_READ_FIELD(ah, reg_cl_gain,
296 AR_PHY_CL_TAB_CL_GAIN_MOD);
298 if (olpc_gain_delta >= 128)
299 olpc_gain_delta = olpc_gain_delta - 256;
301 thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
303 voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
305 desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
306 voltage_gain_corr + desired_scale + cl_gain_mod;
311 static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
313 int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
314 int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
315 u32 *gain_table_entries = ah->paprd_gain_table_entries;
317 selected_gain_entry = gain_table_entries[gain_index];
318 txbb1dbgain = selected_gain_entry & 0x7;
319 txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
320 txmxrgain = (selected_gain_entry >> 5) & 0xf;
321 padrvgnA = (selected_gain_entry >> 9) & 0xf;
322 padrvgnB = (selected_gain_entry >> 13) & 0xf;
323 padrvgnC = (selected_gain_entry >> 17) & 0xf;
324 padrvgnD = (selected_gain_entry >> 21) & 0x3;
326 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
327 AR_PHY_TX_FORCED_GAIN_FORCED_TXBB1DBGAIN, txbb1dbgain);
328 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
329 AR_PHY_TX_FORCED_GAIN_FORCED_TXBB6DBGAIN, txbb6dbgain);
330 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
331 AR_PHY_TX_FORCED_GAIN_FORCED_TXMXRGAIN, txmxrgain);
332 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
333 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNA, padrvgnA);
334 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
335 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNB, padrvgnB);
336 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
337 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNC, padrvgnC);
338 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
339 AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGND, padrvgnD);
340 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
341 AR_PHY_TX_FORCED_GAIN_FORCED_ENABLE_PAL, 0);
342 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
343 AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN, 0);
344 REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
345 REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCE_DAC_GAIN, 0);
348 static inline int find_expn(int num)
353 static inline int find_proper_scale(int expn, int N)
355 return (expn > N) ? expn - 10 : 0;
360 static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
362 unsigned int thresh_accum_cnt;
363 int x_est[NUM_BIN + 1], Y[NUM_BIN + 1], theta[NUM_BIN + 1];
364 int PA_in[NUM_BIN + 1];
365 int B1_tmp[NUM_BIN + 1], B2_tmp[NUM_BIN + 1];
366 unsigned int B1_abs_max, B2_abs_max;
367 int max_index, scale_factor;
368 int y_est[NUM_BIN + 1];
369 int x_est_fxp1_nonlin, x_tilde[NUM_BIN + 1];
370 unsigned int x_tilde_abs;
371 int G_fxp, Y_intercept, order_x_by_y, M, I, L, sum_y_sqr, sum_y_quad;
372 int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
373 int Q_scale_B, Q_beta, Q_alpha, alpha, beta, order_1, order_2;
374 int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
376 int theta_low_bin = 0;
379 /* disregard any bin that contains <= 16 samples */
380 thresh_accum_cnt = 16;
383 memset(theta, 0, sizeof(theta));
384 memset(x_est, 0, sizeof(x_est));
385 memset(Y, 0, sizeof(Y));
386 memset(y_est, 0, sizeof(y_est));
387 memset(x_tilde, 0, sizeof(x_tilde));
389 for (i = 0; i < NUM_BIN; i++) {
390 s32 accum_cnt, accum_tx, accum_rx, accum_ang;
392 /* number of samples */
393 accum_cnt = data_L[i] & 0xffff;
395 if (accum_cnt <= thresh_accum_cnt)
398 /* sum(tx amplitude) */
399 accum_tx = ((data_L[i] >> 16) & 0xffff) |
400 ((data_U[i] & 0x7ff) << 16);
402 /* sum(rx amplitude distance to lower bin edge) */
403 accum_rx = ((data_U[i] >> 11) & 0x1f) |
404 ((data_L[i + 23] & 0xffff) << 5);
407 accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
408 ((data_U[i + 23] & 0x7ff) << 16);
410 accum_tx <<= scale_factor;
411 accum_rx <<= scale_factor;
412 x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
415 Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
417 (1 << scale_factor) * max_index + 16;
419 if (accum_ang >= (1 << 26))
420 accum_ang -= 1 << 27;
422 theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
429 * Find average theta of first 5 bin and all of those to same value.
430 * Curve is linear at that range.
432 for (i = 1; i < 6; i++)
433 theta_low_bin += theta[i];
435 theta_low_bin = theta_low_bin / 5;
436 for (i = 1; i < 6; i++)
437 theta[i] = theta_low_bin;
439 /* Set values at origin */
440 theta[0] = theta_low_bin;
441 for (i = 0; i <= max_index; i++)
442 theta[i] -= theta_low_bin;
448 /* low signal gain */
449 if (x_est[6] == x_est[3])
453 (((Y[6] - Y[3]) * 1 << scale_factor) +
454 (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
456 /* prevent division by zero */
461 (G_fxp * (x_est[0] - x_est[3]) +
462 (1 << scale_factor)) / (1 << scale_factor) + Y[3];
464 for (i = 0; i <= max_index; i++)
465 y_est[i] = Y[i] - Y_intercept;
467 for (i = 0; i <= 3; i++) {
469 x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
472 if (y_est[max_index] == 0)
476 x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
480 (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
482 if (order_x_by_y == 0)
484 else if (order_x_by_y == 1)
489 I = (max_index > 15) ? 7 : max_index >> 1;
496 for (i = 0; i <= L; i++) {
499 unsigned int tmp_abs;
501 /* prevent division by zero */
502 if (y_est[i + I] == 0)
506 x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
510 (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
513 (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
515 (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
517 (y_est[i + I] * y_est[i + I] +
518 (scale_factor * scale_factor)) / (scale_factor *
520 tmp_abs = abs(x_tilde[i]);
521 if (tmp_abs > x_tilde_abs)
522 x_tilde_abs = tmp_abs;
524 y_quad = y_sqr * y_sqr;
525 sum_y_sqr = sum_y_sqr + y_sqr;
526 sum_y_quad = sum_y_quad + y_quad;
527 B1_tmp[i] = y_sqr * (L + 1);
533 for (i = 0; i <= L; i++) {
536 B1_tmp[i] -= sum_y_sqr;
537 B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
539 abs_val = abs(B1_tmp[i]);
540 if (abs_val > B1_abs_max)
541 B1_abs_max = abs_val;
543 abs_val = abs(B2_tmp[i]);
544 if (abs_val > B2_abs_max)
545 B2_abs_max = abs_val;
548 Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
549 Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
550 Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
554 for (i = 0; i <= L; i++) {
555 x_tilde[i] = x_tilde[i] / (1 << Q_x);
556 B1_tmp[i] = B1_tmp[i] / (1 << Q_B1);
557 B2_tmp[i] = B2_tmp[i] / (1 << Q_B2);
558 beta_raw = beta_raw + B1_tmp[i] * x_tilde[i];
559 alpha_raw = alpha_raw + B2_tmp[i] * x_tilde[i];
563 ((sum_y_quad / scale_factor) * (L + 1) -
564 (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
566 Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
567 scale_B = scale_B / (1 << Q_scale_B);
570 Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
571 Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
572 beta_raw = beta_raw / (1 << Q_beta);
573 alpha_raw = alpha_raw / (1 << Q_alpha);
574 alpha = (alpha_raw << 10) / scale_B;
575 beta = (beta_raw << 10) / scale_B;
576 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
577 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
578 order1_5x = order_1 / 5;
579 order2_3x = order_2 / 3;
580 order1_5x_rem = order_1 - 5 * order1_5x;
581 order2_3x_rem = order_2 - 3 * order2_3x;
583 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
585 y5 = ((beta * tmp) >> 6) >> order1_5x;
586 y5 = (y5 * tmp) >> order1_5x;
587 y5 = (y5 * tmp) >> order1_5x;
588 y5 = (y5 * tmp) >> order1_5x;
589 y5 = (y5 * tmp) >> order1_5x;
590 y5 = y5 >> order1_5x_rem;
591 y3 = (alpha * tmp) >> order2_3x;
592 y3 = (y3 * tmp) >> order2_3x;
593 y3 = (y3 * tmp) >> order2_3x;
594 y3 = y3 >> order2_3x_rem;
595 PA_in[i] = y5 + y3 + (256 * tmp) / G_fxp;
598 tmp = PA_in[i] - PA_in[i - 1];
601 PA_in[i - 1] + (PA_in[i - 1] -
605 PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
611 for (i = 0; i <= L; i++) {
613 ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
615 ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
617 ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
618 beta_raw = beta_raw + B1_tmp[i] * theta_tilde;
619 alpha_raw = alpha_raw + B2_tmp[i] * theta_tilde;
622 Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
623 Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
624 beta_raw = beta_raw / (1 << Q_beta);
625 alpha_raw = alpha_raw / (1 << Q_alpha);
627 alpha = (alpha_raw << 10) / scale_B;
628 beta = (beta_raw << 10) / scale_B;
629 order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
630 order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
631 order1_5x = order_1 / 5;
632 order2_3x = order_2 / 3;
633 order1_5x_rem = order_1 - 5 * order1_5x;
634 order2_3x_rem = order_2 - 3 * order2_3x;
636 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
639 /* pa_table[4] is calculated from PA_angle for i=5 */
645 y5 = (((beta * tmp - 64) >> 6) -
646 (1 << order1_5x)) / (1 << order1_5x);
648 y5 = ((((beta * tmp - 64) >> 6) +
649 (1 << order1_5x)) / (1 << order1_5x));
651 y5 = (y5 * tmp) / (1 << order1_5x);
652 y5 = (y5 * tmp) / (1 << order1_5x);
653 y5 = (y5 * tmp) / (1 << order1_5x);
654 y5 = (y5 * tmp) / (1 << order1_5x);
655 y5 = y5 / (1 << order1_5x_rem);
659 (1 << order2_3x)) / (1 << order2_3x);
662 (1 << order2_3x)) / (1 << order2_3x);
663 y3 = (y3 * tmp) / (1 << order2_3x);
664 y3 = (y3 * tmp) / (1 << order2_3x);
665 y3 = y3 / (1 << order2_3x_rem);
673 else if (PA_angle > 150)
677 pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
679 PA_angle = (PA_angle + 2) >> 1;
680 pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
689 void ar9003_paprd_populate_single_table(struct ath_hw *ah,
690 struct ath9k_hw_cal_data *caldata,
693 u32 *paprd_table_val = caldata->pa_table[chain];
694 u32 small_signal_gain = caldata->small_signal_gain[chain];
695 u32 training_power = ah->paprd_training_power;
700 reg = AR_PHY_PAPRD_MEM_TAB_B0;
702 reg = AR_PHY_PAPRD_MEM_TAB_B1;
704 reg = AR_PHY_PAPRD_MEM_TAB_B2;
706 for (i = 0; i < PAPRD_TABLE_SZ; i++) {
707 REG_WRITE(ah, reg, paprd_table_val[i]);
712 reg = AR_PHY_PA_GAIN123_B0;
714 reg = AR_PHY_PA_GAIN123_B1;
716 reg = AR_PHY_PA_GAIN123_B2;
718 REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
720 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
721 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
724 if (ah->caps.tx_chainmask & BIT(1))
725 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B1,
726 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
729 if (ah->caps.tx_chainmask & BIT(2))
730 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B2,
731 AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
734 EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
736 int ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
738 unsigned int i, desired_gain, gain_index;
739 unsigned int train_power = ah->paprd_training_power;
741 desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
744 for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
745 if (ah->paprd_gain_table_index[i] >= desired_gain)
750 ar9003_tx_force_gain(ah, gain_index);
752 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
753 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
757 EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
759 int ar9003_paprd_create_curve(struct ath_hw *ah,
760 struct ath9k_hw_cal_data *caldata, int chain)
762 u16 *small_signal_gain = &caldata->small_signal_gain[chain];
763 u32 *pa_table = caldata->pa_table[chain];
764 u32 *data_L, *data_U;
769 memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
771 buf = kmalloc(2 * 48 * sizeof(u32), GFP_ATOMIC);
778 REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
779 AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
781 reg = AR_PHY_CHAN_INFO_TAB_0;
782 for (i = 0; i < 48; i++)
783 data_L[i] = REG_READ(ah, reg + (i << 2));
785 REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
786 AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
788 for (i = 0; i < 48; i++)
789 data_U[i] = REG_READ(ah, reg + (i << 2));
791 if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
794 REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
795 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
801 EXPORT_SYMBOL(ar9003_paprd_create_curve);
803 int ar9003_paprd_init_table(struct ath_hw *ah)
807 ret = ar9003_paprd_setup_single_table(ah);
811 ar9003_paprd_get_gain_table(ah);
814 EXPORT_SYMBOL(ar9003_paprd_init_table);
816 bool ar9003_paprd_is_done(struct ath_hw *ah)
818 int paprd_done, agc2_pwr;
819 paprd_done = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
820 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
822 if (paprd_done == 0x1) {
823 agc2_pwr = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
824 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_AGC2_PWR);
826 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
827 "AGC2_PWR = 0x%x training done = 0x%x\n",
828 agc2_pwr, paprd_done);
830 * agc2_pwr range should not be less than 'IDEAL_AGC2_PWR_CHANGE'
831 * when the training is completely done, otherwise retraining is
832 * done to make sure the value is in ideal range
834 if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
840 EXPORT_SYMBOL(ar9003_paprd_is_done);