Struct Regulator

Source
pub struct Regulator<State = Dynamic>
where State: RegulatorState,
{ /* private fields */ }
Expand description

A struct regulator abstraction.

§Examples

§Enabling a regulator

This example uses Regulator<Enabled>, which is suitable for drivers that enable a regulator at probe time and leave them on until the device is removed or otherwise shutdown.

These users can store Regulator<Enabled> directly in their driver’s private data struct.

fn enable(dev: &Device, min_voltage: Voltage, max_voltage: Voltage) -> Result {
    // Obtain a reference to a (fictitious) regulator.
    let regulator: Regulator<Disabled> = Regulator::<Disabled>::get(dev, c_str!("vcc"))?;

    // The voltage can be set before enabling the regulator if needed, e.g.:
    regulator.set_voltage(min_voltage, max_voltage)?;

    // The same applies for `get_voltage()`, i.e.:
    let voltage: Voltage = regulator.get_voltage()?;

    // Enables the regulator, consuming the previous value.
    //
    // From now on, the regulator is known to be enabled because of the type
    // `Enabled`.
    //
    // If this operation fails, the `Error` will contain the regulator
    // reference, so that the operation may be retried.
    let regulator: Regulator<Enabled> =
        regulator.try_into_enabled().map_err(|error| error.error)?;

    // The voltage can also be set after enabling the regulator, e.g.:
    regulator.set_voltage(min_voltage, max_voltage)?;

    // The same applies for `get_voltage()`, i.e.:
    let voltage: Voltage = regulator.get_voltage()?;

    // Dropping an enabled regulator will disable it. The refcount will be
    // decremented.
    drop(regulator);

    // ...

    Ok(())
}

A more concise shortcut is available for enabling a regulator. This is equivalent to regulator_get_enable():

fn enable(dev: &Device) -> Result {
    // Obtain a reference to a (fictitious) regulator and enable it.
    let regulator: Regulator<Enabled> = Regulator::<Enabled>::get(dev, c_str!("vcc"))?;

    // Dropping an enabled regulator will disable it. The refcount will be
    // decremented.
    drop(regulator);

    // ...

    Ok(())
}

§Disabling a regulator

fn disable(dev: &Device, regulator: Regulator<Enabled>) -> Result {
    // We can also disable an enabled regulator without reliquinshing our
    // refcount:
    //
    // If this operation fails, the `Error` will contain the regulator
    // reference, so that the operation may be retried.
    let regulator: Regulator<Disabled> =
        regulator.try_into_disabled().map_err(|error| error.error)?;

    // The refcount will be decremented when `regulator` is dropped.
    drop(regulator);

    // ...

    Ok(())
}

§Using Regulator<Dynamic>

This example mimics the behavior of the C API, where the user is in control of the enabled reference count. This is useful for drivers that might call enable and disable to manage the enable reference count at runtime, perhaps as a result of open() and close() calls or whatever other driver-specific or subsystem-specific hooks.

struct PrivateData {
    regulator: Regulator<Dynamic>,
}

// A fictictious probe function that obtains a regulator and sets it up.
fn probe(dev: &Device) -> Result<PrivateData> {
    // Obtain a reference to a (fictitious) regulator.
    let mut regulator = Regulator::<Dynamic>::get(dev, c_str!("vcc"))?;

    Ok(PrivateData { regulator })
}

// A fictictious function that indicates that the device is going to be used.
fn open(dev: &Device, data: &mut PrivateData) -> Result {
    // Increase the `enabled` reference count.
    data.regulator.enable()?;

    Ok(())
}

fn close(dev: &Device, data: &mut PrivateData) -> Result {
    // Decrease the `enabled` reference count.
    data.regulator.disable()?;

    Ok(())
}

fn remove(dev: &Device, data: PrivateData) -> Result {
    // `PrivateData` is dropped here, which will drop the
    // `Regulator<Dynamic>` in turn.
    //
    // The reference that was obtained by `regulator_get()` will be
    // released, but it is up to the user to make sure that the number of calls
    // to `enable()` and `disabled()` are balanced before this point.
    Ok(())
}

§Invariants

  • inner is a non-null wrapper over a pointer to a struct regulator obtained from regulator_get().

Implementations§

Source§

impl<T: RegulatorState> Regulator<T>

Source

pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Result

Sets the voltage for the regulator.

This can be used to ensure that the device powers up cleanly.

Source

pub fn get_voltage(&self) -> Result<Voltage>

Gets the current voltage of the regulator.

Source§

impl Regulator<Disabled>

Source

pub fn get(dev: &Device, name: &CStr) -> Result<Self>

Obtains a Regulator instance from the system.

Source

pub fn try_into_enabled(self) -> Result<Regulator<Enabled>, Error<Disabled>>

Attempts to convert the regulator to an enabled state.

Source§

impl Regulator<Enabled>

Source

pub fn get(dev: &Device, name: &CStr) -> Result<Self>

Obtains a Regulator instance from the system and enables it.

This is equivalent to calling regulator_get_enable() in the C API.

Source

pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>>

Attempts to convert the regulator to a disabled state.

Source§

impl Regulator<Dynamic>

Source

pub fn get(dev: &Device, name: &CStr) -> Result<Self>

Obtains a Regulator instance from the system. The current state of the regulator is unknown and it is up to the user to manage the enabled reference count.

This closely mimics the behavior of the C API and can be used to dynamically manage the enabled reference count at runtime.

Source

pub fn enable(&mut self) -> Result

Increases the enabled reference count.

Source

pub fn disable(&mut self) -> Result

Decreases the enabled reference count.

Source§

impl<T: IsEnabled> Regulator<T>

Source

pub fn is_enabled(&self) -> bool

Checks if the regulator is enabled.

Trait Implementations§

Source§

impl<T: RegulatorState> Drop for Regulator<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<State> Freeze for Regulator<State>

§

impl<State> RefUnwindSafe for Regulator<State>
where State: RefUnwindSafe,

§

impl<State = Dynamic> !Send for Regulator<State>

§

impl<State = Dynamic> !Sync for Regulator<State>

§

impl<State> Unpin for Regulator<State>
where State: Unpin,

§

impl<State> UnwindSafe for Regulator<State>
where State: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.