linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandru Ardelean <alexandru.ardelean@analog.com>
To: <linux-hwmon@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: <robh+dt@kernel.org>, <linux@roeck-us.net>, <jdelvare@suse.com>,
	<mark.thoren@analog.com>, <ardeleanalex@gmail.com>,
	Alexandru Ardelean <alexandru.ardelean@analog.com>
Subject: [PATCH v3 2/4] hwmon: (ltc2945): clamp values before converting
Date: Thu, 7 Jan 2021 12:34:15 +0200	[thread overview]
Message-ID: <20210107103417.16010-3-alexandru.ardelean@analog.com> (raw)
In-Reply-To: <20210107103417.16010-1-alexandru.ardelean@analog.com>

We can compute the full-scale values for power, voltages and current, and
apply them before conversion.

This way we sanitize the input from userspace a bit.
We can't however clamp the value for power, since that is represented in
micro-Watts. The full-scale power that can be represented is around 10480
Watts, which in micro-Watts ends up being around 10 billion micro-Watts.

Current and voltage is represented in millis, so these can be clamped.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/hwmon/ltc2945.c | 48 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c
index 42a59170da78..41df2c8b7673 100644
--- a/drivers/hwmon/ltc2945.c
+++ b/drivers/hwmon/ltc2945.c
@@ -58,6 +58,15 @@
 #define CONTROL_MULT_SELECT	(1 << 0)
 #define CONTROL_TEST_MODE	(1 << 4)
 
+/* Full scale ranges (page 4 of the datasheet) */
+
+#define LTC2945_VIN_FULL_SCALE_MV	102375
+#define LTC2945_ADIN_FULL_SCALE_MV	2048
+
+/* Power and current computed assuming a 1mOhm sense resistor */
+#define LTC2945_POWER_FULL_SCALE_UW	10485759375ULL
+#define LTC2945_SENSE_FULL_SCALE_MA	102375
+
 /**
  * struct ltc2945_state - driver instance specific data
  * @regmap:		regmap object to access device registers
@@ -154,6 +163,43 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg)
 	return val;
 }
 
+static unsigned long ltc2945_val_clamp(u8 reg, unsigned long val)
+{
+	switch (reg) {
+	case LTC2945_POWER_H:
+	case LTC2945_MAX_POWER_H:
+	case LTC2945_MIN_POWER_H:
+	case LTC2945_MAX_POWER_THRES_H:
+	case LTC2945_MIN_POWER_THRES_H:
+		/* No sense in clamping now, LTC2945_POWER_FULL_SCALE_UW is larger than UINT32_MAX */
+		return val;
+	case LTC2945_VIN_H:
+	case LTC2945_MAX_VIN_H:
+	case LTC2945_MIN_VIN_H:
+	case LTC2945_MAX_VIN_THRES_H:
+	case LTC2945_MIN_VIN_THRES_H:
+		return clamp_val(val, 0, LTC2945_VIN_FULL_SCALE_MV);
+	case LTC2945_ADIN_H:
+	case LTC2945_MAX_ADIN_H:
+	case LTC2945_MIN_ADIN_THRES_H:
+	case LTC2945_MAX_ADIN_THRES_H:
+	case LTC2945_MIN_ADIN_H:
+		return clamp_val(val, 0, LTC2945_ADIN_FULL_SCALE_MV);
+	case LTC2945_SENSE_H:
+	case LTC2945_MAX_SENSE_H:
+	case LTC2945_MIN_SENSE_H:
+	case LTC2945_MAX_SENSE_THRES_H:
+	case LTC2945_MIN_SENSE_THRES_H:
+		return clamp_val(val, 0, LTC2945_SENSE_FULL_SCALE_MA);
+	default:
+		/*
+		 * This is unlikely to happen, and if it does, it should
+		 * error out on the next call, we can't return negative here
+		 */
+		return 0;
+	}
+}
+
 static int ltc2945_val_to_reg(struct device *dev, u8 reg,
 			      unsigned long val)
 {
@@ -257,6 +303,8 @@ static ssize_t ltc2945_value_store(struct device *dev,
 	if (ret)
 		return ret;
 
+	val = ltc2945_val_clamp(reg, val);
+
 	/* convert to register value, then clamp and write result */
 	regval = ltc2945_val_to_reg(dev, reg, val);
 	if (is_power_reg(reg)) {
-- 
2.17.1


  parent reply	other threads:[~2021-01-07 10:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-07 10:34 [PATCH v3 0/4] hwmon: (ltc2945): add support for sense resistor Alexandru Ardelean
2021-01-07 10:34 ` [PATCH v3 1/4] hwmon: (ltc2945): wrap regmap into an ltc2945_state struct Alexandru Ardelean
2021-01-07 10:34 ` Alexandru Ardelean [this message]
2021-01-07 10:34 ` [PATCH v3 3/4] hwmon: (ltc2945): add support for sense resistor Alexandru Ardelean
2021-01-07 13:28   ` kernel test robot
2021-01-07 14:10     ` Alexandru Ardelean
2021-01-07 14:23   ` kernel test robot
2021-01-07 15:25   ` Guenter Roeck
2021-01-07 15:44     ` Alexandru Ardelean
2021-01-07 17:35       ` Guenter Roeck
2021-01-08  9:18         ` Alexandru Ardelean
2021-01-07 10:34 ` [PATCH v3 4/4] dt-bindings: hwmon: ltc2945: add device tree doc for ltc2945 Alexandru Ardelean
2021-01-07 15:28 ` [PATCH v3 0/4] hwmon: (ltc2945): add support for sense resistor 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=20210107103417.16010-3-alexandru.ardelean@analog.com \
    --to=alexandru.ardelean@analog.com \
    --cc=ardeleanalex@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mark.thoren@analog.com \
    --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).