public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6 0/4]  hwmon:(ina238)Add support for SQ52206
@ 2025-03-27 13:18 Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 1/4] hwmon:(ina238)Add ina238_config to save configurations for different chips Wenliang Yan
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-27 13:18 UTC (permalink / raw)
  To: linux, jdelvare, christophe.jaillet
  Cc: Wenliang Yan, robh, krzk+dt, conor+dt, corbet, linux-hwmon,
	linux-kernel

Add support for Silergy i2c power monitor SQ52206 to the ina238
driver as those two are similar.

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
---

Add new chip SQ52206, the datasheet depends on 
https://us1.silergy.com/cloud/index/uniqid/676b659b4a503
The password is fx6NEe.

Changes in v6:
- Split the patch and explain in detail how the formula changes and the reasons for adding content.
- Link to v5: https://lore.kernel.org/linux-hwmon/20250313075501.5435-1-wenliang202407@163.com/

Changes in v5:
- Add the description of change log in PATCH 0 and PATCH 2.
- Link to v4: https://lore.kernel.org/linux-hwmon/20250125030300.1230967-1-wenliang202407@163.com/

Changes in v4:
- Add 'Acked-by: Krzysztof Kozlowski' information to PATCH 1.

- Formatting adjustments have been made to ina2xx.c in PATCH 2.
Including:
1.Change 'sprintf()' in the 'energy1_input_show()' to 'sysfs_emit()'.
2.Move up the "enum ina238_ids chip" line in the 'ina238_probe()' to keep RCT style.
3.Remove the last comma when describing 'ina238_of_match[]' to keep the
format consistent with the 'i2c_device_id ina238_id[]' structure.
4.Change the '5bytes' in the description to '5 bytes'.

- Link to v3: https://lore.kernel.org/linux-hwmon/20250117082017.688212-1-wenliang202407@163.com/

Changes in v3:
- Add the description of the different features of SQ52206 in the ina2xx.yaml(PATCH 1).

- Change program logic errors in ina238.c(PATCH 2).
Including:
1.Change 'break' to 'return 0' after 'if (has_power_highest)'.
2.Add {} after 'if (chip == sq52206)' in 'ina238_probe(struct i2c_client *client)'.
3.Change 'data->config->has_energy ? NULL : ina238_groups' to 
'data->config->has_energy ? ina238_groups : NULL'

- Link to v2: https://lore.kernel.org/linux-hwmon/20250113035023.365697-1-wenliang202407@163.com/

Changes in v2:
- Explain why sq52206 compatibility has been added to ina2xx.yaml.

- addressed various review comments.

- Link to v1: https://lore.kernel.org/linux-hwmon/20241224063559.391061-1-wenliang202407@163.com/

