Skip to contentSkip to conversation

pwm: airoha: Add support for EN7581 SoC

PATCH Superseded Christian Marangi 3 participants 7 messages Jun 30, 2025 – Jul 4, 2025 PWM

v19 of 24 Superseded Latest is v24 →

lore
Share & tools
lore
#0 lore raw

PATCH v19 1/1 pwm: airoha: Add support for EN7581 SoC

From: Benjamin Larsson <[email protected]>

Introduce driver for PWM module available on EN7581 SoC.

---
Changes v19:
- Fix copyright update
- Drop use of refcount and use mutex + simple counter

Changes v18:
- Fix not inizialized period_ns
- Update copyright name
- Improve define and comments
- Add array_size.h
- Better handle refcount in airoha_pwm_consume_generator

Changes v17:
- Drop math64.h patch
- Move to rounddown()
- Use u32 for period_ns (after clamp)
- Use ticks in inner function and for buckets

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 | 625 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 636 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 +625 -0

625 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw
In reply to Christian Marangi

Hello Christian,

drivers/pwm/pwm-airoha.c line 94

Quoted 10 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +10 -0
+struct airoha_pwm_bucket {
+	int used;
+	u32 period_ticks;
+	u32 duty_ticks;
+};
+
+struct airoha_pwm {
+	struct regmap *regmap;
+	/* Global mutex to protect bucket used counter */
+	struct mutex mutex;

I think you don't need that mutex. There is a chip lock already used by the core and that is held during .get_state() and .apply() serializing these calls (and more).

Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7 -0
+	DECLARE_BITMAP(initialized, AIROHA_PWM_MAX_CHANNELS);
+
+	struct airoha_pwm_bucket buckets[AIROHA_PWM_NUM_BUCKETS];
+
+	/* Cache bucket used by each pwm channel */
+	u8 channel_bucket[AIROHA_PWM_MAX_CHANNELS];
+};

drivers/pwm/pwm-airoha.c

Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +9 -0
[...]
+static int airoha_pwm_apply_bucket_config(struct airoha_pwm *pc, int bucket,
+					  u32 duty_ticks, u32 period_ticks)
+{
+	u32 mask, shift, val;
+	u64 offset;
+	int ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_CYCLE_CFG);

Do you really need offset as a 64 bit variable? At least on 32 bit archs

	offset = bucket / AIROHA_PWM_BUCKET_PER_CYCLE_CFG;
	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(bucket % AIROHA_PWM_BUCKET_PER_CYCLE_CFG);

should be cheaper. Also can bucket better be an unsigned value (to make it obvious that no strange things happen with the division)?

Quoted 17 lines
drivers/pwm/pwm-airoha.c new file +17 -0
+	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(shift);
+
+	/* Configure frequency divisor */
+	mask = AIROHA_PWM_WAVE_GEN_CYCLE << shift;
+	val = FIELD_PREP(AIROHA_PWM_WAVE_GEN_CYCLE, period_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_CYCLE_CFG_VALUE(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_FLASH_PROD);
+	shift = AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT(shift);
+
+	/* Configure duty cycle */
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_HIGH << shift;
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_HIGH, duty_ticks) << shift;

drivers/pwm/pwm-airoha.c line 302

Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10 -0
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_LOW << shift;
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_LOW,
+			 AIROHA_PWM_DUTY_FULL - duty_ticks) << shift;
+	return regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				  mask, val);

Strange hardware, why do you have to configure both the high and the low relative duty? What happens if AIROHA_PWM_GPIO_FLASH_PRD_LOW + AIROHA_PWM_GPIO_FLASH_PRD_HIGH != AIROHA_PWM_DUTY_FULL?

Quoted 37 lines
drivers/pwm/pwm-airoha.c new file +36 -0
+}
+
[...]
+static int airoha_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			    const struct pwm_state *state)
+{
+	struct airoha_pwm *pc = pwmchip_get_drvdata(chip);
+	u32 period_ticks, duty_ticks;
+	u32 period_ns, duty_ns;
+
+	if (!state->enabled) {
+		airoha_pwm_disable(pc, pwm);
+		return 0;
+	}
+
+	/* Only normal polarity is supported */
+	if (state->polarity == PWM_POLARITY_INVERSED)
+		return -EINVAL;
+
+	/* Exit early if period is less than minimum supported */
+	if (state->period < AIROHA_PWM_PERIOD_TICK_NS)
+		return -EINVAL;
+
+	/* Clamp period to MAX supported value */
+	if (state->period > AIROHA_PWM_PERIOD_MAX_NS)
+		period_ns = AIROHA_PWM_PERIOD_MAX_NS;
+	else
+		period_ns = state->period;
+
+	/* Validate duty to configured period */
+	if (state->duty_cycle > period_ns)
+		duty_ns = period_ns;
+	else
+		duty_ns = state->duty_cycle;
+
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can

drivers/pwm/pwm-airoha.c line 538

Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10 -0
+	 * share a generator.
+	 */
+	period_ns = rounddown(period_ns, AIROHA_PWM_PERIOD_TICK_NS);
+
+	/*
+	 * Duty is divided in 255 segment, normalize it to check if we
+	 * can share a generator.
+	 */
+	duty_ns = DIV_U64_ROUND_UP(duty_ns * AIROHA_PWM_DUTY_FULL,
+				   AIROHA_PWM_DUTY_FULL);

This looks bogus. This is just duty_ns = duty_ns, or what do I miss? Also duty_ns is an u32 and AIROHA_PWM_DUTY_FULL an int, so there is no need for a 64 bit division.

drivers/pwm/pwm-airoha.c line 545

Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6 -0
+	/* Convert ns to ticks */
+	period_ticks = airoha_pwm_get_period_ticks_from_ns(period_ns);
+	duty_ticks = airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns);
+
+	return airoha_pwm_config(pc, pwm, period_ticks, duty_ticks);
+}

Best regards
Uwe

#3 lore raw
Quoted 38 lines · Uwe Kleine-König

Hello Christian,

drivers/pwm/pwm-airoha.c line 94

Quoted 10 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +10 -0
+struct airoha_pwm_bucket {
+	int used;
+	u32 period_ticks;
+	u32 duty_ticks;
+};
+
+struct airoha_pwm {
+	struct regmap *regmap;
+	/* Global mutex to protect bucket used counter */
+	struct mutex mutex;

I think you don't need that mutex. There is a chip lock already used by the core and that is held during .get_state() and .apply() serializing these calls (and more).

Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7 -0
+	DECLARE_BITMAP(initialized, AIROHA_PWM_MAX_CHANNELS);
+
+	struct airoha_pwm_bucket buckets[AIROHA_PWM_NUM_BUCKETS];
+
+	/* Cache bucket used by each pwm channel */
+	u8 channel_bucket[AIROHA_PWM_MAX_CHANNELS];
+};

drivers/pwm/pwm-airoha.c

Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +9 -0
[...]
+static int airoha_pwm_apply_bucket_config(struct airoha_pwm *pc, int bucket,
+					  u32 duty_ticks, u32 period_ticks)
+{
+	u32 mask, shift, val;
+	u64 offset;
+	int ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_CYCLE_CFG);

Do you really need offset as a 64 bit variable? At least on 32 bit archs

Quoted 6 lines
	offset = bucket / AIROHA_PWM_BUCKET_PER_CYCLE_CFG;
	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(bucket % AIROHA_PWM_BUCKET_PER_CYCLE_CFG);

should be cheaper. Also can bucket better be an unsigned value (to make it obvious that no strange things happen with the division)?

No for this u32 is ok.

Quoted 23 lines
Quoted 23 lines
drivers/pwm/pwm-airoha.c new file +23 -0
+	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(shift);
+
+	/* Configure frequency divisor */
+	mask = AIROHA_PWM_WAVE_GEN_CYCLE << shift;
+	val = FIELD_PREP(AIROHA_PWM_WAVE_GEN_CYCLE, period_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_CYCLE_CFG_VALUE(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_FLASH_PROD);
+	shift = AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT(shift);
+
+	/* Configure duty cycle */
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_HIGH << shift;
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_HIGH, duty_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_LOW << shift;
Quoted 9 lines

drivers/pwm/pwm-airoha.c line 302

Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4 -0
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_LOW,
+			 AIROHA_PWM_DUTY_FULL - duty_ticks) << shift;
+	return regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				  mask, val);

Strange hardware, why do you have to configure both the high and the low relative duty? What happens if AIROHA_PWM_GPIO_FLASH_PRD_LOW + AIROHA_PWM_GPIO_FLASH_PRD_HIGH != AIROHA_PWM_DUTY_FULL?

From documentation it gets rejected and configured bucket doesn't work.

Quoted 43 lines
Quoted 43 lines
drivers/pwm/pwm-airoha.c new file +42 -0
+}
+
[...]
+static int airoha_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			    const struct pwm_state *state)
+{
+	struct airoha_pwm *pc = pwmchip_get_drvdata(chip);
+	u32 period_ticks, duty_ticks;
+	u32 period_ns, duty_ns;
+
+	if (!state->enabled) {
+		airoha_pwm_disable(pc, pwm);
+		return 0;
+	}
+
+	/* Only normal polarity is supported */
+	if (state->polarity == PWM_POLARITY_INVERSED)
+		return -EINVAL;
+
+	/* Exit early if period is less than minimum supported */
+	if (state->period < AIROHA_PWM_PERIOD_TICK_NS)
+		return -EINVAL;
+
+	/* Clamp period to MAX supported value */
+	if (state->period > AIROHA_PWM_PERIOD_MAX_NS)
+		period_ns = AIROHA_PWM_PERIOD_MAX_NS;
+	else
+		period_ns = state->period;
+
+	/* Validate duty to configured period */
+	if (state->duty_cycle > period_ns)
+		duty_ns = period_ns;
+	else
+		duty_ns = state->duty_cycle;
+
+	/*
+	 * Period goes at 4ns step, normalize it to check if we can
+	 * share a generator.
+	 */
+	period_ns = rounddown(period_ns, AIROHA_PWM_PERIOD_TICK_NS);
+
+	/*
+	 * Duty is divided in 255 segment, normalize it to check if we
Quoted 9 lines

drivers/pwm/pwm-airoha.c line 538

Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4 -0
+	 * can share a generator.
+	 */
+	duty_ns = DIV_U64_ROUND_UP(duty_ns * AIROHA_PWM_DUTY_FULL,
+				   AIROHA_PWM_DUTY_FULL);

This looks bogus. This is just duty_ns = duty_ns, or what do I miss? Also duty_ns is an u32 and AIROHA_PWM_DUTY_FULL an int, so there is no need for a 64 bit division.

duty_ns * 255 goes beyond max u32.

225000000000.

Some revision ago it was asked to round also the duty_ns. And this is really to round_up duty in 255 segment.

Consider that this have the ROUND_UP part that is different than doing simple stuff like "duty * 255/255"

Hope we can solve these 2 problem since I think it's the last changes.

Quoted 9 lines

drivers/pwm/pwm-airoha.c line 545

Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6 -0
+	/* Convert ns to ticks */
+	period_ticks = airoha_pwm_get_period_ticks_from_ns(period_ns);
+	duty_ticks = airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns);
+
+	return airoha_pwm_config(pc, pwm, period_ticks, duty_ticks);
+}

Best regards
Uwe

Signature · 1 line
	Ansuel
#4 lore raw
Quoted 33 lines · Christian Marangi
Quoted 23 lines · Uwe Kleine-König
Quoted 23 lines
drivers/pwm/pwm-airoha.c new file +23 -0
+	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(shift);
+
+	/* Configure frequency divisor */
+	mask = AIROHA_PWM_WAVE_GEN_CYCLE << shift;
+	val = FIELD_PREP(AIROHA_PWM_WAVE_GEN_CYCLE, period_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_CYCLE_CFG_VALUE(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_FLASH_PROD);
+	shift = AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT(shift);
+
+	/* Configure duty cycle */
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_HIGH << shift;
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_HIGH, duty_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_LOW << shift;
Quoted 8 lines

drivers/pwm/pwm-airoha.c line 302

Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4 -0
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_LOW,
+			 AIROHA_PWM_DUTY_FULL - duty_ticks) << shift;
+	return regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				  mask, val);

Strange hardware, why do you have to configure both the high and the low relative duty? What happens if AIROHA_PWM_GPIO_FLASH_PRD_LOW + AIROHA_PWM_GPIO_FLASH_PRD_HIGH != AIROHA_PWM_DUTY_FULL?

Quoted 1 line

From documentation it gets rejected and configured bucket doesn't work.

ok.

Quoted 12 lines
Quoted 11 lines

drivers/pwm/pwm-airoha.c line 538

Quoted 7 lines

[...]

drivers/pwm/pwm-airoha.c new file +6 -0
+	/*
+	 * Duty is divided in 255 segment, normalize it to check if we
+	 * can share a generator.
+	 */
+	duty_ns = DIV_U64_ROUND_UP(duty_ns * AIROHA_PWM_DUTY_FULL,
+				   AIROHA_PWM_DUTY_FULL);

This looks bogus. This is just duty_ns = duty_ns, or what do I miss? Also duty_ns is an u32 and AIROHA_PWM_DUTY_FULL an int, so there is no need for a 64 bit division.

Quoted 1 line

duty_ns * 255 goes beyond max u32.

In that case duty_ns * AIROHA_PWM_DUTY_FULL overflows to a smaller value. Just because the value then is used by DIV_U64_ROUND_UP doesn't fix the overflow. You need (u64)duty_ns * AIROHA_PWM_DUTY_FULL then.

Quoted 4 lines

225000000000.

Some revision ago it was asked to round also the duty_ns. And this is really to round_up duty in 255 segment.

Yes, and I identified this as the code that intends to do that. Please double check this really works. I would claim you need:

	duty_ns = DIV_ROUND_UP(duty_ns, AIROHA_PWM_DUTY_FULL) * AIROHA_PWM_DUTY_FULL;

here because no matter if you round up or down, dividing n * AIROHA_PWM_DUTY_FULL by AIROHA_PWM_DUTY_FULL yields n.

Best regards
Uwe

#5 lore raw
Quoted 61 lines · Uwe Kleine-König
Quoted 33 lines · Christian Marangi
Quoted 23 lines · Uwe Kleine-König
Quoted 23 lines
drivers/pwm/pwm-airoha.c new file +23 -0
+	shift = AIROHA_PWM_REG_CYCLE_CFG_SHIFT(shift);
+
+	/* Configure frequency divisor */
+	mask = AIROHA_PWM_WAVE_GEN_CYCLE << shift;
+	val = FIELD_PREP(AIROHA_PWM_WAVE_GEN_CYCLE, period_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_CYCLE_CFG_VALUE(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	offset = bucket;
+	shift = do_div(offset, AIROHA_PWM_BUCKET_PER_FLASH_PROD);
+	shift = AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT(shift);
+
+	/* Configure duty cycle */
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_HIGH << shift;
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_HIGH, duty_ticks) << shift;
+	ret = regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				 mask, val);
+	if (ret)
+		return ret;
+
+	mask = AIROHA_PWM_GPIO_FLASH_PRD_LOW << shift;
Quoted 8 lines

drivers/pwm/pwm-airoha.c line 302

Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4 -0
+	val = FIELD_PREP(AIROHA_PWM_GPIO_FLASH_PRD_LOW,
+			 AIROHA_PWM_DUTY_FULL - duty_ticks) << shift;
+	return regmap_update_bits(pc->regmap, AIROHA_PWM_REG_GPIO_FLASH_PRD_SET(offset),
+				  mask, val);

Strange hardware, why do you have to configure both the high and the low relative duty? What happens if AIROHA_PWM_GPIO_FLASH_PRD_LOW + AIROHA_PWM_GPIO_FLASH_PRD_HIGH != AIROHA_PWM_DUTY_FULL?

Quoted 1 line

From documentation it gets rejected and configured bucket doesn't work.

ok.

Quoted 12 lines
Quoted 11 lines

drivers/pwm/pwm-airoha.c line 538

Quoted 7 lines

[...]

drivers/pwm/pwm-airoha.c new file +6 -0
+	/*
+	 * Duty is divided in 255 segment, normalize it to check if we
+	 * can share a generator.
+	 */
+	duty_ns = DIV_U64_ROUND_UP(duty_ns * AIROHA_PWM_DUTY_FULL,
+				   AIROHA_PWM_DUTY_FULL);

This looks bogus. This is just duty_ns = duty_ns, or what do I miss? Also duty_ns is an u32 and AIROHA_PWM_DUTY_FULL an int, so there is no need for a 64 bit division.

Quoted 1 line

duty_ns * 255 goes beyond max u32.

In that case duty_ns * AIROHA_PWM_DUTY_FULL overflows to a smaller value. Just because the value then is used by DIV_U64_ROUND_UP doesn't fix the overflow. You need (u64)duty_ns * AIROHA_PWM_DUTY_FULL then.

Quoted 4 lines

225000000000.

Some revision ago it was asked to round also the duty_ns. And this is really to round_up duty in 255 segment.

Quoted 8 lines

Yes, and I identified this as the code that intends to do that. Please double check this really works. I would claim you need:

	duty_ns = DIV_ROUND_UP(duty_ns, AIROHA_PWM_DUTY_FULL) * AIROHA_PWM_DUTY_FULL;

here because no matter if you round up or down, dividing n * AIROHA_PWM_DUTY_FULL by AIROHA_PWM_DUTY_FULL yields n.

Ok I made some test with a testing program to simulate various way to normalize the value and yes you are right there is a problem here.

Also duty_ns = DIV_ROUND_UP(duty_ns, AIROHA_PWM_DUTY_FULL) * AIROHA_PWM_DUTY_FULL; doesn't really fit here.

The solution I found is the following

DIV_U64_ROUND_UP(airoha_pwm_get_duty_ticks_from_ns(period, duty) * period, 255)

and airoha_pwm_get_duty_ticks_from_ns is (duty * 255 / period)

I also tested other viable way to reduce the redundant formula but the main problem is that on big numbers (example when duty = period, too many division error for integer division adds up (due to necessary rounding) so we end up with not precise number that the tick actually reflect or even goin beyond the period number (as duty must be <= period)

But the thing is that since duty tick depends on period and now the bucket base everything on the tick, I really feel normalizing duty is not needed at all.

With the working normalize we would have

duty_ns = DIV_U64_ROUND_UP(airoha_pwm_get_duty_ticks_from_ns(period, duty) * period, 255);

duty_tick = airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns)

With the normalization already done by airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns).

What do you think?

Signature · 1 line
	Ansuel
#6 lore raw

Hello,

Quoted 3 lines · Christian Marangi

But the thing is that since duty tick depends on period and now the bucket base everything on the tick, I really feel normalizing duty is not needed at all.

that sounds right. The normalizing is done by just having an integer result for the involved divisions. (Actually I understood that when I requested to consider ticks instead of ns for the bucket configuration, but wasn't aware any more later in this thread.)

Best regards
Uwe