[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251118125148.95603-8-wenliang202407@163.com>
Date: Tue, 18 Nov 2025 07:51:47 -0500
From: Wenliang Yan <wenliang202407@....com>
To: linux@...ck-us.net,
Jean Delvare <jdelvare@...e.com>
Cc: Wenliang Yan <wenliang202407@....com>,
Rob Herring <robh@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>,
Conor Dooley <conor+dt@...nel.org>,
Jonathan Corbet <corbet@....net>,
linux-hwmon@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH v2 7/8] hwmon: (ina3221) Support write/read functions for 'power' attribute
SQ52210 adds power attributes to report power data and implements
corresponding read/write functions for this purpose. This includes
reading power values, reading alert thresholds, reading alert
trigger status, and writing alert thresholds.
Signed-off-by: Wenliang Yan <wenliang202407@....com>
---
drivers/hwmon/ina3221.c | 79 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
index 9a25a1b40856..197fc3a468e4 100644
--- a/drivers/hwmon/ina3221.c
+++ b/drivers/hwmon/ina3221.c
@@ -92,6 +92,9 @@ enum ina3221_fields {
/* Alert Flags: SF is the summation-alert flag */
F_SF, F_CF3, F_CF2, F_CF1,
+ /* Alert Flags: AFF is the alert function flag */
+ F_AFF3, F_AFF2, F_AFF1,
+
/* sentinel */
F_MAX_FIELDS
};
@@ -107,6 +110,10 @@ static const struct reg_field ina3221_reg_fields[] = {
[F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7),
[F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8),
[F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9),
+
+ [F_AFF3] = REG_FIELD(SQ52210_ALERT_CONFIG, 1, 1),
+ [F_AFF2] = REG_FIELD(SQ52210_ALERT_CONFIG, 2, 2),
+ [F_AFF1] = REG_FIELD(SQ52210_ALERT_CONFIG, 3, 3),
};
enum ina3221_channels {
@@ -505,6 +512,60 @@ static int ina3221_read_curr(struct device *dev, u32 attr,
}
}
+static const u8 ina3221_power_reg[][INA3221_NUM_CHANNELS] = {
+ [hwmon_power_input] = { SQ52210_POWER1, SQ52210_POWER2, SQ52210_POWER3 },
+ [hwmon_power_crit] = { SQ52210_ALERT_LIMIT1, SQ52210_ALERT_LIMIT2,
+ SQ52210_ALERT_LIMIT3 },
+ [hwmon_power_crit_alarm] = { F_AFF1, F_AFF2, F_AFF3 },
+};
+
+static int ina3221_read_power(struct device *dev, u32 attr, int channel, long *val)
+{
+ struct ina3221_data *ina = dev_get_drvdata(dev);
+ u8 reg = ina3221_power_reg[attr][channel];
+ int regval, ret;
+
+ switch (attr) {
+ case hwmon_power_input:
+ if (!ina3221_is_enabled(ina, channel))
+ return -ENODATA;
+
+ /* Write CONFIG register to trigger a single-shot measurement */
+ if (ina->single_shot) {
+ regmap_write(ina->regmap, INA3221_CONFIG,
+ ina->reg_config);
+
+ ret = ina3221_wait_for_data(ina);
+ if (ret)
+ return ret;
+ }
+
+ fallthrough;
+ case hwmon_power_crit:
+ ret = ina3221_read_value(ina, reg, ®val);
+ if (ret)
+ return ret;
+ /* Return power in mW */
+ *val = DIV_ROUND_CLOSEST(regval * ina->power_lsb_uW, 1000);
+ return 0;
+ case hwmon_power_crit_alarm:
+ /* No actual register read if channel is disabled */
+ if (!ina3221_is_enabled(ina, channel)) {
+ /* Return 0 for alert flags */
+ *val = 0;
+ return 0;
+ }
+
+ ret = regmap_field_read(ina->fields[reg], ®val);
+ if (ret)
+ return ret;
+ *val = regval;
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
static int sq52210_alert_limit_write(struct ina3221_data *ina, u32 attr, int channel, long val)
{
struct regmap *regmap = ina->regmap;
@@ -723,6 +784,18 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable)
return ret;
}
+static int ina3221_write_power(struct device *dev, u32 attr, int channel, long val)
+{
+ struct ina3221_data *ina = dev_get_drvdata(dev);
+
+ switch (attr) {
+ case hwmon_power_crit:
+ return sq52210_alert_limit_write(ina, attr, channel, val);
+ default:
+ return 0;
+ }
+}
+
static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{
@@ -739,6 +812,9 @@ static int ina3221_read(struct device *dev, enum hwmon_sensor_types type,
case hwmon_curr:
ret = ina3221_read_curr(dev, attr, channel, val);
break;
+ case hwmon_power:
+ ret = ina3221_read_power(dev, attr, channel, val);
+ break;
default:
ret = -EOPNOTSUPP;
break;
@@ -762,6 +838,9 @@ static int ina3221_write(struct device *dev, enum hwmon_sensor_types type,
case hwmon_curr:
ret = ina3221_write_curr(dev, attr, channel, val);
break;
+ case hwmon_power:
+ ret = ina3221_write_power(dev, attr, channel, val);
+ break;
default:
ret = -EOPNOTSUPP;
break;
--
2.17.1
Powered by blists - more mailing lists