Skip to contentSkip to conversation

Add OpenCores PTC PWM support

PATCH Superseded Hal Feng 2 participants 4 messages Nov 7

Subsystems (3)

v24 of 25 Superseded Latest is v25 →

lore
Share & tools
lore

Hal Feng

author
#0 lore raw

v24 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 v23:
- Drop the Tested-by tag for the dt-bindings patch.

Changes since v22:
- Drop patch 2 since it is already merged.
- Drop the starfive,jhb100-pwm compatible from the dt-bindings, as the pwm module of JHB100 SoC differs slightly from the OpenCores PTC PWM module in polarity handling. The support for JHB100 SoC will be added in a separate patch.
- Move the polarity check later in .apply().
- Add comments for keeping the reference when the pwm is already enabled.
- Rebase on linux-next.

Changes since v21:
- Allow non-zero duty cycles shorter than one clock tick to round down to zero.
- Drop the .free() callback to preserve the PWM hardware state after pwm_put().
- Update the comment.

Changes since v20:
- Fix 0% duty cycle support.

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:
v23: https://lore.kernel.org/all/[email protected]/
v22: https://lore.kernel.org/all/[email protected]/
v21: https://lore.kernel.org/all/[email protected]/
v20: https://lore.kernel.org/all/[email protected]/
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 (2):
dt-bindings: pwm: opencores: Update compatibles, examples and
maintainers
pwm: Add OpenCores PTC PWM driver

 .../bindings/pwm/opencores,pwm.yaml           |   5 +-
 MAINTAINERS                                   |   6 +
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-ocores.c                      | 299 ++++++++++++++++++
 5 files changed, 320 insertions(+), 3 deletions(-)
 create mode 100644 drivers/pwm/pwm-ocores.c

base-commit: d48723711bae9b8cdc56719c448ba42fef28cbee

Signature · 1 line
2.43.2
author
#1 lore raw

PATCH v24 1/2 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.

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.

Fixes 2529085831b0 ("dt-bindings: pwm: Add bindings for OpenCores PWM Controller")
---
 Documentation/devicetree/bindings/pwm/opencores,pwm.yaml | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/pwm/opencores,pwm.yaml +2 -3
index 52a59d245cdb..b15468f97b59 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
@@ -23,7 +23,6 @@ properties:
       - enum:
           - starfive,jh7100-pwm
           - starfive,jh7110-pwm
-          - starfive,jh8100-pwm
       - const: opencores,pwm-v1
 
   reg:
@@ -49,7 +48,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
author
#2 lore raw

PATCH v24 2/2 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 | 299 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 318 insertions(+)
 create mode 100644 drivers/pwm/pwm-ocores.c
MAINTAINERS +6 -0
index b2eaee98852f..7935ff64b95e 100644
@@ -21082,6 +21082,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 729776086879..138e7cd6e991 100644
@@ -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
index 5630a521a7cf..4d2d14c4852f 100644
@@ -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 +299 -0

299 lines not on this page · raw message

Signature · 1 line
2.43.2
#3 lore raw

Re: [PATCH v24 2/2] pwm: Add OpenCores PTC PWM driver

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] pwm: ocores: clk_get_rate() called before enabling clock in ocores_pwm_probe() - [Low] pwm: ocores…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] pwm: ocores: clk_get_rate() called before enabling clock in ocores_pwm_probe()
- [Low] pwm: ocores: unlocked and pre-enable clk_get_rate() in ocores_pwm_probe()
--

commit 33c73933621c3feed0e1d70460454bd0821561cc
Author: Hal Feng <[email protected]>

pwm: Add OpenCores PTC PWM driver

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

Quoted 5 lines
drivers/pwm/pwm-ocores.c new file +0 -0
new file mode 100644
index 0000000000000..4c7e87cb009f6

[ ... ]

Quoted 18 lines
drivers/pwm/pwm-ocores.c new file +18 -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 PTR_ERR(chip);
+
+	chip->ops = &ocores_pwm_ops;
+	ddata = pwmchip_get_drvdata(chip);
+
+	ddata->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(ddata->base))
+		return dev_err_probe(dev, PTR_ERR(ddata->base),
+				     "Failed to map IO resources\n");

drivers/pwm/pwm-ocores.c line 235

Quoted 10 lines
drivers/pwm/pwm-ocores.c new file +10 -0
+
+	ddata->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(ddata->clk))
+		return dev_err_probe(dev, PTR_ERR(ddata->clk),
+				     "Failed to get clock\n");
+
+	ddata->clk_rate = clk_get_rate(ddata->clk);
+	if (!ddata->clk_rate || ddata->clk_rate > NSEC_PER_SEC)
+		return dev_err_probe(dev, -EINVAL,
+				     "Invalid clock rate: %lu\n", ddata->clk_rate);

[Severity: Low]
Does caching ddata->clk_rate here without locking the clock rate via devm_clk_rate_exclusive_get() and after enabling the clock leave the driver vulnerable to an invalid initial rate or runtime clock rate changes? If the parent clock rate changes after probe, ocores_pwm_apply() and ocores_pwm_get_state() will compute inaccurate periods and duty cycles from the stale rate, or violate the NSEC_PER_SEC bound.

drivers/pwm/pwm-ocores.c line 246

Quoted 11 lines
drivers/pwm/pwm-ocores.c new file +11 -0
+
+	ddata->rst = devm_reset_control_get_optional_shared(dev, NULL);
+	if (IS_ERR(ddata->rst))
+		return dev_err_probe(dev, PTR_ERR(ddata->rst),
+				     "Failed to get reset\n");
+
+	platform_set_drvdata(pdev, ddata);
+
+	ret = ocores_pwm_runtime_resume(dev);
+	if (ret)
+		return ret;

[Severity: Low]
Should clk_get_rate(ddata->clk) be called after ocores_pwm_runtime_resume() enables the clock? The clk API only guarantees a valid rate once the clock is enabled, so querying it before clk_prepare_enable() can return 0 (failing probe with -EINVAL) or a stale rate on clock providers that compute their rate on enable.

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