Introduce Allwinner H616 PWM controller
Subsystems (3)
v4 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 v6.19-rc4
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.
NB2: for geopolitical reasons, I didn't re-use the old series that Paul was referring to.
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/superflous 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: sun50i: Add H616 PWM support
arm64: dts: allwinner: h616: add PWM controller
MAINTAINERS: Add entry on Allwinner 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-sun50i-h616.c | 936 ++++++++++++++++++ 6 files changed, 1019 insertions(+), 1 deletion(-) create mode 100644 drivers/pwm/pwm-sun50i-h616.c
base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
PATCH v4 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
@@ -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
PATCH v4 2/4 pwm: sun50i: Add H616 PWM support
In reply to Richard GenoudAdd 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-sun50i-h616.c | 936 ++++++++++++++++++++++++++++++++++ 3 files changed, 949 insertions(+) create mode 100644 drivers/pwm/pwm-sun50i-h616.c
drivers/pwm/Kconfig +12 -0
@@ -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_SUN50I_H616 + tristate "Allwinner H616 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-h616. + config PWM_SUNPLUS tristate "Sunplus PWM support" depends on ARCH_SUNPLUS || COMPILE_TEST
drivers/pwm/Makefile +1 -0
@@ -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_SUN50I_H616) += pwm-sun50i-h616.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-sun50i-h616.c new file +936 -0
936 lines not on this page · raw message
Re: [PATCH v4 2/4] pwm: sun50i: Add H616 PWM support
Hi Richard,
drivers/pwm/pwm-sun50i-h616.c line 37
Quoted 2 lines · Richard Genoud
drivers/pwm/pwm-sun50i-h616.c new file +2
-0+/* PWM IRQ Enable Register */ +#define H616_PWM_IER 0x0
I think it would make more sense to keep the full register names from the manual after the suffix and stick to them. It makes things easier when comparing the code with documentation or the reference implementation.
So something like SUN8I_PWM_PIER here.
Quoted 20 lines
drivers/pwm/pwm-sun50i-h616.c new file +20
-0+ +/* PWM IRQ Status Register */ +#define H616_PWM_ISR 0x4 + +/* PWM Capture IRQ Enable Register */ +#define H616_PWM_CIER 0x10 + +/* PWM Capture IRQ Status Register */ +#define H616_PWM_CISR 0x14 + +/* PWMCC Pairs Clock Configuration Registers */ +#define H616_PWM_XY_CLK_CR (pair) (0x20 + ((pair) * 0x4)) +#define H616_PWM_XY_CLK_CR_SRC_SHIFT 7 +#define H616_PWM_XY_CLK_CR_SRC_MASK 1 +#define H616_PWM_XY_CLK_CR_GATE_BIT 4 +#define H616_PWM_XY_CLK_CR_BYPASS_BIT (chan) ((chan) % 2 + 5) +#define H616_PWM_XY_CLK_CR_DIV_M_SHIFT 0 + +/* PWMCC Pairs Dead Zone Control Registers */ +#define H616_PWM_XY_DZ (pair) (0x30 + ((pair) * 0x4))
drivers/pwm/pwm-sun50i-h616.c line 67
Quoted 10 lines
drivers/pwm/pwm-sun50i-h616.c new file +10
-0+ +/* PWM Enable Register */ +#define H616_PWM_ENR 0x40 +#define H616_PWM_ENABLE (x) BIT(x) + +/* PWM Capture Enable Register */ +#define H616_PWM_CER 0x44 + +/* PWM Control Register */ +#define H616_PWM_CTRL_REG (chan) (0x60 + (chan) * 0x20)
You're sometimes calling the register offset _REG and sometimes not. Both options are fine but you need to keep it consistent across the whole definitions. I would be enclined to not use it after using the register names coming from the manual as suggested above.
Also you're sometimes using "chan", sometimes "ch" for the argument to the register macros. This is inconsistent and you might as well just use "c" everywhere so it doesn't take too much space.
drivers/pwm/pwm-sun50i-h616.c line 80
Quoted 13 lines
drivers/pwm/pwm-sun50i-h616.c new file +13
-0+#define H616_PWM_CTRL_PRESCAL_K_SHIFT 0 +#define H616_PWM_CTRL_PRESCAL_K_WIDTH 8 +#define H616_PWM_CTRL_ACTIVE_STATE BIT(8) + +/* PWM Period Register */ +#define H616_PWM_PERIOD_REG (ch) (0x64 + (ch) * 0x20) +#define H616_PWM_PERIOD_MASK GENMASK(31, 16) +#define H616_PWM_DUTY_MASK GENMASK(15, 0) +#define H616_PWM_REG_PERIOD (reg) (FIELD_GET(H616_PWM_PERIOD_MASK, reg) + 1) +#define H616_PWM_REG_DUTY (reg) FIELD_GET(H616_PWM_DUTY_MASK, reg) +#define H616_PWM_PERIOD (prd) FIELD_PREP(H616_PWM_PERIOD_MASK, (prd) - 1) +#define H616_PWM_DUTY (dty) FIELD_PREP(H616_PWM_DUTY_MASK, dty) +#define H616_PWM_PERIOD_MAX (FIELD_MAX(H616_PWM_PERIOD_MASK) + 1)
Using REG as a prefix feels a bit confusing here. I would rather see:
#define SUN8I_PWM_PPR(c) (0x64 + (c) * 0x20)
#define SUN8I_PWM_PPR_PERIOD(p) FIELD_PREP(...)
#define SUN8I_PWM_PPR_PERIOD_VALUE(r) FIELD_GET(...)
#define SUN8I_PWN_PPR_PERIOD_MAX FIELD_MAX(...)
#define SUN8I_PWM_PPR_DUTY(d) FIELD_PREP(...)
#define SUN8I_PWM_PPR_DUTY_VALUE(r) FIELD_GET(...)
Quoted 217 lines
drivers/pwm/pwm-sun50i-h616.c new file +217
-0+ +/* PWM Count Register */ +#define H616_PWM_CNT_REG (x) (0x68 + (x) * 0x20) + +/* PWM Capture Control Register */ +#define H616_PWM_CCR (x) (0x6c + (x) * 0x20) + +/* PWM Capture Rise Lock Register */ +#define H616_PWM_CRLR (x) (0x70 + (x) * 0x20) + +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR (x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX (chan) ((chan) >> 2) + +/* +* Block diagram of the PWM clock controller: +* +* _____ ______ ________ +* OSC24M --->| | | | | | +* APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy +* |_____| |______| |________| +* ________ +* | | +* +->| /div_k |---> H616_PWM_clock_x +* | |________| +* | ______ +* | | | +* +-->| Gate |----> H616_PWM_bypass_clock_x +* | |______| +* H616_PWM_clock_src_xy ----+ ________ +* | | | +* +->| /div_k |---> H616_PWM_clock_y +* | |________| +* | ______ +* | | | +* +-->| Gate |----> H616_PWM_bypass_clock_y +* |______| +* +* NB: when the bypass is set, all the PWM logic is bypassed. +* So, the duty cycle and polarity can't be modified (we just have a clock). +* The bypass in PWM mode is used to achieve a 1/2 relative duty cycle with the +* fastest clock. +* +* H616_PWM_clock_x/y serve for the PWM purpose. +* H616_PWM_bypass_clock_x/y serve for the clock-provider purpose. +* +*/ + +/* +* Table used for /div_m (diviser before obtaining H616_PWM_clock_src_xy) +* It's actually CLK_DIVIDER_POWER_OF_TWO, but limited to /256 +*/ +#define CLK_TABLE_DIV_M_ENTRY (i) { \ + .val = (i), .div = 1 << (i) \ +} + +static const struct clk_div_table clk_table_div_m[] = { +CLK_TABLE_DIV_M_ENTRY (0 ), +CLK_TABLE_DIV_M_ENTRY (1 ), +CLK_TABLE_DIV_M_ENTRY (2 ), +CLK_TABLE_DIV_M_ENTRY (3 ), +CLK_TABLE_DIV_M_ENTRY (4 ), +CLK_TABLE_DIV_M_ENTRY (5 ), +CLK_TABLE_DIV_M_ENTRY (6 ), +CLK_TABLE_DIV_M_ENTRY (7 ), +CLK_TABLE_DIV_M_ENTRY (8 ), + {/* sentinel */ } +}; + +#define H616_PWM_XY_SRC_GATE (_pair, _reg) \ +struct clk_gate gate_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .bit_idx = H616_PWM_XY_CLK_CR_GATE_BIT, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_MUX (_pair, _reg) \ +struct clk_mux mux_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_XY_CLK_CR_SRC_SHIFT, \ + .mask = H616_PWM_XY_CLK_CR_SRC_MASK, \ + .flags = CLK_MUX_ROUND_CLOSEST, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_mux_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_DIV (_pair, _reg) \ +struct clk_divider rate_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_XY_CLK_CR_DIV_M_SHIFT, \ + .table = clk_table_div_m, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_DIV (_idx, _reg) \ +struct clk_divider rate_x_##_idx = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_CTRL_PRESCAL_K_SHIFT, \ + .width = H616_PWM_CTRL_PRESCAL_K_WIDTH, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_BYPASS_GATE (_idx) \ +struct clk_gate gate_x_bypass_##_idx = { \ + .reg = (void *)H616_PWM_ENR, \ + .bit_idx = _idx, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_CLK_SRC (_pair, _reg) \ + static H616_PWM_XY_SRC_MUX(_pair, _reg); \ + static H616_PWM_XY_SRC_GATE(_pair, _reg); \ + static H616_PWM_XY_SRC_DIV(_pair, _reg) + +#define H616_PWM_X_CLK (_idx) \ + static H616_PWM_X_DIV(_idx, H616_PWM_CTRL_REG(_idx)) + +#define H616_PWM_X_BYPASS_CLK (_idx) \ + H616_PWM_X_BYPASS_GATE(_idx) + +#define REF_CLK_XY_SRC (_pair) \ + { \ + .name = "pwm-clk-src" #_pair, \ + .mux_hw = &mux_xy_##_pair.hw, \ + .gate_hw = &gate_xy_##_pair.hw, \ + .rate_hw = &rate_xy_##_pair.hw, \ + } + +#define REF_CLK_X (_idx, _pair) \ + { \ + .name = "pwm-clk" #_idx, \ + .parent_names = (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents = 1, \ + .rate_hw = &rate_x_##_idx.hw, \ + .flags = CLK_SET_RATE_PARENT, \ + } + +#define REF_CLK_BYPASS (_idx, _pair) \ + { \ + .name = "pwm-clk-bypass" #_idx, \ + .parent_names = (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents = 1, \ + .gate_hw = &gate_x_bypass_##_idx.hw, \ + .flags = CLK_SET_RATE_PARENT, \ + } + +/* +* H616_PWM_clock_src_xy generation: +* _____ ______ ________ +* OSC24M --->| | | | | | +* APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy +* |_____| |______| |________| +*/ +H616_PWM_XY_CLK_SRC (01 ,H616_PWM_XY_CLK_CR (0 )); +H616_PWM_XY_CLK_SRC (23 ,H616_PWM_XY_CLK_CR (1 )); +H616_PWM_XY_CLK_SRC (45 ,H616_PWM_XY_CLK_CR (2 )); + +/* +* H616_PWM_clock_x_div generation: +* ________ +* | | H616_PWM_clock_x/y +* H616_PWM_clock_src_xy --->| /div_k |---------------> +* |________| +*/ +H616_PWM_X_CLK (0 ); +H616_PWM_X_CLK (1 ); +H616_PWM_X_CLK (2 ); +H616_PWM_X_CLK (3 ); +H616_PWM_X_CLK (4 ); +H616_PWM_X_CLK (5 ); + +/* +* H616_PWM_bypass_clock_xy generation: +* ______ +* | | +* H616_PWM_clock_src_xy ---->| Gate |-------> H616_PWM_bypass_clock_x +* |______| +* +* The gate is actually H616_PWM_ENR register. +*/ +H616_PWM_X_BYPASS_CLK (0 ); +H616_PWM_X_BYPASS_CLK (1 ); +H616_PWM_X_BYPASS_CLK (2 ); +H616_PWM_X_BYPASS_CLK (3 ); +H616_PWM_X_BYPASS_CLK (4 ); +H616_PWM_X_BYPASS_CLK (5 ); + +struct clk_pwm_data { +const char *name; +const char **parent_names; +unsigned int num_parents; +struct clk_hw *mux_hw; +struct clk_hw *rate_hw; +struct clk_hw *gate_hw; +unsigned long flags; +}; + +#define CLK_BYPASS (h616chip, ch) ((h616chip)->data->npwm + (ch)) +#define CLK_XY_SRC_IDX (h616chip, ch) ((h616chip)->data->npwm * 2 + ((ch) >> 1)) +static struct clk_pwm_data pwmcc_data[] = { +REF_CLK_X (0 ,01 ), +REF_CLK_X (1 ,01 ), +REF_CLK_X (2 ,23 ), +REF_CLK_X (3 ,23 ), +REF_CLK_X (4 ,45 ), +REF_CLK_X (5 ,45 ), +REF_CLK_BYPASS (0 ,01 ),
drivers/pwm/pwm-sun50i-h616.c line 307
Quoted 10 lines
drivers/pwm/pwm-sun50i-h616.c new file +10
-0+REF_CLK_BYPASS (1 ,01 ), +REF_CLK_BYPASS (2 ,23 ), +REF_CLK_BYPASS (3 ,23 ), +REF_CLK_BYPASS (4 ,45 ), +REF_CLK_BYPASS (5 ,45 ), +REF_CLK_XY_SRC (01 ), +REF_CLK_XY_SRC (23 ), + REF_CLK_XY_SRC(45), + { /* sentinel */ } +};
We'll probably need a way to tie these static definitions to a particular instance of the unit for a given chip. But I guess that can be done later when adding more chips to the driver.
I'm not too versed in the clk and pwm APIs but the rest generally looks good to me.
All the best,
Paul
Signature · 6 lines
Paul Kocialkowski, Independent contractor - sys-base - https://www.sys-base.io/ Free software developer - https://www.paulk.fr/ Expert in multimedia, graphics and embedded hardware support with Linux.
Re: [PATCH v4 2/4] pwm: sun50i: Add H616 PWM support
Hi Paul,
Le 09/04/2026 à 19:30, Paul Kocialkowski a écrit :
Quoted 11 lines
Hi Richard,
drivers/pwm/pwm-sun50i-h616.c line 37
Quoted 2 lines · Richard Genoud
drivers/pwm/pwm-sun50i-h616.c new file +2
-0+/* PWM IRQ Enable Register */ +#define H616_PWM_IER 0x0I think it would make more sense to keep the full register names from the manual after the suffix and stick to them. It makes things easier when comparing the code with documentation or the reference implementation.
So something like
SUN8I_PWM_PIERhere.
Ok, that make sense.
Quoted 28 lines
Quoted 28 lines
drivers/pwm/pwm-sun50i-h616.c new file +27
-0+ +/* PWM IRQ Status Register */ +#define H616_PWM_ISR 0x4 + +/* PWM Capture IRQ Enable Register */ +#define H616_PWM_CIER 0x10 + +/* PWM Capture IRQ Status Register */ +#define H616_PWM_CISR 0x14 + +/* PWMCC Pairs Clock Configuration Registers */ +#define H616_PWM_XY_CLK_CR (pair) (0x20 + ((pair) * 0x4)) +#define H616_PWM_XY_CLK_CR_SRC_SHIFT 7 +#define H616_PWM_XY_CLK_CR_SRC_MASK 1 +#define H616_PWM_XY_CLK_CR_GATE_BIT 4 +#define H616_PWM_XY_CLK_CR_BYPASS_BIT (chan) ((chan) % 2 + 5) +#define H616_PWM_XY_CLK_CR_DIV_M_SHIFT 0 + +/* PWMCC Pairs Dead Zone Control Registers */ +#define H616_PWM_XY_DZ (pair) (0x30 + ((pair) * 0x4)) + +/* PWM Enable Register */ +#define H616_PWM_ENR 0x40 +#define H616_PWM_ENABLE (x) BIT(x) + +/* PWM Capture Enable Register */ +#define H616_PWM_CER 0x44
Quoted 8 lines
drivers/pwm/pwm-sun50i-h616.c line 67
Quoted 3 lines
drivers/pwm/pwm-sun50i-h616.c new file +3
-0+ +/* PWM Control Register */ +#define H616_PWM_CTRL_REG (chan) (0x60 + (chan) * 0x20)You're sometimes calling the register offset
_REGand sometimes not. Both options are fine but you need to keep it consistent across the whole definitions. I would be enclined to not use it after using the register names coming from the manual as suggested above.
I see what you mean, so H616_PWM_CTRL_REG() would become SUN8I_PWM_PCR()
Quoted 4 lines
Also you're sometimes using "chan", sometimes "ch" for the argument to the register macros. This is inconsistent and you might as well just use "c" everywhere so it doesn't take too much space.
Indeed.
Quoted 15 lines
Quoted 4 lines
drivers/pwm/pwm-sun50i-h616.c new file +3
-0+#define H616_PWM_CTRL_PRESCAL_K_SHIFT 0 +#define H616_PWM_CTRL_PRESCAL_K_WIDTH 8 +#define H616_PWM_CTRL_ACTIVE_STATE BIT(8)drivers/pwm/pwm-sun50i-h616.c line 80
Quoted 10 lines
drivers/pwm/pwm-sun50i-h616.c new file +10
-0+ +/* PWM Period Register */ +#define H616_PWM_PERIOD_REG (ch) (0x64 + (ch) * 0x20) +#define H616_PWM_PERIOD_MASK GENMASK(31, 16) +#define H616_PWM_DUTY_MASK GENMASK(15, 0) +#define H616_PWM_REG_PERIOD (reg) (FIELD_GET(H616_PWM_PERIOD_MASK, reg) + 1) +#define H616_PWM_REG_DUTY (reg) FIELD_GET(H616_PWM_DUTY_MASK, reg) +#define H616_PWM_PERIOD (prd) FIELD_PREP(H616_PWM_PERIOD_MASK, (prd) - 1) +#define H616_PWM_DUTY (dty) FIELD_PREP(H616_PWM_DUTY_MASK, dty) +#define H616_PWM_PERIOD_MAX (FIELD_MAX(H616_PWM_PERIOD_MASK) + 1)
Quoted 7 lines
Using REG as a prefix feels a bit confusing here. I would rather see:
#defineSUN8I_PWM_PPR(c)(0x64+ (c) *0x20)
#defineSUN8I_PWM_PPR_PERIOD(p)FIELD_PREP(...)
#defineSUN8I_PWM_PPR_PERIOD_VALUE(r)FIELD_GET(...)
#defineSUN8I_PWN_PPR_PERIOD_MAXFIELD_MAX(...)
#defineSUN8I_PWM_PPR_DUTY(d)FIELD_PREP(...)
#defineSUN8I_PWM_PPR_DUTY_VALUE(r)FIELD_GET(...)
That's right, that would be less confusing.
Quoted 233 lines
Quoted 218 lines
drivers/pwm/pwm-sun50i-h616.c new file +217
-0+ +/* PWM Count Register */ +#define H616_PWM_CNT_REG (x) (0x68 + (x) * 0x20) + +/* PWM Capture Control Register */ +#define H616_PWM_CCR (x) (0x6c + (x) * 0x20) + +/* PWM Capture Rise Lock Register */ +#define H616_PWM_CRLR (x) (0x70 + (x) * 0x20) + +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR (x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX (chan) ((chan) >> 2) + +/* +* Block diagram of the PWM clock controller: +* +* _____ ______ ________ +* OSC24M --->| | | | | | +* APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy +* |_____| |______| |________| +* ________ +* | | +* +->| /div_k |---> H616_PWM_clock_x +* | |________| +* | ______ +* | | | +* +-->| Gate |----> H616_PWM_bypass_clock_x +* | |______| +* H616_PWM_clock_src_xy ----+ ________ +* | | | +* +->| /div_k |---> H616_PWM_clock_y +* | |________| +* | ______ +* | | | +* +-->| Gate |----> H616_PWM_bypass_clock_y +* |______| +* +* NB: when the bypass is set, all the PWM logic is bypassed. +* So, the duty cycle and polarity can't be modified (we just have a clock). +* The bypass in PWM mode is used to achieve a 1/2 relative duty cycle with the +* fastest clock. +* +* H616_PWM_clock_x/y serve for the PWM purpose. +* H616_PWM_bypass_clock_x/y serve for the clock-provider purpose. +* +*/ + +/* +* Table used for /div_m (diviser before obtaining H616_PWM_clock_src_xy) +* It's actually CLK_DIVIDER_POWER_OF_TWO, but limited to /256 +*/ +#define CLK_TABLE_DIV_M_ENTRY (i) { \ + .val = (i), .div = 1 << (i) \ +} + +static const struct clk_div_table clk_table_div_m[] = { +CLK_TABLE_DIV_M_ENTRY (0 ), +CLK_TABLE_DIV_M_ENTRY (1 ), +CLK_TABLE_DIV_M_ENTRY (2 ), +CLK_TABLE_DIV_M_ENTRY (3 ), +CLK_TABLE_DIV_M_ENTRY (4 ), +CLK_TABLE_DIV_M_ENTRY (5 ), +CLK_TABLE_DIV_M_ENTRY (6 ), +CLK_TABLE_DIV_M_ENTRY (7 ), +CLK_TABLE_DIV_M_ENTRY (8 ), + {/* sentinel */ } +}; + +#define H616_PWM_XY_SRC_GATE (_pair, _reg) \ +struct clk_gate gate_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .bit_idx = H616_PWM_XY_CLK_CR_GATE_BIT, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_MUX (_pair, _reg) \ +struct clk_mux mux_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_XY_CLK_CR_SRC_SHIFT, \ + .mask = H616_PWM_XY_CLK_CR_SRC_MASK, \ + .flags = CLK_MUX_ROUND_CLOSEST, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_mux_ops, \ + } \ +} + +#define H616_PWM_XY_SRC_DIV (_pair, _reg) \ +struct clk_divider rate_xy_##_pair = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_XY_CLK_CR_DIV_M_SHIFT, \ + .table = clk_table_div_m, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_DIV (_idx, _reg) \ +struct clk_divider rate_x_##_idx = { \ + .reg = (void *)(_reg), \ + .shift = H616_PWM_CTRL_PRESCAL_K_SHIFT, \ + .width = H616_PWM_CTRL_PRESCAL_K_WIDTH, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_divider_ops, \ + } \ +} + +#define H616_PWM_X_BYPASS_GATE (_idx) \ +struct clk_gate gate_x_bypass_##_idx = { \ + .reg = (void *)H616_PWM_ENR, \ + .bit_idx = _idx, \ + .hw.init = &(struct clk_init_data){ \ + .ops = &clk_gate_ops, \ + } \ +} + +#define H616_PWM_XY_CLK_SRC (_pair, _reg) \ + static H616_PWM_XY_SRC_MUX(_pair, _reg); \ + static H616_PWM_XY_SRC_GATE(_pair, _reg); \ + static H616_PWM_XY_SRC_DIV(_pair, _reg) + +#define H616_PWM_X_CLK (_idx) \ + static H616_PWM_X_DIV(_idx, H616_PWM_CTRL_REG(_idx)) + +#define H616_PWM_X_BYPASS_CLK (_idx) \ + H616_PWM_X_BYPASS_GATE(_idx) + +#define REF_CLK_XY_SRC (_pair) \ + { \ + .name = "pwm-clk-src" #_pair, \ + .mux_hw = &mux_xy_##_pair.hw, \ + .gate_hw = &gate_xy_##_pair.hw, \ + .rate_hw = &rate_xy_##_pair.hw, \ + } + +#define REF_CLK_X (_idx, _pair) \ + { \ + .name = "pwm-clk" #_idx, \ + .parent_names = (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents = 1, \ + .rate_hw = &rate_x_##_idx.hw, \ + .flags = CLK_SET_RATE_PARENT, \ + } + +#define REF_CLK_BYPASS (_idx, _pair) \ + { \ + .name = "pwm-clk-bypass" #_idx, \ + .parent_names = (const char *[]){ "pwm-clk-src" #_pair }, \ + .num_parents = 1, \ + .gate_hw = &gate_x_bypass_##_idx.hw, \ + .flags = CLK_SET_RATE_PARENT, \ + } + +/* +* H616_PWM_clock_src_xy generation: +* _____ ______ ________ +* OSC24M --->| | | | | | +* APB1 ----->| Mux |--->| Gate |--->| /div_m |-----> H616_PWM_clock_src_xy +* |_____| |______| |________| +*/ +H616_PWM_XY_CLK_SRC (01 ,H616_PWM_XY_CLK_CR (0 )); +H616_PWM_XY_CLK_SRC (23 ,H616_PWM_XY_CLK_CR (1 )); +H616_PWM_XY_CLK_SRC (45 ,H616_PWM_XY_CLK_CR (2 )); + +/* +* H616_PWM_clock_x_div generation: +* ________ +* | | H616_PWM_clock_x/y +* H616_PWM_clock_src_xy --->| /div_k |---------------> +* |________| +*/ +H616_PWM_X_CLK (0 ); +H616_PWM_X_CLK (1 ); +H616_PWM_X_CLK (2 ); +H616_PWM_X_CLK (3 ); +H616_PWM_X_CLK (4 ); +H616_PWM_X_CLK (5 ); + +/* +* H616_PWM_bypass_clock_xy generation: +* ______ +* | | +* H616_PWM_clock_src_xy ---->| Gate |-------> H616_PWM_bypass_clock_x +* |______| +* +* The gate is actually H616_PWM_ENR register. +*/ +H616_PWM_X_BYPASS_CLK (0 ); +H616_PWM_X_BYPASS_CLK (1 ); +H616_PWM_X_BYPASS_CLK (2 ); +H616_PWM_X_BYPASS_CLK (3 ); +H616_PWM_X_BYPASS_CLK (4 ); +H616_PWM_X_BYPASS_CLK (5 ); + +struct clk_pwm_data { +const char *name; +const char **parent_names; +unsigned int num_parents; +struct clk_hw *mux_hw; +struct clk_hw *rate_hw; +struct clk_hw *gate_hw; +unsigned long flags; +}; + +#define CLK_BYPASS (h616chip, ch) ((h616chip)->data->npwm + (ch)) +#define CLK_XY_SRC_IDX (h616chip, ch) ((h616chip)->data->npwm * 2 + ((ch) >> 1)) +static struct clk_pwm_data pwmcc_data[] = { +REF_CLK_X (0 ,01 ), +REF_CLK_X (1 ,01 ), +REF_CLK_X (2 ,23 ), +REF_CLK_X (3 ,23 ), +REF_CLK_X (4 ,45 ), +REF_CLK_X (5 ,45 ), +REF_CLK_BYPASS (0 ,01 ),drivers/pwm/pwm-sun50i-h616.c line 307
Quoted 10 lines
drivers/pwm/pwm-sun50i-h616.c new file +10
-0+REF_CLK_BYPASS (1 ,01 ), +REF_CLK_BYPASS (2 ,23 ), +REF_CLK_BYPASS (3 ,23 ), +REF_CLK_BYPASS (4 ,45 ), +REF_CLK_BYPASS (5 ,45 ), +REF_CLK_XY_SRC (01 ), +REF_CLK_XY_SRC (23 ), + REF_CLK_XY_SRC(45), + { /* sentinel */ } +};We'll probably need a way to tie these static definitions to a particular instance of the unit for a given chip. But I guess that can be done later when adding more chips to the driver.
Quoted 2 lines
I'm not too versed in the clk and pwm APIs but the rest generally looks good to me.
Thanks!
Quoted 5 lines
All the best,
Paul
PATCH v4 2/4 pwm: sun50i: Add H616 PWM support
In reply to Richard GenoudHi Richard,
drivers/pwm/pwm-sun50i-h616.c line 95
Quoted 6 lines
drivers/pwm/pwm-sun50i-h616.c new file +6
-0+ +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR (x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX (chan) ((chan) >> 2) +
It looks like there's a typo or a mistake in the PAIR_IDX calculation. It should be like ((chan) >> 1).
For example, for the 5th channel the result will be 1, but it should be 2.
Best regards
Maksim
Re: [PATCH v4 2/4] pwm: sun50i: Add H616 PWM support
Hi Maksim,
Quoted 10 lines · On Mon 13 Apr 26, 15:39, [email protected] wrote:
drivers/pwm/pwm-sun50i-h616.c line 95
Quoted 6 lines
drivers/pwm/pwm-sun50i-h616.c new file +6
-0+ +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR (x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX (chan) ((chan) >> 2) +It looks like there's a typo or a mistake in the
PAIR_IDXcalculation. It should be like ((chan) >> 1).
For example, for the 5th channel the result will be 1, but it should be 2.
Had a quick look at it too and I agree with you. A right shift by 2 essentially groups pwms by 4, not 2. So pwms 2 and 3 would also be reported as part of index 0.
All the best,
Paul
Signature · 6 lines
Paul Kocialkowski, Independent contractor - sys-base - https://www.sys-base.io/ Free software developer - https://www.paulk.fr/ Expert in multimedia, graphics and embedded hardware support with Linux.
Re: [PATCH v4 2/4] pwm: sun50i: Add H616 PWM support
In reply to [email protected]Le 13/04/2026 à 14:39, [email protected] a écrit :
Quoted 12 lines
Hi Richard,
drivers/pwm/pwm-sun50i-h616.c line 95
Quoted 6 lines
drivers/pwm/pwm-sun50i-h616.c new file +6
-0+ +/* PWM Capture Fall Lock Register */ +#define H616_PWM_CFLR (x) (0x74 + (x) * 0x20) + +#define H616_PWM_PAIR_IDX (chan) ((chan) >> 2) +It looks like there's a typo or a mistake in the
PAIR_IDXcalculation. It should be like ((chan) >> 1).
For example, for the 5th channel the result will be 1, but it should be 2.
Good catch!
I mainly tested with PWM1 as it's the only one easily accessible with a scope.
I'll change that in the next iteration.
Thanks!
Quoted 4 lines
Best regards
Maksim
PATCH v4 3/4 arm64: dts: allwinner: h616: add PWM controller
In reply to Richard GenoudThe 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
@@ -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";
PATCH v4 4/4 MAINTAINERS: Add entry on Allwinner H616 PWM driver
In reply to Richard GenoudAdd myself as maintainer of Allwinner H616 PWM driver and device-tree bindings.
--- MAINTAINERS | 5 +++++ 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
@@ -906,6 +906,11 @@ S: Maintained F: Documentation/devicetree/bindings/sound/allwinner,sun50i-h6-dmic.yaml F: sound/soc/sunxi/sun50i-dmic.c +ALLWINNER H616 PWM DRIVER +M: Richard Genoud <[email protected]> +S: Maintained +F: drivers/pwm/pwm-sun50i-h616.c + ALLWINNER HARDWARE SPINLOCK SUPPORT M: Wilken Gottwalt <[email protected]> S: Maintained
Le 05/03/2026 à 10:19, Richard Genoud a écrit :
Quoted 70 lines
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.
Quoted 9 lines
This series is based onto v6.19-rc4
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.
NB2: for geopolitical reasons, I didn't re-use the old series that Paul was referring to.
Hi Uwe, do you plan to grab this series or does it needs some further reviews?
Regards,
Richard
Quoted 52 lines
Changes since v3:
- gather Acked-by/Tested-by
- fix cast from pointer to integer of different size (kernel test robot with arc platform)
- adddevm_actionforclk_hw_unregister_compositeas suggested by Philipp
- remove now unusedpwm_removeas suggested by PhilippChanges since v2:
- useU32_MAXinstead of definingUINT32_MAX
- add a comment onU32_MAXusage inclk_round_rate()
- changeclk_table_div_m(use macros)
- fix formatting (double space, superfluous comma, extra line feed)
- fix the parent clock order
- simplify code by usingscoped_guard()
- add missing const into_h616_pwm_chip()and rename toh616_pwm_from_chip()
- add/remove missing/superflous error messages
- renamecnt->period_ticks,duty_cnt->duty_ticks
- fixPWM_PERIOD_MAX
- add .remove() callback
- fixDIV_ROUND_CLOSEST_ULL->DIV_ROUND_UP_ULL
- addH616_prefix
- protect_regin macros
- switch to waveforms instead of apply/get_state
- shrink structh616_pwm_channel
- rebase on v6.19-rc4Changes since v1:
- rebase onto v6.19-rc1
- add missing headers
- removeMODULE_ALIAS(suggested by Krzysztof)
- use sun4i-pwm binding instead of creating a new one (suggested by Krzysztof)
- retrieve the parent clocks from the devicetree
- switchnum_parentsto unsigned intRichard Genoud (4):
dt-bindings: pwm: allwinner: add h616 pwm compatible
pwm: sun50i: Add H616 PWM support
arm64: dts: allwinner: h616: add PWM controller
MAINTAINERS: Add entry on Allwinner 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-sun50i-h616.c | 936 ++++++++++++++++++ 6 files changed, 1019 insertions(+), 1 deletion(-)create mode 100644
drivers/pwm/pwm-sun50i-h616.cbase-commit:
11439c4635edd669ae435eec308f4ab8a0804808
Hello Richard,
Quoted 2 lines · Richard GENOUD
Hi Uwe, do you plan to grab this series or does it needs some further reviews?
I didn't find the time to look into your patches yet, but I have them on my list. Unfortunately there is quite some more on that list, so I'll have to ask you for some patience.
Best regards
Uwe
Hi Richard,
Quoted 1 line · Richard Genoud
Allwinner H616 PWM controller is quite different from the A10 one.
As I've mentionned before, this PWM controller is not specific to the H616 but also appears in other chips, so the name of the driver and registers should not mention H616.
After further investigation, I can see multiple versions of this new PWM IP being used in different chips, starting with the R40/V40 (sun8iw11) in 2016.
The latest downstream BSP driver has a list of the different generations:
https://github.com/radxa/allwinner-bsp/blob/cubie-aiot-v1.4.6/drivers/pwm/pwm-sunxi.c#L1901
We have a first generation called v100/v101 for the following chips:
H616, R328 and R40. A second generation is called v200 and brings slight register layout differences for A133, D1/T113-S3 and V851. Subsequent iterations (v201-5) are used in more recent chips like A527 and A733 and seem register-compatible with v200 (from a quick look).
So what I suggest here is to rename the driver "sun8i-pwm" and eventually add a list of generations to the driver and different registers when needed, with an appropriate suffix in their name.
But since you're currently only dealing with H616, this work can be done later when introducing support for more chips.
Quoted 41 lines
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).
Quoted 6 lines
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.
From what I understand the pwm-clock driver will either assume a fixed rate set in device-tree or deduce the rate from the pwm period. In any case it will check that the pwm period (which it cannot change) is the same as the requested clock period.
So I agree that pwm-clock is unable to change the clock rate at runtime and will just use whatever frequency the pwm is running at (which is typically set in the device-tree consumer property).
Quoted 3 lines
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.
I agree that the driver needs to register as a proper clock provider in addition to pwm. But what happens if the same PWM clock is requested both from the clk side and the pwm side?
Quoted 14 lines
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.
Quoted 2 lines
And actually, this PWM is really a PWM AND a real clock when the bypass is set.
Make sense to me.
Quoted 61 lines
This series is based onto v6.19-rc4
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.
NB2: for geopolitical reasons, I didn't re-use the old series that Paul was referring to.
Changes since v3:
- gather Acked-by/Tested-by
- fix cast from pointer to integer of different size (kernel test robot with arc platform)
- adddevm_actionforclk_hw_unregister_compositeas suggested by Philipp
- remove now unusedpwm_removeas suggested by PhilippChanges since v2:
- useU32_MAXinstead of definingUINT32_MAX
- add a comment onU32_MAXusage inclk_round_rate()
- changeclk_table_div_m(use macros)
- fix formatting (double space, superfluous comma, extra line feed)
- fix the parent clock order
- simplify code by usingscoped_guard()
- add missing const into_h616_pwm_chip()and rename toh616_pwm_from_chip()
- add/remove missing/superflous error messages
- renamecnt->period_ticks,duty_cnt->duty_ticks
- fixPWM_PERIOD_MAX
- add .remove() callback
- fixDIV_ROUND_CLOSEST_ULL->DIV_ROUND_UP_ULL
- addH616_prefix
- protect_regin macros
- switch to waveforms instead of apply/get_state
- shrink structh616_pwm_channel
- rebase on v6.19-rc4Changes since v1:
- rebase onto v6.19-rc1
- add missing headers
- removeMODULE_ALIAS(suggested by Krzysztof)
- use sun4i-pwm binding instead of creating a new one (suggested by Krzysztof)
- retrieve the parent clocks from the devicetree
- switchnum_parentsto unsigned intRichard Genoud (4):
dt-bindings: pwm: allwinner: add h616 pwm compatible
pwm: sun50i: Add H616 PWM support
arm64: dts: allwinner: h616: add PWM controller
MAINTAINERS: Add entry on Allwinner 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-sun50i-h616.c | 936 ++++++++++++++++++ 6 files changed, 1019 insertions(+), 1 deletion(-) create mode 100644 drivers/pwm/pwm-sun50i-h616.cbase-commit:
11439c4635edd669ae435eec308f4ab8a0804808
Signature · 6 lines
Paul Kocialkowski, Independent contractor - sys-base - https://www.sys-base.io/ Free software developer - https://www.paulk.fr/ Expert in multimedia, graphics and embedded hardware support with Linux.
Le 09/04/2026 à 19:16, Paul Kocialkowski a écrit :
Quoted 21 lines
Hi Richard,
Quoted 1 line · Richard Genoud
Allwinner H616 PWM controller is quite different from the A10 one.
As I've mentionned before, this PWM controller is not specific to the H616 but also appears in other chips, so the name of the driver and registers should not mention H616.
After further investigation, I can see multiple versions of this new PWM IP being used in different chips, starting with the R40/V40 (sun8iw11) in 2016.
The latest downstream BSP driver has a list of the different generations:
https://github.com/radxa/allwinner-bsp/blob/cubie-aiot-v1.4.6/drivers/pwm/pwm-sunxi.c#L1901We have a first generation called v100/v101 for the following chips:
H616, R328 and R40. A second generation is called v200 and brings slight register layout differences for A133, D1/T113-S3 and V851. Subsequent iterations (v201-5) are used in more recent chips like A527 and A733 and seem register-compatible with v200 (from a quick look).
Quoted 6 lines
So what I suggest here is to rename the driver "sun8i-pwm" and eventually add a list of generations to the driver and different registers when needed, with an appropriate suffix in their name.
But since you're currently only dealing with H616, this work can be done later when introducing support for more chips.
ok, I'm fine with that :)
Quoted 58 lines
Quoted 42 lines
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).Quoted 6 lines
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.From what I understand the pwm-clock driver will either assume a fixed rate
set in device-tree or deduce the rate from the pwm period. In any case it will check that the pwm period (which it cannot change) is the same as the requested clock period.So I agree that pwm-clock is unable to change the clock rate at runtime and will just use whatever frequency the pwm is running at (which is typically set in the device-tree consumer property).
Quoted 7 lines
Quoted 3 lines
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.
I agree that the driver needs to register as a proper clock provider in addition to pwm. But what happens if the same PWM clock is requested both from the clk side and the pwm side?
The first to request it is the winner :)
The other ones will receive a -EBUSY
In h616_pwm_request() and h616_pwm_of_clk_get(), the channel mode is checked, and if it's free to use, it's set as either PWM or CLK mode so that it can't be requested a second time.
Quoted 15 lines
Quoted 15 lines
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.
Quoted 67 lines
Quoted 2 lines
And actually, this PWM is really a PWM AND a real clock when the bypass is set.
Make sense to me.
Quoted 61 lines
This series is based onto v6.19-rc4
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.
NB2: for geopolitical reasons, I didn't re-use the old series that Paul was referring to.
Changes since v3:
- gather Acked-by/Tested-by
- fix cast from pointer to integer of different size (kernel test robot with arc platform)
- adddevm_actionforclk_hw_unregister_compositeas suggested by Philipp
- remove now unusedpwm_removeas suggested by PhilippChanges since v2:
- useU32_MAXinstead of definingUINT32_MAX
- add a comment onU32_MAXusage inclk_round_rate()
- changeclk_table_div_m(use macros)
- fix formatting (double space, superfluous comma, extra line feed)
- fix the parent clock order
- simplify code by usingscoped_guard()
- add missing const into_h616_pwm_chip()and rename toh616_pwm_from_chip()
- add/remove missing/superflous error messages
- renamecnt->period_ticks,duty_cnt->duty_ticks
- fixPWM_PERIOD_MAX
- add .remove() callback
- fixDIV_ROUND_CLOSEST_ULL->DIV_ROUND_UP_ULL
- addH616_prefix
- protect_regin macros
- switch to waveforms instead of apply/get_state
- shrink structh616_pwm_channel
- rebase on v6.19-rc4Changes since v1:
- rebase onto v6.19-rc1
- add missing headers
- removeMODULE_ALIAS(suggested by Krzysztof)
- use sun4i-pwm binding instead of creating a new one (suggested by Krzysztof)
- retrieve the parent clocks from the devicetree
- switchnum_parentsto unsigned intRichard Genoud (4):
dt-bindings: pwm: allwinner: add h616 pwm compatible
pwm: sun50i: Add H616 PWM support
arm64: dts: allwinner: h616: add PWM controller
MAINTAINERS: Add entry on Allwinner 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-sun50i-h616.c | 936 ++++++++++++++++++ 6 files changed, 1019 insertions(+), 1 deletion(-)create mode 100644
drivers/pwm/pwm-sun50i-h616.cbase-commit:
11439c4635edd669ae435eec308f4ab8a0804808
Regards,
Richard