Linux PWM subsystem development
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <ukleinek@kernel.org>
To: dongxuyang@eswincomputing.com
Cc: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org,
	 ben-linux@fluff.org, ben.dooks@codethink.co.uk,
	linux-pwm@vger.kernel.org,  devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, p.zabel@pengutronix.de,
	 daniel.lezcano@kernel.org, tglx@kernel.org,
	ningyu@eswincomputing.com,  linmin@eswincomputing.com,
	xuxiang@eswincomputing.com, wangguosheng@eswincomputing.com,
	 pinkesh.vaghela@einfochips.com
Subject: Re: [PATCH v14 3/5] pwm: dwc: add of/platform support
Date: Thu, 3 Dec 2026 18:31:24 +0100	[thread overview]
Message-ID: <axGhs14H2rqx5Xed@monoceros> (raw)
In-Reply-To: <20260730111553.1886-1-dongxuyang@eswincomputing.com>

[-- Attachment #1: Type: text/plain, Size: 13456 bytes --]

Hello,

On Thu, Jul 30, 2026 at 07:15:53PM +0800, dongxuyang@eswincomputing.com wrote:
> diff --git a/drivers/pwm/pwm-dwc-core.c b/drivers/pwm/pwm-dwc-core.c
> index 6dabec93a3c6..f612226618a5 100644
> --- a/drivers/pwm/pwm-dwc-core.c
> +++ b/drivers/pwm/pwm-dwc-core.c
> @@ -12,8 +12,10 @@
>  #define DEFAULT_SYMBOL_NAMESPACE "dwc_pwm"
>  
>  #include <linux/bitops.h>
> +#include <linux/clk.h>
>  #include <linux/export.h>
>  #include <linux/kernel.h>
> +#include <linux/math64.h>
>  #include <linux/module.h>
>  #include <linux/pci.h>
>  #include <linux/pm_runtime.h>
> @@ -39,26 +41,82 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
>  				     struct pwm_device *pwm,
>  				     const struct pwm_state *state)
>  {
> -	u64 tmp;
> +	u64 tmp, period_cyc;
>  	u32 ctrl;
>  	u32 high;
>  	u32 low;
>  
> -	/*
> -	 * Calculate width of low and high period in terms of input clock
> -	 * periods and check are the result within HW limits between 1 and
> -	 * 2^32 periods.
> -	 */
> -	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);
> -	if (tmp < 1 || tmp > (1ULL << 32))
> -		return -ERANGE;
> -	low = tmp - 1;
> -
> -	tmp = DIV_ROUND_CLOSEST_ULL(state->period - state->duty_cycle,
> -				    dwc->clk_ns);
> -	if (tmp < 1 || tmp > (1ULL << 32))
> -		return -ERANGE;
> -	high = tmp - 1;
> +	if (dwc->clk)
> +		dwc->clk_rate = clk_get_rate(dwc->clk);
> +
> +	if (dwc->features & DWC_TIM_CTRL_0N100PWM_EN) {
> +		/*
> +		 * Calculate the total period in clock cycles first, then the
> +		 * duty cycle. Derive the complementary half as the remainder to
> +		 * avoid compounding two independent floor-truncation errors:
> +		 * floor(duty) + floor(period - duty) can be one cycle short of
> +		 * floor(period). The PWM core requires the maximal achievable
> +		 * period not exceeding the requested value.
> +		 */
> +		period_cyc = mul_u64_u64_div_u64(state->period, dwc->clk_rate,
> +						 NSEC_PER_SEC);
> +		if (!period_cyc)
> +			return -ERANGE;
> +
> +		tmp = mul_u64_u64_div_u64(state->duty_cycle, dwc->clk_rate,
> +					  NSEC_PER_SEC);
> +		/*
> +		 * Calculate the width of the low and high periods in terms of
> +		 * input clock periods, and check whether the results are within
> +		 * the hardware limits of 0 to 2^32 periods.
> +		 */
> +		if (tmp >= (1ULL << 32) || period_cyc - tmp >= (1ULL << 32))
> +			return -ERANGE;
> +
> +		/*
> +		 * The hardware has no polarity register. Polarity inversion is
> +		 * achieved by swapping the low and high load-count registers:
> +		 * NORMAL (active-high): duty_cycle ->
> +		 *				HIGH period (DWC_TIM_LD_CNT2)
> +		 * INVERSED (active-low): duty_cycle ->
> +		 *				LOW period (DWC_TIM_LD_CNT)
> +		 */

This is cheating. Don't do that. The right way is to make the driver
provide the waveform callbacks instead of .apply() and .get_state().

> +		if (state->polarity == PWM_POLARITY_NORMAL) {
> +			high = tmp;
> +			low = period_cyc - tmp;
> +		} else {
> +			low = tmp;
> +			high = period_cyc - tmp;
> +		}
> +	} else {
> +		/*
> +		 * Calculate width of low and high period in terms of input
> +		 * clock periods and check are the result within HW limits
> +		 * between 1 and 2^32 periods.
> +		 * Polarity inversion uses the same register-swap technique as
> +		 * the 0N100 path above.
> +		 * Derive the complementary half from the total period to avoid
> +		 * compounding two independent floor-truncation errors.
> +		 */
> +		tmp = mul_u64_u64_div_u64(state->duty_cycle, dwc->clk_rate,
> +					  NSEC_PER_SEC);
> +		if (tmp < 1 || tmp > (1ULL << 32))
> +			return -ERANGE;
> +
> +		period_cyc = mul_u64_u64_div_u64(state->period, dwc->clk_rate,
> +						 NSEC_PER_SEC);
> +		/* period_cyc - tmp: complementary half; tmp <= period_cyc */
> +		if (period_cyc - tmp < 1 || period_cyc - tmp > (1ULL << 32))
> +			return -ERANGE;
> +
> +		if (state->polarity == PWM_POLARITY_NORMAL) {
> +			high = tmp - 1;
> +			low = period_cyc - tmp - 1;
> +		} else {
> +			low = tmp - 1;
> +			high = period_cyc - tmp - 1;
> +		}
> +	}
>  
>  	/*
>  	 * Specification says timer usage flow is to disable timer, then

This patch does too much and I think it should be split. The patch's
topic (according to Subject) is to add platform support, that shouldn't
need algorithmic changes to .apply().

> @@ -74,6 +132,7 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
>  	 * width of low period and latter the width of high period in terms
>  	 * multiple of input clock periods:
>  	 * Width = ((Count + 1) * input clock period).
> +	 * Width = (Count * input clock period) : supported 0% and 100%.
>  	 */
>  	dwc_pwm_writel(dwc, low, DWC_TIM_LD_CNT(pwm->hwpwm));
>  	dwc_pwm_writel(dwc, high, DWC_TIM_LD_CNT2(pwm->hwpwm));
> @@ -85,6 +144,14 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
>  	 * periods are set by Load Count registers.
>  	 */
>  	ctrl = DWC_TIM_CTRL_MODE_USER | DWC_TIM_CTRL_PWM;
> +	/*
> +	 * Mask interrupts to prevent unmasked timer interrupts on shared IRQ
> +	 * systems where no IRQ handler is installed.
> +	 */
> +	ctrl |= DWC_TIM_CTRL_INT_MASK;
> +	if (dwc->features & DWC_TIM_CTRL_0N100PWM_EN)
> +		ctrl |= DWC_TIM_CTRL_0N100PWM_EN;
> +
>  	dwc_pwm_writel(dwc, ctrl, DWC_TIM_CTRL(pwm->hwpwm));
>  
>  	/*
> @@ -99,14 +166,18 @@ static int dwc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
>  			 const struct pwm_state *state)
>  {
>  	struct dwc_pwm *dwc = to_dwc_pwm(chip);
> -
> -	if (state->polarity != PWM_POLARITY_INVERSED)
> -		return -EINVAL;
> +	int ret;
>  
>  	if (state->enabled) {
> -		if (!pwm->state.enabled)
> -			pm_runtime_get_sync(pwmchip_parent(chip));
> -		return __dwc_pwm_configure_timer(dwc, pwm, state);
> +		if (!pwm->state.enabled) {
> +			ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
> +			if (ret < 0)
> +				return ret;
> +		}
> +		ret = __dwc_pwm_configure_timer(dwc, pwm, state);
> +		if (ret && !pwm->state.enabled)
> +			pm_runtime_put_sync(pwmchip_parent(chip));
> +		return ret;
>  	} else {
>  		if (pwm->state.enabled) {
>  			__dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
> [...]
> diff --git a/drivers/pwm/pwm-dwc-of.c b/drivers/pwm/pwm-dwc-of.c
> new file mode 100644
> index 000000000000..e96e5569eb81
> --- /dev/null
> +++ b/drivers/pwm/pwm-dwc-of.c
> @@ -0,0 +1,298 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * DesignWare PWM Controller driver OF
> + *
> + * Copyright (C) 2026 SiFive, Inc.
> + */
> +
> +#define DEFAULT_SYMBOL_NAMESPACE "dwc_pwm_of"

