pub use hdmi_hal::phy::TmdsClockRatio as ClockRatio;
#[non_exhaustive]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScramblingThreshold {
#[default]
Above340,
AtOrAbove340,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SinkTmdsFacts {
pub scdc_present: bool,
pub scrambling_supported: bool,
pub lte_340mcsc_scramble: bool,
pub frl_capable: bool,
}
impl SinkTmdsFacts {
pub const NONE: Self = Self::new(false, false, false);
#[must_use]
pub const fn new(
scdc_present: bool,
scrambling_supported: bool,
lte_340mcsc_scramble: bool,
) -> Self {
Self {
scdc_present,
scrambling_supported,
lte_340mcsc_scramble,
frl_capable: scdc_present,
}
}
#[must_use]
pub const fn with_frl_capable(mut self, frl_capable: bool) -> Self {
self.frl_capable = frl_capable;
self
}
}
#[cfg(feature = "display-types")]
impl From<&display_types::cea861::HdmiForumSinkCap> for SinkTmdsFacts {
fn from(cap: &display_types::cea861::HdmiForumSinkCap) -> Self {
Self::new(
cap.scdc_present,
cap.scdc_present && cap.max_tmds_rate_mhz != 0,
cap.lte_340mcsc_scramble,
)
.with_frl_capable(cap.max_frl_rate != display_types::cea861::HdmiForumFrl::NotSupported)
}
}
#[cfg(feature = "display-types")]
impl From<Option<&display_types::cea861::HdmiForumSinkCap>> for SinkTmdsFacts {
fn from(cap: Option<&display_types::cea861::HdmiForumSinkCap>) -> Self {
cap.map_or(Self::NONE, Self::from)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceTmdsFacts {
pub scrambling: bool,
pub low_rate_scrambling: bool,
pub max_character_rate_khz: u32,
}
impl SourceTmdsFacts {
#[must_use]
pub const fn new(low_rate_scrambling: bool) -> Self {
Self {
scrambling: true,
low_rate_scrambling,
max_character_rate_khz: u32::MAX,
}
}
#[must_use]
pub const fn with_max_character_rate_khz(mut self, max_character_rate_khz: u32) -> Self {
self.max_character_rate_khz = max_character_rate_khz;
self
}
#[must_use]
pub const fn with_scrambling(mut self, scrambling: bool) -> Self {
self.scrambling = scrambling;
self
}
}
impl From<&hdmi_hal::phy::TmdsPhyCapabilities> for SourceTmdsFacts {
fn from(caps: &hdmi_hal::phy::TmdsPhyCapabilities) -> Self {
Self {
scrambling: caps.scrambling,
low_rate_scrambling: caps.scrambles_at_low_rate(),
max_character_rate_khz: caps.max_character_rate_khz,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TmdsPolicy {
pub low_rate_scrambling: bool,
pub threshold: ScramblingThreshold,
pub tmds_config_gate: TmdsConfigGate,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub enum TmdsConfigGate {
#[default]
AnyScdcSink,
ScramblingSink,
}
impl TmdsPolicy {
pub const DEFAULT: Self = Self {
low_rate_scrambling: true,
threshold: ScramblingThreshold::Above340,
tmds_config_gate: TmdsConfigGate::AnyScdcSink,
};
#[must_use]
pub const fn with_low_rate_scrambling(self, low_rate_scrambling: bool) -> Self {
Self {
low_rate_scrambling,
..self
}
}
#[must_use]
pub const fn with_threshold(self, threshold: ScramblingThreshold) -> Self {
Self { threshold, ..self }
}
#[must_use]
pub const fn with_tmds_config_gate(self, tmds_config_gate: TmdsConfigGate) -> Self {
Self {
tmds_config_gate,
..self
}
}
}
impl Default for TmdsPolicy {
fn default() -> Self {
Self::DEFAULT
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SinkTmdsConfig {
pub scrambling_enable: bool,
pub high_tmds_clock_ratio: bool,
}
impl SinkTmdsConfig {
#[must_use]
pub const fn new(scrambling_enable: bool, high_tmds_clock_ratio: bool) -> Self {
Self {
scrambling_enable,
high_tmds_clock_ratio,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TmdsConfigReading {
pub config: SinkTmdsConfig,
pub raw: u8,
}
impl TmdsConfigReading {
#[must_use]
pub const fn new(config: SinkTmdsConfig, raw: u8) -> Self {
Self { config, raw }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScramblingStatus {
pub set: bool,
raw: u8,
}
impl ScramblingStatus {
#[must_use]
pub const fn new(set: bool, raw: u8) -> Self {
Self { set, raw }
}
#[must_use]
pub const fn raw(&self) -> u8 {
self.raw
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sink_facts_none_is_all_clear() {
assert_eq!(SinkTmdsFacts::NONE, SinkTmdsFacts::new(false, false, false));
const { assert!(!SinkTmdsFacts::NONE.frl_capable) };
assert!(SinkTmdsFacts::new(true, true, true).frl_capable);
assert!(!SinkTmdsFacts::new(false, true, true).frl_capable);
}
#[test]
fn sink_facts_new_keeps_field_order() {
let f = SinkTmdsFacts::new(true, false, true);
assert!(f.scdc_present);
assert!(!f.scrambling_supported);
assert!(f.lte_340mcsc_scramble);
}
#[test]
fn policy_default_matches_const() {
assert_eq!(TmdsPolicy::default(), TmdsPolicy::DEFAULT);
const { assert!(TmdsPolicy::DEFAULT.low_rate_scrambling) };
assert_eq!(TmdsPolicy::DEFAULT.threshold, ScramblingThreshold::Above340);
assert_eq!(
ScramblingThreshold::default(),
ScramblingThreshold::Above340
);
}
#[test]
fn policy_builders_change_one_field() {
let p = TmdsPolicy::DEFAULT.with_low_rate_scrambling(false);
assert!(!p.low_rate_scrambling);
assert_eq!(p.threshold, ScramblingThreshold::Above340);
let p = TmdsPolicy::DEFAULT.with_threshold(ScramblingThreshold::AtOrAbove340);
assert!(p.low_rate_scrambling);
assert_eq!(p.threshold, ScramblingThreshold::AtOrAbove340);
}
#[test]
fn source_facts_and_sink_config_new() {
assert!(SourceTmdsFacts::new(true).low_rate_scrambling);
let c = SinkTmdsConfig::new(true, false);
assert!(c.scrambling_enable);
assert!(!c.high_tmds_clock_ratio);
}
#[test]
fn a_reading_keeps_the_byte_beside_the_fields() {
const BOTH: SinkTmdsConfig = SinkTmdsConfig::new(true, true);
let reading = TmdsConfigReading::new(BOTH, 0xA3);
assert_eq!(reading.config, BOTH);
assert_eq!(reading.raw, 0xA3);
assert_ne!(reading, TmdsConfigReading::new(BOTH, 0x03));
assert!(matches!(reading.config, BOTH));
}
#[test]
fn scrambling_status_keeps_the_byte() {
const STATUS: ScramblingStatus = ScramblingStatus::new(true, 0x81);
const { assert!(STATUS.set) };
assert_eq!(STATUS.raw(), 0x81);
assert_ne!(STATUS, ScramblingStatus::new(true, 0x01));
}
#[test]
fn constructors_are_const() {
const SINK: SinkTmdsFacts = SinkTmdsFacts::new(true, true, true);
const SOURCE: SourceTmdsFacts = SourceTmdsFacts::new(false);
const POLICY: TmdsPolicy = TmdsPolicy::DEFAULT
.with_low_rate_scrambling(false)
.with_threshold(ScramblingThreshold::AtOrAbove340);
const CONFIG: SinkTmdsConfig = SinkTmdsConfig::new(false, true);
const READ: TmdsConfigReading = TmdsConfigReading::new(CONFIG, 0x02);
const { assert!(READ.config.high_tmds_clock_ratio && READ.raw == 0x02) };
const { assert!(SINK.scdc_present && !SOURCE.low_rate_scrambling) };
const { assert!(!POLICY.low_rate_scrambling && CONFIG.high_tmds_clock_ratio) };
}
#[cfg(feature = "display-types")]
mod from_display_types {
use super::*;
use display_types::cea861::{HdmiForumFrl, HdmiForumSinkCap};
fn cap(scdc: bool, max_mhz: u16, lte: bool) -> HdmiForumSinkCap {
cap_with_frl(scdc, max_mhz, lte, HdmiForumFrl::NotSupported)
}
fn cap_with_frl(
scdc: bool,
max_mhz: u16,
lte: bool,
frl: HdmiForumFrl,
) -> HdmiForumSinkCap {
HdmiForumSinkCap::new(
1, max_mhz, scdc, false, false, false, lte, false, false, false, frl, false, false,
false, false, false, false, false, false, false, false, false, None, None, None,
)
}
#[test]
fn every_combination() {
let table = [
((false, 0, false), (false, false, false)),
((false, 0, true), (false, false, true)),
((false, 600, false), (false, false, false)),
((false, 600, true), (false, false, true)),
((true, 0, false), (true, false, false)),
((true, 0, true), (true, false, true)),
((true, 600, false), (true, true, false)),
((true, 600, true), (true, true, true)),
];
for ((scdc, max_mhz, lte), (f_scdc, f_supported, f_lte)) in table {
assert_eq!(
SinkTmdsFacts::from(&cap(scdc, max_mhz, lte)),
SinkTmdsFacts::new(f_scdc, f_supported, f_lte).with_frl_capable(false),
"{scdc} {max_mhz} {lte}"
);
}
}
#[test]
fn absent_block_is_none() {
assert_eq!(SinkTmdsFacts::from(None), SinkTmdsFacts::NONE);
let c = cap(true, 600, true);
assert_eq!(
SinkTmdsFacts::from(Some(&c)),
SinkTmdsFacts::new(true, true, true).with_frl_capable(false)
);
}
#[test]
fn frl_capable_follows_max_frl_rate() {
let frl = cap_with_frl(true, 600, false, HdmiForumFrl::Rate6Gbps4Lanes);
assert!(SinkTmdsFacts::from(&frl).frl_capable);
assert!(!SinkTmdsFacts::from(&cap(true, 600, false)).frl_capable);
}
}
}