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
Subject: [PATCH] pwm: Prevent a glitch in compat code
Date: Mon,  8 Mar 2021 10:36:00 +0100	[thread overview]
Message-ID: <20210308093600.25455-1-u.kleine-koenig@pengutronix.de> (raw)

When a PWM is to be disabled, configuring the duty cycle and
period before actually disabling the hardware might result in either a
glitch or a delay. So check for disabling first and return early in this
case.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pwm/core.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 4058d3c86a45..4604ca3e0e62 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -597,6 +597,12 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
 			pwm->state.polarity = state->polarity;
 		}
 
+		if (!state->enabled && pwm->state.enabled) {
+			chip->ops->disable(chip, pwm);
+			pwm->state.enabled = false;
+			return 0;
+		}
+
 		if (state->period != pwm->state.period ||
 		    state->duty_cycle != pwm->state.duty_cycle) {
 			int duty_cycle, period;
@@ -620,16 +626,12 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
 			pwm->state.period = state->period;
 		}
 
-		if (state->enabled != pwm->state.enabled) {
-			if (state->enabled) {
-				err = chip->ops->enable(chip, pwm);
-				if (err)
-					return err;
-			} else {
-				chip->ops->disable(chip, pwm);
-			}
+		if (!pwm->state.enabled) {
+			err = chip->ops->enable(chip, pwm);
+			if (err)
+				return err;
 
-			pwm->state.enabled = state->enabled;
+			pwm->state.enabled = true;
 		}
 	}
 
-- 
2.30.1


             reply	other threads:[~2021-03-08  9:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-08  9:36 Uwe Kleine-König [this message]
2021-03-22 11:01 ` [PATCH] pwm: Prevent a glitch in compat code Thierry Reding
2021-03-22 11:11   ` Uwe Kleine-König
2021-04-10 21:46     ` Uwe Kleine-König
2021-04-11  8:22       ` 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=20210308093600.25455-1-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --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.