de1972307b0a54471fe8198234ad5efbe8a2d69c
[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 "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
25         if (val) {
26                 ah->paprd_table_write_done = true;
27
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);
34         }
35
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);
44 }
45 EXPORT_SYMBOL(ar9003_paprd_enable);
46
47 static int ar9003_get_training_power_2g(struct ath_hw *ah)
48 {
49         struct ath9k_channel *chan = ah->curchan;
50         unsigned int power, scale, delta;
51
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);
55
56         delta = abs((int) ah->paprd_target_power - (int) power);
57         if (delta > scale)
58                 return -1;
59
60         if (delta < 4)
61                 power -= 4 - delta;
62
63         return power;
64 }
65
66 static int ar9003_get_training_power_5g(struct ath_hw *ah)
67 {
68         struct ath_common *common = ath9k_hw_common(ah);
69         struct ath9k_channel *chan = ah->curchan;
70         unsigned int power, scale, delta;
71
72         scale = ar9003_get_paprd_scale_factor(ah, chan);
73
74         if (IS_CHAN_HT40(chan))
75                 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE8,
76                         AR_PHY_POWERTX_RATE8_POWERTXHT40_5);
77         else
78                 power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE6,
79                         AR_PHY_POWERTX_RATE6_POWERTXHT20_5);
80
81         power += scale;
82         delta = abs((int) ah->paprd_target_power - (int) power);
83         if (delta > scale)
84                 return -1;
85
86         switch (get_streams(common->tx_chainmask)) {
87         case 1:
88                 delta = 6;
89                 break;
90         case 2:
91                 delta = 4;
92                 break;
93         case 3:
94                 delta = 2;
95                 break;
96         default:
97                 delta = 0;
98                 ath_dbg(common, ATH_DBG_CALIBRATE,
99                 "Invalid tx-chainmask: %u\n", common->tx_chainmask);
100         }
101
102         power += delta;
103         return power;
104 }
105
106 static int ar9003_paprd_setup_single_table(struct ath_hw *ah)
107 {
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
113         };
114         static const u32 ctrl1[3] = {
115                 AR_PHY_PAPRD_CTRL1_B0,
116                 AR_PHY_PAPRD_CTRL1_B1,
117                 AR_PHY_PAPRD_CTRL1_B2
118         };
119         int training_power;
120         int i;
121
122         if (IS_CHAN_2GHZ(ah->curchan))
123                 training_power = ar9003_get_training_power_2g(ah);
124         else
125                 training_power = ar9003_get_training_power_5g(ah);
126
127         ath_dbg(common, ATH_DBG_CALIBRATE,
128                 "Training power: %d, Target power: %d\n",
129                 training_power, ah->paprd_target_power);
130
131         if (training_power < 0) {
132                 ath_dbg(common, ATH_DBG_CALIBRATE,
133                         "PAPRD target power delta out of range");
134                 return -ERANGE;
135         }
136         ah->paprd_training_power = training_power;
137
138         REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2AM, AR_PHY_PAPRD_AM2AM_MASK,
139                       ah->paprd_ratemask);
140         REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2PM, AR_PHY_PAPRD_AM2PM_MASK,
141                       ah->paprd_ratemask);
142         REG_RMW_FIELD(ah, AR_PHY_PAPRD_HT40, AR_PHY_PAPRD_HT40_MASK,
143                       ah->paprd_ratemask_ht40);
144
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);
162         }
163
164         ar9003_paprd_enable(ah, false);
165
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,
193                               -3);
194         else
195                 REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
196                               AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
197                               -6);
198         REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
199                       AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_ADC_DESIRED_SIZE,
200                       -15);
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,
209                       100);
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);
226         return 0;
227 }
228
229 static void ar9003_paprd_get_gain_table(struct ath_hw *ah)
230 {
231         u32 *entry = ah->paprd_gain_table_entries;
232         u8 *index = ah->paprd_gain_table_index;
233         u32 reg = AR_PHY_TXGAIN_TABLE;
234         int i;
235
236         memset(entry, 0, sizeof(ah->paprd_gain_table_entries));
237         memset(index, 0, sizeof(ah->paprd_gain_table_index));
238
239         for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
240                 entry[i] = REG_READ(ah, reg);
241                 index[i] = (entry[i] >> 24) & 0xff;
242                 reg += 4;
243         }
244 }
245
246 static unsigned int ar9003_get_desired_gain(struct ath_hw *ah, int chain,
247                                             int target_power)
248 {
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;
256
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);
273
274         switch (chain) {
275         case 0:
276                 reg_olpc = AR_PHY_TPC_11_B0;
277                 reg_cl_gain = AR_PHY_CL_TAB_0;
278                 break;
279         case 1:
280                 reg_olpc = AR_PHY_TPC_11_B1;
281                 reg_cl_gain = AR_PHY_CL_TAB_1;
282                 break;
283         case 2:
284                 reg_olpc = AR_PHY_TPC_11_B2;
285                 reg_cl_gain = AR_PHY_CL_TAB_2;
286                 break;
287         default:
288                 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
289                 "Invalid chainmask: %d\n", chain);
290                 break;
291         }
292
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);
297
298         if (olpc_gain_delta >= 128)
299                 olpc_gain_delta = olpc_gain_delta - 256;
300
301         thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
302                              (256 / 2)) / 256;
303         voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
304                              (128 / 2)) / 128;
305         desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
306             voltage_gain_corr + desired_scale + cl_gain_mod;
307
308         return desired_gain;
309 }
310
311 static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
312 {
313         int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
314         int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
315         u32 *gain_table_entries = ah->paprd_gain_table_entries;
316
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;
325
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);
346 }
347
348 static inline int find_expn(int num)
349 {
350         return fls(num) - 1;
351 }
352
353 static inline int find_proper_scale(int expn, int N)
354 {
355         return (expn > N) ? expn - 10 : 0;
356 }
357
358 #define NUM_BIN 23
359
360 static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
361 {
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;
375         int y5, y3, tmp;
376         int theta_low_bin = 0;
377         int i;
378
379         /* disregard any bin that contains <= 16 samples */
380         thresh_accum_cnt = 16;
381         scale_factor = 5;
382         max_index = 0;
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));
388
389         for (i = 0; i < NUM_BIN; i++) {
390                 s32 accum_cnt, accum_tx, accum_rx, accum_ang;
391
392                 /* number of samples */
393                 accum_cnt = data_L[i] & 0xffff;
394
395                 if (accum_cnt <= thresh_accum_cnt)
396                         continue;
397
398                 /* sum(tx amplitude) */
399                 accum_tx = ((data_L[i] >> 16) & 0xffff) |
400                     ((data_U[i] & 0x7ff) << 16);
401
402                 /* sum(rx amplitude distance to lower bin edge) */
403                 accum_rx = ((data_U[i] >> 11) & 0x1f) |
404                     ((data_L[i + 23] & 0xffff) << 5);
405
406                 /* sum(angles) */
407                 accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
408                     ((data_U[i + 23] & 0x7ff) << 16);
409
410                 accum_tx <<= scale_factor;
411                 accum_rx <<= scale_factor;
412                 x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
413                     scale_factor;
414
415                 Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
416                             scale_factor) +
417                             (1 << scale_factor) * max_index + 16;
418
419                 if (accum_ang >= (1 << 26))
420                         accum_ang -= 1 << 27;
421
422                 theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
423                     accum_cnt;
424
425                 max_index++;
426         }
427
428         /*
429          * Find average theta of first 5 bin and all of those to same value.
430          * Curve is linear at that range.
431          */
432         for (i = 1; i < 6; i++)
433                 theta_low_bin += theta[i];
434
435         theta_low_bin = theta_low_bin / 5;
436         for (i = 1; i < 6; i++)
437                 theta[i] = theta_low_bin;
438
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;
443
444         x_est[0] = 0;
445         Y[0] = 0;
446         scale_factor = 8;
447
448         /* low signal gain */
449         if (x_est[6] == x_est[3])
450                 return false;
451
452         G_fxp =
453             (((Y[6] - Y[3]) * 1 << scale_factor) +
454              (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
455
456         /* prevent division by zero */
457         if (G_fxp == 0)
458                 return false;
459
460         Y_intercept =
461             (G_fxp * (x_est[0] - x_est[3]) +
462              (1 << scale_factor)) / (1 << scale_factor) + Y[3];
463
464         for (i = 0; i <= max_index; i++)
465                 y_est[i] = Y[i] - Y_intercept;
466
467         for (i = 0; i <= 3; i++) {
468                 y_est[i] = i * 32;
469                 x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
470         }
471
472         if (y_est[max_index] == 0)
473                 return false;
474
475         x_est_fxp1_nonlin =
476             x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
477                                 G_fxp) / G_fxp;
478
479         order_x_by_y =
480             (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
481
482         if (order_x_by_y == 0)
483                 M = 10;
484         else if (order_x_by_y == 1)
485                 M = 9;
486         else
487                 M = 8;
488
489         I = (max_index > 15) ? 7 : max_index >> 1;
490         L = max_index - I;
491         scale_factor = 8;
492         sum_y_sqr = 0;
493         sum_y_quad = 0;
494         x_tilde_abs = 0;
495
496         for (i = 0; i <= L; i++) {
497                 unsigned int y_sqr;
498                 unsigned int y_quad;
499                 unsigned int tmp_abs;
500
501                 /* prevent division by zero */
502                 if (y_est[i + I] == 0)
503                         return false;
504
505                 x_est_fxp1_nonlin =
506                     x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
507                                     G_fxp) / G_fxp;
508
509                 x_tilde[i] =
510                     (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
511                                                                           I];
512                 x_tilde[i] =
513                     (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
514                 x_tilde[i] =
515                     (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
516                 y_sqr =
517                     (y_est[i + I] * y_est[i + I] +
518                      (scale_factor * scale_factor)) / (scale_factor *
519                                                        scale_factor);
520                 tmp_abs = abs(x_tilde[i]);
521                 if (tmp_abs > x_tilde_abs)
522                         x_tilde_abs = tmp_abs;
523
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);
528                 B2_tmp[i] = y_sqr;
529         }
530
531         B1_abs_max = 0;
532         B2_abs_max = 0;
533         for (i = 0; i <= L; i++) {
534                 int abs_val;
535
536                 B1_tmp[i] -= sum_y_sqr;
537                 B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
538
539                 abs_val = abs(B1_tmp[i]);
540                 if (abs_val > B1_abs_max)
541                         B1_abs_max = abs_val;
542
543                 abs_val = abs(B2_tmp[i]);
544                 if (abs_val > B2_abs_max)
545                         B2_abs_max = abs_val;
546         }
547
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);
551
552         beta_raw = 0;
553         alpha_raw = 0;
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];
560         }
561
562         scale_B =
563             ((sum_y_quad / scale_factor) * (L + 1) -
564              (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
565
566         Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
567         scale_B = scale_B / (1 << Q_scale_B);
568         if (scale_B == 0)
569                 return false;
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;
582
583         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
584                 tmp = i * 32;
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;
596
597                 if (i >= 2) {
598                         tmp = PA_in[i] - PA_in[i - 1];
599                         if (tmp < 0)
600                                 PA_in[i] =
601                                     PA_in[i - 1] + (PA_in[i - 1] -
602                                                     PA_in[i - 2]);
603                 }
604
605                 PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
606         }
607
608         beta_raw = 0;
609         alpha_raw = 0;
610
611         for (i = 0; i <= L; i++) {
612                 int theta_tilde =
613                     ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
614                 theta_tilde =
615                     ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
616                 theta_tilde =
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;
620         }
621
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);
626
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;
635
636         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
637                 int PA_angle;
638
639                 /* pa_table[4] is calculated from PA_angle for i=5 */
640                 if (i == 4)
641                         continue;
642
643                 tmp = i * 32;
644                 if (beta > 0)
645                         y5 = (((beta * tmp - 64) >> 6) -
646                               (1 << order1_5x)) / (1 << order1_5x);
647                 else
648                         y5 = ((((beta * tmp - 64) >> 6) +
649                                (1 << order1_5x)) / (1 << order1_5x));
650
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);
656
657                 if (beta > 0)
658                         y3 = (alpha * tmp -
659                               (1 << order2_3x)) / (1 << order2_3x);
660                 else
661                         y3 = (alpha * tmp +
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);
666
667                 if (i < 4) {
668                         PA_angle = 0;
669                 } else {
670                         PA_angle = y5 + y3;
671                         if (PA_angle < -150)
672                                 PA_angle = -150;
673                         else if (PA_angle > 150)
674                                 PA_angle = 150;
675                 }
676
677                 pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
678                 if (i == 5) {
679                         PA_angle = (PA_angle + 2) >> 1;
680                         pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
681                             (PA_angle & 0x7ff);
682                 }
683         }
684
685         *gain = G_fxp;
686         return true;
687 }
688
689 void ar9003_paprd_populate_single_table(struct ath_hw *ah,
690                                         struct ath9k_hw_cal_data *caldata,
691                                         int chain)
692 {
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;
696         u32 reg = 0;
697         int i;
698
699         if (chain == 0)
700                 reg = AR_PHY_PAPRD_MEM_TAB_B0;
701         else if (chain == 1)
702                 reg = AR_PHY_PAPRD_MEM_TAB_B1;
703         else if (chain == 2)
704                 reg = AR_PHY_PAPRD_MEM_TAB_B2;
705
706         for (i = 0; i < PAPRD_TABLE_SZ; i++) {
707                 REG_WRITE(ah, reg, paprd_table_val[i]);
708                 reg = reg + 4;
709         }
710
711         if (chain == 0)
712                 reg = AR_PHY_PA_GAIN123_B0;
713         else if (chain == 1)
714                 reg = AR_PHY_PA_GAIN123_B1;
715         else
716                 reg = AR_PHY_PA_GAIN123_B2;
717
718         REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
719
720         REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
721                       AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
722                       training_power);
723
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,
727                               training_power);
728
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,
732                               training_power);
733 }
734 EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
735
736 int ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
737 {
738         unsigned int i, desired_gain, gain_index;
739         unsigned int train_power = ah->paprd_training_power;
740
741         desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
742
743         gain_index = 0;
744         for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
745                 if (ah->paprd_gain_table_index[i] >= desired_gain)
746                         break;
747                 gain_index++;
748         }
749
750         ar9003_tx_force_gain(ah, gain_index);
751
752         REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
753                         AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
754
755         return 0;
756 }
757 EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
758
759 int ar9003_paprd_create_curve(struct ath_hw *ah,
760                               struct ath9k_hw_cal_data *caldata, int chain)
761 {
762         u16 *small_signal_gain = &caldata->small_signal_gain[chain];
763         u32 *pa_table = caldata->pa_table[chain];
764         u32 *data_L, *data_U;
765         int i, status = 0;
766         u32 *buf;
767         u32 reg;
768
769         memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
770
771         buf = kmalloc(2 * 48 * sizeof(u32), GFP_ATOMIC);
772         if (!buf)
773                 return -ENOMEM;
774
775         data_L = &buf[0];
776         data_U = &buf[48];
777
778         REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
779                     AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
780
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));
784
785         REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
786                     AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
787
788         for (i = 0; i < 48; i++)
789                 data_U[i] = REG_READ(ah, reg + (i << 2));
790
791         if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
792                 status = -2;
793
794         REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
795                     AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
796
797         kfree(buf);
798
799         return status;
800 }
801 EXPORT_SYMBOL(ar9003_paprd_create_curve);
802
803 int ar9003_paprd_init_table(struct ath_hw *ah)
804 {
805         int ret;
806
807         ret = ar9003_paprd_setup_single_table(ah);
808         if (ret < 0)
809             return ret;
810
811         ar9003_paprd_get_gain_table(ah);
812         return 0;
813 }
814 EXPORT_SYMBOL(ar9003_paprd_init_table);
815
816 bool ar9003_paprd_is_done(struct ath_hw *ah)
817 {
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);
821
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);
825
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);
829         /*
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
833          */
834                 if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
835                         paprd_done = 0;
836         }
837
838         return !!paprd_done;
839 }
840 EXPORT_SYMBOL(ar9003_paprd_is_done);