Skip to content

Commit

Permalink
rust: regulator: add regulator consumer abstractions
Browse files Browse the repository at this point in the history
Add a rust abstraction for the regulator consumer API.

Signed-off-by: Fabien Parent <[email protected]>
  • Loading branch information
Fabo committed Mar 1, 2024
1 parent ee3fa46 commit 523f9d9
Show file tree
Hide file tree
Showing 4 changed files with 439 additions and 0 deletions.
1 change: 1 addition & 0 deletions rust/bindings/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <linux/poll.h>
#include <linux/refcount.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/sched.h>
#include <linux/security.h>
#include <linux/slab.h>
Expand Down
2 changes: 2 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub mod prelude;
pub mod print;
#[cfg(CONFIG_REGMAP)]
pub mod regmap;
#[cfg(CONFIG_REGULATOR)]
pub mod regulator;
pub mod security;
mod static_assert;
#[doc(hidden)]
Expand Down
44 changes: 44 additions & 0 deletions rust/kernel/regulator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-2.0

//! SoC Regulators
pub mod consumer;

use crate::{
bindings,
error::{code::*, Error, Result},
};

/// [`consumer::Regulator`] and [`driver::Regulator`] operating modes
#[derive(Copy, Clone)]
#[repr(u32)]
pub enum Mode {
/// Invalid mode
Invalid = bindings::REGULATOR_MODE_INVALID,
/// Regulator can handle fast changes in it's load
Fast = bindings::REGULATOR_MODE_FAST,
/// Normal regulator power supply mode
Normal = bindings::REGULATOR_MODE_NORMAL,
/// Regulator runs in a more efficient mode for light loads
Idle = bindings::REGULATOR_MODE_IDLE,
/// Regulator runs in the most efficient mode for very light loads
Standby = bindings::REGULATOR_MODE_STANDBY,
}

impl TryFrom<core::ffi::c_uint> for Mode {
type Error = Error;

/// Convert a mode represented as an unsigned integer into its Rust enum equivalent
///
/// If the integer does not match any of the [`Mode`], then [`EINVAL`] is returned
fn try_from(mode: core::ffi::c_uint) -> Result<Self> {
match mode {
bindings::REGULATOR_MODE_FAST => Ok(Self::Fast),
bindings::REGULATOR_MODE_NORMAL => Ok(Self::Normal),
bindings::REGULATOR_MODE_IDLE => Ok(Self::Idle),
bindings::REGULATOR_MODE_STANDBY => Ok(Self::Standby),
bindings::REGULATOR_MODE_INVALID => Ok(Self::Invalid),
_ => Err(EINVAL),
}
}
}
Loading

0 comments on commit 523f9d9

Please sign in to comment.