Update designware pwm driver
Subsystems (2)
v7 cover Update designware pwm driver
From: Xuyang Dong <[email protected]>
There is already a patch [1] for the DesignWare PWM driver, which is posted by Ben and still under review. Based on this patch, this series is a continuation of [1] to add support for IP versions 2.11a and later, which includes support for "Pulse Width Modulation with 0% and 100% Duty Cycle".
Supported chips:
ESWIN EIC7700 series SoC.
Test:
Tested this patch on the Sifive HiFive Premier P550 (which uses the EIC7700 SoC).
[1] https://lore.kernel.org/lkml/[email protected]/
Updates:
Changes in v7:
- YAML:
- Dropped Conor's Acked-by due to significant schema changes.
- Rename patch 1 from "dt-bindings: pwm: dwc: add optional reset" to "dt-bindings: pwm: dwc: Add eswin compatible and resets property".
- Update the commit message to explain why the EIC7700 supports only one reset.
- Add contstraints 'minItems: 1' and 'maxItems: 1' for the resets property of eswin,eic7700-pwm.
- Add an example for eswin,eic7700-pwm.
- Link to v6: https://lore.kernel.org/all/[email protected]/
Changes in v6:
- YAML:
- Drop properties resets and its items description for eswin,eic7700-pwm.
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes in v5:
- YAML:
- Add 'eswin,eic7700-pwm' compatible string.
- Add the items description for the resets property and set minItems to 1.
- Require resets property with exactly 1 reset for eswin,eic7700-pwm compatible.
- Driver:
- Add support for 'eswin,eic7700-pwm' compatible.
- Add structure dwc_pwm_plat_data to manage the API for obtaining resets.
- Link to v4: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- YAML:
- Change maxItems from 1 to 2. As there is a corresponding reset signal for each clock domain, the effective maxItems of the resets property is set to 2.
- Update the YAML commit message to describe the hardware.
- Driver:
- Replace devm_reset_control_get_optional_exclusive() with devm_reset_control_array_get_optional_exclusive(). Since the number of reset signals has increased from one to two, we need to use the array API to acquire them.
- Link to v3: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- YAML:
- Added a clear justification for the optional resets property. It is required to support proper controller initialization when no PWM channel is active at boot time, while allowing the driver to skip reset deassertion if any channel is already enabled.
- Driver:
- Update the boundary value check of tmp in __dwc_pwm_configure_timer() for DWC_TIM_CTRL_0N100PWM_EN.
- Replace 'sizeof(struct dwc_pwm_drvdata)' with 'struct_size(data, chips, 1)'.
- Drop devm_clk_get_enabled() in favor of devm_clk_get() with explicit clk_prepare_enable() and clk_disable_unprepare() allowing runtime PM to manage clock state.
- Replace devm_reset_control_get_optional_exclusive_deasserted() with devm_reset_control_get_optional_exclusive() and issue a full reset via reset_control_reset() only when no PWM channel is active at probe time.
- Detect bootloader-enabled PWM channels by reading the enable bit, and initialize runtime PM as active for those channels by calling pm_runtime_set_active() and pm_runtime_get_noresume().
- Remove autosuspend as it is not required for this driver.
- Use explicit pm_runtime_enable() and pm_runtime_disable() instead of the managed devm_pm_runtime_enable() variant to ensure correct cleanup.
- On device removal, recheck the channel enable status. If any channel remains active, call pm_runtime_put_noidle() before disabling clocks via clk_disable_unprepare().
Resume device before register access during removal if it is runtime suspended, and re-suspend it afterward.
- If device is suspended, resume it before register access during system resume/suspend.
- Use pm_ptr() instead of pm_sleep_ptr() for correct PM operation.
- Link to v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- YAML:
- Remove eswin,eic7700-pwm.yaml. Use snps,dw-apb-timers-pwm2.yaml. The description in snps,dw-apb-timers-pwm2.yaml is better.
- Add the resets property as optional, as defined in the databook.
- Remove snps,pwm-full-range-enable as no additional property is needed.
- Driver:
- Change the file from pwm-dwc-eic7700.c to pwm-dwc-of.c from [1].
- Define DWC_TIM_VERSION_ID_2_11A 2.11a as the baseline version.
- Enable the 0% and 100% duty cycle mode by setting dwc->feature if the version read from the TIMERS_COMP_VERSION register is later than or equal to DWC_TIM_VERSION_ID_2_11A.
- Use the DIV_ROUND_UP_ULL() to calculate width in the .apply and .get_state.
- Additionally, Power Management (PM) support has been added to the pwm-dwc-of.c driver.
- Drop the headers that are not used.
- Use devm_clk_get_enabled() instead of devm_clk_get().
- Drop of_match_ptr.
- Fix build error with 1ULL << 32.
- Link to v1: https://lore.kernel.org/all/[email protected]/
- Link to v9: https://lore.kernel.org/lkml/[email protected]/
Xuyang Dong (2):
dt-bindings: pwm: dwc: Add eswin compatible and resets property
pwm: dwc: add of/platform support
.../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 38 +- drivers/pwm/Kconfig | 10 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-dwc-core.c | 101 +++-- drivers/pwm/pwm-dwc-of.c | 346 ++++++++++++++++++ drivers/pwm/pwm-dwc.h | 25 +- 6 files changed, 488 insertions(+), 33 deletions(-) create mode 100644 drivers/pwm/pwm-dwc-of.c
--
2.34.1
PATCH v7 1/2 dt-bindings: pwm: dwc: Add eswin compatible and resets property
From: Xuyang Dong <[email protected]>
EIC7700 use DesignWare IP for PWM controllers. Add ESWIN EIC7700 support in snps,dw-apb-timers-pwm2.yaml.
The DesignWare PWM includes separate reset signals dedicated to each clock domain:
The presetn signal resets logic in pclk domain.
The timer_N_resetn signal resets logic in the timer_N_clk domain. The resets are active-low.
The generic snps,dw-apb-timers-pwm2 binding allows one or two optional reset lines depending on SoC integration.
On EIC7700, the presetn and timer_N_resetn inputs are physically tied to a single reset line, therefore exactly one reset is required.
--- .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +34 -4
@@ -20,12 +20,11 @@ description: instead of having to encode the IP version number in the device tree compatible. -allOf: - - $ref: pwm.yaml# - properties: compatible: - const: snps,dw-apb-timers-pwm2 + enum: + - snps,dw-apb-timers-pwm2 + - eswin,eic7700-pwm reg: maxItems: 1
@@ -43,6 +42,12 @@ properties: - const: bus - const: timer + resets: + minItems: 1 + items: + - description: Interface bus reset + - description: PWM timer logic reset + snps,pwm-number: $ref: /schemas/types.yaml#/definitions/uint32 description: The number of PWM channels configured for this instance
@@ -54,6 +59,22 @@ required: - clocks - clock-names +allOf: + - $ref: pwm.yaml# + + - if: + properties: + compatible: + contains: + const: eswin,eic7700-pwm + then: + properties: + resets: + minItems: 1 + maxItems: 1 + required: + - resets + additionalProperties: false examples:
@@ -65,3 +86,12 @@ examples: clocks = <&bus>, <&timer>; clock-names = "bus", "timer"; }; + - | + pwm@50818000 { + compatible = "eswin,eic7700-pwm"; + reg = <0x50818000 0x4000>; + #pwm-cells = <3>; + clocks = <&bus>, <&timer>; + clock-names = "bus", "timer"; + resets = <&reset>; + };
Signature · 1 line
2.34.1
PATCH v7 2/2 pwm: dwc: add of/platform support
In reply to [email protected]The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
The controller is reset only when no PWM channel is enabled. Otherwise, clocks are enabled and the runtime PM state is updated to reflect the active hardware configuration.
--- drivers/pwm/Kconfig | 10 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-dwc-core.c | 101 ++++++++--- drivers/pwm/pwm-dwc-of.c | 346 +++++++++++++++++++++++++++++++++++++ drivers/pwm/pwm-dwc.h | 25 ++- 5 files changed, 454 insertions(+), 29 deletions(-) create mode 100644 drivers/pwm/pwm-dwc-of.c
drivers/pwm/Kconfig +10 -0
@@ -249,6 +249,16 @@ config PWM_DWC To compile this driver as a module, choose M here: the module will be called pwm-dwc. +config PWM_DWC_OF + tristate "DesignWare PWM Controller (OF bus)" + depends on HAS_IOMEM && (OF || COMPILE_TEST) + select PWM_DWC_CORE + help + PWM driver for Synopsys DWC PWM Controller on an OF bus or + a platform bus. + To compile this driver as a module, choose M here: the module + will be called pwm-dwc-of. + config PWM_EP93XX tristate "Cirrus Logic EP93xx PWM support" depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_PWM_CRC) += pwm-crc.o obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o obj-$(CONFIG_PWM_DWC_CORE) += pwm-dwc-core.o obj-$(CONFIG_PWM_DWC) += pwm-dwc.o +obj-$(CONFIG_PWM_DWC_OF) += pwm-dwc-of.o obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o obj-$(CONFIG_PWM_GPIO) += pwm-gpio.o
drivers/pwm/pwm-dwc-core.c +79 -22
153 lines not on this page · raw message
drivers/pwm/pwm-dwc-of.c new file +346 -0
346 lines not on this page · raw message
drivers/pwm/pwm-dwc.h +18 -7
@@ -26,12 +26,19 @@ MODULE_IMPORT_NS("dwc_pwm");#define DWC_TIMERS_TOTAL 8/* Timer Control Register */ -#define DWC_TIM_CTRL_EN BIT(0) -#define DWC_TIM_CTRL_MODE BIT(1) -#define DWC_TIM_CTRL_MODE_FREE (0 << 1) -#define DWC_TIM_CTRL_MODE_USER (1 << 1) -#define DWC_TIM_CTRL_INT_MASK BIT(2) -#define DWC_TIM_CTRL_PWM BIT(3) +#define DWC_TIM_CTRL_EN BIT(0) +#define DWC_TIM_CTRL_MODE BIT(1) +#define DWC_TIM_CTRL_MODE_FREE (0 << 1) +#define DWC_TIM_CTRL_MODE_USER BIT(1) +#define DWC_TIM_CTRL_INT_MASK BIT(2) +#define DWC_TIM_CTRL_PWM BIT(3) +#define DWC_TIM_CTRL_0N100PWM_EN BIT(4) + +/* +* The version 2.11a and later add "Pulse Width Modulation with +* 0% and 100% Duty Cycle". +*/ +#define DWC_TIM_VERSION_ID_2_11A 0x3231312astruct dwc_pwm_info {unsigned int nr;
@@ -52,8 +59,12 @@ struct dwc_pwm_ctx {struct dwc_pwm {void __iomem *base; -unsigned int clk_ns; +struct clk *bus_clk; +struct clk *clk; +unsigned long clk_rate; +struct reset_control *rst;struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL ]; +u32 features; };static inline struct dwc_pwm *to_dwc_pwm (struct pwm_chip *chip)
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v7 2/2] pwm: dwc: add of/platform support
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
New issues:
- [High] Registering the PWM chip before completing runtime PM initialization exposes the hardware to consumers while clocks might be turned off.
- [High] Manual hardware teardown in .remove() precedes pwm_chip deregistration, leading to potential bus faults.
- [High] TOCTOU race in remove() when conditionally acquiring a runtime PM reference allows concurrent runtime suspend while accessing registers.
- [High] System resume is incorrectly aborted if expected runtime PM idle operations return -EAGAIN.
- [High] Unconditional assertion of the reset control on the probe error path and during module removal permanently kills active hardware and unbalances the reset API.
- [High] remove() artificially drops runtime PM references that belong to active consumers, causing an underflow.
- [Medium] Integer overflow in timer configuration allows invalid large duty cycle/period values to bypass validation.
- [Medium] Clock references are leaked during module removal if the device is held in an active PM state but no PWM channels are enabled.
- [Medium] The driver rounds UP duty cycle and period calculations instead of rounding DOWN, violating PWM subsystem rules.
- [Medium] The driver evaluates the static device tree polarity instead of the dynamically requested runtime polarity, mapping the duty cycle incorrectly.
Pre-existing issues:
- [High] dwc_pwm_get_state() ignores the return value of pm_runtime_get_sync(), allowing unprotected hardware access. --
commit 517966ff9e8e50806716056dd4bd3f2f88b832c0
Author: Xuyang Dong <[email protected]>
pwm: dwc: add of/platform support
The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
Quoted 4 lines
drivers/pwm/pwm-dwc-core.c +0
-0
[ ... ]
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +0
-0@@ -44,21 +45,52 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
[ ... ]
drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 57
Quoted 7 lines
drivers/pwm/pwm-dwc-core.c +7
-0+if (dwc->features &DWC_TIM_CTRL_0N100PWM_EN ) { +/* +* Calculate width of low and high period in terms of input +* clock periods and check are the result within HW limits +* between 0 and 2^32 periods. +*/ + tmp = state->duty_cycle * dwc->clk_rate;
[Severity: Medium]
Could this multiplication overflow the 64-bit boundary before the hardware limit check if a consumer requests an extremely large period? Both duty_cycle and period are u64 nanoseconds, while clk_rate is an unsigned long. A wrapped-around small value would evade the hardware limit check entirely. Should this use mul_u64_u64_div_u64() to scale the values safely?
drivers/pwm/pwm-dwc-core.c
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +1
-0+ tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );
[Severity: Medium]
Does this violate the PWM subsystem rules by rounding up instead of down? The PWM core framework strictly requires that the .apply callback rounds down the requested period and duty cycle to the nearest achievable hardware value.
drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 65
Quoted 7 lines
drivers/pwm/pwm-dwc-core.c +7
-0+if (tmp >= (1ULL <<32 )) +return -ERANGE ; + +if (pwm->args.polarity ==PWM_POLARITY_INVERSED ) + high = tmp; +else + low = tmp;
[Severity: Medium]
Should this evaluate the dynamically requested state->polarity instead of the static device tree polarity in pwm->args.polarity? Since dwc_pwm_apply() enforces PWM_POLARITY_INVERSED, if the device tree was configured with PWM_POLARITY_NORMAL, this will program the hardware with the inverted duty cycle relative to what the consumer actually requested.
[ ... ]
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 164
Quoted 9 lines
drivers/pwm/pwm-dwc-core.c +1
-0@@ -121,11 +157,17 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) {struct dwc_pwm *dwc =to_dwc_pwm (chip); +unsigned long clk_rate;u64 duty, period;u32 ctrl, ld, ld2;pm_runtime_get_sync (pwmchip_parent (chip));
[Severity: High]
This isn't a bug introduced by this patch, but the return value of pm_runtime_get_sync() is ignored here. If the function fails (for example, returning -EACCES when PM runtime is disabled during driver removal, or before it is enabled in probe), the hardware might not be powered on and clocks could remain gated. The function will then proceed to read the DWC_TIM_CTRL and DWC_TIM_LD_CNT registers, which can cause a synchronous external abort when accessing unclocked hardware. Can we add a check for the return value here?
Quoted 5 lines
drivers/pwm/pwm-dwc-of.c new file +0
-0
[ ... ]
Quoted 2 lines
drivers/pwm/pwm-dwc-of.c new file +2
-0+static int dwc_pwm_plat_probe (struct platform_device *pdev) +{
[ ... ]
Quoted 15 lines
drivers/pwm/pwm-dwc-of.c new file +15
-0+ ret =devm_pwmchip_add (dev, chip); +if (ret) { +dev_err (dev,"failed to add pwm chip\n" ); + goto reset_assert; + } + + data->chips[0 ] = chip; +dev_set_drvdata (dev, data); + +/* +* If any PWM channel is enabled, mark device active and hold runtime PM +* references for each enabled channel. Otherwise, gate the clocks. +*/ +if (pwm_en) { +pm_runtime_set_active (dev);
drivers/pwm/pwm-dwc-of.c line 149
Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10
-0+for (i =0 ; i < chip->npwm; i++) { +if (ctrl[i]) +pm_runtime_get_noresume (dev); + } + }else { +clk_disable_unprepare (dwc->clk); +clk_disable_unprepare (dwc->bus_clk); + } + +pm_runtime_enable (dev);
[Severity: High]
Does registering the PWM chip before enabling PM expose the hardware to consumers prematurely? devm_pwmchip_add() is called before pm_runtime_enable(). If a consumer accesses the PWM during this window, the core driver's pm_runtime_get_sync() call would fail with -EACCES, but still proceed to access registers. At the same time, probe might gate the clocks if pwm_en is false, leading to a bus fault.
drivers/pwm/pwm-dwc-of.c line 154
Quoted 5 lines
drivers/pwm/pwm-dwc-of.c new file +5
-0+ +return 0 ; + +reset_assert: +reset_control_assert (dwc->rst);
[Severity: High]
Does this unconditionally assert the reset control on the error path? The commit message mentions resetting only when no PWM channel is enabled. If pwm_en was true, this forcibly kills actively running hardware. If pwm_en was false, probe used reset_control_reset() (which is a pulse), so asserting it here leaves the line permanently asserted in an unbalanced state.
Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10
-0+disable_clk: +clk_disable_unprepare (dwc->clk); +disable_busclk: +clk_disable_unprepare (dwc->bus_clk); + +return ret; +} + +static void dwc_pwm_plat_remove (struct platform_device *pdev) +{
[ ... ]
drivers/pwm/pwm-dwc-of.c line 178
Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7
-0+/* +* Resume the device if it is runtime suspended to allow +* safe register access. +*/ + pm_flags =pm_runtime_status_suspended (&pdev->dev); +if (pm_flags) +pm_runtime_get_sync (&pdev->dev);
[Severity: High]
Is there a race condition when conditionally acquiring this PM reference? If pm_runtime_status_suspended() returns false, the code proceeds to read registers without taking a reference. An auto-suspend timer or a concurrent consumer could suspend the device immediately after this check, turning off the clocks and causing the subsequent DWC_TIM_CTRL reads to trigger a bus fault. Should pm_runtime_get_sync() be called unconditionally?
drivers/pwm/pwm-dwc-of.c line 185
Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7
-0+ +for (idx =0 ; idx < chip->npwm; idx++) { +if (dwc_pwm_readl (dwc,DWC_TIM_CTRL (idx)) &DWC_TIM_CTRL_EN ) { + pwm_en = true; +pm_runtime_put_noidle (&pdev->dev); + } + }
[Severity: High]
Does dropping the PM reference here underflow the usage count for active consumers? If a consumer has taken over the PWM channel, it logically owns that reference via dwc_pwm_apply(). Because devres unregisters the PWM chip after remove() finishes, the PWM core will eventually call dwc_pwm_apply(..., enabled=false), which calls pm_runtime_put_sync(). Dropping it here artificially might lead to a usage count underflow.
drivers/pwm/pwm-dwc-of.c line 197
Quoted 12 lines
drivers/pwm/pwm-dwc-of.c new file +12
-0+ +/* +* Re-suspend the device if it was runtime suspended prior to +* the register access. +*/ +if (pm_flags) +pm_runtime_put_sync (&pdev->dev); + +if (pwm_en) { +clk_disable_unprepare (dwc->clk); +clk_disable_unprepare (dwc->bus_clk); + }
[Severity: Medium]
Will this leak clock references if the device is active but no PWM channels are enabled? If pwm_en is false but the device's PM runtime state is RPM_ACTIVE (due to an open sysfs handle or a pending auto-suspend), skipping this block leaves the clocks on. The subsequent pm_runtime_disable() does not turn off clocks, permanently leaking them.
drivers/pwm/pwm-dwc-of.c line 201
Quoted 4 lines
drivers/pwm/pwm-dwc-of.c new file +4
-0+ +pm_runtime_disable (&pdev->dev); +reset_control_assert (dwc->rst); +}
[Severity: High]
Does manually tearing down the hardware here lead to bus faults during devres cleanup? Since the driver registers the chip using devm_pwmchip_add(), devres will unregister the PWM chip after dwc_pwm_plat_remove() returns. During that window, the device is fully accessible to consumers, but clocks are off and reset is asserted. Additionally, unconditionally asserting the reset here ignores the earlier logic to keep active hardware running if pwm_en was true.
[ ... ]
Quoted 2 lines
drivers/pwm/pwm-dwc-of.c new file +2
-0+static int dwc_pwm_resume (struct device *dev) +{
[ ... ]
Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7
-0+for (idx =0 ; idx < chip->npwm; idx++) { +dwc_pwm_writel (dwc, dwc->ctx[idx].cnt,DWC_TIM_LD_CNT (idx)); +dwc_pwm_writel (dwc, dwc->ctx[idx].cnt2,DWC_TIM_LD_CNT2 (idx)); +dwc_pwm_writel (dwc, dwc->ctx[idx].ctrl,DWC_TIM_CTRL (idx)); + } + +if (pm_flags) {
drivers/pwm/pwm-dwc-of.c line 313
Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10
-0+/* Balance the refcount taken by pm_runtime_get_sync +* if it was used +*/ + ret =pm_runtime_put_sync (dev); +if (ret <0 ) +return ret; + } + +return 0 ; +}
[Severity: High]
Should a legitimate -EAGAIN from pm_runtime_put_sync() abort the system resume? According to PM subsystem rules, pm_runtime_put_sync() can return -EAGAIN if another thread holds a usage reference. Returning this expected error code from a system resume callback will unexpectedly abort the entire system resume process for the device.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v7 1/2] dt-bindings: pwm: dwc: Add eswin compatible and resets property
In reply to [email protected]Quoted 5 lines · On Fri, Jun 05, 2026 at 04:23:18PM +0800, [email protected] wrote:
From: Xuyang Dong <[email protected]>
EIC7700 use DesignWare IP for PWM controllers. Add ESWIN EIC7700 support in snps,dw-apb-timers-pwm2.yaml.
Quoted 7 lines
The DesignWare PWM includes separate reset signals dedicated to each clock domain:
The presetn signal resets logic in pclk domain.
Thetimer_N_resetnsignal resets logic in thetimer_N_clkdomain. The resets are active-low.The generic snps,dw-apb-timers-pwm2 binding allows one or two optional
I don't know what is the generic binding, but it does not allow. Open the file: there are no resets at all, so it does not allow them. Or you mixed tenses here and you wanted to describe the change?
The present tense describes current state of source code before applying the patch. The patch transform that current state, so you don't use present tense to show what will be future.
Unless you meant here a device, not binding. I would be picky here except that your binding is incorrect which made me looking for answers. I cannot find these answers.
Quoted 49 lines
reset lines depending on SoC integration.
On EIC7700, the presetn and
timer_N_resetninputs are physically tied to a single reset line, therefore exactly one reset is required.Signed-off-by Xuyang Dong <[email protected]>--- .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-)Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +11
-4@@ -20,12 +20,11 @@ description: instead of having to encode the IP version number in the device tree compatible. -allOf: - - $ref: pwm.yaml# - properties: compatible: - const: snps,dw-apb-timers-pwm2 + enum: + - snps,dw-apb-timers-pwm2 + - eswin,eic7700-pwm reg: maxItems: 1@@ -43,6 +42,12 @@ properties: - const: bus - const: timer + resets: + minItems: 1 + items: + - description: Interface bus reset + - description: PWM timer logic reset + snps,pwm-number: $ref: /schemas/types.yaml#/definitions/uint32 description: The number of PWM channels configured for this instance@@ -54,6 +59,22 @@ required: - clocks - clock-names +allOf: + - $ref: pwm.yaml#
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml required: · line 73
Quoted 10 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +10
-0+ + - if: + properties: + compatible: + contains: + const: eswin,eic7700-pwm + then: + properties: + resets: + minItems: 1
Drop
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml required: · line 74
Quoted 1 line
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +1
-0+ maxItems: 1
So you want to add resets for the existing variant claiming that they were missing? Probably we should tell you that earlier, although the patch was going odd directions, but you need to split it. First you fix existing devices with explanation why. Then you add new compatible with constraint for one reset.
Best regards,
Krzysztof
Re: Re: [PATCH v7 1/2] dt-bindings: pwm: dwc: Add eswin compatible and resets property
Quoted 10 lines
Quoted 10 lines
EIC7700 use DesignWare IP for PWM controllers. Add ESWIN EIC7700 support in snps,dw-apb-timers-pwm2.yaml.
The DesignWare PWM includes separate reset signals dedicated to each clock domain:
The presetn signal resets logic in pclk domain.
Thetimer_N_resetnsignal resets logic in thetimer_N_clkdomain. The resets are active-low.
Quoted 6 lines
Quoted 1 line
The generic snps,dw-apb-timers-pwm2 binding allows one or two optional
I don't know what is the generic binding, but it does not allow. Open the file: there are no resets at all, so it does not allow them. Or you mixed tenses here and you wanted to describe the change?
Hi Krzysztof,
Thanks for your comments and time.
Regarding snps,dw-apb-timers-pwm2, we previously intended to add the resets property in the same patch.
However, as you suggested, we will split it into a separate patch.
Quoted 55 lines
The present tense describes current state of source code before applying the patch. The patch transform that current state, so you don't use present tense to show what will be future.
Unless you meant here a device, not binding. I would be picky here except that your binding is incorrect which made me looking for answers. I cannot find these answers.
Quoted 47 lines
reset lines depending on SoC integration.
On EIC7700, the presetn and
timer_N_resetninputs are physically tied to a single reset line, therefore exactly one reset is required.Signed-off-by Xuyang Dong <[email protected]>--- .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-)Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +9
-4@@ -20,12 +20,11 @@ description: instead of having to encode the IP version number in the device tree compatible. -allOf: - - $ref: pwm.yaml# - properties: compatible: - const: snps,dw-apb-timers-pwm2 + enum: + - snps,dw-apb-timers-pwm2 + - eswin,eic7700-pwm reg: maxItems: 1@@ -43,6 +42,12 @@ properties: - const: bus - const: timer + resets: + minItems: 1 + items: + - description: Interface bus reset + - description: PWM timer logic reset + snps,pwm-number: $ref: /schemas/types.yaml#/definitions/uint32 description: The number of PWM channels configured for this instance@@ -54,6 +59,22 @@ required: - clocks - clock-names
Quoted 15 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml required: · line 73
Quoted 12 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +12
-0+allOf: + - $ref: pwm.yaml# + + - if: + properties: + compatible: + contains: + const: eswin,eic7700-pwm + then: + properties: + resets: + minItems: 1Drop
Will fix in the next version.
Quoted 8 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml required: · line 74
Quoted 1 line
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml +1
-0+ maxItems: 1So you want to add resets for the existing variant claiming that they were missing? Probably we should tell you that earlier, although the patch was going odd directions, but you need to split it. First you fix existing devices with explanation why. Then you add new compatible with constraint for one reset.
Based on your comments, we will split this patch into two. One patch explains why we add the resets property, and the other adds the eswin compatible string and the limit for resets.
Best regards,
Xuyang Dong
Re: [PATCH v7 1/2] dt-bindings: pwm: dwc: Add eswin compatible and resets property
Quoted 17 lines · Xuyang Dong
Quoted 10 lines
Quoted 10 lines
EIC7700 use DesignWare IP for PWM controllers. Add ESWIN EIC7700 support in snps,dw-apb-timers-pwm2.yaml.
The DesignWare PWM includes separate reset signals dedicated to each clock domain:
The presetn signal resets logic in pclk domain.
Thetimer_N_resetnsignal resets logic in thetimer_N_clkdomain. The resets are active-low.Quoted 6 lines
Quoted 1 line
The generic snps,dw-apb-timers-pwm2 binding allows one or two optional
I don't know what is the generic binding, but it does not allow. Open the file: there are no resets at all, so it does not allow them. Or you mixed tenses here and you wanted to describe the change?
Quoted 7 lines
Hi Krzysztof,
Thanks for your comments and time.
Regarding snps,dw-apb-timers-pwm2, we previously intended to add the resets property in the same patch.
However, as you suggested, we will split it into a separate patch.
I find commit msg still confusing. Please always clearly explain WHY you are doing changes.
Best regards,
Krzysztof
Re: Re: [PATCH v7 1/2] dt-bindings: pwm: dwc: Add eswin compatible and resets property
Quoted 21 lines · Xuyang Dong
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
EIC7700 use DesignWare IP for PWM controllers. Add ESWIN EIC7700 support in snps,dw-apb-timers-pwm2.yaml.
The DesignWare PWM includes separate reset signals dedicated to each clock domain:
The presetn signal resets logic in pclk domain.
Thetimer_N_resetnsignal resets logic in thetimer_N_clkdomain. The resets are active-low.Quoted 6 lines
Quoted 1 line
The generic snps,dw-apb-timers-pwm2 binding allows one or two optional
I don't know what is the generic binding, but it does not allow. Open the file: there are no resets at all, so it does not allow them. Or you mixed tenses here and you wanted to describe the change?
Hi Krzysztof,
Quoted 9 lines
Quoted 5 lines
Thanks for your comments and time.
Regarding snps,dw-apb-timers-pwm2, we previously intended to add the resets property in the same patch.
However, as you suggested, we will split it into a separate patch.I find commit msg still confusing. Please always clearly explain WHY you are doing changes.
Hi Krzysztof,
Thanks for your comments.
According to the DesignWare IP for PWM controllers manual, it seems to me that Ben's previous submission may have missed the optional resets property.
Therefore, I would like to confirm: should I submit a separate fix patch for the resets property, or include it as a feature patch in this series?
Best regards,
Xuyang Dong
Re: [PATCH v7 1/2] dt-bindings: pwm: dwc: Add eswin compatible and resets property
Quoted 14 lines · Xuyang Dong
Quoted 11 lines
Quoted 7 lines
Hi Krzysztof,
Thanks for your comments and time.
Regarding snps,dw-apb-timers-pwm2, we previously intended to add the resets property in the same patch.
However, as you suggested, we will split it into a separate patch.I find commit msg still confusing. Please always clearly explain WHY you are doing changes.
Hi Krzysztof,
Quoted 8 lines
Thanks for your comments.
According to the DesignWare IP for PWM controllers manual, it seems to me that Ben's previous submission may have missed the optional resets property.
Therefore, I would like to confirm: should I submit a separate fix patch for the resets property, or include it as a feature patch in this series?
One change is adding resets to existing binding for existing device. With its own rationale.
Another change is adding new device compatible with its own necessary changes (if resets are different).
Best regards,
Krzysztof
Re: Re: [PATCH v7 2/2] pwm: dwc: add of/platform support
In reply to [email protected]Quoted 34 lines
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
New issues:
- [High] Registering the PWM chip before completing runtime PM initialization exposes the hardware to consumers while clocks might be turned off.
- [High] Manual hardware teardown in.remove()precedespwm_chipderegistration, leading to potential bus faults.
- [High] TOCTOU race inremove()when conditionally acquiring a runtime PM reference allows concurrent runtime suspend while accessing registers.
- [High] System resume is incorrectly aborted if expected runtime PM idle operations return-EAGAIN.
- [High] Unconditional assertion of the reset control on the probe error path and during module removal permanently kills active hardware and unbalances the reset API.
- [High]remove()artificially drops runtime PM references that belong to active consumers, causing an underflow.
- [Medium] Integer overflow in timer configuration allows invalid large duty cycle/period values to bypass validation.
- [Medium] Clock references are leaked during module removal if the device is held in an active PM state but no PWM channels are enabled.
- [Medium] The driver rounds UP duty cycle and period calculations instead of rounding DOWN, violating PWM subsystem rules.
- [Medium] The driver evaluates the static device tree polarity instead of the dynamically requested runtime polarity, mapping the duty cycle incorrectly.Pre-existing issues:
- [High]dwc_pwm_get_state()ignores the return value ofpm_runtime_get_sync(), allowing unprotected hardware access. --commit
517966ff9e8e50806716056dd4bd3f2f88b832c0
Author: Xuyang Dong <[email protected]>pwm: dwc: add of/platform support
The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
Quoted 4 lines
drivers/pwm/pwm-dwc-core.c +0
-0[ ... ]
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +0
-0@@ -44,21 +45,52 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,[ ... ]
Quoted 15 lines
drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 57
Quoted 7 lines
drivers/pwm/pwm-dwc-core.c +7
-0+if (dwc->features &DWC_TIM_CTRL_0N100PWM_EN ) { +/* +* Calculate width of low and high period in terms of input +* clock periods and check are the result within HW limits +* between 0 and 2^32 periods. +*/ + tmp = state->duty_cycle * dwc->clk_rate;[Severity: Medium]
Could this multiplication overflow the 64-bit boundary before the hardware limit check if a consumer requests an extremely large period? Bothduty_cycleand period are u64 nanoseconds, whileclk_rateis an unsigned long. A wrapped-around small value would evade the hardware limit check entirely. Should this usemul_u64_u64_div_u64()to scale the values safely?
Hi Sashiko,
Use mul_u64_u64_div_u64() to scale the values safely. Fix this issue in the next version.
Quoted 7 lines
drivers/pwm/pwm-dwc-core.c
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +1
-0+ tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );[Severity: Medium]
Does this violate the PWM subsystem rules by rounding up instead of down? The PWM core framework strictly requires that the .apply callback rounds down the requested period and duty cycle to the nearest achievable hardware value.
Fix this issue in the next version with mul_u64_u64_div_u64().
Quoted 6 lines
Quoted 6 lines
drivers/pwm/pwm-dwc-core.c +6
-0+if (tmp >= (1ULL <<32 )) +return -ERANGE ; + +if (pwm->args.polarity ==PWM_POLARITY_INVERSED ) + high = tmp; +else
Quoted 9 lines
drivers/pwm/pwm-dwc-core.c
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +1
-0+ low = tmp;[Severity: Medium]
Should this evaluate the dynamically requestedstate->polarityinstead of the static device tree polarity inpwm->args.polarity? Sincedwc_pwm_apply()enforcesPWM_POLARITY_INVERSED, if the device tree was configured withPWM_POLARITY_NORMAL, this will program the hardware with the inverted duty cycle relative to what the consumer actually requested.
Here, we expect to support both polarity scenarios, PWM_POLARITY_INVERSED and PWM_POLARITY_NORMAL, passed in via the device tree. Therefore, we prefer to keep it. Do you have any thoughts or suggestions?
Quoted 9 lines
[ ... ]
Quoted 8 lines
drivers/pwm/pwm-dwc-core.c +1
-0@@ -121,11 +157,17 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) {struct dwc_pwm *dwc =to_dwc_pwm (chip); +unsigned long clk_rate;u64 duty, period;u32 ctrl, ld, ld2;
Quoted 11 lines
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 164
Quoted 1 line
drivers/pwm/pwm-dwc-core.c +0
-0pm_runtime_get_sync (pwmchip_parent (chip));[Severity: High]
This isn't a bug introduced by this patch, but the return value ofpm_runtime_get_sync()is ignored here. If the function fails (for example, returning-EACCESwhen PM runtime is disabled during driver removal, or before it is enabled in probe), the hardware might not be powered on and clocks could remain gated. The function will then proceed to read theDWC_TIM_CTRLandDWC_TIM_LD_CNTregisters, which can cause a synchronous external abort when accessing unclocked hardware. Can we add a check for the return value here?
Add a check for pm_runtime_get_sync() in the next version.
Quoted 35 lines
Quoted 5 lines
drivers/pwm/pwm-dwc-of.c new file +0
-0[ ... ]
Quoted 2 lines
drivers/pwm/pwm-dwc-of.c new file +2
-0+static int dwc_pwm_plat_probe (struct platform_device *pdev) +{[ ... ]
Quoted 15 lines
drivers/pwm/pwm-dwc-of.c new file +15
-0+ ret =devm_pwmchip_add (dev, chip); +if (ret) { +dev_err (dev,"failed to add pwm chip\n" ); + goto reset_assert; + } + + data->chips[0 ] = chip; +dev_set_drvdata (dev, data); + +/* +* If any PWM channel is enabled, mark device active and hold runtime PM +* references for each enabled channel. Otherwise, gate the clocks. +*/ +if (pwm_en) { +pm_runtime_set_active (dev);drivers/pwm/pwm-dwc-of.c line 149
Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10
-0+for (i =0 ; i < chip->npwm; i++) { +if (ctrl[i]) +pm_runtime_get_noresume (dev); + } + }else { +clk_disable_unprepare (dwc->clk); +clk_disable_unprepare (dwc->bus_clk); + } + +pm_runtime_enable (dev);
Quoted 8 lines
[Severity: High]
Does registering the PWM chip before enabling PM expose the hardware to consumers prematurely?devm_pwmchip_add()is called beforepm_runtime_enable(). If a consumer accesses the PWM during this window, the core driver'spm_runtime_get_sync()call would fail with-EACCES, but still proceed to access registers. At the same time, probe might gate the clocks ifpwm_enis false, leading to a bus fault.
Move devm_pwmchip_add() after pm_runtime_enable().
Quoted 4 lines
Quoted 4 lines
drivers/pwm/pwm-dwc-of.c new file +4
-0+ +return 0 ; + +reset_assert:
Quoted 9 lines
drivers/pwm/pwm-dwc-of.c
Quoted 1 line
drivers/pwm/pwm-dwc-of.c new file +1
-0+reset_control_assert (dwc->rst);[Severity: High]
Does this unconditionally assert the reset control on the error path? The commit message mentions resetting only when no PWM channel is enabled. Ifpwm_enwas true, this forcibly kills actively running hardware. Ifpwm_enwas false, probe usedreset_control_reset()(which is a pulse), so asserting it here leaves the line permanently asserted in an unbalanced state.
Remove reset_assert label and reset_control_assert().
Quoted 11 lines
Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10
-0+disable_clk: +clk_disable_unprepare (dwc->clk); +disable_busclk: +clk_disable_unprepare (dwc->bus_clk); + +return ret; +} + +static void dwc_pwm_plat_remove (struct platform_device *pdev) +{[ ... ]
Quoted 16 lines
drivers/pwm/pwm-dwc-of.c line 178
Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7
-0+/* +* Resume the device if it is runtime suspended to allow +* safe register access. +*/ + pm_flags =pm_runtime_status_suspended (&pdev->dev); +if (pm_flags) +pm_runtime_get_sync (&pdev->dev);[Severity: High]
Is there a race condition when conditionally acquiring this PM reference? Ifpm_runtime_status_suspended()returns false, the code proceeds to read registers without taking a reference. An auto-suspend timer or a concurrent consumer could suspend the device immediately after this check, turning off the clocks and causing the subsequentDWC_TIM_CTRLreads to trigger a bus fault. Shouldpm_runtime_get_sync()be called unconditionally?
Remove pm_runtime_status_suspended() and the state check, and directly use pm_runtime_get_sync() instead.
Quoted 8 lines
drivers/pwm/pwm-dwc-of.c line 185
Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7
-0+ +for (idx =0 ; idx < chip->npwm; idx++) { +if (dwc_pwm_readl (dwc,DWC_TIM_CTRL (idx)) &DWC_TIM_CTRL_EN ) { + pwm_en = true; +pm_runtime_put_noidle (&pdev->dev); + } + }
Quoted 8 lines
[Severity: High]
Does dropping the PM reference here underflow the usage count for active consumers? If a consumer has taken over the PWM channel, it logically owns that reference viadwc_pwm_apply(). Because devres unregisters the PWM chip afterremove()finishes, the PWM core will eventually calldwc_pwm_apply(..., enabled=false), which callspm_runtime_put_sync(). Dropping it here artificially might lead to a usage count underflow.
Remove pwm_en, but keep pm_runtime_put_noidle().
When the PWM is enabled, pm_runtime_get_sync() is called to increase the usage count. When the PWM is disabled, pm_runtime_put_sync() is called to decrease the usage count. However, if the PWM is unloaded directly after being enabled, the PWM remains in the enabled state and the usage count will not be decreased. Therefore, when unloading the device, if the PWM enable bit is still 1, pm_runtime_put_noidle() is used to decrease the usage count.
Quoted 11 lines
Quoted 11 lines
drivers/pwm/pwm-dwc-of.c new file +11
-0+ +/* +* Re-suspend the device if it was runtime suspended prior to +* the register access. +*/ +if (pm_flags) +pm_runtime_put_sync (&pdev->dev); + +if (pwm_en) { +clk_disable_unprepare (dwc->clk); +clk_disable_unprepare (dwc->bus_clk);
Quoted 9 lines
drivers/pwm/pwm-dwc-of.c
Quoted 1 line
drivers/pwm/pwm-dwc-of.c new file +1
-0+ }[Severity: Medium]
Will this leak clock references if the device is active but no PWM channels are enabled? Ifpwm_enis false but the device's PM runtime state isRPM_ACTIVE(due to an open sysfs handle or a pending auto-suspend), skipping this block leaves the clocks on. The subsequentpm_runtime_disable()does not turn off clocks, permanently leaking them.
Use pm_runtime_status_suspended() to check the status. If the device is not suspended (i.e., active), call clk_disable_unprepare(). If it is suspended, skip this block.
Quoted 5 lines
drivers/pwm/pwm-dwc-of.c line 201
Quoted 4 lines
drivers/pwm/pwm-dwc-of.c new file +4
-0+ +pm_runtime_disable (&pdev->dev); +reset_control_assert (dwc->rst); +}
Quoted 8 lines
[Severity: High]
Does manually tearing down the hardware here lead to bus faults during devres cleanup? Since the driver registers the chip usingdevm_pwmchip_add(), devres will unregister the PWM chip afterdwc_pwm_plat_remove()returns. During that window, the device is fully accessible to consumers, but clocks are off and reset is asserted. Additionally, unconditionally asserting the reset here ignores the earlier logic to keep active hardware running ifpwm_enwas true.
Would it be more appropriate to use pwmchip_add() along with an explicit pwmchip_remove() before turning off the clocks, instead of relying on devm_pwmchip_add()? I'm concerned that the devres cleanup might lead to bus faults. What do you think?
Add a check of dwc->rst before calling reset_control_assert().
Quoted 20 lines
[ ... ]
Quoted 2 lines
drivers/pwm/pwm-dwc-of.c new file +2
-0+static int dwc_pwm_resume (struct device *dev) +{[ ... ]
Quoted 16 lines
drivers/pwm/pwm-dwc-of.c new file +16
-0+for (idx =0 ; idx < chip->npwm; idx++) { +dwc_pwm_writel (dwc, dwc->ctx[idx].cnt,DWC_TIM_LD_CNT (idx)); +dwc_pwm_writel (dwc, dwc->ctx[idx].cnt2,DWC_TIM_LD_CNT2 (idx)); +dwc_pwm_writel (dwc, dwc->ctx[idx].ctrl,DWC_TIM_CTRL (idx)); + } + +if (pm_flags) { +/* Balance the refcount taken by pm_runtime_get_sync +* if it was used +*/ + ret =pm_runtime_put_sync (dev); +if (ret <0 ) +return ret; + } + +return 0 ;
Quoted 9 lines
Quoted 1 line
+}
[Severity: High]
Should a legitimate-EAGAINfrompm_runtime_put_sync()abort the system resume? According to PM subsystem rules,pm_runtime_put_sync()can return-EAGAINif another thread holds a usage reference. Returning this expected error code from a system resume callback will unexpectedly abort the entire system resume process for the device.
Drop the check of pm_runtime_put_sync().
Best regards,
Xuyang Dong
Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2