ar9287_olc.c revision 222301
1/*
2 * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * $FreeBSD: head/sys/dev/ath/ath_hal/ar9002/ar9287_olc.c 222301 2011-05-26 09:15:33Z adrian $
26 */
27#include "opt_ah.h"
28
29#include "ah.h"
30#include "ah_internal.h"
31
32#include "ah_eeprom_v14.h"
33
34#include "ar9002/ar9280.h"
35#include "ar5416/ar5416reg.h"
36#include "ar5416/ar5416phy.h"
37#include "ar9002/ar9002phy.h"
38
39#include "ar9002/ar9287phy.h"
40#include "ar9002/ar9287an.h"
41#include "ar9002/ar9287_olc.h"
42
43void
44ar9287olcInit(struct ath_hal *ah)
45{
46	OS_REG_SET_BIT(ah, AR_PHY_TX_PWRCTRL9,
47	    AR_PHY_TX_PWRCTRL9_RES_DC_REMOVAL);
48	OS_A_REG_RMW_FIELD(ah, AR9287_AN_TXPC0,
49	    AR9287_AN_TXPC0_TXPCMODE,
50	    AR9287_AN_TXPC0_TXPCMODE_TEMPSENSE);
51	OS_DELAY(100);
52}
53
54/*
55 * Run temperature compensation calibration.
56 *
57 * The TX gain table is adjusted depending upon the difference
58 * between the initial PDADC value and the currently read
59 * average TX power sample value. This value is only valid if
60 * frames have been transmitted, so currPDADC will be 0 if
61 * no frames have yet been transmitted.
62 */
63void
64ar9287olcTemperatureCompensation(struct ath_hal *ah)
65{
66	uint32_t rddata;
67	int32_t delta, currPDADC, slope;
68
69	rddata = OS_REG_READ(ah, AR_PHY_TX_PWRCTRL4);
70	currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
71
72	if (AH5416(ah)->initPDADC == 0 || currPDADC == 0) {
73		/*
74		 * Zero value indicates that no frames have been transmitted
75		 * yet, can't do temperature compensation until frames are
76		 * transmitted.
77		 */
78		return;
79	} else {
80		int8_t val;
81		(void) (ath_hal_eepromGet(ah, AR_EEP_TEMPSENSE_SLOPE, &val));
82		slope = val;
83
84		if (slope == 0) { /* to avoid divide by zero case */
85			delta = 0;
86		} else {
87			delta = ((currPDADC - AH5416(ah)->initPDADC)*4) / slope;
88		}
89		OS_REG_RMW_FIELD(ah, AR_PHY_CH0_TX_PWRCTRL11,
90		    AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
91		OS_REG_RMW_FIELD(ah, AR_PHY_CH1_TX_PWRCTRL11,
92		    AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
93	}
94}
95