Skip to content

Commit e45a178

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: Restore initial state if a legacy callback fails
It is not entirely accurate to go back to the initial state after e.g. .enable() failed, as .config() still modified the hardware, but this same inconsistency exists for drivers that implement .apply(). Signed-off-by: Uwe Kleine-König <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 92f69e5 commit e45a178

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

drivers/pwm/core.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,8 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
526526
const struct pwm_state *state)
527527
{
528528
int err;
529+
struct pwm_state initial_state = pwm->state;
529530

530-
/*
531-
* FIXME: restore the initial state in case of error.
532-
*/
533531
if (state->polarity != pwm->state.polarity) {
534532
if (!chip->ops->set_polarity)
535533
return -EINVAL;
@@ -550,7 +548,7 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
550548

551549
err = chip->ops->set_polarity(chip, pwm, state->polarity);
552550
if (err)
553-
return err;
551+
goto rollback;
554552

555553
pwm->state.polarity = state->polarity;
556554
}
@@ -573,18 +571,22 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
573571
state->duty_cycle,
574572
state->period);
575573
if (err)
576-
return err;
574+
goto rollback;
577575

578576
pwm->state.period = state->period;
579577
pwm->state.duty_cycle = state->duty_cycle;
580578

581579
if (!pwm->state.enabled) {
582580
err = chip->ops->enable(chip, pwm);
583581
if (err)
584-
return err;
582+
goto rollback;
585583
}
586584

587585
return 0;
586+
587+
rollback:
588+
pwm->state = initial_state;
589+
return err;
588590
}
589591

590592
/**

0 commit comments

Comments
 (0)