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