Add ExclusiveResetControl::get_array() and get_array_optional(), which get all the reset lines in the resets property of a device as a single reset control. This is for drivers that assert and deassert all the reset lines of their device together.
Signed-off-by: Daniel Almeida <[email protected]> --- rust/helpers/reset.c | 7 ++++ rust/kernel/reset.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/rust/helpers/reset.c b/rust/helpers/reset.c index ea2e6f09b7d5..435f2c7c815d 100644 --- a/rust/helpers/reset.c +++ b/rust/helpers/reset.c @@ -16,6 +16,13 @@ rust_helper_reset_control_get_optional_exclusive(struct device *dev, } #ifndef CONFIG_RESET_CONTROLLER +__rust_helper struct reset_control * +rust_helper_fwnode_reset_control_array_get(struct fwnode_handle *fwnode, + enum reset_control_flags flags) +{ + return fwnode_reset_control_array_get(fwnode, flags); +} + __rust_helper void rust_helper_reset_control_put(struct reset_control *rstc) { reset_control_put(rstc); diff --git a/rust/kernel/reset.rs b/rust/kernel/reset.rs index 396f1e19ee2e..7ca83f2d6abf 100644 --- a/rust/kernel/reset.rs +++ b/rust/kernel/reset.rs @@ -9,6 +9,9 @@ //! assert and deassert the line. A new control is in the [`Unknown`] state. Its `assert()` and //! `deassert()` methods, when they succeed, move it to the [`Asserted`] or [`Deasserted`] state. //! +//! A driver can also get a single reset control for all the reset lines in the `resets` +//! property of its device. Its operations then act on all of those lines. +//! //! C header: [`include/linux/reset.h`](srctree/include/linux/reset.h) //! //! See [Reset controller API](https://docs.kernel.org/driver-api/reset.html) for more information. @@ -96,8 +99,9 @@ fn from(err: Error<T>) -> Self { /// Exclusive means that the driver has direct control over the reset line. A reset line can be /// held by one [`ExclusiveResetControl`] at a time. /// -/// A reset control obtained with [`ExclusiveResetControl::get_optional`] may have no reset line, -/// in which case every operation on it succeeds and does nothing. +/// A reset control obtained with [`ExclusiveResetControl::get_optional`] or +/// [`ExclusiveResetControl::get_array_optional`] may have no reset line, in which case every +/// operation on it succeeds and does nothing. /// /// The reset control is released when the [`ExclusiveResetControl`] is dropped. The lifetime `'a` /// is that of the reference to the bound device it was obtained from, so an @@ -143,6 +147,8 @@ fn from(err: Error<T>) -> Self { /// /// - [`reset_control_get_exclusive()`] /// - [`reset_control_get_optional_exclusive()`] +/// - `fwnode_reset_control_array_get()` with the `RESET_CONTROL_EXCLUSIVE` or +/// `RESET_CONTROL_OPTIONAL_EXCLUSIVE` flag /// /// It is treated as an opaque token that may only be accessed using C API methods (e.g., it may be /// `NULL` if the C API returns `NULL`). The [`ExclusiveResetControl`] owns the reference that the @@ -213,6 +219,92 @@ pub fn get_optional(dev: &'a Device<Bound>, name: Option<&CStr>) -> Result<Self> _phantom: PhantomData, }) } + + /// Gets the reset lines of `dev` as one [`ExclusiveResetControl`], as [`Self::get_array`] does + /// or, if `optional` is set, as [`Self::get_array_optional`] does. + fn get_array_internal(dev: &'a Device<Bound>, optional: bool) -> Result<Self> { + let flags = if optional { + bindings::reset_control_flags_RESET_CONTROL_OPTIONAL_EXCLUSIVE + } else { + bindings::reset_control_flags_RESET_CONTROL_EXCLUSIVE + }; + + let fwnode = dev + .fwnode() + .map_or(ptr::null_mut(), |fwnode| fwnode.as_raw()); + + // SAFETY: `fwnode` is the firmware node of `dev` or `NULL`, which + // `fwnode_reset_control_array_get()` checks for. + let inner = unsafe { bindings::fwnode_reset_control_array_get(fwnode, flags) }; + let inner = from_err_ptr(inner)?; + + // INVARIANT: `inner` comes from a successful call to `fwnode_reset_control_array_get()` + // with one of the two flags above, and the returned value owns the reference from that + // call. + Ok(Self { + inner, + _phantom: PhantomData, + }) + } + + /// Gets the reset lines listed in the `resets` property of `dev` as one + /// [`ExclusiveResetControl`]. + /// + /// Its operations act on all the lines, in no guaranteed order. If one of them fails on a + /// line, the opposite operation is run on the lines it had already succeeded on, whatever + /// state they were in before. This includes operations that are done on drop. The caller only + /// gets the error from the line that failed, while errors from the opposite operation are + /// discarded. + /// + /// Equivalent to the kernel's `fwnode_reset_control_array_get()` on the firmware node of + /// `dev`, as `devm_reset_control_array_get_exclusive()` does. + /// + /// # Examples + /// + /// Keeping all the reset lines of a device deasserted: + /// + /// ``` + /// use kernel::{ + /// device::{ + /// Bound, + /// Device, // + /// }, + /// reset::{ + /// Deasserted, + /// ExclusiveResetControl, // + /// }, + /// }; + /// + /// struct Data<'a> { + /// // Dropping `Data` tries to assert the lines again. + /// resets: ExclusiveResetControl<'a, Deasserted>, + /// } + /// + /// fn probe<'a>(dev: &'a Device<Bound>) -> Result<Data<'a>> { + /// let resets = ExclusiveResetControl::get_array(dev)?.deassert()?; + /// + /// Ok(Data { resets }) + /// } + /// ``` + #[inline] + pub fn get_array(dev: &'a Device<Bound>) -> Result<Self> { + Self::get_array_internal(dev, false) + } + + /// Gets the reset lines listed in the `resets` property of `dev` as one + /// [`ExclusiveResetControl`], if there are any. + /// + /// Behaves the same as [`Self::get_array`], except when `dev` has no firmware node, when its + /// `resets` property has no entry or cannot be read, or when `CONFIG_RESET_CONTROLLER` is + /// disabled. In those cases, it returns an [`ExclusiveResetControl`] on which every operation + /// succeeds and does nothing. + /// + /// Equivalent to the kernel's `fwnode_reset_control_array_get()` on the firmware node of + /// `dev`, as `devm_reset_control_array_get_optional_exclusive()` does. + #[inline] + pub fn get_array_optional(dev: &'a Device<Bound>) -> Result<Self> { + Self::get_array_internal(dev, true) + } } impl<'a, State: ResetState> ExclusiveResetControl<'a, State> { -- 2.56.0
