Skip to contentSkip to conversation

pwm: Add pwm driver for Sophgo SG2042

PATCH Applied Chen Wang 4 participants 9 messages Feb 5, 2025 – Feb 9, 2025

Subsystems (4)
lore
Share & tools
lore

Chen Wang

author
#0 lore raw

v7 cover pwm: Add pwm driver for Sophgo SG2042

From: Chen Wang <[email protected]>

Add driver for pwm controller of Sophgo SG2042 SoC.

Thanks,
Chen

---

Changes in v7:
The patch series is based on v6.14-rc1.

Fixed following issues as per comments from Uwe Kleine-König, thanks.

- dt-bindings: change value of "#pwm-cells" from 2 to 3.
- driver: rename variables period/hlperiod to period_ticks/hlperiod_ticks and improve coding of apply()/get_state(); use devm_reset_control_get_optional_shared_deasserted() instead of devm_reset_control_get_optional_shared() and reset_control_deassert(); add more comments and other miscellaneous code improvements.

Changes in v6:
Nothing major changes just rebased onto v6.13-rc1 and retested. You can simply review or test the patches at the link [6].

Changes in v5:
The patch series is based on v6.12-rc1. You can simply review or test the patches at the link [5].

Updated driver to add resets property for pwm controller node as per suggestion from Inochi.

Changes in v4:
The patch series is based on v6.12-rc1. You can simply review or test the patches at the link [4].

Updated driver to set property atomic of pwm_chip to true as per suggestion from Sean.

Changes in v3:
The patch series is catched up with v6.12-rc1. You can simply review or test the patches at the link [3].

Add patch #3 for dts part change.

Changes in v2:
The patch series is based on v6.11-rc6. You can simply review or test the patches at the link [2].

Fixed following issues as per comments from Yixun Lan, Krzysztof Kozlowski and Uwe Kleine-König, thanks.

- Some minor issues in dt-bindings.
- driver issues, use macros with name prefix for registers access; add limitations comments; fixed potential calculation overflow problem; add .get_state() callback and other miscellaneous code improvements.

Changes in v1:
The patch series is based on v6.11-rc6. You can simply review or test the patches at the link [1].

---

Chen Wang (3):
dt-bindings: pwm: sophgo: add PWM controller for SG2042
pwm: sophgo: add driver for Sophgo SG2042 PWM
riscv: sophgo: dts: add pwm controller for SG2042 SoC

 .../bindings/pwm/sophgo,sg2042-pwm.yaml       |  58 ++++++
 arch/riscv/boot/dts/sophgo/sg2042.dtsi        |   9 +
 drivers/pwm/Kconfig                           |  10 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-sophgo-sg2042.c               | 196 ++++++++++++++++++
 5 files changed, 274 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml
 create mode 100644 drivers/pwm/pwm-sophgo-sg2042.c

base-commit: 2014c95afecee3e76ca4a56956a936e23283f05b

Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v7 1/3 dt-bindings: pwm: sophgo: add PWM controller for SG2042

From: Chen Wang <[email protected]>

Sophgo SG2042 contains a PWM controller, which has 4 channels and can generate PWM waveforms output.

---
 .../bindings/pwm/sophgo,sg2042-pwm.yaml       | 58 +++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml
Documentation/devicetree/bindings/pwm/sophgo,sg2042-pwm.yaml new file +58 -0
new file mode 100644
index 000000000000..bbb6326d47d7
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/sophgo,sg2042-pwm.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Sophgo SG2042 PWM controller
+
+maintainers:
+  - Chen Wang <[email protected]>
+
+description:
+  This controller contains 4 channels which can generate PWM waveforms.
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    const: sophgo,sg2042-pwm
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  clock-names:
+    items:
+      - const: apb
+
+  resets:
+    maxItems: 1
+
+  "#pwm-cells":
+    const: 3
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - resets
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/reset/sophgo,sg2042-reset.h>
+
+    pwm@7f006000 {
+        compatible = "sophgo,sg2042-pwm";
+        reg = <0x7f006000 0x1000>;
+        #pwm-cells = <3>;
+        clocks = <&clock 67>;
+        clock-names = "apb";
+        resets = <&rstgen RST_PWM>;
+    };
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v7 2/3 pwm: sophgo: add driver for Sophgo SG2042 PWM

In reply to Chen Wang

From: Chen Wang <[email protected]>

Add a PWM driver for PWM controller in Sophgo SG2042 SoC.

---
 drivers/pwm/Kconfig             |  10 ++
 drivers/pwm/Makefile            |   1 +
 drivers/pwm/pwm-sophgo-sg2042.c | 196 ++++++++++++++++++++++++++++++++
 3 files changed, 207 insertions(+)
 create mode 100644 drivers/pwm/pwm-sophgo-sg2042.c
drivers/pwm/Kconfig +10 -0
index 0915c1e7df16..ec85f3895936 100644
@@ -584,6 +584,16 @@ config PWM_SL28CPLD
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-sl28cpld.
 
+config PWM_SOPHGO_SG2042
+	tristate "Sophgo SG2042 PWM support"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	help
+	  PWM driver for the PWM controller on Sophgo SG2042 SoC. The PWM
+	  controller supports outputing 4 channels of PWM waveforms.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm_sophgo_sg2042.
+
 config PWM_SPEAR
 	tristate "STMicroelectronics SPEAr PWM support"
 	depends on PLAT_SPEAR || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 9081e0c0e9e0..539e0def3f82 100644
@@ -53,6 +53,7 @@ obj-$(CONFIG_PWM_RZ_MTU3)	+= pwm-rz-mtu3.o
 obj-$(CONFIG_PWM_SAMSUNG)	+= pwm-samsung.o
 obj-$(CONFIG_PWM_SIFIVE)	+= pwm-sifive.o
 obj-$(CONFIG_PWM_SL28CPLD)	+= pwm-sl28cpld.o
+obj-$(CONFIG_PWM_SOPHGO_SG2042)	+= pwm-sophgo-sg2042.o
 obj-$(CONFIG_PWM_SPEAR)		+= pwm-spear.o
 obj-$(CONFIG_PWM_SPRD)		+= pwm-sprd.o
 obj-$(CONFIG_PWM_STI)		+= pwm-sti.o
drivers/pwm/pwm-sophgo-sg2042.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v7 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM

Hello Chen,

I was tempted to apply this patch while reading throug it until nearly the end ...

Quoted 4 lines · Chen Wang

[...]

(quoted diff) +2 -0
+static int pwm_sg2042_probe(struct platform_device *pdev)
+{
[...]

Review

Quoted 10 lines
(quoted diff) +10 -0
+	rst = devm_reset_control_get_optional_shared_deasserted(dev, NULL);
+	if (IS_ERR(rst))
+		return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset\n");
+
+	chip->ops = &pwm_sg2042_ops;
+	chip->atomic = true;
+
+	ret = devm_pwmchip_add(dev, chip);
+	if (ret < 0) {
+		reset_control_assert(rst);

This is wrong (well, or unneeded). With devm_reset_control_get_optional_shared_deasserted() the devm cleanup cares for reasserting the reset.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
+	}
+
+	return 0;
+}

If you want I can apply and squash the following in:

drivers/pwm/pwm-sophgo-sg2042.c +1 -3
index ce8cf8af3402..ff4639d849ce 100644
@@ -174,10 +174,8 @@ static int pwm_sg2042_probe(struct platform_device *pdev)
 	chip->atomic = true;
 
 	ret = devm_pwmchip_add(dev, chip);
-	if (ret < 0) {
-		reset_control_assert(rst);
+	if (ret < 0)
 		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
-	}
 
 	return 0;
 }

ack?

Best regards
Uwe

#4 lore raw

