Skip to content

Commit 523f9d9

Browse files
committed
rust: regulator: add regulator consumer abstractions
Add a rust abstraction for the regulator consumer API. Signed-off-by: Fabien Parent <[email protected]>
1 parent ee3fa46 commit 523f9d9

File tree

4 files changed

+439
-0
lines changed

4 files changed

+439
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/poll.h>
2525
#include <linux/refcount.h>
2626
#include <linux/regmap.h>
27+
#include <linux/regulator/consumer.h>
2728
#include <linux/sched.h>
2829
#include <linux/security.h>
2930
#include <linux/slab.h>

rust/kernel/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub mod prelude;
5555
pub mod print;
5656
#[cfg(CONFIG_REGMAP)]
5757
pub mod regmap;
58+
#[cfg(CONFIG_REGULATOR)]
59+
pub mod regulator;
5860
pub mod security;
5961
mod static_assert;
6062
#[doc(hidden)]

rust/kernel/regulator.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! SoC Regulators
4+
5+
pub mod consumer;
6+
7+
use crate::{
8+
bindings,
9+
error::{code::*, Error, Result},
10+
};
11+
12+
/// [`consumer::Regulator`] and [`driver::Regulator`] operating modes
13+
#[derive(Copy, Clone)]
14+
#[repr(u32)]
15+
pub enum Mode {
16+
/// Invalid mode
17+
Invalid = bindings::REGULATOR_MODE_INVALID,
18+
/// Regulator can handle fast changes in it's load
19+
Fast = bindings::REGULATOR_MODE_FAST,
20+
/// Normal regulator power supply mode
21+
Normal = bindings::REGULATOR_MODE_NORMAL,
22+
/// Regulator runs in a more efficient mode for light loads
23+
Idle = bindings::REGULATOR_MODE_IDLE,
24+
/// Regulator runs in the most efficient mode for very light loads
25+
Standby = bindings::REGULATOR_MODE_STANDBY,
26+
}
27+
28+
impl TryFrom<core::ffi::c_uint> for Mode {
29+
type Error = Error;
30+
31+
/// Convert a mode represented as an unsigned integer into its Rust enum equivalent
32+
///
33+
/// If the integer does not match any of the [`Mode`], then [`EINVAL`] is returned
34+
fn try_from(mode: core::ffi::c_uint) -> Result<Self> {
35+
match mode {
36+
bindings::REGULATOR_MODE_FAST => Ok(Self::Fast),
37+
bindings::REGULATOR_MODE_NORMAL => Ok(Self::Normal),
38+
bindings::REGULATOR_MODE_IDLE => Ok(Self::Idle),
39+
bindings::REGULATOR_MODE_STANDBY => Ok(Self::Standby),
40+
bindings::REGULATOR_MODE_INVALID => Ok(Self::Invalid),
41+
_ => Err(EINVAL),
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)