From: "Onur Özkan" <work@onurozkan.dev>
To: Maurice Hieronymus <mhi@mailbox.org>
Cc: "Michael Turquette" <mturquette@baylibre.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Drew Fustini" <fustini@kernel.org>,
"Guo Ren" <guoren@kernel.org>, "Fu Wei" <wefu@redhat.com>,
"Michal Wilczynski" <m.wilczynski@samsung.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
linux-clk@vger.kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org,
linux-pwm@vger.kernel.org
Subject: Re: [PATCH v2 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get
Date: Mon, 8 Jun 2026 14:09:13 +0300 [thread overview]
Message-ID: <20260608110920.422773-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260605-rate-exclusive-get-th1520-v2-1-695640bf7249@mailbox.org>
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
>
next prev parent reply other threads:[~2026-06-08 11:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260608110920.422773-1-work@onurozkan.dev \
--to=work@onurozkan.dev \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=fustini@kernel.org \
--cc=gary@garyguo.net \
--cc=guoren@kernel.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lossin@kernel.org \
--cc=m.wilczynski@samsung.com \
--cc=mhi@mailbox.org \
--cc=mturquette@baylibre.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tmgross@umich.edu \
--cc=ukleinek@kernel.org \
--cc=wefu@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®