Struct Regulator

Source
pub struct Regulator<State>
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(())
}

If a driver only cares about the regulator being on for as long it is bound to a device, then it should use devm_enable or devm_enable_optional. This should be the default use-case unless more fine-grained control over the regulator’s state is required.

fn enable(dev: &Device<Bound>) -> Result {
    // Obtain a reference to a (fictitious) regulator and enable it. This
    // call only returns whether the operation succeeded.
    regulator::devm_enable(dev, c_str!("vcc"))?;

    // The regulator will be disabled and put when `dev` is unbound.
    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(())
}

§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<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
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<State> Freeze for Regulator<State>

§

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

§

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.