Skip to contentSkip to conversation

iio: adc: ti-ads7950: normalize return value of gpio_get

PATCH Superseded Dmitry Torokhov 3 participants 3 messages Feb 18 – Feb 19

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: ti-ads7950: normalize return value of gpio_get

The GPIO get callback is expected to return 0 or 1 (or a negative error code). Ensure that the value returned by ti_ads7950_get() for output pins is normalized to the [0, 1] range.

---
 drivers/iio/adc/ti-ads7950.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads7950.c +1 -1
index bbe1ce577789..0c4db18ec4d7 100644
@@ -433,7 +433,7 @@ static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset)
 
 	/* If set as output, return the output */
 	if (st->gpio_cmd_settings_bitmask & BIT(offset)) {
-		ret = st->cmd_settings_bitmask & BIT(offset);
+		ret = !!(st->cmd_settings_bitmask & BIT(offset));
 		goto out;
 	}
 
Signature · 5 lines
2.53.0.335.g19a08e0c02-goog


-- 
Dmitry
reviewer
#1 lore raw
Quoted 14 lines · Dmitry Torokhov

The GPIO get callback is expected to return 0 or 1 (or a negative error code). Ensure that the value returned by ti_ads7950_get() for output pins is normalized to the [0, 1] range.

---
 drivers/iio/adc/ti-ads7950.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads7950.c +0 -0
index bbe1ce577789..0c4db18ec4d7 100644

drivers/iio/adc/ti-ads7950.c static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset) · line 436

Quoted 5 lines
drivers/iio/adc/ti-ads7950.c +1 -1
@@ -433,7 +433,7 @@ static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset)
 
 	/* If set as output, return the output */
 	if (st->gpio_cmd_settings_bitmask & BIT(offset)) {
-		ret = st->cmd_settings_bitmask & BIT(offset);
+		ret = !!(st->cmd_settings_bitmask & BIT(offset));
		ret = (st->cmd_settings_bitmask & BIT(offset)) ? 1 : 0;

Would be consistent with the style of the rest of the function (see below).

drivers/iio/adc/ti-ads7950.c static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset) · line 439

Quoted 3 lines
drivers/iio/adc/ti-ads7950.c +0 -0
 		goto out;
 	}
 

There is actually another bug with the return value of this function:

	ret = ((st->single_rx >> 12) & BIT(offset)) ? 1 : 0;

	/* Revert back to original settings */
	st->cmd_settings_bitmask &= ~TI_ADS7950_CR_GPIO_DATA;
	st->single_tx = TI_ADS7950_MAN_CMD_SETTINGS(st);
	ret = spi_sync(st->spi, &st->scan_single_msg);
	if (ret)
		goto out;

out:

	mutex_unlock(&st->slock);

	return ret;
}

When the gpio is an input, the function always returns 0 or error, never 1 because ret gets written over.

It looks like we need to introduce an extra variable to fix that one.

If we make the new variable bool, then we don't have to mess with !! or ? 1 : 0 to normalize it. The compiler will do it for us.

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Dmitry Torokhov
Quoted 26 lines · Dmitry Torokhov

The GPIO get callback is expected to return 0 or 1 (or a negative error code). Ensure that the value returned by ti_ads7950_get() for output pins is normalized to the [0, 1] range.

---
 drivers/iio/adc/ti-ads7950.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/ti-ads7950.c +1 -2
index bbe1ce577789..0c4db18ec4d7 100644
@@ -433,7 +433,7 @@ static int ti_ads7950_get(struct gpio_chip *chip, unsigned int offset)
 
 	/* If set as output, return the output */
 	if (st->gpio_cmd_settings_bitmask & BIT(offset)) {
-		ret = st->cmd_settings_bitmask & BIT(offset);
+		ret = !!(st->cmd_settings_bitmask & BIT(offset));
 		goto out;
 	}
 
--

2.53.0.335.g19a08e0c02-goog

Quoted 3 lines

--
Dmitry