Commit c5794cfac09a585945e1632451900594db19393b
Committed by
Guenter Roeck
1 parent
17296feb3c
Exists in
master
and in
6 other branches
hwmon: (w83627ehf) Better fix for negative temperature values
It is more efficient to left-align 8-bit temperature values, so that 8-bit and 9-bit temperature values can be handled exactly the same way in the rest of the code. Signed-off-by: Jean Delvare <khali@linux-fr.org> Cc: Guenter Roeck <guenter.roeck@ericsson.com> Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com>
Showing 1 changed file with 26 additions and 25 deletions Side-by-side Diff
drivers/hwmon/w83627ehf.c
... | ... | @@ -388,23 +388,6 @@ |
388 | 388 | return 1 << reg; |
389 | 389 | } |
390 | 390 | |
391 | -static inline int | |
392 | -temp_from_reg(u16 reg, s16 regval) | |
393 | -{ | |
394 | - if (is_word_sized(reg)) | |
395 | - return LM75_TEMP_FROM_REG(regval); | |
396 | - return ((s8)regval) * 1000; | |
397 | -} | |
398 | - | |
399 | -static inline u16 | |
400 | -temp_to_reg(u16 reg, long temp) | |
401 | -{ | |
402 | - if (is_word_sized(reg)) | |
403 | - return LM75_TEMP_TO_REG(temp); | |
404 | - return (s8)DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, -127000, 128000), | |
405 | - 1000); | |
406 | -} | |
407 | - | |
408 | 391 | /* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */ |
409 | 392 | |
410 | 393 | static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 }; |
... | ... | @@ -561,6 +544,26 @@ |
561 | 544 | return 0; |
562 | 545 | } |
563 | 546 | |
547 | +/* We left-align 8-bit temperature values to make the code simpler */ | |
548 | +static u16 w83627ehf_read_temp(struct w83627ehf_data *data, u16 reg) | |
549 | +{ | |
550 | + u16 res; | |
551 | + | |
552 | + res = w83627ehf_read_value(data, reg); | |
553 | + if (!is_word_sized(reg)) | |
554 | + res <<= 8; | |
555 | + | |
556 | + return res; | |
557 | +} | |
558 | + | |
559 | +static int w83627ehf_write_temp(struct w83627ehf_data *data, u16 reg, | |
560 | + u16 value) | |
561 | +{ | |
562 | + if (!is_word_sized(reg)) | |
563 | + value >>= 8; | |
564 | + return w83627ehf_write_value(data, reg, value); | |
565 | +} | |
566 | + | |
564 | 567 | /* This function assumes that the caller holds data->update_lock */ |
565 | 568 | static void nct6775_write_fan_div(struct w83627ehf_data *data, int nr) |
566 | 569 | { |
567 | 570 | |
568 | 571 | |
... | ... | @@ -862,15 +865,15 @@ |
862 | 865 | for (i = 0; i < NUM_REG_TEMP; i++) { |
863 | 866 | if (!(data->have_temp & (1 << i))) |
864 | 867 | continue; |
865 | - data->temp[i] = w83627ehf_read_value(data, | |
868 | + data->temp[i] = w83627ehf_read_temp(data, | |
866 | 869 | data->reg_temp[i]); |
867 | 870 | if (data->reg_temp_over[i]) |
868 | 871 | data->temp_max[i] |
869 | - = w83627ehf_read_value(data, | |
872 | + = w83627ehf_read_temp(data, | |
870 | 873 | data->reg_temp_over[i]); |
871 | 874 | if (data->reg_temp_hyst[i]) |
872 | 875 | data->temp_max_hyst[i] |
873 | - = w83627ehf_read_value(data, | |
876 | + = w83627ehf_read_temp(data, | |
874 | 877 | data->reg_temp_hyst[i]); |
875 | 878 | } |
876 | 879 | |
... | ... | @@ -1166,8 +1169,7 @@ |
1166 | 1169 | struct sensor_device_attribute *sensor_attr = \ |
1167 | 1170 | to_sensor_dev_attr(attr); \ |
1168 | 1171 | int nr = sensor_attr->index; \ |
1169 | - return sprintf(buf, "%d\n", \ | |
1170 | - temp_from_reg(data->addr[nr], data->reg[nr])); \ | |
1172 | + return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->reg[nr])); \ | |
1171 | 1173 | } |
1172 | 1174 | show_temp_reg(reg_temp, temp); |
1173 | 1175 | show_temp_reg(reg_temp_over, temp_max); |
... | ... | @@ -1188,9 +1190,8 @@ |
1188 | 1190 | if (err < 0) \ |
1189 | 1191 | return err; \ |
1190 | 1192 | mutex_lock(&data->update_lock); \ |
1191 | - data->reg[nr] = temp_to_reg(data->addr[nr], val); \ | |
1192 | - w83627ehf_write_value(data, data->addr[nr], \ | |
1193 | - data->reg[nr]); \ | |
1193 | + data->reg[nr] = LM75_TEMP_TO_REG(val); \ | |
1194 | + w83627ehf_write_temp(data, data->addr[nr], data->reg[nr]); \ | |
1194 | 1195 | mutex_unlock(&data->update_lock); \ |
1195 | 1196 | return count; \ |
1196 | 1197 | } |