xref: /linux/samples/rust/rust_soc.rs (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Rust SoC Platform driver sample.
4 
5 use kernel::{
6     acpi,
7     device::Core,
8     of,
9     platform,
10     prelude::*,
11     soc,
12     str::CString,
13     sync::aref::ARef, //
14 };
15 use pin_init::pin_init_scope;
16 
17 #[pin_data]
18 struct SampleSocDriver {
19     pdev: ARef<platform::Device>,
20     #[pin]
21     _dev_reg: soc::Registration,
22 }
23 
24 kernel::of_device_table!(
25     OF_TABLE,
26     MODULE_OF_TABLE,
27     <SampleSocDriver as platform::Driver>::IdInfo,
28     [(of::DeviceId::new(c"test,rust-device"), ())]
29 );
30 
31 kernel::acpi_device_table!(
32     ACPI_TABLE,
33     MODULE_ACPI_TABLE,
34     <SampleSocDriver as platform::Driver>::IdInfo,
35     [(acpi::DeviceId::new(c"LNUXBEEF"), ())]
36 );
37 
38 impl platform::Driver for SampleSocDriver {
39     type IdInfo = ();
40     type Data<'bound> = Self;
41     const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
42     const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
43 
44     fn probe<'bound>(
45         pdev: &'bound platform::Device<Core<'_>>,
46         _info: Option<&'bound Self::IdInfo>,
47     ) -> impl PinInit<Self, Error> + 'bound {
48         dev_dbg!(pdev, "Probe Rust SoC driver sample.\n");
49 
50         let pdev = pdev.into();
51         pin_init_scope(move || {
52             let machine = CString::try_from(c"My cool ACME15 dev board")?;
53             let family = CString::try_from(c"ACME")?;
54             let revision = CString::try_from(c"1.2")?;
55             let serial_number = CString::try_from(c"12345")?;
56             let soc_id = CString::try_from(c"ACME15")?;
57 
58             let attr = soc::Attributes {
59                 machine: Some(machine),
60                 family: Some(family),
61                 revision: Some(revision),
62                 serial_number: Some(serial_number),
63                 soc_id: Some(soc_id),
64             };
65 
66             Ok(try_pin_init!(SampleSocDriver {
67                 pdev: pdev,
68                 _dev_reg <- soc::Registration::new(attr),
69             }? Error))
70         })
71     }
72 }
73 
74 kernel::module_platform_driver! {
75     type: SampleSocDriver,
76     name: "rust_soc",
77     authors: ["Matthew Maurer"],
78     description: "Rust SoC Driver",
79     license: "GPL",
80 }
81