[PATCH v2 3/3] samples: rust: add SPI driver sample

From: Shuai Liu

Date: Mon Oct 26 2026 - 06:20:10 EST


Add a sample driver for the Rust SPI abstractions to demonstrate their
usage.

Signed-off-by: Shuai Liu <847807867@xxxxxx>
---
samples/rust/Kconfig | 11 ++++++
samples/rust/Makefile | 1 +
samples/rust/rust_driver_spi.rs | 66 +++++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+)
create mode 100644 samples/rust/rust_driver_spi.rs

diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index 31d62533ef25..f99bf52654a1 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -84,6 +84,17 @@ config SAMPLE_RUST_DEBUGFS_SCOPED

If unsure, say N.

+config SAMPLE_RUST_DRIVER_SPI
+ tristate "SPI Driver"
+ depends on SPI=y
+ help
+ This option builds the Rust SPI driver sample.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_driver_spi.
+
+ If unsure, say N.
+
config SAMPLE_RUST_DRIVER_I2C
tristate "I2C Driver"
depends on I2C=y
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index b986b681cde5..efe04638a990 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) += rust_debugfs.o
obj-$(CONFIG_SAMPLE_RUST_DEBUGFS_SCOPED) += rust_debugfs_scoped.o
obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o
+obj-$(CONFIG_SAMPLE_RUST_DRIVER_SPI) += rust_driver_spi.o
obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) += rust_i2c_client.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
diff --git a/samples/rust/rust_driver_spi.rs b/samples/rust/rust_driver_spi.rs
new file mode 100644
index 000000000000..7eee7b735794
--- /dev/null
+++ b/samples/rust/rust_driver_spi.rs
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust SPI driver sample.
+
+use kernel::{
+ device::Core,
+ of,
+ prelude::*,
+ spi, //
+};
+
+struct SampleDriver;
+
+kernel::spi_device_table! {
+ SPI_TABLE,
+ <SampleDriver as spi::Driver>::IdInfo,
+ [(spi::DeviceId::new(c"rust_driver_spi"), 0)]
+}
+
+kernel::of_device_table! {
+ OF_TABLE,
+ <SampleDriver as spi::Driver>::IdInfo,
+ [(of::DeviceId::new(c"test,rust_driver_spi"), 0)]
+}
+
+impl spi::Driver for SampleDriver {
+ type IdInfo = u32;
+ type Data<'bound> = Self;
+
+ const SPI_ID_TABLE: Option<spi::IdTable<Self::IdInfo>> = Some(&SPI_TABLE);
+ const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
+
+ fn probe<'bound>(
+ sdev: &'bound spi::SpiDevice<Core<'_>>,
+ info: Option<&'bound Self::IdInfo>,
+ ) -> impl PinInit<Self, Error> + 'bound {
+ let dev = sdev.as_ref();
+
+ dev_info!(dev, "Probe Rust SPI driver sample.\n");
+ dev_info!(dev, "max_speed_hz: {}.\n", sdev.max_speed_hz());
+ dev_info!(dev, "bits_per_word: {}.\n", sdev.bits_per_word());
+ dev_info!(dev, "mode: {:#x}.\n", sdev.mode());
+
+ if let Some(info) = info {
+ dev_info!(dev, "Probed with info: '{}'.\n", info);
+ }
+
+ Ok(Self)
+ }
+
+ fn shutdown<'bound>(sdev: &'bound spi::SpiDevice<Core<'_>>, _this: Pin<&Self>) {
+ dev_info!(sdev.as_ref(), "Shutdown Rust SPI driver sample.\n");
+ }
+
+ fn unbind<'bound>(sdev: &'bound spi::SpiDevice<Core<'_>>, _this: Pin<&Self>) {
+ dev_info!(sdev.as_ref(), "Unbind Rust SPI driver sample.\n");
+ }
+}
+
+kernel::module_spi_driver! {
+ type: SampleDriver,
+ name: "rust_driver_spi",
+ authors: ["Shuai Liu"],
+ description: "Rust SPI driver",
+ license: "GPL v2",
+}
--
2.43.0