xref: /linux/samples/rust/rust_driver_i2c.rs (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Rust I2C driver sample.
4 
5 use kernel::{
6     acpi,
7     device::Core,
8     i2c,
9     of,
10     prelude::*, //
11 };
12 
13 struct SampleDriver;
14 
15 kernel::acpi_device_table! {
16     ACPI_TABLE,
17     <SampleDriver as i2c::Driver>::IdInfo,
18     [(acpi::DeviceId::new(c"LNUXBEEF"), 0)]
19 }
20 
21 kernel::i2c_device_table! {
22     I2C_TABLE,
23     <SampleDriver as i2c::Driver>::IdInfo,
24     [(i2c::DeviceId::new(c"rust_driver_i2c"), 0)]
25 }
26 
27 kernel::of_device_table! {
28     OF_TABLE,
29     <SampleDriver as i2c::Driver>::IdInfo,
30     [(of::DeviceId::new(c"test,rust_driver_i2c"), 0)]
31 }
32 
33 impl i2c::Driver for SampleDriver {
34     type IdInfo = u32;
35     type Data<'bound> = Self;
36 
37     const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
38     const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
39     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
40 
41     fn probe<'bound>(
42         idev: &'bound i2c::I2cClient<Core<'_>>,
43         info: Option<&'bound Self::IdInfo>,
44     ) -> impl PinInit<Self, Error> + 'bound {
45         let dev = idev.as_ref();
46 
47         dev_info!(dev, "Probe Rust I2C driver sample.\n");
48 
49         if let Some(info) = info {
50             dev_info!(dev, "Probed with info: '{}'.\n", info);
51         }
52 
53         Ok(Self)
54     }
55 
56     fn shutdown<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
57         dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
58     }
59 
60     fn unbind<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
61         dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
62     }
63 }
64 
65 kernel::module_i2c_driver! {
66     type: SampleDriver,
67     name: "rust_driver_i2c",
68     authors: ["Igor Korotin"],
69     description: "Rust I2C driver",
70     license: "GPL v2",
71 }
72