Re: [PATCH v7 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM

Quoted 48 lines · Uwe Kleine-König

Hello Chen,

I was tempted to apply this patch while reading throug it until nearly the end ...

Quoted 4 lines · Chen Wang

[...]

(quoted diff) +2 -0
+static int pwm_sg2042_probe(struct platform_device *pdev)
+{
[...]

Review

Quoted 10 lines
(quoted diff) +10 -0
+	rst = devm_reset_control_get_optional_shared_deasserted(dev, NULL);
+	if (IS_ERR(rst))
+		return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset\n");
+
+	chip->ops = &pwm_sg2042_ops;
+	chip->atomic = true;
+
+	ret = devm_pwmchip_add(dev, chip);
+	if (ret < 0) {
+		reset_control_assert(rst);

This is wrong (well, or unneeded). With devm_reset_control_get_optional_shared_deasserted() the devm cleanup cares for reasserting the reset.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
+	}
+
+	return 0;
+}

If you want I can apply and squash the following in:

drivers/pwm/pwm-sophgo-sg2042.c +1 -3
index ce8cf8af3402..ff4639d849ce 100644
@@ -174,10 +174,8 @@ static int pwm_sg2042_probe(struct platform_device *pdev)
  	chip->atomic = true;
 
  	ret = devm_pwmchip_add(dev, chip);
-	if (ret < 0) {
-		reset_control_assert(rst);
+	if (ret < 0)
  		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
-	}
 
  	return 0;
  }
Quoted 4 lines

ack?

Best regards
Uwe

Ack.

Thanks,

Chen

#5 lore raw

Re: [PATCH v7 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM

Hello,

Quoted 18 lines · Chen Wang
Quoted 17 lines · Uwe Kleine-König

I was tempted to apply this patch while reading throug it until nearly the end ...

Review

Quoted 1 line
(quoted diff) +1 -0
+		reset_control_assert(rst);

This is wrong (well, or unneeded). With devm_reset_control_get_optional_shared_deasserted() the devm cleanup cares for reasserting the reset.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
+	}
+
+	return 0;
+}

If you want I can apply and squash the following in:

Quoted 5 lines
Quoted 3 lines

[...]

ack?

Ack.

Great. Pushed to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

with the suggested fixup.

Best regards
Uwe

maintainer
#6 lore raw

Re: [PATCH v7 2/3] pwm: sophgo: add driver for Sophgo SG2042 PWM

Quoted 25 lines · Uwe Kleine-König

Hello,

Quoted 22 lines · Chen Wang
Quoted 20 lines · Uwe Kleine-König

I was tempted to apply this patch while reading throug it until nearly the end ...

Review

Quoted 1 line
(quoted diff) +1 -0
+		reset_control_assert(rst);

This is wrong (well, or unneeded). With devm_reset_control_get_optional_shared_deasserted() the devm cleanup cares for reasserting the reset.

Review

Quoted 5 lines
(quoted diff) +5 -0
+		return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
+	}
+
+	return 0;
+}

If you want I can apply and squash the following in:

[...]

ack?

Quoted 8 lines
Quoted 1 line

Ack.

Great. Pushed to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

with the suggested fixup.

Good, I will take the dts next Monday.

Regards,
Inochi

author
#7 lore raw

PATCH v7 3/3 riscv: sophgo: dts: add pwm controller for SG2042 SoC

In reply to Chen Wang

From: Chen Wang <[email protected]>

SG2042 has one PWM controller, which has 4 pwm output channels.

---
 arch/riscv/boot/dts/sophgo/sg2042.dtsi | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/riscv/boot/dts/sophgo/sg2042.dtsi +9 -0
index e62ac51ac55a..4449c762d663 100644
@@ -165,6 +165,15 @@ port2a: gpio-controller@0 {
 			};
 		};
 
+		pwm: pwm@703000c000 {
+			compatible = "sophgo,sg2042-pwm";
+			reg = <0x70 0x3000c000 0x0 0x20>;
+			#pwm-cells = <3>;
+			clocks = <&clkgen GATE_CLK_APB_PWM>;
+			clock-names = "apb";
+			resets = <&rstgen RST_PWM>;
+		};
+
 		pllclk: clock-controller@70300100c0 {
 			compatible = "sophgo,sg2042-pll";
 			reg = <0x70 0x300100c0 0x0 0x40>;
Signature · 1 line
2.34.1