From: Chris Packham <chris.packham@alliedtelesis.co.nz>
To: jdelvare@suse.com, linux@roeck-us.net,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Michael.Wang@alliedtelesis.co.nz,
Chris Packham <chris.packham@alliedtelesis.co.nz>
Subject: [PATCHv1] hwmon: adm9240: handle temperature readings below 0
Date: Tue, 4 Oct 2016 17:08:00 +1300 [thread overview]
Message-ID: <20161004040800.9689-1-chris.packham@alliedtelesis.co.nz> (raw)
Unlike the temperature thresholds the temperature data is a 9-bit signed
value. This allows and additional 0.5 degrees of precision on the
reading but means we can't rely on sign-extension to handle negative
values.
Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
---
drivers/hwmon/adm9240.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c
index 98114ce..6929b25 100644
--- a/drivers/hwmon/adm9240.c
+++ b/drivers/hwmon/adm9240.c
@@ -107,6 +107,21 @@ static inline s8 TEMP_TO_REG(long val)
return clamp_val(SCALE(val, 1, 1000), -40, 127);
}
+/* 8-bit signed temperature (from Thigh, Tlow etc) */
+static inline int TEMP_FROM_REG(s8 val)
+{
+ return val * 1000;
+}
+
+/* 9-bit signed temperature (from temperature data) */
+static inline int TEMP_FROM_DATA_REG(s16 val)
+{
+ if (val > 255)
+ val -= 512;
+
+ return val * 500;
+}
+
/* two fans, each with low fan speed limit */
static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
{
@@ -263,7 +278,7 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
char *buf)
{
struct adm9240_data *data = adm9240_update_device(dev);
- return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
+ return sprintf(buf, "%d\n", TEMP_FROM_DATA_REG(data->temp));
}
static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
@@ -271,7 +286,7 @@ static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
struct adm9240_data *data = adm9240_update_device(dev);
- return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
+ return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[attr->index]));
}
static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
--
2.10.0
next reply other threads:[~2016-10-04 4:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-04 4:08 Chris Packham [this message]
2016-10-04 20:53 ` [PATCHv1] hwmon: adm9240: handle temperature readings below 0 Guenter Roeck
2016-10-04 21:09 ` Chris Packham
2016-10-04 22:09 ` Guenter Roeck
2016-10-05 5:00 ` adm9240 error handling (was Re: [PATCHv1] hwmon: adm9240: handle temperature readings below 0) Chris Packham
2016-10-05 13:23 ` Guenter Roeck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161004040800.9689-1-chris.packham@alliedtelesis.co.nz \
--to=chris.packham@alliedtelesis.co.nz \
--cc=Michael.Wang@alliedtelesis.co.nz \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.