* [PATCH v13 0/3] Add RP1 PWM controller support
@ 2026-11-24 17:36 Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller Andrea della Porta
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Andrea della Porta @ 2026-11-24 17:36 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm, robh, krzk+dt, conor+dt,
devicetree, florian.fainelli, rjui, sbranden,
bcm-kernel-feedback-list, linux-rpi-kernel, linux-arm-kernel,
linux-kernel
Cc: naush, svarbanov, gary, linux, linux-hwmon, krzysztof.kozlowski,
Andrea della Porta
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/ae9a4df9-b1f2-4f00-ab86-d9cf8d9bc387@roeck-us.net/
[2] - https://lore.kernel.org/all/7c437a10-985b-402c-b3ac-cfa97c53d014@broadcom.com/
[3] - https://lore.kernel.org/all/20261102192302.GA882027-robh@kernel.org/
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
--
2.51.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
@ 2026-11-24 17:36 ` Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver Andrea della Porta
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Andrea della Porta @ 2026-11-24 17:36 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm, robh, krzk+dt, conor+dt,
devicetree, florian.fainelli, rjui, sbranden,
bcm-kernel-feedback-list, linux-rpi-kernel, linux-arm-kernel,
linux-kernel
Cc: naush, svarbanov, gary, linux, linux-hwmon, krzysztof.kozlowski,
Andrea della Porta
From: Naushir Patuck <naush@raspberrypi.com>
Add the devicetree binding documentation for the PWM
controller found in the Raspberry Pi RP1 chipset.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Co-developed-by: Stanimir Varbanov <svarbanov@suse.de>
Signed-off-by: Stanimir Varbanov <svarbanov@suse.de>
Signed-off-by: Andrea della Porta <andrea.porta@suse.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@oss.qualcomm.com>
---
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/20261102192302.GA882027-robh@kernel.org/
---
.../bindings/pwm/raspberrypi,rp1-pwm.yaml | 66 +++++++++++++++++++
1 file changed, 66 insertions(+)
create mode 100644 Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
diff --git a/Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml b/Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
new file mode 100644
index 000000000000..be020583fd0b
--- /dev/null
+++ b/Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
@@ -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 <naush@raspberrypi.com>
+ - Andrea della Porta <andrea.porta@suse.com>
+ - Stanimir Varbanov <svarbanov@suse.de>
+
+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>;
+ };
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller Andrea della Porta
@ 2026-11-24 17:36 ` Andrea della Porta
2026-11-24 17:46 ` sashiko-bot
2026-11-24 17:36 ` [PATCH v13 3/3] pwm: rp1: Register auxiliary device for the fan tachometer Andrea della Porta
2026-11-24 17:46 ` [PATCH v13 0/3] Add RP1 PWM controller support sashiko-bot
3 siblings, 1 reply; 6+ messages in thread
From: Andrea della Porta @ 2026-11-24 17:36 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm, robh, krzk+dt, conor+dt,
devicetree, florian.fainelli, rjui, sbranden,
bcm-kernel-feedback-list, linux-rpi-kernel, linux-arm-kernel,
linux-kernel
Cc: naush, svarbanov, gary, linux, linux-hwmon, krzysztof.kozlowski,
Andrea della Porta
From: Naushir Patuck <naush@raspberrypi.com>
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.
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Co-developed-by: Stanimir Varbanov <svarbanov@suse.de>
Signed-off-by: Stanimir Varbanov <svarbanov@suse.de>
Co-developed-by: Andrea della Porta <andrea.porta@suse.com>
Signed-off-by: Andrea della Porta <andrea.porta@suse.com>
---
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
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065..5c7b280a2bdb 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -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 <andrea.porta@suse.com>
+L: linux-pwm@vger.kernel.org
+S: Maintained
+F: Documentation/devicetree/bindings/pwm/raspberrypi,rp1-pwm.yaml
+F: drivers/pwm/pwm-rp1.c
+
RC-CORE / LIRC FRAMEWORK
M: Sean Young <sean@mess.org>
L: linux-media@vger.kernel.org
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 729776086879..523789e82355 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -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
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 5630a521a7cf..c07fd24f69f3 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -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
diff --git a/drivers/pwm/pwm-rp1.c b/drivers/pwm/pwm-rp1.c
new file mode 100644
index 000000000000..69ce0db4e9aa
--- /dev/null
+++ b/drivers/pwm/pwm-rp1.c
@@ -0,0 +1,428 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * pwm-rp1.c
+ *
+ * Raspberry Pi RP1 PWM.
+ *
+ * Copyright © 2026 Raspberry Pi Ltd.
+ *
+ * Author: Naushir Patuck (naush@raspberrypi.com)
+ *
+ * Based on the pwm-bcm2835 driver by:
+ * Bart Tanghe <bart.tanghe@thomasmore.be>
+ *
+ * Datasheet: https://pip-assets.raspberrypi.com/categories/892-raspberry-pi-5/documents/RP-008370-DS-1-rp1-peripherals.pdf?disposition=inline
+ *
+ * Limitations:
+ * - Channels can be enabled/disabled through a global update flag, while the
+ * period and duty per-channel registers are independently updatable, and
+ * they are latched on the end of (specific channel) period strobe.
+ * This means that period and duty changes might result in glitches if the
+ * period/duty is changed exactly during an end of period strobe.
+ * - Since the duty/period registers are freely updatable (do not depend on
+ * the global update flag), setting one of them close to the period end and
+ * the other right afterwards results in a mixed output for that cycle because
+ * the write ops are not atomic.
+ * - The global update flag prevents mis-sampling of multi-bit bus signals in
+ * the PWM clock domain. This ensures that all PWM channel settings update
+ * on the same PWM clock cycle. Channels start in sync only if they share the
+ * same period.
+ * - If both duty and period are set to 0, the output is a constant low signal
+ * if polarity is normal or a constant high signal if polarity is inversed.
+ * - When disabled the output is driven to 0 if polarity is normal, or to 1
+ * if polarity is inversed.
+ * - Disabling the PWM stops the output immediately, without waiting for current
+ * period to complete first.
+ * - Channels are phase-capable, but on RPi5, the firmware can use a channel
+ * phase register to report the RPM of the fan connected to that PWM
+ * channel. As a result, phase control will be ignored for now.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pwm.h>
+#include <linux/units.h>
+#include <linux/spinlock.h>
+
+#define RP1_PWM_GLB_CTRL 0x000
+#define RP1_PWM_GLB_CTRL_CHANNEL_ENABLE(chan) BIT(chan)
+#define RP1_PWM_GLB_CTRL_SET_UPDATE BIT(31)
+
+#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,
+ 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))
+
+#define RP1_PWM_RANGE(chan) (0x018 + ((chan) * 0x10))
+#define RP1_PWM_PHASE(chan) (0x01C + ((chan) * 0x10))
+#define RP1_PWM_DUTY(chan) (0x020 + ((chan) * 0x10))
+
+#define RP1_PWM_NUM_PWMS 4
+
+struct rp1_pwm {
+ void __iomem *base;
+ struct clk *clk;
+ unsigned long clk_rate;
+ bool clk_enabled;
+ spinlock_t lock; /* sync for RP1_PWM_GLB_CTRL RMW access */
+};
+
+struct rp1_pwm_waveform {
+ u32 period_ticks;
+ u32 duty_ticks;
+ bool enabled;
+ bool inverted_polarity;
+};
+
+static void rp1_pwm_apply_config(struct pwm_chip *chip)
+{
+ struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+ u32 value;
+
+ /* update the changed registers on the next strobe to avoid glitches */
+ value = readl(rp1->base + RP1_PWM_GLB_CTRL);
+ value |= RP1_PWM_GLB_CTRL_SET_UPDATE;
+ writel(value, rp1->base + RP1_PWM_GLB_CTRL);
+}
+
+static int rp1_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+ struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+ u32 value, polarity;
+
+ /* init channel to reset defaults, preserving the polarity bit */
+ value = readl(rp1->base + RP1_PWM_CHAN_CTRL(pwm->hwpwm));
+ polarity = FIELD_GET(RP1_PWM_CHAN_CTRL_POLARITY, value);
+ value = u32_replace_bits(RP1_PWM_CHAN_CTRL_DEFAULT, polarity, RP1_PWM_CHAN_CTRL_POLARITY);
+ writel(value, rp1->base + RP1_PWM_CHAN_CTRL(pwm->hwpwm));
+
+ return 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;
+
+ if (!wf->period_length_ns) {
+ *wfhw = (typeof(*wfhw)){
+ .enabled = false,
+ .inverted_polarity = false,
+ };
+ return 0;
+ }
+
+ period_ticks = mul_u64_u64_div_u64(wf->period_length_ns, clk_rate, NSEC_PER_SEC);
+
+ /*
+ * The period is limited to U32_MAX, and it will be decremented by one later
+ * to allow 100% duty cycle.
+ */
+ if (period_ticks > U32_MAX) {
+ period_ticks = U32_MAX;
+ } else if (period_ticks < 2) {
+ period_ticks = 2;
+ ret = 1;
+ }
+
+ duty_ticks = mul_u64_u64_div_u64(wf->duty_length_ns, clk_rate, NSEC_PER_SEC);
+ duty_ticks = min(duty_ticks, period_ticks);
+ offset_ticks = mul_u64_u64_div_u64(wf->duty_offset_ns, clk_rate, NSEC_PER_SEC);
+ if (offset_ticks >= period_ticks)
+ offset_ticks = period_ticks - 1;
+ if (duty_ticks && offset_ticks &&
+ duty_ticks + offset_ticks >= period_ticks) {
+ wfhw->duty_ticks = period_ticks - duty_ticks;
+ wfhw->inverted_polarity = true;
+ } else {
+ wfhw->duty_ticks = duty_ticks;
+ wfhw->inverted_polarity = false;
+ }
+ /* Account for the extra tick at the end of the period */
+ wfhw->period_ticks = period_ticks - 1;
+
+ wfhw->enabled = true;
+
+ return ret;
+}
+
+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 {
+ 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);
+ }
+
+ return 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;
+
+ /* set polarity */
+ value = readl(rp1->base + RP1_PWM_CHAN_CTRL(pwm->hwpwm));
+ if (!wfhw->inverted_polarity)
+ value &= ~RP1_PWM_CHAN_CTRL_POLARITY;
+ else
+ value |= RP1_PWM_CHAN_CTRL_POLARITY;
+ writel(value, rp1->base + RP1_PWM_CHAN_CTRL(pwm->hwpwm));
+
+ guard(spinlock_irqsave)(&rp1->lock);
+
+ /* early exit if disabled */
+ ctrl = readl(rp1->base + RP1_PWM_GLB_CTRL);
+ if (!wfhw->enabled) {
+ ctrl &= ~RP1_PWM_GLB_CTRL_CHANNEL_ENABLE(pwm->hwpwm);
+ /* just disable the channel and bail out early */
+ writel(ctrl, rp1->base + RP1_PWM_GLB_CTRL);
+ rp1_pwm_apply_config(chip);
+ return 0;
+ }
+
+ /* set period and duty cycle */
+ writel(wfhw->period_ticks, rp1->base + RP1_PWM_RANGE(pwm->hwpwm));
+ writel(wfhw->duty_ticks, rp1->base + RP1_PWM_DUTY(pwm->hwpwm));
+
+ /* enable the channel */
+ ctrl |= RP1_PWM_GLB_CTRL_CHANNEL_ENABLE(pwm->hwpwm);
+ writel(ctrl, rp1->base + RP1_PWM_GLB_CTRL);
+
+ rp1_pwm_apply_config(chip);
+
+ return 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;
+
+ *wfhw = (typeof(*wfhw)) {
+ .period_ticks = 0,
+ .duty_ticks = 0,
+ };
+
+ value = readl(rp1->base + RP1_PWM_GLB_CTRL);
+ wfhw->enabled = !!(value & RP1_PWM_GLB_CTRL_CHANNEL_ENABLE(pwm->hwpwm));
+
+ value = readl(rp1->base + RP1_PWM_CHAN_CTRL(pwm->hwpwm));
+ wfhw->inverted_polarity = !!(value & RP1_PWM_CHAN_CTRL_POLARITY);
+
+ if (wfhw->enabled) {
+ wfhw->period_ticks = readl(rp1->base + RP1_PWM_RANGE(pwm->hwpwm));
+ wfhw->duty_ticks = readl(rp1->base + RP1_PWM_DUTY(pwm->hwpwm));
+ }
+
+ return 0;
+}
+
+static const struct pwm_ops rp1_pwm_ops = {
+ .sizeof_wfhw = sizeof(struct rp1_pwm_waveform),
+ .request = rp1_pwm_request,
+ .round_waveform_tohw = rp1_pwm_round_waveform_tohw,
+ .round_waveform_fromhw = rp1_pwm_round_waveform_fromhw,
+ .read_waveform = rp1_pwm_read_waveform,
+ .write_waveform = rp1_pwm_write_waveform,
+};
+
+static int rp1_pwm_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ unsigned long clk_rate;
+ struct pwm_chip *chip;
+ struct rp1_pwm *rp1;
+ int ret;
+
+ chip = devm_pwmchip_alloc(dev, RP1_PWM_NUM_PWMS, sizeof(*rp1));
+ if (IS_ERR(chip))
+ return PTR_ERR(chip);
+
+ rp1 = pwmchip_get_drvdata(chip);
+ spin_lock_init(&rp1->lock);
+
+ rp1->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(rp1->base))
+ return PTR_ERR(rp1->base);
+
+ rp1->clk = devm_clk_get(dev, NULL);
+ if (IS_ERR(rp1->clk))
+ return dev_err_probe(dev, PTR_ERR(rp1->clk), "Clock not found\n");
+
+ ret = clk_prepare_enable(rp1->clk);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to enable clock\n");
+ rp1->clk_enabled = true;
+
+ ret = devm_clk_rate_exclusive_get(dev, rp1->clk);
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to get exclusive rate\n");
+ goto err_disable_clk;
+ }
+
+ clk_rate = clk_get_rate(rp1->clk);
+ if (!clk_rate) {
+ ret = dev_err_probe(dev, -EINVAL, "Failed to get clock rate\n");
+ goto err_disable_clk;
+ }
+ /*
+ * To prevent u64 overflow in period calculations:
+ * mul_u64_u64_div_u64(period_ns, clk_rate, NSEC_PER_SEC)
+ * If clk_rate > 1 GHz, the result can overflow.
+ */
+ if (clk_rate > HZ_PER_GHZ) {
+ ret = dev_err_probe(dev, -EINVAL, "Clock rate > 1 GHz is not supported\n");
+ goto err_disable_clk;
+ }
+ rp1->clk_rate = clk_rate;
+
+ chip->ops = &rp1_pwm_ops;
+ chip->atomic = true;
+
+ platform_set_drvdata(pdev, chip);
+
+ ret = pwmchip_add(chip);
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to register PWM chip\n");
+ goto err_disable_clk;
+ }
+
+ return 0;
+
+err_disable_clk:
+ clk_disable_unprepare(rp1->clk);
+
+ return ret;
+}
+
+static void rp1_pwm_remove(struct platform_device *pdev)
+{
+ struct pwm_chip *chip = platform_get_drvdata(pdev);
+ struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+
+ pwmchip_remove(chip);
+
+ if (rp1->clk_enabled) {
+ clk_disable_unprepare(rp1->clk);
+ rp1->clk_enabled = false;
+ }
+}
+
+/*
+ * The PWM core is never powered off, so it retains its
+ * register configuration across suspend/resume. This
+ * means there's no need to save and restore the
+ * channel control register.
+ */
+static int rp1_pwm_suspend(struct device *dev)
+{
+ struct pwm_chip *chip = dev_get_drvdata(dev);
+ struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+
+ if (rp1->clk_enabled) {
+ clk_disable_unprepare(rp1->clk);
+ rp1->clk_enabled = false;
+ }
+
+ return 0;
+}
+
+static int rp1_pwm_resume(struct device *dev)
+{
+ struct pwm_chip *chip = dev_get_drvdata(dev);
+ struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+ int ret;
+
+ ret = clk_prepare_enable(rp1->clk);
+ if (ret) {
+ dev_err(dev, "Failed to enable clock on resume: %pe\n", ERR_PTR(ret));
+ return ret;
+ }
+ rp1->clk_enabled = true;
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(rp1_pwm_pm_ops, rp1_pwm_suspend, rp1_pwm_resume);
+
+static const struct of_device_id rp1_pwm_of_match[] = {
+ { .compatible = "raspberrypi,rp1-pwm" },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, rp1_pwm_of_match);
+
+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),
+ },
+};
+module_platform_driver(rp1_pwm_driver);
+
+MODULE_DESCRIPTION("RP1 PWM driver");
+MODULE_AUTHOR("Naushir Patuck <naush@raspberrypi.com>");
+MODULE_AUTHOR("Andrea della Porta <andrea.porta@suse.com>");
+MODULE_LICENSE("GPL");
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v13 3/3] pwm: rp1: Register auxiliary device for the fan tachometer
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver Andrea della Porta
@ 2026-11-24 17:36 ` Andrea della Porta
2026-11-24 17:46 ` [PATCH v13 0/3] Add RP1 PWM controller support sashiko-bot
3 siblings, 0 replies; 6+ messages in thread
From: Andrea della Porta @ 2026-11-24 17:36 UTC (permalink / raw)
To: Uwe Kleine-König, linux-pwm, robh, krzk+dt, conor+dt,
devicetree, florian.fainelli, rjui, sbranden,
bcm-kernel-feedback-list, linux-rpi-kernel, linux-arm-kernel,
linux-kernel
Cc: naush, svarbanov, gary, linux, linux-hwmon, krzysztof.kozlowski,
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.
Signed-off-by: Andrea della Porta <andrea.porta@suse.com>
---
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
diff --git a/MAINTAINERS b/MAINTAINERS
index 5c7b280a2bdb..26f8520c02ee 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23382,6 +23382,7 @@ L: linux-pwm@vger.kernel.org
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 <sean@mess.org>
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 523789e82355..7b271a16a483 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -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
diff --git a/drivers/pwm/pwm-rp1.c b/drivers/pwm/pwm-rp1.c
index 69ce0db4e9aa..f7443b9f9fa1 100644
--- a/drivers/pwm/pwm-rp1.c
+++ b/drivers/pwm/pwm-rp1.c
@@ -38,6 +38,7 @@
* channel. As a result, phase control will be ignored for now.
*/
+#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/clk.h>
@@ -49,6 +50,7 @@
#include <linux/pwm.h>
#include <linux/units.h>
#include <linux/spinlock.h>
+#include <soc/bcm2835/raspberrypi-pwm-rp1.h>
#define RP1_PWM_GLB_CTRL 0x000
#define RP1_PWM_GLB_CTRL_CHANNEL_ENABLE(chan) BIT(chan)
@@ -79,12 +81,16 @@ enum rp1_pwm_ctrl_mode {
#define RP1_PWM_NUM_PWMS 4
+/* Channel whose phase register is used by the firmware to report the fan RPM */
+#define RP1_PWM_FAN_TACH_CHAN 2
+
struct rp1_pwm {
void __iomem *base;
struct clk *clk;
unsigned long clk_rate;
bool clk_enabled;
spinlock_t lock; /* sync for RP1_PWM_GLB_CTRL RMW access */
+ struct auxiliary_device *fan_adev;
};
struct rp1_pwm_waveform {
@@ -286,6 +292,32 @@ static const struct pwm_ops rp1_pwm_ops = {
.write_waveform = rp1_pwm_write_waveform,
};
+/**
+ * rp1_pwm_read_tachometer() - read the fan speed reported by the RP1 firmware
+ * @adev: the "fan" auxiliary device registered by this driver
+ * @rpm: where to store the fan speed, in RPM
+ *
+ * Meant to be called only by the driver bound to the auxiliary device
+ * created by rp1_pwm_probe(), which carries the controller private data as
+ * platform data. The auxiliary device is unregistered before the PWM
+ * controller resources are released, so that data is guaranteed to be
+ * valid for as long as the auxiliary driver is bound.
+ *
+ * Return: 0 on success, negative errno otherwise.
+ */
+int rp1_pwm_read_tachometer(struct auxiliary_device *adev, u32 *rpm)
+{
+ struct rp1_pwm *rp1 = dev_get_platdata(&adev->dev);
+
+ if (!rp1->clk_enabled)
+ return -EBUSY;
+
+ *rpm = readl(rp1->base + RP1_PWM_PHASE(RP1_PWM_FAN_TACH_CHAN));
+
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(rp1_pwm_read_tachometer, "RP1_PWM");
+
static int rp1_pwm_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
@@ -347,8 +379,23 @@ static int rp1_pwm_probe(struct platform_device *pdev)
goto err_disable_clk;
}
+ /*
+ * The fan tachometer is exposed to the hwmon subsystem through an
+ * auxiliary device. The chip id is unique, so it is used to avoid
+ * name clashes if more than one RP1 PWM controller is enabled.
+ */
+ rp1->fan_adev = auxiliary_device_create(dev, KBUILD_MODNAME, "fan",
+ rp1, chip->id);
+ if (!rp1->fan_adev) {
+ ret = dev_err_probe(dev, -ENODEV,
+ "Failed to register fan auxiliary device\n");
+ goto err_remove_chip;
+ }
+
return 0;
+err_remove_chip:
+ pwmchip_remove(chip);
err_disable_clk:
clk_disable_unprepare(rp1->clk);
@@ -360,6 +407,8 @@ static void rp1_pwm_remove(struct platform_device *pdev)
struct pwm_chip *chip = platform_get_drvdata(pdev);
struct rp1_pwm *rp1 = pwmchip_get_drvdata(chip);
+ /* Unbind the fan driver before tearing down the PWM controller */
+ auxiliary_device_destroy(rp1->fan_adev);
pwmchip_remove(chip);
if (rp1->clk_enabled) {
diff --git a/include/soc/bcm2835/raspberrypi-pwm-rp1.h b/include/soc/bcm2835/raspberrypi-pwm-rp1.h
new file mode 100644
index 000000000000..290a052480bc
--- /dev/null
+++ b/include/soc/bcm2835/raspberrypi-pwm-rp1.h
@@ -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__ */
--
2.51.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver
2026-11-24 17:36 ` [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver Andrea della Porta
@ 2026-11-24 17:46 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-24 17:46 UTC (permalink / raw)
To: Andrea della Porta, devicetree; +Cc: linux-hwmon, robh, conor+dt
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 <naush@raspberrypi.com>
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.
> diff --git a/drivers/pwm/pwm-rp1.c b/drivers/pwm/pwm-rp1.c
> new file mode 100644
> index 0000000000000..69ce0db4e9aa0
> --- /dev/null
> +++ b/drivers/pwm/pwm-rp1.c
[ ... ]
> +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 {
> + 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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124173658.31030-1-andrea.porta@suse.com?part=2
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v13 0/3] Add RP1 PWM controller support
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
` (2 preceding siblings ...)
2026-11-24 17:36 ` [PATCH v13 3/3] pwm: rp1: Register auxiliary device for the fan tachometer Andrea della Porta
@ 2026-11-24 17:46 ` sashiko-bot
3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-24 17:46 UTC (permalink / raw)
To: Andrea della Porta; +Cc: linux-hwmon
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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124173658.31030-1-andrea.porta@suse.com
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-24 17:46 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 17:36 [PATCH v13 0/3] Add RP1 PWM controller support Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 1/3] dt-bindings: pwm: Add Raspberry Pi RP1 PWM controller Andrea della Porta
2026-11-24 17:36 ` [PATCH v13 2/3] pwm: rp1: Add RP1 PWM controller driver Andrea della Porta
2026-11-24 17:46 ` sashiko-bot
2026-11-24 17:36 ` [PATCH v13 3/3] pwm: rp1: Register auxiliary device for the fan tachometer Andrea della Porta
2026-11-24 17:46 ` [PATCH v13 0/3] Add RP1 PWM controller support sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox