Skip to content

Commit

Permalink
hwmon: (ad7314) Do proper sign extension
Browse files Browse the repository at this point in the history
The comment above (data << 2) >> 2 explains what the intention is: To
use bit 13 of the 14-bit value data as the sign bit. However, this
doesn't work due to C's promotion rules. data has type s16, but data
<< 2 has type int. To get sign extension, that expression would have
to be cast back to an s16 before being shifted (at which point C's
promotion rules would then kick in again and promote the left operand
to int). As it stands, both expressions are no-ops for any value of
data.

Avoid these subtleties by using the existing API for
this. sign_extend32 works equally well for 8 and 16 bit types.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
  • Loading branch information
Rasmus Villemoes authored and Guenter Roeck committed Jan 26, 2015
1 parent a14c707 commit 984faa1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions drivers/hwmon/ad7314.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/bitops.h>

/*
* AD7314 temperature masks
Expand Down Expand Up @@ -67,7 +68,7 @@ static ssize_t ad7314_show_temperature(struct device *dev,
switch (spi_get_device_id(chip->spi_dev)->driver_data) {
case ad7314:
data = (ret & AD7314_TEMP_MASK) >> AD7314_TEMP_SHIFT;
data = (data << 6) >> 6;
data = sign_extend32(data, 9);

return sprintf(buf, "%d\n", 250 * data);
case adt7301:
Expand All @@ -78,7 +79,7 @@ static ssize_t ad7314_show_temperature(struct device *dev,
* register. 1lsb - 31.25 milli degrees centigrade
*/
data = ret & ADT7301_TEMP_MASK;
data = (data << 2) >> 2;
data = sign_extend32(data, 13);

return sprintf(buf, "%d\n",
DIV_ROUND_CLOSEST(data * 3125, 100));
Expand Down

0 comments on commit 984faa1

Please sign in to comment.