linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4] regulator (max5970): Add hwmon support
@ 2023-09-19  5:48 Naresh Solanki
  2023-09-26 15:28 ` Mark Brown
  2023-09-26 19:36 ` Guenter Roeck
  0 siblings, 2 replies; 5+ messages in thread
From: Naresh Solanki @ 2023-09-19  5:48 UTC (permalink / raw)
  To: broonie, zev, Liam Girdwood, Jean Delvare, Guenter Roeck
  Cc: Naresh Solanki, linux-kernel, linux-hwmon

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

Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
---
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 | 138 +++++++++++++++++++++++++-
 1 file changed, 136 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
index b56a174cde3d..06168fe1dadd 100644
--- a/drivers/regulator/max5970-regulator.c
+++ b/drivers/regulator/max5970-regulator.c
@@ -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 (!rdevs || !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;
 
@@ -451,9 +575,10 @@ static int max597x_regulator_probe(struct platform_device *pdev)
 	else
 		return -ENODEV;
 
-	i2c_set_clientdata(i2c, max597x);
 	num_switches = max597x->num_switches;
 
+	rdevs = devm_kcalloc(&i2c->dev, num_switches, sizeof(struct regulator_dev *), GFP_KERNEL);
+
 	for (i = 0; i < num_switches; i++) {
 		data =
 		    devm_kzalloc(&i2c->dev, sizeof(struct max5970_regulator),
@@ -485,6 +610,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: 41f02a383ac652f1a0b5538c5901b7ec93e37290
-- 
2.41.0


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

* Re: [PATCH v4] regulator (max5970): Add hwmon support
  2023-09-19  5:48 [PATCH v4] regulator (max5970): Add hwmon support Naresh Solanki
@ 2023-09-26 15:28 ` Mark Brown
  2023-09-26 19:43   ` Guenter Roeck
  2023-09-26 19:36 ` Guenter Roeck
  1 sibling, 1 reply; 5+ messages in thread
From: Mark Brown @ 2023-09-26 15:28 UTC (permalink / raw)
  To: Naresh Solanki
  Cc: zev, Liam Girdwood, Jean Delvare, Guenter Roeck, linux-kernel,
	linux-hwmon

[-- Attachment #1: Type: text/plain, Size: 282 bytes --]

On Tue, Sep 19, 2023 at 07:48:23AM +0200, Naresh Solanki wrote:
> Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage
> and current monitoring. This feature is seamlessly integrated through
> the hwmon subsystem.

Guenter, are you OK with the hwmon side of this?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v4] regulator (max5970): Add hwmon support
  2023-09-19  5:48 [PATCH v4] regulator (max5970): Add hwmon support Naresh Solanki
  2023-09-26 15:28 ` Mark Brown
@ 2023-09-26 19:36 ` Guenter Roeck
  2023-10-04  8:22   ` Naresh Solanki
  1 sibling, 1 reply; 5+ messages in thread
From: Guenter Roeck @ 2023-09-26 19:36 UTC (permalink / raw)
  To: Naresh Solanki
  Cc: broonie, zev, Liam Girdwood, Jean Delvare, linux-kernel, linux-hwmon

On Tue, Sep 19, 2023 at 07:48:23AM +0200, Naresh Solanki wrote:
> Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage
> and current monitoring. This feature is seamlessly integrated through
> the hwmon subsystem.
> 
> Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
> ---
> 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 | 138 +++++++++++++++++++++++++-
>  1 file changed, 136 insertions(+), 2 deletions(-)
> 
> 
> base-commit: 41f02a383ac652f1a0b5538c5901b7ec93e37290
> 
> diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
> index b56a174cde3d..06168fe1dadd 100644
> --- a/drivers/regulator/max5970-regulator.c
> +++ b/drivers/regulator/max5970-regulator.c
> @@ -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 (!rdevs || !rdevs[channel])

How can rdevs be NULL here ? Unless I am missing something, that can
only happen if its allocation in the probe function failed, but then
the system would long since have crashed before it gets here.

Also, rdevs[] only has as many entries as there are switches. That can
be 1 or 2. However, the hwmon subsystem will always assume that there are
two channels, because it is told so, and it will thus try to access rdevs[1]
even if only a single channel was allocated.

> +		return 0;
> +
> +	ddata = rdev_get_drvdata(rdevs[channel]);

With the above in mind, I wonder what that points to if channel == 1 and
ddata->num_switches == 1.

> +
> +	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;
>  
> @@ -451,9 +575,10 @@ static int max597x_regulator_probe(struct platform_device *pdev)
>  	else
>  		return -ENODEV;
>  
> -	i2c_set_clientdata(i2c, max597x);
>  	num_switches = max597x->num_switches;
>  
> +	rdevs = devm_kcalloc(&i2c->dev, num_switches, sizeof(struct regulator_dev *), GFP_KERNEL);
> +

What if devm_kcalloc() returns NULL ?

>  	for (i = 0; i < num_switches; i++) {
>  		data =
>  		    devm_kzalloc(&i2c->dev, sizeof(struct max5970_regulator),
> @@ -485,6 +610,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,

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

* Re: [PATCH v4] regulator (max5970): Add hwmon support
  2023-09-26 15:28 ` Mark Brown
@ 2023-09-26 19:43   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2023-09-26 19:43 UTC (permalink / raw)
  To: Mark Brown
  Cc: Naresh Solanki, zev, Liam Girdwood, Jean Delvare, linux-kernel,
	linux-hwmon

On Tue, Sep 26, 2023 at 05:28:15PM +0200, Mark Brown wrote:
> On Tue, Sep 19, 2023 at 07:48:23AM +0200, Naresh Solanki wrote:
> > Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage
> > and current monitoring. This feature is seamlessly integrated through
> > the hwmon subsystem.
> 
> Guenter, are you OK with the hwmon side of this?

Sorry, I lost track. Not really; see my response to the patch.

I think the memory allocation in that driver is a bit too complicated.
It trades simplicity for minor memory savings, and even those savings
are questionable. It would have been much easier and simpler to just
add a two-element regulator_dev array to struct max5970_regulator
and live with the "lost" memory if only one of them is used. I suspect
that would have consumed less memory and code overall.

Guenter

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

* Re: [PATCH v4] regulator (max5970): Add hwmon support
  2023-09-26 19:36 ` Guenter Roeck
@ 2023-10-04  8:22   ` Naresh Solanki
  0 siblings, 0 replies; 5+ messages in thread
From: Naresh Solanki @ 2023-10-04  8:22 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: broonie, zev, Liam Girdwood, Jean Delvare, linux-kernel, linux-hwmon

Hi Guenter,


On Wed, 27 Sept 2023 at 01:06, Guenter Roeck <linux@roeck-us.net> wrote:
>
> On Tue, Sep 19, 2023 at 07:48:23AM +0200, Naresh Solanki wrote:
> > Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage
> > and current monitoring. This feature is seamlessly integrated through
> > the hwmon subsystem.
> >
> > Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
> > ---
> > 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 | 138 +++++++++++++++++++++++++-
> >  1 file changed, 136 insertions(+), 2 deletions(-)
> >
> >
> > base-commit: 41f02a383ac652f1a0b5538c5901b7ec93e37290
> >
> > diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
> > index b56a174cde3d..06168fe1dadd 100644
> > --- a/drivers/regulator/max5970-regulator.c
> > +++ b/drivers/regulator/max5970-regulator.c
> > @@ -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 (!rdevs || !rdevs[channel])
>
> How can rdevs be NULL here ? Unless I am missing something, that can
> only happen if its allocation in the probe function failed, but then
> the system would long since have crashed before it gets here.
>
> Also, rdevs[] only has as many entries as there are switches. That can
> be 1 or 2. However, the hwmon subsystem will always assume that there are
> two channels, because it is told so, and it will thus try to access rdevs[1]
> even if only a single channel was allocated.
Yes. Will update to:
if (!rdevs[channel])
        return 0;
Also will simplify memory allocation to have two element array.
>
> > +             return 0;
> > +
> > +     ddata = rdev_get_drvdata(rdevs[channel]);
>
> With the above in mind, I wonder what that points to if channel == 1 and
> ddata->num_switches == 1.
This can be avoided with memory allocation simplification & previous
if condition check.
>
> > +
> > +     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;
> >
> > @@ -451,9 +575,10 @@ static int max597x_regulator_probe(struct platform_device *pdev)
> >       else
> >               return -ENODEV;
> >
> > -     i2c_set_clientdata(i2c, max597x);
> >       num_switches = max597x->num_switches;
> >
> > +     rdevs = devm_kcalloc(&i2c->dev, num_switches, sizeof(struct regulator_dev *), GFP_KERNEL);
> > +
>
> What if devm_kcalloc() returns NULL ?
Will add error checking here.
>
> >       for (i = 0; i < num_switches; i++) {
> >               data =
> >                   devm_kzalloc(&i2c->dev, sizeof(struct max5970_regulator),
> > @@ -485,6 +610,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,


Regards,
Naresh

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

end of thread, other threads:[~2023-10-04  8:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-19  5:48 [PATCH v4] regulator (max5970): Add hwmon support Naresh Solanki
2023-09-26 15:28 ` Mark Brown
2023-09-26 19:43   ` Guenter Roeck
2023-09-26 19:36 ` Guenter Roeck
2023-10-04  8:22   ` Naresh Solanki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).