Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * OF helpers for the GPIO API
0004  *
0005  * Copyright (c) 2007-2008  MontaVista Software, Inc.
0006  *
0007  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
0008  */
0009 
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/errno.h>
0013 #include <linux/module.h>
0014 #include <linux/io.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/of.h>
0017 #include <linux/of_address.h>
0018 #include <linux/of_gpio.h>
0019 #include <linux/pinctrl/pinctrl.h>
0020 #include <linux/slab.h>
0021 #include <linux/gpio/machine.h>
0022 
0023 #include "gpiolib.h"
0024 #include "gpiolib-of.h"
0025 
0026 /**
0027  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
0028  * @dev:    Consuming device
0029  * @con_id: Function within the GPIO consumer
0030  *
0031  * Some elder GPIO controllers need special quirks. Currently we handle
0032  * the Freescale and PPC GPIO controller with bindings that doesn't use the
0033  * established "cs-gpios" for chip selects but instead rely on
0034  * "gpios" for the chip select lines. If we detect this, we redirect
0035  * the counting of "cs-gpios" to count "gpios" transparent to the
0036  * driver.
0037  */
0038 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
0039 {
0040     struct device_node *np = dev->of_node;
0041 
0042     if (!IS_ENABLED(CONFIG_SPI_MASTER))
0043         return 0;
0044     if (!con_id || strcmp(con_id, "cs"))
0045         return 0;
0046     if (!of_device_is_compatible(np, "fsl,spi") &&
0047         !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
0048         !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
0049         return 0;
0050     return of_gpio_named_count(np, "gpios");
0051 }
0052 
0053 /*
0054  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
0055  *
0056  * FIXME: get rid of those external users by converting them to GPIO
0057  * descriptors and let them all use gpiod_count()
0058  */
0059 int of_gpio_get_count(struct device *dev, const char *con_id)
0060 {
0061     int ret;
0062     char propname[32];
0063     unsigned int i;
0064 
0065     ret = of_gpio_spi_cs_get_count(dev, con_id);
0066     if (ret > 0)
0067         return ret;
0068 
0069     for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
0070         if (con_id)
0071             snprintf(propname, sizeof(propname), "%s-%s",
0072                  con_id, gpio_suffixes[i]);
0073         else
0074             snprintf(propname, sizeof(propname), "%s",
0075                  gpio_suffixes[i]);
0076 
0077         ret = of_gpio_named_count(dev->of_node, propname);
0078         if (ret > 0)
0079             break;
0080     }
0081     return ret ? ret : -ENOENT;
0082 }
0083 
0084 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
0085 {
0086     struct of_phandle_args *gpiospec = data;
0087 
0088     return chip->gpiodev->dev.of_node == gpiospec->np &&
0089                 chip->of_xlate &&
0090                 chip->of_xlate(chip, gpiospec, NULL) >= 0;
0091 }
0092 
0093 static struct gpio_chip *of_find_gpiochip_by_xlate(
0094                     struct of_phandle_args *gpiospec)
0095 {
0096     return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
0097 }
0098 
0099 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
0100                     struct of_phandle_args *gpiospec,
0101                     enum of_gpio_flags *flags)
0102 {
0103     int ret;
0104 
0105     if (chip->of_gpio_n_cells != gpiospec->args_count)
0106         return ERR_PTR(-EINVAL);
0107 
0108     ret = chip->of_xlate(chip, gpiospec, flags);
0109     if (ret < 0)
0110         return ERR_PTR(ret);
0111 
0112     return gpiochip_get_desc(chip, ret);
0113 }
0114 
0115 /**
0116  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
0117  * to set the .valid_mask
0118  * @gc: the target gpio_chip
0119  *
0120  * Return: true if the valid mask needs to be set
0121  */
0122 bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
0123 {
0124     int size;
0125     const struct device_node *np = gc->of_node;
0126 
0127     size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
0128     if (size > 0 && size % 2 == 0)
0129         return true;
0130     return false;
0131 }
0132 
0133 static void of_gpio_flags_quirks(const struct device_node *np,
0134                  const char *propname,
0135                  enum of_gpio_flags *flags,
0136                  int index)
0137 {
0138     /*
0139      * Some GPIO fixed regulator quirks.
0140      * Note that active low is the default.
0141      */
0142     if (IS_ENABLED(CONFIG_REGULATOR) &&
0143         (of_device_is_compatible(np, "regulator-fixed") ||
0144          of_device_is_compatible(np, "reg-fixed-voltage") ||
0145          (!(strcmp(propname, "enable-gpio") &&
0146         strcmp(propname, "enable-gpios")) &&
0147           of_device_is_compatible(np, "regulator-gpio")))) {
0148         bool active_low = !of_property_read_bool(np,
0149                              "enable-active-high");
0150         /*
0151          * The regulator GPIO handles are specified such that the
0152          * presence or absence of "enable-active-high" solely controls
0153          * the polarity of the GPIO line. Any phandle flags must
0154          * be actively ignored.
0155          */
0156         if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
0157             pr_warn("%s GPIO handle specifies active low - ignored\n",
0158                 of_node_full_name(np));
0159             *flags &= ~OF_GPIO_ACTIVE_LOW;
0160         }
0161         if (active_low)
0162             *flags |= OF_GPIO_ACTIVE_LOW;
0163     }
0164     /*
0165      * Legacy open drain handling for fixed voltage regulators.
0166      */
0167     if (IS_ENABLED(CONFIG_REGULATOR) &&
0168         of_device_is_compatible(np, "reg-fixed-voltage") &&
0169         of_property_read_bool(np, "gpio-open-drain")) {
0170         *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
0171         pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
0172             of_node_full_name(np));
0173     }
0174 
0175     /*
0176      * Legacy handling of SPI active high chip select. If we have a
0177      * property named "cs-gpios" we need to inspect the child node
0178      * to determine if the flags should have inverted semantics.
0179      */
0180     if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
0181         of_property_read_bool(np, "cs-gpios")) {
0182         struct device_node *child;
0183         u32 cs;
0184         int ret;
0185 
0186         for_each_child_of_node(np, child) {
0187             ret = of_property_read_u32(child, "reg", &cs);
0188             if (ret)
0189                 continue;
0190             if (cs == index) {
0191                 /*
0192                  * SPI children have active low chip selects
0193                  * by default. This can be specified negatively
0194                  * by just omitting "spi-cs-high" in the
0195                  * device node, or actively by tagging on
0196                  * GPIO_ACTIVE_LOW as flag in the device
0197                  * tree. If the line is simultaneously
0198                  * tagged as active low in the device tree
0199                  * and has the "spi-cs-high" set, we get a
0200                  * conflict and the "spi-cs-high" flag will
0201                  * take precedence.
0202                  */
0203                 if (of_property_read_bool(child, "spi-cs-high")) {
0204                     if (*flags & OF_GPIO_ACTIVE_LOW) {
0205                         pr_warn("%s GPIO handle specifies active low - ignored\n",
0206                             of_node_full_name(child));
0207                         *flags &= ~OF_GPIO_ACTIVE_LOW;
0208                     }
0209                 } else {
0210                     if (!(*flags & OF_GPIO_ACTIVE_LOW))
0211                         pr_info("%s enforce active low on chipselect handle\n",
0212                             of_node_full_name(child));
0213                     *flags |= OF_GPIO_ACTIVE_LOW;
0214                 }
0215                 of_node_put(child);
0216                 break;
0217             }
0218         }
0219     }
0220 
0221     /* Legacy handling of stmmac's active-low PHY reset line */
0222     if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
0223         !strcmp(propname, "snps,reset-gpio") &&
0224         of_property_read_bool(np, "snps,reset-active-low"))
0225         *flags |= OF_GPIO_ACTIVE_LOW;
0226 }
0227 
0228 /**
0229  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
0230  * @np:     device node to get GPIO from
0231  * @propname:   property name containing gpio specifier(s)
0232  * @index:  index of the GPIO
0233  * @flags:  a flags pointer to fill in
0234  *
0235  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
0236  * value on the error condition. If @flags is not NULL the function also fills
0237  * in flags for the GPIO.
0238  */
0239 static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np,
0240              const char *propname, int index, enum of_gpio_flags *flags)
0241 {
0242     struct of_phandle_args gpiospec;
0243     struct gpio_chip *chip;
0244     struct gpio_desc *desc;
0245     int ret;
0246 
0247     ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
0248                          &gpiospec);
0249     if (ret) {
0250         pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
0251             __func__, propname, np, index);
0252         return ERR_PTR(ret);
0253     }
0254 
0255     chip = of_find_gpiochip_by_xlate(&gpiospec);
0256     if (!chip) {
0257         desc = ERR_PTR(-EPROBE_DEFER);
0258         goto out;
0259     }
0260 
0261     desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
0262     if (IS_ERR(desc))
0263         goto out;
0264 
0265     if (flags)
0266         of_gpio_flags_quirks(np, propname, flags, index);
0267 
0268     pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
0269          __func__, propname, np, index,
0270          PTR_ERR_OR_ZERO(desc));
0271 
0272 out:
0273     of_node_put(gpiospec.np);
0274 
0275     return desc;
0276 }
0277 
0278 int of_get_named_gpio_flags(const struct device_node *np, const char *list_name,
0279                 int index, enum of_gpio_flags *flags)
0280 {
0281     struct gpio_desc *desc;
0282 
0283     desc = of_get_named_gpiod_flags(np, list_name, index, flags);
0284 
0285     if (IS_ERR(desc))
0286         return PTR_ERR(desc);
0287     else
0288         return desc_to_gpio(desc);
0289 }
0290 EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
0291 
0292 /**
0293  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
0294  * @node:   handle of the OF node
0295  * @propname:   name of the DT property representing the GPIO
0296  * @index:  index of the GPIO to obtain for the consumer
0297  * @dflags: GPIO initialization flags
0298  * @label:  label to attach to the requested GPIO
0299  *
0300  * Returns:
0301  * On successful request the GPIO pin is configured in accordance with
0302  * provided @dflags.
0303  *
0304  * In case of error an ERR_PTR() is returned.
0305  */
0306 struct gpio_desc *gpiod_get_from_of_node(const struct device_node *node,
0307                      const char *propname, int index,
0308                      enum gpiod_flags dflags,
0309                      const char *label)
0310 {
0311     unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
0312     struct gpio_desc *desc;
0313     enum of_gpio_flags flags;
0314     bool active_low = false;
0315     bool single_ended = false;
0316     bool open_drain = false;
0317     bool transitory = false;
0318     int ret;
0319 
0320     desc = of_get_named_gpiod_flags(node, propname,
0321                     index, &flags);
0322 
0323     if (!desc || IS_ERR(desc)) {
0324         return desc;
0325     }
0326 
0327     active_low = flags & OF_GPIO_ACTIVE_LOW;
0328     single_ended = flags & OF_GPIO_SINGLE_ENDED;
0329     open_drain = flags & OF_GPIO_OPEN_DRAIN;
0330     transitory = flags & OF_GPIO_TRANSITORY;
0331 
0332     ret = gpiod_request(desc, label);
0333     if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
0334         return desc;
0335     if (ret)
0336         return ERR_PTR(ret);
0337 
0338     if (active_low)
0339         lflags |= GPIO_ACTIVE_LOW;
0340 
0341     if (single_ended) {
0342         if (open_drain)
0343             lflags |= GPIO_OPEN_DRAIN;
0344         else
0345             lflags |= GPIO_OPEN_SOURCE;
0346     }
0347 
0348     if (transitory)
0349         lflags |= GPIO_TRANSITORY;
0350 
0351     if (flags & OF_GPIO_PULL_UP)
0352         lflags |= GPIO_PULL_UP;
0353 
0354     if (flags & OF_GPIO_PULL_DOWN)
0355         lflags |= GPIO_PULL_DOWN;
0356 
0357     if (flags & OF_GPIO_PULL_DISABLE)
0358         lflags |= GPIO_PULL_DISABLE;
0359 
0360     ret = gpiod_configure_flags(desc, propname, lflags, dflags);
0361     if (ret < 0) {
0362         gpiod_put(desc);
0363         return ERR_PTR(ret);
0364     }
0365 
0366     return desc;
0367 }
0368 EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
0369 
0370 /*
0371  * The SPI GPIO bindings happened before we managed to establish that GPIO
0372  * properties should be named "foo-gpios" so we have this special kludge for
0373  * them.
0374  */
0375 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
0376                       enum of_gpio_flags *of_flags)
0377 {
0378     char prop_name[32]; /* 32 is max size of property name */
0379     const struct device_node *np = dev->of_node;
0380     struct gpio_desc *desc;
0381 
0382     /*
0383      * Hopefully the compiler stubs the rest of the function if this
0384      * is false.
0385      */
0386     if (!IS_ENABLED(CONFIG_SPI_MASTER))
0387         return ERR_PTR(-ENOENT);
0388 
0389     /* Allow this specifically for "spi-gpio" devices */
0390     if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
0391         return ERR_PTR(-ENOENT);
0392 
0393     /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
0394     snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
0395 
0396     desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
0397     return desc;
0398 }
0399 
0400 /*
0401  * The old Freescale bindings use simply "gpios" as name for the chip select
0402  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
0403  * with a special quirk.
0404  */
0405 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
0406                          const char *con_id,
0407                          unsigned int idx,
0408                          unsigned long *flags)
0409 {
0410     const struct device_node *np = dev->of_node;
0411 
0412     if (!IS_ENABLED(CONFIG_SPI_MASTER))
0413         return ERR_PTR(-ENOENT);
0414 
0415     /* Allow this specifically for Freescale and PPC devices */
0416     if (!of_device_is_compatible(np, "fsl,spi") &&
0417         !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
0418         !of_device_is_compatible(np, "ibm,ppc4xx-spi"))
0419         return ERR_PTR(-ENOENT);
0420     /* Allow only if asking for "cs-gpios" */
0421     if (!con_id || strcmp(con_id, "cs"))
0422         return ERR_PTR(-ENOENT);
0423 
0424     /*
0425      * While all other SPI controllers use "cs-gpios" the Freescale
0426      * uses just "gpios" so translate to that when "cs-gpios" is
0427      * requested.
0428      */
0429     return of_find_gpio(dev, NULL, idx, flags);
0430 }
0431 
0432 /*
0433  * Some regulator bindings happened before we managed to establish that GPIO
0434  * properties should be named "foo-gpios" so we have this special kludge for
0435  * them.
0436  */
0437 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
0438                         enum of_gpio_flags *of_flags)
0439 {
0440     /* These are the connection IDs we accept as legacy GPIO phandles */
0441     const char *whitelist[] = {
0442         "wlf,ldoena", /* Arizona */
0443         "wlf,ldo1ena", /* WM8994 */
0444         "wlf,ldo2ena", /* WM8994 */
0445     };
0446     const struct device_node *np = dev->of_node;
0447     struct gpio_desc *desc;
0448     int i;
0449 
0450     if (!IS_ENABLED(CONFIG_REGULATOR))
0451         return ERR_PTR(-ENOENT);
0452 
0453     if (!con_id)
0454         return ERR_PTR(-ENOENT);
0455 
0456     i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
0457     if (i < 0)
0458         return ERR_PTR(-ENOENT);
0459 
0460     desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
0461     return desc;
0462 }
0463 
0464 static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
0465                           const char *con_id,
0466                           enum of_gpio_flags *of_flags)
0467 {
0468     if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
0469         return ERR_PTR(-ENOENT);
0470 
0471     if (!con_id || strcmp(con_id, "wlf,reset"))
0472         return ERR_PTR(-ENOENT);
0473 
0474     return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
0475 }
0476 
0477 static struct gpio_desc *of_find_usb_gpio(struct device *dev,
0478                       const char *con_id,
0479                       enum of_gpio_flags *of_flags)
0480 {
0481     /*
0482      * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
0483      * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
0484      * suffix.
0485      */
0486     if (!IS_ENABLED(CONFIG_TYPEC_FUSB302))
0487         return ERR_PTR(-ENOENT);
0488 
0489     if (!con_id || strcmp(con_id, "fcs,int_n"))
0490         return ERR_PTR(-ENOENT);
0491 
0492     return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
0493 }
0494 
0495 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
0496                    unsigned int idx, unsigned long *flags)
0497 {
0498     char prop_name[32]; /* 32 is max size of property name */
0499     enum of_gpio_flags of_flags;
0500     struct gpio_desc *desc;
0501     unsigned int i;
0502 
0503     /* Try GPIO property "foo-gpios" and "foo-gpio" */
0504     for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
0505         if (con_id)
0506             snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
0507                  gpio_suffixes[i]);
0508         else
0509             snprintf(prop_name, sizeof(prop_name), "%s",
0510                  gpio_suffixes[i]);
0511 
0512         desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
0513                         &of_flags);
0514 
0515         if (!gpiod_not_found(desc))
0516             break;
0517     }
0518 
0519     if (gpiod_not_found(desc)) {
0520         /* Special handling for SPI GPIOs if used */
0521         desc = of_find_spi_gpio(dev, con_id, &of_flags);
0522     }
0523 
0524     if (gpiod_not_found(desc)) {
0525         /* This quirk looks up flags and all */
0526         desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
0527         if (!IS_ERR(desc))
0528             return desc;
0529     }
0530 
0531     if (gpiod_not_found(desc)) {
0532         /* Special handling for regulator GPIOs if used */
0533         desc = of_find_regulator_gpio(dev, con_id, &of_flags);
0534     }
0535 
0536     if (gpiod_not_found(desc))
0537         desc = of_find_arizona_gpio(dev, con_id, &of_flags);
0538 
0539     if (gpiod_not_found(desc))
0540         desc = of_find_usb_gpio(dev, con_id, &of_flags);
0541 
0542     if (IS_ERR(desc))
0543         return desc;
0544 
0545     if (of_flags & OF_GPIO_ACTIVE_LOW)
0546         *flags |= GPIO_ACTIVE_LOW;
0547 
0548     if (of_flags & OF_GPIO_SINGLE_ENDED) {
0549         if (of_flags & OF_GPIO_OPEN_DRAIN)
0550             *flags |= GPIO_OPEN_DRAIN;
0551         else
0552             *flags |= GPIO_OPEN_SOURCE;
0553     }
0554 
0555     if (of_flags & OF_GPIO_TRANSITORY)
0556         *flags |= GPIO_TRANSITORY;
0557 
0558     if (of_flags & OF_GPIO_PULL_UP)
0559         *flags |= GPIO_PULL_UP;
0560     if (of_flags & OF_GPIO_PULL_DOWN)
0561         *flags |= GPIO_PULL_DOWN;
0562     if (of_flags & OF_GPIO_PULL_DISABLE)
0563         *flags |= GPIO_PULL_DISABLE;
0564 
0565     return desc;
0566 }
0567 
0568 /**
0569  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
0570  * @np:     device node to get GPIO from
0571  * @chip:   GPIO chip whose hog is parsed
0572  * @idx:    Index of the GPIO to parse
0573  * @name:   GPIO line name
0574  * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
0575  *      of_find_gpio() or of_parse_own_gpio()
0576  * @dflags: gpiod_flags - optional GPIO initialization flags
0577  *
0578  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
0579  * value on the error condition.
0580  */
0581 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
0582                        struct gpio_chip *chip,
0583                        unsigned int idx, const char **name,
0584                        unsigned long *lflags,
0585                        enum gpiod_flags *dflags)
0586 {
0587     struct device_node *chip_np;
0588     enum of_gpio_flags xlate_flags;
0589     struct of_phandle_args gpiospec;
0590     struct gpio_desc *desc;
0591     unsigned int i;
0592     u32 tmp;
0593     int ret;
0594 
0595     chip_np = chip->of_node;
0596     if (!chip_np)
0597         return ERR_PTR(-EINVAL);
0598 
0599     xlate_flags = 0;
0600     *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
0601     *dflags = GPIOD_ASIS;
0602 
0603     ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
0604     if (ret)
0605         return ERR_PTR(ret);
0606 
0607     gpiospec.np = chip_np;
0608     gpiospec.args_count = tmp;
0609 
0610     for (i = 0; i < tmp; i++) {
0611         ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
0612                          &gpiospec.args[i]);
0613         if (ret)
0614             return ERR_PTR(ret);
0615     }
0616 
0617     desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
0618     if (IS_ERR(desc))
0619         return desc;
0620 
0621     if (xlate_flags & OF_GPIO_ACTIVE_LOW)
0622         *lflags |= GPIO_ACTIVE_LOW;
0623     if (xlate_flags & OF_GPIO_TRANSITORY)
0624         *lflags |= GPIO_TRANSITORY;
0625     if (xlate_flags & OF_GPIO_PULL_UP)
0626         *lflags |= GPIO_PULL_UP;
0627     if (xlate_flags & OF_GPIO_PULL_DOWN)
0628         *lflags |= GPIO_PULL_DOWN;
0629     if (xlate_flags & OF_GPIO_PULL_DISABLE)
0630         *lflags |= GPIO_PULL_DISABLE;
0631 
0632     if (of_property_read_bool(np, "input"))
0633         *dflags |= GPIOD_IN;
0634     else if (of_property_read_bool(np, "output-low"))
0635         *dflags |= GPIOD_OUT_LOW;
0636     else if (of_property_read_bool(np, "output-high"))
0637         *dflags |= GPIOD_OUT_HIGH;
0638     else {
0639         pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
0640             desc_to_gpio(desc), np);
0641         return ERR_PTR(-EINVAL);
0642     }
0643 
0644     if (name && of_property_read_string(np, "line-name", name))
0645         *name = np->name;
0646 
0647     return desc;
0648 }
0649 
0650 /**
0651  * of_gpiochip_add_hog - Add all hogs in a hog device node
0652  * @chip:   gpio chip to act on
0653  * @hog:    device node describing the hogs
0654  *
0655  * Returns error if it fails otherwise 0 on success.
0656  */
0657 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
0658 {
0659     enum gpiod_flags dflags;
0660     struct gpio_desc *desc;
0661     unsigned long lflags;
0662     const char *name;
0663     unsigned int i;
0664     int ret;
0665 
0666     for (i = 0;; i++) {
0667         desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
0668         if (IS_ERR(desc))
0669             break;
0670 
0671         ret = gpiod_hog(desc, name, lflags, dflags);
0672         if (ret < 0)
0673             return ret;
0674 
0675 #ifdef CONFIG_OF_DYNAMIC
0676         desc->hog = hog;
0677 #endif
0678     }
0679 
0680     return 0;
0681 }
0682 
0683 /**
0684  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
0685  * @chip:   gpio chip to act on
0686  *
0687  * This is only used by of_gpiochip_add to request/set GPIO initial
0688  * configuration.
0689  * It returns error if it fails otherwise 0 on success.
0690  */
0691 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
0692 {
0693     struct device_node *np;
0694     int ret;
0695 
0696     for_each_available_child_of_node(chip->of_node, np) {
0697         if (!of_property_read_bool(np, "gpio-hog"))
0698             continue;
0699 
0700         ret = of_gpiochip_add_hog(chip, np);
0701         if (ret < 0) {
0702             of_node_put(np);
0703             return ret;
0704         }
0705 
0706         of_node_set_flag(np, OF_POPULATED);
0707     }
0708 
0709     return 0;
0710 }
0711 
0712 #ifdef CONFIG_OF_DYNAMIC
0713 /**
0714  * of_gpiochip_remove_hog - Remove all hogs in a hog device node
0715  * @chip:   gpio chip to act on
0716  * @hog:    device node describing the hogs
0717  */
0718 static void of_gpiochip_remove_hog(struct gpio_chip *chip,
0719                    struct device_node *hog)
0720 {
0721     struct gpio_desc *desc;
0722 
0723     for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED)
0724         if (desc->hog == hog)
0725             gpiochip_free_own_desc(desc);
0726 }
0727 
0728 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
0729 {
0730     return device_match_of_node(&chip->gpiodev->dev, data);
0731 }
0732 
0733 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
0734 {
0735     return gpiochip_find(np, of_gpiochip_match_node);
0736 }
0737 
0738 static int of_gpio_notify(struct notifier_block *nb, unsigned long action,
0739               void *arg)
0740 {
0741     struct of_reconfig_data *rd = arg;
0742     struct gpio_chip *chip;
0743     int ret;
0744 
0745     /*
0746      * This only supports adding and removing complete gpio-hog nodes.
0747      * Modifying an existing gpio-hog node is not supported (except for
0748      * changing its "status" property, which is treated the same as
0749      * addition/removal).
0750      */
0751     switch (of_reconfig_get_state_change(action, arg)) {
0752     case OF_RECONFIG_CHANGE_ADD:
0753         if (!of_property_read_bool(rd->dn, "gpio-hog"))
0754             return NOTIFY_OK;   /* not for us */
0755 
0756         if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
0757             return NOTIFY_OK;
0758 
0759         chip = of_find_gpiochip_by_node(rd->dn->parent);
0760         if (chip == NULL)
0761             return NOTIFY_OK;   /* not for us */
0762 
0763         ret = of_gpiochip_add_hog(chip, rd->dn);
0764         if (ret < 0) {
0765             pr_err("%s: failed to add hogs for %pOF\n", __func__,
0766                    rd->dn);
0767             of_node_clear_flag(rd->dn, OF_POPULATED);
0768             return notifier_from_errno(ret);
0769         }
0770         break;
0771 
0772     case OF_RECONFIG_CHANGE_REMOVE:
0773         if (!of_node_check_flag(rd->dn, OF_POPULATED))
0774             return NOTIFY_OK;   /* already depopulated */
0775 
0776         chip = of_find_gpiochip_by_node(rd->dn->parent);
0777         if (chip == NULL)
0778             return NOTIFY_OK;   /* not for us */
0779 
0780         of_gpiochip_remove_hog(chip, rd->dn);
0781         of_node_clear_flag(rd->dn, OF_POPULATED);
0782         break;
0783     }
0784 
0785     return NOTIFY_OK;
0786 }
0787 
0788 struct notifier_block gpio_of_notifier = {
0789     .notifier_call = of_gpio_notify,
0790 };
0791 #endif /* CONFIG_OF_DYNAMIC */
0792 
0793 /**
0794  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
0795  * @gc:     pointer to the gpio_chip structure
0796  * @gpiospec:   GPIO specifier as found in the device tree
0797  * @flags:  a flags pointer to fill in
0798  *
0799  * This is simple translation function, suitable for the most 1:1 mapped
0800  * GPIO chips. This function performs only one sanity check: whether GPIO
0801  * is less than ngpios (that is specified in the gpio_chip).
0802  */
0803 static int of_gpio_simple_xlate(struct gpio_chip *gc,
0804                 const struct of_phandle_args *gpiospec,
0805                 u32 *flags)
0806 {
0807     /*
0808      * We're discouraging gpio_cells < 2, since that way you'll have to
0809      * write your own xlate function (that will have to retrieve the GPIO
0810      * number and the flags from a single gpio cell -- this is possible,
0811      * but not recommended).
0812      */
0813     if (gc->of_gpio_n_cells < 2) {
0814         WARN_ON(1);
0815         return -EINVAL;
0816     }
0817 
0818     if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
0819         return -EINVAL;
0820 
0821     if (gpiospec->args[0] >= gc->ngpio)
0822         return -EINVAL;
0823 
0824     if (flags)
0825         *flags = gpiospec->args[1];
0826 
0827     return gpiospec->args[0];
0828 }
0829 
0830 /**
0831  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
0832  * @np:     device node of the GPIO chip
0833  * @mm_gc:  pointer to the of_mm_gpio_chip allocated structure
0834  * @data:   driver data to store in the struct gpio_chip
0835  *
0836  * To use this function you should allocate and fill mm_gc with:
0837  *
0838  * 1) In the gpio_chip structure:
0839  *    - all the callbacks
0840  *    - of_gpio_n_cells
0841  *    - of_xlate callback (optional)
0842  *
0843  * 3) In the of_mm_gpio_chip structure:
0844  *    - save_regs callback (optional)
0845  *
0846  * If succeeded, this function will map bank's memory and will
0847  * do all necessary work for you. Then you'll able to use .regs
0848  * to manage GPIOs from the callbacks.
0849  */
0850 int of_mm_gpiochip_add_data(struct device_node *np,
0851                 struct of_mm_gpio_chip *mm_gc,
0852                 void *data)
0853 {
0854     int ret = -ENOMEM;
0855     struct gpio_chip *gc = &mm_gc->gc;
0856 
0857     gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
0858     if (!gc->label)
0859         goto err0;
0860 
0861     mm_gc->regs = of_iomap(np, 0);
0862     if (!mm_gc->regs)
0863         goto err1;
0864 
0865     gc->base = -1;
0866 
0867     if (mm_gc->save_regs)
0868         mm_gc->save_regs(mm_gc);
0869 
0870     of_node_put(mm_gc->gc.of_node);
0871     mm_gc->gc.of_node = of_node_get(np);
0872 
0873     ret = gpiochip_add_data(gc, data);
0874     if (ret)
0875         goto err2;
0876 
0877     return 0;
0878 err2:
0879     of_node_put(np);
0880     iounmap(mm_gc->regs);
0881 err1:
0882     kfree(gc->label);
0883 err0:
0884     pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
0885     return ret;
0886 }
0887 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
0888 
0889 /**
0890  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
0891  * @mm_gc:  pointer to the of_mm_gpio_chip allocated structure
0892  */
0893 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
0894 {
0895     struct gpio_chip *gc = &mm_gc->gc;
0896 
0897     if (!mm_gc)
0898         return;
0899 
0900     gpiochip_remove(gc);
0901     iounmap(mm_gc->regs);
0902     kfree(gc->label);
0903 }
0904 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
0905 
0906 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
0907 {
0908     int len, i;
0909     u32 start, count;
0910     struct device_node *np = chip->of_node;
0911 
0912     len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
0913     if (len < 0 || len % 2 != 0)
0914         return;
0915 
0916     for (i = 0; i < len; i += 2) {
0917         of_property_read_u32_index(np, "gpio-reserved-ranges",
0918                        i, &start);
0919         of_property_read_u32_index(np, "gpio-reserved-ranges",
0920                        i + 1, &count);
0921         if (start >= chip->ngpio || start + count > chip->ngpio)
0922             continue;
0923 
0924         bitmap_clear(chip->valid_mask, start, count);
0925     }
0926 };
0927 
0928 #ifdef CONFIG_PINCTRL
0929 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
0930 {
0931     struct device_node *np = chip->of_node;
0932     struct of_phandle_args pinspec;
0933     struct pinctrl_dev *pctldev;
0934     int index = 0, ret;
0935     const char *name;
0936     static const char group_names_propname[] = "gpio-ranges-group-names";
0937     struct property *group_names;
0938 
0939     if (!np)
0940         return 0;
0941 
0942     if (!of_property_read_bool(np, "gpio-ranges") &&
0943         chip->of_gpio_ranges_fallback) {
0944         return chip->of_gpio_ranges_fallback(chip, np);
0945     }
0946 
0947     group_names = of_find_property(np, group_names_propname, NULL);
0948 
0949     for (;; index++) {
0950         ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
0951                 index, &pinspec);
0952         if (ret)
0953             break;
0954 
0955         pctldev = of_pinctrl_get(pinspec.np);
0956         of_node_put(pinspec.np);
0957         if (!pctldev)
0958             return -EPROBE_DEFER;
0959 
0960         if (pinspec.args[2]) {
0961             if (group_names) {
0962                 of_property_read_string_index(np,
0963                         group_names_propname,
0964                         index, &name);
0965                 if (strlen(name)) {
0966                     pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
0967                         np);
0968                     break;
0969                 }
0970             }
0971             /* npins != 0: linear range */
0972             ret = gpiochip_add_pin_range(chip,
0973                     pinctrl_dev_get_devname(pctldev),
0974                     pinspec.args[0],
0975                     pinspec.args[1],
0976                     pinspec.args[2]);
0977             if (ret)
0978                 return ret;
0979         } else {
0980             /* npins == 0: special range */
0981             if (pinspec.args[1]) {
0982                 pr_err("%pOF: Illegal gpio-range format.\n",
0983                     np);
0984                 break;
0985             }
0986 
0987             if (!group_names) {
0988                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
0989                     np, group_names_propname);
0990                 break;
0991             }
0992 
0993             ret = of_property_read_string_index(np,
0994                         group_names_propname,
0995                         index, &name);
0996             if (ret)
0997                 break;
0998 
0999             if (!strlen(name)) {
1000                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
1001                 np);
1002                 break;
1003             }
1004 
1005             ret = gpiochip_add_pingroup_range(chip, pctldev,
1006                         pinspec.args[0], name);
1007             if (ret)
1008                 return ret;
1009         }
1010     }
1011 
1012     return 0;
1013 }
1014 
1015 #else
1016 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
1017 #endif
1018 
1019 int of_gpiochip_add(struct gpio_chip *chip)
1020 {
1021     int ret;
1022 
1023     if (!chip->of_node)
1024         return 0;
1025 
1026     if (!chip->of_xlate) {
1027         chip->of_gpio_n_cells = 2;
1028         chip->of_xlate = of_gpio_simple_xlate;
1029     }
1030 
1031     if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
1032         return -EINVAL;
1033 
1034     of_gpiochip_init_valid_mask(chip);
1035 
1036     ret = of_gpiochip_add_pin_range(chip);
1037     if (ret)
1038         return ret;
1039 
1040     of_node_get(chip->of_node);
1041 
1042     ret = of_gpiochip_scan_gpios(chip);
1043     if (ret)
1044         of_node_put(chip->of_node);
1045 
1046     return ret;
1047 }
1048 
1049 void of_gpiochip_remove(struct gpio_chip *chip)
1050 {
1051     of_node_put(chip->of_node);
1052 }
1053 
1054 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
1055 {
1056     /* Set default OF node to parent's one if present */
1057     if (gc->parent)
1058         gdev->dev.of_node = gc->parent->of_node;
1059 
1060     if (gc->fwnode)
1061         gc->of_node = to_of_node(gc->fwnode);
1062 
1063     /* If the gpiochip has an assigned OF node this takes precedence */
1064     if (gc->of_node)
1065         gdev->dev.of_node = gc->of_node;
1066     else
1067         gc->of_node = gdev->dev.of_node;
1068 }