[PATCH v4] iio: dac: mcp47feb02: Fix passing uninitialized vref1_uV for no Vref1 case

Ariana Lazar posted 1 patch 3 days, 17 hours ago
drivers/iio/dac/mcp47feb02.c | 37 ++++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)
[PATCH v4] iio: dac: mcp47feb02: Fix passing uninitialized vref1_uV for no Vref1 case
Posted by Ariana Lazar 3 days, 17 hours ago
Ensure that if a device has Vref1 but reading the regulator returns an
error, mcp47feb02_init_ctrl_regs() is not called with an uninitialized
vref1_uV value. Also add a device_property_present() check for the Vref1
supply before reading the regulator.

Fixes: dd154646d292 ("iio: dac: mcp47feb02: Fix Vref validation [1-999] case")
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/all/adiPnla0M5EzvgD-@stanley.mountain/

Signed-off-by: Ariana Lazar <ariana.lazar@microchip.com>
---
Changes in v4:
- Remove handle ref mismatch fixes from this patch series to be submitted
   separately.
- Return err if an external voltage reference is selected, but is 0uV
- Link to v3: https://lore.kernel.org/r/20260603-mcp47feb02-fix4-v3-1-ed09975cad15@microchip.com

Changes in v3:
- Handle mismatch between vref modes at power up and enable regulator
   only after the user corrects it.
- Link to v2: https://lore.kernel.org/r/20260420-mcp47feb02-fix4-v2-1-8b758eaa8bcf@microchip.com

Changes in v2:
- return the reading regulator error to not use vref1_uV uninitialized in
   mcp47feb02_init_ctrl_regs() call 
- add device_property_present() check for the Vref1
- Link to v1: https://lore.kernel.org/r/20260414-mcp47feb02-fix4-v1-1-9d71badfd25e@microchip.com
---
 drivers/iio/dac/mcp47feb02.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/drivers/iio/dac/mcp47feb02.c b/drivers/iio/dac/mcp47feb02.c
index faccb804a5ed548088aaf83266b16ed45a92916c..5f84b46ec6f3d2f59ea815444a94444dfec00e91 100644
--- a/drivers/iio/dac/mcp47feb02.c
+++ b/drivers/iio/dac/mcp47feb02.c
@@ -1136,26 +1136,33 @@ static int mcp47feb02_probe(struct i2c_client *client)
 
 	vdd_uV = ret;
 
-	ret = devm_regulator_get_enable_read_voltage(dev, "vref");
-	if (ret > 0) {
-		vref_uV = ret;
+	if (device_property_present(dev, "vref-supply")) {
+		vref_uV = devm_regulator_get_enable_read_voltage(dev, "vref");
+		if (vref_uV < 0)
+			return vref_uV;
+
+		if (vref_uV == 0)
+			return dev_err_probe(dev, -EINVAL, "Vref is 0 uV.\n");
+
 		data->use_vref = true;
 	} else {
 		vref_uV = 0;
-		dev_dbg(dev, "using internal band gap as voltage reference.\n");
-		dev_dbg(dev, "Vref is unavailable.\n");
+		dev_dbg(dev, "Using internal band gap as voltage reference.\n");
 	}
 
-	if (chip_features->have_ext_vref1) {
-		ret = devm_regulator_get_enable_read_voltage(dev, "vref1");
-		if (ret > 0) {
-			vref1_uV = ret;
-			data->use_vref1 = true;
-		} else {
-			vref1_uV = 0;
-			dev_dbg(dev, "using internal band gap as voltage reference 1.\n");
-			dev_dbg(dev, "Vref1 is unavailable.\n");
-		}
+	if (chip_features->have_ext_vref1 &&
+	    device_property_present(dev, "vref1-supply")) {
+		vref1_uV = devm_regulator_get_enable_read_voltage(dev, "vref1");
+		if (vref1_uV < 0)
+			return vref1_uV;
+
+		if (vref1_uV == 0)
+			return dev_err_probe(dev, -EINVAL, "Vref1 is 0 uV.\n");
+
+		data->use_vref1 = true;
+	} else {
+		vref1_uV = 0;
+		dev_dbg(dev, "Using internal band gap as voltage reference 1.\n");
 	}
 
 	ret = mcp47feb02_init_ctrl_regs(data);

---
base-commit: 51e7665ab81f02adc80a1219c260ee678e9c6eb8
change-id: 20260414-mcp47feb02-fix4-614de9334f22

Best regards,
-- 
Ariana Lazar <ariana.lazar@microchip.com>
Re: [PATCH v4] iio: dac: mcp47feb02: Fix passing uninitialized vref1_uV for no Vref1 case
Posted by David Lechner 3 days, 15 hours ago
On Thu, Jun 4, 2026 at 3:47 PM Ariana Lazar <ariana.lazar@microchip.com> wrote:
>
> Ensure that if a device has Vref1 but reading the regulator returns an
> error, mcp47feb02_init_ctrl_regs() is not called with an uninitialized
> vref1_uV value. Also add a device_property_present() check for the Vref1
> supply before reading the regulator.
>
> Fixes: dd154646d292 ("iio: dac: mcp47feb02: Fix Vref validation [1-999] case")
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/all/adiPnla0M5EzvgD-@stanley.mountain/
>
> Signed-off-by: Ariana Lazar <ariana.lazar@microchip.com>
> ---

Reviewed-by: David Lechner <dlechner@baylibre.com>
Re: [PATCH v4] iio: dac: mcp47feb02: Fix passing uninitialized vref1_uV for no Vref1 case
Posted by Jonathan Cameron 2 days, 18 hours ago
On Thu, 4 Jun 2026 18:28:01 +0200
David Lechner <dlechner@baylibre.com> wrote:

> On Thu, Jun 4, 2026 at 3:47 PM Ariana Lazar <ariana.lazar@microchip.com> wrote:
> >
> > Ensure that if a device has Vref1 but reading the regulator returns an
> > error, mcp47feb02_init_ctrl_regs() is not called with an uninitialized
> > vref1_uV value. Also add a device_property_present() check for the Vref1
> > supply before reading the regulator.
> >
> > Fixes: dd154646d292 ("iio: dac: mcp47feb02: Fix Vref validation [1-999] case")
> > Reported-by: Dan Carpenter <error27@gmail.com>
> > Closes: https://lore.kernel.org/all/adiPnla0M5EzvgD-@stanley.mountain/
> >

Fixed this up. There must not be blank lines in tag blocks!  If I miss
cleaning these up then Greg won't take the pull. So be very careful about this.

> > Signed-off-by: Ariana Lazar <ariana.lazar@microchip.com>
> > ---  
> 
> Reviewed-by: David Lechner <dlechner@baylibre.com>
Applied to the fixes-togreg branch of iio.git.
This is a little less minimal than ideal, but it is still readable so
I've taken it without asking for it to be split into minimal fix + refactor.

Jonathan