Skip to contentSkip to conversation

RFC on synpsys pwm driver changes

RFC Superseded Ben Dooks 4 participants 27 messages Aug 16, 2022 – Sep 30, 2022

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

v4 cover RFC on synpsys pwm driver changes

New version of the pwm timers patch, hopefully all review comments are sorted out, however I have not had time to fully test this and I do not have a PCI system to test it on either.

The series has been moved around a bit to try to get some of the simpler changes in before splitting and to make the OF driver a single addition.

v4:
- split pci and of into new modules
- fixup review comments
- fix typos in dt-bindings
v3:
- change the compatible name
- squash down pwm count patch
- fixup patch naming

v2:
- fix #pwm-cells count to be 3
- fix indetation
- merge the two clock patches
- add HAS_IOMEM as a config dependency

Ben Dooks (10):
dt-bindings: pwm: Document Synopsys DesignWare
snps,pwm-dw-apb-timers-pwm2
pwm: dwc: allow driver to be built with COMPILE_TEST
pwm: dwc: change &pci->dev to dev in probe
pwm: dwc: move memory alloc to own function
pwm: dwc: use devm_pwmchip_add
pwm: dwc: split pci out of core driver
pwm: dwc: make timer clock configurable
pwm: dwc: add of/platform support
pwm: dwc: add snps,pwm-number to limit pwm count
pwm: dwc: add PWM bit unset in get_state call

 .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml |  69 ++++++
 drivers/pwm/Kconfig                           |  24 ++-
 drivers/pwm/Makefile                          |   2 +
 drivers/pwm/pwm-dwc-of.c                      |  86 ++++++++
 drivers/pwm/pwm-dwc-pci.c                     | 134 ++++++++++++
 drivers/pwm/pwm-dwc.c                         | 197 +++---------------
 drivers/pwm/pwm-dwc.h                         |  60 ++++++
 7 files changed, 402 insertions(+), 170 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml
 create mode 100644 drivers/pwm/pwm-dwc-of.c
 create mode 100644 drivers/pwm/pwm-dwc-pci.c
 create mode 100644 drivers/pwm/pwm-dwc.h
Signature · 1 line
2.35.1
author
#1 lore raw

RFC v4 1/10 dt-bindings: pwm: Document Synopsys DesignWare snps,pwm-dw-apb-timers-pwm2

Add documentation for the bindings for Synopsys' DesignWare PWM block as we will be adding DT/platform support to the Linux driver soon.

---
v4:
- fixed typos, added reg
v3:
- add description and example
- merge the snps,pwm-number into this patch
- rename snps,pwm to snps,dw-apb-timers-pwm2
v2:
- fix #pwm-cells to be 3
- fix indentation and ordering issues

---
 .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +69 -0
new file mode 100644
index 000000000000..e7feae6d4404
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) 2022 SiFive, Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/snps,dw-apb-timers-pwm2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Synopsys DW-APB timers PWM controller
+
+maintainers:
+  - Ben Dooks <[email protected]>
+
+description:
+  This describes the DesignWare APB timers module when used in the PWM
+  mode. The IP core can be generated with various options which can
+  control the functionality, the number of PWMs available and other
+  internal controls the designer requires.
+
+  The IP block has a version register so this can be used for detection
+  instead of having to encode the IP version number in the device tree
+  comaptible.
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    const: snps,dw-apb-timers-pwm2
+
+  "#pwm-cells":
+    const: 3
+
+  clocks:
+    items:
+      - description: Interface bus clock
+      - description: PWM reference clock
+
+  clock-names:
+    items:
+      - const: bus
+      - const: timer
+
+  snps,pwm-number:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: The number of PWM channels configured for this instance
+    enum: [1, 2, 3, 4, 5, 6, 7, 8]
+
+  reg:
+    maxItems: 1
+
+required:
+  - "#pwm-cells"
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+
+additionalProperties: false
+
+
+examples:
+  - |
+    pwm: pwm@180000 {
+      #pwm-cells = <3>;
+      compatible = "snps,dw-apb-timers-pwm2";
+      reg = <0x180000 0x200>;
+      clocks = <&bus &timer>;
+      clock-names = "bus", "timer";
+    };
Signature · 1 line
2.35.1
#2 lore raw

Re: [RFC v4 01/10] dt-bindings: pwm: Document Synopsys DesignWare snps,pwm-dw-apb-timers-pwm2

Quoted 69 lines · Ben Dooks

Add documentation for the bindings for Synopsys' DesignWare PWM block as we will be adding DT/platform support to the Linux driver soon.

---
v4:
- fixed typos, added reg
v3:
- add description and example
- merge the snps,pwm-number into this patch
- rename snps,pwm to snps,dw-apb-timers-pwm2
v2:
- fix #pwm-cells to be 3
- fix indentation and ordering issues

---
 .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +44 -0
new file mode 100644
index 000000000000..e7feae6d4404
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) 2022 SiFive, Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/snps,dw-apb-timers-pwm2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Synopsys DW-APB timers PWM controller
+
+maintainers:
+  - Ben Dooks <[email protected]>
+
+description:
+  This describes the DesignWare APB timers module when used in the PWM
+  mode. The IP core can be generated with various options which can
+  control the functionality, the number of PWMs available and other
+  internal controls the designer requires.
+
+  The IP block has a version register so this can be used for detection
+  instead of having to encode the IP version number in the device tree
+  comaptible.
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    const: snps,dw-apb-timers-pwm2
+
+  "#pwm-cells":
+    const: 3
+
+  clocks:
+    items:
+      - description: Interface bus clock
+      - description: PWM reference clock
+
+  clock-names:
+    items:
+      - const: bus
+      - const: timer
+
+  snps,pwm-number:
+    $ref: /schemas/types.yaml#/definitions/uint32

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 54

Quoted 10 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +10 -0
+    description: The number of PWM channels configured for this instance
+    enum: [1, 2, 3, 4, 5, 6, 7, 8]
+
+  reg:
+    maxItems: 1
+
+required:
+  - "#pwm-cells"
+  - compatible
+  - reg

Keep the same order as list of properties.

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 60

Quoted 6 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +6 -0
+  - clocks
+  - clock-names
+
+additionalProperties: false
+
+

Just one blank line.

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 66

Quoted 6 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +6 -0
+examples:
+  - |
+    pwm: pwm@180000 {
+      #pwm-cells = <3>;
+      compatible = "snps,dw-apb-timers-pwm2";
+      reg = <0x180000 0x200>;

The convention of DTS is: compatible, then reg, then rest of properties.

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 67

Quoted 1 line
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +1 -0
+      clocks = <&bus &timer>;

You put here one item, not two. This has to be <&bus>, <&timer>

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 69

Quoted 2 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +2 -0
+      clock-names = "bus", "timer";
+    };

Best regards,
Krzysztof

author
#3 lore raw

Re: [RFC v4 01/10] dt-bindings: pwm: Document Synopsys DesignWare snps,pwm-dw-apb-timers-pwm2

Quoted 74 lines · Krzysztof Kozlowski
Quoted 73 lines · Ben Dooks

Add documentation for the bindings for Synopsys' DesignWare PWM block as we will be adding DT/platform support to the Linux driver soon.

