LCOV - code coverage report
Current view: top level - drivers/gpio - gpiolib-of.c (source / functions) Hit Total Coverage
Test: Real Lines: 120 295 40.7 %
Date: 2020-10-17 15:46:16 Functions: 0 23 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0+
       2                 :            : /*
       3                 :            :  * OF helpers for the GPIO API
       4                 :            :  *
       5                 :            :  * Copyright (c) 2007-2008  MontaVista Software, Inc.
       6                 :            :  *
       7                 :            :  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
       8                 :            :  */
       9                 :            : 
      10                 :            : #include <linux/device.h>
      11                 :            : #include <linux/err.h>
      12                 :            : #include <linux/errno.h>
      13                 :            : #include <linux/module.h>
      14                 :            : #include <linux/io.h>
      15                 :            : #include <linux/gpio/consumer.h>
      16                 :            : #include <linux/of.h>
      17                 :            : #include <linux/of_address.h>
      18                 :            : #include <linux/of_gpio.h>
      19                 :            : #include <linux/pinctrl/pinctrl.h>
      20                 :            : #include <linux/slab.h>
      21                 :            : #include <linux/gpio/machine.h>
      22                 :            : 
      23                 :            : #include "gpiolib.h"
      24                 :            : #include "gpiolib-of.h"
      25                 :            : 
      26                 :            : /**
      27                 :            :  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
      28                 :            :  * Some elder GPIO controllers need special quirks. Currently we handle
      29                 :            :  * the Freescale GPIO controller with bindings that doesn't use the
      30                 :            :  * established "cs-gpios" for chip selects but instead rely on
      31                 :            :  * "gpios" for the chip select lines. If we detect this, we redirect
      32                 :            :  * the counting of "cs-gpios" to count "gpios" transparent to the
      33                 :            :  * driver.
      34                 :            :  */
      35                 :          0 : int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
      36                 :            : {
      37                 :          0 :         struct device_node *np = dev->of_node;
      38                 :            : 
      39                 :            :         if (!IS_ENABLED(CONFIG_SPI_MASTER))
      40                 :            :                 return 0;
      41                 :          0 :         if (!con_id || strcmp(con_id, "cs"))
      42                 :            :                 return 0;
      43                 :          0 :         if (!of_device_is_compatible(np, "fsl,spi") &&
      44                 :          0 :             !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
      45                 :            :                 return 0;
      46                 :          0 :         return of_gpio_named_count(np, "gpios");
      47                 :            : }
      48                 :            : 
      49                 :            : /*
      50                 :            :  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
      51                 :            :  *
      52                 :            :  * FIXME: get rid of those external users by converting them to GPIO
      53                 :            :  * descriptors and let them all use gpiod_get_count()
      54                 :            :  */
      55                 :          0 : int of_gpio_get_count(struct device *dev, const char *con_id)
      56                 :            : {
      57                 :            :         int ret;
      58                 :            :         char propname[32];
      59                 :            :         unsigned int i;
      60                 :            : 
      61                 :          0 :         ret = of_gpio_spi_cs_get_count(dev, con_id);
      62                 :          0 :         if (ret > 0)
      63                 :            :                 return ret;
      64                 :            : 
      65                 :          0 :         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
      66                 :          0 :                 if (con_id)
      67                 :          0 :                         snprintf(propname, sizeof(propname), "%s-%s",
      68                 :            :                                  con_id, gpio_suffixes[i]);
      69                 :            :                 else
      70                 :          0 :                         snprintf(propname, sizeof(propname), "%s",
      71                 :            :                                  gpio_suffixes[i]);
      72                 :            : 
      73                 :          0 :                 ret = of_gpio_named_count(dev->of_node, propname);
      74                 :          0 :                 if (ret > 0)
      75                 :            :                         break;
      76                 :            :         }
      77                 :          0 :         return ret ? ret : -ENOENT;
      78                 :            : }
      79                 :            : 
      80                 :          3 : static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
      81                 :            : {
      82                 :            :         struct of_phandle_args *gpiospec = data;
      83                 :            : 
      84                 :          3 :         return chip->gpiodev->dev.of_node == gpiospec->np &&
      85                 :          3 :                                 chip->of_xlate &&
      86                 :          3 :                                 chip->of_xlate(chip, gpiospec, NULL) >= 0;
      87                 :            : }
      88                 :            : 
      89                 :            : static struct gpio_chip *of_find_gpiochip_by_xlate(
      90                 :            :                                         struct of_phandle_args *gpiospec)
      91                 :            : {
      92                 :          3 :         return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
      93                 :            : }
      94                 :            : 
      95                 :          3 : static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
      96                 :            :                                         struct of_phandle_args *gpiospec,
      97                 :            :                                         enum of_gpio_flags *flags)
      98                 :            : {
      99                 :            :         int ret;
     100                 :            : 
     101                 :          3 :         if (chip->of_gpio_n_cells != gpiospec->args_count)
     102                 :            :                 return ERR_PTR(-EINVAL);
     103                 :            : 
     104                 :          3 :         ret = chip->of_xlate(chip, gpiospec, flags);
     105                 :          3 :         if (ret < 0)
     106                 :          0 :                 return ERR_PTR(ret);
     107                 :            : 
     108                 :          3 :         return gpiochip_get_desc(chip, ret);
     109                 :            : }
     110                 :            : 
     111                 :            : /**
     112                 :            :  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
     113                 :            :  * to set the .valid_mask
     114                 :            :  * @dev: the device for the GPIO provider
     115                 :            :  * @return: true if the valid mask needs to be set
     116                 :            :  */
     117                 :          3 : bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
     118                 :            : {
     119                 :            :         int size;
     120                 :          3 :         struct device_node *np = gc->of_node;
     121                 :            : 
     122                 :            :         size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
     123                 :          3 :         if (size > 0 && size % 2 == 0)
     124                 :            :                 return true;
     125                 :          3 :         return false;
     126                 :            : }
     127                 :            : 
     128                 :          3 : static void of_gpio_flags_quirks(struct device_node *np,
     129                 :            :                                  const char *propname,
     130                 :            :                                  enum of_gpio_flags *flags,
     131                 :            :                                  int index)
     132                 :            : {
     133                 :            :         /*
     134                 :            :          * Handle MMC "cd-inverted" and "wp-inverted" semantics.
     135                 :            :          */
     136                 :            :         if (IS_ENABLED(CONFIG_MMC)) {
     137                 :            :                 /*
     138                 :            :                  * Active low is the default according to the
     139                 :            :                  * SDHCI specification and the device tree
     140                 :            :                  * bindings. However the code in the current
     141                 :            :                  * kernel was written such that the phandle
     142                 :            :                  * flags were always respected, and "cd-inverted"
     143                 :            :                  * would invert the flag from the device phandle.
     144                 :            :                  */
     145                 :          3 :                 if (!strcmp(propname, "cd-gpios")) {
     146                 :          0 :                         if (of_property_read_bool(np, "cd-inverted"))
     147                 :          0 :                                 *flags ^= OF_GPIO_ACTIVE_LOW;
     148                 :            :                 }
     149                 :            :         }
     150                 :            :         /*
     151                 :            :          * Some GPIO fixed regulator quirks.
     152                 :            :          * Note that active low is the default.
     153                 :            :          */
     154                 :          3 :         if (IS_ENABLED(CONFIG_REGULATOR) &&
     155                 :          3 :             (of_device_is_compatible(np, "regulator-fixed") ||
     156                 :          3 :              of_device_is_compatible(np, "reg-fixed-voltage") ||
     157                 :          3 :              (!(strcmp(propname, "enable-gpio") &&
     158                 :          3 :                 strcmp(propname, "enable-gpios")) &&
     159                 :          0 :               of_device_is_compatible(np, "regulator-gpio")))) {
     160                 :            :                 /*
     161                 :            :                  * The regulator GPIO handles are specified such that the
     162                 :            :                  * presence or absence of "enable-active-high" solely controls
     163                 :            :                  * the polarity of the GPIO line. Any phandle flags must
     164                 :            :                  * be actively ignored.
     165                 :            :                  */
     166                 :          0 :                 if (*flags & OF_GPIO_ACTIVE_LOW) {
     167                 :          0 :                         pr_warn("%s GPIO handle specifies active low - ignored\n",
     168                 :            :                                 of_node_full_name(np));
     169                 :          0 :                         *flags &= ~OF_GPIO_ACTIVE_LOW;
     170                 :            :                 }
     171                 :          0 :                 if (!of_property_read_bool(np, "enable-active-high"))
     172                 :          0 :                         *flags |= OF_GPIO_ACTIVE_LOW;
     173                 :            :         }
     174                 :            :         /*
     175                 :            :          * Legacy open drain handling for fixed voltage regulators.
     176                 :            :          */
     177                 :          3 :         if (IS_ENABLED(CONFIG_REGULATOR) &&
     178                 :          3 :             of_device_is_compatible(np, "reg-fixed-voltage") &&
     179                 :            :             of_property_read_bool(np, "gpio-open-drain")) {
     180                 :          0 :                 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
     181                 :          0 :                 pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
     182                 :            :                         of_node_full_name(np));
     183                 :            :         }
     184                 :            : 
     185                 :            :         /*
     186                 :            :          * Legacy handling of SPI active high chip select. If we have a
     187                 :            :          * property named "cs-gpios" we need to inspect the child node
     188                 :            :          * to determine if the flags should have inverted semantics.
     189                 :            :          */
     190                 :          3 :         if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
     191                 :            :             of_property_read_bool(np, "cs-gpios")) {
     192                 :            :                 struct device_node *child;
     193                 :            :                 u32 cs;
     194                 :            :                 int ret;
     195                 :            : 
     196                 :          0 :                 for_each_child_of_node(np, child) {
     197                 :            :                         ret = of_property_read_u32(child, "reg", &cs);
     198                 :          0 :                         if (ret)
     199                 :          0 :                                 continue;
     200                 :          0 :                         if (cs == index) {
     201                 :            :                                 /*
     202                 :            :                                  * SPI children have active low chip selects
     203                 :            :                                  * by default. This can be specified negatively
     204                 :            :                                  * by just omitting "spi-cs-high" in the
     205                 :            :                                  * device node, or actively by tagging on
     206                 :            :                                  * GPIO_ACTIVE_LOW as flag in the device
     207                 :            :                                  * tree. If the line is simultaneously
     208                 :            :                                  * tagged as active low in the device tree
     209                 :            :                                  * and has the "spi-cs-high" set, we get a
     210                 :            :                                  * conflict and the "spi-cs-high" flag will
     211                 :            :                                  * take precedence.
     212                 :            :                                  */
     213                 :          0 :                                 if (of_property_read_bool(child, "spi-cs-high")) {
     214                 :          0 :                                         if (*flags & OF_GPIO_ACTIVE_LOW) {
     215                 :          0 :                                                 pr_warn("%s GPIO handle specifies active low - ignored\n",
     216                 :            :                                                         of_node_full_name(child));
     217                 :          0 :                                                 *flags &= ~OF_GPIO_ACTIVE_LOW;
     218                 :            :                                         }
     219                 :            :                                 } else {
     220                 :          0 :                                         if (!(*flags & OF_GPIO_ACTIVE_LOW))
     221                 :          0 :                                                 pr_info("%s enforce active low on chipselect handle\n",
     222                 :            :                                                         of_node_full_name(child));
     223                 :          0 :                                         *flags |= OF_GPIO_ACTIVE_LOW;
     224                 :            :                                 }
     225                 :          0 :                                 of_node_put(child);
     226                 :          0 :                                 break;
     227                 :            :                         }
     228                 :            :                 }
     229                 :            :         }
     230                 :            : 
     231                 :            :         /* Legacy handling of stmmac's active-low PHY reset line */
     232                 :            :         if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
     233                 :            :             !strcmp(propname, "snps,reset-gpio") &&
     234                 :            :             of_property_read_bool(np, "snps,reset-active-low"))
     235                 :            :                 *flags |= OF_GPIO_ACTIVE_LOW;
     236                 :          3 : }
     237                 :            : 
     238                 :            : /**
     239                 :            :  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
     240                 :            :  * @np:         device node to get GPIO from
     241                 :            :  * @propname:   property name containing gpio specifier(s)
     242                 :            :  * @index:      index of the GPIO
     243                 :            :  * @flags:      a flags pointer to fill in
     244                 :            :  *
     245                 :            :  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
     246                 :            :  * value on the error condition. If @flags is not NULL the function also fills
     247                 :            :  * in flags for the GPIO.
     248                 :            :  */
     249                 :          3 : static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
     250                 :            :                      const char *propname, int index, enum of_gpio_flags *flags)
     251                 :            : {
     252                 :            :         struct of_phandle_args gpiospec;
     253                 :            :         struct gpio_chip *chip;
     254                 :            :         struct gpio_desc *desc;
     255                 :            :         int ret;
     256                 :            : 
     257                 :          3 :         ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
     258                 :            :                                              &gpiospec);
     259                 :          3 :         if (ret) {
     260                 :            :                 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
     261                 :            :                         __func__, propname, np, index);
     262                 :          3 :                 return ERR_PTR(ret);
     263                 :            :         }
     264                 :            : 
     265                 :            :         chip = of_find_gpiochip_by_xlate(&gpiospec);
     266                 :          3 :         if (!chip) {
     267                 :            :                 desc = ERR_PTR(-EPROBE_DEFER);
     268                 :            :                 goto out;
     269                 :            :         }
     270                 :            : 
     271                 :          3 :         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
     272                 :          3 :         if (IS_ERR(desc))
     273                 :            :                 goto out;
     274                 :            : 
     275                 :          3 :         if (flags)
     276                 :          3 :                 of_gpio_flags_quirks(np, propname, flags, index);
     277                 :            : 
     278                 :            :         pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
     279                 :            :                  __func__, propname, np, index,
     280                 :            :                  PTR_ERR_OR_ZERO(desc));
     281                 :            : 
     282                 :            : out:
     283                 :          3 :         of_node_put(gpiospec.np);
     284                 :            : 
     285                 :          3 :         return desc;
     286                 :            : }
     287                 :            : 
     288                 :          0 : int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
     289                 :            :                             int index, enum of_gpio_flags *flags)
     290                 :            : {
     291                 :            :         struct gpio_desc *desc;
     292                 :            : 
     293                 :          0 :         desc = of_get_named_gpiod_flags(np, list_name, index, flags);
     294                 :            : 
     295                 :          0 :         if (IS_ERR(desc))
     296                 :          0 :                 return PTR_ERR(desc);
     297                 :            :         else
     298                 :          0 :                 return desc_to_gpio(desc);
     299                 :            : }
     300                 :            : EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
     301                 :            : 
     302                 :            : /**
     303                 :            :  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
     304                 :            :  * @node:       handle of the OF node
     305                 :            :  * @propname:   name of the DT property representing the GPIO
     306                 :            :  * @index:      index of the GPIO to obtain for the consumer
     307                 :            :  * @dflags:     GPIO initialization flags
     308                 :            :  * @label:      label to attach to the requested GPIO
     309                 :            :  *
     310                 :            :  * Returns:
     311                 :            :  * On successful request the GPIO pin is configured in accordance with
     312                 :            :  * provided @dflags.
     313                 :            :  *
     314                 :            :  * In case of error an ERR_PTR() is returned.
     315                 :            :  */
     316                 :          3 : struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
     317                 :            :                                          const char *propname, int index,
     318                 :            :                                          enum gpiod_flags dflags,
     319                 :            :                                          const char *label)
     320                 :            : {
     321                 :            :         unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
     322                 :            :         struct gpio_desc *desc;
     323                 :            :         enum of_gpio_flags flags;
     324                 :            :         bool active_low = false;
     325                 :            :         bool single_ended = false;
     326                 :            :         bool open_drain = false;
     327                 :            :         bool transitory = false;
     328                 :            :         int ret;
     329                 :            : 
     330                 :          3 :         desc = of_get_named_gpiod_flags(node, propname,
     331                 :            :                                         index, &flags);
     332                 :            : 
     333                 :          3 :         if (!desc || IS_ERR(desc)) {
     334                 :            :                 return desc;
     335                 :            :         }
     336                 :            : 
     337                 :          3 :         active_low = flags & OF_GPIO_ACTIVE_LOW;
     338                 :          3 :         single_ended = flags & OF_GPIO_SINGLE_ENDED;
     339                 :          3 :         open_drain = flags & OF_GPIO_OPEN_DRAIN;
     340                 :          3 :         transitory = flags & OF_GPIO_TRANSITORY;
     341                 :            : 
     342                 :          3 :         ret = gpiod_request(desc, label);
     343                 :          3 :         if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
     344                 :            :                 return desc;
     345                 :          3 :         if (ret)
     346                 :          0 :                 return ERR_PTR(ret);
     347                 :            : 
     348                 :          3 :         if (active_low)
     349                 :            :                 lflags |= GPIO_ACTIVE_LOW;
     350                 :            : 
     351                 :          3 :         if (single_ended) {
     352                 :          0 :                 if (open_drain)
     353                 :          0 :                         lflags |= GPIO_OPEN_DRAIN;
     354                 :            :                 else
     355                 :          0 :                         lflags |= GPIO_OPEN_SOURCE;
     356                 :            :         }
     357                 :            : 
     358                 :          3 :         if (transitory)
     359                 :          0 :                 lflags |= GPIO_TRANSITORY;
     360                 :            : 
     361                 :          3 :         ret = gpiod_configure_flags(desc, propname, lflags, dflags);
     362                 :          3 :         if (ret < 0) {
     363                 :          0 :                 gpiod_put(desc);
     364                 :          0 :                 return ERR_PTR(ret);
     365                 :            :         }
     366                 :            : 
     367                 :            :         return desc;
     368                 :            : }
     369                 :            : EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
     370                 :            : 
     371                 :            : /*
     372                 :            :  * The SPI GPIO bindings happened before we managed to establish that GPIO
     373                 :            :  * properties should be named "foo-gpios" so we have this special kludge for
     374                 :            :  * them.
     375                 :            :  */
     376                 :          3 : static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
     377                 :            :                                           enum of_gpio_flags *of_flags)
     378                 :            : {
     379                 :            :         char prop_name[32]; /* 32 is max size of property name */
     380                 :          3 :         struct device_node *np = dev->of_node;
     381                 :            :         struct gpio_desc *desc;
     382                 :            : 
     383                 :            :         /*
     384                 :            :          * Hopefully the compiler stubs the rest of the function if this
     385                 :            :          * is false.
     386                 :            :          */
     387                 :            :         if (!IS_ENABLED(CONFIG_SPI_MASTER))
     388                 :            :                 return ERR_PTR(-ENOENT);
     389                 :            : 
     390                 :            :         /* Allow this specifically for "spi-gpio" devices */
     391                 :          3 :         if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
     392                 :            :                 return ERR_PTR(-ENOENT);
     393                 :            : 
     394                 :            :         /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
     395                 :          0 :         snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
     396                 :            : 
     397                 :          0 :         desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
     398                 :          0 :         return desc;
     399                 :            : }
     400                 :            : 
     401                 :            : /*
     402                 :            :  * The old Freescale bindings use simply "gpios" as name for the chip select
     403                 :            :  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
     404                 :            :  * with a special quirk.
     405                 :            :  */
     406                 :          3 : static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
     407                 :            :                                              const char *con_id,
     408                 :            :                                              unsigned int idx,
     409                 :            :                                              unsigned long *flags)
     410                 :            : {
     411                 :          3 :         struct device_node *np = dev->of_node;
     412                 :            : 
     413                 :            :         if (!IS_ENABLED(CONFIG_SPI_MASTER))
     414                 :            :                 return ERR_PTR(-ENOENT);
     415                 :            : 
     416                 :            :         /* Allow this specifically for Freescale devices */
     417                 :          3 :         if (!of_device_is_compatible(np, "fsl,spi") &&
     418                 :          3 :             !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
     419                 :            :                 return ERR_PTR(-ENOENT);
     420                 :            :         /* Allow only if asking for "cs-gpios" */
     421                 :          0 :         if (!con_id || strcmp(con_id, "cs"))
     422                 :            :                 return ERR_PTR(-ENOENT);
     423                 :            : 
     424                 :            :         /*
     425                 :            :          * While all other SPI controllers use "cs-gpios" the Freescale
     426                 :            :          * uses just "gpios" so translate to that when "cs-gpios" is
     427                 :            :          * requested.
     428                 :            :          */
     429                 :          0 :         return of_find_gpio(dev, NULL, idx, flags);
     430                 :            : }
     431                 :            : 
     432                 :            : /*
     433                 :            :  * Some regulator bindings happened before we managed to establish that GPIO
     434                 :            :  * properties should be named "foo-gpios" so we have this special kludge for
     435                 :            :  * them.
     436                 :            :  */
     437                 :          3 : static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
     438                 :            :                                                 enum of_gpio_flags *of_flags)
     439                 :            : {
     440                 :            :         /* These are the connection IDs we accept as legacy GPIO phandles */
     441                 :          3 :         const char *whitelist[] = {
     442                 :            :                 "wlf,ldoena", /* Arizona */
     443                 :            :                 "wlf,ldo1ena", /* WM8994 */
     444                 :            :                 "wlf,ldo2ena", /* WM8994 */
     445                 :            :         };
     446                 :          3 :         struct device_node *np = dev->of_node;
     447                 :            :         struct gpio_desc *desc;
     448                 :            :         int i;
     449                 :            : 
     450                 :            :         if (!IS_ENABLED(CONFIG_REGULATOR))
     451                 :            :                 return ERR_PTR(-ENOENT);
     452                 :            : 
     453                 :          3 :         if (!con_id)
     454                 :            :                 return ERR_PTR(-ENOENT);
     455                 :            : 
     456                 :          3 :         i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
     457                 :          3 :         if (i < 0)
     458                 :            :                 return ERR_PTR(-ENOENT);
     459                 :            : 
     460                 :          0 :         desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
     461                 :          0 :         return desc;
     462                 :            : }
     463                 :            : 
     464                 :          3 : static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
     465                 :            :                                               const char *con_id,
     466                 :            :                                               enum of_gpio_flags *of_flags)
     467                 :            : {
     468                 :            :         if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
     469                 :            :                 return ERR_PTR(-ENOENT);
     470                 :            : 
     471                 :          3 :         if (!con_id || strcmp(con_id, "wlf,reset"))
     472                 :            :                 return ERR_PTR(-ENOENT);
     473                 :            : 
     474                 :          0 :         return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
     475                 :            : }
     476                 :            : 
     477                 :          3 : struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
     478                 :            :                                unsigned int idx, unsigned long *flags)
     479                 :            : {
     480                 :            :         char prop_name[32]; /* 32 is max size of property name */
     481                 :            :         enum of_gpio_flags of_flags;
     482                 :            :         struct gpio_desc *desc;
     483                 :            :         unsigned int i;
     484                 :            : 
     485                 :            :         /* Try GPIO property "foo-gpios" and "foo-gpio" */
     486                 :          3 :         for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
     487                 :          3 :                 if (con_id)
     488                 :          3 :                         snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
     489                 :            :                                  gpio_suffixes[i]);
     490                 :            :                 else
     491                 :          3 :                         snprintf(prop_name, sizeof(prop_name), "%s",
     492                 :            :                                  gpio_suffixes[i]);
     493                 :            : 
     494                 :          3 :                 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
     495                 :            :                                                 &of_flags);
     496                 :            : 
     497                 :          3 :                 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
     498                 :            :                         break;
     499                 :            :         }
     500                 :            : 
     501                 :          3 :         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
     502                 :            :                 /* Special handling for SPI GPIOs if used */
     503                 :          3 :                 desc = of_find_spi_gpio(dev, con_id, &of_flags);
     504                 :            :         }
     505                 :            : 
     506                 :          3 :         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
     507                 :            :                 /* This quirk looks up flags and all */
     508                 :          3 :                 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
     509                 :          3 :                 if (!IS_ERR(desc))
     510                 :            :                         return desc;
     511                 :            :         }
     512                 :            : 
     513                 :          3 :         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
     514                 :            :                 /* Special handling for regulator GPIOs if used */
     515                 :          3 :                 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
     516                 :            :         }
     517                 :            : 
     518                 :          3 :         if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
     519                 :          3 :                 desc = of_find_arizona_gpio(dev, con_id, &of_flags);
     520                 :            : 
     521                 :          3 :         if (IS_ERR(desc))
     522                 :            :                 return desc;
     523                 :            : 
     524                 :          0 :         if (of_flags & OF_GPIO_ACTIVE_LOW)
     525                 :          0 :                 *flags |= GPIO_ACTIVE_LOW;
     526                 :            : 
     527                 :          0 :         if (of_flags & OF_GPIO_SINGLE_ENDED) {
     528                 :          0 :                 if (of_flags & OF_GPIO_OPEN_DRAIN)
     529                 :          0 :                         *flags |= GPIO_OPEN_DRAIN;
     530                 :            :                 else
     531                 :          0 :                         *flags |= GPIO_OPEN_SOURCE;
     532                 :            :         }
     533                 :            : 
     534                 :          0 :         if (of_flags & OF_GPIO_TRANSITORY)
     535                 :          0 :                 *flags |= GPIO_TRANSITORY;
     536                 :            : 
     537                 :          0 :         if (of_flags & OF_GPIO_PULL_UP)
     538                 :          0 :                 *flags |= GPIO_PULL_UP;
     539                 :          0 :         if (of_flags & OF_GPIO_PULL_DOWN)
     540                 :          0 :                 *flags |= GPIO_PULL_DOWN;
     541                 :            : 
     542                 :          0 :         return desc;
     543                 :            : }
     544                 :            : 
     545                 :            : /**
     546                 :            :  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
     547                 :            :  * @np:         device node to get GPIO from
     548                 :            :  * @chip:       GPIO chip whose hog is parsed
     549                 :            :  * @idx:        Index of the GPIO to parse
     550                 :            :  * @name:       GPIO line name
     551                 :            :  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
     552                 :            :  *              of_find_gpio() or of_parse_own_gpio()
     553                 :            :  * @dflags:     gpiod_flags - optional GPIO initialization flags
     554                 :            :  *
     555                 :            :  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
     556                 :            :  * value on the error condition.
     557                 :            :  */
     558                 :          0 : static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
     559                 :            :                                            struct gpio_chip *chip,
     560                 :            :                                            unsigned int idx, const char **name,
     561                 :            :                                            unsigned long *lflags,
     562                 :            :                                            enum gpiod_flags *dflags)
     563                 :            : {
     564                 :            :         struct device_node *chip_np;
     565                 :            :         enum of_gpio_flags xlate_flags;
     566                 :            :         struct of_phandle_args gpiospec;
     567                 :            :         struct gpio_desc *desc;
     568                 :            :         unsigned int i;
     569                 :            :         u32 tmp;
     570                 :            :         int ret;
     571                 :            : 
     572                 :          0 :         chip_np = chip->of_node;
     573                 :          0 :         if (!chip_np)
     574                 :            :                 return ERR_PTR(-EINVAL);
     575                 :            : 
     576                 :          0 :         xlate_flags = 0;
     577                 :          0 :         *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
     578                 :          0 :         *dflags = 0;
     579                 :            : 
     580                 :            :         ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
     581                 :          0 :         if (ret)
     582                 :          0 :                 return ERR_PTR(ret);
     583                 :            : 
     584                 :          0 :         gpiospec.np = chip_np;
     585                 :          0 :         gpiospec.args_count = tmp;
     586                 :            : 
     587                 :          0 :         for (i = 0; i < tmp; i++) {
     588                 :          0 :                 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
     589                 :          0 :                                                  &gpiospec.args[i]);
     590                 :          0 :                 if (ret)
     591                 :          0 :                         return ERR_PTR(ret);
     592                 :            :         }
     593                 :            : 
     594                 :          0 :         desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
     595                 :          0 :         if (IS_ERR(desc))
     596                 :            :                 return desc;
     597                 :            : 
     598                 :          0 :         if (xlate_flags & OF_GPIO_ACTIVE_LOW)
     599                 :          0 :                 *lflags |= GPIO_ACTIVE_LOW;
     600                 :          0 :         if (xlate_flags & OF_GPIO_TRANSITORY)
     601                 :          0 :                 *lflags |= GPIO_TRANSITORY;
     602                 :            : 
     603                 :          0 :         if (of_property_read_bool(np, "input"))
     604                 :          0 :                 *dflags |= GPIOD_IN;
     605                 :          0 :         else if (of_property_read_bool(np, "output-low"))
     606                 :          0 :                 *dflags |= GPIOD_OUT_LOW;
     607                 :          0 :         else if (of_property_read_bool(np, "output-high"))
     608                 :          0 :                 *dflags |= GPIOD_OUT_HIGH;
     609                 :            :         else {
     610                 :          0 :                 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
     611                 :            :                         desc_to_gpio(desc), np);
     612                 :          0 :                 return ERR_PTR(-EINVAL);
     613                 :            :         }
     614                 :            : 
     615                 :          0 :         if (name && of_property_read_string(np, "line-name", name))
     616                 :          0 :                 *name = np->name;
     617                 :            : 
     618                 :          0 :         return desc;
     619                 :            : }
     620                 :            : 
     621                 :            : /**
     622                 :            :  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
     623                 :            :  * @chip:       gpio chip to act on
     624                 :            :  *
     625                 :            :  * This is only used by of_gpiochip_add to request/set GPIO initial
     626                 :            :  * configuration.
     627                 :            :  * It returns error if it fails otherwise 0 on success.
     628                 :            :  */
     629                 :          3 : static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
     630                 :            : {
     631                 :            :         struct gpio_desc *desc = NULL;
     632                 :            :         struct device_node *np;
     633                 :            :         const char *name;
     634                 :            :         unsigned long lflags;
     635                 :            :         enum gpiod_flags dflags;
     636                 :            :         unsigned int i;
     637                 :            :         int ret;
     638                 :            : 
     639                 :          3 :         for_each_available_child_of_node(chip->of_node, np) {
     640                 :          3 :                 if (!of_property_read_bool(np, "gpio-hog"))
     641                 :          3 :                         continue;
     642                 :            : 
     643                 :          0 :                 for (i = 0;; i++) {
     644                 :          0 :                         desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
     645                 :            :                                                  &dflags);
     646                 :          0 :                         if (IS_ERR(desc))
     647                 :            :                                 break;
     648                 :            : 
     649                 :          0 :                         ret = gpiod_hog(desc, name, lflags, dflags);
     650                 :          0 :                         if (ret < 0) {
     651                 :          0 :                                 of_node_put(np);
     652                 :          0 :                                 return ret;
     653                 :            :                         }
     654                 :          0 :                 }
     655                 :            :         }
     656                 :            : 
     657                 :            :         return 0;
     658                 :            : }
     659                 :            : 
     660                 :            : /**
     661                 :            :  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
     662                 :            :  * @gc:         pointer to the gpio_chip structure
     663                 :            :  * @gpiospec:   GPIO specifier as found in the device tree
     664                 :            :  * @flags:      a flags pointer to fill in
     665                 :            :  *
     666                 :            :  * This is simple translation function, suitable for the most 1:1 mapped
     667                 :            :  * GPIO chips. This function performs only one sanity check: whether GPIO
     668                 :            :  * is less than ngpios (that is specified in the gpio_chip).
     669                 :            :  */
     670                 :          3 : static int of_gpio_simple_xlate(struct gpio_chip *gc,
     671                 :            :                                 const struct of_phandle_args *gpiospec,
     672                 :            :                                 u32 *flags)
     673                 :            : {
     674                 :            :         /*
     675                 :            :          * We're discouraging gpio_cells < 2, since that way you'll have to
     676                 :            :          * write your own xlate function (that will have to retrieve the GPIO
     677                 :            :          * number and the flags from a single gpio cell -- this is possible,
     678                 :            :          * but not recommended).
     679                 :            :          */
     680                 :          3 :         if (gc->of_gpio_n_cells < 2) {
     681                 :          0 :                 WARN_ON(1);
     682                 :          0 :                 return -EINVAL;
     683                 :            :         }
     684                 :            : 
     685                 :          3 :         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
     686                 :            :                 return -EINVAL;
     687                 :            : 
     688                 :          3 :         if (gpiospec->args[0] >= gc->ngpio)
     689                 :            :                 return -EINVAL;
     690                 :            : 
     691                 :          3 :         if (flags)
     692                 :          3 :                 *flags = gpiospec->args[1];
     693                 :            : 
     694                 :          3 :         return gpiospec->args[0];
     695                 :            : }
     696                 :            : 
     697                 :            : /**
     698                 :            :  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
     699                 :            :  * @np:         device node of the GPIO chip
     700                 :            :  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
     701                 :            :  * @data:       driver data to store in the struct gpio_chip
     702                 :            :  *
     703                 :            :  * To use this function you should allocate and fill mm_gc with:
     704                 :            :  *
     705                 :            :  * 1) In the gpio_chip structure:
     706                 :            :  *    - all the callbacks
     707                 :            :  *    - of_gpio_n_cells
     708                 :            :  *    - of_xlate callback (optional)
     709                 :            :  *
     710                 :            :  * 3) In the of_mm_gpio_chip structure:
     711                 :            :  *    - save_regs callback (optional)
     712                 :            :  *
     713                 :            :  * If succeeded, this function will map bank's memory and will
     714                 :            :  * do all necessary work for you. Then you'll able to use .regs
     715                 :            :  * to manage GPIOs from the callbacks.
     716                 :            :  */
     717                 :          0 : int of_mm_gpiochip_add_data(struct device_node *np,
     718                 :            :                             struct of_mm_gpio_chip *mm_gc,
     719                 :            :                             void *data)
     720                 :            : {
     721                 :            :         int ret = -ENOMEM;
     722                 :          0 :         struct gpio_chip *gc = &mm_gc->gc;
     723                 :            : 
     724                 :          0 :         gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
     725                 :          0 :         if (!gc->label)
     726                 :            :                 goto err0;
     727                 :            : 
     728                 :          0 :         mm_gc->regs = of_iomap(np, 0);
     729                 :          0 :         if (!mm_gc->regs)
     730                 :            :                 goto err1;
     731                 :            : 
     732                 :          0 :         gc->base = -1;
     733                 :            : 
     734                 :          0 :         if (mm_gc->save_regs)
     735                 :          0 :                 mm_gc->save_regs(mm_gc);
     736                 :            : 
     737                 :          0 :         mm_gc->gc.of_node = np;
     738                 :            : 
     739                 :          0 :         ret = gpiochip_add_data(gc, data);
     740                 :          0 :         if (ret)
     741                 :            :                 goto err2;
     742                 :            : 
     743                 :            :         return 0;
     744                 :            : err2:
     745                 :          0 :         iounmap(mm_gc->regs);
     746                 :            : err1:
     747                 :          0 :         kfree(gc->label);
     748                 :            : err0:
     749                 :          0 :         pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
     750                 :          0 :         return ret;
     751                 :            : }
     752                 :            : EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
     753                 :            : 
     754                 :            : /**
     755                 :            :  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
     756                 :            :  * @mm_gc:      pointer to the of_mm_gpio_chip allocated structure
     757                 :            :  */
     758                 :          0 : void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
     759                 :            : {
     760                 :          0 :         struct gpio_chip *gc = &mm_gc->gc;
     761                 :            : 
     762                 :          0 :         if (!mm_gc)
     763                 :          0 :                 return;
     764                 :            : 
     765                 :          0 :         gpiochip_remove(gc);
     766                 :          0 :         iounmap(mm_gc->regs);
     767                 :          0 :         kfree(gc->label);
     768                 :            : }
     769                 :            : EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
     770                 :            : 
     771                 :          3 : static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
     772                 :            : {
     773                 :            :         int len, i;
     774                 :            :         u32 start, count;
     775                 :          3 :         struct device_node *np = chip->of_node;
     776                 :            : 
     777                 :            :         len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
     778                 :          3 :         if (len < 0 || len % 2 != 0)
     779                 :          3 :                 return;
     780                 :            : 
     781                 :          0 :         for (i = 0; i < len; i += 2) {
     782                 :          0 :                 of_property_read_u32_index(np, "gpio-reserved-ranges",
     783                 :            :                                            i, &start);
     784                 :          0 :                 of_property_read_u32_index(np, "gpio-reserved-ranges",
     785                 :          0 :                                            i + 1, &count);
     786                 :          0 :                 if (start >= chip->ngpio || start + count >= chip->ngpio)
     787                 :          0 :                         continue;
     788                 :            : 
     789                 :          0 :                 bitmap_clear(chip->valid_mask, start, count);
     790                 :            :         }
     791                 :            : };
     792                 :            : 
     793                 :            : #ifdef CONFIG_PINCTRL
     794                 :          3 : static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
     795                 :            : {
     796                 :          3 :         struct device_node *np = chip->of_node;
     797                 :            :         struct of_phandle_args pinspec;
     798                 :            :         struct pinctrl_dev *pctldev;
     799                 :            :         int index = 0, ret;
     800                 :            :         const char *name;
     801                 :            :         static const char group_names_propname[] = "gpio-ranges-group-names";
     802                 :            :         struct property *group_names;
     803                 :            : 
     804                 :          3 :         if (!np)
     805                 :            :                 return 0;
     806                 :            : 
     807                 :          3 :         group_names = of_find_property(np, group_names_propname, NULL);
     808                 :            : 
     809                 :          0 :         for (;; index++) {
     810                 :          3 :                 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
     811                 :            :                                 index, &pinspec);
     812                 :          3 :                 if (ret)
     813                 :            :                         break;
     814                 :            : 
     815                 :          0 :                 pctldev = of_pinctrl_get(pinspec.np);
     816                 :          0 :                 of_node_put(pinspec.np);
     817                 :          0 :                 if (!pctldev)
     818                 :            :                         return -EPROBE_DEFER;
     819                 :            : 
     820                 :          0 :                 if (pinspec.args[2]) {
     821                 :          0 :                         if (group_names) {
     822                 :            :                                 of_property_read_string_index(np,
     823                 :            :                                                 group_names_propname,
     824                 :            :                                                 index, &name);
     825                 :          0 :                                 if (strlen(name)) {
     826                 :          0 :                                         pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
     827                 :            :                                                 np);
     828                 :          0 :                                         break;
     829                 :            :                                 }
     830                 :            :                         }
     831                 :            :                         /* npins != 0: linear range */
     832                 :          0 :                         ret = gpiochip_add_pin_range(chip,
     833                 :            :                                         pinctrl_dev_get_devname(pctldev),
     834                 :            :                                         pinspec.args[0],
     835                 :            :                                         pinspec.args[1],
     836                 :            :                                         pinspec.args[2]);
     837                 :          0 :                         if (ret)
     838                 :          0 :                                 return ret;
     839                 :            :                 } else {
     840                 :            :                         /* npins == 0: special range */
     841                 :          0 :                         if (pinspec.args[1]) {
     842                 :          0 :                                 pr_err("%pOF: Illegal gpio-range format.\n",
     843                 :            :                                         np);
     844                 :          0 :                                 break;
     845                 :            :                         }
     846                 :            : 
     847                 :          0 :                         if (!group_names) {
     848                 :          0 :                                 pr_err("%pOF: GPIO group range requested but no %s property.\n",
     849                 :            :                                         np, group_names_propname);
     850                 :          0 :                                 break;
     851                 :            :                         }
     852                 :            : 
     853                 :            :                         ret = of_property_read_string_index(np,
     854                 :            :                                                 group_names_propname,
     855                 :            :                                                 index, &name);
     856                 :          0 :                         if (ret)
     857                 :            :                                 break;
     858                 :            : 
     859                 :          0 :                         if (!strlen(name)) {
     860                 :          0 :                                 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
     861                 :            :                                 np);
     862                 :          0 :                                 break;
     863                 :            :                         }
     864                 :            : 
     865                 :          0 :                         ret = gpiochip_add_pingroup_range(chip, pctldev,
     866                 :            :                                                 pinspec.args[0], name);
     867                 :          0 :                         if (ret)
     868                 :          0 :                                 return ret;
     869                 :            :                 }
     870                 :          0 :         }
     871                 :            : 
     872                 :            :         return 0;
     873                 :            : }
     874                 :            : 
     875                 :            : #else
     876                 :            : static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
     877                 :            : #endif
     878                 :            : 
     879                 :          3 : int of_gpiochip_add(struct gpio_chip *chip)
     880                 :            : {
     881                 :            :         int ret;
     882                 :            : 
     883                 :          3 :         if (!chip->of_node)
     884                 :            :                 return 0;
     885                 :            : 
     886                 :          3 :         if (!chip->of_xlate) {
     887                 :          3 :                 chip->of_gpio_n_cells = 2;
     888                 :          3 :                 chip->of_xlate = of_gpio_simple_xlate;
     889                 :            :         }
     890                 :            : 
     891                 :          3 :         if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
     892                 :            :                 return -EINVAL;
     893                 :            : 
     894                 :          3 :         of_gpiochip_init_valid_mask(chip);
     895                 :            : 
     896                 :          3 :         ret = of_gpiochip_add_pin_range(chip);
     897                 :          3 :         if (ret)
     898                 :            :                 return ret;
     899                 :            : 
     900                 :            :         /* If the chip defines names itself, these take precedence */
     901                 :          3 :         if (!chip->names)
     902                 :          3 :                 devprop_gpiochip_set_names(chip,
     903                 :          3 :                                            of_fwnode_handle(chip->of_node));
     904                 :            : 
     905                 :          3 :         of_node_get(chip->of_node);
     906                 :            : 
     907                 :          3 :         ret = of_gpiochip_scan_gpios(chip);
     908                 :          3 :         if (ret)
     909                 :          0 :                 of_node_put(chip->of_node);
     910                 :            : 
     911                 :          3 :         return ret;
     912                 :            : }
     913                 :            : 
     914                 :          0 : void of_gpiochip_remove(struct gpio_chip *chip)
     915                 :            : {
     916                 :          0 :         of_node_put(chip->of_node);
     917                 :          0 : }
    

Generated by: LCOV version 1.14