Re: [PATCH V3 2/2] rust: Add initial clk abstractions

From: Viresh Kumar
Date: Mon Mar 03 2025 - 06:33:44 EST


On 03-03-25, 11:04, Alice Ryhl wrote:
> > +/// A simple implementation of optional `Clk`.
> > +pub struct OptionalClk(Clk);
>
> What is this?

This came up during review of the previous version [1]. A resource (clk in this
case) can be optional for a driver to work and such drivers call
clk_get_optional(). Similar APIs are present in other frameworks as well.

I was not sure if this should be implemented as a separate method in struct Clk
itself or like this.

> > +impl OptionalClk {
> > + /// Gets optional clock corresponding to a device and a connection id and returns `Clk`.
> > + pub fn get(dev: &Device, name: Option<&CStr>) -> Result<Self> {
> > + let con_id = if let Some(name) = name {
> > + name.as_ptr() as *const _
> > + } else {
> > + ptr::null()
> > + };
> > +
> > + // SAFETY: It is safe to call `clk_get_optional()` for a valid device pointer.
> > + Ok(Self(Clk(from_err_ptr(unsafe {
> > + bindings::clk_get_optional(dev.as_raw(), con_id)
> > + })?)))
> > + }
> > +}

--
viresh

[1] https://lore.kernel.org/all/20250221215931.GA134397-robh@xxxxxxxxxx/