Skip to contentSkip to conversation

regulator (max5970): Add hwmon support

PATCH Superseded Naresh Solanki 3 participants 4 messages Oct 5, 2023 – Oct 17, 2023

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 regulator (max5970): Add hwmon support

Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage and current monitoring. This feature is seamlessly integrated through the hwmon subsystem.

---
Changes in V5:
- Simplify memory allocation for rdevs
Changes in V4:
- Use IS_REACHABLE
- Use rdevs array for hwmon ops.
- Remove duplicate i2c_set_clientdata
Changes in V3:
- Update signed-off
- Add break
- Update hwmon dev register name to max5970
- Remove changes in Kconfig.
Changes in V2:
- default case added for switch statement
- Add dependency on HWMON

---
 drivers/regulator/max5970-regulator.c | 141 +++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 2 deletions(-)
drivers/regulator/max5970-regulator.c +139 -2
index b56a174cde3d..71b4a543339a 100644
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/hwmon.h>
 #include <linux/module.h>
 #include <linux/io.h>
 #include <linux/of.h>
@@ -32,6 +33,128 @@ enum max597x_regulator_id {
 	MAX597X_SW1,
 };
 
+static int max5970_read_adc(struct regmap *regmap, int reg, long *val)
+{
+	u8 reg_data[2];
+	int ret;
+
+	ret = regmap_bulk_read(regmap, reg, &reg_data[0], 2);
+	if (ret < 0)
+		return ret;
+
+	*val = (reg_data[0] << 2) | (reg_data[1] & 3);
+
+	return 0;
+}
+
+static int max5970_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
+{
+	struct regulator_dev **rdevs = dev_get_drvdata(dev);
+	struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]);
+	struct regmap *regmap = ddata->regmap;
+	int ret;
+
+	switch (type) {
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val);
+			/*
+			 * Calculate current from ADC value, IRNG range & shunt resistor value.
+			 * ddata->irng holds the voltage corresponding to the maximum value the
+			 * 10-bit ADC can measure.
+			 * To obtain the output, multiply the ADC value by the IRNG range (in
+			 * millivolts) and then divide it by the maximum value of the 10-bit ADC.
+			 */
+			*val = (*val * ddata->irng) >> 10;
+			/* Convert the voltage meansurement across shunt resistor to current */
+			*val = (*val * 1000) / ddata->shunt_micro_ohms;
+			return ret;
+		default:
+			return -EOPNOTSUPP;
+		}
+
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val);
+			/*
+			 * Calculate voltage from ADC value and MON range.
+			 * ddata->mon_rng holds the voltage corresponding to the maximum value the
+			 * 10-bit ADC can measure.
+			 * To obtain the output, multiply the ADC value by the MON range (in
+			 * microvolts) and then divide it by the maximum value of the 10-bit ADC.
+			 */
+			*val = mul_u64_u32_shr(*val, ddata->mon_rng, 10);
+			/* uV to mV */
+			*val = *val / 1000;
+			return ret;
+		default:
+			return -EOPNOTSUPP;
+		}
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t max5970_is_visible(const void *data,
+				  enum hwmon_sensor_types type,
+				  u32 attr, int channel)
+{
+	struct regulator_dev **rdevs = (struct regulator_dev **)data;
+	struct max5970_regulator *ddata;
+
+	if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel])
+		return 0;
+
+	ddata = rdev_get_drvdata(rdevs[channel]);
+
+	if (!ddata || channel >= ddata->num_switches)
+		return 0;
+
+	switch (type) {
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			return 0444;
+		default:
+			break;
+		}
+		break;
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			/* Current measurement requires knowledge of the shunt resistor value. */
+			if (ddata->shunt_micro_ohms)
+				return 0444;
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+static const struct hwmon_ops max5970_hwmon_ops = {
+	.is_visible = max5970_is_visible,
+	.read = max5970_read,
+};
+
+static const struct hwmon_channel_info *max5970_info[] = {
+	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
+	HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT),
+	NULL
+};
+
+static const struct hwmon_chip_info max5970_chip_info = {
+	.ops = &max5970_hwmon_ops,
+	.info = max5970_info,
+};
+
 static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
 {
 	int ret, reg;
@@ -431,7 +554,8 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	struct i2c_client *i2c = to_i2c_client(pdev->dev.parent);
 	struct regulator_config config = { };
 	struct regulator_dev *rdev;
-	struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
+	struct regulator_dev **rdevs = NULL;
+	struct device *hwmon_dev;
 	int num_switches;
 	int ret, i;
 
@@ -442,6 +566,11 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	if (!max597x)
 		return -ENOMEM;
 
+	rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *),
+			     GFP_KERNEL);
+	if (!rdevs)
+		return -ENOMEM;
+
 	i2c_set_clientdata(i2c, max597x);
 
 	if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978"))
@@ -451,7 +580,6 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	else
 		return -ENODEV;
 
-	i2c_set_clientdata(i2c, max597x);
 	num_switches = max597x->num_switches;
 
 	for (i = 0; i < num_switches; i++) {
@@ -485,6 +613,15 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 		max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
 	}
 
+	if (IS_REACHABLE(CONFIG_HWMON)) {
+		hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs,
+								 &max5970_chip_info, NULL);
+		if (IS_ERR(hwmon_dev)) {
+			return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev),
+					     "Unable to register hwmon device\n");
+		}
+	}
+
 	if (i2c->irq) {
 		ret =
 		    max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches,

base-commit: f9a1d31874c383f58bb4f89bfe79b764682cd026

Signature · 1 line
2.41.0
maintainer
#1 lore raw
Quoted 4 lines · Naresh Solanki

Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage and current monitoring. This feature is seamlessly integrated through the hwmon subsystem.

Quoted 8 lines

---
Changes in V5:
- Simplify memory allocation for rdevs
Changes in V4:
- Use IS_REACHABLE
- Use rdevs array for hwmon ops.
- Remove duplicate i2c_set_clientdata

I just noticed this one. Not my decision how to handle this, but the change is really unrelated to this patch.

Quoted 45 lines

Changes in V3:
- Update signed-off
- Add break
- Update hwmon dev register name to max5970
- Remove changes in Kconfig.
Changes in V2:
- default case added for switch statement
- Add dependency on HWMON

---
 drivers/regulator/max5970-regulator.c | 141 +++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 2 deletions(-)
drivers/regulator/max5970-regulator.c +18 -0
index b56a174cde3d..71b4a543339a 100644
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/hwmon.h>
 #include <linux/module.h>
 #include <linux/io.h>
 #include <linux/of.h>
@@ -32,6 +33,128 @@ enum max597x_regulator_id {
 	MAX597X_SW1,
 };
 
+static int max5970_read_adc(struct regmap *regmap, int reg, long *val)
+{
+	u8 reg_data[2];
+	int ret;
+
+	ret = regmap_bulk_read(regmap, reg, &reg_data[0], 2);
+	if (ret < 0)
+		return ret;
+
+	*val = (reg_data[0] << 2) | (reg_data[1] & 3);
+
+	return 0;
+}
+
+static int max5970_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
+{

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 62

Quoted 10 lines
drivers/regulator/max5970-regulator.c +10 -0
+	struct regulator_dev **rdevs = dev_get_drvdata(dev);
+	struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]);
+	struct regmap *regmap = ddata->regmap;
+	int ret;
+
+	switch (type) {
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val);
			if (ret < 0)
				return ret;

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 73

Quoted 11 lines
drivers/regulator/max5970-regulator.c +11 -0
+			/*
+			 * Calculate current from ADC value, IRNG range & shunt resistor value.
+			 * ddata->irng holds the voltage corresponding to the maximum value the
+			 * 10-bit ADC can measure.
+			 * To obtain the output, multiply the ADC value by the IRNG range (in
+			 * millivolts) and then divide it by the maximum value of the 10-bit ADC.
+			 */
+			*val = (*val * ddata->irng) >> 10;
+			/* Convert the voltage meansurement across shunt resistor to current */
+			*val = (*val * 1000) / ddata->shunt_micro_ohms;
+			return ret;
			return 0;

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 81

Quoted 8 lines
drivers/regulator/max5970-regulator.c +8 -0
+		default:
+			return -EOPNOTSUPP;
+		}
+
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val);
			if (ret < 0)
				return ret;

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 92

Quoted 11 lines
drivers/regulator/max5970-regulator.c +11 -0
+			/*
+			 * Calculate voltage from ADC value and MON range.
+			 * ddata->mon_rng holds the voltage corresponding to the maximum value the
+			 * 10-bit ADC can measure.
+			 * To obtain the output, multiply the ADC value by the MON range (in
+			 * microvolts) and then divide it by the maximum value of the 10-bit ADC.
+			 */
+			*val = mul_u64_u32_shr(*val, ddata->mon_rng, 10);
+			/* uV to mV */
+			*val = *val / 1000;
+			return ret;
			return 0;
Quoted 11 lines
drivers/regulator/max5970-regulator.c +11 -0
+		default:
+			return -EOPNOTSUPP;
+		}
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t max5970_is_visible(const void *data,
+				  enum hwmon_sensor_types type,
+				  u32 attr, int channel)

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 113

Quoted 10 lines
drivers/regulator/max5970-regulator.c +10 -0
+{
+	struct regulator_dev **rdevs = (struct regulator_dev **)data;
+	struct max5970_regulator *ddata;
+
+	if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel])
+		return 0;
+
+	ddata = rdev_get_drvdata(rdevs[channel]);
+
+	if (!ddata || channel >= ddata->num_switches)

How would ddata ever be NULL ? I don't see that check elsewhere in the driver.

Quoted 7 lines
drivers/regulator/max5970-regulator.c +7 -0
+		return 0;
+
+	switch (type) {
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			return 0444;

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 130

Quoted 10 lines
drivers/regulator/max5970-regulator.c +10 -0
+		default:
+			break;
+		}
+		break;
+	case hwmon_curr:
+		switch (attr) {
+		case hwmon_curr_input:
+			/* Current measurement requires knowledge of the shunt resistor value. */
+			if (ddata->shunt_micro_ohms)
+				return 0444;

The common approach in hwmon drivers is to define a default (typically 1 mOhm) if the value is not set, to let the user configure it from userspace with the sensors configuration file.

Checking ... Never mind. I guess the problem here is that while the shunt-resistor-micro-ohms property is mandatory, the dt author can explicitly configure a shunt resistor value of 0 and that is accepted by the rest of the code. I am not sure if that makes sense, but ... With this in mind, keep as is, but please add a note explaining that this can happen.

Quoted 81 lines
drivers/regulator/max5970-regulator.c +43 -2
+			break;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+	return 0;
+}
+
+static const struct hwmon_ops max5970_hwmon_ops = {
+	.is_visible = max5970_is_visible,
+	.read = max5970_read,
+};
+
+static const struct hwmon_channel_info *max5970_info[] = {
+	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
+	HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT),
+	NULL
+};
+
+static const struct hwmon_chip_info max5970_chip_info = {
+	.ops = &max5970_hwmon_ops,
+	.info = max5970_info,
+};
+
 static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
 {
 	int ret, reg;
@@ -431,7 +554,8 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	struct i2c_client *i2c = to_i2c_client(pdev->dev.parent);
 	struct regulator_config config = { };
 	struct regulator_dev *rdev;
-	struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
+	struct regulator_dev **rdevs = NULL;
+	struct device *hwmon_dev;
 	int num_switches;
 	int ret, i;
 
@@ -442,6 +566,11 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	if (!max597x)
 		return -ENOMEM;
 
+	rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *),
+			     GFP_KERNEL);
+	if (!rdevs)
+		return -ENOMEM;
+
 	i2c_set_clientdata(i2c, max597x);
 
 	if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978"))
@@ -451,7 +580,6 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	else
 		return -ENODEV;
 
-	i2c_set_clientdata(i2c, max597x);
 	num_switches = max597x->num_switches;
 
 	for (i = 0; i < num_switches; i++) {
@@ -485,6 +613,15 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 		max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
 	}
 
