feat(wifi): calibrate an RTL8822C's gain table and predistortion

This commit is contained in:
Tyler Smith committed 2026-10-31 09:33:49 +00:00
1 parent 351042d77f
commit 68591e7c39
6 files changed
+1621 -7

No files matched your search

@@ -7,6 +7,7 @@
pub mod attach;
pub mod beacon;
pub mod bootstrap;
pub mod calibrate;
pub mod ccmp;
pub mod coex;
pub mod commands;
File diff suppressed because it is too large. Load diff
@@ -204,14 +204,24 @@ impl Coex {
Err(Error::ArbiterBusy)
}
/// `rtw_coex_write_indirect_reg`.
fn arbiter_write<I: Io>(io: &mut I, address: u32, mask: u32, value: u32) -> Result<(), Error> {
/// `rtw_coex_read_indirect_reg`.
pub(super) fn arbiter_read<I: Io>(io: &mut I, address: u32) -> Result<u32, Error> {
Self::arbiter_ready(io)?;
io.write32(
REG_WL2LTECOEX_INDIRECT_ACCESS_CTRL_V1,
0x800f_0000 | address,
)?;
let old = io.read32(REG_WL2LTECOEX_INDIRECT_ACCESS_READ_DATA_V1)?;
Ok(io.read32(REG_WL2LTECOEX_INDIRECT_ACCESS_READ_DATA_V1)?)
}
/// `rtw_coex_write_indirect_reg`.
pub(super) fn arbiter_write<I: Io>(
io: &mut I,
address: u32,
mask: u32,
value: u32,
) -> Result<(), Error> {
let old = Self::arbiter_read(io, address)?;
let new = (old & !mask) | ((value << mask.trailing_zeros()) & mask);
Self::arbiter_ready(io)?;
io.write32(REG_WL2LTECOEX_INDIRECT_ACCESS_WRITE_DATA_V1, new)?;
@@ -14,6 +14,8 @@ const RSSI_MONITOR: u32 = 0x42;
const WL_PHY_INFO: u32 = 0x58;
const SCAN: u32 = 0x59;
const ADAPTIVITY: u32 = 0x5a;
/// `H2C_CMD_WIFI_CALIBRATION`.
const CALIBRATION: u32 = 0x6d;
/// `C2H_SCAN_RESULT`.
const SCAN_RESULT: u8 = 0x38;
@@ -92,6 +94,11 @@ pub fn phy_info(tx_mbps: u32, rx_mbps: u32, tx_rate: u8, rx_rate: u8, evm_db: u8
)
}
/// `rtw_fw_inform_rfk_status`, which tells Bluetooth too.
pub fn calibration(start: bool) -> (u32, u32) {
(CALIBRATION | (u32::from(start) << 8), 0)
}
/// `rtw_fw_scan_notify`.
pub fn scan(start: bool) -> (u32, u32) {
(SCAN | (u32::from(start) << 8), 0)
@@ -41,6 +41,18 @@ pub struct Report {
raw: [u8; LENGTH],
capabilities: Capabilities,
}
#[cfg(test)]
impl Report {
/// What a two-path radio on `profile` reports.
pub(crate) fn of_two_paths(profile: Profile) -> Self {
let raw = [0; LENGTH];
Self {
profile,
raw,
capabilities: decode(1 << 27, &raw),
}
}
}
impl Report {
pub fn profile(&self) -> Profile {
self.profile
@@ -7,6 +7,7 @@
//! [`Rtl8822c`]: super::station::Rtl8822c
use super::{
bootstrap::{Native, Up},
calibrate::Calibration,
coex::{self, Coex, Link},
commands::{self, Report},
descriptor::Packet,
@@ -141,6 +142,7 @@ pub struct Hardware {
sheets: (FactorySheet, FactorySheet),
/// `rtwdev->need_rfk`: the channel changed outside a scan.
calibrate: bool,
calibration: Calibration,
band: Band,
connected: bool,
address: Mac,
@@ -183,6 +185,7 @@ impl Hardware {
} = up;
let initial = phy.factory().calibration().initial();
let cck_bounds = initial.cck_bounds();
let calibration = Calibration::new(board.raw().power_track_raw);
let dynamic = Dynamic::new(dynamic::Initial {
gains: initial.igi_history(),
crystal_cap: initial.crystal_cap(),
@@ -211,6 +214,7 @@ impl Hardware {
pages,
sheets: regd::sheets(alpha2),
calibrate: true,
calibration,
band: Band::Ghz2,
connected: false,
address: mac,
@@ -309,6 +313,23 @@ impl Hardware {
Ok(())
}
/// `rtw8822c_rfk_handshake`.
fn calibration_handshake(&mut self, starting: bool) -> Result<(), String> {
if starting {
let io = self.control.io().map_err(failed("registers"))?;
self.calibration
.await_bluetooth(io)
.map_err(failed("bluetooth calibration"))?;
}
self.control
.command(commands::calibration(starting))
.map_err(failed("calibration notice"))?;
let io = self.control.io().map_err(failed("registers"))?;
self.calibration
.await_firmware(io)
.map_err(failed("calibration handshake"))
}
/// `rtw8822c_rfk_power_save`.
fn calibration_power_save(&mut self, on: bool) -> Result<(), String> {
let io = self.control.io().map_err(failed("registers"))?;
@@ -432,10 +453,18 @@ impl Device for Hardware {
return Ok(());
}
self.calibration_power_save(false)?;
self.calibration_handshake(true)?;
let io = self.control.io().map_err(failed("registers"))?;
// SAFETY: the only worker owns the radio and its frames wait for it.
unsafe { self.calibration.even_gains(io, &self.report) }.map_err(failed("gains"))?;
self.calibration_handshake(false)?;
// SAFETY: as above.
unsafe { self.control.iqk(&mut self.transmit, self.pages) }.map_err(failed("iqk"))?;
self.limitations
.push("gain and predistortion calibration on an RTL8822C");
let io = self.control.io().map_err(failed("registers"))?;
// SAFETY: as above.
unsafe { self.calibration.predistort(io, &self.report) }
.map_err(failed("predistortion"))?;
self.limitations.extend(self.calibration.take_shortfalls());
self.calibration_power_save(true)
}
@@ -543,8 +572,7 @@ impl Device for Hardware {
.map_err(failed("cam"))
}
/// `rtw_phy_dynamic_mechanism` as far as it goes: all but predistortion
/// tracking, which waits on its calibration.
/// `rtw_phy_dynamic_mechanism`.
fn watchdog(&mut self, scanning: bool, stbc: bool) -> Result<u8, String> {
self.dynamic.close_period();
if scanning {
@@ -589,6 +617,8 @@ impl Device for Hardware {
.track_frequency(io, linked, bluetooth_off)
.map_err(failed("crystal"))?;
// SAFETY: this worker owns the radio and its frames wait for it.
unsafe { self.calibration.track(io, &self.report) }.map_err(failed("predistortion"))?;
// SAFETY: as above.
unsafe { self.dynamic.track_temperature(io, &self.report, band) }
.map_err(failed("temperature"))?;
self.adapt()?;