mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ferdinand Schwenk via B4 Relay <devnull+ferdinand.schwenk.advastore.com@kernel.org>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ferdinand Schwenk <ferdinand.schwenk@advastore.com>,
	 richard.leitner@linux.dev
Subject: [PATCH 2/2] hwmon: ina238: use microseconds for update_interval
Date: Fri, 22 May 2026 09:03:04 +0200	[thread overview]
Message-ID: <20260522-hwmon-ina238-add-samples-update-interval-v1-2-e1acfceb447e@advastore.com> (raw)
In-Reply-To: <20260522-hwmon-ina238-add-samples-update-interval-v1-0-e1acfceb447e@advastore.com>

From: Ferdinand Schwenk <ferdinand.schwenk@advastore.com>

The INA238 family supports conversion times as low as 50 us.  The
standard hwmon sysfs ABI unit for update_interval is milliseconds,
which causes sub-millisecond conversion times to round to zero and
makes fine-grained control impossible.

Change the unit of chip/update_interval from milliseconds to
microseconds so that all eight conversion-time steps (50, 84, 150,
280, 540, 1052, 2074, 4120 us for INA238 and 66, 118, 310, 566,
1070, 2090, 4140, 8230 us for SQ52206) are accessible without loss
of precision.

Signed-off-by: Ferdinand Schwenk <ferdinand.schwenk@advastore.com>
---
 drivers/hwmon/ina238.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
index 7bc18e5445b2..db0c0b5344d6 100644
--- a/drivers/hwmon/ina238.c
+++ b/drivers/hwmon/ina238.c
@@ -310,11 +310,14 @@ static inline u32 ina238_reg_to_interval_us(struct ina238_data *data)
 	return vbusct + vshct + vtct;
 }
 
-/* Converting update_interval in msec to a single conversion time in usec */
-static inline u32 ina238_interval_ms_to_conv_time(long interval)
+/* Converting update_interval in usec to a single conversion time in usec.
+ * update_interval represents the raw ADC cycle time (VBUSCT + VSHCT + VTCT),
+ * independent of averaging.  Since all three fields are always set equal,
+ * the target per-field conversion time is simply interval / 3.
+ */
+static inline u32 ina238_interval_us_to_conv_time(long interval)
 {
-	/* 3 equal conversion times (VBUSCT, VSHCT, VTCT) cover all measurement types */
-	return DIV_ROUND_CLOSEST(interval * 1000, 3);
+	return DIV_ROUND_CLOSEST(interval, 3);
 }
 
 static int ina238_read_chip(struct device *dev, u32 attr, long *val)
@@ -327,8 +330,8 @@ static int ina238_read_chip(struct device *dev, u32 attr, long *val)
 					  INA238_ADC_CONFIG_AVG_SHIFT];
 		return 0;
 	case hwmon_chip_update_interval:
-		/* Return in msec */
-		*val = DIV_ROUND_CLOSEST(ina238_reg_to_interval_us(data), 1000);
+		/* Return in usec */
+		*val = ina238_reg_to_interval_us(data);
 		return 0;
 	default:
 		return -EOPNOTSUPP;
@@ -353,7 +356,7 @@ static int ina238_write_chip(struct device *dev, u32 attr, long val)
 		data->adc_config = adc_config;
 		return 0;
 	case hwmon_chip_update_interval:
-		val = ina238_interval_ms_to_conv_time(val);
+		val = ina238_interval_us_to_conv_time(val);
 		idx = find_closest(val, data->config->conv_time,
 				   ARRAY_SIZE(ina238_conv_time));
 		adc_config = (data->adc_config &

-- 
2.54.0



  parent reply	other threads:[~2026-05-22  7:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22  7:03 [PATCH 0/2] hwmon: ina238: Add samples and update_interval support Ferdinand Schwenk via B4 Relay
2026-05-22  7:03 ` [PATCH 1/2] hwmon: ina238: add support for samples and update_interval Ferdinand Schwenk via B4 Relay
2026-05-22  7:03 ` Ferdinand Schwenk via B4 Relay [this message]
2026-05-22 13:18 ` [PATCH 0/2] hwmon: ina238: Add samples and update_interval support Guenter Roeck
2026-05-26  9:08   ` AW: " Ferdinand Schwenk
2026-05-28  4:01     ` 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=20260522-hwmon-ina238-add-samples-update-interval-v1-2-e1acfceb447e@advastore.com \
    --to=devnull+ferdinand.schwenk.advastore.com@kernel.org \
    --cc=ferdinand.schwenk@advastore.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=richard.leitner@linux.dev \
    /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

all inboxes | Powered by JetHome®