linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration
@ 2020-12-09 20:48 Lino Sanfilippo
  2020-12-10 12:58 ` Uwe Kleine-König
  2020-12-10 17:21 ` Thierry Reding
  0 siblings, 2 replies; 3+ messages in thread
From: Lino Sanfilippo @ 2020-12-09 20:48 UTC (permalink / raw)
  To: thierry.reding
  Cc: u.kleine-koenig, lee.jones, nsaenzjulienne, f.fainelli, rjui,
	sean, sbranden, bcm-kernel-feedback-list, linux-pwm,
	linux-rpi-kernel, linux-arm-kernel, linux-kernel,
	Lino Sanfilippo

Use the newer .apply function of pwm_ops instead of .config, .enable,
.disable and .set_polarity. This guarantees atomic changes of the pwm
controller configuration. It also reduces the size of the driver.

Since now period is a 64 bit value, add an extra check to reject periods
that exceed the possible max value for the 32 bit register.

This has been tested on a Raspberry PI 4.

Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
---

v4: Remove a superfluous blank line
    Remove an unneeded cast (both requested by Uwe Kleine-König)
v3: Check against period truncation (also based on a review by Uwe)
v2: Fix compiler error for 64 bit builds

 drivers/pwm/pwm-bcm2835.c | 69 +++++++++++++++++------------------------------
 1 file changed, 24 insertions(+), 45 deletions(-) j

diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
index d78f86f..4b1ee44 100644
--- a/drivers/pwm/pwm-bcm2835.c
+++ b/drivers/pwm/pwm-bcm2835.c
@@ -58,13 +58,15 @@ static void bcm2835_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
 	writel(value, pc->base + PWM_CONTROL);
 }
 
-static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
-			      int duty_ns, int period_ns)
+static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
+			     const struct pwm_state *state)
 {
+
 	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
 	unsigned long rate = clk_get_rate(pc->clk);
+	unsigned long long period;
 	unsigned long scaler;
-	u32 period;
+	u32 val;
 
 	if (!rate) {
 		dev_err(pc->dev, "failed to get clock rate\n");
@@ -72,54 +74,34 @@ static int bcm2835_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
 	}
 
 	scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate);
-	period = DIV_ROUND_CLOSEST(period_ns, scaler);
+	/* set period */
+	period = DIV_ROUND_CLOSEST_ULL(state->period, scaler);
 
-	if (period < PERIOD_MIN)
+	/* dont accept a period that is too small or has been truncated */
+	if ((period < PERIOD_MIN) || (period > U32_MAX))
 		return -EINVAL;
 
-	writel(DIV_ROUND_CLOSEST(duty_ns, scaler),
-	       pc->base + DUTY(pwm->hwpwm));
 	writel(period, pc->base + PERIOD(pwm->hwpwm));
 
-	return 0;
-}
-
-static int bcm2835_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
-{
-	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
-	u32 value;
-
-	value = readl(pc->base + PWM_CONTROL);
-	value |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
-	writel(value, pc->base + PWM_CONTROL);
+	/* set duty cycle */
+	val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, scaler);
+	writel(val, pc->base + DUTY(pwm->hwpwm));
 
-	return 0;
-}
+	/* set polarity */
+	val = readl(pc->base + PWM_CONTROL);
 
-static void bcm2835_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
-{
-	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
-	u32 value;
-
-	value = readl(pc->base + PWM_CONTROL);
-	value &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
-	writel(value, pc->base + PWM_CONTROL);
-}
-
-static int bcm2835_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
-				enum pwm_polarity polarity)
-{
-	struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
-	u32 value;
-
-	value = readl(pc->base + PWM_CONTROL);
+	if (state->polarity == PWM_POLARITY_NORMAL)
+		val &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
+	else
+		val |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
 
-	if (polarity == PWM_POLARITY_NORMAL)
-		value &= ~(PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm));
+	/* enable/disable */
+	if (state->enabled)
+		val |= PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm);
 	else
-		value |= PWM_POLARITY << PWM_CONTROL_SHIFT(pwm->hwpwm);
+		val &= ~(PWM_ENABLE << PWM_CONTROL_SHIFT(pwm->hwpwm));
 
-	writel(value, pc->base + PWM_CONTROL);
+	writel(val, pc->base + PWM_CONTROL);
 
 	return 0;
 }
@@ -127,10 +109,7 @@ static int bcm2835_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
 static const struct pwm_ops bcm2835_pwm_ops = {
 	.request = bcm2835_pwm_request,
 	.free = bcm2835_pwm_free,
-	.config = bcm2835_pwm_config,
-	.enable = bcm2835_pwm_enable,
-	.disable = bcm2835_pwm_disable,
-	.set_polarity = bcm2835_set_polarity,
+	.apply = bcm2835_pwm_apply,
 	.owner = THIS_MODULE,
 };
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration
  2020-12-09 20:48 [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration Lino Sanfilippo
@ 2020-12-10 12:58 ` Uwe Kleine-König
  2020-12-10 17:21 ` Thierry Reding
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2020-12-10 12:58 UTC (permalink / raw)
  To: Lino Sanfilippo
  Cc: thierry.reding, lee.jones, nsaenzjulienne, f.fainelli, rjui,
	sean, sbranden, bcm-kernel-feedback-list, linux-pwm,
	linux-rpi-kernel, linux-arm-kernel, linux-kernel

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

On Wed, Dec 09, 2020 at 09:48:25PM +0100, Lino Sanfilippo wrote:
> Use the newer .apply function of pwm_ops instead of .config, .enable,
> .disable and .set_polarity. This guarantees atomic changes of the pwm
> controller configuration. It also reduces the size of the driver.
> 
> Since now period is a 64 bit value, add an extra check to reject periods
> that exceed the possible max value for the 32 bit register.
> 
> This has been tested on a Raspberry PI 4.
> 
> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>

I replied to v3 earlier, this was supposed to go to this v4 of course.
Still had v3 open for comparing the two. So to ensure it is picked up:

Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

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 --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration
  2020-12-09 20:48 [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration Lino Sanfilippo
  2020-12-10 12:58 ` Uwe Kleine-König
@ 2020-12-10 17:21 ` Thierry Reding
  1 sibling, 0 replies; 3+ messages in thread
From: Thierry Reding @ 2020-12-10 17:21 UTC (permalink / raw)
  To: Lino Sanfilippo
  Cc: u.kleine-koenig, lee.jones, nsaenzjulienne, f.fainelli, rjui,
	sean, sbranden, bcm-kernel-feedback-list, linux-pwm,
	linux-rpi-kernel, linux-arm-kernel, linux-kernel

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

On Wed, Dec 09, 2020 at 09:48:25PM +0100, Lino Sanfilippo wrote:
> Use the newer .apply function of pwm_ops instead of .config, .enable,
> .disable and .set_polarity. This guarantees atomic changes of the pwm
> controller configuration. It also reduces the size of the driver.
> 
> Since now period is a 64 bit value, add an extra check to reject periods
> that exceed the possible max value for the 32 bit register.
> 
> This has been tested on a Raspberry PI 4.
> 
> Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
> ---
> 
> v4: Remove a superfluous blank line
>     Remove an unneeded cast (both requested by Uwe Kleine-König)
> v3: Check against period truncation (also based on a review by Uwe)
> v2: Fix compiler error for 64 bit builds
> 
>  drivers/pwm/pwm-bcm2835.c | 69 +++++++++++++++++------------------------------
>  1 file changed, 24 insertions(+), 45 deletions(-) j

Applied, thanks.

Thierry

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-10 17:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-09 20:48 [PATCH v4] pwm: bcm2835: Support apply function for atomic configuration Lino Sanfilippo
2020-12-10 12:58 ` Uwe Kleine-König
2020-12-10 17:21 ` Thierry Reding

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).