[PATCH v2 1/7] rust: Add basic PWM abstractions

Benno Lossin lossin at kernel.org
Thu Jun 12 02:12:18 PDT 2025


On Tue Jun 10, 2025 at 2:52 PM CEST, Michal Wilczynski wrote:
> Introduce safe Rust abstractions for the Linux PWM subsystem. These
> abstractions provide ergonomic, lifetime managed wrappers around the
> core C data structures and functions, enabling the development of PWM
> chip drivers in safe Rust.
>
> This initial version provides the core building blocks for writing a PWM
> chip provider driver, with a focus on safety, resource management, and
> idiomatic Rust patterns.
>
> The main components are:
>
> Ownership and Lifetime Management:
>  - The pwm::Chip type, an ARef managed wrapper for struct pwm_chip,
>    correctly handles the object's lifetime by using the embedded struct
>    device's reference counter.
>  - A pwm::Registration RAII guard ensures that a call to register a
>    chip (pwmchip_add) is always paired with a call to unregister it
>    (pwmchip_remove), preventing resource leaks.
>
> Safe Type Wrappers:
>  - Safe, idiomatic Rust types (Polarity, Waveform, State, Args,
>    Device) are provided to abstract away the raw C structs and enums.
>    The State wrapper holds its data by value, avoiding unnecessary
>    heap allocations.
>
> Driver Operations (PwmOps):
>  - A generic PwmOps trait allows drivers to implement the standard
>    PWM operations. It uses an associated type (WfHw) for the driver's
>    hardware specific waveform data, moving unsafe serialization logic into
>    the abstraction layer.
>    The trait exposes the modern waveform API (round_waveform_tohw,
>    write_waveform, etc.) as well as the other standard kernel callbacks
>    (get_state, request, apply).
>  - A create_pwm_ops function generates a C-compatible vtable from a
>    PwmOps implementor.
>
> This foundational layer is designed to be used by subsequent patches to
> implement specific PWM chip drivers in Rust.
>
> Signed-off-by: Michal Wilczynski <m.wilczynski at samsung.com>
> ---
>  MAINTAINERS                     |   6 +
>  drivers/pwm/Kconfig             |  13 +
>  rust/bindings/bindings_helper.h |   1 +
>  rust/helpers/helpers.c          |   1 +
>  rust/helpers/pwm.c              |  20 +
>  rust/kernel/lib.rs              |   2 +
>  rust/kernel/pwm.rs              | 864 ++++++++++++++++++++++++++++++++++++++++
>  7 files changed, 907 insertions(+)

Do you mind splitting this into smaller commits to make review easier?

---
Cheers,
Benno




More information about the linux-riscv mailing list