Skip to contentSkip to conversation

staging: iio: adc: ad7816: Use devm_gpiod_get_optional() for busy GPIO

PATCH Superseded Taha Narimani 5 participants 8 messages Jun 3

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: iio: adc: ad7816: Use devm_gpiod_get_optional() for busy GPIO

The driver currently utilizes devm_gpiod_get() for the 'busy' line, which makes the GPIO mandatory. However, the busy pin is hardware-optional depending on the specific board configuration.

Switch to devm_gpiod_get_optional() to allow boards that do not have this pin wired up to still probe the driver successfully, and remove the redundant conditional chip-ID check since the optional API handles missing descriptors gracefully.

---
Changes in v2:
- Fixed trailing whitespace and missing newline at the end of the file.
- Converted the file format to Unix (LF) to remove carriage returns.
- Removed the explicit chip-ID check around the busy pin logic.
- Improved the commit message to provide clear architectural justification.

 drivers/staging/iio/adc/ad7816.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/staging/iio/adc/ad7816.c +9 -10
index 0eac484..039b34d 100644
@@ -84,7 +84,7 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
 		gpiod_set_value(chip->convert_pin, 1);
 	}
 
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {
 		while (gpiod_get_value(chip->busy_pin))
 			cpu_relax();
 	}
@@ -380,15 +380,14 @@ static int ad7816_probe(struct spi_device *spi_dev)
 			ret);
 		return ret;
 	}
-	if (chip->id == ID_AD7817) {
-		chip->busy_pin = devm_gpiod_get(&spi_dev->dev, "busy",
-						GPIOD_IN);
-		if (IS_ERR(chip->busy_pin)) {
-			ret = PTR_ERR(chip->busy_pin);
-			dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
-				ret);
-			return ret;
-		}
+
+	chip->busy_pin = devm_gpiod_get_optional(&spi_dev->dev, "busy",
+						 GPIOD_IN);
+	if (IS_ERR(chip->busy_pin)) {
+		ret = PTR_ERR(chip->busy_pin);
+		dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
+			ret);
+		return ret;
 	}
 
 	indio_dev->name = spi_get_device_id(spi_dev)->name;
Signature · 1 line
2.53.0
#1 lore raw

drivers/staging/iio/adc/ad7816.c static int ad7816_probe(struct spi_device *spi_dev) · line 389

Quoted 7 lines · Taha Narimani

---

drivers/staging/iio/adc/ad7816.c +6 -0
+       chip->busy_pin = devm_gpiod_get_optional(&spi_dev->dev, "busy",
+                                                GPIOD_IN);
+       if (IS_ERR(chip->busy_pin)) {
+               ret = PTR_ERR(chip->busy_pin);
+               dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
+                       ret);

Looking at this, you could eventually move all of the dev_err() calls in the probe() function to use dev_err_probe(). (For another patch though).

Signature · 3 lines
Kind regards

CJD
#2 lore raw
Quoted 1 line · Joshua Crofts

On Wed, 3 Jun 2026 at 11:07, Taha Narimani <[email protected]> wrote:

...

Quoted 9 lines

drivers/staging/iio/adc/ad7816.c static int ad7816_probe(struct spi_device *spi_dev) · line 389

Quoted 6 lines
drivers/staging/iio/adc/ad7816.c +6 -0
+       chip->busy_pin = devm_gpiod_get_optional(&spi_dev->dev, "busy",
+                                                GPIOD_IN);
+       if (IS_ERR(chip->busy_pin)) {
+               ret = PTR_ERR(chip->busy_pin);
+               dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
+                       ret);

Looking at this, you could eventually move all of the dev_err() calls in the probe() function to use dev_err_probe(). (For another patch though).

No, it must be in this patch, otherwise it might regress quite badly (from the user, who wants to see a bootlog nice and clean, perspective).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Taha Narimani
Quoted 8 lines · Taha Narimani

The driver currently utilizes devm_gpiod_get() for the 'busy' line, which makes the GPIO mandatory. However, the busy pin is hardware-optional depending on the specific board configuration.

Switch to devm_gpiod_get_optional() to allow boards that do not have this pin wired up to still probe the driver successfully, and remove the redundant conditional chip-ID check since the optional API handles missing descriptors gracefully.

...

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) · line 87

Quoted 2 lines
drivers/staging/iio/adc/ad7816.c +1 -1
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {

If we get GPIO optional, this check wouldn't be necessary anymore as the below should return 0 IIRC in this case.

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)

Quoted 6 lines
drivers/staging/iio/adc/ad7816.c +2 -0
 		while (gpiod_get_value(chip->busy_pin))
 			cpu_relax();
 	}
 
+	chip->busy_pin = devm_gpiod_get_optional(&spi_dev->dev, "busy",
+						 GPIOD_IN);

Make it a single line. Perhaps with a help of

	struct device *dev = &spi_dev->dev;

added to the top of the function.

drivers/staging/iio/adc/ad7816.c static int ad7816_probe(struct spi_device *spi_dev) · line 390

Quoted 5 lines
drivers/staging/iio/adc/ad7816.c +5 -0
+	if (IS_ERR(chip->busy_pin)) {
+		ret = PTR_ERR(chip->busy_pin);
+		dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
+			ret);
+		return ret;

You can't do that, it will spam bootlog very quickly if this GPIO is deferred and never appears. The proper way is to

		return dev_err_probe(dev, PTR_ERR(chip->busy_pin), "...");
Quoted 1 line

}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Taha Narimani

The driver currently utilizes devm_gpiod_get() for the 'busy' line, which makes the GPIO mandatory. However, the busy pin is hardware-optional depending on the specific board configuration.

Switch to devm_gpiod_get_optional() to allow boards that do not have this pin wired up to still probe the driver successfully, and remove the redundant conditional chip-ID check since the optional API handles missing descriptors gracefully.

Quoted 8 lines

...

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) · line 87

Quoted 2 lines
drivers/staging/iio/adc/ad7816.c +1 -1
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {

If we get GPIO optional, this check wouldn't be necessary anymore as the below should return 0 IIRC in this case.

No, it's still necessary. It can be NULL because of the CONFIG_ in which case, sure, gpiod_get_value() is a no-op. But it can also be NULL because of the device tree and in that case we need the check to avoid a NULL pointer dereference.

Quoted 3 lines

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) · line 90

Quoted 3 lines
drivers/staging/iio/adc/ad7816.c +0 -0
 		while (gpiod_get_value(chip->busy_pin))
 			cpu_relax();
 	}

regards,
dan carpenter

#5 lore raw
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Wed, Jun 03, 2026 at 12:33:33PM +0000, Taha Narimani wrote:

...

Quoted 10 lines
Quoted 5 lines

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) · line 87

Quoted 2 lines
drivers/staging/iio/adc/ad7816.c +1 -1
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {

If we get GPIO optional, this check wouldn't be necessary anymore as the below should return 0 IIRC in this case.

No, it's still necessary. It can be NULL because of the CONFIG_ in which case, sure, gpiod_get_value() is a no-op. But it can also be NULL because of the device tree and in that case we need the check to avoid a NULL pointer dereference.

Can you elaborate on the latter more? I fail to see that. What I see is that the function either implemented or not is NULL-aware.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/staging/iio/adc/ad7816.c +0 -0
 		while (gpiod_get_value(chip->busy_pin))
 			cpu_relax();
 	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Wed, Jun 03, 2026 at 12:33:33PM +0000, Taha Narimani wrote:

...

Quoted 6 lines
Quoted 5 lines

drivers/staging/iio/adc/ad7816.c static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data) · line 87

Quoted 2 lines
drivers/staging/iio/adc/ad7816.c +1 -1
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {

If we get GPIO optional, this check wouldn't be necessary anymore as the below should return 0 IIRC in this case.

Quoted 8 lines
Quoted 4 lines

No, it's still necessary. It can be NULL because of the CONFIG_ in which case, sure, gpiod_get_value() is a no-op. But it can also be NULL because of the device tree and in that case we need the check to avoid a NULL pointer dereference.

Can you elaborate on the latter more? I fail to see that. What I see is that the function either implemented or not is NULL-aware.

Ah, yes. You're right. The VALIDATE_DESC() macro has a return hiding inside. I hadn't seen that.

regards,
dan carpenter

maintainer
#7 lore raw
In reply to Taha Narimani
Quoted 10 lines · On Wed, 3 Jun 2026 12:33:33 +0000 Taha Narimani <[email protected]>…

The driver currently utilizes devm_gpiod_get() for the 'busy' line, which makes the GPIO mandatory. However, the busy pin is hardware-optional depending on the specific board configuration.

Switch to devm_gpiod_get_optional() to allow boards that do not have this pin wired up to still probe the driver successfully, and remove the redundant conditional chip-ID check since the optional API handles missing descriptors gracefully.

I tried to pick this up, but it doesn't apply. I suspect that's because you've put it on top of your previous patch which modified the checks on chip->id

Please send it as a single patch. Also this is fixing a false assumption in the driver so it should have an appropriate Fixes tag.

Trivial comment inline.

Thanks,

Jonathan

Quoted 29 lines

---
Changes in v2:
- Fixed trailing whitespace and missing newline at the end of the file.
- Converted the file format to Unix (LF) to remove carriage returns.
- Removed the explicit chip-ID check around the busy pin logic.
- Improved the commit message to provide clear architectural justification.

 drivers/staging/iio/adc/ad7816.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/staging/iio/adc/ad7816.c +1 -3
index 0eac484..039b34d 100644
@@ -84,7 +84,7 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip, u16 *data)
 		gpiod_set_value(chip->convert_pin, 1);
 	}
 
-	if (chip->id == ID_AD7817) {
+	if (chip->busy_pin) {
 		while (gpiod_get_value(chip->busy_pin))
 			cpu_relax();
 	}
@@ -380,15 +380,14 @@ static int ad7816_probe(struct spi_device *spi_dev)
 			ret);
 		return ret;
 	}
-	if (chip->id == ID_AD7817) {
-		chip->busy_pin = devm_gpiod_get(&spi_dev->dev, "busy",

drivers/staging/iio/adc/ad7816.c static int ad7816_probe(struct spi_device *spi_dev) · line 385

Quoted 10 lines
drivers/staging/iio/adc/ad7816.c +3 -7
-						GPIOD_IN);
-		if (IS_ERR(chip->busy_pin)) {
-			ret = PTR_ERR(chip->busy_pin);
-			dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
-				ret);
-			return ret;
-		}
+
+	chip->busy_pin = devm_gpiod_get_optional(&spi_dev->dev, "busy",
+						 GPIOD_IN);

Trivial: We are more relaxed on line lengths these days so for cases like this where it would only go a little past 80 chars to have it on one line I would generally prefer it that way.

Quoted 8 lines
drivers/staging/iio/adc/ad7816.c +5 -0
+	if (IS_ERR(chip->busy_pin)) {
+		ret = PTR_ERR(chip->busy_pin);
+		dev_err(&spi_dev->dev, "Failed to request busy GPIO: %d\n",
+			ret);
+		return ret;
 	}
 
 	indio_dev->name = spi_get_device_id(spi_dev)->name;