Skip to contentSkip to conversation

Add RP1 PWM controller support

PATCH New Andrea della Porta 2 participants 6 messages Nov 24

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v13 cover Add RP1 PWM controller support

Hi,

This patchset adds support for the PWM controller found in the Raspberry Pi RP1 southbridge, used on the Raspberry Pi 5 to drive the cooling fan.

Compared to v12, the driver now also registers a "fan" auxiliary device (pwm_rp1.fan.<id>). On the Raspberry Pi 5 the RP1 firmware measures the fan tachometer pulses and writes the RPM value in the phase register of PWM channel 2; the companion hwmon driver, sent as a separate series to the hwmon list, binds to that auxiliary device and reads the value through rp1_pwm_read_tachometer(), exported in the 'RP1_PWM' symbol namespace. This is the approach agreed in [1], and it removes the need for any dedicated DT node or binding for the fan.

The DTS node for the controller is already in v7.4-rc1 [2]. The DT binding has been applied by Rob [3] for consistency to v7.4 and is part of v7.4-rc2: as a consequence, patch 1 is included only to make this series apply 'logically' on top of v7.4-rc1 and can be dropped when rebasing.

Changes in v13:
- dropped the DTS patch, already merged.
- add patch 3, registering the auxiliary device for the fan tachometer and exporting the accessor used by the hwmon auxiliary driver. The exported function was originally part of the fan patchset. Since it is really part of the pwm driver to be be taken by Uwe, I moved it in this patchset.
- Kconfig: pwm module now depends on AUXILIARY_BUS.
- added a macro to define the phase channel number from which to read the fan speed.
- removed the 'Tested-by' tag from Gary (see V12) since the patchset has been significally changed and need to be tested again.
- the controller private data is passed to the auxiliary device as platform data, so the exported accessor no longer depends on how the parent platform device stores its driver data.
- dropped the unused 'pwm' argument from rp1_pwm_apply_config().
- added a MAINTAINERS entry for the driver, its DT binding and header.

[1] - https://lore.kernel.org/all/[email protected]/
[2] - https://lore.kernel.org/all/[email protected]/
[3] - https://lore.kernel.org/all/[email protected]/

Thanks,
Andrea

This version has been prepared with the help of an AI coding assistant (Claude Opus 5.5).

Andrea della Porta (1):
pwm: rp1: Register auxiliary device for the fan tachometer

Naushir Patuck (2):
dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller
pwm: rp1: Add RP1 PWM controller driver

 .../bindings/pwm/raspberrypi,rp1-pwm.yaml     |  66 +++
 MAINTAINERS                                   |   8 +
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-rp1.c                         | 477 ++++++++++++++++++
 include/soc/bcm2835/raspberrypi-pwm-rp1.h     |  12 +
 6 files changed, 576 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

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.51.0
#1 lore raw

PATCH v13 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.

---
The DT binding has been applied by Rob (see [1]) for consistency tov7.4 and is part of v7.4-rc2: as a consequence, this patch is included only to make this series apply 'logically' on top of v7.4-rc1 and can be dropped when rebasing.

[1] - https://lore.kernel.org/all/[email protected]/

---
 .../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
new file mode 100644
index 000000000000..be020583fd0b
@@ -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.51.0
#2 lore raw

PATCH v13 2/3 pwm: rp1: Add RP1 PWM controller driver

In reply to Andrea della Porta

From: 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.

---
 MAINTAINERS           |   7 +
 drivers/pwm/Kconfig   |   7 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-rp1.c | 428 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 443 insertions(+)
 create mode 100644 drivers/pwm/pwm-rp1.c
MAINTAINERS +7 -0
index 93abbb315065..5c7b280a2bdb 100644
@@ -23376,6 +23376,13 @@ F:	drivers/clk/clk-rp1.c
 F:	drivers/misc/rp1/
 F:	drivers/pinctrl/pinctrl-rp1.c
 
+RASPBERRY PI RP1 PWM DRIVER
+M:	Andrea della Porta <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
+F:	drivers/pwm/pwm-rp1.c
+
 RC-CORE / LIRC FRAMEWORK
 M:	Sean Young <[email protected]>
 L:	[email protected]
drivers/pwm/Kconfig +7 -0
index 729776086879..523789e82355 100644
@@ -637,6 +637,13 @@ 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
+	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
index 5630a521a7cf..c07fd24f69f3 100644
@@ -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 +428 -0

428 lines not on this page · raw message

Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] pwm: rp1: u32 overflow and invalid offset in rp1_pwm_round_waveform_fromhw() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] pwm: rp1: u32 overflow and invalid offset in rp1_pwm_round_waveform_fromhw()
--

commit 44a8d1ea8978f906a50c0662a30ab9153df78f82
Author: Naushir Patuck <[email protected]>

pwm: rp1: Add RP1 PWM controller driver

This commit adds a PWM driver for the 4-channel PWM controller in the Raspberry Pi RP1 southbridge.

Quoted 5 lines
drivers/pwm/pwm-rp1.c new file +0 -0
new file mode 100644
index 0000000000000..69ce0db4e9aa0

[ ... ]

Quoted 23 lines
drivers/pwm/pwm-rp1.c new file +23 -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,
+						(u32)clk_rate);
+
+	if (!wfhw->inverted_polarity) {
+		ticks = min_t(u64, wfhw->duty_ticks, (u64)wfhw->period_ticks + 1);
+		wf->duty_length_ns = DIV_ROUND_UP_ULL(ticks * NSEC_PER_SEC,
+						      (u32)clk_rate);
+	} else {

drivers/pwm/pwm-rp1.c line 207

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, (u32)clk_rate);
+		wf->duty_offset_ns = DIV_ROUND_UP_ULL((u64)(wfhw->period_ticks + 1 - ticks) *
+						      NSEC_PER_SEC, (u32)clk_rate);
+	}

[Severity: Medium]
Can the wf->duty_offset_ns calculation overflow u32 or produce an invalid waveform when inverted_polarity is set?

In (u64)(wfhw->period_ticks + 1 - ticks), wfhw->period_ticks + 1 is evaluated in 32-bit unsigned arithmetic before subtracting the u64 ticks, so when wfhw->period_ticks is U32_MAX it wraps to 0 and underflows u64 for any ticks > 0.

Also, when wfhw->duty_ticks >= (u64)wfhw->period_ticks + 1, ticks is 0 (wf->duty_length_ns == 0) and wf->duty_offset_ns becomes equal to wf->period_length_ns, which pwm_wf_valid() rejects because a non-zero duty_offset_ns must be strictly less than period_length_ns.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH v13 3/3 pwm: rp1: Register auxiliary device for the fan tachometer

In reply to Andrea della Porta

On the Raspberry Pi 5, the firmware running on the RP1 Cortex-M core measures the fan tachometer pulses and periodically writes the computed RPM value into the phase register of PWM channel 2, inside the RP1 PWM controller register block.

The Linux PWM framework has no abstraction to report such a value, and the reading belongs to the hwmon subsystem. Following the auxiliary bus model, let the PWM driver instantiate a "fan" auxiliary device (pwm_rp1.fan.<id>) to which the hwmon driver can bind, without needing any additional DT node.

The way the tachometer value is obtained (the register is shared with the channel phase control) is an implementation detail of the PWM driver, so provide rp1_pwm_read_tachometer(), exported in the 'RP1_PWM' symbol namespace, for the auxiliary driver to use, as documented in Documentation/driver-api/auxiliary_bus.rst.

The auxiliary device is destroyed first in .remove(), so that the hwmon driver is unbound before the PWM controller resources are released.

---
 MAINTAINERS                               |  1 +
 drivers/pwm/Kconfig                       |  5 +++
 drivers/pwm/pwm-rp1.c                     | 49 +++++++++++++++++++++++
 include/soc/bcm2835/raspberrypi-pwm-rp1.h | 12 ++++++
 4 files changed, 67 insertions(+)
 create mode 100644 include/soc/bcm2835/raspberrypi-pwm-rp1.h
MAINTAINERS +1 -0
index 5c7b280a2bdb..26f8520c02ee 100644
@@ -23382,6 +23382,7 @@ L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
 F:	drivers/pwm/pwm-rp1.c
+F:	include/soc/bcm2835/raspberrypi-pwm-rp1.h
 
 RC-CORE / LIRC FRAMEWORK
 M:	Sean Young <[email protected]>
drivers/pwm/Kconfig +5 -0
index 523789e82355..7b271a16a483 100644
@@ -641,9 +641,14 @@ config PWM_RASPBERRYPI_RP1
 	tristate "RP1 PWM support"
 	depends on MISC_RP1 || COMPILE_TEST
 	depends on HAS_IOMEM
+	select AUXILIARY_BUS
 	help
 	  PWM framework driver for Raspberry Pi RP1 controller.
 
+	  The driver also registers an auxiliary device that allows the
+	  companion hwmon driver (SENSORS_RP1_FAN) to report the fan speed
+	  measured by the RP1 firmware.
+
 config PWM_SAMSUNG
 	tristate "Samsung PWM support"
 	depends on PLAT_SAMSUNG || ARCH_S5PV210 || ARCH_EXYNOS || COMPILE_TEST
drivers/pwm/pwm-rp1.c +49 -0

93 lines not on this page · raw message

include/soc/bcm2835/raspberrypi-pwm-rp1.h new file +12 -0
new file mode 100644
index 000000000000..290a052480bc
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __SOC_RASPBERRY_PWM_RP1_H__
+#define __SOC_RASPBERRY_PWM_RP1_H__
+
+#include <linux/types.h>
+
+struct auxiliary_device;
+
+int rp1_pwm_read_tachometer(struct auxiliary_device *adev, u32 *rpm);
+
+#endif /* __SOC_RASPBERRY_PWM_RP1_H__ */
Signature · 1 line
2.51.0
#5 lore raw
In reply to Andrea della Porta
Sashiko has finished reviewing this 3-patch series:

Sashiko has finished reviewing this 3-patch series:

- Patches 1 and 3: no issues found.
- Patch 2 ("pwm: rp1: Add RP1 PWM controller driver"): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]