---
v4:
- fixed typos, added reg
v3:
- add description and example
- merge the snps,pwm-number into this patch
- rename snps,pwm to snps,dw-apb-timers-pwm2
v2:
- fix #pwm-cells to be 3
- fix indentation and ordering issues

---
  .../bindings/pwm/snps,dw-apb-timers-pwm2.yaml | 69 +++++++++++++++++++
  1 file changed, 69 insertions(+)

create mode 100644 Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +48 -0
new file mode 100644
index 000000000000..e7feae6d4404
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) 2022 SiFive, Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/pwm/snps,dw-apb-timers-pwm2.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Synopsys DW-APB timers PWM controller
+
+maintainers:
+  - Ben Dooks <[email protected]>
+
+description:
+  This describes the DesignWare APB timers module when used in the PWM
+  mode. The IP core can be generated with various options which can
+  control the functionality, the number of PWMs available and other
+  internal controls the designer requires.
+
+  The IP block has a version register so this can be used for detection
+  instead of having to encode the IP version number in the device tree
+  comaptible.
+
+allOf:
+  - $ref: pwm.yaml#
+
+properties:
+  compatible:
+    const: snps,dw-apb-timers-pwm2
+
+  "#pwm-cells":
+    const: 3
+
+  clocks:
+    items:
+      - description: Interface bus clock
+      - description: PWM reference clock
+
+  clock-names:
+    items:
+      - const: bus
+      - const: timer
+
+  snps,pwm-number:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: The number of PWM channels configured for this instance
+    enum: [1, 2, 3, 4, 5, 6, 7, 8]
+
+  reg:
Quoted 8 lines

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 54

Quoted 6 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +6 -0
+    maxItems: 1
+
+required:
+  - "#pwm-cells"
+  - compatible
+  - reg

Keep the same order as list of properties.

ok, will fix.

Quoted 16 lines

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 60

Quoted 6 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +6 -0
+  - clocks
+  - clock-names
+
+additionalProperties: false
+
+

Just one blank line.

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 66

Quoted 6 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +6 -0
+examples:
+  - |
+    pwm: pwm@180000 {
+      #pwm-cells = <3>;
+      compatible = "snps,dw-apb-timers-pwm2";
+      reg = <0x180000 0x200>;
Quoted 8 lines

The convention of DTS is: compatible, then reg, then rest of properties.

Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml line 67

Quoted 1 line
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +1 -0
+      clocks = <&bus &timer>;

You put here one item, not two. This has to be <&bus>, <&timer>

Quoted 2 lines
Documentation/devicetree/bindings/pwm/snps,dw-apb-timers-pwm2.yaml new file +2 -0
+      clock-names = "bus", "timer";
+    };

Argh, thanks, I completely missed this as our platform only has one clock provider for this (the bus and timer clocks are the same)

Quoted 2 lines

Best regards,
Krzysztof

Thanks for the review.

I guess this is now too late for 6.0-rc ?

#4 lore raw

Re: [RFC v4 01/10] dt-bindings: pwm: Document Synopsys DesignWare snps,pwm-dw-apb-timers-pwm2

Quoted 7 lines · Ben Dooks
Quoted 3 lines

Best regards,
Krzysztof

Thanks for the review.

I guess this is now too late for 6.0-rc ?

Code for 6.0-rc would have to be accepted by subsystem maintainer between 3 to 5 weeks ago (depending on maintainer), so it's late by 3-5 weeks.

Best regards,
Krzysztof

author
#5 lore raw

RFC v4 2/10 pwm: dwc: allow driver to be built with COMPILE_TEST

In reply to Ben Dooks

Allow dwc driver to be built with COMPILE_TEST should allow better coverage when build testing.

---
v4:
- moved to earlier in the series
v3:
- add HAS_IOMEM depdency for compile testing

---
 drivers/pwm/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pwm/Kconfig +2 -1
index 60d13a949bc5..3f3c53af4a56 100644
@@ -176,7 +176,8 @@ config PWM_CROS_EC
 
 config PWM_DWC
 	tristate "DesignWare PWM Controller"
-	depends on PCI
+	depends on PCI || COMPILE_TEST
+	depends on HAS_IOMEM
 	help
 	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
 
Signature · 1 line
2.35.1
Acked-by on v4 2/10
#6 lore raw

Re: [RFC v4 02/10] pwm: dwc: allow driver to be built with COMPILE_TEST

Hello,

Quoted 4 lines · Ben Dooks

Allow dwc driver to be built with COMPILE_TEST should allow better coverage when build testing.

Very welcome, so:

Thanks
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#7 lore raw

Re: [RFC v4 02/10] pwm: dwc: allow driver to be built with COMPILE_TEST

In reply to Ben Dooks

Hello Thierry,

Quoted 4 lines · Ben Dooks

Allow dwc driver to be built with COMPILE_TEST should allow better coverage when build testing.

You marked this patch as "changes requested" without feedback. This patch is part of a bigger series, but makes sense to be applied stand alone, too.

Ditto for patch #3 IMHO.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#8 lore raw

RFC v4 3/10 pwm: dwc: change &pci->dev to dev in probe

In reply to Ben Dooks

The dwc_pwm_probe() assignes dev to be &pci->dev but then uses &pci->dev throughout the function. Change these all to the be 'dev' variable to make lines shorter.

---
 drivers/pwm/pwm-dwc.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/pwm/pwm-dwc.c +4 -6
index 7568300bb11e..c706ef9a7ba1 100644
@@ -202,14 +202,13 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 	struct dwc_pwm *dwc;
 	int ret;
 
-	dwc = devm_kzalloc(&pci->dev, sizeof(*dwc), GFP_KERNEL);
+	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
 	if (!dwc)
 		return -ENOMEM;
 
 	ret = pcim_enable_device(pci);
 	if (ret) {
-		dev_err(&pci->dev,
-			"Failed to enable device (%pe)\n", ERR_PTR(ret));
+		dev_err(dev, "Failed to enable device (%pe)\n", ERR_PTR(ret));
 		return ret;
 	}
 
