Skip to contentSkip to conversation

iio: dac: max5522: use devm_regulator_get_enable_read_voltage()

PATCH Superseded Antoniu Miclaus 4 participants 7 messages Feb 10 – Feb 15 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/iio/dac/max5522.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
drivers/iio/dac/max5522.c +6 -15
index 1b8fe6b8d26e..cda1cf572e77 100644
@@ -34,7 +34,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 +79,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 +143,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 / 1000;
 
 	state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
 
Signature · 1 line
2.43.0
#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.

...

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

Quoted 2 lines
drivers/iio/dac/max5522.c +1 -1
-	struct regulator *vrefin_reg;
+	unsigned short vref_mv;

vref_mV ?

...

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 12 lines · On Tue, 10 Feb 2026 21:35:26 +0200 Andy Shevchenko…
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.

...

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

Quoted 2 lines
drivers/iio/dac/max5522.c +1 -1
-	struct regulator *vrefin_reg;
+	unsigned short vref_mv;  

vref_mV ?

Quoted 6 lines

...

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;  

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

I know I've argued against proliferation of the X_PER_Y macros but this one does seem to be very common. Maybe it's time to bring in

MICROVOLTS_PER_MILLIVOLT even if it's longer than the open coded version?

Andy, what do you think? I do worry that people will see it as fine to add many many others if we start with this one though. So maybe not.

Anyhow, doesn't need to be in this series.

Jonathan

Quoted 1 line
#3 lore raw
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Tue, 10 Feb 2026 21:35:26 +0200 Andy Shevchenko…

On Tue, Feb 10, 2026 at 07:51:27PM +0200, Antoniu Miclaus wrote:

...

Quoted 5 lines
Quoted 4 lines

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;  

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

Quoted 7 lines

I know I've argued against proliferation of the X_PER_Y macros but this one does seem to be very common. Maybe it's time to bring in

MICROVOLTS_PER_MILLIVOLT even if it's longer than the open coded version?

Andy, what do you think? I do worry that people will see it as fine to add many many others if we start with this one though. So maybe not.

IIRC (sorry, if I am wrong) it was you who objected in the first place and I proposed that from day 1 discussing the "(MICRO / MILLI)" approach a year (?) or so ago. Answering to the Q: I am in favour of having that definition.

Quoted 1 line

Anyhow, doesn't need to be in this series.

True.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#4 lore raw
Quoted 20 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Tue, 10 Feb 2026 21:35:26 +0200 Andy Shevchenko…

On Tue, Feb 10, 2026 at 07:51:27PM +0200, Antoniu Miclaus wrote:

...

Quoted 5 lines
Quoted 4 lines

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;  

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

Quoted 7 lines

I know I've argued against proliferation of the X_PER_Y macros but this one does seem to be very common. Maybe it's time to bring in

MICROVOLTS_PER_MILLIVOLT even if it's longer than the open coded version?

Andy, what do you think? I do worry that people will see it as fine to add many many others if we start with this one though. So maybe not.

Quoted 9 lines

IIRC (sorry, if I am wrong) it was you who objected in the first place and I proposed that from day 1 discussing the "(MICRO / MILLI)" approach a year (?) or so ago. Answering to the Q: I am in favour of having that definition.

Quoted 1 line

Anyhow, doesn't need to be in this series.

True.

How about MICRO_PER_MILLI? Then we won't have to add a new one for every different type of unit.

#5 lore raw
Quoted 5 lines · David Lechner
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Tue, 10 Feb 2026 21:35:26 +0200 Andy Shevchenko…

On Tue, Feb 10, 2026 at 07:51:27PM +0200, Antoniu Miclaus wrote:

...

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
Quoted 4 lines

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;  

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

Quoted 7 lines

I know I've argued against proliferation of the X_PER_Y macros but this one does seem to be very common. Maybe it's time to bring in

MICROVOLTS_PER_MILLIVOLT even if it's longer than the open coded version?

Andy, what do you think? I do worry that people will see it as fine to add many many others if we start with this one though. So maybe not.

IIRC (sorry, if I am wrong) it was you who objected in the first place and I proposed that from day 1 discussing the "(MICRO / MILLI)" approach a year (?) or so ago. Answering to the Q: I am in favour of having that definition.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Anyhow, doesn't need to be in this series.

True.

How about MICRO_PER_MILLI? Then we won't have to add a new one for every different type of unit.

Dunno, as a physicist, I prefer the way with units.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#6 lore raw
Quoted 19 lines · On Sun, 15 Feb 2026 09:53:08 +0200 Andy Shevchenko…
Quoted 5 lines · David Lechner
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Jonathan Cameron
Quoted 1 line · On Tue, 10 Feb 2026 21:35:26 +0200 Andy Shevchenko…

On Tue, Feb 10, 2026 at 07:51:27PM +0200, Antoniu Miclaus wrote:

...

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 4 lines

drivers/iio/dac/max5522.c static int max5522_spi_probe(struct spi_device *spi) · line 150

Quoted 1 line
drivers/iio/dac/max5522.c +1 -0
+	state->vref_mv = ret / 1000;    

"1000" --> "(MICRO / MILLI)" ?
(yes, this way with the parentheses).

I know I've argued against proliferation of the X_PER_Y macros but this one does seem to be very common. Maybe it's time to bring in

MICROVOLTS_PER_MILLIVOLT even if it's longer than the open coded version?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Andy, what do you think? I do worry that people will see it as fine to add many many others if we start with this one though. So maybe not.

IIRC (sorry, if I am wrong) it was you who objected in the first place and I proposed that from day 1 discussing the "(MICRO / MILLI)" approach a year (?) or so ago. Answering to the Q: I am in favour of having that definition.

Yup. I got convinced over time! I still a little doubtful but seemed like it was worth revisiting the question.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Anyhow, doesn't need to be in this series.

True.

How about MICRO_PER_MILLI? Then we won't have to add a new one for every different type of unit.

I think it only commonly occurs for voltage because of the whole mismatch between the regulators framework and both IIO and HWMON in units. That's based on gut feel though rather actually checking the code.

Jonathan

Quoted 3 lines

Dunno, as a physicist, I prefer the way with units.