[PATCH v2] pwm: bcm2835: Support apply function for atomic configuration

Uwe Kleine-König u.kleine-koenig at pengutronix.de
Sun Nov 29 13:10:50 EST 2020


On Sat, Nov 28, 2020 at 01:02:06PM +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.
> 
> This has been tested on a Raspberry PI 4.
> 
> v2: Fixed compiler error

Changelog between review rounds go to below the tripple-dash below.

> Signed-off-by: Lino Sanfilippo <LinoSanfilippo at gmx.de>
> ---
>  drivers/pwm/pwm-bcm2835.c | 64 ++++++++++++++++-------------------------------
>  1 file changed, 21 insertions(+), 43 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-bcm2835.c b/drivers/pwm/pwm-bcm2835.c
> index 6841dcf..dad7443 100644
> --- a/drivers/pwm/pwm-bcm2835.c
> +++ b/drivers/pwm/pwm-bcm2835.c
> @@ -58,13 +58,14 @@ 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 scaler;
> -	u32 period;
> +	u32 value;
>  
>  	if (!rate) {
>  		dev_err(pc->dev, "failed to get clock rate\n");
> @@ -72,65 +73,42 @@ 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 */
> +	value = DIV_ROUND_CLOSEST_ULL(state->period, scaler);

You're storing an unsigned long long (i.e. 64 bits) in an u32. If you
are sure that this won't discard relevant bits, please explain this in a
comment for the cursory reader.

Also note that round_closed is probably wrong, as .apply() is supposed
to round down the period to the next achievable period. (But fixing this
has to do done in a separate patch.)

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20201129/934b720e/attachment-0001.sig>


More information about the linux-arm-kernel mailing list