+	if (IS_REACHABLE(CONFIG_HWMON)) {
+		hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs,
+								 &max5970_chip_info, NULL);
+		if (IS_ERR(hwmon_dev)) {
+			return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev),
+					     "Unable to register hwmon device\n");
+		}
+	}
+
 	if (i2c->irq) {
 		ret =
 		    max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches,

base-commit: f9a1d31874c383f58bb4f89bfe79b764682cd026

Signature · 1 line
2.41.0
maintainer
#2 lore raw
Quoted 6 lines · Guenter Roeck
Quoted 1 line · Naresh Solanki

- Remove duplicate i2c_set_clientdata

I just noticed this one. Not my decision how to handle this, but the change is really unrelated to this patch.

It should really be a separate commit but it's not the end of the world.

#3 lore raw
In reply to Guenter Roeck

Hi,

Quoted 15 lines · Guenter Roeck
Quoted 4 lines · Naresh Solanki

Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage and current monitoring. This feature is seamlessly integrated through the hwmon subsystem.

Quoted 8 lines

---
Changes in V5:
- Simplify memory allocation for rdevs
Changes in V4:
- Use IS_REACHABLE
- Use rdevs array for hwmon ops.
- Remove duplicate i2c_set_clientdata

Quoted 2 lines

I just noticed this one. Not my decision how to handle this, but the change is really unrelated to this patch.

Sure. Will make separate change for it.

Quoted 51 lines
Quoted 51 lines

Changes in V3:
- Update signed-off
- Add break
- Update hwmon dev register name to max5970
- Remove changes in Kconfig.
Changes in V2:
- default case added for switch statement
- Add dependency on HWMON

---
 drivers/regulator/max5970-regulator.c | 141 +++++++++++++++++++++++++-
 1 file changed, 139 insertions(+), 2 deletions(-)
drivers/regulator/max5970-regulator.c +23 -0
index b56a174cde3d..71b4a543339a 100644
@@ -10,6 +10,7 @@
 #include <linux/bitops.h>
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/hwmon.h>
 #include <linux/module.h>
 #include <linux/io.h>
 #include <linux/of.h>
@@ -32,6 +33,128 @@ enum max597x_regulator_id {
      MAX597X_SW1,
 };
 
+static int max5970_read_adc(struct regmap *regmap, int reg, long *val)
+{
+     u8 reg_data[2];
+     int ret;
+
+     ret = regmap_bulk_read(regmap, reg, &reg_data[0], 2);
+     if (ret < 0)
+             return ret;
+
+     *val = (reg_data[0] << 2) | (reg_data[1] & 3);
+
+     return 0;
+}
+
+static int max5970_read(struct device *dev, enum hwmon_sensor_types type,
+                     u32 attr, int channel, long *val)
+{
+     struct regulator_dev **rdevs = dev_get_drvdata(dev);
+     struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]);
+     struct regmap *regmap = ddata->regmap;
+     int ret;
+
Quoted 8 lines

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 62

Quoted 5 lines
drivers/regulator/max5970-regulator.c +5 -0
+     switch (type) {
+     case hwmon_curr:
+             switch (attr) {
+             case hwmon_curr_input:
+                     ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val);
                        if (ret < 0)
                                return ret;

Ack

Quoted 13 lines

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 73

Quoted 12 lines
drivers/regulator/max5970-regulator.c +11 -0
+                     /*
+                      * Calculate current from ADC value, IRNG range & shunt resistor value.
+                      * ddata->irng holds the voltage corresponding to the maximum value the
+                      * 10-bit ADC can measure.
+                      * To obtain the output, multiply the ADC value by the IRNG range (in
+                      * millivolts) and then divide it by the maximum value of the 10-bit ADC.
+                      */
+                     *val = (*val * ddata->irng) >> 10;
+                     /* Convert the voltage meansurement across shunt resistor to current */
+                     *val = (*val * 1000) / ddata->shunt_micro_ohms;
+                     return ret;
Quoted 1 line
                        return 0;

Ack

Quoted 9 lines

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 81

Quoted 9 lines
drivers/regulator/max5970-regulator.c +8 -0
+             default:
+                     return -EOPNOTSUPP;
+             }
+
+     case hwmon_in:
+             switch (attr) {
+             case hwmon_in_input:
+                     ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val);
Quoted 3 lines
                        if (ret < 0)
                                return ret;

Ack

Quoted 12 lines

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 92

Quoted 11 lines
drivers/regulator/max5970-regulator.c +11 -0
+                     /*
+                      * Calculate voltage from ADC value and MON range.
+                      * ddata->mon_rng holds the voltage corresponding to the maximum value the
+                      * 10-bit ADC can measure.
+                      * To obtain the output, multiply the ADC value by the MON range (in
+                      * microvolts) and then divide it by the maximum value of the 10-bit ADC.
+                      */
+                     *val = mul_u64_u32_shr(*val, ddata->mon_rng, 10);
+                     /* uV to mV */
+                     *val = *val / 1000;
+                     return ret;
Quoted 2 lines
                        return 0;

Ack

Quoted 16 lines
Quoted 16 lines
drivers/regulator/max5970-regulator.c +16 -0
+             default:
+                     return -EOPNOTSUPP;
+             }
+     default:
+             return -EOPNOTSUPP;
+     }
+}
+
+static umode_t max5970_is_visible(const void *data,
+                               enum hwmon_sensor_types type,
+                               u32 attr, int channel)
+{
+     struct regulator_dev **rdevs = (struct regulator_dev **)data;
+     struct max5970_regulator *ddata;
+
+     if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel])
Quoted 9 lines

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 113

Quoted 5 lines
drivers/regulator/max5970-regulator.c +5 -0
+             return 0;
+
+     ddata = rdev_get_drvdata(rdevs[channel]);
+
+     if (!ddata || channel >= ddata->num_switches)

How would ddata ever be NULL ? I don't see that check elsewhere in the driver.

Will remove the NULL check.

Quoted 22 lines
Quoted 7 lines
drivers/regulator/max5970-regulator.c +7 -0
+             return 0;
+
+     switch (type) {
+     case hwmon_in:
+             switch (attr) {
+             case hwmon_in_input:
+                     return 0444;

drivers/regulator/max5970-regulator.c enum max597x_regulator_id { · line 130

Quoted 10 lines
drivers/regulator/max5970-regulator.c +10 -0
+             default:
+                     break;
+             }
+             break;
+     case hwmon_curr:
+             switch (attr) {
+             case hwmon_curr_input:
+                     /* Current measurement requires knowledge of the shunt resistor value. */
+                     if (ddata->shunt_micro_ohms)
+                             return 0444;

The common approach in hwmon drivers is to define a default (typically 1 mOhm) if the value is not set, to let the user configure it from userspace with the sensors configuration file.

Quoted 6 lines

Checking ... Never mind. I guess the problem here is that while the shunt-resistor-micro-ohms property is mandatory, the dt author can explicitly configure a shunt resistor value of 0 and that is accepted by the rest of the code. I am not sure if that makes sense, but ... With this in mind, keep as is, but please add a note explaining that this can happen.

Ack

Regards,
Naresh

Quoted 82 lines
Quoted 82 lines
drivers/regulator/max5970-regulator.c +43 -2
+                     break;
+             default:
+                     break;
+             }
+             break;
+     default:
+             break;
+     }
+     return 0;
+}
+
+static const struct hwmon_ops max5970_hwmon_ops = {
+     .is_visible = max5970_is_visible,
+     .read = max5970_read,
+};
+
+static const struct hwmon_channel_info *max5970_info[] = {
+     HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
+     HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT),
+     NULL
+};
+
+static const struct hwmon_chip_info max5970_chip_info = {
+     .ops = &max5970_hwmon_ops,
+     .info = max5970_info,
+};
+
 static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity)
 {
      int ret, reg;
@@ -431,7 +554,8 @@ static int max597x_regulator_probe(struct platform_device *pdev)
      struct i2c_client *i2c = to_i2c_client(pdev->dev.parent);
      struct regulator_config config = { };
      struct regulator_dev *rdev;
-     struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
+     struct regulator_dev **rdevs = NULL;
+     struct device *hwmon_dev;
      int num_switches;
      int ret, i;
 
@@ -442,6 +566,11 @@ static int max597x_regulator_probe(struct platform_device *pdev)
      if (!max597x)
              return -ENOMEM;
 
+     rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *),
+                          GFP_KERNEL);
+     if (!rdevs)
+             return -ENOMEM;
+
      i2c_set_clientdata(i2c, max597x);
 
      if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978"))
@@ -451,7 +580,6 @@ static int max597x_regulator_probe(struct platform_device *pdev)
      else
              return -ENODEV;
 
-     i2c_set_clientdata(i2c, max597x);
      num_switches = max597x->num_switches;
 
      for (i = 0; i < num_switches; i++) {
@@ -485,6 +613,15 @@ static int max597x_regulator_probe(struct platform_device *pdev)
              max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
      }
 
+     if (IS_REACHABLE(CONFIG_HWMON)) {
+             hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs,
+                                                              &max5970_chip_info, NULL);
+             if (IS_ERR(hwmon_dev)) {
+                     return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev),
+                                          "Unable to register hwmon device\n");
+             }
+     }
+
      if (i2c->irq) {
              ret =
                  max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches,

base-commit: f9a1d31874c383f58bb4f89bfe79b764682cd026 --
2.41.0