]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
samples: rust: add Rust I2C sample driver
authorIgor Korotin <igor.korotin.linux@gmail.com>
Sun, 16 Nov 2025 16:22:04 +0000 (16:22 +0000)
committerDanilo Krummrich <dakr@kernel.org>
Mon, 17 Nov 2025 21:28:25 +0000 (10:28 +1300)
Add a new `rust_driver_i2c` sample, showing how to create a new
i2c driver using ACPI/OF/Legacy ID tables.

Acked-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Signed-off-by: Igor Korotin <igor.korotin.linux@gmail.com>
Link: https://patch.msgid.link/20251116162204.171518-1-igor.korotin.linux@gmail.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
MAINTAINERS
samples/rust/Kconfig
samples/rust/Makefile
samples/rust/rust_driver_i2c.rs [new file with mode: 0644]

index a8dfde2f56331eb4560e0ea5ea146ad6f41cd6c0..8133987c4b54c4a9b5d0b759e14e93a13fb4b084 100644 (file)
@@ -11750,6 +11750,7 @@ R:      Daniel Almeida <daniel.almeida@collabora.com>
 L:     rust-for-linux@vger.kernel.org
 S:     Maintained
 F:     rust/kernel/i2c.rs
+F:     samples/rust/rust_driver_i2c.rs
 
 I2C SUBSYSTEM HOST DRIVERS
 M:     Andi Shyti <andi.shyti@kernel.org>
index c376eb899b7aaef73d095bd78b2b24f6c598f2d8..181466319588710dcabadd4a915f6748bb66da78 100644 (file)
@@ -84,6 +84,17 @@ config SAMPLE_RUST_DEBUGFS_SCOPED
 
          If unsure, say N.
 
+config SAMPLE_RUST_DRIVER_I2C
+       tristate "I2C Driver"
+       depends on I2C=y
+       help
+         This option builds the Rust I2C driver sample.
+
+         To compile this as a module, choose M here:
+         the module will be called rust_driver_i2c.
+
+         If unsure, say N.
+
 config SAMPLE_RUST_DRIVER_PCI
        tristate "PCI Driver"
        depends on PCI
index cf8422f8f219367d4d87a5e1b52706cccf03994e..aec865e2adef707cfe7ad9de3b1156065ea0890b 100644 (file)
@@ -7,6 +7,7 @@ obj-$(CONFIG_SAMPLE_RUST_PRINT)                 += rust_print.o
 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_PCI)           += rust_driver_pci.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM)      += rust_driver_platform.o
 obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB)           += rust_driver_usb.o
diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c.rs
new file mode 100644 (file)
index 0000000..ecefeca
--- /dev/null
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust I2C driver sample.
+
+use kernel::{
+    acpi,
+    c_str,
+    device::Core,
+    i2c,
+    of,
+    prelude::*, //
+};
+
+struct SampleDriver;
+
+kernel::acpi_device_table! {
+    ACPI_TABLE,
+    MODULE_ACPI_TABLE,
+    <SampleDriver as i2c::Driver>::IdInfo,
+    [(acpi::DeviceId::new(c_str!("LNUXBEEF")), 0)]
+}
+
+kernel::i2c_device_table! {
+    I2C_TABLE,
+    MODULE_I2C_TABLE,
+    <SampleDriver as i2c::Driver>::IdInfo,
+    [(i2c::DeviceId::new(c_str!("rust_driver_i2c")), 0)]
+}
+
+kernel::of_device_table! {
+    OF_TABLE,
+    MODULE_OF_TABLE,
+    <SampleDriver as i2c::Driver>::IdInfo,
+    [(of::DeviceId::new(c_str!("test,rust_driver_i2c")), 0)]
+}
+
+impl i2c::Driver for SampleDriver {
+    type IdInfo = u32;
+
+    const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
+    const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
+    const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
+
+    fn probe(
+        idev: &i2c::I2cClient<Core>,
+        info: Option<&Self::IdInfo>,
+    ) -> impl PinInit<Self, Error> {
+        let dev = idev.as_ref();
+
+        dev_info!(dev, "Probe Rust I2C driver sample.\n");
+
+        if let Some(info) = info {
+            dev_info!(dev, "Probed with info: '{}'.\n", info);
+        }
+
+        Ok(Self)
+    }
+
+    fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+        dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
+    }
+
+    fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) {
+        dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
+    }
+}
+
+kernel::module_i2c_driver! {
+    type: SampleDriver,
+    name: "rust_driver_i2c",
+    authors: ["Igor Korotin"],
+    description: "Rust I2C driver",
+    license: "GPL v2",
+}