Skip to content

Commit 9dbd1ab

Browse files
committed
gpiolib: check the 'ngpios' property in core gpiolib code
Several drivers read the 'ngpios' device property on their own, but since it's defined as a standard GPIO property in the device tree bindings anyway, it's a good candidate for generalization. If the driver didn't set its gc->ngpio, try to read the 'ngpios' property from the GPIO device's firmware node before bailing out. Signed-off-by: Bartosz Golaszewski <[email protected]> Suggested-by: Andy Shevchenko <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]>
1 parent e5ab49c commit 9dbd1ab

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

drivers/gpio/gpiolib.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
599599
int base = gc->base;
600600
unsigned int i;
601601
int ret = 0;
602+
u32 ngpios;
602603

603604
/*
604605
* First: allocate and populate the internal stat container, and
@@ -646,6 +647,26 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
646647
goto err_free_dev_name;
647648
}
648649

650+
/*
651+
* Try the device properties if the driver didn't supply the number
652+
* of GPIO lines.
653+
*/
654+
if (gc->ngpio == 0) {
655+
ret = device_property_read_u32(&gdev->dev, "ngpios", &ngpios);
656+
if (ret == -ENODATA)
657+
/*
658+
* -ENODATA means that there is no property found and
659+
* we want to issue the error message to the user.
660+
* Besides that, we want to return different error code
661+
* to state that supplied value is not valid.
662+
*/
663+
ngpios = 0;
664+
else if (ret)
665+
goto err_free_descs;
666+
667+
gc->ngpio = ngpios;
668+
}
669+
649670
if (gc->ngpio == 0) {
650671
chip_err(gc, "tried to insert a GPIO chip with zero lines\n");
651672
ret = -EINVAL;

0 commit comments

Comments
 (0)