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

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;
 	}
 
 	return 0;
-- 
2.30.2


  parent reply	other threads:[~2021-07-01  7:29 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 ` Uwe Kleine-König [this message]
2021-08-19 13:36   ` [PATCH 2/3] pwm: Prevent a glitch for legacy drivers 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
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=20210701072927.328254-3-u.kleine-koenig@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 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.