From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B8C14229A6 for ; Sun, 6 Sep 2026 08:46:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788684383; cv=none; b=Ts5xPBtW2F+OevsK0BbMf+lf/Fd+/YoF4hjb+ccLI0g7nLp7UhbmGlWsgKR4sdxqAwKYdFttT6vaC59wNryFENmcxboRsoaoOGSIS/sakddQW5VXRSUxZihrmPKp7g1QAHRwWIv+obFPHw18c3F0gbX2X7YS4HJAFqSvV8LYU54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788684383; c=relaxed/simple; bh=UFA5fI8C+4EiuKIzvPGp1iegNLyohGUY8/0+ZYqGEXA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PgtiPbRkKZ+/JNrOBFfAXtdoklNM95Q4U1J8RnUeFdvT1ZVbC4I0hLEmePJ1rSFWL2JlH+BRR2qcva7upm1pXvdEgcXEa67e5mjwCBEDGvh71QKhJ29bQgElZex8w3+C1f8U/a4nnYiVnFodyLBAFSfbouCv8fpaGF5UzwUNNtc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j0j/fD/L; arc=none smtp.client-ip=209.85.210.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j0j/fD/L" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-8487214ad2bso2761039b3a.1 for ; Sun, 06 Sep 2026 01:46:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788684381; x=1789289181; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/gkzIc2mrM4Qdq/WyMfgp/R6p5u/2x3xKFYvN0yjh84=; b=j0j/fD/LrMDYgCiVOcsLBVWp7yp4OKf/wP9yCmjM3LwC1xlKJxSyos6fMKVNGC09DV v7DniNZbWTKezAljB9rkcENQwJcw0jDXDDAkOlIbVYjw87LzDMuxJOdo0tr1XpbscNjJ AB27Ya6EqEv3bYT0ROSD/ZChbHh7/w1aoHi+WMYVITzk+u84gMMmmB8EW5S/hHIv7rwh o8uiHmTjMvAuMnAPtPvodmE4VSTvGjQAV8/hCVStt3ZrO0s39Ls/GWbGQIwUcI+5nzp6 2Qfc60dn5PHCPujww5uF9QFUT5a+noyshVBpMKHqgHE82JNKPxQmWeGwg76ysuwe9Ccv nBeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788684381; x=1789289181; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/gkzIc2mrM4Qdq/WyMfgp/R6p5u/2x3xKFYvN0yjh84=; b=MUvK0zJ8+u5LmIxPrgzgSOW8NAdswrHBpUOAK/Ng2OrS8Tpps5M8KpuJYRR8jS+WKV Dd16paryV/qE1BpB0oUGvN0Hlc3CLMfZVAMGlT6YH4CI5x9Hj8d+sk26SLv4ZiV2CKeQ qng+OL5Tqz40sZ5mNdHl/M3VP6Hfrzjn+WZO3aNg4lC+GjNTIHzEQ9A57+ayxnBoxh9V BG9nbA6n4DSbhmHpJypQ2mgzQjI6v+hDK03AgjKL//ObS0AYHCoAB42nBPstSQELkXcC Yye0XPdGyC4JztVjdSMp+BlSSZDbyfCV++MzpGvOicN2w24NiYECJHRkoWviJywGlo9D /tOA== X-Forwarded-Encrypted: i=1; AKwUvBx48brR42xSZDqbxtQ4XK9SbV6q20tdsK7JWggW17iS31NWLyRBc6+9JTqCoLL+zw6zGjlixW3uh8bSYICeNQ==@vger.kernel.org X-Gm-Message-State: AFuF++m3LBR9CPUtDwEEEgDdYmmJwiJudd9FEY8Hq7IPB0eTLiR6e0kp zDP8bheIztxMz0r1hhmNr4FdJX7ULzBHzwQVxmjiLYPXx6JoBaJHU9l9 X-Gm-Gg: AYBFou2zrodVVSWVFIpYvl/H/aHq/CVLSbdMWruVYrijiNsY2OiafVrd9FkvBUmrdGr EKXXH8xcP8eItard/Uc8gv8P5JWk3yiCaZKbe512BPQcePaALYtDh2PlfmWM9gangZcfdNAG82B rDplZhqNAPplcUritjBE+XBwlpX+D/Qo1MWforoX6khaji7OF9sS1Hq4AQwtTdKaD+7DBoE0bDb peXDZZt15CjANNbutKXchWNx4Omj3foMU6xEhKP9fjbtYMH9flemg2Tlarwk+HhQZ7bfHyOheOk 4qmDKk9NmXfrqk1m1uTFOYeG203OsGPsmMskUKbFyq6gJNV8LLJLwVBHGrWpg2PqO1wIkBi9BEl Z2gmFO0O/RWzOHqhI14+19ssHsdhU4aLPCrBCp8OALOKwKVOT0FSZ95wDj/J3g3n9ML5OZmluFV yTSjifuFzAkyn54Q8D2bbVhFLT+saYH6d+ZNEr6vXjTXiCQPwbgToeev7XXAujmSlhrRlUbS5WB L3YqL9u X-Received: by 2002:aa7:88c3:0:b0:848:4424:2b8e with SMTP id d2e1a72fcca58-861696788b3mr22513081b3a.3.1788684380945; Sun, 06 Sep 2026 01:46:20 -0700 (PDT) Received: from localhost (madb688426.ap.nuro.jp. [219.104.132.38]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cc4554770f6sm2697918a12.24.2026.09.06.01.46.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Sep 2026 01:46:20 -0700 (PDT) From: Kohei Ito Date: Sun, 06 Sep 2026 17:45:49 +0900 Subject: [PATCH 1/3] rust: gpio: add GPIO module with common definitions Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260906-add-rust-gpio-consumer-v1-1-24d192f93760@gmail.com> References: <20260906-add-rust-gpio-consumer-v1-0-24d192f93760@gmail.com> In-Reply-To: <20260906-add-rust-gpio-consumer-v1-0-24d192f93760@gmail.com> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-gpio@vger.kernel.org, Kohei Ito X-Mailer: b4 0.15.2 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 --- 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 #include #include +#include #include #include #include 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 for LineDirection { + type Error = Error; + fn try_from(value: c_int) -> Result { + 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 for LogicalLineLevel { + type Error = Error; + fn try_from(value: c_int) -> Result { + 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 for PhysicalLineLevel { + type Error = Error; + fn try_from(value: c_int) -> Result { + 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