public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Fabien Parent <parent.f@gmail.com>
To: "Rob Herring" <robh@kernel.org>,
	"Saravana Kannan" <saravanak@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Wolfram Sang" <wsa+renesas@sang-engineering.com>,
	"Mark Brown" <broonie@kernel.org>,
	"Liam Girdwood" <lgirdwood@gmail.com>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Bjorn Andersson" <andersson@kernel.org>,
	"Konrad Dybcio" <konradybcio@kernel.org>,
	"Fabien Parent" <parent.f@gmail.com>
Cc: devicetree@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org,
	 linux-arm-msm@vger.kernel.org, vinod.koul@linaro.org,
	 Fabien Parent <fabien.parent@linaro.org>
Subject: [PATCH 4/9] rust: regulator: add abstraction for Regulator's modes
Date: Wed, 18 Dec 2024 15:36:34 -0800	[thread overview]
Message-ID: <20241218-ncv6336-v1-4-b8d973747f7a@gmail.com> (raw)
In-Reply-To: <20241218-ncv6336-v1-0-b8d973747f7a@gmail.com>

From: Fabien Parent <fabien.parent@linaro.org>

The type regulator::Mode is used by both the regulator consumer
abstraction and the regulator driver abstraction. This commits
adds a shared abstraction for it.

Signed-off-by: Fabien Parent <fabien.parent@linaro.org>
---
 MAINTAINERS                     |  1 +
 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/lib.rs              |  2 ++
 rust/kernel/regulator.rs        | 42 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 46 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index acb3942eb1b66ec2bc09ac50f51c2054b7b45355..90c231f0aa7381aa8d206fb94c5d1f013dfcae41 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25159,6 +25159,7 @@ F:	Documentation/power/regulator/
 F:	drivers/regulator/
 F:	include/dt-bindings/regulator/
 F:	include/linux/regulator/
+F:	rust/kernel/regulator.rs
 K:	regulator_get_optional
 
 VOLTAGE AND CURRENT REGULATOR IRQ HELPERS
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 48d2b91b34067e7e9ee9c64c2e42681e988e9aad..b18d772bc3a0e78d749cc9e5ae81a4237a57f8c5 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -29,6 +29,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>
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 456e979724d1079045cb157086ff2b2ed0fcca3b..3aa36648e9571e305a89f5d1353c0dd44e136384 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -68,6 +68,8 @@
 pub mod rbtree;
 #[cfg(CONFIG_REGMAP)]
 pub mod regmap;
+#[cfg(CONFIG_REGULATOR)]
+pub mod regulator;
 pub mod revocable;
 pub mod security;
 pub mod seq_file;
diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs
new file mode 100644
index 0000000000000000000000000000000000000000..d695ac955193efcfda62770784a92d70d606b93d
--- /dev/null
+++ b/rust/kernel/regulator.rs
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! SoC Regulators
+
+use crate::{
+    bindings,
+    error::{code::*, Error, Result},
+};
+
+/// Regulators 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),
+        }
+    }
+}

-- 
2.45.2


  parent reply	other threads:[~2024-12-18 23:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-18 23:36 [PATCH 0/9] Regulator driver with I2C/Regmap Rust abstractions Fabien Parent
2024-12-18 23:36 ` [PATCH 1/9] rust: i2c: add basic I2C client abstraction Fabien Parent
2024-12-19 13:03   ` Rob Herring
2024-12-19 15:33     ` Fabien Parent
2024-12-18 23:36 ` [PATCH 2/9] rust: add abstraction for regmap Fabien Parent
2024-12-20 13:16   ` Mark Brown
2024-12-18 23:36 ` [PATCH 3/9] rust: error: add declaration for ENOTRECOVERABLE error Fabien Parent
2024-12-18 23:36 ` Fabien Parent [this message]
2024-12-18 23:36 ` [PATCH 5/9] rust: regulator: add Regulator Driver abstraction Fabien Parent
2024-12-19 10:26   ` Danilo Krummrich
2024-12-19 16:00     ` Fabien Parent
2024-12-19 18:58       ` Mark Brown
2024-12-18 23:36 ` [PATCH 6/9] rust: regulator: add support for regmap Fabien Parent
2024-12-18 23:36 ` [PATCH 7/9] dt-bindings: regulator: add binding for ncv6336 regulator Fabien Parent
2024-12-19  9:28   ` Krzysztof Kozlowski
2024-12-19 16:13     ` Fabien Parent
2024-12-21 20:20       ` Krzysztof Kozlowski
2024-12-18 23:36 ` [PATCH 8/9] regulator: add driver " Fabien Parent
2024-12-19 10:19   ` Dirk Behme
2024-12-20 14:50   ` Mark Brown
2024-12-18 23:36 ` [PATCH 9/9] arm64: dts: qcom: apq8039-t2: add node " Fabien Parent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241218-ncv6336-v1-4-b8d973747f7a@gmail.com \
    --to=parent.f@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=andersson@kernel.org \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=fabien.parent@linaro.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=konradybcio@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=robh@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=saravanak@google.com \
    --cc=tmgross@umich.edu \
    --cc=vinod.koul@linaro.org \
    --cc=wsa+renesas@sang-engineering.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox