designware pwm driver updates
v9 cover designware pwm driver updates
This series is an update for the DesignWare PWM driver to add support for OF (and split the PCI bits out if aynone else wants them). This is mostly the same as the v8 series, but with code moved around and module-namespace added, plus review comments processed.
We no longer have access to the hardware to test any further modifications.
I've not added the tested-by tags as there have been alterations to this series.
Note, the dt binding seems to have already been applied.
The lengthy changelog:
v9:
- fix multi-line comment issue
- changed rounding in calc-rate code
- changed email addresses to codethink one
- fix spelling mistake(s)
- add clk_rate_exclusive_get() to lock clock rate for now
v8:
- updated reviewed tags
- fix module name for pci version
- fix config symbol bug in makefile
- remove pci compile-test (mostly not used for pci)
- push the compile-test into the platform/of driver
v7:
- fixup kconfig from previous pcie changes
- re-order kconfig to make dwc core be selected by PCI driver
- move clk variable to patch it is used in
v6:
- fix removal ordering of DWC_PERIOD_NS
v5:
- fixed kconfig string error
- merged pwm-nr into main of code
- split of code from pci code
- updated pwm-nr capping
- fix duplicate error reporting in of-code
- fix return in of-probe
- remove unecessary remove function as devm_ functions sort this
- fixed ordering of properties
- added missing reg item
- fixed missing split of the two clock sources.
- get bus clock in of code
v4:
- split pci and of into new modules
- fixup review comments
- fix typos in dt-bindings
v3:
- change the compatible name
- squash down pwm count patch
- fixup patch naming
v2:
- fix #pwm-cells count to be 3
- fix indetation
- merge the two clock patches
- add HAS_IOMEM as a config dependency
Ben Dooks (6):
pwm: dwc: split pci out of core driver
pwm: dwc: make timer clock configurable
pwm: dwc: add PWM bit unset in get_state call
pwm: dwc: use clock rate in hz to avoid rounding issues
pwm: dwc: round rate divisions up
pwm: dwc: add of/platform support
drivers/pwm/Kconfig | 24 ++++- drivers/pwm/Makefile | 2 + drivers/pwm/pwm-dwc-core.c | 197 ++++++++++++++++++++++++++++++++++++ drivers/pwm/pwm-dwc-of.c | 93 +++++++++++++++++ drivers/pwm/pwm-dwc.c | 198 +------------------------------------ drivers/pwm/pwm-dwc.h | 61 ++++++++++++ 6 files changed, 380 insertions(+), 195 deletions(-) create mode 100644 drivers/pwm/pwm-dwc-core.c create mode 100644 drivers/pwm/pwm-dwc-of.c create mode 100644 drivers/pwm/pwm-dwc.h
Signature · 1 line
2.40.1
PATCH v9 1/6 pwm: dwc: split pci out of core driver
Moving towards adding non-pci support for the driver, move the pci parts out of the core into their own module. This is partly due to the module_driver() code only being allowed once in a module and also to avoid a number of #ifdef if we build a single file in a system without pci support.
---
v9:
- change back to codethink email from sifive
v8:
- add module namespace
- remove compile-test for pci case, doesn't make sense
- fix makefile, missed config symbol changes
v7:
- re-order kconfig to make dwc core be selected by PCI driver
v6:
- put DWC_PERIOD_NS back to avoid bisect issues
v4:
- removed DWC_PERIOD_NS as not needed
--- drivers/pwm/Kconfig | 14 ++- drivers/pwm/Makefile | 1 + drivers/pwm/pwm-dwc-core.c | 176 +++++++++++++++++++++++++++++++++ drivers/pwm/pwm-dwc.c | 197 +------------------------------------ drivers/pwm/pwm-dwc.h | 60 +++++++++++ 5 files changed, 253 insertions(+), 195 deletions(-) create mode 100644 drivers/pwm/pwm-dwc-core.c create mode 100644 drivers/pwm/pwm-dwc.h
drivers/pwm/Kconfig +12 -2
@@ -186,9 +186,19 @@ config PWM_CROS_EC PWM driver for exposing a PWM attached to the ChromeOS Embedded Controller. +config PWM_DWC_CORE + tristate + depends on HAS_IOMEM + help + PWM driver for Synopsys DWC PWM Controller. + + To compile this driver as a module, build the dependecies as + modules, this will be called pwm-dwc-core. + config PWM_DWC - tristate "DesignWare PWM Controller" - depends on PCI + tristate "DesignWare PWM Controller (PCI bus)" + depends on HAS_IOMEM && PCI + select PWM_DWC_CORE help PWM driver for Synopsys DWC PWM Controller attached to a PCI bus.
drivers/pwm/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_PWM_CLK) += pwm-clk.o obj-$(CONFIG_PWM_CLPS711X) += pwm-clps711x.o obj-$(CONFIG_PWM_CRC) += pwm-crc.o obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o +obj-$(CONFIG_PWM_DWC_CORE) += pwm-dwc-core.o obj-$(CONFIG_PWM_DWC) += pwm-dwc.o obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o
drivers/pwm/pwm-dwc-core.c new file +176 -0
176 lines not on this page · raw message
drivers/pwm/pwm-dwc.c +4 -193
214 lines not on this page · raw message
drivers/pwm/pwm-dwc.h new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.40.1
Re: [PATCH v9 1/6] pwm: dwc: split pci out of core driver
Hello,
Quoted 7 lines · Ben Dooks
Moving towards adding non-pci support for the driver, move the pci parts out of the core into their own module. This is partly due to the
module_driver()code only being allowed once in a module and also to avoid a number of #ifdef if we build a single file in a system without pci support.Signed-off-by Ben Dooks <[email protected]>
Great you continue here even though your commercial interest already ended!
This series conflicts with my lifetime series[1], but I guess we can sort that one when at least one of these two series is applied.
[1] https://lore.kernel.org/linux-pwm/[email protected]/
Quoted 14 lines
[...]
drivers/pwm/pwm-dwc.c +1
-1@@ -1,6 +1,6 @@// SPDX-License-Identifier: GPL-2.0 /* -* DesignWare PWM Controller driver +* DesignWare PWM Controller driver (PCI part) * * Copyright (C) 2018-2020 Intel Corporation *
drivers/pwm/pwm-dwc.c line 17
Quoted 5 lines
drivers/pwm/pwm-dwc.c +2
-0@@ -13,6 +13,8 @@* periods are one or more input clock periods long. */ +#define DEFAULT_MOUDLE_NAMESPACE dwc_pwm +
There is no exported symbol in this driver, so this #define is unused.
drivers/pwm/pwm-dwc.c line 20
Quoted 3 lines
drivers/pwm/pwm-dwc.c +0
-0#include <linux/bitops.h> #include <linux/export.h> #include <linux/kernel.h>
Otherwise looks fine:
(with or without dropping DEFAULT_MOUDLE_NAMESPACE from pwm-dwc.c)
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH v9 2/6 pwm: dwc: make timer clock configurable
In reply to Ben DooksAdd a configurable clock base rate for the pwm as when being built for non-PCI the block may be sourced from an internal clock.
---
v9:
- moved email to codethink from sifive
v8:
- add reviewed by, fixed issue with previous renames.
v7:
- remove the "struct clk *" clk field from dwc_pwm_ctx, not used here,
v6:
- removed DWC_CLK_PERIOD_NS as it is now not needed
v4:
- moved earlier before the of changes to make the of changes one patch
v2:
- removed the ifdef and merged the other clock patch in here
--- drivers/pwm/pwm-dwc-core.c | 9 +++++---- drivers/pwm/pwm-dwc.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/pwm/pwm-dwc-core.c +5 -4
@@ -49,13 +49,13 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* periods and check are the result within HW limits between 1 and * 2^32 periods. */ - tmp =DIV_ROUND_CLOSEST_ULL (state->duty_cycle,DWC_CLK_PERIOD_NS ); + tmp =DIV_ROUND_CLOSEST_ULL (state->duty_cycle, dwc->clk_ns);if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; tmp =DIV_ROUND_CLOSEST_ULL (state->period - state->duty_cycle, -DWC_CLK_PERIOD_NS ); + dwc->clk_ns);if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;
@@ -130,12 +130,12 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, duty =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); duty +=1 ; - duty *=DWC_CLK_PERIOD_NS ; + duty *= dwc->clk_ns; state->duty_cycle = duty; period =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); period +=1 ; - period *=DWC_CLK_PERIOD_NS ; + period *= dwc->clk_ns; period += duty; state->period = period;
@@ -160,6 +160,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)if (!dwc)return NULL ; + dwc->clk_ns =10 ; dwc->chip.dev = dev; dwc->chip.ops = &dwc_pwm_ops; dwc->chip.npwm =DWC_TIMERS_TOTAL ;
drivers/pwm/pwm-dwc.h +1 -1
@@ -24,7 +24,6 @@ MODULE_IMPORT_NS(dwc_pwm);#define DWC_TIMERS_COMP_VERSION 0xac#define DWC_TIMERS_TOTAL 8 -#define DWC_CLK_PERIOD_NS 10/* Timer Control Register */ #define DWC_TIM_CTRL_EN BIT(0)
@@ -43,6 +42,7 @@ struct dwc_pwm_ctx {struct dwc_pwm {struct pwm_chip chip;void __iomem *base; +unsigned int clk_ns;struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL ]; };#define to_dwc_pwm (p) (container_of((p), struct dwc_pwm, chip))
Signature · 1 line
2.40.1
PATCH v9 3/6 pwm: dwc: add PWM bit unset in get_state call
In reply to Ben DooksIf we are not in PWM mode, then the output is technically a 50% output based on a single timer instead of the high-low based on the two counters. Add a check for the PWM mode in dwc_pwm_get_state() and if DWC_TIM_CTRL_PWM is not set, then return a 50% cycle.
This may only be an issue on initialisation, as the rest of the code currently assumes we're always going to have the extended PWM mode using two counters.
---
v9:
- fixed multi-line comment
- put authour back to codethink email from sifive
v8:
- fixed rename issues
v4:
- fixed review comment on mulit-line calculations
--- drivers/pwm/pwm-dwc-core.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/pwm/pwm-dwc-core.c +19 -11
@@ -122,24 +122,32 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, {struct dwc_pwm *dwc =to_dwc_pwm (chip);u64 duty, period; +u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); - state->enabled = !!(dwc_pwm_readl (dwc, -DWC_TIM_CTRL (pwm->hwpwm)) &DWC_TIM_CTRL_EN ); + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); + ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); + ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - duty =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); - duty +=1 ; - duty *= dwc->clk_ns; - state->duty_cycle = duty; + state->enabled = !!(ctrl &DWC_TIM_CTRL_EN ); - period =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - period +=1 ; - period *= dwc->clk_ns; - period += duty; - state->period = period; +/* +* If we're not in PWM, technically the output is a 50-50 +* based on the timer load-count only. +*/ +if (ctrl &DWC_TIM_CTRL_PWM ) { + duty = (ld +1 ) * dwc->clk_ns; + period = (ld2 +1 ) * dwc->clk_ns; + period += duty; + }else { + duty = (ld +1 ) * dwc->clk_ns; + period = duty *2 ; + } state->polarity =PWM_POLARITY_INVERSED ; + state->period = period; + state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);
Signature · 1 line
2.40.1
Re: [PATCH v9 3/6] pwm: dwc: add PWM bit unset in get_state call
Hello,
[dropping William Salmon and Jude Onyenegecha from Cc: as in the other mails before]
I'd change the Subject to:
pwm: dwc: Support DWC_TIM_CTRL_PWM unset in .get_state()
Quoted 59 lines · Ben Dooks
If we are not in PWM mode, then the output is technically a 50% output based on a single timer instead of the high-low based on the two counters. Add a check for the PWM mode in
dwc_pwm_get_state()and ifDWC_TIM_CTRL_PWMis not set, then return a 50% cycle.This may only be an issue on initialisation, as the rest of the code currently assumes we're always going to have the extended PWM mode using two counters.
Signed-off-by Ben Dooks <[email protected]>---
v9:
- fixed multi-line comment
- put authour back to codethink email from sifive
v8:
- fixed rename issues
v4:
- fixed review comment on mulit-line calculations--- drivers/pwm/pwm-dwc-core.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)drivers/pwm/pwm-dwc-core.c +13
-11@@ -122,24 +122,32 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, {struct dwc_pwm *dwc =to_dwc_pwm (chip);u64 duty, period; +u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); - state->enabled = !!(dwc_pwm_readl (dwc, -DWC_TIM_CTRL (pwm->hwpwm)) &DWC_TIM_CTRL_EN ); + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); + ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); + ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - duty =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); - duty +=1 ; - duty *= dwc->clk_ns; - state->duty_cycle = duty; + state->enabled = !!(ctrl &DWC_TIM_CTRL_EN ); - period =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - period +=1 ; - period *= dwc->clk_ns; - period += duty; - state->period = period; +/* +* If we're not in PWM, technically the output is a 50-50 +* based on the timer load-count only. +*/ +if (ctrl &DWC_TIM_CTRL_PWM ) { + duty = (ld +1 ) * dwc->clk_ns; + period = (ld2 +1 ) * dwc->clk_ns; + period += duty;
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 152
Quoted 10 lines
drivers/pwm/pwm-dwc-core.c +6
-0+ }else { + duty = (ld +1 ) * dwc->clk_ns; + period = duty *2 ; + } state->polarity =PWM_POLARITY_INVERSED ; + state->period = period; + state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);
The change looks right,
Do you intend to address the review feedback for the other patches in this series? It would be sad if you efforts didn't result in these improvements getting in.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v9 3/6] pwm: dwc: add PWM bit unset in get_state call
Quoted 82 lines · Uwe Kleine-König
Hello,
[dropping William Salmon and Jude Onyenegecha from Cc: as in the other mails before]
I'd change the Subject to:
pwm: dwc: Support
DWC_TIM_CTRL_PWMunset in .get_state()Quoted 59 lines · Ben Dooks
If we are not in PWM mode, then the output is technically a 50% output based on a single timer instead of the high-low based on the two counters. Add a check for the PWM mode in
dwc_pwm_get_state()and ifDWC_TIM_CTRL_PWMis not set, then return a 50% cycle.This may only be an issue on initialisation, as the rest of the code currently assumes we're always going to have the extended PWM mode using two counters.
Signed-off-by Ben Dooks <[email protected]>---
v9:
- fixed multi-line comment
- put authour back to codethink email from sifive
v8:
- fixed rename issues
v4:
- fixed review comment on mulit-line calculations--- drivers/pwm/pwm-dwc-core.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)drivers/pwm/pwm-dwc-core.c +13
-11@@ -122,24 +122,32 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, {struct dwc_pwm *dwc =to_dwc_pwm (chip);u64 duty, period; +u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); - state->enabled = !!(dwc_pwm_readl (dwc, -DWC_TIM_CTRL (pwm->hwpwm)) &DWC_TIM_CTRL_EN ); + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); + ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); + ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - duty =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); - duty +=1 ; - duty *= dwc->clk_ns; - state->duty_cycle = duty; + state->enabled = !!(ctrl &DWC_TIM_CTRL_EN ); - period =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm)); - period +=1 ; - period *= dwc->clk_ns; - period += duty; - state->period = period; +/* +* If we're not in PWM, technically the output is a 50-50 +* based on the timer load-count only. +*/ +if (ctrl &DWC_TIM_CTRL_PWM ) { + duty = (ld +1 ) * dwc->clk_ns; + period = (ld2 +1 ) * dwc->clk_ns; + period += duty;drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 152
Quoted 10 lines
drivers/pwm/pwm-dwc-core.c +6
-0+ }else { + duty = (ld +1 ) * dwc->clk_ns; + period = duty *2 ; + } state->polarity =PWM_POLARITY_INVERSED ; + state->period = period; + state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);The change looks right,
Quoted 5 lines
Reviewed-by Uwe Kleine-König <[email protected]>Do you intend to address the review feedback for the other patches in this series? It would be sad if you efforts didn't result in these improvements getting in.
I'm going to try and get through the review comments this week, I've been ill and then on leave so not had any time to look at this.
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html
PATCH v9 4/6 pwm: dwc: use clock rate in hz to avoid rounding issues
In reply to Ben DooksAs noted, the clock-rate when not a nice multiple of ns is probably going to end up with inacurate calculations, as well as on a non pci system the rate may change (although we've not put a clock rate change notifier in this code yet) so we also add some quick checks of the rate when we do any calculations with it.
Signed-off-by; Ben Dooks <[email protected]>
---
v9:
- fixed commit spelling
- changed to use codethink email instead of sifive
v8:
- fixup post rename
- move to earlier in series
--- drivers/pwm/pwm-dwc-core.c | 24 +++++++++++++++--------- drivers/pwm/pwm-dwc.h | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-)
drivers/pwm/pwm-dwc-core.c +15 -9
@@ -49,13 +49,14 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* periods and check are the result within HW limits between 1 and * 2^32 periods. */ - tmp =DIV_ROUND_CLOSEST_ULL (state->duty_cycle, dwc->clk_ns); + tmp = state->duty_cycle * dwc->clk_rate; + tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; - tmp =DIV_ROUND_CLOSEST_ULL (state->period - state->duty_cycle, - dwc->clk_ns); + tmp = (state->period - state->duty_cycle) * dwc->clk_rate; + tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;
@@ -121,11 +122,14 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) {struct dwc_pwm *dwc =to_dwc_pwm (chip); +unsigned long clk_rate;u64 duty, period;u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); + clk_rate = dwc->clk_rate; + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm));
@@ -137,17 +141,19 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,* based on the timer load-count only. */ if (ctrl &DWC_TIM_CTRL_PWM ) { - duty = (ld +1 ) * dwc->clk_ns; - period = (ld2 +1 ) * dwc->clk_ns; + duty = ld +1 ; + period = ld2 +1 ; period += duty; }else { - duty = (ld +1 ) * dwc->clk_ns; + duty = ld +1 ; period = duty *2 ; } + duty *=NSEC_PER_SEC ; + period *=NSEC_PER_SEC ; + state->period =DIV_ROUND_CLOSEST_ULL (period, clk_rate); + state->duty_cycle =DIV_ROUND_CLOSEST_ULL (duty, clk_rate); state->polarity =PWM_POLARITY_INVERSED ; - state->period = period; - state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);
@@ -168,7 +174,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)if (!dwc)return NULL ; - dwc->clk_ns =10 ; + dwc->clk_rate =NSEC_PER_SEC /10 ; dwc->chip.dev = dev; dwc->chip.ops = &dwc_pwm_ops; dwc->chip.npwm =DWC_TIMERS_TOTAL ;
drivers/pwm/pwm-dwc.h +1 -1
@@ -42,7 +42,7 @@ struct dwc_pwm_ctx {struct dwc_pwm {struct pwm_chip chip;void __iomem *base; -unsigned int clk_ns; +unsigned long clk_rate;struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL ]; };#define to_dwc_pwm (p) (container_of((p), struct dwc_pwm, chip))
Signature · 1 line
2.40.1
Re: [PATCH v9 4/6] pwm: dwc: use clock rate in hz to avoid rounding issues
Hello,
[Dropped William Salmon and Jude Onyenegecha from the list of recipents, their email addresses don't seem to work any more.]
Quoted 25 lines · Ben Dooks
As noted, the clock-rate when not a nice multiple of ns is probably going to end up with inacurate calculations, as well as on a non pci system the rate may change (although we've not put a clock rate change notifier in this code yet) so we also add some quick checks of the rate when we do any calculations with it.
Signed-off-by; Ben Dooks <[email protected]>
Reported-by Uwe Kleine-König <[email protected]>---
v9:
- fixed commit spelling
- changed to use codethink email instead of sifive
v8:
- fixup post rename
- move to earlier in series--- drivers/pwm/pwm-dwc-core.c | 24 +++++++++++++++--------- drivers/pwm/pwm-dwc.h | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-)drivers/pwm/pwm-dwc-core.c +0
-0
drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 52
Quoted 5 lines
drivers/pwm/pwm-dwc-core.c +1
-1@@ -49,13 +49,14 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* periods and check are the result within HW limits between 1 and * 2^32 periods. */ - tmp =DIV_ROUND_CLOSEST_ULL (state->duty_cycle, dwc->clk_ns); + tmp = state->duty_cycle * dwc->clk_rate;
This might overflow. You can prevent this by asserting that clk_rate is <= NSEC_PER_SEC and using mul_u64_u64_div_u64.
Quoted 34 lines
drivers/pwm/pwm-dwc-core.c +7
-4+ tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; - tmp =DIV_ROUND_CLOSEST_ULL (state->period - state->duty_cycle, - dwc->clk_ns); + tmp = (state->period - state->duty_cycle) * dwc->clk_rate; + tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;@@ -121,11 +122,14 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) {struct dwc_pwm *dwc =to_dwc_pwm (chip); +unsigned long clk_rate;u64 duty, period;u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); + clk_rate = dwc->clk_rate; + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm));@@ -137,17 +141,19 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,* based on the timer load-count only. */ if (ctrl &DWC_TIM_CTRL_PWM ) { - duty = (ld +1 ) * dwc->clk_ns; - period = (ld2 +1 ) * dwc->clk_ns; + duty = ld +1 ;
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 153
Quoted 10 lines
drivers/pwm/pwm-dwc-core.c +4
-1+ period = ld2 +1 ; period += duty; }else { - duty = (ld +1 ) * dwc->clk_ns; + duty = ld +1 ; period = duty *2 ; } + duty *=NSEC_PER_SEC ; + period *=NSEC_PER_SEC ;
A comment that/why this cannot overflow would be nice. (I didn't check, maybe it can?)
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 155
Quoted 2 lines
drivers/pwm/pwm-dwc-core.c +2
-0+ state->period =DIV_ROUND_CLOSEST_ULL (period, clk_rate); + state->duty_cycle =DIV_ROUND_CLOSEST_ULL (duty, clk_rate);
Without having thought deeply about this, I think you need to round up here. Hmm, but given that .apply() uses round-closest, too, this needs to be addressed separately.
(The ugly thing about round-closest is that .apply(mypwm, .get_state(mypwm)) isn't idempotent in general. Consider a PWM that can implement period = 41.7ns and period = 42.4 ns. If it's configured with 42.4, .get_state will return period = 42. Reapplying this will configure for 41.7ns. This won't happen with the PCI clkrate, but it might in the of case. Another reason to use rounding-down in .apply is that mul_u64_u64_div_u64 doesn't have a round-nearest variant.)
Quoted 20 lines
drivers/pwm/pwm-dwc-core.c +1
-3state->polarity =PWM_POLARITY_INVERSED ; - state->period = period; - state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);@@ -168,7 +174,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)if (!dwc)return NULL ; - dwc->clk_ns =10 ; + dwc->clk_rate =NSEC_PER_SEC /10 ; dwc->chip.dev = dev; dwc->chip.ops = &dwc_pwm_ops; dwc->chip.npwm =DWC_TIMERS_TOTAL ;drivers/pwm/pwm-dwc.h +0
-0
drivers/pwm/pwm-dwc.h struct dwc_pwm_ctx { · line 45
Quoted 5 lines
drivers/pwm/pwm-dwc.h +1
-1@@ -42,7 +42,7 @@ struct dwc_pwm_ctx {struct dwc_pwm {struct pwm_chip chip;void __iomem *base; -unsigned int clk_ns; +unsigned long clk_rate;
Given that clk_ns was only introduced in patch #2 I think it would be cleaner to squash these two patches together.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Re: [PATCH v9 4/6] pwm: dwc: use clock rate in hz to avoid rounding issues
Quoted 82 lines · Uwe Kleine-König
Hello,
[Dropped William Salmon and Jude Onyenegecha from the list of recipents, their email addresses don't seem to work any more.]
Quoted 25 lines · Ben Dooks
As noted, the clock-rate when not a nice multiple of ns is probably going to end up with inacurate calculations, as well as on a non pci system the rate may change (although we've not put a clock rate change notifier in this code yet) so we also add some quick checks of the rate when we do any calculations with it.
Signed-off-by; Ben Dooks <[email protected]>
Reported-by Uwe Kleine-König <[email protected]>---
v9:
- fixed commit spelling
- changed to use codethink email instead of sifive
v8:
- fixup post rename
- move to earlier in series--- drivers/pwm/pwm-dwc-core.c | 24 +++++++++++++++--------- drivers/pwm/pwm-dwc.h | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-)drivers/pwm/pwm-dwc-core.c +0
-0drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 52
Quoted 5 lines
drivers/pwm/pwm-dwc-core.c +1
-1@@ -49,13 +49,14 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* periods and check are the result within HW limits between 1 and * 2^32 periods. */ - tmp =DIV_ROUND_CLOSEST_ULL (state->duty_cycle, dwc->clk_ns); + tmp = state->duty_cycle * dwc->clk_rate;This might overflow. You can prevent this by asserting that
clk_rateis <=NSEC_PER_SECand usingmul_u64_u64_div_u64.Quoted 42 lines
drivers/pwm/pwm-dwc-core.c +9
-5+ tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; - tmp =DIV_ROUND_CLOSEST_ULL (state->period - state->duty_cycle, - dwc->clk_ns); + tmp = (state->period - state->duty_cycle) * dwc->clk_rate; + tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;@@ -121,11 +122,14 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state) {struct dwc_pwm *dwc =to_dwc_pwm (chip); +unsigned long clk_rate;u64 duty, period;u32 ctrl, ld, ld2;pm_runtime_get_sync (chip->dev); + clk_rate = dwc->clk_rate; + ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm)); ld =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT (pwm->hwpwm)); ld2 =dwc_pwm_readl (dwc,DWC_TIM_LD_CNT2 (pwm->hwpwm));@@ -137,17 +141,19 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,* based on the timer load-count only. */ if (ctrl &DWC_TIM_CTRL_PWM ) { - duty = (ld +1 ) * dwc->clk_ns; - period = (ld2 +1 ) * dwc->clk_ns; + duty = ld +1 ; + period = ld2 +1 ; period += duty; }else { - duty = (ld +1 ) * dwc->clk_ns; + duty = ld +1 ; period = duty *2 ; }
Quoted 5 lines
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 153
Quoted 2 lines
drivers/pwm/pwm-dwc-core.c +2
-0+ duty *=NSEC_PER_SEC ; + period *=NSEC_PER_SEC ;A comment that/why this cannot overflow would be nice. (I didn't check, maybe it can?)
I /think/ that as long as NSEC_PER_SEC 2^32 then this shouldn't overflow.
Quoted 36 lines
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 155
Quoted 3 lines
drivers/pwm/pwm-dwc-core.c +2
-0+ state->period =DIV_ROUND_CLOSEST_ULL (period, clk_rate); + state->duty_cycle =DIV_ROUND_CLOSEST_ULL (duty, clk_rate);Without having thought deeply about this, I think you need to round up here. Hmm, but given that .apply() uses round-closest, too, this needs to be addressed separately.
(The ugly thing about round-closest is that .apply(mypwm, .get_state(mypwm)) isn't idempotent in general. Consider a PWM that can implement period = 41.7ns and period = 42.4 ns. If it's configured with 42.4, .get_state will return period = 42. Reapplying this will configure for 41.7ns. This won't happen with the PCI clkrate, but it might in the of case. Another reason to use rounding-down in .apply is that
mul_u64_u64_div_u64doesn't have a round-nearest variant.)Quoted 20 lines
drivers/pwm/pwm-dwc-core.c +1
-3state->polarity =PWM_POLARITY_INVERSED ; - state->period = period; - state->duty_cycle = duty;pm_runtime_put_sync (chip->dev);@@ -168,7 +174,7 @@ struct dwc_pwm *dwc_pwm_alloc(struct device *dev)if (!dwc)return NULL ; - dwc->clk_ns =10 ; + dwc->clk_rate =NSEC_PER_SEC /10 ; dwc->chip.dev = dev; dwc->chip.ops = &dwc_pwm_ops; dwc->chip.npwm =DWC_TIMERS_TOTAL ;drivers/pwm/pwm-dwc.h +0
-0@@ -42,7 +42,7 @@ struct dwc_pwm_ctx {
Quoted 8 lines
drivers/pwm/pwm-dwc.h struct dwc_pwm_ctx { · line 45
Quoted 5 lines
drivers/pwm/pwm-dwc.h +1
-1struct dwc_pwm {struct pwm_chip chip;void __iomem *base; -unsigned int clk_ns; +unsigned long clk_rate;Given that
clk_nswas only introduced in patch #2 I think it would be cleaner to squash these two patches together.
I'll have a look at how much work re-ordering the patches would be.
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html
Re: [PATCH v9 4/6] pwm: dwc: use clock rate in hz to avoid rounding issues
Hello Ben,
Quoted 9 lines · Ben Dooks
Quoted 5 lines · Uwe Kleine-König
drivers/pwm/pwm-dwc-core.c static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, · line 153
Quoted 2 lines
drivers/pwm/pwm-dwc-core.c +2
-0+ duty *=NSEC_PER_SEC ; + period *=NSEC_PER_SEC ;A comment that/why this cannot overflow would be nice. (I didn't check, maybe it can?)
I /think/ that as long as
NSEC_PER_SEC2^32 then this shouldn't overflow.
I guess NSEC_PER_SEC won't change in the near future :-)
double checking and writing the result down in a comment would be appreciated.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH v9 5/6 pwm: dwc: round rate divisions up
In reply to Ben DooksAs suggested, round up the counter variables to ensure we always produce a longer period calculation.
--- drivers/pwm/pwm-dwc-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pwm/pwm-dwc-core.c +2 -2
@@ -50,13 +50,13 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* 2^32 periods. */ tmp = state->duty_cycle * dwc->clk_rate; - tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC ); + tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; tmp = (state->period - state->duty_cycle) * dwc->clk_rate; - tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC ); + tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;
Signature · 1 line
2.40.1
Re: [PATCH v9 5/6] pwm: dwc: round rate divisions up
Hello,
[again dropped William Salmon and Jude Onyenegecha from Cc:]
Quoted 19 lines · Ben Dooks
As suggested, round up the counter variables to ensure we always produce a longer period calculation.
Reported-by Uwe Kleine-König <[email protected]>Signed-off-by Ben Dooks <[email protected]>--- drivers/pwm/pwm-dwc-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/pwm/pwm-dwc-core.c +1
-1@@ -50,13 +50,13 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,* 2^32 periods. */ tmp = state->duty_cycle * dwc->clk_rate; - tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC ); + tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );
drivers/pwm/pwm-dwc-core.c static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc, · line 62
Quoted 10 lines
drivers/pwm/pwm-dwc-core.c +1
-1if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; low = tmp -1 ; tmp = (state->period - state->duty_cycle) * dwc->clk_rate; - tmp =DIV_ROUND_CLOSEST_ULL (tmp,NSEC_PER_SEC ); + tmp =DIV_ROUND_UP_ULL (tmp,NSEC_PER_SEC );if (tmp <1 || tmp > (1ULL <<32 ))return -ERANGE ; high = tmp -1 ;
Ah, I asked for that in the reply I just sent out to patch #4. Maybe move this before the change from #4?! I think .get_state needs to be adapted accoringly (to round up).
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
PATCH v9 6/6 pwm: dwc: add of/platform support
In reply to Ben DooksThe dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
---
v9:
- add clk_rate_exclusive_get() to lock clock rate for now
- fix email address from sifive to codethink
- fix module namespace
v8:
- add compile test for of-case
- add module namespace
- move later in the series
v7:
- fixup kconfig from previous pcie changes
v5:
- fix missing " in kconfig
- remove .remove method, devm already sorts this.
- merge pwm-number code
- split the of code out of the core
- get bus clock
v4:
- moved the compile test code earlier
- fixed review comments
- used NS_PER_SEC
- use devm_clk_get_enabled
- ensure we get the bus clock
v3:
- changed compatible name
--- drivers/pwm/Kconfig | 10 ++++ drivers/pwm/Makefile | 1 + drivers/pwm/pwm-dwc-core.c | 6 +++ drivers/pwm/pwm-dwc-of.c | 93 ++++++++++++++++++++++++++++++++++++++ drivers/pwm/pwm-dwc.c | 1 + drivers/pwm/pwm-dwc.h | 1 + 6 files changed, 112 insertions(+) create mode 100644 drivers/pwm/pwm-dwc-of.c
drivers/pwm/Kconfig +10 -0
@@ -205,6 +205,16 @@ config PWM_DWC To compile this driver as a module, choose M here: the module will be called pwm-dwc. +config PWM_DWC_OF + tristate "DesignWare PWM Controller (OF bus)" + depends on HAS_IOMEM && (OF || COMPILE_TEST) + select PWM_DWC_CORE + help + PWM driver for Synopsys DWC PWM Controller on an OF bus. + + To compile this driver as a module, choose M here: the module + will be called pwm-dwc-of. + config PWM_EP93XX tristate "Cirrus Logic EP93xx PWM support" depends on ARCH_EP93XX || COMPILE_TEST
drivers/pwm/Makefile +1 -0
@@ -17,6 +17,7 @@ obj-$(CONFIG_PWM_CRC) += pwm-crc.o obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o obj-$(CONFIG_PWM_DWC_CORE) += pwm-dwc-core.o obj-$(CONFIG_PWM_DWC) += pwm-dwc.o +obj-$(CONFIG_PWM_DWC_OF) += pwm-dwc-of.o obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o obj-$(CONFIG_PWM_HIBVT) += pwm-hibvt.o
drivers/pwm/pwm-dwc-core.c +6 -0
@@ -16,6 +16,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/pci.h> +#include <linux/clk.h> #include <linux/pm_runtime.h> #include <linux/pwm.h>
@@ -44,6 +45,9 @@ static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,u32 high;u32 low; +if (dwc->clk) + dwc->clk_rate =clk_get_rate (dwc->clk); +/* * Calculate width of low and high period in terms of input clock * periods and check are the result within HW limits between 1 and
@@ -128,6 +132,8 @@ static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,pm_runtime_get_sync (chip->dev); +if (dwc->clk) + dwc->clk_rate =clk_get_rate (dwc->clk); clk_rate = dwc->clk_rate; ctrl =dwc_pwm_readl (dwc,DWC_TIM_CTRL (pwm->hwpwm));
drivers/pwm/pwm-dwc-of.c new file +93 -0
93 lines not on this page · raw message
drivers/pwm/pwm-dwc.c +1 -0
@@ -20,6 +20,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/pci.h> +#include <linux/clk.h> #include <linux/pm_runtime.h> #include <linux/pwm.h>
drivers/pwm/pwm-dwc.h +1 -0
@@ -42,6 +42,7 @@ struct dwc_pwm_ctx {struct dwc_pwm {struct pwm_chip chip;void __iomem *base; +struct clk *clk;unsigned long clk_rate;struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL ]; };
Signature · 1 line
2.40.1
Re: [PATCH v9 6/6] pwm: dwc: add of/platform support
Quoted 3 lines
The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
Hi Ben,
We're currently working on a platform driver for the DW_apb_timers PWM controller used in our EIC7700 SoC. We noticed that you submitted a patch for a DW PWM platform controller back in 2023, and we would like to kindly ask about its current status. Do you have any plans to get it merged into mainline?
Regards,
Xuyang Dong
Re: [PATCH v9 6/6] pwm: dwc: add of/platform support
Quoted 11 lines · On 10/11/2025 09:05, [email protected] wrote:
Quoted 3 lines
The dwc pwm controller can be used in non-PCI systems, so allow either platform or OF based probing.
Hi Ben,
We're currently working on a platform driver for the
DW_apb_timersPWM controller used in our EIC7700 SoC. We noticed that you submitted a patch for a DW PWM platform controller back in 2023, and we would like to kindly ask about its current status. Do you have any plans to get it merged into mainline?
Hi, I think we got a lot of the code re-ordering in but never finished as the project came to an end and our customer decided never to ship any actual silicon.
I am not sure how well our patches will apply now, but could have a quick look at this to see how much is left to do.
Unfortunately we had to delete everything at the end of the project so I do not have any sort of test setup.
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html