linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Cormier, Jonathan" <jcormier@criticallink.com>
To: linux-hwmon@vger.kernel.org
Cc: John Pruitt <jpruitt@criticallink.com>,
	Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Bob Duke <bduke@criticallink.com>,
	"Cormier, Jonathan" <jcormier@criticallink.com>
Subject: [PATCH 2/2] hwmon: ltc2945: Allow setting shunt resistor
Date: Wed, 14 Dec 2022 17:07:27 -0500	[thread overview]
Message-ID: <20221214220727.1350784-3-jcormier@criticallink.com> (raw)
In-Reply-To: <20221214220727.1350784-1-jcormier@criticallink.com>

From: John Pruitt <jpruitt@criticallink.com>

Added the ability to specify the value of the shunt resistor in the
device tree instead of assuming it is 1 milliOhm. The value in the
device tree has the name shunt-resistor-micro-ohms and the
default value is 1000 micro-ohms in order to preserve the
current behavior.

Signed-off-by: "Cormier, Jonathan" <jcormier@criticallink.com>
Signed-off-by: John Pruitt <jpruitt@criticallink.com>
---
 drivers/hwmon/ltc2945.c | 86 +++++++++++++++++++++++++++++++----------
 1 file changed, 65 insertions(+), 21 deletions(-)

diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c
index 9adebb59f604..4e8b92e53133 100644
--- a/drivers/hwmon/ltc2945.c
+++ b/drivers/hwmon/ltc2945.c
@@ -58,6 +58,11 @@
 #define CONTROL_MULT_SELECT	(1 << 0)
 #define CONTROL_TEST_MODE	(1 << 4)
 
+struct ltc2945_data {
+	struct regmap *regmap;
+	u32 shunt_resistor; // number of microohms
+};
+
 static inline bool is_power_reg(u8 reg)
 {
 	return reg < LTC2945_SENSE_H;
@@ -66,7 +71,9 @@ static inline bool is_power_reg(u8 reg)
 /* Return the value from the given register in uW, mV, or mA */
 static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 {
-	struct regmap *regmap = dev_get_drvdata(dev);
+	struct ltc2945_data *data = dev_get_drvdata(dev);
+	struct regmap *regmap = data->regmap;
+	u32 shunt_resistor = data->shunt_resistor;
 	unsigned int control;
 	u8 buf[3];
 	long long val;
@@ -93,8 +100,11 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 	case LTC2945_MIN_POWER_THRES_H:
 		/*
 		 * Convert to uW by assuming current is measured with
-		 * an 1mOhm sense resistor, similar to current
-		 * measurements.
+		 * a 1000 microOhm sense resistor (aka shunt resistor)
+		 * or what is specified in DT, similar to current
+		 * measurements. The shunt_resistor value is in
+		 * microOhms.
+		 *
 		 * Control register bit 0 selects if voltage at SENSE+/VDD
 		 * or voltage at ADIN is used to measure power.
 		 */
@@ -108,6 +118,8 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 			/* 0.5 mV * 25 uV = 0.0125 uV resolution. */
 			val = (val * 25LL) >> 1;
 		}
+		val *= 1000;
+		val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
 		break;
 	case LTC2945_VIN_H:
 	case LTC2945_MAX_VIN_H:
@@ -132,12 +144,15 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 	case LTC2945_MIN_SENSE_THRES_H:
 		/*
 		 * 25 uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA. If a different sense
-		 * resistor is installed, calculate the actual current by
-		 * dividing the reported current by the sense resistor value
-		 * in mOhm.
+		 * an 1000 microOhm sense (or shunt) resistor, in mA.
+		 * If a different shunt resistor is installed, calculate
+		 * the actual current by dividing the reported current
+		 * by the shunt resistor value in microOhms. The actual
+		 * The actual shunt resistor value can be specified
+		 * in the device tree.
 		 */
-		val *= 25;
+		val *= 25 * 1000;
+		val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor);
 		break;
 	default:
 		return -EINVAL;
