Rust for Linux List
 help / color / mirror / Atom feed
From: Kohei Ito <koheiito.dev@gmail.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 linux-gpio@vger.kernel.org, Kohei Ito <koheiito.dev@gmail.com>
Subject: [PATCH 1/3] rust: gpio: add GPIO module with common definitions
Date: Sun, 06 Sep 2026 17:45:49 +0900	[thread overview]
Message-ID: <20260906-add-rust-gpio-consumer-v1-1-24d192f93760@gmail.com> (raw)
In-Reply-To: <20260906-add-rust-gpio-consumer-v1-0-24d192f93760@gmail.com>

Add the top-level GPIO module with minimal common definitions. This
module is the basis for future Rust GPIO extensions.

Signed-off-by: Kohei Ito <koheiito.dev@gmail.com>
---
 rust/bindings/bindings_helper.h |   1 +
 rust/kernel/gpio.rs             | 152 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 3 files changed, 155 insertions(+)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 1124785e210b..98b048b36771 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -61,6 +61,7 @@
 #include <linux/file.h>
 #include <linux/firmware.h>
 #include <linux/fs.h>
+#include <linux/gpio/defs.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
 #include <linux/io-pgtable.h>
diff --git a/rust/kernel/gpio.rs b/rust/kernel/gpio.rs
new file mode 100644
index 000000000000..819efc8a0c05
--- /dev/null
+++ b/rust/kernel/gpio.rs
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! GPIO abstractions.
+
+use crate::{
+    error::{
+        Error,
+        Result, //
+    },
+    fmt,
+    prelude::*, //
+};
+
+/// Describes GPIO direction.
+#[derive(Clone, Copy, PartialEq, Eq)]
+#[repr(u32)]
+pub enum LineDirection {
+    /// Represent the output direction.
+    Out = bindings::GPIO_LINE_DIRECTION_OUT,
+
+    /// Represent the input direction.
+    In = bindings::GPIO_LINE_DIRECTION_IN,
+}
+
+impl core::ops::Not for LineDirection {
+    type Output = Self;
+    fn not(self) -> Self::Output {
+        match self {
+            Self::Out => Self::In,
+            Self::In => Self::Out,
+        }
+    }
+}
+
+impl TryFrom<c_int> for LineDirection {
+    type Error = Error;
+    fn try_from(value: c_int) -> Result<Self> {
+        match value {
+            v if v == Self::Out as c_int => Ok(Self::Out),
+            v if v == Self::In as c_int => Ok(Self::In),
+            _ => Err(EINVAL),
+        }
+    }
+}
+
+impl fmt::Display for LineDirection {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            Self::In => f.pad("In"),
+            Self::Out => f.pad("Out"),
+        }
+    }
+}
+
+/// Describes the logical GPIO level, i.e. taking the ACTIVE_LOW status into account.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub enum LogicalLineLevel {
+    /// Represent the logical inactive level.
+    Inactive,
+
+    /// Represent the logical active level.
+    Active,
+}
+
+impl core::ops::Not for LogicalLineLevel {
+    type Output = Self;
+    fn not(self) -> Self::Output {
+        match self {
+            Self::Inactive => Self::Active,
+            Self::Active => Self::Inactive,
+        }
+    }
+}
+
+impl LogicalLineLevel {
+    fn as_c_int(&self) -> c_int {
+        match self {
+            Self::Inactive => 0,
+            Self::Active => 1,
+        }
+    }
+}
+
+impl TryFrom<c_int> for LogicalLineLevel {
+    type Error = Error;
+    fn try_from(value: c_int) -> Result<Self> {
+        match value {
+            0 => Ok(Self::Inactive),
+            1 => Ok(Self::Active),
+            _ => Err(EINVAL),
+        }
+    }
+}
+
+impl fmt::Display for LogicalLineLevel {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            Self::Inactive => f.pad("Inactive"),
+            Self::Active => f.pad("Active"),
+        }
+    }
+}
+
+/// Describes the raw GPIO level, i.e. the value of its physical line without regard for its
+/// ACTIVE_LOW status.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub enum PhysicalLineLevel {
+    /// Represent the physical LOW level.
+    Low,
+
+    /// Represent the physical HIGH level.
+    High,
+}
+
+impl core::ops::Not for PhysicalLineLevel {
+    type Output = Self;
+    fn not(self) -> Self::Output {
+        match self {
+            Self::Low => Self::High,
+            Self::High => Self::Low,
+        }
+    }
+}
+
+impl PhysicalLineLevel {
+    fn as_c_int(&self) -> c_int {
+        match self {
+            Self::Low => 0,
+            Self::High => 1,
+        }
+    }
+}
+
+impl TryFrom<c_int> for PhysicalLineLevel {
+    type Error = Error;
+    fn try_from(value: c_int) -> Result<Self> {
+        match value {
+            0 => Ok(Self::Low),
+            1 => Ok(Self::High),
+            _ => Err(EINVAL),
+        }
+    }
+}
+
+impl fmt::Display for PhysicalLineLevel {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            Self::Low => f.pad("Low"),
+            Self::High => f.pad("High"),
+        }
+    }
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 9512af7156df..6c96c1269249 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -73,6 +73,8 @@
 pub mod firmware;
 pub mod fmt;
 pub mod fs;
+#[cfg(CONFIG_GPIOLIB)]
+pub mod gpio;
 #[cfg(CONFIG_GPU_BUDDY = "y")]
 pub mod gpu;
 #[cfg(CONFIG_I2C = "y")]

-- 
2.50.1


  reply	other threads:[~2026-09-06  8:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-06  8:45 [PATCH 0/3] rust: Add basic GPIO consumer abstractions Kohei Ito
2026-09-06  8:45 ` Kohei Ito [this message]
2026-09-06  9:56   ` [PATCH 1/3] rust: gpio: add GPIO module with common definitions Miguel Ojeda
2026-09-06 13:09     ` Gary Guo
2026-09-06 15:53       ` Kohei Ito
2026-09-06  8:45 ` [PATCH 2/3] rust: gpio: Add basic consumer abstractions Kohei Ito
2026-09-10  7:38   ` Bartosz Golaszewski
2026-09-13  8:58   ` Alexandre Courbot
2026-09-06  8:45 ` [PATCH 3/3] sample: rust: Add GPIO consumer sample driver Kohei Ito
2026-09-10  7:37   ` Bartosz Golaszewski
2026-09-13  8:46     ` Kohei Ito

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=20260906-add-rust-gpio-consumer-v1-1-24d192f93760@gmail.com \
    --to=koheiito.dev@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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