From nobody Fri Oct 3 13:32:05 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CE041EEA49; Fri, 29 Aug 2025 21:12:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756501944; cv=pass; b=edjvuFp521rldhFF7eYSuiLVzOe2n9P0Wr2GMj0fAyHJx3y6H3kGX1eCKfOF4urFv+eadyJZgEt5xS3bCamZqLWThPrwRQUn9yAlOai5IBdi0gENtCqb3Qy/PSthSC8FTQMVfcUyViDOajae7VdPJp7P3pkYZwkIII0o9zE57Ns= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756501944; c=relaxed/simple; bh=IIUWAVosf84Vc3aZQ2fQ8EZCXYVMr2x5PGHrWDVNg4U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XtILxRtvaKF8w2NriKfsbm6lYy964jJHkTnJvpnsPC1XpPwvm+JM45hdLfDMB899F/uRosdpOnvy0zC9yXb6R9UnqfbuKvNzBwiQdI0iPI+7K6cNn/Av6JbE9geHfV+BqEkDSCV1NxAbBV3NcwC4mqd4BMZX3i7o+sIPEVhK4io= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=KbXcc+5H; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="KbXcc+5H" ARC-Seal: i=1; a=rsa-sha256; t=1756501923; cv=none; d=zohomail.com; s=zohoarc; b=dRs2LrdHqTq7YKqPmkNWD8U0bLZ4bSEJ3nz43mizOz13BPGHIyABxvywwElbagmh64Ur+TcbTrl8v0J+q9a1PbIWK0+uMkIDwGBD7tSO7PVak4q9BkrVe0V+qfoG1r21rhQ0VMOVnt2X/NrQhN20V1HRAglailXrhxk5xpbGLTg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756501923; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=uUtezq+LZ7Ws3yiBh/zy4oG0wsEbWJ6pHMwpgE2qR/g=; b=GYopPzXAyJBdXWpu+9OUPG3rJJBASkSr7E7IcME0+7gAnpd7KM3P52qjaUJQvy41vEd7YFEnMEEHhruH/qaMWFL1vQ/Ryax/KRhBYpZIA74GJk/ON25buyvs1aPa0DCj787YdunlzBADOZwliOCQTaS6L/PvGqN1a/oFln7sBk4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1756501923; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=uUtezq+LZ7Ws3yiBh/zy4oG0wsEbWJ6pHMwpgE2qR/g=; b=KbXcc+5HBaNSaB8BEju41DoPz6twD18oBQxLh9t8kYyqYr5xGlzbIuShWsmfzLcU iOsjlQzDT09POXvz8epS50XzurTCYCrQFgSmGlEVQXjnAZvgxKVesqeZZwwkMmXXDGd rrOKZEzZJ76l3EbuQZVot68KKKESh0OY9/zbHcO0= Received: by mx.zohomail.com with SMTPS id 1756501920131349.5983155234038; Fri, 29 Aug 2025 14:12:00 -0700 (PDT) From: Daniel Almeida Date: Fri, 29 Aug 2025 18:11:31 -0300 Subject: [PATCH 1/2] rust: regulator: remove Regulator Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250829-regulator-remove-dynamic-v1-1-deb59205e8e9@collabora.com> References: <20250829-regulator-remove-dynamic-v1-0-deb59205e8e9@collabora.com> In-Reply-To: <20250829-regulator-remove-dynamic-v1-0-deb59205e8e9@collabora.com> To: Liam Girdwood , Mark Brown , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Daniel Almeida X-Mailer: b4 0.14.2 X-ZohoMailClient: External After some experimenting and further discussion, it is starting to look like Regulator might be a footgun. It turns out that one can get the same behavior by correctly using just Regulator and Regulator, so there is no need to directly expose the manual refcounting ability of Regulator to clients. Remove it while we do not have any other users. Suggested-by: Danilo Krummrich Signed-off-by: Daniel Almeida Reviewed-by: Alexandre Courbot Reviewed-by: Alice Ryhl Reviewed-by: Danilo Krummrich --- rust/kernel/regulator.rs | 88 +-------------------------------------------= ---- 1 file changed, 1 insertion(+), 87 deletions(-) diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs index 65f3a125348f2d821898188b4ac6a0b593f18bf2..60993373f4d911f4f0cbec2510f= 0c67efa24a51b 100644 --- a/rust/kernel/regulator.rs +++ b/rust/kernel/regulator.rs @@ -30,7 +30,6 @@ pub trait Sealed {} =20 impl Sealed for super::Enabled {} impl Sealed for super::Disabled {} - impl Sealed for super::Dynamic {} } =20 /// A trait representing the different states a [`Regulator`] can be in. @@ -50,13 +49,6 @@ pub trait RegulatorState: private::Sealed + 'static { /// own an `enable` reference count, but the regulator may still be on. pub struct Disabled; =20 -/// A state that models the C API. The [`Regulator`] can be either enabled= or -/// disabled, and the user is in control of the reference count. This is a= lso -/// the default state. -/// -/// Use [`Regulator::is_enabled`] to check the regulator's current state. -pub struct Dynamic; - impl RegulatorState for Enabled { const DISABLE_ON_DROP: bool =3D true; } @@ -65,14 +57,9 @@ impl RegulatorState for Disabled { const DISABLE_ON_DROP: bool =3D false; } =20 -impl RegulatorState for Dynamic { - const DISABLE_ON_DROP: bool =3D false; -} - /// A trait that abstracts the ability to check if a [`Regulator`] is enab= led. pub trait IsEnabled: RegulatorState {} impl IsEnabled for Disabled {} -impl IsEnabled for Dynamic {} =20 /// An error that can occur when trying to convert a [`Regulator`] between= states. pub struct Error { @@ -183,64 +170,13 @@ pub struct Error { /// } /// ``` /// -/// ## Using [`Regulator`] -/// -/// This example mimics the behavior of the C API, where the user is in -/// control of the enabled reference count. This is useful for drivers that -/// might call enable and disable to manage the `enable` reference count at -/// runtime, perhaps as a result of `open()` and `close()` calls or whatev= er -/// other driver-specific or subsystem-specific hooks. -/// -/// ``` -/// # use kernel::prelude::*; -/// # use kernel::c_str; -/// # use kernel::device::Device; -/// # use kernel::regulator::{Regulator, Dynamic}; -/// struct PrivateData { -/// regulator: Regulator, -/// } -/// -/// // A fictictious probe function that obtains a regulator and sets it u= p. -/// fn probe(dev: &Device) -> Result { -/// // Obtain a reference to a (fictitious) regulator. -/// let mut regulator =3D Regulator::::get(dev, c_str!("vcc")= )?; -/// -/// Ok(PrivateData { regulator }) -/// } -/// -/// // A fictictious function that indicates that the device is going to b= e used. -/// fn open(dev: &Device, data: &mut PrivateData) -> Result { -/// // Increase the `enabled` reference count. -/// data.regulator.enable()?; -/// -/// Ok(()) -/// } -/// -/// fn close(dev: &Device, data: &mut PrivateData) -> Result { -/// // Decrease the `enabled` reference count. -/// data.regulator.disable()?; -/// -/// Ok(()) -/// } -/// -/// fn remove(dev: &Device, data: PrivateData) -> Result { -/// // `PrivateData` is dropped here, which will drop the -/// // `Regulator` in turn. -/// // -/// // The reference that was obtained by `regulator_get()` will be -/// // released, but it is up to the user to make sure that the number= of calls -/// // to `enable()` and `disabled()` are balanced before this point. -/// Ok(()) -/// } -/// ``` -/// /// # Invariants /// /// - `inner` is a non-null wrapper over a pointer to a `struct /// regulator` obtained from [`regulator_get()`]. /// /// [`regulator_get()`]: https://docs.kernel.org/driver-api/regulator.html= #c.regulator_get -pub struct Regulator +pub struct Regulator where State: RegulatorState, { @@ -354,28 +290,6 @@ pub fn try_into_disabled(self) -> Result, Error> { } } =20 -impl Regulator { - /// Obtains a [`Regulator`] instance from the system. The current stat= e of - /// the regulator is unknown and it is up to the user to manage the en= abled - /// reference count. - /// - /// This closely mimics the behavior of the C API and can be used to - /// dynamically manage the enabled reference count at runtime. - pub fn get(dev: &Device, name: &CStr) -> Result { - Regulator::get_internal(dev, name) - } - - /// Increases the `enabled` reference count. - pub fn enable(&mut self) -> Result { - self.enable_internal() - } - - /// Decreases the `enabled` reference count. - pub fn disable(&mut self) -> Result { - self.disable_internal() - } -} - impl Regulator { /// Checks if the regulator is enabled. pub fn is_enabled(&self) -> bool { --=20 2.51.0 From nobody Fri Oct 3 13:32:05 2025 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91B4D346A0E; Fri, 29 Aug 2025 21:12:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756501942; cv=pass; b=RmdPpYviqBVW6P41sg4VJkS58u7Zocdj4XuefStIrUxGCJlUBpMoNhUZRCAYeC8Z64332Fh6nCH9BkP8IUzLOp/U6l0NpVdLteK5azPC8qCCgoITg9SVD8+Hsi6W5BT3h++sZnASyuRoRRx9OC5knjohc6/2txIptBlCrUUUCS8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756501942; c=relaxed/simple; bh=ItWpvm4bGwoKXd7BDzJgsgm7des1fGWWNf8eydo67QY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WXtdiVks86EGsvrbc1NT/ba0qr7AFHmSlYDtpzmdKie+9K2hgS9IjdVInF5wD4nItZ2c/eFBNm3ZKnj2icMhOrC+KRUN9FGe8Ut2CUu68X3AP7xoXbXYMupKl6T94jFGPlQyb8UKO+poRdEQpUCHF5I4++gVbXlBK59TJvvPe4Y= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b=W0y44hMY; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=daniel.almeida@collabora.com header.b="W0y44hMY" ARC-Seal: i=1; a=rsa-sha256; t=1756501927; cv=none; d=zohomail.com; s=zohoarc; b=Tj1+pNltDF4rLBYKGK8AFrnv4JZ8QzQkmNYJMi1apllwDTH92J//jH+HDcUX09nMRih4G1E4bwmxB8BNh30AQMtNwwGyRcmEeQk6sDhaeYify1ActtcOikI45iN2oGbGeoH2ry1fuaZm+hQrsJvY9fW2xuNQaKqV9q1SdK3/CrI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756501927; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=OAdnPymjRmAyyFEKwSL0amv/o6XCe9TnJ/NMzqx/8EA=; b=HNc3M8GRI2xD/WSFaFuMFn8Aypaqm0x2h8KsWF7pv9UTX1XS25ekD4pqN3JPbbXNKRWAKCPjVr+JpNhFyoufVudPTk7DACmlz5YxK2c6H59hfzia6xSROlxWCcMnhlq9rSMYZ1mJQd6Fz9wRJAihS1YaPvnz7HlQv3KGkwl+8Ug= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1756501927; s=zohomail; d=collabora.com; i=daniel.almeida@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=OAdnPymjRmAyyFEKwSL0amv/o6XCe9TnJ/NMzqx/8EA=; b=W0y44hMYI/+7S6fcdzVzVdLLspe2BJblDBiaB8tlYhVAvylcV3c92sIA/WDMSp40 /5rrHTzCedZEfbvwwNOhXg0wjbsI9WfhAusXyRC7bC5GhKeEZz9COtUf33+75O/ooD3 GBXlKoEjoTgGLOFXKXrop2SlKyxErUSrf5NDNhpM= Received: by mx.zohomail.com with SMTPS id 175650192416034.151306544894396; Fri, 29 Aug 2025 14:12:04 -0700 (PDT) From: Daniel Almeida Date: Fri, 29 Aug 2025 18:11:32 -0300 Subject: [PATCH 2/2] rust: regulator: add devm_regulator_get_enable API Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250829-regulator-remove-dynamic-v1-2-deb59205e8e9@collabora.com> References: <20250829-regulator-remove-dynamic-v1-0-deb59205e8e9@collabora.com> In-Reply-To: <20250829-regulator-remove-dynamic-v1-0-deb59205e8e9@collabora.com> To: Liam Girdwood , Mark Brown , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Daniel Almeida X-Mailer: b4 0.14.2 X-ZohoMailClient: External A lot of drivers only care about enabling the regulator for as long as the underlying Device is bound. This can be easily observed due to the extensive use of `devm_regulator_get_enable` and `devm_regulator_get_enable_optional` throughout the kernel. Therefore, make this helper available in Rust. Also add an example noting how it should be the default API unless the driver needs more fine-grained control over the regulator. Suggested-by: Danilo Krummrich Signed-off-by: Daniel Almeida --- rust/helpers/regulator.c | 10 ++++++++++ rust/kernel/regulator.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/rust/helpers/regulator.c b/rust/helpers/regulator.c index cd8b7ba648ee33dd14326c9242fb6c96ab8e32a7..11bc332443bd064f4b5afd350ff= c045badff9076 100644 --- a/rust/helpers/regulator.c +++ b/rust/helpers/regulator.c @@ -40,4 +40,14 @@ int rust_helper_regulator_is_enabled(struct regulator *r= egulator) return regulator_is_enabled(regulator); } =20 +int rust_helper_devm_regulator_get_enable(struct device *dev, const char *= id) +{ + return devm_regulator_get_enable(dev, id); +} + +int rust_helper_devm_regulator_get_enable_optional(struct device *dev, con= st char *id) +{ + return devm_regulator_get_enable_optional(dev, id); +} + #endif diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs index 60993373f4d911f4f0cbec2510f0c67efa24a51b..73d4c9b56dca9c676793d78e35e= 5758d18eef3e8 100644 --- a/rust/kernel/regulator.rs +++ b/rust/kernel/regulator.rs @@ -18,7 +18,7 @@ =20 use crate::{ bindings, - device::Device, + device::{Bound, Device}, error::{from_err_ptr, to_result, Result}, prelude::*, }; @@ -70,6 +70,26 @@ pub struct Error { pub regulator: Regulator, } =20 +/// Enables a regulator whose lifetime is tied to the lifetime of `dev`. +/// +/// This calls `regulator_disable()` and `regulator_put()` automatically on +/// driver detach. +/// +/// This API is identical to `devm_regulator_get_enable()`, and should be +/// preferred if the caller only cares about the regulator being on. +pub fn enable(dev: &Device, name: &CStr) -> Result { + // SAFETY: `dev` is a valid and bound device, while `name` is a valid C + // string. + to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), n= ame.as_ptr()) }) +} + +/// Same as [`enable`], but calls `devm_regulator_get_enable_optional` ins= tead. +pub fn enable_optional(dev: &Device, name: &CStr) -> Result { + // SAFETY: `dev` is a valid and bound device, while `name` is a valid C + // string. + to_result(unsafe { bindings::devm_regulator_get_enable_optional(dev.as= _raw(), name.as_ptr()) }) +} + /// A `struct regulator` abstraction. /// /// # Examples @@ -146,6 +166,26 @@ pub struct Error { /// } /// ``` /// +/// If a driver only cares about the regulator being on for as long it is = bound +/// to a device, then it should use [`regulator::get_enabled`] or +/// [`regulator::get_enabled_optional`]. This should be the default use-ca= se +/// unless they need more fine-grained control over the regulator's state. +/// +/// ``` +/// # use kernel::prelude::*; +/// # use kernel::c_str; +/// # use kernel::device::{Bound, Device}; +/// # use kernel::regulator; +/// fn enable(dev: &Device) -> Result { +/// // Obtain a reference to a (fictitious) regulator and enable it. T= his +/// // call only returns whether the operation succeeded. +/// regulator::enable(dev, c_str!("vcc"))?; +/// +/// // The regulator will be disabled and put when `dev` is unbound. +/// Ok(()) +/// } +/// ``` +/// /// ## Disabling a regulator /// /// ``` --=20 2.51.0