pwm: Fix various bugs reported by Sashiko
Subsystems (3)
v2 cover pwm: Fix various bugs reported by Sashiko
These issues were reported by Sashiko.
None of these changes have been tested on physical hardware.
v1 => v2:
- Drop tegra (superseded by 9ad5602d4943) and loongson / axi-pwmgen (already applied)
- mxs: Check pwm->state.enabled directly instead of calling the consumer helper pwm_is_enabled() (Uwe)
- fsl-ftm: Move ipg_clk enablement after the channel loop and document why it is needed around regcache_sync() (Uwe)
- omap-dmtimer: Propagate negative get_pwm_status() error codes out of pwm_omap_dmtimer_apply() (Uwe)
- tiehrpwm: Clarify runtime PM state before pm_runtime_enable() in the commit description and match teardown ordering in ehrpwm_pwm_remove() (Uwe)
- img: Clear autosuspend in img_pwm_remove() and document why driver data remains valid for runtime PM callbacks after pwmchip_remove() (Uwe)
v1: https://lore.kernel.org/all/[email protected]/
Lee Jones (5):
pwm: mxs: Return early when disabling and fix error-path clock leak
pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply()
pwm: tiehrpwm: Enable runtime PM before registering PWM chip
pwm: img: Unregister PWM chip before disabling runtime PM and clocks
drivers/pwm/pwm-fsl-ftm.c | 12 ++++++++ drivers/pwm/pwm-img.c | 9 +++--- drivers/pwm/pwm-mxs.c | 34 +++++++++++---------- drivers/pwm/pwm-omap-dmtimer.c | 54 +++++++++++++++++++++++++--------- drivers/pwm/pwm-tiehrpwm.c | 19 ++++++------ 5 files changed, 86 insertions(+), 42 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 1/5 pwm: mxs: Return early when disabling and fix error-path clock leak
When disabling a PWM channel (!state->enabled), the PWM core permits state->period to be zero. mxs_pwm_apply() currently falls through into the period and duty-cycle calculations even when !state->enabled, causing do_div(c, state->period) to divide by zero when state->period is zero. In addition, if the requested period exceeds the maximum divider (div >= PERIOD_CDIV_MAX) while the PWM channel is currently disabled, mxs_pwm_apply() returns -EINVAL without disabling and unpreparing mxs->clk.
Handle the !state->enabled path upfront and return early, balance clk_prepare_enable() on the -EINVAL error path, and check pwm->state.enabled directly rather than calling the consumer helper pwm_is_enabled().
---
v1 => v2:
- Check pwm->state.enabled directly instead of calling the consumer helper pwm_is_enabled() (Uwe)
- Drop Frank Li's Reviewed-by due to code changes
drivers/pwm/pwm-mxs.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/pwm/pwm-mxs.c +19 -15
@@ -56,20 +56,25 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long c;unsigned int pol_bits; +if (!state->enabled) { +if (pwm->state.enabled) { +writel (1 << pwm->hwpwm, mxs->base +PWM_CTRL +CLR ); +clk_disable_unprepare (mxs->clk); + } +return 0 ; + } +/* * If the PWM channel is disabled, make sure to turn on the * clock before calling clk_get_rate() and writing to the * registers. Otherwise, just keep it enabled. */ -if (!pwm_is_enabled (pwm)) { +if (!pwm->state.enabled) { ret =clk_prepare_enable (mxs->clk);if (ret)return ret; } -if (!state->enabled &&pwm_is_enabled (pwm)) -writel (1 << pwm->hwpwm, mxs->base +PWM_CTRL +CLR ); - rate =clk_get_rate (mxs->clk);while (1 ) { c = rate >> cdiv_shift[div];
@@ -78,8 +83,11 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,if (c <PERIOD_PERIOD_MAX )break ; div++; -if (div >=PERIOD_CDIV_MAX ) +if (div >=PERIOD_CDIV_MAX ) { +if (!pwm->state.enabled) +clk_disable_unprepare (mxs->clk);return -EINVAL ; + } } period_cycles = c;
@@ -101,16 +109,12 @@ static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,writel (PERIOD_PERIOD (period_cycles) | pol_bits |PERIOD_CDIV (div), mxs->base +PWM_PERIOD0 + pwm->hwpwm *0x20 ); -if (state->enabled) { -if (!pwm_is_enabled (pwm)) { -/* -* The clock was enabled above. Just enable -* the channel in the control register. -*/ -writel (1 << pwm->hwpwm, mxs->base +PWM_CTRL +SET ); - } - }else { -clk_disable_unprepare (mxs->clk); +if (!pwm->state.enabled) { +/* +* The clock was enabled above. Just enable +* the channel in the control register. +*/ +writel (1 << pwm->hwpwm, mxs->base +PWM_CTRL +SET ); }return 0 ; }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 2/5 pwm: fsl-ftm: Enable ipg_clk around regcache_sync() during resume
In reply to Lee JonesOn SoCs with a dedicated IP interface clock (ipg_clk), register accesses require ipg_clk to be enabled in addition to the ftm_sys clock managed by regmap_mmio. In fsl_pwm_resume(), ipg_clk is only enabled inside the channel loop for channels with PWMF_REQUESTED set, so if no channels are currently requested, regcache_sync() writes to the hardware registers while ipg_clk is disabled.
Enable ipg_clk around regcache_sync() in fsl_pwm_resume(), matching fsl_pwm_init().
---
v1 => v2:
- Move clk_prepare_enable(fpc->ipg_clk) after the channel loop and add an explanatory comment (Uwe)
drivers/pwm/pwm-fsl-ftm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/pwm/pwm-fsl-ftm.c +12 -0
@@ -501,6 +501,7 @@ static int fsl_pwm_resume(struct device *dev) {struct pwm_chip *chip =dev_get_drvdata (dev);struct fsl_pwm_chip *fpc =to_fsl_chip (chip); +int ret;int i;for (i =0 ; i < chip->npwm; i++) {
@@ -518,10 +519,21 @@ static int fsl_pwm_resume(struct device *dev) clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); } +/* +* regmap_mmio only manages ftm_sys, whereas register accesses on SoCs +* with a dedicated IP interface clock also require ipg_clk. Enable +* ipg_clk around regcache_sync() in case no channels are requested. +*/ + ret =clk_prepare_enable (fpc->ipg_clk); +if (ret) +return ret; +/* restore all registers from cache */ regcache_cache_only (fpc->regmap, false);regcache_sync (fpc->regmap); +clk_disable_unprepare (fpc->ipg_clk); +return 0 ; }#endif
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 3/5 pwm: omap-dmtimer: Propagate get_pwm_status() errors in apply()
In reply to Lee Jonesomap->pdata->get_pwm_status() returns a signed int that can be a negative error code on runtime PM failure or invalid timer cookie. pwm_omap_dmtimer_is_enabled() and pwm_omap_dmtimer_polarity() store the return value in an unsigned u32 and test OMAP_TIMER_CTRL_ST (BIT(0)) and OMAP_TIMER_CTRL_SCPWM (BIT(7)), misinterpreting negative error codes (such as -EINVAL or -EACCES) as an enabled or inverted timer.
Store the return value in a signed int and propagate negative error codes through to pwm_omap_dmtimer_apply().
---
v1 => v2:
- Propagate negative get_pwm_status() error codes through to pwm_omap_dmtimer_apply() (Uwe)
drivers/pwm/pwm-omap-dmtimer.c | 54 +++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 14 deletions(-)
drivers/pwm/pwm-omap-dmtimer.c +40 -14
@@ -107,13 +107,15 @@ static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)* pwm_omap_dmtimer_is_enabled() - Detect if the pwm is enabled. * @omap: Pointer to pwm omap dm timer chip * -* Return true if pwm is enabled else false. +* Return: 1 if pwm is enabled, 0 if disabled, or negative error code. */ -static bool pwm_omap_dmtimer_is_enabled (struct pwm_omap_dmtimer_chip *omap) +static int pwm_omap_dmtimer_is_enabled (struct pwm_omap_dmtimer_chip *omap) { -u32 status; +int status; status = omap->pdata->get_pwm_status (omap->dm_timer); +if (status <0 ) +return status;return !!(status &OMAP_TIMER_CTRL_ST ); }
@@ -122,13 +124,15 @@ static bool pwm_omap_dmtimer_is_enabled(struct pwm_omap_dmtimer_chip *omap)* pwm_omap_dmtimer_polarity() - Detect the polarity of pwm. * @omap: Pointer to pwm omap dm timer chip * -* Return the polarity of pwm. +* Return: Polarity of pwm, or negative error code. */ static int pwm_omap_dmtimer_polarity (struct pwm_omap_dmtimer_chip *omap) { -u32 status; +int status; status = omap->pdata->get_pwm_status (omap->dm_timer); +if (status <0 ) +return status;return !!(status &OMAP_TIMER_CTRL_SCPWM ); }
@@ -237,16 +241,21 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,* @chip: Pointer to PWM controller * @pwm: Pointer to PWM channel * @polarity: New pwm polarity to be set +* +* Return: 0 if successfully changed the polarity else appropriate error. */ -static void pwm_omap_dmtimer_set_polarity (struct pwm_chip *chip, -struct pwm_device *pwm, -enum pwm_polarity polarity) +static int pwm_omap_dmtimer_set_polarity (struct pwm_chip *chip, +struct pwm_device *pwm, +enum pwm_polarity polarity) {struct pwm_omap_dmtimer_chip *omap =to_pwm_omap_dmtimer_chip (chip); -bool enabled; +int enabled;/* Disable the PWM before changing the polarity. */ enabled =pwm_omap_dmtimer_is_enabled (omap); +if (enabled <0 ) +return enabled; +if (enabled) omap->pdata->stop (omap->dm_timer);
@@ -257,6 +266,8 @@ static void pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,if (enabled)pwm_omap_dmtimer_start (omap); + +return 0 ; }/**
@@ -272,22 +283,37 @@ static int pwm_omap_dmtimer_apply(struct pwm_chip *chip, const struct pwm_state *state) {struct pwm_omap_dmtimer_chip *omap =to_pwm_omap_dmtimer_chip (chip); -int ret; +int enabled, polarity, ret; -if (pwm_omap_dmtimer_is_enabled (omap) && !state->enabled) { + enabled =pwm_omap_dmtimer_is_enabled (omap); +if (enabled <0 ) +return enabled; + +if (enabled && !state->enabled) { omap->pdata->stop (omap->dm_timer);return 0 ; } -if (pwm_omap_dmtimer_polarity (omap) != state->polarity) -pwm_omap_dmtimer_set_polarity (chip, pwm, state->polarity); + polarity =pwm_omap_dmtimer_polarity (omap); +if (polarity <0 ) +return polarity; + +if (polarity != state->polarity) { + ret =pwm_omap_dmtimer_set_polarity (chip, pwm, state->polarity); +if (ret) +return ret; + } ret =pwm_omap_dmtimer_config (chip, pwm, state->duty_cycle, state->period);if (ret)return ret; -if (!pwm_omap_dmtimer_is_enabled (omap) && state->enabled) { + enabled =pwm_omap_dmtimer_is_enabled (omap); +if (enabled <0 ) +return enabled; + +if (!enabled && state->enabled) { omap->pdata->set_pwm(omap->dm_timer, state->polarity == PWM_POLARITY_INVERSED, true,
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 4/5 pwm: tiehrpwm: Enable runtime PM before registering PWM chip
In reply to Lee Jonesehrpwm_pwm_probe() calls pwmchip_add() before setting driver data and enabling runtime PM. Because pwmchip_add() immediately exposes the PWM channels to consumers while runtime PM is still disabled on the initially RPM_SUSPENDED device, a consumer invoking ehrpwm_pwm_apply() before pm_runtime_enable() runs causes guard(pm_runtime_active) to fail with -EACCES without resuming the device and accesses unclocked MMIO registers.
Set driver data and enable runtime PM before calling pwmchip_add(), disable runtime PM on the error path, and disable runtime PM before unpreparing tbclk in ehrpwm_pwm_remove() to match.
---
v1 => v2:
- Clarify runtime PM state prior to pm_runtime_enable() in the commit description (Uwe)
- Disable runtime PM before unpreparing tbclk in ehrpwm_pwm_remove() to match probe ordering (Uwe)
drivers/pwm/pwm-tiehrpwm.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/pwm/pwm-tiehrpwm.c +10 -9
@@ -461,18 +461,19 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)return ret; } - ret =pwmchip_add (chip); -if (ret <0 ) { -dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); - goto err_clk_unprepare; - } -platform_set_drvdata (pdev, chip);pm_runtime_enable (&pdev->dev); + ret =pwmchip_add (chip); +if (ret <0 ) { +dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); + goto err_pm_disable; + } +return 0 ; -err_clk_unprepare: +err_pm_disable: +pm_runtime_disable (&pdev->dev);clk_unprepare (pc->tbclk);return ret;
@@ -485,9 +486,9 @@ static void ehrpwm_pwm_remove(struct platform_device *pdev)pwmchip_remove (chip); -clk_unprepare (pc->tbclk); -pm_runtime_disable (&pdev->dev); + +clk_unprepare (pc->tbclk); }static void ehrpwm_pwm_save_context (struct pwm_chip *chip)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 5/5 pwm: img: Unregister PWM chip before disabling runtime PM and clocks
In reply to Lee Jonesimg_pwm_remove() disables runtime PM and gates the peripheral clocks via img_pwm_runtime_suspend() before calling pwmchip_remove(). While the PWM chip is still registered, a consumer can call img_pwm_apply(), where pm_runtime_resume_and_get() fails with -EACCES or img_pwm_disable() writes to unclocked registers after img_pwm_runtime_suspend() has gated the clocks.
Call pwmchip_remove() before disabling runtime PM, clearing autosuspend, and suspending the device. Because chip and its private data are allocated with devm_pwmchip_alloc(), driver data remains valid for any pending runtime PM callbacks invoked during pm_runtime_disable() and img_pwm_runtime_suspend() until devres teardown runs after img_pwm_remove() returns.
---
v1 => v2:
- Clear autosuspend in img_pwm_remove() and document why driver data remains valid for runtime PM callbacks after pwmchip_remove() (Uwe)
drivers/pwm/pwm-img.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pwm/pwm-img.c +5 -4
@@ -338,11 +338,12 @@ static void img_pwm_remove(struct platform_device *pdev) {struct pwm_chip *chip =platform_get_drvdata (pdev); -pm_runtime_disable (&pdev->dev); -if (!pm_runtime_status_suspended (&pdev->dev)) -img_pwm_runtime_suspend (&pdev->dev); -pwmchip_remove (chip); + +pm_runtime_disable (&pdev->dev); +pm_runtime_dont_use_autosuspend (&pdev->dev); +if (!pm_runtime_status_suspended (&pdev->dev)) +img_pwm_runtime_suspend (&pdev->dev); }#ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Re: [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip
In reply to Lee JonesQuoted 8 lines · Lee Jones
ehrpwm_pwm_probe()callspwmchip_add()before setting driver data and enabling runtime PM. Becausepwmchip_add()immediately exposes the PWM channels to consumers while runtime PM is still disabled on the initiallyRPM_SUSPENDEDdevice, a consumer invokingehrpwm_pwm_apply()beforepm_runtime_enable()runs causesguard(pm_runtime_active)to fail with-EACCESwithout resuming the device and accesses unclocked MMIO registers.
Quoted 4 lines
Set driver data and enable runtime PM before calling
pwmchip_add(), disable runtime PM on the error path, and disable runtime PM before unpreparing tbclk inehrpwm_pwm_remove()to match.
To match what? The core of this change is moving pwmchip_add() after pm_runtime_enable(), but in remove you switch clk and PM unwinding, not pwm and PM unwinding. Both changes here are correct, but the second is not explained in the commit message.
Would be better to have a patch before this one that switches clk_prepare(), pm_runtime_enable(), and pwmchip_add() over to their DEVM equivalents. That fixes the remove order issue, and removes the remove() function too. Then this patch can simply move the pwmchip_add to after PM enable without any other actions.
Andrew
Quoted 58 lines
Fixes 19891b20e7c2 ("pwm: pwm-tiehrpwm: PWM driver support for EHRPWM")Signed-off-by Lee Jones <[email protected]>---
v1 => v2:
- Clarify runtime PM state prior topm_runtime_enable()in the commit description (Uwe)
- Disable runtime PM before unpreparing tbclk inehrpwm_pwm_remove()to match probe ordering (Uwe)drivers/pwm/pwm-tiehrpwm.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)drivers/pwm/pwm-tiehrpwm.c +10
-9@@ -461,18 +461,19 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)return ret; } - ret =pwmchip_add (chip); -if (ret <0 ) { -dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); - goto err_clk_unprepare; - } -platform_set_drvdata (pdev, chip);pm_runtime_enable (&pdev->dev); + ret =pwmchip_add (chip); +if (ret <0 ) { +dev_err (&pdev->dev,"pwmchip_add() failed: %d\n" , ret); + goto err_pm_disable; + } +return 0 ; -err_clk_unprepare: +err_pm_disable: +pm_runtime_disable (&pdev->dev);clk_unprepare (pc->tbclk);return ret;@@ -485,9 +486,9 @@ static void ehrpwm_pwm_remove(struct platform_device *pdev)pwmchip_remove (chip); -clk_unprepare (pc->tbclk); -pm_runtime_disable (&pdev->dev); + +clk_unprepare (pc->tbclk); }static void ehrpwm_pwm_save_context (struct pwm_chip *chip)
Uwe Kleine-König
maintainerRe: [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip
[Fixed Thierry's email address, I suspect two of the three @ti addresses also don't work, but I don't know a better one for those.]
Hello,
Quoted 9 lines · Andrew Davis
Quoted 8 lines · Lee Jones
ehrpwm_pwm_probe()callspwmchip_add()before setting driver data and enabling runtime PM. Becausepwmchip_add()immediately exposes the PWM channels to consumers while runtime PM is still disabled on the initiallyRPM_SUSPENDEDdevice, a consumer invokingehrpwm_pwm_apply()beforepm_runtime_enable()runs causesguard(pm_runtime_active)to fail with-EACCESwithout resuming the device and accesses unclocked MMIO registers.
Quoted 5 lines
Quoted 3 lines
Set driver data and enable runtime PM before calling
pwmchip_add(), disable runtime PM on the error path, and disable runtime PM before unpreparing tbclk inehrpwm_pwm_remove()to match.To match what?
To match reverse probe order. Pretty common and IMHO the description is good enough.
Quoted 4 lines
The core of this change is moving
pwmchip_add()afterpm_runtime_enable(), but in remove you switch clk and PM unwinding, not pwm and PM unwinding. Both changes here are correct, but the second is not explained in the commit message.
The commit log says about remove "disable runtime PM before unpreparing tbclk" that pretty well mentions switch clk and PM unwinding?!
Quoted 5 lines
Would be better to have a patch before this one that switches
clk_prepare(),pm_runtime_enable(), andpwmchip_add()over to their DEVM equivalents. That fixes the remove order issue, and removes theremove()function too. Then this patch can simply move thepwmchip_addto after PM enable without any other actions.
For me the patch is fine, if someone invests the effort to convert to devm_* that can be done on top of this change.
Best regards
Uwe
Andrew Davis
Reviewed-byRe: [PATCH v2 4/5] pwm: tiehrpwm: Enable runtime PM before registering PWM chip
Quoted 3 lines · Uwe Kleine-König
[Fixed Thierry's email address, I suspect two of the three @ti addresses also don't work, but I don't know a better one for those.]
All 3 of the @ti addresses are gone, dropping them (all other than me).
Quoted 21 lines
Hello,
Quoted 9 lines · Andrew Davis
Quoted 8 lines · Lee Jones
ehrpwm_pwm_probe()callspwmchip_add()before setting driver data and enabling runtime PM. Becausepwmchip_add()immediately exposes the PWM channels to consumers while runtime PM is still disabled on the initiallyRPM_SUSPENDEDdevice, a consumer invokingehrpwm_pwm_apply()beforepm_runtime_enable()runs causesguard(pm_runtime_active)to fail with-EACCESwithout resuming the device and accesses unclocked MMIO registers.Quoted 5 lines
Quoted 3 lines
Set driver data and enable runtime PM before calling
pwmchip_add(), disable runtime PM on the error path, and disable runtime PM before unpreparing tbclk inehrpwm_pwm_remove()to match.To match what?
To match reverse probe order. Pretty common and IMHO the description is good enough.
Quoted 8 lines
Quoted 4 lines
The core of this change is moving
pwmchip_add()afterpm_runtime_enable(), but in remove you switch clk and PM unwinding, not pwm and PM unwinding. Both changes here are correct, but the second is not explained in the commit message.The commit log says about remove "disable runtime PM before unpreparing tbclk" that pretty well mentions switch clk and PM unwinding?!
But that order was wrong before, and fixing that is unrelated to the rest of this patch (which is clearly about enabling PM before registering PWM). It is just mentioned in passing, basically "BTW fix this unrelated issue while here". Which is fine, just pointing out this could have been two patches for better logical splitting.
Quoted 9 lines
Quoted 5 lines
Would be better to have a patch before this one that switches
clk_prepare(),pm_runtime_enable(), andpwmchip_add()over to their DEVM equivalents. That fixes the remove order issue, and removes theremove()function too. Then this patch can simply move thepwmchip_addto after PM enable without any other actions.For me the patch is fine, if someone invests the effort to convert to devm_* that can be done on top of this change.
Fair enough, I'll switch it over after this goes in.
Quoted 2 lines
Best regards
Uwe