* [PATCH v1 0/2] pwm: bcm2835: Convert to waveform
@ 2026-12-04 17:50 Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 1/2] pwm: bcm2835: Adapt register naming to BCM2835 manual Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 2/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2026-12-04 17:50 UTC (permalink / raw)
To: Florian Fainelli, Ray Jui, Scott Branden
Cc: Broadcom internal kernel review list, Sean Young, linux-pwm,
linux-rpi-kernel, linux-arm-kernel
Hello,
I have these patches sitting in my worktree for a while now. I didn't
retest all the corner cases on hardware since I worked on the changes,
but the behaviour was ok back then.
Maybe the waveform parts in the core will see more usage with this
popular platform converted to it.
The nice side-effect of this conversion is that libpwm now works on the
Raspberry Pi.
Testing and feedback very welcome.
Best regards
Uwe
Uwe Kleine-König (2):
pwm: bcm2835: Adapt register naming to BCM2835 manual
pwm: bcm2835: Convert to waveform
drivers/pwm/pwm-bcm2835.c | 242 +++++++++++++++++++++++++++++---------
1 file changed, 184 insertions(+), 58 deletions(-)
base-commit: d323c02b23e6fb5fa793da658ca11a50d9dfbee5
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v1 1/2] pwm: bcm2835: Adapt register naming to BCM2835 manual
2026-12-04 17:50 [PATCH v1 0/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
@ 2026-12-04 17:50 ` Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 2/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2026-12-04 17:50 UTC (permalink / raw)
To: Florian Fainelli, Ray Jui, Scott Branden
Cc: Broadcom internal kernel review list, Sean Young, linux-pwm,
linux-rpi-kernel, linux-arm-kernel
Use a common prefix "BCM2835_PWM_REG_" for the registers and the full
register name for the fields of the respective registers. Note that the
registers are numbered starting at 1, so there is some explicit +1 and
-1 involved that cancels out however.
Also add some description for the registers and a link to the manual.
There is no change in behaviour intended apart from a minor optimisation
where BCM2835_PWM_REG_CTL_PWEN(n) and BCM2835_PWM_REG_CTL_POLA(n) are
not cleared again after the whole channel's control byte was already
cleared a few lines above.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/pwm-bcm2835.c | 56 +++++++++++++++++++++++----------------
1 file changed, 33 insertions(+), 23 deletions(-)
diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index a4fbf9cfc6f7..2d1b7cf3e16b 100644
--- a/drivers/pwm/pwm-bcm2835.c
+++ b/drivers/pwm/pwm-bcm2835.c
@@ -1,8 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2014 Bart Tanghe <bart.tanghe@thomasmore.be>
+ *
+ * The hardware is (a bit sparsely) described in the document
+ * "BCM2835 ARM Peripherals". Currently it's available at
+ * https://pip.raspberrypi.com/documents/RP-008249-DS
*/
+#include <linux/bits.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/io.h>
@@ -11,17 +16,26 @@
#include <linux/platform_device.h>
#include <linux/pwm.h>
-#define PWM_CONTROL 0x000
-#define PWM_CONTROL_SHIFT(x) ((x) * 8)
-#define PWM_CONTROL_MASK 0xff
-#define PWM_MODE 0x80 /* set timer in PWM mode */
-#define PWM_ENABLE (1 << 0)
-#define PWM_POLARITY (1 << 4)
+/*
+ * CTL Register has 32 bits, each byte holds bits for one channel
+ */
+#define BCM2835_PWM_REG_CTL 0x000
+#define BCM2835_PWM_REG_CTL_MASK(n) GENMASK(((n) - 1) * 8 + 7, ((n) - 1) * 8)
+#define BCM2835_PWM_REG_CTL_PWEN(n) BIT(((n) - 1) * 8)
+#define BCM2835_PWM_REG_CTL_POLA(n) BIT(((n) - 1) * 8 + 4)
+#define BCM2835_PWM_REG_CTL_MSEN(n) BIT(((n) - 1) * 8 + 7)
-#define PERIOD(x) (((x) * 0x10) + 0x10)
-#define DUTY(x) (((x) * 0x10) + 0x14)
+/*
+ * RNG Register has 32 bits and holds the period length in clk ticks
+ */
+#define BCM2835_PWM_REG_RNG(n) (0x010 + 0x10 * ((n) - 1))
+#define BCM2835_PWM_REG_RNG_MIN 0x2
+
+/*
+ * DAT Register has 32 bits and holds the duty length in clk ticks
+ */
+#define BCM2835_PWM_REG_DAT(n) (0x014 + 0x10 * ((n) - 1))
-#define PERIOD_MIN 0x2
struct bcm2835_pwm {
void __iomem *base;
@@ -67,33 +81,29 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
/* don't accept a period that is too small */
- if (period_cycles < PERIOD_MIN)
+ if (period_cycles < BCM2835_PWM_REG_RNG_MIN)
return -EINVAL;
- writel(period_cycles, pc->base + PERIOD(pwm->hwpwm));
+ writel(period_cycles, pc->base + BCM2835_PWM_REG_RNG(pwm->hwpwm + 1));
/* set duty cycle */
val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
- writel(val, pc->base + DUTY(pwm->hwpwm));
+ writel(val, pc->base + BCM2835_PWM_REG_DAT(pwm->hwpwm + 1));
/* set polarity */
- val = readl(pc->base + PWM_CONTROL);
+ val = readl(pc->base + BCM2835_PWM_REG_CTL);
- val &= ~(PWM_CONTROL_MASK << PWM_CONTROL_SHIFT(pwm->hwpwm));
- val |= PWM_MODE << PWM_CONTROL_SHIFT(pwm->hwpwm);
+ val &= ~BCM2835_PWM_REG_CTL_MASK(pwm->hwpwm + 1);
+ val |= BCM2835_PWM_REG_CTL_MSEN(pwm->hwpwm + 1);
- if (state->polarity == PWM_POLARITY_NORMAL)
- val &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
- else
- val |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
+ if (state->polarity != PWM_POLARITY_NORMAL)
+ val |= BCM2835_PWM_REG_CTL_POLA(pwm->hwpwm + 1);
/* enable/disable */
if (state->enabled)
- val |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
- else
- val &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
+ val |= BCM2835_PWM_REG_CTL_PWEN(pwm->hwpwm + 1);
- writel(val, pc->base + PWM_CONTROL);
+ writel(val, pc->base + BCM2835_PWM_REG_CTL);
return 0;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v1 2/2] pwm: bcm2835: Convert to waveform
2026-12-04 17:50 [PATCH v1 0/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 1/2] pwm: bcm2835: Adapt register naming to BCM2835 manual Uwe Kleine-König
@ 2026-12-04 17:50 ` Uwe Kleine-König
1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2026-12-04 17:50 UTC (permalink / raw)
To: Florian Fainelli, Ray Jui, Scott Branden
Cc: Broadcom internal kernel review list, Sean Young, linux-pwm,
linux-rpi-kernel, linux-arm-kernel
Some notable points:
- On disable the polarity bit is cleared so the output is flat low.
This differs from before where with the legacy pwm_state a disabled
hardware kept the inversion bit with
.polarity = PWM_POLARITY_INVERSED. That is ok though as a consumer
that depends on the output level must not disable the PWM and a
disabled waveform representation doesn't have a polarity.
- With the calculation of .max_period_ns at probe time the conversion
in the tohw callback doesn't need to rely on rate ≤ NSEC_PER_SEC
which is the usual way other drivers ensure no overflow. The
calculation also works for higher rates (which is a theoretical
exercise though as it doesn't happen.) The remaining limitation to
rate ≤ U32_MAX could be easily lifted by using a division function
supporting a bigger divisor in the calculation of .max_period_ns.
- The rounding of period and duty_cycle changed from round-nearest to
round-down which matches the API contract for waveform callbacks.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
---
drivers/pwm/pwm-bcm2835.c | 206 +++++++++++++++++++++++++++++---------
1 file changed, 161 insertions(+), 45 deletions(-)
diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index 2d1b7cf3e16b..db92d9f0554d 100644
--- a/drivers/pwm/pwm-bcm2835.c
+++ b/drivers/pwm/pwm-bcm2835.c
@@ -41,75 +41,167 @@ struct bcm2835_pwm {
void __iomem *base;
struct clk *clk;
unsigned long rate;
+ u64 max_period_ns;
};
-static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
+static inline struct bcm2835_pwm *bcm2835_pwm_from_chip(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
-static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
- const struct pwm_state *state)
+struct bcm2835_pwm_waveform {
+ u32 ctl;
+ u32 rng;
+ u32 dat;
+};
+
+static int bcm2835_pwm_round_waveform_tohw(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const struct pwm_waveform *wf,
+ void *_wfhw)
{
+ struct bcm2835_pwm *pc = bcm2835_pwm_from_chip(chip);
+ struct bcm2835_pwm_waveform *wfhw = _wfhw;
+ int ret = 0;
+ u32 duty_ticks, offset_ticks;
- struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
- unsigned long long period_cycles;
- u64 max_period;
+ if (wf->period_length_ns == 0) {
+ *wfhw = (struct bcm2835_pwm_waveform){
+ .ctl = 0,
+ };
- u32 val;
+ return 0;
+ }
+
+ if (wf->period_length_ns >= pc->max_period_ns) {
+ wfhw->rng = U32_MAX;
+ } else {
+ /*
+ * pc->max_period_ns is chosen such that the calculated .rng is
+ * at most U32_MAX - 1 and so this doesn't overflow.
+ */
+ wfhw->rng = mul_u64_u64_div_u64(wf->period_length_ns, pc->rate, NSEC_PER_SEC);
+ if (wfhw->rng < BCM2835_PWM_REG_RNG_MIN) {
+ wfhw->rng = BCM2835_PWM_REG_RNG_MIN;
+ ret = 1;
+ }
+ }
+
+ if (wf->duty_length_ns >= pc->max_period_ns)
+ duty_ticks = U32_MAX;
+ else
+ duty_ticks = mul_u64_u64_div_u64(wf->duty_length_ns, pc->rate, NSEC_PER_SEC);
+
+ if (wf->duty_offset_ns >= pc->max_period_ns)
+ offset_ticks = U32_MAX;
+ else
+ offset_ticks = mul_u64_u64_div_u64(wf->duty_offset_ns, pc->rate, NSEC_PER_SEC);
+
+ wfhw->ctl = BCM2835_PWM_REG_CTL_PWEN(pwm->hwpwm + 1) |
+ BCM2835_PWM_REG_CTL_MSEN(pwm->hwpwm + 1);
/*
- * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC
- * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the
- * multiplication period * rate doesn't overflow.
- * To calculate the maximal possible period that guarantees the
- * above inequality:
- *
- * round(period * rate / NSEC_PER_SEC) <= U32_MAX
- * <=> period * rate / NSEC_PER_SEC < U32_MAX + 0.5
- * <=> period * rate < (U32_MAX + 0.5) * NSEC_PER_SEC
- * <=> period < ((U32_MAX + 0.5) * NSEC_PER_SEC) / rate
- * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate
- * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1
+ * With wf->duty_length_ns ≤ wf->period_length_ns we have
+ * duty_ticks ≤ wfhw->rng and so there is no underflow in the last
+ * comparison.
*/
- max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1;
+ if (duty_ticks && offset_ticks && offset_ticks >= wfhw->rng - duty_ticks) {
+ wfhw->ctl |= BCM2835_PWM_REG_CTL_POLA(pwm->hwpwm + 1);
+ wfhw->dat = wfhw->rng - duty_ticks;
+ } else {
+ wfhw->dat = duty_ticks;
+ }
- if (state->period > max_period)
- return -EINVAL;
+ dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> CTL: %08x, RNG: %08x, DAT: %08x\n",
+ pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, pc->rate,
+ wfhw->ctl, wfhw->rng, wfhw->dat);
- /* set period */
- period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
+ return ret;
+}
- /* don't accept a period that is too small */
- if (period_cycles < BCM2835_PWM_REG_RNG_MIN)
- return -EINVAL;
+static int bcm2835_pwm_round_waveform_fromhw(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const void *_wfhw,
+ struct pwm_waveform *wf)
+{
+ struct bcm2835_pwm *pc = bcm2835_pwm_from_chip(chip);
+ const struct bcm2835_pwm_waveform *wfhw = _wfhw;
- writel(period_cycles, pc->base + BCM2835_PWM_REG_RNG(pwm->hwpwm + 1));
+ if (wfhw->ctl & BCM2835_PWM_REG_CTL_PWEN(pwm->hwpwm + 1)) {
+ /* clamp DAT to RNG to prevent over- and underflows */
+ u32 dat = min(wfhw->dat, wfhw->rng);
+ u64 dat_ns;
- /* set duty cycle */
- val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
- writel(val, pc->base + BCM2835_PWM_REG_DAT(pwm->hwpwm + 1));
+ wf->period_length_ns = mul_u64_u64_div_u64_roundup(wfhw->rng, NSEC_PER_SEC,
+ pc->rate);
- /* set polarity */
- val = readl(pc->base + BCM2835_PWM_REG_CTL);
+ dat_ns = mul_u64_u64_div_u64_roundup(dat, NSEC_PER_SEC, pc->rate);
- val &= ~BCM2835_PWM_REG_CTL_MASK(pwm->hwpwm + 1);
- val |= BCM2835_PWM_REG_CTL_MSEN(pwm->hwpwm + 1);
+ if (wfhw->ctl & BCM2835_PWM_REG_CTL_POLA(pwm->hwpwm + 1)) {
+ wf->duty_length_ns = mul_u64_u64_div_u64_roundup(wfhw->rng - dat,
+ NSEC_PER_SEC, pc->rate);
+ wf->duty_offset_ns = dat_ns;
+ } else {
+ wf->duty_length_ns = dat_ns;
+ wf->duty_offset_ns = 0;
+ }
+ } else {
+ *wf = (struct pwm_waveform){
+ .period_length_ns = 0,
+ };
+ }
- if (state->polarity != PWM_POLARITY_NORMAL)
- val |= BCM2835_PWM_REG_CTL_POLA(pwm->hwpwm + 1);
+ dev_dbg(&chip->dev, "pwm#%u: CTL: %08x, RNG: %08x, DAT: %08x @%lu -> %lld/%lld [+%lld]\n",
+ pwm->hwpwm, wfhw->ctl, wfhw->rng, wfhw->dat, pc->rate,
+ wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns);
- /* enable/disable */
- if (state->enabled)
- val |= BCM2835_PWM_REG_CTL_PWEN(pwm->hwpwm + 1);
+ return 0;
+}
- writel(val, pc->base + BCM2835_PWM_REG_CTL);
+static int bcm2835_pwm_read_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ void *_wfhw)
+{
+ struct bcm2835_pwm *pc = bcm2835_pwm_from_chip(chip);
+ struct bcm2835_pwm_waveform *wfhw = _wfhw;
+
+ wfhw->rng = readl(pc->base + BCM2835_PWM_REG_RNG(pwm->hwpwm + 1));
+ wfhw->dat = readl(pc->base + BCM2835_PWM_REG_DAT(pwm->hwpwm + 1));
+ wfhw->ctl = readl(pc->base + BCM2835_PWM_REG_CTL);
+
+ return 0;
+}
+
+static int bcm2835_pwm_write_waveform(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ const void *_wfhw)
+{
+ struct bcm2835_pwm *pc = bcm2835_pwm_from_chip(chip);
+ const struct bcm2835_pwm_waveform *wfhw = _wfhw;
+ u32 ctl;
+
+ ctl = readl(pc->base + BCM2835_PWM_REG_CTL);
+ ctl &= ~BCM2835_PWM_REG_CTL_MASK(pwm->hwpwm + 1);
+ ctl |= wfhw->ctl & BCM2835_PWM_REG_CTL_MASK(pwm->hwpwm + 1);
+
+ if (!(ctl & BCM2835_PWM_REG_CTL_PWEN(pwm->hwpwm + 1))) {
+ writel(ctl, pc->base + BCM2835_PWM_REG_CTL);
+ return 0;
+ }
+
+ writel(wfhw->rng, pc->base + BCM2835_PWM_REG_RNG(pwm->hwpwm + 1));
+ writel(wfhw->dat, pc->base + BCM2835_PWM_REG_DAT(pwm->hwpwm + 1));
+ writel(ctl, pc->base + BCM2835_PWM_REG_CTL);
return 0;
}
static const struct pwm_ops bcm2835_pwm_ops = {
- .apply = bcm2835_pwm_apply,
+ .sizeof_wfhw = sizeof(struct bcm2835_pwm_waveform),
+ .round_waveform_tohw = bcm2835_pwm_round_waveform_tohw,
+ .round_waveform_fromhw = bcm2835_pwm_round_waveform_fromhw,
+ .read_waveform = bcm2835_pwm_read_waveform,
+ .write_waveform = bcm2835_pwm_write_waveform,
};
static int bcm2835_pwm_probe(struct platform_device *pdev)
@@ -122,7 +214,7 @@ static int bcm2835_pwm_probe(struct platform_device *pdev)
chip = devm_pwmchip_alloc(dev, 2, sizeof(*pc));
if (IS_ERR(chip))
return PTR_ERR(chip);
- pc = to_bcm2835_pwm(chip);
+ pc = bcm2835_pwm_from_chip(chip);
pc->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(pc->base))
@@ -139,9 +231,33 @@ static int bcm2835_pwm_probe(struct platform_device *pdev)
"fail to get exclusive rate\n");
pc->rate = clk_get_rate(pc->clk);
- if (!pc->rate)
+ /* the division below assumes the divisor fits into an u32 */
+ if (!pc->rate || pc->rate > U32_MAX)
return dev_err_probe(dev, -EINVAL,
- "failed to get clock rate\n");
+ "Invalid clock rate (%lu)\n", pc->rate);
+
+ /*
+ * The ticks value is calculated as:
+ *
+ * period_ns * rate
+ * ticks = ------------------
+ * NSEC_PER_SEC
+ *
+ * To properly limit the calculations in the tohw callback determine the
+ * smallest period_ns value that corresponds to at least U32_MAX ticks
+ * which is the maximal value RNG and DAT can hold:
+ *
+ * ⎣period_ns * rate / NSEC_PER_SEC⎦ ≥ U32_MAX
+ * ⟺ period_ns * rate / NSEC_PER_SEC ≥ U32_MAX
+ * ⟺ period_ns ≥ U32_MAX * NSEC_PER_SEC / rate
+ * ⟺ period_ns ≥ ⎡U32_MAX * NSEC_PER_SEC / rate⎤
+ *
+ * So for period_ns < ⎡U32_MAX * NSEC_PER_SEC / rate⎤ the calculation
+ * yields a value that fits into the 32 bit register (RNG or DAT) and
+ * otherwise U32_MAX is the correctly rounded result.
+ */
+ pc->max_period_ns = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC,
+ pc->rate);
chip->ops = &bcm2835_pwm_ops;
chip->atomic = true;
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 18:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 17:50 [PATCH v1 0/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 1/2] pwm: bcm2835: Adapt register naming to BCM2835 manual Uwe Kleine-König
2026-12-04 17:50 ` [PATCH v1 2/2] pwm: bcm2835: Convert to waveform Uwe Kleine-König
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox