Hello Marek, On Thu, May 11, 2023 at 08:18:53PM +0200, Marek Vasut wrote: > In case the PWM is not enabled, the period can still be left unconfigured, > i.e. zero . Currently the pwm_class_apply_state() errors out in such a case. > This e.g. makes suspend fail on systems where pwmchip has been exported via > sysfs interface, but left unconfigured before suspend occurred. > > Failing case: > " > $ echo 1 > /sys/class/pwm/pwmchip4/export > $ echo mem > /sys/power/state > ... > pwm pwmchip4: PM: dpm_run_callback(): pwm_class_suspend+0x1/0xa8 returns -22 > pwm pwmchip4: PM: failed to suspend: error -22 > PM: Some devices failed to suspend, or early wake event detected > " > > Working case: > " > $ echo 1 > /sys/class/pwm/pwmchip4/export > $ echo 100 > /sys/class/pwm/pwmchip4/pwm1/period > $ echo 10 > /sys/class/pwm/pwmchip4/pwm1/duty_cycle > $ echo mem > /sys/power/state > ... > " > > Permit unset period in pwm_class_apply_state() in case the PWM is disabled > to fix this issue. > > Fixes: ef2bf4997f7d ("pwm: Improve args checking in pwm_apply_state()") > Signed-off-by: Marek Vasut > --- > Cc: Brian Norris > Cc: "Uwe Kleine-König" > Cc: Geert Uytterhoeven > Cc: Thierry Reding > Cc: Yoshihiro Shimoda > Cc: linux-pwm@vger.kernel.org > --- > drivers/pwm/core.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c > index 3dacceaef4a9b..87252b70f1c81 100644 > --- a/drivers/pwm/core.c > +++ b/drivers/pwm/core.c > @@ -510,8 +510,8 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state) > */ > might_sleep(); > > - if (!pwm || !state || !state->period || > - state->duty_cycle > state->period) > + if (!pwm || !state || (state->enabled && > + (!state->period || state->duty_cycle > state->period))) While I tend to agree that the checks about period and duty_cycle are (somewhat) irrelevant if enabled == false, I fear we'd break assumptions here as some drivers configure duty_cycle/period in hardware even with .enabled == false, and so proably rely on period > 0 and duty_cycle <= period. Another approach would be to skip pwm_apply_state() in pwm_class_suspend() if the state is already disabled. That one sounds safer. Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |