pinctrl: stm32: set default gpio line names using pin names
authorValentin Caron <valentin.caron@foss.st.com>
Tue, 20 Jun 2023 10:43:49 +0000 (12:43 +0200)
committerLinus Walleij <linus.walleij@linaro.org>
Thu, 10 Aug 2023 08:12:39 +0000 (10:12 +0200)
Add stm32_pctrl_get_desc_pin_from_gpio function to find a stm32 pin
descriptor which is matching with a gpio.
Most of the time pin number is equal to pin index in array. So the first
part of the function is useful to speed up.

And during gpio bank register, we set default gpio names with pin names.

Signed-off-by: Valentin Caron <valentin.caron@foss.st.com>
Acked-by: Alexandre TORGUE <alexandre.torgue@foss.st.com>
Link: https://lore.kernel.org/r/20230620104349.834687-1-valentin.caron@foss.st.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/pinctrl/stm32/pinctrl-stm32.c

index cbdb283589653d50e9398b623f418efde75c2ef2..a73385a431de98120db87aae78565c59866e9416 100644 (file)
@@ -1274,6 +1274,28 @@ static const struct pinconf_ops stm32_pconf_ops = {
        .pin_config_dbg_show    = stm32_pconf_dbg_show,
 };
 
+static struct stm32_desc_pin *stm32_pctrl_get_desc_pin_from_gpio(struct stm32_pinctrl *pctl,
+                                                                struct stm32_gpio_bank *bank,
+                                                                unsigned int offset)
+{
+       unsigned int stm32_pin_nb = bank->bank_nr * STM32_GPIO_PINS_PER_BANK + offset;
+       struct stm32_desc_pin *pin_desc;
+       int i;
+
+       /* With few exceptions (e.g. bank 'Z'), pin number matches with pin index in array */
+       pin_desc = pctl->pins + stm32_pin_nb;
+       if (pin_desc->pin.number == stm32_pin_nb)
+               return pin_desc;
+
+       /* Otherwise, loop all array to find the pin with the right number */
+       for (i = 0; i < pctl->npins; i++) {
+               pin_desc = pctl->pins + i;
+               if (pin_desc->pin.number == stm32_pin_nb)
+                       return pin_desc;
+       }
+       return NULL;
+}
+
 static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode_handle *fwnode)
 {
        struct stm32_gpio_bank *bank = &pctl->banks[pctl->nbanks];
@@ -1284,6 +1306,8 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
        struct resource res;
        int npins = STM32_GPIO_PINS_PER_BANK;
        int bank_nr, err, i = 0;
+       struct stm32_desc_pin *stm32_pin;
+       char **names;
 
        if (!IS_ERR(bank->rstc))
                reset_control_deassert(bank->rstc);
@@ -1353,6 +1377,17 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
                }
        }
 
+       names = devm_kcalloc(dev, npins, sizeof(char *), GFP_KERNEL);
+       for (i = 0; i < npins; i++) {
+               stm32_pin = stm32_pctrl_get_desc_pin_from_gpio(pctl, bank, i);
+               if (stm32_pin && stm32_pin->pin.name)
+                       names[i] = devm_kasprintf(dev, GFP_KERNEL, "%s", stm32_pin->pin.name);
+               else
+                       names[i] = NULL;
+       }
+
+       bank->gpio_chip.names = (const char * const *)names;
+
        err = gpiochip_add_data(&bank->gpio_chip, bank);
        if (err) {
                dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_nr);