LCOV - code coverage report
Current view: top level - drivers/gpio - gpiolib.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 335 1547 21.7 %
Date: 2020-09-30 20:25:40 Functions: 43 151 28.5 %
Branches: 148 1184 12.5 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : #include <linux/bitmap.h>
       3                 :            : #include <linux/kernel.h>
       4                 :            : #include <linux/module.h>
       5                 :            : #include <linux/interrupt.h>
       6                 :            : #include <linux/irq.h>
       7                 :            : #include <linux/spinlock.h>
       8                 :            : #include <linux/list.h>
       9                 :            : #include <linux/device.h>
      10                 :            : #include <linux/err.h>
      11                 :            : #include <linux/debugfs.h>
      12                 :            : #include <linux/seq_file.h>
      13                 :            : #include <linux/gpio.h>
      14                 :            : #include <linux/idr.h>
      15                 :            : #include <linux/slab.h>
      16                 :            : #include <linux/acpi.h>
      17                 :            : #include <linux/gpio/driver.h>
      18                 :            : #include <linux/gpio/machine.h>
      19                 :            : #include <linux/pinctrl/consumer.h>
      20                 :            : #include <linux/cdev.h>
      21                 :            : #include <linux/fs.h>
      22                 :            : #include <linux/uaccess.h>
      23                 :            : #include <linux/compat.h>
      24                 :            : #include <linux/anon_inodes.h>
      25                 :            : #include <linux/file.h>
      26                 :            : #include <linux/kfifo.h>
      27                 :            : #include <linux/poll.h>
      28                 :            : #include <linux/timekeeping.h>
      29                 :            : #include <uapi/linux/gpio.h>
      30                 :            : 
      31                 :            : #include "gpiolib.h"
      32                 :            : #include "gpiolib-of.h"
      33                 :            : #include "gpiolib-acpi.h"
      34                 :            : 
      35                 :            : #define CREATE_TRACE_POINTS
      36                 :            : #include <trace/events/gpio.h>
      37                 :            : 
      38                 :            : /* Implementation infrastructure for GPIO interfaces.
      39                 :            :  *
      40                 :            :  * The GPIO programming interface allows for inlining speed-critical
      41                 :            :  * get/set operations for common cases, so that access to SOC-integrated
      42                 :            :  * GPIOs can sometimes cost only an instruction or two per bit.
      43                 :            :  */
      44                 :            : 
      45                 :            : 
      46                 :            : /* When debugging, extend minimal trust to callers and platform code.
      47                 :            :  * Also emit diagnostic messages that may help initial bringup, when
      48                 :            :  * board setup or driver bugs are most common.
      49                 :            :  *
      50                 :            :  * Otherwise, minimize overhead in what may be bitbanging codepaths.
      51                 :            :  */
      52                 :            : #ifdef  DEBUG
      53                 :            : #define extra_checks    1
      54                 :            : #else
      55                 :            : #define extra_checks    0
      56                 :            : #endif
      57                 :            : 
      58                 :            : #define dont_test_bit(b,d) (0)
      59                 :            : 
      60                 :            : /* Device and char device-related information */
      61                 :            : static DEFINE_IDA(gpio_ida);
      62                 :            : static dev_t gpio_devt;
      63                 :            : #define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
      64                 :            : static struct bus_type gpio_bus_type = {
      65                 :            :         .name = "gpio",
      66                 :            : };
      67                 :            : 
      68                 :            : /*
      69                 :            :  * Number of GPIOs to use for the fast path in set array
      70                 :            :  */
      71                 :            : #define FASTPATH_NGPIO CONFIG_GPIOLIB_FASTPATH_LIMIT
      72                 :            : 
      73                 :            : /* gpio_lock prevents conflicts during gpio_desc[] table updates.
      74                 :            :  * While any GPIO is requested, its gpio_chip is not removable;
      75                 :            :  * each GPIO's "requested" flag serves as a lock and refcount.
      76                 :            :  */
      77                 :            : DEFINE_SPINLOCK(gpio_lock);
      78                 :            : 
      79                 :            : static DEFINE_MUTEX(gpio_lookup_lock);
      80                 :            : static LIST_HEAD(gpio_lookup_list);
      81                 :            : LIST_HEAD(gpio_devices);
      82                 :            : 
      83                 :            : static DEFINE_MUTEX(gpio_machine_hogs_mutex);
      84                 :            : static LIST_HEAD(gpio_machine_hogs);
      85                 :            : 
      86                 :            : static void gpiochip_free_hogs(struct gpio_chip *chip);
      87                 :            : static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
      88                 :            :                                 struct lock_class_key *lock_key,
      89                 :            :                                 struct lock_class_key *request_key);
      90                 :            : static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
      91                 :            : static int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip);
      92                 :            : static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
      93                 :            : static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
      94                 :            : 
      95                 :            : static bool gpiolib_initialized;
      96                 :            : 
      97                 :            : static inline void desc_set_label(struct gpio_desc *d, const char *label)
      98                 :            : {
      99                 :        828 :         d->label = label;
     100                 :            : }
     101                 :            : 
     102                 :            : /**
     103                 :            :  * gpio_to_desc - Convert a GPIO number to its descriptor
     104                 :            :  * @gpio: global GPIO number
     105                 :            :  *
     106                 :            :  * Returns:
     107                 :            :  * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
     108                 :            :  * with the given number exists in the system.
     109                 :            :  */
     110                 :          0 : struct gpio_desc *gpio_to_desc(unsigned gpio)
     111                 :            : {
     112                 :            :         struct gpio_device *gdev;
     113                 :            :         unsigned long flags;
     114                 :            : 
     115                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
     116                 :            : 
     117         [ #  # ]:          0 :         list_for_each_entry(gdev, &gpio_devices, list) {
     118   [ #  #  #  # ]:          0 :                 if (gdev->base <= gpio &&
     119                 :          0 :                     gdev->base + gdev->ngpio > gpio) {
     120                 :            :                         spin_unlock_irqrestore(&gpio_lock, flags);
     121                 :          0 :                         return &gdev->descs[gpio - gdev->base];
     122                 :            :                 }
     123                 :            :         }
     124                 :            : 
     125                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
     126                 :            : 
     127         [ #  # ]:          0 :         if (!gpio_is_valid(gpio))
     128                 :          0 :                 WARN(1, "invalid GPIO %d\n", gpio);
     129                 :            : 
     130                 :            :         return NULL;
     131                 :            : }
     132                 :            : EXPORT_SYMBOL_GPL(gpio_to_desc);
     133                 :            : 
     134                 :            : /**
     135                 :            :  * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
     136                 :            :  *                     hardware number for this chip
     137                 :            :  * @chip: GPIO chip
     138                 :            :  * @hwnum: hardware number of the GPIO for this chip
     139                 :            :  *
     140                 :            :  * Returns:
     141                 :            :  * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
     142                 :            :  * in the given chip for the specified hardware number.
     143                 :            :  */
     144                 :        414 : struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
     145                 :            :                                     u16 hwnum)
     146                 :            : {
     147                 :        414 :         struct gpio_device *gdev = chip->gpiodev;
     148                 :            : 
     149   [ #  #  #  #  :        414 :         if (hwnum >= gdev->ngpio)
          #  #  #  #  #  
          #  #  #  #  #  
                   +  - ]
     150                 :            :                 return ERR_PTR(-EINVAL);
     151                 :            : 
     152                 :        414 :         return &gdev->descs[hwnum];
     153                 :            : }
     154                 :            : 
     155                 :            : /**
     156                 :            :  * desc_to_gpio - convert a GPIO descriptor to the integer namespace
     157                 :            :  * @desc: GPIO descriptor
     158                 :            :  *
     159                 :            :  * This should disappear in the future but is needed since we still
     160                 :            :  * use GPIO numbers for error messages and sysfs nodes.
     161                 :            :  *
     162                 :            :  * Returns:
     163                 :            :  * The global GPIO number for the GPIO specified by its descriptor.
     164                 :            :  */
     165                 :          0 : int desc_to_gpio(const struct gpio_desc *desc)
     166                 :            : {
     167                 :    2425657 :         return desc->gdev->base + (desc - &desc->gdev->descs[0]);
     168                 :            : }
     169                 :            : EXPORT_SYMBOL_GPL(desc_to_gpio);
     170                 :            : 
     171                 :            : 
     172                 :            : /**
     173                 :            :  * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
     174                 :            :  * @desc:       descriptor to return the chip of
     175                 :            :  */
     176                 :          0 : struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
     177                 :            : {
     178   [ #  #  #  #  :        414 :         if (!desc || !desc->gdev)
          #  #  #  #  +  
          -  +  -  #  #  
                   #  # ]
     179                 :            :                 return NULL;
     180                 :        414 :         return desc->gdev->chip;
     181                 :            : }
     182                 :            : EXPORT_SYMBOL_GPL(gpiod_to_chip);
     183                 :            : 
     184                 :            : /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
     185                 :          0 : static int gpiochip_find_base(int ngpio)
     186                 :            : {
     187                 :            :         struct gpio_device *gdev;
     188                 :          0 :         int base = ARCH_NR_GPIOS - ngpio;
     189                 :            : 
     190         [ #  # ]:          0 :         list_for_each_entry_reverse(gdev, &gpio_devices, list) {
     191                 :            :                 /* found a free space? */
     192         [ #  # ]:          0 :                 if (gdev->base + gdev->ngpio <= base)
     193                 :            :                         break;
     194                 :            :                 else
     195                 :            :                         /* nope, check the space right before the chip */
     196                 :          0 :                         base = gdev->base - ngpio;
     197                 :            :         }
     198                 :            : 
     199         [ #  # ]:          0 :         if (gpio_is_valid(base)) {
     200                 :            :                 pr_debug("%s: found new base at %d\n", __func__, base);
     201                 :          0 :                 return base;
     202                 :            :         } else {
     203                 :          0 :                 pr_err("%s: cannot find free range\n", __func__);
     204                 :          0 :                 return -ENOSPC;
     205                 :            :         }
     206                 :            : }
     207                 :            : 
     208                 :            : /**
     209                 :            :  * gpiod_get_direction - return the current direction of a GPIO
     210                 :            :  * @desc:       GPIO to get the direction of
     211                 :            :  *
     212                 :            :  * Returns 0 for output, 1 for input, or an error code in case of error.
     213                 :            :  *
     214                 :            :  * This function may sleep if gpiod_cansleep() is true.
     215                 :            :  */
     216                 :        414 : int gpiod_get_direction(struct gpio_desc *desc)
     217                 :            : {
     218                 :            :         struct gpio_chip *chip;
     219                 :            :         unsigned offset;
     220                 :            :         int ret;
     221                 :            : 
     222                 :            :         chip = gpiod_to_chip(desc);
     223                 :        414 :         offset = gpio_chip_hwgpio(desc);
     224                 :            : 
     225                 :            :         /*
     226                 :            :          * Open drain emulation using input mode may incorrectly report
     227                 :            :          * input here, fix that up.
     228                 :            :          */
     229   [ -  +  #  # ]:        414 :         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) &&
     230                 :            :             test_bit(FLAG_IS_OUT, &desc->flags))
     231                 :            :                 return 0;
     232                 :            : 
     233         [ +  - ]:        414 :         if (!chip->get_direction)
     234                 :            :                 return -ENOTSUPP;
     235                 :            : 
     236                 :        414 :         ret = chip->get_direction(chip, offset);
     237         [ +  + ]:        414 :         if (ret > 0) {
     238                 :            :                 /* GPIOF_DIR_IN, or other positive */
     239                 :            :                 ret = 1;
     240                 :        207 :                 clear_bit(FLAG_IS_OUT, &desc->flags);
     241                 :            :         }
     242         [ +  + ]:        414 :         if (ret == 0) {
     243                 :            :                 /* GPIOF_DIR_OUT */
     244                 :        207 :                 set_bit(FLAG_IS_OUT, &desc->flags);
     245                 :            :         }
     246                 :        414 :         return ret;
     247                 :            : }
     248                 :            : EXPORT_SYMBOL_GPL(gpiod_get_direction);
     249                 :            : 
     250                 :            : /*
     251                 :            :  * Add a new chip to the global chips list, keeping the list of chips sorted
     252                 :            :  * by range(means [base, base + ngpio - 1]) order.
     253                 :            :  *
     254                 :            :  * Return -EBUSY if the new chip overlaps with some other chip's integer
     255                 :            :  * space.
     256                 :            :  */
     257                 :        207 : static int gpiodev_add_to_list(struct gpio_device *gdev)
     258                 :            : {
     259                 :            :         struct gpio_device *prev, *next;
     260                 :            : 
     261         [ +  - ]:        207 :         if (list_empty(&gpio_devices)) {
     262                 :            :                 /* initial entry in list */
     263                 :        207 :                 list_add_tail(&gdev->list, &gpio_devices);
     264                 :        207 :                 return 0;
     265                 :            :         }
     266                 :            : 
     267                 :          0 :         next = list_entry(gpio_devices.next, struct gpio_device, list);
     268         [ #  # ]:          0 :         if (gdev->base + gdev->ngpio <= next->base) {
     269                 :            :                 /* add before first entry */
     270                 :          0 :                 list_add(&gdev->list, &gpio_devices);
     271                 :          0 :                 return 0;
     272                 :            :         }
     273                 :            : 
     274                 :          0 :         prev = list_entry(gpio_devices.prev, struct gpio_device, list);
     275         [ #  # ]:          0 :         if (prev->base + prev->ngpio <= gdev->base) {
     276                 :            :                 /* add behind last entry */
     277                 :          0 :                 list_add_tail(&gdev->list, &gpio_devices);
     278                 :          0 :                 return 0;
     279                 :            :         }
     280                 :            : 
     281         [ #  # ]:          0 :         list_for_each_entry_safe(prev, next, &gpio_devices, list) {
     282                 :            :                 /* at the end of the list */
     283         [ #  # ]:          0 :                 if (&next->list == &gpio_devices)
     284                 :            :                         break;
     285                 :            : 
     286                 :            :                 /* add between prev and next */
     287         [ #  # ]:          0 :                 if (prev->base + prev->ngpio <= gdev->base
     288         [ #  # ]:          0 :                                 && gdev->base + gdev->ngpio <= next->base) {
     289                 :          0 :                         list_add(&gdev->list, &prev->list);
     290                 :          0 :                         return 0;
     291                 :            :                 }
     292                 :            :         }
     293                 :            : 
     294                 :          0 :         dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
     295                 :          0 :         return -EBUSY;
     296                 :            : }
     297                 :            : 
     298                 :            : /*
     299                 :            :  * Convert a GPIO name to its descriptor
     300                 :            :  */
     301                 :          0 : static struct gpio_desc *gpio_name_to_desc(const char * const name)
     302                 :            : {
     303                 :            :         struct gpio_device *gdev;
     304                 :            :         unsigned long flags;
     305                 :            : 
     306                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
     307                 :            : 
     308         [ #  # ]:          0 :         list_for_each_entry(gdev, &gpio_devices, list) {
     309                 :            :                 int i;
     310                 :            : 
     311         [ #  # ]:          0 :                 for (i = 0; i != gdev->ngpio; ++i) {
     312                 :          0 :                         struct gpio_desc *desc = &gdev->descs[i];
     313                 :            : 
     314   [ #  #  #  # ]:          0 :                         if (!desc->name || !name)
     315                 :          0 :                                 continue;
     316                 :            : 
     317         [ #  # ]:          0 :                         if (!strcmp(desc->name, name)) {
     318                 :            :                                 spin_unlock_irqrestore(&gpio_lock, flags);
     319                 :          0 :                                 return desc;
     320                 :            :                         }
     321                 :            :                 }
     322                 :            :         }
     323                 :            : 
     324                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
     325                 :            : 
     326                 :          0 :         return NULL;
     327                 :            : }
     328                 :            : 
     329                 :            : /*
     330                 :            :  * Takes the names from gc->names and checks if they are all unique. If they
     331                 :            :  * are, they are assigned to their gpio descriptors.
     332                 :            :  *
     333                 :            :  * Warning if one of the names is already used for a different GPIO.
     334                 :            :  */
     335                 :        207 : static int gpiochip_set_desc_names(struct gpio_chip *gc)
     336                 :            : {
     337                 :        207 :         struct gpio_device *gdev = gc->gpiodev;
     338                 :            :         int i;
     339                 :            : 
     340         [ -  + ]:        207 :         if (!gc->names)
     341                 :            :                 return 0;
     342                 :            : 
     343                 :            :         /* First check all names if they are unique */
     344         [ #  # ]:          0 :         for (i = 0; i != gc->ngpio; ++i) {
     345                 :            :                 struct gpio_desc *gpio;
     346                 :            : 
     347                 :          0 :                 gpio = gpio_name_to_desc(gc->names[i]);
     348         [ #  # ]:          0 :                 if (gpio)
     349                 :          0 :                         dev_warn(&gdev->dev,
     350                 :            :                                  "Detected name collision for GPIO name '%s'\n",
     351                 :            :                                  gc->names[i]);
     352                 :            :         }
     353                 :            : 
     354                 :            :         /* Then add all names to the GPIO descriptors */
     355         [ #  # ]:          0 :         for (i = 0; i != gc->ngpio; ++i)
     356                 :          0 :                 gdev->descs[i].name = gc->names[i];
     357                 :            : 
     358                 :            :         return 0;
     359                 :            : }
     360                 :            : 
     361                 :          0 : static unsigned long *gpiochip_allocate_mask(struct gpio_chip *chip)
     362                 :            : {
     363                 :            :         unsigned long *p;
     364                 :            : 
     365                 :          0 :         p = bitmap_alloc(chip->ngpio, GFP_KERNEL);
     366         [ #  # ]:          0 :         if (!p)
     367                 :            :                 return NULL;
     368                 :            : 
     369                 :            :         /* Assume by default all GPIOs are valid */
     370                 :          0 :         bitmap_fill(p, chip->ngpio);
     371                 :            : 
     372                 :          0 :         return p;
     373                 :            : }
     374                 :            : 
     375                 :        207 : static int gpiochip_alloc_valid_mask(struct gpio_chip *gc)
     376                 :            : {
     377   [ +  -  -  + ]:        207 :         if (!(of_gpio_need_valid_mask(gc) || gc->init_valid_mask))
     378                 :            :                 return 0;
     379                 :            : 
     380                 :          0 :         gc->valid_mask = gpiochip_allocate_mask(gc);
     381         [ #  # ]:          0 :         if (!gc->valid_mask)
     382                 :            :                 return -ENOMEM;
     383                 :            : 
     384                 :          0 :         return 0;
     385                 :            : }
     386                 :            : 
     387                 :            : static int gpiochip_init_valid_mask(struct gpio_chip *gc)
     388                 :            : {
     389         [ -  + ]:        207 :         if (gc->init_valid_mask)
     390                 :          0 :                 return gc->init_valid_mask(gc,
     391                 :            :                                            gc->valid_mask,
     392                 :          0 :                                            gc->ngpio);
     393                 :            : 
     394                 :            :         return 0;
     395                 :            : }
     396                 :            : 
     397                 :            : static void gpiochip_free_valid_mask(struct gpio_chip *gpiochip)
     398                 :            : {
     399                 :          0 :         bitmap_free(gpiochip->valid_mask);
     400                 :          0 :         gpiochip->valid_mask = NULL;
     401                 :            : }
     402                 :            : 
     403                 :          0 : bool gpiochip_line_is_valid(const struct gpio_chip *gpiochip,
     404                 :            :                                 unsigned int offset)
     405                 :            : {
     406                 :            :         /* No mask means all valid */
     407   [ -  +  #  #  :      11592 :         if (likely(!gpiochip->valid_mask))
             -  +  #  # ]
     408                 :            :                 return true;
     409                 :          0 :         return test_bit(offset, gpiochip->valid_mask);
     410                 :            : }
     411                 :            : EXPORT_SYMBOL_GPL(gpiochip_line_is_valid);
     412                 :            : 
     413                 :            : /*
     414                 :            :  * GPIO line handle management
     415                 :            :  */
     416                 :            : 
     417                 :            : /**
     418                 :            :  * struct linehandle_state - contains the state of a userspace handle
     419                 :            :  * @gdev: the GPIO device the handle pertains to
     420                 :            :  * @label: consumer label used to tag descriptors
     421                 :            :  * @descs: the GPIO descriptors held by this handle
     422                 :            :  * @numdescs: the number of descriptors held in the descs array
     423                 :            :  */
     424                 :            : struct linehandle_state {
     425                 :            :         struct gpio_device *gdev;
     426                 :            :         const char *label;
     427                 :            :         struct gpio_desc *descs[GPIOHANDLES_MAX];
     428                 :            :         u32 numdescs;
     429                 :            : };
     430                 :            : 
     431                 :            : #define GPIOHANDLE_REQUEST_VALID_FLAGS \
     432                 :            :         (GPIOHANDLE_REQUEST_INPUT | \
     433                 :            :         GPIOHANDLE_REQUEST_OUTPUT | \
     434                 :            :         GPIOHANDLE_REQUEST_ACTIVE_LOW | \
     435                 :            :         GPIOHANDLE_REQUEST_OPEN_DRAIN | \
     436                 :            :         GPIOHANDLE_REQUEST_OPEN_SOURCE)
     437                 :            : 
     438                 :          0 : static long linehandle_ioctl(struct file *filep, unsigned int cmd,
     439                 :            :                              unsigned long arg)
     440                 :            : {
     441                 :          0 :         struct linehandle_state *lh = filep->private_data;
     442                 :          0 :         void __user *ip = (void __user *)arg;
     443                 :            :         struct gpiohandle_data ghd;
     444                 :            :         DECLARE_BITMAP(vals, GPIOHANDLES_MAX);
     445                 :            :         int i;
     446                 :            : 
     447         [ #  # ]:          0 :         if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
     448                 :            :                 /* NOTE: It's ok to read values of output lines. */
     449                 :          0 :                 int ret = gpiod_get_array_value_complex(false,
     450                 :            :                                                         true,
     451                 :            :                                                         lh->numdescs,
     452                 :          0 :                                                         lh->descs,
     453                 :            :                                                         NULL,
     454                 :            :                                                         vals);
     455         [ #  # ]:          0 :                 if (ret)
     456                 :            :                         return ret;
     457                 :            : 
     458                 :          0 :                 memset(&ghd, 0, sizeof(ghd));
     459         [ #  # ]:          0 :                 for (i = 0; i < lh->numdescs; i++)
     460                 :          0 :                         ghd.values[i] = test_bit(i, vals);
     461                 :            : 
     462         [ #  # ]:          0 :                 if (copy_to_user(ip, &ghd, sizeof(ghd)))
     463                 :            :                         return -EFAULT;
     464                 :            : 
     465                 :          0 :                 return 0;
     466         [ #  # ]:          0 :         } else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
     467                 :            :                 /*
     468                 :            :                  * All line descriptors were created at once with the same
     469                 :            :                  * flags so just check if the first one is really output.
     470                 :            :                  */
     471         [ #  # ]:          0 :                 if (!test_bit(FLAG_IS_OUT, &lh->descs[0]->flags))
     472                 :            :                         return -EPERM;
     473                 :            : 
     474         [ #  # ]:          0 :                 if (copy_from_user(&ghd, ip, sizeof(ghd)))
     475                 :            :                         return -EFAULT;
     476                 :            : 
     477                 :            :                 /* Clamp all values to [0,1] */
     478         [ #  # ]:          0 :                 for (i = 0; i < lh->numdescs; i++)
     479                 :          0 :                         __assign_bit(i, vals, ghd.values[i]);
     480                 :            : 
     481                 :            :                 /* Reuse the array setting function */
     482                 :          0 :                 return gpiod_set_array_value_complex(false,
     483                 :            :                                               true,
     484                 :            :                                               lh->numdescs,
     485                 :          0 :                                               lh->descs,
     486                 :            :                                               NULL,
     487                 :            :                                               vals);
     488                 :            :         }
     489                 :            :         return -EINVAL;
     490                 :            : }
     491                 :            : 
     492                 :            : #ifdef CONFIG_COMPAT
     493                 :            : static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
     494                 :            :                              unsigned long arg)
     495                 :            : {
     496                 :            :         return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
     497                 :            : }
     498                 :            : #endif
     499                 :            : 
     500                 :          0 : static int linehandle_release(struct inode *inode, struct file *filep)
     501                 :            : {
     502                 :          0 :         struct linehandle_state *lh = filep->private_data;
     503                 :          0 :         struct gpio_device *gdev = lh->gdev;
     504                 :            :         int i;
     505                 :            : 
     506         [ #  # ]:          0 :         for (i = 0; i < lh->numdescs; i++)
     507                 :          0 :                 gpiod_free(lh->descs[i]);
     508                 :          0 :         kfree(lh->label);
     509                 :          0 :         kfree(lh);
     510                 :          0 :         put_device(&gdev->dev);
     511                 :          0 :         return 0;
     512                 :            : }
     513                 :            : 
     514                 :            : static const struct file_operations linehandle_fileops = {
     515                 :            :         .release = linehandle_release,
     516                 :            :         .owner = THIS_MODULE,
     517                 :            :         .llseek = noop_llseek,
     518                 :            :         .unlocked_ioctl = linehandle_ioctl,
     519                 :            : #ifdef CONFIG_COMPAT
     520                 :            :         .compat_ioctl = linehandle_ioctl_compat,
     521                 :            : #endif
     522                 :            : };
     523                 :            : 
     524                 :          0 : static int linehandle_create(struct gpio_device *gdev, void __user *ip)
     525                 :            : {
     526                 :            :         struct gpiohandle_request handlereq;
     527                 :            :         struct linehandle_state *lh;
     528                 :            :         struct file *file;
     529                 :            :         int fd, i, count = 0, ret;
     530                 :            :         u32 lflags;
     531                 :            : 
     532         [ #  # ]:          0 :         if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
     533                 :            :                 return -EFAULT;
     534         [ #  # ]:          0 :         if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
     535                 :            :                 return -EINVAL;
     536                 :            : 
     537                 :          0 :         lflags = handlereq.flags;
     538                 :            : 
     539                 :            :         /* Return an error if an unknown flag is set */
     540         [ #  # ]:          0 :         if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
     541                 :            :                 return -EINVAL;
     542                 :            : 
     543                 :            :         /*
     544                 :            :          * Do not allow both INPUT & OUTPUT flags to be set as they are
     545                 :            :          * contradictory.
     546                 :            :          */
     547         [ #  # ]:          0 :         if ((lflags & GPIOHANDLE_REQUEST_INPUT) &&
     548                 :            :             (lflags & GPIOHANDLE_REQUEST_OUTPUT))
     549                 :            :                 return -EINVAL;
     550                 :            : 
     551                 :            :         /*
     552                 :            :          * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
     553                 :            :          * the hardware actually supports enabling both at the same time the
     554                 :            :          * electrical result would be disastrous.
     555                 :            :          */
     556         [ #  # ]:          0 :         if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
     557                 :            :             (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
     558                 :            :                 return -EINVAL;
     559                 :            : 
     560                 :            :         /* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
     561   [ #  #  #  # ]:          0 :         if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
     562                 :            :             ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
     563                 :            :              (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
     564                 :            :                 return -EINVAL;
     565                 :            : 
     566                 :          0 :         lh = kzalloc(sizeof(*lh), GFP_KERNEL);
     567         [ #  # ]:          0 :         if (!lh)
     568                 :            :                 return -ENOMEM;
     569                 :          0 :         lh->gdev = gdev;
     570                 :          0 :         get_device(&gdev->dev);
     571                 :            : 
     572                 :            :         /* Make sure this is terminated */
     573                 :          0 :         handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
     574         [ #  # ]:          0 :         if (strlen(handlereq.consumer_label)) {
     575                 :          0 :                 lh->label = kstrdup(handlereq.consumer_label,
     576                 :            :                                     GFP_KERNEL);
     577         [ #  # ]:          0 :                 if (!lh->label) {
     578                 :            :                         ret = -ENOMEM;
     579                 :            :                         goto out_free_lh;
     580                 :            :                 }
     581                 :            :         }
     582                 :            : 
     583                 :            :         /* Request each GPIO */
     584         [ #  # ]:          0 :         for (i = 0; i < handlereq.lines; i++) {
     585                 :          0 :                 u32 offset = handlereq.lineoffsets[i];
     586                 :            :                 struct gpio_desc *desc;
     587                 :            : 
     588         [ #  # ]:          0 :                 if (offset >= gdev->ngpio) {
     589                 :            :                         ret = -EINVAL;
     590                 :            :                         goto out_free_descs;
     591                 :            :                 }
     592                 :            : 
     593                 :          0 :                 desc = &gdev->descs[offset];
     594                 :          0 :                 ret = gpiod_request(desc, lh->label);
     595         [ #  # ]:          0 :                 if (ret)
     596                 :            :                         goto out_free_descs;
     597                 :          0 :                 lh->descs[i] = desc;
     598                 :          0 :                 count = i + 1;
     599                 :            : 
     600         [ #  # ]:          0 :                 if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
     601                 :          0 :                         set_bit(FLAG_ACTIVE_LOW, &desc->flags);
     602         [ #  # ]:          0 :                 if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
     603                 :          0 :                         set_bit(FLAG_OPEN_DRAIN, &desc->flags);
     604         [ #  # ]:          0 :                 if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
     605                 :          0 :                         set_bit(FLAG_OPEN_SOURCE, &desc->flags);
     606                 :            : 
     607                 :          0 :                 ret = gpiod_set_transitory(desc, false);
     608         [ #  # ]:          0 :                 if (ret < 0)
     609                 :            :                         goto out_free_descs;
     610                 :            : 
     611                 :            :                 /*
     612                 :            :                  * Lines have to be requested explicitly for input
     613                 :            :                  * or output, else the line will be treated "as is".
     614                 :            :                  */
     615         [ #  # ]:          0 :                 if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
     616                 :          0 :                         int val = !!handlereq.default_values[i];
     617                 :            : 
     618                 :          0 :                         ret = gpiod_direction_output(desc, val);
     619         [ #  # ]:          0 :                         if (ret)
     620                 :            :                                 goto out_free_descs;
     621         [ #  # ]:          0 :                 } else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
     622                 :          0 :                         ret = gpiod_direction_input(desc);
     623         [ #  # ]:          0 :                         if (ret)
     624                 :            :                                 goto out_free_descs;
     625                 :            :                 }
     626                 :            :                 dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
     627                 :            :                         offset);
     628                 :            :         }
     629                 :            :         /* Let i point at the last handle */
     630                 :            :         i--;
     631                 :          0 :         lh->numdescs = handlereq.lines;
     632                 :            : 
     633                 :          0 :         fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
     634         [ #  # ]:          0 :         if (fd < 0) {
     635                 :            :                 ret = fd;
     636                 :            :                 goto out_free_descs;
     637                 :            :         }
     638                 :            : 
     639                 :          0 :         file = anon_inode_getfile("gpio-linehandle",
     640                 :            :                                   &linehandle_fileops,
     641                 :            :                                   lh,
     642                 :            :                                   O_RDONLY | O_CLOEXEC);
     643         [ #  # ]:          0 :         if (IS_ERR(file)) {
     644                 :            :                 ret = PTR_ERR(file);
     645                 :            :                 goto out_put_unused_fd;
     646                 :            :         }
     647                 :            : 
     648                 :          0 :         handlereq.fd = fd;
     649         [ #  # ]:          0 :         if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
     650                 :            :                 /*
     651                 :            :                  * fput() will trigger the release() callback, so do not go onto
     652                 :            :                  * the regular error cleanup path here.
     653                 :            :                  */
     654                 :          0 :                 fput(file);
     655                 :          0 :                 put_unused_fd(fd);
     656                 :          0 :                 return -EFAULT;
     657                 :            :         }
     658                 :            : 
     659                 :          0 :         fd_install(fd, file);
     660                 :            : 
     661                 :            :         dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
     662                 :            :                 lh->numdescs);
     663                 :            : 
     664                 :          0 :         return 0;
     665                 :            : 
     666                 :            : out_put_unused_fd:
     667                 :          0 :         put_unused_fd(fd);
     668                 :            : out_free_descs:
     669         [ #  # ]:          0 :         for (i = 0; i < count; i++)
     670                 :          0 :                 gpiod_free(lh->descs[i]);
     671                 :          0 :         kfree(lh->label);
     672                 :            : out_free_lh:
     673                 :          0 :         kfree(lh);
     674                 :          0 :         put_device(&gdev->dev);
     675                 :          0 :         return ret;
     676                 :            : }
     677                 :            : 
     678                 :            : /*
     679                 :            :  * GPIO line event management
     680                 :            :  */
     681                 :            : 
     682                 :            : /**
     683                 :            :  * struct lineevent_state - contains the state of a userspace event
     684                 :            :  * @gdev: the GPIO device the event pertains to
     685                 :            :  * @label: consumer label used to tag descriptors
     686                 :            :  * @desc: the GPIO descriptor held by this event
     687                 :            :  * @eflags: the event flags this line was requested with
     688                 :            :  * @irq: the interrupt that trigger in response to events on this GPIO
     689                 :            :  * @wait: wait queue that handles blocking reads of events
     690                 :            :  * @events: KFIFO for the GPIO events
     691                 :            :  * @read_lock: mutex lock to protect reads from colliding with adding
     692                 :            :  * new events to the FIFO
     693                 :            :  * @timestamp: cache for the timestamp storing it between hardirq
     694                 :            :  * and IRQ thread, used to bring the timestamp close to the actual
     695                 :            :  * event
     696                 :            :  */
     697                 :            : struct lineevent_state {
     698                 :            :         struct gpio_device *gdev;
     699                 :            :         const char *label;
     700                 :            :         struct gpio_desc *desc;
     701                 :            :         u32 eflags;
     702                 :            :         int irq;
     703                 :            :         wait_queue_head_t wait;
     704                 :            :         DECLARE_KFIFO(events, struct gpioevent_data, 16);
     705                 :            :         struct mutex read_lock;
     706                 :            :         u64 timestamp;
     707                 :            : };
     708                 :            : 
     709                 :            : #define GPIOEVENT_REQUEST_VALID_FLAGS \
     710                 :            :         (GPIOEVENT_REQUEST_RISING_EDGE | \
     711                 :            :         GPIOEVENT_REQUEST_FALLING_EDGE)
     712                 :            : 
     713                 :          0 : static __poll_t lineevent_poll(struct file *filep,
     714                 :            :                                    struct poll_table_struct *wait)
     715                 :            : {
     716                 :          0 :         struct lineevent_state *le = filep->private_data;
     717                 :            :         __poll_t events = 0;
     718                 :            : 
     719                 :          0 :         poll_wait(filep, &le->wait, wait);
     720                 :            : 
     721         [ #  # ]:          0 :         if (!kfifo_is_empty(&le->events))
     722                 :            :                 events = EPOLLIN | EPOLLRDNORM;
     723                 :            : 
     724                 :          0 :         return events;
     725                 :            : }
     726                 :            : 
     727                 :            : 
     728                 :          0 : static ssize_t lineevent_read(struct file *filep,
     729                 :            :                               char __user *buf,
     730                 :            :                               size_t count,
     731                 :            :                               loff_t *f_ps)
     732                 :            : {
     733                 :          0 :         struct lineevent_state *le = filep->private_data;
     734                 :            :         unsigned int copied;
     735                 :            :         int ret;
     736                 :            : 
     737         [ #  # ]:          0 :         if (count < sizeof(struct gpioevent_data))
     738                 :            :                 return -EINVAL;
     739                 :            : 
     740                 :            :         do {
     741         [ #  # ]:          0 :                 if (kfifo_is_empty(&le->events)) {
     742         [ #  # ]:          0 :                         if (filep->f_flags & O_NONBLOCK)
     743                 :            :                                 return -EAGAIN;
     744                 :            : 
     745   [ #  #  #  #  :          0 :                         ret = wait_event_interruptible(le->wait,
                   #  # ]
     746                 :            :                                         !kfifo_is_empty(&le->events));
     747         [ #  # ]:          0 :                         if (ret)
     748                 :          0 :                                 return ret;
     749                 :            :                 }
     750                 :            : 
     751         [ #  # ]:          0 :                 if (mutex_lock_interruptible(&le->read_lock))
     752                 :            :                         return -ERESTARTSYS;
     753                 :          0 :                 ret = kfifo_to_user(&le->events, buf, count, &copied);
     754                 :          0 :                 mutex_unlock(&le->read_lock);
     755                 :            : 
     756         [ #  # ]:          0 :                 if (ret)
     757                 :          0 :                         return ret;
     758                 :            : 
     759                 :            :                 /*
     760                 :            :                  * If we couldn't read anything from the fifo (a different
     761                 :            :                  * thread might have been faster) we either return -EAGAIN if
     762                 :            :                  * the file descriptor is non-blocking, otherwise we go back to
     763                 :            :                  * sleep and wait for more data to arrive.
     764                 :            :                  */
     765   [ #  #  #  # ]:          0 :                 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
     766                 :            :                         return -EAGAIN;
     767                 :            : 
     768         [ #  # ]:          0 :         } while (copied == 0);
     769                 :            : 
     770                 :          0 :         return copied;
     771                 :            : }
     772                 :            : 
     773                 :          0 : static int lineevent_release(struct inode *inode, struct file *filep)
     774                 :            : {
     775                 :          0 :         struct lineevent_state *le = filep->private_data;
     776                 :          0 :         struct gpio_device *gdev = le->gdev;
     777                 :            : 
     778                 :          0 :         free_irq(le->irq, le);
     779                 :          0 :         gpiod_free(le->desc);
     780                 :          0 :         kfree(le->label);
     781                 :          0 :         kfree(le);
     782                 :          0 :         put_device(&gdev->dev);
     783                 :          0 :         return 0;
     784                 :            : }
     785                 :            : 
     786                 :          0 : static long lineevent_ioctl(struct file *filep, unsigned int cmd,
     787                 :            :                             unsigned long arg)
     788                 :            : {
     789                 :          0 :         struct lineevent_state *le = filep->private_data;
     790                 :          0 :         void __user *ip = (void __user *)arg;
     791                 :            :         struct gpiohandle_data ghd;
     792                 :            : 
     793                 :            :         /*
     794                 :            :          * We can get the value for an event line but not set it,
     795                 :            :          * because it is input by definition.
     796                 :            :          */
     797         [ #  # ]:          0 :         if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
     798                 :            :                 int val;
     799                 :            : 
     800                 :          0 :                 memset(&ghd, 0, sizeof(ghd));
     801                 :            : 
     802                 :          0 :                 val = gpiod_get_value_cansleep(le->desc);
     803         [ #  # ]:          0 :                 if (val < 0)
     804                 :            :                         return val;
     805                 :          0 :                 ghd.values[0] = val;
     806                 :            : 
     807         [ #  # ]:          0 :                 if (copy_to_user(ip, &ghd, sizeof(ghd)))
     808                 :            :                         return -EFAULT;
     809                 :            : 
     810                 :          0 :                 return 0;
     811                 :            :         }
     812                 :            :         return -EINVAL;
     813                 :            : }
     814                 :            : 
     815                 :            : #ifdef CONFIG_COMPAT
     816                 :            : static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
     817                 :            :                                    unsigned long arg)
     818                 :            : {
     819                 :            :         return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
     820                 :            : }
     821                 :            : #endif
     822                 :            : 
     823                 :            : static const struct file_operations lineevent_fileops = {
     824                 :            :         .release = lineevent_release,
     825                 :            :         .read = lineevent_read,
     826                 :            :         .poll = lineevent_poll,
     827                 :            :         .owner = THIS_MODULE,
     828                 :            :         .llseek = noop_llseek,
     829                 :            :         .unlocked_ioctl = lineevent_ioctl,
     830                 :            : #ifdef CONFIG_COMPAT
     831                 :            :         .compat_ioctl = lineevent_ioctl_compat,
     832                 :            : #endif
     833                 :            : };
     834                 :            : 
     835                 :          0 : static irqreturn_t lineevent_irq_thread(int irq, void *p)
     836                 :            : {
     837                 :            :         struct lineevent_state *le = p;
     838                 :            :         struct gpioevent_data ge;
     839                 :            :         int ret;
     840                 :            : 
     841                 :            :         /* Do not leak kernel stack to userspace */
     842                 :          0 :         memset(&ge, 0, sizeof(ge));
     843                 :            : 
     844                 :            :         /*
     845                 :            :          * We may be running from a nested threaded interrupt in which case
     846                 :            :          * we didn't get the timestamp from lineevent_irq_handler().
     847                 :            :          */
     848         [ #  # ]:          0 :         if (!le->timestamp)
     849                 :          0 :                 ge.timestamp = ktime_get_real_ns();
     850                 :            :         else
     851                 :          0 :                 ge.timestamp = le->timestamp;
     852                 :            : 
     853         [ #  # ]:          0 :         if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
     854                 :          0 :             && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
     855                 :          0 :                 int level = gpiod_get_value_cansleep(le->desc);
     856         [ #  # ]:          0 :                 if (level)
     857                 :            :                         /* Emit low-to-high event */
     858                 :          0 :                         ge.id = GPIOEVENT_EVENT_RISING_EDGE;
     859                 :            :                 else
     860                 :            :                         /* Emit high-to-low event */
     861                 :          0 :                         ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
     862         [ #  # ]:          0 :         } else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE) {
     863                 :            :                 /* Emit low-to-high event */
     864                 :          0 :                 ge.id = GPIOEVENT_EVENT_RISING_EDGE;
     865         [ #  # ]:          0 :         } else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
     866                 :            :                 /* Emit high-to-low event */
     867                 :          0 :                 ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
     868                 :            :         } else {
     869                 :            :                 return IRQ_NONE;
     870                 :            :         }
     871                 :            : 
     872         [ #  # ]:          0 :         ret = kfifo_put(&le->events, ge);
     873         [ #  # ]:          0 :         if (ret)
     874                 :          0 :                 wake_up_poll(&le->wait, EPOLLIN);
     875                 :            : 
     876                 :            :         return IRQ_HANDLED;
     877                 :            : }
     878                 :            : 
     879                 :          0 : static irqreturn_t lineevent_irq_handler(int irq, void *p)
     880                 :            : {
     881                 :            :         struct lineevent_state *le = p;
     882                 :            : 
     883                 :            :         /*
     884                 :            :          * Just store the timestamp in hardirq context so we get it as
     885                 :            :          * close in time as possible to the actual event.
     886                 :            :          */
     887                 :          0 :         le->timestamp = ktime_get_real_ns();
     888                 :            : 
     889                 :          0 :         return IRQ_WAKE_THREAD;
     890                 :            : }
     891                 :            : 
     892                 :          0 : static int lineevent_create(struct gpio_device *gdev, void __user *ip)
     893                 :            : {
     894                 :            :         struct gpioevent_request eventreq;
     895                 :            :         struct lineevent_state *le;
     896                 :            :         struct gpio_desc *desc;
     897                 :            :         struct file *file;
     898                 :            :         u32 offset;
     899                 :            :         u32 lflags;
     900                 :            :         u32 eflags;
     901                 :            :         int fd;
     902                 :            :         int ret;
     903                 :            :         int irqflags = 0;
     904                 :            : 
     905         [ #  # ]:          0 :         if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
     906                 :            :                 return -EFAULT;
     907                 :            : 
     908                 :          0 :         le = kzalloc(sizeof(*le), GFP_KERNEL);
     909         [ #  # ]:          0 :         if (!le)
     910                 :            :                 return -ENOMEM;
     911                 :          0 :         le->gdev = gdev;
     912                 :          0 :         get_device(&gdev->dev);
     913                 :            : 
     914                 :            :         /* Make sure this is terminated */
     915                 :          0 :         eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
     916         [ #  # ]:          0 :         if (strlen(eventreq.consumer_label)) {
     917                 :          0 :                 le->label = kstrdup(eventreq.consumer_label,
     918                 :            :                                     GFP_KERNEL);
     919         [ #  # ]:          0 :                 if (!le->label) {
     920                 :            :                         ret = -ENOMEM;
     921                 :            :                         goto out_free_le;
     922                 :            :                 }
     923                 :            :         }
     924                 :            : 
     925                 :          0 :         offset = eventreq.lineoffset;
     926                 :          0 :         lflags = eventreq.handleflags;
     927                 :          0 :         eflags = eventreq.eventflags;
     928                 :            : 
     929         [ #  # ]:          0 :         if (offset >= gdev->ngpio) {
     930                 :            :                 ret = -EINVAL;
     931                 :            :                 goto out_free_label;
     932                 :            :         }
     933                 :            : 
     934                 :            :         /* Return an error if a unknown flag is set */
     935   [ #  #  #  # ]:          0 :         if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
     936                 :          0 :             (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
     937                 :            :                 ret = -EINVAL;
     938                 :            :                 goto out_free_label;
     939                 :            :         }
     940                 :            : 
     941                 :            :         /* This is just wrong: we don't look for events on output lines */
     942         [ #  # ]:          0 :         if ((lflags & GPIOHANDLE_REQUEST_OUTPUT) ||
     943                 :          0 :             (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
     944                 :            :             (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)) {
     945                 :            :                 ret = -EINVAL;
     946                 :            :                 goto out_free_label;
     947                 :            :         }
     948                 :            : 
     949                 :          0 :         desc = &gdev->descs[offset];
     950                 :          0 :         ret = gpiod_request(desc, le->label);
     951         [ #  # ]:          0 :         if (ret)
     952                 :            :                 goto out_free_label;
     953                 :          0 :         le->desc = desc;
     954                 :          0 :         le->eflags = eflags;
     955                 :            : 
     956         [ #  # ]:          0 :         if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
     957                 :          0 :                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
     958                 :            : 
     959                 :          0 :         ret = gpiod_direction_input(desc);
     960         [ #  # ]:          0 :         if (ret)
     961                 :            :                 goto out_free_desc;
     962                 :            : 
     963                 :          0 :         le->irq = gpiod_to_irq(desc);
     964         [ #  # ]:          0 :         if (le->irq <= 0) {
     965                 :            :                 ret = -ENODEV;
     966                 :            :                 goto out_free_desc;
     967                 :            :         }
     968                 :            : 
     969         [ #  # ]:          0 :         if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
     970                 :            :                 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
     971         [ #  # ]:          0 :                         IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
     972         [ #  # ]:          0 :         if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
     973                 :          0 :                 irqflags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
     974         [ #  # ]:          0 :                         IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
     975                 :          0 :         irqflags |= IRQF_ONESHOT;
     976                 :            : 
     977                 :          0 :         INIT_KFIFO(le->events);
     978                 :          0 :         init_waitqueue_head(&le->wait);
     979                 :          0 :         mutex_init(&le->read_lock);
     980                 :            : 
     981                 :            :         /* Request a thread to read the events */
     982                 :          0 :         ret = request_threaded_irq(le->irq,
     983                 :            :                         lineevent_irq_handler,
     984                 :            :                         lineevent_irq_thread,
     985                 :            :                         irqflags,
     986                 :            :                         le->label,
     987                 :            :                         le);
     988         [ #  # ]:          0 :         if (ret)
     989                 :            :                 goto out_free_desc;
     990                 :            : 
     991                 :          0 :         fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
     992         [ #  # ]:          0 :         if (fd < 0) {
     993                 :            :                 ret = fd;
     994                 :            :                 goto out_free_irq;
     995                 :            :         }
     996                 :            : 
     997                 :          0 :         file = anon_inode_getfile("gpio-event",
     998                 :            :                                   &lineevent_fileops,
     999                 :            :                                   le,
    1000                 :            :                                   O_RDONLY | O_CLOEXEC);
    1001         [ #  # ]:          0 :         if (IS_ERR(file)) {
    1002                 :            :                 ret = PTR_ERR(file);
    1003                 :            :                 goto out_put_unused_fd;
    1004                 :            :         }
    1005                 :            : 
    1006                 :          0 :         eventreq.fd = fd;
    1007         [ #  # ]:          0 :         if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
    1008                 :            :                 /*
    1009                 :            :                  * fput() will trigger the release() callback, so do not go onto
    1010                 :            :                  * the regular error cleanup path here.
    1011                 :            :                  */
    1012                 :          0 :                 fput(file);
    1013                 :          0 :                 put_unused_fd(fd);
    1014                 :          0 :                 return -EFAULT;
    1015                 :            :         }
    1016                 :            : 
    1017                 :          0 :         fd_install(fd, file);
    1018                 :            : 
    1019                 :          0 :         return 0;
    1020                 :            : 
    1021                 :            : out_put_unused_fd:
    1022                 :          0 :         put_unused_fd(fd);
    1023                 :            : out_free_irq:
    1024                 :          0 :         free_irq(le->irq, le);
    1025                 :            : out_free_desc:
    1026                 :          0 :         gpiod_free(le->desc);
    1027                 :            : out_free_label:
    1028                 :          0 :         kfree(le->label);
    1029                 :            : out_free_le:
    1030                 :          0 :         kfree(le);
    1031                 :          0 :         put_device(&gdev->dev);
    1032                 :          0 :         return ret;
    1033                 :            : }
    1034                 :            : 
    1035                 :            : /*
    1036                 :            :  * gpio_ioctl() - ioctl handler for the GPIO chardev
    1037                 :            :  */
    1038                 :          0 : static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
    1039                 :            : {
    1040                 :          0 :         struct gpio_device *gdev = filp->private_data;
    1041                 :          0 :         struct gpio_chip *chip = gdev->chip;
    1042                 :          0 :         void __user *ip = (void __user *)arg;
    1043                 :            : 
    1044                 :            :         /* We fail any subsequent ioctl():s when the chip is gone */
    1045         [ #  # ]:          0 :         if (!chip)
    1046                 :            :                 return -ENODEV;
    1047                 :            : 
    1048                 :            :         /* Fill in the struct and pass to userspace */
    1049         [ #  # ]:          0 :         if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
    1050                 :            :                 struct gpiochip_info chipinfo;
    1051                 :            : 
    1052                 :          0 :                 memset(&chipinfo, 0, sizeof(chipinfo));
    1053                 :            : 
    1054                 :          0 :                 strncpy(chipinfo.name, dev_name(&gdev->dev),
    1055                 :            :                         sizeof(chipinfo.name));
    1056                 :          0 :                 chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
    1057                 :          0 :                 strncpy(chipinfo.label, gdev->label,
    1058                 :            :                         sizeof(chipinfo.label));
    1059                 :          0 :                 chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
    1060                 :          0 :                 chipinfo.lines = gdev->ngpio;
    1061         [ #  # ]:          0 :                 if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
    1062                 :            :                         return -EFAULT;
    1063                 :          0 :                 return 0;
    1064         [ #  # ]:          0 :         } else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
    1065                 :            :                 struct gpioline_info lineinfo;
    1066                 :            :                 struct gpio_desc *desc;
    1067                 :            : 
    1068         [ #  # ]:          0 :                 if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
    1069                 :            :                         return -EFAULT;
    1070         [ #  # ]:          0 :                 if (lineinfo.line_offset >= gdev->ngpio)
    1071                 :            :                         return -EINVAL;
    1072                 :            : 
    1073                 :          0 :                 desc = &gdev->descs[lineinfo.line_offset];
    1074         [ #  # ]:          0 :                 if (desc->name) {
    1075                 :          0 :                         strncpy(lineinfo.name, desc->name,
    1076                 :            :                                 sizeof(lineinfo.name));
    1077                 :          0 :                         lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
    1078                 :            :                 } else {
    1079                 :          0 :                         lineinfo.name[0] = '\0';
    1080                 :            :                 }
    1081         [ #  # ]:          0 :                 if (desc->label) {
    1082                 :          0 :                         strncpy(lineinfo.consumer, desc->label,
    1083                 :            :                                 sizeof(lineinfo.consumer));
    1084                 :          0 :                         lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
    1085                 :            :                 } else {
    1086                 :          0 :                         lineinfo.consumer[0] = '\0';
    1087                 :            :                 }
    1088                 :            : 
    1089                 :            :                 /*
    1090                 :            :                  * Userspace only need to know that the kernel is using
    1091                 :            :                  * this GPIO so it can't use it.
    1092                 :            :                  */
    1093                 :          0 :                 lineinfo.flags = 0;
    1094   [ #  #  #  # ]:          0 :                 if (test_bit(FLAG_REQUESTED, &desc->flags) ||
    1095         [ #  # ]:          0 :                     test_bit(FLAG_IS_HOGGED, &desc->flags) ||
    1096         [ #  # ]:          0 :                     test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
    1097         [ #  # ]:          0 :                     test_bit(FLAG_EXPORT, &desc->flags) ||
    1098         [ #  # ]:          0 :                     test_bit(FLAG_SYSFS, &desc->flags) ||
    1099                 :          0 :                     !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset))
    1100                 :          0 :                         lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
    1101         [ #  # ]:          0 :                 if (test_bit(FLAG_IS_OUT, &desc->flags))
    1102                 :          0 :                         lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
    1103         [ #  # ]:          0 :                 if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    1104                 :          0 :                         lineinfo.flags |= GPIOLINE_FLAG_ACTIVE_LOW;
    1105         [ #  # ]:          0 :                 if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
    1106                 :          0 :                         lineinfo.flags |= (GPIOLINE_FLAG_OPEN_DRAIN |
    1107                 :            :                                            GPIOLINE_FLAG_IS_OUT);
    1108         [ #  # ]:          0 :                 if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
    1109                 :          0 :                         lineinfo.flags |= (GPIOLINE_FLAG_OPEN_SOURCE |
    1110                 :            :                                            GPIOLINE_FLAG_IS_OUT);
    1111                 :            : 
    1112         [ #  # ]:          0 :                 if (copy_to_user(ip, &lineinfo, sizeof(lineinfo)))
    1113                 :            :                         return -EFAULT;
    1114                 :          0 :                 return 0;
    1115         [ #  # ]:          0 :         } else if (cmd == GPIO_GET_LINEHANDLE_IOCTL) {
    1116                 :          0 :                 return linehandle_create(gdev, ip);
    1117         [ #  # ]:          0 :         } else if (cmd == GPIO_GET_LINEEVENT_IOCTL) {
    1118                 :          0 :                 return lineevent_create(gdev, ip);
    1119                 :            :         }
    1120                 :            :         return -EINVAL;
    1121                 :            : }
    1122                 :            : 
    1123                 :            : #ifdef CONFIG_COMPAT
    1124                 :            : static long gpio_ioctl_compat(struct file *filp, unsigned int cmd,
    1125                 :            :                               unsigned long arg)
    1126                 :            : {
    1127                 :            :         return gpio_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
    1128                 :            : }
    1129                 :            : #endif
    1130                 :            : 
    1131                 :            : /**
    1132                 :            :  * gpio_chrdev_open() - open the chardev for ioctl operations
    1133                 :            :  * @inode: inode for this chardev
    1134                 :            :  * @filp: file struct for storing private data
    1135                 :            :  * Returns 0 on success
    1136                 :            :  */
    1137                 :          0 : static int gpio_chrdev_open(struct inode *inode, struct file *filp)
    1138                 :            : {
    1139                 :          0 :         struct gpio_device *gdev = container_of(inode->i_cdev,
    1140                 :            :                                               struct gpio_device, chrdev);
    1141                 :            : 
    1142                 :            :         /* Fail on open if the backing gpiochip is gone */
    1143         [ #  # ]:          0 :         if (!gdev->chip)
    1144                 :            :                 return -ENODEV;
    1145                 :          0 :         get_device(&gdev->dev);
    1146                 :          0 :         filp->private_data = gdev;
    1147                 :            : 
    1148                 :          0 :         return nonseekable_open(inode, filp);
    1149                 :            : }
    1150                 :            : 
    1151                 :            : /**
    1152                 :            :  * gpio_chrdev_release() - close chardev after ioctl operations
    1153                 :            :  * @inode: inode for this chardev
    1154                 :            :  * @filp: file struct for storing private data
    1155                 :            :  * Returns 0 on success
    1156                 :            :  */
    1157                 :          0 : static int gpio_chrdev_release(struct inode *inode, struct file *filp)
    1158                 :            : {
    1159                 :          0 :         struct gpio_device *gdev = container_of(inode->i_cdev,
    1160                 :            :                                               struct gpio_device, chrdev);
    1161                 :            : 
    1162                 :          0 :         put_device(&gdev->dev);
    1163                 :          0 :         return 0;
    1164                 :            : }
    1165                 :            : 
    1166                 :            : 
    1167                 :            : static const struct file_operations gpio_fileops = {
    1168                 :            :         .release = gpio_chrdev_release,
    1169                 :            :         .open = gpio_chrdev_open,
    1170                 :            :         .owner = THIS_MODULE,
    1171                 :            :         .llseek = no_llseek,
    1172                 :            :         .unlocked_ioctl = gpio_ioctl,
    1173                 :            : #ifdef CONFIG_COMPAT
    1174                 :            :         .compat_ioctl = gpio_ioctl_compat,
    1175                 :            : #endif
    1176                 :            : };
    1177                 :            : 
    1178                 :          0 : static void gpiodevice_release(struct device *dev)
    1179                 :            : {
    1180                 :            :         struct gpio_device *gdev = dev_get_drvdata(dev);
    1181                 :            : 
    1182                 :            :         list_del(&gdev->list);
    1183                 :          0 :         ida_simple_remove(&gpio_ida, gdev->id);
    1184                 :          0 :         kfree_const(gdev->label);
    1185                 :          0 :         kfree(gdev->descs);
    1186                 :          0 :         kfree(gdev);
    1187                 :          0 : }
    1188                 :            : 
    1189                 :        207 : static int gpiochip_setup_dev(struct gpio_device *gdev)
    1190                 :            : {
    1191                 :            :         int ret;
    1192                 :            : 
    1193                 :        207 :         cdev_init(&gdev->chrdev, &gpio_fileops);
    1194                 :        207 :         gdev->chrdev.owner = THIS_MODULE;
    1195                 :        207 :         gdev->dev.devt = MKDEV(MAJOR(gpio_devt), gdev->id);
    1196                 :            : 
    1197                 :        207 :         ret = cdev_device_add(&gdev->chrdev, &gdev->dev);
    1198         [ +  - ]:        207 :         if (ret)
    1199                 :            :                 return ret;
    1200                 :            : 
    1201                 :            :         chip_dbg(gdev->chip, "added GPIO chardev (%d:%d)\n",
    1202                 :            :                  MAJOR(gpio_devt), gdev->id);
    1203                 :            : 
    1204                 :        207 :         ret = gpiochip_sysfs_register(gdev);
    1205         [ +  - ]:        207 :         if (ret)
    1206                 :            :                 goto err_remove_device;
    1207                 :            : 
    1208                 :            :         /* From this point, the .release() function cleans up gpio_device */
    1209                 :        207 :         gdev->dev.release = gpiodevice_release;
    1210                 :            :         pr_debug("%s: registered GPIOs %d to %d on device: %s (%s)\n",
    1211                 :            :                  __func__, gdev->base, gdev->base + gdev->ngpio - 1,
    1212                 :            :                  dev_name(&gdev->dev), gdev->chip->label ? : "generic");
    1213                 :            : 
    1214                 :        207 :         return 0;
    1215                 :            : 
    1216                 :            : err_remove_device:
    1217                 :          0 :         cdev_device_del(&gdev->chrdev, &gdev->dev);
    1218                 :          0 :         return ret;
    1219                 :            : }
    1220                 :            : 
    1221                 :          0 : static void gpiochip_machine_hog(struct gpio_chip *chip, struct gpiod_hog *hog)
    1222                 :            : {
    1223                 :            :         struct gpio_desc *desc;
    1224                 :            :         int rv;
    1225                 :            : 
    1226                 :          0 :         desc = gpiochip_get_desc(chip, hog->chip_hwnum);
    1227         [ #  # ]:          0 :         if (IS_ERR(desc)) {
    1228                 :          0 :                 pr_err("%s: unable to get GPIO desc: %ld\n",
    1229                 :            :                        __func__, PTR_ERR(desc));
    1230                 :          0 :                 return;
    1231                 :            :         }
    1232                 :            : 
    1233         [ #  # ]:          0 :         if (test_bit(FLAG_IS_HOGGED, &desc->flags))
    1234                 :            :                 return;
    1235                 :            : 
    1236                 :          0 :         rv = gpiod_hog(desc, hog->line_name, hog->lflags, hog->dflags);
    1237         [ #  # ]:          0 :         if (rv)
    1238                 :          0 :                 pr_err("%s: unable to hog GPIO line (%s:%u): %d\n",
    1239                 :            :                        __func__, chip->label, hog->chip_hwnum, rv);
    1240                 :            : }
    1241                 :            : 
    1242                 :        207 : static void machine_gpiochip_add(struct gpio_chip *chip)
    1243                 :            : {
    1244                 :            :         struct gpiod_hog *hog;
    1245                 :            : 
    1246                 :        207 :         mutex_lock(&gpio_machine_hogs_mutex);
    1247                 :            : 
    1248         [ -  + ]:        207 :         list_for_each_entry(hog, &gpio_machine_hogs, list) {
    1249         [ #  # ]:          0 :                 if (!strcmp(chip->label, hog->chip_label))
    1250                 :          0 :                         gpiochip_machine_hog(chip, hog);
    1251                 :            :         }
    1252                 :            : 
    1253                 :        207 :         mutex_unlock(&gpio_machine_hogs_mutex);
    1254                 :        207 : }
    1255                 :            : 
    1256                 :        207 : static void gpiochip_setup_devs(void)
    1257                 :            : {
    1258                 :            :         struct gpio_device *gdev;
    1259                 :            :         int ret;
    1260                 :            : 
    1261         [ -  + ]:        207 :         list_for_each_entry(gdev, &gpio_devices, list) {
    1262                 :          0 :                 ret = gpiochip_setup_dev(gdev);
    1263         [ #  # ]:          0 :                 if (ret)
    1264                 :          0 :                         pr_err("%s: Failed to initialize gpio device (%d)\n",
    1265                 :            :                                dev_name(&gdev->dev), ret);
    1266                 :            :         }
    1267                 :        207 : }
    1268                 :            : 
    1269                 :        207 : int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data,
    1270                 :            :                                struct lock_class_key *lock_key,
    1271                 :            :                                struct lock_class_key *request_key)
    1272                 :            : {
    1273                 :            :         unsigned long   flags;
    1274                 :            :         int             ret = 0;
    1275                 :            :         unsigned        i;
    1276                 :        207 :         int             base = chip->base;
    1277                 :            :         struct gpio_device *gdev;
    1278                 :            : 
    1279                 :            :         /*
    1280                 :            :          * First: allocate and populate the internal stat container, and
    1281                 :            :          * set up the struct device.
    1282                 :            :          */
    1283                 :        207 :         gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
    1284         [ +  - ]:        207 :         if (!gdev)
    1285                 :            :                 return -ENOMEM;
    1286                 :        207 :         gdev->dev.bus = &gpio_bus_type;
    1287                 :        207 :         gdev->chip = chip;
    1288                 :        207 :         chip->gpiodev = gdev;
    1289         [ +  - ]:        207 :         if (chip->parent) {
    1290                 :        207 :                 gdev->dev.parent = chip->parent;
    1291                 :        207 :                 gdev->dev.of_node = chip->parent->of_node;
    1292                 :            :         }
    1293                 :            : 
    1294                 :            : #ifdef CONFIG_OF_GPIO
    1295                 :            :         /* If the gpiochip has an assigned OF node this takes precedence */
    1296         [ +  - ]:        207 :         if (chip->of_node)
    1297                 :        207 :                 gdev->dev.of_node = chip->of_node;
    1298                 :            :         else
    1299                 :          0 :                 chip->of_node = gdev->dev.of_node;
    1300                 :            : #endif
    1301                 :            : 
    1302                 :        207 :         gdev->id = ida_simple_get(&gpio_ida, 0, 0, GFP_KERNEL);
    1303         [ +  - ]:        207 :         if (gdev->id < 0) {
    1304                 :            :                 ret = gdev->id;
    1305                 :            :                 goto err_free_gdev;
    1306                 :            :         }
    1307                 :        207 :         dev_set_name(&gdev->dev, "gpiochip%d", gdev->id);
    1308                 :        207 :         device_initialize(&gdev->dev);
    1309                 :            :         dev_set_drvdata(&gdev->dev, gdev);
    1310   [ +  -  +  - ]:        207 :         if (chip->parent && chip->parent->driver)
    1311                 :        207 :                 gdev->owner = chip->parent->driver->owner;
    1312         [ #  # ]:          0 :         else if (chip->owner)
    1313                 :            :                 /* TODO: remove chip->owner */
    1314                 :          0 :                 gdev->owner = chip->owner;
    1315                 :            :         else
    1316                 :          0 :                 gdev->owner = THIS_MODULE;
    1317                 :            : 
    1318                 :        414 :         gdev->descs = kcalloc(chip->ngpio, sizeof(gdev->descs[0]), GFP_KERNEL);
    1319         [ +  - ]:        207 :         if (!gdev->descs) {
    1320                 :            :                 ret = -ENOMEM;
    1321                 :            :                 goto err_free_ida;
    1322                 :            :         }
    1323                 :            : 
    1324         [ -  + ]:        207 :         if (chip->ngpio == 0) {
    1325                 :          0 :                 chip_err(chip, "tried to insert a GPIO chip with zero lines\n");
    1326                 :            :                 ret = -EINVAL;
    1327                 :          0 :                 goto err_free_descs;
    1328                 :            :         }
    1329                 :            : 
    1330         [ -  + ]:        207 :         if (chip->ngpio > FASTPATH_NGPIO)
    1331                 :          0 :                 chip_warn(chip, "line cnt %u is greater than fast path cnt %u\n",
    1332                 :            :                 chip->ngpio, FASTPATH_NGPIO);
    1333                 :            : 
    1334         [ +  - ]:        207 :         gdev->label = kstrdup_const(chip->label ?: "unknown", GFP_KERNEL);
    1335         [ +  - ]:        207 :         if (!gdev->label) {
    1336                 :            :                 ret = -ENOMEM;
    1337                 :            :                 goto err_free_descs;
    1338                 :            :         }
    1339                 :            : 
    1340                 :        207 :         gdev->ngpio = chip->ngpio;
    1341                 :        207 :         gdev->data = data;
    1342                 :            : 
    1343                 :        207 :         spin_lock_irqsave(&gpio_lock, flags);
    1344                 :            : 
    1345                 :            :         /*
    1346                 :            :          * TODO: this allocates a Linux GPIO number base in the global
    1347                 :            :          * GPIO numberspace for this chip. In the long run we want to
    1348                 :            :          * get *rid* of this numberspace and use only descriptors, but
    1349                 :            :          * it may be a pipe dream. It will not happen before we get rid
    1350                 :            :          * of the sysfs interface anyways.
    1351                 :            :          */
    1352         [ -  + ]:        207 :         if (base < 0) {
    1353                 :          0 :                 base = gpiochip_find_base(chip->ngpio);
    1354         [ #  # ]:          0 :                 if (base < 0) {
    1355                 :            :                         ret = base;
    1356                 :            :                         spin_unlock_irqrestore(&gpio_lock, flags);
    1357                 :            :                         goto err_free_label;
    1358                 :            :                 }
    1359                 :            :                 /*
    1360                 :            :                  * TODO: it should not be necessary to reflect the assigned
    1361                 :            :                  * base outside of the GPIO subsystem. Go over drivers and
    1362                 :            :                  * see if anyone makes use of this, else drop this and assign
    1363                 :            :                  * a poison instead.
    1364                 :            :                  */
    1365                 :          0 :                 chip->base = base;
    1366                 :            :         }
    1367                 :        207 :         gdev->base = base;
    1368                 :            : 
    1369                 :        207 :         ret = gpiodev_add_to_list(gdev);
    1370         [ -  + ]:        207 :         if (ret) {
    1371                 :            :                 spin_unlock_irqrestore(&gpio_lock, flags);
    1372                 :            :                 goto err_free_label;
    1373                 :            :         }
    1374                 :            : 
    1375                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    1376                 :            : 
    1377         [ +  + ]:      11385 :         for (i = 0; i < chip->ngpio; i++)
    1378                 :      11178 :                 gdev->descs[i].gdev = gdev;
    1379                 :            : 
    1380                 :            : #ifdef CONFIG_PINCTRL
    1381                 :        207 :         INIT_LIST_HEAD(&gdev->pin_ranges);
    1382                 :            : #endif
    1383                 :            : 
    1384                 :        207 :         ret = gpiochip_set_desc_names(chip);
    1385         [ +  - ]:        207 :         if (ret)
    1386                 :            :                 goto err_remove_from_list;
    1387                 :            : 
    1388                 :        207 :         ret = gpiochip_alloc_valid_mask(chip);
    1389         [ +  - ]:        207 :         if (ret)
    1390                 :            :                 goto err_remove_from_list;
    1391                 :            : 
    1392                 :        207 :         ret = of_gpiochip_add(chip);
    1393         [ +  - ]:        207 :         if (ret)
    1394                 :            :                 goto err_free_gpiochip_mask;
    1395                 :            : 
    1396                 :            :         ret = gpiochip_init_valid_mask(chip);
    1397         [ +  - ]:        207 :         if (ret)
    1398                 :            :                 goto err_remove_of_chip;
    1399                 :            : 
    1400         [ +  + ]:      11178 :         for (i = 0; i < chip->ngpio; i++) {
    1401                 :      11178 :                 struct gpio_desc *desc = &gdev->descs[i];
    1402                 :            : 
    1403   [ +  -  +  - ]:      22356 :                 if (chip->get_direction && gpiochip_line_is_valid(chip, i)) {
    1404         [ +  + ]:      11178 :                         if (!chip->get_direction(chip, i))
    1405                 :       1242 :                                 set_bit(FLAG_IS_OUT, &desc->flags);
    1406                 :            :                         else
    1407                 :       9936 :                                 clear_bit(FLAG_IS_OUT, &desc->flags);
    1408                 :            :                 } else {
    1409         [ #  # ]:          0 :                         if (!chip->direction_input)
    1410                 :          0 :                                 set_bit(FLAG_IS_OUT, &desc->flags);
    1411                 :            :                         else
    1412                 :          0 :                                 clear_bit(FLAG_IS_OUT, &desc->flags);
    1413                 :            :                 }
    1414                 :            :         }
    1415                 :            : 
    1416                 :            :         acpi_gpiochip_add(chip);
    1417                 :            : 
    1418                 :        207 :         machine_gpiochip_add(chip);
    1419                 :            : 
    1420                 :            :         ret = gpiochip_irqchip_init_hw(chip);
    1421         [ +  - ]:        207 :         if (ret)
    1422                 :            :                 goto err_remove_acpi_chip;
    1423                 :            : 
    1424                 :        207 :         ret = gpiochip_irqchip_init_valid_mask(chip);
    1425         [ +  - ]:        207 :         if (ret)
    1426                 :            :                 goto err_remove_acpi_chip;
    1427                 :            : 
    1428                 :        207 :         ret = gpiochip_add_irqchip(chip, lock_key, request_key);
    1429         [ +  - ]:        207 :         if (ret)
    1430                 :            :                 goto err_remove_irqchip_mask;
    1431                 :            : 
    1432                 :            :         /*
    1433                 :            :          * By first adding the chardev, and then adding the device,
    1434                 :            :          * we get a device node entry in sysfs under
    1435                 :            :          * /sys/bus/gpio/devices/gpiochipN/dev that can be used for
    1436                 :            :          * coldplug of device nodes and other udev business.
    1437                 :            :          * We can do this only if gpiolib has been initialized.
    1438                 :            :          * Otherwise, defer until later.
    1439                 :            :          */
    1440         [ +  - ]:        207 :         if (gpiolib_initialized) {
    1441                 :        207 :                 ret = gpiochip_setup_dev(gdev);
    1442         [ -  + ]:        207 :                 if (ret)
    1443                 :            :                         goto err_remove_irqchip;
    1444                 :            :         }
    1445                 :            :         return 0;
    1446                 :            : 
    1447                 :            : err_remove_irqchip:
    1448                 :          0 :         gpiochip_irqchip_remove(chip);
    1449                 :            : err_remove_irqchip_mask:
    1450                 :            :         gpiochip_irqchip_free_valid_mask(chip);
    1451                 :            : err_remove_acpi_chip:
    1452                 :            :         acpi_gpiochip_remove(chip);
    1453                 :            : err_remove_of_chip:
    1454                 :          0 :         gpiochip_free_hogs(chip);
    1455                 :          0 :         of_gpiochip_remove(chip);
    1456                 :            : err_free_gpiochip_mask:
    1457                 :          0 :         gpiochip_remove_pin_ranges(chip);
    1458                 :            :         gpiochip_free_valid_mask(chip);
    1459                 :            : err_remove_from_list:
    1460                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
    1461                 :            :         list_del(&gdev->list);
    1462                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    1463                 :            : err_free_label:
    1464                 :          0 :         kfree_const(gdev->label);
    1465                 :            : err_free_descs:
    1466                 :          0 :         kfree(gdev->descs);
    1467                 :            : err_free_ida:
    1468                 :          0 :         ida_simple_remove(&gpio_ida, gdev->id);
    1469                 :            : err_free_gdev:
    1470                 :            :         /* failures here can mean systems won't boot... */
    1471         [ #  # ]:          0 :         pr_err("%s: GPIOs %d..%d (%s) failed to register, %d\n", __func__,
    1472                 :            :                gdev->base, gdev->base + gdev->ngpio - 1,
    1473                 :            :                chip->label ? : "generic", ret);
    1474                 :          0 :         kfree(gdev);
    1475                 :          0 :         return ret;
    1476                 :            : }
    1477                 :            : EXPORT_SYMBOL_GPL(gpiochip_add_data_with_key);
    1478                 :            : 
    1479                 :            : /**
    1480                 :            :  * gpiochip_get_data() - get per-subdriver data for the chip
    1481                 :            :  * @chip: GPIO chip
    1482                 :            :  *
    1483                 :            :  * Returns:
    1484                 :            :  * The per-subdriver data for the chip.
    1485                 :            :  */
    1486                 :    2436630 : void *gpiochip_get_data(struct gpio_chip *chip)
    1487                 :            : {
    1488                 :    2436630 :         return chip->gpiodev->data;
    1489                 :            : }
    1490                 :            : EXPORT_SYMBOL_GPL(gpiochip_get_data);
    1491                 :            : 
    1492                 :            : /**
    1493                 :            :  * gpiochip_remove() - unregister a gpio_chip
    1494                 :            :  * @chip: the chip to unregister
    1495                 :            :  *
    1496                 :            :  * A gpio_chip with any GPIOs still requested may not be removed.
    1497                 :            :  */
    1498                 :          0 : void gpiochip_remove(struct gpio_chip *chip)
    1499                 :            : {
    1500                 :          0 :         struct gpio_device *gdev = chip->gpiodev;
    1501                 :            :         struct gpio_desc *desc;
    1502                 :            :         unsigned long   flags;
    1503                 :            :         unsigned        i;
    1504                 :            :         bool            requested = false;
    1505                 :            : 
    1506                 :            :         /* FIXME: should the legacy sysfs handling be moved to gpio_device? */
    1507                 :          0 :         gpiochip_sysfs_unregister(gdev);
    1508                 :          0 :         gpiochip_free_hogs(chip);
    1509                 :            :         /* Numb the device, cancelling all outstanding operations */
    1510                 :          0 :         gdev->chip = NULL;
    1511                 :          0 :         gpiochip_irqchip_remove(chip);
    1512                 :            :         acpi_gpiochip_remove(chip);
    1513                 :          0 :         of_gpiochip_remove(chip);
    1514                 :          0 :         gpiochip_remove_pin_ranges(chip);
    1515                 :            :         gpiochip_free_valid_mask(chip);
    1516                 :            :         /*
    1517                 :            :          * We accept no more calls into the driver from this point, so
    1518                 :            :          * NULL the driver data pointer
    1519                 :            :          */
    1520                 :          0 :         gdev->data = NULL;
    1521                 :            : 
    1522                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
    1523         [ #  # ]:          0 :         for (i = 0; i < gdev->ngpio; i++) {
    1524                 :          0 :                 desc = &gdev->descs[i];
    1525         [ #  # ]:          0 :                 if (test_bit(FLAG_REQUESTED, &desc->flags))
    1526                 :            :                         requested = true;
    1527                 :            :         }
    1528                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    1529                 :            : 
    1530         [ #  # ]:          0 :         if (requested)
    1531                 :          0 :                 dev_crit(&gdev->dev,
    1532                 :            :                          "REMOVING GPIOCHIP WITH GPIOS STILL REQUESTED\n");
    1533                 :            : 
    1534                 :            :         /*
    1535                 :            :          * The gpiochip side puts its use of the device to rest here:
    1536                 :            :          * if there are no userspace clients, the chardev and device will
    1537                 :            :          * be removed, else it will be dangling until the last user is
    1538                 :            :          * gone.
    1539                 :            :          */
    1540                 :          0 :         cdev_device_del(&gdev->chrdev, &gdev->dev);
    1541                 :          0 :         put_device(&gdev->dev);
    1542                 :          0 : }
    1543                 :            : EXPORT_SYMBOL_GPL(gpiochip_remove);
    1544                 :            : 
    1545                 :          0 : static void devm_gpio_chip_release(struct device *dev, void *res)
    1546                 :            : {
    1547                 :          0 :         struct gpio_chip *chip = *(struct gpio_chip **)res;
    1548                 :            : 
    1549                 :          0 :         gpiochip_remove(chip);
    1550                 :          0 : }
    1551                 :            : 
    1552                 :            : /**
    1553                 :            :  * devm_gpiochip_add_data() - Resource manager gpiochip_add_data()
    1554                 :            :  * @dev: pointer to the device that gpio_chip belongs to.
    1555                 :            :  * @chip: the chip to register, with chip->base initialized
    1556                 :            :  * @data: driver-private data associated with this chip
    1557                 :            :  *
    1558                 :            :  * Context: potentially before irqs will work
    1559                 :            :  *
    1560                 :            :  * The gpio chip automatically be released when the device is unbound.
    1561                 :            :  *
    1562                 :            :  * Returns:
    1563                 :            :  * A negative errno if the chip can't be registered, such as because the
    1564                 :            :  * chip->base is invalid or already associated with a different chip.
    1565                 :            :  * Otherwise it returns zero as a success code.
    1566                 :            :  */
    1567                 :        207 : int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
    1568                 :            :                            void *data)
    1569                 :            : {
    1570                 :            :         struct gpio_chip **ptr;
    1571                 :            :         int ret;
    1572                 :            : 
    1573                 :            :         ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
    1574                 :            :                              GFP_KERNEL);
    1575         [ +  - ]:        207 :         if (!ptr)
    1576                 :            :                 return -ENOMEM;
    1577                 :            : 
    1578                 :        207 :         ret = gpiochip_add_data(chip, data);
    1579         [ -  + ]:        207 :         if (ret < 0) {
    1580                 :          0 :                 devres_free(ptr);
    1581                 :          0 :                 return ret;
    1582                 :            :         }
    1583                 :            : 
    1584                 :        207 :         *ptr = chip;
    1585                 :        207 :         devres_add(dev, ptr);
    1586                 :            : 
    1587                 :        207 :         return 0;
    1588                 :            : }
    1589                 :            : EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
    1590                 :            : 
    1591                 :            : /**
    1592                 :            :  * gpiochip_find() - iterator for locating a specific gpio_chip
    1593                 :            :  * @data: data to pass to match function
    1594                 :            :  * @match: Callback function to check gpio_chip
    1595                 :            :  *
    1596                 :            :  * Similar to bus_find_device.  It returns a reference to a gpio_chip as
    1597                 :            :  * determined by a user supplied @match callback.  The callback should return
    1598                 :            :  * 0 if the device doesn't match and non-zero if it does.  If the callback is
    1599                 :            :  * non-zero, this function will return to the caller and not iterate over any
    1600                 :            :  * more gpio_chips.
    1601                 :            :  */
    1602                 :        414 : struct gpio_chip *gpiochip_find(void *data,
    1603                 :            :                                 int (*match)(struct gpio_chip *chip,
    1604                 :            :                                              void *data))
    1605                 :            : {
    1606                 :            :         struct gpio_device *gdev;
    1607                 :            :         struct gpio_chip *chip = NULL;
    1608                 :            :         unsigned long flags;
    1609                 :            : 
    1610                 :        414 :         spin_lock_irqsave(&gpio_lock, flags);
    1611         [ +  - ]:        414 :         list_for_each_entry(gdev, &gpio_devices, list)
    1612   [ +  -  +  - ]:        414 :                 if (gdev->chip && match(gdev->chip, data)) {
    1613                 :        414 :                         chip = gdev->chip;
    1614                 :        414 :                         break;
    1615                 :            :                 }
    1616                 :            : 
    1617                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    1618                 :            : 
    1619                 :        414 :         return chip;
    1620                 :            : }
    1621                 :            : EXPORT_SYMBOL_GPL(gpiochip_find);
    1622                 :            : 
    1623                 :          0 : static int gpiochip_match_name(struct gpio_chip *chip, void *data)
    1624                 :            : {
    1625                 :            :         const char *name = data;
    1626                 :            : 
    1627                 :          0 :         return !strcmp(chip->label, name);
    1628                 :            : }
    1629                 :            : 
    1630                 :            : static struct gpio_chip *find_chip_by_name(const char *name)
    1631                 :            : {
    1632                 :          0 :         return gpiochip_find((void *)name, gpiochip_match_name);
    1633                 :            : }
    1634                 :            : 
    1635                 :            : #ifdef CONFIG_GPIOLIB_IRQCHIP
    1636                 :            : 
    1637                 :            : /*
    1638                 :            :  * The following is irqchip helper code for gpiochips.
    1639                 :            :  */
    1640                 :            : 
    1641                 :            : static int gpiochip_irqchip_init_hw(struct gpio_chip *gc)
    1642                 :            : {
    1643                 :            :         struct gpio_irq_chip *girq = &gc->irq;
    1644                 :            : 
    1645         [ -  + ]:        207 :         if (!girq->init_hw)
    1646                 :            :                 return 0;
    1647                 :            : 
    1648                 :          0 :         return girq->init_hw(gc);
    1649                 :            : }
    1650                 :            : 
    1651                 :        207 : static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gc)
    1652                 :            : {
    1653                 :            :         struct gpio_irq_chip *girq = &gc->irq;
    1654                 :            : 
    1655         [ -  + ]:        207 :         if (!girq->init_valid_mask)
    1656                 :            :                 return 0;
    1657                 :            : 
    1658                 :          0 :         girq->valid_mask = gpiochip_allocate_mask(gc);
    1659         [ #  # ]:          0 :         if (!girq->valid_mask)
    1660                 :            :                 return -ENOMEM;
    1661                 :            : 
    1662                 :          0 :         girq->init_valid_mask(gc, girq->valid_mask, gc->ngpio);
    1663                 :            : 
    1664                 :          0 :         return 0;
    1665                 :            : }
    1666                 :            : 
    1667                 :            : static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
    1668                 :            : {
    1669                 :          0 :         bitmap_free(gpiochip->irq.valid_mask);
    1670                 :          0 :         gpiochip->irq.valid_mask = NULL;
    1671                 :            : }
    1672                 :            : 
    1673                 :          0 : bool gpiochip_irqchip_irq_valid(const struct gpio_chip *gpiochip,
    1674                 :            :                                 unsigned int offset)
    1675                 :            : {
    1676         [ #  # ]:          0 :         if (!gpiochip_line_is_valid(gpiochip, offset))
    1677                 :            :                 return false;
    1678                 :            :         /* No mask means all valid */
    1679         [ #  # ]:          0 :         if (likely(!gpiochip->irq.valid_mask))
    1680                 :            :                 return true;
    1681                 :          0 :         return test_bit(offset, gpiochip->irq.valid_mask);
    1682                 :            : }
    1683                 :            : EXPORT_SYMBOL_GPL(gpiochip_irqchip_irq_valid);
    1684                 :            : 
    1685                 :            : /**
    1686                 :            :  * gpiochip_set_cascaded_irqchip() - connects a cascaded irqchip to a gpiochip
    1687                 :            :  * @gc: the gpiochip to set the irqchip chain to
    1688                 :            :  * @parent_irq: the irq number corresponding to the parent IRQ for this
    1689                 :            :  * chained irqchip
    1690                 :            :  * @parent_handler: the parent interrupt handler for the accumulated IRQ
    1691                 :            :  * coming out of the gpiochip. If the interrupt is nested rather than
    1692                 :            :  * cascaded, pass NULL in this handler argument
    1693                 :            :  */
    1694                 :        414 : static void gpiochip_set_cascaded_irqchip(struct gpio_chip *gc,
    1695                 :            :                                           unsigned int parent_irq,
    1696                 :            :                                           irq_flow_handler_t parent_handler)
    1697                 :            : {
    1698                 :            :         struct gpio_irq_chip *girq = &gc->irq;
    1699                 :        414 :         struct device *dev = &gc->gpiodev->dev;
    1700                 :            : 
    1701         [ -  + ]:        414 :         if (!girq->domain) {
    1702                 :          0 :                 chip_err(gc, "called %s before setting up irqchip\n",
    1703                 :            :                          __func__);
    1704                 :          0 :                 return;
    1705                 :            :         }
    1706                 :            : 
    1707         [ +  - ]:        414 :         if (parent_handler) {
    1708         [ -  + ]:        414 :                 if (gc->can_sleep) {
    1709                 :          0 :                         chip_err(gc,
    1710                 :            :                                  "you cannot have chained interrupts on a chip that may sleep\n");
    1711                 :          0 :                         return;
    1712                 :            :                 }
    1713                 :        414 :                 girq->parents = devm_kcalloc(dev, 1,
    1714                 :            :                                              sizeof(*girq->parents),
    1715                 :            :                                              GFP_KERNEL);
    1716         [ -  + ]:        414 :                 if (!girq->parents) {
    1717                 :          0 :                         chip_err(gc, "out of memory allocating parent IRQ\n");
    1718                 :          0 :                         return;
    1719                 :            :                 }
    1720                 :        414 :                 girq->parents[0] = parent_irq;
    1721                 :        414 :                 girq->num_parents = 1;
    1722                 :            :                 /*
    1723                 :            :                  * The parent irqchip is already using the chip_data for this
    1724                 :            :                  * irqchip, so our callbacks simply use the handler_data.
    1725                 :            :                  */
    1726                 :        414 :                 irq_set_chained_handler_and_data(parent_irq, parent_handler,
    1727                 :            :                                                  gc);
    1728                 :            :         }
    1729                 :            : }
    1730                 :            : 
    1731                 :            : /**
    1732                 :            :  * gpiochip_set_chained_irqchip() - connects a chained irqchip to a gpiochip
    1733                 :            :  * @gpiochip: the gpiochip to set the irqchip chain to
    1734                 :            :  * @irqchip: the irqchip to chain to the gpiochip
    1735                 :            :  * @parent_irq: the irq number corresponding to the parent IRQ for this
    1736                 :            :  * chained irqchip
    1737                 :            :  * @parent_handler: the parent interrupt handler for the accumulated IRQ
    1738                 :            :  * coming out of the gpiochip.
    1739                 :            :  */
    1740                 :        414 : void gpiochip_set_chained_irqchip(struct gpio_chip *gpiochip,
    1741                 :            :                                   struct irq_chip *irqchip,
    1742                 :            :                                   unsigned int parent_irq,
    1743                 :            :                                   irq_flow_handler_t parent_handler)
    1744                 :            : {
    1745         [ -  + ]:        414 :         if (gpiochip->irq.threaded) {
    1746                 :          0 :                 chip_err(gpiochip, "tried to chain a threaded gpiochip\n");
    1747                 :        414 :                 return;
    1748                 :            :         }
    1749                 :            : 
    1750                 :        414 :         gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, parent_handler);
    1751                 :            : }
    1752                 :            : EXPORT_SYMBOL_GPL(gpiochip_set_chained_irqchip);
    1753                 :            : 
    1754                 :            : /**
    1755                 :            :  * gpiochip_set_nested_irqchip() - connects a nested irqchip to a gpiochip
    1756                 :            :  * @gpiochip: the gpiochip to set the irqchip nested handler to
    1757                 :            :  * @irqchip: the irqchip to nest to the gpiochip
    1758                 :            :  * @parent_irq: the irq number corresponding to the parent IRQ for this
    1759                 :            :  * nested irqchip
    1760                 :            :  */
    1761                 :          0 : void gpiochip_set_nested_irqchip(struct gpio_chip *gpiochip,
    1762                 :            :                                  struct irq_chip *irqchip,
    1763                 :            :                                  unsigned int parent_irq)
    1764                 :            : {
    1765                 :          0 :         gpiochip_set_cascaded_irqchip(gpiochip, parent_irq, NULL);
    1766                 :          0 : }
    1767                 :            : EXPORT_SYMBOL_GPL(gpiochip_set_nested_irqchip);
    1768                 :            : 
    1769                 :            : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
    1770                 :            : 
    1771                 :            : /**
    1772                 :            :  * gpiochip_set_hierarchical_irqchip() - connects a hierarchical irqchip
    1773                 :            :  * to a gpiochip
    1774                 :            :  * @gc: the gpiochip to set the irqchip hierarchical handler to
    1775                 :            :  * @irqchip: the irqchip to handle this level of the hierarchy, the interrupt
    1776                 :            :  * will then percolate up to the parent
    1777                 :            :  */
    1778                 :          0 : static void gpiochip_set_hierarchical_irqchip(struct gpio_chip *gc,
    1779                 :            :                                               struct irq_chip *irqchip)
    1780                 :            : {
    1781                 :            :         /* DT will deal with mapping each IRQ as we go along */
    1782         [ #  # ]:          0 :         if (is_of_node(gc->irq.fwnode))
    1783                 :            :                 return;
    1784                 :            : 
    1785                 :            :         /*
    1786                 :            :          * This is for legacy and boardfile "irqchip" fwnodes: allocate
    1787                 :            :          * irqs upfront instead of dynamically since we don't have the
    1788                 :            :          * dynamic type of allocation that hardware description languages
    1789                 :            :          * provide. Once all GPIO drivers using board files are gone from
    1790                 :            :          * the kernel we can delete this code, but for a transitional period
    1791                 :            :          * it is necessary to keep this around.
    1792                 :            :          */
    1793         [ #  # ]:          0 :         if (is_fwnode_irqchip(gc->irq.fwnode)) {
    1794                 :            :                 int i;
    1795                 :            :                 int ret;
    1796                 :            : 
    1797         [ #  # ]:          0 :                 for (i = 0; i < gc->ngpio; i++) {
    1798                 :            :                         struct irq_fwspec fwspec;
    1799                 :            :                         unsigned int parent_hwirq;
    1800                 :            :                         unsigned int parent_type;
    1801                 :            :                         struct gpio_irq_chip *girq = &gc->irq;
    1802                 :            : 
    1803                 :            :                         /*
    1804                 :            :                          * We call the child to parent translation function
    1805                 :            :                          * only to check if the child IRQ is valid or not.
    1806                 :            :                          * Just pick the rising edge type here as that is what
    1807                 :            :                          * we likely need to support.
    1808                 :            :                          */
    1809                 :          0 :                         ret = girq->child_to_parent_hwirq(gc, i,
    1810                 :            :                                                           IRQ_TYPE_EDGE_RISING,
    1811                 :            :                                                           &parent_hwirq,
    1812                 :            :                                                           &parent_type);
    1813         [ #  # ]:          0 :                         if (ret) {
    1814                 :          0 :                                 chip_err(gc, "skip set-up on hwirq %d\n",
    1815                 :            :                                          i);
    1816                 :          0 :                                 continue;
    1817                 :            :                         }
    1818                 :            : 
    1819                 :          0 :                         fwspec.fwnode = gc->irq.fwnode;
    1820                 :            :                         /* This is the hwirq for the GPIO line side of things */
    1821                 :          0 :                         fwspec.param[0] = girq->child_offset_to_irq(gc, i);
    1822                 :            :                         /* Just pick something */
    1823                 :          0 :                         fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
    1824                 :          0 :                         fwspec.param_count = 2;
    1825                 :          0 :                         ret = __irq_domain_alloc_irqs(gc->irq.domain,
    1826                 :            :                                                       /* just pick something */
    1827                 :            :                                                       -1,
    1828                 :            :                                                       1,
    1829                 :            :                                                       NUMA_NO_NODE,
    1830                 :            :                                                       &fwspec,
    1831                 :            :                                                       false,
    1832                 :            :                                                       NULL);
    1833         [ #  # ]:          0 :                         if (ret < 0) {
    1834                 :          0 :                                 chip_err(gc,
    1835                 :            :                                          "can not allocate irq for GPIO line %d parent hwirq %d in hierarchy domain: %d\n",
    1836                 :            :                                          i, parent_hwirq,
    1837                 :            :                                          ret);
    1838                 :            :                         }
    1839                 :            :                 }
    1840                 :            :         }
    1841                 :            : 
    1842                 :          0 :         chip_err(gc, "%s unknown fwnode type proceed anyway\n", __func__);
    1843                 :            : 
    1844                 :          0 :         return;
    1845                 :            : }
    1846                 :            : 
    1847                 :          0 : static int gpiochip_hierarchy_irq_domain_translate(struct irq_domain *d,
    1848                 :            :                                                    struct irq_fwspec *fwspec,
    1849                 :            :                                                    unsigned long *hwirq,
    1850                 :            :                                                    unsigned int *type)
    1851                 :            : {
    1852                 :            :         /* We support standard DT translation */
    1853   [ #  #  #  # ]:          0 :         if (is_of_node(fwspec->fwnode) && fwspec->param_count == 2) {
    1854                 :          0 :                 return irq_domain_translate_twocell(d, fwspec, hwirq, type);
    1855                 :            :         }
    1856                 :            : 
    1857                 :            :         /* This is for board files and others not using DT */
    1858         [ #  # ]:          0 :         if (is_fwnode_irqchip(fwspec->fwnode)) {
    1859                 :            :                 int ret;
    1860                 :            : 
    1861                 :          0 :                 ret = irq_domain_translate_twocell(d, fwspec, hwirq, type);
    1862         [ #  # ]:          0 :                 if (ret)
    1863                 :            :                         return ret;
    1864         [ #  # ]:          0 :                 WARN_ON(*type == IRQ_TYPE_NONE);
    1865                 :            :                 return 0;
    1866                 :            :         }
    1867                 :            :         return -EINVAL;
    1868                 :            : }
    1869                 :            : 
    1870                 :          0 : static int gpiochip_hierarchy_irq_domain_alloc(struct irq_domain *d,
    1871                 :            :                                                unsigned int irq,
    1872                 :            :                                                unsigned int nr_irqs,
    1873                 :            :                                                void *data)
    1874                 :            : {
    1875                 :          0 :         struct gpio_chip *gc = d->host_data;
    1876                 :            :         irq_hw_number_t hwirq;
    1877                 :          0 :         unsigned int type = IRQ_TYPE_NONE;
    1878                 :            :         struct irq_fwspec *fwspec = data;
    1879                 :            :         struct irq_fwspec parent_fwspec;
    1880                 :            :         unsigned int parent_hwirq;
    1881                 :            :         unsigned int parent_type;
    1882                 :            :         struct gpio_irq_chip *girq = &gc->irq;
    1883                 :            :         int ret;
    1884                 :            : 
    1885                 :            :         /*
    1886                 :            :          * The nr_irqs parameter is always one except for PCI multi-MSI
    1887                 :            :          * so this should not happen.
    1888                 :            :          */
    1889         [ #  # ]:          0 :         WARN_ON(nr_irqs != 1);
    1890                 :            : 
    1891                 :          0 :         ret = gc->irq.child_irq_domain_ops.translate(d, fwspec, &hwirq, &type);
    1892         [ #  # ]:          0 :         if (ret)
    1893                 :            :                 return ret;
    1894                 :            : 
    1895                 :          0 :         chip_info(gc, "allocate IRQ %d, hwirq %lu\n", irq,  hwirq);
    1896                 :            : 
    1897                 :          0 :         ret = girq->child_to_parent_hwirq(gc, hwirq, type,
    1898                 :            :                                           &parent_hwirq, &parent_type);
    1899         [ #  # ]:          0 :         if (ret) {
    1900                 :          0 :                 chip_err(gc, "can't look up hwirq %lu\n", hwirq);
    1901                 :          0 :                 return ret;
    1902                 :            :         }
    1903                 :          0 :         chip_info(gc, "found parent hwirq %u\n", parent_hwirq);
    1904                 :            : 
    1905                 :            :         /*
    1906                 :            :          * We set handle_bad_irq because the .set_type() should
    1907                 :            :          * always be invoked and set the right type of handler.
    1908                 :            :          */
    1909                 :          0 :         irq_domain_set_info(d,
    1910                 :            :                             irq,
    1911                 :            :                             hwirq,
    1912                 :            :                             gc->irq.chip,
    1913                 :            :                             gc,
    1914                 :            :                             girq->handler,
    1915                 :            :                             NULL, NULL);
    1916                 :            :         irq_set_probe(irq);
    1917                 :            : 
    1918                 :            :         /*
    1919                 :            :          * Create a IRQ fwspec to send up to the parent irqdomain:
    1920                 :            :          * specify the hwirq we address on the parent and tie it
    1921                 :            :          * all together up the chain.
    1922                 :            :          */
    1923                 :          0 :         parent_fwspec.fwnode = d->parent->fwnode;
    1924                 :            :         /* This parent only handles asserted level IRQs */
    1925                 :          0 :         girq->populate_parent_fwspec(gc, &parent_fwspec, parent_hwirq,
    1926                 :            :                                      parent_type);
    1927                 :          0 :         chip_info(gc, "alloc_irqs_parent for %d parent hwirq %d\n",
    1928                 :            :                   irq, parent_hwirq);
    1929                 :            :         irq_set_lockdep_class(irq, gc->irq.lock_key, gc->irq.request_key);
    1930                 :          0 :         ret = irq_domain_alloc_irqs_parent(d, irq, 1, &parent_fwspec);
    1931         [ #  # ]:          0 :         if (ret)
    1932                 :          0 :                 chip_err(gc,
    1933                 :            :                          "failed to allocate parent hwirq %d for hwirq %lu\n",
    1934                 :            :                          parent_hwirq, hwirq);
    1935                 :            : 
    1936                 :          0 :         return ret;
    1937                 :            : }
    1938                 :            : 
    1939                 :          0 : static unsigned int gpiochip_child_offset_to_irq_noop(struct gpio_chip *chip,
    1940                 :            :                                                       unsigned int offset)
    1941                 :            : {
    1942                 :          0 :         return offset;
    1943                 :            : }
    1944                 :            : 
    1945                 :            : static void gpiochip_hierarchy_setup_domain_ops(struct irq_domain_ops *ops)
    1946                 :            : {
    1947                 :          0 :         ops->activate = gpiochip_irq_domain_activate;
    1948                 :          0 :         ops->deactivate = gpiochip_irq_domain_deactivate;
    1949                 :          0 :         ops->alloc = gpiochip_hierarchy_irq_domain_alloc;
    1950                 :          0 :         ops->free = irq_domain_free_irqs_common;
    1951                 :            : 
    1952                 :            :         /*
    1953                 :            :          * We only allow overriding the translate() function for
    1954                 :            :          * hierarchical chips, and this should only be done if the user
    1955                 :            :          * really need something other than 1:1 translation.
    1956                 :            :          */
    1957         [ #  # ]:          0 :         if (!ops->translate)
    1958                 :          0 :                 ops->translate = gpiochip_hierarchy_irq_domain_translate;
    1959                 :            : }
    1960                 :            : 
    1961                 :          0 : static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
    1962                 :            : {
    1963   [ #  #  #  # ]:          0 :         if (!gc->irq.child_to_parent_hwirq ||
    1964                 :          0 :             !gc->irq.fwnode) {
    1965                 :          0 :                 chip_err(gc, "missing irqdomain vital data\n");
    1966                 :          0 :                 return -EINVAL;
    1967                 :            :         }
    1968                 :            : 
    1969         [ #  # ]:          0 :         if (!gc->irq.child_offset_to_irq)
    1970                 :          0 :                 gc->irq.child_offset_to_irq = gpiochip_child_offset_to_irq_noop;
    1971                 :            : 
    1972         [ #  # ]:          0 :         if (!gc->irq.populate_parent_fwspec)
    1973                 :          0 :                 gc->irq.populate_parent_fwspec =
    1974                 :            :                         gpiochip_populate_parent_fwspec_twocell;
    1975                 :            : 
    1976                 :            :         gpiochip_hierarchy_setup_domain_ops(&gc->irq.child_irq_domain_ops);
    1977                 :            : 
    1978                 :          0 :         gc->irq.domain = irq_domain_create_hierarchy(
    1979                 :            :                 gc->irq.parent_domain,
    1980                 :            :                 0,
    1981                 :          0 :                 gc->ngpio,
    1982                 :            :                 gc->irq.fwnode,
    1983                 :          0 :                 &gc->irq.child_irq_domain_ops,
    1984                 :            :                 gc);
    1985                 :            : 
    1986         [ #  # ]:          0 :         if (!gc->irq.domain)
    1987                 :            :                 return -ENOMEM;
    1988                 :            : 
    1989                 :          0 :         gpiochip_set_hierarchical_irqchip(gc, gc->irq.chip);
    1990                 :            : 
    1991                 :          0 :         return 0;
    1992                 :            : }
    1993                 :            : 
    1994                 :            : static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
    1995                 :            : {
    1996                 :          0 :         return !!gc->irq.parent_domain;
    1997                 :            : }
    1998                 :            : 
    1999                 :          0 : void gpiochip_populate_parent_fwspec_twocell(struct gpio_chip *chip,
    2000                 :            :                                              struct irq_fwspec *fwspec,
    2001                 :            :                                              unsigned int parent_hwirq,
    2002                 :            :                                              unsigned int parent_type)
    2003                 :            : {
    2004                 :          0 :         fwspec->param_count = 2;
    2005                 :          0 :         fwspec->param[0] = parent_hwirq;
    2006                 :          0 :         fwspec->param[1] = parent_type;
    2007                 :          0 : }
    2008                 :            : EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_twocell);
    2009                 :            : 
    2010                 :          0 : void gpiochip_populate_parent_fwspec_fourcell(struct gpio_chip *chip,
    2011                 :            :                                               struct irq_fwspec *fwspec,
    2012                 :            :                                               unsigned int parent_hwirq,
    2013                 :            :                                               unsigned int parent_type)
    2014                 :            : {
    2015                 :          0 :         fwspec->param_count = 4;
    2016                 :          0 :         fwspec->param[0] = 0;
    2017                 :          0 :         fwspec->param[1] = parent_hwirq;
    2018                 :          0 :         fwspec->param[2] = 0;
    2019                 :          0 :         fwspec->param[3] = parent_type;
    2020                 :          0 : }
    2021                 :            : EXPORT_SYMBOL_GPL(gpiochip_populate_parent_fwspec_fourcell);
    2022                 :            : 
    2023                 :            : #else
    2024                 :            : 
    2025                 :            : static int gpiochip_hierarchy_add_domain(struct gpio_chip *gc)
    2026                 :            : {
    2027                 :            :         return -EINVAL;
    2028                 :            : }
    2029                 :            : 
    2030                 :            : static bool gpiochip_hierarchy_is_hierarchical(struct gpio_chip *gc)
    2031                 :            : {
    2032                 :            :         return false;
    2033                 :            : }
    2034                 :            : 
    2035                 :            : #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
    2036                 :            : 
    2037                 :            : /**
    2038                 :            :  * gpiochip_irq_map() - maps an IRQ into a GPIO irqchip
    2039                 :            :  * @d: the irqdomain used by this irqchip
    2040                 :            :  * @irq: the global irq number used by this GPIO irqchip irq
    2041                 :            :  * @hwirq: the local IRQ/GPIO line offset on this gpiochip
    2042                 :            :  *
    2043                 :            :  * This function will set up the mapping for a certain IRQ line on a
    2044                 :            :  * gpiochip by assigning the gpiochip as chip data, and using the irqchip
    2045                 :            :  * stored inside the gpiochip.
    2046                 :            :  */
    2047                 :          0 : int gpiochip_irq_map(struct irq_domain *d, unsigned int irq,
    2048                 :            :                      irq_hw_number_t hwirq)
    2049                 :            : {
    2050                 :          0 :         struct gpio_chip *chip = d->host_data;
    2051                 :            :         int ret = 0;
    2052                 :            : 
    2053         [ #  # ]:          0 :         if (!gpiochip_irqchip_irq_valid(chip, hwirq))
    2054                 :            :                 return -ENXIO;
    2055                 :            : 
    2056                 :          0 :         irq_set_chip_data(irq, chip);
    2057                 :            :         /*
    2058                 :            :          * This lock class tells lockdep that GPIO irqs are in a different
    2059                 :            :          * category than their parents, so it won't report false recursion.
    2060                 :            :          */
    2061                 :            :         irq_set_lockdep_class(irq, chip->irq.lock_key, chip->irq.request_key);
    2062                 :          0 :         irq_set_chip_and_handler(irq, chip->irq.chip, chip->irq.handler);
    2063                 :            :         /* Chips that use nested thread handlers have them marked */
    2064         [ #  # ]:          0 :         if (chip->irq.threaded)
    2065                 :          0 :                 irq_set_nested_thread(irq, 1);
    2066                 :            :         irq_set_noprobe(irq);
    2067                 :            : 
    2068         [ #  # ]:          0 :         if (chip->irq.num_parents == 1)
    2069                 :          0 :                 ret = irq_set_parent(irq, chip->irq.parents[0]);
    2070         [ #  # ]:          0 :         else if (chip->irq.map)
    2071                 :          0 :                 ret = irq_set_parent(irq, chip->irq.map[hwirq]);
    2072                 :            : 
    2073         [ #  # ]:          0 :         if (ret < 0)
    2074                 :            :                 return ret;
    2075                 :            : 
    2076                 :            :         /*
    2077                 :            :          * No set-up of the hardware will happen if IRQ_TYPE_NONE
    2078                 :            :          * is passed as default type.
    2079                 :            :          */
    2080         [ #  # ]:          0 :         if (chip->irq.default_type != IRQ_TYPE_NONE)
    2081                 :          0 :                 irq_set_irq_type(irq, chip->irq.default_type);
    2082                 :            : 
    2083                 :            :         return 0;
    2084                 :            : }
    2085                 :            : EXPORT_SYMBOL_GPL(gpiochip_irq_map);
    2086                 :            : 
    2087                 :          0 : void gpiochip_irq_unmap(struct irq_domain *d, unsigned int irq)
    2088                 :            : {
    2089                 :          0 :         struct gpio_chip *chip = d->host_data;
    2090                 :            : 
    2091         [ #  # ]:          0 :         if (chip->irq.threaded)
    2092                 :          0 :                 irq_set_nested_thread(irq, 0);
    2093                 :            :         irq_set_chip_and_handler(irq, NULL, NULL);
    2094                 :          0 :         irq_set_chip_data(irq, NULL);
    2095                 :          0 : }
    2096                 :            : EXPORT_SYMBOL_GPL(gpiochip_irq_unmap);
    2097                 :            : 
    2098                 :            : static const struct irq_domain_ops gpiochip_domain_ops = {
    2099                 :            :         .map    = gpiochip_irq_map,
    2100                 :            :         .unmap  = gpiochip_irq_unmap,
    2101                 :            :         /* Virtually all GPIO irqchips are twocell:ed */
    2102                 :            :         .xlate  = irq_domain_xlate_twocell,
    2103                 :            : };
    2104                 :            : 
    2105                 :            : /*
    2106                 :            :  * TODO: move these activate/deactivate in under the hierarchicial
    2107                 :            :  * irqchip implementation as static once SPMI and SSBI (all external
    2108                 :            :  * users) are phased over.
    2109                 :            :  */
    2110                 :            : /**
    2111                 :            :  * gpiochip_irq_domain_activate() - Lock a GPIO to be used as an IRQ
    2112                 :            :  * @domain: The IRQ domain used by this IRQ chip
    2113                 :            :  * @data: Outermost irq_data associated with the IRQ
    2114                 :            :  * @reserve: If set, only reserve an interrupt vector instead of assigning one
    2115                 :            :  *
    2116                 :            :  * This function is a wrapper that calls gpiochip_lock_as_irq() and is to be
    2117                 :            :  * used as the activate function for the &struct irq_domain_ops. The host_data
    2118                 :            :  * for the IRQ domain must be the &struct gpio_chip.
    2119                 :            :  */
    2120                 :          0 : int gpiochip_irq_domain_activate(struct irq_domain *domain,
    2121                 :            :                                  struct irq_data *data, bool reserve)
    2122                 :            : {
    2123                 :          0 :         struct gpio_chip *chip = domain->host_data;
    2124                 :            : 
    2125                 :          0 :         return gpiochip_lock_as_irq(chip, data->hwirq);
    2126                 :            : }
    2127                 :            : EXPORT_SYMBOL_GPL(gpiochip_irq_domain_activate);
    2128                 :            : 
    2129                 :            : /**
    2130                 :            :  * gpiochip_irq_domain_deactivate() - Unlock a GPIO used as an IRQ
    2131                 :            :  * @domain: The IRQ domain used by this IRQ chip
    2132                 :            :  * @data: Outermost irq_data associated with the IRQ
    2133                 :            :  *
    2134                 :            :  * This function is a wrapper that will call gpiochip_unlock_as_irq() and is to
    2135                 :            :  * be used as the deactivate function for the &struct irq_domain_ops. The
    2136                 :            :  * host_data for the IRQ domain must be the &struct gpio_chip.
    2137                 :            :  */
    2138                 :          0 : void gpiochip_irq_domain_deactivate(struct irq_domain *domain,
    2139                 :            :                                     struct irq_data *data)
    2140                 :            : {
    2141                 :          0 :         struct gpio_chip *chip = domain->host_data;
    2142                 :            : 
    2143                 :          0 :         return gpiochip_unlock_as_irq(chip, data->hwirq);
    2144                 :            : }
    2145                 :            : EXPORT_SYMBOL_GPL(gpiochip_irq_domain_deactivate);
    2146                 :            : 
    2147                 :          0 : static int gpiochip_to_irq(struct gpio_chip *chip, unsigned offset)
    2148                 :            : {
    2149                 :          0 :         struct irq_domain *domain = chip->irq.domain;
    2150                 :            : 
    2151         [ #  # ]:          0 :         if (!gpiochip_irqchip_irq_valid(chip, offset))
    2152                 :            :                 return -ENXIO;
    2153                 :            : 
    2154                 :            : #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
    2155         [ #  # ]:          0 :         if (irq_domain_is_hierarchy(domain)) {
    2156                 :            :                 struct irq_fwspec spec;
    2157                 :            : 
    2158                 :          0 :                 spec.fwnode = domain->fwnode;
    2159                 :          0 :                 spec.param_count = 2;
    2160                 :          0 :                 spec.param[0] = chip->irq.child_offset_to_irq(chip, offset);
    2161                 :          0 :                 spec.param[1] = IRQ_TYPE_NONE;
    2162                 :            : 
    2163                 :          0 :                 return irq_create_fwspec_mapping(&spec);
    2164                 :            :         }
    2165                 :            : #endif
    2166                 :            : 
    2167                 :          0 :         return irq_create_mapping(domain, offset);
    2168                 :            : }
    2169                 :            : 
    2170                 :          0 : static int gpiochip_irq_reqres(struct irq_data *d)
    2171                 :            : {
    2172                 :            :         struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
    2173                 :            : 
    2174                 :          0 :         return gpiochip_reqres_irq(chip, d->hwirq);
    2175                 :            : }
    2176                 :            : 
    2177                 :          0 : static void gpiochip_irq_relres(struct irq_data *d)
    2178                 :            : {
    2179                 :            :         struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
    2180                 :            : 
    2181                 :          0 :         gpiochip_relres_irq(chip, d->hwirq);
    2182                 :          0 : }
    2183                 :            : 
    2184                 :          0 : static void gpiochip_irq_enable(struct irq_data *d)
    2185                 :            : {
    2186                 :            :         struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
    2187                 :            : 
    2188                 :          0 :         gpiochip_enable_irq(chip, d->hwirq);
    2189         [ #  # ]:          0 :         if (chip->irq.irq_enable)
    2190                 :          0 :                 chip->irq.irq_enable(d);
    2191                 :            :         else
    2192                 :          0 :                 chip->irq.chip->irq_unmask(d);
    2193                 :          0 : }
    2194                 :            : 
    2195                 :          0 : static void gpiochip_irq_disable(struct irq_data *d)
    2196                 :            : {
    2197                 :            :         struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
    2198                 :            : 
    2199                 :            :         /*
    2200                 :            :          * Since we override .irq_disable() we need to mimic the
    2201                 :            :          * behaviour of __irq_disable() in irq/chip.c.
    2202                 :            :          * First call .irq_disable() if it exists, else mimic the
    2203                 :            :          * behaviour of mask_irq() which calls .irq_mask() if
    2204                 :            :          * it exists.
    2205                 :            :          */
    2206         [ #  # ]:          0 :         if (chip->irq.irq_disable)
    2207                 :          0 :                 chip->irq.irq_disable(d);
    2208         [ #  # ]:          0 :         else if (chip->irq.chip->irq_mask)
    2209                 :          0 :                 chip->irq.chip->irq_mask(d);
    2210                 :          0 :         gpiochip_disable_irq(chip, d->hwirq);
    2211                 :          0 : }
    2212                 :            : 
    2213                 :        207 : static void gpiochip_set_irq_hooks(struct gpio_chip *gpiochip)
    2214                 :            : {
    2215                 :        207 :         struct irq_chip *irqchip = gpiochip->irq.chip;
    2216                 :            : 
    2217   [ +  -  +  - ]:        414 :         if (!irqchip->irq_request_resources &&
    2218                 :        207 :             !irqchip->irq_release_resources) {
    2219                 :        207 :                 irqchip->irq_request_resources = gpiochip_irq_reqres;
    2220                 :        207 :                 irqchip->irq_release_resources = gpiochip_irq_relres;
    2221                 :            :         }
    2222   [ -  +  +  - ]:        207 :         if (WARN_ON(gpiochip->irq.irq_enable))
    2223                 :            :                 return;
    2224                 :            :         /* Check if the irqchip already has this hook... */
    2225         [ -  + ]:        207 :         if (irqchip->irq_enable == gpiochip_irq_enable) {
    2226                 :            :                 /*
    2227                 :            :                  * ...and if so, give a gentle warning that this is bad
    2228                 :            :                  * practice.
    2229                 :            :                  */
    2230                 :          0 :                 chip_info(gpiochip,
    2231                 :            :                           "detected irqchip that is shared with multiple gpiochips: please fix the driver.\n");
    2232                 :          0 :                 return;
    2233                 :            :         }
    2234                 :        207 :         gpiochip->irq.irq_enable = irqchip->irq_enable;
    2235                 :        207 :         gpiochip->irq.irq_disable = irqchip->irq_disable;
    2236                 :        207 :         irqchip->irq_enable = gpiochip_irq_enable;
    2237                 :        207 :         irqchip->irq_disable = gpiochip_irq_disable;
    2238                 :            : }
    2239                 :            : 
    2240                 :            : /**
    2241                 :            :  * gpiochip_add_irqchip() - adds an IRQ chip to a GPIO chip
    2242                 :            :  * @gpiochip: the GPIO chip to add the IRQ chip to
    2243                 :            :  * @lock_key: lockdep class for IRQ lock
    2244                 :            :  * @request_key: lockdep class for IRQ request
    2245                 :            :  */
    2246                 :        207 : static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
    2247                 :            :                                 struct lock_class_key *lock_key,
    2248                 :            :                                 struct lock_class_key *request_key)
    2249                 :            : {
    2250                 :        207 :         struct irq_chip *irqchip = gpiochip->irq.chip;
    2251                 :            :         const struct irq_domain_ops *ops = NULL;
    2252                 :            :         struct device_node *np;
    2253                 :            :         unsigned int type;
    2254                 :            :         unsigned int i;
    2255                 :            : 
    2256         [ -  + ]:        207 :         if (!irqchip)
    2257                 :            :                 return 0;
    2258                 :            : 
    2259   [ #  #  #  # ]:          0 :         if (gpiochip->irq.parent_handler && gpiochip->can_sleep) {
    2260                 :          0 :                 chip_err(gpiochip, "you cannot have chained interrupts on a chip that may sleep\n");
    2261                 :          0 :                 return -EINVAL;
    2262                 :            :         }
    2263                 :            : 
    2264                 :          0 :         np = gpiochip->gpiodev->dev.of_node;
    2265                 :          0 :         type = gpiochip->irq.default_type;
    2266                 :            : 
    2267                 :            :         /*
    2268                 :            :          * Specifying a default trigger is a terrible idea if DT or ACPI is
    2269                 :            :          * used to configure the interrupts, as you may end up with
    2270                 :            :          * conflicting triggers. Tell the user, and reset to NONE.
    2271                 :            :          */
    2272   [ #  #  #  # ]:          0 :         if (WARN(np && type != IRQ_TYPE_NONE,
    2273                 :            :                  "%s: Ignoring %u default trigger\n", np->full_name, type))
    2274                 :            :                 type = IRQ_TYPE_NONE;
    2275                 :            : 
    2276                 :            :         if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
    2277                 :            :                 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
    2278                 :            :                                  "Ignoring %u default trigger\n", type);
    2279                 :            :                 type = IRQ_TYPE_NONE;
    2280                 :            :         }
    2281                 :            : 
    2282                 :          0 :         gpiochip->to_irq = gpiochip_to_irq;
    2283                 :          0 :         gpiochip->irq.default_type = type;
    2284                 :          0 :         gpiochip->irq.lock_key = lock_key;
    2285                 :          0 :         gpiochip->irq.request_key = request_key;
    2286                 :            : 
    2287                 :            :         /* If a parent irqdomain is provided, let's build a hierarchy */
    2288         [ #  # ]:          0 :         if (gpiochip_hierarchy_is_hierarchical(gpiochip)) {
    2289                 :          0 :                 int ret = gpiochip_hierarchy_add_domain(gpiochip);
    2290         [ #  # ]:          0 :                 if (ret)
    2291                 :            :                         return ret;
    2292                 :            :         } else {
    2293                 :            :                 /* Some drivers provide custom irqdomain ops */
    2294         [ #  # ]:          0 :                 if (gpiochip->irq.domain_ops)
    2295                 :            :                         ops = gpiochip->irq.domain_ops;
    2296                 :            : 
    2297         [ #  # ]:          0 :                 if (!ops)
    2298                 :            :                         ops = &gpiochip_domain_ops;
    2299                 :          0 :                 gpiochip->irq.domain = irq_domain_add_simple(np,
    2300                 :          0 :                         gpiochip->ngpio,
    2301                 :            :                         gpiochip->irq.first,
    2302                 :            :                         ops, gpiochip);
    2303         [ #  # ]:          0 :                 if (!gpiochip->irq.domain)
    2304                 :            :                         return -EINVAL;
    2305                 :            :         }
    2306                 :            : 
    2307         [ #  # ]:          0 :         if (gpiochip->irq.parent_handler) {
    2308         [ #  # ]:          0 :                 void *data = gpiochip->irq.parent_handler_data ?: gpiochip;
    2309                 :            : 
    2310         [ #  # ]:          0 :                 for (i = 0; i < gpiochip->irq.num_parents; i++) {
    2311                 :            :                         /*
    2312                 :            :                          * The parent IRQ chip is already using the chip_data
    2313                 :            :                          * for this IRQ chip, so our callbacks simply use the
    2314                 :            :                          * handler_data.
    2315                 :            :                          */
    2316                 :          0 :                         irq_set_chained_handler_and_data(gpiochip->irq.parents[i],
    2317                 :            :                                                          gpiochip->irq.parent_handler,
    2318                 :            :                                                          data);
    2319                 :            :                 }
    2320                 :            :         }
    2321                 :            : 
    2322                 :          0 :         gpiochip_set_irq_hooks(gpiochip);
    2323                 :            : 
    2324                 :            :         acpi_gpiochip_request_interrupts(gpiochip);
    2325                 :            : 
    2326                 :          0 :         return 0;
    2327                 :            : }
    2328                 :            : 
    2329                 :            : /**
    2330                 :            :  * gpiochip_irqchip_remove() - removes an irqchip added to a gpiochip
    2331                 :            :  * @gpiochip: the gpiochip to remove the irqchip from
    2332                 :            :  *
    2333                 :            :  * This is called only from gpiochip_remove()
    2334                 :            :  */
    2335                 :          0 : static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip)
    2336                 :            : {
    2337                 :          0 :         struct irq_chip *irqchip = gpiochip->irq.chip;
    2338                 :            :         unsigned int offset;
    2339                 :            : 
    2340                 :            :         acpi_gpiochip_free_interrupts(gpiochip);
    2341                 :            : 
    2342   [ #  #  #  # ]:          0 :         if (irqchip && gpiochip->irq.parent_handler) {
    2343                 :            :                 struct gpio_irq_chip *irq = &gpiochip->irq;
    2344                 :            :                 unsigned int i;
    2345                 :            : 
    2346         [ #  # ]:          0 :                 for (i = 0; i < irq->num_parents; i++)
    2347                 :          0 :                         irq_set_chained_handler_and_data(irq->parents[i],
    2348                 :            :                                                          NULL, NULL);
    2349                 :            :         }
    2350                 :            : 
    2351                 :            :         /* Remove all IRQ mappings and delete the domain */
    2352         [ #  # ]:          0 :         if (gpiochip->irq.domain) {
    2353                 :            :                 unsigned int irq;
    2354                 :            : 
    2355         [ #  # ]:          0 :                 for (offset = 0; offset < gpiochip->ngpio; offset++) {
    2356         [ #  # ]:          0 :                         if (!gpiochip_irqchip_irq_valid(gpiochip, offset))
    2357                 :          0 :                                 continue;
    2358                 :            : 
    2359                 :          0 :                         irq = irq_find_mapping(gpiochip->irq.domain, offset);
    2360                 :          0 :                         irq_dispose_mapping(irq);
    2361                 :            :                 }
    2362                 :            : 
    2363                 :          0 :                 irq_domain_remove(gpiochip->irq.domain);
    2364                 :            :         }
    2365                 :            : 
    2366         [ #  # ]:          0 :         if (irqchip) {
    2367         [ #  # ]:          0 :                 if (irqchip->irq_request_resources == gpiochip_irq_reqres) {
    2368                 :          0 :                         irqchip->irq_request_resources = NULL;
    2369                 :          0 :                         irqchip->irq_release_resources = NULL;
    2370                 :            :                 }
    2371         [ #  # ]:          0 :                 if (irqchip->irq_enable == gpiochip_irq_enable) {
    2372                 :          0 :                         irqchip->irq_enable = gpiochip->irq.irq_enable;
    2373                 :          0 :                         irqchip->irq_disable = gpiochip->irq.irq_disable;
    2374                 :            :                 }
    2375                 :            :         }
    2376                 :          0 :         gpiochip->irq.irq_enable = NULL;
    2377                 :          0 :         gpiochip->irq.irq_disable = NULL;
    2378                 :          0 :         gpiochip->irq.chip = NULL;
    2379                 :            : 
    2380                 :            :         gpiochip_irqchip_free_valid_mask(gpiochip);
    2381                 :          0 : }
    2382                 :            : 
    2383                 :            : /**
    2384                 :            :  * gpiochip_irqchip_add_key() - adds an irqchip to a gpiochip
    2385                 :            :  * @gpiochip: the gpiochip to add the irqchip to
    2386                 :            :  * @irqchip: the irqchip to add to the gpiochip
    2387                 :            :  * @first_irq: if not dynamically assigned, the base (first) IRQ to
    2388                 :            :  * allocate gpiochip irqs from
    2389                 :            :  * @handler: the irq handler to use (often a predefined irq core function)
    2390                 :            :  * @type: the default type for IRQs on this irqchip, pass IRQ_TYPE_NONE
    2391                 :            :  * to have the core avoid setting up any default type in the hardware.
    2392                 :            :  * @threaded: whether this irqchip uses a nested thread handler
    2393                 :            :  * @lock_key: lockdep class for IRQ lock
    2394                 :            :  * @request_key: lockdep class for IRQ request
    2395                 :            :  *
    2396                 :            :  * This function closely associates a certain irqchip with a certain
    2397                 :            :  * gpiochip, providing an irq domain to translate the local IRQs to
    2398                 :            :  * global irqs in the gpiolib core, and making sure that the gpiochip
    2399                 :            :  * is passed as chip data to all related functions. Driver callbacks
    2400                 :            :  * need to use gpiochip_get_data() to get their local state containers back
    2401                 :            :  * from the gpiochip passed as chip data. An irqdomain will be stored
    2402                 :            :  * in the gpiochip that shall be used by the driver to handle IRQ number
    2403                 :            :  * translation. The gpiochip will need to be initialized and registered
    2404                 :            :  * before calling this function.
    2405                 :            :  *
    2406                 :            :  * This function will handle two cell:ed simple IRQs and assumes all
    2407                 :            :  * the pins on the gpiochip can generate a unique IRQ. Everything else
    2408                 :            :  * need to be open coded.
    2409                 :            :  */
    2410                 :        207 : int gpiochip_irqchip_add_key(struct gpio_chip *gpiochip,
    2411                 :            :                              struct irq_chip *irqchip,
    2412                 :            :                              unsigned int first_irq,
    2413                 :            :                              irq_flow_handler_t handler,
    2414                 :            :                              unsigned int type,
    2415                 :            :                              bool threaded,
    2416                 :            :                              struct lock_class_key *lock_key,
    2417                 :            :                              struct lock_class_key *request_key)
    2418                 :            : {
    2419                 :            :         struct device_node *of_node;
    2420                 :            : 
    2421         [ +  - ]:        207 :         if (!gpiochip || !irqchip)
    2422                 :            :                 return -EINVAL;
    2423                 :            : 
    2424         [ -  + ]:        207 :         if (!gpiochip->parent) {
    2425                 :          0 :                 pr_err("missing gpiochip .dev parent pointer\n");
    2426                 :          0 :                 return -EINVAL;
    2427                 :            :         }
    2428                 :        207 :         gpiochip->irq.threaded = threaded;
    2429                 :        207 :         of_node = gpiochip->parent->of_node;
    2430                 :            : #ifdef CONFIG_OF_GPIO
    2431                 :            :         /*
    2432                 :            :          * If the gpiochip has an assigned OF node this takes precedence
    2433                 :            :          * FIXME: get rid of this and use gpiochip->parent->of_node
    2434                 :            :          * everywhere
    2435                 :            :          */
    2436         [ +  - ]:        207 :         if (gpiochip->of_node)
    2437                 :            :                 of_node = gpiochip->of_node;
    2438                 :            : #endif
    2439                 :            :         /*
    2440                 :            :          * Specifying a default trigger is a terrible idea if DT or ACPI is
    2441                 :            :          * used to configure the interrupts, as you may end-up with
    2442                 :            :          * conflicting triggers. Tell the user, and reset to NONE.
    2443                 :            :          */
    2444   [ -  +  -  + ]:        207 :         if (WARN(of_node && type != IRQ_TYPE_NONE,
    2445                 :            :                  "%pOF: Ignoring %d default trigger\n", of_node, type))
    2446                 :            :                 type = IRQ_TYPE_NONE;
    2447                 :            :         if (has_acpi_companion(gpiochip->parent) && type != IRQ_TYPE_NONE) {
    2448                 :            :                 acpi_handle_warn(ACPI_HANDLE(gpiochip->parent),
    2449                 :            :                                  "Ignoring %d default trigger\n", type);
    2450                 :            :                 type = IRQ_TYPE_NONE;
    2451                 :            :         }
    2452                 :            : 
    2453                 :        207 :         gpiochip->irq.chip = irqchip;
    2454                 :        207 :         gpiochip->irq.handler = handler;
    2455                 :        207 :         gpiochip->irq.default_type = type;
    2456                 :        207 :         gpiochip->to_irq = gpiochip_to_irq;
    2457                 :        207 :         gpiochip->irq.lock_key = lock_key;
    2458                 :        207 :         gpiochip->irq.request_key = request_key;
    2459                 :        207 :         gpiochip->irq.domain = irq_domain_add_simple(of_node,
    2460                 :        207 :                                         gpiochip->ngpio, first_irq,
    2461                 :            :                                         &gpiochip_domain_ops, gpiochip);
    2462         [ -  + ]:        207 :         if (!gpiochip->irq.domain) {
    2463                 :          0 :                 gpiochip->irq.chip = NULL;
    2464                 :          0 :                 return -EINVAL;
    2465                 :            :         }
    2466                 :            : 
    2467                 :        207 :         gpiochip_set_irq_hooks(gpiochip);
    2468                 :            : 
    2469                 :            :         acpi_gpiochip_request_interrupts(gpiochip);
    2470                 :            : 
    2471                 :        207 :         return 0;
    2472                 :            : }
    2473                 :            : EXPORT_SYMBOL_GPL(gpiochip_irqchip_add_key);
    2474                 :            : 
    2475                 :            : #else /* CONFIG_GPIOLIB_IRQCHIP */
    2476                 :            : 
    2477                 :            : static inline int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
    2478                 :            :                                        struct lock_class_key *lock_key,
    2479                 :            :                                        struct lock_class_key *request_key)
    2480                 :            : {
    2481                 :            :         return 0;
    2482                 :            : }
    2483                 :            : static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip) {}
    2484                 :            : 
    2485                 :            : static inline int gpiochip_irqchip_init_hw(struct gpio_chip *gpiochip)
    2486                 :            : {
    2487                 :            :         return 0;
    2488                 :            : }
    2489                 :            : 
    2490                 :            : static inline int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip)
    2491                 :            : {
    2492                 :            :         return 0;
    2493                 :            : }
    2494                 :            : static inline void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip)
    2495                 :            : { }
    2496                 :            : 
    2497                 :            : #endif /* CONFIG_GPIOLIB_IRQCHIP */
    2498                 :            : 
    2499                 :            : /**
    2500                 :            :  * gpiochip_generic_request() - request the gpio function for a pin
    2501                 :            :  * @chip: the gpiochip owning the GPIO
    2502                 :            :  * @offset: the offset of the GPIO to request for GPIO function
    2503                 :            :  */
    2504                 :        414 : int gpiochip_generic_request(struct gpio_chip *chip, unsigned offset)
    2505                 :            : {
    2506                 :        414 :         return pinctrl_gpio_request(chip->gpiodev->base + offset);
    2507                 :            : }
    2508                 :            : EXPORT_SYMBOL_GPL(gpiochip_generic_request);
    2509                 :            : 
    2510                 :            : /**
    2511                 :            :  * gpiochip_generic_free() - free the gpio function from a pin
    2512                 :            :  * @chip: the gpiochip to request the gpio function for
    2513                 :            :  * @offset: the offset of the GPIO to free from GPIO function
    2514                 :            :  */
    2515                 :          0 : void gpiochip_generic_free(struct gpio_chip *chip, unsigned offset)
    2516                 :            : {
    2517                 :          0 :         pinctrl_gpio_free(chip->gpiodev->base + offset);
    2518                 :          0 : }
    2519                 :            : EXPORT_SYMBOL_GPL(gpiochip_generic_free);
    2520                 :            : 
    2521                 :            : /**
    2522                 :            :  * gpiochip_generic_config() - apply configuration for a pin
    2523                 :            :  * @chip: the gpiochip owning the GPIO
    2524                 :            :  * @offset: the offset of the GPIO to apply the configuration
    2525                 :            :  * @config: the configuration to be applied
    2526                 :            :  */
    2527                 :        828 : int gpiochip_generic_config(struct gpio_chip *chip, unsigned offset,
    2528                 :            :                             unsigned long config)
    2529                 :            : {
    2530                 :        828 :         return pinctrl_gpio_set_config(chip->gpiodev->base + offset, config);
    2531                 :            : }
    2532                 :            : EXPORT_SYMBOL_GPL(gpiochip_generic_config);
    2533                 :            : 
    2534                 :            : #ifdef CONFIG_PINCTRL
    2535                 :            : 
    2536                 :            : /**
    2537                 :            :  * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping
    2538                 :            :  * @chip: the gpiochip to add the range for
    2539                 :            :  * @pctldev: the pin controller to map to
    2540                 :            :  * @gpio_offset: the start offset in the current gpio_chip number space
    2541                 :            :  * @pin_group: name of the pin group inside the pin controller
    2542                 :            :  *
    2543                 :            :  * Calling this function directly from a DeviceTree-supported
    2544                 :            :  * pinctrl driver is DEPRECATED. Please see Section 2.1 of
    2545                 :            :  * Documentation/devicetree/bindings/gpio/gpio.txt on how to
    2546                 :            :  * bind pinctrl and gpio drivers via the "gpio-ranges" property.
    2547                 :            :  */
    2548                 :          0 : int gpiochip_add_pingroup_range(struct gpio_chip *chip,
    2549                 :            :                         struct pinctrl_dev *pctldev,
    2550                 :            :                         unsigned int gpio_offset, const char *pin_group)
    2551                 :            : {
    2552                 :            :         struct gpio_pin_range *pin_range;
    2553                 :          0 :         struct gpio_device *gdev = chip->gpiodev;
    2554                 :            :         int ret;
    2555                 :            : 
    2556                 :          0 :         pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
    2557         [ #  # ]:          0 :         if (!pin_range) {
    2558                 :          0 :                 chip_err(chip, "failed to allocate pin ranges\n");
    2559                 :          0 :                 return -ENOMEM;
    2560                 :            :         }
    2561                 :            : 
    2562                 :            :         /* Use local offset as range ID */
    2563                 :          0 :         pin_range->range.id = gpio_offset;
    2564                 :          0 :         pin_range->range.gc = chip;
    2565                 :          0 :         pin_range->range.name = chip->label;
    2566                 :          0 :         pin_range->range.base = gdev->base + gpio_offset;
    2567                 :          0 :         pin_range->pctldev = pctldev;
    2568                 :            : 
    2569                 :          0 :         ret = pinctrl_get_group_pins(pctldev, pin_group,
    2570                 :            :                                         &pin_range->range.pins,
    2571                 :            :                                         &pin_range->range.npins);
    2572         [ #  # ]:          0 :         if (ret < 0) {
    2573                 :          0 :                 kfree(pin_range);
    2574                 :          0 :                 return ret;
    2575                 :            :         }
    2576                 :            : 
    2577                 :          0 :         pinctrl_add_gpio_range(pctldev, &pin_range->range);
    2578                 :            : 
    2579                 :            :         chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
    2580                 :            :                  gpio_offset, gpio_offset + pin_range->range.npins - 1,
    2581                 :            :                  pinctrl_dev_get_devname(pctldev), pin_group);
    2582                 :            : 
    2583                 :          0 :         list_add_tail(&pin_range->node, &gdev->pin_ranges);
    2584                 :            : 
    2585                 :          0 :         return 0;
    2586                 :            : }
    2587                 :            : EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
    2588                 :            : 
    2589                 :            : /**
    2590                 :            :  * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping
    2591                 :            :  * @chip: the gpiochip to add the range for
    2592                 :            :  * @pinctl_name: the dev_name() of the pin controller to map to
    2593                 :            :  * @gpio_offset: the start offset in the current gpio_chip number space
    2594                 :            :  * @pin_offset: the start offset in the pin controller number space
    2595                 :            :  * @npins: the number of pins from the offset of each pin space (GPIO and
    2596                 :            :  *      pin controller) to accumulate in this range
    2597                 :            :  *
    2598                 :            :  * Returns:
    2599                 :            :  * 0 on success, or a negative error-code on failure.
    2600                 :            :  *
    2601                 :            :  * Calling this function directly from a DeviceTree-supported
    2602                 :            :  * pinctrl driver is DEPRECATED. Please see Section 2.1 of
    2603                 :            :  * Documentation/devicetree/bindings/gpio/gpio.txt on how to
    2604                 :            :  * bind pinctrl and gpio drivers via the "gpio-ranges" property.
    2605                 :            :  */
    2606                 :          0 : int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
    2607                 :            :                            unsigned int gpio_offset, unsigned int pin_offset,
    2608                 :            :                            unsigned int npins)
    2609                 :            : {
    2610                 :            :         struct gpio_pin_range *pin_range;
    2611                 :          0 :         struct gpio_device *gdev = chip->gpiodev;
    2612                 :            :         int ret;
    2613                 :            : 
    2614                 :          0 :         pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
    2615         [ #  # ]:          0 :         if (!pin_range) {
    2616                 :          0 :                 chip_err(chip, "failed to allocate pin ranges\n");
    2617                 :          0 :                 return -ENOMEM;
    2618                 :            :         }
    2619                 :            : 
    2620                 :            :         /* Use local offset as range ID */
    2621                 :          0 :         pin_range->range.id = gpio_offset;
    2622                 :          0 :         pin_range->range.gc = chip;
    2623                 :          0 :         pin_range->range.name = chip->label;
    2624                 :          0 :         pin_range->range.base = gdev->base + gpio_offset;
    2625                 :          0 :         pin_range->range.pin_base = pin_offset;
    2626                 :          0 :         pin_range->range.npins = npins;
    2627                 :          0 :         pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name,
    2628                 :            :                         &pin_range->range);
    2629         [ #  # ]:          0 :         if (IS_ERR(pin_range->pctldev)) {
    2630                 :            :                 ret = PTR_ERR(pin_range->pctldev);
    2631                 :          0 :                 chip_err(chip, "could not create pin range\n");
    2632                 :          0 :                 kfree(pin_range);
    2633                 :          0 :                 return ret;
    2634                 :            :         }
    2635                 :            :         chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
    2636                 :            :                  gpio_offset, gpio_offset + npins - 1,
    2637                 :            :                  pinctl_name,
    2638                 :            :                  pin_offset, pin_offset + npins - 1);
    2639                 :            : 
    2640                 :          0 :         list_add_tail(&pin_range->node, &gdev->pin_ranges);
    2641                 :            : 
    2642                 :          0 :         return 0;
    2643                 :            : }
    2644                 :            : EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
    2645                 :            : 
    2646                 :            : /**
    2647                 :            :  * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings
    2648                 :            :  * @chip: the chip to remove all the mappings for
    2649                 :            :  */
    2650                 :          0 : void gpiochip_remove_pin_ranges(struct gpio_chip *chip)
    2651                 :            : {
    2652                 :            :         struct gpio_pin_range *pin_range, *tmp;
    2653                 :          0 :         struct gpio_device *gdev = chip->gpiodev;
    2654                 :            : 
    2655         [ #  # ]:          0 :         list_for_each_entry_safe(pin_range, tmp, &gdev->pin_ranges, node) {
    2656                 :            :                 list_del(&pin_range->node);
    2657                 :          0 :                 pinctrl_remove_gpio_range(pin_range->pctldev,
    2658                 :            :                                 &pin_range->range);
    2659                 :          0 :                 kfree(pin_range);
    2660                 :            :         }
    2661                 :          0 : }
    2662                 :            : EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges);
    2663                 :            : 
    2664                 :            : #endif /* CONFIG_PINCTRL */
    2665                 :            : 
    2666                 :            : /* These "optional" allocation calls help prevent drivers from stomping
    2667                 :            :  * on each other, and help provide better diagnostics in debugfs.
    2668                 :            :  * They're called even less than the "set direction" calls.
    2669                 :            :  */
    2670                 :        414 : static int gpiod_request_commit(struct gpio_desc *desc, const char *label)
    2671                 :            : {
    2672                 :        414 :         struct gpio_chip        *chip = desc->gdev->chip;
    2673                 :            :         int                     ret;
    2674                 :            :         unsigned long           flags;
    2675                 :            :         unsigned                offset;
    2676                 :            : 
    2677         [ -  + ]:        414 :         if (label) {
    2678                 :          0 :                 label = kstrdup_const(label, GFP_KERNEL);
    2679         [ #  # ]:          0 :                 if (!label)
    2680                 :            :                         return -ENOMEM;
    2681                 :            :         }
    2682                 :            : 
    2683                 :        414 :         spin_lock_irqsave(&gpio_lock, flags);
    2684                 :            : 
    2685                 :            :         /* NOTE:  gpio_request() can be called in early boot,
    2686                 :            :          * before IRQs are enabled, for non-sleeping (SOC) GPIOs.
    2687                 :            :          */
    2688                 :            : 
    2689         [ +  - ]:        414 :         if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) {
    2690         [ -  + ]:        414 :                 desc_set_label(desc, label ? : "?");
    2691                 :            :                 ret = 0;
    2692                 :            :         } else {
    2693                 :          0 :                 kfree_const(label);
    2694                 :            :                 ret = -EBUSY;
    2695                 :          0 :                 goto done;
    2696                 :            :         }
    2697                 :            : 
    2698         [ +  - ]:        414 :         if (chip->request) {
    2699                 :            :                 /* chip->request may sleep */
    2700                 :            :                 spin_unlock_irqrestore(&gpio_lock, flags);
    2701                 :        414 :                 offset = gpio_chip_hwgpio(desc);
    2702         [ +  - ]:        414 :                 if (gpiochip_line_is_valid(chip, offset))
    2703                 :        414 :                         ret = chip->request(chip, offset);
    2704                 :            :                 else
    2705                 :            :                         ret = -EINVAL;
    2706                 :        414 :                 spin_lock_irqsave(&gpio_lock, flags);
    2707                 :            : 
    2708         [ -  + ]:        414 :                 if (ret < 0) {
    2709                 :            :                         desc_set_label(desc, NULL);
    2710                 :          0 :                         kfree_const(label);
    2711                 :          0 :                         clear_bit(FLAG_REQUESTED, &desc->flags);
    2712                 :          0 :                         goto done;
    2713                 :            :                 }
    2714                 :            :         }
    2715         [ +  - ]:        414 :         if (chip->get_direction) {
    2716                 :            :                 /* chip->get_direction may sleep */
    2717                 :            :                 spin_unlock_irqrestore(&gpio_lock, flags);
    2718                 :        414 :                 gpiod_get_direction(desc);
    2719                 :        414 :                 spin_lock_irqsave(&gpio_lock, flags);
    2720                 :            :         }
    2721                 :            : done:
    2722                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    2723                 :        414 :         return ret;
    2724                 :            : }
    2725                 :            : 
    2726                 :            : /*
    2727                 :            :  * This descriptor validation needs to be inserted verbatim into each
    2728                 :            :  * function taking a descriptor, so we need to use a preprocessor
    2729                 :            :  * macro to avoid endless duplication. If the desc is NULL it is an
    2730                 :            :  * optional GPIO and calls should just bail out.
    2731                 :            :  */
    2732                 :    2426899 : static int validate_desc(const struct gpio_desc *desc, const char *func)
    2733                 :            : {
    2734         [ +  - ]:    2426899 :         if (!desc)
    2735                 :            :                 return 0;
    2736         [ -  + ]:    2426899 :         if (IS_ERR(desc)) {
    2737                 :          0 :                 pr_warn("%s: invalid GPIO (errorpointer)\n", func);
    2738                 :          0 :                 return PTR_ERR(desc);
    2739                 :            :         }
    2740         [ -  + ]:    2426899 :         if (!desc->gdev) {
    2741                 :          0 :                 pr_warn("%s: invalid GPIO (no device)\n", func);
    2742                 :          0 :                 return -EINVAL;
    2743                 :            :         }
    2744         [ -  + ]:    2426899 :         if (!desc->gdev->chip) {
    2745                 :          0 :                 dev_warn(&desc->gdev->dev,
    2746                 :            :                          "%s: backing chip is gone\n", func);
    2747                 :          0 :                 return 0;
    2748                 :            :         }
    2749                 :            :         return 1;
    2750                 :            : }
    2751                 :            : 
    2752                 :            : #define VALIDATE_DESC(desc) do { \
    2753                 :            :         int __valid = validate_desc(desc, __func__); \
    2754                 :            :         if (__valid <= 0) \
    2755                 :            :                 return __valid; \
    2756                 :            :         } while (0)
    2757                 :            : 
    2758                 :            : #define VALIDATE_DESC_VOID(desc) do { \
    2759                 :            :         int __valid = validate_desc(desc, __func__); \
    2760                 :            :         if (__valid <= 0) \
    2761                 :            :                 return; \
    2762                 :            :         } while (0)
    2763                 :            : 
    2764                 :        414 : int gpiod_request(struct gpio_desc *desc, const char *label)
    2765                 :            : {
    2766                 :            :         int ret = -EPROBE_DEFER;
    2767                 :            :         struct gpio_device *gdev;
    2768                 :            : 
    2769         [ +  - ]:        414 :         VALIDATE_DESC(desc);
    2770                 :        414 :         gdev = desc->gdev;
    2771                 :            : 
    2772         [ +  - ]:        414 :         if (try_module_get(gdev->owner)) {
    2773                 :        414 :                 ret = gpiod_request_commit(desc, label);
    2774         [ -  + ]:        414 :                 if (ret < 0)
    2775                 :          0 :                         module_put(gdev->owner);
    2776                 :            :                 else
    2777                 :        414 :                         get_device(&gdev->dev);
    2778                 :            :         }
    2779                 :            : 
    2780                 :            :         if (ret)
    2781                 :            :                 gpiod_dbg(desc, "%s: status %d\n", __func__, ret);
    2782                 :            : 
    2783                 :        414 :         return ret;
    2784                 :            : }
    2785                 :            : 
    2786                 :          0 : static bool gpiod_free_commit(struct gpio_desc *desc)
    2787                 :            : {
    2788                 :            :         bool                    ret = false;
    2789                 :            :         unsigned long           flags;
    2790                 :            :         struct gpio_chip        *chip;
    2791                 :            : 
    2792                 :          0 :         might_sleep();
    2793                 :            : 
    2794                 :          0 :         gpiod_unexport(desc);
    2795                 :            : 
    2796                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
    2797                 :            : 
    2798                 :          0 :         chip = desc->gdev->chip;
    2799   [ #  #  #  # ]:          0 :         if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) {
    2800         [ #  # ]:          0 :                 if (chip->free) {
    2801                 :            :                         spin_unlock_irqrestore(&gpio_lock, flags);
    2802         [ #  # ]:          0 :                         might_sleep_if(chip->can_sleep);
    2803                 :          0 :                         chip->free(chip, gpio_chip_hwgpio(desc));
    2804                 :          0 :                         spin_lock_irqsave(&gpio_lock, flags);
    2805                 :            :                 }
    2806                 :          0 :                 kfree_const(desc->label);
    2807                 :            :                 desc_set_label(desc, NULL);
    2808                 :          0 :                 clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
    2809                 :          0 :                 clear_bit(FLAG_REQUESTED, &desc->flags);
    2810                 :          0 :                 clear_bit(FLAG_OPEN_DRAIN, &desc->flags);
    2811                 :          0 :                 clear_bit(FLAG_OPEN_SOURCE, &desc->flags);
    2812                 :          0 :                 clear_bit(FLAG_IS_HOGGED, &desc->flags);
    2813                 :            :                 ret = true;
    2814                 :            :         }
    2815                 :            : 
    2816                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    2817                 :          0 :         return ret;
    2818                 :            : }
    2819                 :            : 
    2820                 :          0 : void gpiod_free(struct gpio_desc *desc)
    2821                 :            : {
    2822   [ #  #  #  #  :          0 :         if (desc && desc->gdev && gpiod_free_commit(desc)) {
                   #  # ]
    2823                 :          0 :                 module_put(desc->gdev->owner);
    2824                 :          0 :                 put_device(&desc->gdev->dev);
    2825                 :            :         } else {
    2826                 :            :                 WARN_ON(extra_checks);
    2827                 :            :         }
    2828                 :          0 : }
    2829                 :            : 
    2830                 :            : /**
    2831                 :            :  * gpiochip_is_requested - return string iff signal was requested
    2832                 :            :  * @chip: controller managing the signal
    2833                 :            :  * @offset: of signal within controller's 0..(ngpio - 1) range
    2834                 :            :  *
    2835                 :            :  * Returns NULL if the GPIO is not currently requested, else a string.
    2836                 :            :  * The string returned is the label passed to gpio_request(); if none has been
    2837                 :            :  * passed it is a meaningless, non-NULL constant.
    2838                 :            :  *
    2839                 :            :  * This function is for use by GPIO controller drivers.  The label can
    2840                 :            :  * help with diagnostics, and knowing that the signal is used as a GPIO
    2841                 :            :  * can help avoid accidentally multiplexing it to another controller.
    2842                 :            :  */
    2843                 :          0 : const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset)
    2844                 :            : {
    2845                 :            :         struct gpio_desc *desc;
    2846                 :            : 
    2847         [ #  # ]:          0 :         if (offset >= chip->ngpio)
    2848                 :            :                 return NULL;
    2849                 :            : 
    2850                 :          0 :         desc = &chip->gpiodev->descs[offset];
    2851                 :            : 
    2852         [ #  # ]:          0 :         if (test_bit(FLAG_REQUESTED, &desc->flags) == 0)
    2853                 :            :                 return NULL;
    2854                 :          0 :         return desc->label;
    2855                 :            : }
    2856                 :            : EXPORT_SYMBOL_GPL(gpiochip_is_requested);
    2857                 :            : 
    2858                 :            : /**
    2859                 :            :  * gpiochip_request_own_desc - Allow GPIO chip to request its own descriptor
    2860                 :            :  * @chip: GPIO chip
    2861                 :            :  * @hwnum: hardware number of the GPIO for which to request the descriptor
    2862                 :            :  * @label: label for the GPIO
    2863                 :            :  * @lflags: lookup flags for this GPIO or 0 if default, this can be used to
    2864                 :            :  * specify things like line inversion semantics with the machine flags
    2865                 :            :  * such as GPIO_OUT_LOW
    2866                 :            :  * @dflags: descriptor request flags for this GPIO or 0 if default, this
    2867                 :            :  * can be used to specify consumer semantics such as open drain
    2868                 :            :  *
    2869                 :            :  * Function allows GPIO chip drivers to request and use their own GPIO
    2870                 :            :  * descriptors via gpiolib API. Difference to gpiod_request() is that this
    2871                 :            :  * function will not increase reference count of the GPIO chip module. This
    2872                 :            :  * allows the GPIO chip module to be unloaded as needed (we assume that the
    2873                 :            :  * GPIO chip driver handles freeing the GPIOs it has requested).
    2874                 :            :  *
    2875                 :            :  * Returns:
    2876                 :            :  * A pointer to the GPIO descriptor, or an ERR_PTR()-encoded negative error
    2877                 :            :  * code on failure.
    2878                 :            :  */
    2879                 :          0 : struct gpio_desc *gpiochip_request_own_desc(struct gpio_chip *chip, u16 hwnum,
    2880                 :            :                                             const char *label,
    2881                 :            :                                             enum gpio_lookup_flags lflags,
    2882                 :            :                                             enum gpiod_flags dflags)
    2883                 :            : {
    2884                 :            :         struct gpio_desc *desc = gpiochip_get_desc(chip, hwnum);
    2885                 :            :         int ret;
    2886                 :            : 
    2887         [ #  # ]:          0 :         if (IS_ERR(desc)) {
    2888                 :          0 :                 chip_err(chip, "failed to get GPIO descriptor\n");
    2889                 :          0 :                 return desc;
    2890                 :            :         }
    2891                 :            : 
    2892                 :          0 :         ret = gpiod_request_commit(desc, label);
    2893         [ #  # ]:          0 :         if (ret < 0)
    2894                 :          0 :                 return ERR_PTR(ret);
    2895                 :            : 
    2896                 :          0 :         ret = gpiod_configure_flags(desc, label, lflags, dflags);
    2897         [ #  # ]:          0 :         if (ret) {
    2898                 :          0 :                 chip_err(chip, "setup of own GPIO %s failed\n", label);
    2899                 :          0 :                 gpiod_free_commit(desc);
    2900                 :          0 :                 return ERR_PTR(ret);
    2901                 :            :         }
    2902                 :            : 
    2903                 :            :         return desc;
    2904                 :            : }
    2905                 :            : EXPORT_SYMBOL_GPL(gpiochip_request_own_desc);
    2906                 :            : 
    2907                 :            : /**
    2908                 :            :  * gpiochip_free_own_desc - Free GPIO requested by the chip driver
    2909                 :            :  * @desc: GPIO descriptor to free
    2910                 :            :  *
    2911                 :            :  * Function frees the given GPIO requested previously with
    2912                 :            :  * gpiochip_request_own_desc().
    2913                 :            :  */
    2914                 :          0 : void gpiochip_free_own_desc(struct gpio_desc *desc)
    2915                 :            : {
    2916   [ #  #  #  # ]:          0 :         if (desc)
    2917                 :          0 :                 gpiod_free_commit(desc);
    2918                 :          0 : }
    2919                 :            : EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
    2920                 :            : 
    2921                 :            : /*
    2922                 :            :  * Drivers MUST set GPIO direction before making get/set calls.  In
    2923                 :            :  * some cases this is done in early boot, before IRQs are enabled.
    2924                 :            :  *
    2925                 :            :  * As a rule these aren't called more than once (except for drivers
    2926                 :            :  * using the open-drain emulation idiom) so these are natural places
    2927                 :            :  * to accumulate extra debugging checks.  Note that we can't (yet)
    2928                 :            :  * rely on gpio_request() having been called beforehand.
    2929                 :            :  */
    2930                 :            : 
    2931                 :        414 : static int gpio_set_config(struct gpio_chip *gc, unsigned offset,
    2932                 :            :                            enum pin_config_param mode)
    2933                 :            : {
    2934                 :            :         unsigned long config;
    2935                 :            :         unsigned arg;
    2936                 :            : 
    2937         [ +  - ]:        414 :         switch (mode) {
    2938                 :            :         case PIN_CONFIG_BIAS_PULL_DOWN:
    2939                 :            :         case PIN_CONFIG_BIAS_PULL_UP:
    2940                 :            :                 arg = 1;
    2941                 :            :                 break;
    2942                 :            : 
    2943                 :            :         default:
    2944                 :            :                 arg = 0;
    2945                 :            :         }
    2946                 :            : 
    2947                 :        414 :         config = PIN_CONF_PACKED(mode, arg);
    2948         [ +  - ]:        414 :         return gc->set_config ? gc->set_config(gc, offset, config) : -ENOTSUPP;
    2949                 :            : }
    2950                 :            : 
    2951                 :            : /**
    2952                 :            :  * gpiod_direction_input - set the GPIO direction to input
    2953                 :            :  * @desc:       GPIO to set to input
    2954                 :            :  *
    2955                 :            :  * Set the direction of the passed GPIO to input, such as gpiod_get_value() can
    2956                 :            :  * be called safely on it.
    2957                 :            :  *
    2958                 :            :  * Return 0 in case of success, else an error code.
    2959                 :            :  */
    2960                 :        207 : int gpiod_direction_input(struct gpio_desc *desc)
    2961                 :            : {
    2962                 :            :         struct gpio_chip        *chip;
    2963                 :            :         int                     ret = 0;
    2964                 :            : 
    2965         [ +  - ]:        207 :         VALIDATE_DESC(desc);
    2966                 :        207 :         chip = desc->gdev->chip;
    2967                 :            : 
    2968                 :            :         /*
    2969                 :            :          * It is legal to have no .get() and .direction_input() specified if
    2970                 :            :          * the chip is output-only, but you can't specify .direction_input()
    2971                 :            :          * and not support the .get() operation, that doesn't make sense.
    2972                 :            :          */
    2973   [ -  +  #  # ]:        207 :         if (!chip->get && chip->direction_input) {
    2974         [ #  # ]:          0 :                 gpiod_warn(desc,
    2975                 :            :                            "%s: missing get() but have direction_input()\n",
    2976                 :            :                            __func__);
    2977                 :          0 :                 return -EIO;
    2978                 :            :         }
    2979                 :            : 
    2980                 :            :         /*
    2981                 :            :          * If we have a .direction_input() callback, things are simple,
    2982                 :            :          * just call it. Else we are some input-only chip so try to check the
    2983                 :            :          * direction (if .get_direction() is supported) else we silently
    2984                 :            :          * assume we are in input mode after this.
    2985                 :            :          */
    2986         [ +  - ]:        207 :         if (chip->direction_input) {
    2987                 :        207 :                 ret = chip->direction_input(chip, gpio_chip_hwgpio(desc));
    2988   [ #  #  #  # ]:          0 :         } else if (chip->get_direction &&
    2989                 :          0 :                   (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
    2990         [ #  # ]:          0 :                 gpiod_warn(desc,
    2991                 :            :                            "%s: missing direction_input() operation and line is output\n",
    2992                 :            :                            __func__);
    2993                 :          0 :                 return -EIO;
    2994                 :            :         }
    2995         [ +  - ]:        207 :         if (ret == 0)
    2996                 :        207 :                 clear_bit(FLAG_IS_OUT, &desc->flags);
    2997                 :            : 
    2998         [ -  + ]:        207 :         if (test_bit(FLAG_PULL_UP, &desc->flags))
    2999                 :          0 :                 gpio_set_config(chip, gpio_chip_hwgpio(desc),
    3000                 :            :                                 PIN_CONFIG_BIAS_PULL_UP);
    3001         [ -  + ]:        207 :         else if (test_bit(FLAG_PULL_DOWN, &desc->flags))
    3002                 :          0 :                 gpio_set_config(chip, gpio_chip_hwgpio(desc),
    3003                 :            :                                 PIN_CONFIG_BIAS_PULL_DOWN);
    3004                 :            : 
    3005                 :        207 :         trace_gpio_direction(desc_to_gpio(desc), 1, ret);
    3006                 :            : 
    3007                 :        207 :         return ret;
    3008                 :            : }
    3009                 :            : EXPORT_SYMBOL_GPL(gpiod_direction_input);
    3010                 :            : 
    3011                 :        414 : static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
    3012                 :            : {
    3013                 :        414 :         struct gpio_chip *gc = desc->gdev->chip;
    3014                 :        414 :         int val = !!value;
    3015                 :            :         int ret = 0;
    3016                 :            : 
    3017                 :            :         /*
    3018                 :            :          * It's OK not to specify .direction_output() if the gpiochip is
    3019                 :            :          * output-only, but if there is then not even a .set() operation it
    3020                 :            :          * is pretty tricky to drive the output line.
    3021                 :            :          */
    3022   [ -  +  #  # ]:        414 :         if (!gc->set && !gc->direction_output) {
    3023         [ #  # ]:          0 :                 gpiod_warn(desc,
    3024                 :            :                            "%s: missing set() and direction_output() operations\n",
    3025                 :            :                            __func__);
    3026                 :          0 :                 return -EIO;
    3027                 :            :         }
    3028                 :            : 
    3029         [ +  - ]:        414 :         if (gc->direction_output) {
    3030                 :        414 :                 ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
    3031                 :            :         } else {
    3032                 :            :                 /* Check that we are in output mode if we can */
    3033   [ #  #  #  # ]:          0 :                 if (gc->get_direction &&
    3034                 :          0 :                     gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
    3035         [ #  # ]:          0 :                         gpiod_warn(desc,
    3036                 :            :                                 "%s: missing direction_output() operation\n",
    3037                 :            :                                 __func__);
    3038                 :          0 :                         return -EIO;
    3039                 :            :                 }
    3040                 :            :                 /*
    3041                 :            :                  * If we can't actively set the direction, we are some
    3042                 :            :                  * output-only chip, so just drive the output as desired.
    3043                 :            :                  */
    3044                 :          0 :                 gc->set(gc, gpio_chip_hwgpio(desc), val);
    3045                 :            :         }
    3046                 :            : 
    3047         [ +  - ]:        414 :         if (!ret)
    3048                 :        414 :                 set_bit(FLAG_IS_OUT, &desc->flags);
    3049                 :        414 :         trace_gpio_value(desc_to_gpio(desc), 0, val);
    3050                 :        414 :         trace_gpio_direction(desc_to_gpio(desc), 0, ret);
    3051                 :        414 :         return ret;
    3052                 :            : }
    3053                 :            : 
    3054                 :            : /**
    3055                 :            :  * gpiod_direction_output_raw - set the GPIO direction to output
    3056                 :            :  * @desc:       GPIO to set to output
    3057                 :            :  * @value:      initial output value of the GPIO
    3058                 :            :  *
    3059                 :            :  * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
    3060                 :            :  * be called safely on it. The initial value of the output must be specified
    3061                 :            :  * as raw value on the physical line without regard for the ACTIVE_LOW status.
    3062                 :            :  *
    3063                 :            :  * Return 0 in case of success, else an error code.
    3064                 :            :  */
    3065                 :          0 : int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
    3066                 :            : {
    3067         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    3068                 :          0 :         return gpiod_direction_output_raw_commit(desc, value);
    3069                 :            : }
    3070                 :            : EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
    3071                 :            : 
    3072                 :            : /**
    3073                 :            :  * gpiod_direction_output - set the GPIO direction to output
    3074                 :            :  * @desc:       GPIO to set to output
    3075                 :            :  * @value:      initial output value of the GPIO
    3076                 :            :  *
    3077                 :            :  * Set the direction of the passed GPIO to output, such as gpiod_set_value() can
    3078                 :            :  * be called safely on it. The initial value of the output must be specified
    3079                 :            :  * as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
    3080                 :            :  * account.
    3081                 :            :  *
    3082                 :            :  * Return 0 in case of success, else an error code.
    3083                 :            :  */
    3084                 :        414 : int gpiod_direction_output(struct gpio_desc *desc, int value)
    3085                 :            : {
    3086                 :            :         struct gpio_chip *gc;
    3087                 :            :         int ret;
    3088                 :            : 
    3089         [ +  - ]:        414 :         VALIDATE_DESC(desc);
    3090         [ -  + ]:        414 :         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    3091                 :          0 :                 value = !value;
    3092                 :            :         else
    3093                 :        414 :                 value = !!value;
    3094                 :            : 
    3095                 :            :         /* GPIOs used for enabled IRQs shall not be set as output */
    3096                 :            :         if (dont_test_bit(FLAG_USED_AS_IRQ, &desc->flags) &&
    3097                 :            :             dont_test_bit(FLAG_IRQ_IS_ENABLED, &desc->flags)) {
    3098                 :            :                 gpiod_err(desc,
    3099                 :            :                           "%s: tried to set a GPIO tied to an IRQ as output\n",
    3100                 :            :                           __func__);
    3101                 :            :                 return -EIO;
    3102                 :            :         }
    3103                 :            : 
    3104                 :        414 :         gc = desc->gdev->chip;
    3105         [ -  + ]:        414 :         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
    3106                 :            :                 /* First see if we can enable open drain in hardware */
    3107                 :          0 :                 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
    3108                 :            :                                       PIN_CONFIG_DRIVE_OPEN_DRAIN);
    3109         [ #  # ]:          0 :                 if (!ret)
    3110                 :            :                         goto set_output_value;
    3111                 :            :                 /* Emulate open drain by not actively driving the line high */
    3112         [ #  # ]:          0 :                 if (value) {
    3113                 :          0 :                         ret = gpiod_direction_input(desc);
    3114                 :          0 :                         goto set_output_flag;
    3115                 :            :                 }
    3116                 :            :         }
    3117         [ -  + ]:        414 :         else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
    3118                 :          0 :                 ret = gpio_set_config(gc, gpio_chip_hwgpio(desc),
    3119                 :            :                                       PIN_CONFIG_DRIVE_OPEN_SOURCE);
    3120         [ #  # ]:          0 :                 if (!ret)
    3121                 :            :                         goto set_output_value;
    3122                 :            :                 /* Emulate open source by not actively driving the line low */
    3123         [ #  # ]:          0 :                 if (!value) {
    3124                 :          0 :                         ret = gpiod_direction_input(desc);
    3125                 :          0 :                         goto set_output_flag;
    3126                 :            :                 }
    3127                 :            :         } else {
    3128                 :        414 :                 gpio_set_config(gc, gpio_chip_hwgpio(desc),
    3129                 :            :                                 PIN_CONFIG_DRIVE_PUSH_PULL);
    3130                 :            :         }
    3131                 :            : 
    3132                 :            : set_output_value:
    3133                 :        414 :         return gpiod_direction_output_raw_commit(desc, value);
    3134                 :            : 
    3135                 :            : set_output_flag:
    3136                 :            :         /*
    3137                 :            :          * When emulating open-source or open-drain functionalities by not
    3138                 :            :          * actively driving the line (setting mode to input) we still need to
    3139                 :            :          * set the IS_OUT flag or otherwise we won't be able to set the line
    3140                 :            :          * value anymore.
    3141                 :            :          */
    3142         [ #  # ]:          0 :         if (ret == 0)
    3143                 :          0 :                 set_bit(FLAG_IS_OUT, &desc->flags);
    3144                 :          0 :         return ret;
    3145                 :            : }
    3146                 :            : EXPORT_SYMBOL_GPL(gpiod_direction_output);
    3147                 :            : 
    3148                 :            : /**
    3149                 :            :  * gpiod_set_debounce - sets @debounce time for a GPIO
    3150                 :            :  * @desc: descriptor of the GPIO for which to set debounce time
    3151                 :            :  * @debounce: debounce time in microseconds
    3152                 :            :  *
    3153                 :            :  * Returns:
    3154                 :            :  * 0 on success, %-ENOTSUPP if the controller doesn't support setting the
    3155                 :            :  * debounce time.
    3156                 :            :  */
    3157                 :          0 : int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce)
    3158                 :            : {
    3159                 :            :         struct gpio_chip        *chip;
    3160                 :            :         unsigned long           config;
    3161                 :            : 
    3162         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    3163                 :          0 :         chip = desc->gdev->chip;
    3164   [ #  #  #  # ]:          0 :         if (!chip->set || !chip->set_config) {
    3165                 :            :                 gpiod_dbg(desc,
    3166                 :            :                           "%s: missing set() or set_config() operations\n",
    3167                 :            :                           __func__);
    3168                 :            :                 return -ENOTSUPP;
    3169                 :            :         }
    3170                 :            : 
    3171                 :            :         config = pinconf_to_config_packed(PIN_CONFIG_INPUT_DEBOUNCE, debounce);
    3172                 :          0 :         return chip->set_config(chip, gpio_chip_hwgpio(desc), config);
    3173                 :            : }
    3174                 :            : EXPORT_SYMBOL_GPL(gpiod_set_debounce);
    3175                 :            : 
    3176                 :            : /**
    3177                 :            :  * gpiod_set_transitory - Lose or retain GPIO state on suspend or reset
    3178                 :            :  * @desc: descriptor of the GPIO for which to configure persistence
    3179                 :            :  * @transitory: True to lose state on suspend or reset, false for persistence
    3180                 :            :  *
    3181                 :            :  * Returns:
    3182                 :            :  * 0 on success, otherwise a negative error code.
    3183                 :            :  */
    3184                 :        414 : int gpiod_set_transitory(struct gpio_desc *desc, bool transitory)
    3185                 :            : {
    3186                 :            :         struct gpio_chip *chip;
    3187                 :            :         unsigned long packed;
    3188                 :            :         int gpio;
    3189                 :            :         int rc;
    3190                 :            : 
    3191         [ +  - ]:        414 :         VALIDATE_DESC(desc);
    3192                 :            :         /*
    3193                 :            :          * Handle FLAG_TRANSITORY first, enabling queries to gpiolib for
    3194                 :            :          * persistence state.
    3195                 :            :          */
    3196         [ -  + ]:        414 :         if (transitory)
    3197                 :          0 :                 set_bit(FLAG_TRANSITORY, &desc->flags);
    3198                 :            :         else
    3199                 :        414 :                 clear_bit(FLAG_TRANSITORY, &desc->flags);
    3200                 :            : 
    3201                 :            :         /* If the driver supports it, set the persistence state now */
    3202                 :        414 :         chip = desc->gdev->chip;
    3203         [ +  - ]:        414 :         if (!chip->set_config)
    3204                 :            :                 return 0;
    3205                 :            : 
    3206                 :        414 :         packed = pinconf_to_config_packed(PIN_CONFIG_PERSIST_STATE,
    3207                 :        414 :                                           !transitory);
    3208                 :            :         gpio = gpio_chip_hwgpio(desc);
    3209                 :        414 :         rc = chip->set_config(chip, gpio, packed);
    3210         [ -  + ]:        414 :         if (rc == -ENOTSUPP) {
    3211                 :            :                 dev_dbg(&desc->gdev->dev, "Persistence not supported for GPIO %d\n",
    3212                 :            :                                 gpio);
    3213                 :            :                 return 0;
    3214                 :            :         }
    3215                 :            : 
    3216                 :          0 :         return rc;
    3217                 :            : }
    3218                 :            : EXPORT_SYMBOL_GPL(gpiod_set_transitory);
    3219                 :            : 
    3220                 :            : /**
    3221                 :            :  * gpiod_is_active_low - test whether a GPIO is active-low or not
    3222                 :            :  * @desc: the gpio descriptor to test
    3223                 :            :  *
    3224                 :            :  * Returns 1 if the GPIO is active-low, 0 otherwise.
    3225                 :            :  */
    3226                 :          0 : int gpiod_is_active_low(const struct gpio_desc *desc)
    3227                 :            : {
    3228         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    3229                 :          0 :         return test_bit(FLAG_ACTIVE_LOW, &desc->flags);
    3230                 :            : }
    3231                 :            : EXPORT_SYMBOL_GPL(gpiod_is_active_low);
    3232                 :            : 
    3233                 :            : /**
    3234                 :            :  * gpiod_toggle_active_low - toggle whether a GPIO is active-low or not
    3235                 :            :  * @desc: the gpio descriptor to change
    3236                 :            :  */
    3237                 :          0 : void gpiod_toggle_active_low(struct gpio_desc *desc)
    3238                 :            : {
    3239         [ #  # ]:          0 :         VALIDATE_DESC_VOID(desc);
    3240                 :          0 :         change_bit(FLAG_ACTIVE_LOW, &desc->flags);
    3241                 :            : }
    3242                 :            : EXPORT_SYMBOL_GPL(gpiod_toggle_active_low);
    3243                 :            : 
    3244                 :            : /* I/O calls are only valid after configuration completed; the relevant
    3245                 :            :  * "is this a valid GPIO" error checks should already have been done.
    3246                 :            :  *
    3247                 :            :  * "Get" operations are often inlinable as reading a pin value register,
    3248                 :            :  * and masking the relevant bit in that register.
    3249                 :            :  *
    3250                 :            :  * When "set" operations are inlinable, they involve writing that mask to
    3251                 :            :  * one register to set a low value, or a different register to set it high.
    3252                 :            :  * Otherwise locking is needed, so there may be little value to inlining.
    3253                 :            :  *
    3254                 :            :  *------------------------------------------------------------------------
    3255                 :            :  *
    3256                 :            :  * IMPORTANT!!!  The hot paths -- get/set value -- assume that callers
    3257                 :            :  * have requested the GPIO.  That can include implicit requesting by
    3258                 :            :  * a direction setting call.  Marking a gpio as requested locks its chip
    3259                 :            :  * in memory, guaranteeing that these table lookups need no more locking
    3260                 :            :  * and that gpiochip_remove() will fail.
    3261                 :            :  *
    3262                 :            :  * REVISIT when debugging, consider adding some instrumentation to ensure
    3263                 :            :  * that the GPIO was actually requested.
    3264                 :            :  */
    3265                 :            : 
    3266                 :        621 : static int gpiod_get_raw_value_commit(const struct gpio_desc *desc)
    3267                 :            : {
    3268                 :            :         struct gpio_chip        *chip;
    3269                 :            :         int offset;
    3270                 :            :         int value;
    3271                 :            : 
    3272                 :        621 :         chip = desc->gdev->chip;
    3273                 :            :         offset = gpio_chip_hwgpio(desc);
    3274         [ +  - ]:        621 :         value = chip->get ? chip->get(chip, offset) : -EIO;
    3275         [ +  - ]:        621 :         value = value < 0 ? value : !!value;
    3276                 :        621 :         trace_gpio_value(desc_to_gpio(desc), 1, value);
    3277                 :        621 :         return value;
    3278                 :            : }
    3279                 :            : 
    3280                 :          0 : static int gpio_chip_get_multiple(struct gpio_chip *chip,
    3281                 :            :                                   unsigned long *mask, unsigned long *bits)
    3282                 :            : {
    3283         [ #  # ]:          0 :         if (chip->get_multiple) {
    3284                 :          0 :                 return chip->get_multiple(chip, mask, bits);
    3285         [ #  # ]:          0 :         } else if (chip->get) {
    3286                 :            :                 int i, value;
    3287                 :            : 
    3288         [ #  # ]:          0 :                 for_each_set_bit(i, mask, chip->ngpio) {
    3289                 :          0 :                         value = chip->get(chip, i);
    3290         [ #  # ]:          0 :                         if (value < 0)
    3291                 :          0 :                                 return value;
    3292                 :            :                         __assign_bit(i, bits, value);
    3293                 :            :                 }
    3294                 :            :                 return 0;
    3295                 :            :         }
    3296                 :            :         return -EIO;
    3297                 :            : }
    3298                 :            : 
    3299                 :          0 : int gpiod_get_array_value_complex(bool raw, bool can_sleep,
    3300                 :            :                                   unsigned int array_size,
    3301                 :            :                                   struct gpio_desc **desc_array,
    3302                 :            :                                   struct gpio_array *array_info,
    3303                 :            :                                   unsigned long *value_bitmap)
    3304                 :            : {
    3305                 :            :         int ret, i = 0;
    3306                 :            : 
    3307                 :            :         /*
    3308                 :            :          * Validate array_info against desc_array and its size.
    3309                 :            :          * It should immediately follow desc_array if both
    3310                 :            :          * have been obtained from the same gpiod_get_array() call.
    3311                 :            :          */
    3312   [ #  #  #  #  :          0 :         if (array_info && array_info->desc == desc_array &&
                   #  # ]
    3313         [ #  # ]:          0 :             array_size <= array_info->size &&
    3314                 :          0 :             (void *)array_info == desc_array + array_info->size) {
    3315         [ #  # ]:          0 :                 if (!can_sleep)
    3316         [ #  # ]:          0 :                         WARN_ON(array_info->chip->can_sleep);
    3317                 :            : 
    3318                 :          0 :                 ret = gpio_chip_get_multiple(array_info->chip,
    3319                 :            :                                              array_info->get_mask,
    3320                 :            :                                              value_bitmap);
    3321         [ #  # ]:          0 :                 if (ret)
    3322                 :            :                         return ret;
    3323                 :            : 
    3324   [ #  #  #  # ]:          0 :                 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
    3325                 :          0 :                         bitmap_xor(value_bitmap, value_bitmap,
    3326                 :            :                                    array_info->invert_mask, array_size);
    3327                 :            : 
    3328         [ #  # ]:          0 :                 if (bitmap_full(array_info->get_mask, array_size))
    3329                 :            :                         return 0;
    3330                 :            : 
    3331                 :          0 :                 i = find_first_zero_bit(array_info->get_mask, array_size);
    3332                 :            :         } else {
    3333                 :            :                 array_info = NULL;
    3334                 :            :         }
    3335                 :            : 
    3336         [ #  # ]:          0 :         while (i < array_size) {
    3337                 :          0 :                 struct gpio_chip *chip = desc_array[i]->gdev->chip;
    3338                 :            :                 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
    3339                 :            :                 unsigned long *mask, *bits;
    3340                 :            :                 int first, j, ret;
    3341                 :            : 
    3342         [ #  # ]:          0 :                 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
    3343                 :            :                         mask = fastpath;
    3344                 :            :                 } else {
    3345         [ #  # ]:          0 :                         mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
    3346                 :            :                                            sizeof(*mask),
    3347                 :            :                                            can_sleep ? GFP_KERNEL : GFP_ATOMIC);
    3348         [ #  # ]:          0 :                         if (!mask)
    3349                 :          0 :                                 return -ENOMEM;
    3350                 :            :                 }
    3351                 :            : 
    3352                 :          0 :                 bits = mask + BITS_TO_LONGS(chip->ngpio);
    3353                 :            :                 bitmap_zero(mask, chip->ngpio);
    3354                 :            : 
    3355         [ #  # ]:          0 :                 if (!can_sleep)
    3356         [ #  # ]:          0 :                         WARN_ON(chip->can_sleep);
    3357                 :            : 
    3358                 :            :                 /* collect all inputs belonging to the same chip */
    3359                 :            :                 first = i;
    3360                 :            :                 do {
    3361                 :          0 :                         const struct gpio_desc *desc = desc_array[i];
    3362                 :            :                         int hwgpio = gpio_chip_hwgpio(desc);
    3363                 :            : 
    3364                 :            :                         __set_bit(hwgpio, mask);
    3365                 :          0 :                         i++;
    3366                 :            : 
    3367         [ #  # ]:          0 :                         if (array_info)
    3368                 :          0 :                                 i = find_next_zero_bit(array_info->get_mask,
    3369                 :            :                                                        array_size, i);
    3370         [ #  # ]:          0 :                 } while ((i < array_size) &&
    3371         [ #  # ]:          0 :                          (desc_array[i]->gdev->chip == chip));
    3372                 :            : 
    3373                 :          0 :                 ret = gpio_chip_get_multiple(chip, mask, bits);
    3374         [ #  # ]:          0 :                 if (ret) {
    3375         [ #  # ]:          0 :                         if (mask != fastpath)
    3376                 :          0 :                                 kfree(mask);
    3377                 :          0 :                         return ret;
    3378                 :            :                 }
    3379                 :            : 
    3380         [ #  # ]:          0 :                 for (j = first; j < i; ) {
    3381                 :          0 :                         const struct gpio_desc *desc = desc_array[j];
    3382                 :            :                         int hwgpio = gpio_chip_hwgpio(desc);
    3383                 :            :                         int value = test_bit(hwgpio, bits);
    3384                 :            : 
    3385   [ #  #  #  # ]:          0 :                         if (!raw && test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    3386                 :          0 :                                 value = !value;
    3387                 :            :                         __assign_bit(j, value_bitmap, value);
    3388                 :          0 :                         trace_gpio_value(desc_to_gpio(desc), 1, value);
    3389                 :          0 :                         j++;
    3390                 :            : 
    3391         [ #  # ]:          0 :                         if (array_info)
    3392                 :          0 :                                 j = find_next_zero_bit(array_info->get_mask, i,
    3393                 :            :                                                        j);
    3394                 :            :                 }
    3395                 :            : 
    3396         [ #  # ]:          0 :                 if (mask != fastpath)
    3397                 :          0 :                         kfree(mask);
    3398                 :            :         }
    3399                 :            :         return 0;
    3400                 :            : }
    3401                 :            : 
    3402                 :            : /**
    3403                 :            :  * gpiod_get_raw_value() - return a gpio's raw value
    3404                 :            :  * @desc: gpio whose value will be returned
    3405                 :            :  *
    3406                 :            :  * Return the GPIO's raw value, i.e. the value of the physical line disregarding
    3407                 :            :  * its ACTIVE_LOW status, or negative errno on failure.
    3408                 :            :  *
    3409                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3410                 :            :  * complain if the GPIO chip functions potentially sleep.
    3411                 :            :  */
    3412                 :          0 : int gpiod_get_raw_value(const struct gpio_desc *desc)
    3413                 :            : {
    3414         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    3415                 :            :         /* Should be using gpiod_get_raw_value_cansleep() */
    3416         [ #  # ]:          0 :         WARN_ON(desc->gdev->chip->can_sleep);
    3417                 :          0 :         return gpiod_get_raw_value_commit(desc);
    3418                 :            : }
    3419                 :            : EXPORT_SYMBOL_GPL(gpiod_get_raw_value);
    3420                 :            : 
    3421                 :            : /**
    3422                 :            :  * gpiod_get_value() - return a gpio's value
    3423                 :            :  * @desc: gpio whose value will be returned
    3424                 :            :  *
    3425                 :            :  * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
    3426                 :            :  * account, or negative errno on failure.
    3427                 :            :  *
    3428                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3429                 :            :  * complain if the GPIO chip functions potentially sleep.
    3430                 :            :  */
    3431                 :          0 : int gpiod_get_value(const struct gpio_desc *desc)
    3432                 :            : {
    3433                 :            :         int value;
    3434                 :            : 
    3435         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    3436                 :            :         /* Should be using gpiod_get_value_cansleep() */
    3437         [ #  # ]:          0 :         WARN_ON(desc->gdev->chip->can_sleep);
    3438                 :            : 
    3439                 :          0 :         value = gpiod_get_raw_value_commit(desc);
    3440         [ #  # ]:          0 :         if (value < 0)
    3441                 :            :                 return value;
    3442                 :            : 
    3443         [ #  # ]:          0 :         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    3444                 :          0 :                 value = !value;
    3445                 :            : 
    3446                 :          0 :         return value;
    3447                 :            : }
    3448                 :            : EXPORT_SYMBOL_GPL(gpiod_get_value);
    3449                 :            : 
    3450                 :            : /**
    3451                 :            :  * gpiod_get_raw_array_value() - read raw values from an array of GPIOs
    3452                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    3453                 :            :  * @desc_array: array of GPIO descriptors whose values will be read
    3454                 :            :  * @array_info: information on applicability of fast bitmap processing path
    3455                 :            :  * @value_bitmap: bitmap to store the read values
    3456                 :            :  *
    3457                 :            :  * Read the raw values of the GPIOs, i.e. the values of the physical lines
    3458                 :            :  * without regard for their ACTIVE_LOW status.  Return 0 in case of success,
    3459                 :            :  * else an error code.
    3460                 :            :  *
    3461                 :            :  * This function can be called from contexts where we cannot sleep,
    3462                 :            :  * and it will complain if the GPIO chip functions potentially sleep.
    3463                 :            :  */
    3464                 :          0 : int gpiod_get_raw_array_value(unsigned int array_size,
    3465                 :            :                               struct gpio_desc **desc_array,
    3466                 :            :                               struct gpio_array *array_info,
    3467                 :            :                               unsigned long *value_bitmap)
    3468                 :            : {
    3469         [ #  # ]:          0 :         if (!desc_array)
    3470                 :            :                 return -EINVAL;
    3471                 :          0 :         return gpiod_get_array_value_complex(true, false, array_size,
    3472                 :            :                                              desc_array, array_info,
    3473                 :            :                                              value_bitmap);
    3474                 :            : }
    3475                 :            : EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value);
    3476                 :            : 
    3477                 :            : /**
    3478                 :            :  * gpiod_get_array_value() - read values from an array of GPIOs
    3479                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    3480                 :            :  * @desc_array: array of GPIO descriptors whose values will be read
    3481                 :            :  * @array_info: information on applicability of fast bitmap processing path
    3482                 :            :  * @value_bitmap: bitmap to store the read values
    3483                 :            :  *
    3484                 :            :  * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
    3485                 :            :  * into account.  Return 0 in case of success, else an error code.
    3486                 :            :  *
    3487                 :            :  * This function can be called from contexts where we cannot sleep,
    3488                 :            :  * and it will complain if the GPIO chip functions potentially sleep.
    3489                 :            :  */
    3490                 :          0 : int gpiod_get_array_value(unsigned int array_size,
    3491                 :            :                           struct gpio_desc **desc_array,
    3492                 :            :                           struct gpio_array *array_info,
    3493                 :            :                           unsigned long *value_bitmap)
    3494                 :            : {
    3495         [ #  # ]:          0 :         if (!desc_array)
    3496                 :            :                 return -EINVAL;
    3497                 :          0 :         return gpiod_get_array_value_complex(false, false, array_size,
    3498                 :            :                                              desc_array, array_info,
    3499                 :            :                                              value_bitmap);
    3500                 :            : }
    3501                 :            : EXPORT_SYMBOL_GPL(gpiod_get_array_value);
    3502                 :            : 
    3503                 :            : /*
    3504                 :            :  *  gpio_set_open_drain_value_commit() - Set the open drain gpio's value.
    3505                 :            :  * @desc: gpio descriptor whose state need to be set.
    3506                 :            :  * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
    3507                 :            :  */
    3508                 :          0 : static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
    3509                 :            : {
    3510                 :            :         int ret = 0;
    3511                 :          0 :         struct gpio_chip *chip = desc->gdev->chip;
    3512                 :            :         int offset = gpio_chip_hwgpio(desc);
    3513                 :            : 
    3514         [ #  # ]:          0 :         if (value) {
    3515                 :          0 :                 ret = chip->direction_input(chip, offset);
    3516                 :            :         } else {
    3517                 :          0 :                 ret = chip->direction_output(chip, offset, 0);
    3518         [ #  # ]:          0 :                 if (!ret)
    3519                 :          0 :                         set_bit(FLAG_IS_OUT, &desc->flags);
    3520                 :            :         }
    3521                 :          0 :         trace_gpio_direction(desc_to_gpio(desc), value, ret);
    3522         [ #  # ]:          0 :         if (ret < 0)
    3523         [ #  # ]:          0 :                 gpiod_err(desc,
    3524                 :            :                           "%s: Error in set_value for open drain err %d\n",
    3525                 :            :                           __func__, ret);
    3526                 :          0 : }
    3527                 :            : 
    3528                 :            : /*
    3529                 :            :  *  _gpio_set_open_source_value() - Set the open source gpio's value.
    3530                 :            :  * @desc: gpio descriptor whose state need to be set.
    3531                 :            :  * @value: Non-zero for setting it HIGH otherwise it will set to LOW.
    3532                 :            :  */
    3533                 :          0 : static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value)
    3534                 :            : {
    3535                 :            :         int ret = 0;
    3536                 :          0 :         struct gpio_chip *chip = desc->gdev->chip;
    3537                 :            :         int offset = gpio_chip_hwgpio(desc);
    3538                 :            : 
    3539         [ #  # ]:          0 :         if (value) {
    3540                 :          0 :                 ret = chip->direction_output(chip, offset, 1);
    3541         [ #  # ]:          0 :                 if (!ret)
    3542                 :          0 :                         set_bit(FLAG_IS_OUT, &desc->flags);
    3543                 :            :         } else {
    3544                 :          0 :                 ret = chip->direction_input(chip, offset);
    3545                 :            :         }
    3546                 :          0 :         trace_gpio_direction(desc_to_gpio(desc), !value, ret);
    3547         [ #  # ]:          0 :         if (ret < 0)
    3548         [ #  # ]:          0 :                 gpiod_err(desc,
    3549                 :            :                           "%s: Error in set_value for open source err %d\n",
    3550                 :            :                           __func__, ret);
    3551                 :          0 : }
    3552                 :            : 
    3553                 :    2424001 : static void gpiod_set_raw_value_commit(struct gpio_desc *desc, bool value)
    3554                 :            : {
    3555                 :            :         struct gpio_chip        *chip;
    3556                 :            : 
    3557                 :    2424001 :         chip = desc->gdev->chip;
    3558                 :    2424001 :         trace_gpio_value(desc_to_gpio(desc), 0, value);
    3559                 :    4848002 :         chip->set(chip, gpio_chip_hwgpio(desc), value);
    3560                 :    2424001 : }
    3561                 :            : 
    3562                 :            : /*
    3563                 :            :  * set multiple outputs on the same chip;
    3564                 :            :  * use the chip's set_multiple function if available;
    3565                 :            :  * otherwise set the outputs sequentially;
    3566                 :            :  * @mask: bit mask array; one bit per output; BITS_PER_LONG bits per word
    3567                 :            :  *        defines which outputs are to be changed
    3568                 :            :  * @bits: bit value array; one bit per output; BITS_PER_LONG bits per word
    3569                 :            :  *        defines the values the outputs specified by mask are to be set to
    3570                 :            :  */
    3571                 :          0 : static void gpio_chip_set_multiple(struct gpio_chip *chip,
    3572                 :            :                                    unsigned long *mask, unsigned long *bits)
    3573                 :            : {
    3574         [ #  # ]:          0 :         if (chip->set_multiple) {
    3575                 :          0 :                 chip->set_multiple(chip, mask, bits);
    3576                 :            :         } else {
    3577                 :            :                 unsigned int i;
    3578                 :            : 
    3579                 :            :                 /* set outputs if the corresponding mask bit is set */
    3580         [ #  # ]:          0 :                 for_each_set_bit(i, mask, chip->ngpio)
    3581                 :          0 :                         chip->set(chip, i, test_bit(i, bits));
    3582                 :            :         }
    3583                 :          0 : }
    3584                 :            : 
    3585                 :          0 : int gpiod_set_array_value_complex(bool raw, bool can_sleep,
    3586                 :            :                                   unsigned int array_size,
    3587                 :            :                                   struct gpio_desc **desc_array,
    3588                 :            :                                   struct gpio_array *array_info,
    3589                 :            :                                   unsigned long *value_bitmap)
    3590                 :            : {
    3591                 :            :         int i = 0;
    3592                 :            : 
    3593                 :            :         /*
    3594                 :            :          * Validate array_info against desc_array and its size.
    3595                 :            :          * It should immediately follow desc_array if both
    3596                 :            :          * have been obtained from the same gpiod_get_array() call.
    3597                 :            :          */
    3598   [ #  #  #  #  :          0 :         if (array_info && array_info->desc == desc_array &&
                   #  # ]
    3599         [ #  # ]:          0 :             array_size <= array_info->size &&
    3600                 :          0 :             (void *)array_info == desc_array + array_info->size) {
    3601         [ #  # ]:          0 :                 if (!can_sleep)
    3602         [ #  # ]:          0 :                         WARN_ON(array_info->chip->can_sleep);
    3603                 :            : 
    3604   [ #  #  #  # ]:          0 :                 if (!raw && !bitmap_empty(array_info->invert_mask, array_size))
    3605                 :          0 :                         bitmap_xor(value_bitmap, value_bitmap,
    3606                 :            :                                    array_info->invert_mask, array_size);
    3607                 :            : 
    3608                 :          0 :                 gpio_chip_set_multiple(array_info->chip, array_info->set_mask,
    3609                 :            :                                        value_bitmap);
    3610                 :            : 
    3611         [ #  # ]:          0 :                 if (bitmap_full(array_info->set_mask, array_size))
    3612                 :            :                         return 0;
    3613                 :            : 
    3614                 :          0 :                 i = find_first_zero_bit(array_info->set_mask, array_size);
    3615                 :            :         } else {
    3616                 :            :                 array_info = NULL;
    3617                 :            :         }
    3618                 :            : 
    3619         [ #  # ]:          0 :         while (i < array_size) {
    3620                 :          0 :                 struct gpio_chip *chip = desc_array[i]->gdev->chip;
    3621                 :            :                 unsigned long fastpath[2 * BITS_TO_LONGS(FASTPATH_NGPIO)];
    3622                 :            :                 unsigned long *mask, *bits;
    3623                 :            :                 int count = 0;
    3624                 :            : 
    3625         [ #  # ]:          0 :                 if (likely(chip->ngpio <= FASTPATH_NGPIO)) {
    3626                 :            :                         mask = fastpath;
    3627                 :            :                 } else {
    3628         [ #  # ]:          0 :                         mask = kmalloc_array(2 * BITS_TO_LONGS(chip->ngpio),
    3629                 :            :                                            sizeof(*mask),
    3630                 :            :                                            can_sleep ? GFP_KERNEL : GFP_ATOMIC);
    3631         [ #  # ]:          0 :                         if (!mask)
    3632                 :          0 :                                 return -ENOMEM;
    3633                 :            :                 }
    3634                 :            : 
    3635                 :          0 :                 bits = mask + BITS_TO_LONGS(chip->ngpio);
    3636                 :            :                 bitmap_zero(mask, chip->ngpio);
    3637                 :            : 
    3638         [ #  # ]:          0 :                 if (!can_sleep)
    3639         [ #  # ]:          0 :                         WARN_ON(chip->can_sleep);
    3640                 :            : 
    3641                 :            :                 do {
    3642                 :          0 :                         struct gpio_desc *desc = desc_array[i];
    3643                 :            :                         int hwgpio = gpio_chip_hwgpio(desc);
    3644                 :            :                         int value = test_bit(i, value_bitmap);
    3645                 :            : 
    3646                 :            :                         /*
    3647                 :            :                          * Pins applicable for fast input but not for
    3648                 :            :                          * fast output processing may have been already
    3649                 :            :                          * inverted inside the fast path, skip them.
    3650                 :            :                          */
    3651   [ #  #  #  #  :          0 :                         if (!raw && !(array_info &&
                   #  # ]
    3652         [ #  # ]:          0 :                             test_bit(i, array_info->invert_mask)) &&
    3653                 :            :                             test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    3654                 :          0 :                                 value = !value;
    3655                 :          0 :                         trace_gpio_value(desc_to_gpio(desc), 0, value);
    3656                 :            :                         /*
    3657                 :            :                          * collect all normal outputs belonging to the same chip
    3658                 :            :                          * open drain and open source outputs are set individually
    3659                 :            :                          */
    3660   [ #  #  #  # ]:          0 :                         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags) && !raw) {
    3661                 :          0 :                                 gpio_set_open_drain_value_commit(desc, value);
    3662   [ #  #  #  # ]:          0 :                         } else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags) && !raw) {
    3663                 :          0 :                                 gpio_set_open_source_value_commit(desc, value);
    3664                 :            :                         } else {
    3665                 :            :                                 __set_bit(hwgpio, mask);
    3666         [ #  # ]:          0 :                                 if (value)
    3667                 :            :                                         __set_bit(hwgpio, bits);
    3668                 :            :                                 else
    3669                 :            :                                         __clear_bit(hwgpio, bits);
    3670                 :          0 :                                 count++;
    3671                 :            :                         }
    3672                 :          0 :                         i++;
    3673                 :            : 
    3674         [ #  # ]:          0 :                         if (array_info)
    3675                 :          0 :                                 i = find_next_zero_bit(array_info->set_mask,
    3676                 :            :                                                        array_size, i);
    3677         [ #  # ]:          0 :                 } while ((i < array_size) &&
    3678         [ #  # ]:          0 :                          (desc_array[i]->gdev->chip == chip));
    3679                 :            :                 /* push collected bits to outputs */
    3680         [ #  # ]:          0 :                 if (count != 0)
    3681                 :          0 :                         gpio_chip_set_multiple(chip, mask, bits);
    3682                 :            : 
    3683         [ #  # ]:          0 :                 if (mask != fastpath)
    3684                 :          0 :                         kfree(mask);
    3685                 :            :         }
    3686                 :            :         return 0;
    3687                 :            : }
    3688                 :            : 
    3689                 :            : /**
    3690                 :            :  * gpiod_set_raw_value() - assign a gpio's raw value
    3691                 :            :  * @desc: gpio whose value will be assigned
    3692                 :            :  * @value: value to assign
    3693                 :            :  *
    3694                 :            :  * Set the raw value of the GPIO, i.e. the value of its physical line without
    3695                 :            :  * regard for its ACTIVE_LOW status.
    3696                 :            :  *
    3697                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3698                 :            :  * complain if the GPIO chip functions potentially sleep.
    3699                 :            :  */
    3700                 :          0 : void gpiod_set_raw_value(struct gpio_desc *desc, int value)
    3701                 :            : {
    3702         [ #  # ]:          0 :         VALIDATE_DESC_VOID(desc);
    3703                 :            :         /* Should be using gpiod_set_raw_value_cansleep() */
    3704         [ #  # ]:          0 :         WARN_ON(desc->gdev->chip->can_sleep);
    3705                 :          0 :         gpiod_set_raw_value_commit(desc, value);
    3706                 :            : }
    3707                 :            : EXPORT_SYMBOL_GPL(gpiod_set_raw_value);
    3708                 :            : 
    3709                 :            : /**
    3710                 :            :  * gpiod_set_value_nocheck() - set a GPIO line value without checking
    3711                 :            :  * @desc: the descriptor to set the value on
    3712                 :            :  * @value: value to set
    3713                 :            :  *
    3714                 :            :  * This sets the value of a GPIO line backing a descriptor, applying
    3715                 :            :  * different semantic quirks like active low and open drain/source
    3716                 :            :  * handling.
    3717                 :            :  */
    3718                 :    2424001 : static void gpiod_set_value_nocheck(struct gpio_desc *desc, int value)
    3719                 :            : {
    3720         [ -  + ]:    2424001 :         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    3721                 :          0 :                 value = !value;
    3722         [ -  + ]:    2424001 :         if (test_bit(FLAG_OPEN_DRAIN, &desc->flags))
    3723                 :          0 :                 gpio_set_open_drain_value_commit(desc, value);
    3724         [ -  + ]:    2424001 :         else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags))
    3725                 :          0 :                 gpio_set_open_source_value_commit(desc, value);
    3726                 :            :         else
    3727                 :    2424001 :                 gpiod_set_raw_value_commit(desc, value);
    3728                 :    2424001 : }
    3729                 :            : 
    3730                 :            : /**
    3731                 :            :  * gpiod_set_value() - assign a gpio's value
    3732                 :            :  * @desc: gpio whose value will be assigned
    3733                 :            :  * @value: value to assign
    3734                 :            :  *
    3735                 :            :  * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW,
    3736                 :            :  * OPEN_DRAIN and OPEN_SOURCE flags into account.
    3737                 :            :  *
    3738                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3739                 :            :  * complain if the GPIO chip functions potentially sleep.
    3740                 :            :  */
    3741                 :    2424001 : void gpiod_set_value(struct gpio_desc *desc, int value)
    3742                 :            : {
    3743         [ +  - ]:    4848002 :         VALIDATE_DESC_VOID(desc);
    3744                 :            :         /* Should be using gpiod_set_value_cansleep() */
    3745         [ -  + ]:    2424001 :         WARN_ON(desc->gdev->chip->can_sleep);
    3746                 :    2424001 :         gpiod_set_value_nocheck(desc, value);
    3747                 :            : }
    3748                 :            : EXPORT_SYMBOL_GPL(gpiod_set_value);
    3749                 :            : 
    3750                 :            : /**
    3751                 :            :  * gpiod_set_raw_array_value() - assign values to an array of GPIOs
    3752                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    3753                 :            :  * @desc_array: array of GPIO descriptors whose values will be assigned
    3754                 :            :  * @array_info: information on applicability of fast bitmap processing path
    3755                 :            :  * @value_bitmap: bitmap of values to assign
    3756                 :            :  *
    3757                 :            :  * Set the raw values of the GPIOs, i.e. the values of the physical lines
    3758                 :            :  * without regard for their ACTIVE_LOW status.
    3759                 :            :  *
    3760                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3761                 :            :  * complain if the GPIO chip functions potentially sleep.
    3762                 :            :  */
    3763                 :          0 : int gpiod_set_raw_array_value(unsigned int array_size,
    3764                 :            :                               struct gpio_desc **desc_array,
    3765                 :            :                               struct gpio_array *array_info,
    3766                 :            :                               unsigned long *value_bitmap)
    3767                 :            : {
    3768         [ #  # ]:          0 :         if (!desc_array)
    3769                 :            :                 return -EINVAL;
    3770                 :          0 :         return gpiod_set_array_value_complex(true, false, array_size,
    3771                 :            :                                         desc_array, array_info, value_bitmap);
    3772                 :            : }
    3773                 :            : EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value);
    3774                 :            : 
    3775                 :            : /**
    3776                 :            :  * gpiod_set_array_value() - assign values to an array of GPIOs
    3777                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    3778                 :            :  * @desc_array: array of GPIO descriptors whose values will be assigned
    3779                 :            :  * @array_info: information on applicability of fast bitmap processing path
    3780                 :            :  * @value_bitmap: bitmap of values to assign
    3781                 :            :  *
    3782                 :            :  * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
    3783                 :            :  * into account.
    3784                 :            :  *
    3785                 :            :  * This function can be called from contexts where we cannot sleep, and will
    3786                 :            :  * complain if the GPIO chip functions potentially sleep.
    3787                 :            :  */
    3788                 :          0 : int gpiod_set_array_value(unsigned int array_size,
    3789                 :            :                           struct gpio_desc **desc_array,
    3790                 :            :                           struct gpio_array *array_info,
    3791                 :            :                           unsigned long *value_bitmap)
    3792                 :            : {
    3793         [ #  # ]:          0 :         if (!desc_array)
    3794                 :            :                 return -EINVAL;
    3795                 :          0 :         return gpiod_set_array_value_complex(false, false, array_size,
    3796                 :            :                                              desc_array, array_info,
    3797                 :            :                                              value_bitmap);
    3798                 :            : }
    3799                 :            : EXPORT_SYMBOL_GPL(gpiod_set_array_value);
    3800                 :            : 
    3801                 :            : /**
    3802                 :            :  * gpiod_cansleep() - report whether gpio value access may sleep
    3803                 :            :  * @desc: gpio to check
    3804                 :            :  *
    3805                 :            :  */
    3806                 :        414 : int gpiod_cansleep(const struct gpio_desc *desc)
    3807                 :            : {
    3808         [ +  - ]:        414 :         VALIDATE_DESC(desc);
    3809                 :        414 :         return desc->gdev->chip->can_sleep;
    3810                 :            : }
    3811                 :            : EXPORT_SYMBOL_GPL(gpiod_cansleep);
    3812                 :            : 
    3813                 :            : /**
    3814                 :            :  * gpiod_set_consumer_name() - set the consumer name for the descriptor
    3815                 :            :  * @desc: gpio to set the consumer name on
    3816                 :            :  * @name: the new consumer name
    3817                 :            :  */
    3818                 :        414 : int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
    3819                 :            : {
    3820         [ +  - ]:        414 :         VALIDATE_DESC(desc);
    3821         [ +  - ]:        414 :         if (name) {
    3822                 :        414 :                 name = kstrdup_const(name, GFP_KERNEL);
    3823         [ +  - ]:        414 :                 if (!name)
    3824                 :            :                         return -ENOMEM;
    3825                 :            :         }
    3826                 :            : 
    3827                 :        414 :         kfree_const(desc->label);
    3828                 :            :         desc_set_label(desc, name);
    3829                 :            : 
    3830                 :        414 :         return 0;
    3831                 :            : }
    3832                 :            : EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
    3833                 :            : 
    3834                 :            : /**
    3835                 :            :  * gpiod_to_irq() - return the IRQ corresponding to a GPIO
    3836                 :            :  * @desc: gpio whose IRQ will be returned (already requested)
    3837                 :            :  *
    3838                 :            :  * Return the IRQ corresponding to the passed GPIO, or an error code in case of
    3839                 :            :  * error.
    3840                 :            :  */
    3841                 :          0 : int gpiod_to_irq(const struct gpio_desc *desc)
    3842                 :            : {
    3843                 :            :         struct gpio_chip *chip;
    3844                 :            :         int offset;
    3845                 :            : 
    3846                 :            :         /*
    3847                 :            :          * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
    3848                 :            :          * requires this function to not return zero on an invalid descriptor
    3849                 :            :          * but rather a negative error number.
    3850                 :            :          */
    3851   [ #  #  #  #  :          0 :         if (!desc || IS_ERR(desc) || !desc->gdev || !desc->gdev->chip)
             #  #  #  # ]
    3852                 :            :                 return -EINVAL;
    3853                 :            : 
    3854                 :            :         chip = desc->gdev->chip;
    3855                 :            :         offset = gpio_chip_hwgpio(desc);
    3856         [ #  # ]:          0 :         if (chip->to_irq) {
    3857                 :          0 :                 int retirq = chip->to_irq(chip, offset);
    3858                 :            : 
    3859                 :            :                 /* Zero means NO_IRQ */
    3860         [ #  # ]:          0 :                 if (!retirq)
    3861                 :            :                         return -ENXIO;
    3862                 :            : 
    3863                 :          0 :                 return retirq;
    3864                 :            :         }
    3865                 :            :         return -ENXIO;
    3866                 :            : }
    3867                 :            : EXPORT_SYMBOL_GPL(gpiod_to_irq);
    3868                 :            : 
    3869                 :            : /**
    3870                 :            :  * gpiochip_lock_as_irq() - lock a GPIO to be used as IRQ
    3871                 :            :  * @chip: the chip the GPIO to lock belongs to
    3872                 :            :  * @offset: the offset of the GPIO to lock as IRQ
    3873                 :            :  *
    3874                 :            :  * This is used directly by GPIO drivers that want to lock down
    3875                 :            :  * a certain GPIO line to be used for IRQs.
    3876                 :            :  */
    3877                 :          0 : int gpiochip_lock_as_irq(struct gpio_chip *chip, unsigned int offset)
    3878                 :            : {
    3879                 :            :         struct gpio_desc *desc;
    3880                 :            : 
    3881                 :          0 :         desc = gpiochip_get_desc(chip, offset);
    3882         [ #  # ]:          0 :         if (IS_ERR(desc))
    3883                 :          0 :                 return PTR_ERR(desc);
    3884                 :            : 
    3885                 :            :         /*
    3886                 :            :          * If it's fast: flush the direction setting if something changed
    3887                 :            :          * behind our back
    3888                 :            :          */
    3889   [ #  #  #  # ]:          0 :         if (!chip->can_sleep && chip->get_direction) {
    3890                 :          0 :                 int dir = gpiod_get_direction(desc);
    3891                 :            : 
    3892         [ #  # ]:          0 :                 if (dir < 0) {
    3893                 :          0 :                         chip_err(chip, "%s: cannot get GPIO direction\n",
    3894                 :            :                                  __func__);
    3895                 :          0 :                         return dir;
    3896                 :            :                 }
    3897                 :            :         }
    3898                 :            : 
    3899                 :            :         /* To be valid for IRQ the line needs to be input or open drain */
    3900                 :            :         if (dont_test_bit(FLAG_IS_OUT, &desc->flags) &&
    3901                 :            :             !dont_test_bit(FLAG_OPEN_DRAIN, &desc->flags)) {
    3902                 :            :                 chip_err(chip,
    3903                 :            :                          "%s: tried to flag a GPIO set as output for IRQ\n",
    3904                 :            :                          __func__);
    3905                 :            :                 return -EIO;
    3906                 :            :         }
    3907                 :            : 
    3908                 :          0 :         set_bit(FLAG_USED_AS_IRQ, &desc->flags);
    3909                 :          0 :         set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
    3910                 :            : 
    3911                 :            :         /*
    3912                 :            :          * If the consumer has not set up a label (such as when the
    3913                 :            :          * IRQ is referenced from .to_irq()) we set up a label here
    3914                 :            :          * so it is clear this is used as an interrupt.
    3915                 :            :          */
    3916         [ #  # ]:          0 :         if (!desc->label)
    3917                 :            :                 desc_set_label(desc, "interrupt");
    3918                 :            : 
    3919                 :            :         return 0;
    3920                 :            : }
    3921                 :            : EXPORT_SYMBOL_GPL(gpiochip_lock_as_irq);
    3922                 :            : 
    3923                 :            : /**
    3924                 :            :  * gpiochip_unlock_as_irq() - unlock a GPIO used as IRQ
    3925                 :            :  * @chip: the chip the GPIO to lock belongs to
    3926                 :            :  * @offset: the offset of the GPIO to lock as IRQ
    3927                 :            :  *
    3928                 :            :  * This is used directly by GPIO drivers that want to indicate
    3929                 :            :  * that a certain GPIO is no longer used exclusively for IRQ.
    3930                 :            :  */
    3931                 :          0 : void gpiochip_unlock_as_irq(struct gpio_chip *chip, unsigned int offset)
    3932                 :            : {
    3933                 :            :         struct gpio_desc *desc;
    3934                 :            : 
    3935                 :          0 :         desc = gpiochip_get_desc(chip, offset);
    3936         [ #  # ]:          0 :         if (IS_ERR(desc))
    3937                 :          0 :                 return;
    3938                 :            : 
    3939                 :          0 :         clear_bit(FLAG_USED_AS_IRQ, &desc->flags);
    3940                 :          0 :         clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
    3941                 :            : 
    3942                 :            :         /* If we only had this marking, erase it */
    3943   [ #  #  #  # ]:          0 :         if (desc->label && !strcmp(desc->label, "interrupt"))
    3944                 :            :                 desc_set_label(desc, NULL);
    3945                 :            : }
    3946                 :            : EXPORT_SYMBOL_GPL(gpiochip_unlock_as_irq);
    3947                 :            : 
    3948                 :          0 : void gpiochip_disable_irq(struct gpio_chip *chip, unsigned int offset)
    3949                 :            : {
    3950                 :          0 :         struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
    3951                 :            : 
    3952   [ #  #  #  # ]:          0 :         if (!IS_ERR(desc) &&
    3953         [ #  # ]:          0 :             !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags)))
    3954                 :          0 :                 clear_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
    3955                 :          0 : }
    3956                 :            : EXPORT_SYMBOL_GPL(gpiochip_disable_irq);
    3957                 :            : 
    3958                 :          0 : void gpiochip_enable_irq(struct gpio_chip *chip, unsigned int offset)
    3959                 :            : {
    3960                 :          0 :         struct gpio_desc *desc = gpiochip_get_desc(chip, offset);
    3961                 :            : 
    3962   [ #  #  #  # ]:          0 :         if (!IS_ERR(desc) &&
    3963         [ #  # ]:          0 :             !WARN_ON(!test_bit(FLAG_USED_AS_IRQ, &desc->flags))) {
    3964                 :            :                 /*
    3965                 :            :                  * We must not be output when using IRQ UNLESS we are
    3966                 :            :                  * open drain.
    3967                 :            :                  */
    3968   [ #  #  #  #  :          0 :                 WARN_ON(test_bit(FLAG_IS_OUT, &desc->flags) &&
                   #  # ]
    3969                 :            :                         !test_bit(FLAG_OPEN_DRAIN, &desc->flags));
    3970                 :          0 :                 set_bit(FLAG_IRQ_IS_ENABLED, &desc->flags);
    3971                 :            :         }
    3972                 :          0 : }
    3973                 :            : EXPORT_SYMBOL_GPL(gpiochip_enable_irq);
    3974                 :            : 
    3975                 :          0 : bool gpiochip_line_is_irq(struct gpio_chip *chip, unsigned int offset)
    3976                 :            : {
    3977         [ #  # ]:          0 :         if (offset >= chip->ngpio)
    3978                 :            :                 return false;
    3979                 :            : 
    3980                 :          0 :         return test_bit(FLAG_USED_AS_IRQ, &chip->gpiodev->descs[offset].flags);
    3981                 :            : }
    3982                 :            : EXPORT_SYMBOL_GPL(gpiochip_line_is_irq);
    3983                 :            : 
    3984                 :          0 : int gpiochip_reqres_irq(struct gpio_chip *chip, unsigned int offset)
    3985                 :            : {
    3986                 :            :         int ret;
    3987                 :            : 
    3988         [ #  # ]:          0 :         if (!try_module_get(chip->gpiodev->owner))
    3989                 :            :                 return -ENODEV;
    3990                 :            : 
    3991                 :          0 :         ret = gpiochip_lock_as_irq(chip, offset);
    3992         [ #  # ]:          0 :         if (ret) {
    3993                 :          0 :                 chip_err(chip, "unable to lock HW IRQ %u for IRQ\n", offset);
    3994                 :          0 :                 module_put(chip->gpiodev->owner);
    3995                 :          0 :                 return ret;
    3996                 :            :         }
    3997                 :            :         return 0;
    3998                 :            : }
    3999                 :            : EXPORT_SYMBOL_GPL(gpiochip_reqres_irq);
    4000                 :            : 
    4001                 :          0 : void gpiochip_relres_irq(struct gpio_chip *chip, unsigned int offset)
    4002                 :            : {
    4003                 :          0 :         gpiochip_unlock_as_irq(chip, offset);
    4004                 :          0 :         module_put(chip->gpiodev->owner);
    4005                 :          0 : }
    4006                 :            : EXPORT_SYMBOL_GPL(gpiochip_relres_irq);
    4007                 :            : 
    4008                 :          0 : bool gpiochip_line_is_open_drain(struct gpio_chip *chip, unsigned int offset)
    4009                 :            : {
    4010   [ #  #  #  # ]:          0 :         if (offset >= chip->ngpio)
    4011                 :            :                 return false;
    4012                 :            : 
    4013                 :          0 :         return test_bit(FLAG_OPEN_DRAIN, &chip->gpiodev->descs[offset].flags);
    4014                 :            : }
    4015                 :            : EXPORT_SYMBOL_GPL(gpiochip_line_is_open_drain);
    4016                 :            : 
    4017                 :          0 : bool gpiochip_line_is_open_source(struct gpio_chip *chip, unsigned int offset)
    4018                 :            : {
    4019   [ #  #  #  # ]:          0 :         if (offset >= chip->ngpio)
    4020                 :            :                 return false;
    4021                 :            : 
    4022                 :          0 :         return test_bit(FLAG_OPEN_SOURCE, &chip->gpiodev->descs[offset].flags);
    4023                 :            : }
    4024                 :            : EXPORT_SYMBOL_GPL(gpiochip_line_is_open_source);
    4025                 :            : 
    4026                 :          0 : bool gpiochip_line_is_persistent(struct gpio_chip *chip, unsigned int offset)
    4027                 :            : {
    4028         [ #  # ]:          0 :         if (offset >= chip->ngpio)
    4029                 :            :                 return false;
    4030                 :            : 
    4031                 :          0 :         return !test_bit(FLAG_TRANSITORY, &chip->gpiodev->descs[offset].flags);
    4032                 :            : }
    4033                 :            : EXPORT_SYMBOL_GPL(gpiochip_line_is_persistent);
    4034                 :            : 
    4035                 :            : /**
    4036                 :            :  * gpiod_get_raw_value_cansleep() - return a gpio's raw value
    4037                 :            :  * @desc: gpio whose value will be returned
    4038                 :            :  *
    4039                 :            :  * Return the GPIO's raw value, i.e. the value of the physical line disregarding
    4040                 :            :  * its ACTIVE_LOW status, or negative errno on failure.
    4041                 :            :  *
    4042                 :            :  * This function is to be called from contexts that can sleep.
    4043                 :            :  */
    4044                 :          0 : int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
    4045                 :            : {
    4046                 :            :         might_sleep_if(extra_checks);
    4047         [ #  # ]:          0 :         VALIDATE_DESC(desc);
    4048                 :          0 :         return gpiod_get_raw_value_commit(desc);
    4049                 :            : }
    4050                 :            : EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep);
    4051                 :            : 
    4052                 :            : /**
    4053                 :            :  * gpiod_get_value_cansleep() - return a gpio's value
    4054                 :            :  * @desc: gpio whose value will be returned
    4055                 :            :  *
    4056                 :            :  * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into
    4057                 :            :  * account, or negative errno on failure.
    4058                 :            :  *
    4059                 :            :  * This function is to be called from contexts that can sleep.
    4060                 :            :  */
    4061                 :        621 : int gpiod_get_value_cansleep(const struct gpio_desc *desc)
    4062                 :            : {
    4063                 :            :         int value;
    4064                 :            : 
    4065                 :            :         might_sleep_if(extra_checks);
    4066         [ +  - ]:        621 :         VALIDATE_DESC(desc);
    4067                 :        621 :         value = gpiod_get_raw_value_commit(desc);
    4068         [ +  - ]:        621 :         if (value < 0)
    4069                 :            :                 return value;
    4070                 :            : 
    4071         [ -  + ]:        621 :         if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
    4072                 :          0 :                 value = !value;
    4073                 :            : 
    4074                 :        621 :         return value;
    4075                 :            : }
    4076                 :            : EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep);
    4077                 :            : 
    4078                 :            : /**
    4079                 :            :  * gpiod_get_raw_array_value_cansleep() - read raw values from an array of GPIOs
    4080                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    4081                 :            :  * @desc_array: array of GPIO descriptors whose values will be read
    4082                 :            :  * @array_info: information on applicability of fast bitmap processing path
    4083                 :            :  * @value_bitmap: bitmap to store the read values
    4084                 :            :  *
    4085                 :            :  * Read the raw values of the GPIOs, i.e. the values of the physical lines
    4086                 :            :  * without regard for their ACTIVE_LOW status.  Return 0 in case of success,
    4087                 :            :  * else an error code.
    4088                 :            :  *
    4089                 :            :  * This function is to be called from contexts that can sleep.
    4090                 :            :  */
    4091                 :          0 : int gpiod_get_raw_array_value_cansleep(unsigned int array_size,
    4092                 :            :                                        struct gpio_desc **desc_array,
    4093                 :            :                                        struct gpio_array *array_info,
    4094                 :            :                                        unsigned long *value_bitmap)
    4095                 :            : {
    4096                 :            :         might_sleep_if(extra_checks);
    4097         [ #  # ]:          0 :         if (!desc_array)
    4098                 :            :                 return -EINVAL;
    4099                 :          0 :         return gpiod_get_array_value_complex(true, true, array_size,
    4100                 :            :                                              desc_array, array_info,
    4101                 :            :                                              value_bitmap);
    4102                 :            : }
    4103                 :            : EXPORT_SYMBOL_GPL(gpiod_get_raw_array_value_cansleep);
    4104                 :            : 
    4105                 :            : /**
    4106                 :            :  * gpiod_get_array_value_cansleep() - read values from an array of GPIOs
    4107                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    4108                 :            :  * @desc_array: array of GPIO descriptors whose values will be read
    4109                 :            :  * @array_info: information on applicability of fast bitmap processing path
    4110                 :            :  * @value_bitmap: bitmap to store the read values
    4111                 :            :  *
    4112                 :            :  * Read the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
    4113                 :            :  * into account.  Return 0 in case of success, else an error code.
    4114                 :            :  *
    4115                 :            :  * This function is to be called from contexts that can sleep.
    4116                 :            :  */
    4117                 :          0 : int gpiod_get_array_value_cansleep(unsigned int array_size,
    4118                 :            :                                    struct gpio_desc **desc_array,
    4119                 :            :                                    struct gpio_array *array_info,
    4120                 :            :                                    unsigned long *value_bitmap)
    4121                 :            : {
    4122                 :            :         might_sleep_if(extra_checks);
    4123         [ #  # ]:          0 :         if (!desc_array)
    4124                 :            :                 return -EINVAL;
    4125                 :          0 :         return gpiod_get_array_value_complex(false, true, array_size,
    4126                 :            :                                              desc_array, array_info,
    4127                 :            :                                              value_bitmap);
    4128                 :            : }
    4129                 :            : EXPORT_SYMBOL_GPL(gpiod_get_array_value_cansleep);
    4130                 :            : 
    4131                 :            : /**
    4132                 :            :  * gpiod_set_raw_value_cansleep() - assign a gpio's raw value
    4133                 :            :  * @desc: gpio whose value will be assigned
    4134                 :            :  * @value: value to assign
    4135                 :            :  *
    4136                 :            :  * Set the raw value of the GPIO, i.e. the value of its physical line without
    4137                 :            :  * regard for its ACTIVE_LOW status.
    4138                 :            :  *
    4139                 :            :  * This function is to be called from contexts that can sleep.
    4140                 :            :  */
    4141                 :          0 : void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
    4142                 :            : {
    4143                 :            :         might_sleep_if(extra_checks);
    4144         [ #  # ]:          0 :         VALIDATE_DESC_VOID(desc);
    4145                 :          0 :         gpiod_set_raw_value_commit(desc, value);
    4146                 :            : }
    4147                 :            : EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep);
    4148                 :            : 
    4149                 :            : /**
    4150                 :            :  * gpiod_set_value_cansleep() - assign a gpio's value
    4151                 :            :  * @desc: gpio whose value will be assigned
    4152                 :            :  * @value: value to assign
    4153                 :            :  *
    4154                 :            :  * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
    4155                 :            :  * account
    4156                 :            :  *
    4157                 :            :  * This function is to be called from contexts that can sleep.
    4158                 :            :  */
    4159                 :          0 : void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
    4160                 :            : {
    4161                 :            :         might_sleep_if(extra_checks);
    4162         [ #  # ]:          0 :         VALIDATE_DESC_VOID(desc);
    4163                 :          0 :         gpiod_set_value_nocheck(desc, value);
    4164                 :            : }
    4165                 :            : EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
    4166                 :            : 
    4167                 :            : /**
    4168                 :            :  * gpiod_set_raw_array_value_cansleep() - assign values to an array of GPIOs
    4169                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    4170                 :            :  * @desc_array: array of GPIO descriptors whose values will be assigned
    4171                 :            :  * @array_info: information on applicability of fast bitmap processing path
    4172                 :            :  * @value_bitmap: bitmap of values to assign
    4173                 :            :  *
    4174                 :            :  * Set the raw values of the GPIOs, i.e. the values of the physical lines
    4175                 :            :  * without regard for their ACTIVE_LOW status.
    4176                 :            :  *
    4177                 :            :  * This function is to be called from contexts that can sleep.
    4178                 :            :  */
    4179                 :          0 : int gpiod_set_raw_array_value_cansleep(unsigned int array_size,
    4180                 :            :                                        struct gpio_desc **desc_array,
    4181                 :            :                                        struct gpio_array *array_info,
    4182                 :            :                                        unsigned long *value_bitmap)
    4183                 :            : {
    4184                 :            :         might_sleep_if(extra_checks);
    4185         [ #  # ]:          0 :         if (!desc_array)
    4186                 :            :                 return -EINVAL;
    4187                 :          0 :         return gpiod_set_array_value_complex(true, true, array_size, desc_array,
    4188                 :            :                                       array_info, value_bitmap);
    4189                 :            : }
    4190                 :            : EXPORT_SYMBOL_GPL(gpiod_set_raw_array_value_cansleep);
    4191                 :            : 
    4192                 :            : /**
    4193                 :            :  * gpiod_add_lookup_tables() - register GPIO device consumers
    4194                 :            :  * @tables: list of tables of consumers to register
    4195                 :            :  * @n: number of tables in the list
    4196                 :            :  */
    4197                 :          0 : void gpiod_add_lookup_tables(struct gpiod_lookup_table **tables, size_t n)
    4198                 :            : {
    4199                 :            :         unsigned int i;
    4200                 :            : 
    4201                 :          0 :         mutex_lock(&gpio_lookup_lock);
    4202                 :            : 
    4203         [ #  # ]:          0 :         for (i = 0; i < n; i++)
    4204                 :          0 :                 list_add_tail(&tables[i]->list, &gpio_lookup_list);
    4205                 :            : 
    4206                 :          0 :         mutex_unlock(&gpio_lookup_lock);
    4207                 :          0 : }
    4208                 :            : 
    4209                 :            : /**
    4210                 :            :  * gpiod_set_array_value_cansleep() - assign values to an array of GPIOs
    4211                 :            :  * @array_size: number of elements in the descriptor array / value bitmap
    4212                 :            :  * @desc_array: array of GPIO descriptors whose values will be assigned
    4213                 :            :  * @array_info: information on applicability of fast bitmap processing path
    4214                 :            :  * @value_bitmap: bitmap of values to assign
    4215                 :            :  *
    4216                 :            :  * Set the logical values of the GPIOs, i.e. taking their ACTIVE_LOW status
    4217                 :            :  * into account.
    4218                 :            :  *
    4219                 :            :  * This function is to be called from contexts that can sleep.
    4220                 :            :  */
    4221                 :          0 : int gpiod_set_array_value_cansleep(unsigned int array_size,
    4222                 :            :                                    struct gpio_desc **desc_array,
    4223                 :            :                                    struct gpio_array *array_info,
    4224                 :            :                                    unsigned long *value_bitmap)
    4225                 :            : {
    4226                 :            :         might_sleep_if(extra_checks);
    4227         [ #  # ]:          0 :         if (!desc_array)
    4228                 :            :                 return -EINVAL;
    4229                 :          0 :         return gpiod_set_array_value_complex(false, true, array_size,
    4230                 :            :                                              desc_array, array_info,
    4231                 :            :                                              value_bitmap);
    4232                 :            : }
    4233                 :            : EXPORT_SYMBOL_GPL(gpiod_set_array_value_cansleep);
    4234                 :            : 
    4235                 :            : /**
    4236                 :            :  * gpiod_add_lookup_table() - register GPIO device consumers
    4237                 :            :  * @table: table of consumers to register
    4238                 :            :  */
    4239                 :          0 : void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
    4240                 :            : {
    4241                 :          0 :         mutex_lock(&gpio_lookup_lock);
    4242                 :            : 
    4243                 :          0 :         list_add_tail(&table->list, &gpio_lookup_list);
    4244                 :            : 
    4245                 :          0 :         mutex_unlock(&gpio_lookup_lock);
    4246                 :          0 : }
    4247                 :            : EXPORT_SYMBOL_GPL(gpiod_add_lookup_table);
    4248                 :            : 
    4249                 :            : /**
    4250                 :            :  * gpiod_remove_lookup_table() - unregister GPIO device consumers
    4251                 :            :  * @table: table of consumers to unregister
    4252                 :            :  */
    4253                 :          0 : void gpiod_remove_lookup_table(struct gpiod_lookup_table *table)
    4254                 :            : {
    4255                 :          0 :         mutex_lock(&gpio_lookup_lock);
    4256                 :            : 
    4257                 :            :         list_del(&table->list);
    4258                 :            : 
    4259                 :          0 :         mutex_unlock(&gpio_lookup_lock);
    4260                 :          0 : }
    4261                 :            : EXPORT_SYMBOL_GPL(gpiod_remove_lookup_table);
    4262                 :            : 
    4263                 :            : /**
    4264                 :            :  * gpiod_add_hogs() - register a set of GPIO hogs from machine code
    4265                 :            :  * @hogs: table of gpio hog entries with a zeroed sentinel at the end
    4266                 :            :  */
    4267                 :          0 : void gpiod_add_hogs(struct gpiod_hog *hogs)
    4268                 :            : {
    4269                 :            :         struct gpio_chip *chip;
    4270                 :            :         struct gpiod_hog *hog;
    4271                 :            : 
    4272                 :          0 :         mutex_lock(&gpio_machine_hogs_mutex);
    4273                 :            : 
    4274         [ #  # ]:          0 :         for (hog = &hogs[0]; hog->chip_label; hog++) {
    4275                 :          0 :                 list_add_tail(&hog->list, &gpio_machine_hogs);
    4276                 :            : 
    4277                 :            :                 /*
    4278                 :            :                  * The chip may have been registered earlier, so check if it
    4279                 :            :                  * exists and, if so, try to hog the line now.
    4280                 :            :                  */
    4281                 :          0 :                 chip = find_chip_by_name(hog->chip_label);
    4282         [ #  # ]:          0 :                 if (chip)
    4283                 :          0 :                         gpiochip_machine_hog(chip, hog);
    4284                 :            :         }
    4285                 :            : 
    4286                 :          0 :         mutex_unlock(&gpio_machine_hogs_mutex);
    4287                 :          0 : }
    4288                 :            : EXPORT_SYMBOL_GPL(gpiod_add_hogs);
    4289                 :            : 
    4290                 :       1035 : static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
    4291                 :            : {
    4292         [ +  - ]:       1035 :         const char *dev_id = dev ? dev_name(dev) : NULL;
    4293                 :            :         struct gpiod_lookup_table *table;
    4294                 :            : 
    4295                 :       1035 :         mutex_lock(&gpio_lookup_lock);
    4296                 :            : 
    4297         [ -  + ]:       1035 :         list_for_each_entry(table, &gpio_lookup_list, list) {
    4298   [ #  #  #  # ]:          0 :                 if (table->dev_id && dev_id) {
    4299                 :            :                         /*
    4300                 :            :                          * Valid strings on both ends, must be identical to have
    4301                 :            :                          * a match
    4302                 :            :                          */
    4303         [ #  # ]:          0 :                         if (!strcmp(table->dev_id, dev_id))
    4304                 :            :                                 goto found;
    4305                 :            :                 } else {
    4306                 :            :                         /*
    4307                 :            :                          * One of the pointers is NULL, so both must be to have
    4308                 :            :                          * a match
    4309                 :            :                          */
    4310         [ #  # ]:          0 :                         if (dev_id == table->dev_id)
    4311                 :            :                                 goto found;
    4312                 :            :                 }
    4313                 :            :         }
    4314                 :            :         table = NULL;
    4315                 :            : 
    4316                 :            : found:
    4317                 :       1035 :         mutex_unlock(&gpio_lookup_lock);
    4318                 :       1035 :         return table;
    4319                 :            : }
    4320                 :            : 
    4321                 :       1035 : static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
    4322                 :            :                                     unsigned int idx, unsigned long *flags)
    4323                 :            : {
    4324                 :            :         struct gpio_desc *desc = ERR_PTR(-ENOENT);
    4325                 :            :         struct gpiod_lookup_table *table;
    4326                 :            :         struct gpiod_lookup *p;
    4327                 :            : 
    4328                 :       1035 :         table = gpiod_find_lookup_table(dev);
    4329         [ -  + ]:       1035 :         if (!table)
    4330                 :            :                 return desc;
    4331                 :            : 
    4332         [ #  # ]:          0 :         for (p = &table->table[0]; p->chip_label; p++) {
    4333                 :            :                 struct gpio_chip *chip;
    4334                 :            : 
    4335                 :            :                 /* idx must always match exactly */
    4336         [ #  # ]:          0 :                 if (p->idx != idx)
    4337                 :          0 :                         continue;
    4338                 :            : 
    4339                 :            :                 /* If the lookup entry has a con_id, require exact match */
    4340   [ #  #  #  #  :          0 :                 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
                   #  # ]
    4341                 :          0 :                         continue;
    4342                 :            : 
    4343                 :          0 :                 chip = find_chip_by_name(p->chip_label);
    4344                 :            : 
    4345         [ #  # ]:          0 :                 if (!chip) {
    4346                 :            :                         /*
    4347                 :            :                          * As the lookup table indicates a chip with
    4348                 :            :                          * p->chip_label should exist, assume it may
    4349                 :            :                          * still appear later and let the interested
    4350                 :            :                          * consumer be probed again or let the Deferred
    4351                 :            :                          * Probe infrastructure handle the error.
    4352                 :            :                          */
    4353                 :          0 :                         dev_warn(dev, "cannot find GPIO chip %s, deferring\n",
    4354                 :            :                                  p->chip_label);
    4355                 :          0 :                         return ERR_PTR(-EPROBE_DEFER);
    4356                 :            :                 }
    4357                 :            : 
    4358         [ #  # ]:          0 :                 if (chip->ngpio <= p->chip_hwnum) {
    4359                 :          0 :                         dev_err(dev,
    4360                 :            :                                 "requested GPIO %u (%u) is out of range [0..%u] for chip %s\n",
    4361                 :            :                                 idx, p->chip_hwnum, chip->ngpio - 1,
    4362                 :            :                                 chip->label);
    4363                 :          0 :                         return ERR_PTR(-EINVAL);
    4364                 :            :                 }
    4365                 :            : 
    4366                 :            :                 desc = gpiochip_get_desc(chip, p->chip_hwnum);
    4367                 :          0 :                 *flags = p->flags;
    4368                 :            : 
    4369                 :          0 :                 return desc;
    4370                 :            :         }
    4371                 :            : 
    4372                 :            :         return desc;
    4373                 :            : }
    4374                 :            : 
    4375                 :          0 : static int platform_gpio_count(struct device *dev, const char *con_id)
    4376                 :            : {
    4377                 :            :         struct gpiod_lookup_table *table;
    4378                 :            :         struct gpiod_lookup *p;
    4379                 :            :         unsigned int count = 0;
    4380                 :            : 
    4381                 :          0 :         table = gpiod_find_lookup_table(dev);
    4382         [ #  # ]:          0 :         if (!table)
    4383                 :            :                 return -ENOENT;
    4384                 :            : 
    4385         [ #  # ]:          0 :         for (p = &table->table[0]; p->chip_label; p++) {
    4386   [ #  #  #  #  :          0 :                 if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
             #  #  #  # ]
    4387         [ #  # ]:          0 :                     (!con_id && !p->con_id))
    4388                 :          0 :                         count++;
    4389                 :            :         }
    4390         [ #  # ]:          0 :         if (!count)
    4391                 :            :                 return -ENOENT;
    4392                 :            : 
    4393                 :          0 :         return count;
    4394                 :            : }
    4395                 :            : 
    4396                 :            : /**
    4397                 :            :  * gpiod_count - return the number of GPIOs associated with a device / function
    4398                 :            :  *              or -ENOENT if no GPIO has been assigned to the requested function
    4399                 :            :  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
    4400                 :            :  * @con_id:     function within the GPIO consumer
    4401                 :            :  */
    4402                 :          0 : int gpiod_count(struct device *dev, const char *con_id)
    4403                 :            : {
    4404                 :            :         int count = -ENOENT;
    4405                 :            : 
    4406   [ #  #  #  # ]:          0 :         if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node)
    4407                 :          0 :                 count = of_gpio_get_count(dev, con_id);
    4408                 :            :         else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev))
    4409                 :            :                 count = acpi_gpio_count(dev, con_id);
    4410                 :            : 
    4411         [ #  # ]:          0 :         if (count < 0)
    4412                 :          0 :                 count = platform_gpio_count(dev, con_id);
    4413                 :            : 
    4414                 :          0 :         return count;
    4415                 :            : }
    4416                 :            : EXPORT_SYMBOL_GPL(gpiod_count);
    4417                 :            : 
    4418                 :            : /**
    4419                 :            :  * gpiod_get - obtain a GPIO for a given GPIO function
    4420                 :            :  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
    4421                 :            :  * @con_id:     function within the GPIO consumer
    4422                 :            :  * @flags:      optional GPIO initialization flags
    4423                 :            :  *
    4424                 :            :  * Return the GPIO descriptor corresponding to the function con_id of device
    4425                 :            :  * dev, -ENOENT if no GPIO has been assigned to the requested function, or
    4426                 :            :  * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
    4427                 :            :  */
    4428                 :          0 : struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id,
    4429                 :            :                                          enum gpiod_flags flags)
    4430                 :            : {
    4431                 :          0 :         return gpiod_get_index(dev, con_id, 0, flags);
    4432                 :            : }
    4433                 :            : EXPORT_SYMBOL_GPL(gpiod_get);
    4434                 :            : 
    4435                 :            : /**
    4436                 :            :  * gpiod_get_optional - obtain an optional GPIO for a given GPIO function
    4437                 :            :  * @dev: GPIO consumer, can be NULL for system-global GPIOs
    4438                 :            :  * @con_id: function within the GPIO consumer
    4439                 :            :  * @flags: optional GPIO initialization flags
    4440                 :            :  *
    4441                 :            :  * This is equivalent to gpiod_get(), except that when no GPIO was assigned to
    4442                 :            :  * the requested function it will return NULL. This is convenient for drivers
    4443                 :            :  * that need to handle optional GPIOs.
    4444                 :            :  */
    4445                 :        414 : struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
    4446                 :            :                                                   const char *con_id,
    4447                 :            :                                                   enum gpiod_flags flags)
    4448                 :            : {
    4449                 :        414 :         return gpiod_get_index_optional(dev, con_id, 0, flags);
    4450                 :            : }
    4451                 :            : EXPORT_SYMBOL_GPL(gpiod_get_optional);
    4452                 :            : 
    4453                 :            : 
    4454                 :            : /**
    4455                 :            :  * gpiod_configure_flags - helper function to configure a given GPIO
    4456                 :            :  * @desc:       gpio whose value will be assigned
    4457                 :            :  * @con_id:     function within the GPIO consumer
    4458                 :            :  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
    4459                 :            :  *              of_find_gpio() or of_get_gpio_hog()
    4460                 :            :  * @dflags:     gpiod_flags - optional GPIO initialization flags
    4461                 :            :  *
    4462                 :            :  * Return 0 on success, -ENOENT if no GPIO has been assigned to the
    4463                 :            :  * requested function and/or index, or another IS_ERR() code if an error
    4464                 :            :  * occurred while trying to acquire the GPIO.
    4465                 :            :  */
    4466                 :        414 : int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
    4467                 :            :                 unsigned long lflags, enum gpiod_flags dflags)
    4468                 :            : {
    4469                 :            :         int ret;
    4470                 :            : 
    4471         [ -  + ]:        414 :         if (lflags & GPIO_ACTIVE_LOW)
    4472                 :          0 :                 set_bit(FLAG_ACTIVE_LOW, &desc->flags);
    4473                 :            : 
    4474         [ -  + ]:        414 :         if (lflags & GPIO_OPEN_DRAIN)
    4475                 :          0 :                 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
    4476         [ -  + ]:        414 :         else if (dflags & GPIOD_FLAGS_BIT_OPEN_DRAIN) {
    4477                 :            :                 /*
    4478                 :            :                  * This enforces open drain mode from the consumer side.
    4479                 :            :                  * This is necessary for some busses like I2C, but the lookup
    4480                 :            :                  * should *REALLY* have specified them as open drain in the
    4481                 :            :                  * first place, so print a little warning here.
    4482                 :            :                  */
    4483                 :          0 :                 set_bit(FLAG_OPEN_DRAIN, &desc->flags);
    4484         [ #  # ]:          0 :                 gpiod_warn(desc,
    4485                 :            :                            "enforced open drain please flag it properly in DT/ACPI DSDT/board file\n");
    4486                 :            :         }
    4487                 :            : 
    4488         [ -  + ]:        414 :         if (lflags & GPIO_OPEN_SOURCE)
    4489                 :          0 :                 set_bit(FLAG_OPEN_SOURCE, &desc->flags);
    4490                 :            : 
    4491         [ -  + ]:        414 :         if ((lflags & GPIO_PULL_UP) && (lflags & GPIO_PULL_DOWN)) {
    4492         [ #  # ]:          0 :                 gpiod_err(desc,
    4493                 :            :                           "both pull-up and pull-down enabled, invalid configuration\n");
    4494                 :          0 :                 return -EINVAL;
    4495                 :            :         }
    4496                 :            : 
    4497         [ -  + ]:        414 :         if (lflags & GPIO_PULL_UP)
    4498                 :          0 :                 set_bit(FLAG_PULL_UP, &desc->flags);
    4499         [ -  + ]:        414 :         else if (lflags & GPIO_PULL_DOWN)
    4500                 :          0 :                 set_bit(FLAG_PULL_DOWN, &desc->flags);
    4501                 :            : 
    4502                 :        414 :         ret = gpiod_set_transitory(desc, (lflags & GPIO_TRANSITORY));
    4503         [ +  - ]:        414 :         if (ret < 0)
    4504                 :            :                 return ret;
    4505                 :            : 
    4506                 :            :         /* No particular flag request, return here... */
    4507         [ -  + ]:        414 :         if (!(dflags & GPIOD_FLAGS_BIT_DIR_SET)) {
    4508                 :            :                 pr_debug("no flags found for %s\n", con_id);
    4509                 :            :                 return 0;
    4510                 :            :         }
    4511                 :            : 
    4512                 :            :         /* Process flags */
    4513         [ #  # ]:          0 :         if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
    4514                 :          0 :                 ret = gpiod_direction_output(desc,
    4515                 :          0 :                                 !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
    4516                 :            :         else
    4517                 :          0 :                 ret = gpiod_direction_input(desc);
    4518                 :            : 
    4519                 :          0 :         return ret;
    4520                 :            : }
    4521                 :            : 
    4522                 :            : /**
    4523                 :            :  * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
    4524                 :            :  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
    4525                 :            :  * @con_id:     function within the GPIO consumer
    4526                 :            :  * @idx:        index of the GPIO to obtain in the consumer
    4527                 :            :  * @flags:      optional GPIO initialization flags
    4528                 :            :  *
    4529                 :            :  * This variant of gpiod_get() allows to access GPIOs other than the first
    4530                 :            :  * defined one for functions that define several GPIOs.
    4531                 :            :  *
    4532                 :            :  * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
    4533                 :            :  * requested function and/or index, or another IS_ERR() code if an error
    4534                 :            :  * occurred while trying to acquire the GPIO.
    4535                 :            :  */
    4536                 :       1035 : struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
    4537                 :            :                                                const char *con_id,
    4538                 :            :                                                unsigned int idx,
    4539                 :            :                                                enum gpiod_flags flags)
    4540                 :            : {
    4541                 :       1035 :         unsigned long lookupflags = GPIO_LOOKUP_FLAGS_DEFAULT;
    4542                 :            :         struct gpio_desc *desc = NULL;
    4543                 :            :         int ret;
    4544                 :            :         /* Maybe we have a device name, maybe not */
    4545         [ +  - ]:       1035 :         const char *devname = dev ? dev_name(dev) : "?";
    4546                 :            : 
    4547                 :            :         dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id);
    4548                 :            : 
    4549         [ +  - ]:       1035 :         if (dev) {
    4550                 :            :                 /* Using device tree? */
    4551         [ +  + ]:       1035 :                 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
    4552                 :            :                         dev_dbg(dev, "using device tree for GPIO lookup\n");
    4553                 :        828 :                         desc = of_find_gpio(dev, con_id, idx, &lookupflags);
    4554                 :            :                 } else if (ACPI_COMPANION(dev)) {
    4555                 :            :                         dev_dbg(dev, "using ACPI for GPIO lookup\n");
    4556                 :            :                         desc = acpi_find_gpio(dev, con_id, idx, &flags, &lookupflags);
    4557                 :            :                 }
    4558                 :            :         }
    4559                 :            : 
    4560                 :            :         /*
    4561                 :            :          * Either we are not using DT or ACPI, or their lookup did not return
    4562                 :            :          * a result. In that case, use platform lookup as a fallback.
    4563                 :            :          */
    4564   [ +  +  +  - ]:       1035 :         if (!desc || desc == ERR_PTR(-ENOENT)) {
    4565                 :            :                 dev_dbg(dev, "using lookup tables for GPIO lookup\n");
    4566                 :       1035 :                 desc = gpiod_find(dev, con_id, idx, &lookupflags);
    4567                 :            :         }
    4568                 :            : 
    4569         [ -  + ]:       1035 :         if (IS_ERR(desc)) {
    4570                 :            :                 dev_dbg(dev, "No GPIO consumer %s found\n", con_id);
    4571                 :            :                 return desc;
    4572                 :            :         }
    4573                 :            : 
    4574                 :            :         /*
    4575                 :            :          * If a connection label was passed use that, else attempt to use
    4576                 :            :          * the device name as label
    4577                 :            :          */
    4578         [ #  # ]:          0 :         ret = gpiod_request(desc, con_id ? con_id : devname);
    4579         [ #  # ]:          0 :         if (ret < 0) {
    4580   [ #  #  #  # ]:          0 :                 if (ret == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
    4581                 :            :                         /*
    4582                 :            :                          * This happens when there are several consumers for
    4583                 :            :                          * the same GPIO line: we just return here without
    4584                 :            :                          * further initialization. It is a bit if a hack.
    4585                 :            :                          * This is necessary to support fixed regulators.
    4586                 :            :                          *
    4587                 :            :                          * FIXME: Make this more sane and safe.
    4588                 :            :                          */
    4589         [ #  # ]:          0 :                         dev_info(dev, "nonexclusive access to GPIO for %s\n",
    4590                 :            :                                  con_id ? con_id : devname);
    4591                 :          0 :                         return desc;
    4592                 :            :                 } else {
    4593                 :          0 :                         return ERR_PTR(ret);
    4594                 :            :                 }
    4595                 :            :         }
    4596                 :            : 
    4597                 :          0 :         ret = gpiod_configure_flags(desc, con_id, lookupflags, flags);
    4598         [ #  # ]:          0 :         if (ret < 0) {
    4599                 :            :                 dev_dbg(dev, "setup of GPIO %s failed\n", con_id);
    4600                 :            :                 gpiod_put(desc);
    4601                 :          0 :                 return ERR_PTR(ret);
    4602                 :            :         }
    4603                 :            : 
    4604                 :            :         return desc;
    4605                 :            : }
    4606                 :            : EXPORT_SYMBOL_GPL(gpiod_get_index);
    4607                 :            : 
    4608                 :            : /**
    4609                 :            :  * fwnode_get_named_gpiod - obtain a GPIO from firmware node
    4610                 :            :  * @fwnode:     handle of the firmware node
    4611                 :            :  * @propname:   name of the firmware property representing the GPIO
    4612                 :            :  * @index:      index of the GPIO to obtain for the consumer
    4613                 :            :  * @dflags:     GPIO initialization flags
    4614                 :            :  * @label:      label to attach to the requested GPIO
    4615                 :            :  *
    4616                 :            :  * This function can be used for drivers that get their configuration
    4617                 :            :  * from opaque firmware.
    4618                 :            :  *
    4619                 :            :  * The function properly finds the corresponding GPIO using whatever is the
    4620                 :            :  * underlying firmware interface and then makes sure that the GPIO
    4621                 :            :  * descriptor is requested before it is returned to the caller.
    4622                 :            :  *
    4623                 :            :  * Returns:
    4624                 :            :  * On successful request the GPIO pin is configured in accordance with
    4625                 :            :  * provided @dflags.
    4626                 :            :  *
    4627                 :            :  * In case of error an ERR_PTR() is returned.
    4628                 :            :  */
    4629                 :        414 : struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
    4630                 :            :                                          const char *propname, int index,
    4631                 :            :                                          enum gpiod_flags dflags,
    4632                 :            :                                          const char *label)
    4633                 :            : {
    4634                 :            :         unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
    4635                 :            :         struct gpio_desc *desc = ERR_PTR(-ENODEV);
    4636                 :            :         int ret;
    4637                 :            : 
    4638         [ +  - ]:        414 :         if (!fwnode)
    4639                 :            :                 return ERR_PTR(-EINVAL);
    4640                 :            : 
    4641         [ +  - ]:        414 :         if (is_of_node(fwnode)) {
    4642         [ +  - ]:        414 :                 desc = gpiod_get_from_of_node(to_of_node(fwnode),
    4643                 :            :                                               propname, index,
    4644                 :            :                                               dflags,
    4645                 :            :                                               label);
    4646                 :        414 :                 return desc;
    4647                 :            :         } else if (is_acpi_node(fwnode)) {
    4648                 :            :                 struct acpi_gpio_info info;
    4649                 :            : 
    4650                 :            :                 desc = acpi_node_get_gpiod(fwnode, propname, index, &info);
    4651                 :            :                 if (IS_ERR(desc))
    4652                 :            :                         return desc;
    4653                 :            : 
    4654                 :            :                 acpi_gpio_update_gpiod_flags(&dflags, &info);
    4655                 :            :                 acpi_gpio_update_gpiod_lookup_flags(&lflags, &info);
    4656                 :            :         }
    4657                 :            : 
    4658                 :            :         /* Currently only ACPI takes this path */
    4659                 :          0 :         ret = gpiod_request(desc, label);
    4660         [ #  # ]:          0 :         if (ret)
    4661                 :          0 :                 return ERR_PTR(ret);
    4662                 :            : 
    4663                 :          0 :         ret = gpiod_configure_flags(desc, propname, lflags, dflags);
    4664         [ #  # ]:          0 :         if (ret < 0) {
    4665                 :            :                 gpiod_put(desc);
    4666                 :          0 :                 return ERR_PTR(ret);
    4667                 :            :         }
    4668                 :            : 
    4669                 :            :         return desc;
    4670                 :            : }
    4671                 :            : EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
    4672                 :            : 
    4673                 :            : /**
    4674                 :            :  * gpiod_get_index_optional - obtain an optional GPIO from a multi-index GPIO
    4675                 :            :  *                            function
    4676                 :            :  * @dev: GPIO consumer, can be NULL for system-global GPIOs
    4677                 :            :  * @con_id: function within the GPIO consumer
    4678                 :            :  * @index: index of the GPIO to obtain in the consumer
    4679                 :            :  * @flags: optional GPIO initialization flags
    4680                 :            :  *
    4681                 :            :  * This is equivalent to gpiod_get_index(), except that when no GPIO with the
    4682                 :            :  * specified index was assigned to the requested function it will return NULL.
    4683                 :            :  * This is convenient for drivers that need to handle optional GPIOs.
    4684                 :            :  */
    4685                 :          0 : struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
    4686                 :            :                                                         const char *con_id,
    4687                 :            :                                                         unsigned int index,
    4688                 :            :                                                         enum gpiod_flags flags)
    4689                 :            : {
    4690                 :            :         struct gpio_desc *desc;
    4691                 :            : 
    4692                 :        414 :         desc = gpiod_get_index(dev, con_id, index, flags);
    4693   [ #  #  +  - ]:        414 :         if (IS_ERR(desc)) {
    4694   [ #  #  -  + ]:        414 :                 if (PTR_ERR(desc) == -ENOENT)
    4695                 :            :                         return NULL;
    4696                 :            :         }
    4697                 :            : 
    4698                 :          0 :         return desc;
    4699                 :            : }
    4700                 :            : EXPORT_SYMBOL_GPL(gpiod_get_index_optional);
    4701                 :            : 
    4702                 :            : /**
    4703                 :            :  * gpiod_hog - Hog the specified GPIO desc given the provided flags
    4704                 :            :  * @desc:       gpio whose value will be assigned
    4705                 :            :  * @name:       gpio line name
    4706                 :            :  * @lflags:     bitmask of gpio_lookup_flags GPIO_* values - returned from
    4707                 :            :  *              of_find_gpio() or of_get_gpio_hog()
    4708                 :            :  * @dflags:     gpiod_flags - optional GPIO initialization flags
    4709                 :            :  */
    4710                 :          0 : int gpiod_hog(struct gpio_desc *desc, const char *name,
    4711                 :            :               unsigned long lflags, enum gpiod_flags dflags)
    4712                 :            : {
    4713                 :            :         struct gpio_chip *chip;
    4714                 :            :         struct gpio_desc *local_desc;
    4715                 :            :         int hwnum;
    4716                 :            :         int ret;
    4717                 :            : 
    4718                 :            :         chip = gpiod_to_chip(desc);
    4719                 :            :         hwnum = gpio_chip_hwgpio(desc);
    4720                 :            : 
    4721                 :          0 :         local_desc = gpiochip_request_own_desc(chip, hwnum, name,
    4722                 :            :                                                lflags, dflags);
    4723         [ #  # ]:          0 :         if (IS_ERR(local_desc)) {
    4724                 :            :                 ret = PTR_ERR(local_desc);
    4725                 :          0 :                 pr_err("requesting hog GPIO %s (chip %s, offset %d) failed, %d\n",
    4726                 :            :                        name, chip->label, hwnum, ret);
    4727                 :          0 :                 return ret;
    4728                 :            :         }
    4729                 :            : 
    4730                 :            :         /* Mark GPIO as hogged so it can be identified and removed later */
    4731                 :          0 :         set_bit(FLAG_IS_HOGGED, &desc->flags);
    4732                 :            : 
    4733   [ #  #  #  #  :          0 :         pr_info("GPIO line %d (%s) hogged as %s%s\n",
                   #  # ]
    4734                 :            :                 desc_to_gpio(desc), name,
    4735                 :            :                 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ? "output" : "input",
    4736                 :            :                 (dflags&GPIOD_FLAGS_BIT_DIR_OUT) ?
    4737                 :            :                   (dflags&GPIOD_FLAGS_BIT_DIR_VAL) ? "/high" : "/low":"");
    4738                 :            : 
    4739                 :          0 :         return 0;
    4740                 :            : }
    4741                 :            : 
    4742                 :            : /**
    4743                 :            :  * gpiochip_free_hogs - Scan gpio-controller chip and release GPIO hog
    4744                 :            :  * @chip:       gpio chip to act on
    4745                 :            :  */
    4746                 :          0 : static void gpiochip_free_hogs(struct gpio_chip *chip)
    4747                 :            : {
    4748                 :            :         int id;
    4749                 :            : 
    4750         [ #  # ]:          0 :         for (id = 0; id < chip->ngpio; id++) {
    4751         [ #  # ]:          0 :                 if (test_bit(FLAG_IS_HOGGED, &chip->gpiodev->descs[id].flags))
    4752                 :            :                         gpiochip_free_own_desc(&chip->gpiodev->descs[id]);
    4753                 :            :         }
    4754                 :          0 : }
    4755                 :            : 
    4756                 :            : /**
    4757                 :            :  * gpiod_get_array - obtain multiple GPIOs from a multi-index GPIO function
    4758                 :            :  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
    4759                 :            :  * @con_id:     function within the GPIO consumer
    4760                 :            :  * @flags:      optional GPIO initialization flags
    4761                 :            :  *
    4762                 :            :  * This function acquires all the GPIOs defined under a given function.
    4763                 :            :  *
    4764                 :            :  * Return a struct gpio_descs containing an array of descriptors, -ENOENT if
    4765                 :            :  * no GPIO has been assigned to the requested function, or another IS_ERR()
    4766                 :            :  * code if an error occurred while trying to acquire the GPIOs.
    4767                 :            :  */
    4768                 :          0 : struct gpio_descs *__must_check gpiod_get_array(struct device *dev,
    4769                 :            :                                                 const char *con_id,
    4770                 :            :                                                 enum gpiod_flags flags)
    4771                 :            : {
    4772                 :            :         struct gpio_desc *desc;
    4773                 :            :         struct gpio_descs *descs;
    4774                 :            :         struct gpio_array *array_info = NULL;
    4775                 :            :         struct gpio_chip *chip;
    4776                 :            :         int count, bitmap_size;
    4777                 :            : 
    4778                 :          0 :         count = gpiod_count(dev, con_id);
    4779         [ #  # ]:          0 :         if (count < 0)
    4780                 :          0 :                 return ERR_PTR(count);
    4781                 :            : 
    4782                 :          0 :         descs = kzalloc(struct_size(descs, desc, count), GFP_KERNEL);
    4783         [ #  # ]:          0 :         if (!descs)
    4784                 :            :                 return ERR_PTR(-ENOMEM);
    4785                 :            : 
    4786         [ #  # ]:          0 :         for (descs->ndescs = 0; descs->ndescs < count; ) {
    4787                 :          0 :                 desc = gpiod_get_index(dev, con_id, descs->ndescs, flags);
    4788         [ #  # ]:          0 :                 if (IS_ERR(desc)) {
    4789                 :          0 :                         gpiod_put_array(descs);
    4790                 :          0 :                         return ERR_CAST(desc);
    4791                 :            :                 }
    4792                 :            : 
    4793                 :          0 :                 descs->desc[descs->ndescs] = desc;
    4794                 :            : 
    4795                 :            :                 chip = gpiod_to_chip(desc);
    4796                 :            :                 /*
    4797                 :            :                  * If pin hardware number of array member 0 is also 0, select
    4798                 :            :                  * its chip as a candidate for fast bitmap processing path.
    4799                 :            :                  */
    4800   [ #  #  #  # ]:          0 :                 if (descs->ndescs == 0 && gpio_chip_hwgpio(desc) == 0) {
    4801                 :            :                         struct gpio_descs *array;
    4802                 :            : 
    4803         [ #  # ]:          0 :                         bitmap_size = BITS_TO_LONGS(chip->ngpio > count ?
    4804                 :            :                                                     chip->ngpio : count);
    4805                 :            : 
    4806                 :          0 :                         array = kzalloc(struct_size(descs, desc, count) +
    4807                 :          0 :                                         struct_size(array_info, invert_mask,
    4808                 :            :                                         3 * bitmap_size), GFP_KERNEL);
    4809         [ #  # ]:          0 :                         if (!array) {
    4810                 :          0 :                                 gpiod_put_array(descs);
    4811                 :          0 :                                 return ERR_PTR(-ENOMEM);
    4812                 :            :                         }
    4813                 :            : 
    4814                 :          0 :                         memcpy(array, descs,
    4815                 :          0 :                                struct_size(descs, desc, descs->ndescs + 1));
    4816                 :          0 :                         kfree(descs);
    4817                 :            : 
    4818                 :            :                         descs = array;
    4819                 :          0 :                         array_info = (void *)(descs->desc + count);
    4820                 :          0 :                         array_info->get_mask = array_info->invert_mask +
    4821                 :            :                                                   bitmap_size;
    4822                 :          0 :                         array_info->set_mask = array_info->get_mask +
    4823                 :            :                                                   bitmap_size;
    4824                 :            : 
    4825                 :          0 :                         array_info->desc = descs->desc;
    4826                 :          0 :                         array_info->size = count;
    4827                 :          0 :                         array_info->chip = chip;
    4828                 :          0 :                         bitmap_set(array_info->get_mask, descs->ndescs,
    4829                 :          0 :                                    count - descs->ndescs);
    4830                 :          0 :                         bitmap_set(array_info->set_mask, descs->ndescs,
    4831                 :          0 :                                    count - descs->ndescs);
    4832                 :          0 :                         descs->info = array_info;
    4833                 :            :                 }
    4834                 :            :                 /* Unmark array members which don't belong to the 'fast' chip */
    4835   [ #  #  #  # ]:          0 :                 if (array_info && array_info->chip != chip) {
    4836                 :          0 :                         __clear_bit(descs->ndescs, array_info->get_mask);
    4837                 :          0 :                         __clear_bit(descs->ndescs, array_info->set_mask);
    4838                 :            :                 }
    4839                 :            :                 /*
    4840                 :            :                  * Detect array members which belong to the 'fast' chip
    4841                 :            :                  * but their pins are not in hardware order.
    4842                 :            :                  */
    4843   [ #  #  #  # ]:          0 :                 else if (array_info &&
    4844                 :          0 :                            gpio_chip_hwgpio(desc) != descs->ndescs) {
    4845                 :            :                         /*
    4846                 :            :                          * Don't use fast path if all array members processed so
    4847                 :            :                          * far belong to the same chip as this one but its pin
    4848                 :            :                          * hardware number is different from its array index.
    4849                 :            :                          */
    4850         [ #  # ]:          0 :                         if (bitmap_full(array_info->get_mask, descs->ndescs)) {
    4851                 :            :                                 array_info = NULL;
    4852                 :            :                         } else {
    4853                 :          0 :                                 __clear_bit(descs->ndescs,
    4854                 :          0 :                                             array_info->get_mask);
    4855                 :          0 :                                 __clear_bit(descs->ndescs,
    4856                 :          0 :                                             array_info->set_mask);
    4857                 :            :                         }
    4858         [ #  # ]:          0 :                 } else if (array_info) {
    4859                 :            :                         /* Exclude open drain or open source from fast output */
    4860   [ #  #  #  # ]:          0 :                         if (gpiochip_line_is_open_drain(chip, descs->ndescs) ||
    4861                 :            :                             gpiochip_line_is_open_source(chip, descs->ndescs))
    4862                 :          0 :                                 __clear_bit(descs->ndescs,
    4863                 :          0 :                                             array_info->set_mask);
    4864                 :            :                         /* Identify 'fast' pins which require invertion */
    4865         [ #  # ]:          0 :                         if (gpiod_is_active_low(desc))
    4866                 :          0 :                                 __set_bit(descs->ndescs,
    4867                 :          0 :                                           array_info->invert_mask);
    4868                 :            :                 }
    4869                 :            : 
    4870                 :          0 :                 descs->ndescs++;
    4871                 :            :         }
    4872                 :            :         if (array_info)
    4873                 :            :                 dev_dbg(dev,
    4874                 :            :                         "GPIO array info: chip=%s, size=%d, get_mask=%lx, set_mask=%lx, invert_mask=%lx\n",
    4875                 :            :                         array_info->chip->label, array_info->size,
    4876                 :            :                         *array_info->get_mask, *array_info->set_mask,
    4877                 :            :                         *array_info->invert_mask);
    4878                 :          0 :         return descs;
    4879                 :            : }
    4880                 :            : EXPORT_SYMBOL_GPL(gpiod_get_array);
    4881                 :            : 
    4882                 :            : /**
    4883                 :            :  * gpiod_get_array_optional - obtain multiple GPIOs from a multi-index GPIO
    4884                 :            :  *                            function
    4885                 :            :  * @dev:        GPIO consumer, can be NULL for system-global GPIOs
    4886                 :            :  * @con_id:     function within the GPIO consumer
    4887                 :            :  * @flags:      optional GPIO initialization flags
    4888                 :            :  *
    4889                 :            :  * This is equivalent to gpiod_get_array(), except that when no GPIO was
    4890                 :            :  * assigned to the requested function it will return NULL.
    4891                 :            :  */
    4892                 :          0 : struct gpio_descs *__must_check gpiod_get_array_optional(struct device *dev,
    4893                 :            :                                                         const char *con_id,
    4894                 :            :                                                         enum gpiod_flags flags)
    4895                 :            : {
    4896                 :            :         struct gpio_descs *descs;
    4897                 :            : 
    4898                 :          0 :         descs = gpiod_get_array(dev, con_id, flags);
    4899   [ #  #  #  # ]:          0 :         if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
    4900                 :            :                 return NULL;
    4901                 :            : 
    4902                 :          0 :         return descs;
    4903                 :            : }
    4904                 :            : EXPORT_SYMBOL_GPL(gpiod_get_array_optional);
    4905                 :            : 
    4906                 :            : /**
    4907                 :            :  * gpiod_put - dispose of a GPIO descriptor
    4908                 :            :  * @desc:       GPIO descriptor to dispose of
    4909                 :            :  *
    4910                 :            :  * No descriptor can be used after gpiod_put() has been called on it.
    4911                 :            :  */
    4912                 :          0 : void gpiod_put(struct gpio_desc *desc)
    4913                 :            : {
    4914   [ #  #  #  #  :          0 :         if (desc)
                   #  # ]
    4915                 :          0 :                 gpiod_free(desc);
    4916                 :          0 : }
    4917                 :            : EXPORT_SYMBOL_GPL(gpiod_put);
    4918                 :            : 
    4919                 :            : /**
    4920                 :            :  * gpiod_put_array - dispose of multiple GPIO descriptors
    4921                 :            :  * @descs:      struct gpio_descs containing an array of descriptors
    4922                 :            :  */
    4923                 :          0 : void gpiod_put_array(struct gpio_descs *descs)
    4924                 :            : {
    4925                 :            :         unsigned int i;
    4926                 :            : 
    4927         [ #  # ]:          0 :         for (i = 0; i < descs->ndescs; i++)
    4928                 :          0 :                 gpiod_put(descs->desc[i]);
    4929                 :            : 
    4930                 :          0 :         kfree(descs);
    4931                 :          0 : }
    4932                 :            : EXPORT_SYMBOL_GPL(gpiod_put_array);
    4933                 :            : 
    4934                 :        207 : static int __init gpiolib_dev_init(void)
    4935                 :            : {
    4936                 :            :         int ret;
    4937                 :            : 
    4938                 :            :         /* Register GPIO sysfs bus */
    4939                 :        207 :         ret = bus_register(&gpio_bus_type);
    4940         [ -  + ]:        207 :         if (ret < 0) {
    4941                 :          0 :                 pr_err("gpiolib: could not register GPIO bus type\n");
    4942                 :          0 :                 return ret;
    4943                 :            :         }
    4944                 :            : 
    4945                 :        207 :         ret = alloc_chrdev_region(&gpio_devt, 0, GPIO_DEV_MAX, "gpiochip");
    4946         [ -  + ]:        207 :         if (ret < 0) {
    4947                 :          0 :                 pr_err("gpiolib: failed to allocate char dev region\n");
    4948                 :          0 :                 bus_unregister(&gpio_bus_type);
    4949                 :            :         } else {
    4950                 :        207 :                 gpiolib_initialized = true;
    4951                 :        207 :                 gpiochip_setup_devs();
    4952                 :            :         }
    4953                 :        207 :         return ret;
    4954                 :            : }
    4955                 :            : core_initcall(gpiolib_dev_init);
    4956                 :            : 
    4957                 :            : #ifdef CONFIG_DEBUG_FS
    4958                 :            : 
    4959                 :          0 : static void gpiolib_dbg_show(struct seq_file *s, struct gpio_device *gdev)
    4960                 :            : {
    4961                 :            :         unsigned                i;
    4962                 :          0 :         struct gpio_chip        *chip = gdev->chip;
    4963                 :          0 :         unsigned                gpio = gdev->base;
    4964                 :          0 :         struct gpio_desc        *gdesc = &gdev->descs[0];
    4965                 :            :         bool                    is_out;
    4966                 :            :         bool                    is_irq;
    4967                 :            :         bool                    active_low;
    4968                 :            : 
    4969         [ #  # ]:          0 :         for (i = 0; i < gdev->ngpio; i++, gpio++, gdesc++) {
    4970         [ #  # ]:          0 :                 if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
    4971         [ #  # ]:          0 :                         if (gdesc->name) {
    4972                 :          0 :                                 seq_printf(s, " gpio-%-3d (%-20.20s)\n",
    4973                 :            :                                            gpio, gdesc->name);
    4974                 :            :                         }
    4975                 :          0 :                         continue;
    4976                 :            :                 }
    4977                 :            : 
    4978                 :          0 :                 gpiod_get_direction(gdesc);
    4979                 :            :                 is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
    4980                 :            :                 is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
    4981                 :            :                 active_low = test_bit(FLAG_ACTIVE_LOW, &gdesc->flags);
    4982   [ #  #  #  #  :          0 :                 seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s%s",
          #  #  #  #  #  
                      # ]
    4983                 :          0 :                         gpio, gdesc->name ? gdesc->name : "", gdesc->label,
    4984                 :            :                         is_out ? "out" : "in ",
    4985         [ #  # ]:          0 :                         chip->get ? (chip->get(chip, i) ? "hi" : "lo") : "?  ",
    4986                 :            :                         is_irq ? "IRQ " : "",
    4987                 :            :                         active_low ? "ACTIVE LOW" : "");
    4988                 :          0 :                 seq_printf(s, "\n");
    4989                 :            :         }
    4990                 :          0 : }
    4991                 :            : 
    4992                 :          0 : static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos)
    4993                 :            : {
    4994                 :            :         unsigned long flags;
    4995                 :            :         struct gpio_device *gdev = NULL;
    4996                 :          0 :         loff_t index = *pos;
    4997                 :            : 
    4998                 :          0 :         s->private = "";
    4999                 :            : 
    5000                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
    5001         [ #  # ]:          0 :         list_for_each_entry(gdev, &gpio_devices, list)
    5002         [ #  # ]:          0 :                 if (index-- == 0) {
    5003                 :            :                         spin_unlock_irqrestore(&gpio_lock, flags);
    5004                 :          0 :                         return gdev;
    5005                 :            :                 }
    5006                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    5007                 :            : 
    5008                 :          0 :         return NULL;
    5009                 :            : }
    5010                 :            : 
    5011                 :          0 : static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos)
    5012                 :            : {
    5013                 :            :         unsigned long flags;
    5014                 :            :         struct gpio_device *gdev = v;
    5015                 :            :         void *ret = NULL;
    5016                 :            : 
    5017                 :          0 :         spin_lock_irqsave(&gpio_lock, flags);
    5018         [ #  # ]:          0 :         if (list_is_last(&gdev->list, &gpio_devices))
    5019                 :            :                 ret = NULL;
    5020                 :            :         else
    5021                 :          0 :                 ret = list_entry(gdev->list.next, struct gpio_device, list);
    5022                 :            :         spin_unlock_irqrestore(&gpio_lock, flags);
    5023                 :            : 
    5024                 :          0 :         s->private = "\n";
    5025                 :          0 :         ++*pos;
    5026                 :            : 
    5027                 :          0 :         return ret;
    5028                 :            : }
    5029                 :            : 
    5030                 :          0 : static void gpiolib_seq_stop(struct seq_file *s, void *v)
    5031                 :            : {
    5032                 :          0 : }
    5033                 :            : 
    5034                 :          0 : static int gpiolib_seq_show(struct seq_file *s, void *v)
    5035                 :            : {
    5036                 :            :         struct gpio_device *gdev = v;
    5037                 :          0 :         struct gpio_chip *chip = gdev->chip;
    5038                 :            :         struct device *parent;
    5039                 :            : 
    5040         [ #  # ]:          0 :         if (!chip) {
    5041                 :          0 :                 seq_printf(s, "%s%s: (dangling chip)", (char *)s->private,
    5042                 :            :                            dev_name(&gdev->dev));
    5043                 :          0 :                 return 0;
    5044                 :            :         }
    5045                 :            : 
    5046                 :          0 :         seq_printf(s, "%s%s: GPIOs %d-%d", (char *)s->private,
    5047                 :            :                    dev_name(&gdev->dev),
    5048                 :          0 :                    gdev->base, gdev->base + gdev->ngpio - 1);
    5049                 :          0 :         parent = chip->parent;
    5050         [ #  # ]:          0 :         if (parent)
    5051         [ #  # ]:          0 :                 seq_printf(s, ", parent: %s/%s",
    5052                 :          0 :                            parent->bus ? parent->bus->name : "no-bus",
    5053                 :            :                            dev_name(parent));
    5054         [ #  # ]:          0 :         if (chip->label)
    5055                 :          0 :                 seq_printf(s, ", %s", chip->label);
    5056         [ #  # ]:          0 :         if (chip->can_sleep)
    5057                 :          0 :                 seq_printf(s, ", can sleep");
    5058                 :          0 :         seq_printf(s, ":\n");
    5059                 :            : 
    5060         [ #  # ]:          0 :         if (chip->dbg_show)
    5061                 :          0 :                 chip->dbg_show(s, chip);
    5062                 :            :         else
    5063                 :          0 :                 gpiolib_dbg_show(s, gdev);
    5064                 :            : 
    5065                 :            :         return 0;
    5066                 :            : }
    5067                 :            : 
    5068                 :            : static const struct seq_operations gpiolib_seq_ops = {
    5069                 :            :         .start = gpiolib_seq_start,
    5070                 :            :         .next = gpiolib_seq_next,
    5071                 :            :         .stop = gpiolib_seq_stop,
    5072                 :            :         .show = gpiolib_seq_show,
    5073                 :            : };
    5074                 :            : 
    5075                 :          0 : static int gpiolib_open(struct inode *inode, struct file *file)
    5076                 :            : {
    5077                 :          0 :         return seq_open(file, &gpiolib_seq_ops);
    5078                 :            : }
    5079                 :            : 
    5080                 :            : static const struct file_operations gpiolib_operations = {
    5081                 :            :         .owner          = THIS_MODULE,
    5082                 :            :         .open           = gpiolib_open,
    5083                 :            :         .read           = seq_read,
    5084                 :            :         .llseek         = seq_lseek,
    5085                 :            :         .release        = seq_release,
    5086                 :            : };
    5087                 :            : 
    5088                 :        207 : static int __init gpiolib_debugfs_init(void)
    5089                 :            : {
    5090                 :            :         /* /sys/kernel/debug/gpio */
    5091                 :        207 :         debugfs_create_file("gpio", S_IFREG | S_IRUGO, NULL, NULL,
    5092                 :            :                             &gpiolib_operations);
    5093                 :        207 :         return 0;
    5094                 :            : }
    5095                 :            : subsys_initcall(gpiolib_debugfs_init);
    5096                 :            : 
    5097                 :            : #endif  /* DEBUG_FS */

Generated by: LCOV version 1.14