Skip to contentSkip to conversation

pwm: airoha: Add support for EN7581 SoC

PATCH Superseded Christian Marangi 2 participants 8 messages Jun 26, 2025 – Jun 30, 2025 PWM

v18 of 24 Superseded Latest is v24 →

lore
Share & tools
lore
#0 lore raw

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

From: Benjamin Larsson <[email protected]>

Introduce driver for PWM module available on EN7581 SoC.

---
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 | 626 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 637 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 +626 -0

626 lines not on this page · raw message

Signature · 1 line
2.48.1
#1 lore raw
Quoted 3 lines · Christian Marangi

From: Benjamin Larsson <[email protected]>

Introduce driver for PWM module available on EN7581 SoC.

...

Quoted 2 lines

+ * Copyright 2025 Markus Gothe <[email protected]>
+ * Christian Marangi <[email protected]>

I'm not sure you can change the copyright line like this. I would rather expect

* Copyright 2012 Markus Gothe <[email protected]>
* Copyright 2025 Christian Marangi <[email protected]>

(Sorry, I forgot the exact original year there, but you got the point)

...

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

...

drivers/pwm/pwm-airoha.c line 386

Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7 -0
+	/*
+	 * It is necessary to explicitly shift out all zeros after muxing
+	 * to initialize the shift register before enabling PWM
+	 * mode because in PWM mode SIPO will not start shifting until
+	 * it needs to output a non-zero value (bit 31 of led_data
+	 * indicates shifting in progress and it must return to zero
+	 * before led_data can be written or PWM mode can be set)

Missing period at the end.

Quoted 1 line
(quoted diff) +1 -0
+	 */

...

drivers/pwm/pwm-airoha.c line 488

Quoted 11 lines
drivers/pwm/pwm-airoha.c new file +11 -0
+static void airoha_pwm_disable(struct airoha_pwm *pc, struct pwm_device *pwm)
+{
+	/* Disable PWM and release the bucket */
+	airoha_pwm_config_flash_map(pc, pwm->hwpwm, -1);
+	airoha_pwm_release_bucket_config(pc, pwm->hwpwm);
+
+	clear_bit(pwm->hwpwm, pc->initialized);
+
+	/* If no SIPO is used, disable the shift register chip */
+	if (find_next_bit(pc->initialized, AIROHA_PWM_MAX_CHANNELS,
+			  AIROHA_PWM_NUM_GPIO) >= AIROHA_PWM_NUM_GPIO)

This can be rewritten as

	if (!bitmap_read(pc->initialized, AIROHA_PWM_NUM_GPIO, AIROHA_PWM_NUM_SIPO))

(seems to me that the above code is buggy and has to have a check against the
full size of bitmap).

Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3 -0
+		regmap_clear_bits(pc->regmap, AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG,
+				  AIROHA_PWM_SERIAL_GPIO_FLASH_MODE);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 17 lines · Andy Shevchenko
Quoted 3 lines · Christian Marangi

From: Benjamin Larsson <[email protected]>

Introduce driver for PWM module available on EN7581 SoC.

...

Quoted 2 lines

+ * Copyright 2025 Markus Gothe <[email protected]>
+ * Christian Marangi <[email protected]>

I'm not sure you can change the copyright line like this. I would rather expect

* Copyright 2012 Markus Gothe <[email protected]>
* Copyright 2025 Christian Marangi <[email protected]>

(Sorry, I forgot the exact original year there, but you got the point)

Quoted 8 lines

...

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Quoted 30 lines

...

drivers/pwm/pwm-airoha.c line 386

Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7 -0
+	/*
+	 * It is necessary to explicitly shift out all zeros after muxing
+	 * to initialize the shift register before enabling PWM
+	 * mode because in PWM mode SIPO will not start shifting until
+	 * it needs to output a non-zero value (bit 31 of led_data
+	 * indicates shifting in progress and it must return to zero
+	 * before led_data can be written or PWM mode can be set)

Missing period at the end.

Quoted 1 line
(quoted diff) +1 -0
+	 */

...

drivers/pwm/pwm-airoha.c line 488

Quoted 11 lines
drivers/pwm/pwm-airoha.c new file +11 -0
+static void airoha_pwm_disable(struct airoha_pwm *pc, struct pwm_device *pwm)
+{
+	/* Disable PWM and release the bucket */
+	airoha_pwm_config_flash_map(pc, pwm->hwpwm, -1);
+	airoha_pwm_release_bucket_config(pc, pwm->hwpwm);
+
+	clear_bit(pwm->hwpwm, pc->initialized);
+
+	/* If no SIPO is used, disable the shift register chip */
+	if (find_next_bit(pc->initialized, AIROHA_PWM_MAX_CHANNELS,
+			  AIROHA_PWM_NUM_GPIO) >= AIROHA_PWM_NUM_GPIO)

This can be rewritten as

Quoted 5 lines
	if (!bitmap_read(pc->initialized, AIROHA_PWM_NUM_GPIO, AIROHA_PWM_NUM_SIPO))

(seems to me that the above code is buggy and has to have a check against the
full size of bitmap).

Yes I'm confused by find_next_bit. Is the nbits part the rest of the bit or the full size of the bitmap?

Google suggest it should be the full size of the bitmap but I wasn't so sure. Anyway I will use bitmap_read since it seems more readable.

Quoted 9 lines
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3 -0
+		regmap_clear_bits(pc->regmap, AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG,
+				  AIROHA_PWM_SERIAL_GPIO_FLASH_MODE);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 1 line
	Ansuel
#3 lore raw
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko

On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

Quoted 8 lines
Quoted 5 lines

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Then drop the mutex, atomic operations do not need an additional synchronisation. Btw, have you looked at kref APIs? Maybe that would make the intention clearer?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko

On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

Quoted 6 lines
Quoted 5 lines

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Quoted 6 lines
Quoted 2 lines

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Then drop the mutex, atomic operations do not need an additional synchronisation. Btw, have you looked at kref APIs? Maybe that would make the intention clearer?

It's needed for

+ mutex_lock(&pc->mutex);
+ if (refcount_read(&pc->buckets[bucket].used) == 0) {
+ config_bucket = true;
+ refcount_set(&pc->buckets[bucket].used, 1);
+ } else {
+ refcount_inc(&pc->buckets[bucket].used);
+ }
+ mutex_unlock(&pc->mutex);

the refcount_read + refcount_set. As you explained there might be case where refcount_read is zero but nother PWM channel is setting the value so one refcount gets lost.

kref I checked but not useful for the task.

The logic here is

- refcount init as 0 (bucket unused)
- refcount set to 1 on first bucket use (bucket get configured)
- refcount increased if already used
- refcount decreased when PWM channel released
- bucket gets flagged as unused when refcount goes to 0 again

Signature · 1 line
	Ansuel
#5 lore raw
Quoted 4 lines · Christian Marangi
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko

On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Quoted 6 lines
Quoted 2 lines

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Then drop the mutex, atomic operations do not need an additional synchronisation. Btw, have you looked at kref APIs? Maybe that would make the intention clearer?

It's needed for

drivers/pwm/pwm-airoha.c new file +8 -0
+       mutex_lock(&pc->mutex);
+       if (refcount_read(&pc->buckets[bucket].used) == 0) {
+               config_bucket = true;
+               refcount_set(&pc->buckets[bucket].used, 1);
+       } else {
+               refcount_inc(&pc->buckets[bucket].used);
+       }
+       mutex_unlock(&pc->mutex);
Quoted 1 line

the refcount_read + refcount_set.

Which is simply wrong. Nobody should use atomics in such a way. Imagine if somebody wants to copy something like this in their code (in case of no mutex is there), they most likely won't notice this subtle bug.

Quoted 2 lines

As you explained there might be case where refcount_read is zero but nother PWM channel is setting the value so one refcount gets lost.

Right, because you should use refcount_inc_and_test() and initialise it to -MAX instead of 0. Or something like this.

Quoted 1 line

kref I checked but not useful for the task.

Okay.

Quoted 7 lines

The logic here is

- refcount init as 0 (bucket unused)
- refcount set to 1 on first bucket use (bucket get configured)
- refcount increased if already used
- refcount decreased when PWM channel released
- bucket gets flagged as unused when refcount goes to 0 again

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 32 lines · Andy Shevchenko
Quoted 4 lines · Christian Marangi
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko

On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

drivers/pwm/pwm-airoha.c line 94

Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Quoted 6 lines
Quoted 2 lines

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Then drop the mutex, atomic operations do not need an additional synchronisation. Btw, have you looked at kref APIs? Maybe that would make the intention clearer?

It's needed for

drivers/pwm/pwm-airoha.c new file +8 -0
+       mutex_lock(&pc->mutex);
+       if (refcount_read(&pc->buckets[bucket].used) == 0) {
+               config_bucket = true;
+               refcount_set(&pc->buckets[bucket].used, 1);
+       } else {
+               refcount_inc(&pc->buckets[bucket].used);
+       }
+       mutex_unlock(&pc->mutex);
Quoted 7 lines
Quoted 1 line

the refcount_read + refcount_set.

Which is simply wrong. Nobody should use atomics in such a way. Imagine if somebody wants to copy something like this in their code (in case of no mutex is there), they most likely won't notice this subtle bug.

Yes I understand that someone might think the additional mutex can be ""optional""

Quoted 6 lines
Quoted 2 lines

As you explained there might be case where refcount_read is zero but nother PWM channel is setting the value so one refcount gets lost.

Right, because you should use refcount_inc_and_test() and initialise it to -MAX instead of 0. Or something like this.

Mhhh I think API for _inc_and_test doesn't currently exist and I don't feel too confident implementing them currently.

Quoted 12 lines
Quoted 1 line

kref I checked but not useful for the task.

Okay.

Quoted 7 lines

The logic here is

- refcount init as 0 (bucket unused)
- refcount set to 1 on first bucket use (bucket get configured)
- refcount increased if already used
- refcount decreased when PWM channel released
- bucket gets flagged as unused when refcount goes to 0 again

Do you think I should bite the bullet and just drop using refcount and implement a simple int variable protected by a mutex?

Signature · 1 line
	Ansuel
#7 lore raw
Quoted 6 lines · Christian Marangi
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Christian Marangi
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko

On Fri, Jun 27, 2025 at 12:47:53AM +0200, Christian Marangi wrote:

...

Quoted 35 lines
Quoted 24 lines
Quoted 24 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	/* Global mutex to protect bucket used refcount_t */
+	struct mutex mutex;

This makes a little sense. Either you use refcount_t (which is atomic) or use mutex + regular variable.

Quoted 6 lines
Quoted 2 lines

Using a regular variable I lose all the benefits of refcount_t with underflow and other checks.

Then drop the mutex, atomic operations do not need an additional synchronisation. Btw, have you looked at kref APIs? Maybe that would make the intention clearer?

It's needed for

(quoted diff) +8 -0
+       mutex_lock(&pc->mutex);
+       if (refcount_read(&pc->buckets[bucket].used) == 0) {
+               config_bucket = true;
+               refcount_set(&pc->buckets[bucket].used, 1);
+       } else {
+               refcount_inc(&pc->buckets[bucket].used);
+       }
+       mutex_unlock(&pc->mutex);
Quoted 7 lines
Quoted 1 line

the refcount_read + refcount_set.

Which is simply wrong. Nobody should use atomics in such a way. Imagine if somebody wants to copy something like this in their code (in case of no mutex is there), they most likely won't notice this subtle bug.

Yes I understand that someone might think the additional mutex can be ""optional""

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

As you explained there might be case where refcount_read is zero but nother PWM channel is setting the value so one refcount gets lost.

Right, because you should use refcount_inc_and_test() and initialise it to -MAX instead of 0. Or something like this.

Mhhh I think API for _inc_and_test doesn't currently exist and I don't feel too confident implementing them currently.

Ther is refcount_inc_not_zero(), but the main point here that refcount_t seems not fit the case. It doesn't work with negative values, and 0 is special.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

kref I checked but not useful for the task.

Okay.

Quoted 2 lines

The logic here is

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

- refcount init as 0 (bucket unused)
- refcount set to 1 on first bucket use (bucket get configured)
- refcount increased if already used
- refcount decreased when PWM channel released
- bucket gets flagged as unused when refcount goes to 0 again

Do you think I should bite the bullet and just drop using refcount and implement a simple int variable protected by a mutex?

Yes.

Signature · 2 lines
With Best Regards,
Andy Shevchenko