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 3/3] sample: rust: Add GPIO consumer sample driver
Date: Sun, 06 Sep 2026 17:45:51 +0900 [thread overview]
Message-ID: <20260906-add-rust-gpio-consumer-v1-3-24d192f93760@gmail.com> (raw)
In-Reply-To: <20260906-add-rust-gpio-consumer-v1-0-24d192f93760@gmail.com>
Add a sample driver to demonstrate the use of the Rust GPIO APIs.
Signed-off-by: Kohei Ito <koheiito.dev@gmail.com>
---
samples/rust/Kconfig | 11 +++
samples/rust/Makefile | 1 +
samples/rust/rust_gpio_consumer.rs | 156 +++++++++++++++++++++++++++++++++++++
3 files changed, 168 insertions(+)
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index c49ab9106345..1085d90c87f3 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -161,6 +161,17 @@ config SAMPLE_RUST_DRIVER_AUXILIARY
If unsure, say N.
+config SAMPLE_RUST_GPIO_CONSUMER
+ tristate "GPIO Consumer Driver"
+ depends on GPIOLIB
+ help
+ This option builds the Rust GPIO consumer sample.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_gpio_consumer.
+
+ If unsure, say N.
+
config SAMPLE_RUST_SOC
tristate "SoC Driver"
select SOC_BUS
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index 6c0aaa58cccc..1cf181dea6d3 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o
+obj-$(CONFIG_SAMPLE_RUST_GPIO_CONSUMER) += rust_gpio_consumer.o
obj-$(CONFIG_SAMPLE_RUST_SOC) += rust_soc.o
rust_print-y := rust_print_main.o rust_print_events.o
diff --git a/samples/rust/rust_gpio_consumer.rs b/samples/rust/rust_gpio_consumer.rs
new file mode 100644
index 000000000000..b70b305126fc
--- /dev/null
+++ b/samples/rust/rust_gpio_consumer.rs
@@ -0,0 +1,156 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust GPIO consumer driver sample.
+
+use kernel::{
+ device::{
+ self,
+ Core, //
+ },
+ gpio::{
+ self,
+ consumer::{
+ GpioDesc,
+ GpiodFlags, //
+ }, //
+ },
+ of,
+ platform,
+ prelude::*,
+ time::{
+ delay::fsleep,
+ Delta, //
+ }, //
+};
+
+struct SampleDriver;
+
+impl platform::Driver for SampleDriver {
+ type IdInfo = ();
+ type Data<'bound> = Self;
+ const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
+
+ fn probe<'bound>(
+ pdev: &'bound platform::Device<Core<'_>>,
+ _info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
+ let dev = pdev.as_ref();
+
+ dev_dbg!(dev, "Probe Rust GPIO consumer driver sample.\n");
+
+ Self::examine_gpio(dev)?;
+
+ Ok(Self)
+ }
+}
+
+type GetValue = fn(&GpioDesc) -> Result<gpio::LogicalLineLevel>;
+type GetRawValue = fn(&GpioDesc) -> Result<gpio::PhysicalLineLevel>;
+type SetValue = fn(&GpioDesc, gpio::LogicalLineLevel) -> Result<()>;
+type SetRawValue = fn(&GpioDesc, gpio::PhysicalLineLevel) -> Result<()>;
+
+impl SampleDriver {
+ fn examine_gpio(dev: &device::Device) -> Result {
+ let gpiod = GpioDesc::get(dev, None, GpiodFlags::ASIS)?;
+
+ let cansleep = gpiod.cansleep()?;
+ let (get_value, get_raw_value, set_value, set_raw_value): (
+ GetValue,
+ GetRawValue,
+ SetValue,
+ SetRawValue,
+ ) = if cansleep {
+ (
+ |gpiod| gpiod.get_value_cansleep(),
+ |gpiod| gpiod.get_raw_value_cansleep(),
+ |gpiod, value| gpiod.set_value_cansleep(value),
+ |gpiod, value| gpiod.set_raw_value_cansleep(value),
+ )
+ } else {
+ (
+ |gpiod| gpiod.get_value(),
+ |gpiod| gpiod.get_raw_value(),
+ |gpiod, value| gpiod.set_value(value),
+ |gpiod, value| gpiod.set_raw_value(value),
+ )
+ };
+
+ let pr_status = || -> Result<()> {
+ dev_info!(
+ dev,
+ "direction: {}, value: {}, raw value: {}\n",
+ gpiod.get_direction()?,
+ get_value(&gpiod)?,
+ get_raw_value(&gpiod)?
+ );
+ Ok(())
+ };
+
+ let active_low = gpiod.is_active_low()?;
+
+ dev_info!(
+ dev,
+ "got the GPIO ({}{})\n",
+ if active_low {
+ "active low"
+ } else {
+ "active high"
+ },
+ if cansleep { ", sleepy" } else { "" }
+ );
+ pr_status()?;
+
+ gpiod.direction_output(gpio::LogicalLineLevel::Inactive)?;
+ dev_info!(dev, "line is inactivated\n");
+ pr_status()?;
+
+ fsleep(Delta::from_millis(1));
+
+ set_value(&gpiod, gpio::LogicalLineLevel::Active)?;
+ dev_info!(dev, "line is activated\n");
+ pr_status()?;
+
+ fsleep(Delta::from_millis(1));
+
+ set_value(&gpiod, gpio::LogicalLineLevel::Inactive)?;
+ dev_info!(dev, "GPIO: line is inactivated\n");
+ pr_status()?;
+
+ fsleep(Delta::from_millis(1));
+
+ let level = get_raw_value(&gpiod)?;
+
+ gpiod.direction_input()?;
+ dev_info!(dev, "line is input mode\n");
+ pr_status()?;
+
+ fsleep(Delta::from_millis(1));
+
+ gpiod.direction_output_raw(!level)?;
+ dev_info!(dev, "line is toggled\n");
+ pr_status()?;
+
+ fsleep(Delta::from_millis(1));
+
+ set_raw_value(&gpiod, !!level)?;
+ dev_info!(dev, "line is toggled\n");
+ pr_status()?;
+
+ Ok(())
+ }
+}
+
+kernel::of_device_table!(
+ OF_TABLE,
+ MODULE_OF_TABLE,
+ <SampleDriver as platform::Driver>::IdInfo,
+ [(of::DeviceId::new(c"test,rust-gpio-consumer"), ())]
+);
+
+kernel::module_platform_driver! {
+ type: SampleDriver,
+ name: "rust_gpio_consumer",
+ authors: ["Kohei Ito"],
+ description: "Rust GPIO consumer driver",
+ license: "GPL v2",
+}
--
2.50.1
next prev parent 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 ` [PATCH 1/3] rust: gpio: add GPIO module with common definitions Kohei Ito
2026-09-06 9:56 ` 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 ` Kohei Ito [this message]
2026-09-10 7:37 ` [PATCH 3/3] sample: rust: Add GPIO consumer sample driver 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-3-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