@@ -217,14 +216,13 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 
 	ret = pcim_iomap_regions(pci, BIT(0), pci_name(pci));
 	if (ret) {
-		dev_err(&pci->dev,
-			"Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret));
+		dev_err(dev, "Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret));
 		return ret;
 	}
 
 	dwc->base = pcim_iomap_table(pci)[0];
 	if (!dwc->base) {
-		dev_err(&pci->dev, "Base address missing\n");
+		dev_err(dev, "Base address missing\n");
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.35.1
author
#9 lore raw

RFC v4 4/10 pwm: dwc: move memory alloc to own function

In reply to Ben Dooks

In preparation for adding other bus support, move the allocation of the pwm struct out of the main driver code.

---
 drivers/pwm/pwm-dwc.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/pwm/pwm-dwc.c +17 -7
index c706ef9a7ba1..61f11e0a9319 100644
@@ -196,13 +196,29 @@ static const struct pwm_ops dwc_pwm_ops = {
 	.owner = THIS_MODULE,
 };
 
+static struct dwc_pwm *dwc_pwm_alloc(struct device *dev)
+{
+	struct dwc_pwm *dwc;
+
+	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
+	if (!dwc)
+		return NULL;
+
+	dwc->chip.dev = dev;
+	dwc->chip.ops = &dwc_pwm_ops;
+	dwc->chip.npwm = DWC_TIMERS_TOTAL;
+
+	dev_set_drvdata(dev, dwc);
+	return dwc;
+}
+
 static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 {
 	struct device *dev = &pci->dev;
 	struct dwc_pwm *dwc;
 	int ret;
 
-	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
+	dwc = dwc_pwm_alloc(dev);
 	if (!dwc)
 		return -ENOMEM;
 
@@ -226,12 +242,6 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 		return -ENOMEM;
 	}
 
-	pci_set_drvdata(pci, dwc);
-
-	dwc->chip.dev = dev;
-	dwc->chip.ops = &dwc_pwm_ops;
-	dwc->chip.npwm = DWC_TIMERS_TOTAL;
-
 	ret = pwmchip_add(&dwc->chip);
 	if (ret)
 		return ret;
Signature · 1 line
2.35.1
author
#10 lore raw

RFC v4 5/10 pwm: dwc: use devm_pwmchip_add

In reply to Ben Dooks

Use devm_pwmchip_add() to add the pwm chip to avoid having to manually remove it (useful for the next patch which adds the platform-device support).

---
 drivers/pwm/pwm-dwc.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/pwm/pwm-dwc.c +1 -5
index 61f11e0a9319..56cde9da2c0e 100644
@@ -242,7 +242,7 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 		return -ENOMEM;
 	}
 
-	ret = pwmchip_add(&dwc->chip);
+	ret = devm_pwmchip_add(dev, &dwc->chip);
 	if (ret)
 		return ret;
 
@@ -254,12 +254,8 @@ static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 
 static void dwc_pwm_remove(struct pci_dev *pci)
 {
-	struct dwc_pwm *dwc = pci_get_drvdata(pci);
-
 	pm_runtime_forbid(&pci->dev);
 	pm_runtime_get_noresume(&pci->dev);
-
-	pwmchip_remove(&dwc->chip);
 }
 
 #ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.35.1
Reviewed-by on v4 5/10
#11 lore raw

Re: [RFC v4 05/10] pwm: dwc: use devm_pwmchip_add

Quoted 5 lines · Ben Dooks

Use devm_pwmchip_add() to add the pwm chip to avoid having to manually remove it (useful for the next patch which adds the platform-device support).

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#12 lore raw

RFC v4 6/10 pwm: dwc: split pci out of core driver

In reply to Ben Dooks

Moving towards adding non-pci support for the driver, move the pci parts out of the core into their own module. This is partly due to the module_driver() code only being allowed once in a module and also to avoid a number of #ifdef if we build a single file in a system without pci support.

---
 drivers/pwm/Kconfig       |  14 +++-
 drivers/pwm/Makefile      |   1 +
 drivers/pwm/pwm-dwc-pci.c | 133 ++++++++++++++++++++++++++++++++
 drivers/pwm/pwm-dwc.c     | 158 +-------------------------------------
 drivers/pwm/pwm-dwc.h     |  58 ++++++++++++++
 5 files changed, 207 insertions(+), 157 deletions(-)
 create mode 100644 drivers/pwm/pwm-dwc-pci.c
 create mode 100644 drivers/pwm/pwm-dwc.h
drivers/pwm/Kconfig +11 -3
index 3f3c53af4a56..a9f1c554db2b 100644
@@ -175,15 +175,23 @@ config PWM_CROS_EC
 	  Controller.
 
 config PWM_DWC
-	tristate "DesignWare PWM Controller"
-	depends on PCI || COMPILE_TEST
+	tristate "DesignWare PWM Controller core"
 	depends on HAS_IOMEM
 	help
-	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+	  PWM driver for Synopsys DWC PWM Controller.
 
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-dwc.
 
+config PWM_DWC_PCI
+	tristate "DesignWare PWM Controller core"
+	depends on PWM_DWC && HAS_IOMEM && PCI
+	help
+	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc-pci.
+
 config PWM_EP93XX
 	tristate "Cirrus Logic EP93xx PWM support"
 	depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 7bf1a29f02b8..a70d36623129 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
 obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
 obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_PCI)	+= pwm-dwc-pci.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
 obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
drivers/pwm/pwm-dwc-pci.c new file +133 -0

133 lines not on this page · raw message

drivers/pwm/pwm-dwc.c +4 -154

187 lines not on this page · raw message

drivers/pwm/pwm-dwc.h new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.35.1
#13 lore raw

Re: [RFC v4 06/10] pwm: dwc: split pci out of core driver

Hi

Quoted 8 lines · Ben Dooks

Moving towards adding non-pci support for the driver, move the pci parts out of the core into their own module. This is partly due to the module_driver() code only being allowed once in a module and also to avoid a number of #ifdef if we build a single file in a system without pci support.

---

I quickly tested this on Intel Elkhart and didn't notice any regression. A few comments below.

Quoted 20 lines
  drivers/pwm/Kconfig       |  14 +++-
  drivers/pwm/Makefile      |   1 +
  drivers/pwm/pwm-dwc-pci.c | 133 ++++++++++++++++++++++++++++++++
  drivers/pwm/pwm-dwc.c     | 158 +-------------------------------------
  drivers/pwm/pwm-dwc.h     |  58 ++++++++++++++
  5 files changed, 207 insertions(+), 157 deletions(-)

create mode 100644 drivers/pwm/pwm-dwc-pci.c
create mode 100644 drivers/pwm/pwm-dwc.h

drivers/pwm/Kconfig +1 -2
index 3f3c53af4a56..a9f1c554db2b 100644
@@ -175,15 +175,23 @@ config PWM_CROS_EC
  	  Controller.
 
  config PWM_DWC
-	tristate "DesignWare PWM Controller"
-	depends on PCI || COMPILE_TEST
+	tristate "DesignWare PWM Controller core"

drivers/pwm/Kconfig config PWM_CROS_EC · line 187

Quoted 10 lines
drivers/pwm/Kconfig +3 -1
  	depends on HAS_IOMEM
  	help
-	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+	  PWM driver for Synopsys DWC PWM Controller.
 
  	  To compile this driver as a module, choose M here: the module
  	  will be called pwm-dwc.
 
+config PWM_DWC_PCI
+	tristate "DesignWare PWM Controller core"

Same text as core part has. How about "DesignWare PWM Controller PCI driver"?

Quoted 33 lines
drivers/pwm/Kconfig +7 -0
+	depends on PWM_DWC && HAS_IOMEM && PCI
+	help
+	  PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc-pci.
+
  config PWM_EP93XX
  	tristate "Cirrus Logic EP93xx PWM support"
  	depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 7bf1a29f02b8..a70d36623129 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
  obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
  obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
  obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_PCI)	+= pwm-dwc-pci.o
  obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
  obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
  obj-$(CONFIG_PWM_HIBVT)		+= pwm-hibvt.o
