Re: [PATCH v1 2/2] pwm: ocores: Add support for StarFive JHB100 PWM polarity
From: Changhuang Liang
Date: Mon Nov 30 2026 - 21:07:04 EST
Hi, Uwe
Thanks for the review.
> On Mon, Oct 19, 2026 at 04:08:55AM -0700, Changhuang Liang wrote:
> > The StarFive JHB100 PWM is based on the OpenCores PWM v1 controller,
> > but its polarity has been changed from inverted to normal. The generic
> > opencores,pwm-v1 hardware only supports inverted polarity, so the
> > driver currently hardcodes PWM_POLARITY_INVERSED and rejects any
> other
> > polarity, which makes the JHB100 unusable.
> >
> > Add a per-device OCPWM_FLAG_POLARITY_NORMAL flag to describe the
> > polarity capability of the hardware. Devices without this flag keep
> > the existing inverted-polarity behavior.
> >
> > Use the flag in ocores_pwm_get_state() to report the polarity actually
> > supported by the device, and in ocores_pwm_apply() to validate the
> > requested polarity accordingly.
> >
> > Finally, add the "starfive,jhb100-pwm" compatible with the normal
> > polarity flag set.
> >
> > Signed-off-by: Changhuang Liang <changhuang.liang@xxxxxxxxxxxxxxxx>
> > ---
> > drivers/pwm/pwm-ocores.c | 24 +++++++++++++++++++++---
> > 1 file changed, 21 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/pwm/pwm-ocores.c b/drivers/pwm/pwm-ocores.c index
> > 4c7e87cb009f..1c69b856bd80 100644
> > --- a/drivers/pwm/pwm-ocores.c
> > +++ b/drivers/pwm/pwm-ocores.c
> > @@ -18,6 +18,7 @@
> > #include <linux/module.h>
> > #include <linux/platform_device.h>
> > #include <linux/pm_runtime.h>
> > +#include <linux/property.h>
>
> The driver compiles fine without that addition (on arm64), why do you think
> you need it?
The patch adds a call to device_get_match_data(), which is declared in
<linux/property.h>. You're right that it also builds without the explicit
include, because <linux/pwm.h> pulls it in through <linux/of.h> and
<linux/gpio/driver.h>. I added it to not rely on these indirect includes,
following item 1 of Documentation/process/submit-checklist.rst.
So I'd prefer to keep it, but I can drop it if you like that better.
> > #include <linux/pwm.h>
> > #include <linux/reset.h>
> >
> > @@ -31,12 +32,15 @@
> >
> > #define OCPWM_NUM_SAVED_REGS 3
> >
> > +#define OCPWM_FLAG_POLARITY_NORMAL BIT(0)
> > +
> > struct ocores_pwm_device {
> > void __iomem *base;
> > struct clk *clk;
> > unsigned long clk_rate;
> > struct reset_control *rst;
> > u32 saved_regs[OCPWM_NUM_SAVED_REGS];
> > + unsigned long flags;
> > };
> >
> > static int ocores_pwm_get_state(struct pwm_chip *chip, @@ -60,7
> > +64,11 @@ static int ocores_pwm_get_state(struct pwm_chip *chip,
> > if (state->duty_cycle > state->period)
> > state->duty_cycle = state->period;
> >
> > - state->polarity = PWM_POLARITY_INVERSED;
> > + if (ddata->flags & OCPWM_FLAG_POLARITY_NORMAL)
> > + state->polarity = PWM_POLARITY_NORMAL;
> > + else
> > + state->polarity = PWM_POLARITY_INVERSED;
> > +
> > state->enabled = (ctrl_data & OCPWM_CTRL_EN) ? true : false;
> >
> > pm_runtime_put(pwmchip_parent(chip));
> > @@ -85,8 +93,13 @@ static int ocores_pwm_apply(struct pwm_chip *chip,
> > return 0;
> > }
> >
> > - if (state->polarity != PWM_POLARITY_INVERSED)
> > - return -EINVAL;
> > + if (ddata->flags & OCPWM_FLAG_POLARITY_NORMAL) {
> > + if (state->polarity != PWM_POLARITY_NORMAL)
> > + return -EINVAL;
> > + } else {
> > + if (state->polarity != PWM_POLARITY_INVERSED)
> > + return -EINVAL;
> > + }
>
> This could be simplified using something along the lines of:
>
> enum pwm_polarity polarity =
> ddata->flags & OCPWM_FLAG_POLARITY_NORMAL ?
> PWM_POLARITY_NORMAL : PWM_POLARITY_INVERSED;
>
> if (state->polarity != polarity)
> return -EINVAL;
>
> > period_data = mul_u64_u32_div(state->period, ddata->clk_rate,
> NSEC_PER_SEC);
> > if (period_data > U32_MAX)
> > @@ -218,6 +231,7 @@ static int ocores_pwm_probe(struct
> platform_device
> > *pdev)
> >
> > chip->ops = &ocores_pwm_ops;
> > ddata = pwmchip_get_drvdata(chip);
> > + ddata->flags = (unsigned long)device_get_match_data(dev);
> >
> > ddata->base = devm_platform_ioremap_resource(pdev, 0);
> > if (IS_ERR(ddata->base))
> > @@ -279,6 +293,10 @@ static int ocores_pwm_probe(struct
> > platform_device *pdev)
> >
> > static const struct of_device_id ocores_pwm_of_match[] = {
> > { .compatible = "opencores,pwm-v1" },
> > + {
> > + .compatible = "starfive,jhb100-pwm",
> > + .data = (void *)OCPWM_FLAG_POLARITY_NORMAL
> > + },
>
> Please also set .data = (void *)0; for opencores,pwm-v1 to make the reader
> not wonder if that entry was forgotten to be added.
>
Will do.
> > { /* sentinel */ }
> > };
> > MODULE_DEVICE_TABLE(of, ocores_pwm_of_match);
>
> Best regards
> Uwe
Best Regards,
Changhuang