pwm: airoha: Add support for EN7581 SoC
PATCH v17 1/1 pwm: airoha: Add support for EN7581 SoC
From: Benjamin Larsson <[email protected]>
Introduce driver for PWM module available on EN7581 SoC.
---
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 | 613 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 624 insertions(+) create mode 100644 drivers/pwm/pwm-airoha.c
drivers/pwm/Kconfig +10 -0
@@ -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
@@ -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
Quoted 3 lines · Christian Marangi
From: Benjamin Larsson <[email protected]>
Introduce driver for PWM module available on EN7581 SoC.
Almost from my perspective.
If you address the below as suggested, feel free to add
...
drivers/pwm/pwm-airoha.c line 3
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2022 Markus Gothe <[email protected]>
On my calendar 2025 is. Also MODULE_AUTHOR() list disagrees with this (you should add yourself somewhere here).
Quoted 9 lines
+ * Limitations:
+ * - Only 8 concurrent waveform generators are available for 8 combinations of
+ *duty_cycleand period. Waveform generators are shared between 16 GPIO
+ * pins and 17 SIPO GPIO pins.+ * - Supports only normal polarity. + * - On configuration the currently running period is completed. + * - Minimum supported period is 4ms + * - Maximum supported period is 1s+ */
...
drivers/pwm/pwm-airoha.c line 39
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+#define AIROHA_PWM_SGPIO_CLK_DIVR_32 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x3) +#define AIROHA_PWM_SGPIO_CLK_DIVR_16 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x2) +#define AIROHA_PWM_SGPIO_CLK_DIVR_8 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x1) +#define AIROHA_PWM_SGPIO_CLK_DIVR_4 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x0)
Remove 0x:s, they are just noise and moreover the list here is plain numbers, not bit combinations or so. In such cases we use decimal numbers.
...
Quoted 1 line
+/* Cycle cfg handles 4 generators in one register */
I am a bit lost in the meaning of "cycle cfg". Can you elaborate in the comment that it's readable in plain English?
Quoted 1 line
+#define
AIROHA_PWM_BUCKET_PER_CYCLE_CFG4
...
drivers/pwm/pwm-airoha.c line 141
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static u32 airoha_pwm_get_duty_ticks_from_ns (u32 period_ns,u32 duty_ns) +{ +return mul_u64_u32_div (duty_ns,AIROHA_PWM_DUTY_FULL , + period_ns);
It's less than 80 if put on one line!
Quoted 1 line
+}
...
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static int airoha_pwm_get_bucket (struct airoha_pwm *pc,int bucket, +u64 *period_ns,u64 *duty_ns) +{ +u32 period_tick, duty_tick;
drivers/pwm/pwm-airoha.c line 157
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+unsigned int offset; +u32 shift, val; +int ret; + + offset = bucket /AIROHA_PWM_BUCKET_PER_CYCLE_CFG ; + shift = bucket %AIROHA_PWM_BUCKET_PER_CYCLE_CFG ; + shift =AIROHA_PWM_REG_CYCLE_CFG_SHIFT (shift); + + ret =regmap_read (pc->regmap,AIROHA_PWM_REG_CYCLE_CFG_VALUE (offset), + &val);
You may shorten the line and the code (as below the same can be applied) by introducing
struct regmap *map = pc->regmap;
Quoted 25 lines
drivers/pwm/pwm-airoha.c new file +25
-0+if (ret) +return ret; + + period_tick =FIELD_GET (AIROHA_PWM_WAVE_GEN_CYCLE , val >> shift); + *period_ns = period_tick *AIROHA_PWM_PERIOD_TICK_NS ; + + offset = bucket /AIROHA_PWM_BUCKET_PER_FLASH_PROD ; + shift = bucket %AIROHA_PWM_BUCKET_PER_FLASH_PROD ; + shift =AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT (shift); + + ret =regmap_read (pc->regmap,AIROHA_PWM_REG_GPIO_FLASH_PRD_SET (offset), + &val); +if (ret) +return ret; + + duty_tick =FIELD_GET (AIROHA_PWM_GPIO_FLASH_PRD_HIGH , val >> shift); +/* +* Overflow can't occur in multiplication as duty_tick is just 8 bit +* and period_ns is clamped to AIROHA_PWM_PERIOD_MAX_NS and fit in a +* u64. +*/ + *duty_ns =DIV_U64_ROUND_UP (duty_tick * *period_ns,AIROHA_PWM_DUTY_FULL ); + +return 0 ; +}
...
drivers/pwm/pwm-airoha.c line 192
Quoted 9 lines
drivers/pwm/pwm-airoha.c new file +9
-0+static int airoha_pwm_get_generator (struct airoha_pwm *pc,u32 duty_ticks, +u32 period_ticks) +{ +int best = -ENOENT , unused = -ENOENT ; +u32 best_period_ticks =0 ; +u32 best_duty_ticks =0 ; +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) {
You want array_size.h.
Quoted 19 lines
drivers/pwm/pwm-airoha.c new file +18
-0+struct airoha_pwm_bucket *bucket = &pc->buckets[i]; +u32 bucket_period_ticks = bucket->period_ticks; +u32 bucket_duty_ticks = bucket->duty_ticks; +/* If found, save an unused bucket to return it later */ +if (refcount_read (&bucket->used) ==0 ) { + unused = i; +continue ; + } + +/* We found a matching bucket, exit early */ +if (duty_ticks == bucket_duty_ticks && + period_ticks == bucket_period_ticks) +return i; + +/* +* Unlike duty cycle zero, which can be handled by +* disabling PWM, a generator is needed for full duty +* cycle but it can be reused regardless of period
drivers/pwm/pwm-airoha.c line 221
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+*/ +if (duty_ticks ==AIROHA_PWM_DUTY_FULL && + bucket_duty_ticks ==AIROHA_PWM_DUTY_FULL ) +return i; + +/* +* With an unused bucket available, skip searching for +* a bucket to recycle (closer to the requested period/duty) +*/ +if (unused != -ENOENT )
Can unused be negative and have different error code or so? If not, simplify here (and in return below) to
if (unused >= 0)
drivers/pwm/pwm-airoha.c line 233
Quoted 12 lines
drivers/pwm/pwm-airoha.c new file +12
-0+continue ; + +/* Ignore bucket with invalid configs */ +if (bucket_period_ticks > period_ticks || + bucket_duty_ticks > duty_ticks) +continue ; + +/* +* Search for a bucket closer to the requested period/duty +* that has the maximal possible period that isn't bigger +* than the requested period. For that period pick the maximal +* duty_cycle that isn't bigger than the requested duty_cycle.
duty cycle
(with underscore I haven't found a variable in this function the comment refers to)
Quoted 13 lines
drivers/pwm/pwm-airoha.c new file +13
-0+*/ +if (bucket_period_ticks > best_period_ticks || + (bucket_period_ticks == best_period_ticks && + bucket_duty_ticks > best_duty_ticks)) { + best_period_ticks = bucket_period_ticks; + best_duty_ticks = bucket_duty_ticks; + best = i; + } + } + +/* With no unused bucket, return the best one found (if ever) */ +return unused == -ENOENT ? best : unused; +}
...
Quoted 18 lines
drivers/pwm/pwm-airoha.c new file +17
-0+static int airoha_pwm_consume_generator (struct airoha_pwm *pc, +u32 duty_ticks,u32 period_ticks, +unsigned int hwpwm) +{ +int bucket, ret; + +/* +* Search for a bucket that already satisfies duty and period +* or an unused one. +* If not found, -ENOENT is returned. +*/ + bucket =airoha_pwm_get_generator (pc, duty_ticks, period_ticks); +if (bucket <0 ) +return bucket; + +/* Release previous used bucket (if any) */ +airoha_pwm_release_bucket_config (pc, hwpwm);
drivers/pwm/pwm-airoha.c line 325
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+if (refcount_read (&pc->buckets[bucket].used) ==0 ) { + pc->buckets[bucket].period_ticks = period_ticks; + pc->buckets[bucket].duty_ticks = duty_ticks; + ret =airoha_pwm_apply_bucket_config (pc, bucket, + duty_ticks, + period_ticks); +if (ret) +return ret; + +refcount_set (&pc->buckets[bucket].used,1 );
What happens if refcount is updated in between? This is wrong use of atomics.
drivers/pwm/pwm-airoha.c line 327
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+ }else { +refcount_inc (&pc->buckets[bucket].used);
Ditto.
You probably wanted _inc_and_test() variant.
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+ } + +return bucket; +}
...
Quoted 8 lines
drivers/pwm/pwm-airoha.c new file +8
-0+static int airoha_pwm_config (struct airoha_pwm *pc,struct pwm_device *pwm, +u32 duty_ticks,u32 period_ticks) +{ +unsigned int hwpwm = pwm->hwpwm; +int bucket, ret; + + bucket =airoha_pwm_consume_generator (pc, duty_ticks, period_ticks, + hwpwm);
drivers/pwm/pwm-airoha.c line 442
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +9
-0+if (bucket <0 ) +return bucket; + + ret =airoha_pwm_config_flash_map (pc, hwpwm, bucket); +if (ret) { +refcount_dec (&pc->buckets[bucket].used); +return ret; + } +set_bit (hwpwm, pc->initialized);
Does it need to be atomic? (Just asking) If not, use non-atomic variant, i.e. __set_bit().
Quoted 22 lines
drivers/pwm/pwm-airoha.c new file +22
-0+ pc->channel_bucket[hwpwm] = bucket; + +/* +* SIPO are special GPIO attached to a shift register chip. The handling +* of this chip is internal to the SoC that takes care of applying the +* values based on the flash map. To apply a new flash map, it's needed +* to trigger a refresh on the shift register chip. +* If a SIPO is getting configuring , always reinit the shift register +* chip to make sure the correct flash map is applied. +* Skip reconfiguring the shift register if the related hwpwm +* is disabled (as it doesn't need to be mapped). +*/ +if (hwpwm >=AIROHA_PWM_NUM_GPIO ) { + ret =airoha_pwm_sipo_init (pc); +if (ret) { +airoha_pwm_release_bucket_config (pc, hwpwm); +return ret; + } + } + +return 0 ; +}
...
drivers/pwm/pwm-airoha.c line 472
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +6
-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);
As per above.
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+/* 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 ) +regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_FLASH_MODE ); +}
...
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+static int airoha_pwm_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct airoha_pwm *pc; +struct pwm_chip *chip; +unsigned int i;
drivers/pwm/pwm-airoha.c line 578
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+int ret; + + chip =devm_pwmchip_alloc (dev,AIROHA_PWM_MAX_CHANNELS ,sizeof (*pc)); +if (IS_ERR (chip)) +return PTR_ERR (chip); + + chip->ops = &airoha_pwm_ops; + pc =pwmchip_get_drvdata (chip); + + pc->regmap =device_node_to_regmap (dev->parent->of_node);
Please, use dev_of_node(dev->parent).
drivers/pwm/pwm-airoha.c line 585
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7
-0+if (IS_ERR (pc->regmap)) +return dev_err_probe (dev,PTR_ERR (pc->regmap),"Failed to get PWM regmap\n" ); + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) +refcount_set (&pc->buckets[i].used,0 ); + + ret =devm_pwmchip_add (&pdev->dev, chip);
Seems you have dev, why not use it?
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to add PWM chip\n" ); + +return 0 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 219 lines · Andy Shevchenko
Quoted 3 lines · Christian Marangi
From: Benjamin Larsson <[email protected]>
Introduce driver for PWM module available on EN7581 SoC.
Almost from my perspective.
If you address the below as suggested, feel free to addReviewed-by Andy Shevchenko <[email protected]>...
drivers/pwm/pwm-airoha.c line 3
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2022 Markus Gothe <[email protected]> On my calendar 2025 is. Also
MODULE_AUTHOR()list disagrees with this (you should add yourself somewhere here).Quoted 9 lines
+ * Limitations:
+ * - Only 8 concurrent waveform generators are available for 8 combinations of
+ *duty_cycleand period. Waveform generators are shared between 16 GPIO
+ * pins and 17 SIPO GPIO pins.+ * - Supports only normal polarity. + * - On configuration the currently running period is completed. + * - Minimum supported period is 4ms + * - Maximum supported period is 1s+ */
...
drivers/pwm/pwm-airoha.c line 39
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+#define AIROHA_PWM_SGPIO_CLK_DIVR_32 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x3) +#define AIROHA_PWM_SGPIO_CLK_DIVR_16 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x2) +#define AIROHA_PWM_SGPIO_CLK_DIVR_8 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x1) +#define AIROHA_PWM_SGPIO_CLK_DIVR_4 FIELD_PREP_CONST(AIROHA_PWM_SGPIO_CLK_DIVR, 0x0)Remove 0x:s, they are just noise and moreover the list here is plain numbers, not bit combinations or so. In such cases we use decimal numbers.
...
Quoted 1 line
+/* Cycle cfg handles 4 generators in one register */
I am a bit lost in the meaning of "cycle cfg". Can you elaborate in the comment that it's readable in plain English?
Quoted 1 line
+#define
AIROHA_PWM_BUCKET_PER_CYCLE_CFG4...
drivers/pwm/pwm-airoha.c line 141
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static u32 airoha_pwm_get_duty_ticks_from_ns (u32 period_ns,u32 duty_ns) +{ +return mul_u64_u32_div (duty_ns,AIROHA_PWM_DUTY_FULL , + period_ns);It's less than 80 if put on one line!
Quoted 1 line
+}
...
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static int airoha_pwm_get_bucket (struct airoha_pwm *pc,int bucket, +u64 *period_ns,u64 *duty_ns) +{ +u32 period_tick, duty_tick;drivers/pwm/pwm-airoha.c line 157
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+unsigned int offset; +u32 shift, val; +int ret; + + offset = bucket /AIROHA_PWM_BUCKET_PER_CYCLE_CFG ; + shift = bucket %AIROHA_PWM_BUCKET_PER_CYCLE_CFG ; + shift =AIROHA_PWM_REG_CYCLE_CFG_SHIFT (shift); + + ret =regmap_read (pc->regmap,AIROHA_PWM_REG_CYCLE_CFG_VALUE (offset), + &val);You may shorten the line and the code (as below the same can be applied) by introducing
struct regmap *map = pc->regmap;Quoted 25 lines
drivers/pwm/pwm-airoha.c new file +25
-0+if (ret) +return ret; + + period_tick =FIELD_GET (AIROHA_PWM_WAVE_GEN_CYCLE , val >> shift); + *period_ns = period_tick *AIROHA_PWM_PERIOD_TICK_NS ; + + offset = bucket /AIROHA_PWM_BUCKET_PER_FLASH_PROD ; + shift = bucket %AIROHA_PWM_BUCKET_PER_FLASH_PROD ; + shift =AIROHA_PWM_REG_GPIO_FLASH_PRD_SHIFT (shift); + + ret =regmap_read (pc->regmap,AIROHA_PWM_REG_GPIO_FLASH_PRD_SET (offset), + &val); +if (ret) +return ret; + + duty_tick =FIELD_GET (AIROHA_PWM_GPIO_FLASH_PRD_HIGH , val >> shift); +/* +* Overflow can't occur in multiplication as duty_tick is just 8 bit +* and period_ns is clamped to AIROHA_PWM_PERIOD_MAX_NS and fit in a +* u64. +*/ + *duty_ns =DIV_U64_ROUND_UP (duty_tick * *period_ns,AIROHA_PWM_DUTY_FULL ); + +return 0 ; +}...
drivers/pwm/pwm-airoha.c line 192
Quoted 9 lines
drivers/pwm/pwm-airoha.c new file +9
-0+static int airoha_pwm_get_generator (struct airoha_pwm *pc,u32 duty_ticks, +u32 period_ticks) +{ +int best = -ENOENT , unused = -ENOENT ; +u32 best_period_ticks =0 ; +u32 best_duty_ticks =0 ; +unsigned int i; + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) {You want
array_size.h.Quoted 19 lines
drivers/pwm/pwm-airoha.c new file +18
-0+struct airoha_pwm_bucket *bucket = &pc->buckets[i]; +u32 bucket_period_ticks = bucket->period_ticks; +u32 bucket_duty_ticks = bucket->duty_ticks; +/* If found, save an unused bucket to return it later */ +if (refcount_read (&bucket->used) ==0 ) { + unused = i; +continue ; + } + +/* We found a matching bucket, exit early */ +if (duty_ticks == bucket_duty_ticks && + period_ticks == bucket_period_ticks) +return i; + +/* +* Unlike duty cycle zero, which can be handled by +* disabling PWM, a generator is needed for full duty +* cycle but it can be reused regardless of period drivers/pwm/pwm-airoha.c line 221
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+*/ +if (duty_ticks ==AIROHA_PWM_DUTY_FULL && + bucket_duty_ticks ==AIROHA_PWM_DUTY_FULL ) +return i; + +/* +* With an unused bucket available, skip searching for +* a bucket to recycle (closer to the requested period/duty) +*/ +if (unused != -ENOENT )Can unused be negative and have different error code or so? If not, simplify here (and in return below) to
if (unused >= 0)drivers/pwm/pwm-airoha.c line 233
Quoted 12 lines
drivers/pwm/pwm-airoha.c new file +12
-0+continue ; + +/* Ignore bucket with invalid configs */ +if (bucket_period_ticks > period_ticks || + bucket_duty_ticks > duty_ticks) +continue ; + +/* +* Search for a bucket closer to the requested period/duty +* that has the maximal possible period that isn't bigger +* than the requested period. For that period pick the maximal +* duty_cycle that isn't bigger than the requested duty_cycle. duty cycle
(with underscore I haven't found a variable in this function the comment refers to)
Quoted 13 lines
drivers/pwm/pwm-airoha.c new file +13
-0+*/ +if (bucket_period_ticks > best_period_ticks || + (bucket_period_ticks == best_period_ticks && + bucket_duty_ticks > best_duty_ticks)) { + best_period_ticks = bucket_period_ticks; + best_duty_ticks = bucket_duty_ticks; + best = i; + } + } + +/* With no unused bucket, return the best one found (if ever) */ +return unused == -ENOENT ? best : unused; +}...
Quoted 18 lines
drivers/pwm/pwm-airoha.c new file +17
-0+static int airoha_pwm_consume_generator (struct airoha_pwm *pc, +u32 duty_ticks,u32 period_ticks, +unsigned int hwpwm) +{ +int bucket, ret; + +/* +* Search for a bucket that already satisfies duty and period +* or an unused one. +* If not found, -ENOENT is returned. +*/ + bucket =airoha_pwm_get_generator (pc, duty_ticks, period_ticks); +if (bucket <0 ) +return bucket; + +/* Release previous used bucket (if any) */ +airoha_pwm_release_bucket_config (pc, hwpwm);drivers/pwm/pwm-airoha.c line 325
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+if (refcount_read (&pc->buckets[bucket].used) ==0 ) { + pc->buckets[bucket].period_ticks = period_ticks; + pc->buckets[bucket].duty_ticks = duty_ticks; + ret =airoha_pwm_apply_bucket_config (pc, bucket, + duty_ticks, + period_ticks); +if (ret) +return ret; + +refcount_set (&pc->buckets[bucket].used,1 );What happens if refcount is updated in between? This is wrong use of atomics.
Quoted 7 lines
drivers/pwm/pwm-airoha.c line 327
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+ }else { +refcount_inc (&pc->buckets[bucket].used);Ditto.
You probably wanted
_inc_and_test()variant.
The main problem is that adding macro for refcount and atomic is that normaly you expect to have parallel ASM code for it to have real aotmic OP. Anyway I think I solved it with a simple mutex.
The usage of refcount here it's not really for atomic but for object tracking but refcount doesn't love to initialize from 0 and incremented so I have to use this _set and _inc thing.
Quoted 24 lines
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+ } + +return bucket; +}...
Quoted 16 lines
drivers/pwm/pwm-airoha.c new file +16
-0+static int airoha_pwm_config (struct airoha_pwm *pc,struct pwm_device *pwm, +u32 duty_ticks,u32 period_ticks) +{ +unsigned int hwpwm = pwm->hwpwm; +int bucket, ret; + + bucket =airoha_pwm_consume_generator (pc, duty_ticks, period_ticks, + hwpwm); +if (bucket <0 ) +return bucket; + + ret =airoha_pwm_config_flash_map (pc, hwpwm, bucket); +if (ret) { +refcount_dec (&pc->buckets[bucket].used); +return ret; + }
Quoted 5 lines
drivers/pwm/pwm-airoha.c line 442
Quoted 1 line
drivers/pwm/pwm-airoha.c new file +1
-0+set_bit (hwpwm, pc->initialized);Does it need to be atomic? (Just asking) If not, use non-atomic variant, i.e.
__set_bit().
Yes it needs to be atomic since the initialized bitmask is global.
Quoted 84 lines
Quoted 22 lines
drivers/pwm/pwm-airoha.c new file +22
-0+ pc->channel_bucket[hwpwm] = bucket; + +/* +* SIPO are special GPIO attached to a shift register chip. The handling +* of this chip is internal to the SoC that takes care of applying the +* values based on the flash map. To apply a new flash map, it's needed +* to trigger a refresh on the shift register chip. +* If a SIPO is getting configuring , always reinit the shift register +* chip to make sure the correct flash map is applied. +* Skip reconfiguring the shift register if the related hwpwm +* is disabled (as it doesn't need to be mapped). +*/ +if (hwpwm >=AIROHA_PWM_NUM_GPIO ) { + ret =airoha_pwm_sipo_init (pc); +if (ret) { +airoha_pwm_release_bucket_config (pc, hwpwm); +return ret; + } + } + +return 0 ; +}...
drivers/pwm/pwm-airoha.c line 472
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +6
-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);As per above.
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+/* 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 ) +regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_FLASH_MODE ); +}...
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+static int airoha_pwm_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct airoha_pwm *pc; +struct pwm_chip *chip; +unsigned int i;drivers/pwm/pwm-airoha.c line 578
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+int ret; + + chip =devm_pwmchip_alloc (dev,AIROHA_PWM_MAX_CHANNELS ,sizeof (*pc)); +if (IS_ERR (chip)) +return PTR_ERR (chip); + + chip->ops = &airoha_pwm_ops; + pc =pwmchip_get_drvdata (chip); + + pc->regmap =device_node_to_regmap (dev->parent->of_node);Please, use
dev_of_node(dev->parent).drivers/pwm/pwm-airoha.c line 585
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7
-0+if (IS_ERR (pc->regmap)) +return dev_err_probe (dev,PTR_ERR (pc->regmap),"Failed to get PWM regmap\n" ); + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) +refcount_set (&pc->buckets[i].used,0 ); + + ret =devm_pwmchip_add (&pdev->dev, chip);Seems you have dev, why not use it?
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to add PWM chip\n" ); + +return 0 ; +}Signature · 2 lines
With Best Regards, Andy Shevchenko
Signature · 1 line
Ansuel
Fri, Jun 27, 2025 at 12:52:06AM +0200, Christian Marangi kirjoitti:
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Christian Marangi
...
Quoted 20 lines
Quoted 13 lines
drivers/pwm/pwm-airoha.c line 325
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+if (refcount_read (&pc->buckets[bucket].used) ==0 ) { + pc->buckets[bucket].period_ticks = period_ticks; + pc->buckets[bucket].duty_ticks = duty_ticks; + ret =airoha_pwm_apply_bucket_config (pc, bucket, + duty_ticks, + period_ticks); +if (ret) +return ret; + +refcount_set (&pc->buckets[bucket].used,1 );What happens if refcount is updated in between? This is wrong use of atomics.
Quoted 6 lines
drivers/pwm/pwm-airoha.c line 327
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+ }else { +refcount_inc (&pc->buckets[bucket].used);Ditto.
You probably wanted
_inc_and_test()variant.
Quoted 7 lines
The main problem is that adding macro for refcount and atomic is that normaly you expect to have parallel ASM code for it to have real aotmic OP. Anyway I think I solved it with a simple mutex.
The usage of refcount here it's not really for atomic but for object tracking but refcount doesn't love to initialize from 0 and incremented so I have to use this
_setand_incthing.
Indeed, the main use of refcount is to start with 1, when object gets initialised and free it on 0, hence if there are more users the count goes up and drops when a user of the resource drops.
Quoted 1 line
Quoted 1 line
Quoted 1 line
(quoted diff) +1
-0+ }
Signature · 2 lines
With Best Regards, Andy Shevchenko
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Christian,
kernel test robot noticed the following build warnings:
[auto build test WARNING on linus/master]
[also build test WARNING on v6.16-rc3 next-20250626]
[cannot apply to thierry-reding-pwm/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/pwm-airoha-Add-support-for-EN7581-SoC/20250626-035111 base: linus/master patch link: https://lore.kernel.org/r/20250625194919.94214-1-ansuelsmth%40gmail.com patch subject: [PATCH v17] pwm: airoha: Add support for EN7581 SoC config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: clang version 20.1.7 (https://github.com/llvm/llvm-project 6146a88f60492b520a36f8f8f3231e15f3cc6082) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> drivers/pwm/pwm-airoha.c:498:6: warning: variable 'period_ns' is uninitialized when used here [-Wuninitialized]
498 | if (period_ns < AIROHA_PWM_PERIOD_TICK_NS)
| ^~~~~~~~~
drivers/pwm/pwm-airoha.c:486:15: note: initialize the variable 'period_ns' to silence this warning
486 | u32 period_ns, duty_ns;
| ^
| = 01 warning generated.
vim +/period_ns +498 drivers/pwm/pwm-airoha.c
480
481 static int airoha_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
482 const struct pwm_state *state)
483 {
484 struct airoha_pwm *pc = pwmchip_get_drvdata(chip);
485 u32 period_ticks, duty_ticks;
486 u32 period_ns, duty_ns;
487
488 if (!state->enabled) {
489 airoha_pwm_disable(pc, pwm);
490 return 0;
491 }
492
493 /* Only normal polarity is supported */
494 if (state->polarity == PWM_POLARITY_INVERSED)
495 return -EINVAL;
496
497 /* Exit early if period is less than minimum supported */
> 498 if (period_ns < AIROHA_PWM_PERIOD_TICK_NS)
499 return -EINVAL;
500
501 /* Clamp period to MAX supported value */
502 if (state->period > AIROHA_PWM_PERIOD_MAX_NS)
503 period_ns = AIROHA_PWM_PERIOD_MAX_NS;
504 else
505 period_ns = state->period;
506
507 /* Validate duty to configured period */
508 if (state->duty_cycle > period_ns)
509 duty_ns = period_ns;
510 else
511 duty_ns = state->duty_cycle;
512
513 /*
514 * Period goes at 4ns step, normalize it to check if we can
515 * share a generator.
516 */
517 period_ns = rounddown(period_ns, AIROHA_PWM_PERIOD_TICK_NS);
518
519 /*
520 * Duty is divided in 255 segment, normalize it to check if we
521 * can share a generator.
522 */
523 duty_ns = DIV_U64_ROUND_UP(duty_ns * AIROHA_PWM_DUTY_FULL,
524 AIROHA_PWM_DUTY_FULL);
525
526 /* Convert ns to ticks */
527 period_ticks = airoha_pwm_get_period_ticks_from_ns(period_ns);
528 duty_ticks = airoha_pwm_get_duty_ticks_from_ns(period_ns, duty_ns);
529
530 return airoha_pwm_config(pc, pwm, period_ticks, duty_ticks);
531 }
532
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki