18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci  Broadcom B43 wireless driver
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci  G PHY LO (LocalOscillator) Measuring and Control routines
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci  Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
98c2ecf20Sopenharmony_ci  Copyright (c) 2005, 2006 Stefano Brivio <stefano.brivio@polimi.it>
108c2ecf20Sopenharmony_ci  Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
118c2ecf20Sopenharmony_ci  Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
128c2ecf20Sopenharmony_ci  Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci*/
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "b43.h"
188c2ecf20Sopenharmony_ci#include "lo.h"
198c2ecf20Sopenharmony_ci#include "phy_g.h"
208c2ecf20Sopenharmony_ci#include "main.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/sched.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic struct b43_lo_calib *b43_find_lo_calib(struct b43_txpower_lo_control *lo,
288c2ecf20Sopenharmony_ci					      const struct b43_bbatt *bbatt,
298c2ecf20Sopenharmony_ci					       const struct b43_rfatt *rfatt)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct b43_lo_calib *c;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	list_for_each_entry(c, &lo->calib_list, list) {
348c2ecf20Sopenharmony_ci		if (!b43_compare_bbatt(&c->bbatt, bbatt))
358c2ecf20Sopenharmony_ci			continue;
368c2ecf20Sopenharmony_ci		if (!b43_compare_rfatt(&c->rfatt, rfatt))
378c2ecf20Sopenharmony_ci			continue;
388c2ecf20Sopenharmony_ci		return c;
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return NULL;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Write the LocalOscillator Control (adjust) value-pair. */
458c2ecf20Sopenharmony_cistatic void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
488c2ecf20Sopenharmony_ci	u16 value;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (B43_DEBUG) {
518c2ecf20Sopenharmony_ci		if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
528c2ecf20Sopenharmony_ci			b43dbg(dev->wl, "Invalid LO control pair "
538c2ecf20Sopenharmony_ci			       "(I: %d, Q: %d)\n", control->i, control->q);
548c2ecf20Sopenharmony_ci			dump_stack();
558c2ecf20Sopenharmony_ci			return;
568c2ecf20Sopenharmony_ci		}
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci	B43_WARN_ON(phy->type != B43_PHYTYPE_G);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	value = (u8) (control->q);
618c2ecf20Sopenharmony_ci	value |= ((u8) (control->i)) << 8;
628c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_LO_CTL, value);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic u16 lo_measure_feedthrough(struct b43_wldev *dev,
668c2ecf20Sopenharmony_ci				  u16 lna, u16 pga, u16 trsw_rx)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
698c2ecf20Sopenharmony_ci	u16 rfover;
708c2ecf20Sopenharmony_ci	u16 feedthrough;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (phy->gmode) {
738c2ecf20Sopenharmony_ci		lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
748c2ecf20Sopenharmony_ci		pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
778c2ecf20Sopenharmony_ci		B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
788c2ecf20Sopenharmony_ci/*FIXME This assertion fails		B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
798c2ecf20Sopenharmony_ci				    B43_PHY_RFOVERVAL_BW));
808c2ecf20Sopenharmony_ci*/
818c2ecf20Sopenharmony_ci		trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		/* Construct the RF Override Value */
848c2ecf20Sopenharmony_ci		rfover = B43_PHY_RFOVERVAL_UNK;
858c2ecf20Sopenharmony_ci		rfover |= pga;
868c2ecf20Sopenharmony_ci		rfover |= lna;
878c2ecf20Sopenharmony_ci		rfover |= trsw_rx;
888c2ecf20Sopenharmony_ci		if ((dev->dev->bus_sprom->boardflags_lo & B43_BFL_EXTLNA)
898c2ecf20Sopenharmony_ci		    && phy->rev > 6)
908c2ecf20Sopenharmony_ci			rfover |= B43_PHY_RFOVERVAL_EXTLNA;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
938c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
948c2ecf20Sopenharmony_ci		udelay(10);
958c2ecf20Sopenharmony_ci		rfover |= B43_PHY_RFOVERVAL_BW_LBW;
968c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
978c2ecf20Sopenharmony_ci		udelay(10);
988c2ecf20Sopenharmony_ci		rfover |= B43_PHY_RFOVERVAL_BW_LPF;
998c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
1008c2ecf20Sopenharmony_ci		udelay(10);
1018c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
1028c2ecf20Sopenharmony_ci	} else {
1038c2ecf20Sopenharmony_ci		pga |= B43_PHY_PGACTL_UNKNOWN;
1048c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, pga);
1058c2ecf20Sopenharmony_ci		udelay(10);
1068c2ecf20Sopenharmony_ci		pga |= B43_PHY_PGACTL_LOWBANDW;
1078c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, pga);
1088c2ecf20Sopenharmony_ci		udelay(10);
1098c2ecf20Sopenharmony_ci		pga |= B43_PHY_PGACTL_LPF;
1108c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, pga);
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci	udelay(21);
1138c2ecf20Sopenharmony_ci	feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* This is a good place to check if we need to relax a bit,
1168c2ecf20Sopenharmony_ci	 * as this is the main function called regularly
1178c2ecf20Sopenharmony_ci	 * in the LO calibration. */
1188c2ecf20Sopenharmony_ci	cond_resched();
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return feedthrough;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* TXCTL Register and Value Table.
1248c2ecf20Sopenharmony_ci * Returns the "TXCTL Register".
1258c2ecf20Sopenharmony_ci * "value" is the "TXCTL Value".
1268c2ecf20Sopenharmony_ci * "pad_mix_gain" is the PAD Mixer Gain.
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_cistatic u16 lo_txctl_register_table(struct b43_wldev *dev,
1298c2ecf20Sopenharmony_ci				   u16 *value, u16 *pad_mix_gain)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
1328c2ecf20Sopenharmony_ci	u16 reg, v, padmix;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_B) {
1358c2ecf20Sopenharmony_ci		v = 0x30;
1368c2ecf20Sopenharmony_ci		if (phy->radio_rev <= 5) {
1378c2ecf20Sopenharmony_ci			reg = 0x43;
1388c2ecf20Sopenharmony_ci			padmix = 0;
1398c2ecf20Sopenharmony_ci		} else {
1408c2ecf20Sopenharmony_ci			reg = 0x52;
1418c2ecf20Sopenharmony_ci			padmix = 5;
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci	} else {
1448c2ecf20Sopenharmony_ci		if (phy->rev >= 2 && phy->radio_rev == 8) {
1458c2ecf20Sopenharmony_ci			reg = 0x43;
1468c2ecf20Sopenharmony_ci			v = 0x10;
1478c2ecf20Sopenharmony_ci			padmix = 2;
1488c2ecf20Sopenharmony_ci		} else {
1498c2ecf20Sopenharmony_ci			reg = 0x52;
1508c2ecf20Sopenharmony_ci			v = 0x30;
1518c2ecf20Sopenharmony_ci			padmix = 5;
1528c2ecf20Sopenharmony_ci		}
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci	if (value)
1558c2ecf20Sopenharmony_ci		*value = v;
1568c2ecf20Sopenharmony_ci	if (pad_mix_gain)
1578c2ecf20Sopenharmony_ci		*pad_mix_gain = padmix;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return reg;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic void lo_measure_txctl_values(struct b43_wldev *dev)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
1658c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
1668c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = gphy->lo_control;
1678c2ecf20Sopenharmony_ci	u16 reg, mask;
1688c2ecf20Sopenharmony_ci	u16 trsw_rx, pga;
1698c2ecf20Sopenharmony_ci	u16 radio_pctl_reg;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	static const u8 tx_bias_values[] = {
1728c2ecf20Sopenharmony_ci		0x09, 0x08, 0x0A, 0x01, 0x00,
1738c2ecf20Sopenharmony_ci		0x02, 0x05, 0x04, 0x06,
1748c2ecf20Sopenharmony_ci	};
1758c2ecf20Sopenharmony_ci	static const u8 tx_magn_values[] = {
1768c2ecf20Sopenharmony_ci		0x70, 0x40,
1778c2ecf20Sopenharmony_ci	};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (!has_loopback_gain(phy)) {
1808c2ecf20Sopenharmony_ci		radio_pctl_reg = 6;
1818c2ecf20Sopenharmony_ci		trsw_rx = 2;
1828c2ecf20Sopenharmony_ci		pga = 0;
1838c2ecf20Sopenharmony_ci	} else {
1848c2ecf20Sopenharmony_ci		int lb_gain;	/* Loopback gain (in dB) */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci		trsw_rx = 0;
1878c2ecf20Sopenharmony_ci		lb_gain = gphy->max_lb_gain / 2;
1888c2ecf20Sopenharmony_ci		if (lb_gain > 10) {
1898c2ecf20Sopenharmony_ci			radio_pctl_reg = 0;
1908c2ecf20Sopenharmony_ci			pga = abs(10 - lb_gain) / 6;
1918c2ecf20Sopenharmony_ci			pga = clamp_val(pga, 0, 15);
1928c2ecf20Sopenharmony_ci		} else {
1938c2ecf20Sopenharmony_ci			int cmp_val;
1948c2ecf20Sopenharmony_ci			int tmp;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci			pga = 0;
1978c2ecf20Sopenharmony_ci			cmp_val = 0x24;
1988c2ecf20Sopenharmony_ci			if ((phy->rev >= 2) &&
1998c2ecf20Sopenharmony_ci			    (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
2008c2ecf20Sopenharmony_ci				cmp_val = 0x3C;
2018c2ecf20Sopenharmony_ci			tmp = lb_gain;
2028c2ecf20Sopenharmony_ci			if ((10 - lb_gain) < cmp_val)
2038c2ecf20Sopenharmony_ci				tmp = (10 - lb_gain);
2048c2ecf20Sopenharmony_ci			if (tmp < 0)
2058c2ecf20Sopenharmony_ci				tmp += 6;
2068c2ecf20Sopenharmony_ci			else
2078c2ecf20Sopenharmony_ci				tmp += 3;
2088c2ecf20Sopenharmony_ci			cmp_val /= 4;
2098c2ecf20Sopenharmony_ci			tmp /= 4;
2108c2ecf20Sopenharmony_ci			if (tmp >= cmp_val)
2118c2ecf20Sopenharmony_ci				radio_pctl_reg = cmp_val;
2128c2ecf20Sopenharmony_ci			else
2138c2ecf20Sopenharmony_ci				radio_pctl_reg = tmp;
2148c2ecf20Sopenharmony_ci		}
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci	b43_radio_maskset(dev, 0x43, 0xFFF0, radio_pctl_reg);
2178c2ecf20Sopenharmony_ci	b43_gphy_set_baseband_attenuation(dev, 2);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	reg = lo_txctl_register_table(dev, &mask, NULL);
2208c2ecf20Sopenharmony_ci	mask = ~mask;
2218c2ecf20Sopenharmony_ci	b43_radio_mask(dev, reg, mask);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (has_tx_magnification(phy)) {
2248c2ecf20Sopenharmony_ci		int i, j;
2258c2ecf20Sopenharmony_ci		int feedthrough;
2268c2ecf20Sopenharmony_ci		int min_feedth = 0xFFFF;
2278c2ecf20Sopenharmony_ci		u8 tx_magn, tx_bias;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
2308c2ecf20Sopenharmony_ci			tx_magn = tx_magn_values[i];
2318c2ecf20Sopenharmony_ci			b43_radio_maskset(dev, 0x52, 0xFF0F, tx_magn);
2328c2ecf20Sopenharmony_ci			for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
2338c2ecf20Sopenharmony_ci				tx_bias = tx_bias_values[j];
2348c2ecf20Sopenharmony_ci				b43_radio_maskset(dev, 0x52, 0xFFF0, tx_bias);
2358c2ecf20Sopenharmony_ci				feedthrough =
2368c2ecf20Sopenharmony_ci				    lo_measure_feedthrough(dev, 0, pga,
2378c2ecf20Sopenharmony_ci							   trsw_rx);
2388c2ecf20Sopenharmony_ci				if (feedthrough < min_feedth) {
2398c2ecf20Sopenharmony_ci					lo->tx_bias = tx_bias;
2408c2ecf20Sopenharmony_ci					lo->tx_magn = tx_magn;
2418c2ecf20Sopenharmony_ci					min_feedth = feedthrough;
2428c2ecf20Sopenharmony_ci				}
2438c2ecf20Sopenharmony_ci				if (lo->tx_bias == 0)
2448c2ecf20Sopenharmony_ci					break;
2458c2ecf20Sopenharmony_ci			}
2468c2ecf20Sopenharmony_ci			b43_radio_write16(dev, 0x52,
2478c2ecf20Sopenharmony_ci					  (b43_radio_read16(dev, 0x52)
2488c2ecf20Sopenharmony_ci					   & 0xFF00) | lo->tx_bias | lo->
2498c2ecf20Sopenharmony_ci					  tx_magn);
2508c2ecf20Sopenharmony_ci		}
2518c2ecf20Sopenharmony_ci	} else {
2528c2ecf20Sopenharmony_ci		lo->tx_magn = 0;
2538c2ecf20Sopenharmony_ci		lo->tx_bias = 0;
2548c2ecf20Sopenharmony_ci		b43_radio_mask(dev, 0x52, 0xFFF0);	/* TX bias == 0 */
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	lo->txctl_measured_time = jiffies;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic void lo_read_power_vector(struct b43_wldev *dev)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
2628c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
2638c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = gphy->lo_control;
2648c2ecf20Sopenharmony_ci	int i;
2658c2ecf20Sopenharmony_ci	u64 tmp;
2668c2ecf20Sopenharmony_ci	u64 power_vector = 0;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i += 2) {
2698c2ecf20Sopenharmony_ci		tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
2708c2ecf20Sopenharmony_ci		power_vector |= (tmp << (i * 8));
2718c2ecf20Sopenharmony_ci		/* Clear the vector on the device. */
2728c2ecf20Sopenharmony_ci		b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci	if (power_vector)
2758c2ecf20Sopenharmony_ci		lo->power_vector = power_vector;
2768c2ecf20Sopenharmony_ci	lo->pwr_vec_read_time = jiffies;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci/* 802.11/LO/GPHY/MeasuringGains */
2808c2ecf20Sopenharmony_cistatic void lo_measure_gain_values(struct b43_wldev *dev,
2818c2ecf20Sopenharmony_ci				   s16 max_rx_gain, int use_trsw_rx)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
2848c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
2858c2ecf20Sopenharmony_ci	u16 tmp;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (max_rx_gain < 0)
2888c2ecf20Sopenharmony_ci		max_rx_gain = 0;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (has_loopback_gain(phy)) {
2918c2ecf20Sopenharmony_ci		int trsw_rx_gain;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		if (use_trsw_rx) {
2948c2ecf20Sopenharmony_ci			trsw_rx_gain = gphy->trsw_rx_gain / 2;
2958c2ecf20Sopenharmony_ci			if (max_rx_gain >= trsw_rx_gain) {
2968c2ecf20Sopenharmony_ci				trsw_rx_gain = max_rx_gain - trsw_rx_gain;
2978c2ecf20Sopenharmony_ci			}
2988c2ecf20Sopenharmony_ci		} else
2998c2ecf20Sopenharmony_ci			trsw_rx_gain = max_rx_gain;
3008c2ecf20Sopenharmony_ci		if (trsw_rx_gain < 9) {
3018c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 0;
3028c2ecf20Sopenharmony_ci		} else {
3038c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 1;
3048c2ecf20Sopenharmony_ci			trsw_rx_gain -= 8;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci		trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
3078c2ecf20Sopenharmony_ci		gphy->pga_gain = trsw_rx_gain / 3;
3088c2ecf20Sopenharmony_ci		if (gphy->pga_gain >= 5) {
3098c2ecf20Sopenharmony_ci			gphy->pga_gain -= 5;
3108c2ecf20Sopenharmony_ci			gphy->lna_gain = 2;
3118c2ecf20Sopenharmony_ci		} else
3128c2ecf20Sopenharmony_ci			gphy->lna_gain = 0;
3138c2ecf20Sopenharmony_ci	} else {
3148c2ecf20Sopenharmony_ci		gphy->lna_gain = 0;
3158c2ecf20Sopenharmony_ci		gphy->trsw_rx_gain = 0x20;
3168c2ecf20Sopenharmony_ci		if (max_rx_gain >= 0x14) {
3178c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 1;
3188c2ecf20Sopenharmony_ci			gphy->pga_gain = 2;
3198c2ecf20Sopenharmony_ci		} else if (max_rx_gain >= 0x12) {
3208c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 1;
3218c2ecf20Sopenharmony_ci			gphy->pga_gain = 1;
3228c2ecf20Sopenharmony_ci		} else if (max_rx_gain >= 0xF) {
3238c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 1;
3248c2ecf20Sopenharmony_ci			gphy->pga_gain = 0;
3258c2ecf20Sopenharmony_ci		} else {
3268c2ecf20Sopenharmony_ci			gphy->lna_lod_gain = 0;
3278c2ecf20Sopenharmony_ci			gphy->pga_gain = 0;
3288c2ecf20Sopenharmony_ci		}
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	tmp = b43_radio_read16(dev, 0x7A);
3328c2ecf20Sopenharmony_ci	if (gphy->lna_lod_gain == 0)
3338c2ecf20Sopenharmony_ci		tmp &= ~0x0008;
3348c2ecf20Sopenharmony_ci	else
3358c2ecf20Sopenharmony_ci		tmp |= 0x0008;
3368c2ecf20Sopenharmony_ci	b43_radio_write16(dev, 0x7A, tmp);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistruct lo_g_saved_values {
3408c2ecf20Sopenharmony_ci	u8 old_channel;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	/* Core registers */
3438c2ecf20Sopenharmony_ci	u16 reg_3F4;
3448c2ecf20Sopenharmony_ci	u16 reg_3E2;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* PHY registers */
3478c2ecf20Sopenharmony_ci	u16 phy_lo_mask;
3488c2ecf20Sopenharmony_ci	u16 phy_extg_01;
3498c2ecf20Sopenharmony_ci	u16 phy_dacctl_hwpctl;
3508c2ecf20Sopenharmony_ci	u16 phy_dacctl;
3518c2ecf20Sopenharmony_ci	u16 phy_cck_14;
3528c2ecf20Sopenharmony_ci	u16 phy_hpwr_tssictl;
3538c2ecf20Sopenharmony_ci	u16 phy_analogover;
3548c2ecf20Sopenharmony_ci	u16 phy_analogoverval;
3558c2ecf20Sopenharmony_ci	u16 phy_rfover;
3568c2ecf20Sopenharmony_ci	u16 phy_rfoverval;
3578c2ecf20Sopenharmony_ci	u16 phy_classctl;
3588c2ecf20Sopenharmony_ci	u16 phy_cck_3E;
3598c2ecf20Sopenharmony_ci	u16 phy_crs0;
3608c2ecf20Sopenharmony_ci	u16 phy_pgactl;
3618c2ecf20Sopenharmony_ci	u16 phy_cck_2A;
3628c2ecf20Sopenharmony_ci	u16 phy_syncctl;
3638c2ecf20Sopenharmony_ci	u16 phy_cck_30;
3648c2ecf20Sopenharmony_ci	u16 phy_cck_06;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* Radio registers */
3678c2ecf20Sopenharmony_ci	u16 radio_43;
3688c2ecf20Sopenharmony_ci	u16 radio_7A;
3698c2ecf20Sopenharmony_ci	u16 radio_52;
3708c2ecf20Sopenharmony_ci};
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic void lo_measure_setup(struct b43_wldev *dev,
3738c2ecf20Sopenharmony_ci			     struct lo_g_saved_values *sav)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct ssb_sprom *sprom = dev->dev->bus_sprom;
3768c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
3778c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
3788c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = gphy->lo_control;
3798c2ecf20Sopenharmony_ci	u16 tmp;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (b43_has_hardware_pctl(dev)) {
3828c2ecf20Sopenharmony_ci		sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
3838c2ecf20Sopenharmony_ci		sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
3848c2ecf20Sopenharmony_ci		sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
3858c2ecf20Sopenharmony_ci		sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
3868c2ecf20Sopenharmony_ci		sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
3898c2ecf20Sopenharmony_ci		b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
3908c2ecf20Sopenharmony_ci		b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
3918c2ecf20Sopenharmony_ci		b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_B &&
3948c2ecf20Sopenharmony_ci	    phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
3958c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
3968c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci	if (phy->rev >= 2) {
3998c2ecf20Sopenharmony_ci		sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
4008c2ecf20Sopenharmony_ci		sav->phy_analogoverval =
4018c2ecf20Sopenharmony_ci		    b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
4028c2ecf20Sopenharmony_ci		sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
4038c2ecf20Sopenharmony_ci		sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
4048c2ecf20Sopenharmony_ci		sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
4058c2ecf20Sopenharmony_ci		sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
4068c2ecf20Sopenharmony_ci		sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
4098c2ecf20Sopenharmony_ci		b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
4108c2ecf20Sopenharmony_ci		b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
4118c2ecf20Sopenharmony_ci		b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
4128c2ecf20Sopenharmony_ci		if (phy->type == B43_PHYTYPE_G) {
4138c2ecf20Sopenharmony_ci			if ((phy->rev >= 7) &&
4148c2ecf20Sopenharmony_ci			    (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
4158c2ecf20Sopenharmony_ci				b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
4168c2ecf20Sopenharmony_ci			} else {
4178c2ecf20Sopenharmony_ci				b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
4188c2ecf20Sopenharmony_ci			}
4198c2ecf20Sopenharmony_ci		} else {
4208c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_RFOVER, 0);
4218c2ecf20Sopenharmony_ci		}
4228c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci	sav->reg_3F4 = b43_read16(dev, 0x3F4);
4258c2ecf20Sopenharmony_ci	sav->reg_3E2 = b43_read16(dev, 0x3E2);
4268c2ecf20Sopenharmony_ci	sav->radio_43 = b43_radio_read16(dev, 0x43);
4278c2ecf20Sopenharmony_ci	sav->radio_7A = b43_radio_read16(dev, 0x7A);
4288c2ecf20Sopenharmony_ci	sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
4298c2ecf20Sopenharmony_ci	sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
4308c2ecf20Sopenharmony_ci	sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
4318c2ecf20Sopenharmony_ci	sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (!has_tx_magnification(phy)) {
4348c2ecf20Sopenharmony_ci		sav->radio_52 = b43_radio_read16(dev, 0x52);
4358c2ecf20Sopenharmony_ci		sav->radio_52 &= 0x00F0;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_B) {
4388c2ecf20Sopenharmony_ci		sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
4398c2ecf20Sopenharmony_ci		sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
4408c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
4418c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
4428c2ecf20Sopenharmony_ci	} else {
4438c2ecf20Sopenharmony_ci		b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
4448c2ecf20Sopenharmony_ci			    | 0x8000);
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci	b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
4478c2ecf20Sopenharmony_ci		    & 0xF000);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	tmp =
4508c2ecf20Sopenharmony_ci	    (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
4518c2ecf20Sopenharmony_ci	b43_phy_write(dev, tmp, 0x007F);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	tmp = sav->phy_syncctl;
4548c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
4558c2ecf20Sopenharmony_ci	tmp = sav->radio_7A;
4568c2ecf20Sopenharmony_ci	b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
4598c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_G ||
4608c2ecf20Sopenharmony_ci	    (phy->type == B43_PHYTYPE_B &&
4618c2ecf20Sopenharmony_ci	     phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
4628c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
4638c2ecf20Sopenharmony_ci	} else
4648c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
4658c2ecf20Sopenharmony_ci	if (phy->rev >= 2)
4668c2ecf20Sopenharmony_ci		b43_dummy_transmission(dev, false, true);
4678c2ecf20Sopenharmony_ci	b43_gphy_channel_switch(dev, 6, 0);
4688c2ecf20Sopenharmony_ci	b43_radio_read16(dev, 0x51);	/* dummy read */
4698c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_G)
4708c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Re-measure the txctl values, if needed. */
4738c2ecf20Sopenharmony_ci	if (time_before(lo->txctl_measured_time,
4748c2ecf20Sopenharmony_ci			jiffies - B43_LO_TXCTL_EXPIRE))
4758c2ecf20Sopenharmony_ci		lo_measure_txctl_values(dev);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
4788c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
4798c2ecf20Sopenharmony_ci	} else {
4808c2ecf20Sopenharmony_ci		if (phy->type == B43_PHYTYPE_B)
4818c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
4828c2ecf20Sopenharmony_ci		else
4838c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic void lo_measure_restore(struct b43_wldev *dev,
4888c2ecf20Sopenharmony_ci			       struct lo_g_saved_values *sav)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
4918c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
4928c2ecf20Sopenharmony_ci	u16 tmp;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if (phy->rev >= 2) {
4958c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
4968c2ecf20Sopenharmony_ci		tmp = (gphy->pga_gain << 8);
4978c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
4988c2ecf20Sopenharmony_ci		udelay(5);
4998c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
5008c2ecf20Sopenharmony_ci		udelay(2);
5018c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
5028c2ecf20Sopenharmony_ci	} else {
5038c2ecf20Sopenharmony_ci		tmp = (gphy->pga_gain | 0xEFA0);
5048c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_PGACTL, tmp);
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_G) {
5078c2ecf20Sopenharmony_ci		if (phy->rev >= 3)
5088c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
5098c2ecf20Sopenharmony_ci		else
5108c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
5118c2ecf20Sopenharmony_ci		if (phy->rev >= 2)
5128c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
5138c2ecf20Sopenharmony_ci		else
5148c2ecf20Sopenharmony_ci			b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci	b43_write16(dev, 0x3F4, sav->reg_3F4);
5178c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
5188c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
5198c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
5208c2ecf20Sopenharmony_ci	b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
5218c2ecf20Sopenharmony_ci	b43_radio_write16(dev, 0x43, sav->radio_43);
5228c2ecf20Sopenharmony_ci	b43_radio_write16(dev, 0x7A, sav->radio_7A);
5238c2ecf20Sopenharmony_ci	if (!has_tx_magnification(phy)) {
5248c2ecf20Sopenharmony_ci		tmp = sav->radio_52;
5258c2ecf20Sopenharmony_ci		b43_radio_maskset(dev, 0x52, 0xFF0F, tmp);
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci	b43_write16(dev, 0x3E2, sav->reg_3E2);
5288c2ecf20Sopenharmony_ci	if (phy->type == B43_PHYTYPE_B &&
5298c2ecf20Sopenharmony_ci	    phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
5308c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
5318c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci	if (phy->rev >= 2) {
5348c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
5358c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
5368c2ecf20Sopenharmony_ci			      sav->phy_analogoverval);
5378c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
5388c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
5398c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
5408c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
5418c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	if (b43_has_hardware_pctl(dev)) {
5448c2ecf20Sopenharmony_ci		tmp = (sav->phy_lo_mask & 0xBFFF);
5458c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
5468c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
5478c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
5488c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
5498c2ecf20Sopenharmony_ci		b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci	b43_gphy_channel_switch(dev, sav->old_channel, 1);
5528c2ecf20Sopenharmony_ci}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_cistruct b43_lo_g_statemachine {
5558c2ecf20Sopenharmony_ci	int current_state;
5568c2ecf20Sopenharmony_ci	int nr_measured;
5578c2ecf20Sopenharmony_ci	int state_val_multiplier;
5588c2ecf20Sopenharmony_ci	u16 lowest_feedth;
5598c2ecf20Sopenharmony_ci	struct b43_loctl min_loctl;
5608c2ecf20Sopenharmony_ci};
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci/* Loop over each possible value in this state. */
5638c2ecf20Sopenharmony_cistatic int lo_probe_possible_loctls(struct b43_wldev *dev,
5648c2ecf20Sopenharmony_ci				    struct b43_loctl *probe_loctl,
5658c2ecf20Sopenharmony_ci				    struct b43_lo_g_statemachine *d)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
5688c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
5698c2ecf20Sopenharmony_ci	struct b43_loctl test_loctl;
5708c2ecf20Sopenharmony_ci	struct b43_loctl orig_loctl;
5718c2ecf20Sopenharmony_ci	struct b43_loctl prev_loctl = {
5728c2ecf20Sopenharmony_ci		.i = -100,
5738c2ecf20Sopenharmony_ci		.q = -100,
5748c2ecf20Sopenharmony_ci	};
5758c2ecf20Sopenharmony_ci	int i;
5768c2ecf20Sopenharmony_ci	int begin, end;
5778c2ecf20Sopenharmony_ci	int found_lower = 0;
5788c2ecf20Sopenharmony_ci	u16 feedth;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	static const struct b43_loctl modifiers[] = {
5818c2ecf20Sopenharmony_ci		{.i = 1,.q = 1,},
5828c2ecf20Sopenharmony_ci		{.i = 1,.q = 0,},
5838c2ecf20Sopenharmony_ci		{.i = 1,.q = -1,},
5848c2ecf20Sopenharmony_ci		{.i = 0,.q = -1,},
5858c2ecf20Sopenharmony_ci		{.i = -1,.q = -1,},
5868c2ecf20Sopenharmony_ci		{.i = -1,.q = 0,},
5878c2ecf20Sopenharmony_ci		{.i = -1,.q = 1,},
5888c2ecf20Sopenharmony_ci		{.i = 0,.q = 1,},
5898c2ecf20Sopenharmony_ci	};
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	if (d->current_state == 0) {
5928c2ecf20Sopenharmony_ci		begin = 1;
5938c2ecf20Sopenharmony_ci		end = 8;
5948c2ecf20Sopenharmony_ci	} else if (d->current_state % 2 == 0) {
5958c2ecf20Sopenharmony_ci		begin = d->current_state - 1;
5968c2ecf20Sopenharmony_ci		end = d->current_state + 1;
5978c2ecf20Sopenharmony_ci	} else {
5988c2ecf20Sopenharmony_ci		begin = d->current_state - 2;
5998c2ecf20Sopenharmony_ci		end = d->current_state + 2;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci	if (begin < 1)
6028c2ecf20Sopenharmony_ci		begin += 8;
6038c2ecf20Sopenharmony_ci	if (end > 8)
6048c2ecf20Sopenharmony_ci		end -= 8;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
6078c2ecf20Sopenharmony_ci	i = begin;
6088c2ecf20Sopenharmony_ci	d->current_state = i;
6098c2ecf20Sopenharmony_ci	while (1) {
6108c2ecf20Sopenharmony_ci		B43_WARN_ON(!(i >= 1 && i <= 8));
6118c2ecf20Sopenharmony_ci		memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
6128c2ecf20Sopenharmony_ci		test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
6138c2ecf20Sopenharmony_ci		test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
6148c2ecf20Sopenharmony_ci		if ((test_loctl.i != prev_loctl.i ||
6158c2ecf20Sopenharmony_ci		     test_loctl.q != prev_loctl.q) &&
6168c2ecf20Sopenharmony_ci		    (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
6178c2ecf20Sopenharmony_ci			b43_lo_write(dev, &test_loctl);
6188c2ecf20Sopenharmony_ci			feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
6198c2ecf20Sopenharmony_ci							gphy->pga_gain,
6208c2ecf20Sopenharmony_ci							gphy->trsw_rx_gain);
6218c2ecf20Sopenharmony_ci			if (feedth < d->lowest_feedth) {
6228c2ecf20Sopenharmony_ci				memcpy(probe_loctl, &test_loctl,
6238c2ecf20Sopenharmony_ci				       sizeof(struct b43_loctl));
6248c2ecf20Sopenharmony_ci				found_lower = 1;
6258c2ecf20Sopenharmony_ci				d->lowest_feedth = feedth;
6268c2ecf20Sopenharmony_ci				if ((d->nr_measured < 2) &&
6278c2ecf20Sopenharmony_ci				    !has_loopback_gain(phy))
6288c2ecf20Sopenharmony_ci					break;
6298c2ecf20Sopenharmony_ci			}
6308c2ecf20Sopenharmony_ci		}
6318c2ecf20Sopenharmony_ci		memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
6328c2ecf20Sopenharmony_ci		if (i == end)
6338c2ecf20Sopenharmony_ci			break;
6348c2ecf20Sopenharmony_ci		if (i == 8)
6358c2ecf20Sopenharmony_ci			i = 1;
6368c2ecf20Sopenharmony_ci		else
6378c2ecf20Sopenharmony_ci			i++;
6388c2ecf20Sopenharmony_ci		d->current_state = i;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	return found_lower;
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic void lo_probe_loctls_statemachine(struct b43_wldev *dev,
6458c2ecf20Sopenharmony_ci					 struct b43_loctl *loctl,
6468c2ecf20Sopenharmony_ci					 int *max_rx_gain)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
6498c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
6508c2ecf20Sopenharmony_ci	struct b43_lo_g_statemachine d;
6518c2ecf20Sopenharmony_ci	u16 feedth;
6528c2ecf20Sopenharmony_ci	int found_lower;
6538c2ecf20Sopenharmony_ci	struct b43_loctl probe_loctl;
6548c2ecf20Sopenharmony_ci	int max_repeat = 1, repeat_cnt = 0;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	d.nr_measured = 0;
6578c2ecf20Sopenharmony_ci	d.state_val_multiplier = 1;
6588c2ecf20Sopenharmony_ci	if (has_loopback_gain(phy))
6598c2ecf20Sopenharmony_ci		d.state_val_multiplier = 3;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
6628c2ecf20Sopenharmony_ci	if (has_loopback_gain(phy))
6638c2ecf20Sopenharmony_ci		max_repeat = 4;
6648c2ecf20Sopenharmony_ci	do {
6658c2ecf20Sopenharmony_ci		b43_lo_write(dev, &d.min_loctl);
6668c2ecf20Sopenharmony_ci		feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
6678c2ecf20Sopenharmony_ci						gphy->pga_gain,
6688c2ecf20Sopenharmony_ci						gphy->trsw_rx_gain);
6698c2ecf20Sopenharmony_ci		if (feedth < 0x258) {
6708c2ecf20Sopenharmony_ci			if (feedth >= 0x12C)
6718c2ecf20Sopenharmony_ci				*max_rx_gain += 6;
6728c2ecf20Sopenharmony_ci			else
6738c2ecf20Sopenharmony_ci				*max_rx_gain += 3;
6748c2ecf20Sopenharmony_ci			feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
6758c2ecf20Sopenharmony_ci							gphy->pga_gain,
6768c2ecf20Sopenharmony_ci							gphy->trsw_rx_gain);
6778c2ecf20Sopenharmony_ci		}
6788c2ecf20Sopenharmony_ci		d.lowest_feedth = feedth;
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci		d.current_state = 0;
6818c2ecf20Sopenharmony_ci		do {
6828c2ecf20Sopenharmony_ci			B43_WARN_ON(!
6838c2ecf20Sopenharmony_ci				    (d.current_state >= 0
6848c2ecf20Sopenharmony_ci				     && d.current_state <= 8));
6858c2ecf20Sopenharmony_ci			memcpy(&probe_loctl, &d.min_loctl,
6868c2ecf20Sopenharmony_ci			       sizeof(struct b43_loctl));
6878c2ecf20Sopenharmony_ci			found_lower =
6888c2ecf20Sopenharmony_ci			    lo_probe_possible_loctls(dev, &probe_loctl, &d);
6898c2ecf20Sopenharmony_ci			if (!found_lower)
6908c2ecf20Sopenharmony_ci				break;
6918c2ecf20Sopenharmony_ci			if ((probe_loctl.i == d.min_loctl.i) &&
6928c2ecf20Sopenharmony_ci			    (probe_loctl.q == d.min_loctl.q))
6938c2ecf20Sopenharmony_ci				break;
6948c2ecf20Sopenharmony_ci			memcpy(&d.min_loctl, &probe_loctl,
6958c2ecf20Sopenharmony_ci			       sizeof(struct b43_loctl));
6968c2ecf20Sopenharmony_ci			d.nr_measured++;
6978c2ecf20Sopenharmony_ci		} while (d.nr_measured < 24);
6988c2ecf20Sopenharmony_ci		memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci		if (has_loopback_gain(phy)) {
7018c2ecf20Sopenharmony_ci			if (d.lowest_feedth > 0x1194)
7028c2ecf20Sopenharmony_ci				*max_rx_gain -= 6;
7038c2ecf20Sopenharmony_ci			else if (d.lowest_feedth < 0x5DC)
7048c2ecf20Sopenharmony_ci				*max_rx_gain += 3;
7058c2ecf20Sopenharmony_ci			if (repeat_cnt == 0) {
7068c2ecf20Sopenharmony_ci				if (d.lowest_feedth <= 0x5DC) {
7078c2ecf20Sopenharmony_ci					d.state_val_multiplier = 1;
7088c2ecf20Sopenharmony_ci					repeat_cnt++;
7098c2ecf20Sopenharmony_ci				} else
7108c2ecf20Sopenharmony_ci					d.state_val_multiplier = 2;
7118c2ecf20Sopenharmony_ci			} else if (repeat_cnt == 2)
7128c2ecf20Sopenharmony_ci				d.state_val_multiplier = 1;
7138c2ecf20Sopenharmony_ci		}
7148c2ecf20Sopenharmony_ci		lo_measure_gain_values(dev, *max_rx_gain,
7158c2ecf20Sopenharmony_ci				       has_loopback_gain(phy));
7168c2ecf20Sopenharmony_ci	} while (++repeat_cnt < max_repeat);
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic
7208c2ecf20Sopenharmony_cistruct b43_lo_calib *b43_calibrate_lo_setting(struct b43_wldev *dev,
7218c2ecf20Sopenharmony_ci					      const struct b43_bbatt *bbatt,
7228c2ecf20Sopenharmony_ci					      const struct b43_rfatt *rfatt)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
7258c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
7268c2ecf20Sopenharmony_ci	struct b43_loctl loctl = {
7278c2ecf20Sopenharmony_ci		.i = 0,
7288c2ecf20Sopenharmony_ci		.q = 0,
7298c2ecf20Sopenharmony_ci	};
7308c2ecf20Sopenharmony_ci	int max_rx_gain;
7318c2ecf20Sopenharmony_ci	struct b43_lo_calib *cal;
7328c2ecf20Sopenharmony_ci	struct lo_g_saved_values saved_regs;
7338c2ecf20Sopenharmony_ci	/* Values from the "TXCTL Register and Value Table" */
7348c2ecf20Sopenharmony_ci	u16 txctl_reg;
7358c2ecf20Sopenharmony_ci	u16 txctl_value;
7368c2ecf20Sopenharmony_ci	u16 pad_mix_gain;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	saved_regs.old_channel = phy->channel;
7398c2ecf20Sopenharmony_ci	b43_mac_suspend(dev);
7408c2ecf20Sopenharmony_ci	lo_measure_setup(dev, &saved_regs);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	b43_radio_maskset(dev, 0x43, 0xFFF0, rfatt->att);
7458c2ecf20Sopenharmony_ci	b43_radio_maskset(dev, txctl_reg, ~txctl_value, (rfatt->with_padmix ? txctl_value :0));
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	max_rx_gain = rfatt->att * 2;
7488c2ecf20Sopenharmony_ci	max_rx_gain += bbatt->att / 2;
7498c2ecf20Sopenharmony_ci	if (rfatt->with_padmix)
7508c2ecf20Sopenharmony_ci		max_rx_gain -= pad_mix_gain;
7518c2ecf20Sopenharmony_ci	if (has_loopback_gain(phy))
7528c2ecf20Sopenharmony_ci		max_rx_gain += gphy->max_lb_gain;
7538c2ecf20Sopenharmony_ci	lo_measure_gain_values(dev, max_rx_gain,
7548c2ecf20Sopenharmony_ci			       has_loopback_gain(phy));
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	b43_gphy_set_baseband_attenuation(dev, bbatt->att);
7578c2ecf20Sopenharmony_ci	lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	lo_measure_restore(dev, &saved_regs);
7608c2ecf20Sopenharmony_ci	b43_mac_enable(dev);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	if (b43_debug(dev, B43_DBG_LO)) {
7638c2ecf20Sopenharmony_ci		b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
7648c2ecf20Sopenharmony_ci		       "=> I=%d Q=%d\n",
7658c2ecf20Sopenharmony_ci		       bbatt->att, rfatt->att, rfatt->with_padmix,
7668c2ecf20Sopenharmony_ci		       loctl.i, loctl.q);
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	cal = kmalloc(sizeof(*cal), GFP_KERNEL);
7708c2ecf20Sopenharmony_ci	if (!cal) {
7718c2ecf20Sopenharmony_ci		b43warn(dev->wl, "LO calib: out of memory\n");
7728c2ecf20Sopenharmony_ci		return NULL;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci	memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
7758c2ecf20Sopenharmony_ci	memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
7768c2ecf20Sopenharmony_ci	memcpy(&cal->ctl, &loctl, sizeof(loctl));
7778c2ecf20Sopenharmony_ci	cal->calib_time = jiffies;
7788c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cal->list);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	return cal;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci/* Get a calibrated LO setting for the given attenuation values.
7848c2ecf20Sopenharmony_ci * Might return a NULL pointer under OOM! */
7858c2ecf20Sopenharmony_cistatic
7868c2ecf20Sopenharmony_cistruct b43_lo_calib *b43_get_calib_lo_settings(struct b43_wldev *dev,
7878c2ecf20Sopenharmony_ci					       const struct b43_bbatt *bbatt,
7888c2ecf20Sopenharmony_ci					       const struct b43_rfatt *rfatt)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
7918c2ecf20Sopenharmony_ci	struct b43_lo_calib *c;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	c = b43_find_lo_calib(lo, bbatt, rfatt);
7948c2ecf20Sopenharmony_ci	if (c)
7958c2ecf20Sopenharmony_ci		return c;
7968c2ecf20Sopenharmony_ci	/* Not in the list of calibrated LO settings.
7978c2ecf20Sopenharmony_ci	 * Calibrate it now. */
7988c2ecf20Sopenharmony_ci	c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
7998c2ecf20Sopenharmony_ci	if (!c)
8008c2ecf20Sopenharmony_ci		return NULL;
8018c2ecf20Sopenharmony_ci	list_add(&c->list, &lo->calib_list);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	return c;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_civoid b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
8098c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
8108c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = gphy->lo_control;
8118c2ecf20Sopenharmony_ci	int i;
8128c2ecf20Sopenharmony_ci	int rf_offset, bb_offset;
8138c2ecf20Sopenharmony_ci	const struct b43_rfatt *rfatt;
8148c2ecf20Sopenharmony_ci	const struct b43_bbatt *bbatt;
8158c2ecf20Sopenharmony_ci	u64 power_vector;
8168c2ecf20Sopenharmony_ci	bool table_changed = false;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
8198c2ecf20Sopenharmony_ci	B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	power_vector = lo->power_vector;
8228c2ecf20Sopenharmony_ci	if (!update_all && !power_vector)
8238c2ecf20Sopenharmony_ci		return; /* Nothing to do. */
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	/* Suspend the MAC now to avoid continuous suspend/enable
8268c2ecf20Sopenharmony_ci	 * cycles in the loop. */
8278c2ecf20Sopenharmony_ci	b43_mac_suspend(dev);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
8308c2ecf20Sopenharmony_ci		struct b43_lo_calib *cal;
8318c2ecf20Sopenharmony_ci		int idx;
8328c2ecf20Sopenharmony_ci		u16 val;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci		if (!update_all && !(power_vector & (((u64)1ULL) << i)))
8358c2ecf20Sopenharmony_ci			continue;
8368c2ecf20Sopenharmony_ci		/* Update the table entry for this power_vector bit.
8378c2ecf20Sopenharmony_ci		 * The table rows are RFatt entries and columns are BBatt. */
8388c2ecf20Sopenharmony_ci		bb_offset = i / lo->rfatt_list.len;
8398c2ecf20Sopenharmony_ci		rf_offset = i % lo->rfatt_list.len;
8408c2ecf20Sopenharmony_ci		bbatt = &(lo->bbatt_list.list[bb_offset]);
8418c2ecf20Sopenharmony_ci		rfatt = &(lo->rfatt_list.list[rf_offset]);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci		cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
8448c2ecf20Sopenharmony_ci		if (!cal) {
8458c2ecf20Sopenharmony_ci			b43warn(dev->wl, "LO: Could not "
8468c2ecf20Sopenharmony_ci				"calibrate DC table entry\n");
8478c2ecf20Sopenharmony_ci			continue;
8488c2ecf20Sopenharmony_ci		}
8498c2ecf20Sopenharmony_ci		/*FIXME: Is Q really in the low nibble? */
8508c2ecf20Sopenharmony_ci		val = (u8)(cal->ctl.q);
8518c2ecf20Sopenharmony_ci		val |= ((u8)(cal->ctl.i)) << 4;
8528c2ecf20Sopenharmony_ci		kfree(cal);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci		/* Get the index into the hardware DC LT. */
8558c2ecf20Sopenharmony_ci		idx = i / 2;
8568c2ecf20Sopenharmony_ci		/* Change the table in memory. */
8578c2ecf20Sopenharmony_ci		if (i % 2) {
8588c2ecf20Sopenharmony_ci			/* Change the high byte. */
8598c2ecf20Sopenharmony_ci			lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
8608c2ecf20Sopenharmony_ci					 | ((val & 0x00FF) << 8);
8618c2ecf20Sopenharmony_ci		} else {
8628c2ecf20Sopenharmony_ci			/* Change the low byte. */
8638c2ecf20Sopenharmony_ci			lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
8648c2ecf20Sopenharmony_ci					 | (val & 0x00FF);
8658c2ecf20Sopenharmony_ci		}
8668c2ecf20Sopenharmony_ci		table_changed = true;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci	if (table_changed) {
8698c2ecf20Sopenharmony_ci		/* The table changed in memory. Update the hardware table. */
8708c2ecf20Sopenharmony_ci		for (i = 0; i < B43_DC_LT_SIZE; i++)
8718c2ecf20Sopenharmony_ci			b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci	b43_mac_enable(dev);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci/* Fixup the RF attenuation value for the case where we are
8778c2ecf20Sopenharmony_ci * using the PAD mixer. */
8788c2ecf20Sopenharmony_cistatic inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
8798c2ecf20Sopenharmony_ci{
8808c2ecf20Sopenharmony_ci	if (!rf->with_padmix)
8818c2ecf20Sopenharmony_ci		return;
8828c2ecf20Sopenharmony_ci	if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
8838c2ecf20Sopenharmony_ci		rf->att = 4;
8848c2ecf20Sopenharmony_ci}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_civoid b43_lo_g_adjust(struct b43_wldev *dev)
8878c2ecf20Sopenharmony_ci{
8888c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = dev->phy.g;
8898c2ecf20Sopenharmony_ci	struct b43_lo_calib *cal;
8908c2ecf20Sopenharmony_ci	struct b43_rfatt rf;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	memcpy(&rf, &gphy->rfatt, sizeof(rf));
8938c2ecf20Sopenharmony_ci	b43_lo_fixup_rfatt(&rf);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
8968c2ecf20Sopenharmony_ci	if (!cal)
8978c2ecf20Sopenharmony_ci		return;
8988c2ecf20Sopenharmony_ci	b43_lo_write(dev, &cal->ctl);
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_civoid b43_lo_g_adjust_to(struct b43_wldev *dev,
9028c2ecf20Sopenharmony_ci			u16 rfatt, u16 bbatt, u16 tx_control)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	struct b43_rfatt rf;
9058c2ecf20Sopenharmony_ci	struct b43_bbatt bb;
9068c2ecf20Sopenharmony_ci	struct b43_lo_calib *cal;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	memset(&rf, 0, sizeof(rf));
9098c2ecf20Sopenharmony_ci	memset(&bb, 0, sizeof(bb));
9108c2ecf20Sopenharmony_ci	rf.att = rfatt;
9118c2ecf20Sopenharmony_ci	bb.att = bbatt;
9128c2ecf20Sopenharmony_ci	b43_lo_fixup_rfatt(&rf);
9138c2ecf20Sopenharmony_ci	cal = b43_get_calib_lo_settings(dev, &bb, &rf);
9148c2ecf20Sopenharmony_ci	if (!cal)
9158c2ecf20Sopenharmony_ci		return;
9168c2ecf20Sopenharmony_ci	b43_lo_write(dev, &cal->ctl);
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci/* Periodic LO maintenance work */
9208c2ecf20Sopenharmony_civoid b43_lo_g_maintenance_work(struct b43_wldev *dev)
9218c2ecf20Sopenharmony_ci{
9228c2ecf20Sopenharmony_ci	struct b43_phy *phy = &dev->phy;
9238c2ecf20Sopenharmony_ci	struct b43_phy_g *gphy = phy->g;
9248c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = gphy->lo_control;
9258c2ecf20Sopenharmony_ci	unsigned long now;
9268c2ecf20Sopenharmony_ci	unsigned long expire;
9278c2ecf20Sopenharmony_ci	struct b43_lo_calib *cal, *tmp;
9288c2ecf20Sopenharmony_ci	bool current_item_expired = false;
9298c2ecf20Sopenharmony_ci	bool hwpctl;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	if (!lo)
9328c2ecf20Sopenharmony_ci		return;
9338c2ecf20Sopenharmony_ci	now = jiffies;
9348c2ecf20Sopenharmony_ci	hwpctl = b43_has_hardware_pctl(dev);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (hwpctl) {
9378c2ecf20Sopenharmony_ci		/* Read the power vector and update it, if needed. */
9388c2ecf20Sopenharmony_ci		expire = now - B43_LO_PWRVEC_EXPIRE;
9398c2ecf20Sopenharmony_ci		if (time_before(lo->pwr_vec_read_time, expire)) {
9408c2ecf20Sopenharmony_ci			lo_read_power_vector(dev);
9418c2ecf20Sopenharmony_ci			b43_gphy_dc_lt_init(dev, 0);
9428c2ecf20Sopenharmony_ci		}
9438c2ecf20Sopenharmony_ci		//FIXME Recalc the whole DC table from time to time?
9448c2ecf20Sopenharmony_ci	}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	if (hwpctl)
9478c2ecf20Sopenharmony_ci		return;
9488c2ecf20Sopenharmony_ci	/* Search for expired LO settings. Remove them.
9498c2ecf20Sopenharmony_ci	 * Recalibrate the current setting, if expired. */
9508c2ecf20Sopenharmony_ci	expire = now - B43_LO_CALIB_EXPIRE;
9518c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
9528c2ecf20Sopenharmony_ci		if (!time_before(cal->calib_time, expire))
9538c2ecf20Sopenharmony_ci			continue;
9548c2ecf20Sopenharmony_ci		/* This item expired. */
9558c2ecf20Sopenharmony_ci		if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
9568c2ecf20Sopenharmony_ci		    b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
9578c2ecf20Sopenharmony_ci			B43_WARN_ON(current_item_expired);
9588c2ecf20Sopenharmony_ci			current_item_expired = true;
9598c2ecf20Sopenharmony_ci		}
9608c2ecf20Sopenharmony_ci		if (b43_debug(dev, B43_DBG_LO)) {
9618c2ecf20Sopenharmony_ci			b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
9628c2ecf20Sopenharmony_ci			       "I=%d, Q=%d expired\n",
9638c2ecf20Sopenharmony_ci			       cal->bbatt.att, cal->rfatt.att,
9648c2ecf20Sopenharmony_ci			       cal->rfatt.with_padmix,
9658c2ecf20Sopenharmony_ci			       cal->ctl.i, cal->ctl.q);
9668c2ecf20Sopenharmony_ci		}
9678c2ecf20Sopenharmony_ci		list_del(&cal->list);
9688c2ecf20Sopenharmony_ci		kfree(cal);
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci	if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
9718c2ecf20Sopenharmony_ci		/* Recalibrate currently used LO setting. */
9728c2ecf20Sopenharmony_ci		if (b43_debug(dev, B43_DBG_LO))
9738c2ecf20Sopenharmony_ci			b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
9748c2ecf20Sopenharmony_ci		cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
9758c2ecf20Sopenharmony_ci		if (cal) {
9768c2ecf20Sopenharmony_ci			list_add(&cal->list, &lo->calib_list);
9778c2ecf20Sopenharmony_ci			b43_lo_write(dev, &cal->ctl);
9788c2ecf20Sopenharmony_ci		} else
9798c2ecf20Sopenharmony_ci			b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_civoid b43_lo_g_cleanup(struct b43_wldev *dev)
9848c2ecf20Sopenharmony_ci{
9858c2ecf20Sopenharmony_ci	struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
9868c2ecf20Sopenharmony_ci	struct b43_lo_calib *cal, *tmp;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	if (!lo)
9898c2ecf20Sopenharmony_ci		return;
9908c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
9918c2ecf20Sopenharmony_ci		list_del(&cal->list);
9928c2ecf20Sopenharmony_ci		kfree(cal);
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci/* LO Initialization */
9978c2ecf20Sopenharmony_civoid b43_lo_g_init(struct b43_wldev *dev)
9988c2ecf20Sopenharmony_ci{
9998c2ecf20Sopenharmony_ci	if (b43_has_hardware_pctl(dev)) {
10008c2ecf20Sopenharmony_ci		lo_read_power_vector(dev);
10018c2ecf20Sopenharmony_ci		b43_gphy_dc_lt_init(dev, 1);
10028c2ecf20Sopenharmony_ci	}
10038c2ecf20Sopenharmony_ci}
1004