Linux PWM subsystem development
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <ukleinek@kernel.org>
To: keguang.zhang@gmail.com
Cc: Binbin Zhou <zhoubinbin@loongson.cn>,
	linux-pwm@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 3/3] pwm: loongson: Use field helpers for period validation
Date: Mon, 30 Nov 2026 17:03:35 +0100	[thread overview]
Message-ID: <aw2cpsSG3p0uCsL4@monoceros> (raw)
In-Reply-To: <20260920-pwm-loongson-fix-v4-3-41b36b3c6497@gmail.com>

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

On Sun, Sep 20, 2026 at 08:02:08PM +0800, Keguang Zhang via B4 Relay wrote:
> From: Keguang Zhang <keguang.zhang@gmail.com>
> 
> Use FIELD_FIT() to validate the calculated period value and FIELD_MAX()
> to get the maximum value allowed by the 32-bit period field.
> 
> Signed-off-by: Keguang Zhang <keguang.zhang@gmail.com>
> ---
>  drivers/pwm/pwm-loongson.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-loongson.c b/drivers/pwm/pwm-loongson.c
> index 977d867bdb8a..178a7a69a11c 100644
> --- a/drivers/pwm/pwm-loongson.c
> +++ b/drivers/pwm/pwm-loongson.c
> @@ -22,6 +22,7 @@
>   */
>  
>  #include <linux/acpi.h>
> +#include <linux/bitfield.h>
>  #include <linux/clk.h>
>  #include <linux/device.h>
>  #include <linux/init.h>
> @@ -37,6 +38,8 @@
>  #define LOONGSON_PWM_REG_PERIOD		0x8 /* Pulse Period Buffer Register */
>  #define LOONGSON_PWM_REG_CTRL		0xc /* Control Register */
>  
> +#define LOONGSON_PWM_PERIOD_MASK	GENMASK(31, 0)
> +
>  /* Control register bits */
>  #define LOONGSON_PWM_CTRL_REG_EN	BIT(0)  /* Counter Enable Bit */
>  #define LOONGSON_PWM_CTRL_REG_OE	BIT(3)  /* Pulse Output Enable Control Bit, Valid Low */
> @@ -133,8 +136,8 @@ static int pwm_loongson_config(struct pwm_chip *chip, struct pwm_device *pwm,
>  
>  	/* period = period_ns * ddata->clk_rate / NSEC_PER_SEC */
>  	period = mul_u64_u64_div_u64(period_ns, ddata->clk_rate, NSEC_PER_SEC);
> -	if (period > U32_MAX)
> -		period = U32_MAX;
> +	if (!FIELD_FIT(LOONGSON_PWM_PERIOD_MASK, period))
> +		period = FIELD_MAX(LOONGSON_PWM_PERIOD_MASK);

I'm not sure I consider that an improvement. If only a part of
LOONGSON_PWM_REG_PERIOD was available for the period, I'd agree (and
request to rename LOONGSON_PWM_PERIOD_MASK to
LOONGSON_PWM_REG_PERIOD_PERIOD or similar).

While studying the reference manual I also found "Before writing a new
number to the pulse width register, you should clear the EN bit in the
control register and then set the EN bit to 1 after writing the new
number." which the driver doesn't honor. Also I wonder if the RST bit
should be used when modifying the period (or how the hardware behaves if
the internal counter is already higher than the newly configured
period).

Best regards
Uwe

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

  reply	other threads:[~2026-11-30 16:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-20 12:02 [PATCH v4 0/3] pwm: loongson: Fix PWM configuration handling Keguang Zhang via B4 Relay
2026-09-20 12:02 ` [PATCH v4 1/3] pwm: loongson: Fix low pulse buffer register handling Keguang Zhang via B4 Relay
2026-11-30 15:55   ` Uwe Kleine-König
2026-09-20 12:02 ` [PATCH v4 2/3] pwm: loongson: Reset the counters before enabling PWM Keguang Zhang via B4 Relay
2026-11-30 16:10   ` Uwe Kleine-König
2026-09-20 12:02 ` [PATCH v4 3/3] pwm: loongson: Use field helpers for period validation Keguang Zhang via B4 Relay
2026-11-30 16:03   ` Uwe Kleine-König [this message]
2026-11-30 16:04     ` 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=aw2cpsSG3p0uCsL4@monoceros \
    --to=ukleinek@kernel.org \
    --cc=keguang.zhang@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=zhoubinbin@loongson.cn \
    /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