Skip to content

Commit

Permalink
gpiolib: Override gpiochip numbers with DT aliases
Browse files Browse the repository at this point in the history
In the same way that other subsystems support the setting of device
id numbers from Device Tree aliases, allow gpiochip numbers to be
derived from "gpiochip<n>" aliases.

Signed-off-by: Phil Elwell <[email protected]>
  • Loading branch information
pelwell authored and popcornmix committed Jan 2, 2025
1 parent 33dccb3 commit 15f4c3d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion drivers/gpio/gpiolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc);
static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gc);

static bool gpiolib_initialized;
static int first_dynamic_gpiochip_num = -1;

const char *gpiod_get_label(struct gpio_desc *desc)
{
Expand Down Expand Up @@ -927,6 +928,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
unsigned int desc_index;
int base = 0;
int ret = 0;
int id;

/*
* First: allocate and populate the internal stat container, and
Expand All @@ -953,7 +955,16 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
else if (gc->parent)
device_set_node(&gdev->dev, dev_fwnode(gc->parent));

gdev->id = ida_alloc(&gpio_ida, GFP_KERNEL);
if (first_dynamic_gpiochip_num < 0) {
id = of_alias_get_highest_id("gpiochip");
first_dynamic_gpiochip_num = (id >= 0) ? (id + 1) : 0;
}

id = of_alias_get_id(gdev->dev.of_node, "gpiochip");
if (id < 0)
id = first_dynamic_gpiochip_num;

gdev->id = ida_alloc_range(&gpio_ida, id, ~0, GFP_KERNEL);
if (gdev->id < 0) {
ret = gdev->id;
goto err_free_gdev;
Expand Down

0 comments on commit 15f4c3d

Please sign in to comment.