rust: regulator: improve the ergonomics of Rust regulators
Subsystems (2)
Daniel Almeida
reviewerv3 cover rust: regulator: improve the ergonomics of Rust regulators
This small series comes after some extensive discussion on a few minor changes that can improve the current Rust regulator API.
Patch 1 removes Regulator<Dynamic>, as we have now established that there is no usecase that can't use the safer Regulator<Enabled> and Regulator<Disabled> APIs instead.
Patch 2 makes "devm_regulator_enable_get" and "devm_regulator_enable_get_optional" available in Rust. This comes after realizing that a lot of drivers simply care about whether regulators are enabled for as long as the device is bound.
---
Changes in v3:
- Rework the docs (Danilo, Boqun)
- Pick up tags
- Link to v2: https://lore.kernel.org/r/20250908-regulator-remove-dynamic-v2-0-e575ae2cde6a@collabora.com
Changes in v2:
- Picked up tags
- Rebased on regulator/for-next
- Renamed enable() and enable_optional() to devm_enable() and devm_enable_optional().
- Renamed patch 2/2 to pick up the above change
- Added more docs and links where applicable (thanks, Alex)
- Link to v1: https://lore.kernel.org/r/20250829-regulator-remove-dynamic-v1-0-deb59205e8e9@collabora.com
---
Daniel Almeida (2):
rust: regulator: remove Regulator<Dynamic>
rust: regulator: add devm_enable and devm_enable_optional
rust/helpers/regulator.c | 10 ++++ rust/kernel/regulator.rs | 148 +++++++++++++++++++---------------------------- 2 files changed, 70 insertions(+), 88 deletions(-)
---
base-commit: 59e8e7b7f2206d7097e43266722b625715720dfa
change-id: 20250829-regulator-remove-dynamic-f1a6b8c0c1b0
Best regards,
Signature · 1 line
Daniel Almeida <[email protected]>
Daniel Almeida
reviewerPATCH v3 1/2 rust: regulator: remove Regulator<Dynamic>
After some experimenting and further discussion, it is starting to look like Regulator<Dynamic> might be a footgun. It turns out that one can get the same behavior by correctly using just Regulator<Enabled> and Regulator<Disabled>, so there is no need to directly expose the manual refcounting ability of Regulator<Dynamic> to clients.
Remove it while we do not have any other users.
--- rust/kernel/regulator.rs | 88 +----------------------------------------------- 1 file changed, 1 insertion(+), 87 deletions(-)
rust/kernel/regulator.rs +1 -87
@@ -30,7 +30,6 @@ pub trait Sealed {}impl Sealed for super ::Enabled {}impl Sealed for super ::Disabled {} -impl Sealed for super ::Dynamic {} }/// A trait representing the different states a [`Regulator`] can be in.
@@ -50,13 +49,6 @@ pub trait RegulatorState: private::Sealed + 'static {/// own an `enable` reference count, but the regulator may still be on. pub struct Disabled ; -/// A state that models the C API. The [`Regulator`] can be either enabled or -/// disabled, and the user is in control of the reference count. This is also -/// the default state. -/// -/// Use [`Regulator::is_enabled`] to check the regulator's current state. -pub struct Dynamic ; -impl RegulatorState for Enabled {const DISABLE_ON_DROP :bool =true ; }
@@ -65,14 +57,9 @@ impl RegulatorState for Disabled {const DISABLE_ON_DROP :bool =false ; } -impl RegulatorState for Dynamic { -const DISABLE_ON_DROP :bool =false ; -} -/// A trait that abstracts the ability to check if a [`Regulator`] is enabled. pub trait IsEnabled :RegulatorState {}impl IsEnabled for Disabled {} -impl IsEnabled for Dynamic {}/// An error that can occur when trying to convert a [`Regulator`] between states. pub struct Error <State :RegulatorState > {
@@ -183,64 +170,13 @@ pub struct Error<State: RegulatorState> {/// } /// ``` /// -/// ## 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. -/// -/// ``` -/// # use kernel::prelude::*; -/// # use kernel::c_str; -/// # use kernel::device::Device; -/// # use kernel::regulator::{Regulator, Dynamic}; -/// 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 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: &PrivateData) -> Result { -/// // Increase the `enabled` reference count. -/// data.regulator.enable()?; -/// -/// Ok(()) -/// } -/// -/// fn close(dev: &Device, data: &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()`]. /// /// [`regulator_get()`]: https://docs.kernel.org/driver-api/regulator.html#c.regulator_get -pub struct Regulator <State =Dynamic > +pub struct Regulator <State >where State :RegulatorState , {
@@ -351,28 +287,6 @@ pub fn try_into_disabled(self) -> Result<Regulator<Disabled>, Error<Enabled>> { } } -impl Regulator <Dynamic > { -/// 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. -pub fn get (dev: &Device , name: &CStr ) ->Result <Self > { -Regulator ::get_internal (dev, name) - } - -/// Increases the `enabled` reference count. -pub fn enable (&self) ->Result { - self.enable_internal () - } - -/// Decreases the `enabled` reference count. -pub fn disable (&self) ->Result { - self.disable_internal () - } -} -impl <T :IsEnabled >Regulator <T > {/// Checks if the regulator is enabled. pub fn is_enabled (&self) ->bool {
Signature · 1 line
2.51.0
Daniel Almeida
reviewerPATCH v3 2/2 rust: regulator: add devm_enable and devm_enable_optional
In reply to Daniel AlmeidaA lot of drivers only care about enabling the regulator for as long as the underlying Device is bound. This can be easily observed due to the extensive use of devm_regulator_get_enable and devm_regulator_get_enable_optional throughout the kernel.
Therefore, make this helper available in Rust. Also add an example noting how it should be the default API unless the driver needs more fine-grained control over the regulator.
--- rust/helpers/regulator.c | 10 ++++++++ rust/kernel/regulator.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-)
rust/helpers/regulator.c +10 -0
@@ -40,4 +40,14 @@ int rust_helper_regulator_is_enabled(struct regulator *regulator) return regulator_is_enabled(regulator); } +int rust_helper_devm_regulator_get_enable (struct device *dev,const char *id) +{ +return devm_regulator_get_enable (dev, id); +} + +int rust_helper_devm_regulator_get_enable_optional (struct device *dev,const char *id) +{ +return devm_regulator_get_enable_optional (dev, id); +} +#endif
rust/kernel/regulator.rs +59 -1
78 lines not on this page · raw message
Signature · 1 line
2.51.0
Mark Brown
maintainerQuoted 8 lines · Daniel Almeida
This small series comes after some extensive discussion on a few minor changes that can improve the current Rust regulator API.
Patch 1 removes Regulator<Dynamic>, as we have now established that there is no usecase that can't use the safer Regulator<Enabled> and Regulator<Disabled> APIs instead.
[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
Thanks!
[1/2] rust: regulator: remove Regulator<Dynamic>
commit: b87ecbc54f22382ace1cf41645e8652a4ce44d52
[2/2] rust: regulator: add devm_enable and devm_enable_optional
commit: 2e0fd4583d0efcdc260e61a22666c8368f505353All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark