Skip to content

Commit e2ad4da

Browse files
committed
samples: rust: add regulator consumer driver sample
Add a basic sample for using the regulator consumer API. Signed-off-by: Fabien Parent <[email protected]>
1 parent e0bb715 commit e2ad4da

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ config SAMPLE_RUST_PRINT
3030

3131
If unsure, say N.
3232

33+
config SAMPLE_RUST_REGULATOR_CONSUMER
34+
tristate "Regulator consumer device driver"
35+
help
36+
This option builds the Rust regulator consumer driver sample.
37+
38+
To compile this as a module, choose M here:
39+
the module will be called rust_regulator_consumer.
40+
41+
If unsure, say N.
42+
3343
config SAMPLE_RUST_HOSTPROGS
3444
bool "Host programs"
3545
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
44
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
5+
obj-$(CONFIG_SAMPLE_RUST_REGULATOR_CONSUMER) += rust_regulator_consumer.o
56

67
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust regulator consumer driver sample.
4+
5+
use kernel::{c_str, device, module_platform_driver, of, platform, prelude::*,
6+
regulator::consumer::Regulator};
7+
8+
module_platform_driver! {
9+
type: Driver,
10+
name: "rust_regulator_consumer",
11+
license: "GPL",
12+
}
13+
14+
kernel::module_of_id_table!(MOD_TABLE, REGULATOR_CONSUMER_ID_TABLE);
15+
kernel::define_of_id_table! {REGULATOR_CONSUMER_ID_TABLE, (), [
16+
(of::DeviceId::Compatible(b"rust,regulator-consumer"), None),
17+
]}
18+
19+
struct Driver;
20+
impl platform::Driver for Driver {
21+
kernel::driver_of_id_table!(REGULATOR_CONSUMER_ID_TABLE);
22+
23+
fn probe(pdev: &mut platform::Device, _id_info: Option<&Self::IdInfo>) -> Result {
24+
let dev = device::Device::from_dev(pdev);
25+
let vbus = Regulator::get(&dev, c_str!("vbus"))?;
26+
let _ = vbus.enable()?;
27+
28+
Ok(())
29+
}
30+
}

0 commit comments

Comments
 (0)