From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Subject: [PATCH v2 3/5] Input: gpio_decoder - replace custom loop by gpiod_get_array_value_cansleep()
Date: Thu, 13 Nov 2025 16:44:44 +0100 [thread overview]
Message-ID: <20251113154616.3107676-4-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20251113154616.3107676-1-andriy.shevchenko@linux.intel.com>
There is a custom loop that repeats parts of gpiod_get_array_value_cansleep().
Use that in conjunction with bitmap API to make code shorter and easier to
follow.
With this done, add an upper check for amount of GPIOs given based on
the driver's code.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/input/misc/gpio_decoder.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/drivers/input/misc/gpio_decoder.c b/drivers/input/misc/gpio_decoder.c
index a2ae400790f9..7f319b932550 100644
--- a/drivers/input/misc/gpio_decoder.c
+++ b/drivers/input/misc/gpio_decoder.c
@@ -6,11 +6,13 @@
* encoded numeric value into an input event.
*/
+#include <linux/bitmap.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/input.h>
#include <linux/kernel.h>
+#include <linux/minmax.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
@@ -26,23 +28,18 @@ struct gpio_decoder {
static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
{
struct gpio_descs *gpios = decoder->input_gpios;
- unsigned int ret = 0;
- int i, val;
+ DECLARE_BITMAP(values, 32);
+ unsigned int size;
+ int err;
- for (i = 0; i < gpios->ndescs; i++) {
- val = gpiod_get_value_cansleep(gpios->desc[i]);
- if (val < 0) {
- dev_err(decoder->dev,
- "Error reading gpio %d: %d\n",
- desc_to_gpio(gpios->desc[i]), val);
- return val;
- }
-
- val = !!val;
- ret = (ret << 1) | val;
+ size = min(gpios->ndescs, 32U);
+ err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values);
+ if (err) {
+ dev_err(decoder->dev, "Error reading GPIO: %d\n", err);
+ return err;
}
- return ret;
+ return bitmap_read(values, 0, size);
}
static void gpio_decoder_poll_gpios(struct input_dev *input)
@@ -81,6 +78,9 @@ static int gpio_decoder_probe(struct platform_device *pdev)
if (decoder->input_gpios->ndescs < 2)
return dev_err_probe(dev, -EINVAL, "not enough gpios found\n");
+ if (decoder->input_gpios->ndescs > 31)
+ return dev_err_probe(dev, -EINVAL, "too many gpios found\n");
+
if (device_property_read_u32(dev, "decoder-max-value", &max))
max = (1U << decoder->input_gpios->ndescs) - 1;
--
2.50.1
next prev parent reply other threads:[~2025-11-13 15:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-13 15:44 [PATCH v2 0/5] Input: gpio_decoder - update driver to use modern APIs Andy Shevchenko
2025-11-13 15:44 ` [PATCH v2 1/5] Input: gpio_decoder - make use of device properties Andy Shevchenko
2025-11-13 15:44 ` [PATCH v2 2/5] Input: gpio_decoder - unify messages with help of dev_err_probe() Andy Shevchenko
2025-11-13 15:44 ` Andy Shevchenko [this message]
2025-11-13 15:44 ` [PATCH v2 4/5] Input: gpio_decoder - make use of the macros from bits.h Andy Shevchenko
2025-11-13 15:44 ` [PATCH v2 5/5] Input: gpio_decoder - don't use "proxy" headers Andy Shevchenko
2026-01-13 8:56 ` [PATCH v2 0/5] Input: gpio_decoder - update driver to use modern APIs Andy Shevchenko
2026-01-21 21:05 ` Dmitry Torokhov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251113154616.3107676-4-andriy.shevchenko@linux.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox