// SPDX-License-Identifier: GPL-2.012//! Rust I2C client registration sample.3//!4//! An I2C client in Rust cannot exist on its own. To register a new I2C client,5//! it must be bound to a parent device. In this sample driver, a platform device6//! is used as the parent.7//!89//! ACPI match table test10//!11//! This demonstrates how to test an ACPI-based Rust I2C client registration driver12//! using QEMU with a custom SSDT.13//!14//! Steps:15//!16//! 1. **Create an SSDT source file** (`ssdt.dsl`) with the following content:17//!18//! ```asl19//! DefinitionBlock ("", "SSDT", 2, "TEST", "VIRTACPI", 0x00000001)20//! {21//! Scope (\_SB)22//! {23//! Device (T432)24//! {25//! Name (_HID, "LNUXBEEF") // ACPI hardware ID to match26//! Name (_UID, 1)27//! Name (_STA, 0x0F) // Device present, enabled28//! Name (_CRS, ResourceTemplate ()29//! {30//! Memory32Fixed (ReadWrite, 0xFED00000, 0x1000)31//! })32//! }33//! }34//! }35//! ```36//!37//! 2. **Compile the table**:38//!39//! ```sh40//! iasl -tc ssdt.dsl41//! ```42//!43//! This generates `ssdt.aml`44//!45//! 3. **Run QEMU** with the compiled AML file:46//!47//! ```sh48//! qemu-system-x86_64 -m 512M \49//! -enable-kvm \50//! -kernel path/to/bzImage \51//! -append "root=/dev/sda console=ttyS0" \52//! -hda rootfs.img \53//! -serial stdio \54//! -acpitable file=ssdt.aml55//! ```56//!57//! Requirements:58//! - The `rust_driver_platform` must be present either:59//! - built directly into the kernel (`bzImage`), or60//! - available as a `.ko` file and loadable from `rootfs.img`61//!62//! 4. **Verify it worked** by checking `dmesg`:63//!64//! ```65//! rust_driver_platform LNUXBEEF:00: Probed with info: '0'.66//! ```67//!6869use kernel::{70acpi,71c_str,72device,73devres::Devres,74i2c,75of,76platform,77prelude::*,78sync::aref::ARef, //79};8081#[pin_data]82struct SampleDriver {83parent_dev: ARef<platform::Device>,84#[pin]85_reg: Devres<i2c::Registration>,86}8788kernel::of_device_table!(89OF_TABLE,90MODULE_OF_TABLE,91<SampleDriver as platform::Driver>::IdInfo,92[(of::DeviceId::new(c_str!("test,rust-device")), ())]93);9495kernel::acpi_device_table!(96ACPI_TABLE,97MODULE_ACPI_TABLE,98<SampleDriver as platform::Driver>::IdInfo,99[(acpi::DeviceId::new(c_str!("LNUXBEEF")), ())]100);101102const SAMPLE_I2C_CLIENT_ADDR: u16 = 0x30;103const SAMPLE_I2C_ADAPTER_INDEX: i32 = 0;104const BOARD_INFO: i2c::I2cBoardInfo =105i2c::I2cBoardInfo::new(c_str!("rust_driver_i2c"), SAMPLE_I2C_CLIENT_ADDR);106107impl platform::Driver for SampleDriver {108type IdInfo = ();109const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);110const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);111112fn probe(113pdev: &platform::Device<device::Core>,114_info: Option<&Self::IdInfo>,115) -> impl PinInit<Self, Error> {116dev_info!(117pdev.as_ref(),118"Probe Rust I2C Client registration sample.\n"119);120121kernel::try_pin_init!( Self {122parent_dev: pdev.into(),123124_reg <- {125let adapter = i2c::I2cAdapter::get(SAMPLE_I2C_ADAPTER_INDEX)?;126127i2c::Registration::new(&adapter, &BOARD_INFO, pdev.as_ref())128}129})130}131132fn unbind(pdev: &platform::Device<device::Core>, _this: Pin<&Self>) {133dev_info!(134pdev.as_ref(),135"Unbind Rust I2C Client registration sample.\n"136);137}138}139140kernel::module_platform_driver! {141type: SampleDriver,142name: "rust_device_i2c",143authors: ["Danilo Krummrich", "Igor Korotin"],144description: "Rust I2C client registration",145license: "GPL v2",146}147148149