Skip to contentSkip to conversation

Add OpenCores PTC PWM support

PATCH Superseded Hal Feng 3 participants 14 messages May 15 – Jun 3

Subsystems (4)

v18 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
author
#11 lore raw

PATCH v18 3/3 pwm: Add OpenCores PTC PWM driver

In reply to Hal Feng

Add PWM driver for OpenCores PTC IP core.

---
 MAINTAINERS              |   6 +
 drivers/pwm/Kconfig      |  12 ++
 drivers/pwm/Makefile     |   1 +
 drivers/pwm/pwm-ocores.c | 249 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 268 insertions(+)
 create mode 100644 drivers/pwm/pwm-ocores.c
MAINTAINERS +6 -0
index 6aa3fe2ee1bb..14af609f4ada 100644
@@ -20027,6 +20027,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
index 6f3147518376..dd7f3bf5c3eb 100644
@@ -534,6 +534,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
index 0dc0d2b69025..2d47bad7bd74 100644
@@ -47,6 +47,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 +249 -0

249 lines not on this page · raw message

Signature · 1 line
2.43.2

Hal Feng

author
#0 lore raw

v18 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 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:
v17: https://lore.kernel.org/all/[email protected]/

Hal Feng (3):
dt-bindings: pwm: opencores: Drop starfive compatibles and update
maintainers
riscv: dts: starfive: Correct pwm nodes
pwm: Add OpenCores PTC PWM driver

 .../bindings/pwm/opencores,pwm.yaml           |  10 +-
 MAINTAINERS                                   |   6 +
 .../boot/dts/starfive/jh7100-common.dtsi      |  28 +-
 arch/riscv/boot/dts/starfive/jh7100.dtsi      |  69 ++++-
 .../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      |  69 ++++-
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-ocores.c                      | 249 ++++++++++++++++++
 14 files changed, 471 insertions(+), 30 deletions(-)
 create mode 100644 drivers/pwm/pwm-ocores.c

base-commit: 66182ca873a4e87b3496eca79d57f86b76d7f52d

Signature · 7 lines
2.43.2


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#1 lore raw

PATCH v18 1/3 dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

Each of the StarFive JH7100/JH7110/JH8100 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel. The only difference among them is the register base address. There is no need to add starfive compatibles to distinguish them.

I will maintain the pwm module in place of William.

---
 .../devicetree/bindings/pwm/opencores,pwm.yaml         | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +3 -7
index 52a59d245cdb..834fb17ec595 100644
@@ -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
@@ -20,10 +20,6 @@ allOf:
 properties:
   compatible:
     items:
-      - enum:
-          - starfive,jh7100-pwm
-          - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1
 
   reg:
@@ -48,8 +44,8 @@ additionalProperties: false
 examples:
   - |
     pwm@12490000 {
-        compatible = "starfive,jh7110-pwm", "opencores,pwm-v1";
-        reg = <0x12490000 0x10000>;
+        compatible = "opencores,pwm-v1";
+        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
author
#6 lore raw

PATCH v18 2/3 riscv: dts: starfive: Correct pwm nodes

In reply to Hal Feng

Each of the StarFive JH7100/JH7110 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel.

---
 .../boot/dts/starfive/jh7100-common.dtsi      | 28 ++++++--
 arch/riscv/boot/dts/starfive/jh7100.dtsi      | 69 ++++++++++++++++++-
 .../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      | 69 ++++++++++++++++++-
 9 files changed, 200 insertions(+), 23 deletions(-)
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +22 -6
index ae1a6aeb0aea..85106545090e 100644
@@ -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 +66 -3

75 lines not on this page · raw message

arch/riscv/boot/dts/starfive/jh7110-common.dtsi +21 -6
index 8cfe8033305d..5aa225b8bca8 100644
@@ -351,9 +351,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 {
@@ -553,12 +558,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
index 21873612d993..54013c70f4b4 100644
@@ -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
index 025471061d43..31afac27b86d 100644
@@ -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
index aec7ae3d1f5b..a9e82f25efde 100644
@@ -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
index f8797a666dbf..85b56a72dff7 100644
@@ -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
index edc8f4588133..35208f95cd3d 100644
@@ -73,7 +73,11 @@ &pcie1 {
 	status = "okay";
 };
 
-&pwm {
+&pwm0 {
+	status = "okay";
+};
+
+&pwm1 {
 	status = "okay";
 };
 
arch/riscv/boot/dts/starfive/jh7110.dtsi +66 -3

75 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
#2 lore raw

Re: [PATCH v18 1/3] dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

In reply to Hal Feng
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Removing existing StarFive compatible strings breaks DT schema validation for existing device…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Removing existing StarFive compatible strings breaks DT schema validation for existing device trees.
- [Medium] The commit silently modifies the reg space size in the schema example.
- [Low] The compatible schema contains an awkward single-item list structure after modification. --

commit c3152a2a38b5f4d3a25df0696646b840e0d04b60
Author: Hal Feng <[email protected]>

dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

Each of the StarFive SoCs has 8 OpenCores PTC IP cores that only differ by their register base address. There is no need for starfive compatibles to distinguish them, so they are dropped.

Quoted 4 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +0 -0
index 52a59d245cdb1..834fb17ec595e 100644

[ ... ]

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml allOf: · line 23

Quoted 9 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +0 -4
@@ -20,10 +20,6 @@ allOf:
 properties:
   compatible:
     items:
-      - enum:
-          - starfive,jh7100-pwm
-          - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1

Does removing the legacy compatible strings break dtbs_check for existing device trees? Older device trees likely still use the two-string format.

Would it be better to use a oneOf construct to allow both the legacy two-string format and the new single-string format to maintain backward compatibility?

This isn't a bug, but leaving a single element under items is an anti-pattern in dt-schema. Since only one string is allowed now, could this be simplified to just a direct const property instead of an items array?

[ ... ]

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml additionalProperties: false · line 48

Quoted 8 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +2 -2
@@ -48,8 +44,8 @@ additionalProperties: false
 examples:
   - |
     pwm@12490000 {
-        compatible = "starfive,jh7110-pwm", "opencores,pwm-v1";
-        reg = <0x12490000 0x10000>;
+        compatible = "opencores,pwm-v1";
+        reg = <0x12490000 0x10>;

The commit message mentions dropping the starfive compatibles and updating maintainers, but does not document changing the register space size from 0x10000 to 0x10.

Could the commit message be updated to explicitly explain and justify this register size change?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

Re: [PATCH v18 2/3] riscv: dts: starfive: Correct pwm nodes

In reply to Hal Feng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Pinmux configuration properties were erroneously omitted from `pwm1_pins` in `jh7100-common.dtsi`.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Pinmux configuration properties were erroneously omitted from pwm1_pins in jh7100-common.dtsi.
- [Medium] Removal of SoC-specific compatible strings violates Device Tree ABI best practices. --

commit e05af83398a49d79a54da1bad8e64c7c1e5f98eb
Author: Hal Feng <[email protected]>

riscv: dts: starfive: Correct pwm nodes

This commit updates the PWM nodes for StarFive JH7100 and JH7110 SoCs, splitting the original single PWM node into 8 individual nodes representing the 8 OpenCores PTC IP cores.

Quoted 3 lines
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +0 -0

[ ... ]

arch/riscv/boot/dts/starfive/jh7100-common.dtsi GPO_I2C2_PAD_SDA_OEN,

Quoted 8 lines
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +3 -0
+	pwm1_pins: pwm1-0 {
+		pwm1-pins {
+			pinmux =  <GPIOMUX(5,
 				  GPO_PWM_PAD_OUT_BIT1,
 				  GPO_PWM_PAD_OE_N_BIT1,
 				  GPI_NONE)>;
 		};
 	};

In the original code, the single pwm_pins node applied electrical properties like bias-disable, drive-strength = <35>, input-disable, input-schmitt-disable, and slew-rate = <0> to both PWM0 and PWM1 pins. When splitting the node, these properties were copied to pwm0_pins but omitted here.

Will omitting these properties cause the pwm1 output to operate with undefined or default hardware pin states instead of the intended parameters?

Quoted 3 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +0 -0

[ ... ]

arch/riscv/boot/dts/starfive/jh7100.dtsi watchdog@12480000 { · line 364

Quoted 5 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +2 -3
-		pwm: pwm@12490000 {
-			compatible = "starfive,jh7100-pwm", "opencores,pwm-v1";
-			reg = <0x0 0x12490000 0x0 0x10000>;
+		pwm0: pwm@12490000 {
+			compatible = "opencores,pwm-v1";

Is it intentional to drop the SoC-specific "starfive,jh7100-pwm" compatible string here, as well as "starfive,jh7110-pwm" in jh7110.dtsi?

Device Tree best practices generally recommend retaining SoC-specific strings alongside generic IP strings. This ensures that if SoC-specific integration quirks are discovered later, workarounds can be applied without requiring users to update deployed device trees, and it maintains ABI compatibility for older kernels that might match against the removed strings.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#12 lore raw

Re: [PATCH v18 3/3] pwm: Add OpenCores PTC PWM driver

In reply to Hal Feng
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Probe fails unconditionally when CONFIG_PM is disabled. - [High] Teardown symmetry violation in…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Probe fails unconditionally when CONFIG_PM is disabled.
- [High] Teardown symmetry violation in ocores_pwm_pm_disable() leads to reset assertion without an active clock.
- [High] Race condition between pm_runtime_enable() and pm_runtime_get_noresume() allows premature hardware access without a clock.
- [High] Missing context restore on system resume leads to silently broken PWM output.
- [High] Runtime PM usage count leak permanently blocks PM suspend if the driver is unbound while the PWM is enabled.
- [Medium] Subsystem rule violation: Using asynchronous pm_runtime_put() before pm_runtime_disable() causes race conditions.
- [Medium] Hardware minimum period and duty cycle limitations are not enforced, allowing invalid configuration.
- [Medium] Unconditional hardware disable in probe() breaks bootloader handover.
- [Low] Masking of true error codes from devm_pwmchip_alloc().
--

commit 2a9909cb21b11044209c8ae47fbb6d140786ac45
Author: Hal Feng <[email protected]>
Subject: pwm: Add OpenCores PTC PWM driver

This commit adds a new PWM driver for the OpenCores PTC IP core.

Quoted 2 lines
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)
+{

[ ... ]

drivers/pwm/pwm-ocores.c line 84

Quoted 6 lines
drivers/pwm/pwm-ocores.c new file +6 -0
+	if (state->enabled) {
+		if (!pwm_is_enabled(pwm)) {
+			ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
+			if (ret < 0)
+				return ret;
+		}

Does this leak the runtime PM usage count if the driver is unbound while the PWM is enabled?

If the driver is unbound via sysfs while the PWM is actively running, the PWM core marks the chip non-operational but does not disable the running PWM hardware. The devres teardown action ocores_pwm_pm_disable() will run and forcefully turn off the hardware, but it doesn't decrement the elevated PM usage counter.

Since the struct device object persists across bind/unbind cycles, could this leaked usage count permanently prevent the device from entering runtime suspend on subsequent binds?

drivers/pwm/pwm-ocores.c line 96

Quoted 12 lines
drivers/pwm/pwm-ocores.c new file +12 -0
+	} else {
+		if (pwm_is_enabled(pwm)) {
+			writel(0, ddata->base + OCPWM_CTRL);
+			pm_runtime_put(pwmchip_parent(chip));
+		}
+		return 0;
+	}
+
+	writel(0, ddata->base + OCPWM_CTRL);
+	writel(OCPWM_CTRL_RST, ddata->base + OCPWM_CTRL);
+
+	period_data = mul_u64_u32_div(state->period, ddata->clk_rate, NSEC_PER_SEC);

The header comment notes the hardware minimum period and duty cycle is 1 clock cycle. If the requested state->period is smaller than one clock cycle, wouldn't mul_u64_u32_div() silently truncate the result to 0?

Writing 0 to the OCPWM_LRC and OCPWM_HRC registers violates this hardware constraint and might cause undefined IP behavior. Should the driver validate that period_data >= 1 and return -EINVAL if the request cannot be satisfied?

Quoted 2 lines
drivers/pwm/pwm-ocores.c new file +2 -0
+	if (period_data > U32_MAX)
+		period_data = U32_MAX;

[ ... ]

Quoted 8 lines
drivers/pwm/pwm-ocores.c new file +8 -0
+static int ocores_pwm_runtime_resume(struct device *dev)
+{
+	struct ocores_pwm_device *ddata = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_prepare_enable(ddata->clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable clock\n");

drivers/pwm/pwm-ocores.c line 142

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+
+	return 0;
+}
+
+static const struct dev_pm_ops ocores_pwm_pm_ops = {
+	RUNTIME_PM_OPS(ocores_pwm_runtime_suspend,
+		       ocores_pwm_runtime_resume, NULL)
+	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+			    pm_runtime_force_resume)
+};

Will this silently break PWM output upon resuming from system sleep?

During system suspend, if the device's power domain is powered off, the hardware register contents are typically lost.

The ocores_pwm_runtime_resume() callback only re-enables the clock; it doesn't save and restore the PWM configuration registers. If a PWM was actively outputting a signal before system suspend, will it fail to output the correct signal after the system wakes up?

drivers/pwm/pwm-ocores.c line 155

Quoted 12 lines
drivers/pwm/pwm-ocores.c new file +12 -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))
+		ocores_pwm_runtime_suspend(dev);
+
+	reset_control_assert(ddata->rst);
+}

Could asserting the reset while the clock is turned off cause issues?

In ocores_pwm_probe(), the hardware initialization order is strictly clock enable followed by reset deassertion. The explicit probe error path also respects this by reversing the order, asserting reset before disabling the clock.

However, ocores_pwm_pm_disable() calls ocores_pwm_runtime_suspend() (which turns off the clock) before calling reset_control_assert(). If the IP requires an active clock to propagate the reset signal, asserting the reset while the clock is turned off might fail or hang the bus.

drivers/pwm/pwm-ocores.c line 166

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+static int ocores_pwm_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct ocores_pwm_device *ddata;
+	struct pwm_chip *chip;
+	int ret;
+
+	chip = devm_pwmchip_alloc(dev, 1, sizeof(*ddata));
+	if (IS_ERR(chip))
+		return -ENOMEM;

Should this return PTR_ERR(chip) instead of hardcoding -ENOMEM?

devm_pwmchip_alloc() returns an ERR_PTR that can propagate various error codes, and returning -ENOMEM masks the true failure reason.

Quoted 2 lines
drivers/pwm/pwm-ocores.c new file +2 -0
+	chip->ops = &ocores_pwm_ops;
+	ddata = pwmchip_get_drvdata(chip);

[ ... ]

drivers/pwm/pwm-ocores.c line 203

Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +7 -0
+	ret = reset_control_deassert(ddata->rst);
+	if (ret)
+		goto err_clk_disable;
+
+	ret = pm_runtime_set_active(dev);
+	if (ret)
+		goto err_reset_assert;

Does this fail probe unconditionally when CONFIG_PM is disabled?

When CONFIG_PM is disabled, the dummy implementation of pm_runtime_set_active() returns 1. The if (ret) check evaluates to true when ret = 1, causing the probe function to jump to the error path.

Should this check if (ret < 0) to distinguish PM framework errors from successful status returns?

drivers/pwm/pwm-ocores.c line 213

Quoted 9 lines
drivers/pwm/pwm-ocores.c new file +9 -0
+	pm_runtime_enable(dev);
+
+	ret = devm_add_action_or_reset(dev, ocores_pwm_pm_disable, dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to add pm disable action\n");
+
+	pm_runtime_get_noresume(dev);
+
+	writel(0, ddata->base + OCPWM_CTRL);

Are there a couple of issues with this sequence?

First, pm_runtime_enable() is called while the runtime PM usage counter is 0. This opens a window where the PM core can asynchronously suspend the device. The driver then calls pm_runtime_get_noresume(), which increments the usage counter but does not resume the device if it was already suspended.

If the device was suspended during this window, the subsequent writel() would access hardware registers while the clock is turned off. Should pm_runtime_get_noresume() be called before pm_runtime_enable()?

Second, does this unconditional writel(0, ...) break bootloader handover? If the bootloader previously configured and enabled the PWM, this write will instantly power it down. Could this cause hardware state disruption until the consumer driver re-enables it?

drivers/pwm/pwm-ocores.c line 219

Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +5 -0
+	pm_runtime_put(dev);
+
+	ret = devm_pwmchip_add(dev, chip);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Could not register PWM chip\n");

Is it safe to use the asynchronous pm_runtime_put() here?

If devm_pwmchip_add() fails, the devres teardown action ocores_pwm_pm_disable() runs immediately and invokes pm_runtime_disable(). This forcefully cancels the pending asynchronous suspend work from pm_runtime_put(), leaving the device in an undefined PM state.

The Power Management subsystem guidelines dictate that pm_runtime_put_sync() must be used instead of pm_runtime_put() when pm_runtime_disable() might follow immediately.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer NAK
#4 lore raw

Re: [PATCH v18 1/3] dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

In reply to Hal Feng
Quoted 4 lines · Hal Feng

Each of the StarFive JH7100/JH7110/JH8100 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel. The only difference among them is the register base address. There is no need to add starfive compatibles to distinguish them.

NAK, that's not an acceptable reason to remove soc-specific compatibles.

pw-bot: changes-requested

Quoted 46 lines

I will maintain the pwm module in place of William.

---
 .../devicetree/bindings/pwm/opencores,pwm.yaml         | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +3 -7
index 52a59d245cdb..834fb17ec595 100644
@@ -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
@@ -20,10 +20,6 @@ allOf:
 properties:
   compatible:
     items:
-      - enum:
-          - starfive,jh7100-pwm
-          - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1
 
   reg:
@@ -48,8 +44,8 @@ additionalProperties: false
 examples:
   - |
     pwm@12490000 {
-        compatible = "starfive,jh7110-pwm", "opencores,pwm-v1";
-        reg = <0x12490000 0x10000>;
+        compatible = "opencores,pwm-v1";
+        reg = <0x12490000 0x10>;
         clocks = <&clkgen 181>;
         resets = <&rstgen 109>;
         #pwm-cells = <3>;
Signature · 1 line
2.43.2
maintainer
#9 lore raw

Re: [PATCH v18 2/3] riscv: dts: starfive: Correct pwm nodes

In reply to Hal Feng
Quoted 71 lines · Hal Feng

Each of the StarFive JH7100/JH7110 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel.

---
 .../boot/dts/starfive/jh7100-common.dtsi      | 28 ++++++--
 arch/riscv/boot/dts/starfive/jh7100.dtsi      | 69 ++++++++++++++++++-
 .../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      | 69 ++++++++++++++++++-
 9 files changed, 200 insertions(+), 23 deletions(-)
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +22 -6
index ae1a6aeb0aea..85106545090e 100644
@@ -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 +0 -0
index 7de0732b8eab..4629e9747307 100644

arch/riscv/boot/dts/starfive/jh7100.dtsi watchdog@12480000 { · line 365

Quoted 9 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +3 -3
@@ -360,9 +360,72 @@ watchdog@12480000 {
 				 <&rstgen JH7100_RSTN_WDT>;
 		};
 
-		pwm: pwm@12490000 {
-			compatible = "starfive,jh7100-pwm", "opencores,pwm-v1";
-			reg = <0x0 0x12490000 0x0 0x10000>;
+		pwm0: pwm@12490000 {
+			compatible = "opencores,pwm-v1";
+			reg = <0x0 0x12490000 0x0 0x10>;

NAK on the compatibles front, but this also looks very suspect, given the size of the register regions, but I think it is actually correct. You need to explain why it is correct in the commit message.

author
#5 lore raw

Re: [PATCH v18 1/3] dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

In reply to Conor Dooley
Quoted 8 lines · Conor Dooley
Quoted 4 lines · Hal Feng

Each of the StarFive JH7100/JH7110/JH8100 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel. The only difference among them is the register base address. There is no need to add starfive compatibles to distinguish them.

NAK, that's not an acceptable reason to remove soc-specific compatibles.

Got it, because it will break Device Tree ABI.

Best regards,
Hal

Quoted 51 lines

pw-bot: changes-requested

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml $schema:

Quoted 14 lines

I will maintain the pwm module in place of William.

---
 .../devicetree/bindings/pwm/opencores,pwm.yaml         | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +0 -0
index 52a59d245cdb..834fb17ec595 100644
@@ -7,7 +7,7 @@ $schema:

http://devicetree.org/meta-schemas/core.yaml#

Quoted 33 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +1 -1
 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 @@ -20,10 +20,6 @@ allOf:
properties:
compatible:
items:

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +2 -7
-      - enum:
-          - starfive,jh7100-pwm
-          - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1
 
   reg:
@@ -48,8 +44,8 @@ additionalProperties: false
 examples:
   - |
     pwm@12490000 {
-        compatible = "starfive,jh7110-pwm", "opencores,pwm-v1";
-        reg = <0x12490000 0x10000>;
+        compatible = "opencores,pwm-v1";
+        reg = <0x12490000 0x10>;
         clocks = <&clkgen 181>;
         resets = <&rstgen 109>;
         #pwm-cells = <3>;
--

2.43.2

author
#10 lore raw

Re: [PATCH v18 2/3] riscv: dts: starfive: Correct pwm nodes

In reply to Conor Dooley
Quoted 78 lines · Conor Dooley
Quoted 76 lines · Hal Feng

Each of the StarFive JH7100/JH7110 SoCs has 8 OpenCores PTC IP cores. One OpenCores PTC IP core can output one PWM channel.

---
 .../boot/dts/starfive/jh7100-common.dtsi      | 28 ++++++--
 arch/riscv/boot/dts/starfive/jh7100.dtsi      | 69 ++++++++++++++++++-
 .../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      | 69 ++++++++++++++++++-
 9 files changed, 200 insertions(+), 23 deletions(-)
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +22 -6
index ae1a6aeb0aea..85106545090e 100644
@@ -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 +0 -0
index 7de0732b8eab..4629e9747307 100644
@@ -360,9 +360,72 @@ watchdog@12480000 {
 				 <&rstgen JH7100_RSTN_WDT>;
 		};
 
Quoted 10 lines

arch/riscv/boot/dts/starfive/jh7100.dtsi watchdog@12480000 { · line 365

Quoted 6 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +3 -3
-		pwm: pwm@12490000 {
-			compatible = "starfive,jh7100-pwm", "opencores,pwm-v1";
-			reg = <0x0 0x12490000 0x0 0x10000>;
+		pwm0: pwm@12490000 {
+			compatible = "opencores,pwm-v1";
+			reg = <0x0 0x12490000 0x0 0x10>;

NAK on the compatibles front, but this also looks very suspect, given the size of the register regions, but I think it is actually correct.
You need to explain why it is correct in the commit message.

OK. Will keep the old compatibles and explain the change of register size in the commit message.

Best regards,
Hal
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#3 lore raw

Re: [PATCH v18 1/3] dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

In reply to [email protected]
Quoted 41 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Removing existing StarFive compatible strings breaks DT schema validation for existing device trees.
- [Medium] The commit silently modifies the reg space size in the schema example.
- [Low] The compatible schema contains an awkward single-item list structure after modification.
--

commit c3152a2a38b5f4d3a25df0696646b840e0d04b60
Author: Hal Feng <[email protected]>

dt-bindings: pwm: opencores: Drop starfive compatibles and update maintainers

Each of the StarFive SoCs has 8 OpenCores PTC IP cores that only differ by their register base address. There is no need for starfive compatibles to distinguish them, so they are dropped.

Quoted 5 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +0 -0
index 52a59d245cdb1..834fb17ec595e 100644

[ ... ]

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml allOf: · line 23

Quoted 9 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +0 -4
@@ -20,10 +20,6 @@ allOf:
 properties:
   compatible:
     items:
-      - enum:
-          - starfive,jh7100-pwm
-          - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1

Does removing the legacy compatible strings break dtbs_check for existing device trees? Older device trees likely still use the two-string format.

Quoted 6 lines

Would it be better to use a oneOf construct to allow both the legacy two-string format and the new single-string format to maintain backward compatibility?

This isn't a bug, but leaving a single element under items is an anti-pattern in dt-schema. Since only one string is allowed now, could this be simplified to just a direct const property instead of an items array?

Will drop the above change and use oneOf construct to allow both the legacy two-string format and the new single-string format.

Quoted 16 lines

[ ... ]

Documentation/devicetree/bindings/pwm/opencores,pwm.yaml additionalProperties: false · line 48

Quoted 8 lines
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +2 -2
@@ -48,8 +44,8 @@ additionalProperties: false
 examples:
   - |
     pwm@12490000 {
-        compatible = "starfive,jh7110-pwm", "opencores,pwm-v1";
-        reg = <0x12490000 0x10000>;
+        compatible = "opencores,pwm-v1";
+        reg = <0x12490000 0x10>;

The commit message mentions dropping the starfive compatibles and updating maintainers, but does not document changing the register space size from 0x10000 to 0x10.

Quoted 2 lines

Could the commit message be updated to explicitly explain and justify this register size change?

OK. Will add the explanation in the commit messages.

Best regards,
Hal

Quoted 5 lines

--
Sashiko AI
review · https://sashiko.dev/#/patchset/20260515054723.25024-1-hal.feng@s
tarfivetech.com?part=1

author
#8 lore raw

Re: [PATCH v18 2/3] riscv: dts: starfive: Correct pwm nodes

In reply to [email protected]
Quoted 25 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Pinmux configuration properties were erroneously omitted from pwm1_pins in jh7100-common.dtsi.
- [Medium] Removal of SoC-specific compatible strings violates Device Tree ABI best practices.
--

commit e05af83398a49d79a54da1bad8e64c7c1e5f98eb
Author: Hal Feng <[email protected]>

riscv: dts: starfive: Correct pwm nodes

This commit updates the PWM nodes for StarFive JH7100 and JH7110 SoCs, splitting the original single PWM node into 8 individual nodes representing the 8 OpenCores PTC IP cores.

Quoted 4 lines
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +0 -0

[ ... ]

Quoted 13 lines

arch/riscv/boot/dts/starfive/jh7100-common.dtsi GPO_I2C2_PAD_SDA_OEN,

Quoted 8 lines
arch/riscv/boot/dts/starfive/jh7100-common.dtsi +3 -0
+	pwm1_pins: pwm1-0 {
+		pwm1-pins {
+			pinmux =  <GPIOMUX(5,
 				  GPO_PWM_PAD_OUT_BIT1,
 				  GPO_PWM_PAD_OE_N_BIT1,
 				  GPI_NONE)>;
 		};
 	};

In the original code, the single pwm_pins node applied electrical properties like bias-disable, drive-strength = <35>, input-disable, input-schmitt-disable, and slew-rate = <0> to both PWM0 and PWM1 pins. When splitting the node, these properties were copied to pwm0_pins but omitted here.

No, I added the new electrical properties for pwm0-pins, so the old electrical properties are applied to pwm1-pins. You omitted the original code and got a wrong result. Please apply the patches to the mainline before you review them.

Quoted 20 lines

Will omitting these properties cause the pwm1 output to operate with undefined or default hardware pin states instead of the intended parameters?

Quoted 4 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +0 -0

[ ... ]

arch/riscv/boot/dts/starfive/jh7100.dtsi watchdog@12480000 { · line 364

Quoted 5 lines
arch/riscv/boot/dts/starfive/jh7100.dtsi +2 -3
-		pwm: pwm@12490000 {
-			compatible = "starfive,jh7100-pwm", "opencores,pwm-v1";
-			reg = <0x0 0x12490000 0x0 0x10000>;
+		pwm0: pwm@12490000 {
+			compatible = "opencores,pwm-v1";

Is it intentional to drop the SoC-specific "starfive,jh7100-pwm" compatible string here, as well as "starfive,jh7110-pwm" in jh7110.dtsi?

Quoted 5 lines

Device Tree best practices generally recommend retaining SoC-specific strings alongside generic IP strings. This ensures that if SoC-specific integration quirks are discovered later, workarounds can be applied without requiring users to update deployed device trees, and it maintains ABI compatibility for older kernels that might match against the removed strings.

Will drop the compatible change in the next version. Thanks.

Best regards,
Hal

Quoted 5 lines

--
Sashiko AI
review · https://sashiko.dev/#/patchset/20260515054723.25024-1-hal.feng@s
tarfivetech.com?part=2

author
#13 lore raw

Re: [PATCH v18 3/3] pwm: Add OpenCores PTC PWM driver

In reply to [email protected]
Quoted 44 lines · sashiko-bot

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Probe fails unconditionally when CONFIG_PM is disabled.
- [High] Teardown symmetry violation in ocores_pwm_pm_disable() leads to reset assertion without an active clock.
- [High] Race condition between pm_runtime_enable() and pm_runtime_get_noresume() allows premature hardware access without a clock.
- [High] Missing context restore on system resume leads to silently broken PWM output.
- [High] Runtime PM usage count leak permanently blocks PM suspend if the driver is unbound while the PWM is enabled.
- [Medium] Subsystem rule violation: Using asynchronous pm_runtime_put() before pm_runtime_disable() causes race conditions.
- [Medium] Hardware minimum period and duty cycle limitations are not enforced, allowing invalid configuration.
- [Medium] Unconditional hardware disable in probe() breaks bootloader handover.
- [Low] Masking of true error codes from devm_pwmchip_alloc().
--

commit 2a9909cb21b11044209c8ae47fbb6d140786ac45
Author: Hal Feng <[email protected]>
Subject: pwm: Add OpenCores PTC PWM driver

This commit adds a new PWM driver for the OpenCores PTC IP core.

Quoted 2 lines
drivers/pwm/pwm-ocores.c new file +0 -0

[ ... ]

Quoted 3 lines
drivers/pwm/pwm-ocores.c new file +3 -0
+static int ocores_pwm_apply(struct pwm_chip *chip,
+			    struct pwm_device *pwm,
+			    const struct pwm_state *state) {

[ ... ]

drivers/pwm/pwm-ocores.c line 84

Quoted 6 lines
drivers/pwm/pwm-ocores.c new file +6 -0
+	if (state->enabled) {
+		if (!pwm_is_enabled(pwm)) {
+			ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
+			if (ret < 0)
+				return ret;
+		}

Does this leak the runtime PM usage count if the driver is unbound while the PWM is enabled?

Quoted 8 lines

If the driver is unbound via sysfs while the PWM is actively running, the PWM core marks the chip non-operational but does not disable the running PWM hardware. The devres teardown action ocores_pwm_pm_disable() will run and forcefully turn off the hardware, but it doesn't decrement the elevated PM usage counter.

Since the struct device object persists across bind/unbind cycles, could this leaked usage count permanently prevent the device from entering runtime suspend on subsequent binds?

Good catch. Will fix it.

Quoted 19 lines
Quoted 4 lines
drivers/pwm/pwm-ocores.c new file +3 -0
+	} else {
+		if (pwm_is_enabled(pwm)) {
+			writel(0, ddata->base + OCPWM_CTRL);

drivers/pwm/pwm-ocores.c

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+			pm_runtime_put(pwmchip_parent(chip));
+		}
+		return 0;
+	}
+
+	writel(0, ddata->base + OCPWM_CTRL);
+	writel(OCPWM_CTRL_RST, ddata->base + OCPWM_CTRL);
+
+	period_data = mul_u64_u32_div(state->period, ddata->clk_rate,
+NSEC_PER_SEC);

The header comment notes the hardware minimum period and duty cycle is 1 clock cycle. If the requested state->period is smaller than one clock cycle, wouldn't mul_u64_u32_div() silently truncate the result to 0?

Quoted 3 lines

Writing 0 to the OCPWM_LRC and OCPWM_HRC registers violates this hardware constraint and might cause undefined IP behavior. Should the driver validate that period_data >= 1 and return -EINVAL if the request cannot be satisfied?

Will return -EINVAL if period_data == 0 or duty_data == 0.

Quoted 24 lines
Quoted 3 lines
drivers/pwm/pwm-ocores.c new file +2 -0
+	if (period_data > U32_MAX)
+		period_data = U32_MAX;

[ ... ]

Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +7 -0
+static int ocores_pwm_runtime_resume(struct device *dev) {
+	struct ocores_pwm_device *ddata = dev_get_drvdata(dev);
+	int ret;
+
+	ret = clk_prepare_enable(ddata->clk);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to enable clock\n");

drivers/pwm/pwm-ocores.c line 142

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+
+	return 0;
+}
+
+static const struct dev_pm_ops ocores_pwm_pm_ops = {
+	RUNTIME_PM_OPS(ocores_pwm_runtime_suspend,
+		       ocores_pwm_runtime_resume, NULL)
+	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+			    pm_runtime_force_resume)
+};

Will this silently break PWM output upon resuming from system sleep?

Quoted 7 lines

During system suspend, if the device's power domain is powered off, the hardware register contents are typically lost.

The ocores_pwm_runtime_resume() callback only re-enables the clock; it doesn't save and restore the PWM configuration registers. If a PWM was actively outputting a signal before system suspend, will it fail to output the correct signal after the system wakes up?

Yeah, to fix it, will add system suspend/resume handlers to save and restore the pwm registers.

Quoted 15 lines

drivers/pwm/pwm-ocores.c line 155

Quoted 12 lines
drivers/pwm/pwm-ocores.c new file +11 -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))
+		ocores_pwm_runtime_suspend(dev);
+
+	reset_control_assert(ddata->rst);
+}

Could asserting the reset while the clock is turned off cause issues?

Quoted 8 lines

In ocores_pwm_probe(), the hardware initialization order is strictly clock enable followed by reset deassertion. The explicit probe error path also respects this by reversing the order, asserting reset before disabling the clock.

However, ocores_pwm_pm_disable() calls ocores_pwm_runtime_suspend() (which turns off the clock) before calling reset_control_assert(). If the IP requires an active clock to propagate the reset signal, asserting the reset while the clock is turned off might fail or hang the bus.

The reset can be asserted when the clock is turned off. But I agree that asserting the reset first and then turning off the clock will be safer.

Quoted 13 lines

drivers/pwm/pwm-ocores.c line 166

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +9 -0
+static int ocores_pwm_probe(struct platform_device *pdev) {
+	struct device *dev = &pdev->dev;
+	struct ocores_pwm_device *ddata;
+	struct pwm_chip *chip;
+	int ret;
+
+	chip = devm_pwmchip_alloc(dev, 1, sizeof(*ddata));
+	if (IS_ERR(chip))
+		return -ENOMEM;

Should this return PTR_ERR(chip) instead of hardcoding -ENOMEM?

Quoted 2 lines

devm_pwmchip_alloc() returns an ERR_PTR that can propagate various error codes, and returning -ENOMEM masks the true failure reason.

OK. Will return PTR_ERR(chip) instead.

Quoted 14 lines
Quoted 3 lines
drivers/pwm/pwm-ocores.c new file +2 -0
+	chip->ops = &ocores_pwm_ops;
+	ddata = pwmchip_get_drvdata(chip);

[ ... ]

drivers/pwm/pwm-ocores.c line 203

Quoted 7 lines
drivers/pwm/pwm-ocores.c new file +7 -0
+	ret = reset_control_deassert(ddata->rst);
+	if (ret)
+		goto err_clk_disable;
+
+	ret = pm_runtime_set_active(dev);
+	if (ret)
+		goto err_reset_assert;

Does this fail probe unconditionally when CONFIG_PM is disabled?

Quoted 3 lines

When CONFIG_PM is disabled, the dummy implementation of pm_runtime_set_active() returns 1. The if (ret) check evaluates to true when ret = 1, causing the probe function to jump to the error path.

No. The dummy implementation of pm_runtime_set_active() returns 0.

Quoted 17 lines

Should this check if (ret < 0) to distinguish PM framework errors from successful status returns?

drivers/pwm/pwm-ocores.c line 213

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+	pm_runtime_enable(dev);
+
+	ret = devm_add_action_or_reset(dev, ocores_pwm_pm_disable, dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to add pm disable
+action\n");
+
+	pm_runtime_get_noresume(dev);
+
+	writel(0, ddata->base + OCPWM_CTRL);

Are there a couple of issues with this sequence?

Quoted 8 lines

First, pm_runtime_enable() is called while the runtime PM usage counter is 0. This opens a window where the PM core can asynchronously suspend the device. The driver then calls pm_runtime_get_noresume(), which increments the usage counter but does not resume the device if it was already suspended.

If the device was suspended during this window, the subsequent writel() would access hardware registers while the clock is turned off.
Should pm_runtime_get_noresume() be called before pm_runtime_enable()?

I do not think there is a real race here. At this point the PWM chip has not been registered yet, so no consumer can access the device, and pm_runtime_enable() itself does not schedule a suspend request. The device was manually resumed before this sequence. Therefore the following writel() is still protected by the active hardware state.

Quoted 5 lines

Second, does this unconditional writel(0, ...) break bootloader handover? If the bootloader previously configured and enabled the PWM, this write will instantly power it down. Could this cause hardware state disruption until the consumer driver re-enables it?

Agreed. Will drop it.

Quoted 9 lines

drivers/pwm/pwm-ocores.c line 219

Quoted 6 lines
drivers/pwm/pwm-ocores.c new file +5 -0
+	pm_runtime_put(dev);
+
+	ret = devm_pwmchip_add(dev, chip);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Could not register PWM chip\n");

Is it safe to use the asynchronous pm_runtime_put() here?

Quoted 8 lines

If devm_pwmchip_add() fails, the devres teardown action ocores_pwm_pm_disable() runs immediately and invokes pm_runtime_disable(). This forcefully cancels the pending asynchronous suspend work from pm_runtime_put(), leaving the device in an undefined PM state.

The Power Management subsystem guidelines dictate that pm_runtime_put_sync() must be used instead of pm_runtime_put() when pm_runtime_disable() might follow immediately.

Will use pm_runtime_put_sync() instead.

Best regards,
Hal

Quoted 5 lines

--
Sashiko AI
review · https://sashiko.dev/#/patchset/20260515054723.25024-1-hal.feng@s
tarfivetech.com?part=3