Add OpenCores PTC PWM support
Subsystems (4)
v20 cover Add OpenCores PTC PWM support
Add OpenCores PTC PWM driver which is used in StarFive JH7100/JH7110/JHB100 SoC.
I will maintain this pwm module in place of William.
Changes since v19:
- Drop the oneOf construct in dt-bindings.
- Address Sashiko AI review comments for the OpenCores PWM driver. In ocores_pwm_apply(), handling disable requests first, then calculating and validating period/duty for enable requests before taking a runtime PM reference or touching the hardware registers.
Changes since v18:
- Address Sashiko AI review comments for the OpenCores PWM driver.
- Fix runtime PM usage count handling on probe, error paths, PWM release and driver teardown.
- Reject period or duty cycle values below the hardware minimum.
- Restore PWM registers across system sleep resume.
- Return the real error from devm_pwmchip_alloc().
- Preserve bootloader-configured PWM state during probe and keep runtime PM active if the PWM is already enabled.
- Use synchronous runtime PM put before possible teardown.
Changes since v17:
- Simplify the code. Make it more readable.
- Restructure the driver to register the pwm chip for one pwm channel, because each OpenCores PTC IP core only supports one PWM channel. Drop starfive compatibles.
Add patches to fix the dt-bindings and device tree.
- Support runtime pm and system sleep pm.
- Disable the pwm module and reset the pwm counter before updating the period and duty cycle.
- Improve the descriptions.
- Update the dt-bindings maintainer to Hal Feng.
History:
v19: https://lore.kernel.org/all/[email protected]/
v18: https://lore.kernel.org/all/[email protected]/
v17: https://lore.kernel.org/all/[email protected]/
Hal Feng (3):
dt-bindings: pwm: opencores: Update compatibles, examples and
maintainers
riscv: dts: starfive: Correct pwm nodes
pwm: Add OpenCores PTC PWM driver
.../bindings/pwm/opencores,pwm.yaml | 6 +- MAINTAINERS | 6 + .../boot/dts/starfive/jh7100-common.dtsi | 28 +- arch/riscv/boot/dts/starfive/jh7100.dtsi | 67 +++- .../boot/dts/starfive/jh7110-common.dtsi | 27 +- .../boot/dts/starfive/jh7110-milkv-mars.dts | 6 +- .../dts/starfive/jh7110-milkv-marscm.dtsi | 6 +- .../dts/starfive/jh7110-pine64-star64.dts | 6 +- .../jh7110-starfive-visionfive-2-lite.dtsi | 6 +- .../jh7110-starfive-visionfive-2.dtsi | 6 +- arch/riscv/boot/dts/starfive/jh7110.dtsi | 67 +++- drivers/pwm/Kconfig | 12 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-ocores.c | 309 ++++++++++++++++++ 14 files changed, 529 insertions(+), 24 deletions(-) create mode 100644 drivers/pwm/pwm-ocores.c
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
Signature · 7 lines
2.43.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v20 1/3 dt-bindings: pwm: opencores: Update compatibles, examples and maintainers
Remove the jh8100 compatible since the JH8100 SoC has been canceled and will not be released. Add the jhb100 compatible to replace it.
Change the register size in examples to 0x10, since an OpenCores PTC IP has only 4 32-bit registers: CNTR, HRC, LRC and CTRL.
I will maintain this pwm module in place of William.
--- Documentation/devicetree/bindings/pwm/opencores,pwm.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +3 -3
@@ -7,7 +7,7 @@ $schema: http://devicetree.org/meta-schemas/core.yaml# title: OpenCores PWM controller maintainers: - - William Qiu <[email protected]> + - Hal Feng <[email protected]> description: The OpenCores PTC ip core contains a PWM controller. When operating in PWM
@@ -23,7 +23,7 @@ properties: - enum: - starfive,jh7100-pwm - starfive,jh7110-pwm - - starfive,jh8100-pwm + - starfive,jhb100-pwm - const: opencores,pwm-v1 reg:
@@ -49,7 +49,7 @@ examples: - | pwm@12490000 { compatible = "starfive,jh7110-pwm", "opencores,pwm-v1"; - reg = <0x12490000 0x10000>; + reg = <0x12490000 0x10>; clocks = <&clkgen 181>; resets = <&rstgen 109>; #pwm-cells = <3>;
Signature · 7 lines
2.43.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v20 1/3] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers
pw-bot: not-applicable
Conor Dooley
maintainerRe: [PATCH v20 1/3] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers
In reply to Hal FengQuoted 5 lines · Hal Feng
Remove the jh8100 compatible since the JH8100 SoC has been canceled and will not be released. Add the jhb100 compatible to replace it.
Change the register size in examples to
0x10, since an OpenCores PTC IP has only 4 32-bit registers: CNTR, HRC, LRC and CTRL.
Actually, please also add a fixes tag to the original commit adding the binding.
Cheers,
Conor.
Quoted 42 lines
I will maintain this pwm module in place of William.
Signed-off-by Hal Feng <[email protected]>--- Documentation/devicetree/bindings/pwm/opencores,pwm.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +3
-3@@ -7,7 +7,7 @@ $schema: http://devicetree.org/meta-schemas/core.yaml# title: OpenCores PWM controller maintainers: - - William Qiu <[email protected]> + - Hal Feng <[email protected]> description: The OpenCores PTC ip core contains a PWM controller. When operating in PWM@@ -23,7 +23,7 @@ properties: - enum: - starfive,jh7100-pwm - starfive,jh7110-pwm - - starfive,jh8100-pwm + - starfive,jhb100-pwm - const: opencores,pwm-v1 reg:@@ -49,7 +49,7 @@ examples: - | pwm@12490000 { compatible = "starfive,jh7110-pwm", "opencores,pwm-v1"; - reg = <0x12490000 0x10000>; + reg = <0x12490000 0x10>; clocks = <&clkgen 181>; resets = <&rstgen 109>; #pwm-cells = <3>;Signature · 1 line
2.43.2
Re: [PATCH v20 1/3] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers
Quoted 9 lines · Conor Dooley
Quoted 5 lines · Hal Feng
Remove the jh8100 compatible since the JH8100 SoC has been canceled and will not be released. Add the jhb100 compatible to replace it.
Change the register size in examples to
0x10, since an OpenCores PTC IP has only 4 32-bit registers: CNTR, HRC, LRC and CTRL.Actually, please also add a fixes tag to the original commit adding the binding.
OK. Will add it in the next version. Thanks.
Best regards,
Hal
Quoted 34 lines
Cheers,
Conor.Quoted 30 lines
I will maintain this pwm module in place of William.
Signed-off-by Hal Feng <[email protected]>--- Documentation/devicetree/bindings/pwm/opencores,pwm.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +1
-1@@ -7,7 +7,7 @@ $schema: http://devicetree.org/meta-schemas/core.yaml# title: OpenCores PWM controller maintainers: - - William Qiu <[email protected]> + - Hal Feng <[email protected]> description: The OpenCores PTC ip core contains a PWM controller. When operatingin PWM @@ -23,7 +23,7 @@ properties:
- enum:
- starfive,jh7100-pwm
- starfive,jh7110-pwmDocumentation/devicetree/bindings/pwm/opencores,pwm.yaml +1
-1- - starfive,jh8100-pwm + - starfive,jhb100-pwm - const: opencores,pwm-v1
Quoted 13 lines
Quoted 11 lines
reg:
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +1
-2@@ -49,7 +49,7 @@ examples: - | pwm@12490000 { compatible = "starfive,jh7110-pwm", "opencores,pwm-v1"; - reg = <0x12490000 0x10000>; + reg = <0x12490000 0x10>; clocks = <&clkgen 181>; resets = <&rstgen 109>; #pwm-cells = <3>; --Quoted 2 lines
2.43.2
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Conor Dooley
maintainerRe: [PATCH v20 1/3] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers
Quoted 11 lines · Hal Feng
Quoted 9 lines · Conor Dooley
Quoted 5 lines · Hal Feng
Remove the jh8100 compatible since the JH8100 SoC has been canceled and will not be released. Add the jhb100 compatible to replace it.
Change the register size in examples to
0x10, since an OpenCores PTC IP has only 4 32-bit registers: CNTR, HRC, LRC and CTRL.Actually, please also add a fixes tag to the original commit adding the binding.
OK. Will add it in the next version. Thanks.
If this version is up to scratch on the driver front, providing one in a reply here will probably suffice.
PATCH v20 2/3 riscv: dts: starfive: Correct pwm nodes
In reply to Hal FengEach of the StarFive JH7100/JH7110 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel. Change the register size to 0x10, since an OpenCores PTC IP has only 4 32-bit registers: CNTR, HRC, LRC and CTRL.
--- .../boot/dts/starfive/jh7100-common.dtsi | 28 ++++++-- arch/riscv/boot/dts/starfive/jh7100.dtsi | 67 ++++++++++++++++++- .../boot/dts/starfive/jh7110-common.dtsi | 27 ++++++-- .../boot/dts/starfive/jh7110-milkv-mars.dts | 6 +- .../dts/starfive/jh7110-milkv-marscm.dtsi | 6 +- .../dts/starfive/jh7110-pine64-star64.dts | 6 +- .../jh7110-starfive-visionfive-2-lite.dtsi | 6 +- .../jh7110-starfive-visionfive-2.dtsi | 6 +- arch/riscv/boot/dts/starfive/jh7110.dtsi | 67 ++++++++++++++++++- 9 files changed, 198 insertions(+), 21 deletions(-)
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +22 -6
@@ -199,13 +199,23 @@ GPO_I2C2_PAD_SDA_OEN, }; }; - pwm_pins: pwm-0 { - pwm-pins { + pwm0_pins: pwm0-0 { + pwm0-pins { pinmux = <GPIOMUX(7, GPO_PWM_PAD_OUT_BIT0, GPO_PWM_PAD_OE_N_BIT0, - GPI_NONE)>, - <GPIOMUX(5, + GPI_NONE)>; + bias-disable; + drive-strength = <35>; + input-disable; + input-schmitt-disable; + slew-rate = <0>; + }; + }; + + pwm1_pins: pwm1-0 { + pwm1-pins { + pinmux = <GPIOMUX(5, GPO_PWM_PAD_OUT_BIT1, GPO_PWM_PAD_OE_N_BIT1, GPI_NONE)>;
@@ -359,9 +369,15 @@ &osc_aud { clock-frequency = <27000000>; }; -&pwm { +&pwm0 { + pinctrl-names = "default"; + pinctrl-0 = <&pwm0_pins>; + status = "okay"; +}; + +&pwm1 { pinctrl-names = "default"; - pinctrl-0 = <&pwm_pins>; + pinctrl-0 = <&pwm1_pins>; status = "okay"; };
arch/riscv/boot/dts/starfive/jh7100.dtsi +65 -2
74 lines not on this page · raw message
arch/riscv/boot/dts/starfive/jh7110-common.dtsi +21 -6
@@ -326,9 +326,14 @@ uboot@100000 { }; }; -&pwm { +&pwm0 { pinctrl-names = "default"; - pinctrl-0 = <&pwm_pins>; + pinctrl-0 = <&pwm0_pins>; +}; + +&pwm1 { + pinctrl-names = "default"; + pinctrl-0 = <&pwm1_pins>; }; &spi0 {
@@ -528,12 +533,22 @@ GPOEN_ENABLE, }; }; - pwm_pins: pwm-0 { - pwm-pins { + pwm0_pins: pwm0-0 { + pwm0-pins { pinmux = <GPIOMUX(46, GPOUT_SYS_PWM_CHANNEL0, GPOEN_SYS_PWM0_CHANNEL0, - GPI_NONE)>, - <GPIOMUX(59, GPOUT_SYS_PWM_CHANNEL1, + GPI_NONE)>; + bias-disable; + drive-strength = <12>; + input-disable; + input-schmitt-disable; + slew-rate = <0>; + }; + }; + + pwm1_pins: pwm1-0 { + pwm1-pins { + pinmux = <GPIOMUX(59, GPOUT_SYS_PWM_CHANNEL1, GPOEN_SYS_PWM0_CHANNEL1, GPI_NONE)>; bias-disable;
arch/riscv/boot/dts/starfive/jh7110-milkv-mars.dts +5 -1
@@ -68,7 +68,11 @@ &phy0 { motorcomm,tx-clk-adj-enabled; }; -&pwm { +&pwm0 { + status = "okay"; +}; + +&pwm1 { status = "okay"; };
arch/riscv/boot/dts/starfive/jh7110-milkv-marscm.dtsi +5 -1
@@ -87,7 +87,11 @@ &phy0 { motorcomm,tx-clk-adj-enabled; }; -&pwm { +&pwm0 { + status = "okay"; +}; + +&pwm1 { status = "okay"; };
arch/riscv/boot/dts/starfive/jh7110-pine64-star64.dts +5 -1
@@ -95,7 +95,11 @@ &phy1 { motorcomm,tx-clk-100-inverted; }; -&pwm { +&pwm0 { + status = "okay"; +}; + +&pwm1 { status = "okay"; };
arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2-lite.dtsi +5 -1
@@ -74,7 +74,11 @@ &phy0 { tx-internal-delay-ps = <1500>; }; -&pwm { +&pwm0 { + status = "okay"; +}; + +&pwm1 { status = "okay"; };
arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi +5 -1
@@ -73,7 +73,11 @@ &pcie1 { status = "okay"; }; -&pwm { +&pwm0 { + status = "okay"; +}; + +&pwm1 { status = "okay"; };
arch/riscv/boot/dts/starfive/jh7110.dtsi +65 -2
74 lines not on this page · raw message
Signature · 7 lines
2.43.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v20 3/3 pwm: Add OpenCores PTC PWM driver
In reply to Hal FengAdd PWM driver for OpenCores PTC IP core.
--- MAINTAINERS | 6 + drivers/pwm/Kconfig | 12 ++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-ocores.c | 309 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 328 insertions(+) create mode 100644 drivers/pwm/pwm-ocores.c
MAINTAINERS +6 -0
@@ -20270,6 +20270,12 @@ F: Documentation/i2c/busses/i2c-ocores.rst F: drivers/i2c/busses/i2c-ocores.c F: include/linux/platform_data/i2c-ocores.h +OPENCORES PWM DRIVER +M: Hal Feng <[email protected]> +S: Supported +F: Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +F: drivers/pwm/pwm-ocores.c + OPENRISC ARCHITECTURE M: Jonas Bonn <[email protected]> M: Stefan Kristiansson <[email protected]>
drivers/pwm/Kconfig +12 -0
@@ -546,6 +546,18 @@ config PWM_NTXEC controller found in certain e-book readers designed by the original design manufacturer Netronix. +config PWM_OCORES + tristate "OpenCores PTC PWM support" + depends on HAS_IOMEM && OF + depends on COMMON_CLK + depends on ARCH_STARFIVE || COMPILE_TEST + help + PWM driver for OpenCores PTC IP core. + For details see https://opencores.org/projects/ptc. + + To compile this driver as a module, choose M here: the module + will be called pwm-ocores. + config PWM_OMAP_DMTIMER tristate "OMAP Dual-Mode Timer PWM support" depends on OF
drivers/pwm/Makefile +1 -0
@@ -48,6 +48,7 @@ obj-$(CONFIG_PWM_MICROCHIP_CORE) += pwm-microchip-core.o obj-$(CONFIG_PWM_MTK_DISP) += pwm-mtk-disp.o obj-$(CONFIG_PWM_MXS) += pwm-mxs.o obj-$(CONFIG_PWM_NTXEC) += pwm-ntxec.o +obj-$(CONFIG_PWM_OCORES) += pwm-ocores.o obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
drivers/pwm/pwm-ocores.c new file +309 -0
309 lines not on this page · raw message
Signature · 7 lines
2.43.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
[email protected]
AI reviewRe: [PATCH v20 3/3] pwm: Add OpenCores PTC PWM driver
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Disabling the PWM in the `.free` callback causes state desynchronization, leading to an unclocked…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Disabling the PWM in the .free callback causes state desynchronization, leading to an unclocked register access and system crash or unbalanced PM references on subsequent usage.
- [High] The driver's reported inversed polarity contradicts its disabled state behavior, turning the output ON when consumers try to disable it.
- [High] The driver rejects valid 0% duty cycle requests with -EINVAL.
- [High] Incorrect teardown ordering in ocores_pwm_pm_disable asserts the reset line while the IP clock is disabled.
--
commit fbbb508bc7b51076bfb0020eecff0171850a1774
Author: Hal Feng <[email protected]>
Subject: pwm: Add OpenCores PTC PWM driver
This patch adds a PWM driver for the OpenCores PTC IP core. It supports basic PWM functionality and is intended for StarFive SoCs.
Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +0
-0
[ ... ]
Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +5
-0+static int ocores_pwm_apply (struct pwm_chip *chip, +struct pwm_device *pwm, +const struct pwm_state *state) +{ +struct ocores_pwm_device *ddata =pwmchip_get_drvdata (chip);
drivers/pwm/pwm-ocores.c line 85
Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10
-0+bool was_enabled =pwm_is_enabled (pwm); +u64 period_data, duty_data; +int ret; + +if (state->polarity !=PWM_POLARITY_INVERSED ) +return -EINVAL ; + +if (!state->enabled) { +if (was_enabled) { +writel (0 , ddata->base +OCPWM_CTRL );
[Severity: High]
Does this drive the output to the active state when the PWM is disabled?
ocores_pwm_get_state() hardcodes the polarity to PWM_POLARITY_INVERSED. Under inverted polarity, the active state is LOW and the inactive state is HIGH. The PWM framework requires that when a PWM is disabled, it must emit its inactive state.
However, writing 0 to OCPWM_CTRL immediately sets the output to LOW (the active state). This means disabling the PWM results in a 100% duty cycle instead of 0%.
Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +5
-0+pm_runtime_put (pwmchip_parent (chip)); + } +return 0 ; + } +
drivers/pwm/pwm-ocores.c line 100
Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10
-0+ period_data =mul_u64_u32_div (state->period, ddata->clk_rate,NSEC_PER_SEC ); +if (period_data >U32_MAX ) + period_data =U32_MAX ; + + duty_data =mul_u64_u32_div (state->duty_cycle, ddata->clk_rate,NSEC_PER_SEC ); +if (duty_data >U32_MAX ) + duty_data =U32_MAX ; + +if (!period_data || !duty_data) +return -EINVAL ;
[Severity: High]
Does this code reject a valid 0% duty cycle request?
A 0% duty cycle is a fully valid configuration in the PWM subsystem, often used by consumers like pwm-backlight to dim the output to zero without fully disabling the PWM. Returning -EINVAL when duty_data evaluates to 0 breaks these consumers.
drivers/pwm/pwm-ocores.c line 106
Quoted 6 lines
drivers/pwm/pwm-ocores.c new file +6
-0+ +if (!was_enabled) { + ret =pm_runtime_resume_and_get (pwmchip_parent (chip)); +if (ret <0 ) +return ret; + }
[Severity: High]
Can this logic write to hardware registers while the clock is disabled?
If a consumer frees the PWM, ocores_pwm_free() disables the hardware and drops the PM runtime reference. However, the PWM framework does not update the cached pwm->state.enabled when a PWM is freed.
If the PWM is subsequently requested again and ocores_pwm_apply() is called, was_enabled (which comes from pwm_is_enabled(pwm)) will still evaluate to true. If the new consumer sets state->enabled to true, it will skip pm_runtime_resume_and_get() and proceed to write directly to the PWM registers with the IP clock disabled, resulting in a synchronous external abort or crash. Alternatively, if the new consumer sets enabled to false, it will call pm_runtime_put(), leading to a usecount underflow.
Quoted 4 lines
drivers/pwm/pwm-ocores.c new file +4
-0+ +writel (0 , ddata->base +OCPWM_CTRL ); +writel (OCPWM_CTRL_RST , ddata->base +OCPWM_CTRL ); +
[ ... ]
Quoted 6 lines
drivers/pwm/pwm-ocores.c new file +6
-0+static void ocores_pwm_pm_disable (void *data) +{ +struct device *dev = data; +struct ocores_pwm_device *ddata =dev_get_drvdata (dev); + +pm_runtime_disable (dev);
drivers/pwm/pwm-ocores.c line 216
Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10
-0+ +if (!pm_runtime_status_suspended (dev)) { +/* Balance probe's pm_runtime_get_noresume() for bootloader-enabled PWM. */ +if (readl (ddata->base +OCPWM_CTRL ) &OCPWM_CTRL_EN ) +pm_runtime_put_noidle (dev); + +ocores_pwm_runtime_suspend (dev); + } + +reset_control_assert (ddata->rst);
[Severity: High]
Does this ordering assert the reset signal while the IP clock is disabled?
The code calls ocores_pwm_runtime_suspend() (which disables the clock) before calling reset_control_assert(). Asserting a reset line while the corresponding IP clock is disabled can cause bus hangs on APB buses or fail to propagate the reset correctly into the hardware block.
The error path in ocores_pwm_probe() demonstrates the correct teardown ordering by asserting the reset before suspending the runtime PM.
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v20 3/3] pwm: Add OpenCores PTC PWM driver
Quoted 43 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Disabling the PWM in the.freecallback causes state desynchronization, leading to an unclocked register access and system crash or unbalanced PM references on subsequent usage.
- [High] The driver's reported inversed polarity contradicts its disabled state behavior, turning the output ON when consumers try to disable it.
- [High] The driver rejects valid 0% duty cycle requests with-EINVAL.
- [High] Incorrect teardown ordering inocores_pwm_pm_disableasserts the reset line while the IP clock is disabled.
--commit
fbbb508bc7b51076bfb0020eecff0171850a1774
Author: Hal Feng <[email protected]>
Subject: pwm: Add OpenCores PTC PWM driverThis patch adds a PWM driver for the OpenCores PTC IP core. It supports basic PWM functionality and is intended for StarFive SoCs.
Quoted 4 lines
drivers/pwm/pwm-ocores.c new renamed from drivers/pwm/pwm-ocores.c +0
-0file mode 100644 index 0000000000000..e01494a336335
drivers/pwm/pwm-ocores.c new file +0
-0[ ... ]
Quoted 4 lines
drivers/pwm/pwm-ocores.c new file +4
-0+static int ocores_pwm_apply (struct pwm_chip *chip, +struct pwm_device *pwm, +const struct pwm_state *state) { +struct ocores_pwm_device *ddata =pwmchip_get_drvdata (chip);drivers/pwm/pwm-ocores.c line 85
Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10
-0+bool was_enabled =pwm_is_enabled (pwm); +u64 period_data, duty_data; +int ret; + +if (state->polarity !=PWM_POLARITY_INVERSED ) +return -EINVAL ; + +if (!state->enabled) { +if (was_enabled) { +writel (0 , ddata->base +OCPWM_CTRL );[Severity: High]
Does this drive the output to the active state when the PWM is disabled?
Quoted 8 lines
ocores_pwm_get_state()hardcodes the polarity toPWM_POLARITY_INVERSED. Under inverted polarity, the active state is LOW and the inactive state is HIGH. The PWM framework requires that when a PWM is disabled, it must emit its inactive state.However, writing 0 to
OCPWM_CTRLimmediately sets the output to LOW (the active state). This means disabling the PWM results in a 100% duty cycle instead of 0%.
The disabled output level is a hardware limitation and is documented in the driver. The PWM framework documentation says consumers should not rely on the output state of a disabled PWM; consumers that require the inactive state should request duty_cycle = 0 with enabled = true.
Quoted 22 lines
drivers/pwm/pwm-ocores.c
Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +6
-0+pm_runtime_put (pwmchip_parent (chip)); + } +return 0 ; + } + + period_data = mul_u64_u32_div(state->period, ddata->clk_rate,
NSEC_PER_SEC);drivers/pwm/pwm-ocores.c
Quoted 4 lines
drivers/pwm/pwm-ocores.c new file +4
-0+if (period_data >U32_MAX ) + period_data =U32_MAX ; + + duty_data = mul_u64_u32_div(state->duty_cycle, ddata->clk_rate,
NSEC_PER_SEC);drivers/pwm/pwm-ocores.c line 100
Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +5
-0+if (duty_data >U32_MAX ) + duty_data =U32_MAX ; + +if (!period_data || !duty_data) +return -EINVAL ;[Severity: High]
Does this code reject a valid 0% duty cycle request?
Quoted 3 lines
A 0% duty cycle is a fully valid configuration in the PWM subsystem, often used by consumers like pwm-backlight to dim the output to zero without fully disabling the PWM. Returning
-EINVALwhenduty_dataevaluates to 0 breaks these consumers.
Good catch. Will fix it in the next version.
Quoted 15 lines
drivers/pwm/pwm-ocores.c line 106
Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +6
-0+ +if (!was_enabled) { + ret =pm_runtime_resume_and_get (pwmchip_parent (chip)); +if (ret <0 ) +return ret; + }[Severity: High]
Can this logic write to hardware registers while the clock is disabled?If a consumer frees the PWM,
ocores_pwm_free()disables the hardware and drops the PM runtime reference. However, the PWM framework does not update the cachedpwm->state.enabledwhen a PWM is freed.
Quoted 7 lines
If the PWM is subsequently requested again and
ocores_pwm_apply()is called,was_enabled(which comes frompwm_is_enabled(pwm)) will still evaluate to true. If the new consumer setsstate->enabledto true, it will skippm_runtime_resume_and_get()and proceed to write directly to the PWM registers with the IP clock disabled, resulting in a synchronous external abort or crash. Alternatively, if the new consumer sets enabled to false, it will callpm_runtime_put(), leading to a usecount underflow.
I don't think this issue applies here. The driver implements .get_state(), and the PWM core calls pwm_get_state_hw() during pwm_device_request() to refresh pwm->state from hardware. After .free() clears OCPWM_CTRL, a subsequent request observes state.enabled = false, so .apply() will not skip runtime resume based on stale state.
Quoted 26 lines
Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +4
-0+ +writel (0 , ddata->base +OCPWM_CTRL ); +writel (OCPWM_CTRL_RST , ddata->base +OCPWM_CTRL ); +[ ... ]
drivers/pwm/pwm-ocores.c
Quoted 8 lines
drivers/pwm/pwm-ocores.c new file +8
-0+static void ocores_pwm_pm_disable (void *data) { +struct device *dev = data; +struct ocores_pwm_device *ddata =dev_get_drvdata (dev); + +pm_runtime_disable (dev); + +if (!pm_runtime_status_suspended (dev)) { +/* Balance probe's pm_runtime_get_noresume() for bootloader-enabled PWM. */
drivers/pwm/pwm-ocores.c line 216
Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +7
-0+if (readl (ddata->base +OCPWM_CTRL ) &OCPWM_CTRL_EN ) +pm_runtime_put_noidle (dev); + +ocores_pwm_runtime_suspend (dev); + } + +reset_control_assert (ddata->rst);[Severity: High]
Does this ordering assert the reset signal while the IP clock is disabled?
Quoted 7 lines
The code calls
ocores_pwm_runtime_suspend()(which disables the clock) before callingreset_control_assert(). Asserting a reset line while the corresponding IP clock is disabled can cause bus hangs on APB buses or fail to propagate the reset correctly into the hardware block.The error path in
ocores_pwm_probe()demonstrates the correct teardown ordering by asserting the reset before suspending the runtime PM.
For this hardware, the reset signal can be asserted regardless of whether the IP clock is enabled.
Best regards,
Hal
Quoted 7 lines
Quoted 2 lines
+}
--
Sashiko AI
review · https://sashiko.dev/#/patchset/20260629063601.63917-1-hal.feng@s
tarfivetech.com?part=3