@@ -148,7 +163,9 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 static int ltc2945_val_to_reg(struct device *dev, u8 reg,
 			      unsigned long val)
 {
-	struct regmap *regmap = dev_get_drvdata(dev);
+	struct ltc2945_data *data = dev_get_drvdata(dev);
+	struct regmap *regmap = data->regmap;
+	u32 shunt_resistor = data->shunt_resistor;
 	unsigned int control;
 	int ret;
 
@@ -160,8 +177,10 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg,
 	case LTC2945_MIN_POWER_THRES_H:
 		/*
 		 * Convert to register value by assuming current is measured
-		 * with an 1mOhm sense resistor, similar to current
+		 * with a 1000 microOhm sense resistor, (aka shunt resistor)
+		 * or what is specified in DT, similar to current
 		 * measurements.
+		 *
 		 * Control register bit 0 selects if voltage at SENSE+/VDD
 		 * or voltage at ADIN is used to measure power, which in turn
 		 * determines register calculations.
@@ -171,14 +190,16 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg,
 			return ret;
 		if (control & CONTROL_MULT_SELECT) {
 			/* 25 mV * 25 uV = 0.625 uV resolution. */
-			val = DIV_ROUND_CLOSEST(val, 625);
+			val *= shunt_resistor;
+			val = DIV_ROUND_CLOSEST(val, 625 * 1000);
 		} else {
 			/*
 			 * 0.5 mV * 25 uV = 0.0125 uV resolution.
 			 * Divide first to avoid overflow;
 			 * accept loss of accuracy.
 			 */
-			val = DIV_ROUND_CLOSEST(val, 25) * 2;
+			val *= shunt_resistor;
+			val = DIV_ROUND_CLOSEST(val, 25 * 1000) * 2;
 		}
 		break;
 	case LTC2945_VIN_H:
@@ -204,12 +225,14 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg,
 	case LTC2945_MIN_SENSE_THRES_H:
 		/*
 		 * 25 uV resolution. Convert to current as measured with
-		 * an 1 mOhm sense resistor, in mA. If a different sense
-		 * resistor is installed, calculate the actual current by
-		 * dividing the reported current by the sense resistor value
-		 * in mOhm.
+		 * a 1000 microOhm sense (or shunt) resistor, in mA. If
+		 * a different shunt resistor is installed, calculate
+		 * the actual current by dividing the reported current
+		 * by the sense resistor value in microOhms. The actual
+		 * shunt resistor value can be specified in the device tree.
 		 */
-		val = DIV_ROUND_CLOSEST(val, 25);
+		val *= shunt_resistor;
+		val = DIV_ROUND_CLOSEST(val, 25 * 1000);
 		break;
 	default:
 		return -EINVAL;
@@ -234,7 +257,8 @@ static ssize_t ltc2945_value_store(struct device *dev,
 				   const char *buf, size_t count)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
-	struct regmap *regmap = dev_get_drvdata(dev);
+	struct ltc2945_data *data = dev_get_drvdata(dev);
+	struct regmap *regmap = data->regmap;
 	u8 reg = attr->index;
 	unsigned long val;
 	u8 regbuf[3];
@@ -269,7 +293,8 @@ static ssize_t ltc2945_history_store(struct device *dev,
 				     const char *buf, size_t count)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
-	struct regmap *regmap = dev_get_drvdata(dev);
+	struct ltc2945_data *data = dev_get_drvdata(dev);
+	struct regmap *regmap = data->regmap;
 	u8 reg = attr->index;
 	int num_regs = is_power_reg(reg) ? 3 : 2;
 	u8 buf_min[3] = { 0xff, 0xff, 0xff };
@@ -321,7 +346,8 @@ static ssize_t ltc2945_bool_show(struct device *dev,
 				 struct device_attribute *da, char *buf)
 {
 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
-	struct regmap *regmap = dev_get_drvdata(dev);
+	struct ltc2945_data *data = dev_get_drvdata(dev);
+	struct regmap *regmap = data->regmap;
 	unsigned int fault;
 	int ret;
 
@@ -450,6 +476,14 @@ static int ltc2945_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct device *hwmon_dev;
 	struct regmap *regmap;
+	u32 shunt_resistor;
+
+	struct ltc2945_data *data;
+
+	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
+	dev_set_drvdata(dev, data);
 
 	regmap = devm_regmap_init_i2c(client, &ltc2945_regmap_config);
 	if (IS_ERR(regmap)) {
@@ -457,11 +491,21 @@ static int ltc2945_probe(struct i2c_client *client)
 		return PTR_ERR(regmap);
 	}
 
+	data->regmap = regmap;
+	if (of_property_read_u32(client->dev.of_node,
+				 "shunt-resistor-micro-ohms", &shunt_resistor))
+		shunt_resistor = 1000; /* 1000 micro-Ohm if not set via DT */
+
+	if (shunt_resistor == 0)
+		return -EINVAL;
+
+	data->shunt_resistor = shunt_resistor;
+
 	/* Clear faults */
 	regmap_write(regmap, LTC2945_FAULT, 0x00);
 
 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
-							   regmap,
+							   data,
 							   ltc2945_groups);
 	return PTR_ERR_OR_ZERO(hwmon_dev);
 }
