kernel/
regulator.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Regulator abstractions, providing a standard kernel interface to control
4//! voltage and current regulators.
5//!
6//! The intention is to allow systems to dynamically control regulator power
7//! output in order to save power and prolong battery life. This applies to both
8//! voltage regulators (where voltage output is controllable) and current sinks
9//! (where current limit is controllable).
10//!
11//! C header: [`include/linux/regulator/consumer.h`](srctree/include/linux/regulator/consumer.h)
12//!
13//! Regulators are modeled in Rust with a collection of states. Each state may
14//! enforce a given invariant, and they may convert between each other where applicable.
15//!
16//! See [Voltage and current regulator API](https://docs.kernel.org/driver-api/regulator.html)
17//! for more information.
18
19use crate::{
20    bindings,
21    device::{Bound, Device},
22    error::{from_err_ptr, to_result, Result},
23    prelude::*,
24};
25
26use core::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull};
27
28mod private {
29    pub trait Sealed {}
30
31    impl Sealed for super::Enabled {}
32    impl Sealed for super::Disabled {}
33}
34
35/// A trait representing the different states a [`Regulator`] can be in.
36pub trait RegulatorState: private::Sealed + 'static {
37    /// Whether the regulator should be disabled when dropped.
38    const DISABLE_ON_DROP: bool;
39}
40
41/// A state where the [`Regulator`] is known to be enabled.
42///
43/// The `enable` reference count held by this state is decremented when it is
44/// dropped.
45pub struct Enabled;
46
47/// A state where this [`Regulator`] handle has not specifically asked for the
48/// underlying regulator to be enabled. This means that this reference does not
49/// own an `enable` reference count, but the regulator may still be on.
50pub struct Disabled;
51
52impl RegulatorState for Enabled {
53    const DISABLE_ON_DROP: bool = true;
54}
55
56impl RegulatorState for Disabled {
57    const DISABLE_ON_DROP: bool = false;
58}
59
60/// A trait that abstracts the ability to check if a [`Regulator`] is enabled.
61pub trait IsEnabled: RegulatorState {}
62impl IsEnabled for Disabled {}
63
64/// An error that can occur when trying to convert a [`Regulator`] between states.
65pub struct Error<State: RegulatorState> {
66    /// The error that occurred.
67    pub error: kernel::error::Error,
68
69    /// The regulator that caused the error, so that the operation may be retried.
70    pub regulator: Regulator<State>,
71}
72/// Obtains and enables a [`devres`]-managed regulator for a device.
73///
74/// This calls [`regulator_disable()`] and [`regulator_put()`] automatically on
75/// driver detach.
76///
77/// This API is identical to `devm_regulator_get_enable()`, and should be
78/// preferred over the [`Regulator<T: RegulatorState>`] API if the caller only
79/// cares about the regulator being enabled.
80///
81/// [`devres`]: https://docs.kernel.org/driver-api/driver-model/devres.html
82/// [`regulator_disable()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_disable
83/// [`regulator_put()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_put
84pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {
85    // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
86    // string.
87    to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), name.as_char_ptr()) })
88}
89
90/// Same as [`devm_enable`], but calls `devm_regulator_get_enable_optional`
91/// instead.
92///
93/// This obtains and enables a [`devres`]-managed regulator for a device, but
94/// does not print a message nor provides a dummy if the regulator is not found.
95///
96/// This calls [`regulator_disable()`] and [`regulator_put()`] automatically on
97/// driver detach.
98///
99/// [`devres`]: https://docs.kernel.org/driver-api/driver-model/devres.html
100/// [`regulator_disable()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_disable
101/// [`regulator_put()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_put
102pub fn devm_enable_optional(dev: &Device<Bound>, name: &CStr) -> Result {
103    // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
104    // string.
105    to_result(unsafe {
106        bindings::devm_regulator_get_enable_optional(dev.as_raw(), name.as_char_ptr())
107    })
108}
109
110/// A `struct regulator` abstraction.
111///
112/// # Examples
113///
114/// ## Enabling a regulator
115///
116/// This example uses [`Regulator<Enabled>`], which is suitable for drivers that
117/// enable a regulator at probe time and leave them on until the device is
118/// removed or otherwise shutdown.
119///
120/// These users can store [`Regulator<Enabled>`] directly in their driver's
121/// private data struct.
122///
123/// ```
124/// # use kernel::prelude::*;
125/// # use kernel::device::Device;
126/// # use kernel::regulator::{Voltage, Regulator, Disabled, Enabled};
127/// fn enable(dev: &Device, min_voltage: Voltage, max_voltage: Voltage) -> Result {
128///     // Obtain a reference to a (fictitious) regulator.
129///     let regulator: Regulator<Disabled> = Regulator::<Disabled>::get(dev, c"vcc")?;
130///
131///     // The voltage can be set before enabling the regulator if needed, e.g.:
132///     regulator.set_voltage(min_voltage, max_voltage)?;
133///
134///     // The same applies for `get_voltage()`, i.e.:
135///     let voltage: Voltage = regulator.get_voltage()?;
136///
137///     // Enables the regulator, consuming the previous value.
138///     //
139///     // From now on, the regulator is known to be enabled because of the type
140///     // `Enabled`.
141///     //
142///     // If this operation fails, the `Error` will contain the regulator
143///     // reference, so that the operation may be retried.
144///     let regulator: Regulator<Enabled> =
145///         regulator.try_into_enabled().map_err(|error| error.error)?;
146///
147///     // The voltage can also be set after enabling the regulator, e.g.:
148///     regulator.set_voltage(min_voltage, max_voltage)?;
149///
150///     // The same applies for `get_voltage()`, i.e.:
151///     let voltage: Voltage = regulator.get_voltage()?;
152///
153///     // Dropping an enabled regulator will disable it. The refcount will be
154///     // decremented.
155///     drop(regulator);
156///
157///     // ...
158///
159///     Ok(())
160/// }
161/// ```
162///
163/// A more concise shortcut is available for enabling a regulator. This is
164/// equivalent to `regulator_get_enable()`:
165///
166/// ```
167/// # use kernel::prelude::*;
168/// # use kernel::device::Device;
169/// # use kernel::regulator::{Voltage, Regulator, Enabled};
170/// fn enable(dev: &Device) -> Result {
171///     // Obtain a reference to a (fictitious) regulator and enable it.
172///     let regulator: Regulator<Enabled> = Regulator::<Enabled>::get(dev, c"vcc")?;
173///
174///     // Dropping an enabled regulator will disable it. The refcount will be
175///     // decremented.
176///     drop(regulator);
177///
178///     // ...
179///
180///     Ok(())
181/// }
182/// ```
183///
184/// If a driver only cares about the regulator being on for as long it is bound
185/// to a device, then it should use [`devm_enable`] or [`devm_enable_optional`].
186/// This should be the default use-case unless more fine-grained control over
187/// the regulator's state is required.
188///
189/// [`devm_enable`]: crate::regulator::devm_enable
190/// [`devm_optional`]: crate::regulator::devm_enable_optional
191///
192/// ```
193/// # use kernel::prelude::*;
194/// # use kernel::device::{Bound, Device};
195/// # use kernel::regulator;
196/// fn enable(dev: &Device<Bound>) -> Result {
197///     // Obtain a reference to a (fictitious) regulator and enable it. This
198///     // call only returns whether the operation succeeded.
199///     regulator::devm_enable(dev, c"vcc")?;
200///
201///     // The regulator will be disabled and put when `dev` is unbound.
202///     Ok(())
203/// }
204/// ```
205///
206/// ## Disabling a regulator
207///
208/// ```
209/// # use kernel::prelude::*;
210/// # use kernel::device::Device;
211/// # use kernel::regulator::{Regulator, Enabled, Disabled};
212/// fn disable(dev: &Device, regulator: Regulator<Enabled>) -> Result {
213///     // We can also disable an enabled regulator without reliquinshing our
214///     // refcount:
215///     //
216///     // If this operation fails, the `Error` will contain the regulator
217///     // reference, so that the operation may be retried.
218///     let regulator: Regulator<Disabled> =
219///         regulator.try_into_disabled().map_err(|error| error.error)?;
220///
221///     // The refcount will be decremented when `regulator` is dropped.
222///     drop(regulator);
223///
224///     // ...
225///
226///     Ok(())
227/// }
228/// ```
229///
230/// # Invariants
231///
232/// - `inner` is a non-null wrapper over a pointer to a `struct
233///   regulator` obtained from [`regulator_get()`].
234///
235/// [`regulator_get()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_get
236pub struct Regulator<State>
237where
238    State: RegulatorState,
239{
240    inner: NonNull<bindings::regulator>,
241    _phantom: PhantomData<State>,
242}
243
244impl<T: RegulatorState> Regulator<T> {
245    /// Sets the voltage for the regulator.
246    ///
247    /// This can be used to ensure that the device powers up cleanly.
248    pub fn set_voltage(&self, min_voltage: Voltage, max_voltage: Voltage) -> Result {
249        // SAFETY: Safe as per the type invariants of `Regulator`.
250        to_result(unsafe {
251            bindings::regulator_set_voltage(
252                self.inner.as_ptr(),
253                min_voltage.as_microvolts(),
254                max_voltage.as_microvolts(),
255            )
256        })
257    }
258
259    /// Gets the current voltage of the regulator.
260    pub fn get_voltage(&self) -> Result<Voltage> {
261        // SAFETY: Safe as per the type invariants of `Regulator`.
262        let voltage = unsafe { bindings::regulator_get_voltage(self.inner.as_ptr()) };
263
264        to_result(voltage).map(|()| Voltage::from_microvolts(voltage))
265    }
266
267    fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {
268        let inner =
269            // SAFETY: It is safe to call `regulator_get()`, on a device pointer
270            // received from the C code.
271            from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_char_ptr()) })?;
272
273        // SAFETY: We can safely trust `inner` to be a pointer to a valid
274        // regulator if `ERR_PTR` was not returned.
275        let inner = unsafe { NonNull::new_unchecked(inner) };
276
277        Ok(Self {
278            inner,
279            _phantom: PhantomData,
280        })
281    }
282
283    fn enable_internal(&self) -> Result {
284        // SAFETY: Safe as per the type invariants of `Regulator`.
285        to_result(unsafe { bindings::regulator_enable(self.inner.as_ptr()) })
286    }
287
288    fn disable_internal(&self) -> Result {
289        // SAFETY: Safe as per the type invariants of `Regulator`.
290        to_result(unsafe { bindings::regulator_disable(self.inner.as_ptr()) })
291    }
292}
293
294impl Regulator<Disabled> {
295    /// Obtains a [`Regulator`] instance from the system.
296    pub fn get(dev: &Device, name: &CStr) -> Result<Self> {
297        Regulator::get_internal(dev, name)
298    }
299
300    /// Attempts to convert the regulator to an enabled state.
301    pub fn try_into_enabled(self) -> Result<Regulator<Enabled>, Error<Disabled>> {
302        // We will be transferring the ownership of our `regulator_get()` count to
303        // `Regulator<Enabled>`.
304        let regulator = ManuallyDrop::new(self);
305
306        regulator
307            .enable_internal()
308            .map(|()| Regulator {
309                inner: regulator.inner,
310                _phantom: PhantomData,
311            })
312            .map_err(|error| Error {
313                error,
314                regulator: ManuallyDrop::into_inner(regulator),
315            })
316    }
317}
318
319impl Regulator<Enabled> {
320    /// Obtains a [`Regulator`] instance from the system and enables it.
321    ///
322    /// This is equivalent to calling `regulator_get_enable()` in the C API.
323    pub fn get(dev: &Device, name: &CStr) -> Result<Self> {
324        Regulator::<Disabled>::get_internal(dev, name)?
325            .try_into_enabled()
326            .map_err(|error| error.error)
327    }
328
329    /// Attempts to convert the regulator to a disabled state.
330    pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>> {
331        // We will be transferring the ownership of our `regulator_get()` count
332        // to `Regulator<Disabled>`.
333        let regulator = ManuallyDrop::new(self);
334
335        regulator
336            .disable_internal()
337            .map(|()| Regulator {
338                inner: regulator.inner,
339                _phantom: PhantomData,
340            })
341            .map_err(|error| Error {
342                error,
343                regulator: ManuallyDrop::into_inner(regulator),
344            })
345    }
346}
347
348impl<T: IsEnabled> Regulator<T> {
349    /// Checks if the regulator is enabled.
350    pub fn is_enabled(&self) -> bool {
351        // SAFETY: Safe as per the type invariants of `Regulator`.
352        unsafe { bindings::regulator_is_enabled(self.inner.as_ptr()) != 0 }
353    }
354}
355
356impl<T: RegulatorState> Drop for Regulator<T> {
357    fn drop(&mut self) {
358        if T::DISABLE_ON_DROP {
359            // SAFETY: By the type invariants, we know that `self` owns a
360            // reference on the enabled refcount, so it is safe to relinquish it
361            // now.
362            unsafe { bindings::regulator_disable(self.inner.as_ptr()) };
363        }
364        // SAFETY: By the type invariants, we know that `self` owns a reference,
365        // so it is safe to relinquish it now.
366        unsafe { bindings::regulator_put(self.inner.as_ptr()) };
367    }
368}
369
370// SAFETY: It is safe to send a `Regulator<T>` across threads. In particular, a
371// Regulator<T> can be dropped from any thread.
372unsafe impl<T: RegulatorState> Send for Regulator<T> {}
373
374// SAFETY: It is safe to send a &Regulator<T> across threads because the C side
375// handles its own locking.
376unsafe impl<T: RegulatorState> Sync for Regulator<T> {}
377
378/// A voltage.
379///
380/// This type represents a voltage value in microvolts.
381#[repr(transparent)]
382#[derive(Copy, Clone, PartialEq, Eq)]
383pub struct Voltage(i32);
384
385impl Voltage {
386    /// Creates a new `Voltage` from a value in microvolts.
387    pub fn from_microvolts(uv: i32) -> Self {
388        Self(uv)
389    }
390
391    /// Returns the value of the voltage in microvolts as an [`i32`].
392    pub fn as_microvolts(self) -> i32 {
393        self.0
394    }
395}