Skip to content

Commit 8f4fd97

Browse files
committed
hwmon: (amc6821) Make reading and writing fan speed limits consistent
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 <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent af4d04b commit 8f4fd97

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

drivers/hwmon/amc6821.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,20 @@ static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
616616
{
617617
struct amc6821_data *data = dev_get_drvdata(dev);
618618
struct i2c_client *client = data->client;
619-
long val;
619+
unsigned long val;
620620
int ix = to_sensor_dev_attr(attr)->index;
621-
int ret = kstrtol(buf, 10, &val);
621+
int ret = kstrtoul(buf, 10, &val);
622622
if (ret)
623623
return ret;
624-
val = 1 > val ? 0xFFFF : 6000000/val;
624+
625+
/* The minimum fan speed must not be unlimited (0) */
626+
if (ix == IDX_FAN1_MIN && !val)
627+
return -EINVAL;
628+
629+
val = val > 0 ? 6000000 / clamp_val(val, 1, 6000000) : 0;
625630

626631
mutex_lock(&data->update_lock);
627-
data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
632+
data->fan[ix] = clamp_val(val, 0, 0xFFFF);
628633
if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
629634
data->fan[ix] & 0xFF)) {
630635
dev_err(&client->dev, "Register write error, aborting.\n");

0 commit comments

Comments
 (0)