From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.133 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796453048; b=fMQnow9tJfDqHm/oTsHfuqcCMIiOGG+XtvY3AxXXsxatU62iXBhg/du9rgou/rff/qtj tXY7/L41S6XY+8AtUvrjnDu0N+qNtmf5GrOo4YIvpHk3PceS6VYi5pgKc3CitUnwNTBCH uWyILixdtoc72tNUdJm0mgERqysMAj1Wfn1CP2lSMjizDbVw1ihFC7Sgs5dvyISGrBa00 3lAJU61zav+0TK2QgYJHaZ0w6LyfuE82htpJvQBvN4rz+K17vOAR/+jQGARzFV7jtGKGJ 1mr1hZUax4j0tn4+C+0RTwbcU95w08EawH5tRzkdt+hcVgOjnjYfjETai5cOu6Wv9yQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796453048; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=TAd5CYhLYn0c3rmNunFEvzAo2JUWRlM7lSIAvn4FqqM=; b=lRwN1UilQinKtLHevOsdUWEgCEhK63nqJQ/vx+osfqI8wSQ+EJAnarYwo4FEleJN5blz PIHxifvNqOM+p/dwonSuIWBHvDt8AXBVrBrBNaw0jnwxP0pcYLL6+o9DNOeV8/uNmgHm9 PWuLcStX+EKkww26Z5Y1KdcNc9kgyLjKDLt5vBHvEmgfapav4nXsxbCmIMlmoWpQ1pUAV R7ZgbvWjTAMxFb4gXlZA8OgeITlRQQkfxXnbkPVovrPcgNynKmM/l12PfINS3ab7FaZ4p FjtVAwqfUDq2rxJppfMhlfAQG+fpA23RWl27AmVYC+z3CokIu+8vKz5tIWQVHIp63UA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=xZFbZKvK; arc=none smtp.remote-ip=74.125.227.133 Received: from mail-pj2-f5.google.com (mail-pj2-f5.google.com [74.125.227.133]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA6B6606A338 for ; Sat, 5 Dec 2026 06:44:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DA6B6606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=xZFbZKvK Received: by mail-pj2-f5.google.com with SMTP id d9443c01a7336-2f03335ec43so5549095ad.0 for ; Fri, 04 Dec 2026 22:44:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796453047; x=1797057847; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=TAd5CYhLYn0c3rmNunFEvzAo2JUWRlM7lSIAvn4FqqM=; b=xZFbZKvKs4Z0ljoiDHFyyeKAp1Gwecmsv/mwE5xrlJI0obrow5HZ9j8Gh7oi7xKBgs jbgnEjmS02umgy/4KjcfWyCJGdcixbk8xnBTLThV/p819eZaQNhY+pf6SrQhn1hegW0T 8VjnJx1Ww7zvuEZxusA12aFQLGMndugfnMA1n69AZn81U9boH59/ZvtxNlMWDInvmRWH rqngiME7dkgK09qALrva33Ca4ZpbTjJ6YDTImaiY6ZpYEFSBnEe/vY/J3joHTR45zeLv 51+vq7fTiO7LFebrlkr9cjJOKe+JlWk8OMCztIcnIwCvgiz5Fur6M9X/kNZvkUoVB4H/ QW8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796453047; x=1797057847; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=TAd5CYhLYn0c3rmNunFEvzAo2JUWRlM7lSIAvn4FqqM=; b=rlUGmEHxfOtfd5YC3N0HUHSt97mR3K2Jq21NuHanK3NK43luHAGSTGU9+Iz1RuNmbg aa/lfJPHv05LIAcXkIP1W1x1QjKmBGX9usTgy3nRtUEJ607TOgRhGG1plvXqE8t97paA Txb9DpsBgT3egKRc6Rnfl7kEZ3Pk09+315BXpZnpYjvnN3uSim3YY+Lc0QGPrv5+u6kb oyycpsumXWU/913X29FXzjtbQuUQWaYXaitgdvmY1I/BBnkeK98aYx+REqNUFhs8/iOk yGMB+B5Ks0Aeew4vNMf8ONHzDlezLmobQE9QxU2xPTiR90mBtuGuL+80eVVQPU9gT1/9 QrdA== X-Forwarded-Encrypted: i=1; AL+hErE749UIB30XabkZephohR25+jkv/n+mOfDA/6YMrjIYHEmLHDjNksDgu4ANj9OUDvVbhlIoH5aeOHcE@vger.kernel.org X-Gm-Message-State: AB4q+uHrI33EWun3LTZytiKjQ8EcLwD6lhInjKot/ZI1j984J7NwV5Cl 1mBkL4mIBaLVOgx8vusqVOmNrEMP2ZrP39zz79a9+t/ouX4I4pm+TeHX X-Gm-Gg: AZFdTZfr2XNvh6teiREchanDGD9hhbhUkW5JPiNY6PUQJ/u4icH00qXvTvnel9v9HqW wGpjUv+SFMdWU3NNyuS07oYF+lek7QDMraSsTUAT8KcH2OdSD0Xbeae4vetWBhAnOzmaSVq+OzW YTWFHT7H54FP7hpmfLJc63VVOIxGCFEUiTDKWn5nkNIsv7R26F33K4N7mzsGCr7h5+AmmLxwAsf 4GjZWHOnVfNYLjpibSDpkP4wbFeK6TpV8vWjvQhobcbAxExwXRDdi+P9osazkGMa8CHOeGGFuFu xwAelvu2sOtZzSxhxHPUAWcLbBdGb78TQhFFwp4b3yMR1Yr3y9U/Gx6Gu7dD27sabhIVIBBSaQb Umx4Z/puHgSKCPEkj08J2Q4I3/kDWGmeMsNKXGG6qWrG0krmrvIkvJlrBlulqqQs73DETXt9LCa 1FNYQi4dJM9/901EIrG1FHXe3TohVc2O1rNU3iDT6d29hfptRoAbZuLUHsa6oBGMmRybYd1twxG Pij95B3N7W6pIzGik/iCyANob4iac9LKMCGU1SYqR9jTQxK08XK+jPaVp40tDsEduUUn52TbWjh COwDdQkq1qfHWxYEoRHOpQqiVV6tXg== X-Received: by 2002:a17:902:cec3:b0:2df:8382:918b with SMTP id d9443c01a7336-2f1b9bedb1dmr43712695ad.0.1796453046997; Fri, 04 Dec 2026 22:44:06 -0800 (PST) Received: from AERO17.orionisli.com ([2001:49f0:d142:0:1fa4::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b78f9d99sm23235125ad.16.2026.12.04.22.44.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:44:06 -0800 (PST) From: Pufan Jin To: Bartosz Golaszewski , Bartosz Golaszewski , Linus Walleij , Philipp Zabel Cc: Krzysztof Kozlowski , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/5] gpiolib: add gpio_device_find_by_fwnode_args() Date: Sat, 5 Dec 2026 14:43:51 +0800 Message-ID: <20261205064355.137581-2-cyan2254650260@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261205064355.137581-1-cyan2254650260@gmail.com> References: <20261205064355.137581-1-cyan2254650260@gmail.com> X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Callers such as drivers/reset/core.c receive parsed struct fwnode_reference_args referencing a GPIO controller and need the matching struct gpio_device, chip-relative hardware offset, and Linux lookup flags without calling gpio_device_get_chip() or chip->of_xlate() outside gpiolib. Add gpio_device_find_by_fwnode_args() for OF and software nodes: - For OF nodes, match and translate inside gpio_device_find() under gdev->srcu. Return -EPROBE_DEFER if no controller matches the node, or -EINVAL or the negative errno from of_xlate() if the specifier is rejected. Convert OF flags via of_convert_gpio_flags(). - For software nodes, resolve the device via swnode_get_gpio_device() and validate the 2-cell offset and flags reference against gdev->ngpio. Signed-off-by: Pufan Jin --- drivers/gpio/gpiolib-of.c | 93 +++++++++++++++++++++++++++++++++-- drivers/gpio/gpiolib-of.h | 10 ++++ drivers/gpio/gpiolib-swnode.c | 24 +++++++++ drivers/gpio/gpiolib-swnode.h | 6 +++ drivers/gpio/gpiolib.c | 32 ++++++++++++ include/linux/gpio/driver.h | 12 +++++ 6 files changed, 172 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpiolib-of.c b/drivers/gpio/gpiolib-of.c index ea1dfc3a31..0fa973658a 100644 --- a/drivers/gpio/gpiolib-of.c +++ b/drivers/gpio/gpiolib-of.c @@ -721,6 +721,25 @@ struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, return desc; } +static int +of_fwnode_args_to_of_phandle_args(const struct fwnode_reference_args *src, + struct of_phandle_args *dst) +{ + if (src->nargs > MAX_PHANDLE_ARGS) + return -EINVAL; + + dst->np = to_of_node(src->fwnode); + dst->args_count = src->nargs; + + for (int i = 0; i < dst->args_count; i++) { + if (src->args[i] > U32_MAX) + return -EINVAL; + dst->args[i] = src->args[i]; + } + + return 0; +} + struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip, struct fwnode_reference_args *gpiospec, unsigned long *lflags) @@ -728,12 +747,11 @@ struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip, enum of_gpio_flags xlate_flags = 0; struct of_phandle_args args; struct gpio_desc *desc; + int ret; - args.np = to_of_node(gpiospec->fwnode); - args.args_count = gpiospec->nargs; - - for (int i = 0; i < args.args_count; i++) - args.args[i] = gpiospec->args[i]; + ret = of_fwnode_args_to_of_phandle_args(gpiospec, &args); + if (ret) + return ERR_PTR(ret); desc = of_xlate_and_get_gpiod_flags(chip, &args, &xlate_flags); if (IS_ERR(desc)) @@ -744,6 +762,71 @@ struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip, return desc; } +struct of_gpio_xlate_ctx { + const struct of_phandle_args *gpiospec; + unsigned int offset; + enum of_gpio_flags of_flags; + int err; +}; + +static int of_gpiochip_match_and_xlate(struct gpio_chip *gc, const void *data) +{ + struct of_gpio_xlate_ctx *ctx = (void *)data; + enum of_gpio_flags of_flags = 0; + int ret; + + if (!device_match_of_node(&gc->gpiodev->dev, ctx->gpiospec->np)) + return 0; + + if (!gc->of_xlate || gc->of_gpio_n_cells != ctx->gpiospec->args_count) { + ctx->err = -EINVAL; + return 0; + } + + ret = gc->of_xlate(gc, ctx->gpiospec, &of_flags); + if (ret < 0) { + ctx->err = ret; + return 0; + } + + if (ret >= gc->ngpio) { + ctx->err = -EINVAL; + return 0; + } + + ctx->offset = ret; + ctx->of_flags = of_flags; + return 1; +} + +struct gpio_device * +of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags) +{ + struct of_phandle_args gpiospec; + struct of_gpio_xlate_ctx ctx = { + .gpiospec = &gpiospec, + .err = -EPROBE_DEFER, + }; + struct gpio_device *gdev; + int ret; + + ret = of_fwnode_args_to_of_phandle_args(args, &gpiospec); + if (ret) + return ERR_PTR(ret); + + gdev = gpio_device_find(&ctx, of_gpiochip_match_and_xlate); + if (!gdev) + return ERR_PTR(ctx.err); + + if (offset) + *offset = ctx.offset; + if (lflags) + *lflags = of_convert_gpio_flags(ctx.of_flags); + + return gdev; +} + #ifdef CONFIG_OF_DYNAMIC /** * of_gpiochip_remove_hog - Remove all hogs in a hog device node diff --git a/drivers/gpio/gpiolib-of.h b/drivers/gpio/gpiolib-of.h index 347b82f9e3..e9bc405b6d 100644 --- a/drivers/gpio/gpiolib-of.h +++ b/drivers/gpio/gpiolib-of.h @@ -28,6 +28,9 @@ int of_gpio_count(const struct fwnode_handle *fwnode, const char *con_id); struct gpio_desc *of_gpiochip_get_hog_desc(struct gpio_chip *chip, struct fwnode_reference_args *gpiospec, unsigned long *lflags); +struct gpio_device * +of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags); #else static inline struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, @@ -55,6 +58,13 @@ of_gpiochip_get_hog_desc(struct gpio_chip *chip, { return ERR_PTR(-ENOENT); } + +static inline struct gpio_device * +of_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags) +{ + return ERR_PTR(-ENOENT); +} #endif /* CONFIG_OF_GPIO */ extern struct notifier_block gpio_of_notifier; diff --git a/drivers/gpio/gpiolib-swnode.c b/drivers/gpio/gpiolib-swnode.c index 304994c5c7..c9b5b74fdd 100644 --- a/drivers/gpio/gpiolib-swnode.c +++ b/drivers/gpio/gpiolib-swnode.c @@ -135,6 +135,30 @@ int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id) return -ENOENT; } +struct gpio_device * +swnode_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, + unsigned long *lflags) +{ + if (args->nargs != 2) + return ERR_PTR(-EINVAL); + + struct gpio_device *gdev __free(gpio_device_put) = + swnode_get_gpio_device(args->fwnode); + if (IS_ERR(gdev)) + return ERR_CAST(gdev); + + if (args->args[0] >= gdev->ngpio) + return ERR_PTR(-EINVAL); + + if (offset) + *offset = args->args[0]; + if (lflags) + *lflags = args->args[1]; + + return no_free_ptr(gdev); +} + #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) /* * A special node that identifies undefined GPIOs, this is primarily used as diff --git a/drivers/gpio/gpiolib-swnode.h b/drivers/gpio/gpiolib-swnode.h index af849e56f6..0e0c5f6217 100644 --- a/drivers/gpio/gpiolib-swnode.h +++ b/drivers/gpio/gpiolib-swnode.h @@ -4,11 +4,17 @@ #define GPIOLIB_SWNODE_H struct fwnode_handle; +struct fwnode_reference_args; struct gpio_desc; +struct gpio_device; struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode, const char *con_id, unsigned int idx, unsigned long *flags); int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id); +struct gpio_device * +swnode_gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, + unsigned long *lflags); #endif /* GPIOLIB_SWNODE_H */ diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 46a68a6b73..a1da937fab 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1587,6 +1587,38 @@ struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnod } EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode); +/** + * gpio_device_find_by_fwnode_args() - find a GPIO device and translate a + * firmware node reference + * @args: Firmware reference arguments describing the GPIO specifier + * @offset: Optional pointer to store the chip-relative hardware GPIO offset + * @lflags: Optional pointer to store the Linux GPIO lookup flags + * + * Context: Process context. May sleep. + * + * Returns: + * Reference to the matching struct gpio_device on success, which must be + * released with gpio_device_put(), or ERR_PTR() on error: %-EPROBE_DEFER if + * no controller matching @args->fwnode is registered yet, %-EINVAL if @args + * or the specifier is invalid, or %-ENOENT if @args->fwnode is unsupported. + */ +struct gpio_device * +gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags) +{ + if (!args || !args->fwnode) + return ERR_PTR(-EINVAL); + + if (is_of_node(args->fwnode)) + return of_gpio_device_find_by_fwnode_args(args, offset, lflags); + + if (is_software_node(args->fwnode)) + return swnode_gpio_device_find_by_fwnode_args(args, offset, lflags); + + return ERR_PTR(-ENOENT); +} +EXPORT_SYMBOL_GPL(gpio_device_find_by_fwnode_args); + /** * gpio_device_get() - Increase the reference count of this GPIO device * @gdev: GPIO device to increase the refcount for diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h index 4077dc678c..057c2c3016 100644 --- a/include/linux/gpio/driver.h +++ b/include/linux/gpio/driver.h @@ -23,6 +23,7 @@ #include "defs.h" struct device; +struct fwnode_reference_args; struct irq_chip; struct irq_data; struct module; @@ -836,6 +837,9 @@ const char *gpio_device_get_label(struct gpio_device *gdev); struct gpio_device *gpio_device_find_by_label(const char *label); struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode_handle *fwnode); +struct gpio_device * +gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags); #else /* CONFIG_GPIOLIB */ @@ -878,6 +882,14 @@ static inline struct gpio_device *gpio_device_find_by_fwnode(const struct fwnode return NULL; } +static inline struct gpio_device * +gpio_device_find_by_fwnode_args(const struct fwnode_reference_args *args, + unsigned int *offset, unsigned long *lflags) +{ + WARN_ON(1); + return ERR_PTR(-ENODEV); +} + static inline int gpiochip_lock_as_irq(struct gpio_chip *gc, unsigned int offset) { -- 2.56.1