pwm: airoha: Add support for EN7581 SoC
PATCH v23 1/1 pwm: airoha: Add support for EN7581 SoC
From: Benjamin Larsson <[email protected]>
Introduce driver for PWM module available on EN7581 SoC.
---
Changes v23:
- Fix typo in airoha_pwm_config where period and duty variables order was swapped
Changes v22:
- Drop do_div and use raw / and %
- Drop refcount.h include
Changes v21:
- Revert offset to u64 in airoha_pwm_apply_bucket_config (do_div require u64)
Changes v20:
- Add Review tag
- Drop duty normalization
- Drop mutex usage
- Use non-atomic bit OPs
- Use unsigned int for offset and bucket in airoha_pwm_apply_bucket_config
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 | 606 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 617 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 +606 -0
606 lines not on this page · raw message
Signature · 1 line
2.48.1
Uwe Kleine-König
maintainerHello Christian,
Quoted 7 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +7
-0+static u32 airoha_pwm_get_period_ticks_from_ns (u32 period_ns) +{ +return period_ns /AIROHA_PWM_PERIOD_TICK_NS ; +} + +static u32 airoha_pwm_get_duty_ticks_from_ns (u32 period_ns,u32 duty_ns) +{
drivers/pwm/pwm-airoha.c
Quoted 10 lines
drivers/pwm/pwm-airoha.c new file +8
-0+return mul_u64_u32_div (duty_ns,AIROHA_PWM_DUTY_FULL , period_ns); +} + +static int airoha_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +/* +* Period goes at 4ns step, normalize it to check if we can
4 ms or 4 ns?
drivers/pwm/pwm-airoha.c line 524
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+* share a generator. +*/ + period_ns =rounddown (period_ns,AIROHA_PWM_PERIOD_TICK_NS ); + +/* Convert ns to ticks */ + period_ticks =airoha_pwm_get_period_ticks_from_ns (period_ns);
Rounding down to the next multiple of 4ns isn't needed for airoha_pwm_get_period_ticks_from_ns() which is just a division by AIROHA_PWM_PERIOD_TICK_NS.
drivers/pwm/pwm-airoha.c line 525
Quoted 1 line
drivers/pwm/pwm-airoha.c new file +1
-0+ duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns);
As duty_ticks depends on the selected period_ticks, I think the bucket selection algorithm is still wrong.
Consider a request to implement
period_ns = 256 ms duty_ns = 128 ms
which at first correctly results in
period_ticks = 64 duty_ticks = 127
If however all buckets are used and we only find one with say 62 period ticks we get period_ns = 248 and with that duty_ticks should better be 131 and not 127.
drivers/pwm/pwm-airoha.c line 528
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+ +return airoha_pwm_config (pc, pwm, period_ticks, duty_ticks); +}
Best regards
Uwe
Quoted 14 lines · Uwe Kleine-König
Hello Christian,
Quoted 11 lines · Christian Marangi
drivers/pwm/pwm-airoha.c new file +10
-0+static u32 airoha_pwm_get_period_ticks_from_ns (u32 period_ns) +{ +return period_ns /AIROHA_PWM_PERIOD_TICK_NS ; +} + +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); +} +
Quoted 9 lines
drivers/pwm/pwm-airoha.c line 518
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +5
-0+static int airoha_pwm_apply (struct pwm_chip *chip,struct pwm_device *pwm, +const struct pwm_state *state) +{ +/* +* Period goes at 4ns step, normalize it to check if we can 4 ms or 4 ns?
4ms you are right
Quoted 11 lines
drivers/pwm/pwm-airoha.c line 524
Quoted 6 lines
drivers/pwm/pwm-airoha.c new file +6
-0+* share a generator. +*/ + period_ns =rounddown (period_ns,AIROHA_PWM_PERIOD_TICK_NS ); + +/* Convert ns to ticks */ + period_ticks =airoha_pwm_get_period_ticks_from_ns (period_ns);Rounding down to the next multiple of 4ns isn't needed for
airoha_pwm_get_period_ticks_from_ns()which is just a division byAIROHA_PWM_PERIOD_TICK_NS.
Ok will drop.
Quoted 10 lines
drivers/pwm/pwm-airoha.c line 525
Quoted 1 line
drivers/pwm/pwm-airoha.c new file +1
-0+ duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns);As
duty_ticksdepends on the selectedperiod_ticks, I think the bucket selection algorithm is still wrong.Consider a request to implement
period_ns = 256 ms duty_ns = 128 ms
Quoted 9 lines
which at first correctly results in
period_ticks = 64 duty_ticks = 127If however all buckets are used and we only find one with say 62 period ticks we get
period_ns= 248 and with thatduty_ticksshould better be 131 and not 127.
Hi Uwe,
sorry for checking this only now and maybe we need to catch this again.
Maybe we are getting confused here but itsn't this already handled by the upper condition?
/* 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.
*/
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;
}We first limit for a bucket that doesn't got over both period and duty and then we search for period and best duty. This should account for never exceeding a duty since both period and duty are precalculated for the current bucket and even if duty depends on period, again it's precalculated. Am I missing something?
Quoted 6 lines
drivers/pwm/pwm-airoha.c line 528
Quoted 3 lines
drivers/pwm/pwm-airoha.c new file +3
-0+ +return airoha_pwm_config (pc, pwm, period_ticks, duty_ticks); +}Best regards
Uwe
Signature · 1 line
Ansuel
Uwe Kleine-König
maintainerHello Christian,
Quoted 14 lines · Christian Marangi
Quoted 13 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 525
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+ duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns);As
duty_ticksdepends on the selectedperiod_ticks, I think the bucket selection algorithm is still wrong.Consider a request to implement
period_ns = 256 ms duty_ns = 128 mswhich at first correctly results in
Quoted 8 lines
Quoted 6 lines
period_ticks = 64 duty_ticks = 127If however all buckets are used and we only find one with say 62 period ticks we get
period_ns= 248 and with thatduty_ticksshould better be 131 and not 127.sorry for checking this only now and maybe we need to catch this again.
no need to be sorry here. Taking time for replies is fine for me.
Quoted 22 lines
Maybe we are getting confused here but itsn't this already handled by the upper condition?
/* 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. */ 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; }
Quoted 5 lines
We first limit for a bucket that doesn't got over both period and duty and then we search for period and best duty. This should account for never exceeding a duty since both period and duty are precalculated for the current bucket and even if duty depends on period, again it's precalculated. Am I missing something?
Let me describe the issue in more detail:
The period length is configured in the AIROHA_PWM_WAVE_GEN_CYCLE register in multiples of 4 ms. The duty length is configured in the AIROHA_PWM_GPIO_FLASH_PRD_HIGH register in multiples of $period_length/255.
So if you calcultate the number of multiples you need for duty_ns = 128 ms based on the assumption that period_ns = 256 ms the result becomes wrong when you are forced to switch to period_ns = 248.
So to implement a request for period = 256 ms (64 ticks) and duty_cycle = 128 ms (127.5 duty ticks) having the choice between the two buckets:
a) period_ticks = 62; duty_ticks = 127
(period = 248 ms, duty_cycle = 123.51372549019608 ms)
b) period_ticks = 62; duty_ticks = 131
(period = 248 ms, duty_cycle = 127.40392156862744 ms)b) is the better one despite 127 duty_ticks would be an exact match for period_ticks = 64. So the issue is that the "Ignore bucket with invalid configs" kicks out b). That's wrong because
bucket_duty_ticks > duty_ticks
doesn't imply
bucket_duty > duty
.
Best regards
Uwe
Quoted 8 lines · Christian Marangi
Quoted 5 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 518
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+* Period goes at 4ns step, normalize it to check if we can 4 ms or 4 ns?
4ms you are right
One small but important side note (to Uwe as well, however he seems follows what I am about to say). Recently I discovered a nice wrap-up [1] on the writing values with units. And I think we should try to follow it (at bare minimum to be consistent with chosen — Journalism vs. Scientific —
style).
[1]: https://poynton.ca/notes/units/
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 79 lines · Uwe Kleine-König
Hello Christian,
Quoted 14 lines · Christian Marangi
Quoted 13 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 525
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+ duty_ticks =airoha_pwm_get_duty_ticks_from_ns (period_ns, duty_ns);As
duty_ticksdepends on the selectedperiod_ticks, I think the bucket selection algorithm is still wrong.Consider a request to implement
period_ns = 256 ms duty_ns = 128 mswhich at first correctly results in
Quoted 8 lines
Quoted 6 lines
period_ticks = 64 duty_ticks = 127If however all buckets are used and we only find one with say 62 period ticks we get
period_ns= 248 and with thatduty_ticksshould better be 131 and not 127.sorry for checking this only now and maybe we need to catch this again.
no need to be sorry here. Taking time for replies is fine for me.
Quoted 22 lines
Maybe we are getting confused here but itsn't this already handled by the upper condition?
/* 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. */ 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; }Quoted 5 lines
We first limit for a bucket that doesn't got over both period and duty and then we search for period and best duty. This should account for never exceeding a duty since both period and duty are precalculated for the current bucket and even if duty depends on period, again it's precalculated. Am I missing something?
Let me describe the issue in more detail:
The period length is configured in the
AIROHA_PWM_WAVE_GEN_CYCLEregister in multiples of 4 ms. The duty length is configured in theAIROHA_PWM_GPIO_FLASH_PRD_HIGHregister in multiples of $period_length/255.So if you calcultate the number of multiples you need for
duty_ns= 128 ms based on the assumption thatperiod_ns= 256 ms the result becomes wrong when you are forced to switch toperiod_ns= 248.So to implement a request for period = 256 ms (64 ticks) and
duty_cycle= 128 ms (127.5 duty ticks) having the choice between the two buckets:a)
period_ticks= 62;duty_ticks= 127(period = 248 ms, duty_cycle = 123.51372549019608 ms) b) period_ticks = 62; duty_ticks = 131 (period = 248 ms, duty_cycle = 127.40392156862744 ms)b) is the better one despite 127
duty_tickswould be an exact match forperiod_ticks= 64. So the issue is that the "Ignore bucket with invalid configs" kicks out b). That's wrong because
Quoted 8 lines
bucket_duty_ticks>duty_ticksdoesn't imply
bucket_duty> duty.
Thanks for the quick feedback hope we can takle this quick so we can have this finally merged.
I changed the logic to this. What do you think? (I introduced an helper to calculate the ns from raw ticks)
duty_ns = airoha_pwm_get_duty_ns_from_ticks(period_ticks, duty_ticks);
...
/* Ignore bucket with invalid period */
if (bucket_period_ticks > period_ticks)
continue;
/*
* Search for a bucket closer to the requested period
* 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_ticks >= best_period_ticks) {
bucket_duty_ns = airoha_pwm_get_duty_ns_from_ticks(bucket_period_ticks,
bucket_duty_ticks);
/* Skip bucket that goes over the requested duty */
if (bucket_duty_ns > duty_ns)
continue;
if (bucket_duty_ns > best_duty_ns) {
best_period_ticks = bucket_period_ticks;
best_duty_ns = bucket_duty_ns;
best = i;
}
}We first search the period and then we calculate the duty in NS and calculate the duty for each bucket. Should comply with the fact that duty depends on period right?
Signature · 1 line
Ansuel
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Christian Marangi
Quoted 5 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 518
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+* Period goes at 4ns step, normalize it to check if we can 4 ms or 4 ns?
4ms you are right
Quoted 8 lines
One small but important side note (to Uwe as well, however he seems follows what I am about to say). Recently I discovered a nice wrap-up [1] on the writing values with units. And I think we should try to follow it (at bare minimum to be consistent with chosen — Journalism vs. Scientific —
style).
Just to be more precise, on comments we should use NUMBER SPACE UNIT? I think for variables it's problematic.
Signature · 1 line
Ansuel
Quoted 17 lines · Christian Marangi
Quoted 16 lines · Andy Shevchenko
Quoted 8 lines · Christian Marangi
Quoted 5 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 518
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+* Period goes at 4ns step, normalize it to check if we can 4 ms or 4 ns?
4ms you are right
One small but important side note (to Uwe as well, however he seems follows what I am about to say). Recently I discovered a nice wrap-up [1] on the writing values with units. And I think we should try to follow it (at bare minimum to be consistent with chosen — Journalism vs. Scientific —
style).
Quoted 4 lines
Quoted 1 line
Just to be more precise, on comments we should use NUMBER SPACE UNIT? I think for variables it's problematic.
Of course the note only about documentation and comments, not about the code :-) And yes, $NUMBER space $UNIT (personally I prefer the Scientific way).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Uwe Kleine-König
maintainerQuoted 10 lines · Andy Shevchenko
Quoted 8 lines · Christian Marangi
Quoted 5 lines · Uwe Kleine-König
drivers/pwm/pwm-airoha.c line 518
Quoted 1 line · Christian Marangi
drivers/pwm/pwm-airoha.c new file +1
-0+* Period goes at 4ns step, normalize it to check if we can 4 ms or 4 ns?
4ms you are right
Quoted 7 lines
One small but important side note (to Uwe as well, however he seems follows what I am about to say). Recently I discovered a nice wrap-up [1] on the writing values with units. And I think we should try to follow it (at bare minimum to be consistent with chosen — Journalism vs. Scientific —
style).
Looking through that, that matches my expectations, apart from:
Use big K for the multiplier (1024) common in computing
and
When applied to raw bits, bytes or pixels:
M (mega) denotes (1024 K); and
G (giga) denotes .
. In my bubble at least Ki, Mi, Gi, Ti etc. is usual for base-2 prefixes, see https://en.wikipedia.org/wiki/Binary_prefix for a reference which also has:
Prior to the 1999 IEC standard, some industry organizations, such as the Joint Electron Device Engineering Council (JEDEC), noted the common use of the terms kilobyte, megabyte, and gigabyte, and the corresponding symbols KB, MB, and GB in the binary sense, for use in storage capacity measurements. However, other computer industry sectors (such as magnetic storage) continued using those same terms and symbols with the decimal meaning. Since then, the major standards organizations have expressly disapproved the use of SI prefixes to denote binary multiples, and recommended or mandated the use of the IEC prefixes for that purpose, but the use of SI prefixes in this sense has persisted in some fields.
Best regards
Uwe