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 probe( idev: &i2c::I2cClient<Core>, info: Option<&Self::IdInfo>, ) -> impl PinInit<Self, Error>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 shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>)59 fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) { 60 dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n"); 61 } 62 unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>)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 } 75