[PATCH 3/3] sample: rust: Add GPIO consumer sample driver
From: Kohei Ito
Date: Sun Sep 06 2026 - 04:49:54 EST
Add a sample driver to demonstrate the use of the Rust GPIO APIs.
Signed-off-by: Kohei Ito <koheiito.dev@xxxxxxxxx>
---
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