Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples: rust: add Rust I2C sample driver

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>

authored by

Igor Korotin and committed by
Danilo Krummrich
d05b8e97 f3cc26a4

+87
+1
MAINTAINERS
··· 11750 11750 L: rust-for-linux@vger.kernel.org 11751 11751 S: Maintained 11752 11752 F: rust/kernel/i2c.rs 11753 + F: samples/rust/rust_driver_i2c.rs 11753 11754 11754 11755 I2C SUBSYSTEM HOST DRIVERS 11755 11756 M: Andi Shyti <andi.shyti@kernel.org>
+11
samples/rust/Kconfig
··· 84 84 85 85 If unsure, say N. 86 86 87 + config SAMPLE_RUST_DRIVER_I2C 88 + tristate "I2C Driver" 89 + depends on I2C=y 90 + help 91 + This option builds the Rust I2C driver sample. 92 + 93 + To compile this as a module, choose M here: 94 + the module will be called rust_driver_i2c. 95 + 96 + If unsure, say N. 97 + 87 98 config SAMPLE_RUST_DRIVER_PCI 88 99 tristate "PCI Driver" 89 100 depends on PCI
+1
samples/rust/Makefile
··· 7 7 obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) += rust_debugfs.o 8 8 obj-$(CONFIG_SAMPLE_RUST_DEBUGFS_SCOPED) += rust_debugfs_scoped.o 9 9 obj-$(CONFIG_SAMPLE_RUST_DMA) += rust_dma.o 10 + obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o 10 11 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o 11 12 obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o 12 13 obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o
+74
samples/rust/rust_driver_i2c.rs
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + //! Rust I2C driver sample. 4 + 5 + use kernel::{ 6 + acpi, 7 + c_str, 8 + device::Core, 9 + i2c, 10 + of, 11 + prelude::*, // 12 + }; 13 + 14 + struct SampleDriver; 15 + 16 + kernel::acpi_device_table! { 17 + ACPI_TABLE, 18 + MODULE_ACPI_TABLE, 19 + <SampleDriver as i2c::Driver>::IdInfo, 20 + [(acpi::DeviceId::new(c_str!("LNUXBEEF")), 0)] 21 + } 22 + 23 + kernel::i2c_device_table! { 24 + I2C_TABLE, 25 + MODULE_I2C_TABLE, 26 + <SampleDriver as i2c::Driver>::IdInfo, 27 + [(i2c::DeviceId::new(c_str!("rust_driver_i2c")), 0)] 28 + } 29 + 30 + kernel::of_device_table! { 31 + OF_TABLE, 32 + MODULE_OF_TABLE, 33 + <SampleDriver as i2c::Driver>::IdInfo, 34 + [(of::DeviceId::new(c_str!("test,rust_driver_i2c")), 0)] 35 + } 36 + 37 + impl i2c::Driver for SampleDriver { 38 + type IdInfo = u32; 39 + 40 + const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE); 41 + const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE); 42 + const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE); 43 + 44 + fn probe( 45 + idev: &i2c::I2cClient<Core>, 46 + info: Option<&Self::IdInfo>, 47 + ) -> impl PinInit<Self, Error> { 48 + let dev = idev.as_ref(); 49 + 50 + dev_info!(dev, "Probe Rust I2C driver sample.\n"); 51 + 52 + if let Some(info) = info { 53 + dev_info!(dev, "Probed with info: '{}'.\n", info); 54 + } 55 + 56 + Ok(Self) 57 + } 58 + 59 + fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) { 60 + dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n"); 61 + } 62 + 63 + fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) { 64 + dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n"); 65 + } 66 + } 67 + 68 + kernel::module_i2c_driver! { 69 + type: SampleDriver, 70 + name: "rust_driver_i2c", 71 + authors: ["Igor Korotin"], 72 + description: "Rust I2C driver", 73 + license: "GPL v2", 74 + }