[PATCH RFC 2/6] pwm: Add Rust driver for T-HEAD TH1520 SoC
Danilo Krummrich
dakr at kernel.org
Sun May 25 05:03:29 PDT 2025
On Sat, May 24, 2025 at 11:14:56PM +0200, Michal Wilczynski wrote:
> diff --git a/drivers/pwm/pwm_th1520.rs b/drivers/pwm/pwm_th1520.rs
> new file mode 100644
> index 0000000000000000000000000000000000000000..4665e293e8d0bdc1a62a4e295cdaf4d47b3dd134
> --- /dev/null
> +++ b/drivers/pwm/pwm_th1520.rs
> @@ -0,0 +1,272 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// Copyright (c) 2025 Samsung Electronics Co., Ltd.
> +// Author: Michal Wilczynski <m.wilczynski at samsung.com>
> +
> +//! Rust T-HEAD TH1520 PWM driver
> +use kernel::{c_st
> +
> +struct Th1520PwmChipData {
> + clk: Clk,
> + iomem: kernel::devres::Devres<IoMem<0>>,
Why IoMem<0>? If you put the expected memory region size for this chip instead
all your subsequent accesses can be iomem.write() / iomem.read() rather than the
fallible try_{read,write}() variants.
> +impl Th1520PwmChipData {
> + fn _config(
> + &self,
> + hwpwm: u32,
> + duty_ns: u64,
> + period_ns: u64,
> + target_polarity: pwm::Polarity,
> + ) -> Result<u32> {
> + let regs = self.iomem.try_access().ok_or_else(|| {
> + pr_err!("PWM-{}: Failed to access I/O memory in _config\n", hwpwm);
Here and throughout the whole driver, please use the dev_*!() print macros.
Drivers have no reason to use the pr_*!() macros.
> +impl pwm::PwmOps for Th1520PwmChipData {
> + // This driver implements get_state
> + fn apply(
> + pwm_chip_ref: &mut pwm::Chip,
> + pwm_dev: &mut pwm::Device,
> + target_state: &pwm::State,
> + ) -> Result {
I assume those callbacks can't race with pwmchip_remove() called from driver
remove()? I.e. the callbacks are guaranteed to complete before pwmchip_remove()
completes?
If so, this function signature can provide the parent device of the pwm::Chip as
device::Device<device::Bound> reference.
This would allow you to access iomem more efficiently.
Instead of
data.iomem.try_access()
you could do
data.iomem.access(parent) // [1]
which does get you rid of the atomic check and the RCU read side critical
section implied by try_access().
Actually, I should have added this comment and explanation to the abstraction
patch, but forgot about it. :)
[1] https://gitlab.freedesktop.org/drm/kernel/-/blob/drm-next/rust/kernel/devres.rs?ref_type=heads#L213
More information about the linux-riscv
mailing list