Skip to content

Commit a48271f

Browse files
committed
rust: regulator: add regulator consumer abstractions
Add a rust abstraction for the regulator consumer API. Signed-off-by: Fabien Parent <fabien.parent@linaro.org>
1 parent 8b27764 commit a48271f

4 files changed

Lines changed: 440 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/platform_device.h>
2121
#include <linux/refcount.h>
2222
#include <linux/regmap.h>
23+
#include <linux/regulator/consumer.h>
2324
#include <linux/sched.h>
2425
#include <linux/slab.h>
2526
#include <linux/wait.h>

rust/kernel/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub mod print;
5959
pub mod rbtree;
6060
#[cfg(CONFIG_REGMAP)]
6161
pub mod regmap;
62+
#[cfg(CONFIG_REGULATOR)]
63+
pub mod regulator;
6264
pub mod revocable;
6365
mod static_assert;
6466
#[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)