linux-pwm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: Thierry Reding <thierry.reding@gmail.com>
Cc: linux-pwm@vger.kernel.org, Lee Jones <lee.jones@linaro.org>,
	kernel@pengutronix.de, Geert Uytterhoeven <geert@linux-m68k.org>
Subject: Re: [PATCH 3/3] pwm: Restore initial state if a legacy callback fails
Date: Tue, 7 Sep 2021 12:41:31 +0200	[thread overview]
Message-ID: <20210907104131.5ngowf3lmbehy4ni@pengutronix.de> (raw)
In-Reply-To: <YR5cf+UIpc3lLotK@orome.fritz.box>

[-- Attachment #1: Type: text/plain, Size: 2838 bytes --]

On Thu, Aug 19, 2021 at 03:28:31PM +0200, Thierry Reding wrote:
> On Thu, Jul 01, 2021 at 09:29:27AM +0200, Uwe Kleine-König wrote:
> > 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 <u.kleine-koenig@pengutronix.de>
> > ---
> >  drivers/pwm/core.c | 14 ++++++++------
> >  1 file changed, 8 insertions(+), 6 deletions(-)
> > 
> > diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> > index 20afe6d0bc5e..6e30ef9b9b79 100644
> > --- a/drivers/pwm/core.c
> > +++ b/drivers/pwm/core.c
> > @@ -539,10 +539,8 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
> >  			    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;
> > @@ -563,7 +561,7 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
> >  
> >  		err = chip->ops->set_polarity(chip, pwm, state->polarity);
> >  		if (err)
> > -			return err;
> > +			goto rollback;
> >  
> >  		pwm->state.polarity = state->polarity;
> >  	}
> > @@ -586,7 +584,7 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
> >  				state->duty_cycle,
> >  				state->period);
> >  	if (err)
> > -		return err;
> > +		goto rollback;
> >  
> >  	pwm->state.period = state->period;
> >  	pwm->state.duty_cycle = state->duty_cycle;
> > @@ -594,10 +592,14 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
> >  	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;
> >  }
> 
> Can't we achieve the same thing by just removing all the updates to
> pwm->state in pwm_apply_legacy()? Patch 1 in the series now does
> pwm->state = *state for both the atomic and the legacy cases, so if
> we don't update pwm->state explicitly in pwm_apply_legacy(), then
> there should be no need to rollback, right?

No, this doesn't work because the implemention of the callbacks might
rely on ->state holding the result from a previous callback.
See for example drivers/pwm/pwm-lpc18xx-sct.c where .enable() calls
pwm_get_polarity().

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2021-09-07 10:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-01  7:29 [PATCH 0/3] pwm: Some improvements for legacy drivers Uwe Kleine-König
2021-07-01  7:29 ` [PATCH 1/3] pwm: Move legacy driver handling into a dedicated function Uwe Kleine-König
2021-07-01  7:29 ` [PATCH 2/3] pwm: Prevent a glitch for legacy drivers Uwe Kleine-König
2021-08-19 13:36   ` Thierry Reding
2021-09-07 10:36     ` Uwe Kleine-König
2021-07-01  7:29 ` [PATCH 3/3] pwm: Restore initial state if a legacy callback fails Uwe Kleine-König
2021-08-19 13:28   ` Thierry Reding
2021-09-07 10:41     ` Uwe Kleine-König [this message]
2021-07-01  8:58 ` [PATCH 0/3] pwm: Some improvements for legacy drivers Geert Uytterhoeven
2021-07-01 10:45   ` Uwe Kleine-König
2021-07-01 11:41     ` Geert Uytterhoeven
2021-07-01 12:19       ` Uwe Kleine-König
2021-11-05 19:19 ` Uwe Kleine-König
2021-11-17 16:12 ` Thierry Reding
2021-11-23 17:15   ` Uwe Kleine-König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210907104131.5ngowf3lmbehy4ni@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=geert@linux-m68k.org \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).