Skip to content

Commit a50d9a4

Browse files
committed
hwmon: (ds1621) Fix temperature rounding operations
Commit "hwmon: (ds1621) Add ds1721 chip support" broke rounding for chips or configurations with less than 12 bit resolution. Tested-by: Jean Delvare <[email protected]> Acked-by: Jean Delvare <[email protected]> Signed-off-by: Guenter Roeck <[email protected]>
1 parent 41fa9a9 commit a50d9a4

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

drivers/hwmon/ds1621.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ struct ds1621_data {
128128

129129
u16 temp[3]; /* Register values, word */
130130
u8 conf; /* Register encoding, combined */
131+
u8 zbits; /* Resolution encoded as number of
132+
* zero bits */
131133
u16 update_interval; /* Conversion rate in milliseconds */
132134
};
133135

@@ -139,16 +141,14 @@ static inline int DS1621_TEMP_FROM_REG(u16 reg)
139141
/*
140142
* TEMP: 0.001C/bit (-55C to +125C)
141143
* REG:
142-
* - 1621, 1625: 0.5C/bit
143-
* - 1631, 1721, 1731: 0.0625C/bit
144-
* Assume highest resolution and let the bits fall where they may..
144+
* - 1621, 1625: 0.5C/bit, 7 zero-bits
145+
* - 1631, 1721, 1731: 0.0625C/bit, 4 zero-bits
145146
*/
146-
static inline u16 DS1621_TEMP_TO_REG(long temp)
147+
static inline u16 DS1621_TEMP_TO_REG(long temp, u8 zbits)
147148
{
148-
int ntemp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
149-
ntemp += (ntemp < 0 ? -31 : 31);
150-
ntemp = DIV_ROUND_CLOSEST(ntemp * 10, 625) << 4;
151-
return (u16)ntemp;
149+
temp = clamp_val(temp, DS1621_TEMP_MIN, DS1621_TEMP_MAX);
150+
temp = DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
151+
return temp;
152152
}
153153

154154
static void ds1621_init_client(struct i2c_client *client)
@@ -172,6 +172,7 @@ static void ds1621_init_client(struct i2c_client *client)
172172
switch (data->kind) {
173173
case ds1625:
174174
data->update_interval = DS1625_CONVERSION_MAX;
175+
data->zbits = 7;
175176
sreg = DS1621_COM_START;
176177
break;
177178
case ds1631:
@@ -180,10 +181,12 @@ static void ds1621_init_client(struct i2c_client *client)
180181
resol = (new_conf & DS1621_REG_CONFIG_RESOL) >>
181182
DS1621_REG_CONFIG_RESOL_SHIFT;
182183
data->update_interval = ds1721_convrates[resol];
184+
data->zbits = 7 - resol;
183185
sreg = DS1721_COM_START;
184186
break;
185187
default:
186188
data->update_interval = DS1621_CONVERSION_MAX;
189+
data->zbits = 7;
187190
sreg = DS1621_COM_START;
188191
break;
189192
}
@@ -254,7 +257,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,
254257
return err;
255258

256259
mutex_lock(&data->update_lock);
257-
data->temp[attr->index] = DS1621_TEMP_TO_REG(val);
260+
data->temp[attr->index] = DS1621_TEMP_TO_REG(val, data->zbits);
258261
i2c_smbus_write_word_swapped(client, DS1621_REG_TEMP[attr->index],
259262
data->temp[attr->index]);
260263
mutex_unlock(&data->update_lock);

0 commit comments

Comments
 (0)