drivers/pwm/pwm-dwc-pci.c new file +5 -0
new file mode 100644
index 000000000000..2213d0e7f3c8
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DesignWare PWM Controller driver (PCI part)
+ *
+ * Copyright (C) 2018-2020 Intel Corporation

drivers/pwm/pwm-dwc-pci.c line 15

Quoted 10 lines
drivers/pwm/pwm-dwc-pci.c new file +10 -0
+ *
+ * Author: Felipe Balbi (Intel)
+ * Author: Jarkko Nikula <[email protected]>
+ * Author: Raymond Tan <[email protected]>
+ *
+ * Limitations:
+ * - The hardware cannot generate a 0 % or 100 % duty cycle. Both high and low
+ *   periods are one or more input clock periods long.
+ */
+

I think this is more common limitation rather than PCI part.

Quoted 8 lines
drivers/pwm/pwm-dwc.c +1 -1
@@ -1,16 +1,12 @@
  // SPDX-License-Identifier: GPL-2.0
  /*
- * DesignWare PWM Controller driver
+ * DesignWare PWM Controller driver core
   *

drivers/pwm/pwm-dwc.c line 10

Quoted 10 lines
drivers/pwm/pwm-dwc.c +0 -4
   * Copyright (C) 2018-2020 Intel Corporation
   *
   * Author: Felipe Balbi (Intel)
   * Author: Jarkko Nikula <[email protected]>
   * Author: Raymond Tan <[email protected]>
- *
- * Limitations:
- * - The hardware cannot generate a 0 % or 100 % duty cycle. Both high and low
- *   periods are one or more input clock periods long.
   */

Relates to previous comment, is there reason why this limitation is removed from the core part?

drivers/pwm/pwm-dwc.h line 25

Quoted 3 lines
drivers/pwm/pwm-dwc.h new file +1 -0
+#define DWC_CLK_PERIOD_NS	10

Perhaps this addition can be removed if patch 7/10 goes before this patch? It's anyway specific to PCI part only.

author
#14 lore raw

Re: [RFC v4 06/10] pwm: dwc: split pci out of core driver

Quoted 35 lines · Jarkko Nikula

Hi

Quoted 8 lines · Ben Dooks

Moving towards adding non-pci support for the driver, move the pci parts out of the core into their own module. This is partly due to the module_driver() code only being allowed once in a module and also to avoid a number of #ifdef if we build a single file in a system without pci support.

---

I quickly tested this on Intel Elkhart and didn't notice any regression. A few comments below.

Quoted 21 lines

  drivers/pwm/Kconfig       |  14 +++-
  drivers/pwm/Makefile      |   1 + drivers/pwm/pwm-dwc-pci.c | 133 ++++++++++++++++++++++++++++++++ drivers/pwm/pwm-dwc.c     | 158 +------------------------------------- drivers/pwm/pwm-dwc.h     |  58 ++++++++++++++

  5 files changed, 207 insertions(+), 157 deletions(-)

  create mode 100644 drivers/pwm/pwm-dwc-pci.c
  create mode 100644 drivers/pwm/pwm-dwc.h

drivers/pwm/Kconfig +0 -0
index 3f3c53af4a56..a9f1c554db2b 100644
@@ -175,15 +175,23 @@ config PWM_CROS_EC

        Controller.
  config PWM_DWC
-    tristate "DesignWare PWM Controller"
-    depends on PCI || COMPILE_TEST
+    tristate "DesignWare PWM Controller core"
      depends on HAS_IOMEM
      help

Quoted 8 lines
Quoted 6 lines

-      PWM driver for Synopsys DWC PWM Controller attached to a PCI bus. +      PWM driver for Synopsys DWC PWM Controller.
        To compile this driver as a module, choose M here: the module will be called pwm-dwc.
+config PWM_DWC_PCI
+    tristate "DesignWare PWM Controller core"

Same text as core part has. How about "DesignWare PWM Controller PCI driver"?

Thanks, did notice a couple of kconfig issues so will look at that.

Quoted 39 lines
Quoted 39 lines

+    depends on PWM_DWC && HAS_IOMEM && PCI
+    help
+      PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.

drivers/pwm/Kconfig +4 -0
+
+      To compile this driver as a module, choose M here: the module
+      will be called pwm-dwc-pci.
+

  config PWM_EP93XX
      tristate "Cirrus Logic EP93xx PWM support"
      depends on ARCH_EP93XX || COMPILE_TEST

drivers/pwm/Makefile +0 -0
index 7bf1a29f02b8..a70d36623129 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)    += pwm-clps711x.o

  obj-$(CONFIG_PWM_CRC)        += pwm-crc.o
  obj-$(CONFIG_PWM_CROS_EC)    += pwm-cros-ec.o
  obj-$(CONFIG_PWM_DWC)        += pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_PCI)    += pwm-dwc-pci.o
  obj-$(CONFIG_PWM_EP93XX)    += pwm-ep93xx.o
  obj-$(CONFIG_PWM_FSL_FTM)    += pwm-fsl-ftm.o
  obj-$(CONFIG_PWM_HIBVT)        += pwm-hibvt.o

drivers/pwm/pwm-dwc-pci.c new file +10 -0
new file mode 100644
index 000000000000..2213d0e7f3c8
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DesignWare PWM Controller driver (PCI part)
+ *
+ * Copyright (C) 2018-2020 Intel Corporation
+ *
+ * Author: Felipe Balbi (Intel)
+ * Author: Jarkko Nikula <[email protected]>
+ * Author: Raymond Tan <[email protected]>
+ *
Quoted 8 lines
Quoted 6 lines
drivers/pwm/pwm-dwc-pci.c new file +2 -0
+ * Limitations:
+ * - The hardware cannot generate a 0 % or 100 % duty cycle. Both 

high and low
+ *   periods are one or more input clock periods long.
+ */
+

I think this is more common limitation rather than PCI part.

The PCI is based off an core without the support, it is added as a build option as of (IIRC) the 2.13 core.

Quoted 14 lines
Quoted 14 lines
drivers/pwm/pwm-dwc.c +1 -1
@@ -1,16 +1,12 @@
   // SPDX-License-Identifier: GPL-2.0
   /*
- * DesignWare PWM Controller driver
+ * DesignWare PWM Controller driver core
    *
    * Copyright (C) 2018-2020 Intel Corporation
    *
    * Author: Felipe Balbi (Intel)
    * Author: Jarkko Nikula <[email protected]>
    * Author: Raymond Tan <[email protected]>
Quoted 8 lines
Quoted 6 lines
drivers/pwm/pwm-dwc.c +0 -3
- *
- * Limitations:
- * - The hardware cannot generate a 0 % or 100 % duty cycle. Both 

high and low
- *   periods are one or more input clock periods long. */

Relates to previous comment, is there reason why this limitation is removed from the core part?

See above.

Quoted 6 lines

drivers/pwm/pwm-dwc.h line 25

Quoted 4 lines
drivers/pwm/pwm-dwc.h new file +1 -0
+#define DWC_CLK_PERIOD_NS    10

Perhaps this addition can be removed if patch 7/10 goes before this patch? It's anyway specific to PCI part only.

Will look into that

author
#15 lore raw

RFC v4 7/10 pwm: dwc: make timer clock configurable

In reply to Ben Dooks

Add a configurable clock base rate for the pwm as when being built for non-PCI the block may be sourced from an internal clock.

---
v4:
- moved earlier before the of changes to make the of changes one patch
v2:
- removed the ifdef and merged the other clock patch in here

---
 drivers/pwm/pwm-dwc-pci.c |  1 +
 drivers/pwm/pwm-dwc.c     | 10 ++++++----
 drivers/pwm/pwm-dwc.h     |  2 ++
 3 files changed, 9 insertions(+), 4 deletions(-)
drivers/pwm/pwm-dwc-pci.c +1 -0
index 2213d0e7f3c8..949423e368f9 100644
@@ -20,6 +20,7 @@
 #include <linux/pci.h>
 #include <linux/pm_runtime.h>
 #include <linux/pwm.h>
+#include <linux/clk.h>
 
 #include "pwm-dwc.h"
 
drivers/pwm/pwm-dwc.c +6 -4
index 90a8ae1252a1..1251620ab771 100644
@@ -14,6 +14,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/clk.h>
 #include <linux/pm_runtime.h>
 #include <linux/pwm.h>
 
@@ -47,13 +48,13 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
 	 * periods and check are the result within HW limits between 1 and
 	 * 2^32 periods.
 	 */
-	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, DWC_CLK_PERIOD_NS);
+	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);
 	if (tmp < 1 || tmp > (1ULL << 32))
 		return -ERANGE;
 	low = tmp - 1;
 
 	tmp = DIV_ROUND_CLOSEST_ULL(state->period - state->duty_cycle,
-				    DWC_CLK_PERIOD_NS);
+				    dwc->clk_ns);
 	if (tmp < 1 || tmp > (1ULL << 32))
 		return -ERANGE;
 	high = tmp - 1;
@@ -128,12 +129,12 @@ static void dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	duty = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm));
 	duty += 1;
-	duty *= DWC_CLK_PERIOD_NS;
+	duty *= dwc->clk_ns;
 	state->duty_cycle = duty;
 
 	period = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(pwm->hwpwm));
 	period += 1;
-	period *= DWC_CLK_PERIOD_NS;
+	period *= dwc->clk_ns;
 	period += duty;
 	state->period = period;
 
@@ -156,6 +157,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)
 	if (!dwc)
 		return NULL;
 
+	dwc->clk_ns = 10;
 	dwc->chip.dev = dev;
 	dwc->chip.ops = &dwc_pwm_ops;
 	dwc->chip.npwm = DWC_TIMERS_TOTAL;
drivers/pwm/pwm-dwc.h +2 -0
index 68f98eb76152..e5a1f7be7bc8 100644
@@ -41,6 +41,8 @@ struct dwc_pwm_ctx {
 struct dwc_pwm {
 	struct pwm_chip chip;
 	void __iomem *base;
+	struct clk *clk;
+	unsigned int clk_ns;
 	struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL];
 };
 #define to_dwc_pwm(p)	(container_of((p), struct dwc_pwm, chip))
Signature · 1 line
2.35.1
#16 lore raw

Re: [RFC v4 07/10] pwm: dwc: make timer clock configurable

Hello Ben,

Quoted 20 lines · Ben Dooks

Add a configurable clock base rate for the pwm as when being built for non-PCI the block may be sourced from an internal clock.

---
v4:
- moved earlier before the of changes to make the of changes one patch
v2:
- removed the ifdef and merged the other clock patch in here

---
 drivers/pwm/pwm-dwc-pci.c |  1 +
 drivers/pwm/pwm-dwc.c     | 10 ++++++----
 drivers/pwm/pwm-dwc.h     |  2 ++
 3 files changed, 9 insertions(+), 4 deletions(-)
drivers/pwm/pwm-dwc-pci.c +0 -0
index 2213d0e7f3c8..949423e368f9 100644

drivers/pwm/pwm-dwc-pci.c line 26

Quoted 7 lines
drivers/pwm/pwm-dwc-pci.c +1 -0
@@ -20,6 +20,7 @@
 #include <linux/pci.h>
 #include <linux/pm_runtime.h>
 #include <linux/pwm.h>
+#include <linux/clk.h>
 
 #include "pwm-dwc.h"
 

This hunk is strange. Why do you add a header but no user of a symbol declared in that header? Maybe it should go to pwm-dwc.h?

Quoted 44 lines
drivers/pwm/pwm-dwc.c +5 -4
index 90a8ae1252a1..1251620ab771 100644
@@ -14,6 +14,7 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/clk.h>
 #include <linux/pm_runtime.h>
 #include <linux/pwm.h>
 
@@ -47,13 +48,13 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
 	 * periods and check are the result within HW limits between 1 and
 	 * 2^32 periods.
 	 */
-	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, DWC_CLK_PERIOD_NS);
+	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);
 	if (tmp < 1 || tmp > (1ULL << 32))
 		return -ERANGE;
 	low = tmp - 1;
 
 	tmp = DIV_ROUND_CLOSEST_ULL(state->period - state->duty_cycle,
-				    DWC_CLK_PERIOD_NS);
+				    dwc->clk_ns);
 	if (tmp < 1 || tmp > (1ULL << 32))
 		return -ERANGE;
 	high = tmp - 1;
@@ -128,12 +129,12 @@ static void dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 
 	duty = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm));
 	duty += 1;
-	duty *= DWC_CLK_PERIOD_NS;
+	duty *= dwc->clk_ns;
 	state->duty_cycle = duty;
 
 	period = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(pwm->hwpwm));
 	period += 1;
-	period *= DWC_CLK_PERIOD_NS;
+	period *= dwc->clk_ns;
 	period += duty;
 	state->period = period;
 

drivers/pwm/pwm-dwc.c struct dwc_pwm *dwc_pwm_alloc(struct device *dev) · line 163

Quoted 7 lines
drivers/pwm/pwm-dwc.c +1 -0
@@ -156,6 +157,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)
 	if (!dwc)
 		return NULL;
 
+	dwc->clk_ns = 10;
 	dwc->chip.dev = dev;
 	dwc->chip.ops = &dwc_pwm_ops;
 	dwc->chip.npwm = DWC_TIMERS_TOTAL;

After this conversion all usage of DWC_CLK_PERIOD_NS is done so the #define can go away?

Quoted 17 lines
drivers/pwm/pwm-dwc.h +2 -0
index 68f98eb76152..e5a1f7be7bc8 100644
@@ -41,6 +41,8 @@ struct dwc_pwm_ctx {
 struct dwc_pwm {
 	struct pwm_chip chip;
 	void __iomem *base;
+	struct clk *clk;
+	unsigned int clk_ns;
 	struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL];
 };
 #define to_dwc_pwm(p)	(container_of((p), struct dwc_pwm, chip))
Signature · 1 line
2.35.1
Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#17 lore raw

RFC v4 8/10 pwm: dwc: add of/platform support

In reply to Ben Dooks

The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.

---
v4:
- split the of code out of the core
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
v3:
- changed compatible name

---
 drivers/pwm/Kconfig      |  9 +++++
 drivers/pwm/Makefile     |  1 +
 drivers/pwm/pwm-dwc-of.c | 78 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 drivers/pwm/pwm-dwc-of.c
drivers/pwm/Kconfig +9 -0
index a9f1c554db2b..f1735653365f 100644
@@ -192,6 +192,15 @@ config PWM_DWC_PCI
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-dwc-pci.
 
+config PWM_DWC_OF
+	tristate "DesignWare PWM Controller (OF bus)
+	depends on PWM_DWC && OF
+	help
+	  PWM driver for Synopsys DWC PWM Controller on an OF bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc-of.
+
 config PWM_EP93XX
 	tristate "Cirrus Logic EP93xx PWM support"
 	depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index a70d36623129..d1fd1641f077 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
 obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
 obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_OF)	+= pwm-dwc-of.o
 obj-$(CONFIG_PWM_DWC_PCI)	+= pwm-dwc-pci.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
drivers/pwm/pwm-dwc-of.c new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
2.35.1
#18 lore raw

Re: [RFC v4 08/10] pwm: dwc: add of/platform support

Quoted 81 lines · Ben Dooks

The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.

---
v4:
- split the of code out of the core
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
v3:
- changed compatible name

---
 drivers/pwm/Kconfig      |  9 +++++
 drivers/pwm/Makefile     |  1 +
 drivers/pwm/pwm-dwc-of.c | 78 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 drivers/pwm/pwm-dwc-of.c
drivers/pwm/Kconfig +9 -0
index a9f1c554db2b..f1735653365f 100644
@@ -192,6 +192,15 @@ config PWM_DWC_PCI
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-dwc-pci.
 
+config PWM_DWC_OF
+	tristate "DesignWare PWM Controller (OF bus)
+	depends on PWM_DWC && OF
+	help
+	  PWM driver for Synopsys DWC PWM Controller on an OF bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc-of.
+
 config PWM_EP93XX
 	tristate "Cirrus Logic EP93xx PWM support"
 	depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index a70d36623129..d1fd1641f077 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
 obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
 obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
 obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_OF)	+= pwm-dwc-of.o
 obj-$(CONFIG_PWM_DWC_PCI)	+= pwm-dwc-pci.o
 obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
drivers/pwm/pwm-dwc-of.c new file +23 -0
new file mode 100644
index 000000000000..d18fac287325
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DesignWare PWM Controller driver OF
+ *
+ * Copyright (C) 2022 SiFive, Inc.
+ */
+
+#include <linux/bitops.h>
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/pwm.h>
+#include <linux/io.h>
+
+#include "pwm-dwc.h"
+
+static int dwc_pwm_plat_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dwc_pwm *dwc;

drivers/pwm/pwm-dwc-of.c line 33

Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10 -0
+	int ret;
+
+	dwc = dwc_pwm_alloc(dev);
+	if (!dwc)
+		return -ENOMEM;
+
+	dwc->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(dwc->base))
+		return dev_err_probe(dev, PTR_ERR(dwc->base),
+				     "failed to map IO\n");

devm_platform_ioremap_resource() already emits an error message.

drivers/pwm/pwm-dwc-of.c line 40

Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7 -0
+
+	dwc->clk = devm_clk_get_enabled(dev, "timer");
+	if (IS_ERR(dwc->clk))
+		return dev_err_probe(dev, PTR_ERR(dwc->clk),
+				     "failed to get timer clock\n");
+
+	clk_prepare_enable(dwc->clk);

You don't need clk_prepare_enable() as you used devm_clk_get_enabled().

(Otherwise, when keeping clk_prepare_enable() you need to check the return value.)

drivers/pwm/pwm-dwc-of.c line 41

Quoted 1 line
drivers/pwm/pwm-dwc-of.c new file +1 -0
+	dwc->clk_ns = NSEC_PER_SEC / clk_get_rate(dwc->clk);

I think I already pointed out this is non-optimal.

Later you use clk_ns in __dwc_pwm_configure_timer():

	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);

So what you really want here is:

	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * clk_get_rate(dwc->clk), NSEC_PER_SEC);

With for example clk_get_rate(dwc->clk) = 201171875 and duty_cycle = 4096 you now get clk_ns = 4 (while the exact value is 4.97087..), tmp = 1024, while the exact value is 824.

So the idea is to add a clkrate member to the private driver struct, let it default to 100000000 for the pci case and use the line I suggested above.

drivers/pwm/pwm-dwc-of.c line 47

Quoted 6 lines
drivers/pwm/pwm-dwc-of.c new file +6 -0
+
+	ret = devm_pwmchip_add(dev, &dwc->chip);
+	if (ret)
+		return ret;
+
+	return 0;

This is equivalent to

	return ret;

drivers/pwm/pwm-dwc-of.c line 56

Quoted 9 lines
drivers/pwm/pwm-dwc-of.c new file +9 -0
+}
+
+static int dwc_pwm_plat_remove(struct platform_device *pdev)
+{
+	struct dwc_pwm *dwc = platform_get_drvdata(pdev);
+
+	clk_disable_unprepare(dwc->clk);
+	return 0;
+}

When dropping clk_prepare_enable() the .remove callback can go away, too.

Quoted 12 lines
drivers/pwm/pwm-dwc-of.c new file +12 -0
+
+static const struct of_device_id dwc_pwm_dt_ids[] = {
+	{ .compatible = "snps,dw-apb-timers-pwm2" },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, dwc_pwm_dt_ids);
+
+static struct platform_driver dwc_pwm_plat_driver = {
+	.driver = {
+		.name		= "dwc-pwm",
+		.of_match_table  = dwc_pwm_dt_ids,
+	},

drivers/pwm/pwm-dwc-of.c line 78

Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10 -0
+	.probe	= dwc_pwm_plat_probe,
+	.remove	= dwc_pwm_plat_remove,
+};
+
+module_platform_driver(dwc_pwm_plat_driver);
+
+MODULE_ALIAS("platform:dwc-pwm-of");
+MODULE_AUTHOR("Ben Dooks <[email protected]>");
+MODULE_DESCRIPTION("DesignWare PWM Controller");
+MODULE_LICENSE("GPL");

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#19 lore raw

Re: [RFC v4 08/10] pwm: dwc: add of/platform support

Quoted 86 lines · Uwe Kleine-König
Quoted 85 lines · Ben Dooks

The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.

---
v4:
- split the of code out of the core
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
v3:
- changed compatible name

---
  drivers/pwm/Kconfig      |  9 +++++
  drivers/pwm/Makefile     |  1 +
  drivers/pwm/pwm-dwc-of.c | 78 ++++++++++++++++++++++++++++++++++++++++
  3 files changed, 88 insertions(+)

create mode 100644 drivers/pwm/pwm-dwc-of.c

drivers/pwm/Kconfig +9 -0
index a9f1c554db2b..f1735653365f 100644
@@ -192,6 +192,15 @@ config PWM_DWC_PCI
  	  To compile this driver as a module, choose M here: the module
  	  will be called pwm-dwc-pci.
 
