devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 1/3] hwmon: ltc2990: refactor value conversion
@ 2017-07-03  4:28 Tom Levens
  2017-07-03  4:28 ` [PATCH v3 2/3] hwmon: ltc2990: add devicetree binding Tom Levens
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Tom Levens @ 2017-07-03  4:28 UTC (permalink / raw)
  To: Rob Herring, Mark Rutland, Jean Delvare, Guenter Roeck,
	Mike Looijmans
  Cc: devicetree, linux-kernel, linux-hwmon

Conversion from raw values to signed integers has been refactored using
bitops.h. This also fixes a bug where negative temperatures were
converted incorrectly.

Signed-off-by: Tom Levens <tom.levens@cern.ch>
---
 drivers/hwmon/ltc2990.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/drivers/hwmon/ltc2990.c b/drivers/hwmon/ltc2990.c
index 8f8fe05..e320d21 100644
--- a/drivers/hwmon/ltc2990.c
+++ b/drivers/hwmon/ltc2990.c
@@ -11,6 +11,7 @@
  * the chip's internal temperature and Vcc power supply voltage.
  */
 
+#include <linux/bitops.h>
 #include <linux/err.h>
 #include <linux/hwmon.h>
 #include <linux/hwmon-sysfs.h>
@@ -34,15 +35,6 @@
 #define LTC2990_CONTROL_MODE_CURRENT	0x06
 #define LTC2990_CONTROL_MODE_VOLTAGE	0x07
 
-/* convert raw register value to sign-extended integer in 16-bit range */
-static int ltc2990_voltage_to_int(int raw)
-{
-	if (raw & BIT(14))
-		return -(0x4000 - (raw & 0x3FFF)) << 2;
-	else
-		return (raw & 0x3FFF) << 2;
-}
-
 /* Return the converted value from the given register in uV or mC */
 static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result)
 {
@@ -55,18 +47,16 @@ static int ltc2990_get_value(struct i2c_client *i2c, u8 reg, int *result)
 	switch (reg) {
 	case LTC2990_TINT_MSB:
 		/* internal temp, 0.0625 degrees/LSB, 13-bit  */
-		val = (val & 0x1FFF) << 3;
-		*result = (val * 1000) >> 7;
+		*result = sign_extend32(val, 12) * 1000 / 16;
 		break;
 	case LTC2990_V1_MSB:
 	case LTC2990_V3_MSB:
 		 /* Vx-Vy, 19.42uV/LSB. Depends on mode. */
-		*result = ltc2990_voltage_to_int(val) * 1942 / (4 * 100);
+		*result = sign_extend32(val, 14) * 1942 / 100;
 		break;
 	case LTC2990_VCC_MSB:
 		/* Vcc, 305.18μV/LSB, 2.5V offset */
-		*result = (ltc2990_voltage_to_int(val) * 30518 /
-			   (4 * 100 * 1000)) + 2500;
+		*result = sign_extend32(val, 14) * 30518 / (100 * 1000) + 2500;
 		break;
 	default:
 		return -EINVAL; /* won't happen, keep compiler happy */
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-07-12  0:39 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-03  4:28 [PATCH v3 1/3] hwmon: ltc2990: refactor value conversion Tom Levens
2017-07-03  4:28 ` [PATCH v3 2/3] hwmon: ltc2990: add devicetree binding Tom Levens
2017-07-07 14:24   ` Rob Herring
2017-07-03  4:29 ` [PATCH v3 3/3] hwmon: ltc2990: support all measurement modes Tom Levens
     [not found]   ` <1499056140-6064-3-git-send-email-tom.levens-vJEk5272eHo@public.gmane.org>
2017-07-03  6:29     ` Mike Looijmans
     [not found]       ` <341b5c67-c013-5a7c-54b5-e8c2a7c168e7-Oq418RWZeHk@public.gmane.org>
2017-07-03 17:09         ` Guenter Roeck
2017-07-11 22:00           ` Tom Levens
2017-07-04 22:45   ` kbuild test robot
2017-07-08 15:45 ` [v3,1/3] hwmon: ltc2990: refactor value conversion Guenter Roeck
     [not found]   ` <20170708154515.GA6358-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>
2017-07-11 22:03     ` Tom Levens
2017-07-12  0:39       ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).