Skip to contentSkip to conversation

Add OpenCores PTC PWM support

PATCH Superseded Hal Feng 3 participants 10 messages Oct 14 – Oct 19

Subsystems (3)

v23 of 25 Superseded Latest is v25 →

lore
Share & tools
lore

Hal Feng

author
#0 lore raw

v23 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 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:
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: 1fffefb00cc0b0da1f2ae24e3d33364530ea6515

Signature · 1 line
2.43.2
author
#1 lore raw

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

Re: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

Quoted 9 lines · Hal Feng

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.

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover letter?

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

Hello Krzysztof,

Quoted 7 lines · Krzysztof Kozlowski
Quoted 6 lines · Hal Feng

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.

Quoted 6 lines
Quoted 3 lines

I will maintain this pwm module in place of William.

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover letter?

I don't know what Maud did, but if they checked that the various dt linters are still happy, that doesn't deserve a Tested-by:?

Best regards
Uwe

#4 lore raw

Re: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

Quoted 12 lines · Uwe Kleine-König

Hello Krzysztof,

Quoted 9 lines · Krzysztof Kozlowski
Quoted 8 lines · Hal Feng

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.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover letter?

I don't know what Maud did, but if they checked that the various dt linters are still happy, that doesn't deserve a Tested-by:?

No. Why would compilation be called testing? Since when? I also compiled driver and the other code, just like every other boot did, but no tags for us...

Best regards,
Krzysztof

author
#5 lore raw

RE: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

Quoted 18 lines · Krzysztof Kozlowski
Quoted 16 lines · Uwe Kleine-König

Hello Krzysztof,

Quoted 13 lines · Krzysztof Kozlowski
Quoted 9 lines · Hal Feng

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.

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover

Quoted 8 lines

letter?

Quoted 4 lines

I don't know what Maud did, but if they checked that the various dt linters are still happy, that doesn't deserve a Tested-by:?

No. Why would compilation be called testing? Since when? I also compiled driver and the other code, just like every other boot did, but no tags for us...

OK. Let's drop the Tested-by tag in this patch.

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

#6 lore raw

Re: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

In reply to Krzysztof Kozlowski

Hello Krzysztof,

Quoted 15 lines · Krzysztof Kozlowski
Quoted 14 lines · Uwe Kleine-König

Hello Krzysztof,

Quoted 11 lines · Krzysztof Kozlowski
Quoted 9 lines · Hal Feng

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.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover letter?

I don't know what Maud did, but if they checked that the various dt linters are still happy, that doesn't deserve a Tested-by:?

No. Why would compilation be called testing?

I'd call the task of checking if anything breaks with the proposed changes testing. For changes to .c files that most likely requires complilation and running the resulting code on a machine. For dt binding changes it means "testing" if all the existing device trees still conform to the new binding because you cannot compile and run the binding specifications. So in my mind this is still testing and a Tested-by trailer is ok and welcome for me.

And from a more social side: I really appreciate if someone invests the effort to check patches, by whatever procedure is appropriate, and honoring that with a Tested-by: tag is the least we can do.

Best regards
Uwe

#7 lore raw

Re: [PATCH v23 1/2] dt-bindings: pwm: opencores: Update compatibles, examples and maintainers

Quoted 26 lines · Uwe Kleine-König

Hello Krzysztof,

Quoted 15 lines · Krzysztof Kozlowski
Quoted 14 lines · Uwe Kleine-König

Hello Krzysztof,

Quoted 11 lines · Krzysztof Kozlowski
Quoted 9 lines · Hal Feng

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.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Drop, did not happen. Is it so tricky to respond to the one patch here, where the tag would be applicable, instead of replying to cover letter?

I don't know what Maud did, but if they checked that the various dt linters are still happy, that doesn't deserve a Tested-by:?

No. Why would compilation be called testing?

Quoted 7 lines

I'd call the task of checking if anything breaks with the proposed changes testing. For changes to .c files that most likely requires complilation and running the resulting code on a machine. For dt binding changes it means "testing" if all the existing device trees still conform to the new binding because you cannot compile and run the binding specifications. So in my mind this is still testing and a Tested-by trailer is ok and welcome for me.

Running standard build process tools, like checkpatch, sparse, gcc/clang, dt-validate is not testing.

Best regards,
Krzysztof

author
#8 lore raw

PATCH v23 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 1da3623e77ce..f4237ec609fb 100644
@@ -20928,6 +20928,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
author
#9 lore raw

RE: [PATCH v23 2/2] pwm: Add OpenCores PTC PWM driver

Hi, Uwe Kleine-König

Could you please help review and merge this driver patch?

Best regards,
Hal

Quoted 369 lines

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 1da3623e77ce..f4237ec609fb 100644
@@ -20928,6 +20928,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 index renamed from drivers/pwm/Kconfig +0 -0

729776086879..138e7cd6e991 100644

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 index renamed from drivers/pwm/Makefile +0 -0

5630a521a7cf..4d2d14c4852f 100644

drivers/pwm/Makefile +0 -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

drivers/pwm/Makefile +1 -0
+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 renamed from drivers/pwm/pwm-ocores.c +0 -0

file mode 100644 index 000000000000..4c7e87cb009f

drivers/pwm/pwm-ocores.c new file +11 -0
@@ -0,0 +1,299 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * OpenCores PTC PWM Driver
+ *
+ * https://opencores.org/projects/ptc
+ *
+ * Copyright (C) 2018-2026 StarFive Technology Co., Ltd.
+ *
+ * Limitations:
+ * - The hardware only supports inverted polarity.
+ * - The hardware minimum period / non-zero duty_cycle is (1 / pwm_apb

clock frequency).
+ * - The hardware maximum period / duty_cycle of PWM is (U32_MAX / pwm_apb clock frequency).
+ * - The output is immediately set to low when the module is disabled.
+ */

drivers/pwm/pwm-ocores.c new file +44 -0
+
+#include <linux/clk.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/pwm.h>
+#include <linux/reset.h>
+
+#define OCPWM_HRC	0x4
+#define OCPWM_LRC	0x8
+#define OCPWM_CTRL	0xC
+
+#define OCPWM_CTRL_EN	BIT(0)
+#define OCPWM_CTRL_OE	BIT(3)
+#define OCPWM_CTRL_RST	BIT(7)
+
+#define OCPWM_NUM_SAVED_REGS	3
+
+struct ocores_pwm_device {
+	void __iomem *base;
+	struct clk *clk;
+	unsigned long clk_rate;
+	struct reset_control *rst;
+	u32 saved_regs[OCPWM_NUM_SAVED_REGS];
+};
+
+static int ocores_pwm_get_state(struct pwm_chip *chip,
+				struct pwm_device *pwm,
+				struct pwm_state *state)
+{
+	struct ocores_pwm_device *ddata = pwmchip_get_drvdata(chip);
+	u32 period_data, duty_data, ctrl_data;
+	int ret;
+
+	ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
+	if (ret < 0)
+		return ret;
+
+	period_data = readl(ddata->base + OCPWM_LRC);
+	duty_data = readl(ddata->base + OCPWM_HRC);
+	ctrl_data = readl(ddata->base + OCPWM_CTRL);
+
+	state->period = DIV_ROUND_UP_ULL((u64)period_data * NSEC_PER_SEC,

ddata->clk_rate);

drivers/pwm/pwm-ocores.c new file +1 -0
+	state->duty_cycle = DIV_ROUND_UP_ULL((u64)duty_data *

NSEC_PER_SEC, ddata->clk_rate);

drivers/pwm/pwm-ocores.c new file +32 -0
+	if (state->duty_cycle > state->period)
+		state->duty_cycle = state->period;
+
+	state->polarity = PWM_POLARITY_INVERSED;
+	state->enabled = (ctrl_data & OCPWM_CTRL_EN) ? true : false;
+
+	pm_runtime_put(pwmchip_parent(chip));
+
+	return 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);
+	bool was_enabled = pwm_is_enabled(pwm);
+	u64 period_data, duty_data;
+	int ret;
+
+	if (!state->enabled) {
+		if (was_enabled) {
+			writel(0, ddata->base + OCPWM_CTRL);
+			pm_runtime_put(pwmchip_parent(chip));
+		}
+		return 0;
+	}
+
+	if (state->polarity != PWM_POLARITY_INVERSED)
+		return -EINVAL;
+
+	period_data = mul_u64_u32_div(state->period, ddata->clk_rate,

NSEC_PER_SEC);

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 new file +18 -0
+	if (duty_data > U32_MAX)
+		duty_data = U32_MAX;
+
+	if (!period_data)
+		return -EINVAL;
+
+	if (!was_enabled) {
+		ret = pm_runtime_resume_and_get(pwmchip_parent(chip));
+		if (ret < 0)
+			return ret;
+	}
+
+	writel(0, ddata->base + OCPWM_CTRL);
+	writel(OCPWM_CTRL_RST, ddata->base + OCPWM_CTRL);
+
+	writel(period_data, ddata->base + OCPWM_LRC);
+	writel(duty_data, ddata->base + OCPWM_HRC);
+	writel(OCPWM_CTRL_OE | OCPWM_CTRL_EN, ddata->base +

OCPWM_CTRL);

drivers/pwm/pwm-ocores.c new file +70 -0
+
+	return 0;
+}
+
+static const struct pwm_ops ocores_pwm_ops = {
+	.get_state = ocores_pwm_get_state,
+	.apply = ocores_pwm_apply,
+};
+
+static int ocores_pwm_runtime_suspend(struct device *dev) {
+	struct ocores_pwm_device *ddata = dev_get_drvdata(dev);
+
+	clk_disable_unprepare(ddata->clk);
+
+	return 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");
+
+	return 0;
+}
+
+static int __maybe_unused ocores_pwm_suspend(struct device *dev) {
+	struct ocores_pwm_device *ddata = dev_get_drvdata(dev);
+	int ret, i;
+
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
+		return ret;
+
+	for (i = 0; i < OCPWM_NUM_SAVED_REGS; i++)
+		ddata->saved_regs[i] = readl(ddata->base + 4 + 4 * i);
+
+	pm_runtime_put_sync(dev);
+
+	return pm_runtime_force_suspend(dev);
+}
+
+static int __maybe_unused ocores_pwm_resume(struct device *dev) {
+	struct ocores_pwm_device *ddata = dev_get_drvdata(dev);
+	int ret, i;
+
+	ret = pm_runtime_force_resume(dev);
+	if (ret)
+		return ret;
+
+	ret = pm_runtime_resume_and_get(dev);
+	if (ret < 0)
+		return ret;
+
+	writel(0, ddata->base + OCPWM_CTRL);
+	writel(OCPWM_CTRL_RST, ddata->base + OCPWM_CTRL);
+	for (i = 0; i < OCPWM_NUM_SAVED_REGS; i++)
+		writel(ddata->saved_regs[i], ddata->base + 4 + 4 * i);
+
+	pm_runtime_put_sync(dev);
+
+	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(ocores_pwm_suspend,

ocores_pwm_resume) };

drivers/pwm/pwm-ocores.c new file +107 -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 the reference held while the PWM is enabled. */
+		if (readl(ddata->base + OCPWM_CTRL) & OCPWM_CTRL_EN)
+			pm_runtime_put_noidle(dev);
+
+		ocores_pwm_runtime_suspend(dev);
+	}
+
+	reset_control_assert(ddata->rst);
+}
+
+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");
+
+	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);
+
+	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;
+
+	ret = reset_control_deassert(ddata->rst);
+	if (ret)
+		goto err_clk_disable;
+
+	ret = pm_runtime_set_active(dev);
+	if (ret)
+		goto err_reset_assert;
+
+	pm_runtime_get_noresume(dev);
+	pm_runtime_enable(dev);
+
+	/* Keep the reference only if the PWM is already enabled. */
+	if (!(readl(ddata->base + OCPWM_CTRL) & OCPWM_CTRL_EN))
+		pm_runtime_put_sync(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");
+
+	ret = devm_pwmchip_add(dev, chip);
+	if (ret < 0)
+		return dev_err_probe(dev, ret, "Could not register PWM chip\n");
+
+	return 0;
+
+err_reset_assert:
+	reset_control_assert(ddata->rst);
+err_clk_disable:
+	ocores_pwm_runtime_suspend(dev);
+	return dev_err_probe(dev, ret, "Failed to init pwm power\n"); }
+
+static const struct of_device_id ocores_pwm_of_match[] = {
+	{ .compatible = "opencores,pwm-v1" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, ocores_pwm_of_match);
+
+static struct platform_driver ocores_pwm_driver = {
+	.probe = ocores_pwm_probe,
+	.driver = {
+		.name = "ocores-pwm",
+		.of_match_table = ocores_pwm_of_match,
+		.pm = pm_ptr(&ocores_pwm_pm_ops),
+	},
+};
+module_platform_driver(ocores_pwm_driver);
+
+MODULE_AUTHOR("Jieqin Chen");
+MODULE_AUTHOR("Hal Feng <[email protected]>");
+MODULE_DESCRIPTION("OpenCores PTC PWM driver");
Quoted 3 lines

MODULE_LICENSE("GPL");
--
2.43.2

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