Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions drivers/pwm/pwm_th1520.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,10 @@ fn ns_to_cycles(ns: u64, rate_hz: u64) -> u64 {
ns.saturating_mul(rate_hz) / NSEC_PER_SEC_U64
}

fn cycles_to_ns(cycles: u64, rate_hz: u64) -> u64 {
fn cycles_to_ns(cycles: u32, rate_hz: u64) -> u64 {
const NSEC_PER_SEC_U64: u64 = time::NSEC_PER_SEC as u64;

// TODO: Replace with a kernel helper like `mul_u64_u64_div_u64_roundup`
// once available in Rust.
let numerator = cycles
.saturating_mul(NSEC_PER_SEC_U64)
.saturating_add(rate_hz - 1);

numerator / rate_hz
(u64::from(cycles) * NSEC_PER_SEC_U64).div_ceil(rate_hz)
}

/// Hardware-specific waveform representation for TH1520.
Expand Down Expand Up @@ -192,15 +186,15 @@ impl pwm::PwmOps for Th1520PwmDriverData {
return Ok(());
}

wf.period_length_ns = cycles_to_ns(u64::from(wfhw.period_cycles), rate_hz);
wf.period_length_ns = cycles_to_ns(wfhw.period_cycles, rate_hz);

let duty_cycles = u64::from(wfhw.duty_cycles);
let duty_cycles = wfhw.duty_cycles;

if (wfhw.ctrl_val & TH1520_PWM_FPOUT) != 0 {
wf.duty_length_ns = cycles_to_ns(duty_cycles, rate_hz);
wf.duty_offset_ns = 0;
} else {
let period_cycles = u64::from(wfhw.period_cycles);
let period_cycles = wfhw.period_cycles;
let original_duty_cycles = period_cycles.saturating_sub(duty_cycles);

// For an inverted signal, `duty_length_ns` is the high time (period - low_time).
Expand Down