Wenliang Yan (4):
  hwmon:(ina238)Add ina238_config to save configurations for different
    chips
  hwmon:(ina238)Add support for SQ52206
  hwmon:(ina238)Modify the calculation formula to adapt to different
    chips
  dt-bindings:Add SQ52206 to ina2xx devicetree bindings

 .../devicetree/bindings/hwmon/ti,ina2xx.yaml  |   5 +
 Documentation/hwmon/ina238.rst                |  15 ++
 drivers/hwmon/ina238.c                        | 211 +++++++++++++++---
 3 files changed, 198 insertions(+), 33 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v6 1/4] hwmon:(ina238)Add ina238_config to save configurations for different chips
  2025-03-27 13:18 [PATCH v6 0/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
@ 2025-03-27 13:18 ` Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 2/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-27 13:18 UTC (permalink / raw)
  To: linux, Christophe JAILLET, Jean Delvare
  Cc: Wenliang Yan, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Jonathan Corbet, linux-hwmon, linux-kernel

Add structure ina238_config to store proprietary properties for different
chips to meet different chip adaptations

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
---

v5->v6:Split the patch, this patch add a structure with the following contents: 
1. 'has_power_highest' is used to determine whether peak power can be
detected (New features for SQ52206)
2. 'has_energy' is used to determine non-energy detection energy
consumption (new feature for SQ52206)
3. 'temp_shift' is used to distinguish the data bits of the
temperature register
4. 'power_calculate_factor' is used to calculate power
5. 'config_default' is used to save the initial configuration
6. 'bus_voltage_lsb' is used to save the least significant bit
corresponding to bus voltage
7. 'temp_lsb' is used tosave the least significant bit corresponding
to temperature

Configure the architecture content for ina237 and ina238 respectively to
lay the foundation for subsequent adaptation to new chips.

 drivers/hwmon/ina238.c | 57 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
index 2d9f12f68d50..c8d6bd3041b3 100644
--- a/drivers/hwmon/ina238.c
+++ b/drivers/hwmon/ina238.c
@@ -102,7 +102,20 @@ static const struct regmap_config ina238_regmap_config = {
 	.val_bits = 16,
 };
 
+enum ina238_ids { ina238, ina237 };
+
+struct ina238_config {
+	bool has_power_highest;		/* chip detection power peak */
+	bool has_energy;			/* chip detection energy */
+	u8 temp_shift;				/* fixed parameters for temp calculate */
+	u32 power_calculate_factor;	/* fixed parameters for power calculate */
+	u16 config_default;			/* Power-on default state */
+	int bus_voltage_lsb;		/* use for temperature calculate, uV/lsb */
+	int temp_lsb;				/* use for temperature calculate */
+};
+
 struct ina238_data {
+	const struct ina238_config *config;
 	struct i2c_client *client;
 	struct mutex config_lock;
 	struct regmap *regmap;
@@ -110,6 +123,27 @@ struct ina238_data {
 	int gain;
 };
 
+static const struct ina238_config ina238_config[] = {
+	[ina238] = {
+		.has_energy = false,
+		.has_power_highest = false,
+		.temp_shift = 4,
+		.power_calculate_factor = 20,
+		.config_default = INA238_CONFIG_DEFAULT,
+		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
+		.temp_lsb = INA238_DIE_TEMP_LSB,
+	},
+	[ina237] = {
+		.has_energy = false,
+		.has_power_highest = false,
+		.temp_shift = 4,
+		.power_calculate_factor = 20,
+		.config_default = INA238_CONFIG_DEFAULT,
+		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
+		.temp_lsb = INA238_DIE_TEMP_LSB,
+	},
+};
+
 static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
 {
 	u8 data[3];
@@ -536,14 +570,20 @@ static int ina238_probe(struct i2c_client *client)
 	struct device *dev = &client->dev;
 	struct device *hwmon_dev;
 	struct ina238_data *data;
+	enum ina238_ids chip;
 	int config;
 	int ret;
 
+	chip = (uintptr_t)i2c_get_match_data(client);
+
 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
 	if (!data)
 		return -ENOMEM;
 
 	data->client = client;
+	/* set the device type */
+	data->config = &ina238_config[chip];
+
 	mutex_init(&data->config_lock);
 
 	data->regmap = devm_regmap_init_i2c(client, &ina238_regmap_config);
@@ -570,7 +610,7 @@ static int ina238_probe(struct i2c_client *client)
 	}
 
 	/* Setup CONFIG register */
-	config = INA238_CONFIG_DEFAULT;
+	config = data->config->config_default;
 	if (data->gain == 1)
 		config |= INA238_CONFIG_ADCRANGE; /* ADCRANGE = 1 is /1 */
 	ret = regmap_write(data->regmap, INA238_CONFIG, config);
@@ -616,15 +656,22 @@ static int ina238_probe(struct i2c_client *client)
 }
 
 static const struct i2c_device_id ina238_id[] = {
-	{ "ina238" },
+	{ "ina237", ina237 },
+	{ "ina238", ina238 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, ina238_id);
 
 static const struct of_device_id __maybe_unused ina238_of_match[] = {
-	{ .compatible = "ti,ina237" },
-	{ .compatible = "ti,ina238" },
-	{ },
+	{
+		.compatible = "ti,ina237",
+		.data = (void *)ina237
+	},
+	{
+		.compatible = "ti,ina238",
+		.data = (void *)ina238
+	},
+	{ }
 };
 MODULE_DEVICE_TABLE(of, ina238_of_match);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v6 2/4] hwmon:(ina238)Add support for SQ52206
  2025-03-27 13:18 [PATCH v6 0/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 1/4] hwmon:(ina238)Add ina238_config to save configurations for different chips Wenliang Yan
@ 2025-03-27 13:18 ` Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 3/4] hwmon:(ina238)Modify the calculation formula to adapt to different chips Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Wenliang Yan
  3 siblings, 0 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-27 13:18 UTC (permalink / raw)
  To: linux, Christophe JAILLET, Jean Delvare
  Cc: Wenliang Yan, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Jonathan Corbet, linux-hwmon, linux-kernel

Add support for SQ52206 to the Ina238 driver. Add registers,
increase compatibility, add compatibility programs for
multiple chips.

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
---

v5->v6:Split the patch, this patch add support for SQ52206:

1.Incorporate four additional registers to the original register
set of SQ52206 beyond INA238.
2.The ADC measurement range of SQ52206 is divided into 1/2/4, so
change the configuration of INA238_ADC_CONFIG.
3.Add an introduction note to the power and energy formulas
4.Configure the properties of SQ52206
5.Add a 40 bit data reading function to prepare for energy reading.
6.Add peak power reading and add HWMON_P_INPUT_HIGHEST for power.
7.Energy attributes are 5bytes wide, so modified the function for
energy1_input to use u64.

Current_LSB is 1mA/lsb, so the SQ52206's Energy calculation formula is
Energy (mJ) = 16 * 0.24 * register value * 1 * 20000 / rshunt / 4 * gain,
where 20000 is the default resistance value. 4 is the default gain of 4,
20000*gain/ (rshunt*4) indicates the conversion ratio based on the actual
resistance value and the gain ratio. 16 and 0.24 are fixed parameters.
'data->config->power_calculate_factor/100 == 0.24'.

8.Add a corresponding compatible to the driver.

 Documentation/hwmon/ina238.rst |  15 +++++
 drivers/hwmon/ina238.c         | 113 ++++++++++++++++++++++++++++++---
 2 files changed, 120 insertions(+), 8 deletions(-)

diff --git a/Documentation/hwmon/ina238.rst b/Documentation/hwmon/ina238.rst
index d9f479984420..d1b93cf8627f 100644
--- a/Documentation/hwmon/ina238.rst
+++ b/Documentation/hwmon/ina238.rst
@@ -14,6 +14,12 @@ Supported chips:
     Datasheet:
 	https://www.ti.com/lit/gpn/ina238
 
+  * Silergy SQ52206
+
+    Prefix: 'SQ52206'
+
+    Addresses: I2C 0x40 - 0x4f
+
 Author: Nathan Rossi <nathan.rossi@digi.com>
 
 Description
@@ -54,3 +60,12 @@ temp1_input		Die temperature measurement (mC)
 temp1_max		Maximum die temperature threshold (mC)
 temp1_max_alarm		Maximum die temperature alarm
 ======================= =======================================================
+
+Additional sysfs entries for sq52206
+------------------------------------
+
+======================= =======================================================
+energy1_input		Energy measurement (mJ)
+
+power1_input_highest	Peak Power (uW)
+======================= =======================================================
diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
index c8d6bd3041b3..1bf4c850b04d 100644
--- a/drivers/hwmon/ina238.c
+++ b/drivers/hwmon/ina238.c
@@ -21,11 +21,14 @@
 #define INA238_CONFIG			0x0
 #define INA238_ADC_CONFIG		0x1
 #define INA238_SHUNT_CALIBRATION	0x2
+#define SQ52206_SHUNT_TEMPCO		0x3
 #define INA238_SHUNT_VOLTAGE		0x4
 #define INA238_BUS_VOLTAGE		0x5
 #define INA238_DIE_TEMP			0x6
 #define INA238_CURRENT			0x7
 #define INA238_POWER			0x8
+#define SQ52206_ENERGY			0x9
+#define SQ52206_CHARGE			0xa
 #define INA238_DIAG_ALERT		0xb
 #define INA238_SHUNT_OVER_VOLTAGE	0xc
 #define INA238_SHUNT_UNDER_VOLTAGE	0xd
@@ -33,9 +36,12 @@
 #define INA238_BUS_UNDER_VOLTAGE	0xf
 #define INA238_TEMP_LIMIT		0x10
 #define INA238_POWER_LIMIT		0x11
+#define SQ52206_POWER_PEAK		0x20
 #define INA238_DEVICE_ID		0x3f /* not available on INA237 */
 
 #define INA238_CONFIG_ADCRANGE		BIT(4)
+#define SQ52206_CONFIG_ADCRANGE_HIGH	BIT(4)
+#define SQ52206_CONFIG_ADCRANGE_LOW		BIT(3)
 
 #define INA238_DIAG_ALERT_TMPOL		BIT(7)
 #define INA238_DIAG_ALERT_SHNTOL	BIT(6)
@@ -44,12 +50,13 @@
 #define INA238_DIAG_ALERT_BUSUL		BIT(3)
 #define INA238_DIAG_ALERT_POL		BIT(2)
 
-#define INA238_REGISTERS		0x11
+#define INA238_REGISTERS		0x20
 
 #define INA238_RSHUNT_DEFAULT		10000 /* uOhm */
 
 /* Default configuration of device on reset. */
 #define INA238_CONFIG_DEFAULT		0
+#define SQ52206_CONFIG_DEFAULT		0x0005
 /* 16 sample averaging, 1052us conversion time, continuous mode */
 #define INA238_ADC_CONFIG_DEFAULT	0xfb6a
 /* Configure alerts to be based on averaged value (SLOWALERT) */
@@ -87,14 +94,19 @@
  *  shunt = 0x4000 / (819.2 * 10^6) / 0.001 = 20000 uOhms (with 1mA/lsb)
  *
  *  Current (mA) = register value * 20000 / rshunt / 4 * gain
- *  Power (W) = 0.2 * register value * 20000 / rshunt / 4 * gain
+ *  Power (mW) = 0.2 * register value * 20000 / rshunt / 4 * gain
+ *  (Specific for SQ52206)
+ *  Power (mW) = 0.24 * register value * 20000 / rshunt / 4 * gain
+ *  Energy (mJ) = 16 * 0.24 * register value * 20000 / rshunt / 4 * gain
  */
 #define INA238_CALIBRATION_VALUE	16384
 #define INA238_FIXED_SHUNT		20000
 
 #define INA238_SHUNT_VOLTAGE_LSB	5 /* 5 uV/lsb */
 #define INA238_BUS_VOLTAGE_LSB		3125 /* 3.125 mV/lsb */
