math.h: provide rounddown_ull variant for rounddown MACRO
PATCH v15 1/2 math.h: provide rounddown_ull variant for rounddown MACRO
There is currently a problem with the usage of rounddown MACRO with u64 dividends. This cause compilation error on specific arch where 64bit division is done on 32bit system.
To be more specific GCC try 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_ull that can be used exactly for this scenario.
rounddown_ull new MACRO use the do_div MACRO that do the heavy work of handling internally all the magic for 64bit division on 32bit (and indirectly fix the compilation error).
---
Changes v15:
- Add this patch
include/linux/math.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/math.h +11 -0
@@ -88,6 +88,17 @@ __x - (__x % (y)); \ } \ ) +/* +* Same as above but for u64 dividends. divisor must be a 32-bit +* number. +*/ +#define rounddown_ull (x, y) ( \ +{ \ + unsigned long long __x = (x); \ + unsigned long long _tmp = __x; \ + __x - do_div(_tmp, (y)); \ +} \ +)/* * Divide positive or negative dividend by positive or negative divisor
Signature · 1 line
2.48.1
PATCH v15 2/2 pwm: airoha: Add support for EN7581 SoC
From: Benjamin Larsson <[email protected]>
Introduce driver for PWM module available on EN7581 SoC.
---
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 | 11 + drivers/pwm/Makefile | 1 + drivers/pwm/pwm-airoha.c | 570 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 582 insertions(+) create mode 100644 drivers/pwm/pwm-airoha.c
drivers/pwm/Kconfig +11 -0
@@ -54,6 +54,17 @@ 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 + depends on OF + 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 +570 -0
570 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH v15 2/2] pwm: airoha: Add support for EN7581 SoC
Quoted 2 lines · Christian Marangi
Introduce driver for PWM module available on EN7581 SoC.
...
Quoted 10 lines
Signed-off-by Benjamin Larsson <[email protected]>Reviewed-by AngeloGioacchino Del Regno <[email protected]>Co-developed-by Lorenzo Bianconi <[email protected]>Signed-off-by Lorenzo Bianconi <[email protected]>Co-developed-by Christian Marangi <[email protected]>Signed-off-by Christian Marangi <[email protected]>---
Changes v15:
- Fix compilation error for 64bit division on 32bit (patch 01)
- Add prefer async probe
Wow, I am impressed!
...
drivers/pwm/Kconfig config PWM_ADP5585 · line 60
Quoted 5 lines
drivers/pwm/Kconfig +4
-0+config PWM_AIROHA + tristate "Airoha PWM support" + depends on ARCH_AIROHA || COMPILE_TEST + depends on OF
There is nothing dependent on this. If you want to enable run-time, why not using this in conjunction with the COMPILE_TEST?
Quoted 1 line
+ select
REGMAP_MMIO
...
drivers/pwm/pwm-airoha.c line 18
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +4
-0+#include <linux/bitfield.h> +#include <linux/bitops.h> +#include <linux/err.h> +#include <linux/gpio.h>
Have you had a chance to read the top of that header file? No, just no. This header must not be used in the new code.
drivers/pwm/pwm-airoha.c line 24
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +6
-0+#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/math64.h> +#include <linux/mfd/syscon.h> +#include <linux/module.h> +#include <linux/of.h>
Nothing is used from this header. You actually missed mod_devicetable.h.
drivers/pwm/pwm-airoha.c line 27
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h>
Missing headers, such as types.h.
Please, follow the IWYU principle.
...
drivers/pwm/pwm-airoha.c line 91
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +3
-0+struct airoha_pwm { +struct regmap *regmap; +u64 initialized;
Is it bitmap? This looks really weird, at least a comment is a must to explain why 64-bit for the variable that suggests (by naming) only a single bit.
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5
-0+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 line 140
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static u32 airoha_pwm_get_duty_ticks_from_ns (u64 period_ns,u64 duty_ns) +{ +return mul_u64_u64_div_u64 (duty_ns,AIROHA_PWM_DUTY_FULL , + period_ns);
For readability this can be one line.
Quoted 1 line
+}
...
drivers/pwm/pwm-airoha.c line 164
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+regmap_read (pc->regmap,AIROHA_PWM_REG_GPIO_FLASH_PRD_SET (offset), + &val);
Ditto.
Btw, no error checks for regmap_*() calls?
drivers/pwm/pwm-airoha.c line 178
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static int airoha_pwm_get_generator (struct airoha_pwm *pc,u64 duty_ns, +u64 period_ns) +{ +int i, best = -ENOENT , unused = -ENOENT ;
Why is 'i' signed?
Quoted 52 lines
drivers/pwm/pwm-airoha.c new file +52
-0+u64 best_period_ns =0 ; +u64 best_duty_ns =0 ; + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) { +struct airoha_pwm_bucket *bucket = &pc->buckets[i]; +u64 bucket_period_ns = bucket->period_ns; +u64 bucket_duty_ns = bucket->duty_ns; +u32 duty_ticks, duty_ticks_bucket; + +/* If found, save an unused bucket to return it later */ +if (!bucket->used) { + unused = i; +continue ; + } + +/* We found a matching bucket, exit early */ +if (duty_ns == bucket_duty_ns && + period_ns == bucket_period_ns) +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 +*/ + duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns); + duty_ticks_bucket =airoha_pwm_get_duty_ticks_from_ns (bucket_period_ns, + bucket_duty_ns); +if (duty_ticks ==AIROHA_PWM_DUTY_FULL && + duty_ticks_bucket ==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 ) +continue ; + +/* Ignore bucket with invalid configs */ +if (bucket_period_ns > period_ns || + bucket_duty_ns > duty_ns) +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. +*/ + if (bucket_period_ns > best_period_ns || + (bucket_period_ns == best_period_ns &&
drivers/pwm/pwm-airoha.c line 240
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+ bucket_duty_ns > best_duty_ns)) { + best_period_ns = bucket_period_ns; + best_duty_ns = bucket_duty_ns; + best = i; + } + } + +/* With no unused bucket, return the best one found (if ever) */ +return unused == -ENOENT ? best : unused; +}
This entire function reminds me of something from util_macros.h or bsearch.h or similar. Can you double check that you really can't utilise one of those?
...
drivers/pwm/pwm-airoha.c line 249
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+/* Nothing to clear, PWM channel never used */ +if (!(pc->initialized &BIT_ULL (hwpwm))) +return ;
So, it's a bitmap, why not use bitmap types and APIs?
drivers/pwm/pwm-airoha.c line 252
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+ bucket = pc->channel_bucket[hwpwm]; + pc->buckets[bucket].used &= ~BIT_ULL (hwpwm);
Oh, why do you need 'used' to be also 64-bit?
Quoted 1 line
+}
...
drivers/pwm/pwm-airoha.c line 298
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+/* +* Search for a bucket that already satisfy duty and period
satisfies
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+* or an unused one. +* If not found, -ENOENT is returned. +*/
...
drivers/pwm/pwm-airoha.c line 325
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+static int airoha_pwm_sipo_init (struct airoha_pwm *pc) +{ +u32 val; + +if (!(pc->initialized >>AIROHA_PWM_NUM_GPIO )) +return 0 ;
It will be clearer if you use bitmap APIs here to show how many bits are indeed being used in "initialized" for this check. Basically it's something like find_first_set_from() or so (I don't remember names by heart). It will show the starting point and the limit.
...
drivers/pwm/pwm-airoha.c line 328
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_MODE_74HC164 );
This is interesting. Can the gpio-74x164 be used as a whole?
...
Quoted 1 line
+
regmap_write(pc->regmap, AIROHA_PWM_REG_SGPIO_CLK_DLY, 0x0);
'0x' is not needed.
...
drivers/pwm/pwm-airoha.c line 362
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+if (regmap_read_poll_timeout (pc->regmap,AIROHA_PWM_REG_SGPIO_LED_DATA , val, + !(val &AIROHA_PWM_SGPIO_LED_DATA_SHIFT_FLAG ), +10 ,200 *USEC_PER_MSEC )) +return -ETIMEDOUT ;
Why is the error code shadowed?
ret = regmap...
if (ret)
return ret;
...
drivers/pwm/pwm-airoha.c line 362
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+if (regmap_read_poll_timeout (pc->regmap,AIROHA_PWM_REG_SGPIO_LED_DATA , val, + !(val &AIROHA_PWM_SGPIO_LED_DATA_SHIFT_FLAG ), +10 ,200 *USEC_PER_MSEC )) +return -ETIMEDOUT ;
Ditto.
...
Quoted 1 line
+ /* index -1 means disable PWM channel */
Negative index means
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +2
-0+if (index <0 ) { + }
...
drivers/pwm/pwm-airoha.c line 412
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+static int airoha_pwm_config (struct airoha_pwm *pc,struct pwm_device *pwm, +u64 duty_ns,u64 period_ns) +{ +unsigned int hwpwm = pwm->hwpwm; +int bucket; + + bucket =airoha_pwm_consume_generator (pc, duty_ns, period_ns, + hwpwm); +if (bucket <0 ) +return -EBUSY ;
Why is the error code shadowed?
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+ +airoha_pwm_config_flash_map (pc, hwpwm, bucket); + + pc->initialized |=BIT_ULL (hwpwm);
drivers/pwm/pwm-airoha.c line 426
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-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 we are configuring a SIPO, always reinit the shift register chip +* to make sure the correct flash map is applied. +* We skip reconfiguring the shift register if we related hwpwm
s/we/the/ ?
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7
-0+* is disabled (as it doesn't need to be mapped). +*/ +if (!(pc->initialized &BIT_ULL (hwpwm)) && hwpwm >=AIROHA_PWM_NUM_GPIO ) +airoha_pwm_sipo_init (pc); + +return 0 ; +}
...
drivers/pwm/pwm-airoha.c line 373
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+if (!(pc->initialized >>AIROHA_PWM_NUM_GPIO )) +regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_FLASH_MODE );
If you use regmap cache the "initialized" might be not needed at all. It might be possible to read back (from the cache) the current state of some registers. Have you checked if this is a feasible approach?
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* + * Duty goes at 255 step, normalize it to check if we can
"in steps of 255 ns" ?
The original comment is confusing as step in singular form may mislead.
Quoted 2 lines
(quoted diff) +2
-0+ * share a generator. + */
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v15 2/2] pwm: airoha: Add support for EN7581 SoC
Quoted 34 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Introduce driver for PWM module available on EN7581 SoC.
...
Quoted 10 lines
Signed-off-by Benjamin Larsson <[email protected]>Reviewed-by AngeloGioacchino Del Regno <[email protected]>Co-developed-by Lorenzo Bianconi <[email protected]>Signed-off-by Lorenzo Bianconi <[email protected]>Co-developed-by Christian Marangi <[email protected]>Signed-off-by Christian Marangi <[email protected]>---
Changes v15:
- Fix compilation error for 64bit division on 32bit (patch 01)
- Add prefer async probeWow, I am impressed!
...
drivers/pwm/Kconfig config PWM_ADP5585 · line 60
Quoted 5 lines
drivers/pwm/Kconfig +4
-0+config PWM_AIROHA + tristate "Airoha PWM support" + depends on ARCH_AIROHA || COMPILE_TEST + depends on OFThere is nothing dependent on this. If you want to enable run-time, why not using this in conjunction with the
COMPILE_TEST?Quoted 1 line
+ select
REGMAP_MMIO...
Quoted 9 lines
drivers/pwm/pwm-airoha.c line 18
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +4
-0+#include <linux/bitfield.h> +#include <linux/bitops.h> +#include <linux/err.h> +#include <linux/gpio.h> Have you had a chance to read the top of that header file? No, just no. This header must not be used in the new code.
As you can see by the changelog this is very old code so I wasn't aware.
Quoted 8 lines
drivers/pwm/pwm-airoha.c line 24
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +6
-0+#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/math64.h> +#include <linux/mfd/syscon.h> +#include <linux/module.h> +#include <linux/of.h>
Quoted 9 lines
Nothing is used from this header. You actually missed
mod_devicetable.h.drivers/pwm/pwm-airoha.c line 27
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> Missing headers, such as
types.h.
Please, follow the IWYU principle.
Aside from types do you have hint of other missing header? Do you have a tool to identify the missing header?
Quoted 11 lines
...
drivers/pwm/pwm-airoha.c line 91
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +3
-0+struct airoha_pwm { +struct regmap *regmap; +u64 initialized;Is it bitmap? This looks really weird, at least a comment is a must to explain why 64-bit for the variable that suggests (by naming) only a single bit.
There could be 33 PWM channel so it doesn't fit a u32. This is why u64. I feel bitmap might be overkill for the task but if requested, I will change it.
Quoted 8 lines
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +5
-0+struct airoha_pwm_bucket buckets[AIROHA_PWM_NUM_BUCKETS ]; + +/* Cache bucket used by each pwm channel */ +u8 channel_bucket[AIROHA_PWM_MAX_CHANNELS ]; +};...
Quoted 7 lines
drivers/pwm/pwm-airoha.c line 140
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static u32 airoha_pwm_get_duty_ticks_from_ns (u64 period_ns,u64 duty_ns) +{ +return mul_u64_u64_div_u64 (duty_ns,AIROHA_PWM_DUTY_FULL , + period_ns);For readability this can be one line.
Mhhh I try to limit code to 80 column where possible.
Quoted 77 lines
Quoted 1 line
+}
...
drivers/pwm/pwm-airoha.c line 164
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+regmap_read (pc->regmap,AIROHA_PWM_REG_GPIO_FLASH_PRD_SET (offset), + &val);Ditto.
Btw, no error checks for regmap_*() calls?
drivers/pwm/pwm-airoha.c line 178
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+static int airoha_pwm_get_generator (struct airoha_pwm *pc,u64 duty_ns, +u64 period_ns) +{ +int i, best = -ENOENT , unused = -ENOENT ;Why is 'i' signed?
Quoted 58 lines
drivers/pwm/pwm-airoha.c new file +58
-0+u64 best_period_ns =0 ; +u64 best_duty_ns =0 ; + +for (i =0 ; i <ARRAY_SIZE (pc->buckets); i++) { +struct airoha_pwm_bucket *bucket = &pc->buckets[i]; +u64 bucket_period_ns = bucket->period_ns; +u64 bucket_duty_ns = bucket->duty_ns; +u32 duty_ticks, duty_ticks_bucket; + +/* If found, save an unused bucket to return it later */ +if (!bucket->used) { + unused = i; +continue ; + } + +/* We found a matching bucket, exit early */ +if (duty_ns == bucket_duty_ns && + period_ns == bucket_period_ns) +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 +*/ + duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns); + duty_ticks_bucket =airoha_pwm_get_duty_ticks_from_ns (bucket_period_ns, + bucket_duty_ns); +if (duty_ticks ==AIROHA_PWM_DUTY_FULL && + duty_ticks_bucket ==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 ) +continue ; + +/* Ignore bucket with invalid configs */ +if (bucket_period_ns > period_ns || + bucket_duty_ns > duty_ns) +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. +*/ +if (bucket_period_ns > best_period_ns || + (bucket_period_ns == best_period_ns && + bucket_duty_ns > best_duty_ns)) { + best_period_ns = bucket_period_ns; + best_duty_ns = bucket_duty_ns; + best = i; + } + }
Quoted 9 lines
drivers/pwm/pwm-airoha.c line 240
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+ +/* With no unused bucket, return the best one found (if ever) */ +return unused == -ENOENT ? best : unused; +}This entire function reminds me of something from
util_macros.horbsearch.hor similar. Can you double check that you really can't utilise one of those?
I checked and bsearch can't be used and and for util_macros the closest can't be used. As explained in previous revision, it's not simply a matter of finding the closest value but it's about finding a value that is closest to the period_ns and only with that condition satisfied one closest to the duty. We can't mix them as search for the closest of both.
Quoted 6 lines
...
drivers/pwm/pwm-airoha.c line 249
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+/* Nothing to clear, PWM channel never used */ +if (!(pc->initialized &BIT_ULL (hwpwm))) +return ;
Quoted 7 lines
So, it's a bitmap, why not use bitmap types and APIs?
drivers/pwm/pwm-airoha.c line 252
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+ bucket = pc->channel_bucket[hwpwm]; + pc->buckets[bucket].used &= ~BIT_ULL (hwpwm);Oh, why do you need 'used' to be also 64-bit?
In the extreme case, a bucket can be used by all 33 PWM channel.
Quoted 28 lines
Quoted 1 line
+}
...
drivers/pwm/pwm-airoha.c line 298
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+/* +* Search for a bucket that already satisfy duty and period satisfies
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+* or an unused one. +* If not found, -ENOENT is returned. +*/ ...
drivers/pwm/pwm-airoha.c line 325
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+static int airoha_pwm_sipo_init (struct airoha_pwm *pc) +{ +u32 val; + +if (!(pc->initialized >>AIROHA_PWM_NUM_GPIO )) +return 0 ;It will be clearer if you use bitmap APIs here to show how many bits are indeed being used in "initialized" for this check. Basically it's something like
find_first_set_from()or so (I don't remember names by heart). It will show the starting point and the limit.
Quoted 7 lines
...
drivers/pwm/pwm-airoha.c line 328
Quoted 2 lines
drivers/pwm/pwm-airoha.c new file +2
-0+regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_MODE_74HC164 );This is interesting. Can the gpio-74x164 be used as a whole?
It's sad but the shift register chip is entirely handled by the SoC. We can't access to it's registers so the dedicated gpio driver can't be used.
Quoted 79 lines
...
Quoted 1 line
+
regmap_write(pc->regmap, AIROHA_PWM_REG_SGPIO_CLK_DLY, 0x0);'0x' is not needed.
...
drivers/pwm/pwm-airoha.c line 362
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+if (regmap_read_poll_timeout (pc->regmap,AIROHA_PWM_REG_SGPIO_LED_DATA , val, + !(val &AIROHA_PWM_SGPIO_LED_DATA_SHIFT_FLAG ), +10 ,200 *USEC_PER_MSEC )) +return -ETIMEDOUT ;Why is the error code shadowed?
ret = regmap...
if (ret)
return ret;...
drivers/pwm/pwm-airoha.c line 362
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+if (regmap_read_poll_timeout (pc->regmap,AIROHA_PWM_REG_SGPIO_LED_DATA , val, + !(val &AIROHA_PWM_SGPIO_LED_DATA_SHIFT_FLAG ), +10 ,200 *USEC_PER_MSEC )) +return -ETIMEDOUT ;Ditto.
...
Quoted 1 line
+ /* index -1 means disable PWM channel */
Negative index means
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +2
-0+if (index <0 ) { + }...
drivers/pwm/pwm-airoha.c line 412
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-0+static int airoha_pwm_config (struct airoha_pwm *pc,struct pwm_device *pwm, +u64 duty_ns,u64 period_ns) +{ +unsigned int hwpwm = pwm->hwpwm; +int bucket; + + bucket =airoha_pwm_consume_generator (pc, duty_ns, period_ns, + hwpwm); +if (bucket <0 ) +return -EBUSY ;Why is the error code shadowed?
Quoted 4 lines
drivers/pwm/pwm-airoha.c new file +4
-0+ +airoha_pwm_config_flash_map (pc, hwpwm, bucket); + + pc->initialized |=BIT_ULL (hwpwm);drivers/pwm/pwm-airoha.c line 426
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +10
-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 we are configuring a SIPO, always reinit the shift register chip +* to make sure the correct flash map is applied. +* We skip reconfiguring the shift register if we related hwpwm s/we/the/ ?
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +7
-0+* is disabled (as it doesn't need to be mapped). +*/ +if (!(pc->initialized &BIT_ULL (hwpwm)) && hwpwm >=AIROHA_PWM_NUM_GPIO ) +airoha_pwm_sipo_init (pc); + +return 0 ; +}...
Quoted 8 lines
drivers/pwm/pwm-airoha.c line 373
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+if (!(pc->initialized >>AIROHA_PWM_NUM_GPIO )) +regmap_clear_bits (pc->regmap,AIROHA_PWM_REG_SIPO_FLASH_MODE_CFG , +AIROHA_PWM_SERIAL_GPIO_FLASH_MODE );If you use regmap cache the "initialized" might be not needed at all. It might be possible to read back (from the cache) the current state of some registers. Have you checked if this is a feasible approach?
The initialized is still needed to understand if a PWM channel has been provisioned or it's still "dirty" and assigned to a bucket externally to the kernel (for example by a bootloader)
Also the documentation is not very clear on what is really considered a volatile register or not so maybe skipping some write might introduce some unintended regression.
Quoted 8 lines
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* + * Duty goes at 255 step, normalize it to check if we can"in steps of 255 ns" ?
The original comment is confusing as step in singular form may mislead.
I think you are confused duty is divided in 255 segment so I chencged this to Duty is divided in 255 segment, normalize it t...
Quoted 6 lines
Quoted 2 lines
(quoted diff) +2
-0+ * share a generator. + */Signature · 2 lines
With Best Regards, Andy Shevchenko
Signature · 1 line
Ansuel
Re: [PATCH v15 2/2] pwm: airoha: Add support for EN7581 SoC
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko
On Tue, Jun 24, 2025 at 12:11 AM Christian Marangi <[email protected]> wrote:
...
Quoted 8 lines
Quoted 8 lines
drivers/pwm/Kconfig config PWM_ADP5585 · line 60
Quoted 5 lines
drivers/pwm/Kconfig +4
-0+config PWM_AIROHA + tristate "Airoha PWM support" + depends on ARCH_AIROHA || COMPILE_TEST + depends on OFThere is nothing dependent on this. If you want to enable run-time, why not using this in conjunction with the
COMPILE_TEST?
Yes, I understand that. Maybe you should have dropped versioning of the series :-)
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ select
REGMAP_MMIO
...
Quoted 11 lines
Quoted 8 lines
drivers/pwm/pwm-airoha.c line 18
Quoted 5 lines
drivers/pwm/pwm-airoha.c new file +4
-0+#include <linux/bitfield.h> +#include <linux/bitops.h> +#include <linux/err.h> +#include <linux/gpio.h> Have you had a chance to read the top of that header file? No, just no. This header must not be used in the new code.
As you can see by the changelog this is very old code so I wasn't aware.
Understood.
Quoted 10 lines
Quoted 10 lines
drivers/pwm/pwm-airoha.c line 24
Quoted 7 lines
drivers/pwm/pwm-airoha.c new file +6
-0+#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/math64.h> +#include <linux/mfd/syscon.h> +#include <linux/module.h> +#include <linux/of.h> Nothing is used from this header. You actually missed
mod_devicetable.h.
Quoted 8 lines
Quoted 6 lines
drivers/pwm/pwm-airoha.c line 27
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+#include <linux/platform_device.h> +#include <linux/pwm.h> +#include <linux/regmap.h> Missing headers, such as
types.h.
Please, follow the IWYU principle.Aside from types do you have hint of other missing header?
Just above :-)
Quoted 1 line
Do you have a tool to identify the missing header?
Unfortunately no tool available, this is just by experience of reviewing code and doing some cleanups in include/ area.
The rule of thumb, for anythin you use, check which header provides that type or API. But OTOH don't be too pedantic about it, types.h, for example, covers a lot of basic kernel types and many compiler attributes and so on, no need to take care of those separately.
TL;DR: you should go through your code and check it.
...
Quoted 9 lines
Quoted 5 lines
Quoted 1 line
+ u64 initialized;
Is it bitmap? This looks really weird, at least a comment is a must to explain why 64-bit for the variable that suggests (by naming) only a single bit.
There could be 33 PWM channel so it doesn't fit a u32. This is why u64. I feel bitmap might be overkill for the task but if requested, I will change it.
Why? It has a shortcuts for unsigned long, so it should give you no difference on 64-bit compilation. 32-bit might suffer a bit, but if you curious you may check it. The ask here is only based on the variable naming and unclearness of how many bits are in use. Also bitmap will help to understand the code better.
For instance, here
DEFINE_BITMAP(initialized, 33); // or rather a definition for 33
will give immediate understanding how this is used and what are the limits. With u64 it;s unclear what you will do with a potential garbage in the upper bits, for example.
So, let's say I prefer to see bitmap types and APIs here based on the above examples.
...
Quoted 10 lines
Quoted 3 lines
This entire function reminds me of something from
util_macros.horbsearch.hor similar. Can you double check that you really can't utilise one of those?I checked and bsearch can't be used and and for
util_macrosthe closest can't be used. As explained in previous revision, it's not simply a matter of finding the closest value but it's about finding a value that is closest to theperiod_nsand only with that condition satisfied one closest to the duty. We can't mix them as search for the closest of both.
Perhaps adding a comment on top to summarize this, or did I miss it?
...
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
+ pc->buckets[bucket].used &= ~BIT_ULL(hwpwm);
Oh, why do you need 'used' to be also 64-bit?
Right, but I mean why does each of the buckets require a 64-bit value? Can it be handled in a single bitmap or so?
Or is it used like a cluster of the PWMs in one bucket? It feels like bitmap APIs can also improve the implementation here.
Quoted 1 line
In the extreme case, a bucket can be used by all 33 PWM channel.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 2 lines · Christian Marangi
There is currently a problem with the usage of rounddown MACRO with
rounddown() with
Quoted 1 line
u64 dividends. This cause compilation error on specific arch where
causes
Quoted 1 line
64bit division is done on 32bit system.
on the 32-bit
Quoted 1 line
To be more specific GCC try optimize the function and replace it with
to optimize
Quoted 1 line
__umoddi3but this is actually not compiled in the kernel.
__umoddi3()
Quoted 4 lines
Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'To better handle this, introduce a variant of rounddown MACRO,
rounddown(),
Quoted 1 line
rounddown_ullthat can be used exactly for this scenario.
rounddown_ull()
Quoted 1 line
rounddown_ullnew MACRO use thedo_divMACRO that do the heavy work of
The rounddown_ull() is a new macro that uses do_div() to do the heavy work of
Quoted 1 line
handling internally all the magic for 64bit division on 32bit (and
for the 64-bit divisions on the 32-bit platforms (and
Quoted 1 line
indirectly fix the compilation error).
...
Quoted 1 line
- Add this patch
Why are math64 APIs not usable here?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 21 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
There is currently a problem with the usage of rounddown MACRO with
rounddown()withQuoted 1 line
u64 dividends. This cause compilation error on specific arch where
causes
Quoted 1 line
64bit division is done on 32bit system.
on the 32-bit
Quoted 1 line
To be more specific GCC try optimize the function and replace it with
to optimize
Quoted 1 line
__umoddi3but this is actually not compiled in the kernel.
Quoted 8 lines
__umoddi3()Quoted 4 lines
Example:
pwm-airoha.c:(.text+0x8f8): undefined reference to `__umoddi3'To better handle this, introduce a variant of rounddown MACRO,
rounddown(),
For this and the other... Is it correct to use () for MACRO? I assume () should be used only for functions.
Quoted 13 lines
Quoted 2 lines
rounddown_ullthat can be used exactly for this scenario.
rounddown_ull()Quoted 1 line
rounddown_ullnew MACRO use thedo_divMACRO that do the heavy work ofThe
rounddown_ull()is a new macro that usesdo_div()to do the heavy work ofQuoted 1 line
handling internally all the magic for 64bit division on 32bit (and
for the 64-bit divisions on the 32-bit platforms (and
Quoted 8 lines
Quoted 1 line
indirectly fix the compilation error).
...
Quoted 1 line
- Add this patch
Why are math64 APIs not usable here?
There isn't a rounddown API for math64.
Signature · 1 line
Ansuel
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko
On Tue, Jun 24, 2025 at 12:11 AM Christian Marangi <[email protected]> wrote:
...
Quoted 4 lines
Quoted 1 line
rounddown(),For this and the other... Is it correct to use () for MACRO? I assume () should be used only for functions.
When it takes argument, yes. When it doesn't then you don't put it.
...
Quoted 1 line
Quoted 1 line
rounddown_ull()
Btw, I don't like name for this, it's better to be in math64 with the u64 or similar suffixes like it's used for div/mul variants.
Also add a roundup to make the API symmetrical (yes, it's okay that it has no users, it's a macro and doesn't consume memory at run-time).
...
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
- Add this patch
Why are math64 APIs not usable here?
There isn't a rounddown API for math64.
Then implementing it there (in math64.h) looks to me natural thingy that will implicitly suggest this.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko
On Tue, Jun 24, 2025 at 12:11 AM Christian Marangi <[email protected]> wrote:
...
Quoted 6 lines
Quoted 1 line
Quoted 1 line
rounddown_ull()Btw, I don't like name for this, it's better to be in math64 with the u64 or similar suffixes like it's used for div/mul variants.
Also add a roundup to make the API symmetrical (yes, it's okay that it has no users, it's a macro and doesn't consume memory at run-time).
Ha, there is already roundup_u64() in math64!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Christian Marangi
Quoted 1 line · Andy Shevchenko
On Tue, Jun 24, 2025 at 12:11 AM Christian Marangi <[email protected]> wrote:
...
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
rounddown_ull()Btw, I don't like name for this, it's better to be in math64 with the u64 or similar suffixes like it's used for div/mul variants.
Also add a roundup to make the API symmetrical (yes, it's okay that it has no users, it's a macro and doesn't consume memory at run-time).
Ha, there is already
roundup_u64()in math64!
Ha indeed! I was searching for roundup_64 and I missed the last tiny function in math64.h. My bad.
Signature · 1 line
Ansuel