Skip to contentSkip to conversation

math64.h: provide rounddown_u64 variant for rounddown macro

PATCH In review Christian Marangi 3 participants 9 messages Jun 25, 2025 PWM

lore
Share & tools
lore
#0 lore raw

PATCH v16 1/2 math64.h: provide rounddown_u64 variant for rounddown macro

There is currently a problem with the usage of rounddown() macro with u64 dividends. This causes compilation error on specific arch where 64-bit division is done on 32-bit system.

To be more specific GCC try to optimize the function and replace it with __umoddi3() but this is actually not compiled in the kernel.

Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'

To better handle this, introduce a variant of rounddown() macro, rounddown_u64() that can be used exactly for this scenario.

The new rounddown_u64() in math64.h uses do_div() to do the heavy work of handling internally all the magic for the 64-bit division on 32-bit (and indirectly fix the compilation error).

---
Changes v16:
- Move to math64.h
- Fix spelling mistake
- Use inline function (to follow roundup_u64 pattern) Changes v15:
- Add this patch

 include/linux/math64.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/linux/math64.h +16 -0
index 6aaccc1626ab..60cf79d0d7d6 100644
@@ -370,4 +370,20 @@ static inline u64 roundup_u64(u64 x, u32 y)
 {
 	return DIV_U64_ROUND_UP(x, y) * y;
 }
+
+/**
+ * rounddown_u64 - Round down a 64bit value to the next specified 32bit multiple
+ * @x: the value to round
+ * @y: 32bit multiple to round down to
+ *
+ * Rounds @x down to the next multiple of @y. For 32bit @x values, see rounddown
+ * and the faster round_down() for powers of 2.
+ *
+ * Return: rounded up value.
+ */
+static inline u64 rounddown_u64(u64 x, u32 y)
+{
+	u64 tmp = x;
+	return x - do_div(tmp, y);
+}
 #endif /* _LINUX_MATH64_H */
Signature · 1 line
2.48.1
#1 lore raw

PATCH v16 2/2 pwm: airoha: Add support for EN7581 SoC

From: Benjamin Larsson <[email protected]>

Introduce driver for PWM module available on EN7581 SoC.

---
Changes v16:
- Always check regmap return
- Add missing header
- Use bitmask APIs
- Use refcount instead of raw u64
- Fix spelling mistake and improve text
- Drop OF dependency
- Drop redundant always true if condition
- Add myself as Author

Changes v15:
- Fix compilation error for 64bit division on 32bit (patch 01)
- Add prefer async probe

Changes v14:
- Fix logic for bucket recycle
(was tested by not releasing the bucket and observing
the best one gets used)
- Assign bucket period and duty only if not used
- Skip bucket period/duty calculation if already used
- Permit disable also if inversed polarity is asked
- Normalize duty similar to period

Changes v13:
- Reorder include
- Split ticks_from_ns function
- Add additional comments for shift register chip clock
- Address suggested minor optimization (Uwe)

Changes v12:
- Make shift function more readable
- Use unsigned int where possible
- Better comment some SIPO strangeness
- Move SIPO init after flash map config
- Retrun real values in get_state instead of the one saved in bucket
- Improve period_ns parsing so we can better share generators

Changes v11:
- Fix wrong calculation of period and duty
- Use AIROHA_PWM prefix for each define
- Drop set/get special define in favour of BITS and GENMASK
- Correctly use dev_err_probe
- Init bucket with initial values
- Rework define to make use of FIELD_PREP and FIELD_GET

Changes in v10:
- repost just patch 6/6 (pwm driver) since patches {1/6-5/6} have been already applied in linux-pinctrl tree
- pwm: introduce AIROHA_PWM_FIELD_GET and AIROHA_PWM_FIELD_SET macros to get/set field with non-const mask
- pwm: simplify airoha_pwm_get_generator() to report unused generator
and remove double lookup
- pwm: remove device_node pointer in airoha_pwm struct since this is write-only field
- pwm: cosmetics
- Link to v9: https://lore.kernel.org/r/[email protected]

Changes in v9:
- pwm: remove unused properties
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- pwm: add missing properties documentation
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- pinctrl: cosmetics
- pinctrl: fix compilation warning
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- pwm: rely on regmap APIs
- pwm: introduce compatible string
- pinctrl: introduce compatible string
- remove airoha-mfd driver
- add airoha,en7581-pinctrl binding
- add airoha,en7581-pwm binding
- update airoha,en7581-gpio-sysctl binding
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- use spin_lock in airoha_pinctrl_rmw instead of a mutex since it can run
in interrupt context
- remove unused includes in pinctrl driver
- since the irq_chip is immutable, allocate the gpio_irq_chip struct statically in pinctrl driver
- rely on regmap APIs in pinctrl driver but keep the spin_lock local to the driver
- rely on guard/guard_scope APIs in pinctrl driver
- improve naming convention pinctrl driver
- introduce airoha_pinconf_set_pin_value utility routine
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- add 'Limitation' description in pwm driver
- fix comments in pwm driver
- rely on mfd->base __iomem pointer in pwm driver, modify register
offsets according to it and get rid of sgpio_cfg, flash_cfg and
cycle_cfg pointers
- simplify register utility routines in pwm driver
- use 'generator' instead of 'waveform' suffix for pwm routines
- fix possible overflow calculating duty cycle in pwm driver
- do not modify pwm state in free callback in pwm driver
- cap the maximum period in pwm driver
- do not allow inverse polarity in pwm driver
- do not set of_xlate callback in the pwm driver and allow the stack to
do it
- fix MAINTAINERS file for airoha pinctrl driver
- fix undefined reference to __ffsdi2 in pinctrl driver
- simplify airoha,en7581-gpio-sysctl.yam binding
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- introduce airoha-mfd driver
- add pwm driver to the same series
- model pinctrl and pwm drivers as childs of a parent mfd driver.
- access chip-scu memory region in pinctrl driver via syscon
- introduce a single airoha,en7581-gpio-sysctl.yaml binding and get rid
of dedicated bindings for pinctrl and pwm
- add airoha,en7581-chip-scu.yaml binding do the series
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Fix compilation errors
- Collapse some register mappings for gpio and irq controllers
- update dt-bindings according to new register mapping
- fix some dt-bindings errors
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/pwm/Kconfig      |  10 +
 drivers/pwm/Makefile     |   1 +
 drivers/pwm/pwm-airoha.c | 613 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 624 insertions(+)
 create mode 100644 drivers/pwm/pwm-airoha.c
drivers/pwm/Kconfig +10 -0
index c866ed388da9..4aa7d94cd680 100644
@@ -54,6 +54,16 @@ config PWM_ADP5585
 	  This option enables support for the PWM function found in the Analog
 	  Devices ADP5585.
 
+config PWM_AIROHA
+	tristate "Airoha PWM support"
+	depends on ARCH_AIROHA || COMPILE_TEST
+	select REGMAP_MMIO
+	help
+	  Generic PWM framework driver for Airoha SoC.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-airoha.
+
 config PWM_APPLE
 	tristate "Apple SoC PWM support"
 	depends on ARCH_APPLE || COMPILE_TEST
drivers/pwm/Makefile +1 -0
index 5c782af8f49b..cd3e6de2e44a 100644
@@ -2,6 +2,7 @@
 obj-$(CONFIG_PWM)		+= core.o
 obj-$(CONFIG_PWM_AB8500)	+= pwm-ab8500.o
 obj-$(CONFIG_PWM_ADP5585)	+= pwm-adp5585.o
+obj-$(CONFIG_PWM_AIROHA)	+= pwm-airoha.o
 obj-$(CONFIG_PWM_APPLE)		+= pwm-apple.o
 obj-$(CONFIG_PWM_ATMEL)		+= pwm-atmel.o
 obj-$(CONFIG_PWM_ATMEL_HLCDC_PWM)	+= pwm-atmel-hlcdc.o
drivers/pwm/pwm-airoha.c new file +613 -0

613 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC

Hello Christian,

drivers/pwm/pwm-airoha.c line 513

Quoted 5 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +5 -0
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown_u64(period_ns, AIROHA_PWM_PERIOD_TICK_NS);

I don't understand why you need that. If you clamp to AIROHA_PWM_PERIOD_MAX_NS first, you don't need the (expensive) 64-bit operation. If you compare using ticks instead of ns you don't even need to round down, but just do the division that you end up doing anyhow.

Best regards
Uwe

#3 lore raw

Re: [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC

Quoted 14 lines · Uwe Kleine-König

Hello Christian,

drivers/pwm/pwm-airoha.c line 513

Quoted 5 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +5 -0
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown_u64(period_ns, AIROHA_PWM_PERIOD_TICK_NS);

I don't understand why you need that. If you clamp to AIROHA_PWM_PERIOD_MAX_NS first, you don't need the (expensive) 64-bit operation. If you compare using ticks instead of ns you don't even need to round down, but just do the division that you end up doing anyhow.

Correct me if I'm wrong but

#define NSEC_PER_SEC 1000000000L
#define AIROHA_PWM_PERIOD_MAX_NS (1 * NSEC_PER_SEC)

doesn't fit u32 so an u64 is needed.

And using ns until the apply process is handy for bucket sharing. I can change it to reference ticks but I think the round is necessary.

You want to change everything to reference tick? (honestly this is a good chance to introduce this missing API, since I feel also other might benefits from this)

Signature · 1 line
	Ansuel
#4 lore raw

Re: [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC

Quoted 2 lines · Christian Marangi
Quoted 1 line · Uwe Kleine-König

On Wed, Jun 25, 2025 at 02:00:39AM +0200, Christian Marangi wrote:

...

Quoted 11 lines
Quoted 10 lines

drivers/pwm/pwm-airoha.c line 513

Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5 -0
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown_u64(period_ns, AIROHA_PWM_PERIOD_TICK_NS);

I don't understand why you need that. If you clamp to AIROHA_PWM_PERIOD_MAX_NS first, you don't need the (expensive) 64-bit operation. If you compare using ticks instead of ns you don't even need to round down, but just do the division that you end up doing anyhow.

Quoted 6 lines

Correct me if I'm wrong but

#define NSEC_PER_SEC 1000000000L
#define AIROHA_PWM_PERIOD_MAX_NS (1 * NSEC_PER_SEC)

doesn't fit u32 so an u64 is needed.

I'm not sure what was the exact question, but u32 can hold up to 4*10^9, this is just 1*10^9.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC

Quoted 19 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Uwe Kleine-König

On Wed, Jun 25, 2025 at 02:00:39AM +0200, Christian Marangi wrote:

...

Quoted 13 lines
Quoted 10 lines

drivers/pwm/pwm-airoha.c line 513

Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5 -0
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown_u64(period_ns, AIROHA_PWM_PERIOD_TICK_NS);

I don't understand why you need that. If you clamp to AIROHA_PWM_PERIOD_MAX_NS first, you don't need the (expensive) 64-bit operation. If you compare using ticks instead of ns you don't even need to round down, but just do the division that you end up doing anyhow.

Correct me if I'm wrong but

Quoted 8 lines
Quoted 4 lines

#define NSEC_PER_SEC 1000000000L
#define AIROHA_PWM_PERIOD_MAX_NS (1 * NSEC_PER_SEC)

doesn't fit u32 so an u64 is needed.

I'm not sure what was the exact question, but u32 can hold up to 4*10^9, this is just 1*10^9.

Ok feeling stupid... was treating 1000000000 as 0x1000000000 :D

Signature · 1 line
	Ansuel
#6 lore raw

Re: [PATCH v16 2/2] pwm: airoha: Add support for EN7581 SoC

In reply to Christian Marangi

Hello Christian,

Quoted 13 lines · Christian Marangi
Quoted 11 lines · Uwe Kleine-König

drivers/pwm/pwm-airoha.c line 513

Quoted 5 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +5 -0
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown_u64(period_ns, AIROHA_PWM_PERIOD_TICK_NS);

I don't understand why you need that. If you clamp to AIROHA_PWM_PERIOD_MAX_NS first, you don't need the (expensive) 64-bit operation. If you compare using ticks instead of ns you don't even need to round down, but just do the division that you end up doing anyhow.

Quoted 1 line

Correct me if I'm wrong but

I will :-)

Quoted 4 lines

#define NSEC_PER_SEC 1000000000L
#define AIROHA_PWM_PERIOD_MAX_NS (1 * NSEC_PER_SEC)

doesn't fit u32 so an u64 is needed.

1000000000 = 0x3b9aca00, that are 30 bits.

Quoted 2 lines

And using ns until the apply process is handy for bucket sharing. I can change it to reference ticks but I think the round is necessary.

It's only handy as you track ns for the buckets. Changing that to ticks, too, makes this all naturally fit again.

Quoted 3 lines

You want to change everything to reference tick? (honestly this is a good chance to introduce this missing API, since I feel also other might benefits from this)

I don't understand that, but I think yes. Doing the bucket selection in ticks sounds right.

Best regards
Uwe

#7 lore raw
In reply to Christian Marangi
Quoted 16 lines · Christian Marangi

There is currently a problem with the usage of rounddown() macro with u64 dividends. This causes compilation error on specific arch where 64-bit division is done on 32-bit system.

To be more specific GCC try to optimize the function and replace it with __umoddi3() but this is actually not compiled in the kernel.

Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'

To better handle this, introduce a variant of rounddown() macro, rounddown_u64() that can be used exactly for this scenario.

The new rounddown_u64() in math64.h uses do_div() to do the heavy work of handling internally all the magic for the 64-bit division on 32-bit (and indirectly fix the compilation error).

...

Quoted 4 lines

static inline u64 roundup_u64(u64 x, u32 y)
{
return DIV_U64_ROUND_UP(x, y) * y;
}

...

include/linux/math64.h static inline u64 roundup_u64(u64 x, u32 y) · line 388

Quoted 5 lines
include/linux/math64.h +5 -0
+static inline u64 rounddown_u64(u64 x, u32 y)
+{
+	u64 tmp = x;
+	return x - do_div(tmp, y);
+}

Can it be implemented as above?

	return DIV_U64_ROUND_DOWN(x, y) * y;

(yes, it seems we are missing the DIV_U64_ROUND_DOWN() implementation).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 35 lines · Andy Shevchenko
Quoted 16 lines · Christian Marangi

There is currently a problem with the usage of rounddown() macro with u64 dividends. This causes compilation error on specific arch where 64-bit division is done on 32-bit system.

To be more specific GCC try to optimize the function and replace it with __umoddi3() but this is actually not compiled in the kernel.

Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'

To better handle this, introduce a variant of rounddown() macro, rounddown_u64() that can be used exactly for this scenario.

The new rounddown_u64() in math64.h uses do_div() to do the heavy work of handling internally all the magic for the 64-bit division on 32-bit (and indirectly fix the compilation error).

...

Quoted 4 lines

static inline u64 roundup_u64(u64 x, u32 y)
{
return DIV_U64_ROUND_UP(x, y) * y;
}

...

include/linux/math64.h static inline u64 roundup_u64(u64 x, u32 y) · line 388

Quoted 5 lines
include/linux/math64.h +5 -0
+static inline u64 rounddown_u64(u64 x, u32 y)
+{
+	u64 tmp = x;
+	return x - do_div(tmp, y);
+}

Can it be implemented as above?

Quoted 4 lines
	return DIV_U64_ROUND_DOWN(x, y) * y;

(yes, it seems we are missing the DIV_U64_ROUND_DOWN() implementation).

Guess it would be

#define DIV_U64_ROUND_DOWN(ll, d)		\
	({ u32 _tmp = (d); div_u64((ll), _tmp); })

But isn't that just directly div_u64?? (maybe the dividend is enforced u32 with the cast)

and in math.h I can also notice

#define DIV_ROUND_DOWN_ULL(ll, d) \
	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })

tons of macro that do the same thing ahhahah

Really seems I'm opening a can of worm.

Also also division + subtraction isn't less CPU intensive than division
+ multiplication?

I know the compiler does magic on these internally but still...

Signature · 1 line
	Ansuel