]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
hwmon: (amc6821) Make reading and writing fan speed limits consistent
authorGuenter Roeck <linux@roeck-us.net>
Thu, 27 Jun 2024 00:46:04 +0000 (17:46 -0700)
committerGuenter Roeck <linux@roeck-us.net>
Mon, 8 Jul 2024 14:52:36 +0000 (07:52 -0700)
The default value of the maximum fan speed limit register is 0,
essentially translating to an unlimited fan speed. When reading
the limit, a value of 0 is reported in this case. However, writing
a value of 0 results in writing a value of 0xffff into the register,
which is inconsistent.

To solve the problem, permit writing a limit of 0 for the maximim fan
speed, effectively translating to "no limit". Write 0 into the register
if a limit value of 0 is written. Otherwise limit the range to
<1..6000000> and write 1..0xffff into the register. This ensures that
reading and writing from and to a limit register return the same value
while at the same time not changing reported values when reading the
speed or limits.

While at it, restrict fan limit writes to non-negative numbers; writing
a negative limit does not make sense and should be reported instead of
being corrected.

Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/amc6821.c

index dc35e9b21f912c344a73cdcad3f8447c764958ff..8e3cc33d8073c6b66f410e9b4d2e0bbbaffc8e97 100644 (file)
@@ -616,15 +616,20 @@ static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
 {
        struct amc6821_data *data = dev_get_drvdata(dev);
        struct i2c_client *client = data->client;
-       long val;
+       unsigned long val;
        int ix = to_sensor_dev_attr(attr)->index;
-       int ret = kstrtol(buf, 10, &val);
+       int ret = kstrtoul(buf, 10, &val);
        if (ret)
                return ret;
-       val = 1 > val ? 0xFFFF : 6000000/val;
+
+       /* The minimum fan speed must not be unlimited (0) */
+       if (ix == IDX_FAN1_MIN && !val)
+               return -EINVAL;
+
+       val = val > 0 ? 6000000 / clamp_val(val, 1, 6000000) : 0;
 
        mutex_lock(&data->update_lock);
-       data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
+       data->fan[ix] = clamp_val(val, 0, 0xFFFF);
        if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
                        data->fan[ix] & 0xFF)) {
                dev_err(&client->dev, "Register write error, aborting.\n");