pwm: lpss: Fix get_state runtime-pm reference handling
Subsystems (4)
PATCH 1/1 pwm: lpss: Fix get_state runtime-pm reference handling
Before commit cfc4c189bc70 ("pwm: Read initial hardware state at request time"), a driver's get_state callback would get called once per PWM from pwmchip_add().
pwm-lpss' runtime-pm code was relying on this, getting a runtime-pm ref for PWMs which are enabled at probe time from within its get_state callback, before enabling runtime-pm.
The change to calling get_state at request time causes a number of problems:
1. PWMs enabled at probe time may get runtime suspended before they are requested, causing e.g. a LCD backlight controlled by the PWM to turn off.
2. When the request happens when the PWM has been runtime suspended, the ctrl register will read all 1 / 0xffffffff, causing get_state to store bogus values in the pwm_state.
3. get_state was using an async pm_runtime_get() call, because it assumed that runtime-pm has not been enabled yet. If shortly after the request an apply call is made, then the pwm_lpss_is_updating() check may trigger because the resume triggered by the pm_runtime_get() call is not complete yet, so the ctrl register still reads all 1 / 0xffffffff.
This commit fixes these issues by moving the initial pm_runtime_get() call for PWMs which are enabled at probe time to the pwm_lpss_probe() function; and by making get_state take a runtime-pm ref before reading the ctrl reg.
--- drivers/pwm/pwm-lpss.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/pwm/pwm-lpss.c +11 -4
@@ -158,7 +158,6 @@ static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,return 0 ; } -/* This function gets called once from pwmchip_add to get the initial state */ static void pwm_lpss_get_state (struct pwm_chip *chip,struct pwm_device *pwm,struct pwm_state *state) {
@@ -167,6 +166,8 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long base_unit, freq, on_time_div;u32 ctrl; +pm_runtime_get_sync (chip->dev); + base_unit_range =BIT (lpwm->info->base_unit_bits); ctrl =pwm_lpss_read (pwm);
@@ -187,8 +188,7 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->polarity =PWM_POLARITY_NORMAL ; state->enabled = !!(ctrl &PWM_ENABLE ); -if (state->enabled) -pm_runtime_get (chip->dev); +pm_runtime_put (chip->dev); }static const struct pwm_ops pwm_lpss_ops = {
@@ -202,7 +202,8 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, {struct pwm_lpss_chip *lpwm;unsigned long c; -int ret; +int i, ret; +u32 ctrl;if (WARN_ON (info->npwm >MAX_PWMS ))return ERR_PTR (-ENODEV );
@@ -232,6 +233,12 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,return ERR_PTR (ret); } +for (i =0 ; i < lpwm->info->npwm; i++) { + ctrl =pwm_lpss_read (&lpwm->chip.pwms[i]); +if (ctrl &PWM_ENABLE ) +pm_runtime_get (dev); + } +return lpwm; }EXPORT_SYMBOL_GPL (pwm_lpss_probe);
Signature · 1 line
2.26.0
Quoted 24 lines · Hans de Goede
Before commit
cfc4c189bc70("pwm: Read initial hardware state at request time"), a driver'sget_statecallback would get called once per PWM frompwmchip_add().pwm-lpss' runtime-pm code was relying on this, getting a runtime-pm ref for PWMs which are enabled at probe time from within its
get_statecallback, before enabling runtime-pm.The change to calling
get_stateat request time causes a number of problems:1. PWMs enabled at probe time may get runtime suspended before they are requested, causing e.g. a LCD backlight controlled by the PWM to turn off.
2. When the request happens when the PWM has been runtime suspended, the ctrl register will read all 1 /
0xffffffff, causingget_stateto store bogus values in thepwm_state.3.
get_statewas using an asyncpm_runtime_get()call, because it assumed that runtime-pm has not been enabled yet. If shortly after the request an apply call is made, then thepwm_lpss_is_updating()check may trigger because the resume triggered by thepm_runtime_get()call is not complete yet, so the ctrl register still reads all 1 /0xffffffff.
Quoted 3 lines
This commit fixes these issues by moving the initial
pm_runtime_get()call for PWMs which are enabled at probe time to thepwm_lpss_probe()function; and by makingget_statetake a runtime-pm ref before reading the ctrl reg.
One thing to consider below.
Quoted 50 lines
Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")Signed-off-by Hans de Goede <[email protected]>--- drivers/pwm/pwm-lpss.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/pwm/pwm-lpss.c +5
-4@@ -158,7 +158,6 @@ static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,return 0 ; } -/* This function gets called once from pwmchip_add to get the initial state */ static void pwm_lpss_get_state (struct pwm_chip *chip,struct pwm_device *pwm,struct pwm_state *state) {@@ -167,6 +166,8 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long base_unit, freq, on_time_div;u32 ctrl; +pm_runtime_get_sync (chip->dev); + base_unit_range =BIT (lpwm->info->base_unit_bits); ctrl =pwm_lpss_read (pwm);@@ -187,8 +188,7 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->polarity =PWM_POLARITY_NORMAL ; state->enabled = !!(ctrl &PWM_ENABLE ); -if (state->enabled) -pm_runtime_get (chip->dev); +pm_runtime_put (chip->dev); }static const struct pwm_ops pwm_lpss_ops = {@@ -202,7 +202,8 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, {struct pwm_lpss_chip *lpwm;unsigned long c; -int ret; +int i, ret; +u32 ctrl;if (WARN_ON (info->npwm >MAX_PWMS ))return ERR_PTR (-ENODEV );
drivers/pwm/pwm-lpss.c struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, · line 239
Quoted 7 lines
drivers/pwm/pwm-lpss.c +3
-0@@ -232,6 +233,12 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,return ERR_PTR (ret); } +for (i =0 ; i < lpwm->info->npwm; i++) { + ctrl =pwm_lpss_read (&lpwm->chip.pwms[i]); +if (ctrl &PWM_ENABLE )
I would create a helper for this as opposite to pwm_lpss_cond_enable(), something like pwm_lpss_is_enabled().
Quoted 9 lines
drivers/pwm/pwm-lpss.c +3
-0+pm_runtime_get (dev); + } +return lpwm; }
EXPORT_SYMBOL_GPL(pwm_lpss_probe);Signature · 1 line
2.26.0
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi,
Quoted 25 lines · Andy Shevchenko
Quoted 24 lines · Hans de Goede
Before commit
cfc4c189bc70("pwm: Read initial hardware state at request time"), a driver'sget_statecallback would get called once per PWM frompwmchip_add().pwm-lpss' runtime-pm code was relying on this, getting a runtime-pm ref for PWMs which are enabled at probe time from within its
get_statecallback, before enabling runtime-pm.The change to calling
get_stateat request time causes a number of problems:1. PWMs enabled at probe time may get runtime suspended before they are requested, causing e.g. a LCD backlight controlled by the PWM to turn off.
2. When the request happens when the PWM has been runtime suspended, the ctrl register will read all 1 /
0xffffffff, causingget_stateto store bogus values in thepwm_state.3.
get_statewas using an asyncpm_runtime_get()call, because it assumed that runtime-pm has not been enabled yet. If shortly after the request an apply call is made, then thepwm_lpss_is_updating()check may trigger because the resume triggered by thepm_runtime_get()call is not complete yet, so the ctrl register still reads all 1 /0xffffffff.
Quoted 5 lines
Quoted 3 lines
This commit fixes these issues by moving the initial
pm_runtime_get()call for PWMs which are enabled at probe time to thepwm_lpss_probe()function; and by makingget_statetake a runtime-pm ref before reading the ctrl reg.Reviewed-by Andy Shevchenko <[email protected]>
Thank you.
Quoted 55 lines
One thing to consider below.
Quoted 53 lines
Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")Signed-off-by Hans de Goede <[email protected]>--- drivers/pwm/pwm-lpss.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)drivers/pwm/pwm-lpss.c +5
-4@@ -158,7 +158,6 @@ static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,return 0 ; } -/* This function gets called once from pwmchip_add to get the initial state */ static void pwm_lpss_get_state (struct pwm_chip *chip,struct pwm_device *pwm,struct pwm_state *state) {@@ -167,6 +166,8 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,unsigned long long base_unit, freq, on_time_div;u32 ctrl; +pm_runtime_get_sync (chip->dev); + base_unit_range =BIT (lpwm->info->base_unit_bits); ctrl =pwm_lpss_read (pwm);@@ -187,8 +188,7 @@ static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->polarity =PWM_POLARITY_NORMAL ; state->enabled = !!(ctrl &PWM_ENABLE ); -if (state->enabled) -pm_runtime_get (chip->dev); +pm_runtime_put (chip->dev); }static const struct pwm_ops pwm_lpss_ops = {@@ -202,7 +202,8 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, {struct pwm_lpss_chip *lpwm;unsigned long c; -int ret; +int i, ret; +u32 ctrl;if (WARN_ON (info->npwm >MAX_PWMS ))return ERR_PTR (-ENODEV );@@ -232,6 +233,12 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,return ERR_PTR (ret); }
Quoted 7 lines
drivers/pwm/pwm-lpss.c struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, · line 238
Quoted 4 lines
drivers/pwm/pwm-lpss.c +3
-0+for (i =0 ; i < lpwm->info->npwm; i++) { + ctrl =pwm_lpss_read (&lpwm->chip.pwms[i]); +if (ctrl &PWM_ENABLE )I would create a helper for this as opposite to
pwm_lpss_cond_enable(), something likepwm_lpss_is_enabled().
pwm_lpss_cond_enable() is used in multiple places, this check we only need once, so I would prefer to just keep it as is.
Regards,
Hans
Quoted 11 lines
Quoted 10 lines
drivers/pwm/pwm-lpss.c +3
-0+pm_runtime_get (dev); + } +return lpwm; }
EXPORT_SYMBOL_GPL(pwm_lpss_probe);Signature · 1 line
2.26.0
Thierry Reding
AppliedQuoted 28 lines · Hans de Goede
Before commit
cfc4c189bc70("pwm: Read initial hardware state at request time"), a driver'sget_statecallback would get called once per PWM frompwmchip_add().pwm-lpss' runtime-pm code was relying on this, getting a runtime-pm ref for PWMs which are enabled at probe time from within its
get_statecallback, before enabling runtime-pm.The change to calling
get_stateat request time causes a number of problems:1. PWMs enabled at probe time may get runtime suspended before they are requested, causing e.g. a LCD backlight controlled by the PWM to turn off.
2. When the request happens when the PWM has been runtime suspended, the ctrl register will read all 1 /
0xffffffff, causingget_stateto store bogus values in thepwm_state.3.
get_statewas using an asyncpm_runtime_get()call, because it assumed that runtime-pm has not been enabled yet. If shortly after the request an apply call is made, then thepwm_lpss_is_updating()check may trigger because the resume triggered by thepm_runtime_get()call is not complete yet, so the ctrl register still reads all 1 /0xffffffff.This commit fixes these issues by moving the initial
pm_runtime_get()call for PWMs which are enabled at probe time to thepwm_lpss_probe()function; and by makingget_statetake a runtime-pm ref before reading the ctrl reg.
Quoted 7 lines
Fixes cfc4c189bc70 ("pwm: Read initial hardware state at request time")Signed-off-by Hans de Goede <[email protected]>--- drivers/pwm/pwm-lpss.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
Applied, thanks.
Thierry