What is that used for? There are no exports in this file.

> +#include <linux/clk.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/pwm.h>
> +#include <linux/reset.h>
> +
> +#include "pwm-dwc.h"
> +
> +static int dwc_pwm_plat_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct dwc_pwm_drvdata *data;
> +	u32 ctrl[DWC_TIMERS_TOTAL];
> +	struct pwm_chip *chip;
> +	struct dwc_pwm *dwc;
> +	bool pwm_en = false;
> +	u32 nr_pwm, tim_id;
> +	unsigned int i;
> +	int ret;
> +
> +	data = devm_kzalloc(dev, struct_size(data, chips, 1), GFP_KERNEL);
> +	if (!data)
> +		return -ENOMEM;
> +
> +	chip = dwc_pwm_alloc(dev);
> +	if (IS_ERR(chip))
> +		return dev_err_probe(dev, PTR_ERR(chip),
> +				     "failed to alloc pwm\n");

Error messages start with a capital letter please.

> +	dwc = to_dwc_pwm(chip);
> +
> +	dwc->base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(dwc->base))
> +		return PTR_ERR(dwc->base);
> +
> +	if (!device_property_read_u32(dev, "snps,pwm-number", &nr_pwm)) {

s/"snps,pwm-number"/"npwm"/

> +		if (nr_pwm > DWC_TIMERS_TOTAL)
> +			dev_warn(dev, "too many PWMs (%u), capping at %u\n",
> +				 nr_pwm, chip->npwm);
> +		else
> +			chip->npwm = nr_pwm;

Huh, no. chip->npwm is in the core's dominion, don't change that in a
driver.

> +	}
> +
> +	dwc->bus_clk = devm_clk_get(dev, "bus");
> +	if (IS_ERR(dwc->bus_clk))
> +		return dev_err_probe(dev, PTR_ERR(dwc->bus_clk),
> +				     "failed to get bus clock\n");
> +
> +	dwc->clk = devm_clk_get(dev, "timer");
> +	if (IS_ERR(dwc->clk))
> +		return dev_err_probe(dev, PTR_ERR(dwc->clk),
> +				     "failed to get timer clock\n");
> +
> +	ret = devm_clk_rate_exclusive_get(dev, dwc->clk);
> +	if (ret)
> +		return dev_err_probe(dev, ret,
> +				     "failed to get exclusive rate\n");
> +
> +	dwc->clk_rate = clk_get_rate(dwc->clk);

