From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EF1233BBAB; Wed, 25 Feb 2026 09:04:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010259; cv=none; b=nPJRnG4U8+RyQ1jtRXlMjmUBTgO2YR/2mB5eRDB56Vmc1U3r9WjJ9ookfFuZTHAucZGglZ23URyAObWpJZHj4ELTy+pDHTJN+HK32yR3chTGZ97Kdyg5eztCcOiGCLR18A9eqcAvBc/z6yGPad1TJNdStgwty0+qkMkRAHR7TXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010259; c=relaxed/simple; bh=WjsTHLjgxsy+mXRsoUwpiqJQYbq1MnNhmtWxtQbbl8g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=rpa91RlYjkn3QW+GpgjNLF1jK/19hGR9ughCE4GcGZBXuEEbOeCF/OBUaDlxlkBlDtIQ+xFvRUQH4aan2Vx8lS3PoYlAnbsss4Jn4LkSe/jyBmDVgDrQ+73U7nC8fK1EzAXWganm7OEuGx/5FJqS5ziCZk6AjlzHz3bKFNhRkQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=gVQcJL50; arc=none smtp.client-ip=220.197.31.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="gVQcJL50" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=MHv2qgvbLAFuri5 w6URQ/pAxxfhBNmmanOC7CAD1enM=; b=gVQcJL50q8LuRKP83YQ5GDamO82mN3t i8qBZY6H6R1CxBnqwFZTvDamzjxZ7os6BlheLUkG+q8KWimiumrj5DlE4zJwGLJg e2GS0EzO+W8L66DR9XIfsAzSUt4ECMlgWXnwzepafAd5wn5GWB1F3Q4Zv7i42pIU l2/+N+pAXhiU= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S3; Wed, 25 Feb 2026 17:03:36 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Wenliang Yan , Jonathan Corbet , linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 1/8] dt-bindings: hwmon: ti,ina3221: Add SQ52210 Date: Wed, 25 Feb 2026 04:03:17 -0500 Message-Id: <20260225090324.112145-2-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Cw4UGFWkZr13Zw43Wr17ZFb_yoW8Xr43pF s3CFyUWrySqr1fZ39rKFsY9F15Jwn7ua12kFnrGw4SqF4DGa4Fq393Kw1qyFn8ArWfXFW7 WFWI9r4Fg397Ar7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JUqjgxUUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbC5wjqvmmeuugxJAAA3L Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a compatible string for SQ52210, provide brief descriptions for both INA3221 and SQ52210, and define the compatibility relationship between SQ52210 and INA3221. SQ52210 is backward compatible with INA3221. Reviewed-by: Krzysztof Kozlowski Signed-off-by: Wenliang Yan --- .../devicetree/bindings/hwmon/ti,ina3221.yaml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina3221.yaml b/Docu= mentation/devicetree/bindings/hwmon/ti,ina3221.yaml index 5f10f1207d69..2dd2fd148792 100644 --- a/Documentation/devicetree/bindings/hwmon/ti,ina3221.yaml +++ b/Documentation/devicetree/bindings/hwmon/ti,ina3221.yaml @@ -10,9 +10,22 @@ maintainers: - Jean Delvare - Guenter Roeck =20 +description: | + The INA3221 is a three-channel, high-side current and bus voltage monito= r. + + The Silergy SQ52210 is a power monitor that extends the functionality of + the INA3221 by adding additional current registers, power registers, and + alert registers. These features are configured internally by the driver + and require no board-level device tree configuration. + properties: compatible: - const: ti,ina3221 + oneOf: + - items: + - const: silergy,sq52210 + - const: ti,ina3221 + - items: + - const: ti,ina3221 =20 reg: maxItems: 1 --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 519F734A766; Wed, 25 Feb 2026 09:04:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010261; cv=none; b=CEZLToLEuSjnBogzsNgiy+QcUHGExYHc8kpCkVBsdXABTh7MhOYQbBHWW4xCsv+Jhj0E5kI6C0KMUKtY9f/1v5K9PLCDqcKeczKVeZeMsA3EUkvxYbJWqFSqccMqocRt1zv11ypZpGIBro5bdK2XscczE3awgOHJjGnpQkvV32A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010261; c=relaxed/simple; bh=H/w+6on8fYV8qpYT0kb7Mj2iA2mlwXEn1PrOId/TyGo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=DaR8pMP71Al9JlI7vEwgHsyjiZi+GACFP3w9IS6PjH14u8s7dkEK3BlsVfCYlao+9EPcRH0TllKGHpRrpInlBe+QiTmTWaw41sPfrBw3AiX1mgvgW12I19/yUc5mrO01KSFsaTJD5Njyhjsd2fN6kDyaRTL0fG0lE2L6ofX0AL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=A55/MMV0; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="A55/MMV0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=oQP23GsQvFrcqDF p4tVk7ny6xZlM2KY/EMMUq56/WtE=; b=A55/MMV0L5xidJa1d1hD7p4acHazT/E Y+Nqf0LLH3gmT4118dsPsAuoDmaUS8/KF3ZRnOf6pNQAi1yRfHa+OjbZtM7U1Anm 3SsLsUr3blFLRPXpb+L+FB1C9gIbMVFMXTs6VG/mdlL3L4THR9/JlM4yCTuvC2Vs jzH5wH57iOzw= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S4; Wed, 25 Feb 2026 17:03:37 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 2/8] hwmon: (ina3221) Add support for SQ52210 Date: Wed, 25 Feb 2026 04:03:18 -0500 Message-Id: <20260225090324.112145-3-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S4 X-Coremail-Antispam: 1Uf129KBjvJXoWxXFW8Xr1UJFWDXrW3AF1kXwb_yoW5ur1fpa 1rAa4rtr45Xr4Ig3yfKFs5tF15tr4xW3yIvrnrK3yIvF4DAry0gF1rGw4vyF98ZFyfZFsr X34Iy3y8uwnrJr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pi_Ma5UUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbC6Anqvmmeuuk4+QAA3Q Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add Compatible for SQ52210. Compared to the INA3221, the SQ52210 also has current registers, power registers, and limit-related registers. Signed-off-by: Wenliang Yan --- drivers/hwmon/ina3221.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 5ecc68dcf169..47ef4fe694ea 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -34,6 +34,17 @@ #define INA3221_SHUNT_SUM 0x0d #define INA3221_CRIT_SUM 0x0e #define INA3221_MASK_ENABLE 0x0f +#define SQ52210_ALERT_CONFIG 0x12 +#define SQ52210_CALIBRATION 0x14 +#define SQ52210_CURRENT1 0x15 +#define SQ52210_CURRENT2 0x16 +#define SQ52210_CURRENT3 0x17 +#define SQ52210_POWER1 0x18 +#define SQ52210_POWER2 0x19 +#define SQ52210_POWER3 0x1A +#define SQ52210_ALERT_LIMIT1 0x1B +#define SQ52210_ALERT_LIMIT2 0x1C +#define SQ52210_ALERT_LIMIT3 0x1D =20 #define INA3221_CONFIG_MODE_MASK GENMASK(2, 0) #define INA3221_CONFIG_MODE_POWERDOWN 0 @@ -108,8 +119,11 @@ struct ina3221_input { bool summation_disable; }; =20 +enum ina3221_ids { ina3221, sq52210 }; + /** * struct ina3221_data - device specific information + * @chip: Chip type identifier * @pm_dev: Device pointer for pm runtime * @regmap: Register map of the device * @fields: Register fields of the device @@ -120,6 +134,8 @@ struct ina3221_input { * @single_shot: running in single-shot operating mode */ struct ina3221_data { + enum ina3221_ids chip; + struct device *pm_dev; struct regmap *regmap; struct regmap_field *fields[F_MAX_FIELDS]; @@ -734,6 +750,7 @@ static const struct regmap_range ina3221_yes_ranges[] = =3D { regmap_reg_range(INA3221_CONFIG, INA3221_BUS3), regmap_reg_range(INA3221_SHUNT_SUM, INA3221_SHUNT_SUM), regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE), + regmap_reg_range(SQ52210_ALERT_CONFIG, SQ52210_POWER3), }; =20 static const struct regmap_access_table ina3221_volatile_table =3D { @@ -818,13 +835,18 @@ static int ina3221_probe(struct i2c_client *client) struct device *dev =3D &client->dev; struct ina3221_data *ina; struct device *hwmon_dev; + enum ina3221_ids chip; char name[32]; int i, ret; =20 + chip =3D (uintptr_t)i2c_get_match_data(client); + ina =3D devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL); if (!ina) return -ENOMEM; =20 + ina->chip =3D chip; + ina->regmap =3D devm_regmap_init_i2c(client, &ina3221_regmap_config); if (IS_ERR(ina->regmap)) { dev_err(dev, "Unable to allocate register map\n"); @@ -996,13 +1018,21 @@ static DEFINE_RUNTIME_DEV_PM_OPS(ina3221_pm, ina3221= _suspend, ina3221_resume, NULL); =20 static const struct of_device_id ina3221_of_match_table[] =3D { - { .compatible =3D "ti,ina3221", }, + { + .compatible =3D "silergy,sq52210", + .data =3D (void *)sq52210 + }, + { + .compatible =3D "ti,ina3221", + .data =3D (void *)ina3221 + }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, ina3221_of_match_table); =20 static const struct i2c_device_id ina3221_ids[] =3D { - { "ina3221" }, + { "ina3221", ina3221 }, + { "sq52210", sq52210 }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(i2c, ina3221_ids); --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E27D633B97D; Wed, 25 Feb 2026 09:04:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010259; cv=none; b=a6YYWNoJi24Df/iuAl2UbewKtbZqS4Tcb40HfRKZZhYSNM4jUIbVB/V+NWYW0w3WjR+pC+0X21S2xoHgFwmGcoM7Imai+UcfOLmxKQFRX72Oq8/hvOr2OjLlMSZxp/k0SmrBCmOnn0YAjHTGPqyUEGyOuoTEHXJrzWl4ginJJZ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010259; c=relaxed/simple; bh=dUkkZHYey7BZzpb0fTIQid8SBjXfkS8TzA9BdhO8tFY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=I7/JE6YK1nApIP5KskIz8H8Akbzrlm+1TZPOXxMiElbU99bCeScvFwIxgs8L1rB5CsU99ED+2VUwqzW/Fa5XKxHiqBamqijSmbi8MkIhz5pcSYolGjcFm1d7gJ2y2j437Ifa9k7ZoHNIfVFyqij+4LfrDK672vTJ02nzfXMoBr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=ddSWojxt; arc=none smtp.client-ip=220.197.31.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="ddSWojxt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=+v5Xg+kWjmKbK5f EihGrPO5x4iH9HqOpoWxZg+Vd674=; b=ddSWojxt731WBMnk5yiM6gldEaiM8l6 dbxsHJRQMNIrG1EsAUu/QrNzbVBx6kgxsjvaMrYEz/3zssz+diE1N88sUOErDMaI rvsKgbCEj9bkc5tb3f75MuJW4Kqzn/L/X8o0ppBS5y78xU8BuS+qtThZ04zLAd3D o17zbgitKPh8= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S5; Wed, 25 Feb 2026 17:03:38 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 3/8] hwmon: (ina3221) Pre-calculate current and power LSB Date: Wed, 25 Feb 2026 04:03:19 -0500 Message-Id: <20260225090324.112145-4-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S5 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jr4kGF47WrW3Jw1kur1fCrg_yoW7XF4DpF 4rKr1rta40q3WfKa9Ikw4xGF1rtr97Jr42krZrWw1IqFsFkryqk3yrJFyDtFy5Ar15ZF13 X3y7tr4Duan2yaUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRFD73UUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbCwArqvmmeuuom+QAA3m Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The LSB for current and power can be pre-calculated for data read/write operations. The current LSB is determined by the calibration value and shunt resistor value, with the calibration value fixed within the driver. The power LSB can be derived from the current LSB. Signed-off-by: Wenliang Yan --- drivers/hwmon/ina3221.c | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 47ef4fe694ea..e99ac5ef4d8a 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -67,6 +67,7 @@ =20 #define INA3221_CONFIG_DEFAULT 0x7127 #define INA3221_RSHUNT_DEFAULT 10000 +#define SQ52210_SHUNT_LSB 40000000 /* pV/LSB */ =20 enum ina3221_fields { /* Configuration */ @@ -121,8 +122,16 @@ struct ina3221_input { =20 enum ina3221_ids { ina3221, sq52210 }; =20 +struct ina3221_config { + bool has_current_reg; /* chip has internal current reg */ + bool has_power_reg; /* chip has internal power reg */ + int calibration_value; /* calculate current_lsb */ + int power_lsb_factor; +}; + /** * struct ina3221_data - device specific information + * @config: Used to store characteristics of different chips * @chip: Chip type identifier * @pm_dev: Device pointer for pm runtime * @regmap: Register map of the device @@ -131,9 +140,12 @@ enum ina3221_ids { ina3221, sq52210 }; * @reg_config: Register value of INA3221_CONFIG * @summation_shunt_resistor: equivalent shunt resistor value for summation * @summation_channel_control: Value written to SCC field in INA3221_MASK_= ENABLE + * @current_lsb_uA: The value of one LSB corresponding to the current regi= ster + * @power_lsb_uW: The value of one LSB corresponding to the power register * @single_shot: running in single-shot operating mode */ struct ina3221_data { + const struct ina3221_config *config; enum ina3221_ids chip; =20 struct device *pm_dev; @@ -143,10 +155,30 @@ struct ina3221_data { u32 reg_config; int summation_shunt_resistor; u32 summation_channel_control; + long current_lsb_uA; + long power_lsb_uW; =20 bool single_shot; }; =20 +static const struct ina3221_config ina3221_config[] =3D { + [ina3221] =3D { + .has_current_reg =3D false, + .has_power_reg =3D false, + }, + [sq52210] =3D { + .has_current_reg =3D true, + .has_power_reg =3D true, + /* + * With this default value configuration, + * the following formula can be obtained: + * Current_LSB =3D Shunt_LSB / Rshunt + */ + .calibration_value =3D 256, + .power_lsb_factor =3D 20, + }, +}; + static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channe= l) { /* Summation channel checks shunt resistor values */ @@ -697,6 +729,25 @@ static const struct hwmon_chip_info ina3221_chip_info = =3D { }; =20 /* Extra attribute groups */ + +/* + * Calculate the value corresponding to one LSB of the current and + * power registers. + * formula : Current_LSB =3D Shunt_LSB / Rshunt + * Power_LSB =3D power_lsb_factor * Current_LSB + */ +static int ina3221_set_shunt(struct ina3221_data *ina, unsigned long val) +{ + if (!val || val > SQ52210_SHUNT_LSB) + return -EINVAL; + + ina->current_lsb_uA =3D DIV_ROUND_CLOSEST(SQ52210_SHUNT_LSB, val); + ina->power_lsb_uW =3D ina->config->power_lsb_factor * + ina->current_lsb_uA; + + return 0; +} + static ssize_t ina3221_shunt_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -730,6 +781,17 @@ static ssize_t ina3221_shunt_store(struct device *dev, /* Update summation_shunt_resistor for summation channel */ ina->summation_shunt_resistor =3D ina3221_summation_shunt_resistor(ina); =20 + /* + * The current and power registers can only be used when + * all enabled channels have identical shunt resistors + */ + if (ina->summation_shunt_resistor) { + if (ina->config->has_current_reg) { + ret =3D ina3221_set_shunt(ina, val); + if (ret < 0) + return ret; + } + } return count; } =20 @@ -846,6 +908,7 @@ static int ina3221_probe(struct i2c_client *client) return -ENOMEM; =20 ina->chip =3D chip; + ina->config =3D &ina3221_config[chip]; =20 ina->regmap =3D devm_regmap_init_i2c(client, &ina3221_regmap_config); if (IS_ERR(ina->regmap)) { @@ -892,6 +955,16 @@ static int ina3221_probe(struct i2c_client *client) ina->summation_channel_control |=3D BIT(14 - i); } =20 + /* + * The current and power registers can only be used when + * all enabled channels have identical shunt resistors + */ + if (ina->summation_shunt_resistor) { + ret =3D ina3221_set_shunt(ina, ina->summation_shunt_resistor); + if (ret < 0) + return ret; + } + ina->pm_dev =3D dev; dev_set_drvdata(dev, ina); =20 @@ -1009,6 +1082,18 @@ static int ina3221_resume(struct device *dev) dev_err(dev, "Unable to control summation channel\n"); return ret; } + /* + * The calibration register can only be enabled when all + * shunt resistor values are identical. + */ + if (ina->config->has_current_reg) { + ret =3D regmap_write(ina->regmap, SQ52210_CALIBRATION, + ina->config->calibration_value); + if (ret) { + dev_err(dev, "Unable to set calibration value\n"); + return ret; + } + } } =20 return 0; --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6E3E33624A4; Wed, 25 Feb 2026 09:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010283; cv=none; b=OdvCthYSBytRyUf9tjD4b+SQf7JfmV0IJvlsS65UtqxkXIiTnLeqW7LZ7LBAQJdyKXh6O++JCq8v1miWHdWragIfL9FEXXll4GMJMkJN3CRKyuJ/jOp0piMOH2lzn0w9ZRYwvuzeeQZxCHf20KR77z8LCsZpCmy7qgW+6boKtr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010283; c=relaxed/simple; bh=304TDv5014m7Ux4mfAAfqPq4f5IZIonYDuT/BoqTjzg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=npFGIguVLEgG5CJ6sH8IAPFQi/1n4MEDsy3Oc8FKECP7hPmS4qQ/eFDRi9AY7eiUmjabMVW2akjJuUhrBemDVgSOWHhhxxv165iZ6hcmGJ3+3DmKc0L2bSvav0eobcZ7DNZUY5ecftPX+sDOKS28MSCds98a9T2YWdOfef4kf2s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=jDTGTjo+; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="jDTGTjo+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=UmLJgRTsOfEcdmH LfWF0+EFCyL5MwH3DZWpb62HOov8=; b=jDTGTjo+BuOKXHhFMjQgcN8XkH/TvWu 9CF6+6AvvPyJq7o5jym2BHFrA/Sr4YuzDLApcH7TZ39uTuk6iBLYbfkRYQCwBW/z 7ufUeZdhY1NJZf079erX/SvBN0TLgIMdd3GNvv+zv25f1iSP88zlMOAZTWayZRxq urM/VxgtkoOk= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S6; Wed, 25 Feb 2026 17:03:39 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 4/8] hwmon: (ina3221) Support alert configuration Date: Wed, 25 Feb 2026 04:03:20 -0500 Message-Id: <20260225090324.112145-5-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxCF48KrWUGrWkCF4furW3KFg_yoW5Ar1kpF 4akw1rt3WIqF1Sgws7tan5KFyrt34xGFW2yrnrG34vqFsrJryjqw1kGa4DtFyYyrn5ZFyI qa9rtr4UCanrA3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0Jj3Ef5UUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbCwAvqvmmeuusnEAAA3O Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add alert configuration for initialization and resume. Signed-off-by: Wenliang Yan --- drivers/hwmon/ina3221.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index e99ac5ef4d8a..74dd937cc568 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -65,6 +65,8 @@ =20 #define INA3221_MASK_ENABLE_SCC_MASK GENMASK(14, 12) =20 +#define SQ52210_ALERT_CONFIG_MASK GENMASK(15, 4) + #define INA3221_CONFIG_DEFAULT 0x7127 #define INA3221_RSHUNT_DEFAULT 10000 #define SQ52210_SHUNT_LSB 40000000 /* pV/LSB */ @@ -123,6 +125,7 @@ struct ina3221_input { enum ina3221_ids { ina3221, sq52210 }; =20 struct ina3221_config { + bool has_alerts_add; /* chip has addtional alert functions */ bool has_current_reg; /* chip has internal current reg */ bool has_power_reg; /* chip has internal power reg */ int calibration_value; /* calculate current_lsb */ @@ -140,6 +143,7 @@ struct ina3221_config { * @reg_config: Register value of INA3221_CONFIG * @summation_shunt_resistor: equivalent shunt resistor value for summation * @summation_channel_control: Value written to SCC field in INA3221_MASK_= ENABLE + * @alert_type_select: Used to store the alert trigger type * @current_lsb_uA: The value of one LSB corresponding to the current regi= ster * @power_lsb_uW: The value of one LSB corresponding to the power register * @single_shot: running in single-shot operating mode @@ -155,6 +159,7 @@ struct ina3221_data { u32 reg_config; int summation_shunt_resistor; u32 summation_channel_control; + u32 alert_type_select; long current_lsb_uA; long power_lsb_uW; =20 @@ -163,10 +168,12 @@ struct ina3221_data { =20 static const struct ina3221_config ina3221_config[] =3D { [ina3221] =3D { + .has_alerts_add =3D false, .has_current_reg =3D false, .has_power_reg =3D false, }, [sq52210] =3D { + .has_alerts_add =3D true, .has_current_reg =3D true, .has_power_reg =3D true, /* @@ -948,6 +955,10 @@ static int ina3221_probe(struct i2c_client *client) ina->reg_config &=3D ~INA3221_CONFIG_CHx_EN(i); } =20 + /* Initialize alert_type_select */ + if (ina->config->has_alerts_add) + ina->alert_type_select =3D 0; + /* Initialize summation_shunt_resistor for summation channel control */ ina->summation_shunt_resistor =3D ina3221_summation_shunt_resistor(ina); for (i =3D 0; i < INA3221_NUM_CHANNELS; i++) { @@ -1096,6 +1107,17 @@ static int ina3221_resume(struct device *dev) } } =20 + /* Restore alert config register value to hardware */ + if (ina->config->has_alerts_add) { + ret =3D regmap_update_bits(ina->regmap, SQ52210_ALERT_CONFIG, + SQ52210_ALERT_CONFIG_MASK, + ina->alert_type_select & SQ52210_ALERT_CONFIG_MASK); + if (ret) { + dev_err(dev, "Unable to select alert type\n"); + return ret; + } + } + return 0; } =20 --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29B85361672; Wed, 25 Feb 2026 09:04:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010284; cv=none; b=TOveE6iOUNi9UctLZJisrIupOQEIv5/m2zWByhFa6hr2jq6H61Y+hqv+VrGNwD0ORrYi8nn/3r94Z2SqdzN0t73ddrR3p4a3TtycBbziDPcDkNhl+kdiZZuMd1KfER3QaU7yW5DdMpdFJw+lz+U+aaK4wGZSnYEge+mznbbkXio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010284; c=relaxed/simple; bh=nCEllRTgwqT6TYyQ3z6bOo7HSLfLd9xP3chQac1O5Ms=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=aWS9AKAb1Ro3o7KWZRj1sfaispw9+hPCSY4eTJGrtEQAle0Rn02lhjG8d1WPeezQ68qyh+bfUoztW/eNAAIbo3kIrKq9eMeY1s+zS8ojdbwtUy3eDJ9cadUYFKjEJRzMP0i1tjf8vFp0yeCzqNHgZPbFZ9h9JfpyBRGwlhT5m1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=RA+jCZHS; arc=none smtp.client-ip=117.135.210.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="RA+jCZHS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=pvGYLpM8IA+JsCH jNGR1R4xtXpqqfZiLFqmAEnFsfkY=; b=RA+jCZHSqhGOOo22XR8mpm7xJFo7E2k Sw6dtrC7r99xSGUJatkFvEeHWfU+56VIG/5Mws+xDs7PbPFeL7fTs4kErgK1JIdH DUgr4m9Z1fke59MfOvGUU5lgG1PC00xW9V6mfsE7Hzd4c0aF11OICBEYHN+g3TeT g3gRHCTeRs3g= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S7; Wed, 25 Feb 2026 17:03:40 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Shuah Khan , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 5/8] hwmon: (ina3221) Introduce power attribute and alert characteristics Date: Wed, 25 Feb 2026 04:03:21 -0500 Message-Id: <20260225090324.112145-6-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S7 X-Coremail-Antispam: 1Uf129KBjvJXoWxtryfCryfCF48WFW5WF47CFg_yoW7WrW5pa ykX3yfJr18Ar93Zw4xKF4UXFn8t3yxGay7Jr1I9393J3ZrAr1vqr48K3W0qas0kryfur1F k34xXrWrGr13GrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0Jj9MabUUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbCvw7rv2meuu6uwgAA3q Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" SQ52210 has built-in current and power sensors as well as multiple alert functions. Add power attributes and different critical characteristics in hwmon to report the corresponding data. Signed-off-by: Wenliang Yan --- Documentation/hwmon/ina3221.rst | 24 ++++++++++++++ drivers/hwmon/ina3221.c | 57 ++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/Documentation/hwmon/ina3221.rst b/Documentation/hwmon/ina3221.= rst index 8c12c54d2c24..87637d7276ec 100644 --- a/Documentation/hwmon/ina3221.rst +++ b/Documentation/hwmon/ina3221.rst @@ -13,6 +13,13 @@ Supported chips: =20 https://www.ti.com/ =20 + * Silergy SQ52210 + + Prefix: 'SQ52210' + + Addresses: I2C 0x40 - 0x43 + + Author: Andrew F. Davis =20 Description @@ -23,6 +30,9 @@ side of up to three D.C. power supplies. The INA3221 moni= tors both shunt drop and supply voltage, with programmable conversion times and averaging, curr= ent and power are calculated host-side from these. =20 +The SQ52210 is a mostly compatible chip from Silergy. It incorporates inte= rnal +current and power registers, and provides an extra configurable alert func= tion. + Sysfs entries ------------- =20 @@ -72,3 +82,17 @@ update_interval Data conversion time in millisec= ond, following: Note that setting update_interval to 0ms sets both= BC and SC to 140 us (minimum conversion time). =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +Additional sysfs entries for sq52210 +------------------------------------- + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D +in[123]_crit Critical high bus voltage(mV) +in[123]_crit_alarm Bus voltage critical high alarm +in[123]_lcrit Critical low bus voltage(mV) +in[123]_lcrit_alarm Bus voltage critical low alarm +power[123]_input Power(uW) for channels 1, 2, and 3 respectively +power[123]_crit Critical high power(uW) +power[123]_crit_alarm Power critical high alarm +curr[123]_lcrit Critical low current(mA) +curr[123]_lcrit_alarm Current critical low alarm diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 74dd937cc568..a39f5f2c486b 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -645,6 +645,8 @@ static umode_t ina3221_is_visible(const void *drvdata, { const struct ina3221_data *ina =3D drvdata; const struct ina3221_input *input =3D NULL; + bool has_alerts_add =3D ina->config->has_alerts_add; + bool has_power_reg =3D ina->config->has_power_reg; =20 switch (type) { case hwmon_chip: @@ -672,6 +674,16 @@ static umode_t ina3221_is_visible(const void *drvdata, return 0444; case hwmon_in_enable: return 0644; + case hwmon_in_crit: + case hwmon_in_lcrit: + if (has_alerts_add) + return 0644; + return 0; + case hwmon_in_crit_alarm: + case hwmon_in_lcrit_alarm: + if (has_alerts_add) + return 0444; + return 0; default: return 0; } @@ -684,6 +696,31 @@ static umode_t ina3221_is_visible(const void *drvdata, case hwmon_curr_crit: case hwmon_curr_max: return 0644; + case hwmon_curr_lcrit_alarm: + if (has_alerts_add) + return 0444; + return 0; + case hwmon_curr_lcrit: + if (has_alerts_add) + return 0644; + return 0; + default: + return 0; + } + case hwmon_power: + switch (attr) { + case hwmon_power_input: + if (has_power_reg) + return 0444; + return 0; + case hwmon_power_crit_alarm: + if (has_power_reg) + return 0444; + return 0; + case hwmon_power_crit: + if (has_power_reg) + return 0644; + return 0; default: return 0; } @@ -694,7 +731,14 @@ static umode_t ina3221_is_visible(const void *drvdata, =20 #define INA3221_HWMON_CURR_CONFIG (HWMON_C_INPUT | \ HWMON_C_CRIT | HWMON_C_CRIT_ALARM | \ - HWMON_C_MAX | HWMON_C_MAX_ALARM) + HWMON_C_MAX | HWMON_C_MAX_ALARM | \ + HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM) +#define SQ52210_HWMON_POWER_CONFIG (HWMON_P_INPUT | \ + HWMON_P_CRIT | HWMON_P_CRIT_ALARM) +#define SQ52210_HWMON_BUS_CONFIG (HWMON_I_INPUT | \ + HWMON_I_ENABLE | HWMON_I_LABEL | \ + HWMON_I_LCRIT_ALARM | HWMON_I_LCRIT |\ + HWMON_I_CRIT_ALARM | HWMON_I_CRIT) =20 static const struct hwmon_channel_info * const ina3221_info[] =3D { HWMON_CHANNEL_INFO(chip, @@ -704,9 +748,9 @@ static const struct hwmon_channel_info * const ina3221_= info[] =3D { /* 0: dummy, skipped in is_visible */ HWMON_I_INPUT, /* 1-3: input voltage Channels */ - HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, - HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, - HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL, + SQ52210_HWMON_BUS_CONFIG, + SQ52210_HWMON_BUS_CONFIG, + SQ52210_HWMON_BUS_CONFIG, /* 4-6: shunt voltage Channels */ HWMON_I_INPUT, HWMON_I_INPUT, @@ -720,6 +764,11 @@ static const struct hwmon_channel_info * const ina3221= _info[] =3D { INA3221_HWMON_CURR_CONFIG, /* 4: summation of current channels */ HWMON_C_INPUT | HWMON_C_CRIT | HWMON_C_CRIT_ALARM), + HWMON_CHANNEL_INFO(power, + /* 1-3: power channels*/ + SQ52210_HWMON_POWER_CONFIG, + SQ52210_HWMON_POWER_CONFIG, + SQ52210_HWMON_POWER_CONFIG), NULL }; =20 --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE8A434D391; Wed, 25 Feb 2026 09:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010262; cv=none; b=I+yRbSWhYN23hBAlE38iuRTqIR6Y17uTskLMWd4ywYtOmdMlqHaNcplR4WfxPsaJkdADOoDYPLfT/qDeb2GLnb4fRwWCDH7UzGhNRTQ4GzrwfgZRsW3YiZS7/Hhr8zqW9x78rvja5h0El4rPP+v5L1xt566WBhe00sk6yjNzF+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010262; c=relaxed/simple; bh=t5/QwQYt22eFXR46Ijlg3v8DLJ9gFOA2B6JbekDWvzQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=ECDwiAq4MWPVtvd7QphEBUBOEHyQzP7Wgy6SGP1XQ+6RxSnZ2rfz8wyOnpMk9gSNYpE4gTyHoPqncV9/FAOGTZg3cCYAB8FfNYx5qSBTD1IrTX+fdokjt2MWS22nUTD8sM4KP0X+Evlbvy4Dn13/kFao6pSU8wNKBs1GOlU1lco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=TPpA52ly; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="TPpA52ly" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=m7xNvsnxQ1kna7y N70bTSrV7qCYirDsYr8MQNwShets=; b=TPpA52lyAr0VzPWw5rhVKnuYWU8e6/k rAN+x1a1mjum62+3Rk41nm7gYGrJGXRsVXR/dl6dBGFLv0gvwLgPrwIYbtZd9DCN H0XooS9soPXdZhBsd9Myy2+3PM/Dw5iHVLQ1v1YquG420UZsWS8NrrqIB0mt7T9b s3DZHqyNDnH0= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S8; Wed, 25 Feb 2026 17:03:43 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 6/8] hwmon: (ina3221) Modify the 'ina3221_read_value' function Date: Wed, 25 Feb 2026 04:03:22 -0500 Message-Id: <20260225090324.112145-7-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S8 X-Coremail-Antispam: 1Uf129KBjvJXoWxCF4rJw1UXF4UJFy3Ar43ZFb_yoW5uryxpa 4fCF1Fyr17tr1IqwsakFn5GFyrAryxW34jqrnrK3yxZa1UJ3W0gw1ktw4Fvr1rC3Z8ZF1k X3srWw48u3ZrJFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JUXeOdUUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbC6A-rv2meuu85igAA3i Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" SQ52210 adds power, current, and limit registers. The ina3221_read_value function has been refactored to adapt to the new register data reading format. Signed-off-by: Wenliang Yan --- drivers/hwmon/ina3221.c | 67 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index a39f5f2c486b..77fc0dbef40d 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -66,6 +66,14 @@ #define INA3221_MASK_ENABLE_SCC_MASK GENMASK(14, 12) =20 #define SQ52210_ALERT_CONFIG_MASK GENMASK(15, 4) +#define SQ52210_MASK_ALERT_CHANNEL1 (BIT(15) | BIT(12) | BIT(9) | BIT(6)) +#define SQ52210_MASK_ALERT_CHANNEL2 (BIT(14) | BIT(11) | BIT(8) | BIT(5)) +#define SQ52210_MASK_ALERT_CHANNEL3 (BIT(13) | BIT(10) | BIT(7) | BIT(4)) + +#define SQ52210_ALERT_ALL_SUL_MASK (BIT(15) | BIT(14) | BIT(13)) +#define SQ52210_ALERT_ALL_BOL_MASK (BIT(12) | BIT(11) | BIT(10)) +#define SQ52210_ALERT_ALL_BUL_MASK (BIT(9) | BIT(8) | BIT(7)) +#define SQ52210_ALERT_ALL_POL_MASK (BIT(6) | BIT(5) | BIT(4)) =20 #define INA3221_CONFIG_DEFAULT 0x7127 #define INA3221_RSHUNT_DEFAULT 10000 @@ -108,6 +116,13 @@ enum ina3221_channels { INA3221_NUM_CHANNELS }; =20 +enum sq52210_alert_types { + SQ52210_ALERT_SUL, + SQ52210_ALERT_BOL, + SQ52210_ALERT_BUL, + SQ52210_ALERT_POL +}; + /** * struct ina3221_input - channel input source specific information * @label: label of channel input source @@ -284,13 +299,59 @@ static int ina3221_read_value(struct ina3221_data *in= a, unsigned int reg, =20 /* * Shunt Voltage Sum register has 14-bit value with 1-bit shift + * Current registers have 15-bit value + * Power registers have 16-bit value + * ALERT_LIMIT registers have 16-bit value with 3-bit shift * Other Shunt Voltage registers have 12 bits with 3-bit shift */ - if (reg =3D=3D INA3221_SHUNT_SUM || reg =3D=3D INA3221_CRIT_SUM) + switch (reg) { + case INA3221_SHUNT_SUM: + case INA3221_CRIT_SUM: *val =3D sign_extend32(regval >> 1, 14); - else + break; + case SQ52210_CURRENT1: + case SQ52210_CURRENT2: + case SQ52210_CURRENT3: + *val =3D sign_extend32(regval, 15); + break; + case SQ52210_POWER1: + case SQ52210_POWER2: + case SQ52210_POWER3: + *val =3D regval; + break; + case INA3221_BUS1: + case INA3221_BUS2: + case INA3221_BUS3: + case INA3221_SHUNT1: + case INA3221_SHUNT2: + case INA3221_SHUNT3: + case INA3221_WARN1: + case INA3221_WARN2: + case INA3221_WARN3: + case INA3221_CRIT1: + case INA3221_CRIT2: + case INA3221_CRIT3: *val =3D sign_extend32(regval >> 3, 12); - + break; + case SQ52210_ALERT_LIMIT1: + case SQ52210_ALERT_LIMIT2: + case SQ52210_ALERT_LIMIT3: + /* + * This register is a 16-bit register with the lower 3 bits fixed at 0. + * When used to store bus and shunt alert values, shifting is required. + * However, for POL (Power Over Limit), it functions as a 16-bit unsigned + * register where the lower 3 bits being fixed at 0 will result in some + * loss of precision. + */ + if (ina->alert_type_select & SQ52210_ALERT_ALL_POL_MASK) + *val =3D regval; + else + *val =3D sign_extend32(regval >> 3, 12); + break; + default: + *val =3D 0; + return -EOPNOTSUPP; + }; return 0; } =20 --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE25E3644B6; Wed, 25 Feb 2026 09:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010271; cv=none; b=qipvwteHdfv7rrXPdpwhx5RIfkikLsjphigNQhwEqtCKeB9QHzaYsTgQ15wgxEnsuwoA+G+/KNlnwEiQ/XlPYR5BOBQKwWwMqgEiXKrMDQjjUfsPz/aC0zO/pZufoDY3cGyLzXxF7dkl+3qqRWfM2Kdj7rRvdk7LEvAx/h+fwzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010271; c=relaxed/simple; bh=tp3l1avA8e9jgeR4pVoG5isE+y+k5WwP3guUZqvv8Ok=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=RRnHcHTU0vWCHNot36JJoH75EU3ZQefQEOnvqid4i687/ExUCDDGxRQMNVkfIK1KBInDFpUr9v8dkyZ7KqqeyKmh5NRPDNJEr6cI5HM68/Olx3X7ypJiV+6A06+9ZnzTb4f5VFNRpim1LunEMNxH/5094ZmEJ32h0PWZ1iu5mnI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=G06T08xn; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="G06T08xn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=2Tpkh8zZpeow3al c0exR9o2ZaMq8veOz9PoJbbCPifU=; b=G06T08xnAxaC6w7aY5Gck8UHJxHqZ2S +zbLrNU6m3ygHDcQ2b2SfMHOFxti0Dsaz9GiExbJ/DSbgCl1iCnqe52QeEN5+3fD Z7BlTPrj1R8mzvKJdWWY1hngBgPGNnhb0u5s1yvGs9rGM6SkjaE2KJL0+aPvSI8Z Uml++TFHKW/M= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S9; Wed, 25 Feb 2026 17:03:44 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 7/8] hwmon: (ina3221) Support alert_limit_write function and write/read functions for 'power' attribute Date: Wed, 25 Feb 2026 04:03:23 -0500 Message-Id: <20260225090324.112145-8-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S9 X-Coremail-Antispam: 1Uf129KBjvJXoW3JrWxtr4fAry3Jw1fCr4rZrb_yoW3Wryfp3 yxCa4rtr17tr1furs2kFs8G3WFvw4xX3y2yr9rK3sava1UAasY9ryrta4vyFyUGr1fZF47 Xay7AFy5uanrGFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JUFtC7UUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbC5xDswGmeuvAyCgAA3e Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Each channel supports four new alert trigger modes, but only one trigger mode can be active at any given time. Alert values are stored in the same register. The sq52210_alert_limit_write function has been added to write alert threshold values and configure alert source type. 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 --- drivers/hwmon/ina3221.c | 176 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 77fc0dbef40d..f32f0f18e9b8 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, =20 + /* 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[] =3D= { [F_CF3] =3D REG_FIELD(INA3221_MASK_ENABLE, 7, 7), [F_CF2] =3D REG_FIELD(INA3221_MASK_ENABLE, 8, 8), [F_CF1] =3D REG_FIELD(INA3221_MASK_ENABLE, 9, 9), + + [F_AFF3] =3D REG_FIELD(SQ52210_ALERT_CONFIG, 1, 1), + [F_AFF2] =3D REG_FIELD(SQ52210_ALERT_CONFIG, 2, 2), + [F_AFF1] =3D REG_FIELD(SQ52210_ALERT_CONFIG, 3, 3), }; =20 enum ina3221_channels { @@ -123,6 +130,18 @@ enum sq52210_alert_types { SQ52210_ALERT_POL }; =20 +static const u32 alert_groups[] =3D { + SQ52210_MASK_ALERT_CHANNEL1, + SQ52210_MASK_ALERT_CHANNEL2, + SQ52210_MASK_ALERT_CHANNEL3, +}; + +static const u8 limit_regs[] =3D { + SQ52210_ALERT_LIMIT1, + SQ52210_ALERT_LIMIT2, + SQ52210_ALERT_LIMIT3, +}; + /** * struct ina3221_input - channel input source specific information * @label: label of channel input source @@ -504,6 +523,145 @@ static int ina3221_read_curr(struct device *dev, u32 = attr, } } =20 +static const u8 ina3221_power_reg[][INA3221_NUM_CHANNELS] =3D { + [hwmon_power_input] =3D { SQ52210_POWER1, SQ52210_POWER2, SQ52210_POWER3 = }, + [hwmon_power_crit] =3D { SQ52210_ALERT_LIMIT1, SQ52210_ALERT_LIMIT2, + SQ52210_ALERT_LIMIT3 }, + [hwmon_power_crit_alarm] =3D { F_AFF1, F_AFF2, F_AFF3 }, +}; + +static int ina3221_read_power(struct device *dev, u32 attr, int channel, l= ong *val) +{ + struct ina3221_data *ina =3D dev_get_drvdata(dev); + u8 reg =3D 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 =3D ina3221_wait_for_data(ina); + if (ret) + return ret; + } + + fallthrough; + case hwmon_power_crit: + ret =3D ina3221_read_value(ina, reg, ®val); + if (ret) + return ret; + /* Return power in uW */ + *val =3D (u64)regval * (u64)ina->power_lsb_uW; + 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 =3D 0; + return 0; + } + + ret =3D regmap_field_read(ina->fields[reg], ®val); + if (ret) + return ret; + *val =3D regval; + return 0; + default: + return -EOPNOTSUPP; + } +} + +static int sq52210_alert_limit_write(struct ina3221_data *ina, + enum sq52210_alert_types type, int channel, long val) +{ + struct regmap *regmap =3D ina->regmap; + int item =3D channel % INA3221_NUM_CHANNELS; + u8 limit_reg; + u32 alert_group, alert_mask =3D 0; + int regval =3D 0; + int ret; + + if (item >=3D ARRAY_SIZE(alert_groups) || ((type =3D=3D SQ52210_ALERT_POL= ) && val < 0)) + return -EINVAL; + + alert_group =3D alert_groups[item]; + limit_reg =3D limit_regs[item]; + + /* Clear alerts for this channel group first */ + ret =3D regmap_update_bits(regmap, SQ52210_ALERT_CONFIG, alert_group, 0); + if (ret) + return ret; + + /* Determine alert type and calculate register value */ + switch (type) { + /* + * The alert warning logic is implemented by comparing the limit register= values + * with the corresponding alert source register values. The shunt voltage= and bus + * voltage are 13-bit signed values, while power is a 16-bit unsigned val= ue. + * However, the lower 3 bits of the limit register default to 0. Therefor= e, when + * setting warning values, the lower 3 bits will be forced to 0. + * The conversion formulas for the corresponding register values are: + * bus_voltage: (regval / 8mV) << 3 =3D=3D (regval / 1mV) + * shunt_voltage: (regval / 40uV) << 3 =3D=3D (regval / 5uV) + * current: (regval / current_lsb) & 0xfff8 + * power: (regval / current_lsb) & 0xfff8 + */ + case SQ52210_ALERT_SUL: + /* + * For SUL configuration, directly convert it to current + * settings implemented in the ina3221_write_curr function. + */ + return -EOPNOTSUPP; + case SQ52210_ALERT_BOL: + /* BOL: Bus Over Limit - BIT(12), BIT(11), BIT(10) */ + alert_mask =3D BIT(12 - item); + /* Bus Register, signed register */ + regval =3D val & 0xfff8; + regval =3D clamp_val(regval, -32760, 32760); + break; + case SQ52210_ALERT_BUL: + /* BUL: Bus Under Limit - BIT(9), BIT(8), BIT(7) */ + alert_mask =3D BIT(9 - item); + /* Bus Register, signed register */ + regval =3D val & 0xfff8; + regval =3D clamp_val(regval, -32760, 32760); + break; + case SQ52210_ALERT_POL: + /* POL: Power Over Limit - BIT(6), BIT(5), BIT(4) */ + alert_mask =3D BIT(6 - item); + /* Power Register, unsigned register */ + regval =3D (u32)DIV_U64_ROUND_CLOSEST((u64)val, ina->power_lsb_uW); + regval =3D clamp_val(regval, 0, 65528) & 0xfff8; + break; + default: + /* For unsupported attributes, just clear the configuration */ + ina->alert_type_select &=3D ~alert_group; + return -EOPNOTSUPP; + } + + /* Write limit register value */ + ret =3D regmap_write(regmap, limit_reg, regval); + if (ret) + return ret; + + /* Update alert configuration if limit value is non-zero */ + if (regval) { + ina->alert_type_select =3D (ina->alert_type_select & ~alert_group) | ale= rt_mask; + ret =3D regmap_update_bits(regmap, SQ52210_ALERT_CONFIG, + alert_group, alert_mask); + } else { + ina->alert_type_select &=3D ~alert_group; + } + + return ret; +} + static int ina3221_write_chip(struct device *dev, u32 attr, long val) { struct ina3221_data *ina =3D dev_get_drvdata(dev); @@ -640,6 +798,18 @@ static int ina3221_write_enable(struct device *dev, in= t channel, bool enable) return ret; } =20 +static int ina3221_write_power(struct device *dev, u32 attr, int channel, = long val) +{ + struct ina3221_data *ina =3D dev_get_drvdata(dev); + + switch (attr) { + case hwmon_power_crit: + return sq52210_alert_limit_write(ina, SQ52210_ALERT_POL, channel, val); + default: + return 0; + } +} + static int ina3221_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { @@ -656,6 +826,9 @@ static int ina3221_read(struct device *dev, enum hwmon_= sensor_types type, case hwmon_curr: ret =3D ina3221_read_curr(dev, attr, channel, val); break; + case hwmon_power: + ret =3D ina3221_read_power(dev, attr, channel, val); + break; default: ret =3D -EOPNOTSUPP; break; @@ -679,6 +852,9 @@ static int ina3221_write(struct device *dev, enum hwmon= _sensor_types type, case hwmon_curr: ret =3D ina3221_write_curr(dev, attr, channel, val); break; + case hwmon_power: + ret =3D ina3221_write_power(dev, attr, channel, val); + break; default: ret =3D -EOPNOTSUPP; break; --=20 2.17.1 From nobody Thu Apr 16 22:35:29 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C0F83624C8; Wed, 25 Feb 2026 09:04:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010265; cv=none; b=Q4VxQouKyYKNWf7qfiX5jT1pRQZVRj4SQbSG8BV9Nxl9fw6JOLGr6OR5x40ZJeFKsLwqrZF7MO1xSsCmOkG/xs4UFOOnLTLZsK+usU5Y1E00WdY4HdNnoaoqCtKZUVssQih+/bEtS9mvMl36drWchsRSxuLVGZx+dzLWvpN0qD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772010265; c=relaxed/simple; bh=Gt75cHT6L5cWi+x7dEuEj42L4ZWqSz69H0wXJAxT940=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=d9QhlF5m++cDlDttbtGCdQu2dQld6MTVZiJfANejV8bic8FRtVyPk0FXuDcqTDOmBirs8brDk8R+yFvAWem+Y3v4EJIDcgq9sHqRs9MGhVBcWcLMau71qvLiB2BTuSv3enxjJxbIQdHhMxKJZMO85E1+CHEYJjP6S9cRLx6mw1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=RuXe0sxu; arc=none smtp.client-ip=117.135.210.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="RuXe0sxu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id; bh=Ifg6aglyA5pgVBE 3C7J7/qZP05v0Bp5pksaS0RkFfIk=; b=RuXe0sxud9mWaLcAg5gx9TyIpDMN6Vm nzgixAGNCVkNsBcd8AtCV9/klzvf57ctJGM49TKI0aRaV6LsLXktFfUvu5d2+RLy MSE7a1dcPGMoA8/spfiy/90FUWP0MhMCKx/tT3jc6zu3oTg8nf3WYaxEVz9kkq9G aTSgewqhdu6Q= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-1 (Coremail) with SMTP id _____wD3N4nlup5pilgzMg--.30498S10; Wed, 25 Feb 2026 17:03:45 +0800 (CST) From: Wenliang Yan To: linux@roeck-us.net, Jean Delvare Cc: Wenliang Yan , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 8/8] hwmon: (ina3221) Modify write/read functions for 'in' and 'curr' attribute Date: Wed, 25 Feb 2026 04:03:24 -0500 Message-Id: <20260225090324.112145-9-wenliang202407@163.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260225090324.112145-1-wenliang202407@163.com> References: <20260225090324.112145-1-wenliang202407@163.com> X-CM-TRANSID: _____wD3N4nlup5pilgzMg--.30498S10 X-Coremail-Antispam: 1Uf129KBjvJXoW3Gr4UWFW7tryxXr4ruFWxWFg_yoWxWw1fp3 y5CFWrtrWjq3WSgrWIkFs8WFn8trWxW3y2yryDK39YvF4UAr909F95G3Wq9a45Cr93XF4x J3y7trWUua1DtFUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JUXeOdUUUUU= X-CM-SenderInfo: xzhqzxhdqjjiisuqlqqrwthudrp/xtbC5xLswGmeuvIyLgAA36 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Modified the relevant read/write functions for 'in' and 'curr' attributes, adding support for crit, lcrit, crit_alarm, and lcrit_alarm features. Signed-off-by: Wenliang Yan --- drivers/hwmon/ina3221.c | 109 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index f32f0f18e9b8..bb01a698ebe6 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -142,6 +142,12 @@ static const u8 limit_regs[] =3D { SQ52210_ALERT_LIMIT3, }; =20 +static const u8 alert_flag[] =3D { + F_AFF1, + F_AFF2, + F_AFF3, +}; + /** * struct ina3221_input - channel input source specific information * @label: label of channel input source @@ -446,6 +452,42 @@ static int ina3221_read_in(struct device *dev, u32 att= r, int channel, long *val) case hwmon_in_enable: *val =3D ina3221_is_enabled(ina, channel); return 0; + case hwmon_in_crit: + case hwmon_in_lcrit: + /* Only for Bus Voltage */ + if (channel >=3D INA3221_NUM_CHANNELS) + return -EOPNOTSUPP; + + if (!ina3221_is_enabled(ina, channel)) + return -ENODATA; + + reg =3D limit_regs[channel]; + ret =3D ina3221_read_value(ina, reg, ®val); + if (ret) + return ret; + /* + * Scale of bus voltage (mV): LSB is 8mV + */ + *val =3D regval * 8; + return 0; + case hwmon_in_crit_alarm: + case hwmon_in_lcrit_alarm: + /* Only for Bus Voltage */ + if (channel >=3D INA3221_NUM_CHANNELS) + return -EOPNOTSUPP; + /* No actual register read if channel is disabled */ + if (!ina3221_is_enabled(ina, channel)) { + /* Return 0 for alert flags */ + *val =3D 0; + return 0; + } + + reg =3D alert_flag[channel]; + ret =3D regmap_field_read(ina->fields[reg], ®val); + if (ret) + return ret; + *val =3D regval; + return 0; default: return -EOPNOTSUPP; } @@ -457,8 +499,16 @@ static const u8 ina3221_curr_reg[][INA3221_NUM_CHANNEL= S + 1] =3D { [hwmon_curr_max] =3D { INA3221_WARN1, INA3221_WARN2, INA3221_WARN3, 0 }, [hwmon_curr_crit] =3D { INA3221_CRIT1, INA3221_CRIT2, INA3221_CRIT3, INA3221_CRIT_SUM }, + [hwmon_curr_lcrit] =3D { SQ52210_ALERT_LIMIT1, SQ52210_ALERT_LIMIT2, + SQ52210_ALERT_LIMIT3, 0 }, [hwmon_curr_max_alarm] =3D { F_WF1, F_WF2, F_WF3, 0 }, [hwmon_curr_crit_alarm] =3D { F_CF1, F_CF2, F_CF3, F_SF }, + [hwmon_curr_lcrit_alarm] =3D { F_AFF1, F_AFF2, F_AFF3, 0 }, +}; +static const u8 sq52210_curr_reg[INA3221_NUM_CHANNELS] =3D { + SQ52210_CURRENT1, + SQ52210_CURRENT2, + SQ52210_CURRENT3 }; =20 static int ina3221_read_curr(struct device *dev, u32 attr, @@ -467,6 +517,7 @@ static int ina3221_read_curr(struct device *dev, u32 at= tr, struct ina3221_data *ina =3D dev_get_drvdata(dev); struct ina3221_input *input =3D ina->inputs; u8 reg =3D ina3221_curr_reg[attr][channel]; + bool has_current_reg =3D ina->config->has_current_reg; int resistance_uo, voltage_nv; int regval, ret; =20 @@ -489,10 +540,20 @@ static int ina3221_read_curr(struct device *dev, u32 = attr, if (ret) return ret; } + if (has_current_reg =3D=3D true) { + reg =3D sq52210_curr_reg[channel]; + ret =3D ina3221_read_value(ina, reg, ®val); + if (ret) + return ret; + /* Return current in mA */ + *val =3D DIV_U64_ROUND_CLOSEST((u64)regval * (u64)ina->current_lsb_uA, = 1000); + return 0; + } =20 fallthrough; case hwmon_curr_crit: case hwmon_curr_max: + case hwmon_curr_lcrit: if (!resistance_uo) return -ENODATA; =20 @@ -507,6 +568,7 @@ static int ina3221_read_curr(struct device *dev, u32 at= tr, return 0; case hwmon_curr_crit_alarm: case hwmon_curr_max_alarm: + case hwmon_curr_lcrit_alarm: /* No actual register read if channel is disabled */ if (!ina3221_is_enabled(ina, channel)) { /* Return 0 for alert flags */ @@ -617,7 +679,9 @@ static int sq52210_alert_limit_write(struct ina3221_dat= a *ina, * For SUL configuration, directly convert it to current * settings implemented in the ina3221_write_curr function. */ - return -EOPNOTSUPP; + alert_mask =3D BIT(15 - channel); + regval =3D val; + break; case SQ52210_ALERT_BOL: /* BOL: Bus Over Limit - BIT(12), BIT(11), BIT(10) */ alert_mask =3D BIT(12 - item); @@ -711,12 +775,18 @@ static int ina3221_write_curr(struct device *dev, u32= attr, struct ina3221_input *input =3D ina->inputs; u8 reg =3D ina3221_curr_reg[attr][channel]; int resistance_uo, current_ma, voltage_uv; - int regval; + int regval, ret; =20 - if (channel > INA3221_CHANNEL3) - resistance_uo =3D ina->summation_shunt_resistor; - else + if (attr =3D=3D hwmon_curr_lcrit) { + if (channel > INA3221_CHANNEL3) + return -EOPNOTSUPP; resistance_uo =3D input[channel].shunt_resistor; + } else { + if (channel > INA3221_CHANNEL3) + resistance_uo =3D ina->summation_shunt_resistor; + else + resistance_uo =3D input[channel].shunt_resistor; + } =20 if (!resistance_uo) return -EOPNOTSUPP; @@ -747,7 +817,12 @@ static int ina3221_write_curr(struct device *dev, u32 = attr, else regval =3D DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8; =20 - return regmap_write(ina->regmap, reg, regval); + if (attr =3D=3D hwmon_curr_lcrit) + ret =3D sq52210_alert_limit_write(ina, SQ52210_ALERT_SUL, channel, regva= l); + else + ret =3D regmap_write(ina->regmap, reg, regval); + + return ret; } =20 static int ina3221_write_enable(struct device *dev, int channel, bool enab= le) @@ -798,6 +873,26 @@ static int ina3221_write_enable(struct device *dev, in= t channel, bool enable) return ret; } =20 +static int ina3221_write_in(struct device *dev, u32 attr, int channel, lon= g val) +{ + struct ina3221_data *ina =3D dev_get_drvdata(dev); + + if (attr =3D=3D hwmon_in_lcrit || attr =3D=3D hwmon_in_crit) + if (channel >=3D INA3221_NUM_CHANNELS) + return -EOPNOTSUPP; + + switch (attr) { + case hwmon_in_lcrit: + return sq52210_alert_limit_write(ina, SQ52210_ALERT_BUL, channel, val); + case hwmon_in_crit: + return sq52210_alert_limit_write(ina, SQ52210_ALERT_BOL, channel, val); + case hwmon_in_enable: + return ina3221_write_enable(dev, channel, val); + default: + return 0; + } +} + static int ina3221_write_power(struct device *dev, u32 attr, int channel, = long val) { struct ina3221_data *ina =3D dev_get_drvdata(dev); @@ -847,7 +942,7 @@ static int ina3221_write(struct device *dev, enum hwmon= _sensor_types type, break; case hwmon_in: /* 0-align channel ID */ - ret =3D ina3221_write_enable(dev, channel - 1, val); + ret =3D ina3221_write_in(dev, attr, channel - 1, val); break; case hwmon_curr: ret =3D ina3221_write_curr(dev, attr, channel, val); --=20 2.17.1