Skip to contentSkip to conversation

Introduce Allwinner H616 PWM controller

PATCH Superseded Richard Genoud 3 participants 9 messages Apr 16

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover Introduce Allwinner H616 PWM controller

Allwinner H616 PWM controller is quite different from the A10 one.

It can drive 6 PWM channels, and like for the A10, each channel has a bypass that permits to output a clock, bypassing the PWM logic, when enabled.

But, the channels are paired 2 by 2, sharing a first set of MUX/prescaler/gate.
Then, for each channel, there's another prescaler (that will be bypassed if the bypass is enabled for this channel).

It looks like that:

            _____      ______      ________
OSC24M --->|     |    |      |    |        |
APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> PWM_clock_src_xy
           |_____|    |______|    |________|
                          ________
                         |        |
                      +->| /div_k |---> PWM_clock_x
                      |  |________|
                      |    ______
                      |   |      |
                      +-->| Gate |----> PWM_bypass_clock_x
                      |   |______|
PWM_clock_src_xy -----+   ________
                      |  |        |
                      +->| /div_k |---> PWM_clock_y
                      |  |________|
                      |    ______
                      |   |      |
                      +-->| Gate |----> PWM_bypass_clock_y
                          |______|

Where xy can be 0/1, 2/3, 4/5

PWM_clock_x/y serve for the PWM purpose. PWM_bypass_clock_x/y serve for the clock-provider purpose. The common clock framework has been used to manage those clocks.

This PWM driver serves as a clock-provider for PWM_bypass_clocks. This is needed for example by the embedded AC300 PHY which clock comes from PMW5 pin (PB12).

Usually, to get a clock from a PWM driver, we use the pwm-clock driver so that the PWM driver doesn't need to be a clk-provider itself. While this works in most cases, here it just doesn't.
That's because the pwm-clock request a period from the PWM driver, without any clue that it actually wants a clock at a specific frequency, and not a PWM signal with duty cycle capability.
So, the PWM driver doesn't know if it can use the bypass or not, it doesn't even have the real accurate frequency information (23809524 Hz instead of 24MHz) because PWM drivers only deal with periods.

With pwm-clock, we loose a precious information along the way (that we actually want a clock and not a PWM signal).
That's ok with simple PWM drivers that don't have multiple input clocks, but in this case, without this information, we can't know for sure which clock to use.
And here, for instance, if we ask for a 24MHz clock, pwm-clock will requests 42ns (assigned-clocks doesn't help for that matter). The logic is to select the highest clock (100MHz) with no prescaler and a duty cycle value of 2/4 => we have 25MHz instead of 24MHz.
And that's a perfectly fine choice for a PMW, because we still can change the duty cycle in the range [0-4]/4.
But obviously for a clock, we don't care about the duty cycle, but more about the clock accuracy.

And actually, this PWM is really a PWM AND a real clock when the bypass is set.

This series is based onto v7.0

NB: checkpatch is not happy with patch 2, but it's a false positive. It doesn't detect that PWM_XY_SRC_MUX/GATE/DIV are structures, but as it's more readable like that, I prefer keeping it that way.

Changes since v4:
- Fix a bug on bypass for channels greater than 1
- add colons to clarify 2 debug messages
- switch from H616 to sun8i prefix (in code, filename, module name)
- fix consistency issues in macro parameters
- rename some macros with a confusing naming
- rebase on v7.0

Changes since v3:
- gather Acked-by/Tested-by
- fix cast from pointer to integer of different size (kernel test robot with arc platform)
- add devm_action for clk_hw_unregister_composite as suggested by Philipp
- remove now unused pwm_remove as suggested by Philipp

Changes since v2:
- use U32_MAX instead of defining UINT32_MAX
- add a comment on U32_MAX usage in clk_round_rate()
- change clk_table_div_m (use macros)
- fix formatting (double space, superfluous comma, extra line feed)
- fix the parent clock order
- simplify code by using scoped_guard()
- add missing const in to_h616_pwm_chip() and rename to h616_pwm_from_chip()
- add/remove missing/superfluous error messages
- rename cnt->period_ticks, duty_cnt->duty_ticks
- fix PWM_PERIOD_MAX
- add .remove() callback
- fix DIV_ROUND_CLOSEST_ULL->DIV_ROUND_UP_ULL
- add H616_ prefix
- protect _reg in macros
- switch to waveforms instead of apply/get_state
- shrink struct h616_pwm_channel
- rebase on v6.19-rc4

Changes since v1:
- rebase onto v6.19-rc1
- add missing headers
- remove MODULE_ALIAS (suggested by Krzysztof)
- use sun4i-pwm binding instead of creating a new one (suggested by Krzysztof)
- retrieve the parent clocks from the devicetree
- switch num_parents to unsigned int

Richard Genoud (4):
dt-bindings: pwm: allwinner: add h616 pwm compatible
pwm: sun8i: Add H616 PWM support
arm64: dts: allwinner: h616: add PWM controller
MAINTAINERS: Add entry on Allwinner sun8i/H616 PWM driver

 .../bindings/pwm/allwinner,sun4i-a10-pwm.yaml |  19 +-
 MAINTAINERS                                   |   5 +
 .../arm64/boot/dts/allwinner/sun50i-h616.dtsi |  47 +
 drivers/pwm/Kconfig                           |  12 +
 drivers/pwm/Makefile                          |   1 +
 drivers/pwm/pwm-sun8i.c                       | 938 ++++++++++++++++++
 6 files changed, 1021 insertions(+), 1 deletion(-)
 create mode 100644 drivers/pwm/pwm-sun8i.c

base-commit: 028ef9c96e96197026887c0f092424679298aae8

#1 lore raw

PATCH v5 1/4 dt-bindings: pwm: allwinner: add h616 pwm compatible

Allwinner H616 PWM block is quite different from the A10 or H6, but at the end, it uses the same clocks as the H6; so the sun4i pwm binding can be used.
It has 6 channels than can generate PWM waveforms.
If the bypass is enabled (one bypass per channel) the output is no more a PWM waveform, but a clock that can (and is) used as input for other devices, like the AC300 PHY.

---
 .../bindings/pwm/allwinner,sun4i-a10-pwm.yaml | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/pwm/allwinner,sun4i-a10-pwm.yaml +18 -1
index 1197858e431f..4f58110ec98f 100644
@@ -14,6 +14,9 @@ properties:
   "#pwm-cells":
     const: 3
 
+  "#clock-cells":
+    const: 1
+
   compatible:
     oneOf:
       - const: allwinner,sun4i-a10-pwm
@@ -36,6 +39,7 @@ properties:
           - const: allwinner,sun50i-h5-pwm
           - const: allwinner,sun5i-a13-pwm
       - const: allwinner,sun50i-h6-pwm
+      - const: allwinner,sun50i-h616-pwm
 
   reg:
     maxItems: 1
@@ -62,7 +66,9 @@ allOf:
       properties:
         compatible:
           contains:
-            const: allwinner,sun50i-h6-pwm
+            enum:
+              - allwinner,sun50i-h6-pwm
+              - allwinner,sun50i-h616-pwm
 
     then:
       properties:
@@ -83,6 +89,17 @@ allOf:
         clocks:
           maxItems: 1
 
+  - if:
+      not:
+        properties:
+          compatible:
+            contains:
+              const: allwinner,sun50i-h616-pwm
+
+    then:
+      properties:
+        "#clock-cells": false
+
 required:
   - compatible
   - reg
#2 lore raw

Re: [PATCH v5 1/4] dt-bindings: pwm: allwinner: add h616 pwm compatible

Quoted 10 lines · Richard Genoud

Allwinner H616 PWM block is quite different from the A10 or H6, but at the end, it uses the same clocks as the H6; so the sun4i pwm binding can be used.
It has 6 channels than can generate PWM waveforms.
If the bypass is enabled (one bypass per channel) the output is no more a PWM waveform, but a clock that can (and is) used as input for other devices, like the AC300 PHY.

Please drop or help me understand how one can test bindings on the device? Build process tools are not testing.

Don't blindly copy tags from cover letter.

Best regards,
Krzysztof

#3 lore raw

PATCH v5 2/4 pwm: sun8i: Add H616 PWM support

In reply to Richard Genoud

Add driver for Allwinner H616 PWM controller, supporting up to 6 channels.
Those channels output can be either a PWM signal output or a clock output, thanks to the bypass.

The channels are paired (0/1, 2/3 and 4/5) and each pair has a prescaler/mux/gate.
Moreover, each channel has its own prescaler and bypass.

The clock provider part of this driver is needed not only because the H616 PWM controller provides also clocks when bypass is enabled, but really because pwm-clock isn't fit to handle all cases here. pwm-clock would work if the 100MHz clock is requested, but if a lower clock is requested (like 24MHz), it will request a 42ns period to the PWM driver which will happily serve, with the 100MHz clock as input a 25MHz frequency and a duty cycle adjustable in the range [0-4]/4, because that is a sane thing to do for a PWM.
The information missing is that a real clock is resquested, not a PWM.

---
 drivers/pwm/Kconfig     |  12 +
 drivers/pwm/Makefile    |   1 +
 drivers/pwm/pwm-sun8i.c | 938 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 951 insertions(+)
 create mode 100644 drivers/pwm/pwm-sun8i.c
drivers/pwm/Kconfig +12 -0
index 6f3147518376..c4fd682860d6 100644
@@ -736,6 +736,18 @@ config PWM_SUN4I
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-sun4i.
 
+config PWM_SUN8I
+	tristate "Allwinner sun8i/sun50i PWM support"
+	depends on ARCH_SUNXI || COMPILE_TEST
+	depends on HAS_IOMEM && COMMON_CLK
+	help
+	  Generic PWM framework driver for Allwinner H616 SoCs.
+	  It supports generic PWM, but can also provides a plain clock.
+	  The AC300 PHY integrated in H616 SoC needs such a clock.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-sun8i.
+
 config PWM_SUNPLUS
 	tristate "Sunplus PWM support"
 	depends on ARCH_SUNPLUS || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 0dc0d2b69025..ba2e0ec7fc17 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_PWM_STM32)		+= pwm-stm32.o
 obj-$(CONFIG_PWM_STM32_LP)	+= pwm-stm32-lp.o
 obj-$(CONFIG_PWM_STMPE)		+= pwm-stmpe.o
 obj-$(CONFIG_PWM_SUN4I)		+= pwm-sun4i.o
