* [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520
@ 2026-05-26 17:04 Maurice Hieronymus via B4 Relay
2026-05-26 17:04 ` [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Maurice Hieronymus via B4 Relay @ 2026-05-26 17:04 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren,
Fu Wei, Michal Wilczynski, Uwe Kleine-König
Cc: linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm,
Maurice Hieronymus
Add a Rust binding for clk_rate_exclusive_get() / clk_rate_exclusive_put()
and use it in pwm-th1520, which previously carried a TODO noting the
missing binding.
Based on pwm/for-next.
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
Maurice Hieronymus (2):
rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
pwm: th1520: Lock clock rate with clk_rate_exclusive_get
drivers/pwm/pwm_th1520.rs | 8 +++---
rust/kernel/clk.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+), 5 deletions(-)
---
base-commit: 0e5e0db3e954b32a410ab4f74a4faac7a0e8889f
change-id: 20260526-rate-exclusive-get-th1520-764a949de18e
Best regards,
--
Maurice Hieronymus <mhi@mailbox.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
2026-05-26 17:04 [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus via B4 Relay
@ 2026-05-26 17:04 ` Maurice Hieronymus via B4 Relay
2026-06-04 11:17 ` Alice Ryhl
2026-05-26 17:04 ` [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
2026-06-04 6:11 ` [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
2 siblings, 1 reply; 6+ messages in thread
From: Maurice Hieronymus via B4 Relay @ 2026-05-26 17:04 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren,
Fu Wei, Michal Wilczynski, Uwe Kleine-König
Cc: linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm,
Maurice Hieronymus
From: Maurice Hieronymus <mhi@mailbox.org>
Add Rust bindings for clk_rate_exclusive_get() and
clk_rate_exclusive_put().
Clk::rate_exclusive_get() consumes the Clk and returns an ExclusiveClk;
the matching put is issued from its Drop impl. ExclusiveClk derefs to
Clk so existing rate / prepare / enable APIs remain available on the
locked handle.
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
rust/kernel/clk.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
index 7abbd0767d8c..8cda40cb01e4 100644
--- a/rust/kernel/clk.rs
+++ b/rust/kernel/clk.rs
@@ -249,6 +249,23 @@ pub fn set_rate(&self, rate: Hertz) -> Result {
// [`clk_set_rate`].
to_result(unsafe { bindings::clk_set_rate(self.as_raw(), rate.as_hz()) })
}
+
+ /// Acquire exclusive control over the clock's rate.
+ ///
+ /// Consumes the [`Clk`] and returns an [`ExclusiveClk`] that releases the exclusivity
+ /// when dropped. While held, no other consumer may change the clock's rate.
+ ///
+ /// Equivalent to the kernel's [`clk_rate_exclusive_get`] API. Must not be called from
+ /// atomic context.
+ ///
+ /// [`clk_rate_exclusive_get`]:
+ /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_rate_exclusive_get
+ pub fn rate_exclusive_get(self) -> Result<ExclusiveClk> {
+ // SAFETY: By the type invariants, self.as_raw() is a valid argument for
+ // [`clk_rate_exclusive_get`].
+ to_result(unsafe { bindings::clk_rate_exclusive_get(self.as_raw()) })?;
+ Ok(ExclusiveClk(self))
+ }
}
impl Drop for Clk {
@@ -329,6 +346,54 @@ fn deref(&self) -> &Clk {
&self.0
}
}
+
+ /// A [`Clk`] with exclusive control over its rate.
+ ///
+ /// While an [`ExclusiveClk`] exists, no other consumer of the same clock may change its rate.
+ /// Obtained by calling [`Clk::rate_exclusive_get`]; the exclusivity is released automatically
+ /// when the value is dropped, after which the inner [`Clk`] is dropped as usual.
+ ///
+ /// # Invariants
+ ///
+ /// An [`ExclusiveClk`] instance owns a [`Clk`] for which `clk_rate_exclusive_get` has been
+ /// called and the matching `clk_rate_exclusive_put` has not yet been called.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::clk::{Clk, ExclusiveClk};
+ /// use kernel::device::Device;
+ /// use kernel::error::Result;
+ ///
+ /// fn lock_rate(dev: &Device) -> Result<ExclusiveClk> {
+ /// let clk = Clk::get(dev, None)?;
+ /// clk.prepare_enable()?;
+ /// clk.rate_exclusive_get()
+ /// }
+ /// ```
+ ///
+ /// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html
+ pub struct ExclusiveClk(Clk);
+
+ // Make [`ExclusiveClk`] behave like [`Clk`].
+ impl Deref for ExclusiveClk {
+ type Target = Clk;
+
+ fn deref(&self) -> &Clk {
+ &self.0
+ }
+ }
+
+ impl Drop for ExclusiveClk {
+ fn drop(&mut self) {
+ // SAFETY: By the type invariants, self.as_raw() is a valid argument for
+ // [`clk_rate_exclusive_put`] and balances the [`clk_rate_exclusive_get`] call from
+ // [`Clk::rate_exclusive_get`].
+ unsafe {
+ bindings::clk_rate_exclusive_put(self.as_raw());
+ }
+ }
+ }
}
#[cfg(CONFIG_COMMON_CLK)]
--
2.51.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get
2026-05-26 17:04 [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus via B4 Relay
2026-05-26 17:04 ` [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
@ 2026-05-26 17:04 ` Maurice Hieronymus via B4 Relay
2026-06-04 11:17 ` Alice Ryhl
2026-06-04 6:11 ` [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
2 siblings, 1 reply; 6+ messages in thread
From: Maurice Hieronymus via B4 Relay @ 2026-05-26 17:04 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren,
Fu Wei, Michal Wilczynski, Uwe Kleine-König
Cc: linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm,
Maurice Hieronymus
From: Maurice Hieronymus <mhi@mailbox.org>
The driver derives period and duty cycle from the clock rate read at
probe, so a later rate change would silently miscompute waveforms.
Switch to the new ExclusiveClk wrapper to hold the rate for the lifetime
of the driver data and drop the corresponding TODO.
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
drivers/pwm/pwm_th1520.rs | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs
index ddd44a5ce497..19e8621bc52c 100644
--- a/drivers/pwm/pwm_th1520.rs
+++ b/drivers/pwm/pwm_th1520.rs
@@ -22,7 +22,7 @@
use core::ops::Deref;
use kernel::{
- clk::Clk,
+ clk::{Clk, ExclusiveClk},
device::{Bound, Core, Device},
devres,
io::{
@@ -93,7 +93,7 @@ struct Th1520WfHw {
struct Th1520PwmDriverData {
#[pin]
iomem: devres::Devres<IoMem<TH1520_PWM_REG_SIZE>>,
- clk: Clk,
+ clk: ExclusiveClk,
}
impl pwm::PwmOps for Th1520PwmDriverData {
@@ -328,10 +328,8 @@ fn probe(
let clk = Clk::get(dev, None)?;
clk.prepare_enable()?;
+ let clk = clk.rate_exclusive_get()?;
- // TODO: Get exclusive ownership of the clock to prevent rate changes.
- // The Rust equivalent of `clk_rate_exclusive_get()` is not yet available.
- // This should be updated once it is implemented.
let rate_hz = clk.rate().as_hz();
if rate_hz == 0 {
dev_err!(dev, "Clock rate is zero\n");
--
2.51.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520
2026-05-26 17:04 [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus via B4 Relay
2026-05-26 17:04 ` [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
2026-05-26 17:04 ` [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
@ 2026-06-04 6:11 ` Maurice Hieronymus
2 siblings, 0 replies; 6+ messages in thread
From: Maurice Hieronymus @ 2026-06-04 6:11 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Alice Ryhl, Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren,
Fu Wei, Michal Wilczynski, Uwe Kleine-König
Cc: linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On Tue, 2026-05-26 at 19:04 +0200, Maurice Hieronymus via B4 Relay
wrote:
> Add a Rust binding for clk_rate_exclusive_get() /
> clk_rate_exclusive_put()
> and use it in pwm-th1520, which previously carried a TODO noting the
> missing binding.
>
> Based on pwm/for-next.
>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
> ---
> Maurice Hieronymus (2):
> rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
> pwm: th1520: Lock clock rate with clk_rate_exclusive_get
>
> drivers/pwm/pwm_th1520.rs | 8 +++---
> rust/kernel/clk.rs | 65
> +++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 68 insertions(+), 5 deletions(-)
> ---
> base-commit: 0e5e0db3e954b32a410ab4f74a4faac7a0e8889f
> change-id: 20260526-rate-exclusive-get-th1520-764a949de18e
>
> Best regards,
Any opinions on this patch?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
2026-05-26 17:04 ` [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
@ 2026-06-04 11:17 ` Alice Ryhl
0 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-06-04 11:17 UTC (permalink / raw)
To: mhi
Cc: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren, Fu Wei,
Michal Wilczynski, Uwe Kleine-König, linux-clk,
rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On Tue, May 26, 2026 at 07:04:57PM +0200, Maurice Hieronymus via B4 Relay wrote:
> From: Maurice Hieronymus <mhi@mailbox.org>
>
> Add Rust bindings for clk_rate_exclusive_get() and
> clk_rate_exclusive_put().
>
> Clk::rate_exclusive_get() consumes the Clk and returns an ExclusiveClk;
> the matching put is issued from its Drop impl. ExclusiveClk derefs to
> Clk so existing rate / prepare / enable APIs remain available on the
> locked handle.
>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
Overall looks okay to me. With below comments fixed:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> rust/kernel/clk.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 65 insertions(+)
>
> diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
> index 7abbd0767d8c..8cda40cb01e4 100644
> --- a/rust/kernel/clk.rs
> +++ b/rust/kernel/clk.rs
> @@ -249,6 +249,23 @@ pub fn set_rate(&self, rate: Hertz) -> Result {
> // [`clk_set_rate`].
> to_result(unsafe { bindings::clk_set_rate(self.as_raw(), rate.as_hz()) })
> }
> +
> + /// Acquire exclusive control over the clock's rate.
> + ///
> + /// Consumes the [`Clk`] and returns an [`ExclusiveClk`] that releases the exclusivity
> + /// when dropped. While held, no other consumer may change the clock's rate.
> + ///
> + /// Equivalent to the kernel's [`clk_rate_exclusive_get`] API. Must not be called from
> + /// atomic context.
> + ///
> + /// [`clk_rate_exclusive_get`]:
> + /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_rate_exclusive_get
Avoid the newline here. Links are long, and it's okay that they are
long. They must be on one line.
> + pub fn rate_exclusive_get(self) -> Result<ExclusiveClk> {
> + // SAFETY: By the type invariants, self.as_raw() is a valid argument for
> + // [`clk_rate_exclusive_get`].
> + to_result(unsafe { bindings::clk_rate_exclusive_get(self.as_raw()) })?;
> + Ok(ExclusiveClk(self))
> + }
> }
>
> impl Drop for Clk {
> @@ -329,6 +346,54 @@ fn deref(&self) -> &Clk {
> &self.0
> }
> }
> +
> + /// A [`Clk`] with exclusive control over its rate.
> + ///
> + /// While an [`ExclusiveClk`] exists, no other consumer of the same clock may change its rate.
> + /// Obtained by calling [`Clk::rate_exclusive_get`]; the exclusivity is released automatically
> + /// when the value is dropped, after which the inner [`Clk`] is dropped as usual.
> + ///
> + /// # Invariants
> + ///
> + /// An [`ExclusiveClk`] instance owns a [`Clk`] for which `clk_rate_exclusive_get` has been
> + /// called and the matching `clk_rate_exclusive_put` has not yet been called.
> + ///
> + /// # Examples
> + ///
> + /// ```
> + /// use kernel::clk::{Clk, ExclusiveClk};
> + /// use kernel::device::Device;
> + /// use kernel::error::Result;
> + ///
> + /// fn lock_rate(dev: &Device) -> Result<ExclusiveClk> {
> + /// let clk = Clk::get(dev, None)?;
> + /// clk.prepare_enable()?;
> + /// clk.rate_exclusive_get()
> + /// }
> + /// ```
> + ///
> + /// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html
Unused link.
> + pub struct ExclusiveClk(Clk);
> +
> + // Make [`ExclusiveClk`] behave like [`Clk`].
> + impl Deref for ExclusiveClk {
> + type Target = Clk;
> +
> + fn deref(&self) -> &Clk {
> + &self.0
> + }
> + }
> +
> + impl Drop for ExclusiveClk {
> + fn drop(&mut self) {
> + // SAFETY: By the type invariants, self.as_raw() is a valid argument for
> + // [`clk_rate_exclusive_put`] and balances the [`clk_rate_exclusive_get`] call from
> + // [`Clk::rate_exclusive_get`].
> + unsafe {
> + bindings::clk_rate_exclusive_put(self.as_raw());
> + }
Nit: usually we format this with the semicolon outside the unsafe block.
unsafe { bindings::clk_rate_exclusive_put(self.as_raw()) };
Alice
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get
2026-05-26 17:04 ` [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
@ 2026-06-04 11:17 ` Alice Ryhl
0 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-06-04 11:17 UTC (permalink / raw)
To: mhi
Cc: Michael Turquette, Stephen Boyd, Miguel Ojeda, Boqun Feng,
Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Danilo Krummrich, Drew Fustini, Guo Ren, Fu Wei,
Michal Wilczynski, Uwe Kleine-König, linux-clk,
rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On Tue, May 26, 2026 at 07:04:58PM +0200, Maurice Hieronymus via B4 Relay wrote:
> From: Maurice Hieronymus <mhi@mailbox.org>
>
> The driver derives period and duty cycle from the clock rate read at
> probe, so a later rate change would silently miscompute waveforms.
> Switch to the new ExclusiveClk wrapper to hold the rate for the lifetime
> of the driver data and drop the corresponding TODO.
>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-06-04 11:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-26 17:04 [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus via B4 Relay
2026-05-26 17:04 ` [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
2026-06-04 11:17 ` Alice Ryhl
2026-05-26 17:04 ` [PATCH 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus via B4 Relay
2026-06-04 11:17 ` Alice Ryhl
2026-06-04 6:11 ` [PATCH 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox