[PATCH] Input: gpio_decoder - fix bit order reversal introduced by bitmap conversion

Esben Haabendal posted 1 patch 1 month, 1 week ago
drivers/input/misc/gpio_decoder.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] Input: gpio_decoder - fix bit order reversal introduced by bitmap conversion
Posted by Esben Haabendal 1 month, 1 week ago
The switch from a manual loop to gpiod_get_array_value_cansleep() +
bitmap_read() inadvertently reversed the bit order of the decoded value.
The original loop treated GPIO[0] as MSB by shifting left on each
iteration, but bitmap_read() returns GPIO[0] at bit 0 (LSB).

The previous bit order was correct according to device-tree bindings, and
the change broke existing users.

Fixes: 4eec8772e4f5 ("Input: gpio_decoder - replace custom loop by gpiod_get_array_value_cansleep()")
Cc: stable@vger.kernel.org
Signed-off-by: Esben Haabendal <esbenhaabendal@gmail.com>
---
The refactoring from a custom loop to gpiod_get_array_value_cansleep() +
bitmap_read() caused the bit order to be changed from reading MSB first to
LSB first.

The device-tree bindings explicitly states that that it should be MSB
first, and the change obviously is a regression for existing users.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 drivers/input/misc/gpio_decoder.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/input/misc/gpio_decoder.c b/drivers/input/misc/gpio_decoder.c
index 0e4a49845afa..055bfcbd5235 100644
--- a/drivers/input/misc/gpio_decoder.c
+++ b/drivers/input/misc/gpio_decoder.c
@@ -7,6 +7,7 @@
  */
 
 #include <linux/bitmap.h>
+#include <linux/bitrev.h>
 #include <linux/dev_printk.h>
 #include <linux/device/devres.h>
 #include <linux/err.h>
@@ -39,7 +40,7 @@ static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
 		return err;
 	}
 
-	return bitmap_read(values, 0, size);
+	return bitrev32(bitmap_read(values, 0, size)) >> (32 - size);
 }
 
 static void gpio_decoder_poll_gpios(struct input_dev *input)

---
base-commit: 8d3ae59288f1e7d58d76558a6ee96d533bc5019f
change-id: 20260817-gpio-decoder-bitrev-2457d68fd62b

Best regards,
--  
Esben Haabendal <esben@geanix.com>
Re: [PATCH] Input: gpio_decoder - fix bit order reversal introduced by bitmap conversion
Posted by Andy Shevchenko 1 month, 1 week ago
On Mon, Aug 17, 2026 at 01:50:27PM +0200, Esben Haabendal wrote:
> The switch from a manual loop to gpiod_get_array_value_cansleep() +
> bitmap_read() inadvertently reversed the bit order of the decoded value.
> The original loop treated GPIO[0] as MSB by shifting left on each
> iteration, but bitmap_read() returns GPIO[0] at bit 0 (LSB).
> 
> The previous bit order was correct according to device-tree bindings, and
> the change broke existing users.

Oops, thanks for catching this!
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko