From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Rafael J. Wysocki" <rafael@kernel.org>,
"Viresh Kumar" <viresh.kumar@linaro.org>,
"Danilo Krummrich" <dakr@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Michal Wilczynski" <m.wilczynski@samsung.com>,
"Drew Fustini" <fustini@kernel.org>,
"Guo Ren" <guoren@kernel.org>, "Fu Wei" <wefu@redhat.com>,
"Uwe Kleine-König" <ukleinek@kernel.org>,
"Stephen Boyd" <sboyd@kernel.org>,
"Brian Masney" <bmasney+clk@redhat.com>,
"Jerome Brunet" <jbrunet+clk@baylibre.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org,
linux-pwm@vger.kernel.org, linux-clk@vger.kernel.org,
rust-for-linux@vger.kernel.org,
"Boris Brezillon" <boris.brezillon@collabora.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Maurice Hieronymus" <mhi@mailbox.org>
Subject: [PATCH v6 0/7] Clk improvements
Date: Tue, 13 Oct 2026 14:07:39 -0300 [thread overview]
Message-ID: <20261013-clk-type-state-v6-0-a5e9977b3509@collabora.com> (raw)
This series contains a few improvements that simplify clock handling for
drivers.
Patch 1 converts clk.rs to the kernel-vertical style for imports.
Patch 2 replaces OptionalClk with Clk::get_optional(). There is no struct
optional_clk in C, and Clk already allows a NULL pointer.
Patch 3 implements the same typestate pattern that has been used
successfully for Regulators. This is needed because otherwise drivers
will be responsible for unpreparing and disabling clocks themselves and
ultimately handling the reference counts on their own. This is
undesirable. The patch automatically encodes this information using the
type system so that no misuse can occur.
Patch 4 adds guards that prepare and/or enable a clock for the lifetime
of the guard without consuming the Clk, so that a driver can keep a
long-lived Clk<Prepared> and enable it for scoped sections.
Patch 5 makes things more convenient by offering devres-managed APIs. This
lets drivers set clock parameters once and forget about lifetime
management.
Patch 6 adds ExclusiveClk<T>, which holds rate exclusivity over a Clk<T>
in any state until it is dropped.
Patch 7 uses it in pwm-th1520 to lock the clock rate for the lifetime of
the driver data.
---
Changes in v6:
- Rebased onto the latest clk-next.
- Moved the import style conversion to the front (Alexandre, Onur).
- Kept Alexandre's and Onur's Reviewed-by on the imports patch, and
Alexandre's on the devres patch. Dropped Alexandre's Reviewed-by on the
type-state patch, which changed too much since v5.
- Split the OptionalClk removal into its own patch (Gary).
- Replaced "rust: clk: implement Clone for Clk<T>" with EnabledGuard,
PreparedGuard and PrepareEnabledGuard (Alexandre, Gary). Clones shared
a single struct clk, so per-consumer state such as the rate
exclusivity taken by clk_rate_exclusive_get() was shared between them.
- Dropped Clk::<Prepared>::with_enabled(), superseded by EnabledGuard.
- Named the ClkState constants ENABLED and PREPARED from the start
(Alexandre).
- Implemented Debug for Error<State> (Alexandre).
- Documented that dropping a prepared or enabled Clk may sleep
(Alexandre).
- Used a different connection id for the second clock in the example
(Alexandre).
- Used `?` in the per-state get() and get_optional() shortcuts.
- Dropped the trailing `//` from doctest imports (Alexandre).
- Removed the intra-doc links from SAFETY comments (Onur) and reworded
the devm_enable_optional() documentation (Alexandre).
- Expanded the devres commit message (Gary).
- Added ExclusiveClk and its pwm-th1520 user, based on Maurice
Hieronymus's series [1], with ExclusiveClk made generic over the clock
state, ExclusiveClk::new() in place of Clk::rate_exclusive_get(), and
a new ExclusiveClk::into_inner().
- Link to v5: https://patch.msgid.link/20260706-clk-type-state-v5-0-67c5f326a16c@collabora.com
[1] https://lore.kernel.org/rust-for-linux/20260711-rate-exclusive-get-th1520-v4-0-8244a0666a71@mailbox.org/
Changes in v5:
- Rebased onto the latest clk-next.
- New patch: "rust: clk: implement Clone for Clk<T>". Each clone is an
independent view of the same underlying clock, owning its own
prepare/enable counts; the raw pointer is shared through an Arc so
clk_get()/clk_put() stay balanced. Follows the v3 discussion with
Boris Brezillon and Gary Guo.
- Moved rate() and set_rate() from Clk<Enabled> to all states; the C API
does not restrict these to enabled clocks, and some clocks can only
change rate before being prepared.
- Made Clk::<Enabled>::disable() infallible, matching unprepare() and
the void C API (Onur).
- Fixed a typo in the "# Invariants" section, fixed SAFETY comments
that referred to the non-existent `self.0` field, and added missing
INVARIANT comments on the state transitions.
- Added the missing rustdoc link definitions for clk_unprepare and
clk_put (Maurice).
- Dropped the redundant `Result` import and the `use
kernel::error::Result;` lines from the doctests; the prelude already
provides it (Miguel).
- Added #[inline] to the devm_* helpers.
- Link to v4: https://patch.msgid.link/20260618-clk-type-state-v4-0-8be082786080@collabora.com
Changes in v4:
- Rebased onto clk-next. Alice Ryhl's "rust: clk: implement Send and Sync"
series is now merged upstream, so it is no longer carried as a dependency.
- Fixed the build with CONFIG_CPUFREQ_DT_RUST=y. The generic DT cpufreq
driver only has the (unbound) per-CPU device, so it cannot hand a
&Device<Bound> to Policy::set_clk(). Added a pub(crate) Clk::get_unbound()
for the few in-tree abstractions that operate on a device outside a bind
scope; set_clk() now takes &Device and uses it. Clk::get()/get_optional()
and the devm_* helpers still require &Device<Bound>.
- Added impl From<Error<State>> for kernel::error::Error, so the fallible
state transitions can be used with `?` (and chained) instead of
.map_err(|e| e.error).
- Added Clk::<Prepared>::with_enabled(), which runs a closure with the clock
temporarily enabled, scoping the Enabled state without giving up the
prepared clock.
- Documented how to change a clock's state at runtime via an enum, for
drivers that enable/disable across resume/suspend.
- Link to v3: https://lore.kernel.org/r/20260107-clk-type-state-v3-0-77d3e3ee59c2@collabora.com
Changes in v3:
- Rebased on top of 6.19-rc4
- Dropped patch 1 (from Alice), added her series as a dependency instead
- Fixed Tyr, PWM_TH1520 drivers
- Changed clk.rs imports to kernel-vertical style
- Added support get_optional shortcut for Prepared and Enabled (i.e.:
Clk::<Enabled>::get_optional())
- Fixed misplaced #[inline] tag
Thanks, Danilo {
- Moved the devres changes into its own patch
- Require &Device<Bound> for all functions where a &Device is used
- Account for con_in in SAFETY comments where applicable
- Added backticks
}
- Link to v2: https://lore.kernel.org/r/20250910-clk-type-state-v2-0-1b97c11bb631@collabora.com
Changes in v2:
- Added Alice's patch as patch 1, since it is a dependency.
- Added devm helpers (like we did for Regulator<T>)
- Fixed missing clk_put() call in Drop (Danilo)
- Fixed missing parenthesis and wrong docs (Viresh)
- Removed extra "dev" parameter from "shutdown" example (Danilo)
- Removed useless type annotation from example (Danilo)
- Link to v1: https://lore.kernel.org/rust-for-linux/20250729-clk-type-state-v1-1-896b53816f7b@collabora.com/#r
---
Daniel Almeida (6):
rust: clk: use 'kernel vertical style' for imports
rust: clk: replace `OptionalClk` with `Clk::get_optional()`
rust: clk: use the type-state pattern
rust: clk: add guards for scoped prepare and enable
rust: clk: add devres-managed clks
rust: clk: add `ExclusiveClk`
Maurice Hieronymus (1):
pwm: th1520: Lock clock rate with clk_rate_exclusive_get
drivers/cpufreq/rcpufreq_dt.rs | 7 +-
drivers/gpu/drm/tyr/driver.rs | 32 +-
drivers/pwm/pwm_th1520.rs | 25 +-
rust/kernel/clk.rs | 834 +++++++++++++++++++++++++++++++++++------
rust/kernel/cpufreq.rs | 8 +-
5 files changed, 748 insertions(+), 158 deletions(-)
---
base-commit: d3645b43ad60fe1ab3b4a869f6908414d5831a65
change-id: 20250909-clk-type-state-c01aa7dd551d
Best regards,
--
Daniel Almeida <daniel.almeida@collabora.com>
next reply other threads:[~2026-10-13 17:09 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 17:07 Daniel Almeida [this message]
2026-10-13 17:07 ` [PATCH v6 1/7] rust: clk: use 'kernel vertical style' for imports Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 2/7] rust: clk: replace `OptionalClk` with `Clk::get_optional()` Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 3/7] rust: clk: use the type-state pattern Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 4/7] rust: clk: add guards for scoped prepare and enable Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 5/7] rust: clk: add devres-managed clks Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 6/7] rust: clk: add `ExclusiveClk` Daniel Almeida
2026-10-13 17:14 ` Daniel Almeida
2026-10-13 17:07 ` [PATCH v6 7/7] pwm: th1520: Lock clock rate with clk_rate_exclusive_get Daniel Almeida
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261013-clk-type-state-v6-0-a5e9977b3509@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=bmasney+clk@redhat.com \
--cc=boqun@kernel.org \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=fustini@kernel.org \
--cc=gary@garyguo.net \
--cc=guoren@kernel.org \
--cc=jbrunet+clk@baylibre.com \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=lossin@kernel.org \
--cc=m.wilczynski@samsung.com \
--cc=mhi@mailbox.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=ukleinek@kernel.org \
--cc=viresh.kumar@linaro.org \
--cc=wefu@redhat.com \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®