Add RP1 PWM controller support
Subsystems (5)
Andrea della Porta
maintainerv8 cover Add RP1 PWM controller support
This patchset adds support for the PWM controller found on the Raspberry Pi RP1 southbridge. This is necessary to operate the cooling fan connected to one of the PWM channels.
The tachometer pin for the fan speed is managed by the firmware running on the RP1's M-core. It uses the PHASE2 register to report the RPM, which is then exported by this driver via a function. A subsequent patch will add a new device and driver to read the RPM and export this value via hwmon.
Subsequent patches will also add the CPU thermal zone, which acts as a consumer of the PWM device.
Best regards,
Andrea
CHANGES in V8:
- rebased on v7.3-rc1
- dropped the syscon. As a result, reintroduce the .remove
callback
- dropped the reference to syscon in the bindings description
- Kconfig: the driver no longer depends on MFD_SYSCON
- added a new header file to export the function prototype to
read the fan speed
- added the exported rp1_pwm_read_tachometer() function to
obtain the RPM
- dropped a now unused local variable in the probe function
- link to v7: https://lore.kernel.org/all/[email protected]/
Naushir Patuck (2):
dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller
pwm: rp1: Add RP1 PWM controller driver
Stanimir Varbanov (1):
arm64: dts: broadcom: rpi-5: Add RP1 PWM node
.../bindings/pwm/raspberrypi,rp1-pwm.yaml | 66 +++ .../boot/dts/broadcom/bcm2712-rpi-5-b.dts | 12 + arch/arm64/boot/dts/broadcom/rp1-common.dtsi | 9 + drivers/pwm/Kconfig | 8 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-rp1.c | 452 ++++++++++++++++++ include/soc/bcm2835/raspberrypi-pwm-rp1.h | 10 + 7 files changed, 558 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml create mode 100644 drivers/pwm/pwm-rp1.c create mode 100644 include/soc/bcm2835/raspberrypi-pwm-rp1.h
Signature · 1 line
2.35.3
Andrea della Porta
maintainerPATCH v8 1/3 dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller
From: Naushir Patuck <[email protected]>
Add the devicetree binding documentation for the PWM controller found in the Raspberry Pi RP1 chipset.
--- .../bindings/pwm/raspberrypi,rp1-pwm.yaml | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml new file +66 -0
@@ -0,0 +1,66 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pwm/raspberrypi,rp1-pwm.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Raspberry Pi RP1 PWM controller + +maintainers: + - Naushir Patuck <[email protected]> + - Andrea della Porta <[email protected]> + - Stanimir Varbanov <[email protected]> + +allOf: + - $ref: pwm.yaml# + +description: | + The PWM peripheral is a flexible waveform generator with a + variety of operational modes. It has the following features: + - four independent output channels + - 32-bit counter widths + - Seven output generation modes + - Optional per-channel output inversion + - Optional duty-cycle data FIFO with DMA support + - Optional sigma-delta noise shaping engine + Serves as a fan speed provider to other nodes for a PWM-connected + fan. + +properties: + compatible: + const: raspberrypi,rp1-pwm + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + + "#pwm-cells": + const: 3 + + dmas: + maxItems: 1 + description: | + Phandle to the DMA controller and the channel specifier for + the duty-cycle FIFO. + + dma-names: + items: + - const: tx + +required: + - compatible + - reg + - clocks + +unevaluatedProperties: false + +examples: + - | + pwm@98000 { + compatible = "raspberrypi,rp1-pwm"; + reg = <0x98000 0x100>; + clocks = <&rp1_clocks 17>; + #pwm-cells = <3>; + };
Signature · 1 line
2.35.3
Andrea della Porta
maintainerPATCH v8 2/3 pwm: rp1: Add RP1 PWM controller driver
In reply to Andrea della PortaFrom: Naushir Patuck <[email protected]>
The Raspberry Pi RP1 southbridge features an embedded PWM controller with 4 output channels, alongside an RPM interface to read the fan speed on the Raspberry Pi 5.
Add the supporting driver.
--- drivers/pwm/Kconfig | 8 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-rp1.c | 452 ++++++++++++++++++++++ include/soc/bcm2835/raspberrypi-pwm-rp1.h | 10 + 4 files changed, 471 insertions(+) create mode 100644 drivers/pwm/pwm-rp1.c create mode 100644 include/soc/bcm2835/raspberrypi-pwm-rp1.h
drivers/pwm/Kconfig +8 -0
@@ -637,6 +637,14 @@ config PWM_ROCKCHIP Generic PWM framework driver for the PWM controller found on Rockchip SoCs. +config PWM_RASPBERRYPI_RP1 + tristate "RP1 PWM support" + depends on MISC_RP1 || COMPILE_TEST + depends on HAS_IOMEM + select REGMAP_MMIO + help + PWM framework driver for Raspberry Pi RP1 controller. + config PWM_SAMSUNG tristate "Samsung PWM support" depends on PLAT_SAMSUNG || ARCH_S5PV210 || ARCH_EXYNOS || COMPILE_TEST
drivers/pwm/Makefile +1 -0
@@ -57,6 +57,7 @@ obj-$(CONFIG_PWM_RENESAS_RZG2L_GPT) += pwm-rzg2l-gpt.o obj-$(CONFIG_PWM_RENESAS_RZ_MTU3) += pwm-rz-mtu3.o obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o +obj-$(CONFIG_PWM_RASPBERRYPI_RP1) += pwm-rp1.o obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o obj-$(CONFIG_PWM_SIFIVE) += pwm-sifive.o obj-$(CONFIG_PWM_SL28CPLD) += pwm-sl28cpld.o
drivers/pwm/pwm-rp1.c new file +452 -0
452 lines not on this page · raw message
include/soc/bcm2835/raspberrypi-pwm-rp1.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __SOC_RASPBERRY_PWM_RP1_H__ +#define __SOC_RASPBERRY_PWM_RP1_H__ + +#include <linux/device.h> + +int rp1_pwm_read_tachometer (struct device *dev); + +#endif /* __SOC_RASPBERRY_PWM_RP1_H__ */
Signature · 1 line
2.35.3
[email protected]
AI reviewRe: [PATCH v8 2/3] pwm: rp1: Add RP1 PWM controller driver
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [High] Exported function rp1_pwm_read_tachometer accesses devm-allocated driver data and hardware…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [High] Exported function rp1_pwm_read_tachometer accesses devm-allocated driver data and hardware registers without synchronization or power management, risking Use-After-Free and bus faults.
- [High] Calling DIV_ROUND_UP_ULL with a 64-bit divisor causes compiler warnings and link errors on 32-bit platforms.
- [High] Returning a positive value (1) from round_waveform_tohw violates kernel error handling conventions and breaks sysfs writes.
- [Medium] The driver is configured as a tristate module but uses builtin_platform_driver(), preventing module unloading.
- [Medium] Failure to check regmap_read return values can lead to the use of uninitialized stack variables.
- [Medium] Implicit zero-initialization in struct assignment destroys previously read hardware state.
- [Medium] Channel is enabled before the global update flag is toggled, risking mis-sampling of period and duty registers.
- [Low] Missing #include <linux/bitfield.h> for the FIELD_PREP macro.
--
commit ea42a2b908766bfc4699d8f5c75d07f5d52b24b8
Author: Naushir Patuck <[email protected]>
pwm: rp1: Add RP1 PWM controller driver
The Raspberry Pi RP1 southbridge features an embedded PWM controller with 4 output channels. This patch adds the supporting driver framework.
Quoted 6 lines
drivers/pwm/pwm-rp1.c new file +0
-0@@ -0,0 +1,452 @@
[ ... ]
Quoted 8 lines
drivers/pwm/pwm-rp1.c new file +8
-0+#define RP1_PWM_CHAN_CTRL (chan) (0x014 + ((chan) * 0x10)) +#define RP1_PWM_CHAN_CTRL_POLARITY BIT(3) +#define RP1_PWM_CHAN_CTRL_FIFO_POP_MASK BIT(8) +#define RP1_PWM_CHAN_CTRL_MODE GENMASK(2, 0) +enum rp1_pwm_ctrl_mode { +RP1_PWM_CHAN_CTRL_MODE_ZERO , +RP1_PWM_CHAN_CTRL_MODE_TE_MS , +RP1_PWM_CHAN_CTRL_MODE_PC_MS ,
drivers/pwm/pwm-rp1.c line 74
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+RP1_PWM_CHAN_CTRL_MODE_PD_ENC , +RP1_PWM_CHAN_CTRL_MODE_MSB_SER , +RP1_PWM_CHAN_CTRL_MODE_PPM , +RP1_PWM_CHAN_CTRL_MODE_LE_MS , + RP1_PWM_CHAN_CTRL_MODE_LSB_SER, +}; + +#define RP1_PWM_CHAN_CTRL_DEFAULT (RP1_PWM_CHAN_CTRL_FIFO_POP_MASK + \ + FIELD_PREP(RP1_PWM_CHAN_CTRL_MODE, \ + RP1_PWM_CHAN_CTRL_MODE_TE_MS))
[Severity: Low]
Does this rely on an implicit inclusion for the FIELD_PREP macro?
It appears linux/bitfield.h is missing, which could lead to build failures if headers are refactored elsewhere in the kernel.
[ ... ]
drivers/pwm/pwm-rp1.c line 112
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+static void rp1_pwm_apply_config (struct pwm_chip *chip,struct pwm_device *pwm) +{ +struct rp1_pwm *rp1 =pwmchip_get_drvdata (chip); +u32 value; + +/* update the changed registers on the next strobe to avoid glitches */ +regmap_read (rp1->regmap,RP1_PWM_GLB_CTRL , &value); + value |=RP1_PWM_GLB_CTRL_SET_UPDATE ; +regmap_write (rp1->regmap,RP1_PWM_GLB_CTRL , value); +}
[Severity: Medium]
Can regmap_read() fail here and leave value uninitialized?
If the underlying MMIO read were to fail, the uninitialized stack variable would be modified and written directly back into the hardware register. This pattern also occurs in rp1_pwm_write_waveform(), rp1_pwm_read_waveform(), and rp1_pwm_read_tachometer().
[ ... ]
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+static int rp1_pwm_round_waveform_tohw (struct pwm_chip *chip, +struct pwm_device *pwm, +const struct pwm_waveform *wf, +void *_wfhw) +{ +struct rp1_pwm *rp1 =pwmchip_get_drvdata (chip); +u64 period_ticks, duty_ticks, offset_ticks; +struct rp1_pwm_waveform *wfhw = _wfhw; +u64 clk_rate = rp1->clk_rate; +int ret =0 ;
[ ... ]
drivers/pwm/pwm-rp1.c line 154
Quoted 6 lines
drivers/pwm/pwm-rp1.c new file +6
-0+if (period_ticks >U32_MAX ) { + period_ticks =U32_MAX ; + }else if (period_ticks <2 ) { + period_ticks =2 ; + ret =1 ; + }
[Severity: High]
Does returning a positive value (1) here violate kernel error handling conventions and break sysfs write semantics?
Returning a positive value instead of 0 or a negative error code might cause VFS write() implementations to treat the result as a partially completed write, forcing userspace into infinite retry loops when a small period is requested.
[ ... ]
Quoted 22 lines
drivers/pwm/pwm-rp1.c new file +22
-0+static int rp1_pwm_round_waveform_fromhw (struct pwm_chip *chip, +struct pwm_device *pwm, +const void *_wfhw, +struct pwm_waveform *wf) +{ +struct rp1_pwm *rp1 =pwmchip_get_drvdata (chip); +const struct rp1_pwm_waveform *wfhw = _wfhw; +u64 clk_rate = rp1->clk_rate; +u64 ticks; + + *wf = (struct pwm_waveform ){ }; + +if (!wfhw->enabled) +return 0 ; + + wf->period_length_ns =DIV_ROUND_UP_ULL (((u64 )wfhw->period_ticks +1 ) *NSEC_PER_SEC , + clk_rate); + +if (!wfhw->inverted_polarity) { + wf->duty_length_ns =DIV_ROUND_UP_ULL ((u64 )wfhw->duty_ticks *NSEC_PER_SEC , + (u32 )clk_rate); + }else {
drivers/pwm/pwm-rp1.c line 208
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+if (wfhw->duty_ticks > (u64 )wfhw->period_ticks +1 ) { +/* 100% duty cycle case */ + ticks =0 ; + }else { + ticks = (u64 )wfhw->period_ticks +1 - wfhw->duty_ticks; + } + wf->duty_length_ns =DIV_ROUND_UP_ULL (ticks *NSEC_PER_SEC , clk_rate); + wf->duty_offset_ns =DIV_ROUND_UP_ULL ((u64)(wfhw->period_ticks +1 - ticks) * +NSEC_PER_SEC , clk_rate); + }
[Severity: High]
Can this cause build failures on 32-bit architectures?
DIV_ROUND_UP_ULL delegates to do_div, which expects a 32-bit divisor. Passing the uncast 64-bit clk_rate directly for period_length_ns, duty_length_ns, and duty_offset_ns might trigger a __bad_size() link error when compiled on 32-bit platforms allowed by COMPILE_TEST.
[ ... ]
Quoted 7 lines
drivers/pwm/pwm-rp1.c new file +7
-0+static int rp1_pwm_write_waveform (struct pwm_chip *chip, +struct pwm_device *pwm, +const void *_wfhw) +{ +struct rp1_pwm *rp1 =pwmchip_get_drvdata (chip); +const struct rp1_pwm_waveform *wfhw = _wfhw; +u32 value, ctrl;
[ ... ]
drivers/pwm/pwm-rp1.c line 249
Quoted 11 lines
drivers/pwm/pwm-rp1.c new file +11
-0+/* set period and duty cycle */ +regmap_write (rp1->regmap, +RP1_PWM_RANGE (pwm->hwpwm), wfhw->period_ticks); +regmap_write (rp1->regmap, +RP1_PWM_DUTY (pwm->hwpwm), wfhw->duty_ticks); + +/* enable the channel */ + ctrl |=RP1_PWM_GLB_CTRL_CHANNEL_ENABLE (pwm->hwpwm); +regmap_write (rp1->regmap,RP1_PWM_GLB_CTRL , ctrl); + +rp1_pwm_apply_config (chip, pwm);
[Severity: Medium]
Does enabling the channel before toggling the global update flag expose the hardware to a mis-sampling window?
Asserting CHANNEL_ENABLE and writing it to hardware before SET_UPDATE is applied via rp1_pwm_apply_config() violates hardware synchronization rules, potentially causing the PWM clock domain to act on mis-sampled settings.
[ ... ]
Quoted 13 lines
drivers/pwm/pwm-rp1.c new file +13
-0+static int rp1_pwm_read_waveform (struct pwm_chip *chip, +struct pwm_device *pwm, +void *_wfhw) +{ +struct rp1_pwm *rp1 =pwmchip_get_drvdata (chip); +struct rp1_pwm_waveform *wfhw = _wfhw; +u32 value; + +regmap_read (rp1->regmap,RP1_PWM_GLB_CTRL , &value); + wfhw->enabled = !!(value &RP1_PWM_GLB_CTRL_CHANNEL_ENABLE (pwm->hwpwm)); + +regmap_read (rp1->regmap,RP1_PWM_CHAN_CTRL (pwm->hwpwm), &value); + wfhw->inverted_polarity = !!(value &RP1_PWM_CHAN_CTRL_POLARITY );
drivers/pwm/pwm-rp1.c line 276
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+ +if (wfhw->enabled) { +regmap_read (rp1->regmap,RP1_PWM_RANGE (pwm->hwpwm), &wfhw->period_ticks); +regmap_read (rp1->regmap,RP1_PWM_DUTY (pwm->hwpwm), &wfhw->duty_ticks); + }else { + *wfhw = (typeof(*wfhw)) { + .period_ticks = 0, + .duty_ticks = 0, + }; + }
[Severity: Medium]
Does this struct assignment implicitly clear the inverted_polarity and enabled fields?
These fields were populated by regmap_read() calls earlier in the function, and zeroing them out via implicit zero-initialization here seems to discard the synchronized hardware state when reading a disabled channel.
[ ... ]
Quoted 12 lines
drivers/pwm/pwm-rp1.c new file +12
-0+int rp1_pwm_read_tachometer (struct device *dev) +{ +struct pwm_chip *chip; +struct rp1_pwm *rp1; +u32 tach_val; + +if (!dev) +return -EINVAL ; + + chip =dev_get_drvdata (dev); +if (!chip) +return -ENODEV ;
drivers/pwm/pwm-rp1.c line 311
Quoted 10 lines
drivers/pwm/pwm-rp1.c new file +10
-0+ + rp1 =pwmchip_get_drvdata (chip); +if (!rp1) +return -ENODEV ; + +regmap_read (rp1->regmap,RP1_PWM_PHASE (2 ), &tach_val); + +return (int )tach_val; +} +EXPORT_SYMBOL_NS_GPL (rp1_pwm_read_tachometer,"RP1_PWM_FAN" );
[Severity: High]
Can calling this exported function concurrently with module unbind result in a use-after-free?
Retrieving driver data here without synchronization might allow a consumer driver to race with pwmchip_remove() and the subsequent freeing of the chip and rp1 structures.
Additionally, since the device clock is disabled during rp1_pwm_suspend(), would invoking this MMIO read during system suspend trigger a synchronous external abort?
[ ... ]
drivers/pwm/pwm-rp1.c line 447
Quoted 11 lines
drivers/pwm/pwm-rp1.c new file +11
-0+static struct platform_driver rp1_pwm_driver = { + .probe = rp1_pwm_probe, + .remove = rp1_pwm_remove, + .driver = { + .name ="rp1-pwm" , + .of_match_table = rp1_pwm_of_match, + .pm =pm_ptr (&rp1_pwm_pm_ops), + .suppress_bind_attrs = true, + }, +}; +builtin_platform_driver (rp1_pwm_driver);
[Severity: Medium]
Is there a reason builtin_platform_driver() is used instead of module_platform_driver() for a driver configured as a tristate module?
Using builtin_platform_driver() suppresses the generation of a module_exit function, which permanently blocks unloading the module despite the driver exporting an rp1_pwm_remove() function.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Andrea della Porta
maintainerPATCH v8 3/3 arm64: dts: broadcom: rpi-5: Add RP1 PWM node
In reply to Andrea della PortaFrom: Stanimir Varbanov <[email protected]>
The RP1 chipset used on the Raspberry Pi 5 features an integrated PWM controller to drive the cooling fan.
Add the corresponding DT node for this PWM controller.
--- arch/arm64/boot/dts/broadcom/bcm2712-rpi-5-b.dts | 12 ++++++++++++ arch/arm64/boot/dts/broadcom/rp1-common.dtsi | 9 +++++++++ 2 files changed, 21 insertions(+)
arch/arm64/boot/dts/broadcom/bcm2712-rpi-5-b.dts +12 -0
@@ -64,6 +64,12 @@ phy1: ethernet-phy@1 { }; &rp1_gpio { + fan_pwm_default_state: fan-pwm-default-state { + function = "pwm1"; + pins = "gpio45"; + bias-pull-down; + }; + usb_vbus_default_state: usb-vbus-default-state { function = "vbus1"; groups = "vbus1";
@@ -94,6 +100,12 @@ &rp1_i2c6 { pinctrl-names = "default"; }; +&rp1_pwm1 { + pinctrl-0 = <&fan_pwm_default_state>; + pinctrl-names = "default"; + status = "okay"; +}; + &rp1_usb0 { pinctrl-0 = <&usb_vbus_default_state>; pinctrl-names = "default";
arch/arm64/boot/dts/broadcom/rp1-common.dtsi +9 -0
@@ -99,7 +99,16 @@ rp1_i2c6: i2c@40088000 { clocks = <&rp1_clocks RP1_CLK_SYS>; i2c-scl-rising-time-ns = <65>; i2c-scl-falling-time-ns = <100>; + status = "disabled"; + }; + rp1_pwm1: pwm@4009c000 { + compatible = "raspberrypi,rp1-pwm"; + reg = <0x00 0x4009c000 0x0 0x100>; + clocks = <&rp1_clocks RP1_CLK_PWM1>; + assigned-clocks = <&rp1_clocks RP1_CLK_PWM1>; + assigned-clock-rates = <50000000>; + #pwm-cells = <3>; status = "disabled"; };
Signature · 1 line
2.35.3