mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Richard <thomas.richard@bootlin.com>
To: Linus Walleij <linus.walleij@linaro.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Bartosz Golaszewski <brgl@bgdev.pl>,
	 Geert Uytterhoeven <geert+renesas@glider.be>,
	Kees Cook <kees@kernel.org>,  Andy Shevchenko <andy@kernel.org>
Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org,
	 thomas.petazzoni@bootlin.com, DanieleCleri@aaeon.eu,
	GaryWang@aaeon.com.tw,  linux-hardening@vger.kernel.org,
	 Thomas Richard <thomas.richard@bootlin.com>
Subject: [PATCH v4 09/12] gpio: aggregator: handle runtime registration of gpio_desc in gpiochip_fwd
Date: Tue, 29 Apr 2025 16:08:23 +0200	[thread overview]
Message-ID: <20250429-aaeon-up-board-pinctrl-support-v4-9-b3fffc11417d@bootlin.com> (raw)
In-Reply-To: <20250429-aaeon-up-board-pinctrl-support-v4-0-b3fffc11417d@bootlin.com>

Add request() callback to check if the GPIO descriptor was well registered
in the gpiochip_fwd before to use it. This is done to handle the case
where GPIO descriptor is added at runtime in the forwarder.

If at least one GPIO descriptor was not added before the forwarder
registration, we assume the forwarder can sleep as if a GPIO is added at
runtime it may sleep.

Signed-off-by: Thomas Richard <thomas.richard@bootlin.com>
---
 drivers/gpio/gpio-aggregator.c | 55 ++++++++++++++++++++++++++++++++++++------
 include/linux/gpio/forwarder.h |  4 +++
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c
index accf4be68238..230bd8398119 100644
--- a/drivers/gpio/gpio-aggregator.c
+++ b/drivers/gpio/gpio-aggregator.c
@@ -268,6 +268,7 @@ struct gpiochip_fwd {
 		spinlock_t slock;	/* protects tmp[] if !can_sleep */
 	};
 	struct gpiochip_fwd_timing *delay_timings;
+	unsigned long *valid_mask;
 	unsigned long tmp[];		/* values and descs for multiple ops */
 };
 
@@ -288,6 +289,21 @@ struct gpio_chip *gpio_fwd_get_gpiochip(struct gpiochip_fwd *fwd)
 }
 EXPORT_SYMBOL_NS_GPL(gpio_fwd_get_gpiochip, "GPIO_FORWARDER");
 
+/**
+ * gpio_fwd_request - Request a line of the GPIO forwarder
+ * @chip: GPIO chip in the forwarder
+ * @offset: the offset of the line to request
+ *
+ * Returns: 0 on success, or negative errno on failure.
+ */
+int gpio_fwd_request(struct gpio_chip *chip, unsigned int offset)
+{
+	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
+
+	return test_bit(offset, fwd->valid_mask) ? 0 : -ENODEV;
+}
+EXPORT_SYMBOL_NS_GPL(gpio_fwd_request, "GPIO_FORWARDER");
+
 /**
  * gpio_fwd_get_direction - Return the current direction of a GPIO forwarder line
  * @chip: GPIO chip in the forwarder
@@ -299,6 +315,13 @@ int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
 {
 	struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
 
+	/*
+	 * get_direction() is called during gpiochip registration, return input
+	 * direction if there is no descriptor for the line
+	 */
+	if (!test_bit(offset, fwd->valid_mask))
+		return GPIO_LINE_DIRECTION_IN;
+
 	return gpiod_get_direction(fwd->descs[offset]);
 }
 EXPORT_SYMBOL_NS_GPL(gpio_fwd_get_direction, "GPIO_FORWARDER");
@@ -618,11 +641,16 @@ devm_gpio_fwd_alloc(struct device *dev, unsigned int ngpios)
 	if (!fwd->descs)
 		return ERR_PTR(-ENOMEM);
 
+	fwd->valid_mask = devm_bitmap_zalloc(dev, ngpios, GFP_KERNEL);
+	if (!fwd->valid_mask)
+		return ERR_PTR(-ENOMEM);
+
 	chip = &fwd->chip;
 
 	chip->label = label;
 	chip->parent = dev;
 	chip->owner = THIS_MODULE;
+	chip->request = gpio_fwd_request;
 	chip->get_direction = gpio_fwd_get_direction;
 	chip->direction_input = gpio_fwd_direction_input;
 	chip->direction_output = gpio_fwd_direction_output;
@@ -649,27 +677,21 @@ EXPORT_SYMBOL_NS_GPL(devm_gpio_fwd_alloc, "GPIO_FORWARDER");
 int gpio_fwd_gpio_add(struct gpiochip_fwd *fwd, struct gpio_desc *desc,
 		      unsigned int offset)
 {
-	struct gpio_chip *parent = gpiod_to_chip(desc);
 	struct gpio_chip *chip = &fwd->chip;
 
 	if (offset > chip->ngpio)
 		return -EINVAL;
 
-	if (fwd->descs[offset])
+	if (test_and_set_bit(offset, fwd->valid_mask))
 		return -EEXIST;
 
 	/*
 	 * If any of the GPIO lines are sleeping, then the entire forwarder
 	 * will be sleeping.
-	 * If any of the chips support .set_config(), then the forwarder will
-	 * support setting configs.
 	 */
 	if (gpiod_cansleep(desc))
 		chip->can_sleep = true;
 
-	if (parent && parent->set_config)
-		chip->set_config = gpio_fwd_set_config;
-
 	fwd->descs[offset] = desc;
 
 	dev_dbg(chip->parent, "%u => gpio %d irq %d\n", offset,
@@ -679,6 +701,18 @@ int gpio_fwd_gpio_add(struct gpiochip_fwd *fwd, struct gpio_desc *desc,
 }
 EXPORT_SYMBOL_NS_GPL(gpio_fwd_gpio_add, "GPIO_FORWARDER");
 
+/**
+ * gpio_fwd_gpio_free - Remove a GPIO from the forwarder
+ * @fwd: GPIO forwarder
+ * @offset: offset of GPIO to remove
+ */
+void gpio_fwd_gpio_free(struct gpiochip_fwd *fwd, unsigned int offset)
+{
+	if (test_and_clear_bit(offset, fwd->valid_mask))
+		gpiod_put(fwd->descs[offset]);
+}
+EXPORT_SYMBOL_NS_GPL(gpio_fwd_gpio_free, "GPIO_FORWARDER");
+
 /**
  * gpio_fwd_register - Register a GPIO forwarder
  * @fwd: GPIO forwarder
@@ -689,6 +723,13 @@ int gpio_fwd_register(struct gpiochip_fwd *fwd)
 {
 	struct gpio_chip *chip = &fwd->chip;
 
+	/*
+	 * Some gpio_desc were not registered. They will be registered at runtime
+	 * but we have to suppose they can sleep.
+	 */
+	if (bitmap_full(fwd->valid_mask, chip->ngpio))
+		chip->can_sleep = true;
+
 	if (chip->can_sleep)
 		mutex_init(&fwd->mlock);
 	else
diff --git a/include/linux/gpio/forwarder.h b/include/linux/gpio/forwarder.h
index 73a1a15e09a5..f0deb7573f36 100644
--- a/include/linux/gpio/forwarder.h
+++ b/include/linux/gpio/forwarder.h
@@ -12,6 +12,8 @@ struct gpiochip_fwd;
 
 struct gpio_chip *gpio_fwd_get_gpiochip(struct gpiochip_fwd *fwd);
 
+int gpio_fwd_request(struct gpio_chip *chip, unsigned int offset);
+
 int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset);
 
 int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset);
@@ -40,6 +42,8 @@ struct gpiochip_fwd *devm_gpio_fwd_alloc(struct device *dev,
 int gpio_fwd_gpio_add(struct gpiochip_fwd *fwd,
 		      struct gpio_desc *desc, unsigned int offset);
 
+void gpio_fwd_gpio_free(struct gpiochip_fwd *fwd, unsigned int offset);
+
 int gpio_fwd_register(struct gpiochip_fwd *fwd);
 
 #endif

-- 
2.39.5


  parent reply	other threads:[~2025-04-29 14:08 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-29 14:08 [PATCH v4 00/12] Add pinctrl support for the AAEON UP board FPGA Thomas Richard
2025-04-29 14:08 ` [PATCH v4 01/12] gpiolib: add support to register sparse pin range Thomas Richard
2025-04-29 14:08 ` [PATCH v4 02/12] pinctrl: remove extern specifier for functions in machine.h Thomas Richard
2025-04-29 19:49   ` Andy Shevchenko
2025-04-30  9:33     ` Thomas Richard
2025-04-29 14:08 ` [PATCH v4 03/12] pinctrl: core: add devm_pinctrl_register_mappings() Thomas Richard
2025-04-29 19:53   ` Andy Shevchenko
2025-04-29 14:08 ` [PATCH v4 04/12] gpio: aggregator: move GPIO forwarder allocation in a dedicated function Thomas Richard
2025-04-29 14:08 ` [PATCH v4 05/12] gpio: aggregator: refactor the code to add GPIO desc in the forwarder Thomas Richard
2025-04-29 20:05   ` Andy Shevchenko
2025-04-29 14:08 ` [PATCH v4 06/12] gpio: aggregator: refactor the forwarder registration part Thomas Richard
2025-04-29 14:08 ` [PATCH v4 07/12] gpio: aggregator: update gpiochip_fwd_setup_delay_line() parameters Thomas Richard
2025-04-29 14:08 ` [PATCH v4 08/12] gpio: aggregator: export symbols of the GPIO forwarder library Thomas Richard
2025-04-29 20:16   ` Andy Shevchenko
2025-04-29 14:08 ` Thomas Richard [this message]
2025-04-29 20:23   ` [PATCH v4 09/12] gpio: aggregator: handle runtime registration of gpio_desc in gpiochip_fwd Andy Shevchenko
2025-04-29 14:08 ` [PATCH v4 10/12] gpio: aggregator: add possibility to attach data to the forwarder Thomas Richard
2025-04-29 20:27   ` Andy Shevchenko
2025-04-29 14:08 ` [PATCH v4 11/12] lib/string_choices: Add str_input_output() helper Thomas Richard
2025-04-29 20:26   ` Andy Shevchenko
2025-04-29 14:08 ` [PATCH v4 12/12] pinctrl: Add pin controller driver for AAEON UP boards Thomas Richard
2025-04-29 20:41   ` Andy Shevchenko
2025-04-29 18:43 ` [PATCH v4 00/12] Add pinctrl support for the AAEON UP board FPGA Bartosz Golaszewski
2025-04-29 20:29   ` Andy Shevchenko
2025-04-30  7:49     ` Bartosz Golaszewski

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=20250429-aaeon-up-board-pinctrl-support-v4-9-b3fffc11417d@bootlin.com \
    --to=thomas.richard@bootlin.com \
    --cc=DanieleCleri@aaeon.eu \
    --cc=GaryWang@aaeon.com.tw \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=andy@kernel.org \
    --cc=brgl@bgdev.pl \
    --cc=geert+renesas@glider.be \
    --cc=kees@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    /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

all inboxes | Powered by JetHome®