[PATCH RFC 3/3] samples: rust: platform: Add property read examples

From: Rob Herring (Arm)
Date: Fri Oct 25 2024 - 17:10:51 EST


Add some example usage of the device property read methods for
DT/ACPI/swnode properties.

Signed-off-by: Rob Herring (Arm) <robh@xxxxxxxxxx>
---
drivers/of/unittest-data/tests-platform.dtsi | 3 +++
samples/rust/rust_driver_platform.rs | 22 ++++++++++++++++++++++
2 files changed, 25 insertions(+)

diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
index 2caaf1c10ee6..a5369b9343b8 100644
--- a/drivers/of/unittest-data/tests-platform.dtsi
+++ b/drivers/of/unittest-data/tests-platform.dtsi
@@ -37,6 +37,9 @@ dev@100 {
test-device@2 {
compatible = "test,rust-device";
reg = <0x2>;
+
+ test,u32-prop = <0xdeadbeef>;
+ test,i16-array = /bits/ 16 <1 2 (-3) (-4)>;
};
};
};
diff --git a/samples/rust/rust_driver_platform.rs b/samples/rust/rust_driver_platform.rs
index 5cf4a8f86c13..95c290806862 100644
--- a/samples/rust/rust_driver_platform.rs
+++ b/samples/rust/rust_driver_platform.rs
@@ -41,6 +41,28 @@ fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin
}
};

+ let dev = pdev.as_ref();
+ if let Ok(idx) = dev.property_match_string(c_str!("compatible"), c_str!("test,rust-device"))
+ {
+ dev_info!(pdev.as_ref(), "matched compatible string idx = {}\n", idx);
+ }
+
+ let prop = dev.property_read_bool(c_str!("test,bool-prop"));
+ dev_info!(dev, "bool prop is {}\n", prop);
+
+ let _prop = dev.property_read::<u32>(c_str!("test,u32-prop"))?;
+ let prop: u32 = dev.property_read(c_str!("test,u32-prop"))?;
+ dev_info!(dev, "'test,u32-prop' is {:#x}\n", prop);
+
+ let prop: [i16; 4] = dev.property_read_array(c_str!("test,i16-array"))?;
+ dev_info!(dev, "'test,i16-array' is {:?}\n", prop);
+ dev_info!(
+ dev,
+ "'test,i16-array' length is {}\n",
+ dev.property_count_elem::<u16>(c_str!("test,i16-array"))
+ .unwrap()
+ );
+
let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?;

Ok(drvdata.into())

--
2.45.2