+obj-$(CONFIG_PWM_SUN8I)		+= pwm-sun8i.o
 obj-$(CONFIG_PWM_SUNPLUS)	+= pwm-sunplus.o
 obj-$(CONFIG_PWM_TEGRA)		+= pwm-tegra.o
 obj-$(CONFIG_PWM_TH1520)	+= pwm_th1520.o
drivers/pwm/pwm-sun8i.c new file +938 -0

938 lines not on this page · raw message

#4 lore raw

PATCH v5 3/4 arm64: dts: allwinner: h616: add PWM controller

In reply to Richard Genoud

The H616 has a PWM controller that can provide PWM signals, but also plain clocks.

Add the PWM controller node and pins in the device tree.

---
 .../arm64/boot/dts/allwinner/sun50i-h616.dtsi | 47 +++++++++++++++++++
 1 file changed, 47 insertions(+)
arch/arm64/boot/dts/allwinner/sun50i-h616.dtsi +47 -0
index 8d1110c14bad..1c7628a6e4bb 100644
@@ -236,6 +236,17 @@ watchdog: watchdog@30090a0 {
 			clocks = <&osc24M>;
 		};
 
+		pwm: pwm@300a000 {
+			compatible = "allwinner,sun50i-h616-pwm";
+			reg = <0x0300a000 0x400>;
+			clocks = <&osc24M>, <&ccu CLK_BUS_PWM>;
+			clock-names = "mod", "bus";
+			resets = <&ccu RST_BUS_PWM>;
+			#pwm-cells = <3>;
+			#clock-cells = <1>;
+			status = "disabled";
+		};
+
 		pio: pinctrl@300b000 {
 			compatible = "allwinner,sun50i-h616-pinctrl";
 			reg = <0x0300b000 0x400>;
@@ -340,6 +351,42 @@ nand_rb1_pin: nand-rb1-pin {
 				bias-pull-up;
 			};
 
+			/omit-if-no-ref/
+			pwm0_pin: pwm0-pin {
+				pins = "PD28";
+				function = "pwm0";
+			};
+
+			/omit-if-no-ref/
+			pwm1_pin: pwm1-pin {
+				pins = "PG19";
+				function = "pwm1";
+			};
+
+			/omit-if-no-ref/
+			pwm2_pin: pwm2-pin {
+				pins = "PH2";
+				function = "pwm2";
+			};
+
+			/omit-if-no-ref/
+			pwm3_pin: pwm3-pin {
+				pins = "PH0";
+				function = "pwm3";
+			};
+
+			/omit-if-no-ref/
+			pwm4_pin: pwm4-pin {
+				pins = "PI14";
+				function = "pwm4";
+			};
+
+			/omit-if-no-ref/
+			pwm5_pin: pwm5-pin {
+				pins = "PA12";
+				function = "pwm5";
+			};
+
 			/omit-if-no-ref/
 			spi0_pins: spi0-pins {
 				pins = "PC0", "PC2", "PC4";
#5 lore raw

PATCH v5 4/4 MAINTAINERS: Add entry on Allwinner sun8i/H616 PWM driver

In reply to Richard Genoud

Add myself as maintainer of Allwinner SUN8I PWM driver.

maintainer

---
 MAINTAINERS | 5 +++++
 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
index d1cc0e12fe1f..256ab8be19f1 100644
@@ -912,6 +912,11 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/hwlock/allwinner,sun6i-a31-hwspinlock.yaml
 F:	drivers/hwspinlock/sun6i_hwspinlock.c
 
+ALLWINNER SUN8I PWM DRIVER
+M:	Richard Genoud <[email protected]>
+S:	Maintained
+F:	drivers/pwm/pwm-sun8i.c
+
 ALLWINNER THERMAL DRIVER
 M:	Vasily Khoruzhick <[email protected]>
 M:	Yangtao Li <[email protected]>
#6 lore raw

Re: [PATCH v5 4/4] MAINTAINERS: Add entry on Allwinner sun8i/H616 PWM driver

Quoted 3 lines · Richard Genoud

Add myself as maintainer of Allwinner SUN8I PWM driver.

Please drop or help me understand how one can test maintainers change? Build process tools are not testing.

Best regards,
Krzysztof

#7 lore raw

Re: [PATCH v5 4/4] MAINTAINERS: Add entry on Allwinner sun8i/H616 PWM driver

Hello,

Quoted 7 lines · Krzysztof Kozlowski
Quoted 3 lines · Richard Genoud

Add myself as maintainer of Allwinner SUN8I PWM driver.

Please drop or help me understand how one can test maintainers change? Build process tools are not testing.

For me that is fine. This is the only way we have to record that John tested the series. Also if I applied the original series I would have let b4 add it to all patches in reply to a "tested-by" send in reply to the cover letter.

My feedback here would be more: Don't send a new revision for such comments within 30 min, also not for

Quoted 1 line

And you have commit msg trailing junk.

. Maintainers differ, but if this is the only concern and the series is fine otherwise, I'd just fix that when applying. (But I think the "don't send a new iteration on the same day" is more universal, also for more fundamental feedback.) There is no need to bother all subscribers of 5 mailing lists with a new thread in such quick sequence.

Best regards
Uwe