]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/ath/ath9k/ar9003_paprd.c
Merge branch 'master' of git://git.infradead.org/users/linville/wireless
[~andy/linux] / drivers / net / wireless / ath / ath9k / ar9003_paprd.c
1 /*
2  * Copyright (c) 2010-2011 Atheros Communications Inc.
3  *
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.
7  *
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.
15  */
16
17 #include "hw.h"
18 #include "ar9003_phy.h"
19
20 void ar9003_paprd_enable(struct ath_hw *ah, bool val)
21 {
22         struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
23         struct ath9k_channel *chan = ah->curchan;
24         struct ar9300_eeprom *eep = &ah->eeprom.ar9300_eep;
25
26         /*
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,
30          * middle, lower.
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
37          */
38
39         if (IS_CHAN_5GHZ(chan)) {
40                 if (chan->channel >= UPPER_5G_SUB_BAND_START) {
41                         if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
42                                                                   & BIT(30))
43                                 val = false;
44                 } else if (chan->channel >= MID_5G_SUB_BAND_START) {
45                         if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
46                                                                   & BIT(29))
47                                 val = false;
48                 } else {
49                         if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
50                                                                   & BIT(28))
51                                 val = false;
52                 }
53         }
54
55         if (val) {
56                 ah->paprd_table_write_done = true;
57
58                 ah->eep_ops->set_txpower(ah, chan,
59                                 ath9k_regd_get_ctl(regulatory, chan),
60                                 chan->chan->max_antenna_gain * 2,
61                                 chan->chan->max_power * 2,
62                                 min((u32) MAX_RATE_POWER,
63                                 (u32) regulatory->power_limit), false);
64         }
65
66         REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B0,
67                       AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
68         if (ah->caps.tx_chainmask & BIT(1))
69                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B1,
70                               AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
71         if (ah->caps.tx_chainmask & BIT(2))
72                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B2,
73                               AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
74 }
75 EXPORT_SYMBOL(ar9003_paprd_enable);
76
77 static int ar9003_get_training_power_2g(struct ath_hw *ah)
78 {
79         struct ath9k_channel *chan = ah->curchan;
80         unsigned int power, scale, delta;
81
82         scale = ar9003_get_paprd_scale_factor(ah, chan);
83         power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE5,
84                                AR_PHY_POWERTX_RATE5_POWERTXHT20_0);
85
86         delta = abs((int) ah->paprd_target_power - (int) power);
87         if (delta > scale)
88                 return -1;
89
90         if (delta < 4)
91                 power -= 4 - delta;
92
93         return power;
94 }
95
96 static int ar9003_get_training_power_5g(struct ath_hw *ah)
97 {
98         struct ath_common *common = ath9k_hw_common(ah);
99         struct ath9k_channel *chan = ah->curchan;
100         unsigned int power, scale, delta;
101
102         scale = ar9003_get_paprd_scale_factor(ah, chan);
103
104         if (IS_CHAN_HT40(chan))
105                 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE8,
106                         AR_PHY_POWERTX_RATE8_POWERTXHT40_5);
107         else
108                 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE6,
109                         AR_PHY_POWERTX_RATE6_POWERTXHT20_5);
110
111         power += scale;
112         delta = abs((int) ah->paprd_target_power - (int) power);
113         if (delta > scale)
114                 return -1;
115
116         switch (get_streams(ah->txchainmask)) {
117         case 1:
118                 delta = 6;
119                 break;
120         case 2:
121                 delta = 4;
122                 break;
123         case 3:
124                 delta = 2;
125                 break;
126         default:
127                 delta = 0;
128                 ath_dbg(common, ATH_DBG_CALIBRATE,
129                 "Invalid tx-chainmask: %u\n", ah->txchainmask);
130         }
131
132         power += delta;
133         return power;
134 }
135
136 static int ar9003_paprd_setup_single_table(struct ath_hw *ah)
137 {
138         struct ath_common *common = ath9k_hw_common(ah);
139         static const u32 ctrl0[3] = {
140                 AR_PHY_PAPRD_CTRL0_B0,
141                 AR_PHY_PAPRD_CTRL0_B1,
142                 AR_PHY_PAPRD_CTRL0_B2
143         };
144         static const u32 ctrl1[3] = {
145                 AR_PHY_PAPRD_CTRL1_B0,
146                 AR_PHY_PAPRD_CTRL1_B1,
147                 AR_PHY_PAPRD_CTRL1_B2
148         };
149         int training_power;
150         int i, val;
151
152         if (IS_CHAN_2GHZ(ah->curchan))
153                 training_power = ar9003_get_training_power_2g(ah);
154         else
155                 training_power = ar9003_get_training_power_5g(ah);
156
157         ath_dbg(common, ATH_DBG_CALIBRATE,
158                 "Training power: %d, Target power: %d\n",
159                 training_power, ah->paprd_target_power);
160
161         if (training_power < 0) {
162                 ath_dbg(common, ATH_DBG_CALIBRATE,
163                         "PAPRD target power delta out of range");
164                 return -ERANGE;
165         }
166         ah->paprd_training_power = training_power;
167
168         REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2AM, AR_PHY_PAPRD_AM2AM_MASK,
169                       ah->paprd_ratemask);
170         REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2PM, AR_PHY_PAPRD_AM2PM_MASK,
171                       ah->paprd_ratemask);
172         REG_RMW_FIELD(ah, AR_PHY_PAPRD_HT40, AR_PHY_PAPRD_HT40_MASK,
173                       ah->paprd_ratemask_ht40);
174
175         for (i = 0; i < ah->caps.max_txchains; i++) {
176                 REG_RMW_FIELD(ah, ctrl0[i],
177                               AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE_MASK, 1);
178                 REG_RMW_FIELD(ah, ctrl1[i],
179                               AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENABLE, 1);
180                 REG_RMW_FIELD(ah, ctrl1[i],
181                               AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENABLE, 1);
182                 REG_RMW_FIELD(ah, ctrl1[i],
183                               AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
184                 REG_RMW_FIELD(ah, ctrl1[i],
185                               AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT_MASK, 181);
186                 REG_RMW_FIELD(ah, ctrl1[i],
187                               AR_PHY_PAPRD_CTRL1_PAPRD_MAG_SCALE_FACT, 361);
188                 REG_RMW_FIELD(ah, ctrl1[i],
189                               AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
190                 REG_RMW_FIELD(ah, ctrl0[i],
191                               AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
192         }
193
194         ar9003_paprd_enable(ah, false);
195
196         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
197                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_SKIP, 0x30);
198         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
199                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_ENABLE, 1);
200         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
201                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_TX_GAIN_FORCE, 1);
202         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
203                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_RX_BB_GAIN_FORCE, 0);
204         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
205                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_IQCORR_ENABLE, 0);
206         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
207                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_AGC2_SETTLING, 28);
208         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
209                       AR_PHY_PAPRD_TRAINER_CNTL1_CF_CF_PAPRD_TRAIN_ENABLE, 1);
210         val = AR_SREV_9480(ah) ? 0x91 : 147;
211         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL2,
212                       AR_PHY_PAPRD_TRAINER_CNTL2_CF_PAPRD_INIT_RX_BB_GAIN, val);
213         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
214                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_FINE_CORR_LEN, 4);
215         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
216                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_COARSE_CORR_LEN, 4);
217         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
218                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_NUM_CORR_STAGES, 7);
219         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
220                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_MIN_LOOPBACK_DEL, 1);
221         if (AR_SREV_9485(ah) || AR_SREV_9480(ah))
222                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
223                               AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
224                               -3);
225         else
226                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
227                               AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
228                               -6);
229         val = AR_SREV_9480(ah) ? -10 : -15;
230         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
231                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_ADC_DESIRED_SIZE,
232                       val);
233         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
234                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_BBTXMIX_DISABLE, 1);
235         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
236                       AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_SAFETY_DELTA, 0);
237         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
238                       AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_MIN_CORR, 400);
239         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
240                       AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_NUM_TRAIN_SAMPLES,
241                       100);
242         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_0_B0,
243                       AR_PHY_PAPRD_PRE_POST_SCALING, 261376);
244         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_1_B0,
245                       AR_PHY_PAPRD_PRE_POST_SCALING, 248079);
246         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_2_B0,
247                       AR_PHY_PAPRD_PRE_POST_SCALING, 233759);
248         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_3_B0,
249                       AR_PHY_PAPRD_PRE_POST_SCALING, 220464);
250         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_4_B0,
251                       AR_PHY_PAPRD_PRE_POST_SCALING, 208194);
252         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_5_B0,
253                       AR_PHY_PAPRD_PRE_POST_SCALING, 196949);
254         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_6_B0,
255                       AR_PHY_PAPRD_PRE_POST_SCALING, 185706);
256         REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_7_B0,
257                       AR_PHY_PAPRD_PRE_POST_SCALING, 175487);
258         return 0;
259 }
260
261 static void ar9003_paprd_get_gain_table(struct ath_hw *ah)
262 {
263         u32 *entry = ah->paprd_gain_table_entries;
264         u8 *index = ah->paprd_gain_table_index;
265         u32 reg = AR_PHY_TXGAIN_TABLE;
266         int i;
267
268         memset(entry, 0, sizeof(ah->paprd_gain_table_entries));
269         memset(index, 0, sizeof(ah->paprd_gain_table_index));
270
271         for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
272                 entry[i] = REG_READ(ah, reg);
273                 index[i] = (entry[i] >> 24) & 0xff;
274                 reg += 4;
275         }
276 }
277
278 static unsigned int ar9003_get_desired_gain(struct ath_hw *ah, int chain,
279                                             int target_power)
280 {
281         int olpc_gain_delta = 0, cl_gain_mod;
282         int alpha_therm, alpha_volt;
283         int therm_cal_value, volt_cal_value;
284         int therm_value, volt_value;
285         int thermal_gain_corr, voltage_gain_corr;
286         int desired_scale, desired_gain = 0;
287         u32 reg_olpc  = 0, reg_cl_gain  = 0;
288
289         REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
290                     AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
291         desired_scale = REG_READ_FIELD(ah, AR_PHY_TPC_12,
292                                        AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
293         alpha_therm = REG_READ_FIELD(ah, AR_PHY_TPC_19,
294                                      AR_PHY_TPC_19_ALPHA_THERM);
295         alpha_volt = REG_READ_FIELD(ah, AR_PHY_TPC_19,
296                                     AR_PHY_TPC_19_ALPHA_VOLT);
297         therm_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
298                                          AR_PHY_TPC_18_THERM_CAL_VALUE);
299         volt_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
300                                         AR_PHY_TPC_18_VOLT_CAL_VALUE);
301         therm_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
302                                      AR_PHY_BB_THERM_ADC_4_LATEST_THERM_VALUE);
303         volt_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
304                                     AR_PHY_BB_THERM_ADC_4_LATEST_VOLT_VALUE);
305
306         switch (chain) {
307         case 0:
308                 reg_olpc = AR_PHY_TPC_11_B0;
309                 reg_cl_gain = AR_PHY_CL_TAB_0;
310                 break;
311         case 1:
312                 reg_olpc = AR_PHY_TPC_11_B1;
313                 reg_cl_gain = AR_PHY_CL_TAB_1;
314                 break;
315         case 2:
316                 reg_olpc = AR_PHY_TPC_11_B2;
317                 reg_cl_gain = AR_PHY_CL_TAB_2;
318                 break;
319         default:
320                 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
321                 "Invalid chainmask: %d\n", chain);
322                 break;
323         }
324
325         olpc_gain_delta = REG_READ_FIELD(ah, reg_olpc,
326                                          AR_PHY_TPC_11_OLPC_GAIN_DELTA);
327         cl_gain_mod = REG_READ_FIELD(ah, reg_cl_gain,
328                                          AR_PHY_CL_TAB_CL_GAIN_MOD);
329
330         if (olpc_gain_delta >= 128)
331                 olpc_gain_delta = olpc_gain_delta - 256;
332
333         thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
334                              (256 / 2)) / 256;
335         voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
336                              (128 / 2)) / 128;
337         desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
338             voltage_gain_corr + desired_scale + cl_gain_mod;
339
340         return desired_gain;
341 }
342
343 static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
344 {
345         int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
346         int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
347         u32 *gain_table_entries = ah->paprd_gain_table_entries;
348
349         selected_gain_entry = gain_table_entries[gain_index];
350         txbb1dbgain = selected_gain_entry & 0x7;
351         txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
352         txmxrgain = (selected_gain_entry >> 5) & 0xf;
353         padrvgnA = (selected_gain_entry >> 9) & 0xf;
354         padrvgnB = (selected_gain_entry >> 13) & 0xf;
355         padrvgnC = (selected_gain_entry >> 17) & 0xf;
356         padrvgnD = (selected_gain_entry >> 21) & 0x3;
357
358         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
359                       AR_PHY_TX_FORCED_GAIN_FORCED_TXBB1DBGAIN, txbb1dbgain);
360         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
361                       AR_PHY_TX_FORCED_GAIN_FORCED_TXBB6DBGAIN, txbb6dbgain);
362         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
363                       AR_PHY_TX_FORCED_GAIN_FORCED_TXMXRGAIN, txmxrgain);
364         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
365                       AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNA, padrvgnA);
366         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
367                       AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNB, padrvgnB);
368         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
369                       AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNC, padrvgnC);
370         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
371                       AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGND, padrvgnD);
372         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
373                       AR_PHY_TX_FORCED_GAIN_FORCED_ENABLE_PAL, 0);
374         REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
375                       AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN, 0);
376         REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
377         REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCE_DAC_GAIN, 0);
378 }
379
380 static inline int find_expn(int num)
381 {
382         return fls(num) - 1;
383 }
384
385 static inline int find_proper_scale(int expn, int N)
386 {
387         return (expn > N) ? expn - 10 : 0;
388 }
389
390 #define NUM_BIN 23
391
392 static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
393 {
394         unsigned int thresh_accum_cnt;
395         int x_est[NUM_BIN + 1], Y[NUM_BIN + 1], theta[NUM_BIN + 1];
396         int PA_in[NUM_BIN + 1];
397         int B1_tmp[NUM_BIN + 1], B2_tmp[NUM_BIN + 1];
398         unsigned int B1_abs_max, B2_abs_max;
399         int max_index, scale_factor;
400         int y_est[NUM_BIN + 1];
401         int x_est_fxp1_nonlin, x_tilde[NUM_BIN + 1];
402         unsigned int x_tilde_abs;
403         int G_fxp, Y_intercept, order_x_by_y, M, I, L, sum_y_sqr, sum_y_quad;
404         int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
405         int Q_scale_B, Q_beta, Q_alpha, alpha, beta, order_1, order_2;
406         int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
407         int y5, y3, tmp;
408         int theta_low_bin = 0;
409         int i;
410
411         /* disregard any bin that contains <= 16 samples */
412         thresh_accum_cnt = 16;
413         scale_factor = 5;
414         max_index = 0;
415         memset(theta, 0, sizeof(theta));
416         memset(x_est, 0, sizeof(x_est));
417         memset(Y, 0, sizeof(Y));
418         memset(y_est, 0, sizeof(y_est));
419         memset(x_tilde, 0, sizeof(x_tilde));
420
421         for (i = 0; i < NUM_BIN; i++) {
422                 s32 accum_cnt, accum_tx, accum_rx, accum_ang;
423
424                 /* number of samples */
425                 accum_cnt = data_L[i] & 0xffff;
426
427                 if (accum_cnt <= thresh_accum_cnt)
428                         continue;
429
430                 /* sum(tx amplitude) */
431                 accum_tx = ((data_L[i] >> 16) & 0xffff) |
432                     ((data_U[i] & 0x7ff) << 16);
433
434                 /* sum(rx amplitude distance to lower bin edge) */
435                 accum_rx = ((data_U[i] >> 11) & 0x1f) |
436                     ((data_L[i + 23] & 0xffff) << 5);
437
438                 /* sum(angles) */
439                 accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
440                     ((data_U[i + 23] & 0x7ff) << 16);
441
442                 accum_tx <<= scale_factor;
443                 accum_rx <<= scale_factor;
444                 x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
445                     scale_factor;
446
447                 Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
448                             scale_factor) +
449                             (1 << scale_factor) * max_index + 16;
450
451                 if (accum_ang >= (1 << 26))
452                         accum_ang -= 1 << 27;
453
454                 theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
455                     accum_cnt;
456
457                 max_index++;
458         }
459
460         /*
461          * Find average theta of first 5 bin and all of those to same value.
462          * Curve is linear at that range.
463          */
464         for (i = 1; i < 6; i++)
465                 theta_low_bin += theta[i];
466
467         theta_low_bin = theta_low_bin / 5;
468         for (i = 1; i < 6; i++)
469                 theta[i] = theta_low_bin;
470
471         /* Set values at origin */
472         theta[0] = theta_low_bin;
473         for (i = 0; i <= max_index; i++)
474                 theta[i] -= theta_low_bin;
475
476         x_est[0] = 0;
477         Y[0] = 0;
478         scale_factor = 8;
479
480         /* low signal gain */
481         if (x_est[6] == x_est[3])
482                 return false;
483
484         G_fxp =
485             (((Y[6] - Y[3]) * 1 << scale_factor) +
486              (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
487
488         /* prevent division by zero */
489         if (G_fxp == 0)
490                 return false;
491
492         Y_intercept =
493             (G_fxp * (x_est[0] - x_est[3]) +
494              (1 << scale_factor)) / (1 << scale_factor) + Y[3];
495
496         for (i = 0; i <= max_index; i++)
497                 y_est[i] = Y[i] - Y_intercept;
498
499         for (i = 0; i <= 3; i++) {
500                 y_est[i] = i * 32;
501                 x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
502         }
503
504         if (y_est[max_index] == 0)
505                 return false;
506
507         x_est_fxp1_nonlin =
508             x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
509                                 G_fxp) / G_fxp;
510
511         order_x_by_y =
512             (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
513
514         if (order_x_by_y == 0)
515                 M = 10;
516         else if (order_x_by_y == 1)
517                 M = 9;
518         else
519                 M = 8;
520
521         I = (max_index > 15) ? 7 : max_index >> 1;
522         L = max_index - I;
523         scale_factor = 8;
524         sum_y_sqr = 0;
525         sum_y_quad = 0;
526         x_tilde_abs = 0;
527
528         for (i = 0; i <= L; i++) {
529                 unsigned int y_sqr;
530                 unsigned int y_quad;
531                 unsigned int tmp_abs;
532
533                 /* prevent division by zero */
534                 if (y_est[i + I] == 0)
535                         return false;
536
537                 x_est_fxp1_nonlin =
538                     x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
539                                     G_fxp) / G_fxp;
540
541                 x_tilde[i] =
542                     (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
543                                                                           I];
544                 x_tilde[i] =
545                     (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
546                 x_tilde[i] =
547                     (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
548                 y_sqr =
549                     (y_est[i + I] * y_est[i + I] +
550                      (scale_factor * scale_factor)) / (scale_factor *
551                                                        scale_factor);
552                 tmp_abs = abs(x_tilde[i]);
553                 if (tmp_abs > x_tilde_abs)
554                         x_tilde_abs = tmp_abs;
555
556                 y_quad = y_sqr * y_sqr;
557                 sum_y_sqr = sum_y_sqr + y_sqr;
558                 sum_y_quad = sum_y_quad + y_quad;
559                 B1_tmp[i] = y_sqr * (L + 1);
560                 B2_tmp[i] = y_sqr;
561         }
562
563         B1_abs_max = 0;
564         B2_abs_max = 0;
565         for (i = 0; i <= L; i++) {
566                 int abs_val;
567
568                 B1_tmp[i] -= sum_y_sqr;
569                 B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
570
571                 abs_val = abs(B1_tmp[i]);
572                 if (abs_val > B1_abs_max)
573                         B1_abs_max = abs_val;
574
575                 abs_val = abs(B2_tmp[i]);
576                 if (abs_val > B2_abs_max)
577                         B2_abs_max = abs_val;
578         }
579
580         Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
581         Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
582         Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
583
584         beta_raw = 0;
585         alpha_raw = 0;
586         for (i = 0; i <= L; i++) {
587                 x_tilde[i] = x_tilde[i] / (1 << Q_x);
588                 B1_tmp[i] = B1_tmp[i] / (1 << Q_B1);
589                 B2_tmp[i] = B2_tmp[i] / (1 << Q_B2);
590                 beta_raw = beta_raw + B1_tmp[i] * x_tilde[i];
591                 alpha_raw = alpha_raw + B2_tmp[i] * x_tilde[i];
592         }
593
594         scale_B =
595             ((sum_y_quad / scale_factor) * (L + 1) -
596              (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
597
598         Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
599         scale_B = scale_B / (1 << Q_scale_B);
600         if (scale_B == 0)
601                 return false;
602         Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
603         Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
604         beta_raw = beta_raw / (1 << Q_beta);
605         alpha_raw = alpha_raw / (1 << Q_alpha);
606         alpha = (alpha_raw << 10) / scale_B;
607         beta = (beta_raw << 10) / scale_B;
608         order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
609         order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
610         order1_5x = order_1 / 5;
611         order2_3x = order_2 / 3;
612         order1_5x_rem = order_1 - 5 * order1_5x;
613         order2_3x_rem = order_2 - 3 * order2_3x;
614
615         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
616                 tmp = i * 32;
617                 y5 = ((beta * tmp) >> 6) >> order1_5x;
618                 y5 = (y5 * tmp) >> order1_5x;
619                 y5 = (y5 * tmp) >> order1_5x;
620                 y5 = (y5 * tmp) >> order1_5x;
621                 y5 = (y5 * tmp) >> order1_5x;
622                 y5 = y5 >> order1_5x_rem;
623                 y3 = (alpha * tmp) >> order2_3x;
624                 y3 = (y3 * tmp) >> order2_3x;
625                 y3 = (y3 * tmp) >> order2_3x;
626                 y3 = y3 >> order2_3x_rem;
627                 PA_in[i] = y5 + y3 + (256 * tmp) / G_fxp;
628
629                 if (i >= 2) {
630                         tmp = PA_in[i] - PA_in[i - 1];
631                         if (tmp < 0)
632                                 PA_in[i] =
633                                     PA_in[i - 1] + (PA_in[i - 1] -
634                                                     PA_in[i - 2]);
635                 }
636
637                 PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
638         }
639
640         beta_raw = 0;
641         alpha_raw = 0;
642
643         for (i = 0; i <= L; i++) {
644                 int theta_tilde =
645                     ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
646                 theta_tilde =
647                     ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
648                 theta_tilde =
649                     ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
650                 beta_raw = beta_raw + B1_tmp[i] * theta_tilde;
651                 alpha_raw = alpha_raw + B2_tmp[i] * theta_tilde;
652         }
653
654         Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
655         Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
656         beta_raw = beta_raw / (1 << Q_beta);
657         alpha_raw = alpha_raw / (1 << Q_alpha);
658
659         alpha = (alpha_raw << 10) / scale_B;
660         beta = (beta_raw << 10) / scale_B;
661         order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
662         order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
663         order1_5x = order_1 / 5;
664         order2_3x = order_2 / 3;
665         order1_5x_rem = order_1 - 5 * order1_5x;
666         order2_3x_rem = order_2 - 3 * order2_3x;
667
668         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
669                 int PA_angle;
670
671                 /* pa_table[4] is calculated from PA_angle for i=5 */
672                 if (i == 4)
673                         continue;
674
675                 tmp = i * 32;
676                 if (beta > 0)
677                         y5 = (((beta * tmp - 64) >> 6) -
678                               (1 << order1_5x)) / (1 << order1_5x);
679                 else
680                         y5 = ((((beta * tmp - 64) >> 6) +
681                                (1 << order1_5x)) / (1 << order1_5x));
682
683                 y5 = (y5 * tmp) / (1 << order1_5x);
684                 y5 = (y5 * tmp) / (1 << order1_5x);
685                 y5 = (y5 * tmp) / (1 << order1_5x);
686                 y5 = (y5 * tmp) / (1 << order1_5x);
687                 y5 = y5 / (1 << order1_5x_rem);
688
689                 if (beta > 0)
690                         y3 = (alpha * tmp -
691                               (1 << order2_3x)) / (1 << order2_3x);
692                 else
693                         y3 = (alpha * tmp +
694                               (1 << order2_3x)) / (1 << order2_3x);
695                 y3 = (y3 * tmp) / (1 << order2_3x);
696                 y3 = (y3 * tmp) / (1 << order2_3x);
697                 y3 = y3 / (1 << order2_3x_rem);
698
699                 if (i < 4) {
700                         PA_angle = 0;
701                 } else {
702                         PA_angle = y5 + y3;
703                         if (PA_angle < -150)
704                                 PA_angle = -150;
705                         else if (PA_angle > 150)
706                                 PA_angle = 150;
707                 }
708
709                 pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
710                 if (i == 5) {
711                         PA_angle = (PA_angle + 2) >> 1;
712                         pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
713                             (PA_angle & 0x7ff);
714                 }
715         }
716
717         *gain = G_fxp;
718         return true;
719 }
720
721 void ar9003_paprd_populate_single_table(struct ath_hw *ah,
722                                         struct ath9k_hw_cal_data *caldata,
723                                         int chain)
724 {
725         u32 *paprd_table_val = caldata->pa_table[chain];
726         u32 small_signal_gain = caldata->small_signal_gain[chain];
727         u32 training_power = ah->paprd_training_power;
728         u32 reg = 0;
729         int i;
730
731         if (chain == 0)
732                 reg = AR_PHY_PAPRD_MEM_TAB_B0;
733         else if (chain == 1)
734                 reg = AR_PHY_PAPRD_MEM_TAB_B1;
735         else if (chain == 2)
736                 reg = AR_PHY_PAPRD_MEM_TAB_B2;
737
738         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
739                 REG_WRITE(ah, reg, paprd_table_val[i]);
740                 reg = reg + 4;
741         }
742
743         if (chain == 0)
744                 reg = AR_PHY_PA_GAIN123_B0;
745         else if (chain == 1)
746                 reg = AR_PHY_PA_GAIN123_B1;
747         else
748                 reg = AR_PHY_PA_GAIN123_B2;
749
750         REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
751
752         REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
753                       AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
754                       training_power);
755
756         if (ah->caps.tx_chainmask & BIT(1))
757                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B1,
758                               AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
759                               training_power);
760
761         if (ah->caps.tx_chainmask & BIT(2))
762                 /* val AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL correct? */
763                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B2,
764                               AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
765                               training_power);
766 }
767 EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
768
769 int ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
770 {
771         unsigned int i, desired_gain, gain_index;
772         unsigned int train_power = ah->paprd_training_power;
773
774         desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
775
776         gain_index = 0;
777         for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
778                 if (ah->paprd_gain_table_index[i] >= desired_gain)
779                         break;
780                 gain_index++;
781         }
782
783         ar9003_tx_force_gain(ah, gain_index);
784
785         REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
786                         AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
787
788         return 0;
789 }
790 EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
791
792 int ar9003_paprd_create_curve(struct ath_hw *ah,
793                               struct ath9k_hw_cal_data *caldata, int chain)
794 {
795         u16 *small_signal_gain = &caldata->small_signal_gain[chain];
796         u32 *pa_table = caldata->pa_table[chain];
797         u32 *data_L, *data_U;
798         int i, status = 0;
799         u32 *buf;
800         u32 reg;
801
802         memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
803
804         buf = kmalloc(2 * 48 * sizeof(u32), GFP_ATOMIC);
805         if (!buf)
806                 return -ENOMEM;
807
808         data_L = &buf[0];
809         data_U = &buf[48];
810
811         REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
812                     AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
813
814         reg = AR_PHY_CHAN_INFO_TAB_0;
815         for (i = 0; i < 48; i++)
816                 data_L[i] = REG_READ(ah, reg + (i << 2));
817
818         REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
819                     AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
820
821         for (i = 0; i < 48; i++)
822                 data_U[i] = REG_READ(ah, reg + (i << 2));
823
824         if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
825                 status = -2;
826
827         REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
828                     AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
829
830         kfree(buf);
831
832         return status;
833 }
834 EXPORT_SYMBOL(ar9003_paprd_create_curve);
835
836 int ar9003_paprd_init_table(struct ath_hw *ah)
837 {
838         int ret;
839
840         ret = ar9003_paprd_setup_single_table(ah);
841         if (ret < 0)
842             return ret;
843
844         ar9003_paprd_get_gain_table(ah);
845         return 0;
846 }
847 EXPORT_SYMBOL(ar9003_paprd_init_table);
848
849 bool ar9003_paprd_is_done(struct ath_hw *ah)
850 {
851         int paprd_done, agc2_pwr;
852         paprd_done = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
853                                 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
854
855         if (paprd_done == 0x1) {
856                 agc2_pwr = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
857                                 AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_AGC2_PWR);
858
859                 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
860                         "AGC2_PWR = 0x%x training done = 0x%x\n",
861                         agc2_pwr, paprd_done);
862         /*
863          * agc2_pwr range should not be less than 'IDEAL_AGC2_PWR_CHANGE'
864          * when the training is completely done, otherwise retraining is
865          * done to make sure the value is in ideal range
866          */
867                 if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
868                         paprd_done = 0;
869         }
870
871         return !!paprd_done;
872 }
873 EXPORT_SYMBOL(ar9003_paprd_is_done);