diff mbox series

[v2,14/14] hwmon: (ina2xx) Use shunt voltage to calculate current

Message ID 20240830010554.1462861-15-linux@roeck-us.net (mailing list archive)
State Accepted
Headers show
Series hwmon: (ina2xx) Cleanup and convert to use with_info API | expand

Commit Message

Guenter Roeck Aug. 30, 2024, 1:05 a.m. UTC
Since the shunt voltage and the current register report the same values
when the chip is calibrated, we can calculate the current directly
from the shunt voltage without relying on chip calibration.

With this change, the current register is no longer accessed. Its
register address is only used to indicate if reading or writing
current or shunt voltage is desired when accessing registers.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v2: New patch

 drivers/hwmon/ina2xx.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

Comments

Tzung-Bi Shih Aug. 30, 2024, 12:30 p.m. UTC | #1
On Thu, Aug 29, 2024 at 06:05:54PM -0700, Guenter Roeck wrote:
> Since the shunt voltage and the current register report the same values
> when the chip is calibrated, we can calculate the current directly
> from the shunt voltage without relying on chip calibration.
> 
> With this change, the current register is no longer accessed. Its
> register address is only used to indicate if reading or writing
> current or shunt voltage is desired when accessing registers.
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>

Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
diff mbox series

Patch

diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c
index 10c8c475c634..f0fa6d073627 100644
--- a/drivers/hwmon/ina2xx.c
+++ b/drivers/hwmon/ina2xx.c
@@ -480,6 +480,8 @@  static int ina2xx_curr_read(struct device *dev, u32 attr, long *val)
 {
 	struct ina2xx_data *data = dev_get_drvdata(dev);
 	struct regmap *regmap = data->regmap;
+	unsigned int regval;
+	int ret;
 
 	/*
 	 * While the chips supported by this driver do not directly support
@@ -492,7 +494,17 @@  static int ina2xx_curr_read(struct device *dev, u32 attr, long *val)
 	 */
 	switch (attr) {
 	case hwmon_curr_input:
-		return ina2xx_read_init(dev, INA2XX_CURRENT, val);
+		/*
+		 * Since the shunt voltage and the current register report the
+		 * same values when the chip is calibrated, we can calculate
+		 * the current directly from the shunt voltage without relying
+		 * on chip calibration.
+		 */
+		ret = regmap_read(regmap, INA2XX_SHUNT_VOLTAGE, &regval);
+		if (ret)
+			return ret;
+		*val = ina2xx_get_value(data, INA2XX_CURRENT, regval);
+		return 0;
 	case hwmon_curr_lcrit:
 		return ina226_alert_limit_read(data, INA226_SHUNT_UNDER_VOLTAGE_MASK,
 					       INA2XX_CURRENT, val);