| 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 |
MODULE_ACPI_TABLE, |
| 18 |
<SampleDriver as i2c::Driver>::IdInfo, |
| 19 |
[(acpi::DeviceId::new(c"LNUXBEEF"), 0)] |
| 20 |
} |
| 21 |
|
| 22 |
kernel::i2c_device_table! { |
| 23 |
I2C_TABLE, |
| 24 |
MODULE_I2C_TABLE, |
| 25 |
<SampleDriver as i2c::Driver>::IdInfo, |
| 26 |
[(i2c::DeviceId::new(c"rust_driver_i2c"), 0)] |
| 27 |
} |
| 28 |
|
| 29 |
kernel::of_device_table! { |
| 30 |
OF_TABLE, |
| 31 |
MODULE_OF_TABLE, |
| 32 |
<SampleDriver as i2c::Driver>::IdInfo, |
| 33 |
[(of::DeviceId::new(c"test,rust_driver_i2c"), 0)] |
| 34 |
} |
| 35 |
|
| 36 |
impl i2c::Driver for SampleDriver { |
| 37 |
type IdInfo = u32; |
| 38 |
|
| 39 |
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE); |
| 40 |
const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE); |
| 41 |
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE); |
| 42 |
|
| 43 |
fn probe( |
| 44 |
idev: &i2c::I2cClient<Core>, |
| 45 |
info: Option<&Self::IdInfo>, |
| 46 |
) -> impl PinInit<Self, Error> { |
| 47 |
let dev = idev.as_ref(); |
| 48 |
|
| 49 |
dev_info!(dev, "Probe Rust I2C driver sample.\n"); |
| 50 |
|
| 51 |
if let Some(info) = info { |
| 52 |
dev_info!(dev, "Probed with info: '{}'.\n", info); |
| 53 |
} |
| 54 |
|
| 55 |
Ok(Self) |
| 56 |
} |
| 57 |
|
| 58 |
fn shutdown(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) { |
| 59 |
dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n"); |
| 60 |
} |
| 61 |
|
| 62 |
fn unbind(idev: &i2c::I2cClient<Core>, _this: Pin<&Self>) { |
| 63 |
dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
kernel::module_i2c_driver! { |
| 68 |
type: SampleDriver, |
| 69 |
name: "rust_driver_i2c", |
| 70 |
authors: ["Igor Korotin"], |
| 71 |
description: "Rust I2C driver", |
| 72 |
license: "GPL v2", |
| 73 |
} |