-- 
2.25.1


  parent reply	other threads:[~2022-12-14 22:07 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 22:07 [PATCH 0/2] hwmon: ltc2945: Add binding and shunt resistor support Cormier, Jonathan
2022-12-14 22:07 ` [PATCH 1/2] dt-bindings: hwmon: adi,ltc2945: Add binding Cormier, Jonathan
2022-12-15  9:42   ` Krzysztof Kozlowski
     [not found]     ` <CADL8D3ZUE5WbV0oS6hEVUNh9asrhTKQeGR4McR6Kh6qykSFw=Q@mail.gmail.com>
2022-12-15 14:37       ` Jon Cormier
2022-12-14 22:07 ` Cormier, Jonathan [this message]
2022-12-15 15:11   ` [PATCH 2/2] hwmon: ltc2945: Allow setting shunt resistor Guenter Roeck
2022-12-15 19:42     ` Jon Cormier
2023-01-09 23:35   ` [PATCH v3 0/2] hwmon: ltc2945: Add binding and shunt resistor support Jonathan Cormier
2023-01-09 23:35     ` [PATCH v3 1/5] dt-bindings: hwmon: adi,ltc2945: Add binding Jonathan Cormier
2023-01-10  9:07       ` Krzysztof Kozlowski
2023-01-09 23:35     ` [PATCH v3 2/5] hwmon: ltc2945: Add devicetree match table Jonathan Cormier
2023-01-09 23:35     ` [PATCH v3 3/5] hwmon: ltc2945: Handle error case in ltc2945_value_store Jonathan Cormier
2023-01-10  0:04       ` Guenter Roeck
2023-01-10 18:19         ` Jon Cormier
2023-01-10 18:22           ` Guenter Roeck
2023-01-10 19:25             ` Jon Cormier
2023-01-12  0:44               ` Guenter Roeck
2023-01-18 18:32                 ` Jon Cormier
2023-01-09 23:35     ` [PATCH v3 4/5] hwmon: ltc2945: Allow setting shunt resistor Jonathan Cormier
2023-01-09 23:35     ` [PATCH v3 5/5] hwmon: ltc2945: Convert division to DIV_ROUND_CLOSEST_ULL Jonathan Cormier
2022-12-20  0:04 ` [PATCH v2 0/2] hwmon: ltc2945: Add binding and shunt resistor support Cormier, Jonathan
2022-12-20  0:04   ` [PATCH v2 1/4] dt-bindings: hwmon: adi,ltc2945: Add binding Cormier, Jonathan
2022-12-20 10:15     ` Krzysztof Kozlowski
2022-12-20 14:35       ` Jon Cormier
2022-12-20 14:46         ` Guenter Roeck
2022-12-20 21:47           ` Jon Cormier
2022-12-20  0:04   ` [PATCH v2 2/4] hwmon: ltc2945: Add devicetree match table Cormier, Jonathan
2022-12-28 16:43     ` Guenter Roeck
2022-12-20  0:04   ` [PATCH v2 3/4] hwmon: ltc2945: Allow setting shunt resistor Cormier, Jonathan
2022-12-28 16:49     ` Guenter Roeck
2022-12-20  0:04   ` [PATCH v2 4/4] hwmon: ltc2945: Fix possible overflows Cormier, Jonathan
2022-12-28 17: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=20221214220727.1350784-3-jcormier@criticallink.com \
    --to=jcormier@criticallink.com \
    --cc=bduke@criticallink.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jdelvare@suse.com \
    --cc=jpruitt@criticallink.com \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=robh+dt@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;
as well as URLs for NNTP newsgroup(s).