All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] gpio: Allow setting gpio device id via device tree alias
@ 2022-03-22  5:40 haibo.chen
  2022-03-22  8:45 ` Andy Shevchenko
  2022-03-27 23:03 ` Linus Walleij
  0 siblings, 2 replies; 5+ messages in thread
From: haibo.chen @ 2022-03-22  5:40 UTC (permalink / raw)
  To: linus.walleij, brgl, andy.shevchenko
  Cc: linux-gpio, linux-kernel, linux-imx, haibo.chen

From: Haibo Chen <haibo.chen@nxp.com>

For some SoCs which contain different cores, like few ARM A cores
and few ARM M cores. Some GPIO controllers like GPIO3/GPIO4/GPIO5
belong to A core domain, some GPIO controllers like GPIO1/GPIO2
belong to M core domain. Linux only cover A cores, without gpio
alias, we can get gpiochip0/gpiochip1/gpiochip2 to map the real
GPIO3/GPIO4/GPIO5, it's difficult for users to identify this map
relation, and hardcode the gpio device index. With gpio alias,
we can easily make gpiochip3 map to GPIO3, gpiochip4 map to GPIO4.
For GPIO controllers do not claim the alias, it will get one id
which larger than all the claimed aliases.

Signed-off-by: Haibo Chen <haibo.chen@nxp.com>
---
 drivers/gpio/gpiolib.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 56d090258d62..3d24351a33db 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -495,7 +495,7 @@ static void gpiodevice_release(struct device *dev)
 	list_del(&gdev->list);
 	spin_unlock_irqrestore(&gpio_lock, flags);
 
-	ida_free(&gpio_ida, gdev->id);
+	ida_simple_remove(&gpio_ida, gdev->id);
 	kfree_const(gdev->label);
 	kfree(gdev->descs);
 	kfree(gdev);
@@ -594,6 +594,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	unsigned long flags;
 	int base = gc->base;
 	unsigned int i;
+	int alias_id, first_dynamic;
 	int ret = 0;
 	u32 ngpios;
 
@@ -623,11 +624,20 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 	 */
 	gdev->dev.fwnode = dev_fwnode(&gdev->dev) ?: fwnode;
 
-	gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
-	if (gdev->id < 0) {
-		ret = gdev->id;
-		goto err_free_gdev;
+	alias_id = of_alias_get_id(gdev->dev.of_node, "gpio");
+	if (alias_id < 0) {
+		first_dynamic = of_alias_get_highest_id("gpio");
+		if (first_dynamic < 0)
+			first_dynamic = 0;
+		else
+			first_dynamic++;
+		alias_id = ida_simple_get(&gpio_ida, first_dynamic, 0, GFP_KERNEL);
+		if (alias_id < 0) {
+			ret = alias_id;
+			goto err_free_gdev;
+		}
 	}
+	gdev->id = alias_id;
 
 	ret = dev_set_name(&gdev->dev, GPIOCHIP_NAME "%d", gdev->id);
 	if (ret)
@@ -821,7 +831,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 err_free_dev_name:
 	kfree(dev_name(&gdev->dev));
 err_free_ida:
-	ida_free(&gpio_ida, gdev->id);
+	ida_simple_remove(&gpio_ida, gdev->id);
 err_free_gdev:
 	/* failures here can mean systems won't boot... */
 	if (ret != -EPROBE_DEFER) {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] gpio: Allow setting gpio device id via device tree alias
  2022-03-22  5:40 [PATCH v2] gpio: Allow setting gpio device id via device tree alias haibo.chen
@ 2022-03-22  8:45 ` Andy Shevchenko
  2022-03-22 10:16   ` Bough Chen
  2022-03-27 23:03 ` Linus Walleij
  1 sibling, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2022-03-22  8:45 UTC (permalink / raw)
  To: BOUGH CHEN
  Cc: Linus Walleij, Bartosz Golaszewski, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, dl-linux-imx

On Tue, Mar 22, 2022 at 7:52 AM <haibo.chen@nxp.com> wrote:
>
> From: Haibo Chen <haibo.chen@nxp.com>
>
> For some SoCs which contain different cores, like few ARM A cores
> and few ARM M cores. Some GPIO controllers like GPIO3/GPIO4/GPIO5
> belong to A core domain, some GPIO controllers like GPIO1/GPIO2
> belong to M core domain. Linux only cover A cores, without gpio
> alias, we can get gpiochip0/gpiochip1/gpiochip2 to map the real
> GPIO3/GPIO4/GPIO5, it's difficult for users to identify this map
> relation, and hardcode the gpio device index. With gpio alias,

With the GPIO

> we can easily make gpiochip3 map to GPIO3, gpiochip4 map to GPIO4.
> For GPIO controllers do not claim the alias, it will get one id

If GPIO

> which larger than all the claimed aliases.

which is

...

I'm not sure I understand the issue. The other GPIO drivers and hence
user space (which is already quite a question why user space needs
this) may distinguish the GPIO chips by labels and device names.
What's wrong with that approach?

-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH v2] gpio: Allow setting gpio device id via device tree alias
  2022-03-22  8:45 ` Andy Shevchenko
@ 2022-03-22 10:16   ` Bough Chen
  2022-03-27 23:07     ` Linus Walleij
  0 siblings, 1 reply; 5+ messages in thread
From: Bough Chen @ 2022-03-22 10:16 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, Bartosz Golaszewski, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, dl-linux-imx

[-- Attachment #1: Type: text/plain, Size: 1844 bytes --]


> -----Original Message-----
> From: Andy Shevchenko [mailto:andy.shevchenko@gmail.com]
> Sent: 2022年3月22日 16:45
> To: Bough Chen <haibo.chen@nxp.com>
> Cc: Linus Walleij <linus.walleij@linaro.org>; Bartosz Golaszewski
> <brgl@bgdev.pl>; open list:GPIO SUBSYSTEM <linux-gpio@vger.kernel.org>;
> Linux Kernel Mailing List <linux-kernel@vger.kernel.org>; dl-linux-imx
> <linux-imx@nxp.com>
> Subject: Re: [PATCH v2] gpio: Allow setting gpio device id via device tree alias
> 
> On Tue, Mar 22, 2022 at 7:52 AM <haibo.chen@nxp.com> wrote:
> >
> > From: Haibo Chen <haibo.chen@nxp.com>
> >
> > For some SoCs which contain different cores, like few ARM A cores and
> > few ARM M cores. Some GPIO controllers like GPIO3/GPIO4/GPIO5 belong
> > to A core domain, some GPIO controllers like GPIO1/GPIO2 belong to M
> > core domain. Linux only cover A cores, without gpio alias, we can get
> > gpiochip0/gpiochip1/gpiochip2 to map the real GPIO3/GPIO4/GPIO5, it's
> > difficult for users to identify this map relation, and hardcode the
> > gpio device index. With gpio alias,
> 
> With the GPIO
> 
> > we can easily make gpiochip3 map to GPIO3, gpiochip4 map to GPIO4.
> > For GPIO controllers do not claim the alias, it will get one id
> 
> If GPIO
> 
> > which larger than all the claimed aliases.
> 
> which is
> 
> ...
> 
> I'm not sure I understand the issue. The other GPIO drivers and hence user
> space (which is already quite a question why user space needs
> this) may distinguish the GPIO chips by labels and device names.
> What's wrong with that approach?

For users, usually, they will open /dev/gpiochipx to handle gpio related option. 
By the way, how can user get the labels and device names of GPIO chips? 

Best Regards
Haibo Chen
> 
> --
> With Best Regards,
> Andy Shevchenko

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 9551 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] gpio: Allow setting gpio device id via device tree alias
  2022-03-22  5:40 [PATCH v2] gpio: Allow setting gpio device id via device tree alias haibo.chen
  2022-03-22  8:45 ` Andy Shevchenko
@ 2022-03-27 23:03 ` Linus Walleij
  1 sibling, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2022-03-27 23:03 UTC (permalink / raw)
  To: haibo.chen, Rob Herring,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS
  Cc: brgl, andy.shevchenko, linux-gpio, linux-kernel, linux-imx

On Tue, Mar 22, 2022 at 6:52 AM <haibo.chen@nxp.com> wrote:

> From: Haibo Chen <haibo.chen@nxp.com>
>
> For some SoCs which contain different cores, like few ARM A cores
> and few ARM M cores. Some GPIO controllers like GPIO3/GPIO4/GPIO5
> belong to A core domain, some GPIO controllers like GPIO1/GPIO2
> belong to M core domain. Linux only cover A cores, without gpio
> alias, we can get gpiochip0/gpiochip1/gpiochip2 to map the real
> GPIO3/GPIO4/GPIO5, it's difficult for users to identify this map
> relation, and hardcode the gpio device index. With gpio alias,
> we can easily make gpiochip3 map to GPIO3, gpiochip4 map to GPIO4.
> For GPIO controllers do not claim the alias, it will get one id
> which larger than all the claimed aliases.
>
> Signed-off-by: Haibo Chen <haibo.chen@nxp.com>

This is device tree related, adds semantics to aliases and needs
a prerequisite patch to
Documentation/devicetree/bindings/gpio/gpio.txt
at the very least.

Also that patch needs to CC to devicetree@vger.kernel.org

IIRC this has been suggested in the past.

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] gpio: Allow setting gpio device id via device tree alias
  2022-03-22 10:16   ` Bough Chen
@ 2022-03-27 23:07     ` Linus Walleij
  0 siblings, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2022-03-27 23:07 UTC (permalink / raw)
  To: Bough Chen
  Cc: Andy Shevchenko, Bartosz Golaszewski, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, dl-linux-imx

On Tue, Mar 22, 2022 at 11:16 AM Bough Chen <haibo.chen@nxp.com> wrote:

> For users, usually, they will open /dev/gpiochipx to handle gpio related option.

That makes sense, it is intuitive in a way.

> By the way, how can user get the labels and device names of GPIO chips?

The way to get metadata for any /dev/* device such as topology on
buses is through sysfs.

It is also possible to get labels for all gpiochips by iterating over them.

The most user-friendly thing to do is however to make use of libgpiod
and associated example tools such as gpiodetect:
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/

Yours,
Linus Walleij

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-27 23:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22  5:40 [PATCH v2] gpio: Allow setting gpio device id via device tree alias haibo.chen
2022-03-22  8:45 ` Andy Shevchenko
2022-03-22 10:16   ` Bough Chen
2022-03-27 23:07     ` Linus Walleij
2022-03-27 23:03 ` Linus Walleij

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.