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>,
	Lee Jones <lee.jones@linaro.org>
Cc: linux-pwm@vger.kernel.org, kernel@pengutronix.de,
	Thomas Hebb <tommyhebb@gmail.com>,
	Jisheng Zhang <jszhang@marvell.com>
Subject: [PATCH 3/4] pwm: berlin: Implement .apply() callback
Date: Tue,  4 May 2021 15:25:36 +0200	[thread overview]
Message-ID: <20210504132537.62072-3-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20210504132537.62072-1-u.kleine-koenig@pengutronix.de>

To eventually get rid of all legacy drivers convert this driver to the
modern world implementing .apply(). This just pushes down a slightly
optimized variant of how legacy drivers are handled in the core.

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

diff --git a/drivers/pwm/pwm-berlin.c b/drivers/pwm/pwm-berlin.c
index 894ea5869d42..4e838a973c97 100644
--- a/drivers/pwm/pwm-berlin.c
+++ b/drivers/pwm/pwm-berlin.c
@@ -73,7 +73,7 @@ static inline void berlin_pwm_writel(struct berlin_pwm_chip *bpc,
 }
 
 static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
-			     int duty_ns, int period_ns)
+			     u64 duty_ns, u64 period_ns)
 {
 	struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip);
 	bool prescale_4096 = false;
@@ -152,11 +152,44 @@ static void berlin_pwm_disable(struct pwm_chip *chip,
 	berlin_pwm_writel(bpc, pwm->hwpwm, value, BERLIN_PWM_EN);
 }
 
+static int berlin_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			    const struct pwm_state *state)
+{
+	int err;
+	bool enabled = pwm->state.enabled;
+
+	if (state->polarity != pwm->state.polarity) {
+		if (enabled) {
+			berlin_pwm_disable(chip, pwm);
+			enabled = false;
+		}
+
+		err = berlin_pwm_set_polarity(chip, pwm, state->polarity);
+		if (err)
+			return err;
+	}
+
+	if (!state->enabled) {
+		if (enabled)
+			berlin_pwm_disable(chip, pwm);
+		return 0;
+	}
+
+	if (state->period != pwm->state.period ||
+	    state->duty_cycle != pwm->state.duty_cycle) {
+		err = berlin_pwm_config(chip, pwm, state->duty_cycle, state->period);
+		if (err)
+			return err;
+	}
+
+	if (!enabled)
+		return berlin_pwm_enable(chip, pwm);
+
+	return 0;
+}
+
 static const struct pwm_ops berlin_pwm_ops = {
-	.config = berlin_pwm_config,
-	.set_polarity = berlin_pwm_set_polarity,
-	.enable = berlin_pwm_enable,
-	.disable = berlin_pwm_disable,
+	.apply = berlin_pwm_apply,
 	.owner = THIS_MODULE,
 };
 
-- 
2.30.2


  parent reply	other threads:[~2021-05-04 13:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04 13:25 [PATCH 1/4] pwm: berlin: use consistent naming for variables Uwe Kleine-König
2021-05-04 13:25 ` [PATCH 2/4] pwm: berlin: Put channel config into driver data Uwe Kleine-König
2021-06-30  6:18   ` Uwe Kleine-König
2021-09-09 14:16     ` Uwe Kleine-König
2021-11-08 11:04       ` Uwe Kleine-König
2021-11-08 12:09   ` Thierry Reding
2021-11-08 13:08     ` Uwe Kleine-König
2021-05-04 13:25 ` Uwe Kleine-König [this message]
2021-05-04 13:25 ` [PATCH 4/4] pwm: berlin: Don't check the return code of pwmchip_remove() 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=20210504132537.62072-3-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=jszhang@marvell.com \
    --cc=kernel@pengutronix.de \
    --cc=lee.jones@linaro.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=tommyhebb@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).