+config PWM_DWC_OF
+	tristate "DesignWare PWM Controller (OF bus)
+	depends on PWM_DWC && OF
+	help
+	  PWM driver for Synopsys DWC PWM Controller on an OF bus.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-dwc-of.
+
  config PWM_EP93XX
  	tristate "Cirrus Logic EP93xx PWM support"
  	depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index a70d36623129..d1fd1641f077 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLPS711X)	+= pwm-clps711x.o
  obj-$(CONFIG_PWM_CRC)		+= pwm-crc.o
  obj-$(CONFIG_PWM_CROS_EC)	+= pwm-cros-ec.o
  obj-$(CONFIG_PWM_DWC)		+= pwm-dwc.o
+obj-$(CONFIG_PWM_DWC_OF)	+= pwm-dwc-of.o
  obj-$(CONFIG_PWM_DWC_PCI)	+= pwm-dwc-pci.o
  obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
  obj-$(CONFIG_PWM_FSL_FTM)	+= pwm-fsl-ftm.o
drivers/pwm/pwm-dwc-of.c new file +27 -0
new file mode 100644
index 000000000000..d18fac287325
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DesignWare PWM Controller driver OF
+ *
+ * Copyright (C) 2022 SiFive, Inc.
+ */
+
+#include <linux/bitops.h>
+#include <linux/export.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/clk.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/pwm.h>
+#include <linux/io.h>
+
+#include "pwm-dwc.h"
+
+static int dwc_pwm_plat_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dwc_pwm *dwc;
+	int ret;
+
+	dwc = dwc_pwm_alloc(dev);
+	if (!dwc)
Quoted 8 lines

drivers/pwm/pwm-dwc-of.c line 33

Quoted 6 lines
drivers/pwm/pwm-dwc-of.c new file +6 -0
+		return -ENOMEM;
+
+	dwc->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(dwc->base))
+		return dev_err_probe(dev, PTR_ERR(dwc->base),
+				     "failed to map IO\n");

devm_platform_ioremap_resource() already emits an error message.

ok, fixed

Quoted 10 lines

drivers/pwm/pwm-dwc-of.c line 40

Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +7 -0
+
+	dwc->clk = devm_clk_get_enabled(dev, "timer");
+	if (IS_ERR(dwc->clk))
+		return dev_err_probe(dev, PTR_ERR(dwc->clk),
+				     "failed to get timer clock\n");
+
+	clk_prepare_enable(dwc->clk);

You don't need clk_prepare_enable() as you used devm_clk_get_enabled().

Quoted 2 lines

(Otherwise, when keeping clk_prepare_enable() you need to check the return value.)

ok, fixed. I didn't notice that when changing to devm_

Quoted 13 lines

drivers/pwm/pwm-dwc-of.c line 41

Quoted 2 lines
drivers/pwm/pwm-dwc-of.c new file +1 -0
+	dwc->clk_ns = NSEC_PER_SEC / clk_get_rate(dwc->clk);

I think I already pointed out this is non-optimal.

Later you use clk_ns in __dwc_pwm_configure_timer():

	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);

So what you really want here is:

	tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * clk_get_rate(dwc->clk), NSEC_PER_SEC);
Quoted 7 lines

With for example clk_get_rate(dwc->clk) = 201171875 and duty_cycle = 4096 you now get clk_ns = 4 (while the exact value is 4.97087..), tmp = 1024, while the exact value is 824.

So the idea is to add a clkrate member to the private driver struct, let it default to 100000000 for the pci case and use the line I suggested above.

ok, will consider keeping the rate in hz and modifiying the pci version to use 10 * NSEC_PER_SEC for the rate.

I've been trying to avoid changing the pci case, but I think for anything with a clk pointer we should be doing clk_get_rate in the calc code

Quoted 11 lines

drivers/pwm/pwm-dwc-of.c line 47

Quoted 7 lines
drivers/pwm/pwm-dwc-of.c new file +6 -0
+
+	ret = devm_pwmchip_add(dev, &dwc->chip);
+	if (ret)
+		return ret;
+
+	return 0;

This is equivalent to

	return ret;

Will sort that out.

Quoted 4 lines
Quoted 4 lines
drivers/pwm/pwm-dwc-of.c new file +4 -0
+}
+
+static int dwc_pwm_plat_remove(struct platform_device *pdev)
+{
Quoted 8 lines

drivers/pwm/pwm-dwc-of.c line 56

Quoted 5 lines
drivers/pwm/pwm-dwc-of.c new file +5 -0
+	struct dwc_pwm *dwc = platform_get_drvdata(pdev);
+
+	clk_disable_unprepare(dwc->clk);
+	return 0;
+}

When dropping clk_prepare_enable() the .remove callback can go away, too.

thanks, spotted that one a while ago.

Quoted 26 lines
Quoted 12 lines
drivers/pwm/pwm-dwc-of.c new file +12 -0
+
+static const struct of_device_id dwc_pwm_dt_ids[] = {
+	{ .compatible = "snps,dw-apb-timers-pwm2" },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, dwc_pwm_dt_ids);
+
+static struct platform_driver dwc_pwm_plat_driver = {
+	.driver = {
+		.name		= "dwc-pwm",
+		.of_match_table  = dwc_pwm_dt_ids,
+	},

drivers/pwm/pwm-dwc-of.c line 78

Quoted 10 lines
drivers/pwm/pwm-dwc-of.c new file +10 -0
+	.probe	= dwc_pwm_plat_probe,
+	.remove	= dwc_pwm_plat_remove,
+};
+
+module_platform_driver(dwc_pwm_plat_driver);
+
+MODULE_ALIAS("platform:dwc-pwm-of");
+MODULE_AUTHOR("Ben Dooks <[email protected]>");
+MODULE_DESCRIPTION("DesignWare PWM Controller");
+MODULE_LICENSE("GPL");

Best regards
Uwe

#20 lore raw

Re: [RFC v4 08/10] pwm: dwc: add of/platform support

In reply to Ben Dooks

Hello,

Quoted 25 lines · Ben Dooks

The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.

---
v4:
- split the of code out of the core
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
v3:
- changed compatible name

---
 drivers/pwm/Kconfig      |  9 +++++
 drivers/pwm/Makefile     |  1 +
 drivers/pwm/pwm-dwc-of.c | 78 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 drivers/pwm/pwm-dwc-of.c
drivers/pwm/Kconfig +0 -0
index a9f1c554db2b..f1735653365f 100644

drivers/pwm/Kconfig config PWM_DWC_PCI · line 196

Quoted 5 lines
drivers/pwm/Kconfig +2 -0
@@ -192,6 +192,15 @@ config PWM_DWC_PCI
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-dwc-pci.
 
+config PWM_DWC_OF
+	tristate "DesignWare PWM Controller (OF bus)

There is a missing " which results in:

drivers/pwm/Kconfig:196:warning: multi-line strings not supported

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#21 lore raw

Re: [RFC v4 08/10] pwm: dwc: add of/platform support

Quoted 36 lines · Uwe Kleine-König

Hello,

Quoted 25 lines · Ben Dooks

The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.

---
v4:
- split the of code out of the core
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
v3:
- changed compatible name

---
  drivers/pwm/Kconfig      |  9 +++++
  drivers/pwm/Makefile     |  1 +
  drivers/pwm/pwm-dwc-of.c | 78 ++++++++++++++++++++++++++++++++++++++++
  3 files changed, 88 insertions(+)

create mode 100644 drivers/pwm/pwm-dwc-of.c

drivers/pwm/Kconfig +0 -0
index a9f1c554db2b..f1735653365f 100644

drivers/pwm/Kconfig config PWM_DWC_PCI · line 196

Quoted 5 lines
drivers/pwm/Kconfig +2 -0
@@ -192,6 +192,15 @@ config PWM_DWC_PCI
  	  To compile this driver as a module, choose M here: the module
  	  will be called pwm-dwc-pci.
 
+config PWM_DWC_OF
+	tristate "DesignWare PWM Controller (OF bus)

There is a missing " which results in:

Quoted 4 lines

drivers/pwm/Kconfig:196:warning: multi-line strings not supported

Best regards
Uwe

Thanks, fixed.

author
#22 lore raw

RFC v4 9/10 pwm: dwc: add snps,pwm-number to limit pwm count

In reply to Ben Dooks

Add snps,pwm-number property to indicate if the block does not have all 8 of the PWM blocks.

Not sure if this should be a general PWM property consider optional for all PWM types, so have added a specific one here (there is only one other controller with a property for PWM count at the moment)

---
 drivers/pwm/pwm-dwc-of.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/pwm/pwm-dwc-of.c +8 -0
index d18fac287325..65c7e6621bba 100644
@@ -21,12 +21,20 @@ static int dwc_pwm_plat_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct dwc_pwm *dwc;
+	u32 nr_pwm;
 	int ret;
 
 	dwc = dwc_pwm_alloc(dev);
 	if (!dwc)
 		return -ENOMEM;
 
+	if (!device_property_read_u32(dev, "snps,pwm-number", &nr_pwm)) {
+		if (nr_pwm > DWC_TIMERS_TOTAL)
+			dev_err(dev, "too many PWMs specified (%d)\n", nr_pwm);
+		else
+			dwc->chip.npwm = nr_pwm;
+	}
+
 	dwc->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(dwc->base))
 		return dev_err_probe(dev, PTR_ERR(dwc->base),
Signature · 1 line
2.35.1
#23 lore raw

Re: [RFC v4 09/10] pwm: dwc: add snps,pwm-number to limit pwm count

Quoted 20 lines · Ben Dooks

Add snps,pwm-number property to indicate if the block does not have all 8 of the PWM blocks.

Not sure if this should be a general PWM property consider optional for all PWM types, so have added a specific one here (there is only one other controller with a property for PWM count at the moment)

---
 drivers/pwm/pwm-dwc-of.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/pwm/pwm-dwc-of.c +0 -0
index d18fac287325..65c7e6621bba 100644
@@ -21,12 +21,20 @@ static int dwc_pwm_plat_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct dwc_pwm *dwc;

drivers/pwm/pwm-dwc-of.c static int dwc_pwm_plat_probe(struct platform_device *pdev) · line 33

Quoted 10 lines
drivers/pwm/pwm-dwc-of.c +4 -0
+	u32 nr_pwm;
 	int ret;
 
 	dwc = dwc_pwm_alloc(dev);
 	if (!dwc)
 		return -ENOMEM;
 
+	if (!device_property_read_u32(dev, "snps,pwm-number", &nr_pwm)) {
+		if (nr_pwm > DWC_TIMERS_TOTAL)
+			dev_err(dev, "too many PWMs specified (%d)\n", nr_pwm);

Maybe

	dev_err(dev, "too many PWMs specified (%d), falling back to " #DWC_TIMERS_TOTAL "\n", nr_pwm);

to make it obvious the error doesn't prevent probing the device.

Or you believe the dtb and use whatever it specifies.

drivers/pwm/pwm-dwc-of.c static int dwc_pwm_plat_probe(struct platform_device *pdev) · line 40

Quoted 7 lines
drivers/pwm/pwm-dwc-of.c +4 -0
+		else
+			dwc->chip.npwm = nr_pwm;
+	}
+
 	dwc->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(dwc->base))
 		return dev_err_probe(dev, PTR_ERR(dwc->base),

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#24 lore raw

RFC v4 10/10 pwm: dwc: add PWM bit unset in get_state call

In reply to Ben Dooks

If we are not in PWM mode, then the output is technically a 50% output based on a single timer instead of the high-low based on the two counters. Add a check for the PWM mode in dwc_pwm_get_state() and if DWC_TIM_CTRL_PWM is not set, then return a 50% cycle.

This may only be an issue on initialisation, as the rest of the code currently assumes we're always going to have the extended PWM mode using two counters.

---
v4:
- fixed review comment on mulit-line calculations

---
 drivers/pwm/pwm-dwc.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
drivers/pwm/pwm-dwc.c +18 -11
index 1251620ab771..5ef0fe7ea3e9 100644
@@ -121,23 +121,30 @@ static void dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 {
 	struct dwc_pwm *dwc = to_dwc_pwm(chip);
 	u64 duty, period;
+	u32 ctrl, ld, ld2;
 
 	pm_runtime_get_sync(chip->dev);
 
-	state->enabled = !!(dwc_pwm_readl(dwc,
-				DWC_TIM_CTRL(pwm->hwpwm)) & DWC_TIM_CTRL_EN);
+	ctrl = dwc_pwm_readl(dwc, DWC_TIM_CTRL(pwm->hwpwm));
+	ld = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm));
+	ld2 = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(pwm->hwpwm));
 
-	duty = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm));
-	duty += 1;
-	duty *= dwc->clk_ns;
-	state->duty_cycle = duty;
+	state->enabled = !!(ctrl & DWC_TIM_CTRL_EN);
 
-	period = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(pwm->hwpwm));
-	period += 1;
-	period *= dwc->clk_ns;
-	period += duty;
-	state->period = period;
+	/* If we're not in PWM, technically the output is a 50-50
+	 * based on the timer load-count only.
+	 */
+	if (ctrl & DWC_TIM_CTRL_PWM) {
+		duty = (ld + 1) * dwc->clk_ns;
+		period = (ld2 + 1)  * dwc->clk_ns;
+		period += duty;
+	} else {
+		duty = (ld + 1) * dwc->clk_ns;
+		period = duty * 2;
+	}
 
+	state->period = period;
+	state->duty_cycle = duty;
 	state->polarity = PWM_POLARITY_INVERSED;
 
 	pm_runtime_put_sync(chip->dev);
Signature · 1 line
2.35.1
#25 lore raw
In reply to Ben Dooks

Hello Ben,

there was some feedback to individual patches that require a rework. I'm marking the whole series as "changes requested" in the expectation that you will resend them all once you addressed the feedback.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
author
#26 lore raw
Quoted 5 lines · Uwe Kleine-König

Hello Ben,

there was some feedback to individual patches that require a rework. I'm marking the whole series as "changes requested" in the expectation that you will resend them all once you addressed the feedback.

I have most of the feedback done but we've not had a chance to test them fully. I can send a new series later today but the changes for the clock division calculations have yet to be tested.

Signature · 1 line
Ben