From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796319087; b=3xIGgoNV06WgwWAteqIFOKJFHfmRpCwNdUCj5oPOH/cdevgrt65CX7fAch7J9volNe3a kpfjPeWD2reqKmBpyOzHf7y9t6gVX4zWx7bevU48b9wvuf7woQlW3SKbt5SHfpsYO94jY XIBmEQ54/+ssEwAT0PyICe+0m0//ItAPNcQ8PwUuHKVYALCTrwFbeXj2UBeolR/plZTdh Yrlir3ftlu94KluSJOBd+4ztpFHDgEaVaOpd2pon48e/Hcy6s2zfQBD7Ghc/2K+brqMdM RbWSBuGA7cnXqKlqjnEWvMupuPcDa5ZgyDckZJMPB72vP7lupKiRXz2IFP1g3rpL3tg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796319087; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=Z81NQMNJCN2wmlwtblVJLmQ8r1nfHIkQ7cirKYEPvCw=; b=heC3WA2PTA9lu/fNQpZ7sv5z3RWsCwERTijKmZlXMMG1L0g+O4SEImp1XO7GYs9gKaUb GO8jFIqjGFPISTaszTE7S4rFu4a020l/CTiqWiBSqEcN6JFaAPoSpKqaqGWLBe0z59WrQ 2wTBXgwxDqWxr3VxrfqtenDo6vLnwU3EdCZtTQwqCqb4GaoycVjyF2s/ZmU3nVBT3cbS3 2zrzSfhhaLc9Ytr3wtdyZOjXHiBi3N1ACdxVn0PC9ar04AAtJxOQfhpWEIm6dhZnv51Nt /IyqOHE14D0IfuGZanBDVcZHnd3lQIrmOli2ed6vFAG6097wuVOrHGiA/igej3eJ9Ug== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GuNNXbol; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 49D43606A32C; Thu, 3 Dec 2026 17:31:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GuNNXbol Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 9316E1F00896; Thu, 3 Dec 2026 17:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796319087; bh=Z81NQMNJCN2wmlwtblVJLmQ8r1nfHIkQ7cirKYEPvCw=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=GuNNXbolGDzDojFwXdg7JU8XE005Pvkvd3pweRN95V1qy7X8YQkj9OgkLAuWELHvU B1fGfG6p+axcUoIhZDVkxdr4hth1CCFXqLNthcYqWSORfeq/LISGGhn3PSUZqhb3Sr skD2o+Z5IMBv+eEtdh9OhRGrAVBs/+LCSEW3SxjSZ99FyOwLxzS4SWc1OwRdq7ODFK u8KpQCpgxKrchuS2eb8VNec5PG1tNrYKulqmru+YUe/2v2QaxZ+JDsOFjyjl2YNK9j 3WkMO+vo3bKk3a3kFvE6clcop6aC0gC4b58qSwTwhmPeGbKlIQUaPBzAkdALfPPeMD P0Q0W4uJ9iuQA== Date: Thu, 3 Dec 2026 18:31:24 +0100 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= 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 Message-ID: References: <20260730111425.1680-1-dongxuyang@eswincomputing.com> <20260730111553.1886-1-dongxuyang@eswincomputing.com> X-Mailing-List: linux-pwm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="y6vlxbuksv4m7g6n" Content-Disposition: inline In-Reply-To: <20260730111553.1886-1-dongxuyang@eswincomputing.com> --y6vlxbuksv4m7g6n Content-Type: text/plain; protected-headers=v1; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Subject: Re: [PATCH v14 3/5] pwm: dwc: add of/platform support MIME-Version: 1.0 Hello, On Thu, Jul 30, 2026 at 07:15:53PM +0800, dongxuyang@eswincomputing.com wro= te: > 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" > =20 > #include > +#include > #include > #include > +#include > #include > #include > #include > @@ -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; > =20 > - /* > - * 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 =3D DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns); > - if (tmp < 1 || tmp > (1ULL << 32)) > - return -ERANGE; > - low =3D tmp - 1; > - > - tmp =3D DIV_ROUND_CLOSEST_ULL(state->period - state->duty_cycle, > - dwc->clk_ns); > - if (tmp < 1 || tmp > (1ULL << 32)) > - return -ERANGE; > - high =3D tmp - 1; > + if (dwc->clk) > + dwc->clk_rate =3D 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 =3D mul_u64_u64_div_u64(state->period, dwc->clk_rate, > + NSEC_PER_SEC); > + if (!period_cyc) > + return -ERANGE; > + > + tmp =3D 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 >=3D (1ULL << 32) || period_cyc - tmp >=3D (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 =3D=3D PWM_POLARITY_NORMAL) { > + high =3D tmp; > + low =3D period_cyc - tmp; > + } else { > + low =3D tmp; > + high =3D 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 =3D mul_u64_u64_div_u64(state->duty_cycle, dwc->clk_rate, > + NSEC_PER_SEC); > + if (tmp < 1 || tmp > (1ULL << 32)) > + return -ERANGE; > + > + period_cyc =3D mul_u64_u64_div_u64(state->period, dwc->clk_rate, > + NSEC_PER_SEC); > + /* period_cyc - tmp: complementary half; tmp <=3D period_cyc */ > + if (period_cyc - tmp < 1 || period_cyc - tmp > (1ULL << 32)) > + return -ERANGE; > + > + if (state->polarity =3D=3D PWM_POLARITY_NORMAL) { > + high =3D tmp - 1; > + low =3D period_cyc - tmp - 1; > + } else { > + low =3D tmp - 1; > + high =3D period_cyc - tmp - 1; > + } > + } > =20 > /* > * 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 *d= wc, > * width of low period and latter the width of high period in terms > * multiple of input clock periods: > * Width =3D ((Count + 1) * input clock period). > + * Width =3D (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 =3D 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 |=3D DWC_TIM_CTRL_INT_MASK; > + if (dwc->features & DWC_TIM_CTRL_0N100PWM_EN) > + ctrl |=3D DWC_TIM_CTRL_0N100PWM_EN; > + > dwc_pwm_writel(dwc, ctrl, DWC_TIM_CTRL(pwm->hwpwm)); > =20 > /* > @@ -99,14 +166,18 @@ static int dwc_pwm_apply(struct pwm_chip *chip, stru= ct pwm_device *pwm, > const struct pwm_state *state) > { > struct dwc_pwm *dwc =3D to_dwc_pwm(chip); > - > - if (state->polarity !=3D PWM_POLARITY_INVERSED) > - return -EINVAL; > + int ret; > =20 > 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 =3D pm_runtime_resume_and_get(pwmchip_parent(chip)); > + if (ret < 0) > + return ret; > + } > + ret =3D __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 > +#include > +#include > +#include > +#include > + > +#include "pwm-dwc.h" > + > +static int dwc_pwm_plat_probe(struct platform_device *pdev) > +{ > + struct device *dev =3D &pdev->dev; > + struct dwc_pwm_drvdata *data; > + u32 ctrl[DWC_TIMERS_TOTAL]; > + struct pwm_chip *chip; > + struct dwc_pwm *dwc; > + bool pwm_en =3D false; > + u32 nr_pwm, tim_id; > + unsigned int i; > + int ret; > + > + data =3D devm_kzalloc(dev, struct_size(data, chips, 1), GFP_KERNEL); > + if (!data) > + return -ENOMEM; > + > + chip =3D 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 =3D to_dwc_pwm(chip); > + > + dwc->base =3D 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 =3D nr_pwm; Huh, no. chip->npwm is in the core's dominion, don't change that in a driver. > + } > + > + dwc->bus_clk =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D clk_prepare_enable(dwc->bus_clk); > + if (ret) > + return dev_err_probe(dev, ret, > + "failed to enable bus clock\n"); > + > + ret =3D clk_prepare_enable(dwc->clk); > + if (ret) { > + ret =3D 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 =3D reset_control_deassert(dwc->rst); > + if (ret) { > + ret =3D 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 =3D 0; i < chip->npwm; i++) { > + ctrl[i] =3D dwc_pwm_readl(dwc, DWC_TIM_CTRL(i)) & DWC_TIM_CTRL_EN; > + if (ctrl[i]) > + pwm_en =3D 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 =3D reset_control_reset(dwc->rst); > + if (ret) { > + ret =3D dev_err_probe(dev, ret, "failed to reset\n"); > + goto disable_clk; > + } > + } > + > + /* init PWM feature */ > + dwc->features =3D 0; > + /* > + * Support for 0% and 100% duty cycle mode was added in version 2.11a > + * and later. > + */ > + tim_id =3D dwc_pwm_readl(dwc, DWC_TIMERS_COMP_VERSION); > + if (tim_id >=3D DWC_TIM_VERSION_ID_2_11A) > + dwc->features |=3D DWC_TIM_CTRL_0N100PWM_EN; > + That looks generic and so should go into pwm-dwc-core.c?! > + data->chips[0] =3D 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 =3D 0; i < chip->npwm; i++) { > + dwc->ctx[i].cnt =3D dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(i)); > + dwc->ctx[i].cnt2 =3D dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(i)); > + dwc->ctx[i].ctrl =3D 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 =3D 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 =3D pwmchip_add(chip); > + if (ret) { > + ret =3D 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 =3D 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 =3D { > + .driver =3D { > + .name =3D "dwc-pwm", > + .pm =3D pm_ptr(&dwc_pwm_pm_ops), > + .of_match_table =3D dwc_pwm_dt_ids, > + }, > + .probe =3D dwc_pwm_plat_probe, > + .remove =3D 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 "); > +MODULE_DESCRIPTION("DesignWare PWM Controller"); > +MODULE_LICENSE("GPL"); Best regards Uwe --y6vlxbuksv4m7g6n Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmsRp2gACgkQj4D7WH0S /k72SQf/WMnRlFQSFwr3waiX8w74t7jqYnGQCKgBR4TsmkuTEIGFF1Lt8IOWQAnt JUaLVW+m1dlS9tq1jpa6E6NYnZxPpDflliZKCCRQi+C7GMY+acCtE0a9bMoht9Hr cXzaSnBFapkDth+AGo03Bg9LzlinxWKbCuJoKeEAb+Iy0S5FOzP3f4elcIo1aBKY PQQHskd65H2r1AddbqEQf9yBPa1RVTWkIKeG4ALEwDcu5Flq42TCMhGfmNus55m0 1a0mSwZ9t2E6/01+X0ZXvbdfzluzugsEfGUiBM1mlLXxAKbQnXAax/9Qm18nu/RO XJi6D67BCrxEA4zCir4l08YAOGWJKg== =rS9R -----END PGP SIGNATURE----- --y6vlxbuksv4m7g6n--