From e2ad4da918d37db8ad5281dd9c7d69dc1519dd64 Mon Sep 17 00:00:00 2001 From: Fabien Parent Date: Sat, 11 Nov 2023 10:31:18 -0800 Subject: [PATCH] samples: rust: add regulator consumer driver sample Add a basic sample for using the regulator consumer API. Signed-off-by: Fabien Parent --- samples/rust/Kconfig | 10 +++++++++ samples/rust/Makefile | 1 + samples/rust/rust_regulator_consumer.rs | 30 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 samples/rust/rust_regulator_consumer.rs diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index b0f74a81c8f9ad..9651cac737d0c4 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT If unsure, say N. +config SAMPLE_RUST_REGULATOR_CONSUMER + tristate "Regulator consumer device driver" + help + This option builds the Rust regulator consumer driver sample. + + To compile this as a module, choose M here: + the module will be called rust_regulator_consumer. + + If unsure, say N. + config SAMPLE_RUST_HOSTPROGS bool "Host programs" help diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 03086dabbea44f..c80cbc4c3e92a1 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -2,5 +2,6 @@ obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o +obj-$(CONFIG_SAMPLE_RUST_REGULATOR_CONSUMER) += rust_regulator_consumer.o subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/rust_regulator_consumer.rs b/samples/rust/rust_regulator_consumer.rs new file mode 100644 index 00000000000000..d7ab4c5f49901f --- /dev/null +++ b/samples/rust/rust_regulator_consumer.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust regulator consumer driver sample. + +use kernel::{c_str, device, module_platform_driver, of, platform, prelude::*, + regulator::consumer::Regulator}; + +module_platform_driver! { + type: Driver, + name: "rust_regulator_consumer", + license: "GPL", +} + +kernel::module_of_id_table!(MOD_TABLE, REGULATOR_CONSUMER_ID_TABLE); +kernel::define_of_id_table! {REGULATOR_CONSUMER_ID_TABLE, (), [ + (of::DeviceId::Compatible(b"rust,regulator-consumer"), None), +]} + +struct Driver; +impl platform::Driver for Driver { + kernel::driver_of_id_table!(REGULATOR_CONSUMER_ID_TABLE); + + fn probe(pdev: &mut platform::Device, _id_info: Option<&Self::IdInfo>) -> Result { + let dev = device::Device::from_dev(pdev); + let vbus = Regulator::get(&dev, c_str!("vbus"))?; + let _ = vbus.enable()?; + + Ok(()) + } +}