Unverified Commit f5afdd13 authored by Naresh Solanki's avatar Naresh Solanki Committed by Mark Brown
Browse files

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.

Signed-off-by: default avatarNaresh Solanki <naresh.solanki@9elements.com>
Acked-by: default avatarGuenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20231027152830.1269895-1-naresh.solanki@9elements.com


Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent ae61939c
Loading
Loading
Loading
Loading
+143 −1
Original line number Diff line number Diff line
@@ -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,132 @@ 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);
			if (ret < 0)
				return ret;
			/*
			 * 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 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;
			/*
			 * 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 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])
		return 0;

	ddata = rdev_get_drvdata(rdevs[channel]);

	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)
				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 +558,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 +570,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"))
@@ -485,6 +618,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,