From: Michael Harris <michaelharriscode@gmail.com>
To: Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Jonathan Cameron <jic23@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
linux-iio@vger.kernel.org, linux-staging@lists.linux.dev,
linux-kernel@vger.kernel.org,
"Michael Harris" <michaelharriscode@gmail.com>
Subject: [PATCH v2 1/4] staging: iio: adt7316: refactor temperature calculation logic
Date: Thu, 05 Mar 2026 23:16:58 -0800 [thread overview]
Message-ID: <20260305-adt7316-correct-macros-v2-1-3702e3841c42@gmail.com> (raw)
In-Reply-To: <20260305-adt7316-correct-macros-v2-0-3702e3841c42@gmail.com>
Replace the manual sign manipulation with sign_extend32() and change the
affected variable from u16 to s32 to properly handle negative values.
Resolve a logic error where the sign bit was being checked at bit 10
instead of bit 9 for a 10-bit value.
Convert the data variable to be in centidegrees celsius (0.25 C per bit)
so we can use simple division and modulo for sysfs_emit() instead of the
convoluted bit shifting and masking.
Signed-off-by: Michael Harris <michaelharriscode@gmail.com>
---
drivers/staging/iio/addac/adt7316.c | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/drivers/staging/iio/addac/adt7316.c b/drivers/staging/iio/addac/adt7316.c
index 8a9a8262c2bec34f3c3e79d8174f492b9a23fb70..1412808c50c76a68b5771a25c46dd3308c5cbcdb 100644
--- a/drivers/staging/iio/addac/adt7316.c
+++ b/drivers/staging/iio/addac/adt7316.c
@@ -155,9 +155,12 @@
* ADT7316 value masks
*/
#define ADT7316_VALUE_MASK 0xfff
-#define ADT7316_T_VALUE_SIGN 0x400
#define ADT7316_T_VALUE_FLOAT_OFFSET 2
-#define ADT7316_T_VALUE_FLOAT_MASK 0x2
+
+/*
+ * ADT7316 hardware constants
+ */
+#define ADT7316_TEMP_CENTIDEG_PER_BIT 25
/*
* Chip ID
@@ -1090,9 +1093,8 @@ static IIO_DEVICE_ATTR(DAC_internal_Vref, 0644,
static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
int channel, char *buf)
{
- u16 data;
+ s32 data;
u8 msb, lsb;
- char sign = ' ';
int ret;
if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
@@ -1151,15 +1153,13 @@ static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
break;
}
- if (data & ADT7316_T_VALUE_SIGN) {
- /* convert supplement to positive value */
- data = (ADT7316_T_VALUE_SIGN << 1) - data;
- sign = '-';
- }
+ data = sign_extend32(data, 9);
+ data *= ADT7316_TEMP_CENTIDEG_PER_BIT;
- return sysfs_emit(buf, "%c%d.%.2d\n", sign,
- (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
- (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
+ return sysfs_emit(buf, "%s%d.%02u\n",
+ (data < 0 ? "-" : ""),
+ abs(data / 100),
+ abs(data % 100));
}
static ssize_t adt7316_show_VDD(struct device *dev,
--
2.53.0
next prev parent reply other threads:[~2026-03-06 7:17 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-06 7:16 [PATCH v2 0/4] correct macro issues Michael Harris
2026-03-06 7:16 ` Michael Harris [this message]
2026-03-06 14:51 ` [PATCH v2 1/4] staging: iio: adt7316: refactor temperature calculation logic Andy Shevchenko
2026-03-07 11:33 ` Jonathan Cameron
2026-03-06 7:16 ` [PATCH v2 2/4] staging: iio: adt7316: remove shift/offset macros Michael Harris
2026-03-06 14:53 ` Andy Shevchenko
2026-03-10 1:32 ` Michael Harris
2026-03-10 11:29 ` Andy Shevchenko
2026-03-07 11:30 ` Jonathan Cameron
2026-03-06 7:17 ` [PATCH v2 3/4] staging: iio: adt7316: add config names to registers and reorder Michael Harris
2026-03-06 15:04 ` Andy Shevchenko
2026-03-06 7:17 ` [PATCH v2 4/4] staging: iio: adt7316: convert magic numbers to BIT_U32() or GENMASK_U32() Michael Harris
2026-03-06 15:05 ` Andy Shevchenko
2026-03-07 11:33 ` Jonathan Cameron
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=20260305-adt7316-correct-macros-v2-1-3702e3841c42@gmail.com \
--to=michaelharriscode@gmail.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=gregkh@linuxfoundation.org \
--cc=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=nuno.sa@analog.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox