If a GPIO line cannot be exported using a symbolic name from the .names array in the gpiochip, fall back to using the "gpioN" naming system instead of just failing. Cc: Manivannan Sadhasivam <mani@xxxxxxxxxx> Cc: Johan Hovold <johan@xxxxxxxxxx> Suggested-by: Johan Hovold <johan@xxxxxxxxxx> Signed-off-by: Linus Walleij <linus.walleij@xxxxxxxxxx> --- drivers/gpio/gpiolib-sysfs.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib-sysfs.c b/drivers/gpio/gpiolib-sysfs.c index 728f6c687182..a5a0e9238217 100644 --- a/drivers/gpio/gpiolib-sysfs.c +++ b/drivers/gpio/gpiolib-sysfs.c @@ -627,10 +627,24 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change) if (chip->names && chip->names[offset]) ioname = chip->names[offset]; - dev = device_create_with_groups(&gpio_class, &gdev->dev, - MKDEV(0, 0), data, gpio_groups, - ioname ? ioname : "gpio%u", - desc_to_gpio(desc)); + /* + * If we have a symbolic name for the GPIO we try to use that + * for the exported sysfs device/file, as legacy scripts depend + * on it. If we don't have a symbolic name or if there is a + * namespace collision, we stick with the "gpioN" name. + */ + dev = NULL; + if (ioname) + dev = device_create_with_groups(&gpio_class, &gdev->dev, + MKDEV(0, 0), data, gpio_groups, + ioname, + desc_to_gpio(desc)); + if (IS_ERR_OR_NULL(dev)) + dev = device_create_with_groups(&gpio_class, &gdev->dev, + MKDEV(0, 0), data, gpio_groups, + "gpio%u", + desc_to_gpio(desc)); + if (IS_ERR(dev)) { status = PTR_ERR(dev); goto err_free_data; -- 2.28.0