From nobody Mon Jun 22 10:00:31 2026 Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 221272F8E8C; Thu, 18 Jun 2026 03:48:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754517; cv=pass; b=ddL0dVGngO9j2ielfXa+14VPdedK3YE1gDetoKsUvNxxzVqDsudqJnSDu9GqGy9ZL+qGplgqQm58Tv/k3lI0aJC/sxPZc7S4PRlyocwp2CyF5APwHAG7KDDO03ujT+Y+o4wNiDzHSC38Tg73LlU9myYCS5wj2ZBzdKKeVosMd/g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754517; c=relaxed/simple; bh=qJdPSoJGqUedbyPjOVE5zz/r53uyzTIJysyRRugCRLo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r0WoAvD/0M3PboBXzQxGAyitFmAmAtskGgYCx1qTL1uV682AFPLWkm5YkxBHleyzDLedBR5QTF+P3LhBTdeHw8dg2Tc7p79SQJ+IxlD40X/SQ+SzRUL/pk494rARztNYfBqeI4N0pEjk+7aL65NOLZzyc71jsnx88SzhbSgbxzI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=TAuEfBJW; arc=pass smtp.client-ip=136.143.188.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="TAuEfBJW" ARC-Seal: i=1; a=rsa-sha256; t=1781754485; cv=none; d=zohomail.com; s=zohoarc; b=OG1wDp+ktF/vsdMkO3ijqfWmf8Tai/eAFCTnFlv+H5ZLlgO8Rs6HIW9bu/2vaI1PNSAkNtIy68fx7TtUATO3gKCJNpFGARR2vyKB8TIVMbER8fBxAYbPgklhmvb/0b3HA6tduQ0BUccOW5ghM5ARYlKVYJMoRjrPGPiUDIU8vG8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781754485; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=aqLgWBUxmaoKJ17aWU14RXai5ZCYOP7JKNhAYUwcuxE=; b=bp/xkeHD6BOi44q3D6LHo90Wq4k4GivItnucFyzF/klIs6XDIx+C0HnoIHvNwoPo4kQKIGWc5w4EsGRqXOSHCjTD18iejWv/pkDERdhm1YXGrAOt3omFSsJfu8mUom+1fqsyfAFcFw10yCEY0g8AwSvx933/eux03X9TR/6udT8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781754485; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=aqLgWBUxmaoKJ17aWU14RXai5ZCYOP7JKNhAYUwcuxE=; b=TAuEfBJWAAAEMyyFBetgR+wpgfCYx2YmqvZ11lWh4kziw81QMY8v6wnXi3tLBk3r zXE5k7cPvcwYkbjri04r2i+88hLVJjujL+NxtKm+mDpI6Y7Jg3bNxOlA6sC+32PzryI Sxa3+9rYlO3g8/s8rx618rDv0mBupCpXAasY4FwA= Received: by mx.zohomail.com with SMTPS id 1781754482246334.00043534866245; Wed, 17 Jun 2026 20:48:02 -0700 (PDT) From: Daniel Almeida Date: Thu, 18 Jun 2026 00:46:35 -0300 Subject: [PATCH v4 1/3] rust: clk: use the type-state pattern Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260618-clk-type-state-v4-1-8be082786080@collabora.com> References: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> In-Reply-To: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> To: "Rafael J. Wysocki" , Viresh Kumar , Danilo Krummrich , Alice Ryhl , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Drew Fustini , Guo Ren , Fu Wei , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Turquette , Stephen Boyd , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Daniel Almeida , Michal Wilczynski , Brian Masney , Boqun Feng , Boqun Feng Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org, linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org, rust-for-linux@vger.kernel.org, Boris Brezillon X-Mailer: b4 0.15.2 X-ZohoMailClient: External The current Clk abstraction can still be improved on the following issues: a) It only keeps track of a count to clk_get(), which means that users have to manually call disable() and unprepare(), or a variation of those, like disable_unprepare(). b) It allows repeated calls to prepare() or enable(), but it keeps no track of how often these were called, i.e., it's currently legal to write the following: clk.prepare(); clk.prepare(); clk.enable(); clk.enable(); And nothing gets undone on drop(). c) It adds a OptionalClk type that is probably not needed. There is no "struct optional_clk" in C and we should probably not add one. d) It does not let a user express the state of the clk through the type system. For example, there is currently no way to encode that a Clk is enabled via the type system alone. In light of the Regulator abstraction that was recently merged, switch this abstraction to use the type-state pattern instead. It solves both a) and b) by establishing a number of states and the valid ways to transition between them. It also automatically undoes any call to clk_get(), clk_prepare() and clk_enable() as applicable on drop(), so users do not have to do anything special before Clk goes out of scope. It solves c) by removing the OptionalClk type, which is now simply encoded as a Clk whose inner pointer is NULL. It solves d) by directly encoding the state of the Clk into the type, e.g.: Clk is now known to be a Clk that is enabled. The INVARIANTS section for Clk is expanded to highlight the relationship between the states and the respective reference counts that are owned by each of them. The examples are expanded to highlight how a user can transition between states, as well as highlight some of the shortcuts built into the API. The current implementation is also more flexible, in the sense that it allows for more states to be added in the future. This lets us implement different strategies for handling clocks, including one that mimics the current API, allowing for multiple calls to prepare() and enable(). The users (cpufreq.rs/ rcpufreq_dt.rs) were updated by this patch (and not a separate one) to reflect the new changes. This is needed, because otherwise this patch would break the build. Link: https://crates.io/crates/sealed [1] Signed-off-by: Daniel Almeida --- drivers/cpufreq/rcpufreq_dt.rs | 2 +- drivers/gpu/drm/tyr/driver.rs | 31 +-- drivers/pwm/pwm_th1520.rs | 17 +- rust/kernel/clk.rs | 512 ++++++++++++++++++++++++++++++-------= ---- rust/kernel/cpufreq.rs | 8 +- 5 files changed, 396 insertions(+), 174 deletions(-) diff --git a/drivers/cpufreq/rcpufreq_dt.rs b/drivers/cpufreq/rcpufreq_dt.rs index f17bf64c22e2..9d2ec7df4bac 100644 --- a/drivers/cpufreq/rcpufreq_dt.rs +++ b/drivers/cpufreq/rcpufreq_dt.rs @@ -40,7 +40,7 @@ struct CPUFreqDTDevice { freq_table: opp::FreqTable, _mask: CpumaskVar, _token: Option, - _clk: Clk, + _clk: Clk, } =20 #[derive(Default)] diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs index 279710b36a10..a2230aebfea2 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -3,7 +3,7 @@ use kernel::{ clk::{ Clk, - OptionalClk, // + Enabled, // }, device::{ Bound, @@ -49,7 +49,7 @@ pub(crate) struct TyrPlatformDriverData { _device: ARef, } =20 -#[pin_data(PinnedDrop)] +#[pin_data] pub(crate) struct TyrDrmDeviceData { pub(crate) pdev: ARef, =20 @@ -97,13 +97,9 @@ fn probe( pdev: &platform::Device, _info: Option<&Self::IdInfo>, ) -> impl PinInit { - let core_clk =3D Clk::get(pdev.as_ref(), Some(c"core"))?; - let stacks_clk =3D OptionalClk::get(pdev.as_ref(), Some(c"stacks")= )?; - let coregroup_clk =3D OptionalClk::get(pdev.as_ref(), Some(c"coreg= roup"))?; - - core_clk.prepare_enable()?; - stacks_clk.prepare_enable()?; - coregroup_clk.prepare_enable()?; + let core_clk =3D Clk::::get(pdev.as_ref(), Some(c"core"))= ?; + let stacks_clk =3D Clk::::get_optional(pdev.as_ref(), Som= e(c"stacks"))?; + let coregroup_clk =3D Clk::::get_optional(pdev.as_ref(), = Some(c"coregroup"))?; =20 let mali_regulator =3D Regulator::::get(pdev.a= s_ref(), c"mali")?; let sram_regulator =3D Regulator::::get(pdev.a= s_ref(), c"sram")?; @@ -150,17 +146,6 @@ impl PinnedDrop for TyrPlatformDriverData { fn drop(self: Pin<&mut Self>) {} } =20 -#[pinned_drop] -impl PinnedDrop for TyrDrmDeviceData { - fn drop(self: Pin<&mut Self>) { - // TODO: the type-state pattern for Clks will fix this. - let clks =3D self.clks.lock(); - clks.core.disable_unprepare(); - clks.stacks.disable_unprepare(); - clks.coregroup.disable_unprepare(); - } -} - // We need to retain the name "panthor" to achieve drop-in compatibility w= ith // the C driver in the userspace stack. const INFO: drm::DriverInfo =3D drm::DriverInfo { @@ -186,9 +171,9 @@ impl drm::Driver for TyrDrmDriver { =20 #[pin_data] struct Clocks { - core: Clk, - stacks: OptionalClk, - coregroup: OptionalClk, + core: Clk, + stacks: Clk, + coregroup: Clk, } =20 #[pin_data] diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs index ddd44a5ce497..343a1178c5e7 100644 --- a/drivers/pwm/pwm_th1520.rs +++ b/drivers/pwm/pwm_th1520.rs @@ -22,7 +22,7 @@ =20 use core::ops::Deref; use kernel::{ - clk::Clk, + clk::{Clk, Enabled}, device::{Bound, Core, Device}, devres, io::{ @@ -89,11 +89,11 @@ struct Th1520WfHw { } =20 /// The driver's private data struct. It holds all necessary devres manage= d resources. -#[pin_data(PinnedDrop)] +#[pin_data] struct Th1520PwmDriverData { #[pin] iomem: devres::Devres>, - clk: Clk, + clk: Clk, } =20 impl pwm::PwmOps for Th1520PwmDriverData { @@ -298,13 +298,6 @@ fn write_waveform( } } =20 -#[pinned_drop] -impl PinnedDrop for Th1520PwmDriverData { - fn drop(self: Pin<&mut Self>) { - self.clk.disable_unprepare(); - } -} - struct Th1520PwmPlatformDriver; =20 kernel::of_device_table!( @@ -325,9 +318,7 @@ fn probe( let dev =3D pdev.as_ref(); let request =3D pdev.io_request_by_index(0).ok_or(ENODEV)?; =20 - let clk =3D Clk::get(dev, None)?; - - clk.prepare_enable()?; + let clk =3D Clk::::get(dev, None)?; =20 // TODO: Get exclusive ownership of the clock to prevent rate chan= ges. // The Rust equivalent of `clk_rate_exclusive_get()` is not yet av= ailable. diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index 7abbd0767d8c..a62e4c7e252e 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -80,17 +80,103 @@ fn from(freq: Hertz) -> Self { mod common_clk { use super::Hertz; use crate::{ - device::Device, + device::{Bound, Device}, error::{from_err_ptr, to_result, Result}, prelude::*, }; =20 - use core::{ops::Deref, ptr}; + use core::{marker::PhantomData, mem::ManuallyDrop, ptr}; + + mod private { + pub trait Sealed {} + + impl Sealed for super::Unprepared {} + impl Sealed for super::Prepared {} + impl Sealed for super::Enabled {} + } + + /// A trait representing the different states that a [`Clk`] can be in. + pub trait ClkState: private::Sealed { + /// Whether the clock should be disabled when dropped. + const DISABLE_ON_DROP: bool; + + /// Whether the clock should be unprepared when dropped. + const UNPREPARE_ON_DROP: bool; + } + + /// A state where the [`Clk`] is not prepared and not enabled. + pub struct Unprepared; + + /// A state where the [`Clk`] is prepared but not enabled. + pub struct Prepared; + + /// A state where the [`Clk`] is both prepared and enabled. + pub struct Enabled; + + impl ClkState for Unprepared { + const DISABLE_ON_DROP: bool =3D false; + const UNPREPARE_ON_DROP: bool =3D false; + } + + impl ClkState for Prepared { + const DISABLE_ON_DROP: bool =3D false; + const UNPREPARE_ON_DROP: bool =3D true; + } + + impl ClkState for Enabled { + const DISABLE_ON_DROP: bool =3D true; + const UNPREPARE_ON_DROP: bool =3D true; + } + + /// An error that can occur when trying to convert a [`Clk`] between s= tates. + pub struct Error { + /// The error that occurred. + pub error: kernel::error::Error, + + /// The [`Clk`] that caused the error, so that the operation may be + /// retried. + pub clk: Clk, + } + + impl From> for kernel::error::Error { + /// Discards the [`Clk`] and keeps only the error code. + /// + /// This makes the fallible state transitions usable with the `?` + /// operator when the caller does not need to retry the operation = on the + /// original [`Clk`], e.g.: + /// + /// ``` + /// use kernel::clk::{Clk, Enabled, Unprepared}; + /// use kernel::device::{Bound, Device}; + /// use kernel::error::Result; + /// + /// fn get_enabled(dev: &Device) -> Result> { + /// let clk =3D Clk::::get(dev, Some(c"apb_clk"))? + /// .prepare()? + /// .enable()?; + /// Ok(clk) + /// } + /// ``` + #[inline] + fn from(err: Error) -> Self { + err.error + } + } =20 /// A reference-counted clock. /// /// Rust abstraction for the C [`struct clk`]. /// + /// A [`Clk`] instance represents a clock that can be in one of several + /// states: [`Unprepared`], [`Prepared`], or [`Enabled`]. + /// + /// No action needs to be taken when a [`Clk`] is dropped. The calls to + /// `clk_unprepare()` and `clk_disable()` will be placed as applicable. + /// + /// An optional [`Clk`] is treated just like a regular [`Clk`], but its + /// inner `struct clk` pointer is `NULL`. This interfaces correctly wi= th the + /// C API and also exposes all the methods of a regular [`Clk`] to use= rs. + /// /// # Invariants /// /// A [`Clk`] instance holds either a pointer to a valid [`struct clk`= ] created by the C @@ -99,19 +185,37 @@ mod common_clk { /// Instances of this type are reference-counted. Calling [`Clk::get`]= ensures that the /// allocation remains valid for the lifetime of the [`Clk`]. /// + /// The [`Prepared`] state is associated with a single count of + /// `clk_prepare()`, and the [`Enabled`] state is associated with a si= ngle + /// count of `clk_enable()`, and the [`Prepared`] state is associated = with a + /// single count of `clk_prepare()` and `clk_enable()`. + /// + /// All states are associated with a single count of `clk_get()`. + /// /// # Examples /// /// The following example demonstrates how to obtain and configure a c= lock for a device. /// /// ``` - /// use kernel::clk::{Clk, Hertz}; - /// use kernel::device::Device; + /// use kernel::clk::{Clk, Enabled, Hertz, Unprepared, Prepared}; + /// use kernel::device::{Bound, Device}; /// use kernel::error::Result; /// - /// fn configure_clk(dev: &Device) -> Result { - /// let clk =3D Clk::get(dev, Some(c"apb_clk"))?; + /// fn configure_clk(dev: &Device) -> Result { + /// // The fastest way is to use a version of `Clk::get` for the d= esired + /// // state, i.e.: + /// let clk: Clk =3D Clk::::get(dev, Some(c"apb_= clk"))?; + /// + /// // Any other state is also possible, e.g.: + /// let clk: Clk =3D Clk::::get(dev, Some(c"ap= b_clk"))?; /// - /// clk.prepare_enable()?; + /// // Later: + /// // + /// // `?` works directly thanks to `From>`; the fail= ed + /// // `Clk` is dropped on error. Match on the returned `Error` + /// // instead (its `clk` field is the original `Clk`) if you want= to + /// // retry the operation. + /// let clk: Clk =3D clk.enable()?; /// /// let expected_rate =3D Hertz::from_ghz(1); /// @@ -119,111 +223,300 @@ mod common_clk { /// clk.set_rate(expected_rate)?; /// } /// - /// clk.disable_unprepare(); + /// // Nothing is needed here. The drop implementation will undo a= ny + /// // operations as appropriate. + /// Ok(()) + /// } + /// + /// fn shutdown(clk: Clk) -> Result { + /// // The states can be traversed "in the reverse order" as well: + /// let clk: Clk =3D clk.disable()?; + /// + /// // This is of type `Clk`. + /// let clk =3D clk.unprepare(); + /// /// Ok(()) /// } /// ``` /// + /// Drivers that need to change a clock's state at runtime (for exampl= e to + /// enable it on resume and disable it on suspend) can keep it in an e= num + /// and move between the variants: + /// + /// ``` + /// use kernel::clk::{Clk, Enabled, Prepared}; + /// use kernel::error::Result; + /// + /// enum DeviceClk { + /// Suspended(Clk), + /// Resumed(Clk), + /// } + /// + /// impl DeviceClk { + /// fn resume(self) -> Result { + /// Ok(match self { + /// DeviceClk::Suspended(clk) =3D> DeviceClk::Resumed(clk.= enable()?), + /// resumed =3D> resumed, + /// }) + /// } + /// + /// fn suspend(self) -> Result { + /// Ok(match self { + /// DeviceClk::Resumed(clk) =3D> DeviceClk::Suspended(clk.= disable()?), + /// suspended =3D> suspended, + /// }) + /// } + /// } + /// ``` + /// /// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html #[repr(transparent)] - pub struct Clk(*mut bindings::clk); + pub struct Clk { + inner: *mut bindings::clk, + _phantom: core::marker::PhantomData, + } =20 // SAFETY: It is safe to call `clk_put` on another thread than where `= clk_get` was called. - unsafe impl Send for Clk {} + unsafe impl Send for Clk {} =20 // SAFETY: It is safe to call any combination of the `&self` methods i= n parallel, as the // methods are synchronized internally. - unsafe impl Sync for Clk {} + unsafe impl Sync for Clk {} =20 - impl Clk { - /// Gets [`Clk`] corresponding to a [`Device`] and a connection id. + impl Clk { + /// Gets [`Clk`] corresponding to a bound [`Device`] and a connect= ion + /// id. /// /// Equivalent to the kernel's [`clk_get`] API. /// /// [`clk_get`]: https://docs.kernel.org/core-api/kernel-api.html#= c.clk_get - pub fn get(dev: &Device, name: Option<&CStr>) -> Result { + #[inline] + pub fn get(dev: &Device, name: Option<&CStr>) -> Result> { + Self::get_unbound(dev, name) + } + + /// Gets [`Clk`] corresponding to a [`Device`] and a connection id, + /// without requiring the device to be bound. + /// + /// This is sound because [`clk_get`] and [`clk_put`] do not depen= d on the + /// device being bound to a driver. It is `pub(crate)` because a d= river + /// should obtain its clocks through a bound device (see [`Clk::ge= t`]); it + /// is meant for the few in-tree abstractions that operate on a de= vice + /// outside a bind scope, such as the generic DT cpufreq driver. + /// + /// [`clk_get`]: https://docs.kernel.org/core-api/kernel-api.html#= c.clk_get + #[inline] + pub(crate) fn get_unbound(dev: &Device, name: Option<&CStr>) -> Re= sult> { let con_id =3D name.map_or(ptr::null(), |n| n.as_char_ptr()); =20 - // SAFETY: It is safe to call [`clk_get`] for a valid device p= ointer. - // + // SAFETY: It is safe to call [`clk_get`] for a valid device p= ointer + // and any `con_id`, including NULL. + let inner =3D from_err_ptr(unsafe { bindings::clk_get(dev.as_r= aw(), con_id) })?; + // INVARIANT: The reference-count is decremented when [`Clk`] = goes out of scope. - Ok(Self(from_err_ptr(unsafe { - bindings::clk_get(dev.as_raw(), con_id) - })?)) + Ok(Self { + inner, + _phantom: PhantomData, + }) } =20 - /// Obtain the raw [`struct clk`] pointer. + /// Behaves the same as [`Self::get`], except when there is no clo= ck + /// producer. In this case, instead of returning [`ENOENT`], it re= turns + /// a dummy [`Clk`]. #[inline] - pub fn as_raw(&self) -> *mut bindings::clk { - self.0 + pub fn get_optional(dev: &Device, name: Option<&CStr>) -> R= esult> { + let con_id =3D name.map_or(ptr::null(), |n| n.as_char_ptr()); + + // SAFETY: It is safe to call [`clk_get`] for a valid device p= ointer + // and any `con_id`, including NULL. + let inner =3D from_err_ptr(unsafe { bindings::clk_get_optional= (dev.as_raw(), con_id) })?; + + // INVARIANT: The reference-count is decremented when [`Clk`] = goes out of scope. + Ok(Self { + inner, + _phantom: PhantomData, + }) } =20 - /// Enable the clock. + /// Attempts to convert the [`Clk`] to a [`Prepared`] state. /// - /// Equivalent to the kernel's [`clk_enable`] API. + /// Equivalent to the kernel's [`clk_prepare`] API. /// - /// [`clk_enable`]: https://docs.kernel.org/core-api/kernel-api.ht= ml#c.clk_enable + /// [`clk_prepare`]: https://docs.kernel.org/core-api/kernel-api.h= tml#c.clk_prepare #[inline] - pub fn enable(&self) -> Result { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_enable`]. - to_result(unsafe { bindings::clk_enable(self.as_raw()) }) + pub fn prepare(self) -> Result, Error> { + // We will be transferring the ownership of our `clk_get()` co= unt to + // `Clk`. + let clk =3D ManuallyDrop::new(self); + + // SAFETY: By the type invariants, `self.0` is a valid argumen= t for + // [`clk_prepare`]. + to_result(unsafe { bindings::clk_prepare(clk.as_raw()) }) + .map(|()| Clk { + inner: clk.inner, + _phantom: PhantomData, + }) + .map_err(|error| Error { + error, + clk: ManuallyDrop::into_inner(clk), + }) } + } =20 - /// Disable the clock. + impl Clk { + /// Obtains a [`Clk`] from a bound [`Device`] and a connection id = and + /// prepares it. /// - /// Equivalent to the kernel's [`clk_disable`] API. + /// Equivalent to calling [`Clk::get`], followed by [`Clk::prepare= `], + #[inline] + pub fn get(dev: &Device, name: Option<&CStr>) -> Result> { + Clk::::get(dev, name)? + .prepare() + .map_err(|error| error.error) + } + + /// Behaves the same as [`Self::get`], except when there is no clo= ck + /// producer. In this case, instead of returning [`ENOENT`], it re= turns + /// a dummy [`Clk`]. + #[inline] + pub fn get_optional(dev: &Device, name: Option<&CStr>) -> R= esult> { + Clk::::get_optional(dev, name)? + .prepare() + .map_err(|error| error.error) + } + + /// Attempts to convert the [`Clk`] to an [`Unprepared`] state. /// - /// [`clk_disable`]: https://docs.kernel.org/core-api/kernel-api.h= tml#c.clk_disable + /// Equivalent to the kernel's [`clk_unprepare`] API. #[inline] - pub fn disable(&self) { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_disable`]. - unsafe { bindings::clk_disable(self.as_raw()) }; + pub fn unprepare(self) -> Clk { + // We will be transferring the ownership of our `clk_get()` co= unt to + // `Clk`. + let clk =3D ManuallyDrop::new(self); + + // SAFETY: By the type invariants, `self.0` is a valid argumen= t for + // [`clk_unprepare`]. + unsafe { bindings::clk_unprepare(clk.as_raw()) } + + Clk { + inner: clk.inner, + _phantom: PhantomData, + } } =20 - /// Prepare the clock. + /// Attempts to convert the [`Clk`] to an [`Enabled`] state. /// - /// Equivalent to the kernel's [`clk_prepare`] API. + /// Equivalent to the kernel's [`clk_enable`] API. /// - /// [`clk_prepare`]: https://docs.kernel.org/core-api/kernel-api.h= tml#c.clk_prepare + /// [`clk_enable`]: https://docs.kernel.org/core-api/kernel-api.ht= ml#c.clk_enable #[inline] - pub fn prepare(&self) -> Result { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_prepare`]. - to_result(unsafe { bindings::clk_prepare(self.as_raw()) }) + pub fn enable(self) -> Result, Error> { + // We will be transferring the ownership of our `clk_get()` and + // `clk_prepare()` counts to `Clk`. + let clk =3D ManuallyDrop::new(self); + + // SAFETY: By the type invariants, `self.0` is a valid argumen= t for + // [`clk_enable`]. + to_result(unsafe { bindings::clk_enable(clk.as_raw()) }) + .map(|()| Clk { + inner: clk.inner, + _phantom: PhantomData, + }) + .map_err(|error| Error { + error, + clk: ManuallyDrop::into_inner(clk), + }) } =20 - /// Unprepare the clock. + /// Runs `cb` with the clock temporarily enabled. /// - /// Equivalent to the kernel's [`clk_unprepare`] API. + /// The clock is enabled before `cb` runs and disabled again after= wards, + /// so the [`Enabled`] state is scoped to the closure and the [`Cl= k`] + /// remains [`Prepared`]. This is convenient for drivers that only= need + /// the clock running for a short, well-defined section (e.g. while + /// touching registers) without giving up ownership of the prepared + /// clock or threading it through an intermediate state, e.g.: + /// + /// ``` + /// use kernel::clk::{Clk, Enabled, Hertz, Prepared}; + /// use kernel::error::Result; /// - /// [`clk_unprepare`]: https://docs.kernel.org/core-api/kernel-api= .html#c.clk_unprepare + /// fn read_rate(clk: &Clk) -> Result { + /// clk.with_enabled(|clk: &Clk| clk.rate()) + /// } + /// ``` + /// + /// Equivalent to a balanced [`clk_enable`]/[`clk_disable`] pair a= round + /// `cb`. + /// + /// [`clk_enable`]: https://docs.kernel.org/core-api/kernel-api.ht= ml#c.clk_enable + /// [`clk_disable`]: https://docs.kernel.org/core-api/kernel-api.h= tml#c.clk_disable #[inline] - pub fn unprepare(&self) { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_unprepare`]. - unsafe { bindings::clk_unprepare(self.as_raw()) }; + pub fn with_enabled(&self, cb: impl FnOnce(&Clk) -> R)= -> Result { + // SAFETY: By the type invariants, `self.as_raw()` is a valid = argument for + // [`clk_enable`]. + to_result(unsafe { bindings::clk_enable(self.as_raw()) })?; + + // Borrow the same clock as `Clk` for the duration of= `cb`. + // It must not be dropped, as that would run `clk_disable`/`cl= k_put` + // against counts owned by `self`; the matching `clk_disable` = below + // balances the `clk_enable` above instead. + // + // INVARIANT: The clock is enabled for the lifetime of `enable= d`. + let enabled =3D ManuallyDrop::new(Clk:: { + inner: self.inner, + _phantom: PhantomData, + }); + + let ret =3D cb(&enabled); + + // SAFETY: The `clk_enable` above succeeded, so this balances = it. + // `cb` only had a shared reference, so the enable count is un= changed. + unsafe { bindings::clk_disable(self.as_raw()) }; + + Ok(ret) } + } =20 - /// Prepare and enable the clock. + impl Clk { + /// Gets [`Clk`] corresponding to a bound [`Device`] and a connect= ion id + /// and then prepares and enables it. /// - /// Equivalent to calling [`Clk::prepare`] followed by [`Clk::enab= le`]. + /// Equivalent to calling [`Clk::get`], followed by [`Clk::prepare= `], + /// followed by [`Clk::enable`]. #[inline] - pub fn prepare_enable(&self) -> Result { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_prepare_enable`]. - to_result(unsafe { bindings::clk_prepare_enable(self.as_raw())= }) + pub fn get(dev: &Device, name: Option<&CStr>) -> Result> { + Clk::::get(dev, name)? + .enable() + .map_err(|error| error.error) } =20 - /// Disable and unprepare the clock. - /// - /// Equivalent to calling [`Clk::disable`] followed by [`Clk::unpr= epare`]. + /// Behaves the same as [`Self::get`], except when there is no clo= ck + /// producer. In this case, instead of returning [`ENOENT`], it re= turns + /// a dummy [`Clk`]. #[inline] - pub fn disable_unprepare(&self) { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for - // [`clk_disable_unprepare`]. - unsafe { bindings::clk_disable_unprepare(self.as_raw()) }; + pub fn get_optional(dev: &Device, name: Option<&CStr>) -> R= esult> { + Clk::::get_optional(dev, name)? + .enable() + .map_err(|error| error.error) + } + + /// Attempts to disable the [`Clk`] and convert it to the [`Prepar= ed`] + /// state. + #[inline] + pub fn disable(self) -> Result, Error> { + // We will be transferring the ownership of our `clk_get()` and + // `clk_enable()` counts to `Clk`. + let clk =3D ManuallyDrop::new(self); + + // SAFETY: By the type invariants, `self.0` is a valid argumen= t for + // [`clk_disable`]. + unsafe { bindings::clk_disable(clk.as_raw()) }; + + Ok(Clk { + inner: clk.inner, + _phantom: PhantomData, + }) } =20 /// Get clock's rate. @@ -251,82 +544,31 @@ pub fn set_rate(&self, rate: Hertz) -> Result { } } =20 - impl Drop for Clk { - fn drop(&mut self) { - // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for [`clk_put`]. - unsafe { bindings::clk_put(self.as_raw()) }; - } - } - - /// A reference-counted optional clock. - /// - /// A lightweight wrapper around an optional [`Clk`]. An [`OptionalClk= `] represents a [`Clk`] - /// that a driver can function without but may improve performance or = enable additional - /// features when available. - /// - /// # Invariants - /// - /// An [`OptionalClk`] instance encapsulates a [`Clk`] with either a v= alid [`struct clk`] or - /// `NULL` pointer. - /// - /// Instances of this type are reference-counted. Calling [`OptionalCl= k::get`] ensures that the - /// allocation remains valid for the lifetime of the [`OptionalClk`]. - /// - /// # Examples - /// - /// The following example demonstrates how to obtain and configure an = optional clock for a - /// device. The code functions correctly whether or not the clock is a= vailable. - /// - /// ``` - /// use kernel::clk::{OptionalClk, Hertz}; - /// use kernel::device::Device; - /// use kernel::error::Result; - /// - /// fn configure_clk(dev: &Device) -> Result { - /// let clk =3D OptionalClk::get(dev, Some(c"apb_clk"))?; - /// - /// clk.prepare_enable()?; - /// - /// let expected_rate =3D Hertz::from_ghz(1); - /// - /// if clk.rate() !=3D expected_rate { - /// clk.set_rate(expected_rate)?; - /// } - /// - /// clk.disable_unprepare(); - /// Ok(()) - /// } - /// ``` - /// - /// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html - pub struct OptionalClk(Clk); - - impl OptionalClk { - /// Gets [`OptionalClk`] corresponding to a [`Device`] and a conne= ction id. - /// - /// Equivalent to the kernel's [`clk_get_optional`] API. - /// - /// [`clk_get_optional`]: - /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_get_opt= ional - pub fn get(dev: &Device, name: Option<&CStr>) -> Result { - let con_id =3D name.map_or(ptr::null(), |n| n.as_char_ptr()); - - // SAFETY: It is safe to call [`clk_get_optional`] for a valid= device pointer. - // - // INVARIANT: The reference-count is decremented when [`Option= alClk`] goes out of - // scope. - Ok(Self(Clk(from_err_ptr(unsafe { - bindings::clk_get_optional(dev.as_raw(), con_id) - })?))) + impl Clk { + /// Obtain the raw [`struct clk`] pointer. + #[inline] + pub fn as_raw(&self) -> *mut bindings::clk { + self.inner } } =20 - // Make [`OptionalClk`] behave like [`Clk`]. - impl Deref for OptionalClk { - type Target =3D Clk; + impl Drop for Clk { + fn drop(&mut self) { + if T::DISABLE_ON_DROP { + // SAFETY: By the type invariants, self.as_raw() is a vali= d argument for + // [`clk_disable`]. + unsafe { bindings::clk_disable(self.as_raw()) }; + } + + if T::UNPREPARE_ON_DROP { + // SAFETY: By the type invariants, self.as_raw() is a vali= d argument for + // [`clk_unprepare`]. + unsafe { bindings::clk_unprepare(self.as_raw()) }; + } =20 - fn deref(&self) -> &Clk { - &self.0 + // SAFETY: By the type invariants, self.as_raw() is a valid ar= gument for + // [`clk_put`]. + unsafe { bindings::clk_put(self.as_raw()) }; } } } diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs index d8d26870bea2..e837bb1010e0 100644 --- a/rust/kernel/cpufreq.rs +++ b/rust/kernel/cpufreq.rs @@ -553,8 +553,12 @@ pub fn cpus(&mut self) -> &mut cpumask::Cpumask { /// The caller must guarantee that the returned [`Clk`] is not dropped= while it is getting used /// by the C code. #[cfg(CONFIG_COMMON_CLK)] - pub unsafe fn set_clk(&mut self, dev: &Device, name: Option<&CStr>) ->= Result { - let clk =3D Clk::get(dev, name)?; + pub unsafe fn set_clk( + &mut self, + dev: &Device, + name: Option<&CStr>, + ) -> Result> { + let clk =3D Clk::::get_unbound(dev, name)?; self.as_mut_ref().clk =3D clk.as_raw(); Ok(clk) } --=20 2.54.0 From nobody Mon Jun 22 10:00:31 2026 Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 220A71A680E; Thu, 18 Jun 2026 03:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754516; cv=pass; b=PHDfIwzJfaKuF3TERmSE5zEeT/Iib935Ql/qyhqXJ5YnTYU3iakVP7DD+9l4samnyYdT8kn9/CnsmODo0OWeWZ4dpcRXLyTAmuGzT2V/8OpZHaxbaGO6GgP7uQ5jex4GWJjvN9evJsQdRDNf4YvKYmXSUA8sFlr188anQxFMKuA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754516; c=relaxed/simple; bh=K92vzeozg+SMr3UKF6yC0Ojt9r2rQ2iPC4WnligerkU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sFj8SkNCdAPRk7mA7lv30D837pw0/v95iQn5bN85D2TGIY2GqQ/HT5vEexniClyNsZppcxsA7iC+gN68Bobb2Q4ZyS1jX8Ajk4HWK1j2LsG9+u7ObXQjigXcKL+qKSTF4Pah3LA9z+4G6KAeF3zhRAeijzXq1gDMRe5MUOWS4YY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=Pt0xR7Z9; arc=pass smtp.client-ip=136.143.188.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="Pt0xR7Z9" ARC-Seal: i=1; a=rsa-sha256; t=1781754491; cv=none; d=zohomail.com; s=zohoarc; b=e8XJdbebNb5ZqyLSgzsuJ5kUpz5BTTGNsTCyMxEIrLmYyyZPeK12AeUBDRtQo5egC8O+YB+UA3XgjM1t8WaXeIE1jrTFJf97ulB3D9FlWtEhVKlMyxT/lIe4o5DGlQaUCrflyZ/+92yUh9FoqqmRW9O747SMK+uk1BC/fjtR3+Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781754491; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=B+7IWRo1JL0H3u9ZN7rfcBGr3kSsxqV+wwvQWMVfHts=; b=X6qnfqbMQZZl+Lzf/oejQHA+ukw8KbphpXUUaerYwOBkgc1KC38bRwFo4Zz1/NJ8aunDPWiiEm21Q3NjWi3XeILb+6ggVk2rVqgpZnWxvrRuINMs6gTiR19dAJyv1diprTNxfaOg9GnljKHXZ/nfGRt/jOeNMUKDWsMPj4oPufA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781754491; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=B+7IWRo1JL0H3u9ZN7rfcBGr3kSsxqV+wwvQWMVfHts=; b=Pt0xR7Z9nRyDeIqOYI3KNR+9uauRv/jfznC6PmnoaX1VPH7XnLMQczfTn7xAnyMr BIx4JUFTj9cAUu7Gn73TNejalfhPT3b6Q0gMqFGGD7GT2qJBeoZVFiogoYZR7YjYZzg Pobi5Hbuw+tSgae/hiE0/TdOxl8Jd9V7sdwfchXs= Received: by mx.zohomail.com with SMTPS id 1781754488942419.2252700043547; Wed, 17 Jun 2026 20:48:08 -0700 (PDT) From: Daniel Almeida Date: Thu, 18 Jun 2026 00:46:36 -0300 Subject: [PATCH v4 2/3] rust: clk: add devres-managed clks Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260618-clk-type-state-v4-2-8be082786080@collabora.com> References: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> In-Reply-To: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> To: "Rafael J. Wysocki" , Viresh Kumar , Danilo Krummrich , Alice Ryhl , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Drew Fustini , Guo Ren , Fu Wei , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Turquette , Stephen Boyd , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Daniel Almeida , Michal Wilczynski , Brian Masney , Boqun Feng , Boqun Feng Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org, linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org, rust-for-linux@vger.kernel.org, Boris Brezillon X-Mailer: b4 0.15.2 X-ZohoMailClient: External 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 --- rust/kernel/clk.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index a62e4c7e252e..692ee88ca772 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -95,6 +95,49 @@ impl Sealed for super::Prepared {} impl Sealed for super::Enabled {} } =20 + /// Obtains and enables a [`devres`]-managed [`Clk`] for a bound devic= e. + /// + /// [`devres`]: crate::devres::Devres + pub fn devm_enable(dev: &Device, name: Option<&CStr>) -> Result= { + let name =3D name.map_or(ptr::null(), |n| n.as_char_ptr()); + + // SAFETY: It is safe to call [`devm_clk_get_enabled`] with a valid + // 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 devic= e. + /// + /// This does not print any error messages if the clock is not found. + /// + /// [`devres`]: crate::devres::Devres + pub fn devm_enable_optional(dev: &Device, name: Option<&CStr>) = -> Result { + let name =3D name.map_or(ptr::null(), |n| n.as_char_ptr()); + + // SAFETY: It is safe to call [`devm_clk_get_optional_enabled`] wi= th 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. + pub fn devm_enable_optional_with_rate( + dev: &Device, + name: Option<&CStr>, + rate: Hertz, + ) -> Result { + let name =3D 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()) + })?; + Ok(()) + } + /// A trait representing the different states that a [`Clk`] can be in. pub trait ClkState: private::Sealed { /// Whether the clock should be disabled when dropped. --=20 2.54.0 From nobody Mon Jun 22 10:00:31 2026 Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E61A298CB2; Thu, 18 Jun 2026 03:48:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.11 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754522; cv=pass; b=eK4UYe/HV8SOVY7Zm1CA+IFhnr4qmAXkvUMjsjBqMqmQ94fPnNmu5IAF3UaRz0POrtfeEhNVWtgwS8cltXZXL1hyfySNAqbyB0BYQVB+oMHO1Cyt8JM2pH1W27WI6zne/i+z9RA2Tw9+wlogoY42CNFcTP6aMb65hTotJ6WsHl0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781754522; c=relaxed/simple; bh=vmpQMgFf/eBZeeZ4Ycjvtn/NICT/KZSpluoGRjIPNdw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uUKJ2XvgN0enkWCqUL1ieiQPvonwrpwq+IpBbV7krduDVNCdUmW2V92GIRrQg6v1ndtqmUvL283S0B1Svt0UIfiAlJ25Yr4Fs17MWUEe2aSCG0xrPqrgpfZzstJkV0rkR58cx8d0Ej81NI0L08Ut0gFf76VvcZM1pqDRIg7sCII= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=APn++8oq; arc=pass smtp.client-ip=136.143.188.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="APn++8oq" ARC-Seal: i=1; a=rsa-sha256; t=1781754498; cv=none; d=zohomail.com; s=zohoarc; b=mNXOKcw9uzCiiYr5O9KI4cBXfISXVa5n9kATef+RRuQ7wWtji9H+phX7Vnl+CO1/cXeFBhbgYl41JnJ4OZKw+RBiz17jvQrCSJAYW3v6sVj0L6NRuUQV3sXeMmMAn82G//2YrkcSTsJUoFvddbu70aHG/LQF8wtbT3xx1WYpdjg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781754498; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=U2lJv0/DE3CCJO9+3Z4st8Eh7wfAyQlNJPVjWBCZG3U=; b=hSjS4IQnIcFzPUorUV3amUfN+UL1yh7XGTtDtbKKFF8OtyNca7PeM1ZIAGWoTL1mpHEZw6RKaKvqkhepVdlv0+W0ejkpFFwbuqMO/j1w6RpQhb3OIVQ+r37PcY9C6PeXNoiPJsOpYqinastYnKDVWm0LfQ7CQytS8ByVo+EHwDk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1781754498; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=U2lJv0/DE3CCJO9+3Z4st8Eh7wfAyQlNJPVjWBCZG3U=; b=APn++8oq3SbVjjPkB0W+AeYEi4qEdpH7NvFESoL6kiFyxQkL2MlsJJSlh/Lmwtc6 DhmWKbyWgbDZe0ChM4NFZj2zW8rc5KyW9TYaov4K1R1Uoe0EPvHiYb2n4D2fo/cftAF jYgn5x2BDFc2wOttOU4iVtD30mai4ZSz0VM5Zreg= Received: by mx.zohomail.com with SMTPS id 1781754495642490.6198508965592; Wed, 17 Jun 2026 20:48:15 -0700 (PDT) From: Daniel Almeida Date: Thu, 18 Jun 2026 00:46:37 -0300 Subject: [PATCH v4 3/3] rust: clk: use 'kernel vertical style' for imports Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260618-clk-type-state-v4-3-8be082786080@collabora.com> References: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> In-Reply-To: <20260618-clk-type-state-v4-0-8be082786080@collabora.com> To: "Rafael J. Wysocki" , Viresh Kumar , Danilo Krummrich , Alice Ryhl , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Drew Fustini , Guo Ren , Fu Wei , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Turquette , Stephen Boyd , Miguel Ojeda , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Daniel Almeida , Michal Wilczynski , Brian Masney , Boqun Feng , Boqun Feng Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org, linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org, rust-for-linux@vger.kernel.org, Boris Brezillon X-Mailer: b4 0.15.2 X-ZohoMailClient: External Convert all imports to use the new import style. This will make it easier to land new changes in the future. No change of functionality implied. Link: https://docs.kernel.org/rust/coding-guidelines.html#imports Signed-off-by: Daniel Almeida Reviewed-by: Onur =C3=96zkan --- rust/kernel/clk.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++------= ---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs index 692ee88ca772..1412a2f0aedf 100644 --- a/rust/kernel/clk.rs +++ b/rust/kernel/clk.rs @@ -80,12 +80,23 @@ fn from(freq: Hertz) -> Self { mod common_clk { use super::Hertz; use crate::{ - device::{Bound, Device}, - error::{from_err_ptr, to_result, Result}, - prelude::*, + device::{ + Bound, + Device, // + }, + error::{ + from_err_ptr, + to_result, + Result, // + }, + prelude::*, // }; =20 - use core::{marker::PhantomData, mem::ManuallyDrop, ptr}; + use core::{ + marker::PhantomData, + mem::ManuallyDrop, + ptr, // + }; =20 mod private { pub trait Sealed {} @@ -189,8 +200,15 @@ impl From> for kernel::e= rror::Error { /// original [`Clk`], e.g.: /// /// ``` - /// use kernel::clk::{Clk, Enabled, Unprepared}; - /// use kernel::device::{Bound, Device}; + /// use kernel::clk::{ + /// Clk, + /// Enabled, + /// Unprepared, // + /// }; + /// use kernel::device::{ + /// Bound, + /// Device, // + /// }; /// use kernel::error::Result; /// /// fn get_enabled(dev: &Device) -> Result> { @@ -240,8 +258,17 @@ fn from(err: Error) -> Self { /// The following example demonstrates how to obtain and configure a c= lock for a device. /// /// ``` - /// use kernel::clk::{Clk, Enabled, Hertz, Unprepared, Prepared}; - /// use kernel::device::{Bound, Device}; + /// use kernel::clk::{ + /// Clk, + /// Enabled, + /// Hertz, + /// Prepared, + /// Unprepared, // + /// }; + /// use kernel::device::{ + /// Bound, + /// Device, // + /// }; /// use kernel::error::Result; /// /// fn configure_clk(dev: &Device) -> Result { @@ -287,7 +314,11 @@ fn from(err: Error) -> Self { /// and move between the variants: /// /// ``` - /// use kernel::clk::{Clk, Enabled, Prepared}; + /// use kernel::clk::{ + /// Clk, + /// Enabled, + /// Prepared, // + /// }; /// use kernel::error::Result; /// /// enum DeviceClk { @@ -481,7 +512,12 @@ pub fn enable(self) -> Result, Error> { /// clock or threading it through an intermediate state, e.g.: /// /// ``` - /// use kernel::clk::{Clk, Enabled, Hertz, Prepared}; + /// use kernel::clk::{ + /// Clk, + /// Enabled, + /// Hertz, + /// Prepared, // + /// }; /// use kernel::error::Result; /// /// fn read_rate(clk: &Clk) -> Result { --=20 2.54.0