drivers/regulator/Kconfig | 1 + drivers/regulator/max5970-regulator.c | 123 ++++++++++++++++++++++++++ 2 files changed, 124 insertions(+)
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 V2:
- default case added for switch statement
- Add dependency on HWMON
---
drivers/regulator/Kconfig | 1 +
drivers/regulator/max5970-regulator.c | 123 ++++++++++++++++++++++++++
2 files changed, 124 insertions(+)
diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 965d4f0c18a6..ab245893033d 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -559,6 +559,7 @@ config REGULATOR_MAX5970
depends on I2C
depends on OF
depends on MFD_MAX5970
+ depends on HWMON
help
This driver controls a Maxim 5970/5978 switch via I2C bus.
The MAX5970/5978 is a smart switch with no output regulation, but
diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
index b56a174cde3d..c337044e1523 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,120 @@ 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, ®_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 max5970_data *ddata = dev_get_drvdata(dev);
+ struct regmap *regmap = dev_get_regmap(dev->parent, NULL);
+ 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[channel]) >> 10;
+ /* Convert the voltage meansurement across shunt resistor to current */
+ *val = (*val * 1000) / ddata->shunt_micro_ohms[channel];
+ 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[channel], 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 max5970_data *ddata = (struct max5970_data *)data;
+
+ if (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[channel])
+ return 0444;
+ 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;
@@ -432,6 +547,7 @@ static int max597x_regulator_probe(struct platform_device *pdev)
struct regulator_config config = { };
struct regulator_dev *rdev;
struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
+ struct device *hwmon_dev;
int num_switches;
int ret, i;
@@ -485,6 +601,13 @@ static int max597x_regulator_probe(struct platform_device *pdev)
max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
}
+ hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970_hwmon", max597x,
+ &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: 35d0d2350d774fecf596cfb2fb050559fe5e1850
--
2.41.0
Hi Naresh,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 35d0d2350d774fecf596cfb2fb050559fe5e1850]
url: https://github.com/intel-lab-lkp/linux/commits/Naresh-Solanki/regulator-max5970-Add-hwmon-support/20230901-173611
base: 35d0d2350d774fecf596cfb2fb050559fe5e1850
patch link: https://lore.kernel.org/r/20230901093449.838414-1-Naresh.Solanki%409elements.com
patch subject: [PATCH v2] regulator (max5970): Add hwmon support
config: x86_64-randconfig-004-20230902 (https://download.01.org/0day-ci/archive/20230902/202309020434.x0wzOQpo-lkp@intel.com/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230902/202309020434.x0wzOQpo-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202309020434.x0wzOQpo-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/regulator/max5970-regulator.c:124:3: warning: unannotated fall-through between switch labels [-Wimplicit-fallthrough]
default:
^
drivers/regulator/max5970-regulator.c:124:3: note: insert 'break;' to avoid fall-through
default:
^
break;
1 warning generated.
vim +124 drivers/regulator/max5970-regulator.c
99
100 static umode_t max5970_is_visible(const void *data,
101 enum hwmon_sensor_types type,
102 u32 attr, int channel)
103 {
104 struct max5970_data *ddata = (struct max5970_data *)data;
105
106 if (channel >= ddata->num_switches)
107 return 0;
108
109 switch (type) {
110 case hwmon_in:
111 switch (attr) {
112 case hwmon_in_input:
113 return 0444;
114 default:
115 break;
116 }
117 break;
118 case hwmon_curr:
119 switch (attr) {
120 case hwmon_curr_input:
121 /* Current measurement requires knowledge of the shunt resistor value. */
122 if (ddata->shunt_micro_ohms[channel])
123 return 0444;
> 124 default:
125 break;
126 }
127 break;
128 default:
129 break;
130 }
131 return 0;
132 }
133
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On 9/1/23 02:34, 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>
Nit, but you still have this:
CHECK: From:/Signed-off-by: email comments mismatch: 'From: Naresh Solanki <naresh.solanki@9elements.com>' != 'Signed-off-by: Naresh Solanki <Naresh.Solanki@9elements.com>'
> ---
> Changes in V2:
> - default case added for switch statement
> - Add dependency on HWMON
> ---
> drivers/regulator/Kconfig | 1 +
> drivers/regulator/max5970-regulator.c | 123 ++++++++++++++++++++++++++
> 2 files changed, 124 insertions(+)
>
> diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
> index 965d4f0c18a6..ab245893033d 100644
> --- a/drivers/regulator/Kconfig
> +++ b/drivers/regulator/Kconfig
> @@ -559,6 +559,7 @@ config REGULATOR_MAX5970
> depends on I2C
> depends on OF
> depends on MFD_MAX5970
> + depends on HWMON
Not sure if that is acceptable. The maintainer will have to decide.
> help
> This driver controls a Maxim 5970/5978 switch via I2C bus.
> The MAX5970/5978 is a smart switch with no output regulation, but
> diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
> index b56a174cde3d..c337044e1523 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,120 @@ 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, ®_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 max5970_data *ddata = dev_get_drvdata(dev);
> + struct regmap *regmap = dev_get_regmap(dev->parent, NULL);
> + 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[channel]) >> 10;
> + /* Convert the voltage meansurement across shunt resistor to current */
> + *val = (*val * 1000) / ddata->shunt_micro_ohms[channel];
> + 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[channel], 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 max5970_data *ddata = (struct max5970_data *)data;
> +
> + if (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[channel])
> + return 0444;
missing break;
Interesting, I thought the compiler would complain about that nowadays,
but apparently it doesn't.
Guenter
> + 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;
> @@ -432,6 +547,7 @@ static int max597x_regulator_probe(struct platform_device *pdev)
> struct regulator_config config = { };
> struct regulator_dev *rdev;
> struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
> + struct device *hwmon_dev;
> int num_switches;
> int ret, i;
>
> @@ -485,6 +601,13 @@ static int max597x_regulator_probe(struct platform_device *pdev)
> max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
> }
>
> + hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970_hwmon", max597x,
> + &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: 35d0d2350d774fecf596cfb2fb050559fe5e1850
Hi
On Fri, 1 Sept 2023 at 20:52, Guenter Roeck <linux@roeck-us.net> wrote:
>
> On 9/1/23 02:34, 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>
>
> Nit, but you still have this:
>
> CHECK: From:/Signed-off-by: email comments mismatch: 'From: Naresh Solanki <naresh.solanki@9elements.com>' != 'Signed-off-by: Naresh Solanki <Naresh.Solanki@9elements.com>'
Ack. Will fix it in V3.
>
> > ---
> > Changes in V2:
> > - default case added for switch statement
> > - Add dependency on HWMON
> > ---
> > drivers/regulator/Kconfig | 1 +
> > drivers/regulator/max5970-regulator.c | 123 ++++++++++++++++++++++++++
> > 2 files changed, 124 insertions(+)
> >
> > diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
> > index 965d4f0c18a6..ab245893033d 100644
> > --- a/drivers/regulator/Kconfig
> > +++ b/drivers/regulator/Kconfig
> > @@ -559,6 +559,7 @@ config REGULATOR_MAX5970
> > depends on I2C
> > depends on OF
> > depends on MFD_MAX5970
> > + depends on HWMON
>
> Not sure if that is acceptable. The maintainer will have to decide.
>
> > help
> > This driver controls a Maxim 5970/5978 switch via I2C bus.
> > The MAX5970/5978 is a smart switch with no output regulation, but
> > diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c
> > index b56a174cde3d..c337044e1523 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,120 @@ 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, ®_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 max5970_data *ddata = dev_get_drvdata(dev);
> > + struct regmap *regmap = dev_get_regmap(dev->parent, NULL);
> > + 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[channel]) >> 10;
> > + /* Convert the voltage meansurement across shunt resistor to current */
> > + *val = (*val * 1000) / ddata->shunt_micro_ohms[channel];
> > + 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[channel], 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 max5970_data *ddata = (struct max5970_data *)data;
> > +
> > + if (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[channel])
> > + return 0444;
>
> missing break;
>
> Interesting, I thought the compiler would complain about that nowadays,
> but apparently it doesn't.
>
> Guenter
>
> > + 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;
> > @@ -432,6 +547,7 @@ static int max597x_regulator_probe(struct platform_device *pdev)
> > struct regulator_config config = { };
> > struct regulator_dev *rdev;
> > struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES];
> > + struct device *hwmon_dev;
> > int num_switches;
> > int ret, i;
> >
> > @@ -485,6 +601,13 @@ static int max597x_regulator_probe(struct platform_device *pdev)
> > max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms;
> > }
> >
> > + hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970_hwmon", max597x,
> > + &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: 35d0d2350d774fecf596cfb2fb050559fe5e1850
>
© 2016 - 2026 Red Hat, Inc.