[PATCH] hwmon: (lm63) Clamp PWM frequency multiplier to [1, 31]

Guenter Roeck <[email protected]> Mon, 27 Jul 2026 13:21:20 -0700
Newsgroups org.kernel.vger.linux-hwmon
Message-ID <[email protected]>
Sashiko is concerned that reading a PWM frequency multiplier outside
the supported range of [1, 31] might result in bad PWM values written
to the chip. Technically, the chip should never return a value with
the upper 3 bits set, so this should never happen. However, it is
unknown if there are LM63 variants where the upper bits of the register
can be written.

Clamp the PWM frequency multiplier value to [1, 31] when reading it
from the chip to avoid the problem.

Reported-by: Sashiko <[email protected]>
Signed-off-by: Guenter Roeck <[email protected]>
---
 drivers/hwmon/lm63.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/hwmon/lm63.c b/drivers/hwmon/lm63.c
index e2a429e579ac..45a0efa8a39d 100644
--- a/drivers/hwmon/lm63.c
+++ b/drivers/hwmon/lm63.c
@@ -32,6 +32,7 @@
 #include <linux/hwmon-sysfs.h>
 #include <linux/hwmon.h>
 #include <linux/err.h>
+#include <linux/minmax.h>
 #include <linux/mutex.h>
 #include <linux/of.h>
 #include <linux/sysfs.h>
@@ -249,8 +250,7 @@ static struct lm63_data *lm63_update_device(struct device *dev)
 
 		data->pwm1_freq = i2c_smbus_read_byte_data(client,
 				  LM63_REG_PWM_FREQ);
-		if (data->pwm1_freq == 0)
-			data->pwm1_freq = 1;
+		data->pwm1_freq = clamp(data->pwm1_freq, 1, 31);
 		data->pwm1[0] = i2c_smbus_read_byte_data(client,
 				LM63_REG_PWM_VALUE);
 
@@ -1188,8 +1188,7 @@ static void lm63_init_client(struct lm63_data *data)
 
 	/* We may need pwm1_freq before ever updating the client data */
 	data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ);
-	if (data->pwm1_freq == 0)
-		data->pwm1_freq = 1;
+	data->pwm1_freq = clamp(data->pwm1_freq, 1, 31);
 
 	switch (data->kind) {
 	case lm63:
-- 
2.45.2