blob: 4edee6da2b8bdd3e13c56b1eb38527ac2bd7ed8a [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 G PHY LO (LocalOscillator) Measuring and Control routines
6
7 Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
Stefano Brivio1f21ad22007-11-06 22:49:20 +01008 Copyright (c) 2005, 2006 Stefano Brivio <stefano.brivio@polimi.it>
Michael Buesche4d6b792007-09-18 15:39:42 -04009 Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
10 Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
26 Boston, MA 02110-1301, USA.
27
28*/
29
30#include "b43.h"
31#include "lo.h"
32#include "phy.h"
33#include "main.h"
34
35#include <linux/delay.h>
36#include <linux/sched.h>
37
38
Michael Bueschf5eda472008-04-20 16:03:32 +020039static struct b43_lo_calib * b43_find_lo_calib(struct b43_txpower_lo_control *lo,
40 const struct b43_bbatt *bbatt,
41 const struct b43_rfatt *rfatt)
42{
43 struct b43_lo_calib *c;
Michael Buesche4d6b792007-09-18 15:39:42 -040044
Michael Bueschf5eda472008-04-20 16:03:32 +020045 list_for_each_entry(c, &lo->calib_list, list) {
46 if (!b43_compare_bbatt(&c->bbatt, bbatt))
47 continue;
48 if (!b43_compare_rfatt(&c->rfatt, rfatt))
49 continue;
50 return c;
51 }
52
53 return NULL;
54}
Michael Buesche4d6b792007-09-18 15:39:42 -040055
56/* Write the LocalOscillator Control (adjust) value-pair. */
57static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
58{
59 struct b43_phy *phy = &dev->phy;
60 u16 value;
61 u16 reg;
62
63 if (B43_DEBUG) {
64 if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
65 b43dbg(dev->wl, "Invalid LO control pair "
66 "(I: %d, Q: %d)\n", control->i, control->q);
67 dump_stack();
68 return;
69 }
70 }
71
72 value = (u8) (control->q);
73 value |= ((u8) (control->i)) << 8;
74
75 reg = (phy->type == B43_PHYTYPE_B) ? 0x002F : B43_PHY_LO_CTL;
76 b43_phy_write(dev, reg, value);
77}
78
Michael Buesche4d6b792007-09-18 15:39:42 -040079static u16 lo_measure_feedthrough(struct b43_wldev *dev,
80 u16 lna, u16 pga, u16 trsw_rx)
81{
82 struct b43_phy *phy = &dev->phy;
83 u16 rfover;
84 u16 feedthrough;
85
86 if (phy->gmode) {
87 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
88 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
89
90 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
91 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
92/*FIXME This assertion fails B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
93 B43_PHY_RFOVERVAL_BW));
94*/
95 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
96
97 /* Construct the RF Override Value */
98 rfover = B43_PHY_RFOVERVAL_UNK;
99 rfover |= pga;
100 rfover |= lna;
101 rfover |= trsw_rx;
Larry Finger95de2842007-11-09 16:57:18 -0600102 if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
103 && phy->rev > 6)
Michael Buesche4d6b792007-09-18 15:39:42 -0400104 rfover |= B43_PHY_RFOVERVAL_EXTLNA;
105
106 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
107 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
108 udelay(10);
109 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
110 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
111 udelay(10);
112 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
113 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
114 udelay(10);
115 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
116 } else {
117 pga |= B43_PHY_PGACTL_UNKNOWN;
118 b43_phy_write(dev, B43_PHY_PGACTL, pga);
119 udelay(10);
120 pga |= B43_PHY_PGACTL_LOWBANDW;
121 b43_phy_write(dev, B43_PHY_PGACTL, pga);
122 udelay(10);
123 pga |= B43_PHY_PGACTL_LPF;
124 b43_phy_write(dev, B43_PHY_PGACTL, pga);
125 }
126 udelay(21);
127 feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
128
129 /* This is a good place to check if we need to relax a bit,
130 * as this is the main function called regularly
131 * in the LO calibration. */
132 cond_resched();
133
134 return feedthrough;
135}
136
137/* TXCTL Register and Value Table.
138 * Returns the "TXCTL Register".
139 * "value" is the "TXCTL Value".
140 * "pad_mix_gain" is the PAD Mixer Gain.
141 */
142static u16 lo_txctl_register_table(struct b43_wldev *dev,
143 u16 * value, u16 * pad_mix_gain)
144{
145 struct b43_phy *phy = &dev->phy;
146 u16 reg, v, padmix;
147
148 if (phy->type == B43_PHYTYPE_B) {
149 v = 0x30;
150 if (phy->radio_rev <= 5) {
151 reg = 0x43;
152 padmix = 0;
153 } else {
154 reg = 0x52;
155 padmix = 5;
156 }
157 } else {
158 if (phy->rev >= 2 && phy->radio_rev == 8) {
159 reg = 0x43;
160 v = 0x10;
161 padmix = 2;
162 } else {
163 reg = 0x52;
164 v = 0x30;
165 padmix = 5;
166 }
167 }
168 if (value)
169 *value = v;
170 if (pad_mix_gain)
171 *pad_mix_gain = padmix;
172
173 return reg;
174}
175
176static void lo_measure_txctl_values(struct b43_wldev *dev)
177{
178 struct b43_phy *phy = &dev->phy;
179 struct b43_txpower_lo_control *lo = phy->lo_control;
180 u16 reg, mask;
181 u16 trsw_rx, pga;
182 u16 radio_pctl_reg;
183
184 static const u8 tx_bias_values[] = {
185 0x09, 0x08, 0x0A, 0x01, 0x00,
186 0x02, 0x05, 0x04, 0x06,
187 };
188 static const u8 tx_magn_values[] = {
189 0x70, 0x40,
190 };
191
192 if (!has_loopback_gain(phy)) {
193 radio_pctl_reg = 6;
194 trsw_rx = 2;
195 pga = 0;
196 } else {
197 int lb_gain; /* Loopback gain (in dB) */
198
199 trsw_rx = 0;
200 lb_gain = phy->max_lb_gain / 2;
201 if (lb_gain > 10) {
202 radio_pctl_reg = 0;
203 pga = abs(10 - lb_gain) / 6;
204 pga = limit_value(pga, 0, 15);
205 } else {
206 int cmp_val;
207 int tmp;
208
209 pga = 0;
210 cmp_val = 0x24;
211 if ((phy->rev >= 2) &&
212 (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
213 cmp_val = 0x3C;
214 tmp = lb_gain;
215 if ((10 - lb_gain) < cmp_val)
216 tmp = (10 - lb_gain);
217 if (tmp < 0)
218 tmp += 6;
219 else
220 tmp += 3;
221 cmp_val /= 4;
222 tmp /= 4;
223 if (tmp >= cmp_val)
224 radio_pctl_reg = cmp_val;
225 else
226 radio_pctl_reg = tmp;
227 }
228 }
229 b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
230 & 0xFFF0) | radio_pctl_reg);
231 b43_phy_set_baseband_attenuation(dev, 2);
232
233 reg = lo_txctl_register_table(dev, &mask, NULL);
234 mask = ~mask;
235 b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
236 & mask);
237
238 if (has_tx_magnification(phy)) {
239 int i, j;
240 int feedthrough;
241 int min_feedth = 0xFFFF;
242 u8 tx_magn, tx_bias;
243
244 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
245 tx_magn = tx_magn_values[i];
246 b43_radio_write16(dev, 0x52,
247 (b43_radio_read16(dev, 0x52)
248 & 0xFF0F) | tx_magn);
249 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
250 tx_bias = tx_bias_values[j];
251 b43_radio_write16(dev, 0x52,
252 (b43_radio_read16(dev, 0x52)
253 & 0xFFF0) | tx_bias);
254 feedthrough =
255 lo_measure_feedthrough(dev, 0, pga,
256 trsw_rx);
257 if (feedthrough < min_feedth) {
258 lo->tx_bias = tx_bias;
259 lo->tx_magn = tx_magn;
260 min_feedth = feedthrough;
261 }
262 if (lo->tx_bias == 0)
263 break;
264 }
265 b43_radio_write16(dev, 0x52,
266 (b43_radio_read16(dev, 0x52)
267 & 0xFF00) | lo->tx_bias | lo->
268 tx_magn);
269 }
270 } else {
271 lo->tx_magn = 0;
272 lo->tx_bias = 0;
273 b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
274 & 0xFFF0); /* TX bias == 0 */
275 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200276 lo->txctl_measured_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400277}
278
279static void lo_read_power_vector(struct b43_wldev *dev)
280{
281 struct b43_phy *phy = &dev->phy;
282 struct b43_txpower_lo_control *lo = phy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200283 int i;
Michael Buesche4d6b792007-09-18 15:39:42 -0400284 u64 tmp;
285 u64 power_vector = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400286
287 for (i = 0; i < 8; i += 2) {
288 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
Michael Buesche4d6b792007-09-18 15:39:42 -0400289 power_vector |= (tmp << (i * 8));
290 /* Clear the vector on the device. */
291 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
292 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400293 if (power_vector)
294 lo->power_vector = power_vector;
Michael Bueschf5eda472008-04-20 16:03:32 +0200295 lo->pwr_vec_read_time = jiffies;
Michael Buesche4d6b792007-09-18 15:39:42 -0400296}
297
298/* 802.11/LO/GPHY/MeasuringGains */
299static void lo_measure_gain_values(struct b43_wldev *dev,
300 s16 max_rx_gain, int use_trsw_rx)
301{
302 struct b43_phy *phy = &dev->phy;
303 u16 tmp;
304
305 if (max_rx_gain < 0)
306 max_rx_gain = 0;
307
308 if (has_loopback_gain(phy)) {
309 int trsw_rx = 0;
310 int trsw_rx_gain;
311
312 if (use_trsw_rx) {
313 trsw_rx_gain = phy->trsw_rx_gain / 2;
314 if (max_rx_gain >= trsw_rx_gain) {
315 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
316 trsw_rx = 0x20;
317 }
318 } else
319 trsw_rx_gain = max_rx_gain;
320 if (trsw_rx_gain < 9) {
321 phy->lna_lod_gain = 0;
322 } else {
323 phy->lna_lod_gain = 1;
324 trsw_rx_gain -= 8;
325 }
326 trsw_rx_gain = limit_value(trsw_rx_gain, 0, 0x2D);
327 phy->pga_gain = trsw_rx_gain / 3;
328 if (phy->pga_gain >= 5) {
329 phy->pga_gain -= 5;
330 phy->lna_gain = 2;
331 } else
332 phy->lna_gain = 0;
333 } else {
334 phy->lna_gain = 0;
335 phy->trsw_rx_gain = 0x20;
336 if (max_rx_gain >= 0x14) {
337 phy->lna_lod_gain = 1;
338 phy->pga_gain = 2;
339 } else if (max_rx_gain >= 0x12) {
340 phy->lna_lod_gain = 1;
341 phy->pga_gain = 1;
342 } else if (max_rx_gain >= 0xF) {
343 phy->lna_lod_gain = 1;
344 phy->pga_gain = 0;
345 } else {
346 phy->lna_lod_gain = 0;
347 phy->pga_gain = 0;
348 }
349 }
350
351 tmp = b43_radio_read16(dev, 0x7A);
352 if (phy->lna_lod_gain == 0)
353 tmp &= ~0x0008;
354 else
355 tmp |= 0x0008;
356 b43_radio_write16(dev, 0x7A, tmp);
357}
358
359struct lo_g_saved_values {
360 u8 old_channel;
361
362 /* Core registers */
363 u16 reg_3F4;
364 u16 reg_3E2;
365
366 /* PHY registers */
367 u16 phy_lo_mask;
368 u16 phy_extg_01;
369 u16 phy_dacctl_hwpctl;
370 u16 phy_dacctl;
Michael Buesch52507032008-01-09 18:39:09 +0100371 u16 phy_cck_14;
Michael Buesche4d6b792007-09-18 15:39:42 -0400372 u16 phy_hpwr_tssictl;
373 u16 phy_analogover;
374 u16 phy_analogoverval;
375 u16 phy_rfover;
376 u16 phy_rfoverval;
377 u16 phy_classctl;
Michael Buesch52507032008-01-09 18:39:09 +0100378 u16 phy_cck_3E;
Michael Buesche4d6b792007-09-18 15:39:42 -0400379 u16 phy_crs0;
380 u16 phy_pgactl;
Michael Buesch52507032008-01-09 18:39:09 +0100381 u16 phy_cck_2A;
Michael Buesche4d6b792007-09-18 15:39:42 -0400382 u16 phy_syncctl;
Michael Buesch52507032008-01-09 18:39:09 +0100383 u16 phy_cck_30;
384 u16 phy_cck_06;
Michael Buesche4d6b792007-09-18 15:39:42 -0400385
386 /* Radio registers */
387 u16 radio_43;
388 u16 radio_7A;
389 u16 radio_52;
390};
391
392static void lo_measure_setup(struct b43_wldev *dev,
393 struct lo_g_saved_values *sav)
394{
395 struct ssb_sprom *sprom = &dev->dev->bus->sprom;
396 struct b43_phy *phy = &dev->phy;
397 struct b43_txpower_lo_control *lo = phy->lo_control;
398 u16 tmp;
399
400 if (b43_has_hardware_pctl(phy)) {
401 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
402 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
403 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100404 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
Michael Buesche4d6b792007-09-18 15:39:42 -0400405 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
406
407 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
408 b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
409 | 0x100);
410 b43_phy_write(dev, B43_PHY_EXTG(0x01),
411 b43_phy_read(dev, B43_PHY_EXTG(0x01))
412 | 0x40);
413 b43_phy_write(dev, B43_PHY_DACCTL,
414 b43_phy_read(dev, B43_PHY_DACCTL)
415 | 0x40);
Michael Buesch52507032008-01-09 18:39:09 +0100416 b43_phy_write(dev, B43_PHY_CCK(0x14),
417 b43_phy_read(dev, B43_PHY_CCK(0x14))
Michael Buesche4d6b792007-09-18 15:39:42 -0400418 | 0x200);
419 }
420 if (phy->type == B43_PHYTYPE_B &&
421 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
Michael Buesch52507032008-01-09 18:39:09 +0100422 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
423 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
Michael Buesche4d6b792007-09-18 15:39:42 -0400424 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400425 if (phy->rev >= 2) {
426 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
427 sav->phy_analogoverval =
428 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
429 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
430 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
431 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
Michael Buesch52507032008-01-09 18:39:09 +0100432 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
Michael Buesche4d6b792007-09-18 15:39:42 -0400433 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
434
435 b43_phy_write(dev, B43_PHY_CLASSCTL,
436 b43_phy_read(dev, B43_PHY_CLASSCTL)
437 & 0xFFFC);
438 b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
439 & 0x7FFF);
440 b43_phy_write(dev, B43_PHY_ANALOGOVER,
441 b43_phy_read(dev, B43_PHY_ANALOGOVER)
442 | 0x0003);
443 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
444 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
445 & 0xFFFC);
446 if (phy->type == B43_PHYTYPE_G) {
447 if ((phy->rev >= 7) &&
Larry Finger95de2842007-11-09 16:57:18 -0600448 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400449 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
450 } else {
451 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
452 }
453 } else {
454 b43_phy_write(dev, B43_PHY_RFOVER, 0);
455 }
Michael Buesch52507032008-01-09 18:39:09 +0100456 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400457 }
458 sav->reg_3F4 = b43_read16(dev, 0x3F4);
459 sav->reg_3E2 = b43_read16(dev, 0x3E2);
460 sav->radio_43 = b43_radio_read16(dev, 0x43);
461 sav->radio_7A = b43_radio_read16(dev, 0x7A);
462 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
Michael Buesch52507032008-01-09 18:39:09 +0100463 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
Michael Buesche4d6b792007-09-18 15:39:42 -0400464 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
465 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
466
467 if (!has_tx_magnification(phy)) {
468 sav->radio_52 = b43_radio_read16(dev, 0x52);
469 sav->radio_52 &= 0x00F0;
470 }
471 if (phy->type == B43_PHYTYPE_B) {
Michael Buesch52507032008-01-09 18:39:09 +0100472 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
473 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
474 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
475 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
Michael Buesche4d6b792007-09-18 15:39:42 -0400476 } else {
477 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
478 | 0x8000);
479 }
480 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
481 & 0xF000);
482
483 tmp =
Michael Buesch52507032008-01-09 18:39:09 +0100484 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400485 b43_phy_write(dev, tmp, 0x007F);
486
487 tmp = sav->phy_syncctl;
488 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
489 tmp = sav->radio_7A;
490 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
491
Michael Buesch52507032008-01-09 18:39:09 +0100492 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400493 if (phy->type == B43_PHYTYPE_G ||
494 (phy->type == B43_PHYTYPE_B &&
495 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
Michael Buesch52507032008-01-09 18:39:09 +0100496 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
Michael Buesche4d6b792007-09-18 15:39:42 -0400497 } else
Michael Buesch52507032008-01-09 18:39:09 +0100498 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
Michael Buesche4d6b792007-09-18 15:39:42 -0400499 if (phy->rev >= 2)
500 b43_dummy_transmission(dev);
501 b43_radio_selectchannel(dev, 6, 0);
502 b43_radio_read16(dev, 0x51); /* dummy read */
503 if (phy->type == B43_PHYTYPE_G)
Michael Buesch52507032008-01-09 18:39:09 +0100504 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
Michael Bueschf5eda472008-04-20 16:03:32 +0200505
506 /* Re-measure the txctl values, if needed. */
507 if (time_before(lo->txctl_measured_time,
508 jiffies - B43_LO_TXCTL_EXPIRE))
Michael Buesche4d6b792007-09-18 15:39:42 -0400509 lo_measure_txctl_values(dev);
Michael Bueschf5eda472008-04-20 16:03:32 +0200510
Michael Buesche4d6b792007-09-18 15:39:42 -0400511 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
512 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
513 } else {
514 if (phy->type == B43_PHYTYPE_B)
Michael Buesch52507032008-01-09 18:39:09 +0100515 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400516 else
517 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
518 }
519}
520
521static void lo_measure_restore(struct b43_wldev *dev,
522 struct lo_g_saved_values *sav)
523{
524 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -0400525 u16 tmp;
526
527 if (phy->rev >= 2) {
528 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
529 tmp = (phy->pga_gain << 8);
530 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
531 udelay(5);
532 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
533 udelay(2);
534 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
535 } else {
536 tmp = (phy->pga_gain | 0xEFA0);
537 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
538 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400539 if (phy->type == B43_PHYTYPE_G) {
540 if (phy->rev >= 3)
Michael Buesch52507032008-01-09 18:39:09 +0100541 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400542 else
Michael Buesch52507032008-01-09 18:39:09 +0100543 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
Michael Buesche4d6b792007-09-18 15:39:42 -0400544 if (phy->rev >= 2)
Michael Buesch52507032008-01-09 18:39:09 +0100545 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
Michael Buesche4d6b792007-09-18 15:39:42 -0400546 else
Michael Buesch52507032008-01-09 18:39:09 +0100547 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
Michael Buesche4d6b792007-09-18 15:39:42 -0400548 }
549 b43_write16(dev, 0x3F4, sav->reg_3F4);
550 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
Michael Buesch52507032008-01-09 18:39:09 +0100551 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400552 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
553 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
554 b43_radio_write16(dev, 0x43, sav->radio_43);
555 b43_radio_write16(dev, 0x7A, sav->radio_7A);
556 if (!has_tx_magnification(phy)) {
557 tmp = sav->radio_52;
558 b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
559 & 0xFF0F) | tmp);
560 }
561 b43_write16(dev, 0x3E2, sav->reg_3E2);
562 if (phy->type == B43_PHYTYPE_B &&
563 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
Michael Buesch52507032008-01-09 18:39:09 +0100564 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
565 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
Michael Buesche4d6b792007-09-18 15:39:42 -0400566 }
567 if (phy->rev >= 2) {
568 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
569 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
570 sav->phy_analogoverval);
571 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
572 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
573 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
Michael Buesch52507032008-01-09 18:39:09 +0100574 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
Michael Buesche4d6b792007-09-18 15:39:42 -0400575 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
576 }
577 if (b43_has_hardware_pctl(phy)) {
578 tmp = (sav->phy_lo_mask & 0xBFFF);
579 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
580 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
581 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
Michael Buesch52507032008-01-09 18:39:09 +0100582 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
Michael Buesche4d6b792007-09-18 15:39:42 -0400583 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
584 }
585 b43_radio_selectchannel(dev, sav->old_channel, 1);
586}
587
588struct b43_lo_g_statemachine {
589 int current_state;
590 int nr_measured;
591 int state_val_multiplier;
592 u16 lowest_feedth;
593 struct b43_loctl min_loctl;
594};
595
596/* Loop over each possible value in this state. */
597static int lo_probe_possible_loctls(struct b43_wldev *dev,
598 struct b43_loctl *probe_loctl,
599 struct b43_lo_g_statemachine *d)
600{
601 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -0400602 struct b43_loctl test_loctl;
603 struct b43_loctl orig_loctl;
604 struct b43_loctl prev_loctl = {
605 .i = -100,
606 .q = -100,
607 };
608 int i;
609 int begin, end;
610 int found_lower = 0;
611 u16 feedth;
612
613 static const struct b43_loctl modifiers[] = {
614 {.i = 1,.q = 1,},
615 {.i = 1,.q = 0,},
616 {.i = 1,.q = -1,},
617 {.i = 0,.q = -1,},
618 {.i = -1,.q = -1,},
619 {.i = -1,.q = 0,},
620 {.i = -1,.q = 1,},
621 {.i = 0,.q = 1,},
622 };
623
624 if (d->current_state == 0) {
625 begin = 1;
626 end = 8;
627 } else if (d->current_state % 2 == 0) {
628 begin = d->current_state - 1;
629 end = d->current_state + 1;
630 } else {
631 begin = d->current_state - 2;
632 end = d->current_state + 2;
633 }
634 if (begin < 1)
635 begin += 8;
636 if (end > 8)
637 end -= 8;
638
639 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
640 i = begin;
641 d->current_state = i;
642 while (1) {
643 B43_WARN_ON(!(i >= 1 && i <= 8));
644 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
645 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
646 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
647 if ((test_loctl.i != prev_loctl.i ||
648 test_loctl.q != prev_loctl.q) &&
649 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
650 b43_lo_write(dev, &test_loctl);
651 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
652 phy->pga_gain,
653 phy->trsw_rx_gain);
654 if (feedth < d->lowest_feedth) {
655 memcpy(probe_loctl, &test_loctl,
656 sizeof(struct b43_loctl));
657 found_lower = 1;
658 d->lowest_feedth = feedth;
659 if ((d->nr_measured < 2) &&
Michael Bueschf5eda472008-04-20 16:03:32 +0200660 !has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400661 break;
662 }
663 }
664 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
665 if (i == end)
666 break;
667 if (i == 8)
668 i = 1;
669 else
670 i++;
671 d->current_state = i;
672 }
673
674 return found_lower;
675}
676
677static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
678 struct b43_loctl *loctl,
679 int *max_rx_gain)
680{
681 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -0400682 struct b43_lo_g_statemachine d;
683 u16 feedth;
684 int found_lower;
685 struct b43_loctl probe_loctl;
686 int max_repeat = 1, repeat_cnt = 0;
687
688 d.nr_measured = 0;
689 d.state_val_multiplier = 1;
Michael Bueschf5eda472008-04-20 16:03:32 +0200690 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400691 d.state_val_multiplier = 3;
692
693 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
Michael Bueschf5eda472008-04-20 16:03:32 +0200694 if (has_loopback_gain(phy))
Michael Buesche4d6b792007-09-18 15:39:42 -0400695 max_repeat = 4;
696 do {
697 b43_lo_write(dev, &d.min_loctl);
698 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
699 phy->pga_gain,
700 phy->trsw_rx_gain);
Michael Bueschf5eda472008-04-20 16:03:32 +0200701 if (feedth < 0x258) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400702 if (feedth >= 0x12C)
703 *max_rx_gain += 6;
704 else
705 *max_rx_gain += 3;
706 feedth = lo_measure_feedthrough(dev, phy->lna_gain,
707 phy->pga_gain,
708 phy->trsw_rx_gain);
709 }
710 d.lowest_feedth = feedth;
711
712 d.current_state = 0;
713 do {
714 B43_WARN_ON(!
715 (d.current_state >= 0
716 && d.current_state <= 8));
717 memcpy(&probe_loctl, &d.min_loctl,
718 sizeof(struct b43_loctl));
719 found_lower =
720 lo_probe_possible_loctls(dev, &probe_loctl, &d);
721 if (!found_lower)
722 break;
723 if ((probe_loctl.i == d.min_loctl.i) &&
724 (probe_loctl.q == d.min_loctl.q))
725 break;
726 memcpy(&d.min_loctl, &probe_loctl,
727 sizeof(struct b43_loctl));
728 d.nr_measured++;
729 } while (d.nr_measured < 24);
730 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
731
732 if (has_loopback_gain(phy)) {
733 if (d.lowest_feedth > 0x1194)
734 *max_rx_gain -= 6;
735 else if (d.lowest_feedth < 0x5DC)
736 *max_rx_gain += 3;
737 if (repeat_cnt == 0) {
738 if (d.lowest_feedth <= 0x5DC) {
739 d.state_val_multiplier = 1;
740 repeat_cnt++;
741 } else
742 d.state_val_multiplier = 2;
743 } else if (repeat_cnt == 2)
744 d.state_val_multiplier = 1;
745 }
746 lo_measure_gain_values(dev, *max_rx_gain,
747 has_loopback_gain(phy));
748 } while (++repeat_cnt < max_repeat);
749}
750
Michael Bueschf5eda472008-04-20 16:03:32 +0200751static
752struct b43_lo_calib * b43_calibrate_lo_setting(struct b43_wldev *dev,
753 const struct b43_bbatt *bbatt,
754 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400755{
756 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -0400757 struct b43_loctl loctl = {
758 .i = 0,
759 .q = 0,
760 };
Michael Buesche4d6b792007-09-18 15:39:42 -0400761 int max_rx_gain;
Michael Bueschf5eda472008-04-20 16:03:32 +0200762 struct b43_lo_calib *cal;
763 struct lo_g_saved_values uninitialized_var(saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400764 /* Values from the "TXCTL Register and Value Table" */
765 u16 txctl_reg;
766 u16 txctl_value;
767 u16 pad_mix_gain;
768
Michael Bueschf5eda472008-04-20 16:03:32 +0200769 saved_regs.old_channel = phy->channel;
770 b43_mac_suspend(dev);
771 lo_measure_setup(dev, &saved_regs);
Michael Buesche4d6b792007-09-18 15:39:42 -0400772
773 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
774
Michael Bueschf5eda472008-04-20 16:03:32 +0200775 b43_radio_write16(dev, 0x43,
776 (b43_radio_read16(dev, 0x43) & 0xFFF0)
777 | rfatt->att);
778 b43_radio_write16(dev, txctl_reg,
779 (b43_radio_read16(dev, txctl_reg) & ~txctl_value)
780 | (rfatt->with_padmix) ? txctl_value : 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400781
Michael Bueschf5eda472008-04-20 16:03:32 +0200782 max_rx_gain = rfatt->att * 2;
783 max_rx_gain += bbatt->att / 2;
784 if (rfatt->with_padmix)
785 max_rx_gain -= pad_mix_gain;
786 if (has_loopback_gain(phy))
787 max_rx_gain += phy->max_lb_gain;
788 lo_measure_gain_values(dev, max_rx_gain,
789 has_loopback_gain(phy));
Michael Buesche4d6b792007-09-18 15:39:42 -0400790
Michael Bueschf5eda472008-04-20 16:03:32 +0200791 b43_phy_set_baseband_attenuation(dev, bbatt->att);
792 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
Michael Buesche4d6b792007-09-18 15:39:42 -0400793
Michael Bueschf5eda472008-04-20 16:03:32 +0200794 lo_measure_restore(dev, &saved_regs);
795 b43_mac_enable(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400796
Michael Bueschf5eda472008-04-20 16:03:32 +0200797 if (b43_debug(dev, B43_DBG_LO)) {
798 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
799 "=> I=%d Q=%d\n",
800 bbatt->att, rfatt->att, rfatt->with_padmix,
801 loctl.i, loctl.q);
Michael Buesche4d6b792007-09-18 15:39:42 -0400802 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400803
Michael Bueschf5eda472008-04-20 16:03:32 +0200804 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
805 if (!cal) {
806 b43warn(dev->wl, "LO calib: out of memory\n");
807 return NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400808 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200809 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
810 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
811 memcpy(&cal->ctl, &loctl, sizeof(loctl));
812 cal->calib_time = jiffies;
813 INIT_LIST_HEAD(&cal->list);
Michael Buesche4d6b792007-09-18 15:39:42 -0400814
Michael Bueschf5eda472008-04-20 16:03:32 +0200815 return cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400816}
817
Michael Bueschf5eda472008-04-20 16:03:32 +0200818/* Get a calibrated LO setting for the given attenuation values.
819 * Might return a NULL pointer under OOM! */
820static
821struct b43_lo_calib * b43_get_calib_lo_settings(struct b43_wldev *dev,
822 const struct b43_bbatt *bbatt,
823 const struct b43_rfatt *rfatt)
Michael Buesche4d6b792007-09-18 15:39:42 -0400824{
Michael Bueschf5eda472008-04-20 16:03:32 +0200825 struct b43_txpower_lo_control *lo = dev->phy.lo_control;
826 struct b43_lo_calib *c;
827
828 c = b43_find_lo_calib(lo, bbatt, rfatt);
829 if (c)
830 return c;
831 /* Not in the list of calibrated LO settings.
832 * Calibrate it now. */
833 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
834 if (!c)
835 return NULL;
836 list_add(&c->list, &lo->calib_list);
837
838 return c;
Michael Buesche4d6b792007-09-18 15:39:42 -0400839}
840
Michael Bueschf5eda472008-04-20 16:03:32 +0200841void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
Michael Buesche4d6b792007-09-18 15:39:42 -0400842{
843 struct b43_phy *phy = &dev->phy;
Michael Bueschf5eda472008-04-20 16:03:32 +0200844 struct b43_txpower_lo_control *lo = phy->lo_control;
845 int i;
846 int rf_offset, bb_offset;
847 const struct b43_rfatt *rfatt;
848 const struct b43_bbatt *bbatt;
849 u64 power_vector;
850 bool table_changed = 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400851
Michael Bueschf5eda472008-04-20 16:03:32 +0200852 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
853 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
Michael Buesche4d6b792007-09-18 15:39:42 -0400854
Michael Bueschf5eda472008-04-20 16:03:32 +0200855 power_vector = lo->power_vector;
856 if (!update_all && !power_vector)
857 return; /* Nothing to do. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400858
Michael Bueschf5eda472008-04-20 16:03:32 +0200859 /* Suspend the MAC now to avoid continuous suspend/enable
860 * cycles in the loop. */
861 b43_mac_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400862
Michael Bueschf5eda472008-04-20 16:03:32 +0200863 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
864 struct b43_lo_calib *cal;
865 int idx;
866 u16 val;
Michael Buesche4d6b792007-09-18 15:39:42 -0400867
Michael Bueschf5eda472008-04-20 16:03:32 +0200868 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
869 continue;
870 /* Update the table entry for this power_vector bit.
871 * The table rows are RFatt entries and columns are BBatt. */
872 bb_offset = i / lo->rfatt_list.len;
873 rf_offset = i % lo->rfatt_list.len;
874 bbatt = &(lo->bbatt_list.list[bb_offset]);
875 rfatt = &(lo->rfatt_list.list[rf_offset]);
876
877 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
878 if (!cal) {
879 b43warn(dev->wl, "LO: Could not "
880 "calibrate DC table entry\n");
881 continue;
882 }
883 /*FIXME: Is Q really in the low nibble? */
884 val = (u8)(cal->ctl.q);
885 val |= ((u8)(cal->ctl.i)) << 4;
886 kfree(cal);
887
888 /* Get the index into the hardware DC LT. */
889 idx = i / 2;
890 /* Change the table in memory. */
891 if (i % 2) {
892 /* Change the high byte. */
893 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
894 | ((val & 0x00FF) << 8);
895 } else {
896 /* Change the low byte. */
897 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
898 | (val & 0x00FF);
899 }
900 table_changed = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400901 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200902 if (table_changed) {
903 /* The table changed in memory. Update the hardware table. */
904 for (i = 0; i < B43_DC_LT_SIZE; i++)
905 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400906 }
Michael Bueschf5eda472008-04-20 16:03:32 +0200907 b43_mac_enable(dev);
908}
909
910/* Fixup the RF attenuation value for the case where we are
911 * using the PAD mixer. */
912static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
913{
914 if (!rf->with_padmix)
915 return;
916 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
917 rf->att = 4;
Michael Buesche4d6b792007-09-18 15:39:42 -0400918}
919
920void b43_lo_g_adjust(struct b43_wldev *dev)
921{
922 struct b43_phy *phy = &dev->phy;
Michael Bueschf5eda472008-04-20 16:03:32 +0200923 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400924 struct b43_rfatt rf;
Michael Buesche4d6b792007-09-18 15:39:42 -0400925
926 memcpy(&rf, &phy->rfatt, sizeof(rf));
Michael Bueschf5eda472008-04-20 16:03:32 +0200927 b43_lo_fixup_rfatt(&rf);
Michael Buesche4d6b792007-09-18 15:39:42 -0400928
Michael Bueschf5eda472008-04-20 16:03:32 +0200929 cal = b43_get_calib_lo_settings(dev, &phy->bbatt, &rf);
930 if (!cal)
931 return;
932 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400933}
934
935void b43_lo_g_adjust_to(struct b43_wldev *dev,
936 u16 rfatt, u16 bbatt, u16 tx_control)
937{
938 struct b43_rfatt rf;
939 struct b43_bbatt bb;
Michael Bueschf5eda472008-04-20 16:03:32 +0200940 struct b43_lo_calib *cal;
Michael Buesche4d6b792007-09-18 15:39:42 -0400941
942 memset(&rf, 0, sizeof(rf));
943 memset(&bb, 0, sizeof(bb));
944 rf.att = rfatt;
945 bb.att = bbatt;
Michael Bueschf5eda472008-04-20 16:03:32 +0200946 b43_lo_fixup_rfatt(&rf);
947 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
948 if (!cal)
949 return;
950 b43_lo_write(dev, &cal->ctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400951}
952
Michael Bueschf5eda472008-04-20 16:03:32 +0200953/* Periodic LO maintanance work */
954void b43_lo_g_maintanance_work(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -0400955{
956 struct b43_phy *phy = &dev->phy;
957 struct b43_txpower_lo_control *lo = phy->lo_control;
Michael Bueschf5eda472008-04-20 16:03:32 +0200958 unsigned long now;
959 unsigned long expire;
960 struct b43_lo_calib *cal, *tmp;
961 bool current_item_expired = 0;
962 bool hwpctl;
Michael Buesche4d6b792007-09-18 15:39:42 -0400963
Michael Bueschf5eda472008-04-20 16:03:32 +0200964 if (!lo)
965 return;
966 now = jiffies;
967 hwpctl = b43_has_hardware_pctl(phy);
968
969 if (hwpctl) {
970 /* Read the power vector and update it, if needed. */
971 expire = now - B43_LO_PWRVEC_EXPIRE;
972 if (time_before(lo->pwr_vec_read_time, expire)) {
973 lo_read_power_vector(dev);
974 b43_gphy_dc_lt_init(dev, 0);
975 }
976 //FIXME Recalc the whole DC table from time to time?
977 }
978
979 if (hwpctl)
980 return;
981 /* Search for expired LO settings. Remove them.
982 * Recalibrate the current setting, if expired. */
983 expire = now - B43_LO_CALIB_EXPIRE;
984 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
985 if (!time_before(cal->calib_time, expire))
986 continue;
987 /* This item expired. */
988 if (b43_compare_bbatt(&cal->bbatt, &phy->bbatt) &&
989 b43_compare_rfatt(&cal->rfatt, &phy->rfatt)) {
990 B43_WARN_ON(current_item_expired);
991 current_item_expired = 1;
992 }
993 if (b43_debug(dev, B43_DBG_LO)) {
994 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
995 "I=%d, Q=%d expired\n",
996 cal->bbatt.att, cal->rfatt.att,
997 cal->rfatt.with_padmix,
998 cal->ctl.i, cal->ctl.q);
999 }
1000 list_del(&cal->list);
1001 kfree(cal);
1002 }
1003 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
1004 /* Recalibrate currently used LO setting. */
1005 if (b43_debug(dev, B43_DBG_LO))
1006 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
1007 cal = b43_calibrate_lo_setting(dev, &phy->bbatt, &phy->rfatt);
1008 if (cal) {
1009 list_add(&cal->list, &lo->calib_list);
1010 b43_lo_write(dev, &cal->ctl);
1011 } else
1012 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
1013 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001014}
1015
Michael Bueschf5eda472008-04-20 16:03:32 +02001016void b43_lo_g_cleanup(struct b43_wldev *dev)
1017{
1018 struct b43_txpower_lo_control *lo = dev->phy.lo_control;
1019 struct b43_lo_calib *cal, *tmp;
1020
1021 if (!lo)
1022 return;
1023 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
1024 list_del(&cal->list);
1025 kfree(cal);
1026 }
1027}
1028
1029/* LO Initialization */
1030void b43_lo_g_init(struct b43_wldev *dev)
Michael Buesche4d6b792007-09-18 15:39:42 -04001031{
1032 struct b43_phy *phy = &dev->phy;
Michael Buesche4d6b792007-09-18 15:39:42 -04001033
Michael Bueschf5eda472008-04-20 16:03:32 +02001034 if (b43_has_hardware_pctl(phy)) {
1035 lo_read_power_vector(dev);
1036 b43_gphy_dc_lt_init(dev, 1);
1037 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001038}