[PATCH v4 3/3] pwm: loongson: Use field helpers for period validation
From: Keguang Zhang via B4 Relay
Date: Sun Sep 20 2026 - 08:07:40 EST
From: Keguang Zhang <keguang.zhang@xxxxxxxxx>
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@xxxxxxxxx>
---
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);
/* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
duty = mul_u64_u64_div_u64_roundup(duty_ns, ddata->clk_rate, NSEC_PER_SEC);
--
2.43.0