1185377Ssam/*
2187831Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3185377Ssam * Copyright (c) 2002-2008 Atheros Communications, Inc.
4185377Ssam *
5185377Ssam * Permission to use, copy, modify, and/or distribute this software for any
6185377Ssam * purpose with or without fee is hereby granted, provided that the above
7185377Ssam * copyright notice and this permission notice appear in all copies.
8185377Ssam *
9185377Ssam * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10185377Ssam * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11185377Ssam * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12185377Ssam * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13185377Ssam * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14185377Ssam * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15185377Ssam * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16185377Ssam *
17187831Ssam * $FreeBSD$
18185377Ssam */
19185377Ssam#include "opt_ah.h"
20185377Ssam
21185377Ssam#include "ah.h"
22185377Ssam#include "ah_internal.h"
23185377Ssam
24185377Ssam#include "ah_eeprom_v3.h"
25185377Ssam
26185377Ssam#include "ar5212/ar5212.h"
27185377Ssam#include "ar5212/ar5212reg.h"
28185377Ssam#include "ar5212/ar5212phy.h"
29185377Ssam
30185377Ssam#define AH_5212_5112
31185377Ssam#include "ar5212/ar5212.ini"
32185377Ssam
33185377Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
34185377Ssam
35185377Ssamstruct ar5112State {
36185377Ssam	RF_HAL_FUNCS	base;		/* public state, must be first */
37185377Ssam	uint16_t	pcdacTable[PWR_TABLE_SIZE];
38185377Ssam
39185377Ssam	uint32_t	Bank1Data[N(ar5212Bank1_5112)];
40185377Ssam	uint32_t	Bank2Data[N(ar5212Bank2_5112)];
41185377Ssam	uint32_t	Bank3Data[N(ar5212Bank3_5112)];
42185377Ssam	uint32_t	Bank6Data[N(ar5212Bank6_5112)];
43185377Ssam	uint32_t	Bank7Data[N(ar5212Bank7_5112)];
44185377Ssam};
45185377Ssam#define	AR5112(ah)	((struct ar5112State *) AH5212(ah)->ah_rfHal)
46185377Ssam
47185377Ssamstatic	void ar5212GetLowerUpperIndex(uint16_t v,
48185377Ssam		uint16_t *lp, uint16_t listSize,
49185377Ssam		uint32_t *vlo, uint32_t *vhi);
50185377Ssamstatic HAL_BOOL getFullPwrTable(uint16_t numPcdacs, uint16_t *pcdacs,
51185377Ssam		int16_t *power, int16_t maxPower, int16_t *retVals);
52185377Ssamstatic int16_t getPminAndPcdacTableFromPowerTable(int16_t *pwrTableT4,
53185377Ssam		uint16_t retVals[]);
54185377Ssamstatic int16_t getPminAndPcdacTableFromTwoPowerTables(int16_t *pwrTableLXpdT4,
55185377Ssam		int16_t *pwrTableHXpdT4, uint16_t retVals[], int16_t *pMid);
56185377Ssamstatic int16_t interpolate_signed(uint16_t target,
57185377Ssam		uint16_t srcLeft, uint16_t srcRight,
58185377Ssam		int16_t targetLeft, int16_t targetRight);
59185377Ssam
60185377Ssamextern	void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
61185377Ssam		uint32_t numBits, uint32_t firstBit, uint32_t column);
62185377Ssam
63185377Ssamstatic void
64185377Ssamar5112WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
65185377Ssam	int writes)
66185377Ssam{
67185377Ssam	HAL_INI_WRITE_ARRAY(ah, ar5212Modes_5112, modesIndex, writes);
68185377Ssam	HAL_INI_WRITE_ARRAY(ah, ar5212Common_5112, 1, writes);
69185377Ssam	HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_5112, freqIndex, writes);
70185377Ssam}
71185377Ssam
72185377Ssam/*
73185377Ssam * Take the MHz channel value and set the Channel value
74185377Ssam *
75185377Ssam * ASSUMES: Writes enabled to analog bus
76185377Ssam */
77185377Ssamstatic HAL_BOOL
78187831Ssamar5112SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
79185377Ssam{
80187831Ssam	uint16_t freq = ath_hal_gethwchannel(ah, chan);
81185377Ssam	uint32_t channelSel  = 0;
82185377Ssam	uint32_t bModeSynth  = 0;
83185377Ssam	uint32_t aModeRefSel = 0;
84185377Ssam	uint32_t reg32       = 0;
85185377Ssam
86187831Ssam	OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
87185377Ssam
88187831Ssam	if (freq < 4800) {
89185377Ssam		uint32_t txctl;
90185377Ssam
91187831Ssam		if (((freq - 2192) % 5) == 0) {
92187831Ssam			channelSel = ((freq - 672) * 2 - 3040)/10;
93185377Ssam			bModeSynth = 0;
94187831Ssam		} else if (((freq - 2224) % 5) == 0) {
95187831Ssam			channelSel = ((freq - 704) * 2 - 3040) / 10;
96185377Ssam			bModeSynth = 1;
97185377Ssam		} else {
98185377Ssam			HALDEBUG(ah, HAL_DEBUG_ANY,
99185377Ssam			    "%s: invalid channel %u MHz\n",
100187831Ssam			    __func__, freq);
101185377Ssam			return AH_FALSE;
102185377Ssam		}
103185377Ssam
104185377Ssam		channelSel = (channelSel << 2) & 0xff;
105185377Ssam		channelSel = ath_hal_reverseBits(channelSel, 8);
106185377Ssam
107185377Ssam		txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
108187831Ssam		if (freq == 2484) {
109185377Ssam			/* Enable channel spreading for channel 14 */
110185377Ssam			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
111185377Ssam				txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
112185377Ssam		} else {
113185377Ssam			OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
114185377Ssam				txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
115185377Ssam		}
116187831Ssam	} else if (((freq % 5) == 2) && (freq <= 5435)) {
117187831Ssam		freq = freq - 2; /* Align to even 5MHz raster */
118185377Ssam		channelSel = ath_hal_reverseBits(
119185377Ssam			(uint32_t)(((freq - 4800)*10)/25 + 1), 8);
120185377Ssam            	aModeRefSel = ath_hal_reverseBits(0, 2);
121187831Ssam	} else if ((freq % 20) == 0 && freq >= 5120) {
122185377Ssam		channelSel = ath_hal_reverseBits(
123187831Ssam			((freq - 4800) / 20 << 2), 8);
124185377Ssam		aModeRefSel = ath_hal_reverseBits(3, 2);
125187831Ssam	} else if ((freq % 10) == 0) {
126185377Ssam		channelSel = ath_hal_reverseBits(
127187831Ssam			((freq - 4800) / 10 << 1), 8);
128185377Ssam		aModeRefSel = ath_hal_reverseBits(2, 2);
129187831Ssam	} else if ((freq % 5) == 0) {
130185377Ssam		channelSel = ath_hal_reverseBits(
131187831Ssam			(freq - 4800) / 5, 8);
132185377Ssam		aModeRefSel = ath_hal_reverseBits(1, 2);
133185377Ssam	} else {
134185377Ssam		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
135187831Ssam		    __func__, freq);
136185377Ssam		return AH_FALSE;
137185377Ssam	}
138185377Ssam
139185377Ssam	reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
140185377Ssam			(1 << 12) | 0x1;
141185377Ssam	OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
142185377Ssam
143185377Ssam	reg32 >>= 8;
144185377Ssam	OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
145185377Ssam
146185377Ssam	AH_PRIVATE(ah)->ah_curchan = chan;
147185377Ssam	return AH_TRUE;
148185377Ssam}
149185377Ssam
150185377Ssam/*
151185377Ssam * Return a reference to the requested RF Bank.
152185377Ssam */
153185377Ssamstatic uint32_t *
154185377Ssamar5112GetRfBank(struct ath_hal *ah, int bank)
155185377Ssam{
156185377Ssam	struct ar5112State *priv = AR5112(ah);
157185377Ssam
158185377Ssam	HALASSERT(priv != AH_NULL);
159185377Ssam	switch (bank) {
160185377Ssam	case 1: return priv->Bank1Data;
161185377Ssam	case 2: return priv->Bank2Data;
162185377Ssam	case 3: return priv->Bank3Data;
163185377Ssam	case 6: return priv->Bank6Data;
164185377Ssam	case 7: return priv->Bank7Data;
165185377Ssam	}
166185377Ssam	HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
167185377Ssam	    __func__, bank);
168185377Ssam	return AH_NULL;
169185377Ssam}
170185377Ssam
171185377Ssam/*
172185377Ssam * Reads EEPROM header info from device structure and programs
173185377Ssam * all rf registers
174185377Ssam *
175185377Ssam * REQUIRES: Access to the analog rf device
176185377Ssam */
177185377Ssamstatic HAL_BOOL
178187831Ssamar5112SetRfRegs(struct ath_hal *ah,
179187831Ssam	const struct ieee80211_channel *chan,
180185377Ssam	uint16_t modesIndex, uint16_t *rfXpdGain)
181185377Ssam{
182185377Ssam#define	RF_BANK_SETUP(_priv, _ix, _col) do {				    \
183185377Ssam	int i;								    \
184185377Ssam	for (i = 0; i < N(ar5212Bank##_ix##_5112); i++)			    \
185185377Ssam		(_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_5112[i][_col];\
186185377Ssam} while (0)
187187831Ssam	uint16_t freq = ath_hal_gethwchannel(ah, chan);
188185377Ssam	struct ath_hal_5212 *ahp = AH5212(ah);
189185377Ssam	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
190185377Ssam	uint16_t rfXpdSel, gainI;
191185377Ssam	uint16_t ob5GHz = 0, db5GHz = 0;
192185377Ssam	uint16_t ob2GHz = 0, db2GHz = 0;
193185377Ssam	struct ar5112State *priv = AR5112(ah);
194185377Ssam	GAIN_VALUES *gv = &ahp->ah_gainValues;
195185377Ssam	int regWrites = 0;
196185377Ssam
197185377Ssam	HALASSERT(priv);
198185377Ssam
199187831Ssam	HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
200187831Ssam	    __func__, chan->ic_freq, chan->ic_flags, modesIndex);
201187831Ssam
202185377Ssam	/* Setup rf parameters */
203187831Ssam	switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
204187831Ssam	case IEEE80211_CHAN_A:
205187831Ssam		if (freq > 4000 && freq < 5260) {
206185377Ssam			ob5GHz = ee->ee_ob1;
207185377Ssam			db5GHz = ee->ee_db1;
208187831Ssam		} else if (freq >= 5260 && freq < 5500) {
209185377Ssam			ob5GHz = ee->ee_ob2;
210185377Ssam			db5GHz = ee->ee_db2;
211187831Ssam		} else if (freq >= 5500 && freq < 5725) {
212185377Ssam			ob5GHz = ee->ee_ob3;
213185377Ssam			db5GHz = ee->ee_db3;
214187831Ssam		} else if (freq >= 5725) {
215185377Ssam			ob5GHz = ee->ee_ob4;
216185377Ssam			db5GHz = ee->ee_db4;
217185377Ssam		} else {
218185377Ssam			/* XXX else */
219185377Ssam		}
220185377Ssam		rfXpdSel = ee->ee_xpd[headerInfo11A];
221185377Ssam		gainI = ee->ee_gainI[headerInfo11A];
222185377Ssam		break;
223187831Ssam	case IEEE80211_CHAN_B:
224185377Ssam		ob2GHz = ee->ee_ob2GHz[0];
225185377Ssam		db2GHz = ee->ee_db2GHz[0];
226185377Ssam		rfXpdSel = ee->ee_xpd[headerInfo11B];
227185377Ssam		gainI = ee->ee_gainI[headerInfo11B];
228185377Ssam		break;
229187831Ssam	case IEEE80211_CHAN_G:
230187831Ssam	case IEEE80211_CHAN_PUREG:	/* NB: really 108G */
231185377Ssam		ob2GHz = ee->ee_ob2GHz[1];
232185377Ssam		db2GHz = ee->ee_ob2GHz[1];
233185377Ssam		rfXpdSel = ee->ee_xpd[headerInfo11G];
234185377Ssam		gainI = ee->ee_gainI[headerInfo11G];
235185377Ssam		break;
236185377Ssam	default:
237185377Ssam		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
238187831Ssam		    __func__, chan->ic_flags);
239185377Ssam		return AH_FALSE;
240185377Ssam	}
241185377Ssam
242185377Ssam	/* Setup Bank 1 Write */
243185377Ssam	RF_BANK_SETUP(priv, 1, 1);
244185377Ssam
245185377Ssam	/* Setup Bank 2 Write */
246185377Ssam	RF_BANK_SETUP(priv, 2, modesIndex);
247185377Ssam
248185377Ssam	/* Setup Bank 3 Write */
249185377Ssam	RF_BANK_SETUP(priv, 3, modesIndex);
250185377Ssam
251185377Ssam	/* Setup Bank 6 Write */
252185377Ssam	RF_BANK_SETUP(priv, 6, modesIndex);
253185377Ssam
254185377Ssam	ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdSel,     1, 302, 0);
255185377Ssam
256185377Ssam	ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdGain[0], 2, 270, 0);
257185377Ssam	ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdGain[1], 2, 257, 0);
258185377Ssam
259187831Ssam	if (IEEE80211_IS_CHAN_OFDM(chan)) {
260185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
261185377Ssam			gv->currStep->paramVal[GP_PWD_138], 1, 168, 3);
262185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
263185377Ssam			gv->currStep->paramVal[GP_PWD_137], 1, 169, 3);
264185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
265185377Ssam			gv->currStep->paramVal[GP_PWD_136], 1, 170, 3);
266185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
267185377Ssam			gv->currStep->paramVal[GP_PWD_132], 1, 174, 3);
268185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
269185377Ssam			gv->currStep->paramVal[GP_PWD_131], 1, 175, 3);
270185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data,
271185377Ssam			gv->currStep->paramVal[GP_PWD_130], 1, 176, 3);
272185377Ssam	}
273185377Ssam
274185377Ssam	/* Only the 5 or 2 GHz OB/DB need to be set for a mode */
275187831Ssam	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
276185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 287, 0);
277185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 290, 0);
278185377Ssam	} else {
279185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, ob5GHz, 3, 279, 0);
280185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, db5GHz, 3, 282, 0);
281185377Ssam	}
282185377Ssam
283185377Ssam	/* Lower synth voltage for X112 Rev 2.0 only */
284185377Ssam	if (IS_RADX112_REV2(ah)) {
285185377Ssam		/* Non-Reversed analyg registers - so values are pre-reversed */
286185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 90, 2);
287185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 92, 2);
288185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 94, 2);
289185377Ssam		ar5212ModifyRfBuffer(priv->Bank6Data, 2, 1, 254, 2);
290185377Ssam	}
291185377Ssam
292185377Ssam    /* Decrease Power Consumption for 5312/5213 and up */
293185377Ssam    if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_2) {
294185377Ssam        ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 281, 1);
295185377Ssam        ar5212ModifyRfBuffer(priv->Bank6Data, 1, 2, 1, 3);
296185377Ssam        ar5212ModifyRfBuffer(priv->Bank6Data, 1, 2, 3, 3);
297185377Ssam        ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 139, 3);
298185377Ssam        ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 140, 3);
299185377Ssam    }
300185377Ssam
301185377Ssam	/* Setup Bank 7 Setup */
302185377Ssam	RF_BANK_SETUP(priv, 7, modesIndex);
303187831Ssam	if (IEEE80211_IS_CHAN_OFDM(chan))
304185377Ssam		ar5212ModifyRfBuffer(priv->Bank7Data,
305185377Ssam			gv->currStep->paramVal[GP_MIXGAIN_OVR], 2, 37, 0);
306185377Ssam
307185377Ssam	ar5212ModifyRfBuffer(priv->Bank7Data, gainI, 6, 14, 0);
308185377Ssam
309185377Ssam	/* Adjust params for Derby TX power control */
310187831Ssam	if (IEEE80211_IS_CHAN_HALF(chan) || IEEE80211_IS_CHAN_QUARTER(chan)) {
311185377Ssam        	uint32_t	rfDelay, rfPeriod;
312185377Ssam
313185377Ssam        	rfDelay = 0xf;
314187831Ssam        	rfPeriod = (IEEE80211_IS_CHAN_HALF(chan)) ?  0x8 : 0xf;
315185377Ssam        	ar5212ModifyRfBuffer(priv->Bank7Data, rfDelay, 4, 58, 0);
316185377Ssam        	ar5212ModifyRfBuffer(priv->Bank7Data, rfPeriod, 4, 70, 0);
317185377Ssam	}
318185377Ssam
319185377Ssam#ifdef notyet
320185377Ssam	/* Analog registers are setup - EAR can modify */
321185377Ssam	if (ar5212IsEarEngaged(pDev, chan))
322185377Ssam		uint32_t modifier;
323185377Ssam		ar5212EarModify(pDev, EAR_LC_RF_WRITE, chan, &modifier);
324185377Ssam#endif
325185377Ssam	/* Write Analog registers */
326185377Ssam	HAL_INI_WRITE_BANK(ah, ar5212Bank1_5112, priv->Bank1Data, regWrites);
327185377Ssam	HAL_INI_WRITE_BANK(ah, ar5212Bank2_5112, priv->Bank2Data, regWrites);
328185377Ssam	HAL_INI_WRITE_BANK(ah, ar5212Bank3_5112, priv->Bank3Data, regWrites);
329185377Ssam	HAL_INI_WRITE_BANK(ah, ar5212Bank6_5112, priv->Bank6Data, regWrites);
330185377Ssam	HAL_INI_WRITE_BANK(ah, ar5212Bank7_5112, priv->Bank7Data, regWrites);
331185377Ssam
332185377Ssam	/* Now that we have reprogrammed rfgain value, clear the flag. */
333185377Ssam	ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
334185377Ssam	return AH_TRUE;
335185377Ssam#undef	RF_BANK_SETUP
336185377Ssam}
337185377Ssam
338185377Ssam/*
339185377Ssam * Read the transmit power levels from the structures taken from EEPROM
340185377Ssam * Interpolate read transmit power values for this channel
341185377Ssam * Organize the transmit power values into a table for writing into the hardware
342185377Ssam */
343185377Ssamstatic HAL_BOOL
344185377Ssamar5112SetPowerTable(struct ath_hal *ah,
345187831Ssam	int16_t *pPowerMin, int16_t *pPowerMax,
346187831Ssam	const struct ieee80211_channel *chan,
347185377Ssam	uint16_t *rfXpdGain)
348185377Ssam{
349187831Ssam	uint16_t freq = ath_hal_gethwchannel(ah, chan);
350185377Ssam	struct ath_hal_5212 *ahp = AH5212(ah);
351185377Ssam	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
352185377Ssam	uint32_t numXpdGain = IS_RADX112_REV2(ah) ? 2 : 1;
353185377Ssam	uint32_t    xpdGainMask = 0;
354185377Ssam	int16_t     powerMid, *pPowerMid = &powerMid;
355185377Ssam
356185377Ssam	const EXPN_DATA_PER_CHANNEL_5112 *pRawCh;
357185377Ssam	const EEPROM_POWER_EXPN_5112     *pPowerExpn = AH_NULL;
358185377Ssam
359185377Ssam	uint32_t    ii, jj, kk;
360185377Ssam	int16_t     minPwr_t4, maxPwr_t4, Pmin, Pmid;
361185377Ssam
362185377Ssam	uint32_t    chan_idx_L = 0, chan_idx_R = 0;
363185377Ssam	uint16_t    chan_L, chan_R;
364185377Ssam
365185377Ssam	int16_t     pwr_table0[64];
366185377Ssam	int16_t     pwr_table1[64];
367185377Ssam	uint16_t    pcdacs[10];
368185377Ssam	int16_t     powers[10];
369185377Ssam	uint16_t    numPcd;
370185377Ssam	int16_t     powTableLXPD[2][64];
371185377Ssam	int16_t     powTableHXPD[2][64];
372185377Ssam	int16_t     tmpPowerTable[64];
373185377Ssam	uint16_t    xgainList[2];
374185377Ssam	uint16_t    xpdMask;
375185377Ssam
376187831Ssam	switch (chan->ic_flags & IEEE80211_CHAN_ALLTURBOFULL) {
377187831Ssam	case IEEE80211_CHAN_A:
378187831Ssam	case IEEE80211_CHAN_ST:
379185377Ssam		pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11A];
380185377Ssam		xpdGainMask = ee->ee_xgain[headerInfo11A];
381185377Ssam		break;
382187831Ssam	case IEEE80211_CHAN_B:
383185377Ssam		pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11B];
384185377Ssam		xpdGainMask = ee->ee_xgain[headerInfo11B];
385185377Ssam		break;
386187831Ssam	case IEEE80211_CHAN_G:
387187831Ssam	case IEEE80211_CHAN_108G:
388185377Ssam		pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11G];
389185377Ssam		xpdGainMask = ee->ee_xgain[headerInfo11G];
390185377Ssam		break;
391185377Ssam	default:
392185377Ssam		HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown channel flags 0x%x\n",
393187831Ssam		    __func__, chan->ic_flags);
394185377Ssam		return AH_FALSE;
395185377Ssam	}
396185377Ssam
397185377Ssam	if ((xpdGainMask & pPowerExpn->xpdMask) < 1) {
398185377Ssam		HALDEBUG(ah, HAL_DEBUG_ANY,
399185377Ssam		    "%s: desired xpdGainMask 0x%x not supported by "
400185377Ssam		    "calibrated xpdMask 0x%x\n", __func__,
401185377Ssam		    xpdGainMask, pPowerExpn->xpdMask);
402185377Ssam		return AH_FALSE;
403185377Ssam	}
404185377Ssam
405185377Ssam	maxPwr_t4 = (int16_t)(2*(*pPowerMax));	/* pwr_t2 -> pwr_t4 */
406185377Ssam	minPwr_t4 = (int16_t)(2*(*pPowerMin));	/* pwr_t2 -> pwr_t4 */
407185377Ssam
408185377Ssam	xgainList[0] = 0xDEAD;
409185377Ssam	xgainList[1] = 0xDEAD;
410185377Ssam
411185377Ssam	kk = 0;
412185377Ssam	xpdMask = pPowerExpn->xpdMask;
413185377Ssam	for (jj = 0; jj < NUM_XPD_PER_CHANNEL; jj++) {
414185377Ssam		if (((xpdMask >> jj) & 1) > 0) {
415185377Ssam			if (kk > 1) {
416185377Ssam				HALDEBUG(ah, HAL_DEBUG_ANY,
417185377Ssam				    "A maximum of 2 xpdGains supported"
418185377Ssam				    "in pExpnPower data\n");
419185377Ssam				return AH_FALSE;
420185377Ssam			}
421185377Ssam			xgainList[kk++] = (uint16_t)jj;
422185377Ssam		}
423185377Ssam	}
424185377Ssam
425187831Ssam	ar5212GetLowerUpperIndex(freq, &pPowerExpn->pChannels[0],
426185377Ssam		pPowerExpn->numChannels, &chan_idx_L, &chan_idx_R);
427185377Ssam
428185377Ssam	kk = 0;
429185377Ssam	for (ii = chan_idx_L; ii <= chan_idx_R; ii++) {
430185377Ssam		pRawCh = &(pPowerExpn->pDataPerChannel[ii]);
431185377Ssam		if (xgainList[1] == 0xDEAD) {
432185377Ssam			jj = xgainList[0];
433185377Ssam			numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
434185377Ssam			OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
435185377Ssam				numPcd * sizeof(uint16_t));
436185377Ssam			OS_MEMCPY(&powers[0], &pRawCh->pDataPerXPD[jj].pwr_t4[0],
437185377Ssam				numPcd * sizeof(int16_t));
438185377Ssam			if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
439185377Ssam				pRawCh->maxPower_t4, &tmpPowerTable[0])) {
440185377Ssam				return AH_FALSE;
441185377Ssam			}
442185377Ssam			OS_MEMCPY(&powTableLXPD[kk][0], &tmpPowerTable[0],
443185377Ssam				64*sizeof(int16_t));
444185377Ssam		} else {
445185377Ssam			jj = xgainList[0];
446185377Ssam			numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
447185377Ssam			OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
448185377Ssam				numPcd*sizeof(uint16_t));
449185377Ssam			OS_MEMCPY(&powers[0],
450185377Ssam				&pRawCh->pDataPerXPD[jj].pwr_t4[0],
451185377Ssam				numPcd*sizeof(int16_t));
452185377Ssam			if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
453185377Ssam				pRawCh->maxPower_t4, &tmpPowerTable[0])) {
454185377Ssam				return AH_FALSE;
455185377Ssam			}
456185377Ssam			OS_MEMCPY(&powTableLXPD[kk][0], &tmpPowerTable[0],
457185377Ssam				64 * sizeof(int16_t));
458185377Ssam
459185377Ssam			jj = xgainList[1];
460185377Ssam			numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
461185377Ssam			OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
462185377Ssam				numPcd * sizeof(uint16_t));
463185377Ssam			OS_MEMCPY(&powers[0],
464185377Ssam				&pRawCh->pDataPerXPD[jj].pwr_t4[0],
465185377Ssam				numPcd * sizeof(int16_t));
466185377Ssam			if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
467185377Ssam				pRawCh->maxPower_t4, &tmpPowerTable[0])) {
468185377Ssam				return AH_FALSE;
469185377Ssam			}
470185377Ssam			OS_MEMCPY(&powTableHXPD[kk][0], &tmpPowerTable[0],
471185377Ssam				64 * sizeof(int16_t));
472185377Ssam		}
473185377Ssam		kk++;
474185377Ssam	}
475185377Ssam
476185377Ssam	chan_L = pPowerExpn->pChannels[chan_idx_L];
477185377Ssam	chan_R = pPowerExpn->pChannels[chan_idx_R];
478185377Ssam	kk = chan_idx_R - chan_idx_L;
479185377Ssam
480185377Ssam	if (xgainList[1] == 0xDEAD) {
481185377Ssam		for (jj = 0; jj < 64; jj++) {
482185377Ssam			pwr_table0[jj] = interpolate_signed(
483187831Ssam				freq, chan_L, chan_R,
484185377Ssam				powTableLXPD[0][jj], powTableLXPD[kk][jj]);
485185377Ssam		}
486185377Ssam		Pmin = getPminAndPcdacTableFromPowerTable(&pwr_table0[0],
487185377Ssam				ahp->ah_pcdacTable);
488185377Ssam		*pPowerMin = (int16_t) (Pmin / 2);
489185377Ssam		*pPowerMid = (int16_t) (pwr_table0[63] / 2);
490185377Ssam		*pPowerMax = (int16_t) (pwr_table0[63] / 2);
491185377Ssam		rfXpdGain[0] = xgainList[0];
492185377Ssam		rfXpdGain[1] = rfXpdGain[0];
493185377Ssam	} else {
494185377Ssam		for (jj = 0; jj < 64; jj++) {
495185377Ssam			pwr_table0[jj] = interpolate_signed(
496187831Ssam				freq, chan_L, chan_R,
497185377Ssam				powTableLXPD[0][jj], powTableLXPD[kk][jj]);
498185377Ssam			pwr_table1[jj] = interpolate_signed(
499187831Ssam				freq, chan_L, chan_R,
500185377Ssam				powTableHXPD[0][jj], powTableHXPD[kk][jj]);
501185377Ssam		}
502185377Ssam		if (numXpdGain == 2) {
503185377Ssam			Pmin = getPminAndPcdacTableFromTwoPowerTables(
504185377Ssam				&pwr_table0[0], &pwr_table1[0],
505185377Ssam				ahp->ah_pcdacTable, &Pmid);
506185377Ssam			*pPowerMin = (int16_t) (Pmin / 2);
507185377Ssam			*pPowerMid = (int16_t) (Pmid / 2);
508185377Ssam			*pPowerMax = (int16_t) (pwr_table0[63] / 2);
509185377Ssam			rfXpdGain[0] = xgainList[0];
510185377Ssam			rfXpdGain[1] = xgainList[1];
511185377Ssam		} else if (minPwr_t4 <= pwr_table1[63] &&
512185377Ssam			   maxPwr_t4 <= pwr_table1[63]) {
513185377Ssam			Pmin = getPminAndPcdacTableFromPowerTable(
514185377Ssam				&pwr_table1[0], ahp->ah_pcdacTable);
515185377Ssam			rfXpdGain[0] = xgainList[1];
516185377Ssam			rfXpdGain[1] = rfXpdGain[0];
517185377Ssam			*pPowerMin = (int16_t) (Pmin / 2);
518185377Ssam			*pPowerMid = (int16_t) (pwr_table1[63] / 2);
519185377Ssam			*pPowerMax = (int16_t) (pwr_table1[63] / 2);
520185377Ssam		} else {
521185377Ssam			Pmin = getPminAndPcdacTableFromPowerTable(
522185377Ssam				&pwr_table0[0], ahp->ah_pcdacTable);
523185377Ssam			rfXpdGain[0] = xgainList[0];
524185377Ssam			rfXpdGain[1] = rfXpdGain[0];
525185377Ssam			*pPowerMin = (int16_t) (Pmin/2);
526185377Ssam			*pPowerMid = (int16_t) (pwr_table0[63] / 2);
527185377Ssam			*pPowerMax = (int16_t) (pwr_table0[63] / 2);
528185377Ssam		}
529185377Ssam	}
530185377Ssam
531185377Ssam	/*
532185377Ssam	 * Move 5112 rates to match power tables where the max
533185377Ssam	 * power table entry corresponds with maxPower.
534185377Ssam	 */
535185377Ssam	HALASSERT(*pPowerMax <= PCDAC_STOP);
536185377Ssam	ahp->ah_txPowerIndexOffset = PCDAC_STOP - *pPowerMax;
537185377Ssam
538185377Ssam	return AH_TRUE;
539185377Ssam}
540185377Ssam
541185377Ssam/*
542185377Ssam * Returns interpolated or the scaled up interpolated value
543185377Ssam */
544185377Ssamstatic int16_t
545185377Ssaminterpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
546185377Ssam	int16_t targetLeft, int16_t targetRight)
547185377Ssam{
548185377Ssam	int16_t rv;
549185377Ssam
550185377Ssam	if (srcRight != srcLeft) {
551185377Ssam		rv = ((target - srcLeft)*targetRight +
552185377Ssam		      (srcRight - target)*targetLeft) / (srcRight - srcLeft);
553185377Ssam	} else {
554185377Ssam		rv = targetLeft;
555185377Ssam	}
556185377Ssam	return rv;
557185377Ssam}
558185377Ssam
559185377Ssam/*
560185377Ssam * Return indices surrounding the value in sorted integer lists.
561185377Ssam *
562185377Ssam * NB: the input list is assumed to be sorted in ascending order
563185377Ssam */
564185377Ssamstatic void
565185377Ssamar5212GetLowerUpperIndex(uint16_t v, uint16_t *lp, uint16_t listSize,
566185377Ssam                          uint32_t *vlo, uint32_t *vhi)
567185377Ssam{
568185377Ssam	uint32_t target = v;
569185377Ssam	uint16_t *ep = lp+listSize;
570185377Ssam	uint16_t *tp;
571185377Ssam
572185377Ssam	/*
573185377Ssam	 * Check first and last elements for out-of-bounds conditions.
574185377Ssam	 */
575185377Ssam	if (target < lp[0]) {
576185377Ssam		*vlo = *vhi = 0;
577185377Ssam		return;
578185377Ssam	}
579185377Ssam	if (target >= ep[-1]) {
580185377Ssam		*vlo = *vhi = listSize - 1;
581185377Ssam		return;
582185377Ssam	}
583185377Ssam
584185377Ssam	/* look for value being near or between 2 values in list */
585185377Ssam	for (tp = lp; tp < ep; tp++) {
586185377Ssam		/*
587185377Ssam		 * If value is close to the current value of the list
588185377Ssam		 * then target is not between values, it is one of the values
589185377Ssam		 */
590185377Ssam		if (*tp == target) {
591185377Ssam			*vlo = *vhi = tp - lp;
592185377Ssam			return;
593185377Ssam		}
594185377Ssam		/*
595185377Ssam		 * Look for value being between current value and next value
596185377Ssam		 * if so return these 2 values
597185377Ssam		 */
598185377Ssam		if (target < tp[1]) {
599185377Ssam			*vlo = tp - lp;
600185377Ssam			*vhi = *vlo + 1;
601185377Ssam			return;
602185377Ssam		}
603185377Ssam	}
604185377Ssam}
605185377Ssam
606185377Ssamstatic HAL_BOOL
607185377SsamgetFullPwrTable(uint16_t numPcdacs, uint16_t *pcdacs, int16_t *power, int16_t maxPower, int16_t *retVals)
608185377Ssam{
609185377Ssam	uint16_t    ii;
610185377Ssam	uint16_t    idxL = 0;
611185377Ssam	uint16_t    idxR = 1;
612185377Ssam
613185377Ssam	if (numPcdacs < 2) {
614223466Sadrian		HALDEBUG_G(AH_NULL, HAL_DEBUG_ANY,
615185377Ssam		     "%s: at least 2 pcdac values needed [%d]\n",
616185377Ssam		     __func__, numPcdacs);
617185377Ssam		return AH_FALSE;
618185377Ssam	}
619185377Ssam	for (ii = 0; ii < 64; ii++) {
620185377Ssam		if (ii>pcdacs[idxR] && idxR < numPcdacs-1) {
621185377Ssam			idxL++;
622185377Ssam			idxR++;
623185377Ssam		}
624185377Ssam		retVals[ii] = interpolate_signed(ii,
625185377Ssam			pcdacs[idxL], pcdacs[idxR], power[idxL], power[idxR]);
626185377Ssam		if (retVals[ii] >= maxPower) {
627185377Ssam			while (ii < 64)
628185377Ssam				retVals[ii++] = maxPower;
629185377Ssam		}
630185377Ssam	}
631185377Ssam	return AH_TRUE;
632185377Ssam}
633185377Ssam
634185377Ssam/*
635185377Ssam * Takes a single calibration curve and creates a power table.
636185377Ssam * Adjusts the new power table so the max power is relative
637185377Ssam * to the maximum index in the power table.
638185377Ssam *
639185377Ssam * WARNING: rates must be adjusted for this relative power table
640185377Ssam */
641185377Ssamstatic int16_t
642185377SsamgetPminAndPcdacTableFromPowerTable(int16_t *pwrTableT4, uint16_t retVals[])
643185377Ssam{
644185377Ssam    int16_t ii, jj, jjMax;
645185377Ssam    int16_t pMin, currPower, pMax;
646185377Ssam
647185377Ssam    /* If the spread is > 31.5dB, keep the upper 31.5dB range */
648185377Ssam    if ((pwrTableT4[63] - pwrTableT4[0]) > 126) {
649185377Ssam        pMin = pwrTableT4[63] - 126;
650185377Ssam    } else {
651185377Ssam        pMin = pwrTableT4[0];
652185377Ssam    }
653185377Ssam
654185377Ssam    pMax = pwrTableT4[63];
655185377Ssam    jjMax = 63;
656185377Ssam
657185377Ssam    /* Search for highest pcdac 0.25dB below maxPower */
658185377Ssam    while ((pwrTableT4[jjMax] > (pMax - 1) ) && (jjMax >= 0)) {
659185377Ssam        jjMax--;
660185377Ssam    }
661185377Ssam
662185377Ssam    jj = jjMax;
663185377Ssam    currPower = pMax;
664185377Ssam    for (ii = 63; ii >= 0; ii--) {
665185377Ssam        while ((jj < 64) && (jj > 0) && (pwrTableT4[jj] >= currPower)) {
666185377Ssam            jj--;
667185377Ssam        }
668185377Ssam        if (jj == 0) {
669185377Ssam            while (ii >= 0) {
670185377Ssam                retVals[ii] = retVals[ii + 1];
671185377Ssam                ii--;
672185377Ssam            }
673185377Ssam            break;
674185377Ssam        }
675185377Ssam        retVals[ii] = jj;
676185377Ssam        currPower -= 2;  // corresponds to a 0.5dB step
677185377Ssam    }
678185377Ssam    return pMin;
679185377Ssam}
680185377Ssam
681185377Ssam/*
682185377Ssam * Combines the XPD curves from two calibration sets into a single
683185377Ssam * power table and adjusts the power table so the max power is relative
684185377Ssam * to the maximum index in the power table
685185377Ssam *
686185377Ssam * WARNING: rates must be adjusted for this relative power table
687185377Ssam */
688185377Ssamstatic int16_t
689185377SsamgetPminAndPcdacTableFromTwoPowerTables(int16_t *pwrTableLXpdT4,
690185377Ssam	int16_t *pwrTableHXpdT4, uint16_t retVals[], int16_t *pMid)
691185377Ssam{
692185377Ssam    int16_t     ii, jj, jjMax;
693185377Ssam    int16_t     pMin, pMax, currPower;
694185377Ssam    int16_t     *pwrTableT4;
695185377Ssam    uint16_t    msbFlag = 0x40;  // turns on the 7th bit of the pcdac
696185377Ssam
697185377Ssam    /* If the spread is > 31.5dB, keep the upper 31.5dB range */
698185377Ssam    if ((pwrTableLXpdT4[63] - pwrTableHXpdT4[0]) > 126) {
699185377Ssam        pMin = pwrTableLXpdT4[63] - 126;
700185377Ssam    } else {
701185377Ssam        pMin = pwrTableHXpdT4[0];
702185377Ssam    }
703185377Ssam
704185377Ssam    pMax = pwrTableLXpdT4[63];
705185377Ssam    jjMax = 63;
706185377Ssam    /* Search for highest pcdac 0.25dB below maxPower */
707185377Ssam    while ((pwrTableLXpdT4[jjMax] > (pMax - 1) ) && (jjMax >= 0)){
708185377Ssam        jjMax--;
709185377Ssam    }
710185377Ssam
711185377Ssam    *pMid = pwrTableHXpdT4[63];
712185377Ssam    jj = jjMax;
713185377Ssam    ii = 63;
714185377Ssam    currPower = pMax;
715185377Ssam    pwrTableT4 = &(pwrTableLXpdT4[0]);
716185377Ssam    while (ii >= 0) {
717185377Ssam        if ((currPower <= *pMid) || ( (jj == 0) && (msbFlag == 0x40))){
718185377Ssam            msbFlag = 0x00;
719185377Ssam            pwrTableT4 = &(pwrTableHXpdT4[0]);
720185377Ssam            jj = 63;
721185377Ssam        }
722185377Ssam        while ((jj > 0) && (pwrTableT4[jj] >= currPower)) {
723185377Ssam            jj--;
724185377Ssam        }
725185377Ssam        if ((jj == 0) && (msbFlag == 0x00)) {
726185377Ssam            while (ii >= 0) {
727185377Ssam                retVals[ii] = retVals[ii+1];
728185377Ssam                ii--;
729185377Ssam            }
730185377Ssam            break;
731185377Ssam        }
732185377Ssam        retVals[ii] = jj | msbFlag;
733185377Ssam        currPower -= 2;  // corresponds to a 0.5dB step
734185377Ssam        ii--;
735185377Ssam    }
736185377Ssam    return pMin;
737185377Ssam}
738185377Ssam
739185377Ssamstatic int16_t
740185377Ssamar5112GetMinPower(struct ath_hal *ah, const EXPN_DATA_PER_CHANNEL_5112 *data)
741185377Ssam{
742185377Ssam	int i, minIndex;
743185377Ssam	int16_t minGain,minPwr,minPcdac,retVal;
744185377Ssam
745185377Ssam	/* Assume NUM_POINTS_XPD0 > 0 */
746185377Ssam	minGain = data->pDataPerXPD[0].xpd_gain;
747185377Ssam	for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {
748185377Ssam		if (data->pDataPerXPD[i].xpd_gain < minGain) {
749185377Ssam			minIndex = i;
750185377Ssam			minGain = data->pDataPerXPD[i].xpd_gain;
751185377Ssam		}
752185377Ssam	}
753185377Ssam	minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];
754185377Ssam	minPcdac = data->pDataPerXPD[minIndex].pcdac[0];
755185377Ssam	for (i=1; i<NUM_POINTS_XPD0; i++) {
756185377Ssam		if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {
757185377Ssam			minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];
758185377Ssam			minPcdac = data->pDataPerXPD[minIndex].pcdac[i];
759185377Ssam		}
760185377Ssam	}
761185377Ssam	retVal = minPwr - (minPcdac*2);
762185377Ssam	return(retVal);
763185377Ssam}
764185377Ssam
765185377Ssamstatic HAL_BOOL
766187831Ssamar5112GetChannelMaxMinPower(struct ath_hal *ah,
767187831Ssam	const struct ieee80211_channel *chan,
768185377Ssam	int16_t *maxPow, int16_t *minPow)
769185377Ssam{
770187831Ssam	uint16_t freq = chan->ic_freq;		/* NB: never mapped */
771185377Ssam	const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
772185377Ssam	int numChannels=0,i,last;
773185377Ssam	int totalD, totalF,totalMin;
774185377Ssam	const EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;
775185377Ssam	const EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;
776185377Ssam
777185377Ssam	*maxPow = 0;
778187831Ssam	if (IEEE80211_IS_CHAN_A(chan)) {
779185377Ssam		powerArray = ee->ee_modePowerArray5112;
780185377Ssam		data = powerArray[headerInfo11A].pDataPerChannel;
781185377Ssam		numChannels = powerArray[headerInfo11A].numChannels;
782187831Ssam	} else if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan)) {
783185377Ssam		/* XXX - is this correct? Should we also use the same power for turbo G? */
784185377Ssam		powerArray = ee->ee_modePowerArray5112;
785185377Ssam		data = powerArray[headerInfo11G].pDataPerChannel;
786185377Ssam		numChannels = powerArray[headerInfo11G].numChannels;
787187831Ssam	} else if (IEEE80211_IS_CHAN_B(chan)) {
788185377Ssam		powerArray = ee->ee_modePowerArray5112;
789185377Ssam		data = powerArray[headerInfo11B].pDataPerChannel;
790185377Ssam		numChannels = powerArray[headerInfo11B].numChannels;
791185377Ssam	} else {
792185377Ssam		return (AH_TRUE);
793185377Ssam	}
794185377Ssam	/* Make sure the channel is in the range of the TP values
795185377Ssam	 *  (freq piers)
796185377Ssam	 */
797185377Ssam	if (numChannels < 1)
798185377Ssam		return(AH_FALSE);
799185377Ssam
800187831Ssam	if ((freq < data[0].channelValue) ||
801187831Ssam	    (freq > data[numChannels-1].channelValue)) {
802187831Ssam		if (freq < data[0].channelValue) {
803185377Ssam			*maxPow = data[0].maxPower_t4;
804185377Ssam			*minPow = ar5112GetMinPower(ah, &data[0]);
805185377Ssam			return(AH_TRUE);
806185377Ssam		} else {
807185377Ssam			*maxPow = data[numChannels - 1].maxPower_t4;
808185377Ssam			*minPow = ar5112GetMinPower(ah, &data[numChannels - 1]);
809185377Ssam			return(AH_TRUE);
810185377Ssam		}
811185377Ssam	}
812185377Ssam
813185377Ssam	/* Linearly interpolate the power value now */
814185377Ssam	for (last=0,i=0;
815187831Ssam	     (i<numChannels) && (freq > data[i].channelValue);
816185377Ssam	     last=i++);
817185377Ssam	totalD = data[i].channelValue - data[last].channelValue;
818185377Ssam	if (totalD > 0) {
819185377Ssam		totalF = data[i].maxPower_t4 - data[last].maxPower_t4;
820187831Ssam		*maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);
821185377Ssam
822185377Ssam		totalMin = ar5112GetMinPower(ah,&data[i]) - ar5112GetMinPower(ah, &data[last]);
823187831Ssam		*minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) + ar5112GetMinPower(ah, &data[last])*totalD)/totalD);
824185377Ssam		return (AH_TRUE);
825185377Ssam	} else {
826187831Ssam		if (freq == data[i].channelValue) {
827185377Ssam			*maxPow = data[i].maxPower_t4;
828185377Ssam			*minPow = ar5112GetMinPower(ah, &data[i]);
829185377Ssam			return(AH_TRUE);
830185377Ssam		} else
831185377Ssam			return(AH_FALSE);
832185377Ssam	}
833185377Ssam}
834185377Ssam
835185377Ssam/*
836185377Ssam * Free memory for analog bank scratch buffers
837185377Ssam */
838185377Ssamstatic void
839185377Ssamar5112RfDetach(struct ath_hal *ah)
840185377Ssam{
841185377Ssam	struct ath_hal_5212 *ahp = AH5212(ah);
842185377Ssam
843185377Ssam	HALASSERT(ahp->ah_rfHal != AH_NULL);
844185377Ssam	ath_hal_free(ahp->ah_rfHal);
845185377Ssam	ahp->ah_rfHal = AH_NULL;
846185377Ssam}
847185377Ssam
848185377Ssam/*
849185377Ssam * Allocate memory for analog bank scratch buffers
850185377Ssam * Scratch Buffer will be reinitialized every reset so no need to zero now
851185377Ssam */
852185406Ssamstatic HAL_BOOL
853185377Ssamar5112RfAttach(struct ath_hal *ah, HAL_STATUS *status)
854185377Ssam{
855185377Ssam	struct ath_hal_5212 *ahp = AH5212(ah);
856185377Ssam	struct ar5112State *priv;
857185377Ssam
858185377Ssam	HALASSERT(ah->ah_magic == AR5212_MAGIC);
859185377Ssam
860185377Ssam	HALASSERT(ahp->ah_rfHal == AH_NULL);
861185377Ssam	priv = ath_hal_malloc(sizeof(struct ar5112State));
862185377Ssam	if (priv == AH_NULL) {
863185377Ssam		HALDEBUG(ah, HAL_DEBUG_ANY,
864185377Ssam		    "%s: cannot allocate private state\n", __func__);
865185377Ssam		*status = HAL_ENOMEM;		/* XXX */
866185377Ssam		return AH_FALSE;
867185377Ssam	}
868185377Ssam	priv->base.rfDetach		= ar5112RfDetach;
869185377Ssam	priv->base.writeRegs		= ar5112WriteRegs;
870185377Ssam	priv->base.getRfBank		= ar5112GetRfBank;
871185377Ssam	priv->base.setChannel		= ar5112SetChannel;
872185377Ssam	priv->base.setRfRegs		= ar5112SetRfRegs;
873185377Ssam	priv->base.setPowerTable	= ar5112SetPowerTable;
874185377Ssam	priv->base.getChannelMaxMinPower = ar5112GetChannelMaxMinPower;
875185377Ssam	priv->base.getNfAdjust		= ar5212GetNfAdjust;
876185377Ssam
877185377Ssam	ahp->ah_pcdacTable = priv->pcdacTable;
878185377Ssam	ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
879185377Ssam	ahp->ah_rfHal = &priv->base;
880185377Ssam
881185377Ssam	return AH_TRUE;
882185377Ssam}
883185406Ssam
884185406Ssamstatic HAL_BOOL
885185406Ssamar5112Probe(struct ath_hal *ah)
886185406Ssam{
887185406Ssam	return IS_RAD5112(ah);
888185406Ssam}
889185418SsamAH_RF(RF5112, ar5112Probe, ar5112RfAttach);
890