clk_get_rate() should only be called on enabled clocks. Consider using
devm_clk_get_enabled()

> +	if (!dwc->clk_rate)
> +		return dev_err_probe(dev, -EINVAL,
> +				     "failed to get a valid clock rate\n");
> +
> +	dwc->rst = devm_reset_control_array_get_optional_exclusive(dev);
> +

No empty line here.

> +	if (IS_ERR(dwc->rst))
> +		return dev_err_probe(dev, PTR_ERR(dwc->rst),
> +				     "failed to get reset control\n");
> +
> +	ret = clk_prepare_enable(dwc->bus_clk);
> +	if (ret)
> +		return dev_err_probe(dev, ret,
> +				     "failed to enable bus clock\n");
> +
> +	ret = clk_prepare_enable(dwc->clk);
> +	if (ret) {
> +		ret = dev_err_probe(dev, ret, "failed to enable timer clock\n");
> +		goto disable_busclk;
> +	}
> +
> +	/* Ensure the device is out of reset before accessing MMIO registers. */
> +	ret = reset_control_deassert(dwc->rst);
> +	if (ret) {
> +		ret = dev_err_probe(dev, ret, "failed to deassert reset\n");
> +		goto disable_clk;
> +	}
> +
> +	/*
> +	 * Check all channels to see if any channel is enabled.
> +	 * Read the control register of each channel and extract the enable bit
> +	 */
> +	for (i = 0; i < chip->npwm; i++) {
> +		ctrl[i] = dwc_pwm_readl(dwc, DWC_TIM_CTRL(i)) & DWC_TIM_CTRL_EN;
> +		if (ctrl[i])
> +			pwm_en = true;

After you found the first enabled channel, you can break the loop.

> +	}
> +
> +	/*
> +	 * Only issue a reset pulse when all channels are disabled, so a PWM
> +	 * channel already running (e.g. configured by firmware before Linux
> +	 * took over) is left undisturbed.
> +	 */
> +	if (!pwm_en) {
> +		ret = reset_control_reset(dwc->rst);
> +		if (ret) {
> +			ret = dev_err_probe(dev, ret, "failed to reset\n");
> +			goto disable_clk;
> +		}
> +	}
> +
> +	/* init PWM feature */
> +	dwc->features = 0;
> +	/*
> +	 * Support for 0% and 100% duty cycle mode was added in version 2.11a
> +	 * and later.
> +	 */
> +	tim_id = dwc_pwm_readl(dwc, DWC_TIMERS_COMP_VERSION);
> +	if (tim_id >= DWC_TIM_VERSION_ID_2_11A)
> +		dwc->features |= DWC_TIM_CTRL_0N100PWM_EN;
> +

