.../devicetree/bindings/pwm/thead,th1520-pwm.yaml | 48 +++ MAINTAINERS | 8 + arch/riscv/boot/dts/thead/th1520-lichee-pi-4a.dts | 67 ++++ arch/riscv/boot/dts/thead/th1520.dtsi | 18 + drivers/pwm/Kconfig | 14 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm_th1520.rs | 272 +++++++++++++++ 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 | 376 +++++++++++++++++++++ 12 files changed, 828 insertions(+)
This patch series introduces Rust support for the T-HEAD TH1520 PWM
controller and demonstrates its use for fan control on the Sipeed Lichee
Pi 4A board.
The primary goal of this patch series is to introduce a basic set of
Rust abstractions for the Linux PWM subsystem. As a first user and
practical demonstration of these abstractions, the series also provides
a functional PWM driver for the T-HEAD TH1520 SoC. This allows control
of its PWM channels and ultimately enables temperature controlled fan
support for the Lichee Pi 4A board. This work aims to explore the use of
Rust for PWM drivers and lay a foundation for potential future
Rust based PWM drivers.
The series is structured as follows:
Patch 1/6: Introduce basic PWM abstractions
This patch lays the groundwork by adding a Kconfig option for Rust PWM
abstractions, necessary C helper functions, and a new Rust module
(rust/kernel/pwm.rs). This module provides initial safe wrappers for
core PWM data structures (Chip, Device, State, Args, Polarity) and
functions (devm_chip_alloc, devm_chip_add), along with a basic PwmOps
trait focusing on the .apply callback needed by PWM chip providers.
Patch 2/6: Add PWM driver for TH1520 SoC
This introduces the Rust based PWM driver for the T-HEAD TH1520 SoC.
It implements the PwmOps trait using the abstractions from the first
patch and handles the specifics of the TH1520 hardware for configuring
period, duty cycle, and polarity. Resource management leverages devm
for the PWM chip and Rust DevRes for I/O memory, and RAII for clock
handling.
Patch 3/6: dt-bindings: Add PWM T-HEAD controller dt-binding
This patch adds the Device Tree binding documentation for the T-HEAD
TH1520 PWM controller.
Patch 4/6: riscv: dts: thead:: Add PWM controller node
This patch adds the actual Device Tree node for the TH1520 PWM controller.
Patch 5/6: riscv: dts: thead: Add PVT node
Add pvt node for thermal sensor.
Patch 6/6: riscv: dts: thead: Add PWM fan and thermal control
This final patch adds the Device Tree configuration for a PWM controlled
fan to the Sipeed Lichee Pi 4A board DTS file.
Testing:
Tested on the TH1520 SoC. The fan works correctly.
Points for Discussion:
The rust/kernel/pwm.rs abstraction layer is currently minimal,
focusing on the immediate needs of this driver. Feedback on its design,
scope, and potential for generalization would be highly appreciated.
General feedback on the Rust implementation, FFI wrapping patterns, and
adherence to kernel development practices is very welcome.
The patches are based on rust-next, with some dependencies which are not
merged yet - platform Io support [1] and clk abstractions [2].
Reference repository with all the patches together can be found on
github [3].
[1] - https://lore.kernel.org/rust-for-linux/20250509-topics-tyr-platform_iomem-v8-0-e9f1725a40da@collabora.com/
[2] - https://lore.kernel.org/rust-for-linux/0ec0250c1170a8a6efb2db7a6cb49ae974d7ce05.1747634382.git.viresh.kumar@linaro.org/
[3] - https://github.com/mwilczy/linux/commits/rust-next-pwm-working-fan-for-sending/
---
Michal Wilczynski (6):
rust: Add basic PWM abstractions
pwm: Add Rust driver for T-HEAD TH1520 SoC
dt-bindings: pwm: thead: Add T-HEAD TH1520 PWM controller
riscv: dts: thead: Add PWM controller node
riscv: dts: thead: Add PVT node
riscv: dts: thead: Add PWM fan and thermal control
.../devicetree/bindings/pwm/thead,th1520-pwm.yaml | 48 +++
MAINTAINERS | 8 +
arch/riscv/boot/dts/thead/th1520-lichee-pi-4a.dts | 67 ++++
arch/riscv/boot/dts/thead/th1520.dtsi | 18 +
drivers/pwm/Kconfig | 14 +
drivers/pwm/Makefile | 1 +
drivers/pwm/pwm_th1520.rs | 272 +++++++++++++++
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 | 376 +++++++++++++++++++++
12 files changed, 828 insertions(+)
---
base-commit: 9416c85e2767adcf72a21bce15f9c56ed085c5d4
change-id: 20250524-rust-next-pwm-working-fan-for-sending-552ad2d1b193
Best regards,
--
Michal Wilczynski <m.wilczynski@samsung.com>
On Sat, May 24, 2025 at 11:14:54PM +0200, Michal Wilczynski wrote: > This patch series introduces Rust support for the T-HEAD TH1520 PWM > controller and demonstrates its use for fan control on the Sipeed Lichee > Pi 4A board. > > The primary goal of this patch series is to introduce a basic set of > Rust abstractions for the Linux PWM subsystem. As a first user and > practical demonstration of these abstractions, the series also provides > a functional PWM driver for the T-HEAD TH1520 SoC. This allows control > of its PWM channels and ultimately enables temperature controlled fan > support for the Lichee Pi 4A board. This work aims to explore the use of > Rust for PWM drivers and lay a foundation for potential future > Rust based PWM drivers. > > The series is structured as follows: > > Patch 1/6: Introduce basic PWM abstractions > This patch lays the groundwork by adding a Kconfig option for Rust PWM > abstractions, necessary C helper functions, and a new Rust module > (rust/kernel/pwm.rs). This module provides initial safe wrappers for > core PWM data structures (Chip, Device, State, Args, Polarity) and > functions (devm_chip_alloc, devm_chip_add), along with a basic PwmOps > trait focusing on the .apply callback needed by PWM chip providers. > > Patch 2/6: Add PWM driver for TH1520 SoC > This introduces the Rust based PWM driver for the T-HEAD TH1520 SoC. > It implements the PwmOps trait using the abstractions from the first > patch and handles the specifics of the TH1520 hardware for configuring > period, duty cycle, and polarity. Resource management leverages devm > for the PWM chip and Rust DevRes for I/O memory, and RAII for clock > handling. > > Patch 3/6: dt-bindings: Add PWM T-HEAD controller dt-binding > This patch adds the Device Tree binding documentation for the T-HEAD > TH1520 PWM controller. > > Patch 4/6: riscv: dts: thead:: Add PWM controller node > This patch adds the actual Device Tree node for the TH1520 PWM controller. > > Patch 5/6: riscv: dts: thead: Add PVT node > Add pvt node for thermal sensor. > > Patch 6/6: riscv: dts: thead: Add PWM fan and thermal control > This final patch adds the Device Tree configuration for a PWM controlled > fan to the Sipeed Lichee Pi 4A board DTS file. > > Testing: > Tested on the TH1520 SoC. The fan works correctly. > > Points for Discussion: > The rust/kernel/pwm.rs abstraction layer is currently minimal, > focusing on the immediate needs of this driver. Feedback on its design, > scope, and potential for generalization would be highly appreciated. > General feedback on the Rust implementation, FFI wrapping patterns, and > adherence to kernel development practices is very welcome. > > The patches are based on rust-next, with some dependencies which are not > merged yet - platform Io support [1] and clk abstractions [2]. > > Reference repository with all the patches together can be found on > github [3]. > > [1] - https://lore.kernel.org/rust-for-linux/20250509-topics-tyr-platform_iomem-v8-0-e9f1725a40da@collabora.com/ > [2] - https://lore.kernel.org/rust-for-linux/0ec0250c1170a8a6efb2db7a6cb49ae974d7ce05.1747634382.git.viresh.kumar@linaro.org/ > [3] - https://github.com/mwilczy/linux/commits/rust-next-pwm-working-fan-for-sending/ Thanks for the patch series. It will be great to have PWM working upstream. I've not built Linux with Rust before, so I'm going through the quick start [1]. I've also never built Linux with LLVM before but clang seems like the best compiler to use for Rust. Are you using LLVM? Drew [1] https://docs.kernel.org/rust/quick-start.html
On 5/25/25 00:21, Drew Fustini wrote: > On Sat, May 24, 2025 at 11:14:54PM +0200, Michal Wilczynski wrote: >> This patch series introduces Rust support for the T-HEAD TH1520 PWM >> controller and demonstrates its use for fan control on the Sipeed Lichee >> Pi 4A board. >> >> The primary goal of this patch series is to introduce a basic set of >> Rust abstractions for the Linux PWM subsystem. As a first user and >> practical demonstration of these abstractions, the series also provides >> a functional PWM driver for the T-HEAD TH1520 SoC. This allows control >> of its PWM channels and ultimately enables temperature controlled fan >> support for the Lichee Pi 4A board. This work aims to explore the use of >> Rust for PWM drivers and lay a foundation for potential future >> Rust based PWM drivers. >> >> The series is structured as follows: >> >> Patch 1/6: Introduce basic PWM abstractions >> This patch lays the groundwork by adding a Kconfig option for Rust PWM >> abstractions, necessary C helper functions, and a new Rust module >> (rust/kernel/pwm.rs). This module provides initial safe wrappers for >> core PWM data structures (Chip, Device, State, Args, Polarity) and >> functions (devm_chip_alloc, devm_chip_add), along with a basic PwmOps >> trait focusing on the .apply callback needed by PWM chip providers. >> >> Patch 2/6: Add PWM driver for TH1520 SoC >> This introduces the Rust based PWM driver for the T-HEAD TH1520 SoC. >> It implements the PwmOps trait using the abstractions from the first >> patch and handles the specifics of the TH1520 hardware for configuring >> period, duty cycle, and polarity. Resource management leverages devm >> for the PWM chip and Rust DevRes for I/O memory, and RAII for clock >> handling. >> >> Patch 3/6: dt-bindings: Add PWM T-HEAD controller dt-binding >> This patch adds the Device Tree binding documentation for the T-HEAD >> TH1520 PWM controller. >> >> Patch 4/6: riscv: dts: thead:: Add PWM controller node >> This patch adds the actual Device Tree node for the TH1520 PWM controller. >> >> Patch 5/6: riscv: dts: thead: Add PVT node >> Add pvt node for thermal sensor. >> >> Patch 6/6: riscv: dts: thead: Add PWM fan and thermal control >> This final patch adds the Device Tree configuration for a PWM controlled >> fan to the Sipeed Lichee Pi 4A board DTS file. >> >> Testing: >> Tested on the TH1520 SoC. The fan works correctly. >> >> Points for Discussion: >> The rust/kernel/pwm.rs abstraction layer is currently minimal, >> focusing on the immediate needs of this driver. Feedback on its design, >> scope, and potential for generalization would be highly appreciated. >> General feedback on the Rust implementation, FFI wrapping patterns, and >> adherence to kernel development practices is very welcome. >> >> The patches are based on rust-next, with some dependencies which are not >> merged yet - platform Io support [1] and clk abstractions [2]. >> >> Reference repository with all the patches together can be found on >> github [3]. >> >> [1] - https://lore.kernel.org/rust-for-linux/20250509-topics-tyr-platform_iomem-v8-0-e9f1725a40da@collabora.com/ >> [2] - https://lore.kernel.org/rust-for-linux/0ec0250c1170a8a6efb2db7a6cb49ae974d7ce05.1747634382.git.viresh.kumar@linaro.org/ >> [3] - https://protect2.fireeye.com/v1/url?k=53ce9a1b-32458f21-53cf1154-74fe4860008a-0c44c7bcb0c6b2a5&q=1&e=b41cbed0-2556-4543-be6a-a1333ab74001&u=https%3A%2F%2Fgithub.com%2Fmwilczy%2Flinux%2Fcommits%2Frust-next-pwm-working-fan-for-sending%2F > > Thanks for the patch series. It will be great to have PWM working > upstream. > > I've not built Linux with Rust before, so I'm going through the quick > start [1]. I've also never built Linux with LLVM before but clang seems > like the best compiler to use for Rust. Are you using LLVM? Hi Drew, You're correct, Clang is the way to go for Rust in the kernel. I also followed the official quick start guide. To answer your question directly: yes, I'm using LLVM. This is the exact command I use for cross-compilation: make ARCH=riscv LLVM=1 CROSS_COMPILATION variable seems to be unnecessary for the LLVM toolchain. After the build, I load the kernel binary onto my Lichee Pi 4A board (running Debian Trixie) via TFTP, which is the same process I used with the GNU toolchain. > > Drew > > [1] https://docs.kernel.org/rust/quick-start.html > Best regards, -- Michal Wilczynski <m.wilczynski@samsung.com>
On Mon May 26, 2025 at 10:22 AM CEST, Michal Wilczynski wrote: > On 5/25/25 00:21, Drew Fustini wrote: >> Thanks for the patch series. It will be great to have PWM working >> upstream. >> >> I've not built Linux with Rust before, so I'm going through the quick >> start [1]. I've also never built Linux with LLVM before but clang seems >> like the best compiler to use for Rust. Are you using LLVM? > > Hi Drew, > You're correct, Clang is the way to go for Rust in the kernel. I also > followed the official quick start guide. To answer your question > directly: yes, I'm using LLVM. Just to let you know, there is an effort to get rustc to work with a gcc backend rustc_gcc_codegen [1]. And there also is the gccrs project [2] trying to create a gnu Rust compiler. [1]: https://rust-for-linux.com/rustc_codegen_gcc [2]: https://rust-for-linux.com/gccrs They have made a lot of progress over the last year, so we're hopeful that they become usable in the near future. But for the moment, Clang/LLVM is the way to go. Hope this helps! --- Cheers, Benno
On Mon, May 26, 2025 at 11:01:58AM +0200, Benno Lossin wrote: > On Mon May 26, 2025 at 10:22 AM CEST, Michal Wilczynski wrote: > > On 5/25/25 00:21, Drew Fustini wrote: > >> Thanks for the patch series. It will be great to have PWM working > >> upstream. > >> > >> I've not built Linux with Rust before, so I'm going through the quick > >> start [1]. I've also never built Linux with LLVM before but clang seems > >> like the best compiler to use for Rust. Are you using LLVM? > > > > Hi Drew, > > You're correct, Clang is the way to go for Rust in the kernel. I also > > followed the official quick start guide. To answer your question > > directly: yes, I'm using LLVM. > > Just to let you know, there is an effort to get rustc to work with a gcc > backend rustc_gcc_codegen [1]. And there also is the gccrs project [2] > trying to create a gnu Rust compiler. > > [1]: https://rust-for-linux.com/rustc_codegen_gcc > [2]: https://rust-for-linux.com/gccrs > > They have made a lot of progress over the last year, so we're hopeful > that they become usable in the near future. But for the moment, > Clang/LLVM is the way to go. > > Hope this helps! > > --- > Cheers, > Benno Thanks for letting me know about gccrs. I was able to build linux okay with clang: make LLVM=1 ARCH=riscv -j16 It booted okay on the lpi4a: Linux version 6.15.0-next-20250606 (pdp7@thelio) (Ubuntu clang version 18.1.3 (1ubuntu1), Ubuntu LLD 18.1.3) I installed rust with: rustup default beta rustup component add rust-src $ make LLVM=1 rustavailable *** *** Rust bindings generator 'bindgen' versions 0.66.0 and 0.66.1 may not *** work due to a bug (https://github.com/rust-lang/rust-bindgen/pull/2567), *** unless patched (like Debian's). *** Your version: 0.66.1 *** *** *** Please see Documentation/rust/quick-start.rst for details *** on how to set up the Rust support. *** Rust is available! I'm not sure if that bindgen warning matters? Thanks, Drew
On Sun, Jun 8, 2025 at 6:58 PM Drew Fustini <drew@pdp7.com> wrote: > > I'm not sure if that bindgen warning matters? If you don't see the `FromBytesWithNulError` error, then it should be fine, but I would recommend using a newer version anyway. I hope that helps. Cheers, Miguel
On Sun, Jun 08, 2025 at 07:14:18PM +0200, Miguel Ojeda wrote: > On Sun, Jun 8, 2025 at 6:58 PM Drew Fustini <drew@pdp7.com> wrote: > > > > I'm not sure if that bindgen warning matters? > > If you don't see the `FromBytesWithNulError` error, then it should be > fine, but I would recommend using a newer version anyway. > > I hope that helps. > > Cheers, > Miguel Thanks for the quick response. I seemed to have updated it with: cargo install bindgen-cli And it seems Linux is now happy :) $ make LLVM=1 rustavailable Rust is available! -Drew
On Sun, Jun 8, 2025 at 9:58 PM Drew Fustini <drew@pdp7.com> wrote: > > Thanks for the quick response. I seemed to have updated it with: > > cargo install bindgen-cli > > And it seems Linux is now happy :) You're welcome! (By the way, I always recommend passing `--locked` to `cargo install` unless there is a good reason not to -- sadly it is not the default for that subcommand) Cheers, Miguel
© 2016 - 2025 Red Hat, Inc.