Add some example usage of the device property read methods for DT/ACPI/swnode properties.
Unified · Split
| @@ -37,6 +37,9 @@ · 36 unmodified lines | ||
| 37 | 37 | test-device@2 { |
| 38 | 38 | compatible = "test,rust-device"; |
| 39 | 39 | reg = <0x2>; |
| 40 | + | |
| 41 | + test,u32-prop = <0xdeadbeef>; | |
| 42 | + test,i16-array = /bits/ 16 <1 2 (-3) (-4)>; | |
| 40 | 43 | }; |
| 41 | 44 | }; |
| 42 | 45 | |
| @@ -2,7 +2,14 @@ · 1 unmodified line | ||
| 2 | 2 | |
| 3 | 3 | //! Rust Platform driver sample. |
| 4 | 4 | |
| 5 | −use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef}; | |
| 5 | +use kernel::{ | |
| 6 | + c_str, | |
| 7 | + device::{self, Core}, | |
| 8 | + of, platform, | |
| 9 | + prelude::*, | |
| 10 | + str::CString, | |
| 11 | + types::ARef, | |
| 12 | +}; | |
| 6 | 13 | |
| 7 | 14 | struct SampleDriver { |
| 8 | 15 | pdev: ARef<platform::Device>, |
| @@ -31,12 +38,63 @@ impl platform::Driver for SampleDriver { · 22 unmodified lines | ||
| 31 | 38 | dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0); |
| 32 | 39 | } |
| 33 | 40 | |
| 41 | + Self::properties_parse(pdev.as_ref())?; | |
| 42 | + | |
| 34 | 43 | let drvdata = KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?; |
| 35 | 44 | |
| 36 | 45 | Ok(drvdata.into()) |
| 37 | 46 | } |
| 38 | 47 | } |
| 39 | 48 | |
| 49 | +impl SampleDriver { | |
| 50 | + fn properties_parse(dev: &device::Device) -> Result { | |
| 51 | + let fwnode = dev.fwnode().ok_or(ENOENT)?; | |
| 52 | + | |
| 53 | + if let Ok(idx) = | |
| 54 | + fwnode.property_match_string(c_str!("compatible"), c_str!("test,rust-device")) | |
| 55 | + { | |
| 56 | + dev_info!(dev, "matched compatible string idx = {}\n", idx); | |
| 57 | + } | |
| 58 | + | |
| 59 | + let name = c_str!("compatible"); | |
| 60 | + let prop = fwnode.property_read::<CString>(name).required_by(dev)?; | |
| 61 | + dev_info!(dev, "'{name}'='{prop:?}'\n"); | |
| 62 | + | |
| 63 | + let name = c_str!("test,bool-prop"); | |
| 64 | + let prop = fwnode.property_read_bool(c_str!("test,bool-prop")); | |
| 65 | + dev_info!(dev, "'{name}'='{prop}'\n"); | |
| 66 | + | |
| 67 | + if fwnode.property_present(c_str!("test,u32-prop")) { | |
| 68 | + dev_info!(dev, "'test,u32-prop' is present\n"); | |
| 69 | + } | |
| 70 | + | |
| 71 | + let name = c_str!("test,u32-optional-prop"); | |
| 72 | + let prop = fwnode.property_read::<u32>(name).or(0x12); | |
| 73 | + dev_info!(dev, "'{name}'='{prop:#x}' (default = 0x12)\n",); | |
| 74 | + | |
| 75 | + // A missing required property will print an error. Discard the error to | |
| 76 | + // prevent properties_parse from failing in that case. | |
| 77 | + let name = c_str!("test,u32-required-prop"); | |
| 78 | + let _ = fwnode.property_read::<u32>(name).required_by(dev); | |
| 79 | + | |
| 80 | + let name = c_str!("test,u32-prop"); | |
| 81 | + let prop: u32 = fwnode.property_read(name).required_by(dev)?; | |
| 82 | + dev_info!(dev, "'{name}'='{prop:#x}'\n"); | |
| 83 | + | |
| 84 | + let name = c_str!("test,i16-array"); | |
| 85 | + let prop: [i16; 4] = fwnode.property_read(name).required_by(dev)?; | |
| 86 | + dev_info!(dev, "'{name}'='{prop:?}'\n"); | |
| 87 | + let len = fwnode.property_count_elem::<u16>(name)?; | |
| 88 | + dev_info!(dev, "'{name}' length is {len}\n",); | |
| 89 | + | |
| 90 | + let name = c_str!("test,i16-array"); | |
| 91 | + let prop: KVec<i16> = fwnode.property_read_array_vec(name, 4)?.required_by(dev)?; | |
| 92 | + dev_info!(dev, "'{name}'='{prop:?}' (KVec)\n"); | |
| 93 | + | |
| 94 | + Ok(()) | |
| 95 | + } | |
| 96 | +} | |
| 97 | + | |
| 40 | 98 | impl Drop for SampleDriver { |
| 41 | 99 | fn drop(&mut self) { |
| 42 | 100 | dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n"); |