-#define INA238_DIE_TEMP_LSB		125 /* 125 mC/lsb */
+#define INA238_DIE_TEMP_LSB			1250000 /* 125.0000 mC/lsb */
+#define SQ52206_BUS_VOLTAGE_LSB		3750 /* 3.75 mV/lsb */
+#define SQ52206_DIE_TEMP_LSB		78125 /* 7.8125 mC/lsb */
 
 static const struct regmap_config ina238_regmap_config = {
 	.max_register = INA238_REGISTERS,
@@ -102,7 +114,7 @@ static const struct regmap_config ina238_regmap_config = {
 	.val_bits = 16,
 };
 
-enum ina238_ids { ina238, ina237 };
+enum ina238_ids { ina238, ina237, sq52206 };
 
 struct ina238_config {
 	bool has_power_highest;		/* chip detection power peak */
@@ -142,6 +154,15 @@ static const struct ina238_config ina238_config[] = {
 		.bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB,
 		.temp_lsb = INA238_DIE_TEMP_LSB,
 	},
+	[sq52206] = {
+		.has_energy = true,
+		.has_power_highest = true,
+		.temp_shift = 0,
+		.power_calculate_factor = 24,
+		.config_default = SQ52206_CONFIG_DEFAULT,
+		.bus_voltage_lsb = SQ52206_BUS_VOLTAGE_LSB,
+		.temp_lsb = SQ52206_DIE_TEMP_LSB,
+	},
 };
 
 static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
@@ -160,6 +181,24 @@ static int ina238_read_reg24(const struct i2c_client *client, u8 reg, u32 *val)
 	return 0;
 }
 
+static int ina238_read_reg40(const struct i2c_client *client, u8 reg, u64 *val)
+{
+	u8 data[5];
+	u32 low;
+	int err;
+
+	/* 40-bit register read */
+	err = i2c_smbus_read_i2c_block_data(client, reg, 5, data);
+	if (err < 0)
+		return err;
+	if (err != 5)
+		return -EIO;
+	low = (data[1] << 24) | (data[2] << 16) | (data[3] << 8) | data[4];
+	*val = ((long long)data[0] << 32) | low;
+
+	return 0;
+}
+
 static int ina238_read_in(struct device *dev, u32 attr, int channel,
 			  long *val)
 {
@@ -330,6 +369,17 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)
 		if (err)
 			return err;
 
+		/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */
+		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *
+				data->gain, 20 * data->rshunt);
+		/* Clamp value to maximum value of long */
+		*val = clamp_val(power, 0, LONG_MAX);
+		break;
+	case hwmon_power_input_highest:
+		err = ina238_read_reg24(data->client, SQ52206_POWER_PEAK, &regval);
+		if (err)
+			return err;
+
 		/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */
 		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *
 				data->gain, 20 * data->rshunt);
@@ -437,6 +487,25 @@ static int ina238_write_temp(struct device *dev, u32 attr, long val)
 	return regmap_write(data->regmap, INA238_TEMP_LIMIT, regval);
 }
 
+static ssize_t energy1_input_show(struct device *dev,
+				  struct device_attribute *da, char *buf)
+{
+	struct ina238_data *data = dev_get_drvdata(dev);
+	int ret;
+	u64 regval;
+	u64 energy;
+
+	ret = ina238_read_reg40(data->client, SQ52206_ENERGY, &regval);
+	if (ret)
+		return ret;
+
+	/* result in mJ */
+	energy = div_u64(regval * INA238_FIXED_SHUNT *	data->gain * 16 *
+				data->config->power_calculate_factor, 4 * 100 * data->rshunt);
+
+	return sysfs_emit(buf, "%llu\n", energy);
+}
+
 static int ina238_read(struct device *dev, enum hwmon_sensor_types type,
 		       u32 attr, int channel, long *val)
 {
@@ -486,6 +555,9 @@ static umode_t ina238_is_visible(const void *drvdata,
 				 enum hwmon_sensor_types type,
 				 u32 attr, int channel)
 {
+	const struct ina238_data *data = drvdata;
+	bool has_power_highest = data->config->has_power_highest;
+
 	switch (type) {
 	case hwmon_in:
 		switch (attr) {
@@ -513,6 +585,10 @@ static umode_t ina238_is_visible(const void *drvdata,
 			return 0444;
 		case hwmon_power_max:
 			return 0644;
+		case hwmon_power_input_highest:
+			if (has_power_highest)
+				return 0444;
+			return 0;
 		default:
 			return 0;
 		}
@@ -546,7 +622,8 @@ static const struct hwmon_channel_info * const ina238_info[] = {
 			   HWMON_C_INPUT),
 	HWMON_CHANNEL_INFO(power,
 			   /* 0: power */
-			   HWMON_P_INPUT | HWMON_P_MAX | HWMON_P_MAX_ALARM),
+			   HWMON_P_INPUT | HWMON_P_MAX |
+			   HWMON_P_MAX_ALARM | HWMON_P_INPUT_HIGHEST),
 	HWMON_CHANNEL_INFO(temp,
 			   /* 0: die temperature */
 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_ALARM),
@@ -564,6 +641,15 @@ static const struct hwmon_chip_info ina238_chip_info = {
 	.info = ina238_info,
 };
 
+/* energy attributes are 5 bytes wide so we need u64 */
+static DEVICE_ATTR_RO(energy1_input);
+
+static struct attribute *ina238_attrs[] = {
+	&dev_attr_energy1_input.attr,
+	NULL,
+};
+ATTRIBUTE_GROUPS(ina238);
+
 static int ina238_probe(struct i2c_client *client)
 {
 	struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev);
@@ -604,14 +690,19 @@ static int ina238_probe(struct i2c_client *client)
 	/* load shunt gain value */
 	if (device_property_read_u32(dev, "ti,shunt-gain", &data->gain) < 0)
 		data->gain = 4; /* Default of ADCRANGE = 0 */
-	if (data->gain != 1 && data->gain != 4) {
+	if (data->gain != 1 && data->gain != 2 && data->gain != 4) {
 		dev_err(dev, "invalid shunt gain value %u\n", data->gain);
 		return -EINVAL;
 	}
 
 	/* Setup CONFIG register */
 	config = data->config->config_default;
-	if (data->gain == 1)
+	if (chip == sq52206) {
+		if (data->gain == 1)
+			config |= SQ52206_CONFIG_ADCRANGE_HIGH; /* ADCRANGE = 10/11 is /1 */
+		else if (data->gain == 2)
+			config |= SQ52206_CONFIG_ADCRANGE_LOW; /* ADCRANGE = 01 is /2 */
+	} else if (data->gain == 1)
 		config |= INA238_CONFIG_ADCRANGE; /* ADCRANGE = 1 is /1 */
 	ret = regmap_write(data->regmap, INA238_CONFIG, config);
 	if (ret < 0) {
@@ -645,7 +736,8 @@ static int ina238_probe(struct i2c_client *client)
 
 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
 							 &ina238_chip_info,
-							 NULL);
+							 data->config->has_energy ?
+								ina238_groups : NULL);
 	if (IS_ERR(hwmon_dev))
 		return PTR_ERR(hwmon_dev);
 
@@ -658,6 +750,7 @@ static int ina238_probe(struct i2c_client *client)
 static const struct i2c_device_id ina238_id[] = {
 	{ "ina237", ina237 },
 	{ "ina238", ina238 },
+	{ "sq52206", sq52206 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, ina238_id);
@@ -671,6 +764,10 @@ static const struct of_device_id __maybe_unused ina238_of_match[] = {
 		.compatible = "ti,ina238",
 		.data = (void *)ina238
 	},
+	{
+		.compatible = "silergy,sq52206",
+		.data = (void *)sq52206
+	},
 	{ }
 };
 MODULE_DEVICE_TABLE(of, ina238_of_match);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v6 3/4] hwmon:(ina238)Modify the calculation formula to adapt to different chips
  2025-03-27 13:18 [PATCH v6 0/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 1/4] hwmon:(ina238)Add ina238_config to save configurations for different chips Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 2/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
@ 2025-03-27 13:18 ` Wenliang Yan
  2025-03-27 13:18 ` [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Wenliang Yan
  3 siblings, 0 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-27 13:18 UTC (permalink / raw)
  To: linux, Christophe JAILLET, Jean Delvare
  Cc: Wenliang Yan, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Jonathan Corbet, linux-hwmon, linux-kernel

Modify the calculation formula to adapt to different chips.

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
---

v5->v6:Split the patch, this patch introduce the principle of the formula:
1.shunt voltage reading and writing calculate
*val = (regval * INA238_SHUNT_VOLTAGE_LSB) * data->gain / (1000 * 4);
regval = (regval * 1000 * 4) / (INA238_SHUNT_VOLTAGE_LSB * data->gain);
Solves the situation where gain=2 exists

2.power reading and writing calculate
power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT * data->gain *
data->config->power_calculate_factor, 4 * 100 * data->rshunt);

Since the 'data->config->power_calculate_factor' parameter values of different
chips are different, the formula is modified.

Current_LSB is 1mA/lsb, so the SQ52206's Energy calculation formula is
Power (uW) = 0.24(0.2) * register value * 1000 * 20000 / rshunt / 4 * gain,
where 20000 is the default resistance value. 4 is the default gain of 4,
20000*gain/ (rshunt*4) indicates the conversion ratio based on the actual
resistance value and the gain ratio. 0.24(0.2) is fixed parameters.
'data->config->power_calculate_factor/100 == 0.24(0.2)'.

3.read temp calculate
*val = div_s64(((s64)((s16)regval) >> data->config->temp_shift) *
(s64)data->config->temp_lsb, 10000);
Since the 'data->config->temp_shift' parameter values of different
chips are different, the formula is modified.
Cast the parameter regval type s64 to avoid overflows resulting in calculation errors.

4.writr temp calculate
regval = clamp_val(val, -40000, 125000);
Limiting the size of written data prevents overflows that can cause calculation error

 drivers/hwmon/ina238.c | 47 +++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/drivers/hwmon/ina238.c b/drivers/hwmon/ina238.c
index 1bf4c850b04d..7a96b68d4d20 100644
--- a/drivers/hwmon/ina238.c
+++ b/drivers/hwmon/ina238.c
@@ -270,10 +270,10 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel,
 		regval = (s16)regval;
 		if (channel == 0)
 			/* gain of 1 -> LSB / 4 */
-			*val = (regval * INA238_SHUNT_VOLTAGE_LSB) /
-			       (1000 * (4 - data->gain + 1));
+			*val = (regval * INA238_SHUNT_VOLTAGE_LSB) *
+					data->gain / (1000 * 4);
 		else
-			*val = (regval * INA238_BUS_VOLTAGE_LSB) / 1000;
+			*val = (regval * data->config->bus_voltage_lsb) / 1000;
 		break;
 	case hwmon_in_max_alarm:
 	case hwmon_in_min_alarm:
@@ -298,8 +298,8 @@ static int ina238_write_in(struct device *dev, u32 attr, int channel,
 	case 0:
 		/* signed value, clamp to max range +/-163 mV */
 		regval = clamp_val(val, -163, 163);
-		regval = (regval * 1000 * (4 - data->gain + 1)) /
-			 INA238_SHUNT_VOLTAGE_LSB;
+		regval = (regval * 1000 * 4) /
+			 (INA238_SHUNT_VOLTAGE_LSB * data->gain);
 		regval = clamp_val(regval, S16_MIN, S16_MAX);
 
 		switch (attr) {
@@ -315,7 +315,7 @@ static int ina238_write_in(struct device *dev, u32 attr, int channel,
 	case 1:
 		/* signed value, positive values only. Clamp to max 102.396 V */
 		regval = clamp_val(val, 0, 102396);
-		regval = (regval * 1000) / INA238_BUS_VOLTAGE_LSB;
+		regval = (regval * 1000) / data->config->bus_voltage_lsb;
 		regval = clamp_val(regval, 0, S16_MAX);
 
 		switch (attr) {
@@ -370,8 +370,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)
 			return err;
 
 		/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */
-		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *
-				data->gain, 20 * data->rshunt);
+		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *	data->gain *
+				data->config->power_calculate_factor, 4 * 100 * data->rshunt);
 		/* Clamp value to maximum value of long */
 		*val = clamp_val(power, 0, LONG_MAX);
 		break;
@@ -381,8 +381,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)
 			return err;
 
 		/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */
-		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *
-				data->gain, 20 * data->rshunt);
+		power = div_u64(regval * 1000ULL * INA238_FIXED_SHUNT *	data->gain *
+				data->config->power_calculate_factor, 4 * 100 * data->rshunt);
 		/* Clamp value to maximum value of long */
 		*val = clamp_val(power, 0, LONG_MAX);
 		break;
@@ -395,8 +395,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)
 		 * Truncated 24-bit compare register, lower 8-bits are
 		 * truncated. Same conversion to/from uW as POWER register.
 		 */
-		power = div_u64((regval << 8) * 1000ULL * INA238_FIXED_SHUNT *
-			       data->gain, 20 * data->rshunt);
+		power = div_u64((regval << 8) * 1000ULL * INA238_FIXED_SHUNT *	data->gain *
+				data->config->power_calculate_factor, 4 * 100 * data->rshunt);
 		/* Clamp value to maximum value of long */
 		*val = clamp_val(power, 0, LONG_MAX);
 		break;
@@ -428,8 +428,8 @@ static int ina238_write_power(struct device *dev, u32 attr, long val)
 	 * register.
 	 */
 	regval = clamp_val(val, 0, LONG_MAX);
-	regval = div_u64(val * 20ULL * data->rshunt,
-			 1000ULL * INA238_FIXED_SHUNT * data->gain);
+	regval = div_u64(val * 4 * 100 * data->rshunt, data->config->power_calculate_factor *
+			1000ULL * INA238_FIXED_SHUNT * data->gain);
 	regval = clamp_val(regval >> 8, 0, U16_MAX);
 
 	return regmap_write(data->regmap, INA238_POWER_LIMIT, regval);
@@ -446,17 +446,17 @@ static int ina238_read_temp(struct device *dev, u32 attr, long *val)
 		err = regmap_read(data->regmap, INA238_DIE_TEMP, &regval);
 		if (err)
 			return err;
-
-		/* Signed, bits 15-4 of register, result in mC */
-		*val = ((s16)regval >> 4) * INA238_DIE_TEMP_LSB;
+		/* Signed, result in mC */
+		*val = div_s64(((s64)((s16)regval) >> data->config->temp_shift) *
+						(s64)data->config->temp_lsb, 10000);
 		break;
 	case hwmon_temp_max:
 		err = regmap_read(data->regmap, INA238_TEMP_LIMIT, &regval);
 		if (err)
 			return err;
-
-		/* Signed, bits 15-4 of register, result in mC */
-		*val = ((s16)regval >> 4) * INA238_DIE_TEMP_LSB;
+		/* Signed, result in mC */
+		*val = div_s64(((s64)((s16)regval) >> data->config->temp_shift) *
+						(s64)data->config->temp_lsb, 10000);
 		break;
 	case hwmon_temp_max_alarm:
 		err = regmap_read(data->regmap, INA238_DIAG_ALERT, &regval);
@@ -480,9 +480,10 @@ static int ina238_write_temp(struct device *dev, u32 attr, long val)
 	if (attr != hwmon_temp_max)
 		return -EOPNOTSUPP;
 
-	/* Signed, bits 15-4 of register */
-	regval = (val / INA238_DIE_TEMP_LSB) << 4;
-	regval = clamp_val(regval, S16_MIN, S16_MAX) & 0xfff0;
+	/* Signed */
+	regval = clamp_val(val, -40000, 125000);
+	regval = div_s64(val*10000, data->config->temp_lsb) << data->config->temp_shift;
+	regval = clamp_val(regval, S16_MIN, S16_MAX) & (0xffff << data->config->temp_shift);
 
 	return regmap_write(data->regmap, INA238_TEMP_LIMIT, regval);
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings
  2025-03-27 13:18 [PATCH v6 0/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
                   ` (2 preceding siblings ...)
  2025-03-27 13:18 ` [PATCH v6 3/4] hwmon:(ina238)Modify the calculation formula to adapt to different chips Wenliang Yan
@ 2025-03-27 13:18 ` Wenliang Yan
  2025-03-27 14:27   ` Rob Herring (Arm)
  2025-03-27 22:53   ` Guenter Roeck
  3 siblings, 2 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-27 13:18 UTC (permalink / raw)
  To: linux, Jean Delvare, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Wenliang Yan, Jonathan Corbet, linux-hwmon, devicetree,
	linux-kernel, christophe.jaillet

Add the sq52206 compatible to the ina2xx.yaml

Signed-off-by: Wenliang Yan <wenliang202407@163.com>
---

Add the meaning of 'shunt-gain' in SQ52206.

v5->v6:add content to meet the update requirements of the ina2xx.yaml

 Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
index bc03781342c0..8cd672e6bf62 100644
--- a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
+++ b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
@@ -19,6 +19,7 @@ description: |
 properties:
   compatible:
     enum:
+      - silergy,sq52206
       - silergy,sy24655
       - ti,ina209
       - ti,ina219
@@ -58,6 +59,9 @@ properties:
       shunt voltage, and a value of 4 maps to ADCRANGE=0 such that a wider
       voltage range is used.
 
+      For SQ52206,the shunt-gain value 1 mapps to ADCRANGE=10/11, the value 2
+      mapps to ADCRANGE=01, and the value 4 mapps to ADCRANGE=00.
+
       The default value is device dependent, and is defined by the reset value
       of PGA/ADCRANGE in the respective configuration registers.
     $ref: /schemas/types.yaml#/definitions/uint32
@@ -97,6 +101,7 @@ allOf:
         compatible:
           contains:
             enum:
+              - silergy,sy24655
               - silergy,sy24655
               - ti,ina209
               - ti,ina219
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings
  2025-03-27 13:18 ` [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Wenliang Yan
@ 2025-03-27 14:27   ` Rob Herring (Arm)
  2025-03-27 22:53   ` Guenter Roeck
  1 sibling, 0 replies; 8+ messages in thread
From: Rob Herring (Arm) @ 2025-03-27 14:27 UTC (permalink / raw)
  To: Wenliang Yan
  Cc: christophe.jaillet, linux, Jonathan Corbet, Conor Dooley,
	Krzysztof Kozlowski, linux-kernel, Jean Delvare, linux-hwmon,
	devicetree


On Thu, 27 Mar 2025 09:18:41 -0400, Wenliang Yan wrote:
> Add the sq52206 compatible to the ina2xx.yaml
> 
> Signed-off-by: Wenliang Yan <wenliang202407@163.com>
> ---
> 
> Add the meaning of 'shunt-gain' in SQ52206.
> 
> v5->v6:add content to meet the update requirements of the ina2xx.yaml
> 
>  Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml | 5 +++++
>  1 file changed, 5 insertions(+)
> 

My bot found errors running 'make dt_binding_check' on your patch:

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml: allOf:1:if:properties:compatible:contains:enum: ['silergy,sy24655', 'silergy,sy24655', 'ti,ina209', 'ti,ina219', 'ti,ina220', 'ti,ina226', 'ti,ina230', 'ti,ina231', 'ti,ina237', 'ti,ina238', 'ti,ina260'] has non-unique elements
	hint: "enum" must be an array of either integers or strings
	from schema $id: http://devicetree.org/meta-schemas/keywords.yaml#

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/20250327131841.15013-5-wenliang202407@163.com

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings
  2025-03-27 13:18 ` [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Wenliang Yan
  2025-03-27 14:27   ` Rob Herring (Arm)
@ 2025-03-27 22:53   ` Guenter Roeck
  2025-03-28 10:35     ` Wenliang Yan
  1 sibling, 1 reply; 8+ messages in thread
From: Guenter Roeck @ 2025-03-27 22:53 UTC (permalink / raw)
  To: Wenliang Yan, Jean Delvare, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley
  Cc: Jonathan Corbet, linux-hwmon, devicetree, linux-kernel,
	christophe.jaillet

On 3/27/25 06:18, Wenliang Yan wrote:
> Add the sq52206 compatible to the ina2xx.yaml
> 
> Signed-off-by: Wenliang Yan <wenliang202407@163.com>
> ---
> 
> Add the meaning of 'shunt-gain' in SQ52206.
> 
> v5->v6:add content to meet the update requirements of the ina2xx.yaml
> 
>   Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml | 5 +++++
>   1 file changed, 5 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
> index bc03781342c0..8cd672e6bf62 100644
> --- a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
> +++ b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
> @@ -19,6 +19,7 @@ description: |
>   properties:
>     compatible:
>       enum:
> +      - silergy,sq52206
>         - silergy,sy24655
>         - ti,ina209
>         - ti,ina219
> @@ -58,6 +59,9 @@ properties:
>         shunt voltage, and a value of 4 maps to ADCRANGE=0 such that a wider
>         voltage range is used.
>   
> +      For SQ52206,the shunt-gain value 1 mapps to ADCRANGE=10/11, the value 2
> +      mapps to ADCRANGE=01, and the value 4 mapps to ADCRANGE=00.
> +
>         The default value is device dependent, and is defined by the reset value
>         of PGA/ADCRANGE in the respective configuration registers.
>       $ref: /schemas/types.yaml#/definitions/uint32
> @@ -97,6 +101,7 @@ allOf:
>           compatible:
>             contains:
>               enum:
> +              - silergy,sy24655

silergy,sq52206 ?

>                 - silergy,sy24655
>                 - ti,ina209
>                 - ti,ina219


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings
  2025-03-27 22:53   ` Guenter Roeck
@ 2025-03-28 10:35     ` Wenliang Yan
  0 siblings, 0 replies; 8+ messages in thread
From: Wenliang Yan @ 2025-03-28 10:35 UTC (permalink / raw)
  To: linux
  Cc: christophe.jaillet, conor+dt, corbet, devicetree, jdelvare,
	krzk+dt, linux-hwmon, linux-kernel, robh, wenliang202407

At 2025-03-28 06:53:02, "Guenter Roeck" <linux@roeck-us.net> wrote:
>On 3/27/25 06:18, Wenliang Yan wrote:
>> Add the sq52206 compatible to the ina2xx.yaml
>> 
>> Signed-off-by: Wenliang Yan <wenliang202407@163.com>
>> ---
>> 
>> Add the meaning of 'shunt-gain' in SQ52206.
>> 
>> v5->v6:add content to meet the update requirements of the ina2xx.yaml
>> 
>>   Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml | 5 +++++
>>   1 file changed, 5 insertions(+)
>> 
>> diff --git a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
>> index bc03781342c0..8cd672e6bf62 100644
>> --- a/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
>> +++ b/Documentation/devicetree/bindings/hwmon/ti,ina2xx.yaml
>> @@ -19,6 +19,7 @@ description: |
>>   properties:
>>     compatible:
>>       enum:
>> +      - silergy,sq52206
>>         - silergy,sy24655
>>         - ti,ina209
>>         - ti,ina219
>> @@ -58,6 +59,9 @@ properties:
>>         shunt voltage, and a value of 4 maps to ADCRANGE=0 such that a wider
>>         voltage range is used.
>>   
>> +      For SQ52206,the shunt-gain value 1 mapps to ADCRANGE=10/11, the value 2
>> +      mapps to ADCRANGE=01, and the value 4 mapps to ADCRANGE=00.
>> +
>>         The default value is device dependent, and is defined by the reset value
>>         of PGA/ADCRANGE in the respective configuration registers.
>>       $ref: /schemas/types.yaml#/definitions/uint32
>> @@ -97,6 +101,7 @@ allOf:
>>           compatible:
>>             contains:
>>               enum:
>> +              - silergy,sy24655
>
>silergy,sq52206 ?
>

Yes, it is my oversight, which may be the reason why 'make dt_binding_check' fails 

>>                 - silergy,sy24655
>>                 - ti,ina209
>>                 - ti,ina219

Thanks,
Wenliang Yan


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-03-28 10:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-27 13:18 [PATCH v6 0/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
2025-03-27 13:18 ` [PATCH v6 1/4] hwmon:(ina238)Add ina238_config to save configurations for different chips Wenliang Yan
2025-03-27 13:18 ` [PATCH v6 2/4] hwmon:(ina238)Add support for SQ52206 Wenliang Yan
2025-03-27 13:18 ` [PATCH v6 3/4] hwmon:(ina238)Modify the calculation formula to adapt to different chips Wenliang Yan
2025-03-27 13:18 ` [PATCH v6 4/4] dt-bindings:Add SQ52206 to ina2xx devicetree bindings Wenliang Yan
2025-03-27 14:27   ` Rob Herring (Arm)
2025-03-27 22:53   ` Guenter Roeck
2025-03-28 10:35     ` Wenliang Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox