const struct pwm_state *state)
{
int err;
+ struct pwm_state initial_state = pwm->state;
- /*
- * FIXME: restore the initial state in case of error.
- */
if (state->polarity != pwm->state.polarity) {
if (!chip->ops->set_polarity)
return -EINVAL;
err = chip->ops->set_polarity(chip, pwm, state->polarity);
if (err)
- return err;
+ goto rollback;
pwm->state.polarity = state->polarity;
}
state->duty_cycle,
state->period);
if (err)
- return err;
+ goto rollback;
pwm->state.period = state->period;
pwm->state.duty_cycle = state->duty_cycle;
if (!pwm->state.enabled) {
err = chip->ops->enable(chip, pwm);
if (err)
- return err;
+ goto rollback;
}
return 0;
+
+rollback:
+ pwm->state = initial_state;
+ return err;
}
/**