[PATCH v5 0/4] Clk improvements
Daniel Almeida <[email protected]>
| Newsgroups | org.kernel.vger.linux-pwm,org.freedesktop.lists.dri-devel,org.infradead.lists.linux-riscv,org.kernel.vger.linux-clk,org.kernel.vger.linux-kernel,org.kernel.vger.linux-pm,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
This series contains a few improvements that simplifies clock handling for drivers. Patch 1 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 2 implements Clone for Clk<T>, so that a driver can hold a long-lived clock in one state while temporarily moving an independent clone of it through the other states. This is the outcome of the discussion in v3 with Boris Brezillon and Gary Guo. Patch 3 makes things more convenient by offering devres-managed APIs. This lets drivers set clock parameters once and forget about lifetime management. Patch 4 converts clk.rs to the newer kernel-vertical style in order to make future changes easier. The pre-existing error-path imbalance in the C function devm_clk_get_optional_enabled_with_rate() that was noted during the v4 review is a C-side issue and will be addressed by a separate patch. --- 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/[email protected] 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/[email protected] 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/[email protected] 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/[email protected]/#r To: "Rafael J. Wysocki" <[email protected]> To: Viresh Kumar <[email protected]> To: Danilo Krummrich <[email protected]> To: Alice Ryhl <[email protected]> To: Daniel Almeida <[email protected]> To: David Airlie <[email protected]> To: Simona Vetter <[email protected]> To: Michal Wilczynski <[email protected]> To: Drew Fustini <[email protected]> To: Guo Ren <[email protected]> To: Fu Wei <[email protected]> To: Uwe Kleine-König <[email protected]> To: Michael Turquette <[email protected]> To: Stephen Boyd <[email protected]> To: Miguel Ojeda <[email protected]> To: Boqun Feng <[email protected]> To: Gary Guo <[email protected]> To: Björn Roy Baron <[email protected]> To: Benno Lossin <[email protected]> To: Andreas Hindborg <[email protected]> To: Trevor Gross <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] --- Daniel Almeida (4): rust: clk: use the type-state pattern rust: clk: implement Clone for Clk<T> rust: clk: add devres-managed clks rust: clk: use 'kernel vertical style' for imports drivers/cpufreq/rcpufreq_dt.rs | 2 +- drivers/gpu/drm/tyr/driver.rs | 37 +-- drivers/pwm/pwm_th1520.rs | 17 +- rust/kernel/clk.rs | 722 +++++++++++++++++++++++++++++++++-------- rust/kernel/cpufreq.rs | 8 +- 5 files changed, 602 insertions(+), 184 deletions(-) --- base-commit: 92010229c4b38897f1319d260162d2f96925ed17 change-id: 20250909-clk-type-state-c01aa7dd551d Best regards, -- Daniel Almeida <[email protected]>