Skip to contentSkip to conversation

iio: dac: max5522: use devm_regulator_get_enable_read_voltage()

PATCH In review Antoniu Miclaus 3 participants 3 messages Feb 11 – Feb 14 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: dac: max5522: use devm_regulator_get_enable_read_voltage()

Simplify probe by using devm_regulator_get_enable_read_voltage() to get, enable and read the regulator voltage in a single call, caching the value at probe time.

---
Changes in v2:
- use vref_mV naming convention
- use (MICRO / MILLI) instead of 1000 for conversion

 drivers/iio/dac/max5522.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
drivers/iio/dac/max5522.c +7 -15
index 1b8fe6b8d26e..459b333a84a0 100644
@@ -14,6 +14,7 @@
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
+#include <linux/units.h>
 
 #include <linux/iio/iio.h>
 
@@ -34,7 +35,7 @@ struct max5522_state {
 	struct regmap *regmap;
 	const struct max5522_chip_info *chip_info;
 	unsigned short dac_cache[2];
-	struct regulator *vrefin_reg;
+	unsigned short vref_mV;
 };
 
 #define MAX5522_CHANNEL(chan) {	\
@@ -79,17 +80,13 @@ static int max5522_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long info)
 {
 	struct max5522_state *state = iio_priv(indio_dev);
-	int ret;
 
 	switch (info) {
 	case IIO_CHAN_INFO_RAW:
 		*val = state->dac_cache[chan->channel];
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		ret = regulator_get_voltage(state->vrefin_reg);
-		if (ret < 0)
-			return -EINVAL;
-		*val = ret / 1000;
+		*val = state->vref_mV;
 		*val2 = 10;
 		return IIO_VAL_FRACTIONAL_LOG2;
 	default:
@@ -147,16 +144,11 @@ static int max5522_spi_probe(struct spi_device *spi)
 	if (!state->chip_info)
 		return -EINVAL;
 
-	state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin");
-	if (IS_ERR(state->vrefin_reg))
-		return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg),
-				     "Vrefin regulator not specified\n");
-
-	ret = regulator_enable(state->vrefin_reg);
-	if (ret) {
+	ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vrefin");
+	if (ret < 0)
 		return dev_err_probe(&spi->dev, ret,
-				     "Failed to enable vref regulators\n");
-	}
+				     "Failed to get vrefin regulator\n");
+	state->vref_mV = ret / (MICRO / MILLI);
 
 	state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 3 lines · Antoniu Miclaus

Simplify probe by using devm_regulator_get_enable_read_voltage() to get, enable and read the regulator voltage in a single call, caching the value at probe time.

LGTM,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
In reply to Antoniu Miclaus
Quoted 3 lines · On Wed, 11 Feb 2026 10:18:27 +0200 Antoniu Miclaus…

Simplify probe by using devm_regulator_get_enable_read_voltage() to get, enable and read the regulator voltage in a single call, caching the value at probe time.

Mention more on why it should be fine to assume this doesn't change at runtime.

Quoted 22 lines

---
Changes in v2:
- use vref_mV naming convention
- use (MICRO / MILLI) instead of 1000 for conversion

 drivers/iio/dac/max5522.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
drivers/iio/dac/max5522.c +1 -0
index 1b8fe6b8d26e..459b333a84a0 100644
@@ -14,6 +14,7 @@
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/spi/spi.h>
+#include <linux/units.h>
 
 #include <linux/iio/iio.h>
 

drivers/iio/dac/max5522.c struct max5522_state { · line 38

Quoted 5 lines
drivers/iio/dac/max5522.c +1 -1
@@ -34,7 +35,7 @@ struct max5522_state {
 	struct regmap *regmap;
 	const struct max5522_chip_info *chip_info;
 	unsigned short dac_cache[2];
-	struct regulator *vrefin_reg;
+	unsigned short vref_mV;

Why a short?
Seems like the max you 'could' get is MAX_INT / 1000 which will not fit in an unsigned short.

Probably easier to just go with unsigned int and save the effort of reasonsing about it.

Or just use an int and don't even worry about the sign.

J

Quoted 43 lines
drivers/iio/dac/max5522.c +5 -14
 };
 
 #define MAX5522_CHANNEL(chan) {	\
@@ -79,17 +80,13 @@ static int max5522_read_raw(struct iio_dev *indio_dev,
 			    int *val, int *val2, long info)
 {
 	struct max5522_state *state = iio_priv(indio_dev);
-	int ret;
 
 	switch (info) {
 	case IIO_CHAN_INFO_RAW:
 		*val = state->dac_cache[chan->channel];
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_SCALE:
-		ret = regulator_get_voltage(state->vrefin_reg);
-		if (ret < 0)
-			return -EINVAL;
-		*val = ret / 1000;
+		*val = state->vref_mV;
 		*val2 = 10;
 		return IIO_VAL_FRACTIONAL_LOG2;
 	default:
@@ -147,16 +144,11 @@ static int max5522_spi_probe(struct spi_device *spi)
 	if (!state->chip_info)
 		return -EINVAL;
 
-	state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin");
-	if (IS_ERR(state->vrefin_reg))
-		return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg),
-				     "Vrefin regulator not specified\n");
-
-	ret = regulator_enable(state->vrefin_reg);
-	if (ret) {
+	ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vrefin");
+	if (ret < 0)
 		return dev_err_probe(&spi->dev, ret,
-				     "Failed to enable vref regulators\n");
-	}
+				     "Failed to get vrefin regulator\n");
+	state->vref_mV = ret / (MICRO / MILLI);
 
 	state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);