Linux Audio

Check our new training course

Yocto distribution development and maintenance

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