* [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520
@ 2026-06-05 6:59 Maurice Hieronymus
2026-06-05 6:59 ` [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Maurice Hieronymus @ 2026-06-05 6:59 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>
---
Changes in v2:
- Rebase on pwm/for-next
- Remove newline after [`clk_rate_exclusive_get`] comment
- Remove unused [`struct clk`] link
- Put semicolon outside of unsafe block
- Link to v1: https://lore.kernel.org/r/20260526-rate-exclusive-get-th1520-v1-0-34cf034e1764@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 | 60 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 5 deletions(-)
---
base-commit: 5b5e33c44a491d5b5e019f527e028bd567a226fa
change-id: 20260526-rate-exclusive-get-th1520-764a949de18e
Best regards,
--
Maurice Hieronymus <mhi@mailbox.org>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
2026-06-05 6:59 [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
@ 2026-06-05 6:59 ` Maurice Hieronymus
2026-06-08 11:09 ` Onur Özkan
2026-06-05 7:00 ` [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus
2026-06-08 11:08 ` [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Daniel Almeida
2 siblings, 1 reply; 7+ messages in thread
From: Maurice Hieronymus @ 2026-06-05 6:59 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 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.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
---
rust/kernel/clk.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
index 7abbd0767d8c..f834f4833f18 100644
--- a/rust/kernel/clk.rs
+++ b/rust/kernel/clk.rs
@@ -249,6 +249,22 @@ 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 +345,50 @@ 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()
+ /// }
+ /// ```
+ 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 [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get
2026-06-05 6:59 [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
2026-06-05 6:59 ` [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus
@ 2026-06-05 7:00 ` Maurice Hieronymus
2026-06-08 10:08 ` Michal Wilczynski
2026-06-08 11:08 ` [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Daniel Almeida
2 siblings, 1 reply; 7+ messages in thread
From: Maurice Hieronymus @ 2026-06-05 7:00 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
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.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
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 [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get
2026-06-05 7:00 ` [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus
@ 2026-06-08 10:08 ` Michal Wilczynski
0 siblings, 0 replies; 7+ messages in thread
From: Michal Wilczynski @ 2026-06-08 10:08 UTC (permalink / raw)
To: Maurice Hieronymus, 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,
Uwe Kleine-König
Cc: linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On 6/5/26 09:00, Maurice Hieronymus wrote:
> 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.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
> ---
> drivers/pwm/pwm_th1520.rs | 8 +++-----
> 1 file changed, 3 insertions(+), 5 deletions(-)
>
Reviewed-by: Michal Wilczynski <m.wilczynski@samsung.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520
2026-06-05 6:59 [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
2026-06-05 6:59 ` [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus
2026-06-05 7:00 ` [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus
@ 2026-06-08 11:08 ` Daniel Almeida
2026-06-08 20:59 ` Maurice Hieronymus
2 siblings, 1 reply; 7+ messages in thread
From: Daniel Almeida @ 2026-06-08 11:08 UTC (permalink / raw)
To: Maurice Hieronymus
Cc: 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,
linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm
Hi Maurice,
There is some ongoing work to convert Clks to a type state pattern [0]
I wonder if you would be willing to rebase on top of that?
I can push a new version if so.
It seems like we are mostly in agreement with that series, and only
a few nits are missing.
— Daniel
[0]: https://lore.kernel.org/rust-for-linux/20260107-clk-type-state-v3-0-77d3e3ee59c2@collabora.com/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
2026-06-05 6:59 ` [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus
@ 2026-06-08 11:09 ` Onur Özkan
0 siblings, 0 replies; 7+ messages in thread
From: Onur Özkan @ 2026-06-08 11:09 UTC (permalink / raw)
To: Maurice Hieronymus
Cc: 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,
linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On Fri, 05 Jun 2026 08:59:59 +0200
Maurice Hieronymus <mhi@mailbox.org> wrote:
> 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.
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Maurice Hieronymus <mhi@mailbox.org>
> ---
> rust/kernel/clk.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 60 insertions(+)
>
> diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
> index 7abbd0767d8c..f834f4833f18 100644
> --- a/rust/kernel/clk.rs
> +++ b/rust/kernel/clk.rs
> @@ -249,6 +249,22 @@ 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))
> + }
We usually inline FFI wrappers.
> }
>
> impl Drop for Clk {
> @@ -329,6 +345,50 @@ 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()
> + /// }
> + /// ```
> + 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`].
I think [`...`] wouldn't work on regular comments. They are only for
doc-comments.
Thanks,
Onur
> + unsafe { bindings::clk_rate_exclusive_put(self.as_raw()) };
> + }
> + }
> }
>
> #[cfg(CONFIG_COMMON_CLK)]
>
> --
> 2.51.2
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520
2026-06-08 11:08 ` [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Daniel Almeida
@ 2026-06-08 20:59 ` Maurice Hieronymus
0 siblings, 0 replies; 7+ messages in thread
From: Maurice Hieronymus @ 2026-06-08 20:59 UTC (permalink / raw)
To: Daniel Almeida
Cc: 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,
linux-clk, rust-for-linux, linux-kernel, linux-riscv, linux-pwm
On Mon, 2026-06-08 at 08:08 -0300, Daniel Almeida wrote:
> Hi Maurice,
>
> There is some ongoing work to convert Clks to a type state pattern
> [0]
>
> I wonder if you would be willing to rebase on top of that?
> I can push a new version if so.
Definitely! Just let me know when you pushed a new version. Thanks for
that.
>
> It seems like we are mostly in agreement with that series, and only
> a few nits are missing.
>
> — Daniel
>
> [0]:
> https://lore.kernel.org/rust-for-linux/20260107-clk-type-state-v3-0-77d3e3ee59c2@collabora.com/
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-06-08 20:59 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-05 6:59 [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Maurice Hieronymus
2026-06-05 6:59 ` [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get Maurice Hieronymus
2026-06-08 11:09 ` Onur Özkan
2026-06-05 7:00 ` [PATCH v2 2/2] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Maurice Hieronymus
2026-06-08 10:08 ` Michal Wilczynski
2026-06-08 11:08 ` [PATCH v2 0/2] rust: clk: Add ExclusiveClk and use it in pwm-th1520 Daniel Almeida
2026-06-08 20:59 ` Maurice Hieronymus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®