Skip to content

Commit

Permalink
samples: rust: add regulator consumer driver sample
Browse files Browse the repository at this point in the history
Add a basic sample for using the regulator consumer API.

Signed-off-by: Fabien Parent <[email protected]>
  • Loading branch information
Fabo committed Nov 27, 2023
1 parent e0bb715 commit e2ad4da
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/rust/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions samples/rust/rust_regulator_consumer.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}

0 comments on commit e2ad4da

Please sign in to comment.