* [PATCH v2] pwm: th1520: Remove requirement for mul_u64_u64_div_u64_roundup
@ 2026-06-05 7:03 Maurice Hieronymus
2026-06-05 12:07 ` Uwe Kleine-König
0 siblings, 1 reply; 2+ messages in thread
From: Maurice Hieronymus @ 2026-06-05 7:03 UTC (permalink / raw)
To: Drew Fustini, Guo Ren, Fu Wei, Michal Wilczynski,
Uwe Kleine-König, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich
Cc: linux-riscv, linux-pwm, linux-kernel, rust-for-linux,
Maurice Hieronymus
The cycle register is always u32, so cycles_to_ns() can take a u32
instead of a u64. With that narrowing, cycles * NSEC_PER_SEC is at most
u32::MAX * 1e9 (~4.3e18), which fits in u64 without overflow. The
saturating arithmetic is therefore no longer needed, and the ceiling
division can use Rust's u64::div_ceil() directly instead of the
open-coded numerator/denominator form.
This also drops the TODO referring to a future
mul_u64_u64_div_u64_roundup kernel helper, which is no longer required.
Reviewed-by: Michal Wilczynski <m.wilczynski@samsung.com>
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
Changes in v2:
- Rebase on pwm/for-next
- Add Reviewed-by trailers
- Link to v1: https://lore.kernel.org/r/20260521-pwm-th1520-fix-v1-1-df9cd956dab4@mailbox.org
---
drivers/pwm/pwm_th1520.rs | 16 +++++-----------
1 file changed, 5 insertions(+), 11 deletions(-)
diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs
index ddd44a5ce497..933c1ec59c2a 100644
--- a/drivers/pwm/pwm_th1520.rs
+++ b/drivers/pwm/pwm_th1520.rs
@@ -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.
@@ -192,15 +186,15 @@ fn round_waveform_fromhw(
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).
---
base-commit: 5b5e33c44a491d5b5e019f527e028bd567a226fa
change-id: 20260521-pwm-th1520-fix-8e45558bbd31
Best regards,
--
Maurice Hieronymus <mhi@mailbox.org>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH v2] pwm: th1520: Remove requirement for mul_u64_u64_div_u64_roundup
2026-06-05 7:03 [PATCH v2] pwm: th1520: Remove requirement for mul_u64_u64_div_u64_roundup Maurice Hieronymus
@ 2026-06-05 12:07 ` Uwe Kleine-König
0 siblings, 0 replies; 2+ messages in thread
From: Uwe Kleine-König @ 2026-06-05 12:07 UTC (permalink / raw)
To: Maurice Hieronymus
Cc: Drew Fustini, Guo Ren, Fu Wei, Michal Wilczynski, Miguel Ojeda,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
linux-riscv, linux-pwm, linux-kernel, rust-for-linux
[-- Attachment #1: Type: text/plain, Size: 835 bytes --]
Hello,
On Fri, Jun 05, 2026 at 09:03:59AM +0200, Maurice Hieronymus wrote:
> The cycle register is always u32, so cycles_to_ns() can take a u32
> instead of a u64. With that narrowing, cycles * NSEC_PER_SEC is at most
> u32::MAX * 1e9 (~4.3e18), which fits in u64 without overflow. The
> saturating arithmetic is therefore no longer needed, and the ceiling
> division can use Rust's u64::div_ceil() directly instead of the
> open-coded numerator/denominator form.
>
> This also drops the TODO referring to a future
> mul_u64_u64_div_u64_roundup kernel helper, which is no longer required.
>
> Reviewed-by: Michal Wilczynski <m.wilczynski@samsung.com>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
Thanks
Uwe
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-06-05 12:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-05 7:03 [PATCH v2] pwm: th1520: Remove requirement for mul_u64_u64_div_u64_roundup Maurice Hieronymus
2026-06-05 12:07 ` Uwe Kleine-König
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox