From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Kees Cook <kees@kernel.org>, Mika Westerberg <westeri@kernel.org>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Linus Walleij <linus.walleij@linaro.org>,
Manivannan Sadhasivam <mani@kernel.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Saravana Kannan <saravanak@google.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andy Shevchenko <andy@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Srinivas Kandagatla <srini@kernel.org>,
Liam Girdwood <lgirdwood@gmail.com>,
Mark Brown <broonie@kernel.org>, Jaroslav Kysela <perex@perex.cz>,
Takashi Iwai <tiwai@suse.com>,
Alexey Klimov <alexey.klimov@linaro.org>,
linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-gpio@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-sound@vger.kernel.org, linux-arm-msm@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH v3 03/10] gpiolib: implement low-level, shared GPIO support
Date: Wed, 29 Oct 2025 13:44:53 +0200 [thread overview]
Message-ID: <aQH-NcXry6_IlqXQ@smile.fi.intel.com> (raw)
In-Reply-To: <20251029-gpio-shared-v3-3-71c568acf47c@linaro.org>
On Wed, Oct 29, 2025 at 12:20:39PM +0100, Bartosz Golaszewski wrote:
>
> This module scans the device tree (for now only OF nodes are supported
> but care is taken to make other fwnode implementations easy to
> integrate) and determines which GPIO lines are shared by multiple users.
> It stores that information in memory. When the GPIO chip exposing shared
> lines is registered, the shared GPIO descriptors it exposes are marked
> as shared and virtual "proxy" devices that mediate access to the shared
> lines are created. When a consumer of a shared GPIO looks it up, its
> fwnode lookup is redirected to a just-in-time machine lookup that points
> to this proxy device.
>
> This code can be compiled out on platforms which don't use shared GPIOs.
Besides strcmp_suffix() that already exists in OF core, there are also some
existing pieces that seems being repeated here (again). Can we reduce amount
of duplication?
...
> +#if IS_ENABLED(CONFIG_OF)
> +static int gpio_shared_of_traverse(struct device_node *curr)
> +{
I believe parts of this code may be resided somewhere in drivers/of/property.c
or nearby as it has the similar parsing routines.
> + struct gpio_shared_entry *entry;
> + size_t con_id_len, suffix_len;
> + struct fwnode_handle *fwnode;
> + struct of_phandle_args args;
> + struct property *prop;
> + unsigned int offset;
> + const char *suffix;
> + int ret, count, i;
> +
> + for_each_property_of_node(curr, prop) {
> + /*
> + * The standard name for a GPIO property is "foo-gpios"
> + * or "foo-gpio". Some bindings also use "gpios" or "gpio".
> + * There are some legacy device-trees which have a different
> + * naming convention and for which we have rename quirks in
> + * place in gpiolib-of.c. I don't think any of them require
> + * support for shared GPIOs so for now let's just ignore
> + * them. We can always just export the quirk list and
> + * iterate over it here.
> + */
> + if (!strends(prop->name, "-gpios") &&
> + !strends(prop->name, "-gpio") &&
> + strcmp(prop->name, "gpios") != 0 &&
> + strcmp(prop->name, "gpio") != 0)
> + continue;
> +
> + count = of_count_phandle_with_args(curr, prop->name,
> + "#gpio-cells");
> + if (count <= 0)
> + continue;
> +
> + for (i = 0; i < count; i++) {
> + struct device_node *np __free(device_node) = NULL;
> +
> + ret = of_parse_phandle_with_args(curr, prop->name,
> + "#gpio-cells", i,
> + &args);
> + if (ret)
> + continue;
> +
> + np = args.np;
> +
> + if (!of_property_present(np, "gpio-controller"))
> + continue;
> +
> + /*
> + * We support 1, 2 and 3 cell GPIO bindings in the
> + * kernel currently. There's only one old MIPS dts that
> + * has a one-cell binding but there's no associated
> + * consumer so it may as well be an error. There don't
> + * seem to be any 3-cell users of non-exclusive GPIOs,
> + * so we can skip this as well. Let's occupy ourselves
> + * with the predominant 2-cell binding with the first
> + * cell indicating the hardware offset of the GPIO and
> + * the second defining the GPIO flags of the request.
> + */
> + if (args.args_count != 2)
> + continue;
> +
> + fwnode = of_fwnode_handle(args.np);
> + offset = args.args[0];
> +
> + entry = gpio_shared_find_entry(fwnode, offset);
> + if (!entry) {
> + entry = kzalloc(sizeof(*entry), GFP_KERNEL);
> + if (!entry)
> + return -ENOMEM;
> +
> + entry->fwnode = fwnode_handle_get(fwnode);
> + entry->offset = offset;
> + entry->index = count;
> + INIT_LIST_HEAD(&entry->refs);
> +
> + list_add_tail(&entry->list, &gpio_shared_list);
> + }
> +
> + struct gpio_shared_ref *ref __free(kfree) =
> + kzalloc(sizeof(*ref), GFP_KERNEL);
> + if (!ref)
> + return -ENOMEM;
> +
> + ref->fwnode = fwnode_handle_get(of_fwnode_handle(curr));
> + ref->flags = args.args[1];
> +
> + if (strends(prop->name, "gpios"))
> + suffix = "-gpios";
> + else if (strends(prop->name, "gpio"))
> + suffix = "-gpio";
> + else
> + suffix = NULL;
> + if (!suffix)
> + continue;
> +
> + /* We only set con_id if there's actually one. */
> + if (strcmp(prop->name, "gpios") && strcmp(prop->name, "gpio")) {
> + ref->con_id = kstrdup(prop->name, GFP_KERNEL);
> + if (!ref->con_id)
> + return -ENOMEM;
> +
> + con_id_len = strlen(ref->con_id);
> + suffix_len = strlen(suffix);
> +
> + ref->con_id[con_id_len - suffix_len] = '\0';
> + }
> +
> + ref->dev_id = ida_alloc(&gpio_shared_ida, GFP_KERNEL);
> + if (ref->dev_id < 0) {
> + kfree(ref->con_id);
> + return -ENOMEM;
> + }
> +
> + if (!list_empty(&entry->refs))
> + pr_debug("GPIO %u at %s is shared by multiple firmware nodes\n",
> + entry->offset, fwnode_get_name(entry->fwnode));
> +
> + list_add_tail(&no_free_ptr(ref)->list, &entry->refs);
> + }
> + }
> +
> + for_each_child_of_node_scoped(curr, child) {
> + ret = gpio_shared_of_traverse(child);
> + if (ret)
> + return ret;
> + }
> +
> + return 0;
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2025-10-29 11:45 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-29 11:20 [PATCH v3 00/10] gpio: improve support for shared GPIOs Bartosz Golaszewski
2025-10-29 11:20 ` [PATCH v3 01/10] string: provide strends() Bartosz Golaszewski
2025-10-29 11:41 ` Andy Shevchenko
2025-10-29 12:36 ` Bartosz Golaszewski
2025-10-29 15:20 ` Andy Shevchenko
2025-10-29 11:20 ` [PATCH v3 02/10] gpiolib: define GPIOD_FLAG_SHARED Bartosz Golaszewski
2025-11-11 10:39 ` Linus Walleij
2025-10-29 11:20 ` [PATCH v3 03/10] gpiolib: implement low-level, shared GPIO support Bartosz Golaszewski
2025-10-29 11:44 ` Andy Shevchenko [this message]
2025-10-29 12:39 ` Bartosz Golaszewski
2025-10-29 15:18 ` Andy Shevchenko
2025-10-29 15:57 ` Bartosz Golaszewski
2025-10-30 9:30 ` Andy Shevchenko
2025-11-11 10:41 ` Linus Walleij
2025-10-29 11:20 ` [PATCH v3 04/10] gpio: shared-proxy: implement the shared GPIO proxy driver Bartosz Golaszewski
2025-11-11 10:43 ` Linus Walleij
2025-10-29 11:20 ` [PATCH v3 05/10] gpiolib: support shared GPIOs in core subsystem code Bartosz Golaszewski
2025-11-11 10:43 ` Linus Walleij
2025-10-29 11:20 ` [PATCH v3 06/10] gpio: provide gpiod_is_shared() Bartosz Golaszewski
2025-10-29 11:20 ` [PATCH v3 07/10] arm64: select HAVE_SHARED_GPIOS for ARCH_QCOM Bartosz Golaszewski
2025-10-29 11:20 ` [PATCH v3 08/10] ASoC: wsa881x: drop GPIOD_FLAGS_BIT_NONEXCLUSIVE flag from GPIO lookup Bartosz Golaszewski
2025-10-29 11:20 ` [PATCH v3 09/10] ASoC: wsa883x: " Bartosz Golaszewski
2025-10-29 11:20 ` [PATCH v3 10/10] regulator: make the subsystem aware of shared GPIOs Bartosz Golaszewski
2025-10-29 11:54 ` Andy Shevchenko
2025-10-29 12:41 ` Bartosz Golaszewski
2025-10-29 15:21 ` Andy Shevchenko
2025-11-10 9:45 ` [PATCH v3 00/10] gpio: improve support for " Bartosz Golaszewski
2025-11-11 10:46 ` Linus Walleij
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=aQH-NcXry6_IlqXQ@smile.fi.intel.com \
--to=andriy.shevchenko@intel.com \
--cc=akpm@linux-foundation.org \
--cc=alexey.klimov@linaro.org \
--cc=andy@kernel.org \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=conor+dt@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=kees@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lgirdwood@gmail.com \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=mani@kernel.org \
--cc=perex@perex.cz \
--cc=robh@kernel.org \
--cc=saravanak@google.com \
--cc=srini@kernel.org \
--cc=tiwai@suse.com \
--cc=westeri@kernel.org \
--cc=will@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