lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250121-potin-ina238-shunt-voltage-scaling-v1-1-36d5dfe027f5@gmail.com>
Date: Tue, 21 Jan 2025 00:23:25 +0800
From: Potin Lai <potin.lai.pt@...il.com>
To: Jean Delvare <jdelvare@...e.com>, Guenter Roeck <linux@...ck-us.net>, 
 Patrick Williams <patrick@...cx.xyz>
Cc: linux-hwmon@...r.kernel.org, linux-kernel@...r.kernel.org, 
 Cosmo Chou <cosmo.chou@...ntatw.com>, Potin Lai <potin.lai@...ntatw.com>, 
 Potin Lai <potin.lai.pt@...il.com>
Subject: [PATCH] hwmon: ina238: Add support for shunt voltage scaling

The INA238 sensor reports shunt voltage with microvolt precision.
However, the hwmon driver currently exposes this value only in
millivolts via `in0_input`, which results in a loss of precision for
readings within the range of ±1 mV.

This patch introduces an `in0_scale` attribute to provide the scaling
factor applied to the shunt voltage reading. By exposing this attribute,
users can accurately interpret the in0_input values in microvolts,
preserving the sensor's full precision.

Signed-off-by: Potin Lai <potin.lai.pt@...il.com>
---
 drivers/hwmon/ina238.c | 45 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 42 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
index 2d9f12f68d50..58737a0703dc 100644
--- a/drivers/hwmon/ina238.c
+++ b/drivers/hwmon/ina238.c
@@ -8,6 +8,7 @@
 
 #include <linux/err.h>
 #include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
@@ -108,8 +109,42 @@ struct ina238_data {
 	struct regmap *regmap;
 	u32 rshunt;
 	int gain;
+	long shunt_volt_scale;
 };
 
+static ssize_t shunt_volt_scale_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct ina238_data *data = dev_get_drvdata(dev);
+
+	return sprintf(buf, "%d\n", data->shunt_volt_scale);
+}
+
+static ssize_t shunt_volt_scale_store(struct device *dev,
+				      struct device_attribute *attr,
+				      const char *buf, size_t count)
+{
+	struct ina238_data *data = dev_get_drvdata(dev);
+	long val;
+	int err;
+
+	err = kstrtol(buf, 10, &val);
+	if (err)
+		return err;
+
+	data->shunt_volt_scale = val;
+	return count;
+}
+
+static SENSOR_DEVICE_ATTR_RW(in0_scale, shunt_volt_scale, 0);
+
+static struct attribute *ina238_attrs[] = {
+	&sensor_dev_attr_in0_scale.dev_attr.attr,
+	NULL,
+};
+
+ATTRIBUTE_GROUPS(ina238);
+
 static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
 {
 	u8 data[3];
@@ -197,8 +232,9 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel,
 		regval = (s16)regval;
 		if (channel == 0)
 			/* gain of 1 -> LSB / 4 */
-			*val = (regval * INA238_SHUNT_VOLTAGE_LSB) /
-			       (1000 * (4 - data->gain + 1));
+			*val = (regval * INA238_SHUNT_VOLTAGE_LSB *
+				data->shunt_volt_scale) /
+				(1000 * (4 - data->gain + 1));
 		else
 			*val = (regval * INA238_BUS_VOLTAGE_LSB) / 1000;
 		break;
@@ -603,9 +639,12 @@ static int ina238_probe(struct i2c_client *client)
 		return -ENODEV;
 	}
 
+	/* Setup default shunt voltage scale */
+	data->shunt_volt_scale = 1;
+
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
 							 &ina238_chip_info,
-							 NULL);
+							 ina238_groups);
 	if (IS_ERR(hwmon_dev))
 		return PTR_ERR(hwmon_dev);
 

---
base-commit: fc033cf25e612e840e545f8d5ad2edd6ba613ed5
change-id: 20250121-potin-ina238-shunt-voltage-scaling-8a3f9dc3d2b8

Best regards,
-- 
Potin Lai <potin.lai.pt@...il.com>


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