[PATCH v5 3/4] rust: clk: add devres-managed clks
Onur Özkan
work at onurozkan.dev
Sat Aug 1 04:22:52 PDT 2026
On Mon, 06 Jul 2026 11:37:14 -0300
Daniel Almeida <daniel.almeida at collabora.com> wrote:
> The clk API allows fine-grained control, but some drivers might be
> more interested in a "set and forget" API.
>
> Expand the current API to support this. The clock will automatically be
> disabled, unprepared and freed when the device is unbound from the bus
> without further intervention by the driver.
>
> Signed-off-by: Daniel Almeida <daniel.almeida at collabora.com>
> ---
> rust/kernel/clk.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 46 insertions(+)
>
> diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs
> index dd5fd656271e..b9a44f83843a 100644
> --- a/rust/kernel/clk.rs
> +++ b/rust/kernel/clk.rs
> @@ -96,6 +96,52 @@ impl Sealed for super::Prepared {}
> impl Sealed for super::Enabled {}
> }
>
> + /// Obtains and enables a [`devres`]-managed [`Clk`] for a bound device.
> + ///
> + /// [`devres`]: crate::devres::Devres
> + #[inline]
> + pub fn devm_enable(dev: &Device<Bound>, name: Option<&CStr>) -> Result {
> + let name = name.map_or(ptr::null(), |n| n.as_char_ptr());
> +
> + // SAFETY: It is safe to call [`devm_clk_get_enabled`] with a valid
Intra-doc links won't work in regular comments.
> + // device pointer.
> + from_err_ptr(unsafe { bindings::devm_clk_get_enabled(dev.as_raw(), name) })?;
> + Ok(())
> + }
> +
> + /// Obtains and enables a [`devres`]-managed [`Clk`] for a bound device.
> + ///
> + /// This does not print any error messages if the clock is not found.
> + ///
> + /// [`devres`]: crate::devres::Devres
> + #[inline]
> + pub fn devm_enable_optional(dev: &Device<Bound>, name: Option<&CStr>) -> Result {
> + let name = name.map_or(ptr::null(), |n| n.as_char_ptr());
> +
> + // SAFETY: It is safe to call [`devm_clk_get_optional_enabled`] with a
> + // valid device pointer.
> + from_err_ptr(unsafe { bindings::devm_clk_get_optional_enabled(dev.as_raw(), name) })?;
> + Ok(())
> + }
> +
> + /// Same as [`devm_enable_optional`], but also sets the rate.
> + #[inline]
> + pub fn devm_enable_optional_with_rate(
> + dev: &Device<Bound>,
> + name: Option<&CStr>,
> + rate: Hertz,
> + ) -> Result {
> + let name = name.map_or(ptr::null(), |n| n.as_char_ptr());
> +
> + // SAFETY: It is safe to call
> + // [`devm_clk_get_optional_enabled_with_rate`] with a valid device
> + // pointer.
> + from_err_ptr(unsafe {
> + bindings::devm_clk_get_optional_enabled_with_rate(dev.as_raw(), name, rate.as_hz())
> + })?;
Just fyi, there's currently a bug in the C implementation of
devm_clk_get_optional_enabled_with_rate() which I caught while reviewing your
series. The fix patch is already on the list [1]. Other than that, this LGTM.
[1]: https://lore.kernel.org/all/20260801111637.304590-1-work@onurozkan.dev
Regards,
Onur
> + Ok(())
> + }
> +
> /// A trait representing the different states that a [`Clk`] can be in.
> pub trait ClkState: private::Sealed {
> /// Whether the clock is enabled in this state.
>
> --
> 2.54.0
>
More information about the linux-riscv
mailing list