All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection
@ 2026-04-05 21:37 Guilherme Ivo Bozi
  2026-04-06 19:50 ` Andy Shevchenko
  2026-04-12 13:52 ` Marcelo Schmitt
  0 siblings, 2 replies; 4+ messages in thread
From: Guilherme Ivo Bozi @ 2026-04-05 21:37 UTC (permalink / raw)
  To: Marcelo Schmitt, Jonathan Cameron
  Cc: Lars-Peter Clausen, Michael Hennerich, David Lechner,
	Nuno Sá, Andy Shevchenko, linux-iio, linux-kernel,
	guilherme.bozi

Both ad4170_gpio_direction_input() and
ad4170_gpio_direction_output() duplicate the same switch
statement to map a GPIO offset to its corresponding mask.

Replace the switch with a static lookup table, simplifying the code
and avoiding duplication. This also makes future extensions easier.

No functional change intended.

Signed-off-by: Guilherme Ivo Bozi <guilherme.bozi@usp.br>
---
 drivers/iio/adc/ad4170-4.c | 47 ++++++++++++--------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)

diff --git a/drivers/iio/adc/ad4170-4.c b/drivers/iio/adc/ad4170-4.c
index 82205bfae531..d1e971649b6f 100644
--- a/drivers/iio/adc/ad4170-4.c
+++ b/drivers/iio/adc/ad4170-4.c
@@ -1699,34 +1699,29 @@ static int ad4170_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
 	return ret;
 }
 
+static const unsigned long gpio_masks[] = {
+	AD4170_GPIO_MODE_GPIO0_MSK,
+	AD4170_GPIO_MODE_GPIO1_MSK,
+	AD4170_GPIO_MODE_GPIO2_MSK,
+	AD4170_GPIO_MODE_GPIO3_MSK,
+};
+
 static int ad4170_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
 {
 	struct iio_dev *indio_dev = gpiochip_get_data(gc);
 	struct ad4170_state *st = iio_priv(indio_dev);
-	unsigned long gpio_mask;
 	int ret;
 
 	if (!iio_device_claim_direct(indio_dev))
 		return -EBUSY;
 
-	switch (offset) {
-	case 0:
-		gpio_mask = AD4170_GPIO_MODE_GPIO0_MSK;
-		break;
-	case 1:
-		gpio_mask = AD4170_GPIO_MODE_GPIO1_MSK;
-		break;
-	case 2:
-		gpio_mask = AD4170_GPIO_MODE_GPIO2_MSK;
-		break;
-	case 3:
-		gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK;
-		break;
-	default:
+	if (offset >= ARRAY_SIZE(gpio_masks)) {
 		ret = -EINVAL;
 		goto err_release;
 	}
-	ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
+
+	ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG,
+				 gpio_masks[offset],
 				 AD4170_GPIO_MODE_GPIO_INPUT << (2 * offset));
 
 err_release:
@@ -1740,7 +1735,6 @@ static int ad4170_gpio_direction_output(struct gpio_chip *gc,
 {
 	struct iio_dev *indio_dev = gpiochip_get_data(gc);
 	struct ad4170_state *st = iio_priv(indio_dev);
-	unsigned long gpio_mask;
 	int ret;
 
 	ret = ad4170_gpio_set(gc, offset, value);
@@ -1750,24 +1744,13 @@ static int ad4170_gpio_direction_output(struct gpio_chip *gc,
 	if (!iio_device_claim_direct(indio_dev))
 		return -EBUSY;
 
-	switch (offset) {
-	case 0:
-		gpio_mask = AD4170_GPIO_MODE_GPIO0_MSK;
-		break;
-	case 1:
-		gpio_mask = AD4170_GPIO_MODE_GPIO1_MSK;
-		break;
-	case 2:
-		gpio_mask = AD4170_GPIO_MODE_GPIO2_MSK;
-		break;
-	case 3:
-		gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK;
-		break;
-	default:
+	if (offset >= ARRAY_SIZE(gpio_masks)) {
 		ret = -EINVAL;
 		goto err_release;
 	}
-	ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
+
+	ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG,
+				 gpio_masks[offset],
 				 AD4170_GPIO_MODE_GPIO_OUTPUT << (2 * offset));
 
 err_release:
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection
  2026-04-05 21:37 [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection Guilherme Ivo Bozi
@ 2026-04-06 19:50 ` Andy Shevchenko
  2026-04-20 18:01   ` Jonathan Cameron
  2026-04-12 13:52 ` Marcelo Schmitt
  1 sibling, 1 reply; 4+ messages in thread
From: Andy Shevchenko @ 2026-04-06 19:50 UTC (permalink / raw)
  To: Guilherme Ivo Bozi
  Cc: Marcelo Schmitt, Jonathan Cameron, Lars-Peter Clausen,
	Michael Hennerich, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On Sun, Apr 05, 2026 at 06:37:26PM -0300, Guilherme Ivo Bozi wrote:
> Both ad4170_gpio_direction_input() and
> ad4170_gpio_direction_output() duplicate the same switch
> statement to map a GPIO offset to its corresponding mask.
> 
> Replace the switch with a static lookup table, simplifying the code
> and avoiding duplication. This also makes future extensions easier.
> 
> No functional change intended.

If this churn is okay by maintainers,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection
  2026-04-05 21:37 [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection Guilherme Ivo Bozi
  2026-04-06 19:50 ` Andy Shevchenko
@ 2026-04-12 13:52 ` Marcelo Schmitt
  1 sibling, 0 replies; 4+ messages in thread
From: Marcelo Schmitt @ 2026-04-12 13:52 UTC (permalink / raw)
  To: Guilherme Ivo Bozi
  Cc: Marcelo Schmitt, Jonathan Cameron, Lars-Peter Clausen,
	Michael Hennerich, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On 04/05, Guilherme Ivo Bozi wrote:
> Both ad4170_gpio_direction_input() and
> ad4170_gpio_direction_output() duplicate the same switch
> statement to map a GPIO offset to its corresponding mask.
> 
> Replace the switch with a static lookup table, simplifying the code
> and avoiding duplication. This also makes future extensions easier.
> 
> No functional change intended.
> 
> Signed-off-by: Guilherme Ivo Bozi <guilherme.bozi@usp.br>

Acked-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection
  2026-04-06 19:50 ` Andy Shevchenko
@ 2026-04-20 18:01   ` Jonathan Cameron
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2026-04-20 18:01 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Guilherme Ivo Bozi, Marcelo Schmitt, Lars-Peter Clausen,
	Michael Hennerich, David Lechner, Nuno Sá, Andy Shevchenko,
	linux-iio, linux-kernel

On Mon, 6 Apr 2026 22:50:53 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Sun, Apr 05, 2026 at 06:37:26PM -0300, Guilherme Ivo Bozi wrote:
> > Both ad4170_gpio_direction_input() and
> > ad4170_gpio_direction_output() duplicate the same switch
> > statement to map a GPIO offset to its corresponding mask.
> > 
> > Replace the switch with a static lookup table, simplifying the code
> > and avoiding duplication. This also makes future extensions easier.
> > 
> > No functional change intended.  
> 
> If this churn is okay by maintainers,
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> 
Applied.

Thanks,

J


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-04-20 18:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-05 21:37 [PATCH] iio: adc: ad4170: use lookup table for gpio mask selection Guilherme Ivo Bozi
2026-04-06 19:50 ` Andy Shevchenko
2026-04-20 18:01   ` Jonathan Cameron
2026-04-12 13:52 ` Marcelo Schmitt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.