* [PATCH v3] iio: dac: max5522: simplify probe with devm_regulator_get_enable_read_voltage()
@ 2026-02-16 13:17 Antoniu Miclaus
2026-02-20 11:04 ` Jonathan Cameron
0 siblings, 1 reply; 2+ messages in thread
From: Antoniu Miclaus @ 2026-02-16 13:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, Antoniu Miclaus, linux-iio, linux-kernel
Cc: Andy Shevchenko
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.
The reference voltage for this DAC is a fixed hardware configuration
that is not expected to change at runtime, so reading it once during
probe and caching the millivolt value is sufficient.
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
---
Changes in v3:
- change vref_mV type from unsigned short to int to avoid potential
precision loss
- update commit message to clarify why caching voltage at probe is
sufficient
drivers/iio/dac/max5522.c | 22 +++++++---------------
1 file changed, 7 insertions(+), 15 deletions(-)
diff --git a/drivers/iio/dac/max5522.c b/drivers/iio/dac/max5522.c
index 1b8fe6b8d26e..b52a9cc1da79 100644
--- a/drivers/iio/dac/max5522.c
+++ b/drivers/iio/dac/max5522.c
@@ -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;
+ int 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);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v3] iio: dac: max5522: simplify probe with devm_regulator_get_enable_read_voltage()
2026-02-16 13:17 [PATCH v3] iio: dac: max5522: simplify probe with devm_regulator_get_enable_read_voltage() Antoniu Miclaus
@ 2026-02-20 11:04 ` Jonathan Cameron
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-02-20 11:04 UTC (permalink / raw)
To: Antoniu Miclaus
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, Andy Shevchenko
On Mon, 16 Feb 2026 15:17:05 +0200
Antoniu Miclaus <antoniu.miclaus@analog.com> wrote:
> 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.
>
> The reference voltage for this DAC is a fixed hardware configuration
> that is not expected to change at runtime, so reading it once during
> probe and caching the millivolt value is sufficient.
>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
Applied. Thanks,
Jonathan
> ---
> Changes in v3:
> - change vref_mV type from unsigned short to int to avoid potential
> precision loss
> - update commit message to clarify why caching voltage at probe is
> sufficient
>
> drivers/iio/dac/max5522.c | 22 +++++++---------------
> 1 file changed, 7 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/iio/dac/max5522.c b/drivers/iio/dac/max5522.c
> index 1b8fe6b8d26e..b52a9cc1da79 100644
> --- a/drivers/iio/dac/max5522.c
> +++ b/drivers/iio/dac/max5522.c
> @@ -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;
> + int 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);
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-20 11:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-16 13:17 [PATCH v3] iio: dac: max5522: simplify probe with devm_regulator_get_enable_read_voltage() Antoniu Miclaus
2026-02-20 11:04 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome