Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Subject: [PATCH 1/6] hwmon: (max1619) Clamp temperature range when writing limits
Date: Sat, 27 Jul 2024 07:38:15 -0700	[thread overview]
Message-ID: <20240727143820.1358225-2-linux@roeck-us.net> (raw)
In-Reply-To: <20240727143820.1358225-1-linux@roeck-us.net>

Module test code reports underflows when writing sensor limits.

temp2_min: Suspected underflow: [min=-77000, read 101000, written -2147483648]
temp2_max: Suspected underflow: [min=-77000, read 101000, written -2147483648]
temp2_crit: Suspected underflow: [min=-77000, read 101000, written -2147483648]

Clamp temperature ranges when writing limits to fix the problem.
While at it, use sign_extend32() when reading temperatures to make
the code easier to understand.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/max1619.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/drivers/hwmon/max1619.c b/drivers/hwmon/max1619.c
index a89a519cf5d9..464f4c838394 100644
--- a/drivers/hwmon/max1619.c
+++ b/drivers/hwmon/max1619.c
@@ -52,16 +52,6 @@ static const unsigned short normal_i2c[] = {
  * Conversions
  */
 
-static int temp_from_reg(int val)
-{
-	return (val & 0x80 ? val-0x100 : val) * 1000;
-}
-
-static int temp_to_reg(int val)
-{
-	return (val < 0 ? val+0x100*1000 : val) / 1000;
-}
-
 enum temp_index {
 	t_input1 = 0,
 	t_input2,
@@ -142,7 +132,7 @@ static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 	struct max1619_data *data = max1619_update_device(dev);
 
-	return sprintf(buf, "%d\n", temp_from_reg(data->temp[attr->index]));
+	return sprintf(buf, "%d\n", sign_extend(data->temp[attr->index], 7) * 1000);
 }
 
 static ssize_t temp_store(struct device *dev,
@@ -158,7 +148,7 @@ static ssize_t temp_store(struct device *dev,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->temp[attr->index] = temp_to_reg(val);
+	data->temp[attr->index] = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
 	i2c_smbus_write_byte_data(client, regs_write[attr->index],
 				  data->temp[attr->index]);
 	mutex_unlock(&data->update_lock);
-- 
2.39.2


  reply	other threads:[~2024-07-27 14:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-27 14:38 [PATCH 1/6] hwmon: (max1619) Modernize driver Guenter Roeck
2024-07-27 14:38 ` Guenter Roeck [this message]
2024-07-28  4:25   ` [PATCH 1/6] hwmon: (max1619) Clamp temperature range when writing limits Tzung-Bi Shih
2024-07-27 14:38 ` [PATCH 2/6] hwmon: (max1619) Reorder include files to alphabetic order Guenter Roeck
2024-07-28  4:25   ` Tzung-Bi Shih
2024-07-27 14:38 ` [PATCH 3/6] hwmon: (max1619) Convert to use regmap Guenter Roeck
2024-07-28  4:26   ` Tzung-Bi Shih
2024-07-28  5:22     ` Guenter Roeck
2024-07-28 12:17       ` Tzung-Bi Shih
2024-07-27 14:38 ` [PATCH 4/6] hwmon: (max1619) Convert to with_info API Guenter Roeck
2024-07-28  4:26   ` Tzung-Bi Shih
2024-07-27 14:38 ` [PATCH 5/6] hwmon: (max1619) Add support for update_interval attribute Guenter Roeck
2024-07-28  4:26   ` Tzung-Bi Shih
2024-07-27 14:38 ` [PATCH 6/6] hwmon: (max1619) Improve chip detection code Guenter Roeck
2024-07-28  4:26   ` Tzung-Bi Shih

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=20240727143820.1358225-2-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=linux-hwmon@vger.kernel.org \
    /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