That looks generic and so should go into pwm-dwc-core.c?!

> +	data->chips[0] = chip;
> +	dev_set_drvdata(dev, data);
> +
> +	/*
> +	 * Save hardware state to PM context before first resume.
> +	 * Prevents zero-initialized context from overwriting registers
> +	 * and losing bootloader-preset configuration.
> +	 */
> +	for (i = 0; i < chip->npwm; i++) {
> +		dwc->ctx[i].cnt = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(i));
> +		dwc->ctx[i].cnt2 = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(i));
> +		dwc->ctx[i].ctrl = dwc_pwm_readl(dwc, DWC_TIM_CTRL(i));
> +	}
> +
> +	/*
> +	 * If any PWM channel is enabled, mark device active and hold runtime PM
> +	 * references for each enabled channel. Otherwise, gate the clocks.
> +	 *
> +	 * When CONFIG_PM is disabled, pm_runtime callbacks are stubs that never
> +	 * re-enable clocks, so keep clocks on unconditionally in that case.
> +	 */
> +	if (pwm_en) {
> +		pm_runtime_set_active(dev);
> +		for (i = 0; i < chip->npwm; i++) {
> +			if (ctrl[i])
> +				pm_runtime_get_noresume(dev);
> +		}
> +	} else if (IS_ENABLED(CONFIG_PM)) {
> +		clk_disable_unprepare(dwc->clk);
> +		clk_disable_unprepare(dwc->bus_clk);

Are you sure that disabling clocks is right here? After
pm_runtime_enable() the device is assumed to be active and suspend might
be called then which calls clk_disable_unprepare() again underflowing
the enable count.

> +	}
> +
> +	pm_runtime_enable(dev);
> +
> +	ret = pwmchip_add(chip);
> +	if (ret) {
> +		ret = dev_err_probe(dev, ret, "failed to add pwm chip\n");
> +		goto pm_disable;
> +	}
> +
> +	return 0;
> +
> +pm_disable:
> +	pm_runtime_disable(dev);
> +	if (pwm_en) {
> +		for (i = 0; i < chip->npwm; i++) {
> +			if (ctrl[i])
> +				pm_runtime_put_noidle(dev);
> +		}
> +		goto disable_clk;
> +	}
> +	/*
> +	 * When CONFIG_PM is disabled, clocks were never gated and must be
> +	 * disabled here.
> +	 */
> +	if (!IS_ENABLED(CONFIG_PM))
> +		goto disable_clk;
> +
> +	return ret;
> +
> +disable_clk:
> +	clk_disable_unprepare(dwc->clk);
> +disable_busclk:
> +	clk_disable_unprepare(dwc->bus_clk);
> +
> +	return ret;
> +}
> [...]
> +static struct platform_driver dwc_pwm_plat_driver = {
> +	.driver = {
> +		.name = "dwc-pwm",
> +		.pm = pm_ptr(&dwc_pwm_pm_ops),
> +		.of_match_table = dwc_pwm_dt_ids,
> +	},
> +	.probe = dwc_pwm_plat_probe,
> +	.remove = dwc_pwm_plat_remove,
> +};
> +
> +module_platform_driver(dwc_pwm_plat_driver);

No empty line between the platform_driver and module_platform_driver().

> +MODULE_AUTHOR("Ben Dooks <ben.dooks@codethink.co.uk>");
> +MODULE_DESCRIPTION("DesignWare PWM Controller");
> +MODULE_LICENSE("GPL");

Best regards
Uwe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  parent reply	other threads:[~2026-12-03 17:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-30 11:14 [PATCH v14 0/5] Update designware pwm driver dongxuyang
2026-07-30 11:15 ` [PATCH v14 1/5] dt-bindings: pwm: dwc: Document optional resets property dongxuyang
2026-07-30 11:15 ` [PATCH v14 2/5] dt-bindings: pwm: dwc: Add eswin compatible dongxuyang
     [not found]   ` <20260730112958.9ED571F00A3A@smtp.kernel.org>
2026-07-31  6:41     ` Xuyang Dong
2026-07-31  6:57   ` Krzysztof Kozlowski
2026-07-30 11:15 ` [PATCH v14 3/5] pwm: dwc: add of/platform support dongxuyang
     [not found]   ` <20260730113930.8FC151F000E9@smtp.kernel.org>
2026-07-31  6:41     ` Xuyang Dong
2026-12-03 17:31   ` Uwe Kleine-König [this message]
2026-07-30 11:16 ` [PATCH v14 4/5] dt-bindings: timer: dwc: Update resets property items dongxuyang
2026-07-30 11:16 ` [PATCH v14 5/5] clocksource: dw_apb_timer: Use reset array API to handle all resets dongxuyang
     [not found]   ` <20260730115229.145411F000E9@smtp.kernel.org>
2026-07-31  6:41     ` Xuyang Dong
2026-08-12  9:23 ` [PATCH v14 0/5] Update designware pwm driver Xuyang Dong
2026-10-27  9:28   ` Xuyang Dong
2026-12-03 17:06 ` Uwe Kleine-König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axGhs14H2rqx5Xed@monoceros \
    --to=ukleinek@kernel.org \
    --cc=ben-linux@fluff.org \
    --cc=ben.dooks@codethink.co.uk \
    --cc=conor+dt@kernel.org \
    --cc=daniel.lezcano@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dongxuyang@eswincomputing.com \
    --cc=krzk+dt@kernel.org \
    --cc=linmin@eswincomputing.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=ningyu@eswincomputing.com \
    --cc=p.zabel@pengutronix.de \
    --cc=pinkesh.vaghela@einfochips.com \
    --cc=robh@kernel.org \
    --cc=tglx@kernel.org \
    --cc=wangguosheng@eswincomputing.com \
    --cc=xuxiang@eswincomputing.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox