All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Lee Jones <lee.jones@linaro.org>,
	linux-pwm@vger.kernel.org, kernel@pengutronix.de,
	Geert Uytterhoeven <geert@linux-m68k.org>
Subject: Re: [PATCH 2/3] pwm: Prevent a glitch for legacy drivers
Date: Thu, 19 Aug 2021 15:36:26 +0200	[thread overview]
Message-ID: <YR5eWvpEaLxgdWxD@orome.fritz.box> (raw)
In-Reply-To: <20210701072927.328254-3-u.kleine-koenig@pengutronix.de>

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

On Thu, Jul 01, 2021 at 09:29:26AM +0200, Uwe Kleine-König wrote:
> If a running PWM is reconfigured to disabled calling the ->config()
> callback before disabling the hardware might result in a glitch where
> the (maybe) new period and duty_cycle are visible on the output before
> disabling the hardware.
> 
> So handle disabling before calling ->config(). Also exit early in this case
> which is possible because period and duty_cycle don't matter for disabled PWMs.
> In return however ->config has to be called even if state->period ==
> pwm->state.period && state->duty_cycle != pwm->state.duty_cycle because setting
> these might have been skipped in the previous call.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---
>  drivers/pwm/core.c | 41 ++++++++++++++++++++++++-----------------
>  1 file changed, 24 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index 3c72f8963073..20afe6d0bc5e 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -568,26 +568,33 @@ static int pwm_apply_legacy(struct pwm_chip *chip, struct pwm_device *pwm,
>  		pwm->state.polarity = state->polarity;
>  	}
>  
> -	if (state->period != pwm->state.period ||
> -	    state->duty_cycle != pwm->state.duty_cycle) {
> -		err = chip->ops->config(pwm->chip, pwm,
> -					state->duty_cycle,
> -					state->period);
> -		if (err)
> -			return err;
> +	if (!state->enabled) {
> +		if (pwm->state.enabled)
> +			chip->ops->disable(chip, pwm);
>  
> -		pwm->state.period = state->period;
> -		pwm->state.duty_cycle = state->duty_cycle;
> +		return 0;
>  	}
>  
> -	if (state->enabled != pwm->state.enabled) {
> -		if (!pwm->state.enabled) {
> -			err = chip->ops->enable(chip, pwm);
> -			if (err)
> -				return err;
> -		} else {
> -			chip->ops->disable(chip, pwm);
> -		}
> +	/*
> +	 * We cannot skip calling ->config even if state->period ==
> +	 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
> +	 * because we might have exited early in the last call to
> +	 * pwm_apply_state because of !state->enabled and so the two values in
> +	 * pwm->state might not be configured in hardware.
> +	 */
> +	err = chip->ops->config(pwm->chip, pwm,
> +				state->duty_cycle,
> +				state->period);
> +	if (err)
> +		return err;
> +
> +	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;
>  	}

I thought we might have discussed this, but I can't find any record of
it. How is this better than always configuring, whether the PWM is
disabled or not?

From an atomic point of view, the hardware state is expected to match
the PWM state that was passed to ->apply() after it returns. That means
that calling ->get_state() after ->apply() should return the same state
that was passed to ->apply(). With the above that's no longer true. It
doesn't actually matter, because legacy drivers don't support
->get_state(), but conceptually it's not mimicking the atomic API as
well as it could.

Thierry

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

  reply	other threads:[~2021-08-19 13:36 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 [this message]
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
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=YR5eWvpEaLxgdWxD@orome.fritz.box \
    --to=thierry.reding@gmail.com \
    --cc=geert@linux-m68k.org \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=u.kleine-koenig@pengutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.